2013-02-04 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / asan / asan_stats.cc
blob1187bd92f97df8cfc4071c02d8a3271fa7d99d2e
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/asan_interface.h"
17 #include "sanitizer_common/sanitizer_stackdepot.h"
19 namespace __asan {
21 AsanStats::AsanStats() {
22 CHECK(REAL(memset) != 0);
23 REAL(memset)(this, 0, sizeof(AsanStats));
26 static void PrintMallocStatsArray(const char *prefix,
27 uptr (&array)[kNumberOfSizeClasses]) {
28 Printf("%s", prefix);
29 for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
30 if (!array[i]) continue;
31 Printf("%zu:%zu; ", i, array[i]);
33 Printf("\n");
36 void AsanStats::Print() {
37 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
38 malloced>>20, malloced_redzones>>20, mallocs);
39 Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs);
40 Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees);
41 Printf("Stats: %zuM really freed by %zu calls\n",
42 really_freed>>20, real_frees);
43 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
44 (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20,
45 mmaps, munmaps);
47 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size);
48 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
49 PrintMallocStatsArray(" frees by size class: ", freed_by_size);
50 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size);
51 Printf("Stats: malloc large: %zu small slow: %zu\n",
52 malloc_large, malloc_small_slow);
55 static BlockingMutex print_lock(LINKER_INITIALIZED);
57 static void PrintAccumulatedStats() {
58 AsanStats stats;
59 asanThreadRegistry().GetAccumulatedStats(&stats);
60 // Use lock to keep reports from mixing up.
61 BlockingMutexLock lock(&print_lock);
62 stats.Print();
63 StackDepotStats *stack_depot_stats = StackDepotGetStats();
64 Printf("Stats: StackDepot: %zd ids; %zdM mapped\n",
65 stack_depot_stats->n_uniq_ids, stack_depot_stats->mapped >> 20);
66 PrintInternalAllocatorStats();
69 } // namespace __asan
71 // ---------------------- Interface ---------------- {{{1
72 using namespace __asan; // NOLINT
74 uptr __asan_get_current_allocated_bytes() {
75 return asanThreadRegistry().GetCurrentAllocatedBytes();
78 uptr __asan_get_heap_size() {
79 return asanThreadRegistry().GetHeapSize();
82 uptr __asan_get_free_bytes() {
83 return asanThreadRegistry().GetFreeBytes();
86 uptr __asan_get_unmapped_bytes() {
87 return 0;
90 void __asan_print_accumulated_stats() {
91 PrintAccumulatedStats();