blob: cded67e562868643a4757d43ea29cbfeb9f43e07 [file] [log] [blame]
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001/*
2** Licensed under the GPL v2, see the file LICENSE in this tarball
3**
4** Based on nanotop.c from floppyfw project
5**
6** Contact me: vda.linux@googlemail.com */
7
8//TODO:
9// simplify code
10// /proc/locks
11// /proc/stat:
12// disk_io: (3,0):(22272,17897,410702,4375,54750)
13// btime 1059401962
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000014//TODO: use sysinfo libc call/syscall, if appropriate
15// (faster than open/read/close):
16// sysinfo({uptime=15017, loads=[5728, 15040, 16480]
17// totalram=2107416576, freeram=211525632, sharedram=0, bufferram=157204480}
18// totalswap=134209536, freeswap=134209536, procs=157})
Denis Vlasenko1db39b22006-10-11 20:59:02 +000019
Denis Vlasenko1db39b22006-10-11 20:59:02 +000020#include <time.h>
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000021#include "libbb.h"
Denis Vlasenko1db39b22006-10-11 20:59:02 +000022
23typedef unsigned long long ullong;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000024
Denis Vlasenko199c0d52007-05-30 14:48:38 +000025enum { PROC_FILE_SIZE = 4096 };
Denis Vlasenko1db39b22006-10-11 20:59:02 +000026
27typedef struct proc_file {
Denis Vlasenko1db39b22006-10-11 20:59:02 +000028 char *file;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000029 //const char *name;
30 smallint last_gen;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000031} proc_file;
32
Denis Vlasenko199c0d52007-05-30 14:48:38 +000033static const char *const proc_name[] = {
34 "stat", // Must match the order of proc_file's!
35 "loadavg",
36 "net/dev",
37 "meminfo",
38 "diskstats",
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000039 "sys/fs/file-nr"
Denis Vlasenko199c0d52007-05-30 14:48:38 +000040};
41
42struct globals {
43 // Sample generation flip-flop
44 smallint gen;
45 // Linux 2.6? (otherwise assumes 2.4)
46 smallint is26;
47 // 1 if sample delay is not an integer fraction of a second
48 smallint need_seconds;
49 char *cur_outbuf;
50 const char *final_str;
51 int delta;
52 int deltanz;
53 struct timeval tv;
54#define first_proc_file proc_stat
55 proc_file proc_stat; // Must match the order of proc_name's!
56 proc_file proc_loadavg;
57 proc_file proc_net_dev;
58 proc_file proc_meminfo;
59 proc_file proc_diskstats;
60 proc_file proc_sys_fs_filenr;
61};
62#define G (*ptr_to_globals)
63#define gen (G.gen )
64#define is26 (G.is26 )
65#define need_seconds (G.need_seconds )
66#define cur_outbuf (G.cur_outbuf )
67#define final_str (G.final_str )
68#define delta (G.delta )
69#define deltanz (G.deltanz )
70#define tv (G.tv )
71#define proc_stat (G.proc_stat )
72#define proc_loadavg (G.proc_loadavg )
73#define proc_net_dev (G.proc_net_dev )
74#define proc_meminfo (G.proc_meminfo )
75#define proc_diskstats (G.proc_diskstats )
76#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
Denis Vlasenko199c0d52007-05-30 14:48:38 +000077#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +000078 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
79 cur_outbuf = outbuf; \
80 final_str = "\n"; \
81 deltanz = delta = 1000000; \
82} while (0)
Denis Vlasenko1db39b22006-10-11 20:59:02 +000083
Denis Vlasenkoef4bb262007-06-04 12:21:53 +000084// We depend on this being a char[], not char* - we take sizeof() of it
85#define outbuf bb_common_bufsiz1
86
Denis Vlasenko1db39b22006-10-11 20:59:02 +000087static inline void reset_outbuf(void)
88{
89 cur_outbuf = outbuf;
90}
91
92static inline int outbuf_count(void)
93{
94 return cur_outbuf - outbuf;
95}
96
97static void print_outbuf(void)
98{
99 int sz = cur_outbuf - outbuf;
100 if (sz > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000101 xwrite(STDOUT_FILENO, outbuf, sz);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000102 cur_outbuf = outbuf;
103 }
104}
105
106static void put(const char *s)
107{
108 int sz = strlen(s);
109 if (sz > outbuf + sizeof(outbuf) - cur_outbuf)
110 sz = outbuf + sizeof(outbuf) - cur_outbuf;
111 memcpy(cur_outbuf, s, sz);
112 cur_outbuf += sz;
113}
114
115static void put_c(char c)
116{
117 if (cur_outbuf < outbuf + sizeof(outbuf))
118 *cur_outbuf++ = c;
119}
120
121static void put_question_marks(int count)
122{
123 while (count--)
124 put_c('?');
125}
126
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000127static void readfile_z(char *buf, int sz, const char* fname)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000128{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000129// open_read_close() will do two reads in order to be sure we are at EOF,
130// and we don't need/want that.
131// sz = open_read_close(fname, buf, sz-1);
132
133 int fd = xopen(fname, O_RDONLY);
134 buf[0] = '\0';
Denis Vlasenkobc2fd372008-06-30 07:33:52 +0000135 sz = read(fd, buf, sz - 1);
136 if (sz > 0)
137 buf[sz] = '\0';
138 close(fd);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000139}
140
141static const char* get_file(proc_file *pf)
142{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000143 if (pf->last_gen != gen) {
144 pf->last_gen = gen;
145 // We allocate PROC_FILE_SIZE bytes. This wastes memory,
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000146 // but allows us to allocate only once (at first sample)
147 // per proc file, and reuse buffer for each sample
148 if (!pf->file)
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000149 pf->file = xmalloc(PROC_FILE_SIZE);
150 readfile_z(pf->file, PROC_FILE_SIZE, proc_name[pf - &first_proc_file]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000151 }
152 return pf->file;
153}
154
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000155static ullong read_after_slash(const char *p)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000156{
157 p = strchr(p, '/');
158 if (!p) return 0;
159 return strtoull(p+1, NULL, 10);
160}
161
162enum conv_type { conv_decimal, conv_slash };
163
164// Reads decimal values from line. Values start after key, for example:
165// "cpu 649369 0 341297 4336769..." - key is "cpu" here.
166// Values are stored in vec[]. arg_ptr has list of positions
167// we are interested in: for example: 1,2,5 - we want 1st, 2nd and 5th value.
168static int vrdval(const char* p, const char* key,
169 enum conv_type conv, ullong *vec, va_list arg_ptr)
170{
171 int indexline;
172 int indexnext;
173
174 p = strstr(p, key);
175 if (!p) return 1;
176
177 p += strlen(key);
178 indexline = 1;
179 indexnext = va_arg(arg_ptr, int);
180 while (1) {
181 while (*p == ' ' || *p == '\t') p++;
182 if (*p == '\n' || *p == '\0') break;
183
184 if (indexline == indexnext) { // read this value
185 *vec++ = conv==conv_decimal ?
186 strtoull(p, NULL, 10) :
187 read_after_slash(p);
188 indexnext = va_arg(arg_ptr, int);
189 }
190 while (*p > ' ') p++; // skip over value
191 indexline++;
192 }
193 return 0;
194}
195
196// Parses files with lines like "cpu0 21727 0 15718 1813856 9461 10485 0 0":
197// rdval(file_contents, "string_to_find", result_vector, value#, value#...)
198// value# start with 1
199static int rdval(const char* p, const char* key, ullong *vec, ...)
200{
201 va_list arg_ptr;
202 int result;
203
204 va_start(arg_ptr, vec);
205 result = vrdval(p, key, conv_decimal, vec, arg_ptr);
206 va_end(arg_ptr);
207
208 return result;
209}
210
211// Parses files with lines like "... ... ... 3/148 ...."
212static int rdval_loadavg(const char* p, ullong *vec, ...)
213{
214 va_list arg_ptr;
215 int result;
216
217 va_start(arg_ptr, vec);
218 result = vrdval(p, "", conv_slash, vec, arg_ptr);
219 va_end(arg_ptr);
220
221 return result;
222}
223
224// Parses /proc/diskstats
225// 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14
226// 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933
227// 3 1 hda1 0 0 0 0 <- ignore if only 4 fields
228static int rdval_diskstats(const char* p, ullong *vec)
229{
Denis Vlasenko259c9a62008-06-28 23:42:03 +0000230 ullong rd = rd; // for compiler
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000231 int indexline = 0;
232 vec[0] = 0;
233 vec[1] = 0;
234 while (1) {
235 indexline++;
236 while (*p == ' ' || *p == '\t') p++;
237 if (*p == '\0') break;
238 if (*p == '\n') {
239 indexline = 0;
240 p++;
241 continue;
242 }
243 if (indexline == 6) {
244 rd = strtoull(p, NULL, 10);
245 } else if (indexline == 10) {
246 vec[0] += rd; // TODO: *sectorsize (don't know how to find out sectorsize)
247 vec[1] += strtoull(p, NULL, 10);
248 while (*p != '\n' && *p != '\0') p++;
249 continue;
250 }
251 while (*p > ' ') p++; // skip over value
252 }
253 return 0;
254}
255
256static void scale(ullong ul)
257{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000258 char buf[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000259
260 /* see http://en.wikipedia.org/wiki/Tera */
261 smart_ulltoa4(ul, buf, " kmgtpezy");
262 buf[4] = '\0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000263 put(buf);
264}
265
266
267#define S_STAT(a) \
268typedef struct a { \
269 struct s_stat *next; \
270 void (*collect)(struct a *s); \
271 const char *label;
272#define S_STAT_END(a) } a;
273
274S_STAT(s_stat)
275S_STAT_END(s_stat)
276
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000277static void collect_literal(s_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000278{
279}
280
281static s_stat* init_literal(void)
282{
Denis Vlasenkobefd5ea2008-06-28 23:33:02 +0000283 s_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000284 s->collect = collect_literal;
285 return (s_stat*)s;
286}
287
288static s_stat* init_delay(const char *param)
289{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000290 delta = strtoul(param, NULL, 0) * 1000; /* param can be "" */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000291 deltanz = delta > 0 ? delta : 1;
292 need_seconds = (1000000%deltanz) != 0;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000293 return NULL;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000294}
295
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000296static s_stat* init_cr(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000297{
298 final_str = "\r";
299 return (s_stat*)0;
300}
301
302
303// user nice system idle iowait irq softirq (last 3 only in 2.6)
304//cpu 649369 0 341297 4336769 11640 7122 1183
305//cpuN 649369 0 341297 4336769 11640 7122 1183
306enum { CPU_FIELDCNT = 7 };
307S_STAT(cpu_stat)
308 ullong old[CPU_FIELDCNT];
309 int bar_sz;
310 char *bar;
311S_STAT_END(cpu_stat)
312
313
314static void collect_cpu(cpu_stat *s)
315{
316 ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
317 unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
318 ullong all = 0;
319 int norm_all = 0;
320 int bar_sz = s->bar_sz;
321 char *bar = s->bar;
322 int i;
323
324 if (rdval(get_file(&proc_stat), "cpu ", data, 1, 2, 3, 4, 5, 6, 7)) {
325 put_question_marks(bar_sz);
326 return;
327 }
328
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000329 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000330 ullong old = s->old[i];
331 if (data[i] < old) old = data[i]; //sanitize
332 s->old[i] = data[i];
333 all += (data[i] -= old);
334 }
335
336 if (all) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000337 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000338 ullong t = bar_sz * data[i];
339 norm_all += data[i] = t / all;
340 frac[i] = t % all;
341 }
342
343 while (norm_all < bar_sz) {
344 unsigned max = frac[0];
345 int pos = 0;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000346 for (i = 1; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000347 if (frac[i] > max) max = frac[i], pos = i;
348 }
349 frac[pos] = 0; //avoid bumping up same value twice
350 data[pos]++;
351 norm_all++;
352 }
353
354 memset(bar, '.', bar_sz);
355 memset(bar, 'S', data[2]); bar += data[2]; //sys
356 memset(bar, 'U', data[0]); bar += data[0]; //usr
357 memset(bar, 'N', data[1]); bar += data[1]; //nice
358 memset(bar, 'D', data[4]); bar += data[4]; //iowait
359 memset(bar, 'I', data[5]); bar += data[5]; //irq
360 memset(bar, 'i', data[6]); bar += data[6]; //softirq
361 } else {
362 memset(bar, '?', bar_sz);
363 }
364 put(s->bar);
365}
366
367
368static s_stat* init_cpu(const char *param)
369{
370 int sz;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000371 cpu_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000372 s->collect = collect_cpu;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000373 sz = strtoul(param, NULL, 0); /* param can be "" */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000374 if (sz < 10) sz = 10;
375 if (sz > 1000) sz = 1000;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000376 s->bar = xzalloc(sz+1);
377 /*s->bar[sz] = '\0'; - xzalloc did it */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000378 s->bar_sz = sz;
379 return (s_stat*)s;
380}
381
382
383S_STAT(int_stat)
384 ullong old;
385 int no;
386S_STAT_END(int_stat)
387
388static void collect_int(int_stat *s)
389{
390 ullong data[1];
391 ullong old;
392
393 if (rdval(get_file(&proc_stat), "intr", data, s->no)) {
394 put_question_marks(4);
395 return;
396 }
397
398 old = s->old;
399 if (data[0] < old) old = data[0]; //sanitize
400 s->old = data[0];
401 scale(data[0] - old);
402}
403
404static s_stat* init_int(const char *param)
405{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000406 int_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000407 s->collect = collect_int;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000408 if (param[0] == '\0') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000409 s->no = 1;
410 } else {
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000411 int n = xatoi_u(param);
412 s->no = n + 2;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000413 }
414 return (s_stat*)s;
415}
416
417
418S_STAT(ctx_stat)
419 ullong old;
420S_STAT_END(ctx_stat)
421
422static void collect_ctx(ctx_stat *s)
423{
424 ullong data[1];
425 ullong old;
426
427 if (rdval(get_file(&proc_stat), "ctxt", data, 1)) {
428 put_question_marks(4);
429 return;
430 }
431
432 old = s->old;
433 if (data[0] < old) old = data[0]; //sanitize
434 s->old = data[0];
435 scale(data[0] - old);
436}
437
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000438static s_stat* init_ctx(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000439{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000440 ctx_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000441 s->collect = collect_ctx;
442 return (s_stat*)s;
443}
444
445
446S_STAT(blk_stat)
447 const char* lookfor;
448 ullong old[2];
449S_STAT_END(blk_stat)
450
451static void collect_blk(blk_stat *s)
452{
453 ullong data[2];
454 int i;
455
456 if (is26) {
457 i = rdval_diskstats(get_file(&proc_diskstats), data);
458 } else {
459 i = rdval(get_file(&proc_stat), s->lookfor, data, 1, 2);
460 // Linux 2.4 reports bio in Kbytes, convert to sectors:
461 data[0] *= 2;
462 data[1] *= 2;
463 }
464 if (i) {
465 put_question_marks(9);
466 return;
467 }
468
469 for (i=0; i<2; i++) {
470 ullong old = s->old[i];
471 if (data[i] < old) old = data[i]; //sanitize
472 s->old[i] = data[i];
473 data[i] -= old;
474 }
475 scale(data[0]*512); // TODO: *sectorsize
476 put_c(' ');
477 scale(data[1]*512);
478}
479
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000480static s_stat* init_blk(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000481{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000482 blk_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000483 s->collect = collect_blk;
484 s->lookfor = "page";
485 return (s_stat*)s;
486}
487
488
489S_STAT(fork_stat)
490 ullong old;
491S_STAT_END(fork_stat)
492
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000493static void collect_thread_nr(fork_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000494{
495 ullong data[1];
496
497 if (rdval_loadavg(get_file(&proc_loadavg), data, 4)) {
498 put_question_marks(4);
499 return;
500 }
501 scale(data[0]);
502}
503
504static void collect_fork(fork_stat *s)
505{
506 ullong data[1];
507 ullong old;
508
509 if (rdval(get_file(&proc_stat), "processes", data, 1)) {
510 put_question_marks(4);
511 return;
512 }
513
514 old = s->old;
515 if (data[0] < old) old = data[0]; //sanitize
516 s->old = data[0];
517 scale(data[0] - old);
518}
519
520static s_stat* init_fork(const char *param)
521{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000522 fork_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000523 if (*param == 'n') {
524 s->collect = collect_thread_nr;
525 } else {
526 s->collect = collect_fork;
527 }
528 return (s_stat*)s;
529}
530
531
532S_STAT(if_stat)
533 ullong old[4];
534 const char *device;
535 char *device_colon;
536S_STAT_END(if_stat)
537
538static void collect_if(if_stat *s)
539{
540 ullong data[4];
541 int i;
542
543 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 1, 3, 9, 11)) {
544 put_question_marks(10);
545 return;
546 }
547
548 for (i=0; i<4; i++) {
549 ullong old = s->old[i];
550 if (data[i] < old) old = data[i]; //sanitize
551 s->old[i] = data[i];
552 data[i] -= old;
553 }
554 put_c(data[1] ? '*' : ' ');
555 scale(data[0]);
556 put_c(data[3] ? '*' : ' ');
557 scale(data[2]);
558}
559
560static s_stat* init_if(const char *device)
561{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000562 if_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000563
564 if (!device || !device[0])
565 bb_show_usage();
566 s->collect = collect_if;
567
568 s->device = device;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000569 s->device_colon = xasprintf("%s:", device);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000570 return (s_stat*)s;
571}
572
573
574S_STAT(mem_stat)
575 char opt;
576S_STAT_END(mem_stat)
577
578// "Memory" value should not include any caches.
579// IOW: neither "ls -laR /" nor heavy read/write activity
580// should affect it. We'd like to also include any
581// long-term allocated kernel-side mem, but it is hard
582// to figure out. For now, bufs, cached & slab are
583// counted as "free" memory
584//2.6.16:
585//MemTotal: 773280 kB
586//MemFree: 25912 kB - genuinely free
587//Buffers: 320672 kB - cache
588//Cached: 146396 kB - cache
589//SwapCached: 0 kB
590//Active: 183064 kB
591//Inactive: 356892 kB
592//HighTotal: 0 kB
593//HighFree: 0 kB
594//LowTotal: 773280 kB
595//LowFree: 25912 kB
596//SwapTotal: 131064 kB
597//SwapFree: 131064 kB
598//Dirty: 48 kB
599//Writeback: 0 kB
600//Mapped: 96620 kB
601//Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
602// but includes dentries and inodes
603// (== can take arbitrary amount of mem)
604//CommitLimit: 517704 kB
605//Committed_AS: 236776 kB
606//PageTables: 1248 kB
607//VmallocTotal: 516052 kB
608//VmallocUsed: 3852 kB
609//VmallocChunk: 512096 kB
610//HugePages_Total: 0
611//HugePages_Free: 0
612//Hugepagesize: 4096 kB
613static void collect_mem(mem_stat *s)
614{
615 ullong m_total = 0;
616 ullong m_free = 0;
617 ullong m_bufs = 0;
618 ullong m_cached = 0;
619 ullong m_slab = 0;
620
621 if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1)) {
622 put_question_marks(4);
623 return;
624 }
Denis Vlasenko8a2e4212008-02-29 07:46:19 +0000625 if (s->opt == 't') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000626 scale(m_total << 10);
627 return;
628 }
629
630 if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1)
631 || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1)
632 || rdval(proc_meminfo.file, "Cached:", &m_cached, 1)
633 || rdval(proc_meminfo.file, "Slab:", &m_slab , 1)
634 ) {
635 put_question_marks(4);
636 return;
637 }
638
639 m_free += m_bufs + m_cached + m_slab;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000640 switch (s->opt) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000641 case 'f':
642 scale(m_free << 10); break;
643 default:
644 scale((m_total - m_free) << 10); break;
645 }
646}
647
648static s_stat* init_mem(const char *param)
649{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000650 mem_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000651 s->collect = collect_mem;
652 s->opt = param[0];
653 return (s_stat*)s;
654}
655
656
657S_STAT(swp_stat)
658S_STAT_END(swp_stat)
659
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000660static void collect_swp(swp_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000661{
662 ullong s_total[1];
663 ullong s_free[1];
664 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1)
665 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1)
666 ) {
667 put_question_marks(4);
668 return;
669 }
670 scale((s_total[0]-s_free[0]) << 10);
671}
672
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000673static s_stat* init_swp(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000674{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000675 swp_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000676 s->collect = collect_swp;
677 return (s_stat*)s;
678}
679
680
681S_STAT(fd_stat)
682S_STAT_END(fd_stat)
683
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000684static void collect_fd(fd_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000685{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000686 ullong data[2];
687
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000688 if (rdval(get_file(&proc_sys_fs_filenr), "", data, 1, 2)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000689 put_question_marks(4);
690 return;
691 }
692
693 scale(data[0] - data[1]);
694}
695
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000696static s_stat* init_fd(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000697{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000698 fd_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000699 s->collect = collect_fd;
700 return (s_stat*)s;
701}
702
703
704S_STAT(time_stat)
705 int prec;
706 int scale;
707S_STAT_END(time_stat)
708
709static void collect_time(time_stat *s)
710{
711 char buf[sizeof("12:34:56.123456")];
712 struct tm* tm;
713 int us = tv.tv_usec + s->scale/2;
714 time_t t = tv.tv_sec;
715
716 if (us >= 1000000) {
717 t++;
718 us -= 1000000;
719 }
720 tm = localtime(&t);
721
722 sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
723 if (s->prec)
724 sprintf(buf+8, ".%0*d", s->prec, us / s->scale);
725 put(buf);
726}
727
728static s_stat* init_time(const char *param)
729{
730 int prec;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000731 time_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000732
733 s->collect = collect_time;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000734 prec = param[0] - '0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000735 if (prec < 0) prec = 0;
736 else if (prec > 6) prec = 6;
737 s->prec = prec;
738 s->scale = 1;
739 while (prec++ < 6)
740 s->scale *= 10;
741 return (s_stat*)s;
742}
743
744static void collect_info(s_stat *s)
745{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000746 gen ^= 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000747 while (s) {
748 put(s->label);
749 s->collect(s);
750 s = s->next;
751 }
752}
753
754
755typedef s_stat* init_func(const char *param);
756
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000757static const char options[] ALIGN1 = "ncmsfixptbdr";
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000758static init_func *const init_functions[] = {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000759 init_if,
760 init_cpu,
761 init_mem,
762 init_swp,
763 init_fd,
764 init_int,
765 init_ctx,
766 init_fork,
767 init_time,
768 init_blk,
769 init_delay,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000770 init_cr
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000771};
772
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000773int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000774int nmeter_main(int argc, char **argv)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000775{
776 char buf[32];
777 s_stat *first = NULL;
778 s_stat *last = NULL;
779 s_stat *s;
780 char *cur, *prev;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000781
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000782 INIT_G();
783
784 xchdir("/proc");
785
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000786 if (argc != 2)
787 bb_show_usage();
788
Denis Vlasenkocd785fb2008-08-14 21:57:43 +0000789 if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
790 buf[sizeof(buf)-1] = '\0';
791 is26 = (strstr(buf, " 2.4.") == NULL);
792 }
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000793
794 // Can use argv[1] directly, but this will mess up
795 // parameters as seen by e.g. ps. Making a copy...
796 cur = xstrdup(argv[1]);
797 while (1) {
798 char *param, *p;
799 prev = cur;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000800 again:
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000801 cur = strchr(cur, '%');
802 if (!cur)
803 break;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000804 if (cur[1] == '%') { // %%
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000805 overlapping_strcpy(cur, cur + 1);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000806 cur++;
807 goto again;
808 }
809 *cur++ = '\0'; // overwrite %
810 if (cur[0] == '[') {
811 // format: %[foptstring]
812 cur++;
813 p = strchr(options, cur[0]);
814 param = cur+1;
815 while (cur[0] != ']') {
816 if (!cur[0])
817 bb_show_usage();
818 cur++;
819 }
820 *cur++ = '\0'; // overwrite [
821 } else {
822 // format: %NNNNNNf
823 param = cur;
824 while (cur[0] >= '0' && cur[0] <= '9')
825 cur++;
826 if (!cur[0])
827 bb_show_usage();
828 p = strchr(options, cur[0]);
829 *cur++ = '\0'; // overwrite format char
830 }
831 if (!p)
832 bb_show_usage();
833 s = init_functions[p-options](param);
834 if (s) {
835 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000836 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000837 if (!first)
838 first = s;
839 else
840 last->next = s;
841 last = s;
842 } else {
843 // %NNNNd or %r option. remove it from string
844 strcpy(prev + strlen(prev), cur);
845 cur = prev;
846 }
847 }
848 if (prev[0]) {
849 s = init_literal();
850 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000851 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000852 if (!first)
853 first = s;
854 else
855 last->next = s;
856 last = s;
857 }
858
859 // Generate first samples but do not print them, they're bogus
860 collect_info(first);
861 reset_outbuf();
862 if (delta >= 0) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000863 gettimeofday(&tv, NULL);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000864 usleep(delta > 1000000 ? 1000000 : delta - tv.tv_usec%deltanz);
865 }
866
867 while (1) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000868 gettimeofday(&tv, NULL);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000869 collect_info(first);
870 put(final_str);
871 print_outbuf();
872
873 // Negative delta -> no usleep at all
874 // This will hog the CPU but you can have REALLY GOOD
875 // time resolution ;)
876 // TODO: detect and avoid useless updates
877 // (like: nothing happens except time)
878 if (delta >= 0) {
879 int rem;
880 // can be commented out, will sacrifice sleep time precision a bit
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000881 gettimeofday(&tv, NULL);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000882 if (need_seconds)
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000883 rem = delta - ((ullong)tv.tv_sec*1000000 + tv.tv_usec) % deltanz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000884 else
885 rem = delta - tv.tv_usec%deltanz;
886 // Sometimes kernel wakes us up just a tiny bit earlier than asked
887 // Do not go to very short sleep in this case
888 if (rem < delta/128) {
889 rem += delta;
890 }
891 usleep(rem);
892 }
893 }
894
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000895 /*return 0;*/
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000896}