wmclockmon: update change-log
[dockapps.git] / wmcpuload / src / cpu_openbsd.c
blob8f718827990f01cdd28afb1c3312ffd8fbcd23f2
1 /*
2 * cpu_openbsd - module to get cpu usage, for OpenBSD
4 * Copyright (C) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
5 * Copyright (C) 2003 Nedko Arnaudov <nedko@users.sourceforge.net>
7 * Licensed under the GPL
8 */
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "cpu.h"
20 #include <sys/param.h>
21 #include <sys/sysctl.h>
22 #include <sys/dkstat.h>
24 void
25 cpu_init(void)
27 /* You don't need initialization under OpenBSD */
28 return;
31 /* Returns the current CPU usage in percent */
32 int
33 cpu_get_usage(cpu_options *opts)
35 int total, used, result;
36 static int pre_total, pre_used;
38 int mib[] = { CTL_KERN, KERN_CPTIME };
39 unsigned long int cpu_time[CPUSTATES];
40 size_t size = sizeof(cpu_time);
42 /* get cpu time*/
43 if (sysctl(mib, 2, &cpu_time, &size, NULL, 0) < 0)
44 return 0;
46 /* calc usage */
47 total = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_INTR] +
48 cpu_time[CP_NICE] + cpu_time[CP_IDLE];
49 used = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_INTR] +
50 (opts->ignore_nice ? 0 : cpu_time[CP_NICE]);
51 if ((pre_total == 0) || !(total - pre_total > 0)) {
52 result = 0;
53 } else {
54 result = 100 * (double)(used - pre_used) / (double)(total - pre_total);
57 /* save used/total for next calculation */
58 pre_used = used;
59 pre_total = total;
61 return result;