Add libstdc++-raw-cxx.m4 and use it in libsanitizer
[gcc.git] / libsanitizer / asan / asan_stats.cc
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_lock.h"
15 #include "asan_stats.h"
16 #include "asan_thread_registry.h"
17 #include "sanitizer/asan_interface.h"
18
19 namespace __asan {
20
21 AsanStats::AsanStats() {
22 CHECK(REAL(memset) != 0);
23 REAL(memset)(this, 0, sizeof(AsanStats));
24 }
25
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]);
32 }
33 Printf("\n");
34 }
35
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 (%zu full pages) mmaped in %zu calls\n",
44 mmaped>>20, mmaped / GetPageSizeCached(), mmaps);
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 AsanLock 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 ScopedLock lock(&print_lock);
61 stats.Print();
62 }
63
64 } // namespace __asan
65
66 // ---------------------- Interface ---------------- {{{1
67 using namespace __asan; // NOLINT
68
69 uptr __asan_get_current_allocated_bytes() {
70 return asanThreadRegistry().GetCurrentAllocatedBytes();
71 }
72
73 uptr __asan_get_heap_size() {
74 return asanThreadRegistry().GetHeapSize();
75 }
76
77 uptr __asan_get_free_bytes() {
78 return asanThreadRegistry().GetFreeBytes();
79 }
80
81 uptr __asan_get_unmapped_bytes() {
82 return 0;
83 }
84
85 void __asan_print_accumulated_stats() {
86 PrintAccumulatedStats();
87 }