Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

#!/usr/bin/sh
#
# xvmstat - extended vmstat demo in DTrace.
#           Written using DTrace (Solaris 10 3/05).
#
# This has been written to demonstrate fetching similar data as vmstat
# from DTrace, with a few extra fields.
#
# $Id: xvmstat,v 1.1.1.1 2015/09/30 22:01:07 christos Exp $
#
# USAGE:	xvmstat [interval [count]]
#
# FIELDS: 
#		w	swapped out LWPs	number
#		swap	virtual memory free	Mbytes
#		free	free RAM		Mbytes
#		re	page reclaims		pages/sec
#		maj	major faults		pages/sec
#		mf	minor faults		pages/sec
#		cow	copy-on-write faults	pages/sec
#		pro	protection faults	pages/sec
#		sr	scan rate		pages/sec
#		epi	executable page ins	pages/sec
#		epo	executable page outs	pages/sec
#		epf	executable frees	pages/sec
#		api	anonymous page ins	pages/sec
#		apo	anonymous page outs	pages/sec
#		apf	anonymous frees		pages/sec
#		fpi	filesystem page ins	pages/sec
#		fpo	filesystem page outs	pages/sec
#		fpf	filesystem frees	pages/sec
#
# NOTES: 
# - Most of the statistics are in units of pages, unlike the
#   original vmstat command which sometimes uses kilobytes. 
# - As this program does not use Kstat, there is no summary since boot line.
# - Free RAM is both free free + cache free.
#
# SEE ALSO:	vmstat(1M)
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
# CDDL HEADER START
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
#
# CDDL HEADER END
#
# 12-Jun-2005	Brendan Gregg	Created this.
# 01-Mar-2006	   "      "	Last update.
#

##############################
# --- Process Arguments ---
#

### default values
interval=1; count=-1

### check arguments
if [ "$1" = "-h" -o "$1" = "--help" ]; then
	cat <<-END >&2
	USAGE: xvmstat [interval [count]]
	       xvmstat          # 1 second samples, infinite
	  eg,
	       xvmstat 1        # print every 1 second
	       xvmstat 5 6      # print every 5 seconds, 6 times
	END
	exit 1
fi

### argument logic
if [ "$1" -gt 0 ]; then
        interval=$1; count=-1; shift
fi
if [ "$1" -gt 0 ]; then
        count=$1; shift
fi
if [ $interval -eq 0 ]; then
	interval=1
fi


#################################
# --- Main Program, DTrace ---
#
/usr/sbin/dtrace -n '
 #pragma D option quiet

 /*
  * Command line arguments
  */
 inline int INTERVAL   = '$interval';
 inline int COUNTER    = '$count';
 inline int SCREEN = 21;

 /*
  * Initialise variables
  */
 dtrace:::BEGIN
 {
	re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
	epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
	fpi = 0; fpo = 0; fpf = 0;
	lines = SCREEN + 1;
	counts = COUNTER;
	secs = INTERVAL;
	first = 1;
 }

 profile:::tick-1sec
 {
        secs--;
 }

 /*
  * Print header
  */
 dtrace:::BEGIN,
 profile:::tick-1sec
 /first || (secs == 0 && lines > SCREEN)/
 {
	printf("%2s %6s %5s %5s %3s %4s %3s %3s %3s ",
	    "w", "swap", "free", "re", "maj", "mf", "cow", "pro", "sr");
	printf("%3s %3s %3s %3s %3s %3s %3s %3s %3s\n",
	    "epi", "epo", "epf", "api", "apo", "apf", "fpi", "fpo", "fpf");
	lines = 0;
	first = 0;
 }

 /*
  * Probe events
  */
 vminfo:::pgrec      { re += arg0; }
 vminfo:::scan       { sr += arg0; }
 vminfo:::as_fault   { mf += arg0; }
 vminfo:::execpgin   { epi += arg0; }
 vminfo:::execpgout  { epo += arg0; }
 vminfo:::execfree   { epf += arg0; }
 vminfo:::anonpgin   { api += arg0; }
 vminfo:::anonpgout  { apo += arg0; }
 vminfo:::anonfree   { apf += arg0; }
 vminfo:::fspgin     { fpi += arg0; }
 vminfo:::fspgout    { fpo += arg0; }
 vminfo:::fsfree     { fpf += arg0; }
 vminfo:::maj_fault  { maj += arg0; }
 vminfo:::cow_fault  { cow += arg0; }
 vminfo:::prot_fault { pro += arg0; }

 /* 
  * Print output line
  */
 profile:::tick-1sec
 /secs == 0/
 {
	/* fetch free mem */
	this->free = `freemem;

	/*
	 * fetch free swap
	 *
	 * free swap is described in /usr/include/vm/anon.h as,
	 * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
	 */
	this->ani_max = `k_anoninfo.ani_max;
	this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv;
	this->swap = (this->ani_max - this->ani_resv > 0 ?
	    this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree;

	/* fetch w */
	this->w = `nswapped;

	/* convert to Mbytes */
	this->swap *= `_pagesize; this->swap /= 1048576;
	this->free *= `_pagesize; this->free /= 1048576;

	/* convert to per second values */
	re  /= INTERVAL; maj /= INTERVAL; mf  /= INTERVAL;
	cow /= INTERVAL; pro /= INTERVAL; sr  /= INTERVAL;
	epi /= INTERVAL; epo /= INTERVAL; epf /= INTERVAL;
	api /= INTERVAL; apo /= INTERVAL; apf /= INTERVAL;
	fpi /= INTERVAL; fpo /= INTERVAL; fpf /= INTERVAL;

	/* print line */
	printf("%2d %6d %5d %5d %3d %4d %3d %3d %3d ",
	    this->w, this->swap, this->free, re, maj, mf, cow, pro, sr);
	printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
	    epi, epo, epf, api, apo, apf, fpi, fpo, fpf);

	/* clear counters */
	re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
	epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
	fpi = 0; fpo = 0; fpf = 0;

	/* process counts */
	secs = INTERVAL;
	counts--;
	lines++;
 }

 /*
  * End
  */
 profile:::tick-1sec
 /counts == 0/
 {
	exit(0);
 }
'