From 6a64ee938b90a2d27342aa42ba1d2d90da40dc7b Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 18 Aug 2013 15:56:22 +0100 Subject: overlay: Include CPU usage in the overview chart Signed-off-by: Chris Wilson --- overlay/cpu-top.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 overlay/cpu-top.c (limited to 'overlay/cpu-top.c') 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 +#include +#include +#include + +#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; +} -- cgit v1.2.3