vmstat.c (11d38a5764295585a2472d5e861fa8abe1a11eb2) vmstat.c (0e51ea9106f14e6dbefe3ab4ca36e31e3b124038)
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

--- 62 unchanged lines hidden (view full) ---

71#include <paths.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <sysexits.h>
76#include <time.h>
77#include <unistd.h>
78#include <libutil.h>
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

--- 62 unchanged lines hidden (view full) ---

71#include <paths.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <sysexits.h>
76#include <time.h>
77#include <unistd.h>
78#include <libutil.h>
79#include <libxo/xo.h>
79
80
81#define VMSTAT_XO_VERSION "1"
82
80static char da[] = "da";
81
82static struct nlist namelist[] = {
83#define X_SUM 0
84 { "_vm_cnt" },
85#define X_HZ 1
86 { "_hz" },
87#define X_STATHZ 2

--- 91 unchanged lines hidden (view full) ---

179 int reps;
180 char *memf, *nlistf;
181 char errbuf[_POSIX2_LINE_MAX];
182
183 memf = nlistf = NULL;
184 interval = reps = todo = 0;
185 maxshowdevs = 2;
186 hflag = isatty(1);
83static char da[] = "da";
84
85static struct nlist namelist[] = {
86#define X_SUM 0
87 { "_vm_cnt" },
88#define X_HZ 1
89 { "_hz" },
90#define X_STATHZ 2

--- 91 unchanged lines hidden (view full) ---

182 int reps;
183 char *memf, *nlistf;
184 char errbuf[_POSIX2_LINE_MAX];
185
186 memf = nlistf = NULL;
187 interval = reps = todo = 0;
188 maxshowdevs = 2;
189 hflag = isatty(1);
190
191 argc = xo_parse_args(argc, argv);
192 if (argc < 0)
193 return argc;
194
187 while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) {
188 switch (c) {
189 case 'a':
190 aflag++;
191 break;
192 case 'c':
193 reps = atoi(optarg);
194 break;

--- 20 unchanged lines hidden (view full) ---

215 break;
216 case 'N':
217 nlistf = optarg;
218 break;
219 case 'n':
220 nflag = 1;
221 maxshowdevs = atoi(optarg);
222 if (maxshowdevs < 0)
195 while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:stw:z")) != -1) {
196 switch (c) {
197 case 'a':
198 aflag++;
199 break;
200 case 'c':
201 reps = atoi(optarg);
202 break;

--- 20 unchanged lines hidden (view full) ---

223 break;
224 case 'N':
225 nlistf = optarg;
226 break;
227 case 'n':
228 nflag = 1;
229 maxshowdevs = atoi(optarg);
230 if (maxshowdevs < 0)
223 errx(1, "number of devices %d is < 0",
231 xo_errx(1, "number of devices %d is < 0",
224 maxshowdevs);
225 break;
226 case 'o':
227 todo |= OBJSTAT;
228 break;
229 case 'p':
230 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0)
232 maxshowdevs);
233 break;
234 case 'o':
235 todo |= OBJSTAT;
236 break;
237 case 'p':
238 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0)
231 errx(1, "%s", devstat_errbuf);
239 xo_errx(1, "%s", devstat_errbuf);
232 break;
233 case 's':
234 todo |= SUMSTAT;
235 break;
236 case 't':
237#ifdef notyet
238 todo |= TIMESTAT;
239#else
240 break;
241 case 's':
242 todo |= SUMSTAT;
243 break;
244 case 't':
245#ifdef notyet
246 todo |= TIMESTAT;
247#else
240 errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
248 xo_errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
241#endif
242 break;
243 case 'w':
244 /* Convert to milliseconds. */
245 f = atof(optarg);
246 interval = f * 1000;
247 break;
248 case 'z':
249 todo |= ZMEMSTAT;
250 break;
251 case '?':
252 default:
253 usage();
254 }
255 }
256 argc -= optind;
257 argv += optind;
258
249#endif
250 break;
251 case 'w':
252 /* Convert to milliseconds. */
253 f = atof(optarg);
254 interval = f * 1000;
255 break;
256 case 'z':
257 todo |= ZMEMSTAT;
258 break;
259 case '?':
260 default:
261 usage();
262 }
263 }
264 argc -= optind;
265 argv += optind;
266
267 xo_set_version(VMSTAT_XO_VERSION);
259 if (todo == 0)
260 todo = VMSTAT;
261
262 if (memf != NULL) {
263 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
264 if (kd == NULL)
268 if (todo == 0)
269 todo = VMSTAT;
270
271 if (memf != NULL) {
272 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
273 if (kd == NULL)
265 errx(1, "kvm_openfiles: %s", errbuf);
274 xo_errx(1, "kvm_openfiles: %s", errbuf);
266 }
267
268retry_nlist:
269 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
270 if (c > 0) {
275 }
276
277retry_nlist:
278 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
279 if (c > 0) {
280 int bufsize = 0, len = 0;
281 char *buf, *bp;
271 /*
272 * 'cnt' was renamed to 'vm_cnt'. If 'vm_cnt' is not
273 * found try looking up older 'cnt' symbol.
274 * */
275 if (namelist[X_SUM].n_type == 0 &&
276 strcmp(namelist[X_SUM].n_name, "_vm_cnt") == 0) {
277 namelist[X_SUM].n_name = "_cnt";
278 goto retry_nlist;
279 }
282 /*
283 * 'cnt' was renamed to 'vm_cnt'. If 'vm_cnt' is not
284 * found try looking up older 'cnt' symbol.
285 * */
286 if (namelist[X_SUM].n_type == 0 &&
287 strcmp(namelist[X_SUM].n_name, "_vm_cnt") == 0) {
288 namelist[X_SUM].n_name = "_cnt";
289 goto retry_nlist;
290 }
280 warnx("undefined symbols:");
281 for (c = 0;
282 c < (int)(sizeof(namelist)/sizeof(namelist[0]));
283 c++)
284 if (namelist[c].n_type == 0)
291 for (c = 0;
292 c < (int)(sizeof(namelist)/sizeof(namelist[0]));
293 c++)
294 if (namelist[c].n_type == 0)
285 (void)fprintf(stderr, " %s",
295 bufsize += strlen(namelist[c].n_name) + 1;
296 bufsize += len + 1;
297 buf = bp = alloca(bufsize);
298
299 for (c = 0;
300 c < (int)(sizeof(namelist)/sizeof(namelist[0]));
301 c++)
302 if (namelist[c].n_type == 0) {
303 xo_error(" %s",
286 namelist[c].n_name);
304 namelist[c].n_name);
287 (void)fputc('\n', stderr);
305 len = strlen(namelist[c].n_name);
306 *bp++ = ' ';
307 memcpy(bp, namelist[c].n_name, len);
308 bp += len;
309 }
310 *bp = '\0';
311 xo_error("undefined symbols:\n", buf);
288 } else
312 } else
289 warnx("kvm_nlist: %s", kvm_geterr(kd));
313 xo_warnx("kvm_nlist: %s", kvm_geterr(kd));
314 xo_finish();
290 exit(1);
291 }
292 if (kd && Pflag)
315 exit(1);
316 }
317 if (kd && Pflag)
293 errx(1, "Cannot use -P with crash dumps");
318 xo_errx(1, "Cannot use -P with crash dumps");
294
295 if (todo & VMSTAT) {
296 /*
297 * Make sure that the userland devstat version matches the
298 * kernel devstat version. If not, exit and print a
299 * message informing the user of his mistake.
300 */
301 if (devstat_checkversion(NULL) < 0)
319
320 if (todo & VMSTAT) {
321 /*
322 * Make sure that the userland devstat version matches the
323 * kernel devstat version. If not, exit and print a
324 * message informing the user of his mistake.
325 */
326 if (devstat_checkversion(NULL) < 0)
302 errx(1, "%s", devstat_errbuf);
327 xo_errx(1, "%s", devstat_errbuf);
303
304
305 argv = getdrivedata(argv);
306 }
307
308 if (*argv) {
309 f = atof(*argv);
310 interval = f * 1000;

--- 20 unchanged lines hidden (view full) ---

331#ifdef notyet
332 if (todo & TIMESTAT)
333 dotimes();
334#endif
335 if (todo & INTRSTAT)
336 dointr(interval, reps);
337 if (todo & VMSTAT)
338 dovmstat(interval, reps);
328
329
330 argv = getdrivedata(argv);
331 }
332
333 if (*argv) {
334 f = atof(*argv);
335 interval = f * 1000;

--- 20 unchanged lines hidden (view full) ---

356#ifdef notyet
357 if (todo & TIMESTAT)
358 dotimes();
359#endif
360 if (todo & INTRSTAT)
361 dointr(interval, reps);
362 if (todo & VMSTAT)
363 dovmstat(interval, reps);
364 xo_finish();
339 exit(0);
340}
341
342static int
343mysysctl(const char *name, void *oldp, size_t *oldlenp,
344 void *newp, size_t newlen)
345{
346 int error;
347
348 error = sysctlbyname(name, oldp, oldlenp, newp, newlen);
349 if (error != 0 && errno != ENOMEM)
365 exit(0);
366}
367
368static int
369mysysctl(const char *name, void *oldp, size_t *oldlenp,
370 void *newp, size_t newlen)
371{
372 int error;
373
374 error = sysctlbyname(name, oldp, oldlenp, newp, newlen);
375 if (error != 0 && errno != ENOMEM)
350 err(1, "sysctl(%s)", name);
376 xo_err(1, "sysctl(%s)", name);
351 return (error);
352}
353
354static char **
355getdrivedata(char **argv)
356{
357 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
377 return (error);
378}
379
380static char **
381getdrivedata(char **argv)
382{
383 if ((num_devices = devstat_getnumdevs(NULL)) < 0)
358 errx(1, "%s", devstat_errbuf);
384 xo_errx(1, "%s", devstat_errbuf);
359
360 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
361 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
362
363 if (devstat_getdevs(NULL, &cur) == -1)
385
386 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
387 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
388
389 if (devstat_getdevs(NULL, &cur) == -1)
364 errx(1, "%s", devstat_errbuf);
390 xo_errx(1, "%s", devstat_errbuf);
365
366 num_devices = cur.dinfo->numdevs;
367 generation = cur.dinfo->generation;
368
369 specified_devices = (char **)malloc(sizeof(char *));
370 for (num_devices_specified = 0; *argv; ++argv) {
371 if (isdigit(**argv))
372 break;

--- 15 unchanged lines hidden (view full) ---

388 * them any other random devices in the system so that we get to
389 * maxshowdevs devices, if that many devices exist. If the user
390 * specifies devices on the command line, either through a pattern
391 * match or by naming them explicitly, we will give the user only
392 * those devices.
393 */
394 if ((num_devices_specified == 0) && (num_matches == 0)) {
395 if (devstat_buildmatch(da, &matches, &num_matches) != 0)
391
392 num_devices = cur.dinfo->numdevs;
393 generation = cur.dinfo->generation;
394
395 specified_devices = (char **)malloc(sizeof(char *));
396 for (num_devices_specified = 0; *argv; ++argv) {
397 if (isdigit(**argv))
398 break;

--- 15 unchanged lines hidden (view full) ---

414 * them any other random devices in the system so that we get to
415 * maxshowdevs devices, if that many devices exist. If the user
416 * specifies devices on the command line, either through a pattern
417 * match or by naming them explicitly, we will give the user only
418 * those devices.
419 */
420 if ((num_devices_specified == 0) && (num_matches == 0)) {
421 if (devstat_buildmatch(da, &matches, &num_matches) != 0)
396 errx(1, "%s", devstat_errbuf);
422 xo_errx(1, "%s", devstat_errbuf);
397
398 select_mode = DS_SELECT_ADD;
399 } else
400 select_mode = DS_SELECT_ONLY;
401
402 /*
403 * At this point, selectdevs will almost surely indicate that the
404 * device list has changed, so we don't look for return values of 0
405 * or 1. If we get back -1, though, there is an error.
406 */
407 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
408 &select_generation, generation, cur.dinfo->devices,
409 num_devices, matches, num_matches, specified_devices,
410 num_devices_specified, select_mode,
411 maxshowdevs, 0) == -1)
423
424 select_mode = DS_SELECT_ADD;
425 } else
426 select_mode = DS_SELECT_ONLY;
427
428 /*
429 * At this point, selectdevs will almost surely indicate that the
430 * device list has changed, so we don't look for return values of 0
431 * or 1. If we get back -1, though, there is an error.
432 */
433 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
434 &select_generation, generation, cur.dinfo->devices,
435 num_devices, matches, num_matches, specified_devices,
436 num_devices_specified, select_mode,
437 maxshowdevs, 0) == -1)
412 errx(1, "%s", devstat_errbuf);
438 xo_errx(1, "%s", devstat_errbuf);
413
414 return(argv);
415}
416
417/* Return system uptime in nanoseconds */
418static long long
419getuptime(void)
420{

--- 13 unchanged lines hidden (view full) ---

434
435 *pcpup = NULL;
436
437 if (kd == NULL)
438 return;
439
440 maxcpu = kvm_getmaxcpu(kd);
441 if (maxcpu < 0)
439
440 return(argv);
441}
442
443/* Return system uptime in nanoseconds */
444static long long
445getuptime(void)
446{

--- 13 unchanged lines hidden (view full) ---

460
461 *pcpup = NULL;
462
463 if (kd == NULL)
464 return;
465
466 maxcpu = kvm_getmaxcpu(kd);
467 if (maxcpu < 0)
442 errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));
468 xo_errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));
443
444 pcpu = calloc(maxcpu, sizeof(struct pcpu *));
445 if (pcpu == NULL)
469
470 pcpu = calloc(maxcpu, sizeof(struct pcpu *));
471 if (pcpu == NULL)
446 err(1, "calloc");
472 xo_err(1, "calloc");
447
448 for (i = 0; i < maxcpu; i++) {
449 pcpu[i] = kvm_getpcpu(kd, i);
450 if (pcpu[i] == (struct pcpu *)-1)
473
474 for (i = 0; i < maxcpu; i++) {
475 pcpu[i] = kvm_getpcpu(kd, i);
476 if (pcpu[i] == (struct pcpu *)-1)
451 errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
477 xo_errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
452 }
453
454 *maxcpup = maxcpu;
455 *pcpup = pcpu;
456}
457
458static void
459free_pcpu(struct pcpu **pcpu, int maxcpu)

