Home | History | Annotate | Download | only in asan
      1 //===-- asan_stats.cc -----------------------------------------------------===//
      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_internal.h"
     16 #include "asan_stats.h"
     17 #include "asan_thread_registry.h"
     18 #include "sanitizer_common/sanitizer_stackdepot.h"
     19 
     20 namespace __asan {
     21 
     22 AsanStats::AsanStats() {
     23   CHECK(REAL(memset) != 0);
     24   REAL(memset)(this, 0, sizeof(AsanStats));
     25 }
     26 
     27 static void PrintMallocStatsArray(const char *prefix,
     28                                   uptr (&array)[kNumberOfSizeClasses]) {
     29   Printf("%s", prefix);
     30   for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
     31     if (!array[i]) continue;
     32     Printf("%zu:%zu; ", i, array[i]);
     33   }
     34   Printf("\n");
     35 }
     36 
     37 void AsanStats::Print() {
     38   Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
     39              malloced>>20, malloced_redzones>>20, mallocs);
     40   Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs);
     41   Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees);
     42   Printf("Stats: %zuM really freed by %zu calls\n",
     43              really_freed>>20, real_frees);
     44   Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
     45              (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20,
     46              mmaps, munmaps);
     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 BlockingMutex print_lock(LINKER_INITIALIZED);
     57 
     58 static void PrintAccumulatedStats() {
     59   AsanStats stats;
     60   asanThreadRegistry().GetAccumulatedStats(&stats);
     61   // Use lock to keep reports from mixing up.
     62   BlockingMutexLock lock(&print_lock);
     63   stats.Print();
     64   StackDepotStats *stack_depot_stats = StackDepotGetStats();
     65   Printf("Stats: StackDepot: %zd ids; %zdM mapped\n",
     66          stack_depot_stats->n_uniq_ids, stack_depot_stats->mapped >> 20);
     67   PrintInternalAllocatorStats();
     68 }
     69 
     70 }  // namespace __asan
     71 
     72 // ---------------------- Interface ---------------- {{{1
     73 using namespace __asan;  // NOLINT
     74 
     75 uptr __asan_get_current_allocated_bytes() {
     76   return asanThreadRegistry().GetCurrentAllocatedBytes();
     77 }
     78 
     79 uptr __asan_get_heap_size() {
     80   return asanThreadRegistry().GetHeapSize();
     81 }
     82 
     83 uptr __asan_get_free_bytes() {
     84   return asanThreadRegistry().GetFreeBytes();
     85 }
     86 
     87 uptr __asan_get_unmapped_bytes() {
     88   return 0;
     89 }
     90 
     91 void __asan_print_accumulated_stats() {
     92   PrintAccumulatedStats();
     93 }
     94