2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_stats.cc -----------------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Code related to statistics collected by AddressSanitizer.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_stats.h"
|
2013-03-21 19:23:41 +08:00
|
|
|
#include "asan_thread.h"
|
2014-07-08 01:39:31 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_interface.h"
|
2013-03-20 18:11:24 +08:00
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
2012-12-26 14:30:02 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
AsanStats::AsanStats() {
|
2013-09-02 16:39:07 +08:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanStats::Clear() {
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK(REAL(memset));
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(memset)(this, 0, sizeof(AsanStats));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintMallocStatsArray(const char *prefix,
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr (&array)[kNumberOfSizeClasses]) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%s", prefix);
|
2012-05-31 22:35:53 +08:00
|
|
|
for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
|
2011-11-30 09:07:02 +08:00
|
|
|
if (!array[i]) continue;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%zu:%zu; ", i, array[i]);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsanStats::Print() {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("Stats: %zuM malloced (%zuM for red zones) by %zu calls\n",
|
2012-06-06 21:11:29 +08:00
|
|
|
malloced>>20, malloced_redzones>>20, mallocs);
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("Stats: %zuM realloced by %zu calls\n", realloced>>20, reallocs);
|
|
|
|
Printf("Stats: %zuM freed by %zu calls\n", freed>>20, frees);
|
|
|
|
Printf("Stats: %zuM really freed by %zu calls\n",
|
2012-06-06 21:11:29 +08:00
|
|
|
really_freed>>20, real_frees);
|
2012-12-21 20:26:31 +08:00
|
|
|
Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
|
|
|
|
(mmaped-munmaped)>>20, mmaped>>20, munmaped>>20,
|
|
|
|
mmaps, munmaps);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size);
|
|
|
|
PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
|
|
|
|
PrintMallocStatsArray(" frees by size class: ", freed_by_size);
|
|
|
|
PrintMallocStatsArray(" rfrees by size class: ", really_freed_by_size);
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("Stats: malloc large: %zu small slow: %zu\n",
|
2012-06-06 21:11:29 +08:00
|
|
|
malloc_large, malloc_small_slow);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2013-09-02 16:39:07 +08:00
|
|
|
void AsanStats::MergeFrom(const AsanStats *stats) {
|
|
|
|
uptr *dst_ptr = reinterpret_cast<uptr*>(this);
|
|
|
|
const uptr *src_ptr = reinterpret_cast<const uptr*>(stats);
|
|
|
|
uptr num_fields = sizeof(*this) / sizeof(uptr);
|
|
|
|
for (uptr i = 0; i < num_fields; i++)
|
|
|
|
dst_ptr[i] += src_ptr[i];
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2013-09-02 16:39:07 +08:00
|
|
|
static BlockingMutex print_lock(LINKER_INITIALIZED);
|
|
|
|
|
2013-03-20 18:11:24 +08:00
|
|
|
static AsanStats unknown_thread_stats(LINKER_INITIALIZED);
|
2013-09-02 16:39:07 +08:00
|
|
|
static AsanStats dead_threads_stats(LINKER_INITIALIZED);
|
|
|
|
static BlockingMutex dead_threads_stats_lock(LINKER_INITIALIZED);
|
2013-03-20 18:11:24 +08:00
|
|
|
// Required for malloc_zone_statistics() on OS X. This can't be stored in
|
|
|
|
// per-thread AsanStats.
|
|
|
|
static uptr max_malloced_memory;
|
|
|
|
|
2013-09-02 16:39:07 +08:00
|
|
|
static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
|
|
|
|
AsanStats *accumulated_stats = reinterpret_cast<AsanStats*>(arg);
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThreadContext *tctx = static_cast<AsanThreadContext*>(tctx_base);
|
|
|
|
if (AsanThread *t = tctx->thread)
|
2013-09-02 16:39:07 +08:00
|
|
|
accumulated_stats->MergeFrom(&t->stats());
|
2013-03-21 19:23:41 +08:00
|
|
|
}
|
|
|
|
|
2013-09-02 16:39:07 +08:00
|
|
|
static void GetAccumulatedStats(AsanStats *stats) {
|
|
|
|
stats->Clear();
|
2013-03-21 19:23:41 +08:00
|
|
|
{
|
|
|
|
ThreadRegistryLock l(&asanThreadRegistry());
|
2013-09-02 16:39:07 +08:00
|
|
|
asanThreadRegistry()
|
|
|
|
.RunCallbackForEachThreadLocked(MergeThreadStats, stats);
|
|
|
|
}
|
|
|
|
stats->MergeFrom(&unknown_thread_stats);
|
|
|
|
{
|
|
|
|
BlockingMutexLock lock(&dead_threads_stats_lock);
|
|
|
|
stats->MergeFrom(&dead_threads_stats);
|
2013-03-21 19:23:41 +08:00
|
|
|
}
|
2013-03-20 18:11:24 +08:00
|
|
|
// This is not very accurate: we may miss allocation peaks that happen
|
|
|
|
// between two updates of accumulated_stats_. For more accurate bookkeeping
|
|
|
|
// the maximum should be updated on every malloc(), which is unacceptable.
|
2013-09-02 16:39:07 +08:00
|
|
|
if (max_malloced_memory < stats->malloced) {
|
|
|
|
max_malloced_memory = stats->malloced;
|
2013-03-20 18:11:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 16:39:07 +08:00
|
|
|
void FlushToDeadThreadStats(AsanStats *stats) {
|
|
|
|
BlockingMutexLock lock(&dead_threads_stats_lock);
|
|
|
|
dead_threads_stats.MergeFrom(stats);
|
|
|
|
stats->Clear();
|
2013-03-20 18:11:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FillMallocStatistics(AsanMallocStats *malloc_stats) {
|
2013-09-02 16:39:07 +08:00
|
|
|
AsanStats stats;
|
|
|
|
GetAccumulatedStats(&stats);
|
|
|
|
malloc_stats->blocks_in_use = stats.mallocs;
|
|
|
|
malloc_stats->size_in_use = stats.malloced;
|
2013-03-20 18:11:24 +08:00
|
|
|
malloc_stats->max_size_in_use = max_malloced_memory;
|
2013-09-02 16:39:07 +08:00
|
|
|
malloc_stats->size_allocated = stats.mmaped;
|
2013-03-20 18:11:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AsanStats &GetCurrentThreadStats() {
|
|
|
|
AsanThread *t = GetCurrentThread();
|
|
|
|
return (t) ? t->stats() : unknown_thread_stats;
|
|
|
|
}
|
|
|
|
|
2013-09-02 16:39:07 +08:00
|
|
|
static void PrintAccumulatedStats() {
|
|
|
|
AsanStats stats;
|
|
|
|
GetAccumulatedStats(&stats);
|
|
|
|
// Use lock to keep reports from mixing up.
|
|
|
|
BlockingMutexLock lock(&print_lock);
|
|
|
|
stats.Print();
|
|
|
|
StackDepotStats *stack_depot_stats = StackDepotGetStats();
|
2014-05-21 17:02:13 +08:00
|
|
|
Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
|
|
|
|
stack_depot_stats->n_uniq_ids, stack_depot_stats->allocated >> 20);
|
2013-09-02 16:39:07 +08:00
|
|
|
PrintInternalAllocatorStats();
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// ---------------------- Interface ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_current_allocated_bytes() {
|
2013-09-02 16:39:07 +08:00
|
|
|
AsanStats stats;
|
|
|
|
GetAccumulatedStats(&stats);
|
|
|
|
uptr malloced = stats.malloced;
|
|
|
|
uptr freed = stats.freed;
|
2013-03-20 18:11:24 +08:00
|
|
|
// Return sane value if malloced < freed due to racy
|
|
|
|
// way we update accumulated stats.
|
|
|
|
return (malloced > freed) ? malloced - freed : 1;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_heap_size() {
|
2013-09-02 16:39:07 +08:00
|
|
|
AsanStats stats;
|
|
|
|
GetAccumulatedStats(&stats);
|
|
|
|
return stats.mmaped - stats.munmaped;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_free_bytes() {
|
2013-09-02 16:39:07 +08:00
|
|
|
AsanStats stats;
|
|
|
|
GetAccumulatedStats(&stats);
|
|
|
|
uptr total_free = stats.mmaped
|
|
|
|
- stats.munmaped
|
|
|
|
+ stats.really_freed
|
|
|
|
+ stats.really_freed_redzones;
|
|
|
|
uptr total_used = stats.malloced
|
|
|
|
+ stats.malloced_redzones;
|
2013-03-20 18:11:24 +08:00
|
|
|
// Return sane value if total_free < total_used due to racy
|
|
|
|
// way we update accumulated stats.
|
|
|
|
return (total_free > total_used) ? total_free - total_used : 1;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_unmapped_bytes() {
|
2011-11-30 09:07:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __asan_print_accumulated_stats() {
|
|
|
|
PrintAccumulatedStats();
|
|
|
|
}
|