2 ** Licensed under the GPL v2, see the file LICENSE in this tarball
4 ** Based on nanotop.c from floppyfw project
6 ** Contact me: vda.linux@googlemail.com */
12 // disk_io: (3,0):(22272,17897,410702,4375,54750)
18 typedef unsigned long long ullong;
20 enum { proc_file_size = 4096 };
22 typedef struct proc_file {
28 static proc_file proc_stat = { "/proc/stat", -1 };
29 static proc_file proc_loadavg = { "/proc/loadavg", -1 };
30 static proc_file proc_net_dev = { "/proc/net/dev", -1 };
31 static proc_file proc_meminfo = { "/proc/meminfo", -1 };
32 static proc_file proc_diskstats = { "/proc/diskstats", -1 };
35 // Linux 2.6? (otherwise assumes 2.4)
37 static struct timeval tv;
38 static int delta = 1000000;
39 static int deltanz = 1000000;
40 static int need_seconds = 0;
41 static char *final_str = "\n";
43 // We depend on this being a char[], not char* - we take sizeof() of it
44 #define outbuf bb_common_bufsiz1
45 static char *cur_outbuf = outbuf;
48 static inline void reset_outbuf(void)
53 static inline int outbuf_count(void)
55 return cur_outbuf - outbuf;
58 static void print_outbuf(void)
60 int sz = cur_outbuf - outbuf;
67 static void put(const char *s)
70 if (sz > outbuf + sizeof(outbuf) - cur_outbuf)
71 sz = outbuf + sizeof(outbuf) - cur_outbuf;
72 memcpy(cur_outbuf, s, sz);
76 static void put_c(char c)
78 if (cur_outbuf < outbuf + sizeof(outbuf))
82 static void put_question_marks(int count)
88 static int readfile_z(char *buf, int sz, const char* fname)
90 sz = open_read_close(fname, buf, sz-1);
99 static const char* get_file(proc_file *pf)
101 if (pf->gen != gen) {
103 // We allocate proc_file_size bytes. This wastes memory,
104 // but allows us to allocate only once (at first sample)
105 // per proc file, and reuse buffer for each sample
107 pf->file = xmalloc(proc_file_size);
108 readfile_z(pf->file, proc_file_size, pf->name);
113 static inline ullong read_after_slash(const char *p)
117 return strtoull(p+1, NULL, 10);
120 enum conv_type { conv_decimal, conv_slash };
122 // Reads decimal values from line. Values start after key, for example:
123 // "cpu 649369 0 341297 4336769..." - key is "cpu" here.
124 // Values are stored in vec[]. arg_ptr has list of positions
125 // we are interested in: for example: 1,2,5 - we want 1st, 2nd and 5th value.
126 static int vrdval(const char* p, const char* key,
127 enum conv_type conv, ullong *vec, va_list arg_ptr)
137 indexnext = va_arg(arg_ptr, int);
139 while (*p == ' ' || *p == '\t') p++;
140 if (*p == '\n' || *p == '\0') break;
142 if (indexline == indexnext) { // read this value
143 *vec++ = conv==conv_decimal ?
144 strtoull(p, NULL, 10) :
146 indexnext = va_arg(arg_ptr, int);
148 while (*p > ' ') p++; // skip over value
154 // Parses files with lines like "cpu0 21727 0 15718 1813856 9461 10485 0 0":
155 // rdval(file_contents, "string_to_find", result_vector, value#, value#...)
156 // value# start with 1
157 static int rdval(const char* p, const char* key, ullong *vec, ...)
162 va_start(arg_ptr, vec);
163 result = vrdval(p, key, conv_decimal, vec, arg_ptr);
169 // Parses files with lines like "... ... ... 3/148 ...."
170 static int rdval_loadavg(const char* p, ullong *vec, ...)
175 va_start(arg_ptr, vec);
176 result = vrdval(p, "", conv_slash, vec, arg_ptr);
182 // Parses /proc/diskstats
183 // 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14
184 // 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933
185 // 3 1 hda1 0 0 0 0 <- ignore if only 4 fields
186 static int rdval_diskstats(const char* p, ullong *vec)
188 ullong rd = 0; // to avoid "warning: 'rd' might be used uninitialized"
194 while (*p == ' ' || *p == '\t') p++;
195 if (*p == '\0') break;
201 if (indexline == 6) {
202 rd = strtoull(p, NULL, 10);
203 } else if (indexline == 10) {
204 vec[0] += rd; // TODO: *sectorsize (don't know how to find out sectorsize)
205 vec[1] += strtoull(p, NULL, 10);
206 while (*p != '\n' && *p != '\0') p++;
209 while (*p > ' ') p++; // skip over value
214 static void scale(ullong ul)
217 smart_ulltoa5(ul, buf);
224 struct s_stat *next; \
225 void (*collect)(struct a *s); \
227 #define S_STAT_END(a) } a;
232 static void collect_literal(s_stat *s)
236 static s_stat* init_literal(void)
238 s_stat *s = xmalloc(sizeof(s_stat));
239 s->collect = collect_literal;
243 static s_stat* init_delay(const char *param)
245 delta = strtol(param, NULL, 0)*1000;
246 deltanz = delta > 0 ? delta : 1;
247 need_seconds = (1000000%deltanz) != 0;
251 static s_stat* init_cr(const char *param)
258 // user nice system idle iowait irq softirq (last 3 only in 2.6)
259 //cpu 649369 0 341297 4336769 11640 7122 1183
260 //cpuN 649369 0 341297 4336769 11640 7122 1183
261 enum { CPU_FIELDCNT = 7 };
263 ullong old[CPU_FIELDCNT];
269 static void collect_cpu(cpu_stat *s)
271 ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
272 unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
275 int bar_sz = s->bar_sz;
279 if (rdval(get_file(&proc_stat), "cpu ", data, 1, 2, 3, 4, 5, 6, 7)) {
280 put_question_marks(bar_sz);
284 for (i=0; i<CPU_FIELDCNT; i++) {
285 ullong old = s->old[i];
286 if (data[i] < old) old = data[i]; //sanitize
288 all += (data[i] -= old);
292 for (i=0; i<CPU_FIELDCNT; i++) {
293 ullong t = bar_sz * data[i];
294 norm_all += data[i] = t / all;
298 while (norm_all < bar_sz) {
299 unsigned max = frac[0];
301 for (i=1; i<CPU_FIELDCNT; i++) {
302 if (frac[i] > max) max = frac[i], pos = i;
304 frac[pos] = 0; //avoid bumping up same value twice
309 memset(bar, '.', bar_sz);
310 memset(bar, 'S', data[2]); bar += data[2]; //sys
311 memset(bar, 'U', data[0]); bar += data[0]; //usr
312 memset(bar, 'N', data[1]); bar += data[1]; //nice
313 memset(bar, 'D', data[4]); bar += data[4]; //iowait
314 memset(bar, 'I', data[5]); bar += data[5]; //irq
315 memset(bar, 'i', data[6]); bar += data[6]; //softirq
317 memset(bar, '?', bar_sz);
323 static s_stat* init_cpu(const char *param)
326 cpu_stat *s = xmalloc(sizeof(cpu_stat));
327 s->collect = collect_cpu;
328 sz = strtol(param, NULL, 0);
329 if (sz < 10) sz = 10;
330 if (sz > 1000) sz = 1000;
331 s->bar = xmalloc(sz+1);
343 static void collect_int(int_stat *s)
348 if (rdval(get_file(&proc_stat), "intr", data, s->no)) {
349 put_question_marks(4);
354 if (data[0] < old) old = data[0]; //sanitize
356 scale(data[0] - old);
359 static s_stat* init_int(const char *param)
361 int_stat *s = xmalloc(sizeof(int_stat));
362 s->collect = collect_int;
363 if (param[0]=='\0') {
366 int n = strtoul(param, NULL, 0);
377 static void collect_ctx(ctx_stat *s)
382 if (rdval(get_file(&proc_stat), "ctxt", data, 1)) {
383 put_question_marks(4);
388 if (data[0] < old) old = data[0]; //sanitize
390 scale(data[0] - old);
393 static s_stat* init_ctx(const char *param)
395 ctx_stat *s = xmalloc(sizeof(ctx_stat));
396 s->collect = collect_ctx;
406 static void collect_blk(blk_stat *s)
412 i = rdval_diskstats(get_file(&proc_diskstats), data);
414 i = rdval(get_file(&proc_stat), s->lookfor, data, 1, 2);
415 // Linux 2.4 reports bio in Kbytes, convert to sectors:
420 put_question_marks(9);
424 for (i=0; i<2; i++) {
425 ullong old = s->old[i];
426 if (data[i] < old) old = data[i]; //sanitize
430 scale(data[0]*512); // TODO: *sectorsize
435 static s_stat* init_blk(const char *param)
437 blk_stat *s = xmalloc(sizeof(blk_stat));
438 s->collect = collect_blk;
446 S_STAT_END(fork_stat)
448 static void collect_thread_nr(fork_stat *s)
452 if (rdval_loadavg(get_file(&proc_loadavg), data, 4)) {
453 put_question_marks(4);
459 static void collect_fork(fork_stat *s)
464 if (rdval(get_file(&proc_stat), "processes", data, 1)) {
465 put_question_marks(4);
470 if (data[0] < old) old = data[0]; //sanitize
472 scale(data[0] - old);
475 static s_stat* init_fork(const char *param)
477 fork_stat *s = xmalloc(sizeof(fork_stat));
479 s->collect = collect_thread_nr;
481 s->collect = collect_fork;
493 static void collect_if(if_stat *s)
498 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 1, 3, 9, 11)) {
499 put_question_marks(10);
503 for (i=0; i<4; i++) {
504 ullong old = s->old[i];
505 if (data[i] < old) old = data[i]; //sanitize
509 put_c(data[1] ? '*' : ' ');
511 put_c(data[3] ? '*' : ' ');
515 static s_stat* init_if(const char *device)
517 if_stat *s = xmalloc(sizeof(if_stat));
519 if (!device || !device[0])
521 s->collect = collect_if;
524 s->device_colon = xmalloc(strlen(device)+2);
525 strcpy(s->device_colon, device);
526 strcat(s->device_colon, ":");
535 // "Memory" value should not include any caches.
536 // IOW: neither "ls -laR /" nor heavy read/write activity
537 // should affect it. We'd like to also include any
538 // long-term allocated kernel-side mem, but it is hard
539 // to figure out. For now, bufs, cached & slab are
540 // counted as "free" memory
542 //MemTotal: 773280 kB
543 //MemFree: 25912 kB - genuinely free
544 //Buffers: 320672 kB - cache
545 //Cached: 146396 kB - cache
548 //Inactive: 356892 kB
551 //LowTotal: 773280 kB
553 //SwapTotal: 131064 kB
554 //SwapFree: 131064 kB
558 //Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
559 // but includes dentries and inodes
560 // (== can take arbitrary amount of mem)
561 //CommitLimit: 517704 kB
562 //Committed_AS: 236776 kB
563 //PageTables: 1248 kB
564 //VmallocTotal: 516052 kB
565 //VmallocUsed: 3852 kB
566 //VmallocChunk: 512096 kB
569 //Hugepagesize: 4096 kB
570 static void collect_mem(mem_stat *s)
578 if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1)) {
579 put_question_marks(4);
583 scale(m_total << 10);
587 if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1)
588 || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1)
589 || rdval(proc_meminfo.file, "Cached:", &m_cached, 1)
590 || rdval(proc_meminfo.file, "Slab:", &m_slab , 1)
592 put_question_marks(4);
596 m_free += m_bufs + m_cached + m_slab;
599 scale(m_free << 10); break;
601 scale((m_total - m_free) << 10); break;
605 static s_stat* init_mem(const char *param)
607 mem_stat *s = xmalloc(sizeof(mem_stat));
608 s->collect = collect_mem;
617 static void collect_swp(swp_stat *s)
621 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1)
622 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1)
624 put_question_marks(4);
627 scale((s_total[0]-s_free[0]) << 10);
630 static s_stat* init_swp(const char *param)
632 swp_stat *s = xmalloc(sizeof(swp_stat));
633 s->collect = collect_swp;
641 static void collect_fd(fd_stat *s)
646 readfile_z(file, sizeof(file), "/proc/sys/fs/file-nr");
647 if (rdval(file, "", data, 1, 2)) {
648 put_question_marks(4);
652 scale(data[0] - data[1]);
655 static s_stat* init_fd(const char *param)
657 fd_stat *s = xmalloc(sizeof(fd_stat));
658 s->collect = collect_fd;
666 S_STAT_END(time_stat)
668 static void collect_time(time_stat *s)
670 char buf[sizeof("12:34:56.123456")];
672 int us = tv.tv_usec + s->scale/2;
673 time_t t = tv.tv_sec;
681 sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
683 sprintf(buf+8, ".%0*d", s->prec, us / s->scale);
687 static s_stat* init_time(const char *param)
690 time_stat *s = xmalloc(sizeof(time_stat));
692 s->collect = collect_time;
694 if (prec < 0) prec = 0;
695 else if (prec > 6) prec = 6;
703 static void collect_info(s_stat *s)
714 typedef s_stat* init_func(const char *param);
716 static const char options[] = "ncmsfixptbdr";
717 static init_func* init_functions[] = {
732 int nmeter_main(int argc, char* argv[])
735 s_stat *first = NULL;
743 if (open_read_close("/proc/version", buf, sizeof(buf)) > 0)
744 is26 = (strstr(buf, " 2.4.")==NULL);
746 // Can use argv[1] directly, but this will mess up
747 // parameters as seen by e.g. ps. Making a copy...
748 cur = xstrdup(argv[1]);
753 cur = strchr(cur, '%');
756 if (cur[1]=='%') { // %%
761 *cur++ = '\0'; // overwrite %
763 // format: %[foptstring]
765 p = strchr(options, cur[0]);
767 while (cur[0] != ']') {
772 *cur++ = '\0'; // overwrite [
776 while (cur[0] >= '0' && cur[0] <= '9')
780 p = strchr(options, cur[0]);
781 *cur++ = '\0'; // overwrite format char
785 s = init_functions[p-options](param);
795 // %NNNNd or %r option. remove it from string
796 strcpy(prev + strlen(prev), cur);
811 // Generate first samples but do not print them, they're bogus
815 gettimeofday(&tv, 0);
816 usleep(delta > 1000000 ? 1000000 : delta - tv.tv_usec%deltanz);
820 gettimeofday(&tv, 0);
825 // Negative delta -> no usleep at all
826 // This will hog the CPU but you can have REALLY GOOD
827 // time resolution ;)
828 // TODO: detect and avoid useless updates
829 // (like: nothing happens except time)
832 // can be commented out, will sacrifice sleep time precision a bit
833 gettimeofday(&tv, 0);
835 rem = delta - ((ullong)tv.tv_sec*1000000+tv.tv_usec)%deltanz;
837 rem = delta - tv.tv_usec%deltanz;
838 // Sometimes kernel wakes us up just a tiny bit earlier than asked
839 // Do not go to very short sleep in this case
840 if (rem < delta/128) {