From 9c0e8e9429d232a85baef3d6fa8269963fd80741 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Thu, 3 Mar 2022 00:00:00 -0600 Subject: [PATCH] library: guard against 'inf/nan' for 'cpu utilization' When preparing the ps program for a %CUU field, it was revealed the PIDS_UTILIZATION item may sometimes yield a result of 'nan' or 'inf' for that ps program itself. Apparently, it was caused by the short lived nature of such a one-shot program. And, while this anomaly could be handled in ps, the solution belongs in the library. So, I reworked an item's algorithm to avoid this oops. Signed-off-by: Jim Warner --- proc/pids.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proc/pids.c b/proc/pids.c index 6e5e60a4..e9bae0fa 100644 --- a/proc/pids.c +++ b/proc/pids.c @@ -286,7 +286,7 @@ setDECL(TIME_START) { R->result.real = (double)P->start_time / I->hertz; } REG_set(TTY, s_int, tty) setDECL(TTY_NAME) { char buf[64]; freNAME(str)(R); dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV); if (!(R->result.str = strdup(buf))) I->seterr = 1; } setDECL(TTY_NUMBER) { char buf[64]; freNAME(str)(R); dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV|ABBREV_TTY|ABBREV_PTS); if (!(R->result.str = strdup(buf))) I->seterr = 1; } -setDECL(UTILIZATION) { double t; if (I->boot_seconds > 0) { t = I->boot_seconds - ((double)P->start_time / I->hertz); R->result.real = ((double)P->utime + P->stime) * (100.0f / ((double)I->hertz * t)); }} +setDECL(UTILIZATION) { double t; if (I->boot_seconds > 0) { t = I->boot_seconds - ((double)P->start_time / I->hertz); if (t > 0) R->result.real = ((P->utime + P->stime) * 100.0f / I->hertz) / t; }} REG_set(VM_DATA, ul_int, vm_data) REG_set(VM_EXE, ul_int, vm_exe) REG_set(VM_LIB, ul_int, vm_lib) -- 2.40.0