1 //===-- asan_stats.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Code related to statistics collected by AddressSanitizer.
13 //===----------------------------------------------------------------------===//
14 #include "asan_interceptors.h"
15 #include "asan_internal.h"
16 #include "asan_stats.h"
17 #include "asan_thread.h"
18 #include "sanitizer_common/sanitizer_allocator_interface.h"
19 #include "sanitizer_common/sanitizer_mutex.h"
20 #include "sanitizer_common/sanitizer_stackdepot.h"
24 AsanStats::AsanStats() {
28 void AsanStats::Clear() {
30 REAL(memset)(this, 0, sizeof(AsanStats));
33 static void PrintMallocStatsArray(const char *prefix,
34 uptr (&array)[kNumberOfSizeClasses]) {
36 for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
37 if (!array[i]) continue;
38 Printf("%zu:%zu; ", i, array[i]);
43 void AsanStats::Print() {
44 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
45 malloced>>20, malloced_redzones>>20, mallocs);
46 Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs);
47 Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees);
48 Printf("Stats: %zuM really freed by %zu calls\n",
49 really_freed>>20, real_frees);
50 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
51 (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20,
54 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size);
55 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
56 PrintMallocStatsArray(" frees by size class: ", freed_by_size);
57 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size);
58 Printf("Stats: malloc large: %zu small slow: %zu\n",
59 malloc_large, malloc_small_slow);
62 void AsanStats::MergeFrom(const AsanStats *stats) {
63 uptr *dst_ptr = reinterpret_cast<uptr*>(this);
64 const uptr *src_ptr = reinterpret_cast<const uptr*>(stats);
65 uptr num_fields = sizeof(*this) / sizeof(uptr);
66 for (uptr i = 0; i < num_fields; i++)
67 dst_ptr[i] += src_ptr[i];
70 static BlockingMutex print_lock(LINKER_INITIALIZED);
72 static AsanStats unknown_thread_stats(LINKER_INITIALIZED);
73 static AsanStats dead_threads_stats(LINKER_INITIALIZED);
74 static BlockingMutex dead_threads_stats_lock(LINKER_INITIALIZED);
75 // Required for malloc_zone_statistics() on OS X. This can't be stored in
76 // per-thread AsanStats.
77 static uptr max_malloced_memory;
79 static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
80 AsanStats *accumulated_stats = reinterpret_cast<AsanStats*>(arg);
81 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
82 if (AsanThread *t = tctx->thread)
83 accumulated_stats->MergeFrom(&t->stats());
86 static void GetAccumulatedStats(AsanStats *stats) {
89 ThreadRegistryLock l(&asanThreadRegistry());
91 .RunCallbackForEachThreadLocked(MergeThreadStats, stats);
93 stats->MergeFrom(&unknown_thread_stats);
95 BlockingMutexLock lock(&dead_threads_stats_lock);
96 stats->MergeFrom(&dead_threads_stats);
98 // This is not very accurate: we may miss allocation peaks that happen
99 // between two updates of accumulated_stats_. For more accurate bookkeeping
100 // the maximum should be updated on every malloc(), which is unacceptable.
101 if (max_malloced_memory < stats->malloced) {
102 max_malloced_memory = stats->malloced;
106 void FlushToDeadThreadStats(AsanStats *stats) {
107 BlockingMutexLock lock(&dead_threads_stats_lock);
108 dead_threads_stats.MergeFrom(stats);
112 void FillMallocStatistics(AsanMallocStats *malloc_stats) {
114 GetAccumulatedStats(&stats);
115 malloc_stats->blocks_in_use = stats.mallocs;
116 malloc_stats->size_in_use = stats.malloced;
117 malloc_stats->max_size_in_use = max_malloced_memory;
118 malloc_stats->size_allocated = stats.mmaped;
121 AsanStats &GetCurrentThreadStats() {
122 AsanThread *t = GetCurrentThread();
123 return (t) ? t->stats() : unknown_thread_stats;
126 static void PrintAccumulatedStats() {
128 GetAccumulatedStats(&stats);
129 // Use lock to keep reports from mixing up.
130 BlockingMutexLock lock(&print_lock);
132 StackDepotStats *stack_depot_stats = StackDepotGetStats();
133 Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
134 stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20);
135 PrintInternalAllocatorStats();
138 } // namespace __asan
140 // ---------------------- Interface ---------------- {{{1
141 using namespace __asan; // NOLINT
143 uptr __sanitizer_get_current_allocated_bytes() {
145 GetAccumulatedStats(&stats);
146 uptr malloced = stats.malloced;
147 uptr freed = stats.freed;
148 // Return sane value if malloced < freed due to racy
149 // way we update accumulated stats.
150 return (malloced > freed) ? malloced - freed : 1;
153 uptr __sanitizer_get_heap_size() {
155 GetAccumulatedStats(&stats);
156 return stats.mmaped - stats.munmaped;
159 uptr __sanitizer_get_free_bytes() {
161 GetAccumulatedStats(&stats);
162 uptr total_free = stats.mmaped
165 + stats.really_freed_redzones;
166 uptr total_used = stats.malloced
167 + stats.malloced_redzones;
168 // Return sane value if total_free < total_used due to racy
169 // way we update accumulated stats.
170 return (total_free > total_used) ? total_free - total_used : 1;
173 uptr __sanitizer_get_unmapped_bytes() {
177 void __asan_print_accumulated_stats() {
178 PrintAccumulatedStats();