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;
27 if(first){
28 fd = open("/proc/apm", OREAD);
29 return;
30 }
31 readfile(fd);
32 tokens(0);
33 if(ntok >= 7 && atoi(tok[6]) != -1)
34 Bprint(&bout, "battery =%d 100\n", atoi(tok[6]));
35 }
37 void
38 xloadavg(int first)
39 {
40 static int fd = -1;
42 if(first){
43 fd = open("/proc/loadavg", OREAD);
44 return;
45 }
47 readfile(fd);
48 tokens(0);
49 if(ntok >= 1)
50 Bprint(&bout, "load =%d 1000\n", (int)(atof(tok[0])*1000));
51 }
53 void
54 xmeminfo(int first)
55 {
56 int i;
57 vlong tot, used;
58 vlong mtot, mfree;
59 static int fd = -1;
61 if(first){
62 fd = open("/proc/meminfo", OREAD);
63 return;
64 }
66 readfile(fd);
67 mtot = 0;
68 for(i=0; i<nline; i++){
69 tokens(i);
70 if(ntok < 3)
71 continue;
72 tot = atoll(tok[1]);
73 used = atoll(tok[2]);
74 if(strcmp(tok[0], "Mem:") == 0)
75 Bprint(&bout, "mem =%lld %lld\n", used/1024, tot/1024);
76 else if(strcmp(tok[0], "Swap:") == 0)
77 Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
78 else if(strcmp(tok[0], "MemTotal:") == 0)
79 mtot = atoll(tok[1]); /* kb */
80 else if(strcmp(tok[0], "MemFree:") == 0){
81 mfree = atoll(tok[1]);
82 if(mtot < mfree)
83 continue;
84 Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
85 }
86 }
87 }
89 void
90 xnet(int first)
91 {
92 int i, n;
93 vlong totb, totp, tote, totin, totou, totinb, totoub, b, p, e, in, ou, inb, oub;
94 char *q;
95 static int fd = -1;
97 if(first){
98 fd = open("/proc/net/dev", OREAD);
99 return;
102 readfile(fd);
103 n = 0;
104 totb = 0;
105 tote = 0;
106 totp = 0;
107 totin = 0;
108 totou = 0;
109 totinb = 0;
110 totoub = 0;
111 for(i=0; i<nline; i++){
112 if((q = strchr(line[i], ':')) != nil)
113 *q = ' ';
114 tokens(i);
115 if(ntok < 8+8)
116 continue;
117 if(strncmp(tok[0], "eth", 3) != 0)
118 continue;
119 inb = atoll(tok[1]);
120 oub = atoll(tok[9]);
121 in = atoll(tok[2]);
122 ou = atoll(tok[10]);
123 b = inb+oub;
124 p = in+ou;
125 e = atoll(tok[3])+atoll(tok[11]);
126 totb += b;
127 totp += p;
128 tote += e;
129 totin += in;
130 totou += ou;
131 totinb += inb;
132 totoub += oub;
133 n++;
135 Bprint(&bout, "etherb %lld %d\n", totb, n*1000000);
136 Bprint(&bout, "ether %lld %d\n", totp, n*1000);
137 Bprint(&bout, "ethererr %lld %d\n", tote, n*1000);
138 Bprint(&bout, "etherin %lld %d\n", totin, n*1000);
139 Bprint(&bout, "etherout %lld %d\n", totou, n*1000);
140 Bprint(&bout, "etherinb %lld %d\n", totinb, n*1000);
141 Bprint(&bout, "etheroutb %lld %d\n", totoub, n*1000);
144 void
145 xstat(int first)
147 static int fd = -1;
148 int i;
150 if(first){
151 fd = open("/proc/stat", OREAD);
152 return;
155 readfile(fd);
156 for(i=0; i<nline; i++){
157 tokens(i);
158 if(ntok < 2)
159 continue;
160 if(strcmp(tok[0], "cpu") == 0 && ntok >= 5){
161 Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
162 Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
163 Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
164 Bprint(&bout, "idle %lld\n", atoll(tok[4]));
166 /*
167 if(strcmp(tok[0], "page") == 0 && ntok >= 3){
168 Bprint(&bout, "pagein %lld 500\n", atoll(tok[1]));
169 Bprint(&bout, "pageout %lld 500\n", atoll(tok[2]));
170 Bprint(&bout, "page %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
172 if(strcmp(tok[0], "swap") == 0 && ntok >= 3){
173 Bprint(&bout, "swapin %lld 500\n", atoll(tok[1]));
174 Bprint(&bout, "swapout %lld 500\n", atoll(tok[2]));
175 Bprint(&bout, "swap %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
177 */
178 if(strcmp(tok[0], "intr") == 0)
179 Bprint(&bout, "interrupt %lld 1000\n", atoll(tok[1]));
180 if(strcmp(tok[0], "ctxt") == 0)
181 Bprint(&bout, "context %lld 1000\n", atoll(tok[1]));
182 if(strcmp(tok[0], "processes") == 0)
183 Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));