diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2013-08-18 15:56:22 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2013-08-18 15:57:47 +0100 |
commit | 6a64ee938b90a2d27342aa42ba1d2d90da40dc7b (patch) | |
tree | 6394cf98a9b9701805d11415e96facbca5a9287a /overlay/cpu-top.c | |
parent | 98572f0446e62eb889f28efe39fc1501e96093c1 (diff) |
overlay: Include CPU usage in the overview chart
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'overlay/cpu-top.c')
-rw-r--r-- | overlay/cpu-top.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/overlay/cpu-top.c b/overlay/cpu-top.c new file mode 100644 index 00000000..ccfc9a6b --- /dev/null +++ b/overlay/cpu-top.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> + +#include "cpu-top.h" + +int cpu_top_update(struct cpu_top *cpu) +{ + struct cpu_stat *s = &cpu->stat[cpu->count++&1]; + struct cpu_stat *d = &cpu->stat[cpu->count&1]; + uint64_t d_total, d_idle; + char buf[4096]; + int fd, len = -1; + + fd = open("/proc/stat", 0); + if (fd < 0) + return errno; + + len = read(fd, buf, sizeof(buf)-1); + close(fd); + + if (len < 0) + return EIO; + buf[len] = '\0'; + +#ifdef __x86_64__ + sscanf(buf, "cpu %lu %lu %lu %lu", + &s->user, &s->nice, &s->sys, &s->idle); +#else + sscanf(buf, "cpu %llu %llu %llu %llu", + &s->user, &s->nice, &s->sys, &s->idle); +#endif + + s->total = s->user + s->nice + s->sys + s->idle; + if (cpu->count == 1) + return EAGAIN; + + d_total = s->total - d->total; + d_idle = s->idle - d->idle; + cpu->busy = 100 - 100 * d_idle / d_total; + + return 0; +} |