1 //===-- asan_stats.cc -----------------------------------------------------===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // This file is a part of AddressSanitizer, an address sanity checker. 9 // 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_registry.h" 16 #include "sanitizer_common/sanitizer_stackdepot.h" 17 18 namespace __asan { 19 20 AsanStats::AsanStats() { 21 CHECK(REAL(memset) != 0); 22 REAL(memset)(this, 0, sizeof(AsanStats)); 23 } 24 25 static void PrintMallocStatsArray(const char *prefix, 26 uptr (&array)[kNumberOfSizeClasses]) { 27 Printf("%s", prefix); 28 for (uptr i = 0; i < kNumberOfSizeClasses; i++) { 29 if (!array[i]) continue; 30 Printf("%zu:%zu; ", i, array[i]); 31 } 32 Printf("\n"); 33 } 34 35 void AsanStats::Print() { 36 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n", 37 malloced>>20, malloced_redzones>>20, mallocs); 38 Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs); 39 Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees); 40 Printf("Stats: %zuM really freed by %zu calls\n", 41 really_freed>>20, real_frees); 42 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n", 43 (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20, 44 mmaps, munmaps); 45 46 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size); 47 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size); 48 PrintMallocStatsArray(" frees by size class: ", freed_by_size); 49 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size); 50 Printf("Stats: malloc large: %zu small slow: %zu\n", 51 malloc_large, malloc_small_slow); 52 } 53 54 static BlockingMutex print_lock(LINKER_INITIALIZED); 55 56 static void PrintAccumulatedStats() { 57 AsanStats stats; 58 asanThreadRegistry().GetAccumulatedStats(&stats); 59 // Use lock to keep reports from mixing up. 60 BlockingMutexLock lock(&print_lock); 61 stats.Print(); 62 StackDepotStats *stack_depot_stats = StackDepotGetStats(); 63 Printf("Stats: StackDepot: %zd ids; %zdM mapped\n", 64 stack_depot_stats->n_uniq_ids, stack_depot_stats->mapped >> 20); 65 PrintInternalAllocatorStats(); 66 } 67 68 } // namespace __asan 69 70 // ---------------------- Interface ---------------- {{{1 71 using namespace __asan; // NOLINT 72 73 uptr __asan_get_current_allocated_bytes() { 74 return asanThreadRegistry().GetCurrentAllocatedBytes(); 75 } 76 77 uptr __asan_get_heap_size() { 78 return asanThreadRegistry().GetHeapSize(); 79 } 80 81 uptr __asan_get_free_bytes() { 82 return asanThreadRegistry().GetFreeBytes(); 83 } 84 85 uptr __asan_get_unmapped_bytes() { 86 return 0; 87 } 88 89 void __asan_print_accumulated_stats() { 90 PrintAccumulatedStats(); 91 } 92