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