2 * cpu_openbsd - module to get cpu usage, for OpenBSD
4 * Copyright (C) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
5 * Copyright (C) 2003 Nedko Arnaudov <nedko@users.sourceforge.net>
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 OpenBSD */
31 /* Returns the current CPU usage in percent */
33 cpu_get_usage(cpu_options
*opts
)
35 int total
, used
, result
;
36 static int pre_total
, pre_used
;
38 int mib
[] = { CTL_KERN
, KERN_CPTIME
};
39 unsigned long int cpu_time
[CPUSTATES
];
40 size_t size
= sizeof(cpu_time
);
43 if (sysctl(mib
, 2, &cpu_time
, &size
, NULL
, 0) < 0)
47 total
= cpu_time
[CP_USER
] + cpu_time
[CP_SYS
] + cpu_time
[CP_INTR
] +
48 cpu_time
[CP_NICE
] + cpu_time
[CP_IDLE
];
49 used
= cpu_time
[CP_USER
] + cpu_time
[CP_SYS
] + cpu_time
[CP_INTR
] +
50 (opts
->ignore_nice
? 0 : cpu_time
[CP_NICE
]);
51 if ((pre_total
== 0) || !(total
- pre_total
> 0)) {
54 result
= 100 * (double)(used
- pre_used
) / (double)(total
- pre_total
);
57 /* save used/total for next calculation */