dvmstat revision 1.1 1 1.1 christos #!/usr/bin/sh
2 1.1 christos #
3 1.1 christos # dvmstat - vmstat by PID/name/command.
4 1.1 christos # Written using DTrace (Solaris 10 3/05).
5 1.1 christos #
6 1.1 christos # This program provides vmstat like data for one particular PID, a
7 1.1 christos # process name, or when running a command. It prints statistics
8 1.1 christos # every second.
9 1.1 christos #
10 1.1 christos # $Id: dvmstat,v 1.1 2015/09/30 22:01:06 christos Exp $
11 1.1 christos #
12 1.1 christos # USAGE: dvmstat { -p PID | -n name | command }
13 1.1 christos # eg,
14 1.1 christos # dvmstat -p 1871 # examine PID 1871
15 1.1 christos # dvmstat -n tar # examine processes called "tar"
16 1.1 christos # dvmstat df -h # run and examine "df -h"
17 1.1 christos #
18 1.1 christos # FIELDS:
19 1.1 christos # re page reclaims Kbytes
20 1.1 christos # maj major faults Kbytes
21 1.1 christos # mf minor faults Kbytes
22 1.1 christos # fr page frees Kbytes
23 1.1 christos # epi executable page ins Kbytes
24 1.1 christos # epo executable page out Kbytes
25 1.1 christos # api anonymous page ins Kbytes
26 1.1 christos # apo anonymous page outs Kbytes
27 1.1 christos # fpi filesystem page ins Kbytes
28 1.1 christos # fpo filesystem page outs Kbytes
29 1.1 christos # sy system calls number
30 1.1 christos #
31 1.1 christos # SEE ALSO: vmstat(1M)
32 1.1 christos #
33 1.1 christos # NOTES:
34 1.1 christos #
35 1.1 christos # When using dvmstat to run a command - if the command takes some time
36 1.1 christos # to execute, dvmstat will print output every second. If the command runs
37 1.1 christos # in less than a second, then the only one line of output will be printed.
38 1.1 christos #
39 1.1 christos # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
40 1.1 christos #
41 1.1 christos # CDDL HEADER START
42 1.1 christos #
43 1.1 christos # The contents of this file are subject to the terms of the
44 1.1 christos # Common Development and Distribution License, Version 1.0 only
45 1.1 christos # (the "License"). You may not use this file except in compliance
46 1.1 christos # with the License.
47 1.1 christos #
48 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt
49 1.1 christos # or http://www.opensolaris.org/os/licensing.
50 1.1 christos # See the License for the specific language governing permissions
51 1.1 christos # and limitations under the License.
52 1.1 christos #
53 1.1 christos # CDDL HEADER END
54 1.1 christos #
55 1.1 christos # Author: Brendan Gregg [Sydney, Australia]
56 1.1 christos #
57 1.1 christos # 12-Jun-2005 Brendan Gregg Created this.
58 1.1 christos # 08-Jan-2006 " " Last update.
59 1.1 christos #
60 1.1 christos
61 1.1 christos ##############################
62 1.1 christos # --- Process Arguments ---
63 1.1 christos #
64 1.1 christos
65 1.1 christos ### Default variables
66 1.1 christos opt_pid=0; opt_name=0; pid=0; pname="."; opt_command=0; command=""
67 1.1 christos
68 1.1 christos ### Process options
69 1.1 christos while getopts hn:p: name
70 1.1 christos do
71 1.1 christos case $name in
72 1.1 christos p) opt_pid=1; pid=$OPTARG ;;
73 1.1 christos n) opt_name=1; pname=$OPTARG ;;
74 1.1 christos h|?) cat <<-END >&2
75 1.1 christos USAGE: dvmstat [-h] { -p PID | -n name | command }
76 1.1 christos -p PID # examine this PID
77 1.1 christos -n name # examine this process name
78 1.1 christos eg,
79 1.1 christos dvmstat -p 1871 # examine PID 1871
80 1.1 christos dvmstat -n tar # examine processes called "tar"
81 1.1 christos dvmstat df -h # run and examine "df -h"
82 1.1 christos END
83 1.1 christos exit 1
84 1.1 christos esac
85 1.1 christos done
86 1.1 christos shift `expr $OPTIND - 1`
87 1.1 christos
88 1.1 christos
89 1.1 christos ### Option logic
90 1.1 christos if [ $opt_pid -eq 0 -a $opt_name -eq 0 ]; then
91 1.1 christos opt_command=1
92 1.1 christos if [ "$*" = "" ]; then
93 1.1 christos $0 -h
94 1.1 christos exit
95 1.1 christos fi
96 1.1 christos command="$*"
97 1.1 christos fi
98 1.1 christos
99 1.1 christos
100 1.1 christos #################################
101 1.1 christos # --- Main Program, DTrace ---
102 1.1 christos #
103 1.1 christos dtrace='
104 1.1 christos #pragma D option quiet
105 1.1 christos
106 1.1 christos /*
107 1.1 christos * Command line arguments
108 1.1 christos */
109 1.1 christos inline int OPT_pid = '$opt_pid';
110 1.1 christos inline int OPT_name = '$opt_name';
111 1.1 christos inline int OPT_command = '$opt_command';
112 1.1 christos inline int PID = '$pid';
113 1.1 christos inline string NAME = "'$pname'";
114 1.1 christos inline string COMMAND = "'$command'";
115 1.1 christos inline int SCREEN = 21;
116 1.1 christos
117 1.1 christos /*
118 1.1 christos * Initialise variables
119 1.1 christos */
120 1.1 christos dtrace:::BEGIN
121 1.1 christos {
122 1.1 christos epi = 0; epo = 0; api = 0; apo = 0; fpi = 0; fpo = 0;
123 1.1 christos re = 0; mf = 0; maj = 0; fr = 0; sy = 0;
124 1.1 christos lines = SCREEN + 1;
125 1.1 christos header = 0;
126 1.1 christos }
127 1.1 christos
128 1.1 christos /*
129 1.1 christos * Print header
130 1.1 christos */
131 1.1 christos dtrace:::BEGIN,
132 1.1 christos dtrace:::END,
133 1.1 christos profile:::tick-1sec
134 1.1 christos /(OPT_command && probename == "END") ||
135 1.1 christos (!(OPT_command && probename == "BEGIN") && lines++ > SCREEN)/
136 1.1 christos {
137 1.1 christos printf("%6s %5s %5s %4s %4s %4s %4s %4s %4s %4s %6s\n",
138 1.1 christos "re", "maj", "mf", "fr", "epi", "epo", "api", "apo",
139 1.1 christos "fpi", "fpo", "sy");
140 1.1 christos lines = 0;
141 1.1 christos }
142 1.1 christos
143 1.1 christos /*
144 1.1 christos * Probe events
145 1.1 christos *
146 1.1 christos * this intentionally does not use an associative array for storing data,
147 1.1 christos * for reasons of performance.
148 1.1 christos */
149 1.1 christos
150 1.1 christos vminfo:::execpgin
151 1.1 christos /(OPT_pid && pid == PID) ||
152 1.1 christos (OPT_name && execname == NAME) ||
153 1.1 christos (OPT_command && pid == $target)/
154 1.1 christos { epi += arg0; }
155 1.1 christos
156 1.1 christos vminfo:::execpgout
157 1.1 christos /(OPT_pid && pid == PID) ||
158 1.1 christos (OPT_name && execname == NAME) ||
159 1.1 christos (OPT_command && pid == $target)/
160 1.1 christos { epo += arg0; }
161 1.1 christos
162 1.1 christos vminfo:::anonpgin
163 1.1 christos /(OPT_pid && pid == PID) ||
164 1.1 christos (OPT_name && execname == NAME) ||
165 1.1 christos (OPT_command && pid == $target)/
166 1.1 christos { api += arg0; }
167 1.1 christos
168 1.1 christos vminfo:::anonpgout
169 1.1 christos /(OPT_pid && pid == PID) ||
170 1.1 christos (OPT_name && execname == NAME) ||
171 1.1 christos (OPT_command && pid == $target)/
172 1.1 christos { apo += arg0; }
173 1.1 christos
174 1.1 christos vminfo:::fspgin
175 1.1 christos /(OPT_pid && pid == PID) ||
176 1.1 christos (OPT_name && execname == NAME) ||
177 1.1 christos (OPT_command && pid == $target)/
178 1.1 christos { fpi += arg0; }
179 1.1 christos
180 1.1 christos vminfo:::fspgout
181 1.1 christos /(OPT_pid && pid == PID) ||
182 1.1 christos (OPT_name && execname == NAME) ||
183 1.1 christos (OPT_command && pid == $target)/
184 1.1 christos { fpo += arg0; }
185 1.1 christos
186 1.1 christos vminfo:::pgrec
187 1.1 christos /(OPT_pid && pid == PID) ||
188 1.1 christos (OPT_name && execname == NAME) ||
189 1.1 christos (OPT_command && pid == $target)/
190 1.1 christos { re += arg0; }
191 1.1 christos
192 1.1 christos vminfo:::as_fault
193 1.1 christos /(OPT_pid && pid == PID) ||
194 1.1 christos (OPT_name && execname == NAME) ||
195 1.1 christos (OPT_command && pid == $target)/
196 1.1 christos { mf += arg0; }
197 1.1 christos
198 1.1 christos vminfo:::maj_fault
199 1.1 christos /(OPT_pid && pid == PID) ||
200 1.1 christos (OPT_name && execname == NAME) ||
201 1.1 christos (OPT_command && pid == $target)/
202 1.1 christos { maj += arg0; }
203 1.1 christos
204 1.1 christos vminfo:::dfree
205 1.1 christos /(OPT_pid && pid == PID) ||
206 1.1 christos (OPT_name && execname == NAME) ||
207 1.1 christos (OPT_command && pid == $target)/
208 1.1 christos { fr += arg0; }
209 1.1 christos
210 1.1 christos syscall:::entry
211 1.1 christos /(OPT_pid && pid == PID) ||
212 1.1 christos (OPT_name && execname == NAME) ||
213 1.1 christos (OPT_command && pid == $target)/
214 1.1 christos { sy++; }
215 1.1 christos
216 1.1 christos /*
217 1.1 christos * Print output line
218 1.1 christos */
219 1.1 christos profile:::tick-1sec,
220 1.1 christos dtrace:::END
221 1.1 christos {
222 1.1 christos /* convert to Kbytes */
223 1.1 christos re *= `_pagesize / 1024;
224 1.1 christos maj *= `_pagesize / 1024;
225 1.1 christos mf *= `_pagesize / 1024;
226 1.1 christos fr *= `_pagesize / 1024;
227 1.1 christos epi *= `_pagesize / 1024;
228 1.1 christos epo *= `_pagesize / 1024;
229 1.1 christos api *= `_pagesize / 1024;
230 1.1 christos apo *= `_pagesize / 1024;
231 1.1 christos fpi *= `_pagesize / 1024;
232 1.1 christos fpo *= `_pagesize / 1024;
233 1.1 christos
234 1.1 christos /* print line */
235 1.1 christos printf("%6d %5d %5d %4d %4d %4d %4d %4d %4d %4d %6d\n",
236 1.1 christos re, maj, mf, fr, epi, epo, api, apo, fpi, fpo, sy);
237 1.1 christos
238 1.1 christos /* clear counters */
239 1.1 christos epi = 0; epo = 0; api = 0; apo = 0; fpi = 0; fpo = 0;
240 1.1 christos re = 0; mf = 0; maj = 0; fr = 0; sy = 0;
241 1.1 christos }
242 1.1 christos '
243 1.1 christos
244 1.1 christos ### Run DTrace
245 1.1 christos if [ $opt_command -eq 1 ]; then
246 1.1 christos /usr/sbin/dtrace -n "$dtrace" -x evaltime=exec -c "$command" >&2
247 1.1 christos else
248 1.1 christos /usr/sbin/dtrace -n "$dtrace" >&2
249 1.1 christos fi
250 1.1 christos
251