17c478bd9Sstevel@tonic-gate /*
2*44c4f64bSJohn Levon * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate */
47c478bd9Sstevel@tonic-gate
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
77c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
87c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
97c478bd9Sstevel@tonic-gate */
107c478bd9Sstevel@tonic-gate
117c478bd9Sstevel@tonic-gate /* from UCB 5.4 5/17/86 */
127c478bd9Sstevel@tonic-gate /* from SunOS 4.1, SID 1.31 */
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate #include <stdio.h>
157c478bd9Sstevel@tonic-gate #include <stdlib.h>
167c478bd9Sstevel@tonic-gate #include <stdarg.h>
177c478bd9Sstevel@tonic-gate #include <ctype.h>
187c478bd9Sstevel@tonic-gate #include <unistd.h>
197c478bd9Sstevel@tonic-gate #include <memory.h>
207c478bd9Sstevel@tonic-gate #include <string.h>
217c478bd9Sstevel@tonic-gate #include <fcntl.h>
227c478bd9Sstevel@tonic-gate #include <errno.h>
237c478bd9Sstevel@tonic-gate #include <signal.h>
247c478bd9Sstevel@tonic-gate #include <values.h>
257c478bd9Sstevel@tonic-gate #include <poll.h>
264944376cSJohn Levon #include <locale.h>
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include "statcommon.h"
297c478bd9Sstevel@tonic-gate
3000c76d6fStc35445 char *cmdname = "vmstat";
3100c76d6fStc35445 int caught_cont = 0;
327c478bd9Sstevel@tonic-gate
3326fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
344944376cSJohn Levon
357c478bd9Sstevel@tonic-gate static int hz;
367c478bd9Sstevel@tonic-gate static int pagesize;
377c478bd9Sstevel@tonic-gate static double etime;
387c478bd9Sstevel@tonic-gate static int lines = 1;
39*44c4f64bSJohn Levon static int swflag = 0, pflag = 0;
407c478bd9Sstevel@tonic-gate static int suppress_state;
417c478bd9Sstevel@tonic-gate static long iter = 0;
4200c76d6fStc35445 static hrtime_t period_n = 0;
437c478bd9Sstevel@tonic-gate static struct snapshot *ss;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate struct iodev_filter df;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define pgtok(a) ((a) * (pagesize >> 10))
487c478bd9Sstevel@tonic-gate #define denom(x) ((x) ? (x) : 1)
497c478bd9Sstevel@tonic-gate #define REPRINT 19
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static void dovmstats(struct snapshot *old, struct snapshot *new);
527c478bd9Sstevel@tonic-gate static void printhdr(int);
537c478bd9Sstevel@tonic-gate static void dosum(struct sys_snapshot *ss);
547c478bd9Sstevel@tonic-gate static void dointr(struct snapshot *ss);
557c478bd9Sstevel@tonic-gate static void usage(void);
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)587c478bd9Sstevel@tonic-gate main(int argc, char **argv)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate struct snapshot *old = NULL;
617c478bd9Sstevel@tonic-gate enum snapshot_types types = SNAP_SYSTEM;
627c478bd9Sstevel@tonic-gate int summary = 0;
637c478bd9Sstevel@tonic-gate int intr = 0;
647c478bd9Sstevel@tonic-gate kstat_ctl_t *kc;
6500c76d6fStc35445 int forever = 0;
6600c76d6fStc35445 hrtime_t start_n;
674944376cSJohn Levon int c;
684944376cSJohn Levon
694944376cSJohn Levon (void) setlocale(LC_ALL, "");
704944376cSJohn Levon #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
714944376cSJohn Levon #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
724944376cSJohn Levon #endif
734944376cSJohn Levon (void) textdomain(TEXT_DOMAIN);
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate pagesize = sysconf(_SC_PAGESIZE);
767c478bd9Sstevel@tonic-gate hz = sysconf(_SC_CLK_TCK);
777c478bd9Sstevel@tonic-gate
78*44c4f64bSJohn Levon while ((c = getopt(argc, argv, "ipqsST:")) != EOF)
794944376cSJohn Levon switch (c) {
807c478bd9Sstevel@tonic-gate case 'S':
817c478bd9Sstevel@tonic-gate swflag = !swflag;
827c478bd9Sstevel@tonic-gate break;
837c478bd9Sstevel@tonic-gate case 's':
847c478bd9Sstevel@tonic-gate summary = 1;
857c478bd9Sstevel@tonic-gate break;
867c478bd9Sstevel@tonic-gate case 'i':
877c478bd9Sstevel@tonic-gate intr = 1;
887c478bd9Sstevel@tonic-gate break;
897c478bd9Sstevel@tonic-gate case 'q':
907c478bd9Sstevel@tonic-gate suppress_state = 1;
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate case 'p':
937c478bd9Sstevel@tonic-gate pflag++; /* detailed paging info */
947c478bd9Sstevel@tonic-gate break;
954944376cSJohn Levon case 'T':
964944376cSJohn Levon if (optarg) {
974944376cSJohn Levon if (*optarg == 'u')
984944376cSJohn Levon timestamp_fmt = UDATE;
994944376cSJohn Levon else if (*optarg == 'd')
1004944376cSJohn Levon timestamp_fmt = DDATE;
1014944376cSJohn Levon else
1024944376cSJohn Levon usage();
1034944376cSJohn Levon } else {
1044944376cSJohn Levon usage();
1054944376cSJohn Levon }
1064944376cSJohn Levon break;
1077c478bd9Sstevel@tonic-gate default:
1087c478bd9Sstevel@tonic-gate usage();
1097c478bd9Sstevel@tonic-gate }
1104944376cSJohn Levon
1114944376cSJohn Levon argc -= optind;
1124944376cSJohn Levon argv += optind;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /* consistency with iostat */
1157c478bd9Sstevel@tonic-gate types |= SNAP_CPUS;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate if (intr)
1187c478bd9Sstevel@tonic-gate types |= SNAP_INTERRUPTS;
1197c478bd9Sstevel@tonic-gate if (!intr)
1207c478bd9Sstevel@tonic-gate types |= SNAP_IODEVS;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /* max to fit in less than 80 characters */
1237c478bd9Sstevel@tonic-gate df.if_max_iodevs = 4;
1247c478bd9Sstevel@tonic-gate df.if_allowed_types = IODEV_DISK;
1257c478bd9Sstevel@tonic-gate df.if_nr_names = 0;
1267c478bd9Sstevel@tonic-gate df.if_names = safe_alloc(df.if_max_iodevs * sizeof (char *));
1277c478bd9Sstevel@tonic-gate (void) memset(df.if_names, 0, df.if_max_iodevs * sizeof (char *));
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate while (argc > 0 && !isdigit(argv[0][0]) &&
1307c478bd9Sstevel@tonic-gate df.if_nr_names < df.if_max_iodevs) {
1317c478bd9Sstevel@tonic-gate df.if_names[df.if_nr_names] = *argv;
1327c478bd9Sstevel@tonic-gate df.if_nr_names++;
1337c478bd9Sstevel@tonic-gate argc--, argv++;
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate kc = open_kstat();
1377c478bd9Sstevel@tonic-gate
13800c76d6fStc35445 start_n = gethrtime();
13900c76d6fStc35445
1407c478bd9Sstevel@tonic-gate ss = acquire_snapshot(kc, types, &df);
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /* time, in seconds, since boot */
1437c478bd9Sstevel@tonic-gate etime = ss->s_sys.ss_ticks / hz;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (intr) {
1467c478bd9Sstevel@tonic-gate dointr(ss);
1477c478bd9Sstevel@tonic-gate free_snapshot(ss);
1487c478bd9Sstevel@tonic-gate exit(0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate if (summary) {
1517c478bd9Sstevel@tonic-gate dosum(&ss->s_sys);
1527c478bd9Sstevel@tonic-gate free_snapshot(ss);
1537c478bd9Sstevel@tonic-gate exit(0);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (argc > 0) {
1577c478bd9Sstevel@tonic-gate long interval;
1587c478bd9Sstevel@tonic-gate char *endptr;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate errno = 0;
1617c478bd9Sstevel@tonic-gate interval = strtol(argv[0], &endptr, 10);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate if (errno > 0 || *endptr != '\0' || interval <= 0 ||
1647c478bd9Sstevel@tonic-gate interval > MAXINT)
1657c478bd9Sstevel@tonic-gate usage();
16600c76d6fStc35445 period_n = (hrtime_t)interval * NANOSEC;
16700c76d6fStc35445 if (period_n <= 0)
1687c478bd9Sstevel@tonic-gate usage();
1697c478bd9Sstevel@tonic-gate iter = MAXLONG;
1707c478bd9Sstevel@tonic-gate if (argc > 1) {
1717c478bd9Sstevel@tonic-gate iter = strtol(argv[1], NULL, 10);
1727c478bd9Sstevel@tonic-gate if (errno > 0 || *endptr != '\0' || iter <= 0)
1737c478bd9Sstevel@tonic-gate usage();
17400c76d6fStc35445 } else
17500c76d6fStc35445 forever = 1;
1767c478bd9Sstevel@tonic-gate if (argc > 2)
1777c478bd9Sstevel@tonic-gate usage();
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate (void) sigset(SIGCONT, printhdr);
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate dovmstats(old, ss);
18300c76d6fStc35445 while (forever || --iter > 0) {
18400c76d6fStc35445 /* (void) poll(NULL, 0, poll_interval); */
18500c76d6fStc35445
18600c76d6fStc35445 /* Have a kip */
18700c76d6fStc35445 sleep_until(&start_n, period_n, forever, &caught_cont);
18800c76d6fStc35445
1897c478bd9Sstevel@tonic-gate free_snapshot(old);
1907c478bd9Sstevel@tonic-gate old = ss;
1917c478bd9Sstevel@tonic-gate ss = acquire_snapshot(kc, types, &df);
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate if (!suppress_state)
1947c478bd9Sstevel@tonic-gate snapshot_report_changes(old, ss);
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /* if config changed, show stats from boot */
1977c478bd9Sstevel@tonic-gate if (snapshot_has_changed(old, ss)) {
1987c478bd9Sstevel@tonic-gate free_snapshot(old);
1997c478bd9Sstevel@tonic-gate old = NULL;
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate dovmstats(old, ss);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate free_snapshot(old);
2067c478bd9Sstevel@tonic-gate free_snapshot(ss);
2077c478bd9Sstevel@tonic-gate free(df.if_names);
2087c478bd9Sstevel@tonic-gate (void) kstat_close(kc);
2097c478bd9Sstevel@tonic-gate return (0);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate #define DELTA(v) (new->v - (old ? old->v : 0))
2137c478bd9Sstevel@tonic-gate #define ADJ(n) ((adj <= 0) ? n : (adj >= n) ? 1 : n - adj)
2147c478bd9Sstevel@tonic-gate #define adjprintf(fmt, n, val) adj -= (n + 1) - printf(fmt, ADJ(n), val)
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate static int adj; /* number of excess columns */
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2197c478bd9Sstevel@tonic-gate static void
show_disk(void * v1,void * v2,void * d)2207c478bd9Sstevel@tonic-gate show_disk(void *v1, void *v2, void *d)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
2237c478bd9Sstevel@tonic-gate struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
2247c478bd9Sstevel@tonic-gate hrtime_t oldtime = new->is_crtime;
2257c478bd9Sstevel@tonic-gate double hr_etime;
2267c478bd9Sstevel@tonic-gate double reads, writes;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate if (new == NULL)
2297c478bd9Sstevel@tonic-gate return;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (old)
2327c478bd9Sstevel@tonic-gate oldtime = old->is_stats.wlastupdate;
2337c478bd9Sstevel@tonic-gate hr_etime = new->is_stats.wlastupdate - oldtime;
2347c478bd9Sstevel@tonic-gate if (hr_etime == 0.0)
2357c478bd9Sstevel@tonic-gate hr_etime = NANOSEC;
2367c478bd9Sstevel@tonic-gate reads = new->is_stats.reads - (old ? old->is_stats.reads : 0);
2377c478bd9Sstevel@tonic-gate writes = new->is_stats.writes - (old ? old->is_stats.writes : 0);
2387c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2, (reads + writes) / hr_etime * NANOSEC);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate static void
dovmstats(struct snapshot * old,struct snapshot * new)2427c478bd9Sstevel@tonic-gate dovmstats(struct snapshot *old, struct snapshot *new)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate kstat_t *oldsys = NULL;
2457c478bd9Sstevel@tonic-gate kstat_t *newsys = &new->s_sys.ss_agg_sys;
2467c478bd9Sstevel@tonic-gate kstat_t *oldvm = NULL;
2477c478bd9Sstevel@tonic-gate kstat_t *newvm = &new->s_sys.ss_agg_vm;
2487c478bd9Sstevel@tonic-gate double percent_factor;
249c65c9cdcSDonghai Qiao ulong_t sys_updates, vm_updates;
2507c478bd9Sstevel@tonic-gate int count;
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate adj = 0;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate if (old) {
2557c478bd9Sstevel@tonic-gate oldsys = &old->s_sys.ss_agg_sys;
2567c478bd9Sstevel@tonic-gate oldvm = &old->s_sys.ss_agg_vm;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate etime = cpu_ticks_delta(oldsys, newsys);
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate percent_factor = 100.0 / denom(etime);
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * If any time has passed, convert etime to seconds per CPU
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate etime = etime >= 1.0 ? (etime / nr_active_cpus(new)) / hz : 1.0;
266c65c9cdcSDonghai Qiao sys_updates = denom(DELTA(s_sys.ss_sysinfo.updates));
267c65c9cdcSDonghai Qiao vm_updates = denom(DELTA(s_sys.ss_vminfo.updates));
2687c478bd9Sstevel@tonic-gate
2694944376cSJohn Levon if (timestamp_fmt != NODATE) {
27026fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(timestamp_fmt);
2714944376cSJohn Levon lines--;
2724944376cSJohn Levon }
2734944376cSJohn Levon
2744944376cSJohn Levon if (--lines <= 0)
2757c478bd9Sstevel@tonic-gate printhdr(0);
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate adj = 0;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate if (pflag) {
2807c478bd9Sstevel@tonic-gate adjprintf(" %*u", 6,
281c65c9cdcSDonghai Qiao pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
282c65c9cdcSDonghai Qiao / vm_updates)));
2837c478bd9Sstevel@tonic-gate adjprintf(" %*u", 5,
284c65c9cdcSDonghai Qiao pgtok((int)(DELTA(s_sys.ss_vminfo.freemem) / vm_updates)));
2857c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "pgrec")
2867c478bd9Sstevel@tonic-gate / etime);
2877c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 3, (kstat_delta(oldvm, newvm, "hat_fault") +
2887c478bd9Sstevel@tonic-gate kstat_delta(oldvm, newvm, "as_fault")) / etime);
2897c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 3, pgtok(kstat_delta(oldvm, newvm, "dfree"))
2907c478bd9Sstevel@tonic-gate / etime);
2917c478bd9Sstevel@tonic-gate adjprintf(" %*ld", 3, pgtok(new->s_sys.ss_deficit));
2927c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "scan")
2937c478bd9Sstevel@tonic-gate / etime);
2947c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
2957c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "execpgin")) / etime);
2967c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
2977c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "execpgout")) / etime);
2987c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
2997c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "execfree")) / etime);
3007c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
3017c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "anonpgin")) / etime);
3027c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
3037c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "anonpgout")) / etime);
3047c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
3057c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "anonfree")) / etime);
3067c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
3077c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "fspgin")) / etime);
3087c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4,
3097c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "fspgout")) / etime);
3107c478bd9Sstevel@tonic-gate adjprintf(" %*.0f\n", 4,
3117c478bd9Sstevel@tonic-gate pgtok(kstat_delta(oldvm, newvm, "fsfree")) / etime);
3127c478bd9Sstevel@tonic-gate (void) fflush(stdout);
3137c478bd9Sstevel@tonic-gate return;
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
316c65c9cdcSDonghai Qiao adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.runque) / sys_updates);
317c65c9cdcSDonghai Qiao adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.waiting) / sys_updates);
318c65c9cdcSDonghai Qiao adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.swpque) / sys_updates);
3197c478bd9Sstevel@tonic-gate adjprintf(" %*u", 6, pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
320c65c9cdcSDonghai Qiao / vm_updates)));
3217c478bd9Sstevel@tonic-gate adjprintf(" %*u", 5, pgtok((int)(DELTA(s_sys.ss_vminfo.freemem)
322c65c9cdcSDonghai Qiao / vm_updates)));
3237c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 3, swflag?
3247c478bd9Sstevel@tonic-gate kstat_delta(oldvm, newvm, "swapin") / etime :
3257c478bd9Sstevel@tonic-gate kstat_delta(oldvm, newvm, "pgrec") / etime);
3267c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 3, swflag?
3277c478bd9Sstevel@tonic-gate kstat_delta(oldvm, newvm, "swapout") / etime :
3287c478bd9Sstevel@tonic-gate (kstat_delta(oldvm, newvm, "hat_fault")
3297c478bd9Sstevel@tonic-gate + kstat_delta(oldvm, newvm, "as_fault"))
3307c478bd9Sstevel@tonic-gate / etime);
3317c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgin"))
3327c478bd9Sstevel@tonic-gate / etime);
3337c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgout"))
3347c478bd9Sstevel@tonic-gate / etime);
3357c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "dfree"))
3367c478bd9Sstevel@tonic-gate / etime);
3377c478bd9Sstevel@tonic-gate adjprintf(" %*ld", 2, pgtok(new->s_sys.ss_deficit));
3387c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2, kstat_delta(oldvm, newvm, "scan") / etime);
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate (void) snapshot_walk(SNAP_IODEVS, old, new, show_disk, NULL);
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate count = df.if_max_iodevs - new->s_nr_iodevs;
3437c478bd9Sstevel@tonic-gate while (count-- > 0)
3447c478bd9Sstevel@tonic-gate adjprintf(" %*d", 2, 0);
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "intr") / etime);
3477c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "syscall") / etime);
3487c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "pswitch") / etime);
3497c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2,
3507c478bd9Sstevel@tonic-gate kstat_delta(oldsys, newsys, "cpu_ticks_user") * percent_factor);
3517c478bd9Sstevel@tonic-gate adjprintf(" %*.0f", 2, kstat_delta(oldsys, newsys, "cpu_ticks_kernel")
3527c478bd9Sstevel@tonic-gate * percent_factor);
3537c478bd9Sstevel@tonic-gate adjprintf(" %*.0f\n", 2, (kstat_delta(oldsys, newsys, "cpu_ticks_idle")
3547c478bd9Sstevel@tonic-gate + kstat_delta(oldsys, newsys, "cpu_ticks_wait"))
3557c478bd9Sstevel@tonic-gate * percent_factor);
3567c478bd9Sstevel@tonic-gate (void) fflush(stdout);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3607c478bd9Sstevel@tonic-gate static void
print_disk(void * v,void * v2,void * d)3617c478bd9Sstevel@tonic-gate print_disk(void *v, void *v2, void *d)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate struct iodev_snapshot *iodev = (struct iodev_snapshot *)v2;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if (iodev == NULL)
3667c478bd9Sstevel@tonic-gate return;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate (void) printf("%c%c ", iodev->is_name[0], iodev->is_name[2]);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate /* ARGSUSED */
3727c478bd9Sstevel@tonic-gate static void
printhdr(int sig)3737c478bd9Sstevel@tonic-gate printhdr(int sig)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate int i = df.if_max_iodevs - ss->s_nr_iodevs;
3767c478bd9Sstevel@tonic-gate
37700c76d6fStc35445 if (sig == SIGCONT)
37800c76d6fStc35445 caught_cont = 1;
37900c76d6fStc35445
3807c478bd9Sstevel@tonic-gate if (pflag) {
3817c478bd9Sstevel@tonic-gate (void) printf(" memory page ");
3827c478bd9Sstevel@tonic-gate (void) printf("executable anonymous filesystem \n");
3837c478bd9Sstevel@tonic-gate (void) printf(" swap free re mf fr de sr ");
3847c478bd9Sstevel@tonic-gate (void) printf("epi epo epf api apo apf fpi fpo fpf\n");
3857c478bd9Sstevel@tonic-gate lines = REPRINT;
3867c478bd9Sstevel@tonic-gate return;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate (void) printf(" kthr memory page ");
3907c478bd9Sstevel@tonic-gate (void) printf("disk faults cpu\n");
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate if (swflag)
3937c478bd9Sstevel@tonic-gate (void) printf(" r b w swap free si so pi po fr de sr ");
3947c478bd9Sstevel@tonic-gate else
3957c478bd9Sstevel@tonic-gate (void) printf(" r b w swap free re mf pi po fr de sr ");
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate (void) snapshot_walk(SNAP_IODEVS, NULL, ss, print_disk, NULL);
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate while (i-- > 0)
4007c478bd9Sstevel@tonic-gate (void) printf("-- ");
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate (void) printf(" in sy cs us sy id\n");
4037c478bd9Sstevel@tonic-gate lines = REPRINT;
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate static void
sum_out(char const * pretty,kstat_t * ks,char * name)4077c478bd9Sstevel@tonic-gate sum_out(char const *pretty, kstat_t *ks, char *name)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate kstat_named_t *ksn = kstat_data_lookup(ks, name);
4107c478bd9Sstevel@tonic-gate if (ksn == NULL) {
4117c478bd9Sstevel@tonic-gate fail(0, "kstat_data_lookup('%s', '%s') failed",
4127c478bd9Sstevel@tonic-gate ks->ks_name, name);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate (void) printf("%9llu %s\n", ksn->value.ui64, pretty);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate static void
dosum(struct sys_snapshot * ss)4197c478bd9Sstevel@tonic-gate dosum(struct sys_snapshot *ss)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate uint64_t total_faults;
4227c478bd9Sstevel@tonic-gate kstat_named_t *ksn;
4237c478bd9Sstevel@tonic-gate long double nchtotal;
4247c478bd9Sstevel@tonic-gate uint64_t nchhits;
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate sum_out("swap ins", &ss->ss_agg_vm, "swapin");
4277c478bd9Sstevel@tonic-gate sum_out("swap outs", &ss->ss_agg_vm, "swapout");
4287c478bd9Sstevel@tonic-gate sum_out("pages swapped in", &ss->ss_agg_vm, "pgswapin");
4297c478bd9Sstevel@tonic-gate sum_out("pages swapped out", &ss->ss_agg_vm, "pgswapout");
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate ksn = kstat_data_lookup(&ss->ss_agg_vm, "hat_fault");
4327c478bd9Sstevel@tonic-gate if (ksn == NULL) {
4337c478bd9Sstevel@tonic-gate fail(0, "kstat_data_lookup('%s', 'hat_fault') failed",
4347c478bd9Sstevel@tonic-gate ss->ss_agg_vm.ks_name);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate total_faults = ksn->value.ui64;
4377c478bd9Sstevel@tonic-gate ksn = kstat_data_lookup(&ss->ss_agg_vm, "as_fault");
4387c478bd9Sstevel@tonic-gate if (ksn == NULL) {
4397c478bd9Sstevel@tonic-gate fail(0, "kstat_data_lookup('%s', 'as_fault') failed",
4407c478bd9Sstevel@tonic-gate ss->ss_agg_vm.ks_name);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate total_faults += ksn->value.ui64;
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate (void) printf("%9llu total address trans. faults taken\n",
4457c478bd9Sstevel@tonic-gate total_faults);
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate sum_out("page ins", &ss->ss_agg_vm, "pgin");
4487c478bd9Sstevel@tonic-gate sum_out("page outs", &ss->ss_agg_vm, "pgout");
4497c478bd9Sstevel@tonic-gate sum_out("pages paged in", &ss->ss_agg_vm, "pgpgin");
4507c478bd9Sstevel@tonic-gate sum_out("pages paged out", &ss->ss_agg_vm, "pgpgout");
4517c478bd9Sstevel@tonic-gate sum_out("total reclaims", &ss->ss_agg_vm, "pgrec");
4527c478bd9Sstevel@tonic-gate sum_out("reclaims from free list", &ss->ss_agg_vm, "pgfrec");
4537c478bd9Sstevel@tonic-gate sum_out("micro (hat) faults", &ss->ss_agg_vm, "hat_fault");
4547c478bd9Sstevel@tonic-gate sum_out("minor (as) faults", &ss->ss_agg_vm, "as_fault");
4557c478bd9Sstevel@tonic-gate sum_out("major faults", &ss->ss_agg_vm, "maj_fault");
4567c478bd9Sstevel@tonic-gate sum_out("copy-on-write faults", &ss->ss_agg_vm, "cow_fault");
4577c478bd9Sstevel@tonic-gate sum_out("zero fill page faults", &ss->ss_agg_vm, "zfod");
4587c478bd9Sstevel@tonic-gate sum_out("pages examined by the clock daemon", &ss->ss_agg_vm, "scan");
4597c478bd9Sstevel@tonic-gate sum_out("revolutions of the clock hand", &ss->ss_agg_vm, "rev");
4607c478bd9Sstevel@tonic-gate sum_out("pages freed by the clock daemon", &ss->ss_agg_vm, "dfree");
4617c478bd9Sstevel@tonic-gate sum_out("forks", &ss->ss_agg_sys, "sysfork");
4627c478bd9Sstevel@tonic-gate sum_out("vforks", &ss->ss_agg_sys, "sysvfork");
4637c478bd9Sstevel@tonic-gate sum_out("execs", &ss->ss_agg_sys, "sysexec");
4647c478bd9Sstevel@tonic-gate sum_out("cpu context switches", &ss->ss_agg_sys, "pswitch");
4657c478bd9Sstevel@tonic-gate sum_out("device interrupts", &ss->ss_agg_sys, "intr");
4667c478bd9Sstevel@tonic-gate sum_out("traps", &ss->ss_agg_sys, "trap");
4677c478bd9Sstevel@tonic-gate sum_out("system calls", &ss->ss_agg_sys, "syscall");
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate nchtotal = (long double) ss->ss_nc.ncs_hits.value.ui64 +
4707c478bd9Sstevel@tonic-gate (long double) ss->ss_nc.ncs_misses.value.ui64;
4717c478bd9Sstevel@tonic-gate nchhits = ss->ss_nc.ncs_hits.value.ui64;
4727c478bd9Sstevel@tonic-gate (void) printf("%9.0Lf total name lookups (cache hits %.0Lf%%)\n",
4737c478bd9Sstevel@tonic-gate nchtotal, nchhits / denom(nchtotal) * 100);
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate sum_out("user cpu", &ss->ss_agg_sys, "cpu_ticks_user");
4767c478bd9Sstevel@tonic-gate sum_out("system cpu", &ss->ss_agg_sys, "cpu_ticks_kernel");
4777c478bd9Sstevel@tonic-gate sum_out("idle cpu", &ss->ss_agg_sys, "cpu_ticks_idle");
4787c478bd9Sstevel@tonic-gate sum_out("wait cpu", &ss->ss_agg_sys, "cpu_ticks_wait");
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate static void
dointr(struct snapshot * ss)4827c478bd9Sstevel@tonic-gate dointr(struct snapshot *ss)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate size_t i;
4857c478bd9Sstevel@tonic-gate ulong_t total = 0;
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate (void) printf("interrupt total rate\n");
4887c478bd9Sstevel@tonic-gate (void) printf("--------------------------------\n");
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_intrs; i++) {
4917c478bd9Sstevel@tonic-gate (void) printf("%-12.8s %10lu %8.0f\n",
4927c478bd9Sstevel@tonic-gate ss->s_intrs[i].is_name, ss->s_intrs[i].is_total,
4937c478bd9Sstevel@tonic-gate ss->s_intrs[i].is_total / etime);
4947c478bd9Sstevel@tonic-gate total += ss->s_intrs[i].is_total;
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate (void) printf("--------------------------------\n");
4987c478bd9Sstevel@tonic-gate (void) printf("Total %10lu %8.0f\n", total, total / etime);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate
5017c478bd9Sstevel@tonic-gate static void
usage(void)5027c478bd9Sstevel@tonic-gate usage(void)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
505*44c4f64bSJohn Levon "Usage: vmstat [-ipqsS] [-T d|u] [disk ...] "
5064944376cSJohn Levon "[interval [count]]\n");
5077c478bd9Sstevel@tonic-gate exit(1);
5087c478bd9Sstevel@tonic-gate }
509