1 //===-- asan_stats.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Code related to statistics collected by AddressSanitizer.
12 //===----------------------------------------------------------------------===//
13 #include "asan_interceptors.h"
14 #include "asan_internal.h"
15 #include "asan_stats.h"
16 #include "asan_thread.h"
17 #include "sanitizer_common/sanitizer_allocator_interface.h"
18 #include "sanitizer_common/sanitizer_mutex.h"
19 #include "sanitizer_common/sanitizer_stackdepot.h"
23 AsanStats::AsanStats() {
27 void AsanStats::Clear() {
29 REAL(memset
)(this, 0, sizeof(AsanStats
));
32 static void PrintMallocStatsArray(const char *prefix
,
33 uptr (&array
)[kNumberOfSizeClasses
]) {
35 for (uptr i
= 0; i
< kNumberOfSizeClasses
; i
++) {
36 if (!array
[i
]) continue;
37 Printf("%zu:%zu; ", i
, array
[i
]);
42 void AsanStats::Print() {
43 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
44 malloced
>>20, malloced_redzones
>>20, mallocs
);
45 Printf("Stats: %zuM realloced by %zu calls\n", realloced
>>20, reallocs
);
46 Printf("Stats: %zuM freed by %zu calls\n", freed
>>20, frees
);
47 Printf("Stats: %zuM really freed by %zu calls\n",
48 really_freed
>>20, real_frees
);
49 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
50 (mmaped
-munmaped
)>>20, mmaped
>>20, munmaped
>>20,
53 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size
);
54 Printf("Stats: malloc large: %zu\n", malloc_large
);
57 void AsanStats::MergeFrom(const AsanStats
*stats
) {
58 uptr
*dst_ptr
= reinterpret_cast<uptr
*>(this);
59 const uptr
*src_ptr
= reinterpret_cast<const uptr
*>(stats
);
60 uptr num_fields
= sizeof(*this) / sizeof(uptr
);
61 for (uptr i
= 0; i
< num_fields
; i
++)
62 dst_ptr
[i
] += src_ptr
[i
];
65 static BlockingMutex
print_lock(LINKER_INITIALIZED
);
67 static AsanStats
unknown_thread_stats(LINKER_INITIALIZED
);
68 static AsanStats
dead_threads_stats(LINKER_INITIALIZED
);
69 static BlockingMutex
dead_threads_stats_lock(LINKER_INITIALIZED
);
70 // Required for malloc_zone_statistics() on OS X. This can't be stored in
71 // per-thread AsanStats.
72 static uptr max_malloced_memory
;
74 static void MergeThreadStats(ThreadContextBase
*tctx_base
, void *arg
) {
75 AsanStats
*accumulated_stats
= reinterpret_cast<AsanStats
*>(arg
);
76 AsanThreadContext
*tctx
= static_cast<AsanThreadContext
*>(tctx_base
);
77 if (AsanThread
*t
= tctx
->thread
)
78 accumulated_stats
->MergeFrom(&t
->stats());
81 static void GetAccumulatedStats(AsanStats
*stats
) {
84 ThreadRegistryLock
l(&asanThreadRegistry());
86 .RunCallbackForEachThreadLocked(MergeThreadStats
, stats
);
88 stats
->MergeFrom(&unknown_thread_stats
);
90 BlockingMutexLock
lock(&dead_threads_stats_lock
);
91 stats
->MergeFrom(&dead_threads_stats
);
93 // This is not very accurate: we may miss allocation peaks that happen
94 // between two updates of accumulated_stats_. For more accurate bookkeeping
95 // the maximum should be updated on every malloc(), which is unacceptable.
96 if (max_malloced_memory
< stats
->malloced
) {
97 max_malloced_memory
= stats
->malloced
;
101 void FlushToDeadThreadStats(AsanStats
*stats
) {
102 BlockingMutexLock
lock(&dead_threads_stats_lock
);
103 dead_threads_stats
.MergeFrom(stats
);
107 void FillMallocStatistics(AsanMallocStats
*malloc_stats
) {
109 GetAccumulatedStats(&stats
);
110 malloc_stats
->blocks_in_use
= stats
.mallocs
;
111 malloc_stats
->size_in_use
= stats
.malloced
;
112 malloc_stats
->max_size_in_use
= max_malloced_memory
;
113 malloc_stats
->size_allocated
= stats
.mmaped
;
116 AsanStats
&GetCurrentThreadStats() {
117 AsanThread
*t
= GetCurrentThread();
118 return (t
) ? t
->stats() : unknown_thread_stats
;
121 static void PrintAccumulatedStats() {
123 GetAccumulatedStats(&stats
);
124 // Use lock to keep reports from mixing up.
125 BlockingMutexLock
lock(&print_lock
);
127 StackDepotStats
*stack_depot_stats
= StackDepotGetStats();
128 Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
129 stack_depot_stats
->n_uniq_ids
, stack_depot_stats
->allocated
>> 20);
130 PrintInternalAllocatorStats();
133 } // namespace __asan
135 // ---------------------- Interface ---------------- {{{1
136 using namespace __asan
;
138 uptr
__sanitizer_get_current_allocated_bytes() {
140 GetAccumulatedStats(&stats
);
141 uptr malloced
= stats
.malloced
;
142 uptr freed
= stats
.freed
;
143 // Return sane value if malloced < freed due to racy
144 // way we update accumulated stats.
145 return (malloced
> freed
) ? malloced
- freed
: 1;
148 uptr
__sanitizer_get_heap_size() {
150 GetAccumulatedStats(&stats
);
151 return stats
.mmaped
- stats
.munmaped
;
154 uptr
__sanitizer_get_free_bytes() {
156 GetAccumulatedStats(&stats
);
157 uptr total_free
= stats
.mmaped
159 + stats
.really_freed
;
160 uptr total_used
= stats
.malloced
161 + stats
.malloced_redzones
;
162 // Return sane value if total_free < total_used due to racy
163 // way we update accumulated stats.
164 return (total_free
> total_used
) ? total_free
- total_used
: 1;
167 uptr
__sanitizer_get_unmapped_bytes() {
171 void __asan_print_accumulated_stats() {
172 PrintAccumulatedStats();