3 Mac OS X (Darwin) code written by Ben Hines <bhines@alumni.ucsd.edu>
13 #include <mach/mach_init.h>
14 #include <mach/mach_host.h>
18 void status_init(void)
22 void status_exit(void)
25 int status_sensors(int x, int y)
33 int status_net(int type, int direction)
41 int status_disc(char *drive)
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];
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;
60 if (firsttimes == 0) {
61 for (i = 0; i < CPUSMOOTHNESS; i++)
62 cpu_average_list[i] = 0;
64 /* Wait until we have CPUSMOOTHNESS messures */
65 if (firsttimes != CPUSMOOTHNESS)
68 if (host_processor_info (mach_host_self (),
69 PROCESSOR_CPU_LOAD_INFO,
71 (processor_info_array_t*)&pinfo,
76 composite_user = composite_nice = composite_sys = composite_idle = 0;
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];
84 vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count);
88 load = composite_user + composite_sys; /* cpu.user + cpu.sys; */
89 total = load + composite_idle + composite_nice; /* cpu.total; */
91 /* Calculates an average from the last CPUSMOOTHNESS messures */
94 cpu_average_list[current] = (100 * (load - oload)) / (total - ototal);
96 cpu_average_list[current] = (load - oload);
99 if (current == CPUSMOOTHNESS)
105 if (firsttimes != CPUSMOOTHNESS)
110 for (i = 0; i < CPUSMOOTHNESS; i++)
111 cpuload += cpu_average_list[i];
112 return (cpuload / CPUSMOOTHNESS);