1#!/usr/sbin/dtrace -s 2/* 3 * vmstat-p.d - vmstat -p demo in DTrace. 4 * Written using DTrace (Solaris 10 3/05). 5 * 6 * This has been written to demonstrate fetching similar data as vmstat 7 * from DTrace. This program is intended as a starting point for other 8 * DTrace scripts, by beginning with familiar statistics. 9 * 10 * 08-Jan-2006, ver 0.91 11 * 12 * USAGE: vmstat-p.d 13 * 14 * FIELDS: 15 * swap virtual memory free Kbytes 16 * free free RAM Kbytes 17 * re page reclaims Kbytes 18 * mf minor faults Kbytes 19 * sr scan rate pages 20 * epi executable page ins Kbytes 21 * epo executable page outs Kbytes 22 * epf executable frees Kbytes 23 * api anonymous page ins Kbytes 24 * apo anonymous page outs Kbytes 25 * apf anonymous frees Kbytes 26 * fpi filesystem page ins Kbytes 27 * fpo filesystem page outs Kbytes 28 * fpf filesystem frees Kbytes 29 * 30 * NOTES: 31 * Most of the statistics are in units of kilobytes, unlike the 32 * original vmstat command which sometimes uses page counts. 33 * As this program does not use Kstat, there is no summary since 34 * boot line. Free RAM is both free free + cache free. 35 * 36 * SEE ALSO: vmstat(1M) 37 * 38 * COPYRIGHT: Copyright (c) 2005 Brendan Gregg. 39 * 40 * CDDL HEADER START 41 * 42 * The contents of this file are subject to the terms of the 43 * Common Development and Distribution License, Version 1.0 only 44 * (the "License"). You may not use this file except in compliance 45 * with the License. 46 * 47 * You can obtain a copy of the license at Docs/cddl1.txt 48 * or http://www.opensolaris.org/os/licensing. 49 * See the License for the specific language governing permissions 50 * and limitations under the License. 51 * 52 * CDDL HEADER END 53 * 54 * 11-Jun-2005 Brendan Gregg Created this. 55 */ 56 57#pragma D option quiet 58 59inline int SCREEN = 21; 60 61/* 62 * Initialise variables 63 */ 64dtrace:::BEGIN 65{ 66 pi = 0; po = 0; re = 0; sr = 0; mf = 0; fr = 0; 67 sy = 0; in = 0; cs = 0; maj = 0; cow = 0; pro = 0; 68 epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0; 69 fpi = 0; fpo = 0; fpf = 0; 70 lines = SCREEN + 1; 71} 72 73/* 74 * Print header 75 */ 76dtrace:::BEGIN, 77tick-1sec 78/lines++ > SCREEN/ 79{ 80 printf("%14s %13s %16s %14s %13s\n", 81 "memory", "page", "executable", "anonymous", "filesystem"); 82 printf("%9s %7s %5s %4s %3s ", 83 "swap", "free", "re", "mf", "sr"); 84 printf("%4s %4s %4s %4s %4s %4s %4s %4s %4s\n", 85 "epi", "epo", "epf", "api", "apo", "apf", "fpi", "fpo", "fpf"); 86 lines = 0; 87} 88 89/* 90 * Probe events 91 */ 92vminfo:::pgrec { re += arg0; } 93vminfo:::scan { sr += arg0; } 94vminfo:::as_fault { mf += arg0; } 95vminfo:::execpgin { epi += arg0; } 96vminfo:::execpgout { epo += arg0; } 97vminfo:::execfree { epf += arg0; } 98vminfo:::anonpgin { api += arg0; } 99vminfo:::anonpgout { apo += arg0; } 100vminfo:::anonfree { apf += arg0; } 101vminfo:::fspgin { fpi += arg0; } 102vminfo:::fspgout { fpo += arg0; } 103vminfo:::fsfree { fpf += arg0; } 104 105/* 106 * Print output line 107 */ 108profile:::tick-1sec 109{ 110 /* fetch free mem */ 111 this->free = `freemem; 112 113 /* 114 * fetch free swap 115 * 116 * free swap is described in /usr/include/vm/anon.h as, 117 * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree) 118 */ 119 this->ani_max = `k_anoninfo.ani_max; 120 this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv; 121 this->swap = (this->ani_max - this->ani_resv > 0 ? 122 this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree; 123 124 /* fetch w */ 125 this->w = `nswapped; 126 127 /* convert to Kbytes */ 128 epi *= `_pagesize / 1024; 129 epo *= `_pagesize / 1024; 130 epf *= `_pagesize / 1024; 131 api *= `_pagesize / 1024; 132 apo *= `_pagesize / 1024; 133 apf *= `_pagesize / 1024; 134 fpi *= `_pagesize / 1024; 135 fpo *= `_pagesize / 1024; 136 fpf *= `_pagesize / 1024; 137 re *= `_pagesize / 1024; 138 sr *= `_pagesize / 1024; 139 mf *= `_pagesize / 1024; 140 this->swap *= `_pagesize / 1024; 141 this->free *= `_pagesize / 1024; 142 143 /* print line */ 144 printf("%9d %7d %5d %4d %3d ", 145 this->swap, this->free, re, mf, sr); 146 printf("%4d %4d %4d %4d %4d %4d %4d %4d %4d\n", 147 epi, epo, epf, api, apo, apf, fpi, fpo, fpf); 148 149 /* clear counters */ 150 pi = 0; po = 0; re = 0; sr = 0; mf = 0; fr = 0; 151 sy = 0; in = 0; cs = 0; maj = 0; cow = 0; pro = 0; 152 epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0; 153 fpi = 0; fpo = 0; fpf = 0; 154} 155