wmweather+: Remove from repository.
[dockapps.git] / wmcpuload / src / cpu_bsdi.c
blob72349fdfe819ea29910bfb397ba469df20f22ec8
1 /*
2 * cpu_bsdi - module to get cpu usage, for BSDi
4 * Copyright (C) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
5 * Copyright (C) 2002 Nicolas Belan <belan@matranet.com>
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 BSDi */
28 return;
31 /* Returns the current CPU usage in percent */
32 int
33 cpu_get_usage(struct cpu_options *opts)
35 int total, used, result;
36 static int pre_total, pre_used;
37 struct cpustats cpustat;
38 int mib[] = { CTL_KERN, KERN_CPUSTATS };
39 size_t size = sizeof(struct cpustats);
41 /* get cpu time*/
42 if (sysctl(mib, 2, &cpustat, &size, NULL, 0) < 0)
43 return 0;
45 /* calc usage */
46 used = cpustat.cp_time[CP_USER] + cpustat.cp_time[CP_SYS];
47 if (!opts->ignore_nice)
48 used += cpustat.cp_time[CP_NICE];
49 total = used + cpustat.cp_time[CP_IDLE];
51 if (pre_total == 0)
52 result = 0;
53 else if ((total - pre_total) > 0)
54 result = 100 * (double)(used - pre_used) / (double)(total - pre_total);
55 else
56 result = 0;
58 /* save used/total for next calculation */
59 pre_used = used;
60 pre_total = total;
62 return result;