]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - cddl/contrib/dtracetoolkit/Mem/xvmstat
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / cddl / contrib / dtracetoolkit / Mem / xvmstat
1 #!/usr/bin/sh
2 #
3 # xvmstat - extended vmstat 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, with a few extra fields.
8 #
9 # $Id: xvmstat 3 2007-08-01 10:50:08Z brendan $
10 #
11 # USAGE:        xvmstat [interval [count]]
12 #
13 # FIELDS: 
14 #               w       swapped out LWPs        number
15 #               swap    virtual memory free     Mbytes
16 #               free    free RAM                Mbytes
17 #               re      page reclaims           pages/sec
18 #               maj     major faults            pages/sec
19 #               mf      minor faults            pages/sec
20 #               cow     copy-on-write faults    pages/sec
21 #               pro     protection faults       pages/sec
22 #               sr      scan rate               pages/sec
23 #               epi     executable page ins     pages/sec
24 #               epo     executable page outs    pages/sec
25 #               epf     executable frees        pages/sec
26 #               api     anonymous page ins      pages/sec
27 #               apo     anonymous page outs     pages/sec
28 #               apf     anonymous frees         pages/sec
29 #               fpi     filesystem page ins     pages/sec
30 #               fpo     filesystem page outs    pages/sec
31 #               fpf     filesystem frees        pages/sec
32 #
33 # NOTES: 
34 # - Most of the statistics are in units of pages, unlike the
35 #   original vmstat command which sometimes uses kilobytes. 
36 # - As this program does not use Kstat, there is no summary since boot line.
37 # - Free RAM is both free free + cache free.
38 #
39 # SEE ALSO:     vmstat(1M)
40 #
41 # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
42 #
43 # CDDL HEADER START
44 #
45 #  The contents of this file are subject to the terms of the
46 #  Common Development and Distribution License, Version 1.0 only
47 #  (the "License").  You may not use this file except in compliance
48 #  with the License.
49 #
50 #  You can obtain a copy of the license at Docs/cddl1.txt
51 #  or http://www.opensolaris.org/os/licensing.
52 #  See the License for the specific language governing permissions
53 #  and limitations under the License.
54 #
55 # CDDL HEADER END
56 #
57 # 12-Jun-2005   Brendan Gregg   Created this.
58 # 01-Mar-2006      "      "     Last update.
59 #
60
61 ##############################
62 # --- Process Arguments ---
63 #
64
65 ### default values
66 interval=1; count=-1
67
68 ### check arguments
69 if [ "$1" = "-h" -o "$1" = "--help" ]; then
70         cat <<-END >&2
71         USAGE: xvmstat [interval [count]]
72                xvmstat          # 1 second samples, infinite
73           eg,
74                xvmstat 1        # print every 1 second
75                xvmstat 5 6      # print every 5 seconds, 6 times
76         END
77         exit 1
78 fi
79
80 ### argument logic
81 if [ "$1" -gt 0 ]; then
82         interval=$1; count=-1; shift
83 fi
84 if [ "$1" -gt 0 ]; then
85         count=$1; shift
86 fi
87 if [ $interval -eq 0 ]; then
88         interval=1
89 fi
90
91
92 #################################
93 # --- Main Program, DTrace ---
94 #
95 /usr/sbin/dtrace -n '
96  #pragma D option quiet
97
98  /*
99   * Command line arguments
100   */
101  inline int INTERVAL   = '$interval';
102  inline int COUNTER    = '$count';
103  inline int SCREEN = 21;
104
105  /*
106   * Initialise variables
107   */
108  dtrace:::BEGIN
109  {
110         re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
111         epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
112         fpi = 0; fpo = 0; fpf = 0;
113         lines = SCREEN + 1;
114         counts = COUNTER;
115         secs = INTERVAL;
116         first = 1;
117  }
118
119  profile:::tick-1sec
120  {
121         secs--;
122  }
123
124  /*
125   * Print header
126   */
127  dtrace:::BEGIN,
128  profile:::tick-1sec
129  /first || (secs == 0 && lines > SCREEN)/
130  {
131         printf("%2s %6s %5s %5s %3s %4s %3s %3s %3s ",
132             "w", "swap", "free", "re", "maj", "mf", "cow", "pro", "sr");
133         printf("%3s %3s %3s %3s %3s %3s %3s %3s %3s\n",
134             "epi", "epo", "epf", "api", "apo", "apf", "fpi", "fpo", "fpf");
135         lines = 0;
136         first = 0;
137  }
138
139  /*
140   * Probe events
141   */
142  vminfo:::pgrec      { re += arg0; }
143  vminfo:::scan       { sr += arg0; }
144  vminfo:::as_fault   { mf += arg0; }
145  vminfo:::execpgin   { epi += arg0; }
146  vminfo:::execpgout  { epo += arg0; }
147  vminfo:::execfree   { epf += arg0; }
148  vminfo:::anonpgin   { api += arg0; }
149  vminfo:::anonpgout  { apo += arg0; }
150  vminfo:::anonfree   { apf += arg0; }
151  vminfo:::fspgin     { fpi += arg0; }
152  vminfo:::fspgout    { fpo += arg0; }
153  vminfo:::fsfree     { fpf += arg0; }
154  vminfo:::maj_fault  { maj += arg0; }
155  vminfo:::cow_fault  { cow += arg0; }
156  vminfo:::prot_fault { pro += arg0; }
157
158  /* 
159   * Print output line
160   */
161  profile:::tick-1sec
162  /secs == 0/
163  {
164         /* fetch free mem */
165         this->free = `freemem;
166
167         /*
168          * fetch free swap
169          *
170          * free swap is described in /usr/include/vm/anon.h as,
171          * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
172          */
173         this->ani_max = `k_anoninfo.ani_max;
174         this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv;
175         this->swap = (this->ani_max - this->ani_resv > 0 ?
176             this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree;
177
178         /* fetch w */
179         this->w = `nswapped;
180
181         /* convert to Mbytes */
182         this->swap *= `_pagesize; this->swap /= 1048576;
183         this->free *= `_pagesize; this->free /= 1048576;
184
185         /* convert to per second values */
186         re  /= INTERVAL; maj /= INTERVAL; mf  /= INTERVAL;
187         cow /= INTERVAL; pro /= INTERVAL; sr  /= INTERVAL;
188         epi /= INTERVAL; epo /= INTERVAL; epf /= INTERVAL;
189         api /= INTERVAL; apo /= INTERVAL; apf /= INTERVAL;
190         fpi /= INTERVAL; fpo /= INTERVAL; fpf /= INTERVAL;
191
192         /* print line */
193         printf("%2d %6d %5d %5d %3d %4d %3d %3d %3d ",
194             this->w, this->swap, this->free, re, maj, mf, cow, pro, sr);
195         printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
196             epi, epo, epf, api, apo, apf, fpi, fpo, fpf);
197
198         /* clear counters */
199         re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
200         epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
201         fpi = 0; fpo = 0; fpf = 0;
202
203         /* process counts */
204         secs = INTERVAL;
205         counts--;
206         lines++;
207  }
208
209  /*
210   * End
211   */
212  profile:::tick-1sec
213  /counts == 0/
214  {
215         exit(0);
216  }
217 '