Blob


1 /*
2 * No idea whether this will work. It does compile.
3 */
5 #include <u.h>
6 #include <kvm.h>
7 #include <nlist.h>
8 #include <sys/types.h>
9 #include <sys/protosw.h>
10 #include <sys/socket.h>
11 #include <sys/sysctl.h>
12 #include <sys/time.h>
13 #include <sys/dkstat.h>
14 #include <net/if.h>
15 #include <net/if_var.h>
16 #include <net/if_dl.h>
17 #include <net/if_types.h>
18 #include <sys/ioctl.h>
19 #include <limits.h>
20 #include <libc.h>
21 #include <bio.h>
22 #include "dat.h"
24 void xloadavg(int);
25 void xcpu(int);
26 void xswap(int);
27 void xsysctl(int);
28 void xnet(int);
29 void xkvm(int);
31 void (*statfn[])(int) =
32 {
33 xkvm,
34 xloadavg,
35 xswap,
36 xcpu,
37 xsysctl,
38 xnet,
39 0
40 };
42 static kvm_t *kvm;
44 static struct nlist nl[] = {
45 { "_ifnet" },
46 { "_cp_time" },
47 { "" },
48 };
50 void
51 kvminit(void)
52 {
53 char buf[_POSIX2_LINE_MAX];
55 if(kvm)
56 return;
57 kvm = kvm_openfiles(nil, nil, nil, OREAD, buf);
58 if(kvm == nil)
59 return;
60 if(kvm_nlist(kvm, nl) < 0 || nl[0].n_type == 0){
61 kvm = nil;
62 return;
63 }
64 }
66 void
67 xkvm(int first)
68 {
69 if(first)
70 kvminit();
71 }
73 int
74 kread(ulong addr, char *buf, int size)
75 {
76 if(kvm_read(kvm, addr, buf, size) != size){
77 memset(buf, 0, size);
78 return -1;
79 }
80 return size;
81 }
83 void
84 xnet(int first)
85 {
86 ulong out, in, outb, inb, err;
87 static ulong ifnetaddr;
88 ulong addr;
89 struct ifnet ifnet;
90 struct ifnethead ifnethead;
91 char name[16];
93 if(first)
94 return;
96 if(ifnetaddr == 0){
97 ifnetaddr = nl[0].n_value;
98 if(ifnetaddr == 0)
99 return;
102 if(kread(ifnetaddr, (char*)&ifnethead, sizeof ifnethead) < 0)
103 return;
105 out = in = outb = inb = err = 0;
106 addr = (ulong)TAILQ_FIRST(&ifnethead);
107 while(addr){
108 if(kread(addr, (char*)&ifnet, sizeof ifnet) < 0
109 || kread((ulong)ifnet.if_name, name, 16) < 0)
110 return;
111 name[15] = 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);
129 int
130 rsys(char *name, char *buf, int len)
132 size_t l;
134 l = len;
135 if(sysctlbyname(name, buf, &l, nil, 0) < 0)
136 return -1;
137 buf[l] = 0;
138 return l;
141 vlong
142 isys(char *name)
144 ulong u;
145 size_t l;
147 l = sizeof u;
148 if(sysctlbyname(name, &u, &l, nil, 0) < 0)
149 return 0;
150 return u;
153 void
154 xsysctl(int first)
156 static int pgsize;
157 vlong t;
159 if(first){
160 pgsize = isys("vm.stats.vm.v_page_size");
161 if(pgsize == 0)
162 pgsize = 4096;
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,
167 t*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"));
177 void
178 xcpu(int first)
180 #if 0
181 static int stathz;
182 ulong x[20];
183 struct clockinfo *ci;
184 int n;
186 if(first){
187 if(rsys("kern.clockrate", (char*)&x, sizeof x) < sizeof ci)
188 stathz = 128;
189 else{
190 ci = (struct clockinfo*)x;
191 stathz = ci->stathz;
193 return;
196 if((n=rsys("kern.cp_time", (char*)x, sizeof x)) < 5*sizeof(ulong))
197 return;
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);
203 #endif
206 void
207 xloadavg(int first)
209 double l[3];
211 if(first)
212 return;
214 if(getloadavg(l, 3) < 0)
215 return;
216 Bprint(&bout, "load %d 1000\n", (int)(l[0]*1000.0));
219 void
220 xswap(int first)
222 #if 0
223 static struct kvm_swap s;
224 static ulong pgin, pgout;
225 int i, o;
226 static int pgsize;
228 if(first){
229 pgsize = getpagesize();
230 if(pgsize == 0)
231 pgsize = 4096;
232 return;
235 if(kvm == nil)
236 return;
238 i = isys("vm.stats.vm.v_swappgsin");
239 o = isys("vm.stats.vm.v_swappgsout");
240 if(i != pgin || o != pgout){
241 pgin = i;
242 pgout = o;
243 kvm_getswapinfo(kvm, &s, 1, 0);
247 Bprint(&bout, "swap %lld %lld\n", s.ksw_used*(vlong)pgsize, s.ksw_total*(vlong)pgsize);
248 #endif