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