3
0

nmeter.c 21 KB

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