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_allocator_interface.h"
17 #include "sanitizer_common/sanitizer_mutex.h"
18 #include "sanitizer_common/sanitizer_stackdepot.h"
22 AsanStats::AsanStats() {
26 void AsanStats::Clear() {
28 REAL(memset
)(this, 0, sizeof(AsanStats
));
31 static void PrintMallocStatsArray(const char *prefix
,
32 uptr (&array
)[kNumberOfSizeClasses
]) {
34 for (uptr i
= 0; i
< kNumberOfSizeClasses
; i
++) {
35 if (!array
[i
]) continue;
36 Printf("%zu:%zu; ", i
, array
[i
]);
41 void AsanStats::Print() {
42 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
43 malloced
>>20, malloced_redzones
>>20, mallocs
);
44 Printf("Stats: %zuM realloced by %zu calls\n", realloced
>>20, reallocs
);
45 Printf("Stats: %zuM freed by %zu calls\n", freed
>>20, frees
);
46 Printf("Stats: %zuM really freed by %zu calls\n",
47 really_freed
>>20, real_frees
);
48 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
49 (mmaped
-munmaped
)>>20, mmaped
>>20, munmaped
>>20,
52 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size
);
53 Printf("Stats: malloc large: %zu\n", malloc_large
);
56 void AsanStats::MergeFrom(const AsanStats
*stats
) {
57 uptr
*dst_ptr
= reinterpret_cast<uptr
*>(this);
58 const uptr
*src_ptr
= reinterpret_cast<const uptr
*>(stats
);
59 uptr num_fields
= sizeof(*this) / sizeof(uptr
);
60 for (uptr i
= 0; i
< num_fields
; i
++)
61 dst_ptr
[i
] += src_ptr
[i
];
64 static BlockingMutex
print_lock(LINKER_INITIALIZED
);
66 static AsanStats
unknown_thread_stats(LINKER_INITIALIZED
);
67 static AsanStats
dead_threads_stats(LINKER_INITIALIZED
);
68 static BlockingMutex
dead_threads_stats_lock(LINKER_INITIALIZED
);
69 // Required for malloc_zone_statistics() on OS X. This can't be stored in
70 // per-thread AsanStats.
71 static uptr max_malloced_memory
;
73 static void MergeThreadStats(ThreadContextBase
*tctx_base
, void *arg
) {
74 AsanStats
*accumulated_stats
= reinterpret_cast<AsanStats
*>(arg
);
75 AsanThreadContext
*tctx
= static_cast<AsanThreadContext
*>(tctx_base
);
76 if (AsanThread
*t
= tctx
->thread
)
77 accumulated_stats
->MergeFrom(&t
->stats());
80 static void GetAccumulatedStats(AsanStats
*stats
) {
83 ThreadRegistryLock
l(&asanThreadRegistry());
85 .RunCallbackForEachThreadLocked(MergeThreadStats
, stats
);
87 stats
->MergeFrom(&unknown_thread_stats
);
89 BlockingMutexLock
lock(&dead_threads_stats_lock
);
90 stats
->MergeFrom(&dead_threads_stats
);
92 // This is not very accurate: we may miss allocation peaks that happen
93 // between two updates of accumulated_stats_. For more accurate bookkeeping
94 // the maximum should be updated on every malloc(), which is unacceptable.
95 if (max_malloced_memory
< stats
->malloced
) {
96 max_malloced_memory
= stats
->malloced
;
100 void FlushToDeadThreadStats(AsanStats
*stats
) {
101 BlockingMutexLock
lock(&dead_threads_stats_lock
);
102 dead_threads_stats
.MergeFrom(stats
);
106 void FillMallocStatistics(AsanMallocStats
*malloc_stats
) {
108 GetAccumulatedStats(&stats
);
109 malloc_stats
->blocks_in_use
= stats
.mallocs
;
110 malloc_stats
->size_in_use
= stats
.malloced
;
111 malloc_stats
->max_size_in_use
= max_malloced_memory
;
112 malloc_stats
->size_allocated
= stats
.mmaped
;
115 AsanStats
&GetCurrentThreadStats() {
116 AsanThread
*t
= GetCurrentThread();
117 return (t
) ? t
->stats() : unknown_thread_stats
;
120 static void PrintAccumulatedStats() {
122 GetAccumulatedStats(&stats
);
123 // Use lock to keep reports from mixing up.
124 BlockingMutexLock
lock(&print_lock
);
126 StackDepotStats
*stack_depot_stats
= StackDepotGetStats();
127 Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
128 stack_depot_stats
->n_uniq_ids
, stack_depot_stats
->allocated
>> 20);
129 PrintInternalAllocatorStats();
132 } // namespace __asan
134 // ---------------------- Interface ---------------- {{{1
135 using namespace __asan
; // NOLINT
137 uptr
__sanitizer_get_current_allocated_bytes() {
139 GetAccumulatedStats(&stats
);
140 uptr malloced
= stats
.malloced
;
141 uptr freed
= stats
.freed
;
142 // Return sane value if malloced < freed due to racy
143 // way we update accumulated stats.
144 return (malloced
> freed
) ? malloced
- freed
: 1;
147 uptr
__sanitizer_get_heap_size() {
149 GetAccumulatedStats(&stats
);
150 return stats
.mmaped
- stats
.munmaped
;
153 uptr
__sanitizer_get_free_bytes() {
155 GetAccumulatedStats(&stats
);
156 uptr total_free
= stats
.mmaped
158 + stats
.really_freed
;
159 uptr total_used
= stats
.malloced
160 + stats
.malloced_redzones
;
161 // Return sane value if total_free < total_used due to racy
162 // way we update accumulated stats.
163 return (total_free
> total_used
) ? total_free
- total_used
: 1;
166 uptr
__sanitizer_get_unmapped_bytes() {
170 void __asan_print_accumulated_stats() {
171 PrintAccumulatedStats();