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