Skip to content

Commit a8ef9c0

Browse files
authored
[scudo] Add utilization percentages for stats. (#75101)
Refactor the percentage display in the secondary code. Re-use that to display a utilization percentage when displaying fragmentation data.
1 parent 52ba075 commit a8ef9c0

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

compiler-rt/lib/scudo/standalone/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ template <typename T> inline void shuffle(T *A, u32 N, u32 *RandState) {
112112
*RandState = State;
113113
}
114114

115+
inline void computePercentage(uptr Numerator, uptr Denominator, uptr *Integral,
116+
uptr *Fractional) {
117+
constexpr uptr Digits = 100;
118+
if (Denominator == 0) {
119+
*Integral = 100;
120+
*Fractional = 0;
121+
return;
122+
}
123+
124+
*Integral = Numerator * Digits / Denominator;
125+
*Fractional =
126+
(((Numerator * Digits) % Denominator) * Digits + Denominator / 2) /
127+
Denominator;
128+
}
129+
115130
// Platform specific functions.
116131

117132
extern uptr PageSizeCached;

compiler-rt/lib/scudo/standalone/primary32.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,10 +931,14 @@ template <typename Config> class SizeClassAllocator32 {
931931
AllocatedPagesCount - Recorder.getReleasedPagesCount();
932932
const uptr InUseBytes = InUsePages * PageSize;
933933

934+
uptr Integral;
935+
uptr Fractional;
936+
computePercentage(BlockSize * InUseBlocks, InUsePages * PageSize, &Integral,
937+
&Fractional);
934938
Str->append(" %02zu (%6zu): inuse/total blocks: %6zu/%6zu inuse/total "
935-
"pages: %6zu/%6zu inuse bytes: %6zuK\n",
939+
"pages: %6zu/%6zu inuse bytes: %6zuK util: %3zu.%02zu%%\n",
936940
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
937-
AllocatedPagesCount, InUseBytes >> 10);
941+
AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
938942
}
939943

940944
NOINLINE uptr releaseToOSMaybe(SizeClassInfo *Sci, uptr ClassId,

compiler-rt/lib/scudo/standalone/primary64.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,10 +1130,14 @@ template <typename Config> class SizeClassAllocator64 {
11301130
AllocatedPagesCount - Recorder.getReleasedPagesCount();
11311131
const uptr InUseBytes = InUsePages * PageSize;
11321132

1133+
uptr Integral;
1134+
uptr Fractional;
1135+
computePercentage(BlockSize * InUseBlocks, InUsePages * PageSize, &Integral,
1136+
&Fractional);
11331137
Str->append(" %02zu (%6zu): inuse/total blocks: %6zu/%6zu inuse/total "
1134-
"pages: %6zu/%6zu inuse bytes: %6zuK\n",
1138+
"pages: %6zu/%6zu inuse bytes: %6zuK util: %3zu.%02zu%%\n",
11351139
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
1136-
AllocatedPagesCount, InUseBytes >> 10);
1140+
AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
11371141
}
11381142

11391143
NOINLINE uptr releaseToOSMaybe(RegionInfo *Region, uptr ClassId,

compiler-rt/lib/scudo/standalone/secondary.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,16 @@ template <typename Config> class MapAllocatorCache {
155155

156156
void getStats(ScopedString *Str) {
157157
ScopedLock L(Mutex);
158-
u32 Integral = 0;
159-
u32 Fractional = 0;
160-
if (CallsToRetrieve != 0) {
161-
Integral = SuccessfulRetrieves * 100 / CallsToRetrieve;
162-
Fractional = (((SuccessfulRetrieves * 100) % CallsToRetrieve) * 100 +
163-
CallsToRetrieve / 2) /
164-
CallsToRetrieve;
165-
}
158+
uptr Integral;
159+
uptr Fractional;
160+
computePercentage(SuccessfulRetrieves, CallsToRetrieve, &Integral,
161+
&Fractional);
166162
Str->append("Stats: MapAllocatorCache: EntriesCount: %d, "
167163
"MaxEntriesCount: %u, MaxEntrySize: %zu\n",
168164
EntriesCount, atomic_load_relaxed(&MaxEntriesCount),
169165
atomic_load_relaxed(&MaxEntrySize));
170166
Str->append("Stats: CacheRetrievalStats: SuccessRate: %u/%u "
171-
"(%u.%02u%%)\n",
167+
"(%zu.%02zu%%)\n",
172168
SuccessfulRetrieves, CallsToRetrieve, Integral, Fractional);
173169
for (CachedBlock Entry : Entries) {
174170
if (!Entry.isValid())

0 commit comments

Comments
 (0)