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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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) {
Copy link
Contributor Author

@Explorer09 Explorer09 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question is a bit pedantic here. Since we plan to use countDigits for memory address, too, the more proper type for this function's input would be uintptr_t and not size_t. May I ask, is it better for this function's type be changed to uintptr_t to better indicate the intent? The output type can remain size_t as I see no reason for changing it.

Technical thing here: size_t and uintptr_t do not necessarily have the same size. In the C standard's sense, sizeof(size_t) <= sizeof(uintptr_t). On flat memory systems they are indeed the same size, but on other platforms (e.g. segmented memory) size_t can be defined to be narrower than uintptr_t.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use uint64_t and there's no problem (except for some potential upcasts for clang).

But I don't think we need to change this here; size_t is fine.

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