diff --git a/Macros.h b/Macros.h index 1fdfb1ed..14249f3d 100644 --- a/Macros.h +++ b/Macros.h @@ -69,7 +69,7 @@ #define IGNORE_WCASTQUAL_END #endif -/* This subtraction is used by NetBSD / OpenBSD for calculation of CPU usage items. */ +/* This subtraction is used by Linux / NetBSD / OpenBSD for calculation of CPU usage items. */ static inline unsigned long long saturatingSub(unsigned long long a, unsigned long long b) { return a > b ? a - b : 0; } diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 356bb759..7452c199 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -1811,20 +1811,18 @@ static inline double LinuxProcessList_scanCPUTime(ProcessList* super) { // Since we do a subtraction (usertime - guest) and cputime64_to_clock_t() // used in /proc/stat rounds down numbers, it can lead to a case where the // integer overflow. - #define WRAP_SUBTRACT(a,b) (((a) > (b)) ? (a) - (b) : 0) - cpuData->userPeriod = WRAP_SUBTRACT(usertime, cpuData->userTime); - cpuData->nicePeriod = WRAP_SUBTRACT(nicetime, cpuData->niceTime); - cpuData->systemPeriod = WRAP_SUBTRACT(systemtime, cpuData->systemTime); - cpuData->systemAllPeriod = WRAP_SUBTRACT(systemalltime, cpuData->systemAllTime); - cpuData->idleAllPeriod = WRAP_SUBTRACT(idlealltime, cpuData->idleAllTime); - cpuData->idlePeriod = WRAP_SUBTRACT(idletime, cpuData->idleTime); - cpuData->ioWaitPeriod = WRAP_SUBTRACT(ioWait, cpuData->ioWaitTime); - cpuData->irqPeriod = WRAP_SUBTRACT(irq, cpuData->irqTime); - cpuData->softIrqPeriod = WRAP_SUBTRACT(softIrq, cpuData->softIrqTime); - cpuData->stealPeriod = WRAP_SUBTRACT(steal, cpuData->stealTime); - cpuData->guestPeriod = WRAP_SUBTRACT(virtalltime, cpuData->guestTime); - cpuData->totalPeriod = WRAP_SUBTRACT(totaltime, cpuData->totalTime); - #undef WRAP_SUBTRACT + cpuData->userPeriod = saturatingSub(usertime, cpuData->userTime); + cpuData->nicePeriod = saturatingSub(nicetime, cpuData->niceTime); + cpuData->systemPeriod = saturatingSub(systemtime, cpuData->systemTime); + cpuData->systemAllPeriod = saturatingSub(systemalltime, cpuData->systemAllTime); + cpuData->idleAllPeriod = saturatingSub(idlealltime, cpuData->idleAllTime); + cpuData->idlePeriod = saturatingSub(idletime, cpuData->idleTime); + cpuData->ioWaitPeriod = saturatingSub(ioWait, cpuData->ioWaitTime); + cpuData->irqPeriod = saturatingSub(irq, cpuData->irqTime); + cpuData->softIrqPeriod = saturatingSub(softIrq, cpuData->softIrqTime); + cpuData->stealPeriod = saturatingSub(steal, cpuData->stealTime); + cpuData->guestPeriod = saturatingSub(virtalltime, cpuData->guestTime); + cpuData->totalPeriod = saturatingSub(totaltime, cpuData->totalTime); cpuData->userTime = usertime; cpuData->niceTime = nicetime; cpuData->systemTime = systemtime;