diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2015-10-26 17:55:13 -0400 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2015-10-26 17:55:13 -0400 |
commit | b669540e4e5b62f639146f6c373c131319631e6e (patch) | |
tree | 98e2948d52b69d819ffa62042d4440ce17975bc9 | |
parent | ea8a8b2d6be1db4e1aeefde5f38e4316527e8000 (diff) | |
parent | b37d4f172f048a67cbc3cb6bbef00b67212ddb88 (diff) |
Merge pull request #298 from patrickmarlier/issue202
Fix a case where the usertime calculation can overflow (see issue #202)
-rw-r--r-- | linux/LinuxProcessList.c | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 46902aff..92259514 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -695,8 +695,6 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) { unsigned long long int virtalltime = guest + guestnice; unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime; CPUData* cpuData = &(this->cpus[i]); - assert (usertime >= cpuData->userTime); - assert (nicetime >= cpuData->niceTime); assert (systemtime >= cpuData->systemTime); assert (idletime >= cpuData->idleTime); assert (totaltime >= cpuData->totalTime); @@ -707,8 +705,11 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) { assert (softIrq >= cpuData->softIrqTime); assert (steal >= cpuData->stealTime); assert (virtalltime >= cpuData->guestTime); - cpuData->userPeriod = usertime - cpuData->userTime; - cpuData->nicePeriod = nicetime - cpuData->niceTime; + // 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. + cpuData->userPeriod = (usertime > cpuData->userTime) ? usertime - cpuData->userTime : 0; + cpuData->nicePeriod = (nicetime > cpuData->niceTime) ? nicetime - cpuData->niceTime : 0; cpuData->systemPeriod = systemtime - cpuData->systemTime; cpuData->systemAllPeriod = systemalltime - cpuData->systemAllTime; cpuData->idleAllPeriod = idlealltime - cpuData->idleAllTime; @@ -732,7 +733,8 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) { cpuData->guestTime = virtalltime; 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; } |