vmstat: ks_limit is size_t not ssize_t
[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
e0ecab34 39#include <sys/user.h>
984263bc
MD
40#include <sys/param.h>
41#include <sys/time.h>
984263bc
MD
42#include <sys/uio.h>
43#include <sys/namei.h>
44#include <sys/malloc.h>
45#include <sys/signal.h>
46#include <sys/fcntl.h>
47#include <sys/ioctl.h>
48#include <sys/sysctl.h>
49#include <sys/vmmeter.h>
c83c147e 50#include <sys/interrupt.h>
984263bc
MD
51
52#include <vm/vm_param.h>
53
54#include <ctype.h>
55#include <err.h>
56#include <errno.h>
f5d21610 57#include <kinfo.h>
984263bc
MD
58#include <kvm.h>
59#include <limits.h>
60#include <nlist.h>
61#include <paths.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <sysexits.h>
66#include <time.h>
67#include <unistd.h>
68#include <devstat.h>
69
5a190a5c 70static struct nlist namelist[] = {
f5d21610 71#define X_BOOTTIME 0
2c9bc808 72 { "_boottime", 0, 0, 0, 0 },
f5d21610 73#define X_NCHSTATS 1
2c9bc808 74 { "_nchstats", 0, 0, 0, 0 },
4dfb4f32 75#define X_KMEMSTATISTICS 2
2c9bc808 76 { "_kmemstatistics", 0, 0, 0, 0 },
4dfb4f32 77#define X_ZLIST 3
2c9bc808 78 { "_zlist", 0, 0, 0, 0 },
984263bc 79#ifdef notyet
4dfb4f32 80#define X_DEFICIT 4
2c9bc808 81 { "_deficit", 0, 0, 0, 0 },
4dfb4f32 82#define X_FORKSTAT 5
2c9bc808 83 { "_forkstat", 0, 0, 0, 0 },
4dfb4f32 84#define X_REC 6
2c9bc808 85 { "_rectime", 0, 0, 0, 0 },
4dfb4f32 86#define X_PGIN 7
2c9bc808 87 { "_pgintime", 0, 0, 0, 0 },
4dfb4f32 88#define X_XSTATS 8
2c9bc808 89 { "_xstats", 0, 0, 0, 0 },
4dfb4f32 90#define X_END 9
984263bc 91#else
4dfb4f32 92#define X_END 4
984263bc 93#endif
2c9bc808 94 { "", 0, 0, 0, 0 },
984263bc
MD
95};
96
97struct statinfo cur, last;
98int num_devices, maxshowdevs;
99long generation;
100struct device_selection *dev_select;
101int num_selected;
102struct devstat_match *matches;
103int num_matches = 0;
104int num_devices_specified, num_selections;
105long select_generation;
106char **specified_devices;
107devstat_select_mode select_mode;
108
393dfc63
MD
109struct vmmeter vmm, ovmm;
110struct vmstats vms, ovms;
984263bc
MD
111
112int winlines = 20;
113int nflag = 0;
bba17725 114int verbose = 0;
984263bc
MD
115
116kvm_t *kd;
117
f5d21610
JS
118struct kinfo_cputime cp_time, old_cp_time, diff_cp_time;
119
984263bc
MD
120#define FORKSTAT 0x01
121#define INTRSTAT 0x02
122#define MEMSTAT 0x04
123#define SUMSTAT 0x08
124#define TIMESTAT 0x10
125#define VMSTAT 0x20
126#define ZMEMSTAT 0x40
127
3c6b4125
JS
128static void cpustats(void);
129static void dointr(void);
130static void domem(void);
131static void dosum(void);
132static void dozmem(void);
2c9bc808
SW
133static void dovmstat(u_int, int);
134static void kread(int, void *, size_t);
3c6b4125 135static void usage(void);
2c9bc808 136static char **getdrivedata(char **);
3c6b4125 137static long getuptime(void);
2c9bc808
SW
138static void needhdr(int);
139static long pct(long, long);
3c6b4125 140
984263bc 141#ifdef notyet
3c6b4125
JS
142static void dotimes(void); /* Not implemented */
143static void doforkst(void);
984263bc 144#endif
3c6b4125
JS
145static void printhdr(void);
146static void devstats(void);
984263bc
MD
147
148int
2e134091 149main(int argc, char **argv)
984263bc 150{
2e134091 151 int c, todo;
29758f2e 152 u_int interval; /* milliseconds */
984263bc
MD
153 int reps;
154 char *memf, *nlistf;
155 char errbuf[_POSIX2_LINE_MAX];
156
157 memf = nlistf = NULL;
158 interval = reps = todo = 0;
159 maxshowdevs = 2;
bba17725 160 while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stvw:z")) != -1) {
984263bc
MD
161 switch (c) {
162 case 'c':
163 reps = atoi(optarg);
164 break;
165 case 'f':
166#ifdef notyet
167 todo |= FORKSTAT;
168#else
169 errx(EX_USAGE, "sorry, -f is not (re)implemented yet");
170#endif
171 break;
172 case 'i':
173 todo |= INTRSTAT;
174 break;
175 case 'M':
176 memf = optarg;
177 break;
178 case 'm':
179 todo |= MEMSTAT;
180 break;
181 case 'N':
182 nlistf = optarg;
183 break;
184 case 'n':
185 nflag = 1;
186 maxshowdevs = atoi(optarg);
187 if (maxshowdevs < 0)
188 errx(1, "number of devices %d is < 0",
189 maxshowdevs);
190 break;
191 case 'p':
192 if (buildmatch(optarg, &matches, &num_matches) != 0)
193 errx(1, "%s", devstat_errbuf);
194 break;
195 case 's':
196 todo |= SUMSTAT;
197 break;
198 case 't':
199#ifdef notyet
200 todo |= TIMESTAT;
201#else
202 errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
203#endif
204 break;
bba17725
YT
205 case 'v':
206 ++verbose;
207 break;
984263bc 208 case 'w':
29758f2e 209 interval = (u_int)(strtod(optarg, NULL) * 1000.0);
984263bc
MD
210 break;
211 case 'z':
212 todo |= ZMEMSTAT;
213 break;
984263bc
MD
214 default:
215 usage();
216 }
217 }
218 argc -= optind;
219 argv += optind;
220
221 if (todo == 0)
222 todo = VMSTAT;
223
224 /*
225 * Discard setgid privileges if not the running kernel so that bad
226 * guys can't print interesting stuff from kernel memory.
227 */
228 if (nlistf != NULL || memf != NULL)
229 setgid(getgid());
230
231 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
232 if (kd == 0)
233 errx(1, "kvm_openfiles: %s", errbuf);
234
235 if ((c = kvm_nlist(kd, namelist)) != 0) {
236 if (c > 0) {
237 warnx("undefined symbols:");
3c6b4125 238 for (c = 0; c < (int)__arysize(namelist); c++)
984263bc
MD
239 if (namelist[c].n_type == 0)
240 fprintf(stderr, " %s",
241 namelist[c].n_name);
2e134091 242 fputc('\n', stderr);
984263bc
MD
243 } else
244 warnx("kvm_nlist: %s", kvm_geterr(kd));
245 exit(1);
246 }
247
248 if (todo & VMSTAT) {
984263bc
MD
249 struct winsize winsize;
250
251 /*
252 * Make sure that the userland devstat version matches the
253 * kernel devstat version. If not, exit and print a
254 * message informing the user of his mistake.
255 */
256 if (checkversion() < 0)
257 errx(1, "%s", devstat_errbuf);
258
259
260 argv = getdrivedata(argv);
261 winsize.ws_row = 0;
2e134091 262 ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
984263bc
MD
263 if (winsize.ws_row > 0)
264 winlines = winsize.ws_row;
265
266 }
267
268#define BACKWARD_COMPATIBILITY
269#ifdef BACKWARD_COMPATIBILITY
270 if (*argv) {
29758f2e 271 interval = (u_int)(strtod(*argv, NULL) * 1000.0);
984263bc
MD
272 if (*++argv)
273 reps = atoi(*argv);
274 }
275#endif
276
277 if (interval) {
278 if (!reps)
279 reps = -1;
29758f2e
MD
280 } else if (reps) {
281 interval = 1000;
282 }
984263bc
MD
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;
984263bc 391 struct devinfo *tmp_dinfo;
e43bcaac
JG
392 size_t vmm_size = sizeof(vmm);
393 size_t vms_size = sizeof(vms);
394 size_t vmt_size = sizeof(total);
29758f2e 395 int initial = 1;
984263bc 396
2e134091 397 signal(SIGCONT, needhdr);
984263bc 398
984263bc
MD
399 for (hdrcnt = 1;;) {
400 if (!--hdrcnt)
401 printhdr();
f5d21610
JS
402 if (kinfo_get_sched_cputime(&cp_time))
403 err(1, "kinfo_get_sched_cputime");
984263bc
MD
404
405 tmp_dinfo = last.dinfo;
406 last.dinfo = cur.dinfo;
407 cur.dinfo = tmp_dinfo;
408 last.busy_time = cur.busy_time;
409
410 /*
411 * Here what we want to do is refresh our device stats.
412 * getdevs() returns 1 when the device list has changed.
413 * If the device list has changed, we want to go through
414 * the selection process again, in case a device that we
415 * were previously displaying has gone away.
416 */
417 switch (getdevs(&cur)) {
418 case -1:
419 errx(1, "%s", devstat_errbuf);
420 break;
421 case 1: {
422 int retval;
423
424 num_devices = cur.dinfo->numdevs;
425 generation = cur.dinfo->generation;
426
427 retval = selectdevs(&dev_select, &num_selected,
428 &num_selections, &select_generation,
429 generation, cur.dinfo->devices,
430 num_devices, matches, num_matches,
431 specified_devices,
432 num_devices_specified, select_mode,
433 maxshowdevs, 0);
434 switch (retval) {
435 case -1:
436 errx(1, "%s", devstat_errbuf);
437 break;
438 case 1:
439 printhdr();
440 break;
441 default:
442 break;
443 }
444 }
445 default:
446 break;
447 }
448
393dfc63
MD
449 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
450 perror("sysctlbyname: vm.vmstats");
451 exit(1);
984263bc 452 }
393dfc63 453 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
5f548a46 454 perror("sysctlbyname: vm.vmmeter");
393dfc63
MD
455 exit(1);
456 }
457 if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) {
458 perror("sysctlbyname: vm.vmtotal");
459 exit(1);
460 }
7b5a2bb5 461 printf("%2ld %1ld %1ld",
984263bc 462 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
7b5a2bb5
MD
463
464#define vmstat_pgtok(a) \
465 (intmax_t)(((intmax_t)(a) * vms.v_page_size) >> 10)
466#define rate(x) \
467 (intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \
468 / interval)
469
470 printf(" %7jd %6jd ",
471 vmstat_pgtok(total.t_avm),
472 vmstat_pgtok(total.t_free));
473 printf("%4ju ",
474 rate(vmm.v_vm_faults - ovmm.v_vm_faults));
475 printf("%3ju ",
476 rate(vmm.v_reactivated - ovmm.v_reactivated));
477 printf("%3ju ",
478 rate(vmm.v_swapin + vmm.v_vnodein -
479 (ovmm.v_swapin + ovmm.v_vnodein)));
480 printf("%3ju ",
481 rate(vmm.v_swapout + vmm.v_vnodeout -
482 (ovmm.v_swapout + ovmm.v_vnodeout)));
483 printf("%3ju ",
484 rate(vmm.v_tfree - ovmm.v_tfree));
485 printf("%3ju ",
486 rate(vmm.v_pdpages - ovmm.v_pdpages));
984263bc 487 devstats();
7b5a2bb5
MD
488 printf("%4ju %4ju %3ju ",
489 rate(vmm.v_intr - ovmm.v_intr),
490 rate(vmm.v_syscall - ovmm.v_syscall),
491 rate(vmm.v_swtch - ovmm.v_swtch));
984263bc 492 cpustats();
2e134091
LF
493 printf("\n");
494 fflush(stdout);
984263bc
MD
495 if (reps >= 0 && --reps <= 0)
496 break;
393dfc63 497 ovmm = vmm;
29758f2e
MD
498 usleep(interval * 1000);
499 initial = 0;
984263bc
MD
500 }
501}
502
3c6b4125 503static void
1d1731fa 504printhdr(void)
984263bc
MD
505{
506 int i, num_shown;
507
508 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
2e134091 509 printf(" procs memory page%*s", 19, "");
984263bc 510 if (num_shown > 1)
2e134091 511 printf(" disks %*s", num_shown * 4 - 7, "");
984263bc 512 else if (num_shown == 1)
2e134091
LF
513 printf("disk");
514 printf(" faults cpu\n");
515 printf(" r b w avm fre flt re pi po fr sr ");
984263bc
MD
516 for (i = 0; i < num_devices; i++)
517 if ((dev_select[i].selected)
518 && (dev_select[i].selected <= maxshowdevs))
2e134091 519 printf("%c%c%d ", dev_select[i].device_name[0],
984263bc
MD
520 dev_select[i].device_name[1],
521 dev_select[i].unit_number);
2e134091 522 printf(" in sy cs us sy id\n");
984263bc
MD
523 hdrcnt = winlines - 2;
524}
525
526/*
527 * Force a header to be prepended to the next output.
528 */
3c6b4125
JS
529static void
530needhdr(__unused int signo)
984263bc
MD
531{
532
533 hdrcnt = 1;
534}
535
3c6b4125 536static long
1d1731fa 537pct(long top, long bot)
984263bc
MD
538{
539 long ans;
540
541 if (bot == 0)
542 return(0);
543 ans = (quad_t)top * 100 / bot;
544 return (ans);
545}
546
547#define PCT(top, bot) pct((long)(top), (long)(bot))
548
3c6b4125 549static void
1d1731fa 550dosum(void)
984263bc 551{
24e51f36 552 struct nchstats *nch_tmp, nchstats;
e43bcaac
JG
553 size_t vms_size = sizeof(vms);
554 size_t vmm_size = sizeof(vmm);
24e51f36
HP
555 int cpucnt;
556 u_long nchtotal;
27abc816 557 u_long nchpathtotal;
24e51f36 558 size_t nch_size = sizeof(struct nchstats) * SMP_MAXCPU;
984263bc 559
393dfc63
MD
560 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
561 perror("sysctlbyname: vm.vmstats");
562 exit(1);
563 }
564 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
565 perror("sysctlbyname: vm.vmstats");
566 exit(1);
24e51f36 567 }
2e134091
LF
568 printf("%9u cpu context switches\n", vmm.v_swtch);
569 printf("%9u device interrupts\n", vmm.v_intr);
570 printf("%9u software interrupts\n", vmm.v_soft);
571 printf("%9u traps\n", vmm.v_trap);
572 printf("%9u system calls\n", vmm.v_syscall);
573 printf("%9u kernel threads created\n", vmm.v_kthreads);
574 printf("%9u fork() calls\n", vmm.v_forks);
575 printf("%9u vfork() calls\n", vmm.v_vforks);
576 printf("%9u rfork() calls\n", vmm.v_rforks);
577 printf("%9u exec() calls\n", vmm.v_exec);
578 printf("%9u swap pager pageins\n", vmm.v_swapin);
579 printf("%9u swap pager pages paged in\n", vmm.v_swappgsin);
580 printf("%9u swap pager pageouts\n", vmm.v_swapout);
581 printf("%9u swap pager pages paged out\n", vmm.v_swappgsout);
582 printf("%9u vnode pager pageins\n", vmm.v_vnodein);
583 printf("%9u vnode pager pages paged in\n", vmm.v_vnodepgsin);
584 printf("%9u vnode pager pageouts\n", vmm.v_vnodeout);
585 printf("%9u vnode pager pages paged out\n", vmm.v_vnodepgsout);
586 printf("%9u page daemon wakeups\n", vmm.v_pdwakeups);
587 printf("%9u pages examined by the page daemon\n", vmm.v_pdpages);
588 printf("%9u pages reactivated\n", vmm.v_reactivated);
589 printf("%9u copy-on-write faults\n", vmm.v_cow_faults);
590 printf("%9u copy-on-write optimized faults\n", vmm.v_cow_optim);
591 printf("%9u zero fill pages zeroed\n", vmm.v_zfod);
592 printf("%9u zero fill pages prezeroed\n", vmm.v_ozfod);
593 printf("%9u intransit blocking page faults\n", vmm.v_intrans);
594 printf("%9u total VM faults taken\n", vmm.v_vm_faults);
595 printf("%9u pages affected by kernel thread creation\n", vmm.v_kthreadpages);
596 printf("%9u pages affected by fork()\n", vmm.v_forkpages);
597 printf("%9u pages affected by vfork()\n", vmm.v_vforkpages);
598 printf("%9u pages affected by rfork()\n", vmm.v_rforkpages);
599 printf("%9u pages freed\n", vmm.v_tfree);
600 printf("%9u pages freed by daemon\n", vmm.v_dfree);
601 printf("%9u pages freed by exiting processes\n", vmm.v_pfree);
602 printf("%9u pages active\n", vms.v_active_count);
603 printf("%9u pages inactive\n", vms.v_inactive_count);
604 printf("%9u pages in VM cache\n", vms.v_cache_count);
605 printf("%9u pages wired down\n", vms.v_wire_count);
606 printf("%9u pages free\n", vms.v_free_count);
607 printf("%9u bytes per page\n", vms.v_page_size);
7d4d6fdb 608 printf("%9u global smp invltlbs\n", vmm.v_smpinvltlb);
24e51f36
HP
609
610 if ((nch_tmp = malloc(nch_size)) == NULL) {
611 perror("malloc");
612 exit(1);
613 } else {
614 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
615 perror("sysctlbyname vfs.cache.nchstats");
616 free(nch_tmp);
617 exit(1);
618 } else {
619 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
620 perror("realloc");
621 exit(1);
622 }
623 }
624 }
625
626 cpucnt = nch_size / sizeof(struct nchstats);
627 kvm_nch_cpuagg(nch_tmp, &nchstats, cpucnt);
628
984263bc
MD
629 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
630 nchstats.ncs_badhits + nchstats.ncs_falsehits +
27abc816
MD
631 nchstats.ncs_miss;
632 nchpathtotal = nchstats.ncs_longhits + nchstats.ncs_longmiss;
633 printf("%9ld total path lookups\n", nchpathtotal);
634 printf("%9ld total component lookups\n", nchtotal);
2e134091 635 printf(
27abc816 636 "%9s cache hits (%ld%% pos + %ld%% neg)\n",
984263bc 637 "", PCT(nchstats.ncs_goodhits, nchtotal),
27abc816
MD
638 PCT(nchstats.ncs_neghits, nchtotal));
639 printf("%9s deletions %ld%%, falsehits %ld%%\n", "",
984263bc 640 PCT(nchstats.ncs_badhits, nchtotal),
27abc816 641 PCT(nchstats.ncs_falsehits, nchtotal));
24e51f36 642 free(nch_tmp);
984263bc
MD
643}
644
645#ifdef notyet
646void
1d1731fa 647doforkst(void)
984263bc
MD
648{
649 struct forkstat fks;
650
651 kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
2e134091 652 printf("%d forks, %d pages, average %.2f\n",
984263bc 653 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
2e134091 654 printf("%d vforks, %d pages, average %.2f\n",
984263bc
MD
655 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
656}
657#endif
658
659static void
1d1731fa 660devstats(void)
984263bc 661{
3c6b4125 662 int dn;
984263bc
MD
663 long double transfers_per_second;
664 long double busy_seconds;
f5d21610
JS
665
666 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
667 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
668 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
669 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
670 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
671 old_cp_time = cp_time;
984263bc
MD
672
673 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
674
675 for (dn = 0; dn < num_devices; dn++) {
676 int di;
677
678 if ((dev_select[dn].selected == 0)
679 || (dev_select[dn].selected > maxshowdevs))
680 continue;
681
682 di = dev_select[dn].position;
683
684 if (compute_stats(&cur.dinfo->devices[di],
685 &last.dinfo->devices[di], busy_seconds,
686 NULL, NULL, NULL,
687 NULL, &transfers_per_second, NULL,
688 NULL, NULL) != 0)
689 errx(1, "%s", devstat_errbuf);
690
691 printf("%3.0Lf ", transfers_per_second);
692 }
693}
694
3c6b4125 695static void
1d1731fa 696cpustats(void)
984263bc 697{
f5d21610 698 uint64_t total;
3c6b4125 699 double totusage;
f5d21610
JS
700
701 total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
702 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
984263bc 703
984263bc 704 if (total)
3c6b4125 705 totusage = 100.0 / total;
984263bc 706 else
3c6b4125
JS
707 totusage = 0;
708 printf("%2.0f ",
709 (diff_cp_time.cp_user + diff_cp_time.cp_nice) * totusage);
710 printf("%2.0f ",
711 (diff_cp_time.cp_sys + diff_cp_time.cp_intr) * totusage);
712 printf("%2.0f",
713 diff_cp_time.cp_idle * totusage);
984263bc
MD
714}
715
3c6b4125 716static void
1d1731fa 717dointr(void)
984263bc 718{
2e134091
LF
719 u_long *intrcnt, uptime;
720 u_int64_t inttotal;
4dfb4f32 721 size_t nintr, inamlen, i, size;
a73fdee8
MD
722 int nwidth;
723 char *intrstr;
724 char **intrname;
984263bc
MD
725
726 uptime = getuptime();
4dfb4f32
SS
727 if (sysctlbyname("hw.intrnames", NULL, &inamlen, NULL, 0) != 0)
728 errx(1, "sysctlbyname");
a73fdee8
MD
729 intrstr = malloc(inamlen);
730 if (intrstr == NULL)
4dfb4f32 731 err(1, "malloc");
a73fdee8
MD
732 sysctlbyname("hw.intrnames", intrstr, &inamlen, NULL, 0);
733 for (nintr = 0, i = 0; i < inamlen; ++i) {
734 if (intrstr[i] == 0)
4dfb4f32 735 nintr++;
a73fdee8
MD
736 }
737 intrname = malloc(nintr * sizeof(char *));
738 for (i = 0; i < nintr; ++i) {
739 intrname[i] = intrstr;
740 intrstr += strlen(intrstr) + 1;
741 }
742
4dfb4f32 743 size = nintr * sizeof(*intrcnt);
53052796 744 intrcnt = calloc(nintr, sizeof(*intrcnt));
4dfb4f32
SS
745 if (intrcnt == NULL)
746 err(1, "malloc");
c83c147e 747 sysctlbyname("hw.intrcnt", intrcnt, &size, NULL, 0);
a73fdee8 748
bba17725 749 nwidth = 21;
a73fdee8
MD
750 for (i = 0; i < nintr; ++i) {
751 if (nwidth < (int)strlen(intrname[i]))
752 nwidth = (int)strlen(intrname[i]);
753 }
c83c147e 754 if (verbose) nwidth += 12;
a73fdee8 755
5be6fe29
TN
756 printf("%-*.*s %11s %10s\n",
757 nwidth, nwidth, "interrupt", "total", "rate");
984263bc 758 inttotal = 0;
a73fdee8 759 for (i = 0; i < nintr; ++i) {
bba17725 760 int named;
5be6fe29 761 char *infop, irqinfo[72];
bba17725
YT
762
763 if ((named = strncmp(intrname[i], "irq", 3)) != 0 ||
764 intrcnt[i] > 0) {
765 infop = intrname[i];
81a75a2f
SZ
766 if (verbose) {
767 ssize_t irq, cpu;
768
769 irq = i % MAX_INTS;
770 cpu = i / MAX_INTS;
771 if (named) {
772 snprintf(irqinfo, sizeof(irqinfo),
773 "irq%-3zd %3zd: %s",
774 irq, cpu, intrname[i]);
775 } else {
776 snprintf(irqinfo, sizeof(irqinfo),
777 "irq%-3zd %3zd: ", irq, cpu);
778 }
bba17725
YT
779 infop = irqinfo;
780 }
a73fdee8 781 printf("%-*.*s %11lu %10lu\n",
bba17725 782 nwidth, nwidth, infop,
a73fdee8 783 intrcnt[i], intrcnt[i] / uptime);
24a1f87d 784 }
a73fdee8 785 inttotal += intrcnt[i];
984263bc 786 }
a73fdee8
MD
787 printf("%-*.*s %11llu %10llu\n",
788 nwidth, nwidth, "Total",
ce70368f 789 (long long)inttotal, (long long)(inttotal / uptime));
984263bc
MD
790}
791
d0ab431a 792#define MAX_KMSTATS 1024
984263bc 793
2c9bc808 794static long
8fec73d5 795cpuagg(size_t *ary)
027557c6
MD
796{
797 int i;
798 long ttl;
799
800 for (i = ttl = 0; i < SMP_MAXCPU; ++i)
801 ttl += ary[i];
802 return(ttl);
803}
804
3c6b4125 805static void
1d1731fa 806domem(void)
984263bc 807{
2e134091
LF
808 struct malloc_type *ks;
809 int i, j;
24e51f36 810 int first, nkms;
984263bc 811 long totuse = 0, totfree = 0, totreq = 0;
984263bc
MD
812 struct malloc_type kmemstats[MAX_KMSTATS], *kmsp;
813 char buf[1024];
984263bc 814
984263bc
MD
815 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp));
816 for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) {
817 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp,
818 &kmemstats[nkms], sizeof(kmemstats[0])))
819 err(1, "kvm_read(%p)", (void *)kmsp);
820 if (sizeof(buf) != kvm_read(kd,
821 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf)))
822 err(1, "kvm_read(%p)",
3c6b4125 823 kmemstats[nkms].ks_shortdesc);
984263bc
MD
824 buf[sizeof(buf) - 1] = '\0';
825 kmemstats[nkms].ks_shortdesc = strdup(buf);
826 kmsp = kmemstats[nkms].ks_next;
827 }
828 if (kmsp != NULL)
829 warnx("truncated to the first %d memory types", nkms);
984263bc 830
2e134091 831 printf(
984263bc 832 "\nMemory statistics by type Type Kern\n");
2e134091 833 printf(
57d87dd0 834" Type InUse MemUse HighUse Limit Requests Limit Limit\n");
984263bc
MD
835 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) {
836 if (ks->ks_calls == 0)
837 continue;
e850a2fd 838 printf("%19s%7ld%7ldK%7ldK%11zuK%10jd%5u%6u",
984263bc 839 ks->ks_shortdesc,
027557c6 840 cpuagg(ks->ks_inuse), (cpuagg(ks->ks_memuse) + 1023) / 1024,
984263bc 841 (ks->ks_maxused + 1023) / 1024,
ce70368f 842 (ks->ks_limit + 1023) / 1024, (intmax_t)ks->ks_calls,
984263bc
MD
843 ks->ks_limblocks, ks->ks_mapblocks);
844 first = 1;
845 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
846 if ((ks->ks_size & j) == 0)
847 continue;
848 if (first)
849 printf(" ");
850 else
851 printf(",");
852 if(j<1024)
853 printf("%d",j);
854 else
855 printf("%dK",j>>10);
856 first = 0;
857 }
858 printf("\n");
027557c6 859 totuse += cpuagg(ks->ks_memuse);
984263bc
MD
860 totreq += ks->ks_calls;
861 }
2e134091
LF
862 printf("\nMemory Totals: In Use Free Requests\n");
863 printf(" %7ldK %6ldK %8ld\n",
984263bc
MD
864 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
865}
866
3c6b4125 867static void
1d1731fa 868dozmem(void)
984263bc
MD
869{
870 char *buf;
871 size_t bufsize;
872
873 buf = NULL;
874 bufsize = 1024;
875 for (;;) {
876 if ((buf = realloc(buf, bufsize)) == NULL)
877 err(1, "realloc()");
eecd52b2 878 if (sysctlbyname("vm.zone", buf, &bufsize, NULL, 0) == 0)
984263bc
MD
879 break;
880 if (errno != ENOMEM)
881 err(1, "sysctl()");
882 bufsize *= 2;
883 }
884 buf[bufsize] = '\0'; /* play it safe */
2e134091 885 printf("%s\n\n", buf);
984263bc
MD
886 free(buf);
887}
888
889/*
890 * kread reads something from the kernel, given its nlist index.
891 */
3c6b4125 892static void
1d1731fa 893kread(int nlx, void *addr, size_t size)
984263bc 894{
f5d21610 895 const char *sym;
984263bc
MD
896
897 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
898 sym = namelist[nlx].n_name;
899 if (*sym == '_')
900 ++sym;
901 errx(1, "symbol %s not defined", sym);
902 }
3c6b4125 903 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) {
984263bc
MD
904 sym = namelist[nlx].n_name;
905 if (*sym == '_')
906 ++sym;
907 errx(1, "%s: %s", sym, kvm_geterr(kd));
908 }
909}
910
3c6b4125 911static void
1d1731fa 912usage(void)
984263bc 913{
2e134091 914 fprintf(stderr, "%s%s",
bba17725 915 "usage: vmstat [-imsvz] [-c count] [-M core] [-N system] [-w wait]\n",
984263bc
MD
916 " [-n devs] [disks]\n");
917 exit(1);
918}