2 * cpu_darwin - module to get cpu usage, for Darwin
4 * Copyright (C) 2002 Landon Fuller <landonblue@mac.com>
6 * Licensed under the GPL
19 #include <mach/host_info.h>
20 #include <mach/mach_types.h>
21 #include <mach/message.h>
23 static host_cpu_load_info_data_t prevcount
, curcount
;
24 static mach_port_t host_priv_port
;
27 getload(host_cpu_load_info_t cpucounters
)
29 mach_msg_type_number_t count
;
31 count
= HOST_CPU_LOAD_INFO_COUNT
;
32 kr
= host_statistics (host_priv_port
, HOST_CPU_LOAD_INFO
, (host_info_t
) cpucounters
, &count
);
39 host_priv_port
= mach_host_self();
44 /* Returns the current CPU usage in percent */
46 cpu_get_usage(cpu_options
*opts
)
48 double userticks
, systicks
, idleticks
, totalticks
, usedticks
;
51 userticks
= curcount
.cpu_ticks
[CPU_STATE_USER
] - prevcount
.cpu_ticks
[CPU_STATE_USER
];
52 systicks
= curcount
.cpu_ticks
[CPU_STATE_SYSTEM
] - prevcount
.cpu_ticks
[CPU_STATE_SYSTEM
];
53 idleticks
= curcount
.cpu_ticks
[CPU_STATE_IDLE
] - prevcount
.cpu_ticks
[CPU_STATE_IDLE
];
55 usedticks
= userticks
+ systicks
;
56 totalticks
= usedticks
+ idleticks
;
57 return ((100 * (double) usedticks
) / totalticks
);