Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 1 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 2 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 3 | * |
| 4 | * Based on nanotop.c from floppyfw project |
| 5 | * |
Denys Vlasenko | bdaea46 | 2010-08-16 20:42:14 +0200 | [diff] [blame] | 6 | * Contact me: vda.linux@googlemail.com |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | */ |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 8 | //config:config NMETER |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "nmeter (11 kb)" |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: Prints selected system stats continuously, one line per update. |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 13 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 14 | //applet:IF_NMETER(APPLET(nmeter, BB_DIR_USR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 15 | |
| 16 | //kbuild:lib-$(CONFIG_NMETER) += nmeter.o |
| 17 | |
| 18 | //usage:#define nmeter_trivial_usage |
| 19 | //usage: "[-d MSEC] FORMAT_STRING" |
| 20 | //usage:#define nmeter_full_usage "\n\n" |
| 21 | //usage: "Monitor system in real time" |
| 22 | //usage: "\n" |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 23 | //usage: "\n -d MSEC Milliseconds between updates, default:1000, none:-1" |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 24 | //usage: "\n" |
| 25 | //usage: "\nFormat specifiers:" |
Denys Vlasenko | f3d705f | 2017-01-21 03:46:35 +0100 | [diff] [blame] | 26 | //usage: "\n %Nc or %[cN] CPU. N - bar size (default 10)" |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 27 | //usage: "\n (displays: S:system U:user N:niced D:iowait I:irq i:softirq)" |
Denys Vlasenko | abb0952 | 2011-05-26 09:00:23 +0200 | [diff] [blame] | 28 | //usage: "\n %[nINTERFACE] Network INTERFACE" |
| 29 | //usage: "\n %m Allocated memory" |
| 30 | //usage: "\n %[mf] Free memory" |
| 31 | //usage: "\n %[mt] Total memory" |
| 32 | //usage: "\n %s Allocated swap" |
| 33 | //usage: "\n %f Number of used file descriptors" |
| 34 | //usage: "\n %Ni Total/specific IRQ rate" |
| 35 | //usage: "\n %x Context switch rate" |
| 36 | //usage: "\n %p Forks" |
| 37 | //usage: "\n %[pn] # of processes" |
| 38 | //usage: "\n %b Block io" |
| 39 | //usage: "\n %Nt Time (with N decimal points)" |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 40 | //usage: "\n %r Print <cr> instead of <lf> at EOL" |
| 41 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 42 | //TODO: |
| 43 | // simplify code |
| 44 | // /proc/locks |
| 45 | // /proc/stat: |
| 46 | // disk_io: (3,0):(22272,17897,410702,4375,54750) |
| 47 | // btime 1059401962 |
Denis Vlasenko | a6163ca | 2007-06-17 00:35:15 +0000 | [diff] [blame] | 48 | //TODO: use sysinfo libc call/syscall, if appropriate |
| 49 | // (faster than open/read/close): |
| 50 | // sysinfo({uptime=15017, loads=[5728, 15040, 16480] |
| 51 | // totalram=2107416576, freeram=211525632, sharedram=0, bufferram=157204480} |
| 52 | // totalswap=134209536, freeswap=134209536, procs=157}) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | a6163ca | 2007-06-17 00:35:15 +0000 | [diff] [blame] | 54 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 55 | #include "common_bufsiz.h" |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 56 | |
| 57 | typedef unsigned long long ullong; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 59 | enum { /* Preferably use powers of 2 */ |
| 60 | PROC_MIN_FILE_SIZE = 256, |
| 61 | PROC_MAX_FILE_SIZE = 16 * 1024, |
| 62 | }; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 63 | |
| 64 | typedef struct proc_file { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 65 | char *file; |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 66 | int file_sz; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 67 | smallint last_gen; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 68 | } proc_file; |
| 69 | |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 70 | static 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 Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 76 | "sys/fs/file-nr" |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | struct 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; |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 86 | char final_char; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 87 | char *cur_outbuf; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 88 | int delta; |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 89 | unsigned deltanz; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 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 ) |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 104 | #define tv (G.tv ) |
| 105 | #define proc_stat (G.proc_stat ) |
| 106 | #define proc_loadavg (G.proc_loadavg ) |
| 107 | #define proc_net_dev (G.proc_net_dev ) |
| 108 | #define proc_meminfo (G.proc_meminfo ) |
| 109 | #define proc_diskstats (G.proc_diskstats ) |
| 110 | #define proc_sys_fs_filenr (G.proc_sys_fs_filenr) |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 111 | #define outbuf bb_common_bufsiz1 |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 112 | #define INIT_G() do { \ |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 113 | setup_common_bufsiz(); \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 114 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 115 | cur_outbuf = outbuf; \ |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 116 | G.final_char = '\n'; \ |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 117 | G.deltanz = G.delta = 1000000; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 118 | } while (0) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 119 | |
| 120 | static inline void reset_outbuf(void) |
| 121 | { |
| 122 | cur_outbuf = outbuf; |
| 123 | } |
| 124 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 125 | static void print_outbuf(void) |
| 126 | { |
| 127 | int sz = cur_outbuf - outbuf; |
| 128 | if (sz > 0) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 129 | xwrite(STDOUT_FILENO, outbuf, sz); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 130 | cur_outbuf = outbuf; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void put(const char *s) |
| 135 | { |
Denys Vlasenko | 99c71c9 | 2016-03-15 15:28:49 +0100 | [diff] [blame] | 136 | char *p = cur_outbuf; |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 137 | int sz = outbuf + COMMON_BUFSIZE - p; |
Denys Vlasenko | 99c71c9 | 2016-03-15 15:28:49 +0100 | [diff] [blame] | 138 | while (*s && --sz >= 0) |
| 139 | *p++ = *s++; |
| 140 | cur_outbuf = p; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void put_c(char c) |
| 144 | { |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 145 | if (cur_outbuf < outbuf + COMMON_BUFSIZE) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 146 | *cur_outbuf++ = c; |
| 147 | } |
| 148 | |
| 149 | static void put_question_marks(int count) |
| 150 | { |
| 151 | while (count--) |
| 152 | put_c('?'); |
| 153 | } |
| 154 | |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 155 | static void readfile_z(proc_file *pf, const char* fname) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 156 | { |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 157 | // open_read_close() will do two reads in order to be sure we are at EOF, |
| 158 | // and we don't need/want that. |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 159 | int fd; |
| 160 | int sz, rdsz; |
| 161 | char *buf; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 163 | sz = pf->file_sz; |
| 164 | buf = pf->file; |
| 165 | if (!buf) { |
| 166 | buf = xmalloc(PROC_MIN_FILE_SIZE); |
| 167 | sz = PROC_MIN_FILE_SIZE; |
| 168 | } |
| 169 | again: |
| 170 | fd = xopen(fname, O_RDONLY); |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 171 | buf[0] = '\0'; |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 172 | rdsz = read(fd, buf, sz-1); |
Denis Vlasenko | bc2fd37 | 2008-06-30 07:33:52 +0000 | [diff] [blame] | 173 | close(fd); |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 174 | if (rdsz > 0) { |
| 175 | if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) { |
| 176 | sz *= 2; |
| 177 | buf = xrealloc(buf, sz); |
| 178 | goto again; |
| 179 | } |
| 180 | buf[rdsz] = '\0'; |
| 181 | } |
| 182 | pf->file_sz = sz; |
| 183 | pf->file = buf; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static const char* get_file(proc_file *pf) |
| 187 | { |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 188 | if (pf->last_gen != gen) { |
| 189 | pf->last_gen = gen; |
Denis Vlasenko | bfc93a3 | 2008-09-26 11:12:09 +0000 | [diff] [blame] | 190 | readfile_z(pf, proc_name[pf - &first_proc_file]); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 191 | } |
| 192 | return pf->file; |
| 193 | } |
| 194 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 195 | static ullong read_after_slash(const char *p) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 196 | { |
| 197 | p = strchr(p, '/'); |
| 198 | if (!p) return 0; |
| 199 | return strtoull(p+1, NULL, 10); |
| 200 | } |
| 201 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 202 | enum conv_type { |
| 203 | conv_decimal = 0, |
| 204 | conv_slash = 1 |
| 205 | }; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 206 | |
| 207 | // Reads decimal values from line. Values start after key, for example: |
| 208 | // "cpu 649369 0 341297 4336769..." - key is "cpu" here. |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 209 | // Values are stored in vec[]. |
| 210 | // posbits is a bit lit of positions we are interested in. |
| 211 | // for example: 00100110 - we want 1st, 2nd and 5th value. |
| 212 | // posbits.bit0 encodes conversion type. |
| 213 | static int rdval(const char* p, const char* key, ullong *vec, long posbits) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 214 | { |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 215 | unsigned curpos; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 216 | |
| 217 | p = strstr(p, key); |
| 218 | if (!p) return 1; |
| 219 | |
| 220 | p += strlen(key); |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 221 | curpos = 1 << 1; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 222 | while (1) { |
| 223 | while (*p == ' ' || *p == '\t') p++; |
| 224 | if (*p == '\n' || *p == '\0') break; |
| 225 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 226 | if (curpos & posbits) { // read this value |
| 227 | *vec++ = (posbits & 1) == conv_decimal ? |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 228 | strtoull(p, NULL, 10) : |
| 229 | read_after_slash(p); |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 230 | posbits -= curpos; |
| 231 | if (posbits <= 1) |
Denys Vlasenko | e4de8c6 | 2016-03-15 15:22:42 +0100 | [diff] [blame] | 232 | return 0; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 233 | } |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 234 | while (*p > ' ') // skip over the value |
| 235 | p++; |
| 236 | curpos <<= 1; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 241 | // Parses files with lines like "... ... ... 3/148 ...." |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 242 | static int rdval_loadavg(const char* p, ullong *vec, long posbits) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 243 | { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 244 | int result; |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 245 | result = rdval(p, "", vec, posbits | conv_slash); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 246 | return result; |
| 247 | } |
| 248 | |
| 249 | // Parses /proc/diskstats |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 250 | // 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14 |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 251 | // 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933 |
| 252 | // 3 1 hda1 0 0 0 0 <- ignore if only 4 fields |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 253 | // Linux 3.0 (maybe earlier) started printing full stats for hda1 too. |
| 254 | // Had to add code which skips such devices. |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 255 | static int rdval_diskstats(const char* p, ullong *vec) |
| 256 | { |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 257 | char devname[32]; |
| 258 | unsigned devname_len = 0; |
| 259 | int value_idx = 0; |
| 260 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 261 | vec[0] = 0; |
| 262 | vec[1] = 0; |
| 263 | while (1) { |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 264 | value_idx++; |
| 265 | while (*p == ' ' || *p == '\t') |
| 266 | p++; |
| 267 | if (*p == '\0') |
| 268 | break; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 269 | if (*p == '\n') { |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 270 | value_idx = 0; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 271 | p++; |
| 272 | continue; |
| 273 | } |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 274 | if (value_idx == 3) { |
| 275 | char *end = strchrnul(p, ' '); |
| 276 | /* If this a hda1-like device (same prefix as last one + digit)? */ |
| 277 | if (devname_len && strncmp(devname, p, devname_len) == 0 && isdigit(p[devname_len])) { |
| 278 | p = end; |
| 279 | goto skip_line; /* skip entire line */ |
| 280 | } |
| 281 | /* It is not. Remember the name for future checks */ |
| 282 | devname_len = end - p; |
| 283 | if (devname_len > sizeof(devname)-1) |
| 284 | devname_len = sizeof(devname)-1; |
| 285 | strncpy(devname, p, devname_len); |
| 286 | /* devname[devname_len] = '\0'; - not really needed */ |
| 287 | p = end; |
| 288 | } else |
| 289 | if (value_idx == 6) { |
| 290 | // TODO: *sectorsize (don't know how to find out sectorsize) |
| 291 | vec[0] += strtoull(p, NULL, 10); |
| 292 | } else |
| 293 | if (value_idx == 10) { |
| 294 | // TODO: *sectorsize (don't know how to find out sectorsize) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 295 | vec[1] += strtoull(p, NULL, 10); |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 296 | skip_line: |
| 297 | while (*p != '\n' && *p != '\0') |
| 298 | p++; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 299 | continue; |
| 300 | } |
Denys Vlasenko | 3b16034 | 2011-11-01 23:34:46 +0100 | [diff] [blame] | 301 | while ((unsigned char)(*p) > ' ') // skip over value |
| 302 | p++; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void scale(ullong ul) |
| 308 | { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 309 | char buf[5]; |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 310 | |
| 311 | /* see http://en.wikipedia.org/wiki/Tera */ |
Denys Vlasenko | a407cf7 | 2013-09-06 12:53:14 +0200 | [diff] [blame] | 312 | smart_ulltoa4(ul, buf, " kmgtpezy")[0] = '\0'; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 313 | put(buf); |
| 314 | } |
| 315 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 316 | #define S_STAT(a) \ |
| 317 | typedef struct a { \ |
| 318 | struct s_stat *next; \ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 319 | void (*collect)(struct a *s) FAST_FUNC; \ |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 320 | const char *label; |
| 321 | #define S_STAT_END(a) } a; |
| 322 | |
| 323 | S_STAT(s_stat) |
| 324 | S_STAT_END(s_stat) |
| 325 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 326 | static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 327 | { |
| 328 | } |
| 329 | |
| 330 | static s_stat* init_literal(void) |
| 331 | { |
Denis Vlasenko | befd5ea | 2008-06-28 23:33:02 +0000 | [diff] [blame] | 332 | s_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 333 | s->collect = collect_literal; |
| 334 | return (s_stat*)s; |
| 335 | } |
| 336 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 337 | static s_stat* init_cr(const char *param UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 338 | { |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 339 | G.final_char = '\r'; |
| 340 | return NULL; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 343 | // user nice system idle iowait irq softirq (last 3 only in 2.6) |
| 344 | //cpu 649369 0 341297 4336769 11640 7122 1183 |
| 345 | //cpuN 649369 0 341297 4336769 11640 7122 1183 |
| 346 | enum { CPU_FIELDCNT = 7 }; |
| 347 | S_STAT(cpu_stat) |
| 348 | ullong old[CPU_FIELDCNT]; |
Denys Vlasenko | 85b380f | 2019-08-06 16:56:00 +0200 | [diff] [blame] | 349 | unsigned bar_sz; |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 350 | char bar[1]; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 351 | S_STAT_END(cpu_stat) |
| 352 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 353 | static void FAST_FUNC collect_cpu(cpu_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 354 | { |
| 355 | ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 }; |
| 356 | unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 }; |
| 357 | ullong all = 0; |
Denys Vlasenko | 85b380f | 2019-08-06 16:56:00 +0200 | [diff] [blame] | 358 | unsigned norm_all = 0; |
| 359 | unsigned bar_sz = s->bar_sz; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 360 | char *bar = s->bar; |
| 361 | int i; |
| 362 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 363 | if (rdval(get_file(&proc_stat), "cpu ", data, 0 |
| 364 | | (1 << 1) |
| 365 | | (1 << 2) |
| 366 | | (1 << 3) |
| 367 | | (1 << 4) |
| 368 | | (1 << 5) |
| 369 | | (1 << 6) |
| 370 | | (1 << 7)) |
| 371 | ) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 372 | put_question_marks(bar_sz); |
| 373 | return; |
| 374 | } |
| 375 | |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 376 | for (i = 0; i < CPU_FIELDCNT; i++) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 377 | ullong old = s->old[i]; |
| 378 | if (data[i] < old) old = data[i]; //sanitize |
| 379 | s->old[i] = data[i]; |
| 380 | all += (data[i] -= old); |
| 381 | } |
| 382 | |
| 383 | if (all) { |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 384 | for (i = 0; i < CPU_FIELDCNT; i++) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 385 | ullong t = bar_sz * data[i]; |
| 386 | norm_all += data[i] = t / all; |
| 387 | frac[i] = t % all; |
| 388 | } |
| 389 | |
| 390 | while (norm_all < bar_sz) { |
| 391 | unsigned max = frac[0]; |
| 392 | int pos = 0; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 393 | for (i = 1; i < CPU_FIELDCNT; i++) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 394 | if (frac[i] > max) max = frac[i], pos = i; |
| 395 | } |
| 396 | frac[pos] = 0; //avoid bumping up same value twice |
| 397 | data[pos]++; |
| 398 | norm_all++; |
| 399 | } |
| 400 | |
| 401 | memset(bar, '.', bar_sz); |
| 402 | memset(bar, 'S', data[2]); bar += data[2]; //sys |
| 403 | memset(bar, 'U', data[0]); bar += data[0]; //usr |
| 404 | memset(bar, 'N', data[1]); bar += data[1]; //nice |
| 405 | memset(bar, 'D', data[4]); bar += data[4]; //iowait |
| 406 | memset(bar, 'I', data[5]); bar += data[5]; //irq |
| 407 | memset(bar, 'i', data[6]); bar += data[6]; //softirq |
| 408 | } else { |
| 409 | memset(bar, '?', bar_sz); |
| 410 | } |
| 411 | put(s->bar); |
| 412 | } |
| 413 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 414 | static s_stat* init_cpu(const char *param) |
| 415 | { |
| 416 | int sz; |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 417 | cpu_stat *s; |
Denys Vlasenko | 85b380f | 2019-08-06 16:56:00 +0200 | [diff] [blame] | 418 | sz = param[0] ? strtoul(param, NULL, 0) : 10; |
| 419 | if (sz <= 0) sz = 1; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 420 | if (sz > 1000) sz = 1000; |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 421 | s = xzalloc(sizeof(*s) + sz); |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 422 | /*s->bar[sz] = '\0'; - xzalloc did it */ |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 423 | s->bar_sz = sz; |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 424 | s->collect = collect_cpu; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 425 | return (s_stat*)s; |
| 426 | } |
| 427 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 428 | S_STAT(int_stat) |
| 429 | ullong old; |
| 430 | int no; |
| 431 | S_STAT_END(int_stat) |
| 432 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 433 | static void FAST_FUNC collect_int(int_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 434 | { |
| 435 | ullong data[1]; |
| 436 | ullong old; |
| 437 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 438 | if (rdval(get_file(&proc_stat), "intr", data, 1 << s->no)) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 439 | put_question_marks(4); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | old = s->old; |
| 444 | if (data[0] < old) old = data[0]; //sanitize |
| 445 | s->old = data[0]; |
| 446 | scale(data[0] - old); |
| 447 | } |
| 448 | |
| 449 | static s_stat* init_int(const char *param) |
| 450 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 451 | int_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 452 | s->collect = collect_int; |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 453 | if (param[0] == '\0') { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 454 | s->no = 1; |
| 455 | } else { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 456 | int n = xatoi_positive(param); |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 457 | s->no = n + 2; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 458 | } |
| 459 | return (s_stat*)s; |
| 460 | } |
| 461 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 462 | S_STAT(ctx_stat) |
| 463 | ullong old; |
| 464 | S_STAT_END(ctx_stat) |
| 465 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 466 | static void FAST_FUNC collect_ctx(ctx_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 467 | { |
| 468 | ullong data[1]; |
| 469 | ullong old; |
| 470 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 471 | if (rdval(get_file(&proc_stat), "ctxt", data, 1 << 1)) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 472 | put_question_marks(4); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | old = s->old; |
| 477 | if (data[0] < old) old = data[0]; //sanitize |
| 478 | s->old = data[0]; |
| 479 | scale(data[0] - old); |
| 480 | } |
| 481 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 482 | static s_stat* init_ctx(const char *param UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 483 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 484 | ctx_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 485 | s->collect = collect_ctx; |
| 486 | return (s_stat*)s; |
| 487 | } |
| 488 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 489 | S_STAT(blk_stat) |
| 490 | const char* lookfor; |
| 491 | ullong old[2]; |
| 492 | S_STAT_END(blk_stat) |
| 493 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 494 | static void FAST_FUNC collect_blk(blk_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 495 | { |
| 496 | ullong data[2]; |
| 497 | int i; |
| 498 | |
| 499 | if (is26) { |
| 500 | i = rdval_diskstats(get_file(&proc_diskstats), data); |
| 501 | } else { |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 502 | i = rdval(get_file(&proc_stat), s->lookfor, data, 0 |
| 503 | | (1 << 1) |
| 504 | | (1 << 2) |
| 505 | ); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 506 | // Linux 2.4 reports bio in Kbytes, convert to sectors: |
| 507 | data[0] *= 2; |
| 508 | data[1] *= 2; |
| 509 | } |
| 510 | if (i) { |
| 511 | put_question_marks(9); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | for (i=0; i<2; i++) { |
| 516 | ullong old = s->old[i]; |
| 517 | if (data[i] < old) old = data[i]; //sanitize |
| 518 | s->old[i] = data[i]; |
| 519 | data[i] -= old; |
| 520 | } |
| 521 | scale(data[0]*512); // TODO: *sectorsize |
| 522 | put_c(' '); |
| 523 | scale(data[1]*512); |
| 524 | } |
| 525 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 526 | static s_stat* init_blk(const char *param UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 527 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 528 | blk_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 529 | s->collect = collect_blk; |
| 530 | s->lookfor = "page"; |
| 531 | return (s_stat*)s; |
| 532 | } |
| 533 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 534 | S_STAT(fork_stat) |
| 535 | ullong old; |
| 536 | S_STAT_END(fork_stat) |
| 537 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 538 | static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 539 | { |
| 540 | ullong data[1]; |
| 541 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 542 | if (rdval_loadavg(get_file(&proc_loadavg), data, 1 << 4)) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 543 | put_question_marks(4); |
| 544 | return; |
| 545 | } |
| 546 | scale(data[0]); |
| 547 | } |
| 548 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 549 | static void FAST_FUNC collect_fork(fork_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 550 | { |
| 551 | ullong data[1]; |
| 552 | ullong old; |
| 553 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 554 | if (rdval(get_file(&proc_stat), "processes", data, 1 << 1)) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 555 | put_question_marks(4); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | old = s->old; |
| 560 | if (data[0] < old) old = data[0]; //sanitize |
| 561 | s->old = data[0]; |
| 562 | scale(data[0] - old); |
| 563 | } |
| 564 | |
| 565 | static s_stat* init_fork(const char *param) |
| 566 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 567 | fork_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 568 | if (*param == 'n') { |
| 569 | s->collect = collect_thread_nr; |
| 570 | } else { |
| 571 | s->collect = collect_fork; |
| 572 | } |
| 573 | return (s_stat*)s; |
| 574 | } |
| 575 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 576 | S_STAT(if_stat) |
| 577 | ullong old[4]; |
| 578 | const char *device; |
| 579 | char *device_colon; |
| 580 | S_STAT_END(if_stat) |
| 581 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 582 | static void FAST_FUNC collect_if(if_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 583 | { |
| 584 | ullong data[4]; |
| 585 | int i; |
| 586 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 587 | if (rdval(get_file(&proc_net_dev), s->device_colon, data, 0 |
| 588 | | (1 << 1) |
| 589 | | (1 << 3) |
| 590 | | (1 << 9) |
| 591 | | (1 << 11)) |
| 592 | ) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 593 | put_question_marks(10); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | for (i=0; i<4; i++) { |
| 598 | ullong old = s->old[i]; |
| 599 | if (data[i] < old) old = data[i]; //sanitize |
| 600 | s->old[i] = data[i]; |
| 601 | data[i] -= old; |
| 602 | } |
| 603 | put_c(data[1] ? '*' : ' '); |
| 604 | scale(data[0]); |
| 605 | put_c(data[3] ? '*' : ' '); |
| 606 | scale(data[2]); |
| 607 | } |
| 608 | |
| 609 | static s_stat* init_if(const char *device) |
| 610 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 611 | if_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 612 | |
| 613 | if (!device || !device[0]) |
| 614 | bb_show_usage(); |
| 615 | s->collect = collect_if; |
| 616 | |
| 617 | s->device = device; |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 618 | s->device_colon = xasprintf("%s:", device); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 619 | return (s_stat*)s; |
| 620 | } |
| 621 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 622 | S_STAT(mem_stat) |
| 623 | char opt; |
| 624 | S_STAT_END(mem_stat) |
| 625 | |
| 626 | // "Memory" value should not include any caches. |
| 627 | // IOW: neither "ls -laR /" nor heavy read/write activity |
| 628 | // should affect it. We'd like to also include any |
| 629 | // long-term allocated kernel-side mem, but it is hard |
| 630 | // to figure out. For now, bufs, cached & slab are |
| 631 | // counted as "free" memory |
| 632 | //2.6.16: |
| 633 | //MemTotal: 773280 kB |
| 634 | //MemFree: 25912 kB - genuinely free |
| 635 | //Buffers: 320672 kB - cache |
| 636 | //Cached: 146396 kB - cache |
| 637 | //SwapCached: 0 kB |
| 638 | //Active: 183064 kB |
| 639 | //Inactive: 356892 kB |
| 640 | //HighTotal: 0 kB |
| 641 | //HighFree: 0 kB |
| 642 | //LowTotal: 773280 kB |
| 643 | //LowFree: 25912 kB |
| 644 | //SwapTotal: 131064 kB |
| 645 | //SwapFree: 131064 kB |
| 646 | //Dirty: 48 kB |
| 647 | //Writeback: 0 kB |
| 648 | //Mapped: 96620 kB |
| 649 | //Slab: 200668 kB - takes 7 Mb on my box fresh after boot, |
| 650 | // but includes dentries and inodes |
| 651 | // (== can take arbitrary amount of mem) |
| 652 | //CommitLimit: 517704 kB |
| 653 | //Committed_AS: 236776 kB |
| 654 | //PageTables: 1248 kB |
| 655 | //VmallocTotal: 516052 kB |
| 656 | //VmallocUsed: 3852 kB |
| 657 | //VmallocChunk: 512096 kB |
| 658 | //HugePages_Total: 0 |
| 659 | //HugePages_Free: 0 |
| 660 | //Hugepagesize: 4096 kB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 661 | static void FAST_FUNC collect_mem(mem_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 662 | { |
| 663 | ullong m_total = 0; |
| 664 | ullong m_free = 0; |
| 665 | ullong m_bufs = 0; |
| 666 | ullong m_cached = 0; |
| 667 | ullong m_slab = 0; |
| 668 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 669 | if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1 << 1)) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 670 | put_question_marks(4); |
| 671 | return; |
| 672 | } |
Denis Vlasenko | 8a2e421 | 2008-02-29 07:46:19 +0000 | [diff] [blame] | 673 | if (s->opt == 't') { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 674 | scale(m_total << 10); |
| 675 | return; |
| 676 | } |
| 677 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 678 | if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1 << 1) |
| 679 | || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1 << 1) |
| 680 | || rdval(proc_meminfo.file, "Cached:", &m_cached, 1 << 1) |
| 681 | || rdval(proc_meminfo.file, "Slab:", &m_slab , 1 << 1) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 682 | ) { |
| 683 | put_question_marks(4); |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | m_free += m_bufs + m_cached + m_slab; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 688 | switch (s->opt) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 689 | case 'f': |
| 690 | scale(m_free << 10); break; |
| 691 | default: |
| 692 | scale((m_total - m_free) << 10); break; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | static s_stat* init_mem(const char *param) |
| 697 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 698 | mem_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 699 | s->collect = collect_mem; |
| 700 | s->opt = param[0]; |
| 701 | return (s_stat*)s; |
| 702 | } |
| 703 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 704 | S_STAT(swp_stat) |
| 705 | S_STAT_END(swp_stat) |
| 706 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 707 | static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 708 | { |
| 709 | ullong s_total[1]; |
| 710 | ullong s_free[1]; |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 711 | if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1 << 1) |
| 712 | || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1 << 1) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 713 | ) { |
| 714 | put_question_marks(4); |
| 715 | return; |
| 716 | } |
| 717 | scale((s_total[0]-s_free[0]) << 10); |
| 718 | } |
| 719 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 720 | static s_stat* init_swp(const char *param UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 721 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 722 | swp_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 723 | s->collect = collect_swp; |
| 724 | return (s_stat*)s; |
| 725 | } |
| 726 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 727 | S_STAT(fd_stat) |
| 728 | S_STAT_END(fd_stat) |
| 729 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 730 | static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 731 | { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 732 | ullong data[2]; |
| 733 | |
Denys Vlasenko | 8a26fda | 2016-03-15 15:52:40 +0100 | [diff] [blame] | 734 | if (rdval(get_file(&proc_sys_fs_filenr), "", data, 0 |
| 735 | | (1 << 1) |
| 736 | | (1 << 2)) |
| 737 | ) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 738 | put_question_marks(4); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | scale(data[0] - data[1]); |
| 743 | } |
| 744 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 745 | static s_stat* init_fd(const char *param UNUSED_PARAM) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 746 | { |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 747 | fd_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 748 | s->collect = collect_fd; |
| 749 | return (s_stat*)s; |
| 750 | } |
| 751 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 752 | S_STAT(time_stat) |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 753 | unsigned prec; |
| 754 | unsigned scale; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 755 | S_STAT_END(time_stat) |
| 756 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 757 | static void FAST_FUNC collect_time(time_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 758 | { |
| 759 | char buf[sizeof("12:34:56.123456")]; |
| 760 | struct tm* tm; |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 761 | unsigned us = tv.tv_usec + s->scale/2; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 762 | time_t t = tv.tv_sec; |
| 763 | |
| 764 | if (us >= 1000000) { |
| 765 | t++; |
| 766 | us -= 1000000; |
| 767 | } |
| 768 | tm = localtime(&t); |
| 769 | |
| 770 | sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 771 | if (s->prec) |
| 772 | sprintf(buf+8, ".%0*d", s->prec, us / s->scale); |
| 773 | put(buf); |
| 774 | } |
| 775 | |
| 776 | static s_stat* init_time(const char *param) |
| 777 | { |
| 778 | int prec; |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 779 | time_stat *s = xzalloc(sizeof(*s)); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 780 | |
| 781 | s->collect = collect_time; |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 782 | prec = param[0] - '0'; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 783 | if (prec < 0) prec = 0; |
| 784 | else if (prec > 6) prec = 6; |
| 785 | s->prec = prec; |
| 786 | s->scale = 1; |
| 787 | while (prec++ < 6) |
| 788 | s->scale *= 10; |
| 789 | return (s_stat*)s; |
| 790 | } |
| 791 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 792 | static void FAST_FUNC collect_info(s_stat *s) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 793 | { |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 794 | gen ^= 1; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 795 | while (s) { |
| 796 | put(s->label); |
| 797 | s->collect(s); |
| 798 | s = s->next; |
| 799 | } |
| 800 | } |
| 801 | |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 802 | typedef s_stat* init_func(const char *param); |
| 803 | |
Denys Vlasenko | 41b1e2c | 2016-03-15 16:13:23 +0100 | [diff] [blame] | 804 | static const char options[] ALIGN1 = "ncmsfixptbr"; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 805 | static init_func *const init_functions[] = { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 806 | init_if, |
| 807 | init_cpu, |
| 808 | init_mem, |
| 809 | init_swp, |
| 810 | init_fd, |
| 811 | init_int, |
| 812 | init_ctx, |
| 813 | init_fork, |
| 814 | init_time, |
| 815 | init_blk, |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 816 | init_cr |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 817 | }; |
| 818 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 819 | int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 820 | int nmeter_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 821 | { |
| 822 | char buf[32]; |
| 823 | s_stat *first = NULL; |
| 824 | s_stat *last = NULL; |
| 825 | s_stat *s; |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 826 | char *opt_d; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 827 | char *cur, *prev; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 828 | |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 829 | INIT_G(); |
| 830 | |
| 831 | xchdir("/proc"); |
| 832 | |
Denis Vlasenko | cd785fb | 2008-08-14 21:57:43 +0000 | [diff] [blame] | 833 | if (open_read_close("version", buf, sizeof(buf)-1) > 0) { |
| 834 | buf[sizeof(buf)-1] = '\0'; |
| 835 | is26 = (strstr(buf, " 2.4.") == NULL); |
| 836 | } |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 837 | |
Denys Vlasenko | 41b1e2c | 2016-03-15 16:13:23 +0100 | [diff] [blame] | 838 | if (getopt32(argv, "d:", &opt_d)) { |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 839 | G.delta = xatoi(opt_d) * 1000; |
| 840 | G.deltanz = G.delta > 0 ? G.delta : 1; |
| 841 | need_seconds = (1000000 % G.deltanz) != 0; |
Denys Vlasenko | 41b1e2c | 2016-03-15 16:13:23 +0100 | [diff] [blame] | 842 | } |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 843 | argv += optind; |
| 844 | |
| 845 | if (!argv[0]) |
| 846 | bb_show_usage(); |
| 847 | |
| 848 | // Can use argv[0] directly, but this will mess up |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 849 | // parameters as seen by e.g. ps. Making a copy... |
Denys Vlasenko | a604186 | 2010-12-13 15:17:22 +0100 | [diff] [blame] | 850 | cur = xstrdup(argv[0]); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 851 | while (1) { |
| 852 | char *param, *p; |
| 853 | prev = cur; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 854 | again: |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 855 | cur = strchr(cur, '%'); |
| 856 | if (!cur) |
| 857 | break; |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 858 | if (cur[1] == '%') { // %% |
Denis Vlasenko | 0f293b9 | 2008-07-22 20:16:55 +0000 | [diff] [blame] | 859 | overlapping_strcpy(cur, cur + 1); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 860 | cur++; |
| 861 | goto again; |
| 862 | } |
| 863 | *cur++ = '\0'; // overwrite % |
| 864 | if (cur[0] == '[') { |
| 865 | // format: %[foptstring] |
| 866 | cur++; |
| 867 | p = strchr(options, cur[0]); |
| 868 | param = cur+1; |
| 869 | while (cur[0] != ']') { |
| 870 | if (!cur[0]) |
| 871 | bb_show_usage(); |
| 872 | cur++; |
| 873 | } |
| 874 | *cur++ = '\0'; // overwrite [ |
| 875 | } else { |
| 876 | // format: %NNNNNNf |
| 877 | param = cur; |
| 878 | while (cur[0] >= '0' && cur[0] <= '9') |
| 879 | cur++; |
| 880 | if (!cur[0]) |
| 881 | bb_show_usage(); |
| 882 | p = strchr(options, cur[0]); |
| 883 | *cur++ = '\0'; // overwrite format char |
| 884 | } |
| 885 | if (!p) |
| 886 | bb_show_usage(); |
| 887 | s = init_functions[p-options](param); |
| 888 | if (s) { |
| 889 | s->label = prev; |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 890 | /*s->next = NULL; - all initXXX funcs use xzalloc */ |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 891 | if (!first) |
| 892 | first = s; |
| 893 | else |
| 894 | last->next = s; |
| 895 | last = s; |
| 896 | } else { |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 897 | // %r option. remove it from string |
| 898 | overlapping_strcpy(prev + strlen(prev), cur); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 899 | cur = prev; |
| 900 | } |
| 901 | } |
| 902 | if (prev[0]) { |
| 903 | s = init_literal(); |
| 904 | s->label = prev; |
Denis Vlasenko | 333aa0c | 2008-06-28 23:30:37 +0000 | [diff] [blame] | 905 | /*s->next = NULL; - all initXXX funcs use xzalloc */ |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 906 | if (!first) |
| 907 | first = s; |
| 908 | else |
| 909 | last->next = s; |
| 910 | last = s; |
| 911 | } |
| 912 | |
| 913 | // Generate first samples but do not print them, they're bogus |
| 914 | collect_info(first); |
| 915 | reset_outbuf(); |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 916 | if (G.delta >= 0) { |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 917 | gettimeofday(&tv, NULL); |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 918 | usleep(G.delta > 1000000 ? 1000000 : G.delta - tv.tv_usec % G.deltanz); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | while (1) { |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 922 | gettimeofday(&tv, NULL); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 923 | collect_info(first); |
Denys Vlasenko | a63e2a8 | 2016-03-15 16:06:29 +0100 | [diff] [blame] | 924 | put_c(G.final_char); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 925 | print_outbuf(); |
| 926 | |
| 927 | // Negative delta -> no usleep at all |
| 928 | // This will hog the CPU but you can have REALLY GOOD |
| 929 | // time resolution ;) |
| 930 | // TODO: detect and avoid useless updates |
| 931 | // (like: nothing happens except time) |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 932 | if (G.delta >= 0) { |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 933 | int rem; |
| 934 | // can be commented out, will sacrifice sleep time precision a bit |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 935 | gettimeofday(&tv, NULL); |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 936 | if (need_seconds) |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 937 | rem = G.delta - ((ullong)tv.tv_sec*1000000 + tv.tv_usec) % G.deltanz; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 938 | else |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 939 | rem = G.delta - (unsigned)tv.tv_usec % G.deltanz; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 940 | // Sometimes kernel wakes us up just a tiny bit earlier than asked |
| 941 | // Do not go to very short sleep in this case |
Denys Vlasenko | 0d1b71e | 2016-03-15 17:54:17 +0100 | [diff] [blame] | 942 | if (rem < (unsigned)G.delta / 128) { |
| 943 | rem += G.delta; |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 944 | } |
| 945 | usleep(rem); |
| 946 | } |
| 947 | } |
| 948 | |
Denis Vlasenko | 199c0d5 | 2007-05-30 14:48:38 +0000 | [diff] [blame] | 949 | /*return 0;*/ |
Denis Vlasenko | 1db39b2 | 2006-10-11 20:59:02 +0000 | [diff] [blame] | 950 | } |