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