2 * No idea whether this will work. It does compile.
9 #include <sys/protosw.h>
10 #include <sys/socket.h>
11 #include <sys/sysctl.h>
13 #include <sys/dkstat.h>
15 #include <net/if_var.h>
16 #include <net/if_dl.h>
17 #include <net/if_types.h>
18 #include <sys/ioctl.h>
31 void (*statfn[])(int) =
44 static struct nlist nl[] = {
53 char buf[_POSIX2_LINE_MAX];
57 kvm = kvm_openfiles(nil, nil, nil, OREAD, buf);
60 if(kvm_nlist(kvm, nl) < 0 || nl[0].n_type == 0){
74 kread(ulong addr, char *buf, int size)
76 if(kvm_read(kvm, addr, buf, size) != size){
86 ulong out, in, outb, inb, err;
87 static ulong ifnetaddr;
90 struct ifnethead ifnethead;
97 ifnetaddr = nl[0].n_value;
102 if(kread(ifnetaddr, (char*)&ifnethead, sizeof ifnethead) < 0)
105 out = in = outb = inb = err = 0;
106 addr = (ulong)TAILQ_FIRST(&ifnethead);
108 if(kread(addr, (char*)&ifnet, sizeof ifnet) < 0
109 || kread((ulong)ifnet.if_name, name, 16) < 0)
112 addr = (ulong)TAILQ_NEXT(&ifnet, if_link);
113 out += ifnet.if_opackets;
114 in += ifnet.if_ipackets;
115 outb += ifnet.if_obytes;
116 inb += ifnet.if_ibytes;
117 err += ifnet.if_oerrors+ifnet.if_ierrors;
119 Bprint(&bout, "etherin %lud\n", in);
120 Bprint(&bout, "etherout %lud\n", out);
121 Bprint(&bout, "etherinb %lud\n", inb);
122 Bprint(&bout, "etheroutb %lud\n", outb);
123 Bprint(&bout, "ethererr %lud\n", err);
124 Bprint(&bout, "ether %lud\n", in+out);
125 Bprint(&bout, "etherb %lud\n", inb+outb);
130 rsys(char *name, char *buf, int len)
135 if(sysctlbyname(name, buf, &l, nil, 0) < 0)
148 if(sysctlbyname(name, &u, &l, nil, 0) < 0)
160 pgsize = isys("vm.stats.vm.v_page_size");
164 if((t = isys("vm.stats.vm.v_page_count")) != 0)
165 Bprint(&bout, "mem %lld %lld\n",
166 isys("vm.stats.vm.v_active_count")*pgsize,
168 Bprint(&bout, "context %lld 1000\n", isys("vm.stats.sys.v_swtch"));
169 Bprint(&bout, "syscall %lld 1000\n", isys("vm.stats.sys.v_syscall"));
170 Bprint(&bout, "intr %lld 1000\n", isys("vm.stats.sys.v_intr")+isys("vm.stats.sys.v_trap"));
171 Bprint(&bout, "fault %lld 1000\n", isys("vm.stats.vm.v_vm_faults"));
172 Bprint(&bout, "fork %lld 1000\n", isys("vm.stats.vm.v_forks")
173 +isys("vm.stats.vm.v_rforks")
174 +isys("vm.stats.vm.v_vforks"));
183 struct clockinfo *ci;
187 if(rsys("kern.clockrate", (char*)&x, sizeof x) < sizeof ci)
190 ci = (struct clockinfo*)x;
196 if((n=rsys("kern.cp_time", (char*)x, sizeof x)) < 5*sizeof(ulong))
199 Bprint(&bout, "user %lud %d\n", x[CP_USER]+x[CP_NICE], stathz);
200 Bprint(&bout, "sys %lud %d\n", x[CP_SYS], stathz);
201 Bprint(&bout, "cpu %lud %d\n", x[CP_USER]+x[CP_NICE]+x[CP_SYS], stathz);
202 Bprint(&bout, "idle %lud %d\n", x[CP_IDLE], stathz);
214 if(getloadavg(l, 3) < 0)
216 Bprint(&bout, "load %d 1000\n", (int)(l[0]*1000.0));
223 static struct kvm_swap s;
224 static ulong pgin, pgout;
229 pgsize = getpagesize();
238 i = isys("vm.stats.vm.v_swappgsin");
239 o = isys("vm.stats.vm.v_swappgsout");
240 if(i != pgin || o != pgout){
243 kvm_getswapinfo(kvm, &s, 1, 0);
247 Bprint(&bout, "swap %lld %lld\n", s.ksw_used*(vlong)pgsize, s.ksw_total*(vlong)pgsize);