[compiler-rt] [scudo] Add utilization percentages for stats. (PR #75101)
Christopher Ferris via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 11 15:13:10 PST 2023
https://github.com/cferris1000 updated https://github.com/llvm/llvm-project/pull/75101
>From 8638f8dadd72d9573b64407f49e75cc0f58d6c1e Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Fri, 8 Dec 2023 17:05:44 -0800
Subject: [PATCH 1/2] [scudo] Add utilization percentages for stats.
Refactor the percentage display in the secondary code.
Re-use that to display a utilization percentage when
displaying fragmentation data.
---
compiler-rt/lib/scudo/standalone/common.h | 15 +++++++++++++++
compiler-rt/lib/scudo/standalone/primary32.h | 8 ++++++--
compiler-rt/lib/scudo/standalone/primary64.h | 8 ++++++--
compiler-rt/lib/scudo/standalone/secondary.h | 14 +++++---------
4 files changed, 32 insertions(+), 13 deletions(-)
diff --git a/compiler-rt/lib/scudo/standalone/common.h b/compiler-rt/lib/scudo/standalone/common.h
index 3581c946d1608..544a64ad3bf80 100644
--- a/compiler-rt/lib/scudo/standalone/common.h
+++ b/compiler-rt/lib/scudo/standalone/common.h
@@ -112,6 +112,21 @@ template <typename T> inline void shuffle(T *A, u32 N, u32 *RandState) {
*RandState = State;
}
+inline void computePercentage(uptr Numerator, uptr Denominator, uptr *Integral,
+ uptr *Fractional) {
+ constexpr uptr Digits = 100;
+ if (Denominator == 0) {
+ *Integral = 0;
+ *Fractional = 0;
+ return;
+ }
+
+ *Integral = Numerator * Digits / Denominator;
+ *Fractional =
+ (((Numerator * Digits) % Denominator) * Digits + Denominator / 2) /
+ Denominator;
+}
+
// Platform specific functions.
extern uptr PageSizeCached;
diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h
index 8281e02ba164c..df1caae94dadd 100644
--- a/compiler-rt/lib/scudo/standalone/primary32.h
+++ b/compiler-rt/lib/scudo/standalone/primary32.h
@@ -931,10 +931,14 @@ template <typename Config> class SizeClassAllocator32 {
AllocatedPagesCount - Recorder.getReleasedPagesCount();
const uptr InUseBytes = InUsePages * PageSize;
+ uptr Integral;
+ uptr Fractional;
+ computePercentage(BlockSize * InUseBlocks, InUsePages * PageSize, &Integral,
+ &Fractional);
Str->append(" %02zu (%6zu): inuse/total blocks: %6zu/%6zu inuse/total "
- "pages: %6zu/%6zu inuse bytes: %6zuK\n",
+ "pages: %6zu/%6zu inuse bytes: %6zuK util: %2zu.%02zu%%\n",
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
- AllocatedPagesCount, InUseBytes >> 10);
+ AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
}
NOINLINE uptr releaseToOSMaybe(SizeClassInfo *Sci, uptr ClassId,
diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h
index d1929ff7212f4..d1f175cd15a11 100644
--- a/compiler-rt/lib/scudo/standalone/primary64.h
+++ b/compiler-rt/lib/scudo/standalone/primary64.h
@@ -1130,10 +1130,14 @@ template <typename Config> class SizeClassAllocator64 {
AllocatedPagesCount - Recorder.getReleasedPagesCount();
const uptr InUseBytes = InUsePages * PageSize;
+ uptr Integral;
+ uptr Fractional;
+ computePercentage(BlockSize * InUseBlocks, InUsePages * PageSize, &Integral,
+ &Fractional);
Str->append(" %02zu (%6zu): inuse/total blocks: %6zu/%6zu inuse/total "
- "pages: %6zu/%6zu inuse bytes: %6zuK\n",
+ "pages: %6zu/%6zu inuse bytes: %6zuK util: %2zu.%02zu%%\n",
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
- AllocatedPagesCount, InUseBytes >> 10);
+ AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
}
NOINLINE uptr releaseToOSMaybe(RegionInfo *Region, uptr ClassId,
diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index 8dc4c87fa7c6e..f52a4188bcf3a 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -155,20 +155,16 @@ template <typename Config> class MapAllocatorCache {
void getStats(ScopedString *Str) {
ScopedLock L(Mutex);
- u32 Integral = 0;
- u32 Fractional = 0;
- if (CallsToRetrieve != 0) {
- Integral = SuccessfulRetrieves * 100 / CallsToRetrieve;
- Fractional = (((SuccessfulRetrieves * 100) % CallsToRetrieve) * 100 +
- CallsToRetrieve / 2) /
- CallsToRetrieve;
- }
+ uptr Integral;
+ uptr Fractional;
+ computePercentage(SuccessfulRetrieves, CallsToRetrieve, &Integral,
+ &Fractional);
Str->append("Stats: MapAllocatorCache: EntriesCount: %d, "
"MaxEntriesCount: %u, MaxEntrySize: %zu\n",
EntriesCount, atomic_load_relaxed(&MaxEntriesCount),
atomic_load_relaxed(&MaxEntrySize));
Str->append("Stats: CacheRetrievalStats: SuccessRate: %u/%u "
- "(%u.%02u%%)\n",
+ "(%zu.%02zu%%)\n",
SuccessfulRetrieves, CallsToRetrieve, Integral, Fractional);
for (CachedBlock Entry : Entries) {
if (!Entry.isValid())
>From 10368524e8f08901519b02b0a92575faab574a43 Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Mon, 11 Dec 2023 15:08:19 -0800
Subject: [PATCH 2/2] Change to make denominator be represented as 100%.
---
compiler-rt/lib/scudo/standalone/common.h | 2 +-
compiler-rt/lib/scudo/standalone/primary32.h | 2 +-
compiler-rt/lib/scudo/standalone/primary64.h | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/compiler-rt/lib/scudo/standalone/common.h b/compiler-rt/lib/scudo/standalone/common.h
index 544a64ad3bf80..ae45683f1ee38 100644
--- a/compiler-rt/lib/scudo/standalone/common.h
+++ b/compiler-rt/lib/scudo/standalone/common.h
@@ -116,7 +116,7 @@ inline void computePercentage(uptr Numerator, uptr Denominator, uptr *Integral,
uptr *Fractional) {
constexpr uptr Digits = 100;
if (Denominator == 0) {
- *Integral = 0;
+ *Integral = 100;
*Fractional = 0;
return;
}
diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h
index df1caae94dadd..4d03b282d000d 100644
--- a/compiler-rt/lib/scudo/standalone/primary32.h
+++ b/compiler-rt/lib/scudo/standalone/primary32.h
@@ -936,7 +936,7 @@ template <typename Config> class SizeClassAllocator32 {
computePercentage(BlockSize * InUseBlocks, InUsePages * PageSize, &Integral,
&Fractional);
Str->append(" %02zu (%6zu): inuse/total blocks: %6zu/%6zu inuse/total "
- "pages: %6zu/%6zu inuse bytes: %6zuK util: %2zu.%02zu%%\n",
+ "pages: %6zu/%6zu inuse bytes: %6zuK util: %3zu.%02zu%%\n",
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
}
diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h
index d1f175cd15a11..9a642d23620e1 100644
--- a/compiler-rt/lib/scudo/standalone/primary64.h
+++ b/compiler-rt/lib/scudo/standalone/primary64.h
@@ -1135,7 +1135,7 @@ template <typename Config> class SizeClassAllocator64 {
computePercentage(BlockSize * InUseBlocks, InUsePages * PageSize, &Integral,
&Fractional);
Str->append(" %02zu (%6zu): inuse/total blocks: %6zu/%6zu inuse/total "
- "pages: %6zu/%6zu inuse bytes: %6zuK util: %2zu.%02zu%%\n",
+ "pages: %6zu/%6zu inuse bytes: %6zuK util: %3zu.%02zu%%\n",
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
}
More information about the llvm-commits
mailing list