Fix a case where the usertime calculation can overflow (see issue #202)

This commit is contained in:
Patrick Marlier 2015-08-30 21:26:47 +02:00
parent 1d805b36b4
commit b37d4f172f
1 changed files with 7 additions and 5 deletions

View File

@ -695,8 +695,6 @@ 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 (usertime >= cpuData->userTime);
assert (nicetime >= cpuData->niceTime);
assert (systemtime >= cpuData->systemTime); assert (systemtime >= cpuData->systemTime);
assert (idletime >= cpuData->idleTime); assert (idletime >= cpuData->idleTime);
assert (totaltime >= cpuData->totalTime); assert (totaltime >= cpuData->totalTime);
@ -707,8 +705,11 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) {
assert (softIrq >= cpuData->softIrqTime); assert (softIrq >= cpuData->softIrqTime);
assert (steal >= cpuData->stealTime); assert (steal >= cpuData->stealTime);
assert (virtalltime >= cpuData->guestTime); assert (virtalltime >= cpuData->guestTime);
cpuData->userPeriod = usertime - cpuData->userTime; // Since we do a subtraction (usertime - guest) and cputime64_to_clock_t()
cpuData->nicePeriod = nicetime - cpuData->niceTime; // used in /proc/stat rounds down numbers, it can lead to a case where the
// integer overflow.
cpuData->userPeriod = (usertime > cpuData->userTime) ? usertime - cpuData->userTime : 0;
cpuData->nicePeriod = (nicetime > cpuData->niceTime) ? nicetime - cpuData->niceTime : 0;
cpuData->systemPeriod = systemtime - cpuData->systemTime; cpuData->systemPeriod = systemtime - cpuData->systemTime;
cpuData->systemAllPeriod = systemalltime - cpuData->systemAllTime; cpuData->systemAllPeriod = systemalltime - cpuData->systemAllTime;
cpuData->idleAllPeriod = idlealltime - cpuData->idleAllTime; cpuData->idleAllPeriod = idlealltime - cpuData->idleAllTime;
@ -732,7 +733,8 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) {
cpuData->guestTime = virtalltime; cpuData->guestTime = virtalltime;
cpuData->totalTime = totaltime; cpuData->totalTime = totaltime;
} }
double period = (double)this->cpus[0].totalPeriod / cpus; fclose(file); double period = (double)this->cpus[0].totalPeriod / cpus;
fclose(file);
return period; return period;
} }