blob: b8c68c32dfbfa2f8e0721068a973a45fa3a37f27 [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
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054 PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080055 Printf("Stats: malloc large: %zu\n", malloc_large);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000056}
57
Alexey Samsonov717ece52013-09-02 08:39:07 +000058void AsanStats::MergeFrom(const AsanStats *stats) {
59 uptr *dst_ptr = reinterpret_cast<uptr*>(this);
60 const uptr *src_ptr = reinterpret_cast<const uptr*>(stats);
61 uptr num_fields = sizeof(*this) / sizeof(uptr);
62 for (uptr i = 0; i < num_fields; i++)
63 dst_ptr[i] += src_ptr[i];
64}
65
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +000066static BlockingMutex print_lock(LINKER_INITIALIZED);
Kostya Serebryany75f74612011-12-03 02:23:25 +000067
Alexey Samsonov717ece52013-09-02 08:39:07 +000068static AsanStats unknown_thread_stats(LINKER_INITIALIZED);
69static AsanStats dead_threads_stats(LINKER_INITIALIZED);
70static BlockingMutex dead_threads_stats_lock(LINKER_INITIALIZED);
71// Required for malloc_zone_statistics() on OS X. This can't be stored in
72// per-thread AsanStats.
73static uptr max_malloced_memory;
74
75static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
76 AsanStats *accumulated_stats = reinterpret_cast<AsanStats*>(arg);
77 AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
78 if (AsanThread *t = tctx->thread)
79 accumulated_stats->MergeFrom(&t->stats());
80}
81
82static void GetAccumulatedStats(AsanStats *stats) {
83 stats->Clear();
84 {
85 ThreadRegistryLock l(&asanThreadRegistry());
86 asanThreadRegistry()
87 .RunCallbackForEachThreadLocked(MergeThreadStats, stats);
88 }
89 stats->MergeFrom(&unknown_thread_stats);
90 {
91 BlockingMutexLock lock(&dead_threads_stats_lock);
92 stats->MergeFrom(&dead_threads_stats);
93 }
94 // This is not very accurate: we may miss allocation peaks that happen
95 // between two updates of accumulated_stats_. For more accurate bookkeeping
96 // the maximum should be updated on every malloc(), which is unacceptable.
97 if (max_malloced_memory < stats->malloced) {
98 max_malloced_memory = stats->malloced;
99 }
100}
101
102void FlushToDeadThreadStats(AsanStats *stats) {
103 BlockingMutexLock lock(&dead_threads_stats_lock);
104 dead_threads_stats.MergeFrom(stats);
105 stats->Clear();
106}
107
108void FillMallocStatistics(AsanMallocStats *malloc_stats) {
109 AsanStats stats;
110 GetAccumulatedStats(&stats);
111 malloc_stats->blocks_in_use = stats.mallocs;
112 malloc_stats->size_in_use = stats.malloced;
113 malloc_stats->max_size_in_use = max_malloced_memory;
114 malloc_stats->size_allocated = stats.mmaped;
115}
116
117AsanStats &GetCurrentThreadStats() {
118 AsanThread *t = GetCurrentThread();
119 return (t) ? t->stats() : unknown_thread_stats;
120}
121
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122static void PrintAccumulatedStats() {
Alexey Samsonov84f5bac2012-11-19 10:25:17 +0000123 AsanStats stats;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000124 GetAccumulatedStats(&stats);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000125 // Use lock to keep reports from mixing up.
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000126 BlockingMutexLock lock(&print_lock);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000127 stats.Print();
Kostya Serebryany9e3bd382012-12-26 06:30:02 +0000128 StackDepotStats *stack_depot_stats = StackDepotGetStats();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700129 Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
130 stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20);
Kostya Serebryany4b48f452012-12-27 14:09:19 +0000131 PrintInternalAllocatorStats();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000132}
133
134} // namespace __asan
135
136// ---------------------- Interface ---------------- {{{1
137using namespace __asan; // NOLINT
138
Stephen Hines6a211c52014-07-21 00:49:56 -0700139uptr __sanitizer_get_current_allocated_bytes() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000140 AsanStats stats;
141 GetAccumulatedStats(&stats);
142 uptr malloced = stats.malloced;
143 uptr freed = stats.freed;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000144 // Return sane value if malloced < freed due to racy
145 // way we update accumulated stats.
146 return (malloced > freed) ? malloced - freed : 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000147}
148
Stephen Hines6a211c52014-07-21 00:49:56 -0700149uptr __sanitizer_get_heap_size() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000150 AsanStats stats;
151 GetAccumulatedStats(&stats);
152 return stats.mmaped - stats.munmaped;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000153}
154
Stephen Hines6a211c52014-07-21 00:49:56 -0700155uptr __sanitizer_get_free_bytes() {
Alexey Samsonov717ece52013-09-02 08:39:07 +0000156 AsanStats stats;
157 GetAccumulatedStats(&stats);
158 uptr total_free = stats.mmaped
159 - stats.munmaped
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800160 + stats.really_freed;
Alexey Samsonov717ece52013-09-02 08:39:07 +0000161 uptr total_used = stats.malloced
162 + stats.malloced_redzones;
Alexey Samsonovc25e62b2013-03-20 10:11:24 +0000163 // Return sane value if total_free < total_used due to racy
164 // way we update accumulated stats.
165 return (total_free > total_used) ? total_free - total_used : 1;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000166}
167
Stephen Hines6a211c52014-07-21 00:49:56 -0700168uptr __sanitizer_get_unmapped_bytes() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000169 return 0;
170}
171
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000172void __asan_print_accumulated_stats() {
173 PrintAccumulatedStats();
174}