nmeter.c 22 KB

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