--- 111 unchanged lines hidden (view full) ---

571 }
572}
573
574static void
575fill_vmtotal(struct vmtotal *vmtp)
576{
577 if (kd != NULL) {
578 /* XXX fill vmtp */
478 }
479
480 *maxcpup = maxcpu;
481 *pcpup = pcpu;
482}
483
484static void
485free_pcpu(struct pcpu **pcpu, int maxcpu)

--- 111 unchanged lines hidden (view full) ---

597 }
598}
599
600static void
601fill_vmtotal(struct vmtotal *vmtp)
602{
603 if (kd != NULL) {
604 /* XXX fill vmtp */
579 errx(1, "not implemented");
605 xo_errx(1, "not implemented");
580 } else {
581 size_t size = sizeof(*vmtp);
582 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0);
583 if (size != sizeof(*vmtp))
606 } else {
607 size_t size = sizeof(*vmtp);
608 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0);
609 if (size != sizeof(*vmtp))
584 errx(1, "vm.total size mismatch");
610 xo_errx(1, "vm.total size mismatch");
585 }
586}
587
588/* Determine how many cpu columns, and what index they are in kern.cp_times */
589static int
590getcpuinfo(u_long *maskp, int *maxidp)
591{
592 int maxcpu;
593 int maxid;
594 int ncpus;
595 int i, j;
596 int empty;
597 size_t size;
598 long *times;
599 u_long mask;
600
601 if (kd != NULL)
611 }
612}
613
614/* Determine how many cpu columns, and what index they are in kern.cp_times */
615static int
616getcpuinfo(u_long *maskp, int *maxidp)
617{
618 int maxcpu;
619 int maxid;
620 int ncpus;
621 int i, j;
622 int empty;
623 size_t size;
624 long *times;
625 u_long mask;
626
627 if (kd != NULL)
602 errx(1, "not implemented");
628 xo_errx(1, "not implemented");
603 mask = 0;
604 ncpus = 0;
605 size = sizeof(maxcpu);
606 mysysctl("kern.smp.maxcpus", &maxcpu, &size, NULL, 0);
607 if (size != sizeof(maxcpu))
629 mask = 0;
630 ncpus = 0;
631 size = sizeof(maxcpu);
632 mysysctl("kern.smp.maxcpus", &maxcpu, &size, NULL, 0);
633 if (size != sizeof(maxcpu))
608 errx(1, "sysctl kern.smp.maxcpus");
634 xo_errx(1, "sysctl kern.smp.maxcpus");
609 size = sizeof(long) * maxcpu * CPUSTATES;
610 times = malloc(size);
611 if (times == NULL)
635 size = sizeof(long) * maxcpu * CPUSTATES;
636 times = malloc(size);
637 if (times == NULL)
612 err(1, "malloc %zd bytes", size);
638 xo_err(1, "malloc %zd bytes", size);
613 mysysctl("kern.cp_times", times, &size, NULL, 0);
614 maxid = (size / CPUSTATES / sizeof(long)) - 1;
615 for (i = 0; i <= maxid; i++) {
616 empty = 1;
617 for (j = 0; empty && j < CPUSTATES; j++) {
618 if (times[i * CPUSTATES + j] != 0)
619 empty = 0;
620 }

--- 6 unchanged lines hidden (view full) ---

627 *maskp = mask;
628 if (maxidp)
629 *maxidp = maxid;
630 return (ncpus);
631}
632
633
634static void
639 mysysctl("kern.cp_times", times, &size, NULL, 0);
640 maxid = (size / CPUSTATES / sizeof(long)) - 1;
641 for (i = 0; i <= maxid; i++) {
642 empty = 1;
643 for (j = 0; empty && j < CPUSTATES; j++) {
644 if (times[i * CPUSTATES + j] != 0)
645 empty = 0;
646 }

--- 6 unchanged lines hidden (view full) ---

653 *maskp = mask;
654 if (maxidp)
655 *maxidp = maxid;
656 return (ncpus);
657}
658
659
660static void
635prthuman(u_int64_t val, int size)
661prthuman(const char *name, u_int64_t val, int size)
636{
637 char buf[10];
638 int flags;
662{
663 char buf[10];
664 int flags;
665 char fmt[128];
639
666
667 snprintf(fmt, sizeof(fmt), "{:%s/%%*s}", name);
668
640 if (size < 5 || size > 9)
669 if (size < 5 || size > 9)
641 errx(1, "doofus");
670 xo_errx(1, "doofus");
642 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
643 humanize_number(buf, size, val, "", HN_AUTOSCALE, flags);
671 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
672 humanize_number(buf, size, val, "", HN_AUTOSCALE, flags);
644 printf("%*s", size, buf);
673 xo_attr("value", "%ju", (uintmax_t) val);
674 xo_emit(fmt, size, buf);
645}
646
647static int hz, hdrcnt;
648
649static long *cur_cp_times;
650static long *last_cp_times;
651static size_t size_cp_times;
652

--- 41 unchanged lines hidden (view full) ---

694 if (!hz)
695 kread(X_HZ, &hz, sizeof(hz));
696 } else {
697 struct clockinfo clockrate;
698
699 size = sizeof(clockrate);
700 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0);
701 if (size != sizeof(clockrate))
675}
676
677static int hz, hdrcnt;
678
679static long *cur_cp_times;
680static long *last_cp_times;
681static size_t size_cp_times;
682

--- 41 unchanged lines hidden (view full) ---

