blob: 5d5b83b8d5078bb586c56b2918b9dc1361bed744 [file] [log] [blame]
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02002 * Licensed under GPLv2, see file LICENSE in this source tree.
3 *
4 * Based on nanotop.c from floppyfw project
5 *
Denys Vlasenkobdaea462010-08-16 20:42:14 +02006 * Contact me: vda.linux@googlemail.com
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 */
Denis Vlasenko1db39b22006-10-11 20:59:02 +00008
Denys Vlasenkoa6041862010-12-13 15:17:22 +01009//config:config NMETER
10//config: bool "nmeter"
11//config: default y
12//config: help
13//config: Prints selected system stats continuously, one line per update.
14
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010015//applet:IF_NMETER(APPLET(nmeter, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkoa6041862010-12-13 15:17:22 +010016
17//kbuild:lib-$(CONFIG_NMETER) += nmeter.o
18
19//usage:#define nmeter_trivial_usage
20//usage: "[-d MSEC] FORMAT_STRING"
21//usage:#define nmeter_full_usage "\n\n"
22//usage: "Monitor system in real time"
23//usage: "\n"
24//usage: "\n -d MSEC Milliseconds between updates (default:1000)"
25//usage: "\n"
26//usage: "\nFormat specifiers:"
Denys Vlasenkoabb09522011-05-26 09:00:23 +020027//usage: "\n %Nc or %[cN] CPU. N - bar size (default:10)"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010028//usage: "\n (displays: S:system U:user N:niced D:iowait I:irq i:softirq)"
Denys Vlasenkoabb09522011-05-26 09:00:23 +020029//usage: "\n %[nINTERFACE] Network INTERFACE"
30//usage: "\n %m Allocated memory"
31//usage: "\n %[mf] Free memory"
32//usage: "\n %[mt] Total memory"
33//usage: "\n %s Allocated swap"
34//usage: "\n %f Number of used file descriptors"
35//usage: "\n %Ni Total/specific IRQ rate"
36//usage: "\n %x Context switch rate"
37//usage: "\n %p Forks"
38//usage: "\n %[pn] # of processes"
39//usage: "\n %b Block io"
40//usage: "\n %Nt Time (with N decimal points)"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010041//usage: "\n %r Print <cr> instead of <lf> at EOL"
42
Denis Vlasenko1db39b22006-10-11 20:59:02 +000043//TODO:
44// simplify code
45// /proc/locks
46// /proc/stat:
47// disk_io: (3,0):(22272,17897,410702,4375,54750)
48// btime 1059401962
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000049//TODO: use sysinfo libc call/syscall, if appropriate
50// (faster than open/read/close):
51// sysinfo({uptime=15017, loads=[5728, 15040, 16480]
52// totalram=2107416576, freeram=211525632, sharedram=0, bufferram=157204480}
53// totalswap=134209536, freeswap=134209536, procs=157})
Denis Vlasenko1db39b22006-10-11 20:59:02 +000054
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000055#include "libbb.h"
Denis Vlasenko1db39b22006-10-11 20:59:02 +000056
57typedef unsigned long long ullong;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000058
Denis Vlasenkobfc93a32008-09-26 11:12:09 +000059enum { /* Preferably use powers of 2 */
60 PROC_MIN_FILE_SIZE = 256,
61 PROC_MAX_FILE_SIZE = 16 * 1024,
62};
Denis Vlasenko1db39b22006-10-11 20:59:02 +000063
64typedef struct proc_file {
Denis Vlasenko1db39b22006-10-11 20:59:02 +000065 char *file;
Denis Vlasenkobfc93a32008-09-26 11:12:09 +000066 int file_sz;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000067 smallint last_gen;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000068} proc_file;
69
Denis Vlasenko199c0d52007-05-30 14:48:38 +000070static const char *const proc_name[] = {
71 "stat", // Must match the order of proc_file's!
72 "loadavg",
73 "net/dev",
74 "meminfo",
75 "diskstats",
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000076 "sys/fs/file-nr"
Denis Vlasenko199c0d52007-05-30 14:48:38 +000077};
78
79struct globals {
80 // Sample generation flip-flop
81 smallint gen;
82 // Linux 2.6? (otherwise assumes 2.4)
83 smallint is26;
84 // 1 if sample delay is not an integer fraction of a second
85 smallint need_seconds;
86 char *cur_outbuf;
87 const char *final_str;
88 int delta;
89 int deltanz;
90 struct timeval tv;
91#define first_proc_file proc_stat
92 proc_file proc_stat; // Must match the order of proc_name's!
93 proc_file proc_loadavg;
94 proc_file proc_net_dev;
95 proc_file proc_meminfo;
96 proc_file proc_diskstats;
97 proc_file proc_sys_fs_filenr;
98};
99#define G (*ptr_to_globals)
100#define gen (G.gen )
101#define is26 (G.is26 )
102#define need_seconds (G.need_seconds )
103#define cur_outbuf (G.cur_outbuf )
104#define final_str (G.final_str )
105#define delta (G.delta )
106#define deltanz (G.deltanz )
107#define tv (G.tv )
108#define proc_stat (G.proc_stat )
109#define proc_loadavg (G.proc_loadavg )
110#define proc_net_dev (G.proc_net_dev )
111#define proc_meminfo (G.proc_meminfo )
112#define proc_diskstats (G.proc_diskstats )
113#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000114#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000115 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
116 cur_outbuf = outbuf; \
117 final_str = "\n"; \
118 deltanz = delta = 1000000; \
119} while (0)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000120
Denis Vlasenkoef4bb262007-06-04 12:21:53 +0000121// We depend on this being a char[], not char* - we take sizeof() of it
122#define outbuf bb_common_bufsiz1
123
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000124static inline void reset_outbuf(void)
125{
126 cur_outbuf = outbuf;
127}
128
129static inline int outbuf_count(void)
130{
131 return cur_outbuf - outbuf;
132}
133
134static void print_outbuf(void)
135{
136 int sz = cur_outbuf - outbuf;
137 if (sz > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000138 xwrite(STDOUT_FILENO, outbuf, sz);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000139 cur_outbuf = outbuf;
140 }
141}
142
143static void put(const char *s)
144{
145 int sz = strlen(s);
146 if (sz > outbuf + sizeof(outbuf) - cur_outbuf)
147 sz = outbuf + sizeof(outbuf) - cur_outbuf;
148 memcpy(cur_outbuf, s, sz);
149 cur_outbuf += sz;
150}
151
152static void put_c(char c)
153{
154 if (cur_outbuf < outbuf + sizeof(outbuf))
155 *cur_outbuf++ = c;
156}
157
158static void put_question_marks(int count)
159{
160 while (count--)
161 put_c('?');
162}
163
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000164static void readfile_z(proc_file *pf, const char* fname)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000165{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000166// open_read_close() will do two reads in order to be sure we are at EOF,
167// and we don't need/want that.
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000168 int fd;
169 int sz, rdsz;
170 char *buf;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000171
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000172 sz = pf->file_sz;
173 buf = pf->file;
174 if (!buf) {
175 buf = xmalloc(PROC_MIN_FILE_SIZE);
176 sz = PROC_MIN_FILE_SIZE;
177 }
178 again:
179 fd = xopen(fname, O_RDONLY);
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000180 buf[0] = '\0';
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000181 rdsz = read(fd, buf, sz-1);
Denis Vlasenkobc2fd372008-06-30 07:33:52 +0000182 close(fd);
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000183 if (rdsz > 0) {
184 if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) {
185 sz *= 2;
186 buf = xrealloc(buf, sz);
187 goto again;
188 }
189 buf[rdsz] = '\0';
190 }
191 pf->file_sz = sz;
192 pf->file = buf;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000193}
194
195static const char* get_file(proc_file *pf)
196{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000197 if (pf->last_gen != gen) {
198 pf->last_gen = gen;
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000199 readfile_z(pf, proc_name[pf - &first_proc_file]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000200 }
201 return pf->file;
202}
203
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000204static ullong read_after_slash(const char *p)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000205{
206 p = strchr(p, '/');
207 if (!p) return 0;
208 return strtoull(p+1, NULL, 10);
209}
210
211enum conv_type { conv_decimal, conv_slash };
212
213// Reads decimal values from line. Values start after key, for example:
214// "cpu 649369 0 341297 4336769..." - key is "cpu" here.
215// Values are stored in vec[]. arg_ptr has list of positions
216// we are interested in: for example: 1,2,5 - we want 1st, 2nd and 5th value.
217static int vrdval(const char* p, const char* key,
218 enum conv_type conv, ullong *vec, va_list arg_ptr)
219{
220 int indexline;
221 int indexnext;
222
223 p = strstr(p, key);
224 if (!p) return 1;
225
226 p += strlen(key);
227 indexline = 1;
228 indexnext = va_arg(arg_ptr, int);
229 while (1) {
230 while (*p == ' ' || *p == '\t') p++;
231 if (*p == '\n' || *p == '\0') break;
232
233 if (indexline == indexnext) { // read this value
234 *vec++ = conv==conv_decimal ?
235 strtoull(p, NULL, 10) :
236 read_after_slash(p);
237 indexnext = va_arg(arg_ptr, int);
238 }
239 while (*p > ' ') p++; // skip over value
240 indexline++;
241 }
242 return 0;
243}
244
245// Parses files with lines like "cpu0 21727 0 15718 1813856 9461 10485 0 0":
246// rdval(file_contents, "string_to_find", result_vector, value#, value#...)
247// value# start with 1
248static int rdval(const char* p, const char* key, ullong *vec, ...)
249{
250 va_list arg_ptr;
251 int result;
252
253 va_start(arg_ptr, vec);
254 result = vrdval(p, key, conv_decimal, vec, arg_ptr);
255 va_end(arg_ptr);
256
257 return result;
258}
259
260// Parses files with lines like "... ... ... 3/148 ...."
261static int rdval_loadavg(const char* p, ullong *vec, ...)
262{
263 va_list arg_ptr;
264 int result;
265
266 va_start(arg_ptr, vec);
267 result = vrdval(p, "", conv_slash, vec, arg_ptr);
268 va_end(arg_ptr);
269
270 return result;
271}
272
273// Parses /proc/diskstats
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100274// 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000275// 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933
276// 3 1 hda1 0 0 0 0 <- ignore if only 4 fields
Denys Vlasenko3b160342011-11-01 23:34:46 +0100277// Linux 3.0 (maybe earlier) started printing full stats for hda1 too.
278// Had to add code which skips such devices.
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000279static int rdval_diskstats(const char* p, ullong *vec)
280{
Denys Vlasenko3b160342011-11-01 23:34:46 +0100281 char devname[32];
282 unsigned devname_len = 0;
283 int value_idx = 0;
284
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000285 vec[0] = 0;
286 vec[1] = 0;
287 while (1) {
Denys Vlasenko3b160342011-11-01 23:34:46 +0100288 value_idx++;
289 while (*p == ' ' || *p == '\t')
290 p++;
291 if (*p == '\0')
292 break;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000293 if (*p == '\n') {
Denys Vlasenko3b160342011-11-01 23:34:46 +0100294 value_idx = 0;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000295 p++;
296 continue;
297 }
Denys Vlasenko3b160342011-11-01 23:34:46 +0100298 if (value_idx == 3) {
299 char *end = strchrnul(p, ' ');
300 /* If this a hda1-like device (same prefix as last one + digit)? */
301 if (devname_len && strncmp(devname, p, devname_len) == 0 && isdigit(p[devname_len])) {
302 p = end;
303 goto skip_line; /* skip entire line */
304 }
305 /* It is not. Remember the name for future checks */
306 devname_len = end - p;
307 if (devname_len > sizeof(devname)-1)
308 devname_len = sizeof(devname)-1;
309 strncpy(devname, p, devname_len);
310 /* devname[devname_len] = '\0'; - not really needed */
311 p = end;
312 } else
313 if (value_idx == 6) {
314 // TODO: *sectorsize (don't know how to find out sectorsize)
315 vec[0] += strtoull(p, NULL, 10);
316 } else
317 if (value_idx == 10) {
318 // TODO: *sectorsize (don't know how to find out sectorsize)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000319 vec[1] += strtoull(p, NULL, 10);
Denys Vlasenko3b160342011-11-01 23:34:46 +0100320 skip_line:
321 while (*p != '\n' && *p != '\0')
322 p++;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000323 continue;
324 }
Denys Vlasenko3b160342011-11-01 23:34:46 +0100325 while ((unsigned char)(*p) > ' ') // skip over value
326 p++;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000327 }
328 return 0;
329}
330
331static void scale(ullong ul)
332{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000333 char buf[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000334
335 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200336 smart_ulltoa4(ul, buf, " kmgtpezy")[0] = '\0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000337 put(buf);
338}
339
340
341#define S_STAT(a) \
342typedef struct a { \
343 struct s_stat *next; \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200344 void (*collect)(struct a *s) FAST_FUNC; \
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000345 const char *label;
346#define S_STAT_END(a) } a;
347
348S_STAT(s_stat)
349S_STAT_END(s_stat)
350
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200351static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000352{
353}
354
355static s_stat* init_literal(void)
356{
Denis Vlasenkobefd5ea2008-06-28 23:33:02 +0000357 s_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000358 s->collect = collect_literal;
359 return (s_stat*)s;
360}
361
362static s_stat* init_delay(const char *param)
363{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000364 delta = strtoul(param, NULL, 0) * 1000; /* param can be "" */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000365 deltanz = delta > 0 ? delta : 1;
366 need_seconds = (1000000%deltanz) != 0;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000367 return NULL;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000368}
369
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000370static s_stat* init_cr(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000371{
372 final_str = "\r";
373 return (s_stat*)0;
374}
375
376
377// user nice system idle iowait irq softirq (last 3 only in 2.6)
378//cpu 649369 0 341297 4336769 11640 7122 1183
379//cpuN 649369 0 341297 4336769 11640 7122 1183
380enum { CPU_FIELDCNT = 7 };
381S_STAT(cpu_stat)
382 ullong old[CPU_FIELDCNT];
383 int bar_sz;
384 char *bar;
385S_STAT_END(cpu_stat)
386
387
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200388static void FAST_FUNC collect_cpu(cpu_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000389{
390 ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
391 unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
392 ullong all = 0;
393 int norm_all = 0;
394 int bar_sz = s->bar_sz;
395 char *bar = s->bar;
396 int i;
397
398 if (rdval(get_file(&proc_stat), "cpu ", data, 1, 2, 3, 4, 5, 6, 7)) {
399 put_question_marks(bar_sz);
400 return;
401 }
402
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000403 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000404 ullong old = s->old[i];
405 if (data[i] < old) old = data[i]; //sanitize
406 s->old[i] = data[i];
407 all += (data[i] -= old);
408 }
409
410 if (all) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000411 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000412 ullong t = bar_sz * data[i];
413 norm_all += data[i] = t / all;
414 frac[i] = t % all;
415 }
416
417 while (norm_all < bar_sz) {
418 unsigned max = frac[0];
419 int pos = 0;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000420 for (i = 1; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000421 if (frac[i] > max) max = frac[i], pos = i;
422 }
423 frac[pos] = 0; //avoid bumping up same value twice
424 data[pos]++;
425 norm_all++;
426 }
427
428 memset(bar, '.', bar_sz);
429 memset(bar, 'S', data[2]); bar += data[2]; //sys
430 memset(bar, 'U', data[0]); bar += data[0]; //usr
431 memset(bar, 'N', data[1]); bar += data[1]; //nice
432 memset(bar, 'D', data[4]); bar += data[4]; //iowait
433 memset(bar, 'I', data[5]); bar += data[5]; //irq
434 memset(bar, 'i', data[6]); bar += data[6]; //softirq
435 } else {
436 memset(bar, '?', bar_sz);
437 }
438 put(s->bar);
439}
440
441
442static s_stat* init_cpu(const char *param)
443{
444 int sz;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000445 cpu_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000446 s->collect = collect_cpu;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000447 sz = strtoul(param, NULL, 0); /* param can be "" */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000448 if (sz < 10) sz = 10;
449 if (sz > 1000) sz = 1000;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000450 s->bar = xzalloc(sz+1);
451 /*s->bar[sz] = '\0'; - xzalloc did it */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000452 s->bar_sz = sz;
453 return (s_stat*)s;
454}
455
456
457S_STAT(int_stat)
458 ullong old;
459 int no;
460S_STAT_END(int_stat)
461
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200462static void FAST_FUNC collect_int(int_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000463{
464 ullong data[1];
465 ullong old;
466
467 if (rdval(get_file(&proc_stat), "intr", data, s->no)) {
468 put_question_marks(4);
469 return;
470 }
471
472 old = s->old;
473 if (data[0] < old) old = data[0]; //sanitize
474 s->old = data[0];
475 scale(data[0] - old);
476}
477
478static s_stat* init_int(const char *param)
479{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000480 int_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000481 s->collect = collect_int;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000482 if (param[0] == '\0') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000483 s->no = 1;
484 } else {
Denys Vlasenko77832482010-08-12 14:14:45 +0200485 int n = xatoi_positive(param);
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000486 s->no = n + 2;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000487 }
488 return (s_stat*)s;
489}
490
491
492S_STAT(ctx_stat)
493 ullong old;
494S_STAT_END(ctx_stat)
495
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200496static void FAST_FUNC collect_ctx(ctx_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000497{
498 ullong data[1];
499 ullong old;
500
501 if (rdval(get_file(&proc_stat), "ctxt", data, 1)) {
502 put_question_marks(4);
503 return;
504 }
505
506 old = s->old;
507 if (data[0] < old) old = data[0]; //sanitize
508 s->old = data[0];
509 scale(data[0] - old);
510}
511
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000512static s_stat* init_ctx(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000513{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000514 ctx_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000515 s->collect = collect_ctx;
516 return (s_stat*)s;
517}
518
519
520S_STAT(blk_stat)
521 const char* lookfor;
522 ullong old[2];
523S_STAT_END(blk_stat)
524
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200525static void FAST_FUNC collect_blk(blk_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000526{
527 ullong data[2];
528 int i;
529
530 if (is26) {
531 i = rdval_diskstats(get_file(&proc_diskstats), data);
532 } else {
533 i = rdval(get_file(&proc_stat), s->lookfor, data, 1, 2);
534 // Linux 2.4 reports bio in Kbytes, convert to sectors:
535 data[0] *= 2;
536 data[1] *= 2;
537 }
538 if (i) {
539 put_question_marks(9);
540 return;
541 }
542
543 for (i=0; i<2; i++) {
544 ullong old = s->old[i];
545 if (data[i] < old) old = data[i]; //sanitize
546 s->old[i] = data[i];
547 data[i] -= old;
548 }
549 scale(data[0]*512); // TODO: *sectorsize
550 put_c(' ');
551 scale(data[1]*512);
552}
553
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000554static s_stat* init_blk(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000555{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000556 blk_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000557 s->collect = collect_blk;
558 s->lookfor = "page";
559 return (s_stat*)s;
560}
561
562
563S_STAT(fork_stat)
564 ullong old;
565S_STAT_END(fork_stat)
566
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200567static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000568{
569 ullong data[1];
570
571 if (rdval_loadavg(get_file(&proc_loadavg), data, 4)) {
572 put_question_marks(4);
573 return;
574 }
575 scale(data[0]);
576}
577
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200578static void FAST_FUNC collect_fork(fork_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000579{
580 ullong data[1];
581 ullong old;
582
583 if (rdval(get_file(&proc_stat), "processes", data, 1)) {
584 put_question_marks(4);
585 return;
586 }
587
588 old = s->old;
589 if (data[0] < old) old = data[0]; //sanitize
590 s->old = data[0];
591 scale(data[0] - old);
592}
593
594static s_stat* init_fork(const char *param)
595{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000596 fork_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000597 if (*param == 'n') {
598 s->collect = collect_thread_nr;
599 } else {
600 s->collect = collect_fork;
601 }
602 return (s_stat*)s;
603}
604
605
606S_STAT(if_stat)
607 ullong old[4];
608 const char *device;
609 char *device_colon;
610S_STAT_END(if_stat)
611
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200612static void FAST_FUNC collect_if(if_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000613{
614 ullong data[4];
615 int i;
616
617 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 1, 3, 9, 11)) {
618 put_question_marks(10);
619 return;
620 }
621
622 for (i=0; i<4; i++) {
623 ullong old = s->old[i];
624 if (data[i] < old) old = data[i]; //sanitize
625 s->old[i] = data[i];
626 data[i] -= old;
627 }
628 put_c(data[1] ? '*' : ' ');
629 scale(data[0]);
630 put_c(data[3] ? '*' : ' ');
631 scale(data[2]);
632}
633
634static s_stat* init_if(const char *device)
635{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000636 if_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000637
638 if (!device || !device[0])
639 bb_show_usage();
640 s->collect = collect_if;
641
642 s->device = device;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000643 s->device_colon = xasprintf("%s:", device);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000644 return (s_stat*)s;
645}
646
647
648S_STAT(mem_stat)
649 char opt;
650S_STAT_END(mem_stat)
651
652// "Memory" value should not include any caches.
653// IOW: neither "ls -laR /" nor heavy read/write activity
654// should affect it. We'd like to also include any
655// long-term allocated kernel-side mem, but it is hard
656// to figure out. For now, bufs, cached & slab are
657// counted as "free" memory
658//2.6.16:
659//MemTotal: 773280 kB
660//MemFree: 25912 kB - genuinely free
661//Buffers: 320672 kB - cache
662//Cached: 146396 kB - cache
663//SwapCached: 0 kB
664//Active: 183064 kB
665//Inactive: 356892 kB
666//HighTotal: 0 kB
667//HighFree: 0 kB
668//LowTotal: 773280 kB
669//LowFree: 25912 kB
670//SwapTotal: 131064 kB
671//SwapFree: 131064 kB
672//Dirty: 48 kB
673//Writeback: 0 kB
674//Mapped: 96620 kB
675//Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
676// but includes dentries and inodes
677// (== can take arbitrary amount of mem)
678//CommitLimit: 517704 kB
679//Committed_AS: 236776 kB
680//PageTables: 1248 kB
681//VmallocTotal: 516052 kB
682//VmallocUsed: 3852 kB
683//VmallocChunk: 512096 kB
684//HugePages_Total: 0
685//HugePages_Free: 0
686//Hugepagesize: 4096 kB
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200687static void FAST_FUNC collect_mem(mem_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000688{
689 ullong m_total = 0;
690 ullong m_free = 0;
691 ullong m_bufs = 0;
692 ullong m_cached = 0;
693 ullong m_slab = 0;
694
695 if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1)) {
696 put_question_marks(4);
697 return;
698 }
Denis Vlasenko8a2e4212008-02-29 07:46:19 +0000699 if (s->opt == 't') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000700 scale(m_total << 10);
701 return;
702 }
703
704 if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1)
705 || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1)
706 || rdval(proc_meminfo.file, "Cached:", &m_cached, 1)
707 || rdval(proc_meminfo.file, "Slab:", &m_slab , 1)
708 ) {
709 put_question_marks(4);
710 return;
711 }
712
713 m_free += m_bufs + m_cached + m_slab;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000714 switch (s->opt) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000715 case 'f':
716 scale(m_free << 10); break;
717 default:
718 scale((m_total - m_free) << 10); break;
719 }
720}
721
722static s_stat* init_mem(const char *param)
723{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000724 mem_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000725 s->collect = collect_mem;
726 s->opt = param[0];
727 return (s_stat*)s;
728}
729
730
731S_STAT(swp_stat)
732S_STAT_END(swp_stat)
733
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200734static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000735{
736 ullong s_total[1];
737 ullong s_free[1];
738 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1)
739 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1)
740 ) {
741 put_question_marks(4);
742 return;
743 }
744 scale((s_total[0]-s_free[0]) << 10);
745}
746
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000747static s_stat* init_swp(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000748{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000749 swp_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000750 s->collect = collect_swp;
751 return (s_stat*)s;
752}
753
754
755S_STAT(fd_stat)
756S_STAT_END(fd_stat)
757
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200758static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000759{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000760 ullong data[2];
761
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000762 if (rdval(get_file(&proc_sys_fs_filenr), "", data, 1, 2)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000763 put_question_marks(4);
764 return;
765 }
766
767 scale(data[0] - data[1]);
768}
769
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000770static s_stat* init_fd(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000771{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000772 fd_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000773 s->collect = collect_fd;
774 return (s_stat*)s;
775}
776
777
778S_STAT(time_stat)
779 int prec;
780 int scale;
781S_STAT_END(time_stat)
782
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200783static void FAST_FUNC collect_time(time_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000784{
785 char buf[sizeof("12:34:56.123456")];
786 struct tm* tm;
787 int us = tv.tv_usec + s->scale/2;
788 time_t t = tv.tv_sec;
789
790 if (us >= 1000000) {
791 t++;
792 us -= 1000000;
793 }
794 tm = localtime(&t);
795
796 sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
797 if (s->prec)
798 sprintf(buf+8, ".%0*d", s->prec, us / s->scale);
799 put(buf);
800}
801
802static s_stat* init_time(const char *param)
803{
804 int prec;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000805 time_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000806
807 s->collect = collect_time;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000808 prec = param[0] - '0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000809 if (prec < 0) prec = 0;
810 else if (prec > 6) prec = 6;
811 s->prec = prec;
812 s->scale = 1;
813 while (prec++ < 6)
814 s->scale *= 10;
815 return (s_stat*)s;
816}
817
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200818static void FAST_FUNC collect_info(s_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000819{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000820 gen ^= 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000821 while (s) {
822 put(s->label);
823 s->collect(s);
824 s = s->next;
825 }
826}
827
828
829typedef s_stat* init_func(const char *param);
830
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100831// Deprecated %NNNd is to be removed, -d MSEC supersedes it
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000832static const char options[] ALIGN1 = "ncmsfixptbdr";
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000833static init_func *const init_functions[] = {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000834 init_if,
835 init_cpu,
836 init_mem,
837 init_swp,
838 init_fd,
839 init_int,
840 init_ctx,
841 init_fork,
842 init_time,
843 init_blk,
844 init_delay,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000845 init_cr
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000846};
847
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000848int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100849int nmeter_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000850{
851 char buf[32];
852 s_stat *first = NULL;
853 s_stat *last = NULL;
854 s_stat *s;
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100855 char *opt_d;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000856 char *cur, *prev;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000857
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000858 INIT_G();
859
860 xchdir("/proc");
861
Denis Vlasenkocd785fb2008-08-14 21:57:43 +0000862 if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
863 buf[sizeof(buf)-1] = '\0';
864 is26 = (strstr(buf, " 2.4.") == NULL);
865 }
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000866
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100867 if (getopt32(argv, "d:", &opt_d))
868 init_delay(opt_d);
869 argv += optind;
870
871 if (!argv[0])
872 bb_show_usage();
873
874 // Can use argv[0] directly, but this will mess up
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000875 // parameters as seen by e.g. ps. Making a copy...
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100876 cur = xstrdup(argv[0]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000877 while (1) {
878 char *param, *p;
879 prev = cur;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000880 again:
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000881 cur = strchr(cur, '%');
882 if (!cur)
883 break;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000884 if (cur[1] == '%') { // %%
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000885 overlapping_strcpy(cur, cur + 1);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000886 cur++;
887 goto again;
888 }
889 *cur++ = '\0'; // overwrite %
890 if (cur[0] == '[') {
891 // format: %[foptstring]
892 cur++;
893 p = strchr(options, cur[0]);
894 param = cur+1;
895 while (cur[0] != ']') {
896 if (!cur[0])
897 bb_show_usage();
898 cur++;
899 }
900 *cur++ = '\0'; // overwrite [
901 } else {
902 // format: %NNNNNNf
903 param = cur;
904 while (cur[0] >= '0' && cur[0] <= '9')
905 cur++;
906 if (!cur[0])
907 bb_show_usage();
908 p = strchr(options, cur[0]);
909 *cur++ = '\0'; // overwrite format char
910 }
911 if (!p)
912 bb_show_usage();
913 s = init_functions[p-options](param);
914 if (s) {
915 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000916 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000917 if (!first)
918 first = s;
919 else
920 last->next = s;
921 last = s;
922 } else {
923 // %NNNNd or %r option. remove it from string
924 strcpy(prev + strlen(prev), cur);
925 cur = prev;
926 }
927 }
928 if (prev[0]) {
929 s = init_literal();
930 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000931 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000932 if (!first)
933 first = s;
934 else
935 last->next = s;
936 last = s;
937 }
938
939 // Generate first samples but do not print them, they're bogus
940 collect_info(first);
941 reset_outbuf();
942 if (delta >= 0) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000943 gettimeofday(&tv, NULL);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000944 usleep(delta > 1000000 ? 1000000 : delta - tv.tv_usec%deltanz);
945 }
946
947 while (1) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000948 gettimeofday(&tv, NULL);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000949 collect_info(first);
950 put(final_str);
951 print_outbuf();
952
953 // Negative delta -> no usleep at all
954 // This will hog the CPU but you can have REALLY GOOD
955 // time resolution ;)
956 // TODO: detect and avoid useless updates
957 // (like: nothing happens except time)
958 if (delta >= 0) {
959 int rem;
960 // can be commented out, will sacrifice sleep time precision a bit
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000961 gettimeofday(&tv, NULL);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000962 if (need_seconds)
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000963 rem = delta - ((ullong)tv.tv_sec*1000000 + tv.tv_usec) % deltanz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000964 else
965 rem = delta - tv.tv_usec%deltanz;
966 // Sometimes kernel wakes us up just a tiny bit earlier than asked
967 // Do not go to very short sleep in this case
968 if (rem < delta/128) {
969 rem += delta;
970 }
971 usleep(rem);
972 }
973 }
974
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000975 /*return 0;*/
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000976}