src/system/sysload.cpp: fix possible divide by zero bug, stats are not guarantueed to be different for each sample.

Origin commit data
------------------
Branch: ni/coolstream
Commit: 6fdaa869d7
Author: [CST] Bas <bas@coolstreamtech.com>
Date: 2015-02-22 (Sun, 22 Feb 2015)


------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
[CST] Bas
2015-02-22 20:18:07 +08:00
committed by [CST] Focus
parent c16dc96284
commit 567f875f06

View File

@@ -62,8 +62,11 @@ void *cSysLoad::Run(void *arg)
while (getline(in, line)) { while (getline(in, line)) {
unsigned long _stat_user, _stat_nice, _stat_system, _stat_idle; unsigned long _stat_user, _stat_nice, _stat_system, _stat_idle;
if (4 == sscanf(line.c_str(), "cpu %lu %lu %lu %lu", &_stat_user, &_stat_nice, &_stat_system, &_stat_idle)) { if (4 == sscanf(line.c_str(), "cpu %lu %lu %lu %lu", &_stat_user, &_stat_nice, &_stat_system, &_stat_idle)) {
unsigned long _stat_total = _stat_user + _stat_nice + _stat_system + _stat_idle;
if (stat_total) { if (stat_total) {
unsigned long div = _stat_user + _stat_nice + _stat_system + _stat_idle - stat_total; unsigned long div = _stat_total - stat_total;
if (!div) // prevent division by zero if previous stat_total is equal to new.
break;
caller->data_last = (int)(1000 - 1000 * (_stat_idle - stat_idle) / div); caller->data_last = (int)(1000 - 1000 * (_stat_idle - stat_idle) / div);
if (caller->data_avail < caller->data_size) { if (caller->data_avail < caller->data_size) {
caller->data[caller->data_avail++] = caller->data_last; caller->data[caller->data_avail++] = caller->data_last;
@@ -73,7 +76,7 @@ void *cSysLoad::Run(void *arg)
} }
} }
stat_idle = _stat_idle; stat_idle = _stat_idle;
stat_total = _stat_user + _stat_nice + _stat_system + _stat_idle; stat_total = _stat_total;
break; break;
} }
} }