aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/auxstats/Linux.c
blob: 7bc4b7206881f4756cf9a951ba4f21275e4b8240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "dat.h"

void xapm(int);
void xloadavg(int);
void xmeminfo(int);
void xnet(int);
void xstat(int);
void xvmstat(int);
void xwireless(int);

void (*statfn[])(int) =
{
	xapm,
	xloadavg,
	xmeminfo,
	xnet,
	xstat,
	xvmstat,
	xwireless,
	0
};

void
xapm(int first)
{
	static int fd = -1;
	int curr = -1;

	if(first){
		fd = open("/sys/class/power_supply/BAT0/capacity", OREAD);
		return;
	}
	if(fd == -1)
		return;

	readfile(fd);
	tokens(0);
	curr = atoi(tok[0]);

	if(curr != -1)
		Bprint(&bout, "battery =%d 100\n", curr);

}

void
xloadavg(int first)
{
	static int fd = -1;

	if(first){
		fd = open("/proc/loadavg", OREAD);
		return;
	}

	readfile(fd);
	tokens(0);
	if(ntok >= 1)
		Bprint(&bout, "load =%d 1000\n", (int)(atof(tok[0])*1000));
}

void
xmeminfo(int first)
{
	int i;
	vlong tot, used;
	vlong mtot, mfree;
	vlong stot, sfree;
	static int fd = -1;

	if(first){
		fd = open("/proc/meminfo", OREAD);
		return;
	}

	readfile(fd);
	mtot = 0;
	stot = 0;
	mfree = 0;
	sfree = 0;
	for(i=0; i<nline; i++){
		tokens(i);
		if(ntok < 3)
			continue;
		tot = atoll(tok[1]);
		used = atoll(tok[2]);
		if(strcmp(tok[0], "Mem:") == 0)
			Bprint(&bout, "mem =%lld %lld\n", used/1024, tot/1024);
		else if(strcmp(tok[0], "Swap:") == 0)
			Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
		else if(strcmp(tok[0], "MemTotal:") == 0)
			mtot = atoll(tok[1]);	/* kb */
		else if(strcmp(tok[0], "MemFree:") == 0)
			mfree += atoll(tok[1]);
		else if(strcmp(tok[0], "Buffers:") == 0)
			mfree += atoll(tok[1]);
		else if(strcmp(tok[0], "Cached:") == 0){
			mfree += atoll(tok[1]);
			if(mtot < mfree)
				continue;
			Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
		}else if(strcmp(tok[0], "SwapTotal:") == 0)
			stot = atoll(tok[1]);	/* kb */
		else if(strcmp(tok[0], "SwapFree:") == 0){
			sfree = atoll(tok[1]);
			if(stot < sfree)
				continue;
			Bprint(&bout, "swap =%lld %lld\n", stot-sfree, stot);
		}
	}
}

void
xnet(int first)
{
	int i, n;
	vlong totb, totp, tote, totin, totou, totinb, totoub, b, p, e, in, ou, inb, oub;
	char *q;
	static int fd = -1;

	if(first){
		fd = open("/proc/net/dev", OREAD);
		return;
	}

	readfile(fd);
	n = 0;
	totb = 0;
	tote = 0;
	totp = 0;
	totin = 0;
	totou = 0;
	totinb = 0;
	totoub = 0;
	for(i=0; i<nline; i++){
		if((q = strchr(line[i], ':')) != nil)
			*q = ' ';
		tokens(i);
		if(ntok < 8+8)
			continue;
		if(strncmp(tok[0], "eth", 3) != 0 && strncmp(tok[0], "wlan", 4) != 0)
			continue;
		inb = atoll(tok[1]);
		oub = atoll(tok[9]);
		in = atoll(tok[2]);
		ou = atoll(tok[10]);
		b = inb+oub;
		p = in+ou;
		e = atoll(tok[3])+atoll(tok[11]);
		totb += b;
		totp += p;
		tote += e;
		totin += in;
		totou += ou;
		totinb += inb;
		totoub += oub;
		n++;
	}
	Bprint(&bout, "etherb %lld %d\n", totb, n*1000000);
	Bprint(&bout, "ether %lld %d\n", totp, n*1000);
	Bprint(&bout, "ethererr %lld %d\n", tote, n*1000);
	Bprint(&bout, "etherin %lld %d\n", totin, n*1000);
	Bprint(&bout, "etherout %lld %d\n", totou, n*1000);
	Bprint(&bout, "etherinb %lld %d\n", totinb, n*1000);
	Bprint(&bout, "etheroutb %lld %d\n", totoub, n*1000);
}

void
xstat(int first)
{
	static int fd = -1;
	int i;

	if(first){
		fd = open("/proc/stat", OREAD);
		return;
	}

	readfile(fd);
	for(i=0; i<nline; i++){
		tokens(i);
		if(ntok < 2)
			continue;
		if(strcmp(tok[0], "cpu") == 0 && ntok >= 5){
			Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
			Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
			Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
			Bprint(&bout, "idle %lld 100\n", atoll(tok[4]));
		}
	/*
		if(strcmp(tok[0], "page") == 0 && ntok >= 3){
			Bprint(&bout, "pagein %lld 500\n", atoll(tok[1]));
			Bprint(&bout, "pageout %lld 500\n", atoll(tok[2]));
			Bprint(&bout, "page %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
		}
		if(strcmp(tok[0], "swap") == 0 && ntok >= 3){
			Bprint(&bout, "swapin %lld 500\n", atoll(tok[1]));
			Bprint(&bout, "swapout %lld 500\n", atoll(tok[2]));
			Bprint(&bout, "swap %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
		}
	*/
		if(strcmp(tok[0], "intr") == 0)
			Bprint(&bout, "intr %lld 1000\n", atoll(tok[1]));
		if(strcmp(tok[0], "ctxt") == 0)
			Bprint(&bout, "context %lld 10000\n", atoll(tok[1]));
		if(strcmp(tok[0], "processes") == 0)
			Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));
	}
}

void
xvmstat(int first)
{
	static int fd = -1;
	int i;

	if(first){
		fd = open("/proc/vmstat", OREAD);
		return;
	}

	readfile(fd);
	for(i=0; i<nline; i++){
		tokens(i);
		if(ntok < 2)
			continue;
		if(strcmp(tok[0], "pgfault") == 0)
			Bprint(&bout, "fault %lld 100000\n", atoll(tok[1]));
	}
}

void
xwireless(int first)
{
	static int fd = -1;
	int i;

	if(first){
		fd = open("/proc/net/wireless", OREAD);
		return;
	}

	readfile(fd);
	for(i=0; i<nline; i++){
		tokens(i);
		if(ntok < 3)
			continue;
		if(strcmp(tok[0], "wlan0:") == 0)
			Bprint(&bout, "802.11 =%lld 100\n", atoll(tok[2]));
	}
}