Debian Codegraph @

src/asahi/lib/agx_bo.c

raw
1 /*
2 * Copyright 2021 Alyssa Rosenzweig
3 * Copyright 2019 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "agx_bo.h"
8 #include <inttypes.h>
9 #include <stdlib.h>
10 #include "util/hash_table.h"
11 #include "util/ralloc.h"
12 #include "agx_device.h"
13 #include "decode.h"
14 #include "layout.h"
15
16 /* Helper to calculate the bucket index of a BO */
17 static unsigned
18 agx_bucket_index(size_t size)
19 {
20 /* Round down to POT to compute a bucket index */
21 unsigned bucket_index = util_logbase2(size);
22
23 /* Clamp to supported buckets. Huge allocations use the largest bucket */
24 bucket_index = CLAMP(bucket_index, MIN_BO_CACHE_BUCKET, MAX_BO_CACHE_BUCKET);
25
26 /* Reindex from 0 */
27 return (bucket_index - MIN_BO_CACHE_BUCKET);
28 }
29
30 static struct list_head *
31 agx_bucket(struct agx_device *dev, size_t size)
32 {
33 return &dev->bo_cache.buckets[agx_bucket_index(size)];
34 }
35
36 static void
37 agx_bo_cache_remove_locked(struct agx_device *dev, struct agx_bo *bo)
38 {
39 simple_mtx_assert_locked(&dev->bo_cache.lock);
40 list_del(&bo->bucket_link);
41 list_del(&bo->lru_link);
42 dev->bo_cache.size -= bo->size;
43 }
44
45 /* Tries to fetch a BO of sufficient size with the appropriate flags from the
46 * BO cache. If it succeeds, it returns that BO and removes the BO from the
47 * cache. If it fails, it returns NULL signaling the caller to allocate a new
48 * BO. */
49
50 struct agx_bo *
51 agx_bo_cache_fetch(struct agx_device *dev, size_t size, size_t align,
52 uint32_t flags, const bool dontwait)
53 {
54 simple_mtx_lock(&dev->bo_cache.lock);
55 struct list_head *bucket = agx_bucket(dev, size);
56 struct agx_bo *bo = NULL;
57
58 /* Iterate the bucket looking for something suitable */
59 list_for_each_entry_safe(struct agx_bo, entry, bucket, bucket_link) {
60 if (entry->size < size || entry->flags != flags)
61 continue;
62
63 /* Do not return more than 2x oversized BOs. */
64 if (entry->size > 2 * size)
65 continue;
66
67 if (align > entry->align)
68 continue;
69
70 /* This one works, use it */
71 agx_bo_cache_remove_locked(dev, entry);
72 bo = entry;
73 break;
74 }
75 simple_mtx_unlock(&dev->bo_cache.lock);
76
77 return bo;
78 }
79
80 static void
81 agx_bo_cache_evict_stale_bos(struct agx_device *dev, unsigned tv_sec)
82 {
83 struct timespec time;
84
85 clock_gettime(CLOCK_MONOTONIC, &time);
86 list_for_each_entry_safe(struct agx_bo, entry, &dev->bo_cache.lru,
87 lru_link) {
88 /* We want all entries that have been used more than 1 sec ago to be
89 * dropped, others can be kept. Note the <= 2 check and not <= 1. It's
90 * here to account for the fact that we're only testing ->tv_sec, not
91 * ->tv_nsec. That means we might keep entries that are between 1 and 2
92 * seconds old, but we don't really care, as long as unused BOs are
93 * dropped at some point.
94 */
95 if (time.tv_sec - entry->last_used <= 2)
96 break;
97
98 agx_bo_cache_remove_locked(dev, entry);
99 agx_bo_free(dev, entry);
100 }
101 }
102
103 static void
104 agx_bo_cache_put_locked(struct agx_device *dev, struct agx_bo *bo)
105 {
106 struct list_head *bucket = agx_bucket(dev, bo->size);
107 struct timespec time;
108
109 /* Add us to the bucket */
110 list_addtail(&bo->bucket_link, bucket);
111
112 /* Add us to the LRU list and update the last_used field. */
113 list_addtail(&bo->lru_link, &dev->bo_cache.lru);
114 clock_gettime(CLOCK_MONOTONIC, &time);
115 bo->last_used = time.tv_sec;
116
117 /* Update statistics */
118 dev->bo_cache.size += bo->size;
119
120 if (0) {
121 printf("BO cache: %zu KiB (+%zu KiB from %s, hit/miss %" PRIu64
122 "/%" PRIu64 ")\n",
123 DIV_ROUND_UP(dev->bo_cache.size, 1024),
124 DIV_ROUND_UP(bo->size, 1024), bo->label,
125 p_atomic_read(&dev->bo_cache.hits),
126 p_atomic_read(&dev->bo_cache.misses));
127 }
128
129 /* Update label for debug */
130 bo->label = "Unused (BO cache)";
131
132 /* Let's do some cleanup in the BO cache while we hold the lock. */
133 agx_bo_cache_evict_stale_bos(dev, time.tv_sec);
134 }
135
136 /* Tries to add a BO to the cache. Returns if it was successful */
137 static bool
138 agx_bo_cache_put(struct agx_device *dev, struct agx_bo *bo)
139 {
140 if (bo->flags & AGX_BO_SHARED) {
141 return false;
142 } else {
143 simple_mtx_lock(&dev->bo_cache.lock);
144 agx_bo_cache_put_locked(dev, bo);
145 simple_mtx_unlock(&dev->bo_cache.lock);
146
147 return true;
148 }
149 }
150
151 void
152 agx_bo_cache_evict_all(struct agx_device *dev)
153 {
154 simple_mtx_lock(&dev->bo_cache.lock);
155 for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i) {
156 struct list_head *bucket = &dev->bo_cache.buckets[i];
157
158 list_for_each_entry_safe(struct agx_bo, entry, bucket, bucket_link) {
159 agx_bo_cache_remove_locked(dev, entry);
160 agx_bo_free(dev, entry);
161 }
162 }
163 simple_mtx_unlock(&dev->bo_cache.lock);
164 }
165
166 void
167 agx_bo_reference(struct agx_bo *bo)
168 {
169 if (bo) {
170 ASSERTED int count = p_atomic_inc_return(&bo->refcnt);
171 assert(count != 1);
172 }
173 }
174
175 struct label_stat {
176 const char *label;
177 uint32_t count;
178 size_t alloc_B;
179 size_t mapped_B;
180 };
181
182 static void
183 account_bo(struct label_stat *stat, struct agx_bo *bo)
184 {
185 stat->count++;
186 stat->alloc_B += bo->size;
187
188 if (bo->_map != NULL)
189 stat->mapped_B += bo->size;
190 }
191
192 static void
193 print_size(FILE *fp, size_t size_B)
194 {
195 if (size_B >= (1024 * 1024 * 1024)) {
196 fprintf(fp, "%.1f GiB", (double)size_B / (double)(1024 * 1024 * 1024));
197 } else if (size_B >= (1024 * 1024)) {
198 fprintf(fp, "%.1f MiB", (double)size_B / (double)(1024 * 1024));
199 } else if (size_B >= 1024) {
200 fprintf(fp, "%zu KiB", DIV_ROUND_UP(size_B, 1024));
201 } else {
202 fprintf(fp, "%zu B", size_B);
203 }
204 }
205
206 static void
207 print_stat(FILE *fp, struct label_stat *stat)
208 {
209 const char *BOLD = "\e[1m";
210 const char *RESET = "\e[0m";
211
212 fprintf(fp, "%s%s%s: ", BOLD, stat->label, RESET);
213 print_size(fp, stat->alloc_B);
214
215 if (stat->mapped_B) {
216 fprintf(fp, ", mapped ");
217 print_size(fp, stat->mapped_B);
218 }
219
220 fprintf(fp, ", %u BOs\n", stat->count);
221 }
222
223 static int
224 compare_size(const void *a_, const void *b_)
225 {
226 const struct label_stat *const *label_a = a_;
227 const struct label_stat *const *label_b = b_;
228
229 size_t a = (*label_a)->alloc_B;
230 size_t b = (*label_b)->alloc_B;
231
232 return (a > b) ? 1 : (a < b) ? -1 : 0;
233 }
234
235 static void
236 agx_bo_dump_all(struct agx_device *dev)
237 {
238 struct label_stat accum = {.label = "Total"};
239 struct hash_table *totals = _mesa_string_hash_table_create(NULL);
240 bool verbose = dev->debug & AGX_DBG_BODUMPVERBOSE;
241
242 if (verbose)
243 fprintf(stderr, "---\n");
244
245 for (uint32_t handle = 0; handle < dev->max_handle; handle++) {
246 struct agx_bo *bo = agx_lookup_bo(dev, handle);
247 if (!bo->size)
248 continue;
249
250 if (verbose) {
251 fprintf(stderr, "%u: %s %zu KiB\n", handle, bo->label,
252 bo->size / 1024);
253 }
254
255 account_bo(&accum, bo);
256
257 assert(bo->label != NULL && "Everything must be labeled");
258
259 struct hash_entry *ent = _mesa_hash_table_search(totals, bo->label);
260 struct label_stat *ls;
261 if (ent != NULL) {
262 ls = ent->data;
263 } else {
264 ls = rzalloc(totals, struct label_stat);
265 ls->label = bo->label;
266 _mesa_hash_table_insert(totals, bo->label, ls);
267 }
268
269 account_bo(ls, bo);
270 }
271
272 if (verbose) {
273 fprintf(stderr, "\n");
274 }
275
276 unsigned nr_labels = _mesa_hash_table_num_entries(totals);
277 struct label_stat **stats =
278 rzalloc_array(totals, struct label_stat *, nr_labels);
279 unsigned i = 0;
280
281 hash_table_foreach(totals, ent) {
282 assert(i < nr_labels);
283 stats[i++] = ent->data;
284 }
285
286 assert(i == nr_labels);
287
288 /* Sort labels in ascending order of size */
289 qsort(stats, nr_labels, sizeof(struct label_stat *), compare_size);
290
291 for (i = 0; i < nr_labels; ++i) {
292 print_stat(stderr, stats[i]);
293 }
294
295 print_stat(stderr, &accum);
296
297 if (verbose)
298 fprintf(stderr, "---\n\n");
299 else
300 fprintf(stderr, "\n");
301
302 ralloc_free(totals);
303 }
304
305 static void
306 agx_bo_dump_all_periodic(struct agx_device *dev)
307 {
308 if (likely(!(dev->debug & (AGX_DBG_BODUMP | AGX_DBG_BODUMPVERBOSE))))
309 return;
310
311 static time_t agx_last_dumped_time = 0;
312
313 time_t now = time(NULL);
314 if (now == agx_last_dumped_time)
315 return;
316
317 agx_bo_dump_all(dev);
318 agx_last_dumped_time = now;
319 }
320
321 void
322 agx_bo_unreference(struct agx_device *dev, struct agx_bo *bo)
323 {
324 if (!bo)
325 return;
326
327 int refcnt = p_atomic_dec_return(&bo->refcnt);
328 assert(refcnt >= 0);
329
330 /* Don't return to cache if there are still references */
331 if (refcnt)
332 return;
333
334 pthread_mutex_lock(&dev->bo_map_lock);
335
336 /* Someone might have imported this BO while we were waiting for the
337 * lock, let's make sure it's still not referenced before freeing it.
338 */
339 if (p_atomic_read(&bo->refcnt) == 0) {
340 assert(!p_atomic_read_relaxed(&bo->writer));
341
342 if (dev->debug & AGX_DBG_TRACE)
343 agxdecode_track_free(dev->agxdecode, bo);
344
345 if (!agx_bo_cache_put(dev, bo))
346 agx_bo_free(dev, bo);
347 }
348
349 agx_bo_dump_all_periodic(dev);
350
351 pthread_mutex_unlock(&dev->bo_map_lock);
352 }
353
354 struct agx_bo *
355 agx_bo_create(struct agx_device *dev, size_t size, unsigned align,
356 enum agx_bo_flags flags, const char *label)
357 {
358 struct agx_bo *bo;
359 assert(size > 0);
360
361 /* BOs are allocated in pages */
362 size = ALIGN_POT(size, AIL_PAGESIZE);
363 align = MAX2(align, AIL_PAGESIZE);
364
365 /* See if we have a BO already in the cache */
366 bo = agx_bo_cache_fetch(dev, size, align, flags, true);
367
368 /* Update stats based on the first attempt to fetch */
369 if (bo != NULL)
370 p_atomic_inc(&dev->bo_cache.hits);
371 else
372 p_atomic_inc(&dev->bo_cache.misses);
373
374 /* Otherwise, allocate a fresh BO. If allocation fails, we can try waiting
375 * for something in the cache. But if there's no nothing suitable, we should
376 * flush the cache to make space for the new allocation.
377 */
378 if (!bo)
379 bo = dev->ops.bo_alloc(dev, size, align, flags);
380 if (!bo)
381 bo = agx_bo_cache_fetch(dev, size, align, flags, false);
382 if (!bo) {
383 agx_bo_cache_evict_all(dev);
384 bo = dev->ops.bo_alloc(dev, size, align, flags);
385 }
386
387 if (!bo) {
388 fprintf(stderr, "BO creation failed\n");
389 return NULL;
390 }
391
392 bo->label = label;
393 p_atomic_set(&bo->refcnt, 1);
394
395 if (dev->debug & AGX_DBG_TRACE) {
396 agx_bo_map(bo);
397 agxdecode_track_alloc(dev->agxdecode, bo);
398 }
399
400 agx_bo_dump_all_periodic(dev);
401 return bo;
402 }