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
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/sysctl.h>
26 #include <sys/sched.h>
31 /* You don't need initialization under NetBSD */
35 /* Returns the current CPU usage in percent */
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)
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)) {
58 result
= 100 * (double)(used
- pre_used
) / (double)(total
- pre_total
);
61 /* save used/total for next calculation */