[compiler-rt] r368866 - [scudo][standalone] Add more stats to mallinfo

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 09:04:01 PDT 2019


Author: cryptoad
Date: Wed Aug 14 09:04:01 2019
New Revision: 368866

URL: http://llvm.org/viewvc/llvm-project?rev=368866&view=rev
Log:
[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

Modified:
    compiler-rt/trunk/lib/scudo/standalone/local_cache.h
    compiler-rt/trunk/lib/scudo/standalone/mutex.h
    compiler-rt/trunk/lib/scudo/standalone/primary32.h
    compiler-rt/trunk/lib/scudo/standalone/primary64.h
    compiler-rt/trunk/lib/scudo/standalone/stats.h
    compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp
    compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc

Modified: compiler-rt/trunk/lib/scudo/standalone/local_cache.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/local_cache.h?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/local_cache.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/local_cache.h Wed Aug 14 09:04:01 2019
@@ -83,6 +83,7 @@ template <class SizeClassAllocator> stru
     // 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 <class SizeClassAllocator> stru
     const uptr ClassSize = C->ClassSize;
     C->Chunks[C->Count++] = P;
     Stats.sub(StatAllocated, ClassSize);
+    Stats.add(StatFree, ClassSize);
   }
 
   void drain() {

Modified: compiler-rt/trunk/lib/scudo/standalone/mutex.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/mutex.h?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/mutex.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/mutex.h Wed Aug 14 09:04:01 2019
@@ -27,10 +27,10 @@ public:
   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

Modified: compiler-rt/trunk/lib/scudo/standalone/primary32.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/primary32.h?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/primary32.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/primary32.h Wed Aug 14 09:04:01 2019
@@ -318,6 +318,8 @@ private:
     }
     DCHECK(B);
     DCHECK_GT(B->getCount(), 0);
+
+    C->getStats().add(StatFree, AllocatedUser);
     Sci->AllocatedUser += AllocatedUser;
     if (Sci->CanRelease)
       Sci->ReleaseInfo.LastReleaseAtNs = getMonotonicTime();

Modified: compiler-rt/trunk/lib/scudo/standalone/primary64.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/primary64.h?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/primary64.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/primary64.h Wed Aug 14 09:04:01 2019
@@ -309,6 +309,7 @@ private:
     DCHECK(B);
     DCHECK_GT(B->getCount(), 0);
 
+    C->getStats().add(StatFree, AllocatedUser);
     Region->AllocatedUser += AllocatedUser;
     Region->Exhausted = false;
     if (Region->CanRelease)

Modified: compiler-rt/trunk/lib/scudo/standalone/stats.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/stats.h?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/stats.h (original)
+++ compiler-rt/trunk/lib/scudo/standalone/stats.h Wed Aug 14 09:04:01 2019
@@ -17,7 +17,7 @@
 namespace scudo {
 
 // Memory allocator statistics
-enum StatType { StatAllocated, StatMapped, StatCount };
+enum StatType { StatAllocated, StatFree, StatMapped, StatCount };
 
 typedef uptr StatCounters[StatCount];
 

Modified: compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp (original)
+++ compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp Wed Aug 14 09:04:01 2019
@@ -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<size_t>(MI.uordblks), Allocated + BypassQuarantineSize);
+  EXPECT_GT(static_cast<size_t>(MI.hblkhd), 0U);
+  size_t Free = MI.fordblks;
+  free(P);
+  MI = mallinfo();
+  EXPECT_GE(static_cast<size_t>(MI.fordblks), Free + BypassQuarantineSize);
+}

Modified: compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc?rev=368866&r1=368865&r2=368866&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc (original)
+++ compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc Wed Aug 14 09:04:01 2019
@@ -38,8 +38,17 @@ INTERFACE WEAK struct SCUDO_MALLINFO SCU
   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;
 }
 




More information about the llvm-commits mailing list