blob: b92f682ef0234bb7ad3837658d8b1570a9781807 [file] [log] [blame]
rscf0f44012004-04-23 05:12:11 +00001#include <u.h>
rscf0f44012004-04-23 05:12:11 +00002#include <sys/types.h>
3#include <sys/protosw.h>
4#include <sys/socket.h>
5#include <sys/sysctl.h>
6#include <sys/time.h>
7#include <sys/dkstat.h>
8#include <net/if.h>
9#include <net/if_var.h>
10#include <net/if_dl.h>
11#include <net/if_types.h>
rsc81878592005-10-05 13:30:49 +000012#include <ifaddrs.h>
rscf0f44012004-04-23 05:12:11 +000013#include <sys/ioctl.h>
14#include <limits.h>
15#include <libc.h>
16#include <bio.h>
rsc81878592005-10-05 13:30:49 +000017
18#include <mach/mach.h>
19#include <mach/mach_time.h>
20#include <CoreFoundation/CoreFoundation.h>
21#include <IOKit/ps/IOPowerSources.h>
22AUTOFRAMEWORK(CoreFoundation)
23AUTOFRAMEWORK(IOKit)
24
rscf0f44012004-04-23 05:12:11 +000025#include "dat.h"
26
rsc81878592005-10-05 13:30:49 +000027typedef struct Sample Sample;
28
29struct Sample
30{
31 uint seq;
32 host_cpu_load_info_data_t cpu, p_cpu;
33 vm_size_t pgsize;
34 double divisor;
35 uint64_t time, p_time;
36 vm_statistics_data_t vm_stat, p_vm_stat;
37 boolean_t purgeable_is_valid;
rsc62a826a2006-01-27 03:53:22 +000038#ifdef VM_SWAPUSAGE /* 10.4+ */
rsc81878592005-10-05 13:30:49 +000039 struct xsw_usage xsu;
rsc62a826a2006-01-27 03:53:22 +000040#endif
rsc81878592005-10-05 13:30:49 +000041 boolean_t xsu_valid;
42 integer_t syscalls_mach, p_syscalls_mach;
43 integer_t syscalls_unix, p_syscalls_unix;
44 ulong csw, p_csw;
45 uint net_ifaces;
46 uvlong net_ipackets, p_net_ipackets;
47 uvlong net_opackets, p_net_opackets;
48 uvlong net_ibytes, p_net_ibytes;
49 uvlong net_obytes, p_net_obytes;
50 uvlong net_errors, p_net_errors;
51 ulong usecs;
52};
53
54static Sample sample;
55
56void xsample(int);
57void xapm(int);
rscf0f44012004-04-23 05:12:11 +000058void xloadavg(int);
59void xcpu(int);
60void xswap(int);
rsc81878592005-10-05 13:30:49 +000061void xvm(int);
rscf0f44012004-04-23 05:12:11 +000062void xnet(int);
rscf0f44012004-04-23 05:12:11 +000063
64void (*statfn[])(int) =
65{
rsc81878592005-10-05 13:30:49 +000066 xsample,
67 xapm,
rscf0f44012004-04-23 05:12:11 +000068 xloadavg,
69 xswap,
70 xcpu,
rsc81878592005-10-05 13:30:49 +000071 xvm,
rscf0f44012004-04-23 05:12:11 +000072 xnet,
73 0
74};
75
rsc81878592005-10-05 13:30:49 +000076static mach_port_t stat_port;
rscf0f44012004-04-23 05:12:11 +000077
78void
rsc81878592005-10-05 13:30:49 +000079sampleinit(void)
rscf0f44012004-04-23 05:12:11 +000080{
rsc81878592005-10-05 13:30:49 +000081 mach_timebase_info_data_t info;
82
83 if(stat_port)
84 return;
rscf0f44012004-04-23 05:12:11 +000085
rsc81878592005-10-05 13:30:49 +000086 stat_port = mach_host_self();
87 memset(&sample, 0, sizeof sample);
88 if(host_page_size(stat_port, &sample.pgsize) != KERN_SUCCESS)
89 sample.pgsize = 4096;
90
rsccbeb0b22006-04-01 19:24:03 +000091 /* populate clock tick info for timestamps */
rsc81878592005-10-05 13:30:49 +000092 mach_timebase_info(&info);
93 sample.divisor = 1000.0 * (double)info.denom/info.numer;
94 sample.time = mach_absolute_time();
95}
96
97void
98samplenet(void)
99{
100 struct ifaddrs *ifa_list, *ifa;
rscf3832662005-10-19 03:21:50 +0000101 struct if_data *if_data;
102
rsc81878592005-10-05 13:30:49 +0000103 ifa_list = nil;
rscf3832662005-10-19 03:21:50 +0000104 sample.net_ifaces = 0;
rsc81878592005-10-05 13:30:49 +0000105 if(getifaddrs(&ifa_list) == 0){
106 sample.p_net_ipackets = sample.net_ipackets;
107 sample.p_net_opackets = sample.net_opackets;
108 sample.p_net_ibytes = sample.net_ibytes;
rscf3832662005-10-19 03:21:50 +0000109 sample.p_net_obytes = sample.net_obytes;
rsc81878592005-10-05 13:30:49 +0000110 sample.p_net_errors = sample.net_errors;
111
112 sample.net_ipackets = 0;
113 sample.net_opackets = 0;
114 sample.net_ibytes = 0;
115 sample.net_obytes = 0;
116 sample.net_errors = 0;
117 sample.net_ifaces = 0;
118
rscf3832662005-10-19 03:21:50 +0000119 for(ifa=ifa_list; ifa; ifa=ifa->ifa_next){
rsc81878592005-10-05 13:30:49 +0000120 if(ifa->ifa_addr->sa_family != AF_LINK)
121 continue;
122 if((ifa->ifa_flags&(IFF_UP|IFF_RUNNING)) == 0)
123 continue;
124 if(ifa->ifa_data == nil)
125 continue;
126 if(strncmp(ifa->ifa_name, "lo", 2) == 0) /* loopback */
127 continue;
128
rscf3832662005-10-19 03:21:50 +0000129 if_data = (struct if_data*)ifa->ifa_data;
rsc81878592005-10-05 13:30:49 +0000130 sample.net_ipackets += if_data->ifi_ipackets;
131 sample.net_opackets += if_data->ifi_opackets;
132 sample.net_ibytes += if_data->ifi_ibytes;
rscf3832662005-10-19 03:21:50 +0000133 sample.net_obytes += if_data->ifi_obytes;
134 sample.net_errors += if_data->ifi_ierrors + if_data->ifi_oerrors;
rsc81878592005-10-05 13:30:49 +0000135 sample.net_ifaces++;
136 }
137 freeifaddrs(ifa_list);
138 }
139}
140
141
142/*
143 * The following forces the program to be run with the suid root as
144 * all the other stat monitoring apps get set:
145 *
146 * -rwsr-xr-x 1 root wheel 83088 Mar 20 2005 /usr/bin/top
147 * -rwsrwxr-x 1 root admin 54048 Mar 20 2005
148 * /Applications/Utilities/Activity Monitor.app/Contents/Resources/pmTool
149 *
150 * If Darwin eventually encompases more into sysctl then this
151 * won't be required.
152 */
153void
rsc663ddde2005-10-31 01:15:30 +0000154sampleevents(void)
rsc81878592005-10-05 13:30:49 +0000155{
156 uint i, j, pcnt, tcnt;
157 mach_msg_type_number_t count;
158 kern_return_t error;
159 processor_set_t *psets, pset;
160 task_t *tasks;
161 task_events_info_data_t events;
162
163 if((error = host_processor_sets(stat_port, &psets, &pcnt)) != KERN_SUCCESS){
164 Bprint(&bout, "host_processor_sets: %s (make sure auxstats is setuid root)\n",
165 mach_error_string(error));
rscf0f44012004-04-23 05:12:11 +0000166 return;
rsc81878592005-10-05 13:30:49 +0000167 }
168
169 sample.p_syscalls_mach = sample.syscalls_mach;
170 sample.p_syscalls_unix = sample.syscalls_unix;
171 sample.p_csw = sample.csw;
172
173 sample.syscalls_mach = 0;
174 sample.syscalls_unix = 0;
175 sample.csw = 0;
176
177 for(i=0; i<pcnt; i++){
178 if((error=host_processor_set_priv(stat_port, psets[i], &pset)) != KERN_SUCCESS){
179 Bprint(&bout, "host_processor_set_priv: %s\n", mach_error_string(error));
180 return;
181 }
rscf3832662005-10-19 03:21:50 +0000182 if((error=processor_set_tasks(pset, &tasks, &tcnt)) != KERN_SUCCESS){
183 Bprint(&bout, "processor_set_tasks: %s\n", mach_error_string(error));
rsc81878592005-10-05 13:30:49 +0000184 return;
185 }
186 for(j=0; j<tcnt; j++){
187 count = TASK_EVENTS_INFO_COUNT;
188 if(task_info(tasks[j], TASK_EVENTS_INFO, (task_info_t)&events, &count) == KERN_SUCCESS){
189 sample.syscalls_mach += events.syscalls_mach;
190 sample.syscalls_unix += events.syscalls_unix;
191 sample.csw += events.csw;
192 }
193
194 if(tasks[j] != mach_task_self())
195 mach_port_deallocate(mach_task_self(), tasks[j]);
196 }
197
198 if((error = vm_deallocate((vm_map_t)mach_task_self(),
199 (vm_address_t)tasks, tcnt*sizeof(task_t))) != KERN_SUCCESS){
200 Bprint(&bout, "vm_deallocate: %s\n", mach_error_string(error));
201 return;
202 }
203
204 if((error = mach_port_deallocate(mach_task_self(), pset)) != KERN_SUCCESS
205 || (error = mach_port_deallocate(mach_task_self(), psets[i])) != KERN_SUCCESS){
206 Bprint(&bout, "mach_port_deallocate: %s\n", mach_error_string(error));
207 return;
208 }
209 }
210
211 if((error = vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)psets,
212 pcnt*sizeof(processor_set_t))) != KERN_SUCCESS){
213 Bprint(&bout, "vm_deallocate: %s\n", mach_error_string(error));
rscf0f44012004-04-23 05:12:11 +0000214 return;
215 }
216}
217
218void
rsc81878592005-10-05 13:30:49 +0000219xsample(int first)
rscf0f44012004-04-23 05:12:11 +0000220{
rsc81878592005-10-05 13:30:49 +0000221 int mib[2];
222 mach_msg_type_number_t count;
rsc81878592005-10-05 13:30:49 +0000223 size_t len;
224
225 if(first){
226 sampleinit();
227 return;
228 }
229
230 sample.seq++;
231 sample.p_time = sample.time;
232 sample.time = mach_absolute_time();
233
234 sample.p_vm_stat = sample.vm_stat;
235 count = sizeof(sample.vm_stat) / sizeof(natural_t);
236 host_statistics(stat_port, HOST_VM_INFO, (host_info_t)&sample.vm_stat, &count);
237
238 if(sample.seq == 1)
239 sample.p_vm_stat = sample.vm_stat;
240
rsc62a826a2006-01-27 03:53:22 +0000241#ifdef VM_SWAPUSAGE
rsc81878592005-10-05 13:30:49 +0000242 mib[0] = CTL_VM;
243 mib[1] = VM_SWAPUSAGE;
244 len = sizeof sample.xsu;
245 sample.xsu_valid = TRUE;
rscf3832662005-10-19 03:21:50 +0000246 if(sysctl(mib, 2, &sample.xsu, &len, NULL, 0) < 0 && errno == ENOENT)
rsc663ddde2005-10-31 01:15:30 +0000247 sample.xsu_valid = FALSE;
rsc62a826a2006-01-27 03:53:22 +0000248#endif
249
rsc81878592005-10-05 13:30:49 +0000250 samplenet();
251 sampleevents();
252
253 sample.p_cpu = sample.cpu;
254 count = HOST_CPU_LOAD_INFO_COUNT;
255 host_statistics(stat_port, HOST_CPU_LOAD_INFO, (host_info_t)&sample.cpu, &count);
rscf3832662005-10-19 03:21:50 +0000256 sample.usecs = (double)(sample.time - sample.p_time)/sample.divisor;
rsc81878592005-10-05 13:30:49 +0000257 Bprint(&bout, "usecs %lud\n", sample.usecs);
258}
259
260void
261xapm(int first)
262{
263 int i, battery;
264 CFArrayRef array;
265 CFDictionaryRef dict;
266 CFTypeRef cf, src, value;
267
rscf0f44012004-04-23 05:12:11 +0000268 if(first)
rsc81878592005-10-05 13:30:49 +0000269 return;
270
271 src = IOPSCopyPowerSourcesInfo();
272 array = IOPSCopyPowerSourcesList(src);
rscf0f44012004-04-23 05:12:11 +0000273
rsc81878592005-10-05 13:30:49 +0000274 for(i=0; i<CFArrayGetCount(array); i++){
275 cf = CFArrayGetValueAtIndex(array, i);
276 dict = IOPSGetPowerSourceDescription(src, cf);
277 if(dict != nil){
278 value = CFDictionaryGetValue(dict, CFSTR("Current Capacity"));
279 if(value != nil){
280 if(!CFNumberGetValue(value, kCFNumberIntType, &battery))
281 battery = 100;
282 Bprint(&bout, "battery =%d 100\n", battery);
283 break;
284 }
285 }
rscf0f44012004-04-23 05:12:11 +0000286 }
rsc81878592005-10-05 13:30:49 +0000287
288 CFRelease(array);
289 CFRelease(src);
rscf0f44012004-04-23 05:12:11 +0000290}
291
292void
293xnet(int first)
294{
rsc81878592005-10-05 13:30:49 +0000295 uint n;
rscc7a10af2007-04-14 14:59:27 +0000296 ulong err, in, inb, out, outb;
rsc81878592005-10-05 13:30:49 +0000297
298 n = sample.net_ifaces;
rscc7a10af2007-04-14 14:59:27 +0000299 in = sample.net_ipackets - sample.p_net_ipackets;
300 out = sample.net_opackets - sample.p_net_opackets;
301 inb = sample.net_ibytes - sample.p_net_ibytes;
302 outb = sample.net_obytes - sample.p_net_obytes;
303 err = sample.net_errors - sample.p_net_errors;
rsc81878592005-10-05 13:30:49 +0000304
rscc7a10af2007-04-14 14:59:27 +0000305 Bprint(&bout, "etherb %lud %d\n", inb+outb, n*1000000);
306 Bprint(&bout, "ether %lud %d\n", in+out, n*1000);
307 Bprint(&bout, "ethererr %lud %d\n", err, n*1000);
308 Bprint(&bout, "etherin %lud %d\n", in, n*1000);
309 Bprint(&bout, "etherout %lud %d\n", out, n*1000);
310 Bprint(&bout, "etherinb %lud %d\n", inb, n*1000);
311 Bprint(&bout, "etheroutb %lud %d\n", outb, n*1000);
rscf0f44012004-04-23 05:12:11 +0000312}
313
rscf0f44012004-04-23 05:12:11 +0000314int
315rsys(char *name, char *buf, int len)
316{
317 size_t l;
318
319 l = len;
320 if(sysctlbyname(name, buf, &l, nil, 0) < 0)
321 return -1;
322 buf[l] = 0;
323 return l;
324}
325
326vlong
327isys(char *name)
328{
329 ulong u;
330 size_t l;
331
332 l = sizeof u;
333 if(sysctlbyname(name, &u, &l, nil, 0) < 0)
334 return 0;
335 return u;
336}
337
338void
rsc81878592005-10-05 13:30:49 +0000339xvm(int first)
rscf0f44012004-04-23 05:12:11 +0000340{
rsc4492a1e2007-04-12 14:18:57 +0000341 natural_t total, active;
rscf0f44012004-04-23 05:12:11 +0000342
rsc81878592005-10-05 13:30:49 +0000343 if(first)
344 return;
345
346 total = sample.vm_stat.free_count
347 + sample.vm_stat.active_count
348 + sample.vm_stat.inactive_count
349 + sample.vm_stat.wire_count;
rsc4492a1e2007-04-12 14:18:57 +0000350
351 active = sample.vm_stat.active_count
352 + sample.vm_stat.inactive_count
353 + sample.vm_stat.wire_count;
354
rsc81878592005-10-05 13:30:49 +0000355 if(total)
rsc4492a1e2007-04-12 14:18:57 +0000356 Bprint(&bout, "mem =%lud %lud\n", active, total);
rsc81878592005-10-05 13:30:49 +0000357
358 Bprint(&bout, "context %lld 1000\n", (vlong)sample.csw);
359 Bprint(&bout, "syscall %lld 1000\n", (vlong)sample.syscalls_mach+sample.syscalls_unix);
360 Bprint(&bout, "intr %lld 1000\n",
361 isys("vm.stats.sys.v_intr")
362 +isys("vm.stats.sys.v_trap"));
363
rscf3832662005-10-19 03:21:50 +0000364 Bprint(&bout, "fault %lld 1000\n", sample.vm_stat.faults);
rsc81878592005-10-05 13:30:49 +0000365 Bprint(&bout, "fork %lld 1000\n",
366 isys("vm.stats.vm.v_rforks")
rscf0f44012004-04-23 05:12:11 +0000367 +isys("vm.stats.vm.v_vforks"));
rsc81878592005-10-05 13:30:49 +0000368
rsccbeb0b22006-04-01 19:24:03 +0000369/* Bprint(&bout, "hits %lud of %lud lookups (%d%% hit rate)\n", */
370/* (asamp.vm_stat.hits), */
371/* (asamp.vm_stat.lookups), */
372/* (natural_t)(((double)asamp.vm_stat.hits*100)/ (double)asamp.vm_stat.lookups)); */
rscf0f44012004-04-23 05:12:11 +0000373}
374
375void
376xcpu(int first)
377{
rsc81878592005-10-05 13:30:49 +0000378 ulong user, sys, idle, nice, t;
rscf0f44012004-04-23 05:12:11 +0000379
rsc81878592005-10-05 13:30:49 +0000380 if(first)
rscf0f44012004-04-23 05:12:11 +0000381 return;
382
rsc81878592005-10-05 13:30:49 +0000383 sys = sample.cpu.cpu_ticks[CPU_STATE_SYSTEM] -
384 sample.p_cpu.cpu_ticks[CPU_STATE_SYSTEM];
385 idle = sample.cpu.cpu_ticks[CPU_STATE_IDLE] -
386 sample.p_cpu.cpu_ticks[CPU_STATE_IDLE];
387 user = sample.cpu.cpu_ticks[CPU_STATE_USER] -
388 sample.p_cpu.cpu_ticks[CPU_STATE_USER];
389 nice = sample.cpu.cpu_ticks[CPU_STATE_NICE] -
390 sample.p_cpu.cpu_ticks[CPU_STATE_NICE];
391
392 t = sys+idle+user+nice;
393
394 Bprint(&bout, "user =%lud %lud\n", user, t);
395 Bprint(&bout, "sys =%lud %lud\n", sys, t);
396 Bprint(&bout, "idle =%lud %lud\n", idle, t);
397 Bprint(&bout, "nice =%lud %lud\n", nice, t);
rscf0f44012004-04-23 05:12:11 +0000398}
399
400void
401xloadavg(int first)
402{
403 double l[3];
404
405 if(first)
406 return;
407
408 if(getloadavg(l, 3) < 0)
409 return;
410 Bprint(&bout, "load %d 1000\n", (int)(l[0]*1000.0));
411}
412
413void
414xswap(int first)
415{
rsc81878592005-10-05 13:30:49 +0000416 if(first)
rscf0f44012004-04-23 05:12:11 +0000417 return;
rsc2a951f52006-01-27 03:55:18 +0000418
419#ifdef VM_SWAPUSAGE
rsc81878592005-10-05 13:30:49 +0000420 if(sample.xsu_valid)
421 Bprint(&bout, "swap %lld %lld\n",
rscf3832662005-10-19 03:21:50 +0000422 (vlong)sample.xsu.xsu_used,
rsc81878592005-10-05 13:30:49 +0000423 (vlong)sample.xsu.xsu_total);
rsc2a951f52006-01-27 03:55:18 +0000424#endif
rscf0f44012004-04-23 05:12:11 +0000425}