nmeter.c 17 KB

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