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