diff options
author | Explorer09 <explorer09@gmail.com> | 2023-07-29 16:24:12 +0800 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2023-08-18 12:52:28 +0200 |
commit | b416433fbe7ccf935ad4e268396aa423143c2318 (patch) | |
tree | 55a38c76f4f0091ed6b06d7369706bd2f1f6f61e /linux/Platform.c | |
parent | c6fd64fce8502a4e557711abe9fee394763ac52c (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 'linux/Platform.c')
-rw-r--r-- | linux/Platform.c | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/linux/Platform.c b/linux/Platform.c index e0b633ba..c81b3697 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -322,23 +322,26 @@ double Platform_setCPUValues(Meter* this, unsigned int cpu) { v[CPU_METER_KERNEL] = cpuData->systemPeriod / total * 100.0; v[CPU_METER_IRQ] = cpuData->irqPeriod / total * 100.0; v[CPU_METER_SOFTIRQ] = cpuData->softIrqPeriod / total * 100.0; + this->curItems = 5; + v[CPU_METER_STEAL] = cpuData->stealPeriod / total * 100.0; v[CPU_METER_GUEST] = cpuData->guestPeriod / total * 100.0; - v[CPU_METER_IOWAIT] = cpuData->ioWaitPeriod / 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] = cpuData->ioWaitPeriod / total * 100.0; } else { v[CPU_METER_KERNEL] = cpuData->systemAllPeriod / total * 100.0; v[CPU_METER_IRQ] = (cpuData->stealPeriod + cpuData->guestPeriod) / 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] = cpuData->frequency; @@ -842,7 +845,7 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) { } } - if (!now && full && !isnan(capacityLevel)) + if (!now && full && isNonnegative(capacityLevel)) totalRemain += capacityLevel * fullCharge; } else if (type == AC) { @@ -882,12 +885,12 @@ void Platform_getBattery(double* percent, ACPresence* isOnAC) { if (Platform_Battery_method == BAT_PROC) { Platform_Battery_getProcData(percent, isOnAC); - if (isnan(*percent)) + if (!isNonnegative(*percent)) Platform_Battery_method = BAT_SYS; } if (Platform_Battery_method == BAT_SYS) { Platform_Battery_getSysData(percent, isOnAC); - if (isnan(*percent)) + if (!isNonnegative(*percent)) Platform_Battery_method = BAT_ERR; } if (Platform_Battery_method == BAT_ERR) { |