vmstat.c revision 1.47 1 1.47 mrg /* $NetBSD: vmstat.c,v 1.47 1998/07/05 08:02:34 mrg Exp $ */
2 1.45 thorpej
3 1.45 thorpej /*-
4 1.45 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.45 thorpej * All rights reserved.
6 1.45 thorpej *
7 1.45 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.45 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.45 thorpej * NASA Ames Research Center.
10 1.45 thorpej *
11 1.45 thorpej * Redistribution and use in source and binary forms, with or without
12 1.45 thorpej * modification, are permitted provided that the following conditions
13 1.45 thorpej * are met:
14 1.45 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.45 thorpej * notice, this list of conditions and the following disclaimer.
16 1.45 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.45 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.45 thorpej * documentation and/or other materials provided with the distribution.
19 1.45 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.45 thorpej * must display the following acknowledgement:
21 1.45 thorpej * This product includes software developed by the NetBSD
22 1.45 thorpej * Foundation, Inc. and its contributors.
23 1.45 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.45 thorpej * contributors may be used to endorse or promote products derived
25 1.45 thorpej * from this software without specific prior written permission.
26 1.45 thorpej *
27 1.45 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.45 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.45 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.45 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.45 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.45 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.45 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.45 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.45 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.45 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.45 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.45 thorpej */
39 1.21 cgd
40 1.1 cgd /*
41 1.13 cgd * Copyright (c) 1980, 1986, 1991, 1993
42 1.13 cgd * The Regents of the University of California. All rights reserved.
43 1.1 cgd *
44 1.1 cgd * Redistribution and use in source and binary forms, with or without
45 1.1 cgd * modification, are permitted provided that the following conditions
46 1.1 cgd * are met:
47 1.1 cgd * 1. Redistributions of source code must retain the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer.
49 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer in the
51 1.1 cgd * documentation and/or other materials provided with the distribution.
52 1.1 cgd * 3. All advertising materials mentioning features or use of this software
53 1.1 cgd * must display the following acknowledgement:
54 1.1 cgd * This product includes software developed by the University of
55 1.1 cgd * California, Berkeley and its contributors.
56 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
57 1.1 cgd * may be used to endorse or promote products derived from this software
58 1.1 cgd * without specific prior written permission.
59 1.1 cgd *
60 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 cgd * SUCH DAMAGE.
71 1.1 cgd */
72 1.1 cgd
73 1.38 mrg #include <sys/cdefs.h>
74 1.1 cgd #ifndef lint
75 1.38 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
76 1.38 mrg The Regents of the University of California. All rights reserved.\n");
77 1.1 cgd #endif /* not lint */
78 1.1 cgd
79 1.1 cgd #ifndef lint
80 1.21 cgd #if 0
81 1.37 mrg static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
82 1.21 cgd #else
83 1.47 mrg __RCSID("$NetBSD: vmstat.c,v 1.47 1998/07/05 08:02:34 mrg Exp $");
84 1.21 cgd #endif
85 1.1 cgd #endif /* not lint */
86 1.1 cgd
87 1.1 cgd #include <sys/param.h>
88 1.1 cgd #include <sys/time.h>
89 1.1 cgd #include <sys/proc.h>
90 1.1 cgd #include <sys/user.h>
91 1.1 cgd #include <sys/dkstat.h>
92 1.1 cgd #include <sys/buf.h>
93 1.1 cgd #include <sys/namei.h>
94 1.1 cgd #include <sys/malloc.h>
95 1.1 cgd #include <sys/fcntl.h>
96 1.1 cgd #include <sys/ioctl.h>
97 1.13 cgd #include <sys/sysctl.h>
98 1.18 pk #include <sys/device.h>
99 1.13 cgd #include <vm/vm.h>
100 1.45 thorpej #include <err.h>
101 1.1 cgd #include <time.h>
102 1.1 cgd #include <nlist.h>
103 1.1 cgd #include <kvm.h>
104 1.1 cgd #include <errno.h>
105 1.1 cgd #include <unistd.h>
106 1.22 jtc #include <signal.h>
107 1.1 cgd #include <stdio.h>
108 1.1 cgd #include <ctype.h>
109 1.1 cgd #include <stdlib.h>
110 1.1 cgd #include <string.h>
111 1.1 cgd #include <paths.h>
112 1.13 cgd #include <limits.h>
113 1.29 thorpej #include "dkstats.h"
114 1.1 cgd
115 1.45 thorpej #if defined(UVM)
116 1.45 thorpej #include <uvm/uvm_stat.h>
117 1.45 thorpej #endif
118 1.45 thorpej
119 1.13 cgd struct nlist namelist[] = {
120 1.1 cgd #define X_CPTIME 0
121 1.1 cgd { "_cp_time" },
122 1.41 mrg #define X_BOOTTIME 1
123 1.1 cgd { "_boottime" },
124 1.41 mrg #define X_HZ 2
125 1.1 cgd { "_hz" },
126 1.41 mrg #define X_STATHZ 3
127 1.13 cgd { "_stathz" },
128 1.41 mrg #define X_NCHSTATS 4
129 1.1 cgd { "_nchstats" },
130 1.41 mrg #define X_INTRNAMES 5
131 1.1 cgd { "_intrnames" },
132 1.41 mrg #define X_EINTRNAMES 6
133 1.1 cgd { "_eintrnames" },
134 1.41 mrg #define X_INTRCNT 7
135 1.1 cgd { "_intrcnt" },
136 1.41 mrg #define X_EINTRCNT 8
137 1.1 cgd { "_eintrcnt" },
138 1.41 mrg #define X_KMEMSTAT 9
139 1.1 cgd { "_kmemstats" },
140 1.41 mrg #define X_KMEMBUCKETS 10
141 1.1 cgd { "_bucket" },
142 1.41 mrg #define X_ALLEVENTS 11
143 1.18 pk { "_allevents" },
144 1.41 mrg #if defined(UVM)
145 1.41 mrg #define X_END 12
146 1.1 cgd #else
147 1.41 mrg #define X_SUM 12
148 1.41 mrg { "_cnt" },
149 1.31 mycroft #define X_END 13
150 1.1 cgd #endif
151 1.23 phil #if defined(pc532)
152 1.23 phil #define X_IVT (X_END)
153 1.23 phil { "_ivt" },
154 1.23 phil #endif
155 1.1 cgd { "" },
156 1.1 cgd };
157 1.1 cgd
158 1.29 thorpej /* Objects defined in dkstats.c */
159 1.29 thorpej extern struct _disk cur;
160 1.29 thorpej extern char **dr_name;
161 1.29 thorpej extern int *dk_select, dk_ndrive;
162 1.1 cgd
163 1.41 mrg #if defined(UVM)
164 1.41 mrg struct uvmexp uvmexp, ouvmexp;
165 1.41 mrg #else
166 1.13 cgd struct vmmeter sum, osum;
167 1.41 mrg #endif
168 1.29 thorpej int ndrives;
169 1.1 cgd
170 1.1 cgd int winlines = 20;
171 1.1 cgd
172 1.13 cgd kvm_t *kd;
173 1.13 cgd
174 1.1 cgd #define FORKSTAT 0x01
175 1.1 cgd #define INTRSTAT 0x02
176 1.1 cgd #define MEMSTAT 0x04
177 1.1 cgd #define SUMSTAT 0x08
178 1.1 cgd #define VMSTAT 0x20
179 1.45 thorpej #if defined(UVM)
180 1.45 thorpej #define HISTLIST 0x40
181 1.45 thorpej #define HISTDUMP 0x80
182 1.45 thorpej #endif
183 1.1 cgd
184 1.29 thorpej void cpustats __P((void));
185 1.29 thorpej void dkstats __P((void));
186 1.29 thorpej void dointr __P((void));
187 1.29 thorpej void domem __P((void));
188 1.29 thorpej void dosum __P((void));
189 1.29 thorpej void dovmstat __P((u_int, int));
190 1.29 thorpej void kread __P((int, void *, size_t));
191 1.38 mrg void needhdr __P((int));
192 1.38 mrg long getuptime __P((void));
193 1.38 mrg void printhdr __P((void));
194 1.38 mrg long pct __P((long, long));
195 1.29 thorpej void usage __P((void));
196 1.40 thorpej void doforkst __P((void));
197 1.1 cgd
198 1.45 thorpej #if defined(UVM)
199 1.45 thorpej void hist_traverse __P((int, const char *));
200 1.45 thorpej void hist_dodump __P((struct uvm_history *));
201 1.45 thorpej #endif
202 1.45 thorpej
203 1.38 mrg int main __P((int, char **));
204 1.29 thorpej char **choosedrives __P((char **));
205 1.29 thorpej
206 1.38 mrg extern int dkinit __P((int));
207 1.38 mrg extern void dkreadstats __P((void));
208 1.38 mrg extern void dkswap __P((void));
209 1.38 mrg
210 1.29 thorpej /* Namelist and memory file names. */
211 1.29 thorpej char *nlistf, *memf;
212 1.29 thorpej
213 1.47 mrg /* allow old usage [vmstat 1] */
214 1.47 mrg #define BACKWARD_COMPATIBILITY
215 1.47 mrg
216 1.38 mrg int
217 1.1 cgd main(argc, argv)
218 1.38 mrg int argc;
219 1.38 mrg char **argv;
220 1.1 cgd {
221 1.1 cgd extern int optind;
222 1.1 cgd extern char *optarg;
223 1.38 mrg int c, todo;
224 1.1 cgd u_int interval;
225 1.1 cgd int reps;
226 1.13 cgd char errbuf[_POSIX2_LINE_MAX];
227 1.45 thorpej #if defined(UVM)
228 1.45 thorpej const char *histname = NULL;
229 1.45 thorpej #endif
230 1.1 cgd
231 1.13 cgd memf = nlistf = NULL;
232 1.1 cgd interval = reps = todo = 0;
233 1.45 thorpej #if defined(UVM)
234 1.45 thorpej while ((c = getopt(argc, argv, "c:fh:HilM:mN:stw:")) != -1) {
235 1.45 thorpej #else
236 1.39 lukem while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != -1) {
237 1.45 thorpej #endif
238 1.1 cgd switch (c) {
239 1.1 cgd case 'c':
240 1.1 cgd reps = atoi(optarg);
241 1.1 cgd break;
242 1.1 cgd case 'f':
243 1.1 cgd todo |= FORKSTAT;
244 1.1 cgd break;
245 1.45 thorpej #if defined(UVM)
246 1.45 thorpej case 'h':
247 1.45 thorpej histname = optarg;
248 1.45 thorpej /* FALLTHROUGH */
249 1.45 thorpej case 'H':
250 1.45 thorpej todo |= HISTDUMP;
251 1.45 thorpej break;
252 1.45 thorpej #endif
253 1.1 cgd case 'i':
254 1.1 cgd todo |= INTRSTAT;
255 1.1 cgd break;
256 1.45 thorpej #if defined(UVM)
257 1.45 thorpej case 'l':
258 1.45 thorpej todo |= HISTLIST;
259 1.45 thorpej break;
260 1.45 thorpej #endif
261 1.1 cgd case 'M':
262 1.13 cgd memf = optarg;
263 1.1 cgd break;
264 1.1 cgd case 'm':
265 1.1 cgd todo |= MEMSTAT;
266 1.1 cgd break;
267 1.1 cgd case 'N':
268 1.13 cgd nlistf = optarg;
269 1.1 cgd break;
270 1.1 cgd case 's':
271 1.1 cgd todo |= SUMSTAT;
272 1.1 cgd break;
273 1.1 cgd case 'w':
274 1.1 cgd interval = atoi(optarg);
275 1.1 cgd break;
276 1.1 cgd case '?':
277 1.1 cgd default:
278 1.1 cgd usage();
279 1.1 cgd }
280 1.1 cgd }
281 1.1 cgd argc -= optind;
282 1.1 cgd argv += optind;
283 1.1 cgd
284 1.1 cgd if (todo == 0)
285 1.1 cgd todo = VMSTAT;
286 1.1 cgd
287 1.13 cgd /*
288 1.13 cgd * Discard setgid privileges if not the running kernel so that bad
289 1.13 cgd * guys can't print interesting stuff from kernel memory.
290 1.13 cgd */
291 1.13 cgd if (nlistf != NULL || memf != NULL)
292 1.13 cgd setgid(getgid());
293 1.13 cgd
294 1.13 cgd kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
295 1.13 cgd if (kd == 0) {
296 1.1 cgd (void)fprintf(stderr,
297 1.13 cgd "vmstat: kvm_openfiles: %s\n", errbuf);
298 1.1 cgd exit(1);
299 1.1 cgd }
300 1.1 cgd
301 1.13 cgd if ((c = kvm_nlist(kd, namelist)) != 0) {
302 1.1 cgd if (c > 0) {
303 1.1 cgd (void)fprintf(stderr,
304 1.13 cgd "vmstat: undefined symbols:");
305 1.13 cgd for (c = 0;
306 1.13 cgd c < sizeof(namelist)/sizeof(namelist[0]); c++)
307 1.13 cgd if (namelist[c].n_type == 0)
308 1.13 cgd fprintf(stderr, " %s",
309 1.13 cgd namelist[c].n_name);
310 1.1 cgd (void)fputc('\n', stderr);
311 1.1 cgd } else
312 1.1 cgd (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
313 1.13 cgd kvm_geterr(kd));
314 1.1 cgd exit(1);
315 1.1 cgd }
316 1.1 cgd
317 1.1 cgd if (todo & VMSTAT) {
318 1.1 cgd struct winsize winsize;
319 1.1 cgd
320 1.29 thorpej dkinit(0); /* Initialize disk stats, no disks selected. */
321 1.29 thorpej argv = choosedrives(argv); /* Select disks. */
322 1.1 cgd winsize.ws_row = 0;
323 1.47 mrg (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
324 1.1 cgd if (winsize.ws_row > 0)
325 1.1 cgd winlines = winsize.ws_row;
326 1.1 cgd
327 1.1 cgd }
328 1.1 cgd
329 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
330 1.1 cgd if (*argv) {
331 1.1 cgd interval = atoi(*argv);
332 1.1 cgd if (*++argv)
333 1.1 cgd reps = atoi(*argv);
334 1.1 cgd }
335 1.1 cgd #endif
336 1.1 cgd
337 1.1 cgd if (interval) {
338 1.1 cgd if (!reps)
339 1.1 cgd reps = -1;
340 1.1 cgd } else if (reps)
341 1.1 cgd interval = 1;
342 1.1 cgd
343 1.45 thorpej #if defined(UVM)
344 1.45 thorpej if (todo & (HISTLIST|HISTDUMP)) {
345 1.45 thorpej if ((todo & (HISTLIST|HISTDUMP)) == (HISTLIST|HISTDUMP))
346 1.45 thorpej errx(1, "you may list or dump, but not both!");
347 1.45 thorpej hist_traverse(todo, histname);
348 1.45 thorpej }
349 1.45 thorpej #endif
350 1.1 cgd if (todo & FORKSTAT)
351 1.1 cgd doforkst();
352 1.1 cgd if (todo & MEMSTAT)
353 1.1 cgd domem();
354 1.1 cgd if (todo & SUMSTAT)
355 1.13 cgd dosum();
356 1.1 cgd if (todo & INTRSTAT)
357 1.1 cgd dointr();
358 1.1 cgd if (todo & VMSTAT)
359 1.1 cgd dovmstat(interval, reps);
360 1.1 cgd exit(0);
361 1.1 cgd }
362 1.1 cgd
363 1.1 cgd char **
364 1.29 thorpej choosedrives(argv)
365 1.1 cgd char **argv;
366 1.1 cgd {
367 1.38 mrg int i;
368 1.1 cgd
369 1.1 cgd /*
370 1.1 cgd * Choose drives to be displayed. Priority goes to (in order) drives
371 1.1 cgd * supplied as arguments, default drives. If everything isn't filled
372 1.1 cgd * in and there are drives not taken care of, display the first few
373 1.1 cgd * that fit.
374 1.1 cgd */
375 1.1 cgd #define BACKWARD_COMPATIBILITY
376 1.1 cgd for (ndrives = 0; *argv; ++argv) {
377 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
378 1.1 cgd if (isdigit(**argv))
379 1.1 cgd break;
380 1.1 cgd #endif
381 1.1 cgd for (i = 0; i < dk_ndrive; i++) {
382 1.1 cgd if (strcmp(dr_name[i], *argv))
383 1.1 cgd continue;
384 1.29 thorpej dk_select[i] = 1;
385 1.1 cgd ++ndrives;
386 1.1 cgd break;
387 1.1 cgd }
388 1.1 cgd }
389 1.1 cgd for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
390 1.29 thorpej if (dk_select[i])
391 1.1 cgd continue;
392 1.29 thorpej dk_select[i] = 1;
393 1.1 cgd ++ndrives;
394 1.1 cgd }
395 1.1 cgd return(argv);
396 1.1 cgd }
397 1.1 cgd
398 1.1 cgd long
399 1.1 cgd getuptime()
400 1.1 cgd {
401 1.30 cgd static time_t now;
402 1.30 cgd static struct timeval boottime;
403 1.1 cgd time_t uptime;
404 1.1 cgd
405 1.30 cgd if (boottime.tv_sec == 0)
406 1.1 cgd kread(X_BOOTTIME, &boottime, sizeof(boottime));
407 1.1 cgd (void)time(&now);
408 1.30 cgd uptime = now - boottime.tv_sec;
409 1.1 cgd if (uptime <= 0 || uptime > 60*60*24*365*10) {
410 1.1 cgd (void)fprintf(stderr,
411 1.1 cgd "vmstat: time makes no sense; namelist must be wrong.\n");
412 1.1 cgd exit(1);
413 1.1 cgd }
414 1.1 cgd return(uptime);
415 1.1 cgd }
416 1.1 cgd
417 1.1 cgd int hz, hdrcnt;
418 1.1 cgd
419 1.1 cgd void
420 1.1 cgd dovmstat(interval, reps)
421 1.1 cgd u_int interval;
422 1.1 cgd int reps;
423 1.1 cgd {
424 1.1 cgd struct vmtotal total;
425 1.1 cgd time_t uptime, halfuptime;
426 1.17 cgd int mib[2];
427 1.17 cgd size_t size;
428 1.41 mrg int pagesize = getpagesize();
429 1.1 cgd
430 1.1 cgd uptime = getuptime();
431 1.1 cgd halfuptime = uptime / 2;
432 1.1 cgd (void)signal(SIGCONT, needhdr);
433 1.1 cgd
434 1.13 cgd if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
435 1.13 cgd kread(X_STATHZ, &hz, sizeof(hz));
436 1.1 cgd if (!hz)
437 1.1 cgd kread(X_HZ, &hz, sizeof(hz));
438 1.1 cgd
439 1.1 cgd for (hdrcnt = 1;;) {
440 1.1 cgd if (!--hdrcnt)
441 1.1 cgd printhdr();
442 1.29 thorpej /* Read new disk statistics */
443 1.29 thorpej dkreadstats();
444 1.41 mrg #if defined(UVM)
445 1.41 mrg size = sizeof(uvmexp);
446 1.41 mrg mib[0] = CTL_VM;
447 1.41 mrg mib[1] = VM_UVMEXP;
448 1.41 mrg if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
449 1.41 mrg printf("can't get uvmexp: %s\n", strerror(errno));
450 1.41 mrg bzero(&uvmexp, sizeof(uvmexp));
451 1.41 mrg }
452 1.41 mrg #else
453 1.13 cgd kread(X_SUM, &sum, sizeof(sum));
454 1.41 mrg #endif
455 1.13 cgd size = sizeof(total);
456 1.13 cgd mib[0] = CTL_VM;
457 1.13 cgd mib[1] = VM_METER;
458 1.13 cgd if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
459 1.13 cgd printf("Can't get kerninfo: %s\n", strerror(errno));
460 1.13 cgd bzero(&total, sizeof(total));
461 1.13 cgd }
462 1.13 cgd (void)printf("%2d%2d%2d",
463 1.13 cgd total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
464 1.41 mrg #define pgtok(a) (long)((a) * (pagesize >> 10))
465 1.38 mrg #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
466 1.13 cgd (void)printf("%6ld%6ld ",
467 1.1 cgd pgtok(total.t_avm), pgtok(total.t_free));
468 1.41 mrg #if defined(UVM)
469 1.42 mrg (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
470 1.42 mrg (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
471 1.46 mrg (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
472 1.44 mrg (void)printf("%4lu ",
473 1.44 mrg rate(uvmexp.pgswapout - ouvmexp.pgswapout));
474 1.44 mrg (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
475 1.44 mrg (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
476 1.42 mrg dkstats();
477 1.42 mrg (void)printf("%4lu %4lu %3lu ",
478 1.42 mrg rate(uvmexp.intrs - ouvmexp.intrs),
479 1.42 mrg rate(uvmexp.syscalls - ouvmexp.syscalls),
480 1.42 mrg rate(uvmexp.swtch - ouvmexp.swtch));
481 1.42 mrg cpustats();
482 1.42 mrg (void)printf("\n");
483 1.42 mrg (void)fflush(stdout);
484 1.42 mrg if (reps >= 0 && --reps <= 0)
485 1.42 mrg break;
486 1.42 mrg ouvmexp = uvmexp;
487 1.41 mrg #else
488 1.13 cgd (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
489 1.1 cgd (void)printf("%3lu ",
490 1.13 cgd rate(sum.v_reactivated - osum.v_reactivated));
491 1.13 cgd (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
492 1.1 cgd (void)printf("%3lu %3lu ",
493 1.38 mrg rate(sum.v_pageouts - osum.v_pageouts), (u_long)0);
494 1.13 cgd (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
495 1.1 cgd dkstats();
496 1.1 cgd (void)printf("%4lu %4lu %3lu ",
497 1.13 cgd rate(sum.v_intr - osum.v_intr),
498 1.13 cgd rate(sum.v_syscall - osum.v_syscall),
499 1.13 cgd rate(sum.v_swtch - osum.v_swtch));
500 1.1 cgd cpustats();
501 1.1 cgd (void)printf("\n");
502 1.1 cgd (void)fflush(stdout);
503 1.1 cgd if (reps >= 0 && --reps <= 0)
504 1.1 cgd break;
505 1.13 cgd osum = sum;
506 1.41 mrg #endif
507 1.1 cgd uptime = interval;
508 1.1 cgd /*
509 1.1 cgd * We round upward to avoid losing low-frequency events
510 1.1 cgd * (i.e., >= 1 per interval but < 1 per second).
511 1.1 cgd */
512 1.33 thorpej halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
513 1.1 cgd (void)sleep(interval);
514 1.1 cgd }
515 1.1 cgd }
516 1.1 cgd
517 1.38 mrg void
518 1.1 cgd printhdr()
519 1.1 cgd {
520 1.38 mrg int i;
521 1.1 cgd
522 1.44 mrg #if defined(UVM)
523 1.44 mrg (void)printf(" procs memory page%*s", 23, "");
524 1.44 mrg #else
525 1.1 cgd (void)printf(" procs memory page%*s", 20, "");
526 1.44 mrg #endif
527 1.29 thorpej if (ndrives > 0)
528 1.29 thorpej (void)printf("%s %*sfaults cpu\n",
529 1.29 thorpej ((ndrives > 1) ? "disks" : "disk"),
530 1.29 thorpej ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
531 1.1 cgd else
532 1.29 thorpej (void)printf("%*s faults cpu\n",
533 1.29 thorpej ndrives * 3, "");
534 1.29 thorpej
535 1.44 mrg #if defined(UVM)
536 1.44 mrg (void)printf(" r b w avm fre flt re pi po fr sr ");
537 1.44 mrg #else
538 1.1 cgd (void)printf(" r b w avm fre flt re pi po fr sr ");
539 1.44 mrg #endif
540 1.1 cgd for (i = 0; i < dk_ndrive; i++)
541 1.29 thorpej if (dk_select[i])
542 1.1 cgd (void)printf("%c%c ", dr_name[i][0],
543 1.1 cgd dr_name[i][strlen(dr_name[i]) - 1]);
544 1.1 cgd (void)printf(" in sy cs us sy id\n");
545 1.1 cgd hdrcnt = winlines - 2;
546 1.1 cgd }
547 1.1 cgd
548 1.1 cgd /*
549 1.1 cgd * Force a header to be prepended to the next output.
550 1.1 cgd */
551 1.1 cgd void
552 1.38 mrg needhdr(dummy)
553 1.38 mrg int dummy;
554 1.1 cgd {
555 1.1 cgd
556 1.1 cgd hdrcnt = 1;
557 1.1 cgd }
558 1.1 cgd
559 1.38 mrg long
560 1.1 cgd pct(top, bot)
561 1.1 cgd long top, bot;
562 1.1 cgd {
563 1.13 cgd long ans;
564 1.13 cgd
565 1.1 cgd if (bot == 0)
566 1.1 cgd return(0);
567 1.13 cgd ans = (quad_t)top * 100 / bot;
568 1.13 cgd return (ans);
569 1.1 cgd }
570 1.1 cgd
571 1.38 mrg #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
572 1.1 cgd
573 1.1 cgd void
574 1.13 cgd dosum()
575 1.1 cgd {
576 1.1 cgd struct nchstats nchstats;
577 1.1 cgd long nchtotal;
578 1.1 cgd
579 1.41 mrg #if defined(UVM)
580 1.41 mrg int mib[2];
581 1.41 mrg size_t size;
582 1.41 mrg
583 1.41 mrg size = sizeof(uvmexp);
584 1.41 mrg mib[0] = CTL_VM;
585 1.41 mrg mib[1] = VM_UVMEXP;
586 1.41 mrg if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
587 1.41 mrg printf("can't get uvmexp: %s\n", strerror(errno));
588 1.41 mrg bzero(&uvmexp, sizeof(uvmexp));
589 1.41 mrg }
590 1.41 mrg
591 1.44 mrg (void)printf("%9u bytes per page\n", uvmexp.pagesize);
592 1.44 mrg
593 1.44 mrg (void)printf("%9u pages managed\n", uvmexp.npages);
594 1.44 mrg (void)printf("%9u pages free\n", uvmexp.free);
595 1.44 mrg (void)printf("%9u pages active\n", uvmexp.active);
596 1.44 mrg (void)printf("%9u pages inactive\n", uvmexp.inactive);
597 1.44 mrg (void)printf("%9u pages paging\n", uvmexp.paging);
598 1.44 mrg (void)printf("%9u pages wired\n", uvmexp.wired);
599 1.44 mrg (void)printf("%9u reserve pagedaemon pages\n",
600 1.44 mrg uvmexp.reserve_pagedaemon);
601 1.44 mrg (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
602 1.44 mrg
603 1.44 mrg (void)printf("%9u minimum free pages\n", uvmexp.freemin);
604 1.44 mrg (void)printf("%9u target free pages\n", uvmexp.freetarg);
605 1.44 mrg (void)printf("%9u target inactive pages\n", uvmexp.inactarg);
606 1.44 mrg (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
607 1.44 mrg
608 1.44 mrg (void)printf("%9u swap devices\n", uvmexp.nswapdev);
609 1.44 mrg (void)printf("%9u swap pages\n", uvmexp.swpages);
610 1.44 mrg (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
611 1.44 mrg (void)printf("%9u swap allocations\n", uvmexp.nswget);
612 1.44 mrg (void)printf("%9u anons\n", uvmexp.nanon);
613 1.44 mrg (void)printf("%9u free anons\n", uvmexp.nfreeanon);
614 1.44 mrg
615 1.43 mrg (void)printf("%9u total faults taken\n", uvmexp.faults);
616 1.43 mrg (void)printf("%9u traps\n", uvmexp.traps);
617 1.43 mrg (void)printf("%9u device interrupts\n", uvmexp.intrs);
618 1.43 mrg (void)printf("%9u cpu context switches\n", uvmexp.swtch);
619 1.43 mrg (void)printf("%9u software interrupts\n", uvmexp.softs);
620 1.43 mrg (void)printf("%9u system calls\n", uvmexp.syscalls);
621 1.44 mrg (void)printf("%9u pagein requests\n", uvmexp.pageins / CLSIZE);
622 1.44 mrg (void)printf("%9u pageout requests\n", uvmexp.pdpageouts / CLSIZE);
623 1.43 mrg (void)printf("%9u swap ins\n", uvmexp.swapins);
624 1.43 mrg (void)printf("%9u swap outs\n", uvmexp.swapouts);
625 1.44 mrg (void)printf("%9u pages swapped in\n", uvmexp.pgswapin / CLSIZE);
626 1.44 mrg (void)printf("%9u pages swapped out\n", uvmexp.pgswapout / CLSIZE);
627 1.43 mrg (void)printf("%9u forks total\n", uvmexp.forks);
628 1.43 mrg (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
629 1.43 mrg (void)printf("%9u forks shared address space with parent\n",
630 1.43 mrg uvmexp.forks_sharevm);
631 1.44 mrg
632 1.44 mrg (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
633 1.44 mrg (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
634 1.43 mrg (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
635 1.43 mrg (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
636 1.43 mrg (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
637 1.43 mrg uvmexp.fltrelckok);
638 1.43 mrg (void)printf("%9u anon page faults\n", uvmexp.fltanget);
639 1.43 mrg (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
640 1.43 mrg (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
641 1.43 mrg (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
642 1.43 mrg (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
643 1.43 mrg (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
644 1.43 mrg (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
645 1.43 mrg (void)printf("%9u anon faults\n", uvmexp.flt_anon);
646 1.43 mrg (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
647 1.43 mrg (void)printf("%9u object faults\n", uvmexp.flt_obj);
648 1.43 mrg (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
649 1.43 mrg (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
650 1.44 mrg
651 1.44 mrg (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
652 1.44 mrg (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
653 1.44 mrg (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
654 1.44 mrg (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
655 1.44 mrg (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
656 1.44 mrg (void)printf("%9u anonymous pages scanned by daemon\n", uvmexp.pdanscan);
657 1.44 mrg (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
658 1.44 mrg (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
659 1.44 mrg (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
660 1.44 mrg (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
661 1.44 mrg (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
662 1.41 mrg #else
663 1.13 cgd kread(X_SUM, &sum, sizeof(sum));
664 1.13 cgd (void)printf("%9u cpu context switches\n", sum.v_swtch);
665 1.13 cgd (void)printf("%9u device interrupts\n", sum.v_intr);
666 1.13 cgd (void)printf("%9u software interrupts\n", sum.v_soft);
667 1.13 cgd (void)printf("%9u traps\n", sum.v_trap);
668 1.13 cgd (void)printf("%9u system calls\n", sum.v_syscall);
669 1.13 cgd (void)printf("%9u total faults taken\n", sum.v_faults);
670 1.13 cgd (void)printf("%9u swap ins\n", sum.v_swpin);
671 1.13 cgd (void)printf("%9u swap outs\n", sum.v_swpout);
672 1.13 cgd (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
673 1.13 cgd (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
674 1.13 cgd (void)printf("%9u page ins\n", sum.v_pageins);
675 1.13 cgd (void)printf("%9u page outs\n", sum.v_pageouts);
676 1.13 cgd (void)printf("%9u pages paged in\n", sum.v_pgpgin);
677 1.13 cgd (void)printf("%9u pages paged out\n", sum.v_pgpgout);
678 1.13 cgd (void)printf("%9u pages reactivated\n", sum.v_reactivated);
679 1.13 cgd (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
680 1.13 cgd (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
681 1.13 cgd (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
682 1.13 cgd (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
683 1.13 cgd (void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
684 1.13 cgd (void)printf("%9u VM object cache lookups\n", sum.v_lookups);
685 1.13 cgd (void)printf("%9u VM object hits\n", sum.v_hits);
686 1.13 cgd (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
687 1.13 cgd (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
688 1.13 cgd (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
689 1.13 cgd (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
690 1.13 cgd (void)printf("%9u pages free\n", sum.v_free_count);
691 1.13 cgd (void)printf("%9u pages wired down\n", sum.v_wire_count);
692 1.13 cgd (void)printf("%9u pages active\n", sum.v_active_count);
693 1.13 cgd (void)printf("%9u pages inactive\n", sum.v_inactive_count);
694 1.13 cgd (void)printf("%9u bytes per page\n", sum.v_page_size);
695 1.37 mrg (void)printf("%9u target inactive pages\n", sum.v_inactive_target);
696 1.37 mrg (void)printf("%9u target free pages\n", sum.v_free_target);
697 1.37 mrg (void)printf("%9u minimum free pages\n", sum.v_free_min);
698 1.1 cgd #endif
699 1.1 cgd kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
700 1.1 cgd nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
701 1.1 cgd nchstats.ncs_badhits + nchstats.ncs_falsehits +
702 1.1 cgd nchstats.ncs_miss + nchstats.ncs_long;
703 1.1 cgd (void)printf("%9ld total name lookups\n", nchtotal);
704 1.1 cgd (void)printf(
705 1.1 cgd "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
706 1.1 cgd "", PCT(nchstats.ncs_goodhits, nchtotal),
707 1.1 cgd PCT(nchstats.ncs_neghits, nchtotal),
708 1.1 cgd PCT(nchstats.ncs_pass2, nchtotal));
709 1.1 cgd (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
710 1.1 cgd PCT(nchstats.ncs_badhits, nchtotal),
711 1.1 cgd PCT(nchstats.ncs_falsehits, nchtotal),
712 1.1 cgd PCT(nchstats.ncs_long, nchtotal));
713 1.1 cgd }
714 1.1 cgd
715 1.1 cgd void
716 1.1 cgd doforkst()
717 1.1 cgd {
718 1.41 mrg #if defined(UVM)
719 1.41 mrg int mib[2];
720 1.41 mrg size_t size;
721 1.41 mrg
722 1.41 mrg size = sizeof(uvmexp);
723 1.41 mrg mib[0] = CTL_VM;
724 1.41 mrg mib[1] = VM_UVMEXP;
725 1.41 mrg if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
726 1.41 mrg printf("can't get uvmexp: %s\n", strerror(errno));
727 1.41 mrg bzero(&uvmexp, sizeof(uvmexp));
728 1.41 mrg }
729 1.41 mrg (void)printf("%u forks total\n", uvmexp.forks);
730 1.41 mrg (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
731 1.41 mrg (void)printf("%u forks shared address space with parent\n",
732 1.41 mrg uvmexp.forks_sharevm);
733 1.41 mrg #else
734 1.1 cgd
735 1.40 thorpej kread(X_SUM, &sum, sizeof(sum));
736 1.40 thorpej (void)printf("%u forks total\n", sum.v_forks);
737 1.40 thorpej (void)printf("%u forks blocked parent\n", sum.v_forks_ppwait);
738 1.40 thorpej (void)printf("%u forks shared address space with parent\n",
739 1.40 thorpej sum.v_forks_sharevm);
740 1.41 mrg #endif
741 1.1 cgd }
742 1.1 cgd
743 1.1 cgd void
744 1.1 cgd dkstats()
745 1.1 cgd {
746 1.38 mrg int dn, state;
747 1.1 cgd double etime;
748 1.1 cgd
749 1.29 thorpej /* Calculate disk stat deltas. */
750 1.29 thorpej dkswap();
751 1.1 cgd etime = 0;
752 1.1 cgd for (state = 0; state < CPUSTATES; ++state) {
753 1.29 thorpej etime += cur.cp_time[state];
754 1.1 cgd }
755 1.1 cgd if (etime == 0)
756 1.1 cgd etime = 1;
757 1.1 cgd etime /= hz;
758 1.1 cgd for (dn = 0; dn < dk_ndrive; ++dn) {
759 1.29 thorpej if (!dk_select[dn])
760 1.1 cgd continue;
761 1.29 thorpej (void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
762 1.1 cgd }
763 1.1 cgd }
764 1.1 cgd
765 1.1 cgd void
766 1.1 cgd cpustats()
767 1.1 cgd {
768 1.38 mrg int state;
769 1.1 cgd double pct, total;
770 1.1 cgd
771 1.1 cgd total = 0;
772 1.1 cgd for (state = 0; state < CPUSTATES; ++state)
773 1.29 thorpej total += cur.cp_time[state];
774 1.1 cgd if (total)
775 1.1 cgd pct = 100 / total;
776 1.1 cgd else
777 1.1 cgd pct = 0;
778 1.29 thorpej (void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
779 1.29 thorpej (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
780 1.29 thorpej (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
781 1.1 cgd }
782 1.1 cgd
783 1.23 phil #if defined(pc532)
784 1.24 phil /* To get struct iv ...*/
785 1.24 phil #define _KERNEL
786 1.23 phil #include <machine/psl.h>
787 1.24 phil #undef _KERNEL
788 1.23 phil void
789 1.23 phil dointr()
790 1.23 phil {
791 1.38 mrg long i, j, inttotal, uptime;
792 1.23 phil static char iname[64];
793 1.23 phil struct iv ivt[32], *ivp = ivt;
794 1.23 phil
795 1.23 phil iname[63] = '\0';
796 1.23 phil uptime = getuptime();
797 1.23 phil kread(X_IVT, ivp, sizeof(ivt));
798 1.23 phil
799 1.23 phil for (i = 0; i < 2; i++) {
800 1.23 phil (void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
801 1.23 phil (void)printf("interrupt total rate\n");
802 1.23 phil inttotal = 0;
803 1.23 phil for (j = 0; j < 16; j++, ivp++) {
804 1.23 phil if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
805 1.23 phil if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
806 1.23 phil (void)fprintf(stderr, "vmstat: iv_use: %s\n",
807 1.23 phil kvm_geterr(kd));
808 1.23 phil exit(1);
809 1.23 phil }
810 1.23 phil (void)printf("%-12s %8ld %8ld\n", iname,
811 1.23 phil ivp->iv_cnt, ivp->iv_cnt / uptime);
812 1.23 phil inttotal += ivp->iv_cnt;
813 1.23 phil }
814 1.23 phil }
815 1.23 phil (void)printf("Total %8ld %8ld\n",
816 1.23 phil inttotal, inttotal / uptime);
817 1.23 phil }
818 1.23 phil }
819 1.23 phil #else
820 1.1 cgd void
821 1.1 cgd dointr()
822 1.1 cgd {
823 1.38 mrg long *intrcnt, inttotal, uptime;
824 1.38 mrg int nintr, inamlen;
825 1.38 mrg char *intrname;
826 1.28 cgd struct evcntlist allevents;
827 1.28 cgd struct evcnt evcnt, *evptr;
828 1.18 pk struct device dev;
829 1.1 cgd
830 1.1 cgd uptime = getuptime();
831 1.13 cgd nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
832 1.13 cgd inamlen =
833 1.13 cgd namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
834 1.1 cgd intrcnt = malloc((size_t)nintr);
835 1.1 cgd intrname = malloc((size_t)inamlen);
836 1.1 cgd if (intrcnt == NULL || intrname == NULL) {
837 1.1 cgd (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
838 1.1 cgd exit(1);
839 1.1 cgd }
840 1.1 cgd kread(X_INTRCNT, intrcnt, (size_t)nintr);
841 1.1 cgd kread(X_INTRNAMES, intrname, (size_t)inamlen);
842 1.27 cgd (void)printf("interrupt total rate\n");
843 1.1 cgd inttotal = 0;
844 1.1 cgd nintr /= sizeof(long);
845 1.1 cgd while (--nintr >= 0) {
846 1.1 cgd if (*intrcnt)
847 1.27 cgd (void)printf("%-14s %8ld %8ld\n", intrname,
848 1.1 cgd *intrcnt, *intrcnt / uptime);
849 1.1 cgd intrname += strlen(intrname) + 1;
850 1.1 cgd inttotal += *intrcnt++;
851 1.1 cgd }
852 1.18 pk kread(X_ALLEVENTS, &allevents, sizeof allevents);
853 1.28 cgd evptr = allevents.tqh_first;
854 1.28 cgd while (evptr) {
855 1.28 cgd if (kvm_read(kd, (long)evptr, (void *)&evcnt,
856 1.18 pk sizeof evcnt) != sizeof evcnt) {
857 1.38 mrg (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
858 1.18 pk kvm_geterr(kd));
859 1.18 pk exit(1);
860 1.18 pk }
861 1.32 cgd if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
862 1.32 cgd sizeof dev) != sizeof dev) {
863 1.38 mrg (void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
864 1.32 cgd kvm_geterr(kd));
865 1.32 cgd exit(1);
866 1.18 pk }
867 1.32 cgd if (evcnt.ev_count)
868 1.32 cgd (void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
869 1.38 mrg (long)evcnt.ev_count, evcnt.ev_count / uptime);
870 1.32 cgd inttotal += evcnt.ev_count++;
871 1.32 cgd
872 1.28 cgd evptr = evcnt.ev_list.tqe_next;
873 1.18 pk }
874 1.27 cgd (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime);
875 1.1 cgd }
876 1.23 phil #endif
877 1.1 cgd
878 1.1 cgd /*
879 1.1 cgd * These names are defined in <sys/malloc.h>.
880 1.1 cgd */
881 1.1 cgd char *kmemnames[] = INITKMEMNAMES;
882 1.1 cgd
883 1.1 cgd void
884 1.1 cgd domem()
885 1.1 cgd {
886 1.38 mrg struct kmembuckets *kp;
887 1.38 mrg struct kmemstats *ks;
888 1.38 mrg int i, j;
889 1.13 cgd int len, size, first;
890 1.13 cgd long totuse = 0, totfree = 0, totreq = 0;
891 1.13 cgd char *name;
892 1.13 cgd struct kmemstats kmemstats[M_LAST];
893 1.1 cgd struct kmembuckets buckets[MINBUCKET + 16];
894 1.1 cgd
895 1.1 cgd kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
896 1.34 thorpej for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
897 1.34 thorpej i++, kp++) {
898 1.1 cgd if (kp->kb_calls == 0)
899 1.1 cgd continue;
900 1.34 thorpej if (first) {
901 1.34 thorpej (void)printf("Memory statistics by bucket size\n");
902 1.34 thorpej (void)printf(
903 1.34 thorpej " Size In Use Free Requests HighWater Couldfree\n");
904 1.34 thorpej first = 0;
905 1.34 thorpej }
906 1.1 cgd size = 1 << i;
907 1.1 cgd (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
908 1.1 cgd kp->kb_total - kp->kb_totalfree,
909 1.1 cgd kp->kb_totalfree, kp->kb_calls,
910 1.1 cgd kp->kb_highwat, kp->kb_couldfree);
911 1.1 cgd totfree += size * kp->kb_totalfree;
912 1.34 thorpej }
913 1.34 thorpej
914 1.34 thorpej /*
915 1.34 thorpej * If kmem statistics are not being gathered by the kernel,
916 1.34 thorpej * first will still be 1.
917 1.34 thorpej */
918 1.34 thorpej if (first) {
919 1.34 thorpej printf(
920 1.34 thorpej "Kmem statistics are not being gathered by the kernel.\n");
921 1.34 thorpej return;
922 1.1 cgd }
923 1.1 cgd
924 1.1 cgd kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
925 1.13 cgd (void)printf("\nMemory usage type by bucket size\n");
926 1.13 cgd (void)printf(" Size Type(s)\n");
927 1.13 cgd kp = &buckets[MINBUCKET];
928 1.13 cgd for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
929 1.13 cgd if (kp->kb_calls == 0)
930 1.13 cgd continue;
931 1.13 cgd first = 1;
932 1.13 cgd len = 8;
933 1.13 cgd for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
934 1.13 cgd if (ks->ks_calls == 0)
935 1.13 cgd continue;
936 1.13 cgd if ((ks->ks_size & j) == 0)
937 1.13 cgd continue;
938 1.35 is if (kmemnames[i] == 0) {
939 1.35 is kmemnames[i] = malloc(10);
940 1.35 is /* strlen("undef/")+3+1);*/
941 1.35 is snprintf(kmemnames[i], 10, "undef/%d", i);
942 1.35 is /* same 10 as above!!! */
943 1.35 is }
944 1.35 is name = kmemnames[i];
945 1.13 cgd len += 2 + strlen(name);
946 1.13 cgd if (first)
947 1.13 cgd printf("%8d %s", j, name);
948 1.13 cgd else
949 1.13 cgd printf(",");
950 1.13 cgd if (len >= 80) {
951 1.13 cgd printf("\n\t ");
952 1.13 cgd len = 10 + strlen(name);
953 1.13 cgd }
954 1.13 cgd if (!first)
955 1.13 cgd printf(" %s", name);
956 1.13 cgd first = 0;
957 1.13 cgd }
958 1.13 cgd printf("\n");
959 1.13 cgd }
960 1.13 cgd
961 1.1 cgd (void)printf(
962 1.13 cgd "\nMemory statistics by type Type Kern\n");
963 1.13 cgd (void)printf(
964 1.36 is " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
965 1.13 cgd for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
966 1.1 cgd if (ks->ks_calls == 0)
967 1.1 cgd continue;
968 1.36 is (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
969 1.1 cgd kmemnames[i] ? kmemnames[i] : "undefined",
970 1.1 cgd ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
971 1.1 cgd (ks->ks_maxused + 1023) / 1024,
972 1.1 cgd (ks->ks_limit + 1023) / 1024, ks->ks_calls,
973 1.1 cgd ks->ks_limblocks, ks->ks_mapblocks);
974 1.13 cgd first = 1;
975 1.13 cgd for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
976 1.13 cgd if ((ks->ks_size & j) == 0)
977 1.13 cgd continue;
978 1.13 cgd if (first)
979 1.13 cgd printf(" %d", j);
980 1.13 cgd else
981 1.13 cgd printf(",%d", j);
982 1.13 cgd first = 0;
983 1.13 cgd }
984 1.13 cgd printf("\n");
985 1.1 cgd totuse += ks->ks_memuse;
986 1.1 cgd totreq += ks->ks_calls;
987 1.1 cgd }
988 1.13 cgd (void)printf("\nMemory Totals: In Use Free Requests\n");
989 1.13 cgd (void)printf(" %7ldK %6ldK %8ld\n",
990 1.13 cgd (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
991 1.1 cgd }
992 1.1 cgd
993 1.1 cgd /*
994 1.1 cgd * kread reads something from the kernel, given its nlist index.
995 1.1 cgd */
996 1.1 cgd void
997 1.1 cgd kread(nlx, addr, size)
998 1.1 cgd int nlx;
999 1.1 cgd void *addr;
1000 1.1 cgd size_t size;
1001 1.1 cgd {
1002 1.1 cgd char *sym;
1003 1.1 cgd
1004 1.13 cgd if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1005 1.13 cgd sym = namelist[nlx].n_name;
1006 1.1 cgd if (*sym == '_')
1007 1.1 cgd ++sym;
1008 1.1 cgd (void)fprintf(stderr,
1009 1.13 cgd "vmstat: symbol %s not defined\n", sym);
1010 1.1 cgd exit(1);
1011 1.1 cgd }
1012 1.13 cgd if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
1013 1.13 cgd sym = namelist[nlx].n_name;
1014 1.1 cgd if (*sym == '_')
1015 1.1 cgd ++sym;
1016 1.13 cgd (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
1017 1.1 cgd exit(1);
1018 1.1 cgd }
1019 1.1 cgd }
1020 1.1 cgd
1021 1.45 thorpej #if defined(UVM)
1022 1.45 thorpej struct nlist histnl[] = {
1023 1.45 thorpej { "_uvm_histories" },
1024 1.45 thorpej #define X_UVM_HISTORIES 0
1025 1.45 thorpej { NULL },
1026 1.45 thorpej };
1027 1.45 thorpej
1028 1.45 thorpej /*
1029 1.45 thorpej * Traverse the UVM history buffers, performing the requested action.
1030 1.45 thorpej *
1031 1.45 thorpej * Note, we assume that if we're not listing, we're dumping.
1032 1.45 thorpej */
1033 1.45 thorpej void
1034 1.45 thorpej hist_traverse(todo, histname)
1035 1.45 thorpej int todo;
1036 1.45 thorpej const char *histname;
1037 1.45 thorpej {
1038 1.45 thorpej struct uvm_history_head histhead;
1039 1.45 thorpej struct uvm_history hist, *histkva;
1040 1.45 thorpej char *name = NULL;
1041 1.45 thorpej size_t namelen = 0;
1042 1.45 thorpej
1043 1.45 thorpej if (kvm_nlist(kd, histnl) != 0) {
1044 1.45 thorpej printf("UVM history is not compiled into the kernel.\n");
1045 1.45 thorpej return;
1046 1.45 thorpej }
1047 1.45 thorpej
1048 1.45 thorpej if (kvm_read(kd, histnl[X_UVM_HISTORIES].n_value, &histhead,
1049 1.45 thorpej sizeof(histhead)) != sizeof(histhead)) {
1050 1.45 thorpej warnx("unable to read %s: %s",
1051 1.45 thorpej histnl[X_UVM_HISTORIES].n_name, kvm_geterr(kd));
1052 1.45 thorpej return;
1053 1.45 thorpej }
1054 1.45 thorpej
1055 1.45 thorpej if (histhead.lh_first == NULL) {
1056 1.45 thorpej printf("No active UVM history logs.\n");
1057 1.45 thorpej return;
1058 1.45 thorpej }
1059 1.45 thorpej
1060 1.45 thorpej if (todo & HISTLIST)
1061 1.45 thorpej printf("Active UVM histories:");
1062 1.45 thorpej
1063 1.45 thorpej for (histkva = histhead.lh_first; histkva != NULL;
1064 1.45 thorpej histkva = hist.list.le_next) {
1065 1.45 thorpej if (kvm_read(kd, (u_long)histkva, &hist, sizeof(hist)) !=
1066 1.45 thorpej sizeof(hist)) {
1067 1.45 thorpej warnx("unable to read history at %p: %s",
1068 1.45 thorpej histkva, kvm_geterr(kd));
1069 1.45 thorpej goto out;
1070 1.45 thorpej }
1071 1.45 thorpej
1072 1.45 thorpej if (hist.namelen > namelen) {
1073 1.45 thorpej if (name != NULL)
1074 1.45 thorpej free(name);
1075 1.45 thorpej namelen = hist.namelen;
1076 1.45 thorpej if ((name = malloc(namelen + 1)) == NULL)
1077 1.45 thorpej err(1, "malloc history name");
1078 1.45 thorpej }
1079 1.45 thorpej
1080 1.45 thorpej if (kvm_read(kd, (u_long)hist.name, name, namelen) !=
1081 1.45 thorpej namelen) {
1082 1.45 thorpej warnx("unable to read history name at %p: %s",
1083 1.45 thorpej hist.name, kvm_geterr(kd));
1084 1.45 thorpej goto out;
1085 1.45 thorpej }
1086 1.45 thorpej name[namelen] = '\0';
1087 1.45 thorpej if (todo & HISTLIST)
1088 1.45 thorpej printf(" %s", name);
1089 1.45 thorpej else {
1090 1.45 thorpej /*
1091 1.45 thorpej * If we're dumping all histories, do it, else
1092 1.45 thorpej * check to see if this is the one we want.
1093 1.45 thorpej */
1094 1.45 thorpej if (histname == NULL || strcmp(histname, name) == 0) {
1095 1.45 thorpej if (histname == NULL)
1096 1.45 thorpej printf("\nUVM history `%s':\n", name);
1097 1.45 thorpej hist_dodump(&hist);
1098 1.45 thorpej }
1099 1.45 thorpej }
1100 1.45 thorpej }
1101 1.45 thorpej
1102 1.45 thorpej if (todo & HISTLIST)
1103 1.45 thorpej printf("\n");
1104 1.45 thorpej
1105 1.45 thorpej out:
1106 1.45 thorpej if (name != NULL)
1107 1.45 thorpej free(name);
1108 1.45 thorpej }
1109 1.45 thorpej
1110 1.45 thorpej /*
1111 1.45 thorpej * Actually dump the history buffer at the specified KVA.
1112 1.45 thorpej */
1113 1.45 thorpej void
1114 1.45 thorpej hist_dodump(histp)
1115 1.45 thorpej struct uvm_history *histp;
1116 1.45 thorpej {
1117 1.45 thorpej struct uvm_history_ent *histents, *e;
1118 1.45 thorpej size_t histsize;
1119 1.45 thorpej char *fmt = NULL, *fn = NULL;
1120 1.45 thorpej size_t fmtlen = 0, fnlen = 0;
1121 1.45 thorpej int i;
1122 1.45 thorpej
1123 1.45 thorpej histsize = sizeof(struct uvm_history_ent) * histp->n;
1124 1.45 thorpej
1125 1.45 thorpej if ((histents = malloc(histsize)) == NULL)
1126 1.45 thorpej err(1, "malloc history entries");
1127 1.45 thorpej
1128 1.45 thorpej memset(histents, 0, histsize);
1129 1.45 thorpej
1130 1.45 thorpej if (kvm_read(kd, (u_long)histp->e, histents, histsize) != histsize) {
1131 1.45 thorpej warnx("unable to read history entries at %p: %s",
1132 1.45 thorpej histp->e, kvm_geterr(kd));
1133 1.45 thorpej goto out;
1134 1.45 thorpej }
1135 1.45 thorpej
1136 1.45 thorpej i = histp->f;
1137 1.45 thorpej do {
1138 1.45 thorpej e = &histents[i];
1139 1.45 thorpej if (e->fmt != NULL) {
1140 1.45 thorpej if (e->fmtlen > fmtlen) {
1141 1.45 thorpej if (fmt != NULL)
1142 1.45 thorpej free(fmt);
1143 1.45 thorpej fmtlen = e->fmtlen;
1144 1.45 thorpej if ((fmt = malloc(fmtlen + 1)) == NULL)
1145 1.45 thorpej err(1, "malloc printf format");
1146 1.45 thorpej }
1147 1.45 thorpej if (e->fnlen > fnlen) {
1148 1.45 thorpej if (fn != NULL)
1149 1.45 thorpej free(fn);
1150 1.45 thorpej fnlen = e->fnlen;
1151 1.45 thorpej if ((fn = malloc(fnlen + 1)) == NULL)
1152 1.45 thorpej err(1, "malloc function name");
1153 1.45 thorpej }
1154 1.45 thorpej
1155 1.45 thorpej if (kvm_read(kd, (u_long)e->fmt, fmt, fmtlen)
1156 1.45 thorpej != fmtlen) {
1157 1.45 thorpej warnx("unable to read printf format "
1158 1.45 thorpej "at %p: %s", e->fmt, kvm_geterr(kd));
1159 1.45 thorpej goto out;
1160 1.45 thorpej }
1161 1.45 thorpej fmt[fmtlen] = '\0';
1162 1.45 thorpej
1163 1.45 thorpej if (kvm_read(kd, (u_long)e->fn, fn, fnlen) != fnlen) {
1164 1.45 thorpej warnx("unable to read function name "
1165 1.45 thorpej "at %p: %s", e->fn, kvm_geterr(kd));
1166 1.45 thorpej goto out;
1167 1.45 thorpej }
1168 1.45 thorpej fn[fnlen] = '\0';
1169 1.45 thorpej
1170 1.45 thorpej printf("%06ld.%06ld ", e->tv.tv_sec, e->tv.tv_usec);
1171 1.45 thorpej printf("%s#%ld: ", fn, e->call);
1172 1.45 thorpej printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
1173 1.45 thorpej printf("\n");
1174 1.45 thorpej }
1175 1.45 thorpej i = (i + 1) % histp->n;
1176 1.45 thorpej } while (i != histp->f);
1177 1.45 thorpej
1178 1.45 thorpej out:
1179 1.45 thorpej free(histents);
1180 1.45 thorpej if (fmt != NULL)
1181 1.45 thorpej free(fmt);
1182 1.45 thorpej if (fn != NULL)
1183 1.45 thorpej free(fn);
1184 1.45 thorpej }
1185 1.45 thorpej #endif /* UVM */
1186 1.45 thorpej
1187 1.1 cgd void
1188 1.1 cgd usage()
1189 1.1 cgd {
1190 1.47 mrg
1191 1.45 thorpej #if defined(UVM)
1192 1.1 cgd (void)fprintf(stderr,
1193 1.45 thorpej "usage: vmstat [-fHilms] [-h histname] [-c count] [-M core] \
1194 1.1 cgd [-N system] [-w wait] [disks]\n");
1195 1.45 thorpej #else
1196 1.45 thorpej (void)fprintf(stderr,
1197 1.45 thorpej "usage: vmstat [-fims] [-c count] [-M core] \
1198 1.45 thorpej [-N system] [-w wait] [disks]\n");
1199 1.45 thorpej #endif /* UVM */
1200 1.1 cgd exit(1);
1201 1.1 cgd }
1202