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){
87 ulong out, in, outb, inb, err;
88 static ulong ifnetaddr;
91 struct ifnethead ifnethead;
98 ifnetaddr = nl[0].n_value;
103 if(kread(ifnetaddr, (char*)&ifnethead, sizeof ifnethead) < 0)
106 out = in = outb = inb = err = 0;
107 addr = (ulong)TAILQ_FIRST(&ifnethead);
109 if(kread(addr, (char*)&ifnet, sizeof ifnet) < 0
110 || kread((ulong)ifnet.if_name, name, 16) < 0)
113 addr = (ulong)TAILQ_NEXT(&ifnet, if_link);
114 out += ifnet.if_opackets;
115 in += ifnet.if_ipackets;
116 outb += ifnet.if_obytes;
117 inb += ifnet.if_ibytes;
118 err += ifnet.if_oerrors+ifnet.if_ierrors;
120 Bprint(&bout, "etherin %lud\n", in);
121 Bprint(&bout, "etherout %lud\n", out);
122 Bprint(&bout, "etherinb %lud\n", inb);
123 Bprint(&bout, "etheroutb %lud\n", outb);
124 Bprint(&bout, "ethererr %lud\n", err);
125 Bprint(&bout, "ether %lud\n", in+out);
126 Bprint(&bout, "etherb %lud\n", inb+outb);
133 rsys(char *name, char *buf, int len)
138 if(sysctlbyname(name, buf, &l, nil, 0) < 0)
151 if(sysctlbyname(name, &u, &l, nil, 0) < 0)
163 pgsize = isys("vm.stats.vm.v_page_size");
167 if((t = isys("vm.stats.vm.v_page_count")) != 0)
168 Bprint(&bout, "mem %lld %lld\n",
169 isys("vm.stats.vm.v_active_count")*pgsize,
171 Bprint(&bout, "context %lld 1000\n", isys("vm.stats.sys.v_swtch"));
172 Bprint(&bout, "syscall %lld 1000\n", isys("vm.stats.sys.v_syscall"));
173 Bprint(&bout, "intr %lld 1000\n", isys("vm.stats.sys.v_intr")+isys("vm.stats.sys.v_trap"));
174 Bprint(&bout, "fault %lld 1000\n", isys("vm.stats.vm.v_vm_faults"));
175 Bprint(&bout, "fork %lld 1000\n", isys("vm.stats.vm.v_forks")
176 +isys("vm.stats.vm.v_rforks")
177 +isys("vm.stats.vm.v_vforks"));
186 struct clockinfo *ci;
190 if(rsys("kern.clockrate", (char*)&x, sizeof x) < sizeof ci)
193 ci = (struct clockinfo*)x;
199 if((n=rsys("kern.cp_time", (char*)x, sizeof x)) < 5*sizeof(ulong))
202 Bprint(&bout, "user %lud %d\n", x[CP_USER]+x[CP_NICE], stathz);
203 Bprint(&bout, "sys %lud %d\n", x[CP_SYS], stathz);
204 Bprint(&bout, "cpu %lud %d\n", x[CP_USER]+x[CP_NICE]+x[CP_SYS], stathz);
205 Bprint(&bout, "idle %lud %d\n", x[CP_IDLE], stathz);
217 if(getloadavg(l, 3) < 0)
219 Bprint(&bout, "load %d 1000\n", (int)(l[0]*1000.0));
226 static struct kvm_swap s;
227 static ulong pgin, pgout;
232 pgsize = getpagesize();
241 i = isys("vm.stats.vm.v_swappgsin");
242 o = isys("vm.stats.vm.v_swappgsout");
243 if(i != pgin || o != pgout){
246 kvm_getswapinfo(kvm, &s, 1, 0);
250 Bprint(&bout, "swap %lld %lld\n", s.ksw_used*(vlong)pgsize, s.ksw_total*(vlong)pgsize);