wmshutdown: Add icon for freedesktop.org icon themes.
[dockapps.git] / wmcpuload / src / cpu_darwin.c
blobb045153e94c21b525cfb4c30fd86935f20a93635
1 /*
2 * cpu_darwin - module to get cpu usage, for Darwin
4 * Copyright (C) 2002 Landon Fuller <landonblue@mac.com>
6 * Licensed under the GPL
7 */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "cpu.h"
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;
26 static
27 getload(host_cpu_load_info_t cpucounters)
29 mach_msg_type_number_t count;
30 kern_return_t kr;
31 count = HOST_CPU_LOAD_INFO_COUNT;
32 kr = host_statistics (host_priv_port, HOST_CPU_LOAD_INFO, (host_info_t) cpucounters, &count);
33 return (kr);
36 void
37 cpu_init(void)
39 host_priv_port = mach_host_self();
40 getload(&prevcount);
41 return;
44 /* Returns the current CPU usage in percent */
45 int
46 cpu_get_usage(cpu_options *opts)
48 double userticks, systicks, idleticks, totalticks, usedticks;
49 getload(&curcount);
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];
54 prevcount = curcount;
55 usedticks = userticks + systicks;
56 totalticks = usedticks + idleticks;
57 return ((100 * (double) usedticks) / totalticks);