[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.
This commit is contained in:
Christopher Ferris 2023-12-11 16:42:44 -08:00 committed by GitHub
parent 52ba075571
commit a8ef9c0969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 13 deletions

View File

@ -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 = 100;
*Fractional = 0;
return;
}
*Integral = Numerator * Digits / Denominator;
*Fractional =
(((Numerator * Digits) % Denominator) * Digits + Denominator / 2) /
Denominator;
}
// Platform specific functions.
extern uptr PageSizeCached;

View File

@ -931,10 +931,14 @@ private:
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: %3zu.%02zu%%\n",
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
AllocatedPagesCount, InUseBytes >> 10);
AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
}
NOINLINE uptr releaseToOSMaybe(SizeClassInfo *Sci, uptr ClassId,

View File

@ -1130,10 +1130,14 @@ private:
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: %3zu.%02zu%%\n",
ClassId, BlockSize, InUseBlocks, TotalBlocks, InUsePages,
AllocatedPagesCount, InUseBytes >> 10);
AllocatedPagesCount, InUseBytes >> 10, Integral, Fractional);
}
NOINLINE uptr releaseToOSMaybe(RegionInfo *Region, uptr ClassId,

View File

@ -155,20 +155,16 @@ public:
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())