summaryrefslogtreecommitdiffstats
path: root/pcp
diff options
context:
space:
mode:
authorExplorer09 <explorer09@gmail.com>2023-07-29 16:24:12 +0800
committerBenBE <BenBE@geshi.org>2023-08-18 12:52:28 +0200
commitb416433fbe7ccf935ad4e268396aa423143c2318 (patch)
tree55a38c76f4f0091ed6b06d7369706bd2f1f6f61e /pcp
parentc6fd64fce8502a4e557711abe9fee394763ac52c (diff)
Replace isnan() with better comparisons (isgreater(), etc.)
The standard isnan() function is defined to never throw FP exceptions even when the argument is a "signaling" NaN. This makes isnan() more expensive than (x != x) expression unless the compiler flag '-fno-signaling-nans' is given. Introduce functions isNaN(), isNonnegative(), isPositive(), sumPositiveValues() and compareRealNumbers(), and replace isnan() in htop's codebase with the new functions. These functions utilize isgreater() and isgreaterequal() comparisons, which do not throw FP exceptions on "quiet" NaNs, which htop uses extensively. With isnan() removed, there is no need to suppress the warning '-Wno-c11-extensions' in FreeBSD. Remove the code from 'configure.ac'. Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Diffstat (limited to 'pcp')
-rw-r--r--pcp/PCPProcess.c52
-rw-r--r--pcp/Platform.c20
2 files changed, 40 insertions, 32 deletions
diff --git a/pcp/PCPProcess.c b/pcp/PCPProcess.c
index 5e025a1c..2cb27814 100644
--- a/pcp/PCPProcess.c
+++ b/pcp/PCPProcess.c
@@ -104,13 +104,26 @@ void Process_delete(Object* cast) {
}
static void PCPProcess_printDelay(float delay_percent, char* buffer, size_t n) {
- if (isnan(delay_percent)) {
- xSnprintf(buffer, n, " N/A ");
- } else {
+ if (isNonnegative(delay_percent)) {
xSnprintf(buffer, n, "%4.1f ", delay_percent);
+ } else {
+ xSnprintf(buffer, n, " N/A ");
}
}
+static double PCPProcess_totalIORate(const PCPProcess* pp) {
+ double totalRate = NAN;
+ if (isNonnegative(pp->io_rate_read_bps)) {
+ totalRate = pp->io_rate_read_bps;
+ if (isNonnegative(pp->io_rate_write_bps)) {
+ totalRate += pp->io_rate_write_bps;
+ }
+ } else if (isNonnegative(pp->io_rate_write_bps)) {
+ totalRate = pp->io_rate_write_bps;
+ }
+ return totalRate;
+}
+
static void PCPProcess_writeField(const Process* this, RichString* str, ProcessField field) {
const PCPProcess* pp = (const PCPProcess*) this;
bool coloring = this->host->settings->highlightMegabytes;
@@ -141,19 +154,7 @@ static void PCPProcess_writeField(const Process* this, RichString* str, ProcessF
case CNCLWB: Process_printBytes(str, pp->io_cancelled_write_bytes, coloring); return;
case IO_READ_RATE: Process_printRate(str, pp->io_rate_read_bps, coloring); return;
case IO_WRITE_RATE: Process_printRate(str, pp->io_rate_write_bps, coloring); return;
- case IO_RATE: {
- double totalRate = NAN;
- if (!isnan(pp->io_rate_read_bps) && !isnan(pp->io_rate_write_bps))
- totalRate = pp->io_rate_read_bps + pp->io_rate_write_bps;
- else if (!isnan(pp->io_rate_read_bps))
- totalRate = pp->io_rate_read_bps;
- else if (!isnan(pp->io_rate_write_bps))
- totalRate = pp->io_rate_write_bps;
- else
- totalRate = NAN;
- Process_printRate(str, totalRate, coloring);
- return;
- }
+ case IO_RATE: Process_printRate(str, PCPProcess_totalIORate(pp), coloring); return;
case CGROUP: xSnprintf(buffer, n, "%-10s ", pp->cgroup ? pp->cgroup : ""); break;
case OOM: xSnprintf(buffer, n, "%4u ", pp->oom); break;
case PERCENT_CPU_DELAY:
@@ -198,11 +199,14 @@ static void PCPProcess_writeField(const Process* this, RichString* str, ProcessF
RichString_appendWide(str, attr, buffer);
}
-static double adjustNaN(double num) {
- if (isnan(num))
- return -0.0005;
-
- return num;
+/* Compares floating point values for ordering data entries. In this function,
+ NaN is considered "less than" any other floating point value (regardless of
+ sign), and two NaNs are considered "equal" regardless of payload. */
+static int compareRealNumbers(double a, double b) {
+ int result = isgreater(a, b) - isgreater(b, a);
+ if (result)
+ return result;
+ return !isNaN(a) - !isNaN(b);
}
static int PCPProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
@@ -249,11 +253,11 @@ static int PCPProcess_compareByKey(const Process* v1, const Process* v2, Process
case CNCLWB:
return SPACESHIP_NUMBER(p1->io_cancelled_write_bytes, p2->io_cancelled_write_bytes);
case IO_READ_RATE:
- return SPACESHIP_NUMBER(adjustNaN(p1->io_rate_read_bps), adjustNaN(p2->io_rate_read_bps));
+ return compareRealNumbers(p1->io_rate_read_bps, p2->io_rate_read_bps);
case IO_WRITE_RATE:
- return SPACESHIP_NUMBER(adjustNaN(p1->io_rate_write_bps), adjustNaN(p2->io_rate_write_bps));
+ return compareRealNumbers(p1->io_rate_write_bps, p2->io_rate_write_bps);
case IO_RATE:
- return SPACESHIP_NUMBER(adjustNaN(p1->io_rate_read_bps) + adjustNaN(p1->io_rate_write_bps), adjustNaN(p2->io_rate_read_bps) + adjustNaN(p2->io_rate_write_bps));
+ return compareRealNumbers(PCPProcess_totalIORate(p1), PCPProcess_totalIORate(p2));
case CGROUP:
return SPACESHIP_NULLSTR(p1->cgroup, p2->cgroup);
case OOM:
diff --git a/pcp/Platform.c b/pcp/Platform.c
index 12e0f4d7..13746fc2 100644
--- a/pcp/Platform.c
+++ b/pcp/Platform.c
@@ -497,24 +497,28 @@ static double Platform_setOneCPUValues(Meter* this, const Settings* settings, pm
v[CPU_METER_KERNEL] = values[CPU_SYSTEM_PERIOD].ull / total * 100.0;
v[CPU_METER_IRQ] = values[CPU_IRQ_PERIOD].ull / total * 100.0;
v[CPU_METER_SOFTIRQ] = values[CPU_SOFTIRQ_PERIOD].ull / total * 100.0;
+ this->curItems = 5;
+
v[CPU_METER_STEAL] = values[CPU_STEAL_PERIOD].ull / total * 100.0;
v[CPU_METER_GUEST] = values[CPU_GUEST_PERIOD].ull / total * 100.0;
- v[CPU_METER_IOWAIT] = values[CPU_IOWAIT_PERIOD].ull / total * 100.0;
- this->curItems = 8;
- percent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL] + v[CPU_METER_IRQ] + v[CPU_METER_SOFTIRQ];
if (settings->accountGuestInCPUMeter) {
- percent += v[CPU_METER_STEAL] + v[CPU_METER_GUEST];
+ this->curItems = 7;
}
+
+ v[CPU_METER_IOWAIT] = values[CPU_IOWAIT_PERIOD].ull / total * 100.0;
} else {
v[CPU_METER_KERNEL] = values[CPU_SYSTEM_ALL_PERIOD].ull / total * 100.0;
value = values[CPU_STEAL_PERIOD].ull + values[CPU_GUEST_PERIOD].ull;
v[CPU_METER_IRQ] = value / total * 100.0;
this->curItems = 4;
- percent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL] + v[CPU_METER_IRQ];
}
- percent = CLAMP(percent, 0.0, 100.0);
- if (isnan(percent))
- percent = 0.0;
+
+ percent = sumPositiveValues(v, this->curItems);
+ percent = MINIMUM(percent, 100.0);
+
+ if (settings->detailedCPUTime) {
+ this->curItems = 8;
+ }
v[CPU_METER_FREQUENCY] = values[CPU_FREQUENCY].d;
v[CPU_METER_TEMPERATURE] = NAN;

© 2014-2024 Faster IT GmbH | imprint | privacy policy