Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "dat.h"
6 void xapm(int);
7 void xloadavg(int);
8 void xmeminfo(int);
9 void xnet(int);
10 void xstat(int);
11 void xvmstat(int);
12 void xwireless(int);
14 void (*statfn[])(int) =
15 {
16 xapm,
17 xloadavg,
18 xmeminfo,
19 xnet,
20 xstat,
21 xvmstat,
22 xwireless,
23 0
24 };
26 void
27 xapm(int first)
28 {
29 static int fd = -1, fdb = -1;
30 int i, last = -1, curr = -1;
32 if(first){
33 fd = open("/proc/acpi/battery/BAT0/info", OREAD);
34 fdb = open("/proc/acpi/battery/BAT0/state", OREAD);
35 return;
36 }
37 if(fd == -1 || fdb == -1)
38 return;
40 readfile(fd);
41 for(i=0; i<nline; i++){
42 tokens(i);
43 if(ntok < 3)
44 continue;
45 if(strcmp(tok[0], "last") == 0 && strcmp(tok[1], "full") == 0)
46 last = atoi(tok[3]);
47 }
48 readfile(fdb);
49 for(i = 0; i < nline; i++) {
50 tokens(i);
51 if(ntok < 3)
52 continue;
53 if(strcmp(tok[0], "remaining") == 0 && strcmp(tok[1], "capacity:") == 0)
54 curr = atoi(tok[2]);
55 }
57 if(curr != -1 && last != -1)
58 Bprint(&bout, "battery =%d 100\n", (int)(((float)curr/(float)last)*100.0));
60 }
62 void
63 xloadavg(int first)
64 {
65 static int fd = -1;
67 if(first){
68 fd = open("/proc/loadavg", OREAD);
69 return;
70 }
72 readfile(fd);
73 tokens(0);
74 if(ntok >= 1)
75 Bprint(&bout, "load =%d 1000\n", (int)(atof(tok[0])*1000));
76 }
78 void
79 xmeminfo(int first)
80 {
81 int i;
82 vlong tot, used;
83 vlong mtot, mfree;
84 vlong stot, sfree;
85 static int fd = -1;
87 if(first){
88 fd = open("/proc/meminfo", OREAD);
89 return;
90 }
92 readfile(fd);
93 mtot = 0;
94 stot = 0;
95 mfree = 0;
96 sfree = 0;
97 for(i=0; i<nline; i++){
98 tokens(i);
99 if(ntok < 3)
100 continue;
101 tot = atoll(tok[1]);
102 used = atoll(tok[2]);
103 if(strcmp(tok[0], "Mem:") == 0)
104 Bprint(&bout, "mem =%lld %lld\n", used/1024, tot/1024);
105 else if(strcmp(tok[0], "Swap:") == 0)
106 Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
107 else if(strcmp(tok[0], "MemTotal:") == 0)
108 mtot = atoll(tok[1]); /* kb */
109 else if(strcmp(tok[0], "MemFree:") == 0)
110 mfree += atoll(tok[1]);
111 else if(strcmp(tok[0], "Buffers:") == 0)
112 mfree += atoll(tok[1]);
113 else if(strcmp(tok[0], "Cached:") == 0){
114 mfree += atoll(tok[1]);
115 if(mtot < mfree)
116 continue;
117 Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
118 }else if(strcmp(tok[0], "SwapTotal:") == 0)
119 stot = atoll(tok[1]); /* kb */
120 else if(strcmp(tok[0], "SwapFree:") == 0){
121 sfree = atoll(tok[1]);
122 if(stot < sfree)
123 continue;
124 Bprint(&bout, "swap =%lld %lld\n", stot-sfree, stot);
129 void
130 xnet(int first)
132 int i, n;
133 vlong totb, totp, tote, totin, totou, totinb, totoub, b, p, e, in, ou, inb, oub;
134 char *q;
135 static int fd = -1;
137 if(first){
138 fd = open("/proc/net/dev", OREAD);
139 return;
142 readfile(fd);
143 n = 0;
144 totb = 0;
145 tote = 0;
146 totp = 0;
147 totin = 0;
148 totou = 0;
149 totinb = 0;
150 totoub = 0;
151 for(i=0; i<nline; i++){
152 if((q = strchr(line[i], ':')) != nil)
153 *q = ' ';
154 tokens(i);
155 if(ntok < 8+8)
156 continue;
157 if(strncmp(tok[0], "eth", 3) != 0 && strncmp(tok[0], "wlan", 4) != 0)
158 continue;
159 inb = atoll(tok[1]);
160 oub = atoll(tok[9]);
161 in = atoll(tok[2]);
162 ou = atoll(tok[10]);
163 b = inb+oub;
164 p = in+ou;
165 e = atoll(tok[3])+atoll(tok[11]);
166 totb += b;
167 totp += p;
168 tote += e;
169 totin += in;
170 totou += ou;
171 totinb += inb;
172 totoub += oub;
173 n++;
175 Bprint(&bout, "etherb %lld %d\n", totb, n*1000000);
176 Bprint(&bout, "ether %lld %d\n", totp, n*1000);
177 Bprint(&bout, "ethererr %lld %d\n", tote, n*1000);
178 Bprint(&bout, "etherin %lld %d\n", totin, n*1000);
179 Bprint(&bout, "etherout %lld %d\n", totou, n*1000);
180 Bprint(&bout, "etherinb %lld %d\n", totinb, n*1000);
181 Bprint(&bout, "etheroutb %lld %d\n", totoub, n*1000);
184 void
185 xstat(int first)
187 static int fd = -1;
188 int i;
190 if(first){
191 fd = open("/proc/stat", OREAD);
192 return;
195 readfile(fd);
196 for(i=0; i<nline; i++){
197 tokens(i);
198 if(ntok < 2)
199 continue;
200 if(strcmp(tok[0], "cpu") == 0 && ntok >= 5){
201 Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
202 Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
203 Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
204 Bprint(&bout, "idle %lld 100\n", atoll(tok[4]));
206 /*
207 if(strcmp(tok[0], "page") == 0 && ntok >= 3){
208 Bprint(&bout, "pagein %lld 500\n", atoll(tok[1]));
209 Bprint(&bout, "pageout %lld 500\n", atoll(tok[2]));
210 Bprint(&bout, "page %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
212 if(strcmp(tok[0], "swap") == 0 && ntok >= 3){
213 Bprint(&bout, "swapin %lld 500\n", atoll(tok[1]));
214 Bprint(&bout, "swapout %lld 500\n", atoll(tok[2]));
215 Bprint(&bout, "swap %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
217 */
218 if(strcmp(tok[0], "intr") == 0)
219 Bprint(&bout, "intr %lld 1000\n", atoll(tok[1]));
220 if(strcmp(tok[0], "ctxt") == 0)
221 Bprint(&bout, "context %lld 10000\n", atoll(tok[1]));
222 if(strcmp(tok[0], "processes") == 0)
223 Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));
227 void
228 xvmstat(int first)
230 static int fd = -1;
231 int i;
233 if(first){
234 fd = open("/proc/vmstat", OREAD);
235 return;
238 readfile(fd);
239 for(i=0; i<nline; i++){
240 tokens(i);
241 if(ntok < 2)
242 continue;
243 if(strcmp(tok[0], "pgfault") == 0)
244 Bprint(&bout, "fault %lld 100000\n", atoll(tok[1]));
248 void
249 xwireless(int first)
251 static int fd = -1;
252 int i;
254 if(first){
255 fd = open("/proc/net/wireless", OREAD);
256 return;
259 readfile(fd);
260 for(i=0; i<nline; i++){
261 tokens(i);
262 if(ntok < 3)
263 continue;
264 if(strcmp(tok[0], "wlan0:") == 0)
265 Bprint(&bout, "802.11 =%lld 100\n", atoll(tok[2]));