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 #if 0
87 ulong out, in, outb, inb, err;
88 static ulong ifnetaddr;
89 ulong addr;
90 struct ifnet ifnet;
91 struct ifnethead ifnethead;
92 char name[16];
94 if(first)
95 return;
97 if(ifnetaddr == 0){
98 ifnetaddr = nl[0].n_value;
99 if(ifnetaddr == 0)
100 return;
103 if(kread(ifnetaddr, (char*)&ifnethead, sizeof ifnethead) < 0)
104 return;
106 out = in = outb = inb = err = 0;
107 addr = (ulong)TAILQ_FIRST(&ifnethead);
108 while(addr){
109 if(kread(addr, (char*)&ifnet, sizeof ifnet) < 0
110 || kread((ulong)ifnet.if_name, name, 16) < 0)
111 return;
112 name[15] = 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);
127 #endif
128 USED(first);
132 int
133 rsys(char *name, char *buf, int len)
135 size_t l;
137 l = len;
138 if(sysctlbyname(name, buf, &l, nil, 0) < 0)
139 return -1;
140 buf[l] = 0;
141 return l;
144 vlong
145 isys(char *name)
147 ulong u;
148 size_t l;
150 l = sizeof u;
151 if(sysctlbyname(name, &u, &l, nil, 0) < 0)
152 return 0;
153 return u;
156 void
157 xsysctl(int first)
159 static int pgsize;
160 vlong t;
162 if(first){
163 pgsize = isys("vm.stats.vm.v_page_size");
164 if(pgsize == 0)
165 pgsize = 4096;
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,
170 t*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"));
180 void
181 xcpu(int first)
183 #if 0
184 static int stathz;
185 ulong x[20];
186 struct clockinfo *ci;
187 int n;
189 if(first){
190 if(rsys("kern.clockrate", (char*)&x, sizeof x) < sizeof ci)
191 stathz = 128;
192 else{
193 ci = (struct clockinfo*)x;
194 stathz = ci->stathz;
196 return;
199 if((n=rsys("kern.cp_time", (char*)x, sizeof x)) < 5*sizeof(ulong))
200 return;
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);
206 #endif
209 void
210 xloadavg(int first)
212 double l[3];
214 if(first)
215 return;
217 if(getloadavg(l, 3) < 0)
218 return;
219 Bprint(&bout, "load %d 1000\n", (int)(l[0]*1000.0));
222 void
223 xswap(int first)
225 #if 0
226 static struct kvm_swap s;
227 static ulong pgin, pgout;
228 int i, o;
229 static int pgsize;
231 if(first){
232 pgsize = getpagesize();
233 if(pgsize == 0)
234 pgsize = 4096;
235 return;
238 if(kvm == nil)
239 return;
241 i = isys("vm.stats.vm.v_swappgsin");
242 o = isys("vm.stats.vm.v_swappgsout");
243 if(i != pgin || o != pgout){
244 pgin = i;
245 pgout = o;
246 kvm_getswapinfo(kvm, &s, 1, 0);
250 Bprint(&bout, "swap %lld %lld\n", s.ksw_used*(vlong)pgsize, s.ksw_total*(vlong)pgsize);
251 #endif