adapated for Maemo
[shermanaquarium] / sherman-aquarium / shermans / status_darwin.c
1
2 /* 
3    Mac OS X (Darwin) code written by Ben Hines <bhines@alumni.ucsd.edu> 
4
5 */
6
7
8
9 #include "defines.h"
10 #include "aquarium.h"
11 #include "status.h"
12
13 #include <mach/mach_init.h>
14 #include <mach/mach_host.h>
15
16 /* Darwin */
17
18 void status_init(void)
19 {
20 }
21
22 void status_exit(void)
23 {
24 }
25 int status_sensors(int x, int y)
26 {
27     return 0;
28 }
29 int status_swap(void)
30 {
31     return 0;
32 }
33 int status_net(int type, int direction)
34 {
35     return 0;
36 }
37 int status_mem(void)
38 {
39     return 0;
40 }
41 int status_disc(char *drive)
42 {
43     return 0;
44 }
45
46
47 /* Have the old ones global */
48 static u_int64_t oload = 0, ototal = 0;
49 static int firsttimes = 0, current = 0;
50 static int cpu_average_list[CPUSMOOTHNESS];
51
52 int status_cpu()
53 {
54     processor_cpu_load_info_data_t *pinfo;
55     mach_msg_type_number_t info_count;
56     unsigned long composite_user, composite_nice, composite_sys, composite_idle;
57     unsigned int cpuload, n_cpus;
58     u_int64_t load, total;
59     int i;
60     if (firsttimes == 0) {
61         for (i = 0; i < CPUSMOOTHNESS; i++)
62             cpu_average_list[i] = 0;
63     }
64     /* Wait until we have CPUSMOOTHNESS messures */
65     if (firsttimes != CPUSMOOTHNESS)
66         firsttimes++;
67         
68     if (host_processor_info (mach_host_self (),
69                              PROCESSOR_CPU_LOAD_INFO,
70                              &n_cpus,
71                              (processor_info_array_t*)&pinfo,
72                              &info_count)) {
73         return 0;
74     }
75
76     composite_user = composite_nice = composite_sys = composite_idle = 0;
77
78     for (i = 0; i < n_cpus; i++) {
79         composite_user  += pinfo[i].cpu_ticks [CPU_STATE_USER];
80         composite_sys   += pinfo[i].cpu_ticks [CPU_STATE_SYSTEM];
81         composite_idle  += pinfo[i].cpu_ticks [CPU_STATE_IDLE];
82         composite_nice  += pinfo[i].cpu_ticks [CPU_STATE_NICE];
83     }
84     vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count);
85
86     /* user + sys = load
87      * total = total */
88     load = composite_user + composite_sys;      /* cpu.user + cpu.sys; */
89     total = load + composite_idle + composite_nice;     /* cpu.total; */
90
91     /* Calculates an average from the last CPUSMOOTHNESS messures */
92
93     if(total!=ototal)
94         cpu_average_list[current] = (100 * (load - oload)) / (total - ototal);
95     else
96         cpu_average_list[current] = (load - oload);
97
98     current++;
99     if (current == CPUSMOOTHNESS)
100         current = 0;
101
102     oload = load;
103     ototal = total;
104
105     if (firsttimes != CPUSMOOTHNESS)
106         return 0;
107
108     cpuload = 0;
109
110     for (i = 0; i < CPUSMOOTHNESS; i++)
111         cpuload += cpu_average_list[i];
112     return (cpuload / CPUSMOOTHNESS);
113         
114 }
115