wmauda: Fix installation dir
[dockapps.git] / wmcpuload-1.0.0 / src / cpu_openbsd.c
blob743daca9f3dfec951317cc2f94ee7810da517799
1 /*
2 * cpu_openbsd - module to get cpu usage, for OpenBSD
4 * Copyright (C) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
6 * Licensed under the GPL
7 */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "cpu.h"
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
21 #include <sys/dkstat.h>
23 void
24 cpu_init(void)
26 /* You don't need initialization under OpenBSD */
27 return;
30 /* Returns the current CPU usage in percent */
31 int
32 cpu_get_usage(cpu_options *opts)
34 int total, used, result;
35 static int pre_total, pre_used;
37 int mib[] = { CTL_KERN, KERN_CPTIME };
38 unsigned long int cpu_time[CPUSTATES];
39 size_t size = sizeof(cpu_time);
41 /* get cpu time*/
42 if (sysctl(mib, 2, &cpu_time, &size, NULL, 0) < 0)
43 return 0;
45 /* calc usage */
46 used = cpu_time[CP_USER] + cpu_time[CP_SYS];
47 if (!opts->ignore_nice) {
48 used += cpu_time[CP_NICE];
50 total = used + cpu_time[CP_IDLE];
52 if (pre_total == 0) {
53 result = 0;
54 } else if ((total - pre_total) > 0) {
55 result = 100 * (double)(used - pre_used) / (double)(total - pre_total);
56 } else {
57 result = 0;
60 /* save used/total for next calculation */
61 pre_used = used;
62 pre_total = total;
64 return result;