diff options
author | Explorer09 <explorer09@gmail.com> | 2023-08-17 04:21:05 +0800 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2023-08-18 12:52:28 +0200 |
commit | 076b913c7f876ba0489b9751043eeb61aa34680f (patch) | |
tree | 35173363985f959a0e77993b07c3b9fe09a2c1c6 | |
parent | 93d76fd37627a3594f52afda32092fa3858f28dc (diff) |
Compare all percentage fields with compareRealNumbers()
The SPACESHIP_NUMBER() macro does not work well with floating point
values that are possible to be NaNs. Change the compare logic of all
percentage fields of Process entries to use compareRealNumbers().
Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
-rw-r--r-- | Process.c | 2 | ||||
-rw-r--r-- | XUtils.c | 11 | ||||
-rw-r--r-- | XUtils.h | 5 | ||||
-rw-r--r-- | linux/LinuxProcess.c | 16 | ||||
-rw-r--r-- | pcp/PCPDynamicColumn.c | 4 | ||||
-rw-r--r-- | pcp/PCPProcess.c | 16 |
6 files changed, 25 insertions, 29 deletions
@@ -1182,7 +1182,7 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField switch (key) { case PERCENT_CPU: case PERCENT_NORM_CPU: - return SPACESHIP_NUMBER(p1->percent_cpu, p2->percent_cpu); + return compareRealNumbers(p1->percent_cpu, p2->percent_cpu); case PERCENT_MEM: return SPACESHIP_NUMBER(p1->m_resident, p2->m_resident); case COMM: @@ -12,6 +12,7 @@ in the source distribution for its full text. #include <assert.h> #include <errno.h> #include <fcntl.h> +#include <math.h> #include <stdarg.h> #include <stdint.h> #include <stdlib.h> @@ -339,6 +340,16 @@ ssize_t full_write(int fd, const void* buf, size_t count) { return written; } +/* 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. */ +int compareRealNumbers(double a, double b) { + int result = isgreater(a, b) - isgreater(b, a); + if (result) + return result; + return !isNaN(a) - !isNaN(b); +} + /* Computes the sum of all positive floating point values in an array. NaN values in the array are skipped. The returned sum will always be nonnegative. */ @@ -82,6 +82,11 @@ ssize_t xReadfileat(openat_arg_t dirfd, const char* pathname, void* buffer, size ATTR_ACCESS3_R(2, 3) ssize_t full_write(int fd, const void* buf, size_t count); +/* 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. */ +int compareRealNumbers(double a, double b); + /* Computes the sum of all positive floating point values in an array. NaN values in the array are skipped. The returned sum will always be nonnegative. */ diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 0a88644c..87561818 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -310,16 +310,6 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces RichString_appendAscii(str, attr, buffer); } -/* 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 LinuxProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) { const LinuxProcess* p1 = (const LinuxProcess*)v1; const LinuxProcess* p2 = (const LinuxProcess*)v2; @@ -385,11 +375,11 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce return SPACESHIP_NUMBER(p1->oom, p2->oom); #ifdef HAVE_DELAYACCT case PERCENT_CPU_DELAY: - return SPACESHIP_NUMBER(p1->cpu_delay_percent, p2->cpu_delay_percent); + return compareRealNumbers(p1->cpu_delay_percent, p2->cpu_delay_percent); case PERCENT_IO_DELAY: - return SPACESHIP_NUMBER(p1->blkio_delay_percent, p2->blkio_delay_percent); + return compareRealNumbers(p1->blkio_delay_percent, p2->blkio_delay_percent); case PERCENT_SWAP_DELAY: - return SPACESHIP_NUMBER(p1->swapin_delay_percent, p2->swapin_delay_percent); + return compareRealNumbers(p1->swapin_delay_percent, p2->swapin_delay_percent); #endif case IO_PRIORITY: return SPACESHIP_NUMBER(LinuxProcess_effectiveIOPriority(p1), LinuxProcess_effectiveIOPriority(p2)); diff --git a/pcp/PCPDynamicColumn.c b/pcp/PCPDynamicColumn.c index 23b896e6..2481d81d 100644 --- a/pcp/PCPDynamicColumn.c +++ b/pcp/PCPDynamicColumn.c @@ -338,9 +338,9 @@ int PCPDynamicColumn_compareByKey(const PCPProcess* p1, const PCPProcess* p2, Pr case PM_TYPE_U64: return SPACESHIP_NUMBER(atom2.ull, atom1.ull); case PM_TYPE_FLOAT: - return SPACESHIP_NUMBER(atom2.f, atom1.f); + return compareRealNumbers(atom2.f, atom1.f); case PM_TYPE_DOUBLE: - return SPACESHIP_NUMBER(atom2.d, atom1.d); + return compareRealNumbers(atom2.d, atom1.d); default: break; } diff --git a/pcp/PCPProcess.c b/pcp/PCPProcess.c index 2cb27814..eadc9eb4 100644 --- a/pcp/PCPProcess.c +++ b/pcp/PCPProcess.c @@ -199,16 +199,6 @@ static void PCPProcess_writeField(const Process* this, RichString* str, ProcessF RichString_appendWide(str, attr, buffer); } -/* 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) { const PCPProcess* p1 = (const PCPProcess*)v1; const PCPProcess* p2 = (const PCPProcess*)v2; @@ -263,11 +253,11 @@ static int PCPProcess_compareByKey(const Process* v1, const Process* v2, Process case OOM: return SPACESHIP_NUMBER(p1->oom, p2->oom); case PERCENT_CPU_DELAY: - return SPACESHIP_NUMBER(p1->cpu_delay_percent, p2->cpu_delay_percent); + return compareRealNumbers(p1->cpu_delay_percent, p2->cpu_delay_percent); case PERCENT_IO_DELAY: - return SPACESHIP_NUMBER(p1->blkio_delay_percent, p2->blkio_delay_percent); + return compareRealNumbers(p1->blkio_delay_percent, p2->blkio_delay_percent); case PERCENT_SWAP_DELAY: - return SPACESHIP_NUMBER(p1->swapin_delay_percent, p2->swapin_delay_percent); + return compareRealNumbers(p1->swapin_delay_percent, p2->swapin_delay_percent); case CTXT: return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff); case SECATTR: |