From ff30f73cc5aff8f0b0a3b9d4b9925de4679f2a00 Mon Sep 17 00:00:00 2001 From: Kostya Kortchinsky Date: Wed, 14 Aug 2019 16:04:01 +0000 Subject: [PATCH] [scudo][standalone] Add more stats to mallinfo Summary: Android requires additional stats in mallinfo. While we can provide right away the number of bytes mapped (Primary+Secondary), there was no way to get the number of free bytes (only makes sense for the Primary since the Secondary unmaps everything on deallocation). An approximation could be `StatMapped - StatAllocated`, but since we are mapping in `1<<17` increments for the 64-bit Primary, it's fairly inaccurate. So we introduce `StatFree` (note it's `Free`, not `Freed`!), which keeps track of the amount of Primary blocks currently unallocated. Reviewers: cferris, eugenis, vitalybuka, hctim, morehouse Reviewed By: morehouse Subscribers: delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D66112 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@368866 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/scudo/standalone/local_cache.h | 2 ++ lib/scudo/standalone/mutex.h | 8 ++++---- lib/scudo/standalone/primary32.h | 2 ++ lib/scudo/standalone/primary64.h | 1 + lib/scudo/standalone/stats.h | 2 +- lib/scudo/standalone/tests/wrappers_c_test.cpp | 18 +++++++++++++++++- lib/scudo/standalone/wrappers_c.inc | 9 +++++++++ 7 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/scudo/standalone/local_cache.h b/lib/scudo/standalone/local_cache.h index f1c74cf5b5..b08abd3e5d 100644 --- a/lib/scudo/standalone/local_cache.h +++ b/lib/scudo/standalone/local_cache.h @@ -83,6 +83,7 @@ template struct SizeClassAllocatorLocalCache { // performance. It definitely decreases performance on Android though. // if (!SCUDO_ANDROID) PREFETCH(P); Stats.add(StatAllocated, ClassSize); + Stats.sub(StatFree, ClassSize); return P; } @@ -98,6 +99,7 @@ template struct SizeClassAllocatorLocalCache { const uptr ClassSize = C->ClassSize; C->Chunks[C->Count++] = P; Stats.sub(StatAllocated, ClassSize); + Stats.add(StatFree, ClassSize); } void drain() { diff --git a/lib/scudo/standalone/mutex.h b/lib/scudo/standalone/mutex.h index 8cad4195c7..b26b2df066 100644 --- a/lib/scudo/standalone/mutex.h +++ b/lib/scudo/standalone/mutex.h @@ -27,10 +27,10 @@ class HybridMutex { NOINLINE void lock() { if (LIKELY(tryLock())) return; - // The compiler may try to fully unroll the loop, ending up in a - // NumberOfTries*NumberOfYields block of pauses mixed with tryLocks. This - // is large, ugly and unneeded, a compact loop is better for our purpose - // here. Use a pragma to tell the compiler not to unroll the loop. + // The compiler may try to fully unroll the loop, ending up in a + // NumberOfTries*NumberOfYields block of pauses mixed with tryLocks. This + // is large, ugly and unneeded, a compact loop is better for our purpose + // here. Use a pragma to tell the compiler not to unroll the loop. #ifdef __clang__ #pragma nounroll #endif diff --git a/lib/scudo/standalone/primary32.h b/lib/scudo/standalone/primary32.h index 10f921a8d9..79a11bfc1b 100644 --- a/lib/scudo/standalone/primary32.h +++ b/lib/scudo/standalone/primary32.h @@ -318,6 +318,8 @@ template class SizeClassAllocator32 { } DCHECK(B); DCHECK_GT(B->getCount(), 0); + + C->getStats().add(StatFree, AllocatedUser); Sci->AllocatedUser += AllocatedUser; if (Sci->CanRelease) Sci->ReleaseInfo.LastReleaseAtNs = getMonotonicTime(); diff --git a/lib/scudo/standalone/primary64.h b/lib/scudo/standalone/primary64.h index 0149edc899..fd3709ecb8 100644 --- a/lib/scudo/standalone/primary64.h +++ b/lib/scudo/standalone/primary64.h @@ -309,6 +309,7 @@ template class SizeClassAllocator64 { DCHECK(B); DCHECK_GT(B->getCount(), 0); + C->getStats().add(StatFree, AllocatedUser); Region->AllocatedUser += AllocatedUser; Region->Exhausted = false; if (Region->CanRelease) diff --git a/lib/scudo/standalone/stats.h b/lib/scudo/standalone/stats.h index 1243675622..16ef5b89b8 100644 --- a/lib/scudo/standalone/stats.h +++ b/lib/scudo/standalone/stats.h @@ -17,7 +17,7 @@ namespace scudo { // Memory allocator statistics -enum StatType { StatAllocated, StatMapped, StatCount }; +enum StatType { StatAllocated, StatFree, StatMapped, StatCount }; typedef uptr StatCounters[StatCount]; diff --git a/lib/scudo/standalone/tests/wrappers_c_test.cpp b/lib/scudo/standalone/tests/wrappers_c_test.cpp index 4f3c9aea60..5498c165c0 100644 --- a/lib/scudo/standalone/tests/wrappers_c_test.cpp +++ b/lib/scudo/standalone/tests/wrappers_c_test.cpp @@ -191,7 +191,7 @@ TEST(ScudoWrappersCTest, Realloc) { #define M_PURGE -101 #endif -TEST(ScudoWrappersCTest, Mallopt) { +TEST(ScudoWrappersCTest, MallOpt) { errno = 0; EXPECT_EQ(mallopt(-1000, 1), 0); // mallopt doesn't set errno. @@ -223,3 +223,19 @@ TEST(ScudoWrappersCTest, OtherAlloc) { EXPECT_EQ(valloc(SIZE_MAX), nullptr); } + +TEST(ScudoWrappersCTest, MallInfo) { + const size_t BypassQuarantineSize = 1024U; + + struct mallinfo MI = mallinfo(); + size_t Allocated = MI.uordblks; + void *P = malloc(BypassQuarantineSize); + EXPECT_NE(P, nullptr); + MI = mallinfo(); + EXPECT_GE(static_cast(MI.uordblks), Allocated + BypassQuarantineSize); + EXPECT_GT(static_cast(MI.hblkhd), 0U); + size_t Free = MI.fordblks; + free(P); + MI = mallinfo(); + EXPECT_GE(static_cast(MI.fordblks), Free + BypassQuarantineSize); +} diff --git a/lib/scudo/standalone/wrappers_c.inc b/lib/scudo/standalone/wrappers_c.inc index 2beddc7248..cb2202dced 100644 --- a/lib/scudo/standalone/wrappers_c.inc +++ b/lib/scudo/standalone/wrappers_c.inc @@ -38,8 +38,17 @@ INTERFACE WEAK struct SCUDO_MALLINFO SCUDO_PREFIX(mallinfo)(void) { struct SCUDO_MALLINFO Info = {}; scudo::StatCounters Stats; SCUDO_ALLOCATOR.getStats(Stats); + // Space allocated in mmapped regions (bytes) + Info.hblkhd = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatMapped]); + // Maximum total allocated space (bytes) + Info.usmblks = Info.hblkhd; + // Space in freed fastbin blocks (bytes) + Info.fsmblks = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatFree]); + // Total allocated space (bytes) Info.uordblks = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatAllocated]); + // Total free space (bytes) + Info.fordblks = Info.fsmblks; return Info; }