nmeter.c 19 KB

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