Alexey Samsonov | e5f5895 | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_stats.cc -----------------------------------------------------===// |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 15 | #include "asan_internal.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 16 | #include "asan_stats.h" |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 17 | #include "asan_thread.h" |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_allocator_interface.h" |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_mutex.h" |
Kostya Serebryany | 9e3bd38 | 2012-12-26 06:30:02 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 21 | |
| 22 | namespace __asan { |
| 23 | |
| 24 | AsanStats::AsanStats() { |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 25 | Clear(); |
| 26 | } |
| 27 | |
| 28 | void AsanStats::Clear() { |
Kostya Serebryany | a27bdf7 | 2013-04-05 14:40:25 +0000 | [diff] [blame] | 29 | CHECK(REAL(memset)); |
Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame] | 30 | REAL(memset)(this, 0, sizeof(AsanStats)); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void PrintMallocStatsArray(const char *prefix, |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 34 | uptr (&array)[kNumberOfSizeClasses]) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 35 | Printf("%s", prefix); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 36 | for (uptr i = 0; i < kNumberOfSizeClasses; i++) { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 37 | if (!array[i]) continue; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 38 | Printf("%zu:%zu; ", i, array[i]); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 39 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 40 | Printf("\n"); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void AsanStats::Print() { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 44 | Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n", |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 45 | malloced>>20, malloced_redzones>>20, mallocs); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 46 | 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 Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 49 | really_freed>>20, real_frees); |
Kostya Serebryany | e11c5c5 | 2012-12-21 12:26:31 +0000 | [diff] [blame] | 50 | Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n", |
| 51 | (mmaped-munmaped)>>20, mmaped>>20, munmaped>>20, |
| 52 | mmaps, munmaps); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 53 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 54 | PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 55 | Printf("Stats: malloc large: %zu\n", malloc_large); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 58 | void 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 Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 66 | static BlockingMutex print_lock(LINKER_INITIALIZED); |
Kostya Serebryany | 75f7461 | 2011-12-03 02:23:25 +0000 | [diff] [blame] | 67 | |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 68 | static AsanStats unknown_thread_stats(LINKER_INITIALIZED); |
| 69 | static AsanStats dead_threads_stats(LINKER_INITIALIZED); |
| 70 | static 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. |
| 73 | static uptr max_malloced_memory; |
| 74 | |
| 75 | static 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 | |
| 82 | static 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 | |
| 102 | void FlushToDeadThreadStats(AsanStats *stats) { |
| 103 | BlockingMutexLock lock(&dead_threads_stats_lock); |
| 104 | dead_threads_stats.MergeFrom(stats); |
| 105 | stats->Clear(); |
| 106 | } |
| 107 | |
| 108 | void 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 | |
| 117 | AsanStats &GetCurrentThreadStats() { |
| 118 | AsanThread *t = GetCurrentThread(); |
| 119 | return (t) ? t->stats() : unknown_thread_stats; |
| 120 | } |
| 121 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 122 | static void PrintAccumulatedStats() { |
Alexey Samsonov | 84f5bac | 2012-11-19 10:25:17 +0000 | [diff] [blame] | 123 | AsanStats stats; |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 124 | GetAccumulatedStats(&stats); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 125 | // Use lock to keep reports from mixing up. |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 126 | BlockingMutexLock lock(&print_lock); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 127 | stats.Print(); |
Kostya Serebryany | 9e3bd38 | 2012-12-26 06:30:02 +0000 | [diff] [blame] | 128 | StackDepotStats *stack_depot_stats = StackDepotGetStats(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 129 | Printf("Stats: StackDepot: %zd ids; %zdM allocated\n", |
| 130 | stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20); |
Kostya Serebryany | 4b48f45 | 2012-12-27 14:09:19 +0000 | [diff] [blame] | 131 | PrintInternalAllocatorStats(); |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace __asan |
| 135 | |
| 136 | // ---------------------- Interface ---------------- {{{1 |
| 137 | using namespace __asan; // NOLINT |
| 138 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 139 | uptr __sanitizer_get_current_allocated_bytes() { |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 140 | AsanStats stats; |
| 141 | GetAccumulatedStats(&stats); |
| 142 | uptr malloced = stats.malloced; |
| 143 | uptr freed = stats.freed; |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 144 | // Return sane value if malloced < freed due to racy |
| 145 | // way we update accumulated stats. |
| 146 | return (malloced > freed) ? malloced - freed : 1; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 149 | uptr __sanitizer_get_heap_size() { |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 150 | AsanStats stats; |
| 151 | GetAccumulatedStats(&stats); |
| 152 | return stats.mmaped - stats.munmaped; |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 155 | uptr __sanitizer_get_free_bytes() { |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 156 | AsanStats stats; |
| 157 | GetAccumulatedStats(&stats); |
| 158 | uptr total_free = stats.mmaped |
| 159 | - stats.munmaped |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 160 | + stats.really_freed; |
Alexey Samsonov | 717ece5 | 2013-09-02 08:39:07 +0000 | [diff] [blame] | 161 | uptr total_used = stats.malloced |
| 162 | + stats.malloced_redzones; |
Alexey Samsonov | c25e62b | 2013-03-20 10:11:24 +0000 | [diff] [blame] | 163 | // 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 Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 168 | uptr __sanitizer_get_unmapped_bytes() { |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 172 | void __asan_print_accumulated_stats() { |
| 173 | PrintAccumulatedStats(); |
| 174 | } |