wmclockmon: update change-log
[dockapps.git] / wmcpuload / src / cpu_netbsd.c
blobfc3f03b2dd67915e37f5849dc66f0a731d6bfc28
1 /*
2 * cpu_netbsd - module to get cpu usage, for NetBSD
4 * This code is based on cpu_openbsd.c
6 * Copyright (c) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
7 * Copyright (c) 2002 Thomas Runge <coto@core.de>
8 * Copyright (C) 2003 Nedko Arnaudov <nedko@users.sourceforge.net>
10 * Licensed under the GPL
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "cpu.h"
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/sysctl.h>
26 #include <sys/sched.h>
28 void
29 cpu_init(void)
31 /* You don't need initialization under NetBSD */
32 return;
35 /* Returns the current CPU usage in percent */
36 int
37 cpu_get_usage(cpu_options *opts)
39 int total, used, result, idle;
40 static int pre_total, pre_used, pre_idle;
42 int mib[] = { CTL_KERN, KERN_CP_TIME };
43 u_int64_t cpu_time[CPUSTATES];
44 size_t size = sizeof(cpu_time);
46 /* get cpu time via sysctl */
47 if (sysctl(mib, 2, &cpu_time, &size, NULL, 0) < 0)
48 return 0;
50 /* calculate usage */
51 total = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_INTR] +
52 cpu_time[CP_NICE] + cpu_time[CP_IDLE];
53 used = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_INTR] +
54 (opts->ignore_nice ? 0 : cpu_time[CP_NICE]);
55 if ((pre_total == 0) || !(total - pre_total > 0)) {
56 result = 0;
57 } else {
58 result = 100 * (double)(used - pre_used) / (double)(total - pre_total);
61 /* save used/total for next calculation */
62 pre_used = used;
63 pre_total = total;
65 return result;