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_stats.h"
15 #include "asan_thread.h"
16 #include "sanitizer_common/sanitizer_mutex.h"
17 #include "sanitizer_common/sanitizer_stackdepot.h"
21 AsanStats::AsanStats() {
25 void AsanStats::Clear() {
27 REAL(memset
)(this, 0, sizeof(AsanStats
));
30 static void PrintMallocStatsArray(const char *prefix
,
31 uptr (&array
)[kNumberOfSizeClasses
]) {
33 for (uptr i
= 0; i
< kNumberOfSizeClasses
; i
++) {
34 if (!array
[i
]) continue;
35 Printf("%zu:%zu; ", i
, array
[i
]);
40 void AsanStats::Print() {
41 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
42 malloced
>>20, malloced_redzones
>>20, mallocs
);
43 Printf("Stats: %zuM realloced by %zu calls\n", realloced
>>20, reallocs
);
44 Printf("Stats: %zuM freed by %zu calls\n", freed
>>20, frees
);
45 Printf("Stats: %zuM really freed by %zu calls\n",
46 really_freed
>>20, real_frees
);
47 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
48 (mmaped
-munmaped
)>>20, mmaped
>>20, munmaped
>>20,
51 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size
);
52 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size
);
53 PrintMallocStatsArray(" frees by size class: ", freed_by_size
);
54 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size
);
55 Printf("Stats: malloc large: %zu small slow: %zu\n",
56 malloc_large
, malloc_small_slow
);
59 void AsanStats::MergeFrom(const AsanStats
*stats
) {
60 uptr
*dst_ptr
= reinterpret_cast<uptr
*>(this);
61 const uptr
*src_ptr
= reinterpret_cast<const uptr
*>(stats
);
62 uptr num_fields
= sizeof(*this) / sizeof(uptr
);
63 for (uptr i
= 0; i
< num_fields
; i
++)
64 dst_ptr
[i
] += src_ptr
[i
];
67 static BlockingMutex
print_lock(LINKER_INITIALIZED
);
69 static AsanStats
unknown_thread_stats(LINKER_INITIALIZED
);
70 static AsanStats
dead_threads_stats(LINKER_INITIALIZED
);
71 static BlockingMutex
dead_threads_stats_lock(LINKER_INITIALIZED
);
72 // Required for malloc_zone_statistics() on OS X. This can't be stored in
73 // per-thread AsanStats.
74 static uptr max_malloced_memory
;
76 static void MergeThreadStats(ThreadContextBase
*tctx_base
, void *arg
) {
77 AsanStats
*accumulated_stats
= reinterpret_cast<AsanStats
*>(arg
);
78 AsanThreadContext
*tctx
= static_cast<AsanThreadContext
*>(tctx_base
);
79 if (AsanThread
*t
= tctx
->thread
)
80 accumulated_stats
->MergeFrom(&t
->stats());
83 static void GetAccumulatedStats(AsanStats
*stats
) {
86 ThreadRegistryLock
l(&asanThreadRegistry());
88 .RunCallbackForEachThreadLocked(MergeThreadStats
, stats
);
90 stats
->MergeFrom(&unknown_thread_stats
);
92 BlockingMutexLock
lock(&dead_threads_stats_lock
);
93 stats
->MergeFrom(&dead_threads_stats
);
95 // This is not very accurate: we may miss allocation peaks that happen
96 // between two updates of accumulated_stats_. For more accurate bookkeeping
97 // the maximum should be updated on every malloc(), which is unacceptable.
98 if (max_malloced_memory
< stats
->malloced
) {
99 max_malloced_memory
= stats
->malloced
;
103 void FlushToDeadThreadStats(AsanStats
*stats
) {
104 BlockingMutexLock
lock(&dead_threads_stats_lock
);
105 dead_threads_stats
.MergeFrom(stats
);
109 void FillMallocStatistics(AsanMallocStats
*malloc_stats
) {
111 GetAccumulatedStats(&stats
);
112 malloc_stats
->blocks_in_use
= stats
.mallocs
;
113 malloc_stats
->size_in_use
= stats
.malloced
;
114 malloc_stats
->max_size_in_use
= max_malloced_memory
;
115 malloc_stats
->size_allocated
= stats
.mmaped
;
118 AsanStats
&GetCurrentThreadStats() {
119 AsanThread
*t
= GetCurrentThread();
120 return (t
) ? t
->stats() : unknown_thread_stats
;
123 static void PrintAccumulatedStats() {
125 GetAccumulatedStats(&stats
);
126 // Use lock to keep reports from mixing up.
127 BlockingMutexLock
lock(&print_lock
);
129 StackDepotStats
*stack_depot_stats
= StackDepotGetStats();
130 Printf("Stats: StackDepot: %zd ids; %zdM mapped\n",
131 stack_depot_stats
->n_uniq_ids
, stack_depot_stats
->mapped
>> 20);
132 PrintInternalAllocatorStats();
135 } // namespace __asan
137 // ---------------------- Interface ---------------- {{{1
138 using namespace __asan
; // NOLINT
140 uptr
__asan_get_current_allocated_bytes() {
142 GetAccumulatedStats(&stats
);
143 uptr malloced
= stats
.malloced
;
144 uptr freed
= stats
.freed
;
145 // Return sane value if malloced < freed due to racy
146 // way we update accumulated stats.
147 return (malloced
> freed
) ? malloced
- freed
: 1;
150 uptr
__asan_get_heap_size() {
152 GetAccumulatedStats(&stats
);
153 return stats
.mmaped
- stats
.munmaped
;
156 uptr
__asan_get_free_bytes() {
158 GetAccumulatedStats(&stats
);
159 uptr total_free
= stats
.mmaped
162 + stats
.really_freed_redzones
;
163 uptr total_used
= stats
.malloced
164 + stats
.malloced_redzones
;
165 // Return sane value if total_free < total_used due to racy
166 // way we update accumulated stats.
167 return (total_free
> total_used
) ? total_free
- total_used
: 1;
170 uptr
__asan_get_unmapped_bytes() {
174 void __asan_print_accumulated_stats() {
175 PrintAccumulatedStats();