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>
9 * Licensed under the GPL
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/sysctl.h>
25 #include <sys/sched.h>
30 /* You don't need initialization under NetBSD */
34 /* Returns the current CPU usage in percent */
36 cpu_get_usage(cpu_options
*opts
)
38 int total
, used
, result
, idle
;
39 static int pre_total
, pre_used
, pre_idle
;
41 int mib
[] = { CTL_KERN
, KERN_CP_TIME
};
42 u_int64_t cpu_time
[CPUSTATES
];
43 size_t size
= sizeof(cpu_time
);
45 /* get cpu time via sysctl */
46 if (sysctl(mib
, 2, &cpu_time
, &size
, NULL
, 0) < 0)
49 if (opts
->ignore_nice
) cpu_time
[CP_NICE
] = 0;
52 used
= cpu_time
[CP_USER
] + cpu_time
[CP_SYS
] +
53 cpu_time
[CP_NICE
] + cpu_time
[CP_INTR
];
54 total
= used
+ cpu_time
[CP_IDLE
];
55 if ((pre_total
== 0) || !(total
- pre_total
> 0)) {
58 result
= 100 * (double)(used
- pre_used
) / (double)(total
- pre_total
);
61 /* save used/total for next calculation */