1 //===-- asan_stats.cc -----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Code related to statistics collected by AddressSanitizer.
11 //===----------------------------------------------------------------------===//
12 #include "asan_interceptors.h"
13 #include "asan_internal.h"
14 #include "asan_lock.h"
15 #include "asan_stats.h"
16 #include "asan_thread_registry.h"
17 #include "sanitizer/asan_interface.h"
21 AsanStats::AsanStats() {
22 CHECK(REAL(memset
) != 0);
23 REAL(memset
)(this, 0, sizeof(AsanStats
));
26 static void PrintMallocStatsArray(const char *prefix
,
27 uptr (&array
)[kNumberOfSizeClasses
]) {
29 for (uptr i
= 0; i
< kNumberOfSizeClasses
; i
++) {
30 if (!array
[i
]) continue;
31 Printf("%zu:%zu; ", i
, array
[i
]);
36 void AsanStats::Print() {
37 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
38 malloced
>>20, malloced_redzones
>>20, mallocs
);
39 Printf("Stats: %zuM realloced by %zu calls\n", realloced
>>20, reallocs
);
40 Printf("Stats: %zuM freed by %zu calls\n", freed
>>20, frees
);
41 Printf("Stats: %zuM really freed by %zu calls\n",
42 really_freed
>>20, real_frees
);
43 Printf("Stats: %zuM (%zu full pages) mmaped in %zu calls\n",
44 mmaped
>>20, mmaped
/ GetPageSizeCached(), mmaps
);
46 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size
);
47 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size
);
48 PrintMallocStatsArray(" frees by size class: ", freed_by_size
);
49 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size
);
50 Printf("Stats: malloc large: %zu small slow: %zu\n",
51 malloc_large
, malloc_small_slow
);
54 static AsanLock
print_lock(LINKER_INITIALIZED
);
56 static void PrintAccumulatedStats() {
58 asanThreadRegistry().GetAccumulatedStats(&stats
);
59 // Use lock to keep reports from mixing up.
60 ScopedLock
lock(&print_lock
);
66 // ---------------------- Interface ---------------- {{{1
67 using namespace __asan
; // NOLINT
69 uptr
__asan_get_current_allocated_bytes() {
70 return asanThreadRegistry().GetCurrentAllocatedBytes();
73 uptr
__asan_get_heap_size() {
74 return asanThreadRegistry().GetHeapSize();
77 uptr
__asan_get_free_bytes() {
78 return asanThreadRegistry().GetFreeBytes();
81 uptr
__asan_get_unmapped_bytes() {
85 void __asan_print_accumulated_stats() {
86 PrintAccumulatedStats();