any of these values may wrap

This commit is contained in:
Hisham 2016-02-29 21:57:03 -03:00
parent 797bcd0961
commit e77811e99b
1 changed files with 14 additions and 22 deletions

View File

@ -721,31 +721,23 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) {
unsigned long long int virtalltime = guest + guestnice; unsigned long long int virtalltime = guest + guestnice;
unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime; unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;
CPUData* cpuData = &(this->cpus[i]); CPUData* cpuData = &(this->cpus[i]);
assert (systemtime >= cpuData->systemTime);
assert (idletime >= cpuData->idleTime);
assert (totaltime >= cpuData->totalTime);
assert (systemalltime >= cpuData->systemAllTime);
assert (idlealltime >= cpuData->idleAllTime);
assert (ioWait >= cpuData->ioWaitTime);
assert (irq >= cpuData->irqTime);
assert (softIrq >= cpuData->softIrqTime);
assert (steal >= cpuData->stealTime);
assert (virtalltime >= cpuData->guestTime);
// Since we do a subtraction (usertime - guest) and cputime64_to_clock_t() // 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 // used in /proc/stat rounds down numbers, it can lead to a case where the
// integer overflow. // integer overflow.
cpuData->userPeriod = (usertime > cpuData->userTime) ? usertime - cpuData->userTime : 0; #define WRAP_SUBTRACT(a,b) (a > b) ? a - b : 0
cpuData->nicePeriod = (nicetime > cpuData->niceTime) ? nicetime - cpuData->niceTime : 0; cpuData->userPeriod = WRAP_SUBTRACT(usertime, cpuData->userTime);
cpuData->systemPeriod = systemtime - cpuData->systemTime; cpuData->nicePeriod = WRAP_SUBTRACT(nicetime, cpuData->niceTime);
cpuData->systemAllPeriod = systemalltime - cpuData->systemAllTime; cpuData->systemPeriod = WRAP_SUBTRACT(systemtime, cpuData->systemTime);
cpuData->idleAllPeriod = idlealltime - cpuData->idleAllTime; cpuData->systemAllPeriod = WRAP_SUBTRACT(systemalltime, cpuData->systemAllTime);
cpuData->idlePeriod = idletime - cpuData->idleTime; cpuData->idleAllPeriod = WRAP_SUBTRACT(idlealltime, cpuData->idleAllTime);
cpuData->ioWaitPeriod = ioWait - cpuData->ioWaitTime; cpuData->idlePeriod = WRAP_SUBTRACT(idletime, cpuData->idleTime);
cpuData->irqPeriod = irq - cpuData->irqTime; cpuData->ioWaitPeriod = WRAP_SUBTRACT(ioWait, cpuData->ioWaitTime);
cpuData->softIrqPeriod = softIrq - cpuData->softIrqTime; cpuData->irqPeriod = WRAP_SUBTRACT(irq, cpuData->irqTime);
cpuData->stealPeriod = steal - cpuData->stealTime; cpuData->softIrqPeriod = WRAP_SUBTRACT(softIrq, cpuData->softIrqTime);
cpuData->guestPeriod = virtalltime - cpuData->guestTime; cpuData->stealPeriod = WRAP_SUBTRACT(steal, cpuData->stealTime);
cpuData->totalPeriod = totaltime - cpuData->totalTime; cpuData->guestPeriod = WRAP_SUBTRACT(virtalltime, cpuData->guestTime);
cpuData->totalPeriod = WRAP_SUBTRACT(totaltime, cpuData->totalTime);
#undef WRAP_SUBTRACT
cpuData->userTime = usertime; cpuData->userTime = usertime;
cpuData->niceTime = nicetime; cpuData->niceTime = nicetime;
cpuData->systemTime = systemtime; cpuData->systemTime = systemtime;