From 6ec1600f21b1a4df7ce489766d78f6f3b665b832 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Thu, 6 Sep 2018 22:08:41 +0000 Subject: [hwasan] introduce __hwasan_print_memory_usage git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@341592 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/hwasan/hwasan.cc | 18 ++++++++++++++++++ lib/hwasan/hwasan_allocator.cc | 4 ++++ lib/hwasan/hwasan_allocator.h | 1 + lib/hwasan/hwasan_interface_internal.h | 3 +++ lib/hwasan/hwasan_thread.cc | 18 +++++++++++++++--- lib/hwasan/hwasan_thread.h | 14 ++++++++++++++ 6 files changed, 55 insertions(+), 3 deletions(-) (limited to 'lib/hwasan') diff --git a/lib/hwasan/hwasan.cc b/lib/hwasan/hwasan.cc index 37be710b2..02ba6b7b1 100644 --- a/lib/hwasan/hwasan.cc +++ b/lib/hwasan/hwasan.cc @@ -171,6 +171,22 @@ static void HWAsanCheckFailed(const char *file, int line, const char *cond, Die(); } +static void HwasanPrintMemoryUsage() { + auto thread_stats = Thread::GetThreadStats(); + auto *sds = StackDepotGetStats(); + AllocatorStatCounters asc; + GetAllocatorStats(asc); + Printf( + "HWASAN pid: %d rss: %zd threads: %zd stacks: %zd" + " thr_aux: %zd stack_depot: %zd uniq_stacks: %zd" + " heap: %zd\n", + internal_getpid(), GetRSS(), thread_stats.n_live_threads, + thread_stats.total_stack_size, + thread_stats.n_live_threads * Thread::MemoryUsedPerThread(), + sds->allocated, sds->n_uniq_ids, + asc[AllocatorStatMapped]); +} + } // namespace __hwasan // Interface. @@ -439,6 +455,8 @@ void __hwasan_handle_longjmp(const void *sp_dst) { TagMemory(sp, dst - sp, 0); } +void __hwasan_print_memory_usage() { HwasanPrintMemoryUsage(); } + static const u8 kFallbackTag = 0xBB; u8 __hwasan_generate_tag() { diff --git a/lib/hwasan/hwasan_allocator.cc b/lib/hwasan/hwasan_allocator.cc index c54117b3e..c9783b7e0 100644 --- a/lib/hwasan/hwasan_allocator.cc +++ b/lib/hwasan/hwasan_allocator.cc @@ -48,6 +48,10 @@ static atomic_uint8_t hwasan_allocator_tagging_enabled; static const tag_t kFallbackAllocTag = 0xBB; static const tag_t kFallbackFreeTag = 0xBC; +void GetAllocatorStats(AllocatorStatCounters s) { + allocator.GetStats(s); +} + void HwasanAllocatorInit() { atomic_store_relaxed(&hwasan_allocator_tagging_enabled, !flags()->disable_allocator_tagging); diff --git a/lib/hwasan/hwasan_allocator.h b/lib/hwasan/hwasan_allocator.h index 4f8d8a795..6f50e58fc 100644 --- a/lib/hwasan/hwasan_allocator.h +++ b/lib/hwasan/hwasan_allocator.h @@ -97,6 +97,7 @@ struct HeapAllocationRecord { typedef RingBuffer HeapAllocationsRingBuffer; +void GetAllocatorStats(AllocatorStatCounters s); } // namespace __hwasan diff --git a/lib/hwasan/hwasan_interface_internal.h b/lib/hwasan/hwasan_interface_internal.h index ddb0d2ae0..50164136a 100644 --- a/lib/hwasan/hwasan_interface_internal.h +++ b/lib/hwasan/hwasan_interface_internal.h @@ -143,6 +143,9 @@ void __hwasan_thread_enter(); SANITIZER_INTERFACE_ATTRIBUTE void __hwasan_thread_exit(); +SANITIZER_INTERFACE_ATTRIBUTE +void __hwasan_print_memory_usage(); + } // extern "C" #endif // HWASAN_INTERFACE_INTERNAL_H diff --git a/lib/hwasan/hwasan_thread.cc b/lib/hwasan/hwasan_thread.cc index f77c37550..f3471e03b 100644 --- a/lib/hwasan/hwasan_thread.cc +++ b/lib/hwasan/hwasan_thread.cc @@ -26,14 +26,17 @@ static u32 RandomSeed() { Thread *Thread::main_thread; SpinMutex Thread::thread_list_mutex; +Thread::ThreadStats Thread::thread_stats; void Thread::InsertIntoThreadList(Thread *t) { CHECK(!t->next_); + SpinMutexLock l(&thread_list_mutex); + thread_stats.n_live_threads++; + thread_stats.total_stack_size += t->stack_size(); if (!main_thread) { main_thread = t; return; } - SpinMutexLock l(&thread_list_mutex); Thread *last = main_thread; while (last->next_) last = last->next_; @@ -43,6 +46,8 @@ void Thread::InsertIntoThreadList(Thread *t) { void Thread::RemoveFromThreadList(Thread *t) { CHECK_NE(t, main_thread); SpinMutexLock l(&thread_list_mutex); + thread_stats.n_live_threads--; + thread_stats.total_stack_size -= t->stack_size(); Thread *prev = main_thread; Thread *cur = prev->next_; CHECK(cur); @@ -67,10 +72,17 @@ void Thread::Create() { thread->random_state_ = flags()->random_tags ? RandomSeed() : thread->unique_id_; if (auto sz = flags()->heap_history_size) - thread->heap_allocations_ = RingBuffer::New(sz); - InsertIntoThreadList(thread); + thread->heap_allocations_ = HeapAllocationsRingBuffer::New(sz); SetCurrentThread(thread); thread->Init(); + InsertIntoThreadList(thread); +} + +uptr Thread::MemoryUsedPerThread() { + uptr res = sizeof(Thread); + if (auto sz = flags()->heap_history_size) + res += HeapAllocationsRingBuffer::SizeInBytes(sz); + return res; } void Thread::Init() { diff --git a/lib/hwasan/hwasan_thread.h b/lib/hwasan/hwasan_thread.h index 451baf3c0..6b41f51d8 100644 --- a/lib/hwasan/hwasan_thread.h +++ b/lib/hwasan/hwasan_thread.h @@ -26,6 +26,7 @@ class Thread { uptr stack_top() { return stack_top_; } uptr stack_bottom() { return stack_bottom_; } + uptr stack_size() { return stack_top() - stack_bottom(); } uptr tls_begin() { return tls_begin_; } uptr tls_end() { return tls_end_; } bool IsMainThread() { return unique_id_ == 0; } @@ -75,6 +76,18 @@ class Thread { Print("Thread: "); } + struct ThreadStats { + uptr n_live_threads; + uptr total_stack_size; + }; + + static ThreadStats GetThreadStats() { + SpinMutexLock l(&thread_list_mutex); + return thread_stats; + } + + static uptr MemoryUsedPerThread(); + private: // NOTE: There is no Thread constructor. It is allocated // via mmap() and *must* be valid in zero-initialized state. @@ -101,6 +114,7 @@ class Thread { Thread *next_; // All live threads form a linked list. static SpinMutex thread_list_mutex; static Thread *main_thread; + static ThreadStats thread_stats; u64 unique_id_; // counting from zero. -- cgit v1.2.1