Replaces WRAP_SUBTRACT with saturatingSub inline function to reduce code duplication.

This commit is contained in:
fraggerfox 2021-06-12 14:22:57 +05:30 committed by BenBE
parent 2f5b3ef733
commit 3770769ed1
2 changed files with 13 additions and 15 deletions

View File

@ -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;
}

View File

@ -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;