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
20 #include <sys/param.h>
21 #include <sys/sysctl.h>
22 #include <sys/dkstat.h>
27 /* You don't need initialization under BSDi */
31 /* Returns the current CPU usage in percent */
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
);
42 if (sysctl(mib
, 2, &cpustat
, &size
, NULL
, 0) < 0)
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
];
53 else if ((total
- pre_total
) > 0)
54 result
= 100 * (double)(used
- pre_used
) / (double)(total
- pre_total
);
58 /* save used/total for next calculation */