blob: a78b7b1e926d88bd52de20bbf28cd13fb3919487 [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"
Stephen Hines6a211c52014-07-21 00:49:56 -070018#include "sanitizer_common/sanitizer_allocator_interface.h"
Alexey Samsonovc25e62b2013-03-20 10:11:24 +000019#include "sanitizer_common/sanitizer_mutex.h"
Kostya Serebryany9e3bd382012-12-26 06:30:02 +000020#include "sanitizer_common/sanitizer_stackdepot.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000021
22namespace __asan {
23
24AsanStats::AsanStats() {
Alexey Samsonov717ece52013-09-02 08:39:07 +000025 Clear();
26}
27
28void AsanStats::Clear() {
Kostya Serebryanya27bdf72013-04-05 14:40:25 +000029 CHECK(REAL(memset));
Alexey Samsonov09672ca2012-02-08 13:45:31 +000030 REAL(memset)(this, 0, sizeof(AsanStats));
Kostya Serebryany1e172b42011-11-30 01:07:02 +000031}
32
33static void PrintMallocStatsArray(const char *prefix,
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000034 uptr (&array)[kNumberOfSizeClasses]) {
Kostya Serebryany283c2962012-08-28 11:34:40 +000035 Printf("%s", prefix);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000036 for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000037 if (!array[i]) continue;
Kostya Serebryany283c2962012-08-28 11:34:40 +000038 Printf("%zu:%zu; ", i, array[i]);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039 }
Kostya Serebryany283c2962012-08-28 11:34:40 +000040 Printf("\n");
Kostya Serebryany1e172b42011-11-30 01:07:02 +000041}
42
43void AsanStats::Print() {
Kostya Serebryany283c2962012-08-28 11:34:40 +000044 Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
Alexey Samsonove9541012012-06-06 13:11:29 +000045 malloced>>20, malloced_redzones>>20, mallocs);
Kostya Serebryany283c2962012-08-28 11:34:40 +000046 Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs);
47 Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees);
48 Printf("Stats: %zuM really freed by %zu calls\n",
Alexey Samsonove9541012012-06-06 13:11:29 +000049 really_freed>>20, real_frees);
Kostya Serebryanye11c5c52012-12-21 12:26:31 +000050 Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
51 (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20,
52 mmaps, munmaps);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000053
54 PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size);
55 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
56 PrintMallocStatsArray(" frees by size class: ", freed_by_size);
57 PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size);
Kostya Serebryany283c2962012-08-28 11:34:40 +000058 Printf("Stats: malloc large: %zu small slow: %zu\n",
Alexey Samsonove9541012012-06-06 13:11:29 +000059 malloc_large, malloc_small_slow);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000060}
61
Alexey Samsonov717ece52013-09-02 08:39:07 +000062void AsanStats::MergeFrom(const AsanStats *stats) {
63 uptr *dst_ptr = reinterpret_cast<uptr*>(this);
64 const uptr *src_ptr = reinterpret_cast<const uptr*>(stats);
65 uptr num_fields = sizeof(*this) / sizeof(uptr);
66 for (uptr i = 0; i < num_fields; i++)
67 dst_ptr[i] += src_ptr[i];
68}
69
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +000070static BlockingMutex print_lock(LINKER_INITIALIZED);
Kostya Serebryany75f74612011-12-03 02:23:25 +000071
Alexey Samsonov717ece52013-09-02 08:39:07 +000072static AsanStats unknown_thread_stats(LINKER_INITIALIZED);
73static AsanStats dead_threads_stats(LINKER_INITIALIZED);
74static BlockingMutex dead_threads_stats_lock(LINKER_INITIALIZED);
75// Required for malloc_zone_statistics() on OS X. This can't be stored in
76// per-thread AsanStats.
77static uptr max_malloced_memory;
78
79static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
80 AsanStats *accumulated_stats = reinterpret_cast<AsanStats*>(arg);
81 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
82 if (AsanThread *t = tctx->thread)
83 accumulated_stats->MergeFrom(&t->stats());
84}
85
86static void GetAccumulatedStats(AsanStats *stats) {
87 stats->Clear();
88 {
89 ThreadRegistryLock l(&asanThreadRegistry());
90 asanThreadRegistry()
91 .RunCallbackForEachThreadLocked(MergeThreadStats, stats);
92 }
93 stats->MergeFrom(&unknown_thread_stats);
94 {
95 BlockingMutexLock lock(&dead_threads_stats_lock);
96 stats->MergeFrom(&dead_threads_stats);
97 }
98 // This is not very accurate: we may miss allocation peaks that happen
99 // between two updates of accumulated_stats_. For more accurate bookkeeping
100 // the maximum should be updated on every malloc(), which is unacceptable.
101 if (max_malloced_memory < stats->malloced) {
102 max_malloced_memory = stats->malloced;
103 }
104}
105
106void FlushToDeadThreadStats(AsanStats *stats) {
107 BlockingMutexLock lock(&dead_threads_stats_lock);
108 dead_threads_stats.MergeFrom(stats);
109 stats->Clear();
110}
111
112void FillMallocStatistics(AsanMallocStats *malloc_stats) {
113 AsanStats stats;
114 GetAccumulatedStats(&stats);
115 malloc_stats->blocks_in_use = stats.mallocs;
116 malloc_stats->size_in_use = stats.malloced;
117 malloc_stats->max_size_in_use = max_malloced_memory;
118 malloc_stats->size_allocated = stats.mmaped;
119}
120
121AsanStats &GetCurrentThreadStats() {
122 AsanThread *t = GetCurrentThread();
123 return (t) ? t->stats() : unknown_thread_stats;
124}
125
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000126static void PrintAccumulatedStats() {
Alexey Samsonov84f5bac2012-11-19 10:25:17 +0000127 AsanStats stats;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000128 GetAccumulatedStats(&stats);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000129 // Use lock to keep reports from mixing up.
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000130 BlockingMutexLock lock(&print_lock);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131 stats.Print();
Kostya Serebryany9e3bd382012-12-26 06:30:02 +0000132 StackDepotStats *stack_depot_stats = StackDepotGetStats();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700133 Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
134 stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20);
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000135 PrintInternalAllocatorStats();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136}
137
138} // namespace __asan
139
140// ---------------------- Interface ---------------- {{{1
141using namespace __asan; // NOLINT
142
Stephen Hines6a211c52014-07-21 00:49:56 -0700143uptr __sanitizer_get_current_allocated_bytes() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000144 AsanStats stats;
145 GetAccumulatedStats(&stats);
146 uptr malloced = stats.malloced;
147 uptr freed = stats.freed;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000148 // Return sane value if malloced < freed due to racy
149 // way we update accumulated stats.
150 return (malloced > freed) ? malloced - freed : 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000151}
152
Stephen Hines6a211c52014-07-21 00:49:56 -0700153uptr __sanitizer_get_heap_size() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000154 AsanStats stats;
155 GetAccumulatedStats(&stats);
156 return stats.mmaped - stats.munmaped;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000157}
158
Stephen Hines6a211c52014-07-21 00:49:56 -0700159uptr __sanitizer_get_free_bytes() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000160 AsanStats stats;
161 GetAccumulatedStats(&stats);
162 uptr total_free = stats.mmaped
163 - stats.munmaped
164 + stats.really_freed
165 + stats.really_freed_redzones;
166 uptr total_used = stats.malloced
167 + stats.malloced_redzones;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000168 // Return sane value if total_free < total_used due to racy
169 // way we update accumulated stats.
170 return (total_free > total_used) ? total_free - total_used : 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000171}
172
Stephen Hines6a211c52014-07-21 00:49:56 -0700173uptr __sanitizer_get_unmapped_bytes() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174 return 0;
175}
176
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000177void __asan_print_accumulated_stats() {
178 PrintAccumulatedStats();
179}