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);
12 void (*statfn[])(int) =
13 {
14 xapm,
15 xloadavg,
16 xmeminfo,
17 xnet,
18 xstat,
19 0
20 };
22 void
23 xapm(int first)
24 {
25 static int fd = -1, fdb = -1;
26 int i, last = -1, curr = -1;
28 if(first){
29 fd = open("/proc/acpi/battery/BAT0/info", OREAD);
30 fdb = open("/proc/acpi/battery/BAT0/state", OREAD);
31 return;
32 }
33 if(fd == -1 || fdb == -1)
34 return;
36 readfile(fd);
37 for(i=0; i<nline; i++){
38 tokens(i);
39 if(ntok < 3)
40 continue;
41 if(strcmp(tok[0], "last") == 0 && strcmp(tok[1], "full") == 0)
42 last = atoi(tok[3]);
43 }
44 readfile(fdb);
45 for(i = 0; i < nline; i++) {
46 tokens(i);
47 if(ntok < 3)
48 continue;
49 if(strcmp(tok[0], "remaining") == 0 && strcmp(tok[1], "capacity:") == 0)
50 curr = atoi(tok[2]);
51 }
53 if(curr != -1 && last != -1)
54 Bprint(&bout, "battery =%d 100\n", (int)(((float)curr/(float)last)*100.0));
56 }
58 void
59 xloadavg(int first)
60 {
61 static int fd = -1;
63 if(first){
64 fd = open("/proc/loadavg", OREAD);
65 return;
66 }
68 readfile(fd);
69 tokens(0);
70 if(ntok >= 1)
71 Bprint(&bout, "load =%d 1000\n", (int)(atof(tok[0])*1000));
72 }
74 void
75 xmeminfo(int first)
76 {
77 int i;
78 vlong tot, used;
79 vlong mtot, mfree;
80 static int fd = -1;
82 if(first){
83 fd = open("/proc/meminfo", OREAD);
84 return;
85 }
87 readfile(fd);
88 mtot = 0;
89 for(i=0; i<nline; i++){
90 tokens(i);
91 if(ntok < 3)
92 continue;
93 tot = atoll(tok[1]);
94 used = atoll(tok[2]);
95 if(strcmp(tok[0], "Mem:") == 0)
96 Bprint(&bout, "mem =%lld %lld\n", used/1024, tot/1024);
97 else if(strcmp(tok[0], "Swap:") == 0)
98 Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
99 else if(strcmp(tok[0], "MemTotal:") == 0)
100 mtot = atoll(tok[1]); /* kb */
101 else if(strcmp(tok[0], "MemFree:") == 0){
102 mfree = atoll(tok[1]);
103 if(mtot < mfree)
104 continue;
105 Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
110 void
111 xnet(int first)
113 int i, n;
114 vlong totb, totp, tote, totin, totou, totinb, totoub, b, p, e, in, ou, inb, oub;
115 char *q;
116 static int fd = -1;
118 if(first){
119 fd = open("/proc/net/dev", OREAD);
120 return;
123 readfile(fd);
124 n = 0;
125 totb = 0;
126 tote = 0;
127 totp = 0;
128 totin = 0;
129 totou = 0;
130 totinb = 0;
131 totoub = 0;
132 for(i=0; i<nline; i++){
133 if((q = strchr(line[i], ':')) != nil)
134 *q = ' ';
135 tokens(i);
136 if(ntok < 8+8)
137 continue;
138 if(strncmp(tok[0], "eth", 3) != 0)
139 continue;
140 inb = atoll(tok[1]);
141 oub = atoll(tok[9]);
142 in = atoll(tok[2]);
143 ou = atoll(tok[10]);
144 b = inb+oub;
145 p = in+ou;
146 e = atoll(tok[3])+atoll(tok[11]);
147 totb += b;
148 totp += p;
149 tote += e;
150 totin += in;
151 totou += ou;
152 totinb += inb;
153 totoub += oub;
154 n++;
156 Bprint(&bout, "etherb %lld %d\n", totb, n*1000000);
157 Bprint(&bout, "ether %lld %d\n", totp, n*1000);
158 Bprint(&bout, "ethererr %lld %d\n", tote, n*1000);
159 Bprint(&bout, "etherin %lld %d\n", totin, n*1000);
160 Bprint(&bout, "etherout %lld %d\n", totou, n*1000);
161 Bprint(&bout, "etherinb %lld %d\n", totinb, n*1000);
162 Bprint(&bout, "etheroutb %lld %d\n", totoub, n*1000);
165 void
166 xstat(int first)
168 static int fd = -1;
169 int i;
171 if(first){
172 fd = open("/proc/stat", OREAD);
173 return;
176 readfile(fd);
177 for(i=0; i<nline; i++){
178 tokens(i);
179 if(ntok < 2)
180 continue;
181 if(strcmp(tok[0], "cpu") == 0 && ntok >= 5){
182 Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
183 Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
184 Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
185 Bprint(&bout, "idle %lld\n", atoll(tok[4]));
187 /*
188 if(strcmp(tok[0], "page") == 0 && ntok >= 3){
189 Bprint(&bout, "pagein %lld 500\n", atoll(tok[1]));
190 Bprint(&bout, "pageout %lld 500\n", atoll(tok[2]));
191 Bprint(&bout, "page %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
193 if(strcmp(tok[0], "swap") == 0 && ntok >= 3){
194 Bprint(&bout, "swapin %lld 500\n", atoll(tok[1]));
195 Bprint(&bout, "swapout %lld 500\n", atoll(tok[2]));
196 Bprint(&bout, "swap %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
198 */
199 if(strcmp(tok[0], "intr") == 0)
200 Bprint(&bout, "interrupt %lld 1000\n", atoll(tok[1]));
201 if(strcmp(tok[0], "ctxt") == 0)
202 Bprint(&bout, "context %lld 1000\n", atoll(tok[1]));
203 if(strcmp(tok[0], "processes") == 0)
204 Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));