nmeter.c 21 KB

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