3
0

nmeter.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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")[0] = '\0';
  300. put(buf);
  301. }
  302. #define S_STAT(a) \
  303. typedef struct a { \
  304. struct s_stat *next; \
  305. void (*collect)(struct a *s) FAST_FUNC; \
  306. const char *label;
  307. #define S_STAT_END(a) } a;
  308. S_STAT(s_stat)
  309. S_STAT_END(s_stat)
  310. static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM)
  311. {
  312. }
  313. static s_stat* init_literal(void)
  314. {
  315. s_stat *s = xzalloc(sizeof(*s));
  316. s->collect = collect_literal;
  317. return (s_stat*)s;
  318. }
  319. static s_stat* init_delay(const char *param)
  320. {
  321. delta = strtoul(param, NULL, 0) * 1000; /* param can be "" */
  322. deltanz = delta > 0 ? delta : 1;
  323. need_seconds = (1000000%deltanz) != 0;
  324. return NULL;
  325. }
  326. static s_stat* init_cr(const char *param UNUSED_PARAM)
  327. {
  328. final_str = "\r";
  329. return (s_stat*)0;
  330. }
  331. // user nice system idle iowait irq softirq (last 3 only in 2.6)
  332. //cpu 649369 0 341297 4336769 11640 7122 1183
  333. //cpuN 649369 0 341297 4336769 11640 7122 1183
  334. enum { CPU_FIELDCNT = 7 };
  335. S_STAT(cpu_stat)
  336. ullong old[CPU_FIELDCNT];
  337. int bar_sz;
  338. char *bar;
  339. S_STAT_END(cpu_stat)
  340. static void FAST_FUNC collect_cpu(cpu_stat *s)
  341. {
  342. ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
  343. unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
  344. ullong all = 0;
  345. int norm_all = 0;
  346. int bar_sz = s->bar_sz;
  347. char *bar = s->bar;
  348. int i;
  349. if (rdval(get_file(&proc_stat), "cpu ", data, 1, 2, 3, 4, 5, 6, 7)) {
  350. put_question_marks(bar_sz);
  351. return;
  352. }
  353. for (i = 0; i < CPU_FIELDCNT; i++) {
  354. ullong old = s->old[i];
  355. if (data[i] < old) old = data[i]; //sanitize
  356. s->old[i] = data[i];
  357. all += (data[i] -= old);
  358. }
  359. if (all) {
  360. for (i = 0; i < CPU_FIELDCNT; i++) {
  361. ullong t = bar_sz * data[i];
  362. norm_all += data[i] = t / all;
  363. frac[i] = t % all;
  364. }
  365. while (norm_all < bar_sz) {
  366. unsigned max = frac[0];
  367. int pos = 0;
  368. for (i = 1; i < CPU_FIELDCNT; i++) {
  369. if (frac[i] > max) max = frac[i], pos = i;
  370. }
  371. frac[pos] = 0; //avoid bumping up same value twice
  372. data[pos]++;
  373. norm_all++;
  374. }
  375. memset(bar, '.', bar_sz);
  376. memset(bar, 'S', data[2]); bar += data[2]; //sys
  377. memset(bar, 'U', data[0]); bar += data[0]; //usr
  378. memset(bar, 'N', data[1]); bar += data[1]; //nice
  379. memset(bar, 'D', data[4]); bar += data[4]; //iowait
  380. memset(bar, 'I', data[5]); bar += data[5]; //irq
  381. memset(bar, 'i', data[6]); bar += data[6]; //softirq
  382. } else {
  383. memset(bar, '?', bar_sz);
  384. }
  385. put(s->bar);
  386. }
  387. static s_stat* init_cpu(const char *param)
  388. {
  389. int sz;
  390. cpu_stat *s = xzalloc(sizeof(*s));
  391. s->collect = collect_cpu;
  392. sz = strtoul(param, NULL, 0); /* param can be "" */
  393. if (sz < 10) sz = 10;
  394. if (sz > 1000) sz = 1000;
  395. s->bar = xzalloc(sz+1);
  396. /*s->bar[sz] = '\0'; - xzalloc did it */
  397. s->bar_sz = sz;
  398. return (s_stat*)s;
  399. }
  400. S_STAT(int_stat)
  401. ullong old;
  402. int no;
  403. S_STAT_END(int_stat)
  404. static void FAST_FUNC collect_int(int_stat *s)
  405. {
  406. ullong data[1];
  407. ullong old;
  408. if (rdval(get_file(&proc_stat), "intr", data, s->no)) {
  409. put_question_marks(4);
  410. return;
  411. }
  412. old = s->old;
  413. if (data[0] < old) old = data[0]; //sanitize
  414. s->old = data[0];
  415. scale(data[0] - old);
  416. }
  417. static s_stat* init_int(const char *param)
  418. {
  419. int_stat *s = xzalloc(sizeof(*s));
  420. s->collect = collect_int;
  421. if (param[0] == '\0') {
  422. s->no = 1;
  423. } else {
  424. int n = xatoi_positive(param);
  425. s->no = n + 2;
  426. }
  427. return (s_stat*)s;
  428. }
  429. S_STAT(ctx_stat)
  430. ullong old;
  431. S_STAT_END(ctx_stat)
  432. static void FAST_FUNC collect_ctx(ctx_stat *s)
  433. {
  434. ullong data[1];
  435. ullong old;
  436. if (rdval(get_file(&proc_stat), "ctxt", data, 1)) {
  437. put_question_marks(4);
  438. return;
  439. }
  440. old = s->old;
  441. if (data[0] < old) old = data[0]; //sanitize
  442. s->old = data[0];
  443. scale(data[0] - old);
  444. }
  445. static s_stat* init_ctx(const char *param UNUSED_PARAM)
  446. {
  447. ctx_stat *s = xzalloc(sizeof(*s));
  448. s->collect = collect_ctx;
  449. return (s_stat*)s;
  450. }
  451. S_STAT(blk_stat)
  452. const char* lookfor;
  453. ullong old[2];
  454. S_STAT_END(blk_stat)
  455. static void FAST_FUNC collect_blk(blk_stat *s)
  456. {
  457. ullong data[2];
  458. int i;
  459. if (is26) {
  460. i = rdval_diskstats(get_file(&proc_diskstats), data);
  461. } else {
  462. i = rdval(get_file(&proc_stat), s->lookfor, data, 1, 2);
  463. // Linux 2.4 reports bio in Kbytes, convert to sectors:
  464. data[0] *= 2;
  465. data[1] *= 2;
  466. }
  467. if (i) {
  468. put_question_marks(9);
  469. return;
  470. }
  471. for (i=0; i<2; i++) {
  472. ullong old = s->old[i];
  473. if (data[i] < old) old = data[i]; //sanitize
  474. s->old[i] = data[i];
  475. data[i] -= old;
  476. }
  477. scale(data[0]*512); // TODO: *sectorsize
  478. put_c(' ');
  479. scale(data[1]*512);
  480. }
  481. static s_stat* init_blk(const char *param UNUSED_PARAM)
  482. {
  483. blk_stat *s = xzalloc(sizeof(*s));
  484. s->collect = collect_blk;
  485. s->lookfor = "page";
  486. return (s_stat*)s;
  487. }
  488. S_STAT(fork_stat)
  489. ullong old;
  490. S_STAT_END(fork_stat)
  491. static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
  492. {
  493. ullong data[1];
  494. if (rdval_loadavg(get_file(&proc_loadavg), data, 4)) {
  495. put_question_marks(4);
  496. return;
  497. }
  498. scale(data[0]);
  499. }
  500. static void FAST_FUNC collect_fork(fork_stat *s)
  501. {
  502. ullong data[1];
  503. ullong old;
  504. if (rdval(get_file(&proc_stat), "processes", data, 1)) {
  505. put_question_marks(4);
  506. return;
  507. }
  508. old = s->old;
  509. if (data[0] < old) old = data[0]; //sanitize
  510. s->old = data[0];
  511. scale(data[0] - old);
  512. }
  513. static s_stat* init_fork(const char *param)
  514. {
  515. fork_stat *s = xzalloc(sizeof(*s));
  516. if (*param == 'n') {
  517. s->collect = collect_thread_nr;
  518. } else {
  519. s->collect = collect_fork;
  520. }
  521. return (s_stat*)s;
  522. }
  523. S_STAT(if_stat)
  524. ullong old[4];
  525. const char *device;
  526. char *device_colon;
  527. S_STAT_END(if_stat)
  528. static void FAST_FUNC collect_if(if_stat *s)
  529. {
  530. ullong data[4];
  531. int i;
  532. if (rdval(get_file(&proc_net_dev), s->device_colon, data, 1, 3, 9, 11)) {
  533. put_question_marks(10);
  534. return;
  535. }
  536. for (i=0; i<4; i++) {
  537. ullong old = s->old[i];
  538. if (data[i] < old) old = data[i]; //sanitize
  539. s->old[i] = data[i];
  540. data[i] -= old;
  541. }
  542. put_c(data[1] ? '*' : ' ');
  543. scale(data[0]);
  544. put_c(data[3] ? '*' : ' ');
  545. scale(data[2]);
  546. }
  547. static s_stat* init_if(const char *device)
  548. {
  549. if_stat *s = xzalloc(sizeof(*s));
  550. if (!device || !device[0])
  551. bb_show_usage();
  552. s->collect = collect_if;
  553. s->device = device;
  554. s->device_colon = xasprintf("%s:", device);
  555. return (s_stat*)s;
  556. }
  557. S_STAT(mem_stat)
  558. char opt;
  559. S_STAT_END(mem_stat)
  560. // "Memory" value should not include any caches.
  561. // IOW: neither "ls -laR /" nor heavy read/write activity
  562. // should affect it. We'd like to also include any
  563. // long-term allocated kernel-side mem, but it is hard
  564. // to figure out. For now, bufs, cached & slab are
  565. // counted as "free" memory
  566. //2.6.16:
  567. //MemTotal: 773280 kB
  568. //MemFree: 25912 kB - genuinely free
  569. //Buffers: 320672 kB - cache
  570. //Cached: 146396 kB - cache
  571. //SwapCached: 0 kB
  572. //Active: 183064 kB
  573. //Inactive: 356892 kB
  574. //HighTotal: 0 kB
  575. //HighFree: 0 kB
  576. //LowTotal: 773280 kB
  577. //LowFree: 25912 kB
  578. //SwapTotal: 131064 kB
  579. //SwapFree: 131064 kB
  580. //Dirty: 48 kB
  581. //Writeback: 0 kB
  582. //Mapped: 96620 kB
  583. //Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
  584. // but includes dentries and inodes
  585. // (== can take arbitrary amount of mem)
  586. //CommitLimit: 517704 kB
  587. //Committed_AS: 236776 kB
  588. //PageTables: 1248 kB
  589. //VmallocTotal: 516052 kB
  590. //VmallocUsed: 3852 kB
  591. //VmallocChunk: 512096 kB
  592. //HugePages_Total: 0
  593. //HugePages_Free: 0
  594. //Hugepagesize: 4096 kB
  595. static void FAST_FUNC collect_mem(mem_stat *s)
  596. {
  597. ullong m_total = 0;
  598. ullong m_free = 0;
  599. ullong m_bufs = 0;
  600. ullong m_cached = 0;
  601. ullong m_slab = 0;
  602. if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1)) {
  603. put_question_marks(4);
  604. return;
  605. }
  606. if (s->opt == 't') {
  607. scale(m_total << 10);
  608. return;
  609. }
  610. if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1)
  611. || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1)
  612. || rdval(proc_meminfo.file, "Cached:", &m_cached, 1)
  613. || rdval(proc_meminfo.file, "Slab:", &m_slab , 1)
  614. ) {
  615. put_question_marks(4);
  616. return;
  617. }
  618. m_free += m_bufs + m_cached + m_slab;
  619. switch (s->opt) {
  620. case 'f':
  621. scale(m_free << 10); break;
  622. default:
  623. scale((m_total - m_free) << 10); break;
  624. }
  625. }
  626. static s_stat* init_mem(const char *param)
  627. {
  628. mem_stat *s = xzalloc(sizeof(*s));
  629. s->collect = collect_mem;
  630. s->opt = param[0];
  631. return (s_stat*)s;
  632. }
  633. S_STAT(swp_stat)
  634. S_STAT_END(swp_stat)
  635. static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
  636. {
  637. ullong s_total[1];
  638. ullong s_free[1];
  639. if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1)
  640. || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1)
  641. ) {
  642. put_question_marks(4);
  643. return;
  644. }
  645. scale((s_total[0]-s_free[0]) << 10);
  646. }
  647. static s_stat* init_swp(const char *param UNUSED_PARAM)
  648. {
  649. swp_stat *s = xzalloc(sizeof(*s));
  650. s->collect = collect_swp;
  651. return (s_stat*)s;
  652. }
  653. S_STAT(fd_stat)
  654. S_STAT_END(fd_stat)
  655. static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
  656. {
  657. ullong data[2];
  658. if (rdval(get_file(&proc_sys_fs_filenr), "", data, 1, 2)) {
  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. int prec;
  672. int 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. int 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. // Deprecated %NNNd is to be removed, -d MSEC supersedes it
  715. static const char options[] ALIGN1 = "ncmsfixptbdr";
  716. static init_func *const init_functions[] = {
  717. init_if,
  718. init_cpu,
  719. init_mem,
  720. init_swp,
  721. init_fd,
  722. init_int,
  723. init_ctx,
  724. init_fork,
  725. init_time,
  726. init_blk,
  727. init_delay,
  728. init_cr
  729. };
  730. int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  731. int nmeter_main(int argc UNUSED_PARAM, char **argv)
  732. {
  733. char buf[32];
  734. s_stat *first = NULL;
  735. s_stat *last = NULL;
  736. s_stat *s;
  737. char *opt_d;
  738. char *cur, *prev;
  739. INIT_G();
  740. xchdir("/proc");
  741. if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
  742. buf[sizeof(buf)-1] = '\0';
  743. is26 = (strstr(buf, " 2.4.") == NULL);
  744. }
  745. if (getopt32(argv, "d:", &opt_d))
  746. init_delay(opt_d);
  747. argv += optind;
  748. if (!argv[0])
  749. bb_show_usage();
  750. // Can use argv[0] directly, but this will mess up
  751. // parameters as seen by e.g. ps. Making a copy...
  752. cur = xstrdup(argv[0]);
  753. while (1) {
  754. char *param, *p;
  755. prev = cur;
  756. again:
  757. cur = strchr(cur, '%');
  758. if (!cur)
  759. break;
  760. if (cur[1] == '%') { // %%
  761. overlapping_strcpy(cur, cur + 1);
  762. cur++;
  763. goto again;
  764. }
  765. *cur++ = '\0'; // overwrite %
  766. if (cur[0] == '[') {
  767. // format: %[foptstring]
  768. cur++;
  769. p = strchr(options, cur[0]);
  770. param = cur+1;
  771. while (cur[0] != ']') {
  772. if (!cur[0])
  773. bb_show_usage();
  774. cur++;
  775. }
  776. *cur++ = '\0'; // overwrite [
  777. } else {
  778. // format: %NNNNNNf
  779. param = cur;
  780. while (cur[0] >= '0' && cur[0] <= '9')
  781. cur++;
  782. if (!cur[0])
  783. bb_show_usage();
  784. p = strchr(options, cur[0]);
  785. *cur++ = '\0'; // overwrite format char
  786. }
  787. if (!p)
  788. bb_show_usage();
  789. s = init_functions[p-options](param);
  790. if (s) {
  791. s->label = prev;
  792. /*s->next = NULL; - all initXXX funcs use xzalloc */
  793. if (!first)
  794. first = s;
  795. else
  796. last->next = s;
  797. last = s;
  798. } else {
  799. // %NNNNd or %r option. remove it from string
  800. strcpy(prev + strlen(prev), cur);
  801. cur = prev;
  802. }
  803. }
  804. if (prev[0]) {
  805. s = init_literal();
  806. s->label = prev;
  807. /*s->next = NULL; - all initXXX funcs use xzalloc */
  808. if (!first)
  809. first = s;
  810. else
  811. last->next = s;
  812. last = s;
  813. }
  814. // Generate first samples but do not print them, they're bogus
  815. collect_info(first);
  816. reset_outbuf();
  817. if (delta >= 0) {
  818. gettimeofday(&tv, NULL);
  819. usleep(delta > 1000000 ? 1000000 : delta - tv.tv_usec%deltanz);
  820. }
  821. while (1) {
  822. gettimeofday(&tv, NULL);
  823. collect_info(first);
  824. put(final_str);
  825. print_outbuf();
  826. // Negative delta -> no usleep at all
  827. // This will hog the CPU but you can have REALLY GOOD
  828. // time resolution ;)
  829. // TODO: detect and avoid useless updates
  830. // (like: nothing happens except time)
  831. if (delta >= 0) {
  832. int rem;
  833. // can be commented out, will sacrifice sleep time precision a bit
  834. gettimeofday(&tv, NULL);
  835. if (need_seconds)
  836. rem = delta - ((ullong)tv.tv_sec*1000000 + tv.tv_usec) % deltanz;
  837. else
  838. rem = delta - tv.tv_usec%deltanz;
  839. // Sometimes kernel wakes us up just a tiny bit earlier than asked
  840. // Do not go to very short sleep in this case
  841. if (rem < delta/128) {
  842. rem += delta;
  843. }
  844. usleep(rem);
  845. }
  846. }
  847. /*return 0;*/
  848. }