diff options
author | Explorer09 <explorer09@gmail.com> | 2023-07-29 03:32:53 +0800 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2023-08-18 12:52:28 +0200 |
commit | c6fd64fce8502a4e557711abe9fee394763ac52c (patch) | |
tree | 7829bd52c6344efb98d2f784e06cf3674d07e6d9 /pcp | |
parent | 47ae966660612a449b7313273ac3d3f1c0d46796 (diff) |
More robust logic for CPU process percentages (Linux & PCP)
* Use saturatingSub() when subtracting CPU time and I/O read/write
bytes of processes so that the values would never go negative (causing unsigned integer wraparound).
* The CPU percentages of processes are now NaN if the time interval
between measures (the "delta time") is zero. Make the conditional
explicit and avoid division by zero. Previously the percentage values
would be 0.0.
Note: The saturatingSub() function is not used in cpu_delay_percent,
blkio_delay_percent and swapin_delay_percent with a comment added that
explains the reason.
Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Diffstat (limited to 'pcp')
-rw-r--r-- | pcp/PCPProcessList.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/pcp/PCPProcessList.c b/pcp/PCPProcessList.c index 0dac2ad9..1a2d89ed 100644 --- a/pcp/PCPProcessList.c +++ b/pcp/PCPProcessList.c @@ -376,9 +376,11 @@ static bool PCPProcessList_updateProcesses(PCPProcessList* this) { if (tty_nr != proc->tty_nr) PCPProcessList_updateTTY(proc, pid, offset); - float percent_cpu = (pp->utime + pp->stime - lasttimes) / phost->period * 100.0; - proc->percent_cpu = isnan(percent_cpu) ? - 0.0 : CLAMP(percent_cpu, 0.0, host->activeCPUs * 100.0); + proc->percent_cpu = NAN; + if (phost->period > 0.0) { + float percent_cpu = saturatingSub(pp->utime + pp->stime, lasttimes) / phost->period * 100.0; + proc->percent_cpu = MINIMUM(percent_cpu, host->activeCPUs * 100.0F); + } proc->percent_mem = proc->m_resident / (double) host->totalMem * 100.0; Process_updateCPUFieldWidths(proc->percent_cpu); |