1N/A#!/usr/bin/sh
1N/A#
1N/A# xvmstat - extended vmstat demo in DTrace.
1N/A# Written using DTrace (Solaris 10 3/05).
1N/A#
1N/A# This has been written to demonstrate fetching similar data as vmstat
1N/A# from DTrace, with a few extra fields.
1N/A#
1N/A# $Id: xvmstat 3 2007-08-01 10:50:08Z brendan $
1N/A#
1N/A# USAGE: xvmstat [interval [count]]
1N/A#
1N/A# FIELDS:
1N/A# w swapped out LWPs number
1N/A# swap virtual memory free Mbytes
1N/A# free free RAM Mbytes
1N/A# re page reclaims pages/sec
1N/A# maj major faults pages/sec
1N/A# mf minor faults pages/sec
1N/A# cow copy-on-write faults pages/sec
1N/A# pro protection faults pages/sec
1N/A# sr scan rate pages/sec
1N/A# epi executable page ins pages/sec
1N/A# epo executable page outs pages/sec
1N/A# epf executable frees pages/sec
1N/A# api anonymous page ins pages/sec
1N/A# apo anonymous page outs pages/sec
1N/A# apf anonymous frees pages/sec
1N/A# fpi filesystem page ins pages/sec
1N/A# fpo filesystem page outs pages/sec
1N/A# fpf filesystem frees pages/sec
1N/A#
1N/A# NOTES:
1N/A# - Most of the statistics are in units of pages, unlike the
1N/A# original vmstat command which sometimes uses kilobytes.
1N/A# - As this program does not use Kstat, there is no summary since boot line.
1N/A# - Free RAM is both free free + cache free.
1N/A#
1N/A# SEE ALSO: vmstat(1M)
1N/A#
1N/A# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License, Version 1.0 only
1N/A# (the "License"). You may not use this file except in compliance
1N/A# with the License.
1N/A#
1N/A# You can obtain a copy of the license at Docs/cddl1.txt
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A# 12-Jun-2005 Brendan Gregg Created this.
1N/A# 01-Mar-2006 " " Last update.
1N/A#
1N/A
1N/A##############################
1N/A# --- Process Arguments ---
1N/A#
1N/A
1N/A### default values
1N/Ainterval=1; count=-1
1N/A
1N/A### check arguments
1N/Aif [ "$1" = "-h" -o "$1" = "--help" ]; then
1N/A cat <<-END >&2
1N/A USAGE: xvmstat [interval [count]]
1N/A xvmstat # 1 second samples, infinite
1N/A eg,
1N/A xvmstat 1 # print every 1 second
1N/A xvmstat 5 6 # print every 5 seconds, 6 times
1N/A END
1N/A exit 1
1N/Afi
1N/A
1N/A### argument logic
1N/Aif [ "$1" -gt 0 ]; then
1N/A interval=$1; count=-1; shift
1N/Afi
1N/Aif [ "$1" -gt 0 ]; then
1N/A count=$1; shift
1N/Afi
1N/Aif [ $interval -eq 0 ]; then
1N/A interval=1
1N/Afi
1N/A
1N/A
1N/A#################################
1N/A# --- Main Program, DTrace ---
1N/A#
1N/A/usr/sbin/dtrace -n '
1N/A #pragma D option quiet
1N/A
1N/A /*
1N/A * Command line arguments
1N/A */
1N/A inline int INTERVAL = '$interval';
1N/A inline int COUNTER = '$count';
1N/A inline int SCREEN = 21;
1N/A
1N/A /*
1N/A * Initialise variables
1N/A */
1N/A dtrace:::BEGIN
1N/A {
1N/A re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
1N/A epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
1N/A fpi = 0; fpo = 0; fpf = 0;
1N/A lines = SCREEN + 1;
1N/A counts = COUNTER;
1N/A secs = INTERVAL;
1N/A first = 1;
1N/A }
1N/A
1N/A profile:::tick-1sec
1N/A {
1N/A secs--;
1N/A }
1N/A
1N/A /*
1N/A * Print header
1N/A */
1N/A dtrace:::BEGIN,
1N/A profile:::tick-1sec
1N/A /first || (secs == 0 && lines > SCREEN)/
1N/A {
1N/A printf("%2s %6s %5s %5s %3s %4s %3s %3s %3s ",
1N/A "w", "swap", "free", "re", "maj", "mf", "cow", "pro", "sr");
1N/A printf("%3s %3s %3s %3s %3s %3s %3s %3s %3s\n",
1N/A "epi", "epo", "epf", "api", "apo", "apf", "fpi", "fpo", "fpf");
1N/A lines = 0;
1N/A first = 0;
1N/A }
1N/A
1N/A /*
1N/A * Probe events
1N/A */
1N/A vminfo:::pgrec { re += arg0; }
1N/A vminfo:::scan { sr += arg0; }
1N/A vminfo:::as_fault { mf += arg0; }
1N/A vminfo:::execpgin { epi += arg0; }
1N/A vminfo:::execpgout { epo += arg0; }
1N/A vminfo:::execfree { epf += arg0; }
1N/A vminfo:::anonpgin { api += arg0; }
1N/A vminfo:::anonpgout { apo += arg0; }
1N/A vminfo:::anonfree { apf += arg0; }
1N/A vminfo:::fspgin { fpi += arg0; }
1N/A vminfo:::fspgout { fpo += arg0; }
1N/A vminfo:::fsfree { fpf += arg0; }
1N/A vminfo:::maj_fault { maj += arg0; }
1N/A vminfo:::cow_fault { cow += arg0; }
1N/A vminfo:::prot_fault { pro += arg0; }
1N/A
1N/A /*
1N/A * Print output line
1N/A */
1N/A profile:::tick-1sec
1N/A /secs == 0/
1N/A {
1N/A /* fetch free mem */
1N/A this->free = `freemem;
1N/A
1N/A /*
1N/A * fetch free swap
1N/A *
1N/A * free swap is described in /usr/include/vm/anon.h as,
1N/A * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
1N/A */
1N/A this->ani_max = `k_anoninfo.ani_max;
1N/A this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv;
1N/A this->swap = (this->ani_max - this->ani_resv > 0 ?
1N/A this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree;
1N/A
1N/A /* fetch w */
1N/A this->w = `nswapped;
1N/A
1N/A /* convert to Mbytes */
1N/A this->swap *= `_pagesize; this->swap /= 1048576;
1N/A this->free *= `_pagesize; this->free /= 1048576;
1N/A
1N/A /* convert to per second values */
1N/A re /= INTERVAL; maj /= INTERVAL; mf /= INTERVAL;
1N/A cow /= INTERVAL; pro /= INTERVAL; sr /= INTERVAL;
1N/A epi /= INTERVAL; epo /= INTERVAL; epf /= INTERVAL;
1N/A api /= INTERVAL; apo /= INTERVAL; apf /= INTERVAL;
1N/A fpi /= INTERVAL; fpo /= INTERVAL; fpf /= INTERVAL;
1N/A
1N/A /* print line */
1N/A printf("%2d %6d %5d %5d %3d %4d %3d %3d %3d ",
1N/A this->w, this->swap, this->free, re, maj, mf, cow, pro, sr);
1N/A printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
1N/A epi, epo, epf, api, apo, apf, fpi, fpo, fpf);
1N/A
1N/A /* clear counters */
1N/A re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
1N/A epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
1N/A fpi = 0; fpo = 0; fpf = 0;
1N/A
1N/A /* process counts */
1N/A secs = INTERVAL;
1N/A counts--;
1N/A lines++;
1N/A }
1N/A
1N/A /*
1N/A * End
1N/A */
1N/A profile:::tick-1sec
1N/A /counts == 0/
1N/A {
1N/A exit(0);
1N/A }
1N/A'