1 //===-- asan_stats.cc -------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of AddressSanitizer, an address sanity checker. 11 // 12 // Code related to statistics collected by AddressSanitizer. 13 //===----------------------------------------------------------------------===// 14 #include "asan_interceptors.h" 15 #include "asan_interface.h" 16 #include "asan_internal.h" 17 #include "asan_lock.h" 18 #include "asan_stats.h" 19 #include "asan_thread_registry.h" 20 21 namespace __asan { 22 23 AsanStats::AsanStats() { 24 CHECK(REAL(memset) != NULL); 25 REAL(memset)(this, 0, sizeof(AsanStats)); 26 } 27 28 static void PrintMallocStatsArray(const char *prefix, 29 size_t (&array)[kNumberOfSizeClasses]) { 30 Printf("%s", prefix); 31 for (size_t i = 0; i < kNumberOfSizeClasses; i++) { 32 if (!array[i]) continue; 33 Printf("%zu:%zu; ", i, array[i]); 34 } 35 Printf("\n"); 36 } 37 38 void AsanStats::Print() { 39 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n", 40 malloced>>20, malloced_redzones>>20, mallocs); 41 Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs); 42 Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees); 43 Printf("Stats: %zuM really freed by %zu calls\n", 44 really_freed>>20, real_frees); 45 Printf("Stats: %zuM (%zu full pages) mmaped in %zu calls\n", 46 mmaped>>20, mmaped / kPageSize, mmaps); 47 48 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size); 49 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size); 50 PrintMallocStatsArray(" frees by size class: ", freed_by_size); 51 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size); 52 Printf("Stats: malloc large: %zu small slow: %zu\n", 53 malloc_large, malloc_small_slow); 54 } 55 56 static AsanLock print_lock(LINKER_INITIALIZED); 57 58 static void PrintAccumulatedStats() { 59 AsanStats stats = asanThreadRegistry().GetAccumulatedStats(); 60 // Use lock to keep reports from mixing up. 61 ScopedLock lock(&print_lock); 62 stats.Print(); 63 } 64 65 } // namespace __asan 66 67 // ---------------------- Interface ---------------- {{{1 68 using namespace __asan; // NOLINT 69 70 size_t __asan_get_current_allocated_bytes() { 71 return asanThreadRegistry().GetCurrentAllocatedBytes(); 72 } 73 74 size_t __asan_get_heap_size() { 75 return asanThreadRegistry().GetHeapSize(); 76 } 77 78 size_t __asan_get_free_bytes() { 79 return asanThreadRegistry().GetFreeBytes(); 80 } 81 82 size_t __asan_get_unmapped_bytes() { 83 return 0; 84 } 85 86 void __asan_print_accumulated_stats() { 87 PrintAccumulatedStats(); 88 } 89