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