nmeter.c 19 KB

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