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