summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Poszwa <old4@o2.pl>2012-06-28 11:22:00 +0200
committerMichael Stapelberg <michael@stapelberg.de>2012-06-28 12:05:01 +0200
commit859a7690a6472ca6d700d29693d986125d11e4b6 (patch)
treeea3007e28cf8c8da8aa48cec14ad6a4b6c33be8b
parent10ed9efff45c3d267f0602f1f2310d2a35b18853 (diff)
Avoid division by zero when calculating CPU usage.
-rw-r--r--src/print_cpu_usage.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c
index 7820840..ed17db3 100644
--- a/src/print_cpu_usage.c
+++ b/src/print_cpu_usage.c
@@ -40,7 +40,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
curr_total = curr_user + curr_nice + curr_system + curr_idle;
diff_idle = curr_idle - prev_idle;
diff_total = curr_total - prev_total;
- diff_usage = (1000 * (diff_total - diff_idle)/diff_total + 5)/10;
+ diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
prev_total = curr_total;
prev_idle = curr_idle;
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
@@ -71,7 +71,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
curr_total = curr_user + curr_nice + curr_system + curr_idle;
diff_idle = curr_idle - prev_idle;
diff_total = curr_total - prev_total;
- diff_usage = (1000 * (diff_total - diff_idle)/diff_total + 5)/10;
+ diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
prev_total = curr_total;
prev_idle = curr_idle;
#else