724 if (!hz)
725 kread(X_HZ, &hz, sizeof(hz));
726 } else {
727 struct clockinfo clockrate;
728
729 size = sizeof(clockrate);
730 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0);
731 if (size != sizeof(clockrate))
702 errx(1, "clockrate size mismatch");
732 xo_errx(1, "clockrate size mismatch");
703 hz = clockrate.hz;
704 }
705
706 if (Pflag) {
707 ncpus = getcpuinfo(&cpumask, &maxid);
708 size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
709 cur_cp_times = calloc(1, size_cp_times);
710 last_cp_times = calloc(1, size_cp_times);
711 }
712 for (hdrcnt = 1;;) {
713 if (!--hdrcnt)
714 printhdr(maxid, cpumask);
715 if (kd != NULL) {
716 if (kvm_getcptime(kd, cur.cp_time) < 0)
733 hz = clockrate.hz;
734 }
735
736 if (Pflag) {
737 ncpus = getcpuinfo(&cpumask, &maxid);
738 size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
739 cur_cp_times = calloc(1, size_cp_times);
740 last_cp_times = calloc(1, size_cp_times);
741 }
742 for (hdrcnt = 1;;) {
743 if (!--hdrcnt)
744 printhdr(maxid, cpumask);
745 if (kd != NULL) {
746 if (kvm_getcptime(kd, cur.cp_time) < 0)
717 errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
747 xo_errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
718 } else {
719 size = sizeof(cur.cp_time);
720 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0);
721 if (size != sizeof(cur.cp_time))
748 } else {
749 size = sizeof(cur.cp_time);
750 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0);
751 if (size != sizeof(cur.cp_time))
722 errx(1, "cp_time size mismatch");
752 xo_errx(1, "cp_time size mismatch");
723 }
724 if (Pflag) {
725 size = size_cp_times;
726 mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0);
727 if (size != size_cp_times)
753 }
754 if (Pflag) {
755 size = size_cp_times;
756 mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0);
757 if (size != size_cp_times)
728 errx(1, "cp_times mismatch");
758 xo_errx(1, "cp_times mismatch");
729 }
730
731 tmp_dinfo = last.dinfo;
732 last.dinfo = cur.dinfo;
733 cur.dinfo = tmp_dinfo;
734 last.snap_time = cur.snap_time;
735
736 /*
737 * Here what we want to do is refresh our device stats.
738 * getdevs() returns 1 when the device list has changed.
739 * If the device list has changed, we want to go through
740 * the selection process again, in case a device that we
741 * were previously displaying has gone away.
742 */
743 switch (devstat_getdevs(NULL, &cur)) {
744 case -1:
759 }
760
761 tmp_dinfo = last.dinfo;
762 last.dinfo = cur.dinfo;
763 cur.dinfo = tmp_dinfo;
764 last.snap_time = cur.snap_time;
765
766 /*
767 * Here what we want to do is refresh our device stats.
768 * getdevs() returns 1 when the device list has changed.
769 * If the device list has changed, we want to go through
770 * the selection process again, in case a device that we
771 * were previously displaying has gone away.
772 */
773 switch (devstat_getdevs(NULL, &cur)) {
774 case -1:
745 errx(1, "%s", devstat_errbuf);
775 xo_errx(1, "%s", devstat_errbuf);
746 break;
747 case 1: {
748 int retval;
749
750 num_devices = cur.dinfo->numdevs;
751 generation = cur.dinfo->generation;
752
753 retval = devstat_selectdevs(&dev_select, &num_selected,
754 &num_selections, &select_generation,
755 generation, cur.dinfo->devices,
756 num_devices, matches, num_matches,
757 specified_devices,
758 num_devices_specified, select_mode,
759 maxshowdevs, 0);
760 switch (retval) {
761 case -1:
776 break;
777 case 1: {
778 int retval;
779
780 num_devices = cur.dinfo->numdevs;
781 generation = cur.dinfo->generation;
782
783 retval = devstat_selectdevs(&dev_select, &num_selected,
784 &num_selections, &select_generation,
785 generation, cur.dinfo->devices,
786 num_devices, matches, num_matches,
787 specified_devices,
788 num_devices_specified, select_mode,
789 maxshowdevs, 0);
790 switch (retval) {
791 case -1:
762 errx(1, "%s", devstat_errbuf);
792 xo_errx(1, "%s", devstat_errbuf);
763 break;
764 case 1:
765 printhdr(maxid, cpumask);
766 break;
767 default:
768 break;
769 }
770 }
771 default:
772 break;
773 }
774
775 fill_vmmeter(&sum);
776 fill_vmtotal(&total);
793 break;
794 case 1:
795 printhdr(maxid, cpumask);
796 break;
797 default:
798 break;
799 }
800 }
801 default:
802 break;
803 }
804
805 fill_vmmeter(&sum);
806 fill_vmtotal(&total);
777 (void)printf("%1d %1d %1d",
807 xo_open_container("processes");
808 xo_emit("{:runnable/%1d} {:waiting/%ld} "
809 "{:swapped-out/%ld}",
778 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
810 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
811 xo_close_container("processes");
812 xo_open_container("memory");
779#define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
780#define rate(x) (((x) * rate_adj + halfuptime) / uptime) /* round */
781 if (hflag) {
813#define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10))
814#define rate(x) (((x) * rate_adj + halfuptime) / uptime) /* round */
815 if (hflag) {
782 printf("");
783 prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 5);
784 printf(" ");
785 prthuman(total.t_free * (u_int64_t)sum.v_page_size, 5);
786 printf(" ");
787 (void)printf("%5lu ",
788 (unsigned long)rate(sum.v_vm_faults -
789 osum.v_vm_faults));
816 xo_emit("");
817 prthuman("available-memory",
818 total.t_avm * (u_int64_t)sum.v_page_size, 5);
819 xo_emit(" ");
820 prthuman("free-memory",
821 total.t_free * (u_int64_t)sum.v_page_size, 5);
822 xo_emit(" ");
790 } else {
823 } else {
791 printf(" %7d", vmstat_pgtok(total.t_avm));
792 printf(" %7d ", vmstat_pgtok(total.t_free));
793 (void)printf("%4lu ",
794 (unsigned long)rate(sum.v_vm_faults -
795 osum.v_vm_faults));
824 xo_emit(" ");
825 xo_emit("{:available-memory/%7d}",
826 vmstat_pgtok(total.t_avm));
827 xo_emit(" ");
828 xo_emit("{:free-memory/%7d}",
829 vmstat_pgtok(total.t_free));
796 }
830 }
797 (void)printf("%3lu ",
831 xo_emit("{:total-page-faults/%5lu} ",
832 (unsigned long)rate(sum.v_vm_faults -
833 osum.v_vm_faults));
834 xo_close_container("memory");
835
836 xo_open_container("paging-rates");
837 xo_emit("{:page-reactivated/%3lu} ",
798 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated));
838 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated));
799 (void)printf("%3lu ",
839 xo_emit("{:paged-in/%3lu} ",
800 (unsigned long)rate(sum.v_swapin + sum.v_vnodein -
801 (osum.v_swapin + osum.v_vnodein)));
840 (unsigned long)rate(sum.v_swapin + sum.v_vnodein -
841 (osum.v_swapin + osum.v_vnodein)));
802 (void)printf("%3lu ",
842 xo_emit("{:paged-out/%3lu} ",
803 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout -
804 (osum.v_swapout + osum.v_vnodeout)));
843 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout -
844 (osum.v_swapout + osum.v_vnodeout)));
805 (void)printf("%5lu ",
845 xo_emit("{:freed/%5lu} ",
806 (unsigned long)rate(sum.v_tfree - osum.v_tfree));
846 (unsigned long)rate(sum.v_tfree - osum.v_tfree));
807 (void)printf("%4lu ",
847 xo_emit("{:scanned/%4lu} ",
808 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages));
848 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages));
849 xo_close_container("paging-rates");
850
809 devstats();
851 devstats();
810 (void)printf("%4lu %5lu %5lu",
852 xo_open_container("fault-rates");
853 xo_emit("{:interrupts/%4lu} {:system-calls/%5lu} "
854 "{:context-switches/%5u}",
811 (unsigned long)rate(sum.v_intr - osum.v_intr),
812 (unsigned long)rate(sum.v_syscall - osum.v_syscall),
813 (unsigned long)rate(sum.v_swtch - osum.v_swtch));
855 (unsigned long)rate(sum.v_intr - osum.v_intr),
856 (unsigned long)rate(sum.v_syscall - osum.v_syscall),
857 (unsigned long)rate(sum.v_swtch - osum.v_swtch));
858 xo_close_container("fault-rates");
814 if (Pflag)
815 pcpustats(ncpus, cpumask, maxid);
816 else
817 cpustats();
859 if (Pflag)
860 pcpustats(ncpus, cpumask, maxid);
861 else
862 cpustats();
818 (void)printf("\n");
819 (void)fflush(stdout);
863 xo_emit("\n");
864 xo_flush();
820 if (reps >= 0 && --reps <= 0)
821 break;
822 osum = sum;
823 uptime = interval;
824 rate_adj = 1000;
825 /*
826 * We round upward to avoid losing low-frequency events
827 * (i.e., >= 1 per interval but < 1 per millisecond).

--- 8 unchanged lines hidden (view full) ---

836
837static void
838printhdr(int maxid, u_long cpumask)
839{
840 int i, num_shown;
841
842 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
843 if (hflag) {
865 if (reps >= 0 && --reps <= 0)
866 break;
867 osum = sum;
868 uptime = interval;
869 rate_adj = 1000;
870 /*
871 * We round upward to avoid losing low-frequency events
872 * (i.e., >= 1 per interval but < 1 per millisecond).

--- 8 unchanged lines hidden (view full) ---

881
882static void
883printhdr(int maxid, u_long cpumask)
884{
885 int i, num_shown;
886
887 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
888 if (hflag) {
844 (void)printf("procs memory page%*s ", 19, "");
889 xo_emit("{T:procs} {T:memory} ${T:/page%*s}", 19, "");
845 } else {
890 } else {
846 (void)printf("procs memory page%*s ", 19, "");
891 xo_emit("{T:procs} {T:memory} ${T:/page%*s}", 19, "");
847 }
848 if (num_shown > 1)
892 }
893 if (num_shown > 1)
849 (void)printf(" disks %*s", num_shown * 4 - 7, "");
894 xo_emit(" {T:/disks %*s}", num_shown * 4 - 7, "");
850 else if (num_shown == 1)
895 else if (num_shown == 1)
851 (void)printf(" disk");
852 (void)printf(" faults ");
896 xo_emit(" {T:disks}");
897 xo_emit(" {T:faults} ");
853 if (Pflag) {
854 for (i = 0; i <= maxid; i++) {
855 if (cpumask & (1ul << i))
898 if (Pflag) {
899 for (i = 0; i <= maxid; i++) {
900 if (cpumask & (1ul << i))
856 printf(" cpu%d ", i);
901 xo_emit(" {T:/cpu%d} ", i);
857 }
902 }
858 printf("\n");
903 xo_emit("\n");
859 } else
904 } else
860 printf(" cpu\n");
905 xo_emit(" {T:cpu}\n");
861 if (hflag) {
906 if (hflag) {
862 (void)printf("r b w avm fre flt re pi po fr sr ");
907 xo_emit("{T:r} {T:b} {T:w} {T:avm} {T:fre} {T:flt} {T:re} {T:pi} {T:po} {T:fr} {T:sr} ");
863 } else {
908 } else {
864 (void)printf("r b w avm fre flt re pi po fr sr ");
909 xo_emit("{T:r} {T:b} {T:w} {T:avm} {T:fre} {T:flt} {T:re} {T:pi} {T:po} {T:fr} {T:sr} ");
865 }
866 for (i = 0; i < num_devices; i++)
867 if ((dev_select[i].selected)
868 && (dev_select[i].selected <= maxshowdevs))
910 }
911 for (i = 0; i < num_devices; i++)
912 if ((dev_select[i].selected)
913 && (dev_select[i].selected <= maxshowdevs))
869 (void)printf("%c%c%d ", dev_select[i].device_name[0],
914 xo_emit("{T:/%c%c%d} ", dev_select[i].device_name[0],
870 dev_select[i].device_name[1],
871 dev_select[i].unit_number);
915 dev_select[i].device_name[1],
916 dev_select[i].unit_number);
872 (void)printf(" in sy cs");
917 xo_emit(" {T:in} {T:sy} {T:cs}");
873 if (Pflag) {
874 for (i = 0; i <= maxid; i++) {
875 if (cpumask & (1ul << i))
918 if (Pflag) {
919 for (i = 0; i <= maxid; i++) {
920 if (cpumask & (1ul << i))
876 printf(" us sy id");
921 xo_emit(" {T:us} {T:sy} {T:id}");
877 }
922 }
878 printf("\n");
923 xo_emit("\n");
879 } else
924 } else
880 printf(" us sy id\n");
925 xo_emit(" {T:us} {T:sy} {T:id}\n");
881 if (wresized != 0)
882 doresize();
883 hdrcnt = winlines;
884}
885
886/*
887 * Force a header to be prepended to the next output.
888 */

