nmeter.c 18 KB

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