1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* vi: set sw=4 ts=4:
*
* vmstat.c - Report virtual memory statistics.
*
* Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
*
* Not in SUSv4.
USE_VMSTAT(NEWTOY(vmstat, ">2n", TOYFLAG_BIN))
config VMSTAT
bool "vmstat"
default y
help
usage: vmstat [-n] [delay [count]]
-n Display the header only once
delay The delay between updates in seconds, when not specified
the average since boot is displayed.
count Number of updates to display, the default is inifinite.
*/
#include "toys.h"
void read_proc_stat(unsigned int * proc_running, unsigned int * proc_blocked,
uint64_t * sys_irq, uint64_t * sys_ctxt,
uint64_t * cpu_user, uint64_t * cpu_sys, uint64_t * cpu_idle, uint64_t * cpu_wait)
{
char * off;
uint64_t c_user, c_nice, c_sys, c_irq, c_sirq;
int fd = xopen("/proc/stat", O_RDONLY);
if (fdlength(fd) > sizeof(toybuf))
error_exit("/proc/stat is too large");
xread(fd, toybuf, sizeof(toybuf));
off = strstr(toybuf, "cpu ");
// Ignoring steal and guest fields for now.
if (off) sscanf(off, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &c_user, &c_nice, &c_sys, cpu_idle, cpu_wait, &c_irq, &c_sirq);
*cpu_user = c_user + c_nice;
*cpu_sys = c_sys + c_irq + c_sirq;
off = strstr(toybuf, "intr");
if (off) sscanf(off, "intr %Lu", sys_irq);
off = strstr(toybuf, "ctxt");
if (off) sscanf(off, "ctxt %Lu", sys_ctxt);
off = strstr(toybuf, "procs_running");
if (off) sscanf(off, "procs_running %u", proc_running);
(*proc_running)--; // look, i'm invisible.
off = strstr(toybuf, "procs_blocked");
if (off) sscanf(off, "procs_blocked %u", proc_blocked);
close(fd);
}
void read_proc_meminfo(unsigned long * mem_swapped, unsigned long * mem_free,
unsigned long * mem_buff, unsigned long * mem_cache)
{
char * off;
unsigned long swap_total, swap_free;
int fd = xopen("/proc/meminfo", O_RDONLY);
if (fdlength(fd) > sizeof(toybuf))
error_exit("/proc/meminfo is too large");
xread(fd, toybuf, sizeof(toybuf));
off = strstr(toybuf, "MemFree");
if (off) sscanf(off, "MemFree: %lu kB", mem_free);
off = strstr(toybuf, "Buffers");
if (off) sscanf(off, "Buffers: %lu kB", mem_buff);
off = strstr(toybuf, "Cached");
if (off) sscanf(off, "Cached: %lu kB", mem_cache);
off = strstr(toybuf, "SwapFree");
if (off) sscanf(off, "SwapFree: %lu kB", &swap_free);
off = strstr(toybuf, "SwapTotal");
if (off) sscanf(off, "SwapTotal: %lu kB", &swap_total);
*mem_swapped = swap_total - swap_free;
close(fd);
}
void read_proc_vmstat(unsigned long * io_pages_in, unsigned long * io_pages_out, unsigned long * swap_bytes_in, unsigned long * swap_bytes_out)
{
char * off;
unsigned long s_pages_in, s_pages_out;
unsigned long pagesize_kb = sysconf(_SC_PAGESIZE) / 1024L;
int fd = xopen("/proc/vmstat", O_RDONLY);
if (fdlength(fd) > sizeof(toybuf))
error_exit("/proc/vmstat is too large");
xread(fd, toybuf, sizeof(toybuf));
off = strstr(toybuf, "pgpgin");
if (off) sscanf(off, "pgpgin %lu", io_pages_in);
off = strstr(toybuf, "pgpgout");
if (off) sscanf(off, "pgpgout %lu", io_pages_out);
off = strstr(toybuf, "pswpin");
if (off) sscanf(off, "pswpin %lu", &s_pages_in);
*swap_bytes_in = s_pages_in * pagesize_kb;
off = strstr(toybuf, "pswpout");
if (off) sscanf(off, "pswpout %lu", &s_pages_out);
*swap_bytes_out = s_pages_out * pagesize_kb;
close(fd);
}
void vmstat_main(void)
{
const char fmt[] = "%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %4u %2u %2u %2u %2u\n";
unsigned int loop_num = 0, loop_max_num = 0, loop_delay = 0;
unsigned int running = 0, blocked = 0;
unsigned long mem_swap = 0, mem_free = 0, mem_buff = 0, mem_cache = 0;
unsigned long io_pages_in[2], io_pages_out[2], swap_bytes_in[2], swap_bytes_out[2];
uint64_t sys_irq[2], sys_ctxt[2], cpu_user[2], cpu_sys[2], cpu_idle[2], cpu_wait[2];
int first_run = 1;
int no_header = toys.optflags & 0x1;
if (toys.optc >= 1)
loop_delay = atoi(toys.optargs[0]);
if (toys.optc >= 2)
loop_max_num = atoi(toys.optargs[1]);
if (loop_max_num < 0 || loop_delay < 0)
error_exit("Invalid arguments");
while(1) {
uint64_t total_jif;
int idx = loop_num%2;
if(first_run || (!(loop_num % 22) && !no_header)) {
printf("procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----\n");
printf(" r b swpd free buff cache si so bi bo in cs us sy id wa\n");
}
read_proc_stat(&running, &blocked, &sys_irq[idx], &sys_ctxt[idx], &cpu_user[idx],
&cpu_sys[idx], &cpu_idle[idx], &cpu_wait[idx]);
read_proc_meminfo(&mem_swap, &mem_free, &mem_buff, &mem_cache);
read_proc_vmstat(&io_pages_in[idx], &io_pages_out[idx], &swap_bytes_in[idx], &swap_bytes_out[idx]);
if (first_run) {
struct sysinfo inf;
sysinfo(&inf);
first_run = 0;
total_jif = cpu_user[idx] + cpu_idle[idx] + cpu_wait[idx];
printf(fmt, running, blocked, mem_swap, mem_free, mem_buff, mem_cache,
(unsigned) (swap_bytes_in[idx]/inf.uptime),
(unsigned) (swap_bytes_out[idx]/inf.uptime),
(unsigned) (io_pages_in[idx]/inf.uptime),
(unsigned) (io_pages_out[idx]/inf.uptime),
(unsigned) (sys_irq[idx]/inf.uptime),
(unsigned) (sys_ctxt[idx]/inf.uptime),
(unsigned) (100*cpu_user[idx]/total_jif),
(unsigned) (100*cpu_sys[idx]/total_jif),
(unsigned) (100*cpu_idle[idx]/total_jif),
(unsigned) (100*cpu_wait[idx]/total_jif));
}else{
total_jif = cpu_user[idx] - cpu_user[!idx] + cpu_idle[idx] - cpu_idle[!idx] + cpu_wait[idx] - cpu_wait[!idx];
printf(fmt, running, blocked, mem_swap, mem_free, mem_buff, mem_cache,
(unsigned) ((swap_bytes_in[idx] - swap_bytes_in[!idx])/loop_delay),
(unsigned) ((swap_bytes_out[idx] - swap_bytes_out[!idx])/loop_delay),
(unsigned) ((io_pages_in[idx] - io_pages_in[!idx])/loop_delay),
(unsigned) ((io_pages_out[idx] - io_pages_out[!idx])/loop_delay),
(unsigned) ((sys_irq[idx] - sys_irq[!idx])/loop_delay),
(unsigned) ((sys_ctxt[idx] - sys_ctxt[!idx])/loop_delay),
(unsigned) (100*(cpu_user[idx] - cpu_user[!idx])/total_jif),
(unsigned) (100*(cpu_sys[idx] - cpu_sys[!idx]) /total_jif),
(unsigned) (100*(cpu_idle[idx] - cpu_idle[!idx])/total_jif),
(unsigned) (100*(cpu_wait[idx] - cpu_wait[!idx])/total_jif));
}
loop_num++;
if (loop_delay == 0 || (loop_max_num != 0 && loop_num >= loop_max_num))
break;
sleep(loop_delay);
}
}
|