Fix miscellanious size_t related compiler warnings.
[dragonfly.git] / usr.bin / vmstat / vmstat.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1980, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1980, 1986, 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)vmstat.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/vmstat/vmstat.c,v 1.38.2.4 2001/07/31 19:52:41 tmm Exp $
5be6fe29 36 * $DragonFly: src/usr.bin/vmstat/vmstat.c,v 1.23 2008/02/19 18:19:15 thomas Exp $
984263bc
MD
37 */
38
cc970022 39#define _KERNEL_STRUCTURES
05220613 40
984263bc
MD
41#include <sys/param.h>
42#include <sys/time.h>
05220613 43#include <sys/user.h>
984263bc
MD
44#include <sys/uio.h>
45#include <sys/namei.h>
46#include <sys/malloc.h>
47#include <sys/signal.h>
48#include <sys/fcntl.h>
49#include <sys/ioctl.h>
50#include <sys/sysctl.h>
51#include <sys/vmmeter.h>
52
53#include <vm/vm_param.h>
54
55#include <ctype.h>
56#include <err.h>
57#include <errno.h>
f5d21610 58#include <kinfo.h>
984263bc
MD
59#include <kvm.h>
60#include <limits.h>
61#include <nlist.h>
62#include <paths.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <sysexits.h>
67#include <time.h>
68#include <unistd.h>
69#include <devstat.h>
70
5a190a5c 71static struct nlist namelist[] = {
f5d21610 72#define X_BOOTTIME 0
2c9bc808 73 { "_boottime", 0, 0, 0, 0 },
f5d21610 74#define X_NCHSTATS 1
2c9bc808 75 { "_nchstats", 0, 0, 0, 0 },
4dfb4f32 76#define X_KMEMSTATISTICS 2
2c9bc808 77 { "_kmemstatistics", 0, 0, 0, 0 },
4dfb4f32 78#define X_ZLIST 3
2c9bc808 79 { "_zlist", 0, 0, 0, 0 },
984263bc 80#ifdef notyet
4dfb4f32 81#define X_DEFICIT 4
2c9bc808 82 { "_deficit", 0, 0, 0, 0 },
4dfb4f32 83#define X_FORKSTAT 5
2c9bc808 84 { "_forkstat", 0, 0, 0, 0 },
4dfb4f32 85#define X_REC 6
2c9bc808 86 { "_rectime", 0, 0, 0, 0 },
4dfb4f32 87#define X_PGIN 7
2c9bc808 88 { "_pgintime", 0, 0, 0, 0 },
4dfb4f32 89#define X_XSTATS 8
2c9bc808 90 { "_xstats", 0, 0, 0, 0 },
4dfb4f32 91#define X_END 9
984263bc 92#else
4dfb4f32 93#define X_END 4
984263bc 94#endif
2c9bc808 95 { "", 0, 0, 0, 0 },
984263bc
MD
96};
97
98struct statinfo cur, last;
99int num_devices, maxshowdevs;
100long generation;
101struct device_selection *dev_select;
102int num_selected;
103struct devstat_match *matches;
104int num_matches = 0;
105int num_devices_specified, num_selections;
106long select_generation;
107char **specified_devices;
108devstat_select_mode select_mode;
109
393dfc63
MD
110struct vmmeter vmm, ovmm;
111struct vmstats vms, ovms;
984263bc
MD
112
113int winlines = 20;
114int nflag = 0;
bba17725 115int verbose = 0;
984263bc
MD
116
117kvm_t *kd;
118
f5d21610
JS
119struct kinfo_cputime cp_time, old_cp_time, diff_cp_time;
120
984263bc
MD
121#define FORKSTAT 0x01
122#define INTRSTAT 0x02
123#define MEMSTAT 0x04
124#define SUMSTAT 0x08
125#define TIMESTAT 0x10
126#define VMSTAT 0x20
127#define ZMEMSTAT 0x40
128
3c6b4125
JS
129static void cpustats(void);
130static void dointr(void);
131static void domem(void);
132static void dosum(void);
133static void dozmem(void);
2c9bc808
SW
134static void dovmstat(u_int, int);
135static void kread(int, void *, size_t);
3c6b4125 136static void usage(void);
2c9bc808 137static char **getdrivedata(char **);
3c6b4125 138static long getuptime(void);
2c9bc808
SW
139static void needhdr(int);
140static long pct(long, long);
3c6b4125 141
984263bc 142#ifdef notyet
3c6b4125
JS
143static void dotimes(void); /* Not implemented */
144static void doforkst(void);
984263bc 145#endif
3c6b4125
JS
146static void printhdr(void);
147static void devstats(void);
984263bc
MD
148
149int
2e134091 150main(int argc, char **argv)
984263bc 151{
2e134091 152 int c, todo;
984263bc
MD
153 u_int interval;
154 int reps;
155 char *memf, *nlistf;
156 char errbuf[_POSIX2_LINE_MAX];
157
158 memf = nlistf = NULL;
159 interval = reps = todo = 0;
160 maxshowdevs = 2;
bba17725 161 while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stvw:z")) != -1) {
984263bc
MD
162 switch (c) {
163 case 'c':
164 reps = atoi(optarg);
165 break;
166 case 'f':
167#ifdef notyet
168 todo |= FORKSTAT;
169#else
170 errx(EX_USAGE, "sorry, -f is not (re)implemented yet");
171#endif
172 break;
173 case 'i':
174 todo |= INTRSTAT;
175 break;
176 case 'M':
177 memf = optarg;
178 break;
179 case 'm':
180 todo |= MEMSTAT;
181 break;
182 case 'N':
183 nlistf = optarg;
184 break;
185 case 'n':
186 nflag = 1;
187 maxshowdevs = atoi(optarg);
188 if (maxshowdevs < 0)
189 errx(1, "number of devices %d is < 0",
190 maxshowdevs);
191 break;
192 case 'p':
193 if (buildmatch(optarg, &matches, &num_matches) != 0)
194 errx(1, "%s", devstat_errbuf);
195 break;
196 case 's':
197 todo |= SUMSTAT;
198 break;
199 case 't':
200#ifdef notyet
201 todo |= TIMESTAT;
202#else
203 errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
204#endif
205 break;
bba17725
YT
206 case 'v':
207 ++verbose;
208 break;
984263bc
MD
209 case 'w':
210 interval = atoi(optarg);
211 break;
212 case 'z':
213 todo |= ZMEMSTAT;
214 break;
984263bc
MD
215 default:
216 usage();
217 }
218 }
219 argc -= optind;
220 argv += optind;
221
222 if (todo == 0)
223 todo = VMSTAT;
224
225 /*
226 * Discard setgid privileges if not the running kernel so that bad
227 * guys can't print interesting stuff from kernel memory.
228 */
229 if (nlistf != NULL || memf != NULL)
230 setgid(getgid());
231
232 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
233 if (kd == 0)
234 errx(1, "kvm_openfiles: %s", errbuf);
235
236 if ((c = kvm_nlist(kd, namelist)) != 0) {
237 if (c > 0) {
238 warnx("undefined symbols:");
3c6b4125 239 for (c = 0; c < (int)__arysize(namelist); c++)
984263bc
MD
240 if (namelist[c].n_type == 0)
241 fprintf(stderr, " %s",
242 namelist[c].n_name);
2e134091 243 fputc('\n', stderr);
984263bc
MD
244 } else
245 warnx("kvm_nlist: %s", kvm_geterr(kd));
246 exit(1);
247 }
248
249 if (todo & VMSTAT) {
984263bc
MD
250 struct winsize winsize;
251
252 /*
253 * Make sure that the userland devstat version matches the
254 * kernel devstat version. If not, exit and print a
255 * message informing the user of his mistake.
256 */
257 if (checkversion() < 0)
258 errx(1, "%s", devstat_errbuf);
259
260
261 argv = getdrivedata(argv);
262 winsize.ws_row = 0;
2e134091 263 ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
984263bc
MD
264 if (winsize.ws_row > 0)
265 winlines = winsize.ws_row;
266
267 }
268
269#define BACKWARD_COMPATIBILITY
270#ifdef BACKWARD_COMPATIBILITY
271 if (*argv) {
272 interval = atoi(*argv);
273 if (*++argv)
274 reps = atoi(*argv);
275 }
276#endif
277
278 if (interval) {
279 if (!reps)
280 reps = -1;
281 } else if (reps)
282 interval = 1;
283
284#ifdef notyet
285 if (todo & FORKSTAT)
286 doforkst();
287#endif
288 if (todo & MEMSTAT)
289 domem();
290 if (todo & ZMEMSTAT)
291 dozmem();
292 if (todo & SUMSTAT)
293 dosum();
294#ifdef notyet
295 if (todo & TIMESTAT)
296 dotimes();
297#endif
298 if (todo & INTRSTAT)
299 dointr();
300 if (todo & VMSTAT)
301 dovmstat(interval, reps);
302 exit(0);
303}
304
3c6b4125 305static char **
1d1731fa 306getdrivedata(char **argv)
984263bc
MD
307{
308 if ((num_devices = getnumdevs()) < 0)
309 errx(1, "%s", devstat_errbuf);
310
311 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
312 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
313 bzero(cur.dinfo, sizeof(struct devinfo));
314 bzero(last.dinfo, sizeof(struct devinfo));
315
316 if (getdevs(&cur) == -1)
317 errx(1, "%s", devstat_errbuf);
318
319 num_devices = cur.dinfo->numdevs;
320 generation = cur.dinfo->generation;
321
322 specified_devices = (char **)malloc(sizeof(char *));
323 for (num_devices_specified = 0; *argv; ++argv) {
324 if (isdigit(**argv))
325 break;
326 num_devices_specified++;
327 specified_devices = (char **)realloc(specified_devices,
328 sizeof(char *) *
329 num_devices_specified);
330 specified_devices[num_devices_specified - 1] = *argv;
331 }
332 dev_select = NULL;
333
334 if (nflag == 0 && maxshowdevs < num_devices_specified)
335 maxshowdevs = num_devices_specified;
336
337 /*
338 * People are generally only interested in disk statistics when
339 * they're running vmstat. So, that's what we're going to give
340 * them if they don't specify anything by default. We'll also give
341 * them any other random devices in the system so that we get to
342 * maxshowdevs devices, if that many devices exist. If the user
343 * specifies devices on the command line, either through a pattern
344 * match or by naming them explicitly, we will give the user only
345 * those devices.
346 */
347 if ((num_devices_specified == 0) && (num_matches == 0)) {
348 if (buildmatch("da", &matches, &num_matches) != 0)
349 errx(1, "%s", devstat_errbuf);
350
351 select_mode = DS_SELECT_ADD;
352 } else
353 select_mode = DS_SELECT_ONLY;
354
355 /*
356 * At this point, selectdevs will almost surely indicate that the
357 * device list has changed, so we don't look for return values of 0
358 * or 1. If we get back -1, though, there is an error.
359 */
360 if (selectdevs(&dev_select, &num_selected, &num_selections,
361 &select_generation, generation, cur.dinfo->devices,
362 num_devices, matches, num_matches, specified_devices,
363 num_devices_specified, select_mode,
364 maxshowdevs, 0) == -1)
365 errx(1, "%s", devstat_errbuf);
366
367 return(argv);
368}
369
3c6b4125 370static long
1d1731fa 371getuptime(void)
984263bc
MD
372{
373 static time_t now, boottime;
374 time_t uptime;
375
376 if (boottime == 0)
377 kread(X_BOOTTIME, &boottime, sizeof(boottime));
2e134091 378 time(&now);
984263bc
MD
379 uptime = now - boottime;
380 if (uptime <= 0 || uptime > 60*60*24*365*10)
381 errx(1, "time makes no sense; namelist must be wrong");
382 return(uptime);
383}
384
f5d21610 385int hdrcnt;
984263bc 386
3c6b4125 387static void
1d1731fa 388dovmstat(u_int interval, int reps)
984263bc
MD
389{
390 struct vmtotal total;
391 time_t uptime, halfuptime;
392 struct devinfo *tmp_dinfo;
e43bcaac
JG
393 size_t vmm_size = sizeof(vmm);
394 size_t vms_size = sizeof(vms);
395 size_t vmt_size = sizeof(total);
984263bc
MD
396
397 uptime = getuptime();
398 halfuptime = uptime / 2;
2e134091 399 signal(SIGCONT, needhdr);
984263bc 400
984263bc
MD
401 for (hdrcnt = 1;;) {
402 if (!--hdrcnt)
403 printhdr();
f5d21610
JS
404 if (kinfo_get_sched_cputime(&cp_time))
405 err(1, "kinfo_get_sched_cputime");
984263bc
MD
406
407 tmp_dinfo = last.dinfo;
408 last.dinfo = cur.dinfo;
409 cur.dinfo = tmp_dinfo;
410 last.busy_time = cur.busy_time;
411
412 /*
413 * Here what we want to do is refresh our device stats.
414 * getdevs() returns 1 when the device list has changed.
415 * If the device list has changed, we want to go through
416 * the selection process again, in case a device that we
417 * were previously displaying has gone away.
418 */
419 switch (getdevs(&cur)) {
420 case -1:
421 errx(1, "%s", devstat_errbuf);
422 break;
423 case 1: {
424 int retval;
425
426 num_devices = cur.dinfo->numdevs;
427 generation = cur.dinfo->generation;
428
429 retval = selectdevs(&dev_select, &num_selected,
430 &num_selections, &select_generation,
431 generation, cur.dinfo->devices,
432 num_devices, matches, num_matches,
433 specified_devices,
434 num_devices_specified, select_mode,
435 maxshowdevs, 0);
436 switch (retval) {
437 case -1:
438 errx(1, "%s", devstat_errbuf);
439 break;
440 case 1:
441 printhdr();
442 break;
443 default:
444 break;
445 }
446 }
447 default:
448 break;
449 }
450
393dfc63
MD
451 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
452 perror("sysctlbyname: vm.vmstats");
453 exit(1);
984263bc 454 }
393dfc63 455 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
5f548a46 456 perror("sysctlbyname: vm.vmmeter");
393dfc63
MD
457 exit(1);
458 }
459 if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) {
460 perror("sysctlbyname: vm.vmtotal");
461 exit(1);
462 }
2e134091 463 printf("%2d %1d %1d",
984263bc 464 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
393dfc63 465#define vmstat_pgtok(a) ((a) * vms.v_page_size >> 10)
984263bc 466#define rate(x) (((x) + halfuptime) / uptime) /* round */
2e134091 467 printf(" %7ld %6ld ",
984263bc 468 (long)vmstat_pgtok(total.t_avm), (long)vmstat_pgtok(total.t_free));
2e134091 469 printf("%4lu ",
393dfc63 470 (u_long)rate(vmm.v_vm_faults - ovmm.v_vm_faults));
2e134091 471 printf("%3lu ",
393dfc63 472 (u_long)rate(vmm.v_reactivated - ovmm.v_reactivated));
2e134091 473 printf("%3lu ",
393dfc63
MD
474 (u_long)rate(vmm.v_swapin + vmm.v_vnodein -
475 (ovmm.v_swapin + ovmm.v_vnodein)));
2e134091 476 printf("%3lu ",
393dfc63
MD
477 (u_long)rate(vmm.v_swapout + vmm.v_vnodeout -
478 (ovmm.v_swapout + ovmm.v_vnodeout)));
2e134091 479 printf("%3lu ",
393dfc63 480 (u_long)rate(vmm.v_tfree - ovmm.v_tfree));
2e134091 481 printf("%3lu ",
393dfc63 482 (u_long)rate(vmm.v_pdpages - ovmm.v_pdpages));
984263bc 483 devstats();
2e134091 484 printf("%4lu %4lu %3lu ",
393dfc63
MD
485 (u_long)rate(vmm.v_intr - ovmm.v_intr),
486 (u_long)rate(vmm.v_syscall - ovmm.v_syscall),
487 (u_long)rate(vmm.v_swtch - ovmm.v_swtch));
984263bc 488 cpustats();
2e134091
LF
489 printf("\n");
490 fflush(stdout);
984263bc
MD
491 if (reps >= 0 && --reps <= 0)
492 break;
393dfc63 493 ovmm = vmm;
984263bc
MD
494 uptime = interval;
495 /*
496 * We round upward to avoid losing low-frequency events
497 * (i.e., >= 1 per interval but < 1 per second).
498 */
499 if (interval != 1)
500 halfuptime = (uptime + 1) / 2;
501 else
502 halfuptime = 0;
2e134091 503 sleep(interval);
984263bc
MD
504 }
505}
506
3c6b4125 507static void
1d1731fa 508printhdr(void)
984263bc
MD
509{
510 int i, num_shown;
511
512 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
2e134091 513 printf(" procs memory page%*s", 19, "");
984263bc 514 if (num_shown > 1)
2e134091 515 printf(" disks %*s", num_shown * 4 - 7, "");
984263bc 516 else if (num_shown == 1)
2e134091
LF
517 printf("disk");
518 printf(" faults cpu\n");
519 printf(" r b w avm fre flt re pi po fr sr ");
984263bc
MD
520 for (i = 0; i < num_devices; i++)
521 if ((dev_select[i].selected)
522 && (dev_select[i].selected <= maxshowdevs))
2e134091 523 printf("%c%c%d ", dev_select[i].device_name[0],
984263bc
MD
524 dev_select[i].device_name[1],
525 dev_select[i].unit_number);
2e134091 526 printf(" in sy cs us sy id\n");
984263bc
MD
527 hdrcnt = winlines - 2;
528}
529
530/*
531 * Force a header to be prepended to the next output.
532 */
3c6b4125
JS
533static void
534needhdr(__unused int signo)
984263bc
MD
535{
536
537 hdrcnt = 1;
538}
539
3c6b4125 540static long
1d1731fa 541pct(long top, long bot)
984263bc
MD
542{
543 long ans;
544
545 if (bot == 0)
546 return(0);
547 ans = (quad_t)top * 100 / bot;
548 return (ans);
549}
550
551#define PCT(top, bot) pct((long)(top), (long)(bot))
552
3c6b4125 553static void
1d1731fa 554dosum(void)
984263bc 555{
24e51f36 556 struct nchstats *nch_tmp, nchstats;
e43bcaac
JG
557 size_t vms_size = sizeof(vms);
558 size_t vmm_size = sizeof(vmm);
24e51f36
HP
559 int cpucnt;
560 u_long nchtotal;
561 size_t nch_size = sizeof(struct nchstats) * SMP_MAXCPU;
984263bc 562
393dfc63
MD
563 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
564 perror("sysctlbyname: vm.vmstats");
565 exit(1);
566 }
567 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
568 perror("sysctlbyname: vm.vmstats");
569 exit(1);
24e51f36 570 }
2e134091
LF
571 printf("%9u cpu context switches\n", vmm.v_swtch);
572 printf("%9u device interrupts\n", vmm.v_intr);
573 printf("%9u software interrupts\n", vmm.v_soft);
574 printf("%9u traps\n", vmm.v_trap);
575 printf("%9u system calls\n", vmm.v_syscall);
576 printf("%9u kernel threads created\n", vmm.v_kthreads);
577 printf("%9u fork() calls\n", vmm.v_forks);
578 printf("%9u vfork() calls\n", vmm.v_vforks);
579 printf("%9u rfork() calls\n", vmm.v_rforks);
580 printf("%9u exec() calls\n", vmm.v_exec);
581 printf("%9u swap pager pageins\n", vmm.v_swapin);
582 printf("%9u swap pager pages paged in\n", vmm.v_swappgsin);
583 printf("%9u swap pager pageouts\n", vmm.v_swapout);
584 printf("%9u swap pager pages paged out\n", vmm.v_swappgsout);
585 printf("%9u vnode pager pageins\n", vmm.v_vnodein);
586 printf("%9u vnode pager pages paged in\n", vmm.v_vnodepgsin);
587 printf("%9u vnode pager pageouts\n", vmm.v_vnodeout);
588 printf("%9u vnode pager pages paged out\n", vmm.v_vnodepgsout);
589 printf("%9u page daemon wakeups\n", vmm.v_pdwakeups);
590 printf("%9u pages examined by the page daemon\n", vmm.v_pdpages);
591 printf("%9u pages reactivated\n", vmm.v_reactivated);
592 printf("%9u copy-on-write faults\n", vmm.v_cow_faults);
593 printf("%9u copy-on-write optimized faults\n", vmm.v_cow_optim);
594 printf("%9u zero fill pages zeroed\n", vmm.v_zfod);
595 printf("%9u zero fill pages prezeroed\n", vmm.v_ozfod);
596 printf("%9u intransit blocking page faults\n", vmm.v_intrans);
597 printf("%9u total VM faults taken\n", vmm.v_vm_faults);
598 printf("%9u pages affected by kernel thread creation\n", vmm.v_kthreadpages);
599 printf("%9u pages affected by fork()\n", vmm.v_forkpages);
600 printf("%9u pages affected by vfork()\n", vmm.v_vforkpages);
601 printf("%9u pages affected by rfork()\n", vmm.v_rforkpages);
602 printf("%9u pages freed\n", vmm.v_tfree);
603 printf("%9u pages freed by daemon\n", vmm.v_dfree);
604 printf("%9u pages freed by exiting processes\n", vmm.v_pfree);
605 printf("%9u pages active\n", vms.v_active_count);
606 printf("%9u pages inactive\n", vms.v_inactive_count);
607 printf("%9u pages in VM cache\n", vms.v_cache_count);
608 printf("%9u pages wired down\n", vms.v_wire_count);
609 printf("%9u pages free\n", vms.v_free_count);
610 printf("%9u bytes per page\n", vms.v_page_size);
24e51f36
HP
611
612 if ((nch_tmp = malloc(nch_size)) == NULL) {
613 perror("malloc");
614 exit(1);
615 } else {
616 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
617 perror("sysctlbyname vfs.cache.nchstats");
618 free(nch_tmp);
619 exit(1);
620 } else {
621 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
622 perror("realloc");
623 exit(1);
624 }
625 }
626 }
627
628 cpucnt = nch_size / sizeof(struct nchstats);
629 kvm_nch_cpuagg(nch_tmp, &nchstats, cpucnt);
630
984263bc
MD
631 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
632 nchstats.ncs_badhits + nchstats.ncs_falsehits +
633 nchstats.ncs_miss + nchstats.ncs_long;
2e134091
LF
634 printf("%9ld total name lookups\n", nchtotal);
635 printf(
984263bc
MD
636 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
637 "", PCT(nchstats.ncs_goodhits, nchtotal),
638 PCT(nchstats.ncs_neghits, nchtotal),
639 PCT(nchstats.ncs_pass2, nchtotal));
2e134091 640 printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
984263bc
MD
641 PCT(nchstats.ncs_badhits, nchtotal),
642 PCT(nchstats.ncs_falsehits, nchtotal),
643 PCT(nchstats.ncs_long, nchtotal));
24e51f36 644 free(nch_tmp);
984263bc
MD
645}
646
647#ifdef notyet
648void
1d1731fa 649doforkst(void)
984263bc
MD
650{
651 struct forkstat fks;
652
653 kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
2e134091 654 printf("%d forks, %d pages, average %.2f\n",
984263bc 655 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
2e134091 656 printf("%d vforks, %d pages, average %.2f\n",
984263bc
MD
657 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
658}
659#endif
660
661static void
1d1731fa 662devstats(void)
984263bc 663{
3c6b4125 664 int dn;
984263bc
MD
665 long double transfers_per_second;
666 long double busy_seconds;
f5d21610
JS
667
668 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
669 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
670 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
671 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
672 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
673 old_cp_time = cp_time;
984263bc
MD
674
675 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
676
677 for (dn = 0; dn < num_devices; dn++) {
678 int di;
679
680 if ((dev_select[dn].selected == 0)
681 || (dev_select[dn].selected > maxshowdevs))
682 continue;
683
684 di = dev_select[dn].position;
685
686 if (compute_stats(&cur.dinfo->devices[di],
687 &last.dinfo->devices[di], busy_seconds,
688 NULL, NULL, NULL,
689 NULL, &transfers_per_second, NULL,
690 NULL, NULL) != 0)
691 errx(1, "%s", devstat_errbuf);
692
693 printf("%3.0Lf ", transfers_per_second);
694 }
695}
696
3c6b4125 697static void
1d1731fa 698cpustats(void)
984263bc 699{
f5d21610 700 uint64_t total;
3c6b4125 701 double totusage;
f5d21610
JS
702
703 total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
704 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
984263bc 705
984263bc 706 if (total)
3c6b4125 707 totusage = 100.0 / total;
984263bc 708 else
3c6b4125
JS
709 totusage = 0;
710 printf("%2.0f ",
711 (diff_cp_time.cp_user + diff_cp_time.cp_nice) * totusage);
712 printf("%2.0f ",
713 (diff_cp_time.cp_sys + diff_cp_time.cp_intr) * totusage);
714 printf("%2.0f",
715 diff_cp_time.cp_idle * totusage);
984263bc
MD
716}
717
3c6b4125 718static void
1d1731fa 719dointr(void)
984263bc 720{
2e134091
LF
721 u_long *intrcnt, uptime;
722 u_int64_t inttotal;
4dfb4f32 723 size_t nintr, inamlen, i, size;
a73fdee8
MD
724 int nwidth;
725 char *intrstr;
726 char **intrname;
984263bc
MD
727
728 uptime = getuptime();
4dfb4f32
SS
729 if (sysctlbyname("hw.intrnames", NULL, &inamlen, NULL, 0) != 0)
730 errx(1, "sysctlbyname");
a73fdee8
MD
731 intrstr = malloc(inamlen);
732 if (intrstr == NULL)
4dfb4f32 733 err(1, "malloc");
a73fdee8
MD
734 sysctlbyname("hw.intrnames", intrstr, &inamlen, NULL, 0);
735 for (nintr = 0, i = 0; i < inamlen; ++i) {
736 if (intrstr[i] == 0)
4dfb4f32 737 nintr++;
a73fdee8
MD
738 }
739 intrname = malloc(nintr * sizeof(char *));
740 for (i = 0; i < nintr; ++i) {
741 intrname[i] = intrstr;
742 intrstr += strlen(intrstr) + 1;
743 }
744
4dfb4f32 745 size = nintr * sizeof(*intrcnt);
53052796 746 intrcnt = calloc(nintr, sizeof(*intrcnt));
4dfb4f32
SS
747 if (intrcnt == NULL)
748 err(1, "malloc");
749 sysctlbyname("hw.intrcnt", intrcnt, &size, NULL, 0);
a73fdee8 750
bba17725 751 nwidth = 21;
a73fdee8
MD
752 for (i = 0; i < nintr; ++i) {
753 if (nwidth < (int)strlen(intrname[i]))
754 nwidth = (int)strlen(intrname[i]);
755 }
5be6fe29 756 if (verbose) nwidth += 8;
a73fdee8 757
5be6fe29
TN
758 printf("%-*.*s %11s %10s\n",
759 nwidth, nwidth, "interrupt", "total", "rate");
984263bc 760 inttotal = 0;
a73fdee8 761 for (i = 0; i < nintr; ++i) {
bba17725 762 int named;
5be6fe29 763 char *infop, irqinfo[72];
bba17725
YT
764
765 if ((named = strncmp(intrname[i], "irq", 3)) != 0 ||
766 intrcnt[i] > 0) {
767 infop = intrname[i];
768 if (verbose && named) {
769 snprintf(irqinfo, sizeof(irqinfo),
424e8382 770 "irq%zd: %s", i, intrname[i]);
bba17725
YT
771 infop = irqinfo;
772 }
a73fdee8 773 printf("%-*.*s %11lu %10lu\n",
bba17725 774 nwidth, nwidth, infop,
a73fdee8 775 intrcnt[i], intrcnt[i] / uptime);
24a1f87d 776 }
a73fdee8 777 inttotal += intrcnt[i];
984263bc 778 }
a73fdee8
MD
779 printf("%-*.*s %11llu %10llu\n",
780 nwidth, nwidth, "Total",
781 inttotal, inttotal / (u_int64_t) uptime);
984263bc
MD
782}
783
784#define MAX_KMSTATS 200
785
2c9bc808 786static long
027557c6
MD
787cpuagg(long *ary)
788{
789 int i;
790 long ttl;
791
792 for (i = ttl = 0; i < SMP_MAXCPU; ++i)
793 ttl += ary[i];
794 return(ttl);
795}
796
3c6b4125 797static void
1d1731fa 798domem(void)
984263bc 799{
2e134091
LF
800 struct malloc_type *ks;
801 int i, j;
24e51f36 802 int first, nkms;
984263bc 803 long totuse = 0, totfree = 0, totreq = 0;
984263bc
MD
804 struct malloc_type kmemstats[MAX_KMSTATS], *kmsp;
805 char buf[1024];
984263bc 806
984263bc
MD
807 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp));
808 for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) {
809 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp,
810 &kmemstats[nkms], sizeof(kmemstats[0])))
811 err(1, "kvm_read(%p)", (void *)kmsp);
812 if (sizeof(buf) != kvm_read(kd,
813 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf)))
814 err(1, "kvm_read(%p)",
3c6b4125 815 kmemstats[nkms].ks_shortdesc);
984263bc
MD
816 buf[sizeof(buf) - 1] = '\0';
817 kmemstats[nkms].ks_shortdesc = strdup(buf);
818 kmsp = kmemstats[nkms].ks_next;
819 }
820 if (kmsp != NULL)
821 warnx("truncated to the first %d memory types", nkms);
984263bc 822
2e134091 823 printf(
984263bc 824 "\nMemory statistics by type Type Kern\n");
2e134091 825 printf(
8e98fb08 826" Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
984263bc
MD
827 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) {
828 if (ks->ks_calls == 0)
829 continue;
8e98fb08 830 printf("%19s%6ld%6ldK%7ldK%7ldK%9lld%5u%6u",
984263bc 831 ks->ks_shortdesc,
027557c6 832 cpuagg(ks->ks_inuse), (cpuagg(ks->ks_memuse) + 1023) / 1024,
984263bc
MD
833 (ks->ks_maxused + 1023) / 1024,
834 (ks->ks_limit + 1023) / 1024, ks->ks_calls,
835 ks->ks_limblocks, ks->ks_mapblocks);
836 first = 1;
837 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
838 if ((ks->ks_size & j) == 0)
839 continue;
840 if (first)
841 printf(" ");
842 else
843 printf(",");
844 if(j<1024)
845 printf("%d",j);
846 else
847 printf("%dK",j>>10);
848 first = 0;
849 }
850 printf("\n");
027557c6 851 totuse += cpuagg(ks->ks_memuse);
984263bc
MD
852 totreq += ks->ks_calls;
853 }
2e134091
LF
854 printf("\nMemory Totals: In Use Free Requests\n");
855 printf(" %7ldK %6ldK %8ld\n",
984263bc
MD
856 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
857}
858
3c6b4125 859static void
1d1731fa 860dozmem(void)
984263bc
MD
861{
862 char *buf;
863 size_t bufsize;
864
865 buf = NULL;
866 bufsize = 1024;
867 for (;;) {
868 if ((buf = realloc(buf, bufsize)) == NULL)
869 err(1, "realloc()");
eecd52b2 870 if (sysctlbyname("vm.zone", buf, &bufsize, NULL, 0) == 0)
984263bc
MD
871 break;
872 if (errno != ENOMEM)
873 err(1, "sysctl()");
874 bufsize *= 2;
875 }
876 buf[bufsize] = '\0'; /* play it safe */
2e134091 877 printf("%s\n\n", buf);
984263bc
MD
878 free(buf);
879}
880
881/*
882 * kread reads something from the kernel, given its nlist index.
883 */
3c6b4125 884static void
1d1731fa 885kread(int nlx, void *addr, size_t size)
984263bc 886{
f5d21610 887 const char *sym;
984263bc
MD
888
889 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
890 sym = namelist[nlx].n_name;
891 if (*sym == '_')
892 ++sym;
893 errx(1, "symbol %s not defined", sym);
894 }
3c6b4125 895 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) {
984263bc
MD
896 sym = namelist[nlx].n_name;
897 if (*sym == '_')
898 ++sym;
899 errx(1, "%s: %s", sym, kvm_geterr(kd));
900 }
901}
902
3c6b4125 903static void
1d1731fa 904usage(void)
984263bc 905{
2e134091 906 fprintf(stderr, "%s%s",
bba17725 907 "usage: vmstat [-imsvz] [-c count] [-M core] [-N system] [-w wait]\n",
984263bc
MD
908 " [-n devs] [disks]\n");
909 exit(1);
910}