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