Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce countDigits() function for PID and UID field widths #1561

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Row.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void Row_setPidColumnWidth(pid_t maxPid) {
return;
}

Row_pidDigits = (int)log10(maxPid) + 1;
Row_pidDigits = countDigits((size_t)maxPid, 10);
assert(Row_pidDigits <= ROW_MAX_PID_DIGITS);
}

Expand All @@ -99,7 +99,7 @@ void Row_setUidColumnWidth(uid_t maxUid) {
return;
}

Row_uidDigits = (int)log10(maxUid) + 1;
Row_uidDigits = countDigits((size_t)maxUid, 10);
assert(Row_uidDigits <= ROW_MAX_UID_DIGITS);
}

Expand Down
15 changes: 15 additions & 0 deletions XUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ double sumPositiveValues(const double* array, size_t count) {
return sum;
}

/* Counts the number of digits needed to print "n" with a given base.
If "n" is zero, returns 1. This function expects small numbers to
appear often, hence it uses a O(log(n)) time algorithm. */
size_t countDigits(size_t n, size_t base) {
BenBE marked this conversation as resolved.
Show resolved Hide resolved
assert(base > 1);
size_t res = 1;
for (size_t limit = base; n >= limit; limit *= base) {
res++;
if (base && limit > SIZE_MAX / base) {
break;
}
}
return res;
}

#if !defined(HAVE_BUILTIN_CTZ)
// map a bit value mod 37 to its position
static const uint8_t mod37BitPosition[] = {
Expand Down
5 changes: 5 additions & 0 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ int compareRealNumbers(double a, double b);
ATTR_NONNULL ATTR_ACCESS3_R(1, 2)
double sumPositiveValues(const double* array, size_t count);

/* Counts the number of digits needed to print "n" with a given base.
If "n" is zero, returns 1. This function expects small numbers to
appear often, hence it uses a O(log(n)) time algorithm. */
size_t countDigits(size_t n, size_t base);

/* Returns the number of trailing zero bits */
#if defined(HAVE_BUILTIN_CTZ)
static inline unsigned int countTrailingZeros(unsigned int x) {
Expand Down
2 changes: 0 additions & 2 deletions linux/GPU.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ void GPU_readProcessData(LinuxProcessTable* lpt, LinuxProcess* lp, openat_arg_t
unsigned long long int gputimeDelta;
uint64_t monotonicTimeDelta;

Row_updateFieldWidth(GPU_TIME, ceil(log10(new_gpu_time)));

gputimeDelta = saturatingSub(new_gpu_time, lp->gpu_time);
monotonicTimeDelta = host->monotonicMs - host->prevMonotonicMs;
lp->gpu_percent = 100.0F * gputimeDelta / (1000 * 1000) / monotonicTimeDelta;
Expand Down
Loading