Debian Codegraph @

crt/aws-c-common/source/memtrace.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/atomics.h>
7 #include <aws/common/byte_buf.h>
8 #include <aws/common/clock.h>
9 #include <aws/common/hash_table.h>
10 #include <aws/common/logging.h>
11 #include <aws/common/mutex.h>
12 #include <aws/common/priority_queue.h>
13 #include <aws/common/string.h>
14 #include <aws/common/system_info.h>
15
16 /* describes a single live allocation.
17 * allocated by aws_default_allocator() */
18 struct alloc_info {
19 size_t size;
20 uint64_t time;
21 uint64_t stack; /* hash of stack frame pointers */
22 };
23
24 /* Using a flexible array member is the C99 compliant way to have the frames immediately follow the header.
25 *
26 * MSVC doesn't know this for some reason so we need to use a pragma to make
27 * it happy.
28 */
29 #ifdef _MSC_VER
30 # pragma warning(push)
31 # pragma warning(disable : 4200) /* nonstandard extension used: zero-sized array in struct/union */
32 #endif
33
34 /* one of these is stored per unique stack
35 * allocated by aws_default_allocator() */
36 struct stack_trace {
37 size_t depth; /* length of frames[] */
38 void *const frames[]; /* rest of frames are allocated after */
39 };
40
41 #ifdef _MSC_VER
42 # pragma warning(pop)
43 #endif
44
45 /* Tracking structure, used as the allocator impl.
46 * This structure, and all its bookkeeping data structures, are created with the aws_default_allocator().
47 * This is not customizable because it's too expensive for every little allocation to store
48 * a pointer back to its original allocator. */
49 struct alloc_tracer {
50 struct aws_allocator *traced_allocator; /* underlying allocator */
51 enum aws_mem_trace_level level; /* level to trace at */
52 size_t frames_per_stack; /* how many frames to keep per stack */
53 struct aws_atomic_var allocated; /* bytes currently allocated */
54 struct aws_mutex mutex; /* protects everything below */
55 struct aws_hash_table allocs; /* live allocations, maps address -> alloc_info */
56 struct aws_hash_table stacks; /* unique stack traces, maps hash -> stack_trace */
57 };
58
59 /* number of frames to skip in call stacks (s_alloc_tracer_track, and the vtable function) */
60 enum { FRAMES_TO_SKIP = 2 };
61
62 static void *s_trace_mem_acquire(struct aws_allocator *allocator, size_t size);
63 static void s_trace_mem_release(struct aws_allocator *allocator, void *ptr);
64 static void *s_trace_mem_realloc(struct aws_allocator *allocator, void *old_ptr, size_t old_size, size_t new_size);
65 static void *s_trace_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size);
66
67 static struct aws_allocator s_trace_allocator = {
68 .mem_acquire = s_trace_mem_acquire,
69 .mem_release = s_trace_mem_release,
70 .mem_realloc = s_trace_mem_realloc,
71 .mem_calloc = s_trace_mem_calloc,
72 };
73
74 /* for the hash table, to destroy elements */
75 static void s_destroy_alloc(void *data) {
76 struct alloc_info *alloc = data;
77 aws_mem_release(aws_default_allocator(), alloc);
78 }
79
80 static void s_destroy_stacktrace(void *data) {
81 struct stack_trace *stack = data;
82 aws_mem_release(aws_default_allocator(), stack);
83 }
84
85 static void s_alloc_tracer_init(
86 struct alloc_tracer *tracer,
87 struct aws_allocator *traced_allocator,
88 enum aws_mem_trace_level level,
89 size_t frames_per_stack) {
90
91 void *stack[1];
92 if (!aws_backtrace(stack, 1)) {
93 /* clamp level if tracing isn't available */
94 level = level > AWS_MEMTRACE_BYTES ? AWS_MEMTRACE_BYTES : level;
95 }
96
97 tracer->traced_allocator = traced_allocator;
98 tracer->level = level;
99
100 if (tracer->level >= AWS_MEMTRACE_BYTES) {
101 aws_atomic_init_int(&tracer->allocated, 0);
102 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_mutex_init(&tracer->mutex));
103 AWS_FATAL_ASSERT(
104 AWS_OP_SUCCESS ==
105 aws_hash_table_init(
106 &tracer->allocs, aws_default_allocator(), 1024, aws_hash_ptr, aws_ptr_eq, NULL, s_destroy_alloc));
107 }
108
109 if (tracer->level == AWS_MEMTRACE_STACKS) {
110 if (frames_per_stack > 128) {
111 frames_per_stack = 128;
112 }
113 tracer->frames_per_stack = frames_per_stack ? frames_per_stack : 8;
114 AWS_FATAL_ASSERT(
115 AWS_OP_SUCCESS ==
116 aws_hash_table_init(
117 &tracer->stacks, aws_default_allocator(), 1024, aws_hash_ptr, aws_ptr_eq, NULL, s_destroy_stacktrace));
118 }
119 }
120
121 static void s_alloc_tracer_track(struct alloc_tracer *tracer, void *ptr, size_t size) {
122 if (tracer->level == AWS_MEMTRACE_NONE) {
123 return;
124 }
125
126 aws_atomic_fetch_add(&tracer->allocated, size);
127
128 struct alloc_info *alloc = aws_mem_calloc(aws_default_allocator(), 1, sizeof(struct alloc_info));
129 AWS_FATAL_ASSERT(alloc);
130 alloc->size = size;
131 aws_high_res_clock_get_ticks(&alloc->time);
132
133 if (tracer->level == AWS_MEMTRACE_STACKS) {
134 /* capture stack frames,
135 * skip 2 for this function and the allocation vtable function if we have a full stack trace
136 * and otherwise just capture what ever stack trace we got
137 */
138 AWS_VARIABLE_LENGTH_ARRAY(void *, stack_frames, (FRAMES_TO_SKIP + tracer->frames_per_stack));
139 size_t stack_depth = aws_backtrace(stack_frames, FRAMES_TO_SKIP + tracer->frames_per_stack);
140 AWS_FATAL_ASSERT(stack_depth > 0);
141
142 /* hash the stack pointers */
143 struct aws_byte_cursor stack_cursor = aws_byte_cursor_from_array(stack_frames, stack_depth * sizeof(void *));
144 uint64_t stack_id = aws_hash_byte_cursor_ptr(&stack_cursor);
145 alloc->stack = stack_id; /* associate the stack with the alloc */
146
147 aws_mutex_lock(&tracer->mutex);
148 struct aws_hash_element *item = NULL;
149 int was_created = 0;
150 AWS_FATAL_ASSERT(
151 AWS_OP_SUCCESS == aws_hash_table_create(&tracer->stacks, (void *)(uintptr_t)stack_id, &item, &was_created));
152 /* If this is a new stack, save it to the hash */
153 if (was_created) {
154 struct stack_trace *stack = aws_mem_calloc(
155 aws_default_allocator(), 1, sizeof(struct stack_trace) + (sizeof(void *) * tracer->frames_per_stack));
156 AWS_FATAL_ASSERT(stack);
157 /**
158 * Optimizations can affect the number of frames we get and in pathological cases we can
159 * get fewer than FRAMES_TO_SKIP frames, but always at least 1 because code has to start somewhere.
160 * (looking at you gcc with -O3 on aarch64)
161 * With optimizations on we cannot trust the stack trace too much.
162 * Memtracer makes an assumption that stack trace will be available in all cases if stack trace api
163 * works. So in the pathological case of stack_depth <= FRAMES_TO_SKIP lets record all the frames we
164 * have, to at least have an anchor for where allocation is comming from, however inaccurate it is.
165 */
166 if (stack_depth <= FRAMES_TO_SKIP) {
167 memcpy((void **)&stack->frames[0], &stack_frames[0], (stack_depth) * sizeof(void *));
168 stack->depth = stack_depth;
169 item->value = stack;
170 } else {
171 memcpy(
172 (void **)&stack->frames[0],
173 &stack_frames[FRAMES_TO_SKIP],
174 (stack_depth - FRAMES_TO_SKIP) * sizeof(void *));
175 stack->depth = stack_depth - FRAMES_TO_SKIP;
176 item->value = stack;
177 }
178 }
179
180 aws_mutex_unlock(&tracer->mutex);
181 }
182
183 aws_mutex_lock(&tracer->mutex);
184 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_hash_table_put(&tracer->allocs, ptr, alloc, NULL));
185 aws_mutex_unlock(&tracer->mutex);
186 }
187
188 static void s_alloc_tracer_untrack(struct alloc_tracer *tracer, void *ptr) {
189 if (tracer->level == AWS_MEMTRACE_NONE) {
190 return;
191 }
192
193 aws_mutex_lock(&tracer->mutex);
194 struct aws_hash_element *item;
195 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_hash_table_find(&tracer->allocs, ptr, &item));
196 /* because the tracer can be installed at any time, it is possible for an allocation to not
197 * be tracked. Therefore, we make sure the find succeeds, but then check the returned
198 * value */
199 if (item) {
200 AWS_FATAL_ASSERT(item->key == ptr && item->value);
201 struct alloc_info *alloc = item->value;
202 aws_atomic_fetch_sub(&tracer->allocated, alloc->size);
203 s_destroy_alloc(item->value);
204 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_hash_table_remove_element(&tracer->allocs, item));
205 }
206 aws_mutex_unlock(&tracer->mutex);
207 }
208
209 /* used only to resolve stacks -> trace, count, size at dump time */
210 struct stack_metadata {
211 struct aws_string *trace;
212 size_t count;
213 size_t size;
214 };
215
216 static int s_collect_stack_trace(void *context, struct aws_hash_element *item) {
217 struct alloc_tracer *tracer = context;
218 struct aws_hash_table *all_stacks = &tracer->stacks;
219 struct stack_metadata *stack_info = item->value;
220 struct aws_hash_element *stack_item = NULL;
221 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_hash_table_find(all_stacks, item->key, &stack_item));
222 AWS_FATAL_ASSERT(stack_item);
223 struct stack_trace *stack = stack_item->value;
224 void *const *stack_frames = &stack->frames[0];
225
226 /* convert the frame pointers to symbols, and concat into a buffer */
227 char buf[4096] = {0};
228 struct aws_byte_buf stacktrace = aws_byte_buf_from_empty_array(buf, AWS_ARRAY_SIZE(buf));
229 struct aws_byte_cursor newline = aws_byte_cursor_from_c_str("\n");
230 char **symbols = aws_backtrace_symbols(stack_frames, stack->depth);
231 for (size_t idx = 0; idx < stack->depth; ++idx) {
232 if (idx > 0) {
233 aws_byte_buf_append(&stacktrace, &newline);
234 }
235 const char *caller = symbols[idx];
236 if (!caller || !caller[0]) {
237 break;
238 }
239 struct aws_byte_cursor cursor = aws_byte_cursor_from_c_str(caller);
240 aws_byte_buf_append(&stacktrace, &cursor);
241 }
242 aws_mem_release(aws_default_allocator(), symbols);
243 /* record the resultant buffer as a string */
244 stack_info->trace = aws_string_new_from_array(aws_default_allocator(), stacktrace.buffer, stacktrace.len);
245 AWS_FATAL_ASSERT(stack_info->trace);
246 aws_byte_buf_clean_up(&stacktrace);
247 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
248 }
249
250 static int s_stack_info_compare_size(const void *a, const void *b) {
251 const struct stack_metadata *stack_a = *(const struct stack_metadata **)a;
252 const struct stack_metadata *stack_b = *(const struct stack_metadata **)b;
253 return stack_b->size > stack_a->size;
254 }
255
256 static int s_stack_info_compare_count(const void *a, const void *b) {
257 const struct stack_metadata *stack_a = *(const struct stack_metadata **)a;
258 const struct stack_metadata *stack_b = *(const struct stack_metadata **)b;
259 return stack_b->count > stack_a->count;
260 }
261
262 static void s_stack_info_destroy(void *data) {
263 struct stack_metadata *stack = data;
264 struct aws_allocator *allocator = stack->trace->allocator;
265 aws_string_destroy(stack->trace);
266 aws_mem_release(allocator, stack);
267 }
268
269 /* tally up count/size per stack from all allocs */
270 static int s_collect_stack_stats(void *context, struct aws_hash_element *item) {
271 struct aws_hash_table *stack_info = context;
272 struct alloc_info *alloc = item->value;
273 struct aws_hash_element *stack_item = NULL;
274 int was_created = 0;
275 AWS_FATAL_ASSERT(
276 AWS_OP_SUCCESS ==
277 aws_hash_table_create(stack_info, (void *)(uintptr_t)alloc->stack, &stack_item, &was_created));
278 if (was_created) {
279 stack_item->value = aws_mem_calloc(aws_default_allocator(), 1, sizeof(struct stack_metadata));
280 AWS_FATAL_ASSERT(stack_item->value);
281 }
282 struct stack_metadata *stack = stack_item->value;
283 stack->count++;
284 stack->size += alloc->size;
285 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
286 }
287
288 static int s_insert_stacks(void *context, struct aws_hash_element *item) {
289 struct aws_priority_queue *pq = context;
290 struct stack_metadata *stack = item->value;
291 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_priority_queue_push(pq, &stack));
292 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
293 }
294
295 static int s_insert_allocs(void *context, struct aws_hash_element *item) {
296 struct aws_priority_queue *allocs = context;
297 struct alloc_info *alloc = item->value;
298 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_priority_queue_push(allocs, &alloc));
299 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
300 }
301
302 static int s_alloc_compare(const void *a, const void *b) {
303 const struct alloc_info *alloc_a = *(const struct alloc_info **)a;
304 const struct alloc_info *alloc_b = *(const struct alloc_info **)b;
305 return alloc_a->time > alloc_b->time;
306 }
307
308 void aws_mem_tracer_dump(struct aws_allocator *trace_allocator) {
309 struct alloc_tracer *tracer = trace_allocator->impl;
310 if (tracer->level == AWS_MEMTRACE_NONE || aws_atomic_load_int(&tracer->allocated) == 0) {
311 return;
312 }
313
314 aws_mutex_lock(&tracer->mutex);
315
316 size_t num_allocs = aws_hash_table_get_entry_count(&tracer->allocs);
317 AWS_LOGF_TRACE(
318 AWS_LS_COMMON_MEMTRACE, "################################################################################");
319 AWS_LOGF_TRACE(
320 AWS_LS_COMMON_MEMTRACE, "# BEGIN MEMTRACE DUMP #");
321 AWS_LOGF_TRACE(
322 AWS_LS_COMMON_MEMTRACE, "################################################################################");
323 AWS_LOGF_TRACE(
324 AWS_LS_COMMON_MEMTRACE,
325 "tracer: %zu bytes still allocated in %zu allocations",
326 aws_atomic_load_int(&tracer->allocated),
327 num_allocs);
328
329 /* convert stacks from pointers -> symbols */
330 struct aws_hash_table stack_info;
331 AWS_ZERO_STRUCT(stack_info);
332 if (tracer->level == AWS_MEMTRACE_STACKS) {
333 AWS_FATAL_ASSERT(
334 AWS_OP_SUCCESS ==
335 aws_hash_table_init(
336 &stack_info, aws_default_allocator(), 64, aws_hash_ptr, aws_ptr_eq, NULL, s_stack_info_destroy));
337 /* collect active stacks, tally up sizes and counts */
338 aws_hash_table_foreach(&tracer->allocs, s_collect_stack_stats, &stack_info);
339 /* collect stack traces for active stacks */
340 aws_hash_table_foreach(&stack_info, s_collect_stack_trace, tracer);
341 }
342
343 /* sort allocs by time */
344 struct aws_priority_queue allocs;
345 AWS_FATAL_ASSERT(
346 AWS_OP_SUCCESS ==
347 aws_priority_queue_init_dynamic(
348 &allocs, aws_default_allocator(), num_allocs, sizeof(struct alloc_info *), s_alloc_compare));
349 aws_hash_table_foreach(&tracer->allocs, s_insert_allocs, &allocs);
350 /* dump allocs by time */
351 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
352 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "Leaks in order of allocation:");
353 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
354 while (aws_priority_queue_size(&allocs)) {
355 struct alloc_info *alloc = NULL;
356 aws_priority_queue_pop(&allocs, &alloc);
357 if (alloc->stack) {
358 struct aws_hash_element *item = NULL;
359 AWS_FATAL_ASSERT(
360 AWS_OP_SUCCESS == aws_hash_table_find(&stack_info, (void *)(uintptr_t)alloc->stack, &item));
361 struct stack_metadata *stack = item->value;
362 AWS_LOGF_TRACE(
363 AWS_LS_COMMON_MEMTRACE,
364 "ALLOC %zu bytes, stacktrace:\n%s\n",
365 alloc->size,
366 aws_string_c_str(stack->trace));
367 } else {
368 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "ALLOC %zu bytes", alloc->size);
369 }
370 }
371
372 aws_priority_queue_clean_up(&allocs);
373
374 if (tracer->level == AWS_MEMTRACE_STACKS) {
375 size_t num_stacks = aws_hash_table_get_entry_count(&stack_info);
376 /* sort stacks by total size leaked */
377 struct aws_priority_queue stacks_by_size;
378 AWS_FATAL_ASSERT(
379 AWS_OP_SUCCESS == aws_priority_queue_init_dynamic(
380 &stacks_by_size,
381 aws_default_allocator(),
382 num_stacks,
383 sizeof(struct stack_metadata *),
384 s_stack_info_compare_size));
385 aws_hash_table_foreach(&stack_info, s_insert_stacks, &stacks_by_size);
386 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
387 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "Stacks by bytes leaked:");
388 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
389 while (aws_priority_queue_size(&stacks_by_size) > 0) {
390 struct stack_metadata *stack = NULL;
391 aws_priority_queue_pop(&stacks_by_size, &stack);
392 AWS_LOGF_TRACE(
393 AWS_LS_COMMON_MEMTRACE,
394 "%zu bytes in %zu allocations:\n%s\n",
395 stack->size,
396 stack->count,
397 aws_string_c_str(stack->trace));
398 }
399 aws_priority_queue_clean_up(&stacks_by_size);
400
401 /* sort stacks by number of leaks */
402 struct aws_priority_queue stacks_by_count;
403 AWS_FATAL_ASSERT(
404 AWS_OP_SUCCESS == aws_priority_queue_init_dynamic(
405 &stacks_by_count,
406 aws_default_allocator(),
407 num_stacks,
408 sizeof(struct stack_metadata *),
409 s_stack_info_compare_count));
410 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
411 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "Stacks by number of leaks:");
412 AWS_LOGF_TRACE(AWS_LS_COMMON_MEMTRACE, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
413 aws_hash_table_foreach(&stack_info, s_insert_stacks, &stacks_by_count);
414 while (aws_priority_queue_size(&stacks_by_count) > 0) {
415 struct stack_metadata *stack = NULL;
416 aws_priority_queue_pop(&stacks_by_count, &stack);
417 AWS_LOGF_TRACE(
418 AWS_LS_COMMON_MEMTRACE,
419 "%zu allocations leaking %zu bytes:\n%s\n",
420 stack->count,
421 stack->size,
422 aws_string_c_str(stack->trace));
423 }
424 aws_priority_queue_clean_up(&stacks_by_count);
425 aws_hash_table_clean_up(&stack_info);
426 }
427
428 AWS_LOGF_TRACE(
429 AWS_LS_COMMON_MEMTRACE, "################################################################################");
430 AWS_LOGF_TRACE(
431 AWS_LS_COMMON_MEMTRACE, "# END MEMTRACE DUMP #");
432 AWS_LOGF_TRACE(
433 AWS_LS_COMMON_MEMTRACE, "################################################################################");
434
435 aws_mutex_unlock(&tracer->mutex);
436 }
437
438 static void *s_trace_mem_acquire(struct aws_allocator *allocator, size_t size) {
439 struct alloc_tracer *tracer = allocator->impl;
440 void *ptr = aws_mem_acquire(tracer->traced_allocator, size);
441 if (ptr) {
442 s_alloc_tracer_track(tracer, ptr, size);
443 }
444 return ptr;
445 }
446
447 static void s_trace_mem_release(struct aws_allocator *allocator, void *ptr) {
448 struct alloc_tracer *tracer = allocator->impl;
449 s_alloc_tracer_untrack(tracer, ptr);
450 aws_mem_release(tracer->traced_allocator, ptr);
451 }
452
453 static void *s_trace_mem_realloc(struct aws_allocator *allocator, void *old_ptr, size_t old_size, size_t new_size) {
454 struct alloc_tracer *tracer = allocator->impl;
455 void *new_ptr = old_ptr;
456
457 /*
458 * Careful with the ordering of state clean up here.
459 * Tracer keeps a hash table (alloc ptr as key) of meta info about each allocation.
460 * To avoid race conditions during realloc state update needs to be done in
461 * following order to avoid race conditions:
462 * - remove meta info (other threads cant reuse that key, cause ptr is still valid )
463 * - realloc (cant fail, ptr might remain the same)
464 * - add meta info for reallocated mem
465 */
466 s_alloc_tracer_untrack(tracer, old_ptr);
467 aws_mem_realloc(tracer->traced_allocator, &new_ptr, old_size, new_size);
468 s_alloc_tracer_track(tracer, new_ptr, new_size);
469
470 return new_ptr;
471 }
472
473 static void *s_trace_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
474 struct alloc_tracer *tracer = allocator->impl;
475 void *ptr = aws_mem_calloc(tracer->traced_allocator, num, size);
476 if (ptr) {
477 s_alloc_tracer_track(tracer, ptr, num * size);
478 }
479 return ptr;
480 }
481
482 struct aws_allocator *aws_mem_tracer_new(
483 struct aws_allocator *allocator,
484 struct aws_allocator *deprecated,
485 enum aws_mem_trace_level level,
486 size_t frames_per_stack) {
487
488 /* deprecated customizable bookkeeping allocator */
489 (void)deprecated;
490
491 struct alloc_tracer *tracer = NULL;
492 struct aws_allocator *trace_allocator = NULL;
493 aws_mem_acquire_many(
494 aws_default_allocator(),
495 2,
496 &tracer,
497 sizeof(struct alloc_tracer),
498 &trace_allocator,
499 sizeof(struct aws_allocator));
500
501 AWS_FATAL_ASSERT(trace_allocator);
502 AWS_FATAL_ASSERT(tracer);
503
504 AWS_ZERO_STRUCT(*trace_allocator);
505 AWS_ZERO_STRUCT(*tracer);
506
507 /* copy the template vtable s*/
508 *trace_allocator = s_trace_allocator;
509 trace_allocator->impl = tracer;
510
511 s_alloc_tracer_init(tracer, allocator, level, frames_per_stack);
512 return trace_allocator;
513 }
514
515 struct aws_allocator *aws_mem_tracer_destroy(struct aws_allocator *trace_allocator) {
516 struct alloc_tracer *tracer = trace_allocator->impl;
517 struct aws_allocator *allocator = tracer->traced_allocator;
518
519 if (tracer->level != AWS_MEMTRACE_NONE) {
520 aws_mutex_lock(&tracer->mutex);
521 aws_hash_table_clean_up(&tracer->allocs);
522 aws_hash_table_clean_up(&tracer->stacks);
523 aws_mutex_unlock(&tracer->mutex);
524 aws_mutex_clean_up(&tracer->mutex);
525 }
526
527 aws_mem_release(aws_default_allocator(), tracer);
528 /* trace_allocator is freed as part of the block tracer was allocated in */
529
530 return allocator;
531 }
532
533 size_t aws_mem_tracer_bytes(struct aws_allocator *trace_allocator) {
534 struct alloc_tracer *tracer = trace_allocator->impl;
535 if (tracer->level == AWS_MEMTRACE_NONE) {
536 return 0;
537 }
538
539 return aws_atomic_load_int(&tracer->allocated);
540 }
541
542 size_t aws_mem_tracer_count(struct aws_allocator *trace_allocator) {
543 struct alloc_tracer *tracer = trace_allocator->impl;
544 if (tracer->level == AWS_MEMTRACE_NONE) {
545 return 0;
546 }
547
548 aws_mutex_lock(&tracer->mutex);
549 size_t count = aws_hash_table_get_entry_count(&tracer->allocs);
550 aws_mutex_unlock(&tracer->mutex);
551 return count;
552 }