3
0

nmeter.c 19 KB

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