From d8803d3d9251d35499659538244e5dd4550f5aaf Mon Sep 17 00:00:00 2001 From: Kostya Kortchinsky Date: Wed, 25 Apr 2018 18:52:29 +0000 Subject: [PATCH] [scudo] Adding an interface function to print allocator stats Summary: This adds `__scudo_print_stats` as an interface function to display the Primary and Secondary allocator statistics for Scudo. Reviewers: alekseyshl, flowerhack Reviewed By: alekseyshl Subscribers: delcypher, llvm-commits, #sanitizers Differential Revision: https://reviews.llvm.org/D46016 llvm-svn: 330857 --- compiler-rt/include/sanitizer/scudo_interface.h | 5 +++++ compiler-rt/lib/scudo/scudo_allocator.cpp | 9 +++++++++ compiler-rt/lib/scudo/scudo_allocator_combined.h | 5 +++++ compiler-rt/lib/scudo/scudo_interface_internal.h | 3 +++ compiler-rt/test/scudo/stats.c | 21 +++++++++++++++++++++ 5 files changed, 43 insertions(+) create mode 100644 compiler-rt/test/scudo/stats.c diff --git a/compiler-rt/include/sanitizer/scudo_interface.h b/compiler-rt/include/sanitizer/scudo_interface.h index 25979fb..be605f1 100644 --- a/compiler-rt/include/sanitizer/scudo_interface.h +++ b/compiler-rt/include/sanitizer/scudo_interface.h @@ -27,6 +27,11 @@ extern "C" { // can be removed by setting LimitMb to 0. This function's parameters should // be fully trusted to avoid security mishaps. void __scudo_set_rss_limit(size_t LimitMb, int HardLimit); + + // This function outputs various allocator statistics for both the Primary + // and Secondary allocators, including memory usage, number of allocations + // and deallocations. + void __scudo_print_stats(void); #ifdef __cplusplus } // extern "C" #endif diff --git a/compiler-rt/lib/scudo/scudo_allocator.cpp b/compiler-rt/lib/scudo/scudo_allocator.cpp index e0f78a0..c72ac86 100644 --- a/compiler-rt/lib/scudo/scudo_allocator.cpp +++ b/compiler-rt/lib/scudo/scudo_allocator.cpp @@ -594,6 +594,11 @@ struct ScudoAllocator { SoftRssLimitMb = LimitMb; CheckRssLimit = HardRssLimitMb || SoftRssLimitMb; } + + void printStats() { + initThreadMaybe(); + BackendAllocator.printStats(); + } }; static ScudoAllocator Instance(LINKER_INITIALIZED); @@ -743,3 +748,7 @@ void __scudo_set_rss_limit(uptr LimitMb, s32 HardLimit) { return; Instance.setRssLimit(LimitMb, !!HardLimit); } + +void __scudo_print_stats() { + Instance.printStats(); +} diff --git a/compiler-rt/lib/scudo/scudo_allocator_combined.h b/compiler-rt/lib/scudo/scudo_allocator_combined.h index 1c8bb3f..d22f2a3 100644 --- a/compiler-rt/lib/scudo/scudo_allocator_combined.h +++ b/compiler-rt/lib/scudo/scudo_allocator_combined.h @@ -61,6 +61,11 @@ class ScudoCombinedAllocator { Stats.Get(StatType); } + void printStats() { + Primary.PrintStats(); + Secondary.PrintStats(); + } + private: PrimaryAllocator Primary; SecondaryAllocator Secondary; diff --git a/compiler-rt/lib/scudo/scudo_interface_internal.h b/compiler-rt/lib/scudo/scudo_interface_internal.h index e2c63db..3e520a5 100644 --- a/compiler-rt/lib/scudo/scudo_interface_internal.h +++ b/compiler-rt/lib/scudo/scudo_interface_internal.h @@ -25,6 +25,9 @@ const char* __scudo_default_options(); SANITIZER_INTERFACE_ATTRIBUTE void __scudo_set_rss_limit(uptr LimitMb, s32 HardLimit); + +SANITIZER_INTERFACE_ATTRIBUTE +void __scudo_print_stats(); } // extern "C" #endif // SCUDO_INTERFACE_INTERNAL_H_ diff --git a/compiler-rt/test/scudo/stats.c b/compiler-rt/test/scudo/stats.c new file mode 100644 index 0000000..e7cc78f --- /dev/null +++ b/compiler-rt/test/scudo/stats.c @@ -0,0 +1,21 @@ +// RUN: %clang_scudo %s -o %t +// RUN: %run %t 2>&1 | FileCheck %s + +// Tests that the allocator stats printing function exists and outputs +// "something". Currently that "something" is fairly nebulous, as the 32-bit +// primary doesn't output anything, and for the 64-bit one it's highly dependent +// on the size class map and potential library allocations. So keep it very +// generic for now. + +#include + +#include + +int main(int argc, char **argv) +{ + free(malloc(1U)); + __scudo_print_stats(); + return 0; +} + +// CHECK: Stats: -- 2.7.4