blob: 600f1364034cb6e7a300a5cdc160666a3988d7c9 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (c) 2008, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <sys/param.h>
36#include <unistd.h>
37
38struct state {
39 long procs_r;
40 long procs_b;
41
42 long mem_free;
43 long mem_mapped;
44 long mem_anon;
45 long mem_slab;
46
47 long sys_in;
48 long sys_cs;
49 long sys_flt;
50
51 long cpu_us;
52 long cpu_ni;
53 long cpu_sy;
54 long cpu_id;
55 long cpu_wa;
56 long cpu_ir;
57 long cpu_si;
58};
59
60#define MAX_LINE 256
61
62char line[MAX_LINE];
63
64static void read_state(struct state *s);
65static int read_meminfo(struct state *s);
66static int read_stat(struct state *s);
67static int read_vmstat(struct state *s);
68static void print_header(void);
69static void print_line(struct state *old, struct state *new);
70static void usage(char *cmd);
71
72int vmstat_main(int argc, char *argv[]) {
73 struct state s[2];
74 int iterations, delay, header_interval;
75 int toggle, count;
76 int i;
77
78 iterations = 0;
79 delay = 1;
80 header_interval = 20;
81
82 for (i = 1; i < argc; i++) {
83 if (!strcmp(argv[i], "-n")) {
84 if (i >= argc - 1) {
85 fprintf(stderr, "Option -n requires an argument.\n");
86 exit(EXIT_FAILURE);
87 }
88 iterations = atoi(argv[++i]);
89 continue;
90 }
91 if (!strcmp(argv[i], "-d")) {
92 if (i >= argc - 1) {
93 fprintf(stderr, "Option -d requires an argument.\n");
94 exit(EXIT_FAILURE);
95 }
96 delay = atoi(argv[++i]);
97 continue;
98 }
99 if (!strcmp(argv[i], "-r")) {
100 if (i >= argc - 1) {
101 fprintf(stderr, "Option -r requires an argument.\n");
102 exit(EXIT_FAILURE);
103 }
104 header_interval = atoi(argv[++i]);
105 continue;
106 }
107 if (!strcmp(argv[i], "-h")) {
108 usage(argv[0]);
109 exit(EXIT_SUCCESS);
110 }
111 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
112 usage(argv[0]);
113 exit(EXIT_FAILURE);
114 }
115
116 toggle = 0;
117 count = 0;
118
119 if (!header_interval)
120 print_header();
121 read_state(&s[1 - toggle]);
122 while ((iterations == 0) || (iterations-- > 0)) {
123 sleep(delay);
124 read_state(&s[toggle]);
125 if (header_interval) {
126 if (count == 0)
127 print_header();
128 count = (count + 1) % header_interval;
129 }
130 print_line(&s[1 - toggle], &s[toggle]);
131 toggle = 1 - toggle;
132 }
133
134 return 0;
135}
136
137static void read_state(struct state *s) {
138 int error;
139
140 error = read_meminfo(s);
141 if (error) {
142 fprintf(stderr, "vmstat: could not read /proc/meminfo: %s\n", strerror(error));
143 exit(EXIT_FAILURE);
144 }
145
146 error = read_stat(s);
147 if (error) {
148 fprintf(stderr, "vmstat: could not read /proc/stat: %s\n", strerror(error));
149 exit(EXIT_FAILURE);
150 }
151
152 error = read_vmstat(s);
153 if (error) {
154 fprintf(stderr, "vmstat: could not read /proc/vmstat: %s\n", strerror(error));
155 exit(EXIT_FAILURE);
156 }
157}
158
159static int read_meminfo(struct state *s) {
160 FILE *f;
161
162 f = fopen("/proc/meminfo", "r");
163 if (!f) return errno;
164
165 while (fgets(line, MAX_LINE, f)) {
166 sscanf(line, "MemFree: %ld kB", &s->mem_free);
167 sscanf(line, "AnonPages: %ld kB", &s->mem_anon);
168 sscanf(line, "Mapped: %ld kB", &s->mem_mapped);
169 sscanf(line, "Slab: %ld kB", &s->mem_slab);
170 }
171
172 fclose(f);
173
174 return 0;
175}
176
177static int read_stat(struct state *s) {
178 FILE *f;
179
180 f = fopen("/proc/stat", "r");
181 if (!f) return errno;
182
183 while (fgets(line, MAX_LINE, f)) {
184 if (!strncmp(line, "cpu ", 4)) {
185 sscanf(line, "cpu %ld %ld %ld %ld %ld %ld %ld",
186 &s->cpu_us, &s->cpu_ni, &s->cpu_sy, &s->cpu_id, &s->cpu_wa,
187 &s->cpu_ir, &s->cpu_si);
188 }
189 sscanf(line, "intr %ld", &s->sys_in);
190 sscanf(line, "ctxt %ld", &s->sys_cs);
191 sscanf(line, "procs_running %ld", &s->procs_r);
192 sscanf(line, "procs_blocked %ld", &s->procs_b);
193 }
194
195 fclose(f);
196
197 return 0;
198}
199
200static int read_vmstat(struct state *s) {
201 FILE *f;
202
203 f = fopen("/proc/vmstat", "r");
204 if (!f) return errno;
205
206 while (fgets(line, MAX_LINE, f)) {
207 sscanf(line, "pgmajfault %ld", &s->sys_flt);
208 }
209
210 fclose(f);
211
212 return 0;
213}
214
215static void print_header(void) {
216 printf("%-5s %-27s %-14s %-17s\n", "procs", "memory", "system", "cpu");
217 printf("%2s %2s %6s %6s %6s %6s %4s %4s %4s %2s %2s %2s %2s %2s %2s\n", "r", "b", "free", "mapped", "anon", "slab", "in", "cs", "flt", "us", "ni", "sy", "id", "wa", "ir");
218}
219
220/* Jiffies to percent conversion */
221#define JP(jif) ((jif) * 100 / (HZ))
222#define NORM(var) ((var) = (((var) > 99) ? (99) : (var)))
223
224static void print_line(struct state *old, struct state *new) {
225 int us, ni, sy, id, wa, ir;
226 us = JP(new->cpu_us - old->cpu_us); NORM(us);
227 ni = JP(new->cpu_ni - old->cpu_ni); NORM(ni);
228 sy = JP(new->cpu_sy - old->cpu_sy); NORM(sy);
229 id = JP(new->cpu_id - old->cpu_id); NORM(id);
230 wa = JP(new->cpu_wa - old->cpu_wa); NORM(wa);
231 ir = JP(new->cpu_ir - old->cpu_ir); NORM(ir);
232 printf("%2ld %2ld %6ld %6ld %6ld %6ld %4ld %4ld %4ld %2d %2d %2d %2d %2d %2d\n",
233 new->procs_r ? (new->procs_r - 1) : 0, new->procs_b,
234 new->mem_free, new->mem_mapped, new->mem_anon, new->mem_slab,
235 new->sys_in - old->sys_in, new->sys_cs - old->sys_cs, new->sys_flt - old->sys_flt,
236 us, ni, sy, id, wa, ir);
237}
238
239static void usage(char *cmd) {
240 fprintf(stderr, "Usage: %s [ -h ] [ -n iterations ] [ -d delay ] [ -r header_repeat ]\n"
241 " -n iterations How many rows of data to print.\n"
242 " -d delay How long to sleep between rows.\n"
243 " -r header_repeat How many rows to print before repeating\n"
244 " the header. Zero means never repeat.\n"
245 " -h Displays this help screen.\n",
246 cmd);
247}