diff options
author | Aaron Marcher <me@drkhsh.at> | 2018-05-06 17:48:37 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2018-05-06 17:48:37 +0200 |
commit | 2b82bf02488147a3e69a715267934f015fe29a43 (patch) | |
tree | 6cf2049673aa4a671aed2e4691b33ae69f45a6be /components/cpu.c | |
parent | efb41724b5499bf61ab65ff65908d686e59ecdc0 (diff) |
cpu_perc: Port to OpenBSD
In OpenBSD the CPU usage in percent is now computed using KERN_CPTIME
sysctl.
Diffstat (limited to 'components/cpu.c')
-rw-r--r-- | components/cpu.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/components/cpu.c b/components/cpu.c index e7f4cd9..79bb881 100644 --- a/components/cpu.c +++ b/components/cpu.c @@ -68,6 +68,8 @@ return bprintf("%d", perc); } #elif defined(__OpenBSD__) + #include <sys/param.h> + #include <sys/sched.h> #include <sys/sysctl.h> const char * @@ -89,4 +91,37 @@ return bprintf("%d", freq); } + + const char * + cpu_perc(void) + { + int mib[2], perc; + static int valid; + static long int a[CPUSTATES]; + long int b[CPUSTATES]; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_CPTIME; + + size = sizeof(a); + + memcpy(b, a, sizeof(b)); + if (sysctl(mib, 2, &a, &size, NULL, 0) == -1) { + fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno)); + return NULL; + } + if (!valid) { + valid = 1; + return NULL; + } + + perc = 100 * + ((a[CP_USER]+a[CP_NICE]+a[CP_SYS]+a[CP_INTR]) - + (b[CP_USER]+b[CP_NICE]+b[CP_SYS]+b[CP_INTR])) / + ((a[CP_USER]+a[CP_NICE]+a[CP_SYS]+a[CP_INTR]+a[CP_IDLE]) - + (b[CP_USER]+b[CP_NICE]+b[CP_SYS]+b[CP_INTR]+b[CP_IDLE])); + + return bprintf("%d", perc); + } #endif |