annotate toys/vmstat.c @ 559:120b3dde7cf4

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