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