blob: 73dc3c5ac44f1d392c46a8b0972eec7e2ab5b772 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_stats.cc -----------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000015#include "asan_internal.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000016#include "asan_stats.h"
Alexey Samsonovdef1be92013-03-21 11:23:41 +000017#include "asan_thread.h"
Alexey Samsonovc25e62b2013-03-20 10:11:24 +000018#include "sanitizer_common/sanitizer_mutex.h"
Kostya Serebryany9e3bd382012-12-26 06:30:02 +000019#include "sanitizer_common/sanitizer_stackdepot.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000020
21namespace __asan {
22
23AsanStats::AsanStats() {
Alexey Samsonov717ece52013-09-02 08:39:07 +000024 Clear();
25}
26
27void AsanStats::Clear() {
Kostya Serebryanya27bdf72013-04-05 14:40:25 +000028 CHECK(REAL(memset));
Alexey Samsonov09672ca2012-02-08 13:45:31 +000029 REAL(memset)(this, 0, sizeof(AsanStats));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000030}
31
32static void PrintMallocStatsArray(const char *prefix,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000033 uptr (&array)[kNumberOfSizeClasses]) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000034 Printf("%s", prefix);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000035 for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000036 if (!array[i]) continue;
Kostya Serebryany283c2962012-08-28 11:34:40 +000037 Printf("%zu:%zu; ", i, array[i]);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000038 }
Kostya Serebryany283c2962012-08-28 11:34:40 +000039 Printf("\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +000040}
41
42void AsanStats::Print() {
Kostya Serebryany283c2962012-08-28 11:34:40 +000043 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
Alexey Samsonove9541012012-06-06 13:11:29 +000044 malloced>>20, malloced_redzones>>20, mallocs);
Kostya Serebryany283c2962012-08-28 11:34:40 +000045 Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs);
46 Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees);
47 Printf("Stats: %zuM really freed by %zu calls\n",
Alexey Samsonove9541012012-06-06 13:11:29 +000048 really_freed>>20, real_frees);
Kostya Serebryanye11c5c52012-12-21 12:26:31 +000049 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
50 (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20,
51 mmaps, munmaps);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052
53 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size);
54 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
55 PrintMallocStatsArray(" frees by size class: ", freed_by_size);
56 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size);
Kostya Serebryany283c2962012-08-28 11:34:40 +000057 Printf("Stats: malloc large: %zu small slow: %zu\n",
Alexey Samsonove9541012012-06-06 13:11:29 +000058 malloc_large, malloc_small_slow);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059}
60
Alexey Samsonov717ece52013-09-02 08:39:07 +000061void AsanStats::MergeFrom(const AsanStats *stats) {
62 uptr *dst_ptr = reinterpret_cast<uptr*>(this);
63 const uptr *src_ptr = reinterpret_cast<const uptr*>(stats);
64 uptr num_fields = sizeof(*this) / sizeof(uptr);
65 for (uptr i = 0; i < num_fields; i++)
66 dst_ptr[i] += src_ptr[i];
67}
68
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +000069static BlockingMutex print_lock(LINKER_INITIALIZED);
Kostya Serebryany75f74612011-12-03 02:23:25 +000070
Alexey Samsonov717ece52013-09-02 08:39:07 +000071static AsanStats unknown_thread_stats(LINKER_INITIALIZED);
72static AsanStats dead_threads_stats(LINKER_INITIALIZED);
73static BlockingMutex dead_threads_stats_lock(LINKER_INITIALIZED);
74// Required for malloc_zone_statistics() on OS X. This can't be stored in
75// per-thread AsanStats.
76static uptr max_malloced_memory;
77
78static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
79 AsanStats *accumulated_stats = reinterpret_cast<AsanStats*>(arg);
80 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
81 if (AsanThread *t = tctx->thread)
82 accumulated_stats->MergeFrom(&t->stats());
83}
84
85static void GetAccumulatedStats(AsanStats *stats) {
86 stats->Clear();
87 {
88 ThreadRegistryLock l(&asanThreadRegistry());
89 asanThreadRegistry()
90 .RunCallbackForEachThreadLocked(MergeThreadStats, stats);
91 }
92 stats->MergeFrom(&unknown_thread_stats);
93 {
94 BlockingMutexLock lock(&dead_threads_stats_lock);
95 stats->MergeFrom(&dead_threads_stats);
96 }
97 // This is not very accurate: we may miss allocation peaks that happen
98 // between two updates of accumulated_stats_. For more accurate bookkeeping
99 // the maximum should be updated on every malloc(), which is unacceptable.
100 if (max_malloced_memory < stats->malloced) {
101 max_malloced_memory = stats->malloced;
102 }
103}
104
105void FlushToDeadThreadStats(AsanStats *stats) {
106 BlockingMutexLock lock(&dead_threads_stats_lock);
107 dead_threads_stats.MergeFrom(stats);
108 stats->Clear();
109}
110
111void FillMallocStatistics(AsanMallocStats *malloc_stats) {
112 AsanStats stats;
113 GetAccumulatedStats(&stats);
114 malloc_stats->blocks_in_use = stats.mallocs;
115 malloc_stats->size_in_use = stats.malloced;
116 malloc_stats->max_size_in_use = max_malloced_memory;
117 malloc_stats->size_allocated = stats.mmaped;
118}
119
120AsanStats &GetCurrentThreadStats() {
121 AsanThread *t = GetCurrentThread();
122 return (t) ? t->stats() : unknown_thread_stats;
123}
124
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000125static void PrintAccumulatedStats() {
Alexey Samsonov84f5bac2012-11-19 10:25:17 +0000126 AsanStats stats;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000127 GetAccumulatedStats(&stats);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000128 // Use lock to keep reports from mixing up.
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000129 BlockingMutexLock lock(&print_lock);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 stats.Print();
Kostya Serebryany9e3bd382012-12-26 06:30:02 +0000131 StackDepotStats *stack_depot_stats = StackDepotGetStats();
132 Printf("Stats: StackDepot: %zd ids; %zdM mapped\n",
133 stack_depot_stats->n_uniq_ids, stack_depot_stats->mapped >> 20);
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000134 PrintInternalAllocatorStats();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135}
136
137} // namespace __asan
138
139// ---------------------- Interface ---------------- {{{1
140using namespace __asan; // NOLINT
141
Kostya Serebryany9aead372012-05-31 14:11:07 +0000142uptr __asan_get_current_allocated_bytes() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000143 AsanStats stats;
144 GetAccumulatedStats(&stats);
145 uptr malloced = stats.malloced;
146 uptr freed = stats.freed;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000147 // Return sane value if malloced < freed due to racy
148 // way we update accumulated stats.
149 return (malloced > freed) ? malloced - freed : 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000150}
151
Kostya Serebryany9aead372012-05-31 14:11:07 +0000152uptr __asan_get_heap_size() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000153 AsanStats stats;
154 GetAccumulatedStats(&stats);
155 return stats.mmaped - stats.munmaped;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000156}
157
Kostya Serebryany9aead372012-05-31 14:11:07 +0000158uptr __asan_get_free_bytes() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000159 AsanStats stats;
160 GetAccumulatedStats(&stats);
161 uptr total_free = stats.mmaped
162 - stats.munmaped
163 + stats.really_freed
164 + stats.really_freed_redzones;
165 uptr total_used = stats.malloced
166 + stats.malloced_redzones;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000167 // Return sane value if total_free < total_used due to racy
168 // way we update accumulated stats.
169 return (total_free > total_used) ? total_free - total_used : 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000170}
171
Kostya Serebryany9aead372012-05-31 14:11:07 +0000172uptr __asan_get_unmapped_bytes() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000173 return 0;
174}
175
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000176void __asan_print_accumulated_stats() {
177 PrintAccumulatedStats();
178}