nmeter.c 19 KB

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