--- 26 unchanged lines hidden (view full) ---

915 int status;
916 struct winsize w;
917
918 for (;;) {
919 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
920 if (status == -1 && errno == EINTR)
921 continue;
922 else if (status == -1)
926 if (wresized != 0)
927 doresize();
928 hdrcnt = winlines;
929}
930
931/*
932 * Force a header to be prepended to the next output.
933 */

--- 26 unchanged lines hidden (view full) ---

960 int status;
961 struct winsize w;
962
963 for (;;) {
964 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
965 if (status == -1 && errno == EINTR)
966 continue;
967 else if (status == -1)
923 err(1, "ioctl");
968 xo_err(1, "ioctl");
924 if (w.ws_row > 3)
925 winlines = w.ws_row - 3;
926 else
927 winlines = VMSTAT_DEFAULT_LINES;
928 break;
929 }
930
931 /*

--- 6 unchanged lines hidden (view full) ---

938static void
939dotimes(void)
940{
941 unsigned int pgintime, rectime;
942
943 kread(X_REC, &rectime, sizeof(rectime));
944 kread(X_PGIN, &pgintime, sizeof(pgintime));
945 kread(X_SUM, &sum, sizeof(sum));
969 if (w.ws_row > 3)
970 winlines = w.ws_row - 3;
971 else
972 winlines = VMSTAT_DEFAULT_LINES;
973 break;
974 }
975
976 /*

--- 6 unchanged lines hidden (view full) ---

983static void
984dotimes(void)
985{
986 unsigned int pgintime, rectime;
987
988 kread(X_REC, &rectime, sizeof(rectime));
989 kread(X_PGIN, &pgintime, sizeof(pgintime));
990 kread(X_SUM, &sum, sizeof(sum));
946 (void)printf("%u reclaims, %u total time (usec)\n",
991 xo_emit("{:page-reclaims/%u} {N:reclaims}, "
992 "{:reclaim-time/%u} {N:total time (usec)}\n",
947 sum.v_pgrec, rectime);
993 sum.v_pgrec, rectime);
948 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
949 (void)printf("\n");
950 (void)printf("%u page ins, %u total time (msec)\n",
994 xo_emit("{L:average}: {:reclaim-average/%u} {N:usec \\/ reclaim}\n",
995 rectime / sum.v_pgrec);
996 xo_emit("\n");
997 xo_emit("{:page-ins/%u} {N:page ins}, "
998 "{:page-in-time/%u} {N:total time (msec)}\n",
951 sum.v_pgin, pgintime / 10);
999 sum.v_pgin, pgintime / 10);
952 (void)printf("average: %8.1f msec / page in\n",
1000 xo_emit("{L:average}: {:average/%8.1f} {N:msec \\/ page in}\n",
953 pgintime / (sum.v_pgin * 10.0));
954}
955#endif
956
957static long
958pct(long top, long bot)
959{
960 long ans;

--- 8 unchanged lines hidden (view full) ---

969
970static void
971dosum(void)
972{
973 struct nchstats lnchstats;
974 long nchtotal;
975
976 fill_vmmeter(&sum);
1001 pgintime / (sum.v_pgin * 10.0));
1002}
1003#endif
1004
1005static long
1006pct(long top, long bot)
1007{
1008 long ans;

--- 8 unchanged lines hidden (view full) ---

1017
1018static void
1019dosum(void)
1020{
1021 struct nchstats lnchstats;
1022 long nchtotal;
1023
1024 fill_vmmeter(&sum);
977 (void)printf("%9u cpu context switches\n", sum.v_swtch);
978 (void)printf("%9u device interrupts\n", sum.v_intr);
979 (void)printf("%9u software interrupts\n", sum.v_soft);
980 (void)printf("%9u traps\n", sum.v_trap);
981 (void)printf("%9u system calls\n", sum.v_syscall);
982 (void)printf("%9u kernel threads created\n", sum.v_kthreads);
983 (void)printf("%9u fork() calls\n", sum.v_forks);
984 (void)printf("%9u vfork() calls\n", sum.v_vforks);
985 (void)printf("%9u rfork() calls\n", sum.v_rforks);
986 (void)printf("%9u swap pager pageins\n", sum.v_swapin);
987 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
988 (void)printf("%9u swap pager pageouts\n", sum.v_swapout);
989 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
990 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
991 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
992 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
993 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
994 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups);
995 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages);
996 (void)printf("%9u pages reactivated\n", sum.v_reactivated);
997 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
998 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim);
999 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod);
1000 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod);
1001 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
1002 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
1003 (void)printf("%9u page faults requiring I/O\n", sum.v_io_faults);
1004 (void)printf("%9u pages affected by kernel thread creation\n",
1005 sum.v_kthreadpages);
1006 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages);
1007 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages);
1008 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages);
1009 (void)printf("%9u pages cached\n", sum.v_tcached);
1010 (void)printf("%9u pages freed\n", sum.v_tfree);
1011 (void)printf("%9u pages freed by daemon\n", sum.v_dfree);
1012 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
1013 (void)printf("%9u pages active\n", sum.v_active_count);
1014 (void)printf("%9u pages inactive\n", sum.v_inactive_count);
1015 (void)printf("%9u pages in VM cache\n", sum.v_cache_count);
1016 (void)printf("%9u pages wired down\n", sum.v_wire_count);
1017 (void)printf("%9u pages free\n", sum.v_free_count);
1018 (void)printf("%9u bytes per page\n", sum.v_page_size);
1025 xo_open_container("summary-statistics");
1026 xo_emit("{:context-switches/%9u} {N:cpu context switches}\n",
1027 sum.v_swtch);
1028 xo_emit("{:interrupts/%9u} {N:device interrupts}\n",
1029 sum.v_intr);
1030 xo_emit("{:software-interrupts/%9u} {N:software interrupts}\n",
1031 sum.v_soft);
1032 xo_emit("{:traps/%9u} {N:traps}\n", sum.v_trap);
1033 xo_emit("{:system-calls/%9u} {N:system calls}\n",
1034 sum.v_syscall);
1035 xo_emit("{:kernel-threads/%9u} {N:kernel threads created}\n",
1036 sum.v_kthreads);
1037 xo_emit("{:forks/%9u} {N: fork() calls}\n", sum.v_forks);
1038 xo_emit("{:vforks/%9u} {N:vfork() calls}\n",
1039 sum.v_vforks);
1040 xo_emit("{:rforks/%9u} {N:rfork() calls}\n",
1041 sum.v_rforks);
1042 xo_emit("{:swap-ins/%9u} {N:swap pager pageins}\n",
1043 sum.v_swapin);
1044 xo_emit("{:swap-in-pages/%9u} {N:swap pager pages paged in}\n",
1045 sum.v_swappgsin);
1046 xo_emit("{:swap-outs/%9u} {N:swap pager pageouts}\n",
1047 sum.v_swapout);
1048 xo_emit("{:swap-out-pages/%9u} {N:swap pager pages paged out}\n",
1049 sum.v_swappgsout);
1050 xo_emit("{:vnode-page-ins/%9u} {N:vnode pager pageins}\n",
1051 sum.v_vnodein);
1052 xo_emit("{:vnode-page-in-pages/%9u} {N:vnode pager pages paged in}\n",
1053 sum.v_vnodepgsin);
1054 xo_emit("{:vnode-page-outs/%9u} {N:vnode pager pageouts}\n",
1055 sum.v_vnodeout);
1056 xo_emit("{:vnode-page-outs/%9u} {N:vnode pager pages paged out}\n",
1057 sum.v_vnodepgsout);
1058 xo_emit("{:page-daemon-wakeups/%9u} {N:page daemon wakeups}\n",
1059 sum.v_pdwakeups);
1060 xo_emit("{:page-daemon-pages/%9u} {N:pages examined by the page daemon}\n",
1061 sum.v_pdpages);
1062 xo_emit("{:reactivated/%9u} {N:pages reactivated}\n",
1063 sum.v_reactivated);
1064 xo_emit("{:copy-on-write-faults/%9u} {N:copy-on-write faults}\n",
1065 sum.v_cow_faults);
1066 xo_emit("{:copy-on-write-optimized-faults/%9u} {N:copy-on-write optimized faults}\n",
1067 sum.v_cow_optim);
1068 xo_emit("{:zero-fill-pages/%9u} {N:zero fill pages zeroed}\n",
1069 sum.v_zfod);
1070 xo_emit("{:zero-fill-prezeroed/%9u} {N:zero fill pages prezeroed}\n",
1071 sum.v_ozfod);
1072 xo_emit("{:intransit-blocking/%9u} {N:intransit blocking page faults}\n",
1073 sum.v_intrans);
1074 xo_emit("{:total-faults/%9u} {N:total VM faults taken}\n",
1075 sum.v_vm_faults);
1076 xo_emit("{:faults-requiring-io/%9u} {N:page faults requiring I\\/O}\n",
1077 sum.v_io_faults);
1078 xo_emit("{:faults-from-thread-creation/%9u} {N:pages affected by kernel thread creation}\n",
1079 sum.v_kthreadpages);
1080 xo_emit("{:faults-from-fork/%9u} {N:pages affected by fork}()\n",
1081 sum.v_forkpages);
1082 xo_emit("{:faults-from-vfork/%9u} {N:pages affected by vfork}()\n",
1083 sum.v_vforkpages);
1084 xo_emit("{:pages-rfork/%9u} {N:pages affected by rfork}()\n",
1085 sum.v_rforkpages);
1086 xo_emit("{:pages-total-cached/%9u} {N:pages cached}\n",
1087 sum.v_tcached);
1088 xo_emit("{:pages-freed/%9u} {N:pages freed}\n",
1089 sum.v_tfree);
1090 xo_emit("{:pages-freed-by-daemon/%9u} {N:pages freed by daemon}\n",
1091 sum.v_dfree);
1092 xo_emit("{:pages-freed-on-exit/%9u} {N:pages freed by exiting processes}\n",
1093 sum.v_pfree);
1094 xo_emit("{:active-pages/%9u} {N:pages active}\n",
1095 sum.v_active_count);
1096 xo_emit("{:inactive-pages/%9u} {N:pages inactive}\n",
1097 sum.v_inactive_count);
1098 xo_emit("{:vm-cache/%9u} {N:pages in VM cache}\n",
1099 sum.v_cache_count);
1100 xo_emit("{:wired-pages/%9u} {N:pages wired down}\n",
1101 sum.v_wire_count);
1102 xo_emit("{:free-pages/%9u} {N:pages free}\n",
1103 sum.v_free_count);
1104 xo_emit("{:bytes-per-page/%9u} {N:bytes per page}\n", sum.v_page_size);
1019 if (kd != NULL) {
1020 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
1021 } else {
1022 size_t size = sizeof(lnchstats);
1023 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0);
1024 if (size != sizeof(lnchstats))
1105 if (kd != NULL) {
1106 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
1107 } else {
1108 size_t size = sizeof(lnchstats);
1109 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0);
1110 if (size != sizeof(lnchstats))
1025 errx(1, "vfs.cache.nchstats size mismatch");
1111 xo_errx(1, "vfs.cache.nchstats size mismatch");
1026 }
1027 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
1028 lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
1029 lnchstats.ncs_miss + lnchstats.ncs_long;
1112 }
1113 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
1114 lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
1115 lnchstats.ncs_miss + lnchstats.ncs_long;
1030 (void)printf("%9ld total name lookups\n", nchtotal);
1031 (void)printf(
1032 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
1116 xo_emit(":total-name-lookups/%9ld} {N:total name lookups}\n",
1117 nchtotal);
1118 xo_emit("{P:/%9s} {N:cache hits} "
1119 "({:positive-cache-hits/%ld}% pos + "
1120 "{:negative-cache-hits/%ld}% {N:neg}) "
1121 "system {:cache-hit-percent/%ld}% per-directory\n",
1033 "", PCT(lnchstats.ncs_goodhits, nchtotal),
1034 PCT(lnchstats.ncs_neghits, nchtotal),
1035 PCT(lnchstats.ncs_pass2, nchtotal));
1122 "", PCT(lnchstats.ncs_goodhits, nchtotal),
1123 PCT(lnchstats.ncs_neghits, nchtotal),
1124 PCT(lnchstats.ncs_pass2, nchtotal));
1036 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
1125 xo_emit("{P:/%9s} {L:deletions} {:deletions/%ld}%, "
1126 "{L:falsehits} {:false-hits/%ld}%, "
1127 "{L:toolong} {:too-long/%ld}%\n", "",
1037 PCT(lnchstats.ncs_badhits, nchtotal),
1038 PCT(lnchstats.ncs_falsehits, nchtotal),
1039 PCT(lnchstats.ncs_long, nchtotal));
1128 PCT(lnchstats.ncs_badhits, nchtotal),
1129 PCT(lnchstats.ncs_falsehits, nchtotal),
1130 PCT(lnchstats.ncs_long, nchtotal));
1131 xo_close_container("summary-statistics");
1040}
1041
1042static void
1043doforkst(void)
1044{
1045 fill_vmmeter(&sum);
1132}
1133
1134static void
1135doforkst(void)
1136{
1137 fill_vmmeter(&sum);
1046 (void)printf("%u forks, %u pages, average %.2f\n",
1138 xo_open_container("fork-statistics");
1139 xo_emit("{:fork/%u} {N:forks}, {:fork-pages/%u} {N:pages}, "
1140 "{L:average} {:fork-average/%.2f}\n",
1047 sum.v_forks, sum.v_forkpages,
1048 sum.v_forks == 0 ? 0.0 :
1049 (double)sum.v_forkpages / sum.v_forks);
1141 sum.v_forks, sum.v_forkpages,
1142 sum.v_forks == 0 ? 0.0 :
1143 (double)sum.v_forkpages / sum.v_forks);
1050 (void)printf("%u vforks, %u pages, average %.2f\n",
1144 xo_emit("{:vfork/%u} {N:vforks}, {:vfork-pages/%u} {N:pages}, "
1145 "{L:average} {:vfork-average/%.2f}\n",
1051 sum.v_vforks, sum.v_vforkpages,
1052 sum.v_vforks == 0 ? 0.0 :
1053 (double)sum.v_vforkpages / sum.v_vforks);
1146 sum.v_vforks, sum.v_vforkpages,
1147 sum.v_vforks == 0 ? 0.0 :
1148 (double)sum.v_vforkpages / sum.v_vforks);
1054 (void)printf("%u rforks, %u pages, average %.2f\n",
1149 xo_emit("{:rfork/%u} {N:rforks}, {:rfork-pages/%u} {N:pages}, "
1150 "{L:average} {:rfork-average/%.2f}\n",
1055 sum.v_rforks, sum.v_rforkpages,
1056 sum.v_rforks == 0 ? 0.0 :
1057 (double)sum.v_rforkpages / sum.v_rforks);
1151 sum.v_rforks, sum.v_rforkpages,
1152 sum.v_rforks == 0 ? 0.0 :
1153 (double)sum.v_rforkpages / sum.v_rforks);
1154 xo_close_container("fork-statistics");
1058}
1059
1060static void
1061devstats(void)
1062{
1063 int dn, state;
1064 long double transfers_per_second;
1065 long double busy_seconds;
1066 long tmp;
1067
1068 for (state = 0; state < CPUSTATES; ++state) {
1069 tmp = cur.cp_time[state];
1070 cur.cp_time[state] -= last.cp_time[state];
1071 last.cp_time[state] = tmp;
1072 }
1073
1074 busy_seconds = cur.snap_time - last.snap_time;
1075
1155}
1156
1157static void
1158devstats(void)
1159{
1160 int dn, state;
1161 long double transfers_per_second;
1162 long double busy_seconds;
1163 long tmp;
1164
1165 for (state = 0; state < CPUSTATES; ++state) {
1166 tmp = cur.cp_time[state];
1167 cur.cp_time[state] -= last.cp_time[state];
1168 last.cp_time[state] = tmp;
1169 }
1170
1171 busy_seconds = cur.snap_time - last.snap_time;
1172
1173 xo_open_list("device");
1076 for (dn = 0; dn < num_devices; dn++) {
1077 int di;
1078
1079 if ((dev_select[dn].selected == 0)
1080 || (dev_select[dn].selected > maxshowdevs))
1081 continue;
1082
1083 di = dev_select[dn].position;
1084
1085 if (devstat_compute_statistics(&cur.dinfo->devices[di],
1086 &last.dinfo->devices[di], busy_seconds,
1087 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
1088 DSM_NONE) != 0)
1174 for (dn = 0; dn < num_devices; dn++) {
1175 int di;
1176
1177 if ((dev_select[dn].selected == 0)
1178 || (dev_select[dn].selected > maxshowdevs))
1179 continue;
1180
1181 di = dev_select[dn].position;
1182
1183 if (devstat_compute_statistics(&cur.dinfo->devices[di],
1184 &last.dinfo->devices[di], busy_seconds,
1185 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
1186 DSM_NONE) != 0)
1089 errx(1, "%s", devstat_errbuf);
1187 xo_errx(1, "%s", devstat_errbuf);
1090
1188
1091 (void)printf("%3.0Lf ", transfers_per_second);
1189 xo_open_instance("device");
1190 xo_emit("{ekq:name/%c%c%d}{:transfers/%3.0Lf} ",
1191 dev_select[dn].device_name[0],
1192 dev_select[dn].device_name[1],
1193 dev_select[dn].unit_number,
1194 transfers_per_second);
1195 xo_close_instance("device");
1092 }
1196 }
1197 xo_close_list("device");
1093}
1094
1095static void
1198}
1199
1200static void
1096percent(double pct, int *over)
1201percent(const char *name, double pct, int *over)
1097{
1098 char buf[10];
1202{
1203 char buf[10];
1204 char fmt[128];
1099 int l;
1100
1205 int l;
1206
1207 snprintf(fmt, sizeof(fmt), " {:%s/%%*s}", name);
1101 l = snprintf(buf, sizeof(buf), "%.0f", pct);
1102 if (l == 1 && *over) {
1208 l = snprintf(buf, sizeof(buf), "%.0f", pct);
1209 if (l == 1 && *over) {
1103 printf("%s", buf);
1210 xo_emit(fmt, 1, buf);
1104 (*over)--;
1105 } else
1211 (*over)--;
1212 } else
1106 printf("%2s", buf);
1213 xo_emit(fmt, 2, buf);
1107 if (l > 2)
1108 (*over)++;
1109}
1110
1111static void
1112cpustats(void)
1113{
1114 int state, over;
1115 double lpct, total;
1116
1117 total = 0;
1118 for (state = 0; state < CPUSTATES; ++state)
1119 total += cur.cp_time[state];
1120 if (total)
1121 lpct = 100.0 / total;
1122 else
1123 lpct = 0.0;
1124 over = 0;
1214 if (l > 2)
1215 (*over)++;
1216}
1217
1218static void
1219cpustats(void)
1220{
1221 int state, over;
1222 double lpct, total;
1223
1224 total = 0;
1225 for (state = 0; state < CPUSTATES; ++state)
1226 total += cur.cp_time[state];
1227 if (total)
1228 lpct = 100.0 / total;
1229 else
1230 lpct = 0.0;
1231 over = 0;
1125 printf(" ");
1126 percent((cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * lpct, &over);
1127 printf(" ");
1128 percent((cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * lpct, &over);
1129 printf(" ");
1130 percent(cur.cp_time[CP_IDLE] * lpct, &over);
1232 xo_open_container("cpu-statistics");
1233 percent("user", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * lpct, &over);
1234 percent("system", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * lpct, &over);
1235 percent("idle", cur.cp_time[CP_IDLE] * lpct, &over);
1236 xo_close_container("cpu-statistics");
1131}
1132
1133static void
1134pcpustats(int ncpus, u_long cpumask, int maxid)
1135{
1136 int state, i;
1137 double lpct, total;
1138 long tmp;

--- 7 unchanged lines hidden (view full) ---

1146 tmp = cur_cp_times[i * CPUSTATES + state];
1147 cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i *
1148 CPUSTATES + state];
1149 last_cp_times[i * CPUSTATES + state] = tmp;
1150 }
1151 }
1152
1153 over = 0;
1237}
1238
1239static void
1240pcpustats(int ncpus, u_long cpumask, int maxid)
1241{
1242 int state, i;
1243 double lpct, total;
1244 long tmp;

--- 7 unchanged lines hidden (view full) ---

1252 tmp = cur_cp_times[i * CPUSTATES + state];
1253 cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i *
1254 CPUSTATES + state];
1255 last_cp_times[i * CPUSTATES + state] = tmp;
1256 }
1257 }
1258
1259 over = 0;
1260 xo_open_list("cpu");
1154 for (i = 0; i <= maxid; i++) {
1155 if ((cpumask & (1ul << i)) == 0)
1156 continue;
1261 for (i = 0; i <= maxid; i++) {
1262 if ((cpumask & (1ul << i)) == 0)
1263 continue;
1264 xo_open_instance("cpu");
1265 xo_emit("{ke:name/%d}", i);
1157 total = 0;
1158 for (state = 0; state < CPUSTATES; ++state)
1159 total += cur_cp_times[i * CPUSTATES + state];
1160 if (total)
1161 lpct = 100.0 / total;
1162 else
1163 lpct = 0.0;
1266 total = 0;
1267 for (state = 0; state < CPUSTATES; ++state)
1268 total += cur_cp_times[i * CPUSTATES + state];
1269 if (total)
1270 lpct = 100.0 / total;
1271 else
1272 lpct = 0.0;
1164 printf(" ");
1165 percent((cur_cp_times[i * CPUSTATES + CP_USER] +
1273 percent("user", (cur_cp_times[i * CPUSTATES + CP_USER] +
1166 cur_cp_times[i * CPUSTATES + CP_NICE]) * lpct, &over);
1274 cur_cp_times[i * CPUSTATES + CP_NICE]) * lpct, &over);
1167 printf(" ");
1168 percent((cur_cp_times[i * CPUSTATES + CP_SYS] +
1275 percent("system", (cur_cp_times[i * CPUSTATES + CP_SYS] +
1169 cur_cp_times[i * CPUSTATES + CP_INTR]) * lpct, &over);
1276 cur_cp_times[i * CPUSTATES + CP_INTR]) * lpct, &over);
1170 printf(" ");
1171 percent(cur_cp_times[i * CPUSTATES + CP_IDLE] * lpct, &over);
1277 percent("idle", cur_cp_times[i * CPUSTATES + CP_IDLE] * lpct,
1278 &over);
1279 xo_close_instance("cpu");
1172 }
1280 }
1281 xo_close_list("cpu");
1173}
1174
1175static unsigned int
1176read_intrcnts(unsigned long **intrcnts)
1177{
1178 size_t intrcntlen;
1179
1180 if (kd != NULL) {

--- 23 unchanged lines hidden (view full) ---

1204 unsigned long *intrcnt, *old_intrcnt;
1205 uint64_t inttotal, old_inttotal, total_count, total_rate;
1206 char* intrname;
1207 unsigned int i;
1208
1209 inttotal = 0;
1210 old_inttotal = 0;
1211 intrname = intrnames;
1282}
1283
1284static unsigned int
1285read_intrcnts(unsigned long **intrcnts)
1286{
1287 size_t intrcntlen;
1288
1289 if (kd != NULL) {

--- 23 unchanged lines hidden (view full) ---

1313 unsigned long *intrcnt, *old_intrcnt;
1314 uint64_t inttotal, old_inttotal, total_count, total_rate;
1315 char* intrname;
1316 unsigned int i;
1317
1318 inttotal = 0;
1319 old_inttotal = 0;
1320 intrname = intrnames;
1321 xo_emit("{T:/%-*s} {T:/%20s} {T:/%10s}\n",
1322 (int)istrnamlen, "interrupt", "total", "rate");
1323 xo_open_list("interrupt");
1212 for (i = 0, intrcnt=intrcnts, old_intrcnt=old_intrcnts; i < nintr; i++) {
1213 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag)) {
1214 unsigned long count, rate;
1215
1216 count = *intrcnt - *old_intrcnt;
1217 rate = (count * 1000 + period_ms / 2) / period_ms;
1324 for (i = 0, intrcnt=intrcnts, old_intrcnt=old_intrcnts; i < nintr; i++) {
1325 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag)) {
1326 unsigned long count, rate;
1327
1328 count = *intrcnt - *old_intrcnt;
1329 rate = (count * 1000 + period_ms / 2) / period_ms;
1218 (void)printf("%-*s %20lu %10lu\n", (int)istrnamlen,
1219 intrname, count, rate);
1330 xo_open_instance("interrupt");
1331 xo_emit("{k:name/%-*s} {:total/%20lu} {:rate/%10lu}\n",
1332 (int)istrnamlen, intrname, count, rate);
1333 xo_close_instance("interrupt");
1220 }
1221 intrname += strlen(intrname) + 1;
1222 inttotal += *intrcnt++;
1223 old_inttotal += *old_intrcnt++;
1224 }
1225 total_count = inttotal - old_inttotal;
1226 total_rate = (total_count * 1000 + period_ms / 2) / period_ms;
1334 }
1335 intrname += strlen(intrname) + 1;
1336 inttotal += *intrcnt++;
1337 old_inttotal += *old_intrcnt++;
1338 }
1339 total_count = inttotal - old_inttotal;
1340 total_rate = (total_count * 1000 + period_ms / 2) / period_ms;
1227 (void)printf("%-*s %20" PRIu64 " %10" PRIu64 "\n", (int)istrnamlen,
1228 "Total", total_count, total_rate);
1341 xo_close_list("interrupt");
1342 xo_emit("{L:/%-*s} {:total-interrupts/%20" PRIu64 "} "
1343 "{:total-rate/%10" PRIu64 "}\n", (int)istrnamlen,
1344 "Total", total_count, total_rate);
1229}
1230
1231static void
1232dointr(unsigned int interval, int reps)
1233{
1234 unsigned long *intrcnts;
1235 long long uptime, period_ms;
1236 unsigned long *old_intrcnts = NULL;
1237 size_t clen, inamlen, istrnamlen;
1238 char *intrnames, *intrname;
1239
1240 uptime = getuptime();
1241
1242 /* Get the names of each interrupt source */
1243 if (kd != NULL) {
1244 kread(X_SINTRNAMES, &inamlen, sizeof(inamlen));
1245 if ((intrnames = malloc(inamlen)) == NULL)
1345}
1346
1347static void
1348dointr(unsigned int interval, int reps)
1349{
1350 unsigned long *intrcnts;
1351 long long uptime, period_ms;
1352 unsigned long *old_intrcnts = NULL;
1353 size_t clen, inamlen, istrnamlen;
1354 char *intrnames, *intrname;
1355
1356 uptime = getuptime();
1357
1358 /* Get the names of each interrupt source */
1359 if (kd != NULL) {
1360 kread(X_SINTRNAMES, &inamlen, sizeof(inamlen));
1361 if ((intrnames = malloc(inamlen)) == NULL)
1246 err(1, "malloc()");
1362 xo_err(1, "malloc()");
1247 kread(X_INTRNAMES, intrnames, inamlen);
1248 } else {
1249 for (intrnames = NULL, inamlen = 1024; ; inamlen *= 2) {
1250 if ((intrnames = reallocf(intrnames, inamlen)) == NULL)
1363 kread(X_INTRNAMES, intrnames, inamlen);
1364 } else {
1365 for (intrnames = NULL, inamlen = 1024; ; inamlen *= 2) {
1366 if ((intrnames = reallocf(intrnames, inamlen)) == NULL)
1251 err(1, "reallocf()");
1367 xo_err(1, "reallocf()");
1252 if (mysysctl("hw.intrnames",
1253 intrnames, &inamlen, NULL, 0) == 0)
1254 break;
1255 }
1256 }
1257
1258 /* Determine the length of the longest interrupt name */
1259 intrname = intrnames;
1260 istrnamlen = strlen("interrupt");
1261 while(*intrname != '\0') {
1262 clen = strlen(intrname);
1263 if (clen > istrnamlen)
1264 istrnamlen = clen;
1265 intrname += strlen(intrname) + 1;
1266 }
1368 if (mysysctl("hw.intrnames",
1369 intrnames, &inamlen, NULL, 0) == 0)
1370 break;
1371 }
1372 }
1373
1374 /* Determine the length of the longest interrupt name */
1375 intrname = intrnames;
1376 istrnamlen = strlen("interrupt");
1377 while(*intrname != '\0') {
1378 clen = strlen(intrname);
1379 if (clen > istrnamlen)
1380 istrnamlen = clen;
1381 intrname += strlen(intrname) + 1;
1382 }
1267 (void)printf("%-*s %20s %10s\n", (int)istrnamlen, "interrupt", "total",
1268 "rate");
1383 xo_emit("%{T:/%-%s} {T:/%20s} {T:/%10s\n",
1384 (int)istrnamlen, "interrupt", "total", "rate");
1269
1270 /*
1271 * Loop reps times printing differential interrupt counts. If reps is
1272 * zero, then run just once, printing total counts
1273 */
1274 period_ms = uptime / 1000000;
1275 while(1) {
1276 unsigned int nintr;
1277 long long old_uptime;
1278
1279 nintr = read_intrcnts(&intrcnts);
1280 /*
1281 * Initialize old_intrcnts to 0 for the first pass, so
1282 * print_intrcnts will print total interrupts since boot
1283 */
1284 if (old_intrcnts == NULL) {
1285 old_intrcnts = calloc(nintr, sizeof(unsigned long));
1286 if (old_intrcnts == NULL)
1385
1386 /*
1387 * Loop reps times printing differential interrupt counts. If reps is
1388 * zero, then run just once, printing total counts
1389 */
1390 period_ms = uptime / 1000000;
1391 while(1) {
1392 unsigned int nintr;
1393 long long old_uptime;
1394
1395 nintr = read_intrcnts(&intrcnts);
1396 /*
1397 * Initialize old_intrcnts to 0 for the first pass, so
1398 * print_intrcnts will print total interrupts since boot
1399 */
1400 if (old_intrcnts == NULL) {
1401 old_intrcnts = calloc(nintr, sizeof(unsigned long));
1402 if (old_intrcnts == NULL)
1287 err(1, "calloc()");
1403 xo_err(1, "calloc()");
1288 }
1289
1290 print_intrcnts(intrcnts, old_intrcnts, intrnames, nintr,
1291 istrnamlen, period_ms);
1292
1293 free(old_intrcnts);
1294 old_intrcnts = intrcnts;
1295 if (reps >= 0 && --reps <= 0)

--- 9 unchanged lines hidden (view full) ---

1305domemstat_malloc(void)
1306{
1307 struct memory_type_list *mtlp;
1308 struct memory_type *mtp;
1309 int error, first, i;
1310
1311 mtlp = memstat_mtl_alloc();
1312 if (mtlp == NULL) {
1404 }
1405
1406 print_intrcnts(intrcnts, old_intrcnts, intrnames, nintr,
1407 istrnamlen, period_ms);
1408
1409 free(old_intrcnts);
1410 old_intrcnts = intrcnts;
1411 if (reps >= 0 && --reps <= 0)

--- 9 unchanged lines hidden (view full) ---

1421domemstat_malloc(void)
1422{
1423 struct memory_type_list *mtlp;
1424 struct memory_type *mtp;
1425 int error, first, i;
1426
1427 mtlp = memstat_mtl_alloc();
1428 if (mtlp == NULL) {
1313 warn("memstat_mtl_alloc");
1429 xo_warn("memstat_mtl_alloc");
1314 return;
1315 }
1316 if (kd == NULL) {
1317 if (memstat_sysctl_malloc(mtlp, 0) < 0) {
1430 return;
1431 }
1432 if (kd == NULL) {
1433 if (memstat_sysctl_malloc(mtlp, 0) < 0) {
1318 warnx("memstat_sysctl_malloc: %s",
1434 xo_warnx("memstat_sysctl_malloc: %s",
1319 memstat_strerror(memstat_mtl_geterror(mtlp)));
1320 return;
1321 }
1322 } else {
1323 if (memstat_kvm_malloc(mtlp, kd) < 0) {
1324 error = memstat_mtl_geterror(mtlp);
1325 if (error == MEMSTAT_ERROR_KVM)
1435 memstat_strerror(memstat_mtl_geterror(mtlp)));
1436 return;
1437 }
1438 } else {
1439 if (memstat_kvm_malloc(mtlp, kd) < 0) {
1440 error = memstat_mtl_geterror(mtlp);
1441 if (error == MEMSTAT_ERROR_KVM)
1326 warnx("memstat_kvm_malloc: %s",
1442 xo_warnx("memstat_kvm_malloc: %s",
1327 kvm_geterr(kd));
1328 else
1443 kvm_geterr(kd));
1444 else
1329 warnx("memstat_kvm_malloc: %s",
1445 xo_warnx("memstat_kvm_malloc: %s",
1330 memstat_strerror(error));
1331 }
1332 }
1446 memstat_strerror(error));
1447 }
1448 }
1333 printf("%13s %5s %6s %7s %8s Size(s)\n", "Type", "InUse", "MemUse",
1334 "HighUse", "Requests");
1449 xo_emit("{T:/%13s} {T:/%5s} {T:/%6s} {T:/%7s} {T:/%8s} {T:Size(s)}\n",
1450 "Type", "InUse", "MemUse", "HighUse", "Requests");
1451 xo_open_list("memory");
1335 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1336 mtp = memstat_mtl_next(mtp)) {
1337 if (memstat_get_numallocs(mtp) == 0 &&
1338 memstat_get_count(mtp) == 0)
1339 continue;
1452 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1453 mtp = memstat_mtl_next(mtp)) {
1454 if (memstat_get_numallocs(mtp) == 0 &&
1455 memstat_get_count(mtp) == 0)
1456 continue;
1340 printf("%13s %5" PRIu64 " %5" PRIu64 "K %7s %8" PRIu64 " ",
1457 xo_open_instance("memory");
1458 xo_emit("{k:type/%13s} {:in-use/%5" PRIu64 "} "
1459 "{:memory-use/%5" PRIu64 "}{U:K} {:high-use/%7s} "
1460 "{:requests/%8" PRIu64 "} ",
1341 memstat_get_name(mtp), memstat_get_count(mtp),
1342 (memstat_get_bytes(mtp) + 1023) / 1024, "-",
1343 memstat_get_numallocs(mtp));
1344 first = 1;
1461 memstat_get_name(mtp), memstat_get_count(mtp),
1462 (memstat_get_bytes(mtp) + 1023) / 1024, "-",
1463 memstat_get_numallocs(mtp));
1464 first = 1;
1465 xo_open_list("size");
1345 for (i = 0; i < 32; i++) {
1346 if (memstat_get_sizemask(mtp) & (1 << i)) {
1347 if (!first)
1466 for (i = 0; i < 32; i++) {
1467 if (memstat_get_sizemask(mtp) & (1 << i)) {
1468 if (!first)
1348 printf(",");
1349 printf("%d", 1 << (i + 4));
1469 xo_emit(",");
1470 xo_emit("{l:size/%d}", 1 << (i + 4));
1350 first = 0;
1351 }
1352 }
1471 first = 0;
1472 }
1473 }
1353 printf("\n");
1474 xo_close_list("size");
1475 xo_close_instance("memory");
1476 xo_emit("\n");
1354 }
1477 }
1478 xo_close_list("memory");
1355 memstat_mtl_free(mtlp);
1356}
1357
1358static void
1359domemstat_zone(void)
1360{
1361 struct memory_type_list *mtlp;
1362 struct memory_type *mtp;
1363 char name[MEMTYPE_MAXNAME + 1];
1364 int error;
1365
1366 mtlp = memstat_mtl_alloc();
1367 if (mtlp == NULL) {
1479 memstat_mtl_free(mtlp);
1480}
1481
1482static void
1483domemstat_zone(void)
1484{
1485 struct memory_type_list *mtlp;
1486 struct memory_type *mtp;
1487 char name[MEMTYPE_MAXNAME + 1];
1488 int error;
1489
1490 mtlp = memstat_mtl_alloc();
1491 if (mtlp == NULL) {
1368 warn("memstat_mtl_alloc");
1492 xo_warn("memstat_mtl_alloc");
1369 return;
1370 }
1371 if (kd == NULL) {
1372 if (memstat_sysctl_uma(mtlp, 0) < 0) {
1493 return;
1494 }
1495 if (kd == NULL) {
1496 if (memstat_sysctl_uma(mtlp, 0) < 0) {
1373 warnx("memstat_sysctl_uma: %s",
1497 xo_warnx("memstat_sysctl_uma: %s",
1374 memstat_strerror(memstat_mtl_geterror(mtlp)));
1375 return;
1376 }
1377 } else {
1378 if (memstat_kvm_uma(mtlp, kd) < 0) {
1379 error = memstat_mtl_geterror(mtlp);
1380 if (error == MEMSTAT_ERROR_KVM)
1498 memstat_strerror(memstat_mtl_geterror(mtlp)));
1499 return;
1500 }
1501 } else {
1502 if (memstat_kvm_uma(mtlp, kd) < 0) {
1503 error = memstat_mtl_geterror(mtlp);
1504 if (error == MEMSTAT_ERROR_KVM)
1381 warnx("memstat_kvm_uma: %s",
1505 xo_warnx("memstat_kvm_uma: %s",
1382 kvm_geterr(kd));
1383 else
1506 kvm_geterr(kd));
1507 else
1384 warnx("memstat_kvm_uma: %s",
1508 xo_warnx("memstat_kvm_uma: %s",
1385 memstat_strerror(error));
1386 }
1387 }
1509 memstat_strerror(error));
1510 }
1511 }
1388 printf("%-20s %6s %6s %8s %8s %8s %4s %4s\n\n", "ITEM", "SIZE",
1389 "LIMIT", "USED", "FREE", "REQ", "FAIL", "SLEEP");
1512 xo_emit("{T:/%-20s} {T:/%6s} {T:/%6s} {T:/%8s} {T:/%8s} {T:/%8s} "
1513 "{T:/%4s} {T:/%4s}\n\n", "ITEM", "SIZE",
1514 "LIMIT", "USED", "FREE", "REQ", "FAIL", "SLEEP");
1515 xo_open_list("zone");
1390 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1391 mtp = memstat_mtl_next(mtp)) {
1392 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME);
1393 strcat(name, ":");
1516 for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1517 mtp = memstat_mtl_next(mtp)) {
1518 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME);
1519 strcat(name, ":");
1394 printf("%-20s %6" PRIu64 ", %6" PRIu64 ",%8" PRIu64 ",%8" PRIu64
1395 ",%8" PRIu64 ",%4" PRIu64 ",%4" PRIu64 "\n", name,
1396 memstat_get_size(mtp), memstat_get_countlimit(mtp),
1397 memstat_get_count(mtp), memstat_get_free(mtp),
1398 memstat_get_numallocs(mtp), memstat_get_failures(mtp),
1399 memstat_get_sleeps(mtp));
1520 xo_open_instance("zone");
1521 xo_emit("{k:name/%-20s} {:size/%6" PRIu64 "}, "
1522 "{:limit/%6" PRIu64 "},{:used/%8" PRIu64 "},"
1523 "{:free/%8" PRIu64 "},{:requests/%8" PRIu64 "},"
1524 "{:fail/%4" PRIu64 "},{:sleep/%4" PRIu64 "}\n", name,
1525 memstat_get_size(mtp), memstat_get_countlimit(mtp),
1526 memstat_get_count(mtp), memstat_get_free(mtp),
1527 memstat_get_numallocs(mtp), memstat_get_failures(mtp),
1528 memstat_get_sleeps(mtp));
1529 xo_close_instance("zone");
1400 }
1401 memstat_mtl_free(mtlp);
1530 }
1531 memstat_mtl_free(mtlp);
1402 printf("\n");
1532 xo_close_list("zone");
1533 xo_emit("\n");
1403}
1404
1405static void
1406display_object(struct kinfo_vmobject *kvo)
1407{
1408 const char *str;
1409
1534}
1535
1536static void
1537display_object(struct kinfo_vmobject *kvo)
1538{
1539 const char *str;
1540
1410 printf("%5jd ", (uintmax_t)kvo->kvo_resident);
1411 printf("%5jd ", (uintmax_t)kvo->kvo_active);
1412 printf("%5jd ", (uintmax_t)kvo->kvo_inactive);
1413 printf("%3d ", kvo->kvo_ref_count);
1414 printf("%3d ", kvo->kvo_shadow_count);
1541 xo_open_instance("object");
1542 xo_emit("{:resident/%5jd} ", (uintmax_t)kvo->kvo_resident);
1543 xo_emit("{:active/%5jd} ", (uintmax_t)kvo->kvo_active);
1544 xo_emit("{:inactive/%5jd} ", (uintmax_t)kvo->kvo_inactive);
1545 xo_emit("{:refcount/%3d} ", kvo->kvo_ref_count);
1546 xo_emit("{:shadowcount/%3d} ", kvo->kvo_shadow_count);
1415 switch (kvo->kvo_memattr) {
1416#ifdef VM_MEMATTR_UNCACHEABLE
1417 case VM_MEMATTR_UNCACHEABLE:
1418 str = "UC";
1419 break;
1420#endif
1421#ifdef VM_MEMATTR_WRITE_COMBINING
1422 case VM_MEMATTR_WRITE_COMBINING:

--- 44 unchanged lines hidden (view full) ---

1467 case VM_MEMATTR_PREFETCHABLE:
1468 str = "PRE";
1469 break;
1470#endif
1471 default:
1472 str = "??";
1473 break;
1474 }
1547 switch (kvo->kvo_memattr) {
1548#ifdef VM_MEMATTR_UNCACHEABLE
1549 case VM_MEMATTR_UNCACHEABLE:
1550 str = "UC";
1551 break;
1552#endif
1553#ifdef VM_MEMATTR_WRITE_COMBINING
1554 case VM_MEMATTR_WRITE_COMBINING:

--- 44 unchanged lines hidden (view full) ---

1599 case VM_MEMATTR_PREFETCHABLE:
1600 str = "PRE";
1601 break;
1602#endif
1603 default:
1604 str = "??";
1605 break;
1606 }
1475 printf("%-3s ", str);
1607 xo_emit("{:attribute/%-3s} ", str);
1476 switch (kvo->kvo_type) {
1477 case KVME_TYPE_NONE:
1478 str = "--";
1479 break;
1480 case KVME_TYPE_DEFAULT:
1481 str = "df";
1482 break;
1483 case KVME_TYPE_VNODE:

--- 14 unchanged lines hidden (view full) ---

1498 case KVME_TYPE_SG:
1499 str = "sg";
1500 break;
1501 case KVME_TYPE_UNKNOWN:
1502 default:
1503 str = "??";
1504 break;
1505 }
1608 switch (kvo->kvo_type) {
1609 case KVME_TYPE_NONE:
1610 str = "--";
1611 break;
1612 case KVME_TYPE_DEFAULT:
1613 str = "df";
1614 break;
1615 case KVME_TYPE_VNODE:

--- 14 unchanged lines hidden (view full) ---

1630 case KVME_TYPE_SG:
1631 str = "sg";
1632 break;
1633 case KVME_TYPE_UNKNOWN:
1634 default:
1635 str = "??";
1636 break;
1637 }
1506 printf("%-2s ", str);
1507 printf("%-s\n", kvo->kvo_path);
1638 xo_emit("{:type/%-2s} ", str);
1639 xo_emit("{:path/%-s}\n", kvo->kvo_path);
1640 xo_close_instance("object");
1508}
1509
1510static void
1511doobjstat(void)
1512{
1513 struct kinfo_vmobject *kvo;
1514 int cnt, i;
1515
1516 kvo = kinfo_getvmobject(&cnt);
1517 if (kvo == NULL) {
1641}
1642
1643static void
1644doobjstat(void)
1645{
1646 struct kinfo_vmobject *kvo;
1647 int cnt, i;
1648
1649 kvo = kinfo_getvmobject(&cnt);
1650 if (kvo == NULL) {
1518 warn("Failed to fetch VM object list");
1651 xo_warn("Failed to fetch VM object list");
1519 return;
1520 }
1652 return;
1653 }
1521 printf("%5s %5s %5s %3s %3s %3s %2s %s\n", "RES", "ACT", "INACT",
1522 "REF", "SHD", "CM", "TP", "PATH");
1654 xo_emit("{T:RES/%5s} {T:ACT/%5s} {T:INACT/%5s} {T:REF/%3s} {T:SHD/%3s} "
1655 "{T:CM/%3s} {T:TP/%2s} {T:PATH/%s}\n");
1656 xo_open_list("object");
1523 for (i = 0; i < cnt; i++)
1524 display_object(&kvo[i]);
1525 free(kvo);
1657 for (i = 0; i < cnt; i++)
1658 display_object(&kvo[i]);
1659 free(kvo);
1660 xo_close_list("object");
1526}
1527
1528/*
1529 * kread reads something from the kernel, given its nlist index.
1530 */
1531static void
1532kreado(int nlx, void *addr, size_t size, size_t offset)
1533{
1534 const char *sym;
1535
1536 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1537 sym = namelist[nlx].n_name;
1538 if (*sym == '_')
1539 ++sym;
1661}
1662
1663/*
1664 * kread reads something from the kernel, given its nlist index.
1665 */
1666static void
1667kreado(int nlx, void *addr, size_t size, size_t offset)
1668{
1669 const char *sym;
1670
1671 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1672 sym = namelist[nlx].n_name;
1673 if (*sym == '_')
1674 ++sym;
1540 errx(1, "symbol %s not defined", sym);
1675 xo_errx(1, "symbol %s not defined", sym);
1541 }
1542 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr,
1543 size) != size) {
1544 sym = namelist[nlx].n_name;
1545 if (*sym == '_')
1546 ++sym;
1676 }
1677 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr,
1678 size) != size) {
1679 sym = namelist[nlx].n_name;
1680 if (*sym == '_')
1681 ++sym;
1547 errx(1, "%s: %s", sym, kvm_geterr(kd));
1682 xo_errx(1, "%s: %s", sym, kvm_geterr(kd));
1548 }
1549}
1550
1551static void
1552kread(int nlx, void *addr, size_t size)
1553{
1554 kreado(nlx, addr, size, 0);
1555}
1556
1557static char *
1558kgetstr(const char *strp)
1559{
1560 int n = 0, size = 1;
1561 char *ret = NULL;
1562
1563 do {
1564 if (size == n + 1) {
1565 ret = realloc(ret, size);
1566 if (ret == NULL)
1683 }
1684}
1685
1686static void
1687kread(int nlx, void *addr, size_t size)
1688{
1689 kreado(nlx, addr, size, 0);
1690}
1691
1692static char *
1693kgetstr(const char *strp)
1694{
1695 int n = 0, size = 1;
1696 char *ret = NULL;
1697
1698 do {
1699 if (size == n + 1) {
1700 ret = realloc(ret, size);
1701 if (ret == NULL)
1567 err(1, "%s: realloc", __func__);
1702 xo_err(1, "%s: realloc", __func__);
1568 size *= 2;
1569 }
1570 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1)
1703 size *= 2;
1704 }
1705 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1)
1571 errx(1, "%s: %s", __func__, kvm_geterr(kd));
1706 xo_errx(1, "%s: %s", __func__, kvm_geterr(kd));
1572 } while (ret[n++] != '\0');
1573 return (ret);
1574}
1575
1576static void
1577usage(void)
1578{
1707 } while (ret[n++] != '\0');
1708 return (ret);
1709}
1710
1711static void
1712usage(void)
1713{
1579 (void)fprintf(stderr, "%s%s",
1714 xo_error("%s%s",
1580 "usage: vmstat [-afHhimoPsz] [-M core [-N system]] [-c count] [-n devs]\n",
1581 " [-p type,if,pass] [-w wait] [disks] [wait [count]]\n");
1715 "usage: vmstat [-afHhimoPsz] [-M core [-N system]] [-c count] [-n devs]\n",
1716 " [-p type,if,pass] [-w wait] [disks] [wait [count]]\n");
1717 xo_finish();
1582 exit(1);
1583}
1718 exit(1);
1719}