1 | #include "object_cache.h" |
---|
2 | #include <assert.h> |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <string.h> |
---|
6 | |
---|
7 | |
---|
8 | // pthread tls is most likely slower than __thread, but they have destructors so |
---|
9 | // we use a combination of the two here!! |
---|
10 | struct local_cache { |
---|
11 | libtrace_ocache_t *oc; |
---|
12 | size_t total; |
---|
13 | size_t used; |
---|
14 | void **cache; |
---|
15 | bool invalid; |
---|
16 | }; |
---|
17 | |
---|
18 | struct mem_stats { |
---|
19 | struct memfail { |
---|
20 | uint64_t cache_hit; |
---|
21 | uint64_t ring_hit; |
---|
22 | uint64_t miss; |
---|
23 | uint64_t recycled; |
---|
24 | } readbulk, read, write, writebulk; |
---|
25 | }; |
---|
26 | |
---|
27 | extern __thread struct mem_stats mem_hits; |
---|
28 | static __thread size_t t_mem_caches_used = 0; |
---|
29 | static __thread size_t t_mem_caches_total = 0; |
---|
30 | static __thread struct local_cache *t_mem_caches = NULL; |
---|
31 | static pthread_key_t memory_destructor_key; |
---|
32 | static pthread_once_t memory_destructor_once = PTHREAD_ONCE_INIT; |
---|
33 | |
---|
34 | /** |
---|
35 | * @brief unregister_thread assumes we DONT hold spin |
---|
36 | */ |
---|
37 | static inline void unregister_thread(struct local_cache *lc) { |
---|
38 | size_t i; |
---|
39 | if (lc->invalid) |
---|
40 | fprintf(stderr, "Already free'd the thread cache!!\n"); |
---|
41 | pthread_spin_lock(&lc->oc->spin); |
---|
42 | // Remove it from our thread list |
---|
43 | for (i=0; i < lc->oc->nb_thread_list; ++i) { |
---|
44 | if (lc->oc->thread_list[i] == lc) { |
---|
45 | --lc->oc->nb_thread_list; |
---|
46 | lc->oc->thread_list[i] = lc->oc->thread_list[lc->oc->nb_thread_list]; |
---|
47 | lc->oc->thread_list[lc->oc->nb_thread_list] = NULL; |
---|
48 | i = ~0U; |
---|
49 | break; |
---|
50 | } |
---|
51 | } |
---|
52 | if (i != ~0U) { |
---|
53 | fprintf(stderr, "Umm this wasn't registered with us in the first place!!!!IGNORGING!!!!ANGRY\n"); |
---|
54 | pthread_spin_unlock(&lc->oc->spin); |
---|
55 | return; |
---|
56 | } |
---|
57 | lc->invalid = true; |
---|
58 | |
---|
59 | if (lc->oc->max_allocations) { |
---|
60 | libtrace_ringbuffer_swrite_bulk(&lc->oc->rb, lc->cache, lc->used, lc->used); |
---|
61 | } else { |
---|
62 | size_t i; |
---|
63 | // We just run the free these |
---|
64 | for(i = 0; i < lc->used; ++i) { |
---|
65 | lc->oc->free(lc->cache[i]); |
---|
66 | } |
---|
67 | } |
---|
68 | pthread_spin_unlock(&lc->oc->spin); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * @brief register_thread assumes we DONT hold spin |
---|
73 | */ |
---|
74 | static inline void register_thread(libtrace_ocache_t *oc, struct local_cache *lc) { |
---|
75 | lc->invalid = false; |
---|
76 | pthread_spin_lock(&oc->spin); |
---|
77 | if (oc->nb_thread_list == oc->max_nb_thread_list) { |
---|
78 | oc->max_nb_thread_list += 0x10; |
---|
79 | oc->thread_list = realloc(oc->thread_list, sizeof(void*) * oc->max_nb_thread_list); |
---|
80 | } |
---|
81 | oc->thread_list[oc->nb_thread_list] = lc; |
---|
82 | ++oc->nb_thread_list; |
---|
83 | pthread_spin_unlock(&oc->spin); |
---|
84 | } |
---|
85 | |
---|
86 | static void destroy_memory_cache(void *tlsaddr) { |
---|
87 | assert(tlsaddr == t_mem_caches); |
---|
88 | size_t a; |
---|
89 | |
---|
90 | for (a = 0; a < t_mem_caches_used; ++a) { |
---|
91 | unregister_thread(&t_mem_caches[a]); |
---|
92 | // Write these all back to the main buffer, this might have issues we would want to free these |
---|
93 | free(t_mem_caches[a].cache); |
---|
94 | } |
---|
95 | free(t_mem_caches); |
---|
96 | t_mem_caches = NULL; |
---|
97 | } |
---|
98 | |
---|
99 | static void once_memory_cache_key_init() { |
---|
100 | ASSERT_RET(pthread_key_create(&memory_destructor_key, &destroy_memory_cache), == 0); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Adds more space to our mem_caches |
---|
105 | */ |
---|
106 | static void resize_memory_caches() { |
---|
107 | if (t_mem_caches == NULL) { |
---|
108 | pthread_once(&memory_destructor_once, &once_memory_cache_key_init); |
---|
109 | t_mem_caches_total = 0x10; |
---|
110 | t_mem_caches = calloc(0x10, sizeof(struct local_cache)); |
---|
111 | pthread_setspecific(memory_destructor_key, (void *) t_mem_caches); |
---|
112 | } else { |
---|
113 | t_mem_caches += 0x10; |
---|
114 | t_mem_caches = realloc(t_mem_caches, t_mem_caches_total * sizeof(struct local_cache)); |
---|
115 | pthread_setspecific(memory_destructor_key, t_mem_caches); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | static inline struct local_cache * find_cache(libtrace_ocache_t *oc) { |
---|
120 | struct local_cache *lc = NULL; |
---|
121 | size_t i; |
---|
122 | |
---|
123 | for (i = 0; i < t_mem_caches_used; ++i) { |
---|
124 | if (t_mem_caches[i].oc == oc) { |
---|
125 | lc = &t_mem_caches[i]; |
---|
126 | break; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | if (!oc->thread_cache_size) |
---|
131 | return 0; |
---|
132 | |
---|
133 | // Create a cache |
---|
134 | if (!lc) { |
---|
135 | if (t_mem_caches_used == t_mem_caches_total) |
---|
136 | resize_memory_caches(); |
---|
137 | t_mem_caches[t_mem_caches_used].oc = oc; |
---|
138 | t_mem_caches[t_mem_caches_used].used = 0; |
---|
139 | t_mem_caches[t_mem_caches_used].total = oc->thread_cache_size; |
---|
140 | t_mem_caches[t_mem_caches_used].cache = malloc(sizeof(void*) * oc->thread_cache_size); |
---|
141 | t_mem_caches[t_mem_caches_used].invalid = false; |
---|
142 | lc = &t_mem_caches[t_mem_caches_used]; |
---|
143 | // Register it with the underlying ring_buffer |
---|
144 | register_thread(lc->oc, lc); |
---|
145 | ++t_mem_caches_used; |
---|
146 | } |
---|
147 | |
---|
148 | assert(!lc->invalid); |
---|
149 | return lc; |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Creates a object cache, that is a pool of dynamically allocated and recycled |
---|
154 | * objects of a fixed size. This should be faster than malloc and free. |
---|
155 | * The alloc and free methods are supplied by the user and are used when no |
---|
156 | * recycled objects are available, or to tidy the final results. |
---|
157 | * |
---|
158 | * The performance of these pools will decrease if thread caches are used |
---|
159 | * as this results in a list to lookup per thread. The pool is added when |
---|
160 | * to this list when first encountered, these persist untill the thread exits. |
---|
161 | * |
---|
162 | * NOTE: If limit_size is true do not attempt to 'free' any objects that were |
---|
163 | * not created by this pool back otherwise the 'free' might deadlock. Also |
---|
164 | * be cautious when picking the buffer size, upto thread_cache_size*(threads-1) |
---|
165 | * could be unusable at any given time if these are stuck in thread local caches. |
---|
166 | * |
---|
167 | * @param oc A pointer to the object cache structure which is to be initialised. |
---|
168 | * @param alloc The allocation method, must not be NULL. [void *alloc()] |
---|
169 | * @param free The free method used to destroy packets. [void free(void * obj)] |
---|
170 | * @param thread_cache_size A small cache kept on a per thread basis, this can be 0 |
---|
171 | * however should only be done if bulk reads of packets are being performed |
---|
172 | * or contention is minimal. |
---|
173 | * @param buffer_size The number of packets to be stored in the main buffer. |
---|
174 | * @param limit_size If true no more objects than buffer_size will be allocated, |
---|
175 | * reads will block (free never should).Otherwise packets can be freely |
---|
176 | * allocated upon requested and are free'd if there is not enough space for them. |
---|
177 | * @return If successful returns 0 otherwise -1. |
---|
178 | */ |
---|
179 | DLLEXPORT int libtrace_ocache_init(libtrace_ocache_t *oc, void *(*alloc)(void), |
---|
180 | void (*free)(void *), |
---|
181 | size_t thread_cache_size, |
---|
182 | size_t buffer_size, bool limit_size) { |
---|
183 | |
---|
184 | assert(buffer_size); |
---|
185 | assert(alloc); |
---|
186 | assert(free); |
---|
187 | if (libtrace_ringbuffer_init(&oc->rb, buffer_size, LIBTRACE_RINGBUFFER_BLOCKING) != 0) { |
---|
188 | return -1; |
---|
189 | } |
---|
190 | oc->alloc = alloc; |
---|
191 | oc->free = free; |
---|
192 | oc->current_allocations = 0; |
---|
193 | oc->thread_cache_size = thread_cache_size; |
---|
194 | oc->nb_thread_list = 0; |
---|
195 | oc->max_nb_thread_list = 0x10; |
---|
196 | oc->thread_list = calloc(0x10, sizeof(void*)); |
---|
197 | if (oc->thread_list == NULL) { |
---|
198 | libtrace_ringbuffer_destroy(&oc->rb); |
---|
199 | return -1; |
---|
200 | } |
---|
201 | pthread_spin_init(&oc->spin, 0); |
---|
202 | if (limit_size) |
---|
203 | oc->max_allocations = buffer_size; |
---|
204 | else |
---|
205 | oc->max_allocations = 0; |
---|
206 | return 0; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Destroys the object cache. Call this only once all memory has |
---|
211 | * been free'd back and no more accesses will be made. |
---|
212 | * |
---|
213 | * @return Returns the number of packets outstanding, or extra object recevied |
---|
214 | * Ideally this should be zero (0) otherwise some form of memory leak |
---|
215 | * is likely present. Currenty only implemented in the case limit_size |
---|
216 | * is true. |
---|
217 | */ |
---|
218 | DLLEXPORT int libtrace_ocache_destroy(libtrace_ocache_t *oc) { |
---|
219 | void *ele; |
---|
220 | |
---|
221 | while (oc->nb_thread_list) |
---|
222 | unregister_thread(oc->thread_list[0]); |
---|
223 | |
---|
224 | pthread_spin_lock(&oc->spin); |
---|
225 | while (libtrace_ringbuffer_try_read(&oc->rb, &ele)) { |
---|
226 | oc->free(ele); |
---|
227 | if (oc->max_allocations) |
---|
228 | --oc->current_allocations; |
---|
229 | } |
---|
230 | pthread_spin_unlock(&oc->spin); |
---|
231 | |
---|
232 | // Make sure we haven't lost too many packets |
---|
233 | if (oc->current_allocations) |
---|
234 | fprintf(stderr, "!!OCache closing lost, %d packets!!\n", (int) oc->current_allocations); |
---|
235 | else |
---|
236 | /* This is clearly a bug, but I don't know what to replace it with... */ |
---|
237 | fprintf(stderr, "!!OCache closing lost, %d packets!!\n", (int) oc->current_allocations); |
---|
238 | libtrace_ringbuffer_destroy(&oc->rb); |
---|
239 | pthread_spin_destroy(&oc->spin); |
---|
240 | free(oc->thread_list); |
---|
241 | libtrace_zero_ocache(oc); |
---|
242 | if (oc->current_allocations) |
---|
243 | return (int) oc->current_allocations; |
---|
244 | else |
---|
245 | return 0; |
---|
246 | } |
---|
247 | |
---|
248 | static inline size_t libtrace_ocache_alloc_cache(libtrace_ocache_t *oc, void *values[], size_t nb_buffers, size_t min_nb_buffers, |
---|
249 | struct local_cache *lc) { |
---|
250 | libtrace_ringbuffer_t *rb = &oc->rb; |
---|
251 | size_t i; |
---|
252 | |
---|
253 | // We have enough cached!! Yay |
---|
254 | if (nb_buffers <= lc->used) { |
---|
255 | // Copy all from cache |
---|
256 | memcpy(values, &lc->cache[lc->used - nb_buffers], sizeof(void *) * nb_buffers); |
---|
257 | lc->used -= nb_buffers; |
---|
258 | mem_hits.read.cache_hit += nb_buffers; |
---|
259 | mem_hits.readbulk.cache_hit += 1; |
---|
260 | return nb_buffers; |
---|
261 | } |
---|
262 | // Cache is not big enough try read all from ringbuffer |
---|
263 | else if (nb_buffers > lc->total) { |
---|
264 | i = libtrace_ringbuffer_sread_bulk(rb, values, nb_buffers, min_nb_buffers); |
---|
265 | if (i) |
---|
266 | mem_hits.readbulk.ring_hit += 1; |
---|
267 | else |
---|
268 | mem_hits.readbulk.miss += 1; |
---|
269 | mem_hits.read.ring_hit += i; |
---|
270 | } else { // Not enough cached |
---|
271 | // Empty the cache and re-fill it and then see what we're left with |
---|
272 | i = lc->used; |
---|
273 | memcpy(values, lc->cache, sizeof(void *) * lc->used); |
---|
274 | mem_hits.read.cache_hit += i; |
---|
275 | |
---|
276 | // Make sure we still meet the minimum requirement |
---|
277 | if (i < min_nb_buffers) |
---|
278 | lc->used = libtrace_ringbuffer_sread_bulk(rb, lc->cache, lc->total, min_nb_buffers - i); |
---|
279 | else |
---|
280 | lc->used = libtrace_ringbuffer_sread_bulk(rb, lc->cache, lc->total, 0); |
---|
281 | |
---|
282 | if (lc->used == lc->total) |
---|
283 | mem_hits.readbulk.ring_hit += 1; |
---|
284 | else |
---|
285 | mem_hits.readbulk.miss += 1; |
---|
286 | mem_hits.read.ring_hit += lc->used; |
---|
287 | } |
---|
288 | |
---|
289 | // Try fill the remaining |
---|
290 | if (i < nb_buffers && lc->used) { |
---|
291 | size_t remaining; |
---|
292 | remaining = MIN(lc->used, nb_buffers - i); |
---|
293 | memcpy(&values[i], &lc->cache[lc->used - remaining], sizeof(void *) * remaining); |
---|
294 | lc->used -= remaining; |
---|
295 | i += remaining; |
---|
296 | } |
---|
297 | mem_hits.read.miss += nb_buffers - i; |
---|
298 | assert(i >= min_nb_buffers); |
---|
299 | return i; |
---|
300 | } |
---|
301 | |
---|
302 | DLLEXPORT size_t libtrace_ocache_alloc(libtrace_ocache_t *oc, void *values[], size_t nb_buffers, size_t min_nb_buffers) { |
---|
303 | struct local_cache *lc = find_cache(oc); |
---|
304 | size_t i; |
---|
305 | size_t min; |
---|
306 | bool try_alloc = !(oc->max_allocations && oc->max_allocations <= oc->current_allocations); |
---|
307 | |
---|
308 | assert(oc->max_allocations ? nb_buffers < oc->max_allocations : 1); |
---|
309 | min = try_alloc ? 0: min_nb_buffers; |
---|
310 | if (lc) |
---|
311 | i = libtrace_ocache_alloc_cache(oc, values, nb_buffers, min, lc); |
---|
312 | else |
---|
313 | i = libtrace_ringbuffer_sread_bulk(&oc->rb, values, nb_buffers, min); |
---|
314 | |
---|
315 | if (try_alloc) { |
---|
316 | size_t nb; |
---|
317 | |
---|
318 | // Try alloc the rest |
---|
319 | if (oc->max_allocations) { |
---|
320 | pthread_spin_lock(&oc->spin); |
---|
321 | nb = MIN(oc->max_allocations - oc->current_allocations, nb_buffers - i); |
---|
322 | oc->current_allocations += nb; |
---|
323 | pthread_spin_unlock(&oc->spin); |
---|
324 | nb += i; |
---|
325 | } else { |
---|
326 | nb = nb_buffers; |
---|
327 | } |
---|
328 | |
---|
329 | for (;i < nb; ++i) { |
---|
330 | values[i] = (*oc->alloc)(); |
---|
331 | assert(values[i]); |
---|
332 | } |
---|
333 | assert (i == nb); |
---|
334 | // Still got to wait for more |
---|
335 | if (nb < min_nb_buffers) { |
---|
336 | if (lc) |
---|
337 | i += libtrace_ocache_alloc_cache(oc, &values[nb], nb_buffers - nb, min_nb_buffers - nb, lc); |
---|
338 | else |
---|
339 | i += libtrace_ringbuffer_sread_bulk(&oc->rb, &values[nb], nb_buffers - nb, min_nb_buffers - nb); |
---|
340 | } |
---|
341 | } |
---|
342 | assert(i >= min_nb_buffers); |
---|
343 | return i; |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | static inline size_t libtrace_ocache_free_cache(libtrace_ocache_t *oc, void *values[], size_t nb_buffers, size_t min_nb_buffers, |
---|
348 | struct local_cache *lc) { |
---|
349 | libtrace_ringbuffer_t *rb = &oc->rb; |
---|
350 | size_t i; |
---|
351 | |
---|
352 | // We have enough cached!! Yay |
---|
353 | if (nb_buffers <= lc->total - lc->used) { |
---|
354 | // Copy all to the cache |
---|
355 | memcpy(&lc->cache[lc->used], values, sizeof(void *) * nb_buffers); |
---|
356 | lc->used += nb_buffers; |
---|
357 | mem_hits.write.cache_hit += nb_buffers; |
---|
358 | mem_hits.writebulk.cache_hit += 1; |
---|
359 | return nb_buffers; |
---|
360 | } |
---|
361 | // Cache is not big enough try write all to the ringbuffer |
---|
362 | else if (nb_buffers > lc->total) { |
---|
363 | i = libtrace_ringbuffer_swrite_bulk(rb, values, nb_buffers, min_nb_buffers); |
---|
364 | if (i) |
---|
365 | mem_hits.writebulk.ring_hit += 1; |
---|
366 | else |
---|
367 | mem_hits.writebulk.miss += 1; |
---|
368 | mem_hits.write.ring_hit += i; |
---|
369 | } else { // Not enough cache space but there might later |
---|
370 | // Fill the cache and empty it and then see what we're left with |
---|
371 | i = (lc->total - lc->used); |
---|
372 | memcpy(&lc->cache[lc->used], values, sizeof(void *) * i); |
---|
373 | mem_hits.write.cache_hit += i; |
---|
374 | |
---|
375 | // Make sure we still meet the minimum requirement |
---|
376 | if (i < min_nb_buffers) |
---|
377 | lc->used = lc->total - libtrace_ringbuffer_swrite_bulk(rb, lc->cache, lc->total, min_nb_buffers - i); |
---|
378 | else |
---|
379 | lc->used = lc->total - libtrace_ringbuffer_swrite_bulk(rb, lc->cache, lc->total, 0); |
---|
380 | |
---|
381 | // Re originise fulls to the front |
---|
382 | if (lc->used) |
---|
383 | memmove(lc->cache, &lc->cache[lc->total - lc->used], sizeof(void *) * lc->used); |
---|
384 | |
---|
385 | if (lc->used) |
---|
386 | mem_hits.writebulk.miss += 1; |
---|
387 | else |
---|
388 | mem_hits.writebulk.ring_hit += 1; |
---|
389 | mem_hits.write.ring_hit += lc->total - lc->used; |
---|
390 | } |
---|
391 | |
---|
392 | // Try empty the remaining |
---|
393 | if (i < nb_buffers && lc->used != lc->total) { |
---|
394 | size_t remaining; |
---|
395 | remaining = MIN(lc->total - lc->used, nb_buffers - i); |
---|
396 | memcpy(&lc->cache[lc->used], &values[i], sizeof(void *) * remaining); |
---|
397 | lc->used += remaining; |
---|
398 | i += remaining; |
---|
399 | } |
---|
400 | mem_hits.write.miss += nb_buffers - i; |
---|
401 | return i; |
---|
402 | } |
---|
403 | |
---|
404 | DLLEXPORT size_t libtrace_ocache_free(libtrace_ocache_t *oc, void *values[], size_t nb_buffers, size_t min_nb_buffers) { |
---|
405 | struct local_cache *lc = find_cache(oc); |
---|
406 | size_t i; |
---|
407 | size_t min; |
---|
408 | |
---|
409 | assert(oc->max_allocations ? nb_buffers < oc->max_allocations : 1); |
---|
410 | min = oc->max_allocations ? min_nb_buffers : 0; |
---|
411 | if (lc) |
---|
412 | i = libtrace_ocache_free_cache(oc, values, nb_buffers, min, lc); |
---|
413 | else |
---|
414 | i = libtrace_ringbuffer_swrite_bulk(&oc->rb, values, nb_buffers, min); |
---|
415 | |
---|
416 | if (!oc->max_allocations) { |
---|
417 | // Free these normally |
---|
418 | for (;i < min_nb_buffers; ++i) { |
---|
419 | oc->free(values[i]); |
---|
420 | } |
---|
421 | } |
---|
422 | return i; |
---|
423 | } |
---|
424 | |
---|
425 | DLLEXPORT void libtrace_zero_ocache(libtrace_ocache_t *oc) { |
---|
426 | libtrace_zero_ringbuffer(&oc->rb); |
---|
427 | oc->thread_cache_size = 0; |
---|
428 | oc->alloc = NULL; |
---|
429 | oc->free = NULL; |
---|
430 | oc->current_allocations = 0; |
---|
431 | oc->max_allocations = 0; |
---|
432 | oc->nb_thread_list = 0; |
---|
433 | oc->max_nb_thread_list = 0; |
---|
434 | oc->thread_list = NULL; |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * @brief ocache_unregister_thread removes a thread from an ocache. |
---|
439 | * @param The ocache to remove this thread, this will free any packets in the TLS cache |
---|
440 | */ |
---|
441 | DLLEXPORT void libtrace_ocache_unregister_thread(libtrace_ocache_t *oc) { |
---|
442 | size_t i; |
---|
443 | struct local_cache *lc = find_cache(oc); |
---|
444 | |
---|
445 | if (lc) { |
---|
446 | for (i = 0; i < t_mem_caches_used; ++i) { |
---|
447 | if (&t_mem_caches[i] == lc) { |
---|
448 | // Free the cache against the ocache |
---|
449 | unregister_thread(&t_mem_caches[i]); |
---|
450 | free(t_mem_caches[i].cache); |
---|
451 | // And remove it from the thread itself |
---|
452 | --t_mem_caches_used; |
---|
453 | t_mem_caches[i] = t_mem_caches[t_mem_caches_used]; |
---|
454 | memset(&t_mem_caches[t_mem_caches_used], 0, sizeof(struct local_cache)); |
---|
455 | } |
---|
456 | } |
---|
457 | } |
---|
458 | } |
---|