vmstat.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* vmstat.c - Report virtual memory statistics.
  2. *
  3. * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
  4. *
  5. * TODO: I have no idea how "system" category is calculated.
  6. * whatever we're doing isn't matching what other implementations are doing.
  7. USE_VMSTAT(NEWTOY(vmstat, ">2n", TOYFLAG_BIN))
  8. config VMSTAT
  9. bool "vmstat"
  10. default y
  11. help
  12. usage: vmstat [-n] [DELAY [COUNT]]
  13. Print virtual memory statistics, repeating each DELAY seconds, COUNT times.
  14. (With no DELAY, prints one line. With no COUNT, repeats until killed.)
  15. Show processes running and blocked, kilobytes swapped, free, buffered, and
  16. cached, kilobytes swapped in and out per second, file disk blocks input and
  17. output per second, interrupts and context switches per second, percent
  18. of CPU time spent running user code, system code, idle, and awaiting I/O.
  19. First line is since system started, later lines are since last line.
  20. -n Display the header only once
  21. */
  22. #define FOR_vmstat
  23. #include "toys.h"
  24. struct vmstat_proc {
  25. // From /proc/stat (jiffies)
  26. uint64_t user, nice, sys, idle, wait, irq, sirq, intr, ctxt, running, blocked;
  27. // From /proc/meminfo (units are kb)
  28. uint64_t memfree, buffers, cached, swapfree, swaptotal;
  29. // From /proc/vmstat (units are kb)
  30. uint64_t io_in, io_out;
  31. // From /proc/vmstat (units are pages)
  32. uint64_t swap_in, swap_out;
  33. };
  34. // All the elements of vmstat_proc are the same size, so we can populate it as
  35. // a big array, then read the elements back out by name
  36. static void get_vmstat_proc(struct vmstat_proc *vmstat_proc)
  37. {
  38. char *vmstuff[] = { "/proc/stat", "cpu ", 0, 0, 0, 0, 0, 0,
  39. "intr ", "ctxt ", "procs_running ", "procs_blocked ", "/proc/meminfo",
  40. "MemFree: ", "Buffers: ", "Cached: ", "SwapFree: ", "SwapTotal: ",
  41. "/proc/vmstat", "pgpgin ", "pgpgout ", "pswpin ", "pswpout " };
  42. uint64_t *new = (uint64_t *)vmstat_proc;
  43. char *p = p, *name = name, *file = NULL;
  44. int i, j;
  45. // We use vmstuff to fill out vmstat_proc as an array of uint64_t:
  46. // Strings starting with / are the file to find next entries in
  47. // Any other string is a key to search for, with decimal value right after
  48. // 0 means parse another value on same line as last key
  49. for (i = 0; i<ARRAY_LEN(vmstuff); i++) {
  50. if (!vmstuff[i]) p++;
  51. else if (*vmstuff[i] == '/') {
  52. // /proc/stat for a 48-core machine doesn't fit in toybuf.
  53. free(file);
  54. file = xreadfile(name = vmstuff[i], 0, 0);
  55. continue;
  56. } else p = strafter(file, vmstuff[i]);
  57. if (!p || 1!=sscanf(p, "%"PRIu64"%n", new++, &j))
  58. error_exit("Bad %sin %s: %s", vmstuff[i], name, p ? p : "");
  59. p += j;
  60. }
  61. free(file);
  62. }
  63. void vmstat_main(void)
  64. {
  65. struct vmstat_proc top[2];
  66. int i, loop_delay = 0, loop_max = 0;
  67. unsigned loop, rows = 25, page_kb = sysconf(_SC_PAGESIZE)/1024;
  68. char *headers="r\0b\0swpd\0free\0buff\0cache\0si\0so\0bi\0bo\0in\0cs\0us\0"
  69. "sy\0id\0wa", lengths[] = {2,2,7,7,6,7,5,5,5,5,5,5,2,2,2,2};
  70. memset(top, 0, sizeof(top));
  71. if (toys.optc) loop_delay = atolx_range(toys.optargs[0], 0, INT_MAX);
  72. if (toys.optc > 1) loop_max = atolx_range(toys.optargs[1], 1, INT_MAX);
  73. for (loop = 0; !loop_max || loop < loop_max; loop++) {
  74. unsigned idx = loop&1, offset = 0, expected = 0;
  75. uint64_t units, total_hz, *ptr = (uint64_t *)(top+idx),
  76. *oldptr = (uint64_t *)(top+!idx);
  77. if (loop && loop_delay) sleep(loop_delay);
  78. // Print headers
  79. if (rows>3 && !(loop % (rows-3))) {
  80. char *header = headers;
  81. if (!(toys.optflags&FLAG_n) && isatty(1)) terminal_size(0, &rows);
  82. else rows = 0;
  83. printf("procs ------------memory------------ ----swap--- -----io---- ---system-- ----cpu----\n");
  84. for (i=0; i<sizeof(lengths); i++) {
  85. printf(" %*s"+!i, lengths[i], header);
  86. header += strlen(header)+1;
  87. }
  88. xputc('\n');
  89. }
  90. // Read data and combine some fields we display as aggregates
  91. get_vmstat_proc(top+idx);
  92. top[idx].running--; // Don't include ourselves
  93. top[idx].user += top[idx].nice;
  94. top[idx].sys += top[idx].irq + top[idx].sirq;
  95. top[idx].swaptotal -= top[idx].swapfree;
  96. // Collect unit adjustments (outside the inner loop to save time)
  97. if (!loop) {
  98. char *s = toybuf;
  99. xreadfile("/proc/uptime", toybuf, sizeof(toybuf));
  100. while (*(s++) > ' ');
  101. sscanf(s, "%"PRIu64, &units);
  102. } else units = loop_delay;
  103. // add up user, sys, idle, and wait time used since last time
  104. // (Already appended nice to user)
  105. total_hz = 0;
  106. for (i=0; i<4; i++) total_hz += ptr[i+!!i] - oldptr[i+!!i];
  107. // Output values in order[]: running, blocked, swaptotal, memfree, buffers,
  108. // cache, swap_in, swap_out, io_in, io_out, sirq, ctxt, user, sys, idle,wait
  109. for (i=0; i<sizeof(lengths); i++) {
  110. char order[] = {9, 10, 15, 11, 12, 13, 18, 19, 16, 17, 6, 8, 0, 2, 3, 4};
  111. uint64_t out = ptr[order[i]];
  112. int len;
  113. // Adjust rate and units
  114. if (i>5) out -= oldptr[order[i]];
  115. if (order[i]<7) out = ((out*100) + (total_hz/2)) / total_hz;
  116. else if (order[i]>17) out = ((out * page_kb)+(units-1))/units;
  117. else if (order[i]>15) out = ((out)+(units-1))/units;
  118. else if (order[i]<9) out = (out+(units-1)) / units;
  119. // If a field was too big to fit in its slot, try to compensate later
  120. expected += lengths[i] + !!i;
  121. len = expected - offset - !!i;
  122. if (len < 0) len = 0;
  123. offset += printf(" %*"PRIu64+!i, len, out);
  124. }
  125. xputc('\n');
  126. if (!loop_delay) break;
  127. }
  128. }