xref: /titanic_51/usr/src/cmd/plockstat/plockstat.c (revision 351346db78e7d5dac04264f450c72c245c259b06)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*351346dbSahl  * Common Development and Distribution License (the "License").
6*351346dbSahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*351346dbSahl 
227c478bd9Sstevel@tonic-gate /*
23*351346dbSahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <dtrace.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <link.h>
337c478bd9Sstevel@tonic-gate #include <priv.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
42*351346dbSahl #include <libgen.h>
437c478bd9Sstevel@tonic-gate #include <libproc.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static char *g_pname;
467c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
477c478bd9Sstevel@tonic-gate struct ps_prochandle *g_pr;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	E_SUCCESS	0
507c478bd9Sstevel@tonic-gate #define	E_ERROR		1
517c478bd9Sstevel@tonic-gate #define	E_USAGE		2
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * For hold times we use a global associative array since for mutexes, in
557c478bd9Sstevel@tonic-gate  * user-land, it's not invalid to release a sychonization primitive that
567c478bd9Sstevel@tonic-gate  * another thread acquired; rwlocks require a thread-local associative array
577c478bd9Sstevel@tonic-gate  * since multiple thread can hold the same lock for reading. Note that we
587c478bd9Sstevel@tonic-gate  * ignore recursive mutex acquisitions and releases as they don't truly
597c478bd9Sstevel@tonic-gate  * affect lock contention.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate static const char *g_hold_init =
627c478bd9Sstevel@tonic-gate "plockstat$target:::rw-acquire\n"
637c478bd9Sstevel@tonic-gate "{\n"
647c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = timestamp;\n"
657c478bd9Sstevel@tonic-gate "}\n"
667c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-acquire\n"
677c478bd9Sstevel@tonic-gate "/arg1 == 0/\n"
687c478bd9Sstevel@tonic-gate "{\n"
697c478bd9Sstevel@tonic-gate "	mtxhold[arg0] = timestamp;\n"
707c478bd9Sstevel@tonic-gate "}\n";
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static const char *g_hold_histogram =
737c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
747c478bd9Sstevel@tonic-gate "/self->rwhold[arg0] && arg1 == 1/\n"
757c478bd9Sstevel@tonic-gate "{\n"
767c478bd9Sstevel@tonic-gate "	@rw_w_hold[arg0, ustack()] =\n"
777c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwhold[arg0]);\n"
787c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
79*351346dbSahl "	rw_w_hold_found = 1;\n"
807c478bd9Sstevel@tonic-gate "}\n"
817c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
827c478bd9Sstevel@tonic-gate "/self->rwhold[arg0]/\n"
837c478bd9Sstevel@tonic-gate "{\n"
847c478bd9Sstevel@tonic-gate "	@rw_r_hold[arg0, ustack()] =\n"
857c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwhold[arg0]);\n"
867c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
87*351346dbSahl "	rw_r_hold_found = 1;\n"
887c478bd9Sstevel@tonic-gate "}\n"
897c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-release\n"
907c478bd9Sstevel@tonic-gate "/mtxhold[arg0] && arg1 == 0/\n"
917c478bd9Sstevel@tonic-gate "{\n"
927c478bd9Sstevel@tonic-gate "	@mtx_hold[arg0, ustack()] = quantize(timestamp - mtxhold[arg0]);\n"
937c478bd9Sstevel@tonic-gate "	mtxhold[arg0] = 0;\n"
94*351346dbSahl "	mtx_hold_found = 1;\n"
95*351346dbSahl "}\n"
96*351346dbSahl "\n"
97*351346dbSahl "END\n"
98*351346dbSahl "/mtx_hold_found/\n"
99*351346dbSahl "{\n"
100*351346dbSahl "	trace(\"Mutex hold\");\n"
101*351346dbSahl "	printa(@mtx_hold);\n"
102*351346dbSahl "}\n"
103*351346dbSahl "END\n"
104*351346dbSahl "/rw_r_hold_found/\n"
105*351346dbSahl "{\n"
106*351346dbSahl "	trace(\"R/W reader hold\");\n"
107*351346dbSahl "	printa(@rw_r_hold);\n"
108*351346dbSahl "}\n"
109*351346dbSahl "END\n"
110*351346dbSahl "/rw_w_hold_found/\n"
111*351346dbSahl "{\n"
112*351346dbSahl "	trace(\"R/W writer hold\");\n"
113*351346dbSahl "	printa(@rw_w_hold);\n"
1147c478bd9Sstevel@tonic-gate "}\n";
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static const char *g_hold_times =
1177c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
1187c478bd9Sstevel@tonic-gate "/self->rwhold[arg0] && arg1 == 1/\n"
1197c478bd9Sstevel@tonic-gate "{\n"
120*351346dbSahl "	@rw_w_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
121*351346dbSahl "	@rw_w_hold_count[arg0, ustack(5)] = count();\n"
1227c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
123*351346dbSahl "	rw_w_hold_found = 1;\n"
1247c478bd9Sstevel@tonic-gate "}\n"
1257c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n"
1267c478bd9Sstevel@tonic-gate "/self->rwhold[arg0]/\n"
1277c478bd9Sstevel@tonic-gate "{\n"
128*351346dbSahl "	@rw_r_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n"
129*351346dbSahl "	@rw_r_hold_count[arg0, ustack(5)] = count();\n"
1307c478bd9Sstevel@tonic-gate "	self->rwhold[arg0] = 0;\n"
131*351346dbSahl "	rw_r_hold_found = 1;\n"
1327c478bd9Sstevel@tonic-gate "}\n"
1337c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-release\n"
1347c478bd9Sstevel@tonic-gate "/mtxhold[arg0] && arg1 == 0/\n"
1357c478bd9Sstevel@tonic-gate "{\n"
136*351346dbSahl "	@mtx_hold[arg0, ustack(5)] = sum(timestamp - mtxhold[arg0]);\n"
137*351346dbSahl "	@mtx_hold_count[arg0, ustack(5)] = count();\n"
1387c478bd9Sstevel@tonic-gate "	mtxhold[arg0] = 0;\n"
139*351346dbSahl "	mtx_hold_found = 1;\n"
140*351346dbSahl "}\n"
141*351346dbSahl "\n"
142*351346dbSahl "END\n"
143*351346dbSahl "/mtx_hold_found/\n"
144*351346dbSahl "{\n"
145*351346dbSahl "	trace(\"Mutex hold\");\n"
146*351346dbSahl "	printa(@mtx_hold, @mtx_hold_count);\n"
147*351346dbSahl "}\n"
148*351346dbSahl "END\n"
149*351346dbSahl "/rw_r_hold_found/\n"
150*351346dbSahl "{\n"
151*351346dbSahl "	trace(\"R/W reader hold\");\n"
152*351346dbSahl "	printa(@rw_r_hold, @rw_r_hold_count);\n"
153*351346dbSahl "}\n"
154*351346dbSahl "END\n"
155*351346dbSahl "/rw_w_hold_found/\n"
156*351346dbSahl "{\n"
157*351346dbSahl "	trace(\"R/W writer hold\");\n"
158*351346dbSahl "	printa(@rw_w_hold, @rw_w_hold_count);\n"
1597c478bd9Sstevel@tonic-gate "}\n";
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * For contention, we use thread-local associative arrays since we're tracing
1647c478bd9Sstevel@tonic-gate  * a single thread's activity in libc and multiple threads can be blocking or
1657c478bd9Sstevel@tonic-gate  * spinning on the same sychonization primitive.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static const char *g_ctnd_init =
1687c478bd9Sstevel@tonic-gate "plockstat$target:::rw-block\n"
1697c478bd9Sstevel@tonic-gate "{\n"
1707c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = timestamp;\n"
1717c478bd9Sstevel@tonic-gate "}\n"
1727c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-block\n"
1737c478bd9Sstevel@tonic-gate "{\n"
1747c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = timestamp;\n"
1757c478bd9Sstevel@tonic-gate "}\n"
1767c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spin\n"
1777c478bd9Sstevel@tonic-gate "{\n"
1787c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = timestamp;\n"
1797c478bd9Sstevel@tonic-gate "}\n";
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static const char *g_ctnd_histogram =
1827c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1837c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
1847c478bd9Sstevel@tonic-gate "{\n"
1857c478bd9Sstevel@tonic-gate "	@rw_w_block[arg0, ustack()] =\n"
1867c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwblock[arg0]);\n"
1877c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
188*351346dbSahl "	rw_w_block_found = 1;\n"
1897c478bd9Sstevel@tonic-gate "}\n"
1907c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1917c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg2 != 0/\n"
1927c478bd9Sstevel@tonic-gate "{\n"
1937c478bd9Sstevel@tonic-gate "	@rw_r_block[arg0, ustack()] =\n"
1947c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->rwblock[arg0]);\n"
1957c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
196*351346dbSahl "	rw_r_block_found = 1;\n"
1977c478bd9Sstevel@tonic-gate "}\n"
1987c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
1997c478bd9Sstevel@tonic-gate "/self->rwblock[arg0]/\n"
2007c478bd9Sstevel@tonic-gate "{\n"
2017c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
2027c478bd9Sstevel@tonic-gate "}\n"
2037c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
2047c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0] && arg1 != 0/\n"
2057c478bd9Sstevel@tonic-gate "{\n"
2067c478bd9Sstevel@tonic-gate "	@mtx_spin[arg0, ustack()] =\n"
2077c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->mtxspin[arg0]);\n"
2087c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
209*351346dbSahl "	mtx_spin_found = 1;\n"
2107c478bd9Sstevel@tonic-gate "}\n"
2117c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
2127c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0]/\n"
2137c478bd9Sstevel@tonic-gate "{\n"
2147c478bd9Sstevel@tonic-gate "	@mtx_vain_spin[arg0, ustack()] =\n"
2157c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->mtxspin[arg0]);\n"
2167c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
217*351346dbSahl "	mtx_vain_spin_found = 1;\n"
2187c478bd9Sstevel@tonic-gate "}\n"
2197c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
2207c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0] && arg1 != 0/\n"
2217c478bd9Sstevel@tonic-gate "{\n"
2227c478bd9Sstevel@tonic-gate "	@mtx_block[arg0, ustack()] =\n"
2237c478bd9Sstevel@tonic-gate "	    quantize(timestamp - self->mtxblock[arg0]);\n"
2247c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
225*351346dbSahl "	mtx_block_found = 1;\n"
2267c478bd9Sstevel@tonic-gate "}\n"
2277c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
2287c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0]/\n"
2297c478bd9Sstevel@tonic-gate "{\n"
2307c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
231*351346dbSahl "}\n"
232*351346dbSahl "\n"
233*351346dbSahl "END\n"
234*351346dbSahl "/mtx_block_found/\n"
235*351346dbSahl "{\n"
236*351346dbSahl "	trace(\"Mutex block\");\n"
237*351346dbSahl "	printa(@mtx_block);\n"
238*351346dbSahl "}\n"
239*351346dbSahl "END\n"
240*351346dbSahl "/mtx_spin_found/\n"
241*351346dbSahl "{\n"
242*351346dbSahl "	trace(\"Mutex spin\");\n"
243*351346dbSahl "	printa(@mtx_spin);\n"
244*351346dbSahl "}\n"
245*351346dbSahl "END\n"
246*351346dbSahl "/mtx_vain_spin_found/\n"
247*351346dbSahl "{\n"
248*351346dbSahl "	trace(\"Mutex unsuccessful spin\");\n"
249*351346dbSahl "	printa(@mtx_vain_spin);\n"
250*351346dbSahl "}\n"
251*351346dbSahl "END\n"
252*351346dbSahl "/rw_r_block_found/\n"
253*351346dbSahl "{\n"
254*351346dbSahl "	trace(\"R/W reader block\");\n"
255*351346dbSahl "	printa(@rw_r_block);\n"
256*351346dbSahl "}\n"
257*351346dbSahl "END\n"
258*351346dbSahl "/rw_w_block_found/\n"
259*351346dbSahl "{\n"
260*351346dbSahl "	trace(\"R/W writer block\");\n"
261*351346dbSahl "	printa(@rw_w_block);\n"
2627c478bd9Sstevel@tonic-gate "}\n";
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static const char *g_ctnd_times =
2667c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
2677c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n"
2687c478bd9Sstevel@tonic-gate "{\n"
2697c478bd9Sstevel@tonic-gate "	@rw_w_block[arg0, ustack(5)] =\n"
270*351346dbSahl "	    sum(timestamp - self->rwblock[arg0]);\n"
271*351346dbSahl "	@rw_w_block_count[arg0, ustack(5)] = count();\n"
2727c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
273*351346dbSahl "	rw_w_block_found = 1;\n"
2747c478bd9Sstevel@tonic-gate "}\n"
2757c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
2767c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg2 != 0/\n"
2777c478bd9Sstevel@tonic-gate "{\n"
2787c478bd9Sstevel@tonic-gate "	@rw_r_block[arg0, ustack(5)] =\n"
279*351346dbSahl "	    sum(timestamp - self->rwblock[arg0]);\n"
280*351346dbSahl "	@rw_r_block_count[arg0, ustack(5)] = count();\n"
2817c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
282*351346dbSahl "	rw_r_block_found = 1;\n"
2837c478bd9Sstevel@tonic-gate "}\n"
2847c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n"
2857c478bd9Sstevel@tonic-gate "/self->rwblock[arg0]/\n"
2867c478bd9Sstevel@tonic-gate "{\n"
2877c478bd9Sstevel@tonic-gate "	self->rwblock[arg0] = 0;\n"
2887c478bd9Sstevel@tonic-gate "}\n"
2897c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
2907c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0] && arg1 != 0/\n"
2917c478bd9Sstevel@tonic-gate "{\n"
2927c478bd9Sstevel@tonic-gate "	@mtx_spin[arg0, ustack(5)] =\n"
293*351346dbSahl "	    sum(timestamp - self->mtxspin[arg0]);\n"
294*351346dbSahl "	@mtx_spin_count[arg0, ustack(5)] = count();\n"
2957c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
296*351346dbSahl "	mtx_spin_found = 1;\n"
2977c478bd9Sstevel@tonic-gate "}\n"
2987c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n"
2997c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0]/\n"
3007c478bd9Sstevel@tonic-gate "{\n"
3017c478bd9Sstevel@tonic-gate "	@mtx_vain_spin[arg0, ustack(5)] =\n"
302*351346dbSahl "	    sum(timestamp - self->mtxspin[arg0]);\n"
303*351346dbSahl "	@mtx_vain_spin_count[arg0, ustack(5)] = count();\n"
3047c478bd9Sstevel@tonic-gate "	self->mtxspin[arg0] = 0;\n"
305*351346dbSahl "	mtx_vain_spin_found = 1;\n"
3067c478bd9Sstevel@tonic-gate "}\n"
3077c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
3087c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0] && arg1 != 0/\n"
3097c478bd9Sstevel@tonic-gate "{\n"
3107c478bd9Sstevel@tonic-gate "	@mtx_block[arg0, ustack(5)] =\n"
311*351346dbSahl "	    sum(timestamp - self->mtxblock[arg0]);\n"
312*351346dbSahl "	@mtx_block_count[arg0, ustack(5)] = count();\n"
3137c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
314*351346dbSahl "	mtx_block_found = 1;\n"
3157c478bd9Sstevel@tonic-gate "}\n"
3167c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n"
3177c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0]/\n"
3187c478bd9Sstevel@tonic-gate "{\n"
3197c478bd9Sstevel@tonic-gate "	self->mtxblock[arg0] = 0;\n"
320*351346dbSahl "}\n"
321*351346dbSahl "\n"
322*351346dbSahl "END\n"
323*351346dbSahl "/mtx_block_found/\n"
324*351346dbSahl "{\n"
325*351346dbSahl "	trace(\"Mutex block\");\n"
326*351346dbSahl "	printa(@mtx_block, @mtx_block_count);\n"
327*351346dbSahl "}\n"
328*351346dbSahl "END\n"
329*351346dbSahl "/mtx_spin_found/\n"
330*351346dbSahl "{\n"
331*351346dbSahl "	trace(\"Mutex spin\");\n"
332*351346dbSahl "	printa(@mtx_spin, @mtx_spin_count);\n"
333*351346dbSahl "}\n"
334*351346dbSahl "END\n"
335*351346dbSahl "/mtx_vain_spin_found/\n"
336*351346dbSahl "{\n"
337*351346dbSahl "	trace(\"Mutex unsuccessful spin\");\n"
338*351346dbSahl "	printa(@mtx_vain_spin, @mtx_vain_spin_count);\n"
339*351346dbSahl "}\n"
340*351346dbSahl "END\n"
341*351346dbSahl "/rw_r_block_found/\n"
342*351346dbSahl "{\n"
343*351346dbSahl "	trace(\"R/W reader block\");\n"
344*351346dbSahl "	printa(@rw_r_block, @rw_r_block_count);\n"
345*351346dbSahl "}\n"
346*351346dbSahl "END\n"
347*351346dbSahl "/rw_w_block_found/\n"
348*351346dbSahl "{\n"
349*351346dbSahl "	trace(\"R/W writer block\");\n"
350*351346dbSahl "	printa(@rw_w_block, @rw_w_block_count);\n"
3517c478bd9Sstevel@tonic-gate "}\n";
3527c478bd9Sstevel@tonic-gate 
353*351346dbSahl static char g_prog[4096];
3547c478bd9Sstevel@tonic-gate static size_t g_proglen;
3557c478bd9Sstevel@tonic-gate static int g_opt_V, g_opt_s;
3567c478bd9Sstevel@tonic-gate static int g_intr;
357*351346dbSahl static int g_exited;
3587c478bd9Sstevel@tonic-gate static dtrace_optval_t g_nframes;
3597c478bd9Sstevel@tonic-gate static ulong_t g_nent = ULONG_MAX;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate #define	PLOCKSTAT_OPTSTR	"n:ps:e:vx:ACHV"
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static void
3647c478bd9Sstevel@tonic-gate usage(void)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\n"
3677c478bd9Sstevel@tonic-gate 	    "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
3687c478bd9Sstevel@tonic-gate 	    "\t    command [arg...]\n"
3697c478bd9Sstevel@tonic-gate 	    "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n"
3707c478bd9Sstevel@tonic-gate 	    "\t    -p pid\n", g_pname, g_pname);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	exit(E_USAGE);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate static void
3767c478bd9Sstevel@tonic-gate verror(const char *fmt, va_list ap)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	int error = errno;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
3817c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
3847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
3887c478bd9Sstevel@tonic-gate static void
3897c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	va_list ap;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
3947c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
3957c478bd9Sstevel@tonic-gate 	va_end(ap);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (g_pr != NULL && g_dtp != NULL)
3987c478bd9Sstevel@tonic-gate 		dtrace_proc_release(g_dtp, g_pr);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
4047c478bd9Sstevel@tonic-gate static void
4057c478bd9Sstevel@tonic-gate dfatal(const char *fmt, ...)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	va_list ap;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
4127c478bd9Sstevel@tonic-gate 	if (fmt != NULL)
4137c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	va_end(ap);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
4187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
4197c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
4207c478bd9Sstevel@tonic-gate 	} else if (fmt == NULL) {
4217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n",
4227c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (g_pr != NULL) {
4267c478bd9Sstevel@tonic-gate 		dtrace_proc_continue(g_dtp, g_pr);
4277c478bd9Sstevel@tonic-gate 		dtrace_proc_release(g_dtp, g_pr);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
4347c478bd9Sstevel@tonic-gate static void
4357c478bd9Sstevel@tonic-gate notice(const char *fmt, ...)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	va_list ap;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
4407c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
4417c478bd9Sstevel@tonic-gate 	va_end(ap);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate static void
4457c478bd9Sstevel@tonic-gate dprog_add(const char *prog)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	size_t len = strlen(prog);
4487c478bd9Sstevel@tonic-gate 	bcopy(prog, g_prog + g_proglen, len + 1);
4497c478bd9Sstevel@tonic-gate 	g_proglen += len;
450*351346dbSahl 	assert(g_proglen < sizeof (g_prog));
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate static void
4547c478bd9Sstevel@tonic-gate dprog_compile(void)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
4577c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (g_opt_V) {
4607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: vvvv D program vvvv\n", g_pname);
4617c478bd9Sstevel@tonic-gate 		(void) fputs(g_prog, stderr);
4627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ^^^^ D program ^^^^\n", g_pname);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
4667c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
4677c478bd9Sstevel@tonic-gate 		dfatal("failed to compile program");
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
4707c478bd9Sstevel@tonic-gate 		dfatal("failed to enable probes");
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate void
4747c478bd9Sstevel@tonic-gate print_legend(void)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	(void) printf("%5s %8s %-28s %s\n", "Count", "nsec", "Lock", "Caller");
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate void
4807c478bd9Sstevel@tonic-gate print_bar(void)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	(void) printf("---------------------------------------"
4837c478bd9Sstevel@tonic-gate 	    "----------------------------------------\n");
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate void
4877c478bd9Sstevel@tonic-gate print_histogram_header(void)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	(void) printf("\n%10s ---- Time Distribution --- %5s %s\n",
4907c478bd9Sstevel@tonic-gate 	    "nsec", "count", "Stack");
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * Convert an address to a symbolic string or a numeric string. If nolocks
4957c478bd9Sstevel@tonic-gate  * is set, we return an error code if this symbol appears to be a mutex- or
4967c478bd9Sstevel@tonic-gate  * rwlock-related symbol in libc so the caller has a chance to find a more
4977c478bd9Sstevel@tonic-gate  * helpful symbol.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate static int
5007c478bd9Sstevel@tonic-gate getsym(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size,
5017c478bd9Sstevel@tonic-gate     int nolocks)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	char name[256];
5047c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
5057c478bd9Sstevel@tonic-gate 	prsyminfo_t info;
5067c478bd9Sstevel@tonic-gate 	size_t len;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	if (P == NULL || Pxlookup_by_addr(P, addr, name, sizeof (name),
5097c478bd9Sstevel@tonic-gate 	    &sym, &info) != 0) {
5107c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, size, "%#lx", addr);
5117c478bd9Sstevel@tonic-gate 		return (0);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (info.prs_lmid != LM_ID_BASE) {
5157c478bd9Sstevel@tonic-gate 		len = snprintf(buf, size, "LM%lu`", info.prs_lmid);
5167c478bd9Sstevel@tonic-gate 		buf += len;
5177c478bd9Sstevel@tonic-gate 		size -= len;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	len = snprintf(buf, size, "%s`%s", info.prs_object, info.prs_name);
5217c478bd9Sstevel@tonic-gate 	buf += len;
5227c478bd9Sstevel@tonic-gate 	size -= len;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (sym.st_value != addr)
5257c478bd9Sstevel@tonic-gate 		len = snprintf(buf, size, "+%#lx", addr - sym.st_value);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (nolocks && strcmp("libc.so.1", info.prs_object) == 0 &&
5287c478bd9Sstevel@tonic-gate 	    (strstr("mutex", info.prs_name) == 0 ||
5297c478bd9Sstevel@tonic-gate 	    strstr("rw", info.prs_name) == 0))
5307c478bd9Sstevel@tonic-gate 		return (-1);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	return (0);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5367c478bd9Sstevel@tonic-gate static int
537*351346dbSahl process_aggregate(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
5387c478bd9Sstevel@tonic-gate {
539a1b5e537Sbmc 	const dtrace_recdesc_t *rec;
5407c478bd9Sstevel@tonic-gate 	uintptr_t lock;
5417c478bd9Sstevel@tonic-gate 	uint64_t *stack;
542*351346dbSahl 	caddr_t data;
543*351346dbSahl 	pid_t pid;
5447c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
545*351346dbSahl 	char buf[256];
5467c478bd9Sstevel@tonic-gate 	int i, j;
547*351346dbSahl 	uint64_t sum, count, avg;
5487c478bd9Sstevel@tonic-gate 
549*351346dbSahl 	if ((*(uint_t *)arg)++ >= g_nent)
550*351346dbSahl 		return (DTRACE_AGGWALK_NEXT);
5517c478bd9Sstevel@tonic-gate 
552*351346dbSahl 	rec = aggsdata[0]->dtada_desc->dtagd_rec;
553*351346dbSahl 	data = aggsdata[0]->dtada_data;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*LINTED - alignment*/
5567c478bd9Sstevel@tonic-gate 	lock = (uintptr_t)*(uint64_t *)(data + rec[1].dtrd_offset);
5577c478bd9Sstevel@tonic-gate 	/*LINTED - alignment*/
5587c478bd9Sstevel@tonic-gate 	stack = (uint64_t *)(data + rec[2].dtrd_offset);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (!g_opt_s) {
561*351346dbSahl 		/*LINTED - alignment*/
562*351346dbSahl 		sum = *(uint64_t *)(aggsdata[1]->dtada_data +
563*351346dbSahl 		    aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset);
564*351346dbSahl 		/*LINTED - alignment*/
565*351346dbSahl 		count = *(uint64_t *)(aggsdata[2]->dtada_data +
566*351346dbSahl 		    aggsdata[2]->dtada_desc->dtagd_rec[3].dtrd_offset);
5677c478bd9Sstevel@tonic-gate 	} else {
568*351346dbSahl 		uint64_t *a;
569*351346dbSahl 
570*351346dbSahl 		/*LINTED - alignment*/
571*351346dbSahl 		a = (uint64_t *)(aggsdata[1]->dtada_data +
572*351346dbSahl 		    aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset);
573*351346dbSahl 
5747c478bd9Sstevel@tonic-gate 		print_bar();
5757c478bd9Sstevel@tonic-gate 		print_legend();
5767c478bd9Sstevel@tonic-gate 
577*351346dbSahl 		for (count = sum = 0, i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
5787c478bd9Sstevel@tonic-gate 		    i < DTRACE_QUANTIZE_NBUCKETS; i++, j++) {
5797c478bd9Sstevel@tonic-gate 			count += a[i];
580*351346dbSahl 			sum += a[i] << (j - 64);
581*351346dbSahl 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
584*351346dbSahl 	avg = sum / count;
5857c478bd9Sstevel@tonic-gate 	(void) printf("%5llu %8llu ", (u_longlong_t)count, (u_longlong_t)avg);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	pid = stack[0];
5887c478bd9Sstevel@tonic-gate 	P = dtrace_proc_grab(g_dtp, pid, PGRAB_RDONLY);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	(void) getsym(P, lock, buf, sizeof (buf), 0);
5917c478bd9Sstevel@tonic-gate 	(void) printf("%-28s ", buf);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	for (i = 2; i <= 5; i++) {
5947c478bd9Sstevel@tonic-gate 		if (getsym(P, stack[i], buf, sizeof (buf), 1) == 0)
5957c478bd9Sstevel@tonic-gate 			break;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	(void) printf("%s\n", buf);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (g_opt_s) {
6007c478bd9Sstevel@tonic-gate 		int stack_done = 0;
6017c478bd9Sstevel@tonic-gate 		int quant_done = 0;
6027c478bd9Sstevel@tonic-gate 		int first_bin, last_bin;
603*351346dbSahl 		uint64_t bin_size, *a;
604*351346dbSahl 
605*351346dbSahl 		/*LINTED - alignment*/
606*351346dbSahl 		a = (uint64_t *)(aggsdata[1]->dtada_data +
607*351346dbSahl 		    aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		print_histogram_header();
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		for (first_bin = DTRACE_QUANTIZE_ZEROBUCKET;
6127c478bd9Sstevel@tonic-gate 		    a[first_bin] == 0; first_bin++)
6137c478bd9Sstevel@tonic-gate 			continue;
6147c478bd9Sstevel@tonic-gate 		for (last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 63;
6157c478bd9Sstevel@tonic-gate 		    a[last_bin] == 0; last_bin--)
6167c478bd9Sstevel@tonic-gate 			continue;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		for (i = 0; !stack_done || !quant_done; i++) {
6197c478bd9Sstevel@tonic-gate 			if (!stack_done) {
6207c478bd9Sstevel@tonic-gate 				(void) getsym(P, stack[i + 2], buf,
6217c478bd9Sstevel@tonic-gate 				    sizeof (buf), 0);
6227c478bd9Sstevel@tonic-gate 			} else {
6237c478bd9Sstevel@tonic-gate 				buf[0] = '\0';
6247c478bd9Sstevel@tonic-gate 			}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 			if (!quant_done) {
6277c478bd9Sstevel@tonic-gate 				bin_size = a[first_bin];
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 				(void) printf("%10llu |%-24.*s| %5llu %s\n",
6307c478bd9Sstevel@tonic-gate 				    1ULL <<
6317c478bd9Sstevel@tonic-gate 				    (first_bin - DTRACE_QUANTIZE_ZEROBUCKET),
6327c478bd9Sstevel@tonic-gate 				    (int)(24.0 * bin_size / count),
6337c478bd9Sstevel@tonic-gate 				    "@@@@@@@@@@@@@@@@@@@@@@@@@@",
6347c478bd9Sstevel@tonic-gate 				    (u_longlong_t)bin_size, buf);
6357c478bd9Sstevel@tonic-gate 			} else {
6367c478bd9Sstevel@tonic-gate 				(void) printf("%43s %s\n", "", buf);
6377c478bd9Sstevel@tonic-gate 			}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 			if (i + 1 >= g_nframes || stack[i + 3] == 0)
6407c478bd9Sstevel@tonic-gate 				stack_done = 1;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 			if (first_bin++ == last_bin)
6437c478bd9Sstevel@tonic-gate 				quant_done = 1;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	dtrace_proc_release(g_dtp, P);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
652*351346dbSahl /*ARGSUSED*/
653*351346dbSahl static void
654*351346dbSahl prochandler(struct ps_prochandle *P, const char *msg, void *arg)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	const psinfo_t *prp = Ppsinfo(P);
6577c478bd9Sstevel@tonic-gate 	int pid = Pstatus(P)->pr_pid;
6587c478bd9Sstevel@tonic-gate 	char name[SIG2STR_MAX];
6597c478bd9Sstevel@tonic-gate 
660*351346dbSahl 	if (msg != NULL) {
661*351346dbSahl 		notice("pid %d: %s\n", pid, msg);
662*351346dbSahl 		return;
663*351346dbSahl 	}
664*351346dbSahl 
6657c478bd9Sstevel@tonic-gate 	switch (Pstate(P)) {
6667c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * Ideally we would like to always report pr_wstat here, but it
6697c478bd9Sstevel@tonic-gate 		 * isn't possible given current /proc semantics.  If we grabbed
6707c478bd9Sstevel@tonic-gate 		 * the process, Ppsinfo() will either fail or return a zeroed
6717c478bd9Sstevel@tonic-gate 		 * psinfo_t depending on how far the parent is in reaping it.
6727c478bd9Sstevel@tonic-gate 		 * When /proc provides a stable pr_wstat in the status file,
6737c478bd9Sstevel@tonic-gate 		 * this code can be improved by examining this new pr_wstat.
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 		if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
6767c478bd9Sstevel@tonic-gate 			notice("pid %d terminated by %s\n", pid,
6777c478bd9Sstevel@tonic-gate 			    proc_signame(WTERMSIG(prp->pr_wstat),
6787c478bd9Sstevel@tonic-gate 			    name, sizeof (name)));
6797c478bd9Sstevel@tonic-gate 		} else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
6807c478bd9Sstevel@tonic-gate 			notice("pid %d exited with status %d\n",
6817c478bd9Sstevel@tonic-gate 			    pid, WEXITSTATUS(prp->pr_wstat));
6827c478bd9Sstevel@tonic-gate 		} else {
6837c478bd9Sstevel@tonic-gate 			notice("pid %d has exited\n", pid);
6847c478bd9Sstevel@tonic-gate 		}
685*351346dbSahl 		g_exited = 1;
686*351346dbSahl 		break;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	case PS_LOST:
6897c478bd9Sstevel@tonic-gate 		notice("pid %d exec'd a set-id or unobservable program\n", pid);
690*351346dbSahl 		g_exited = 1;
691*351346dbSahl 		break;
692*351346dbSahl 	}
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
695*351346dbSahl /*ARGSUSED*/
696*351346dbSahl static int
697*351346dbSahl chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
698*351346dbSahl {
699*351346dbSahl 	dtrace_eprobedesc_t *epd = data->dtpda_edesc;
700*351346dbSahl 	dtrace_aggvarid_t aggvars[2];
701*351346dbSahl 	const void *buf;
702*351346dbSahl 	int i, nagv;
703*351346dbSahl 
704*351346dbSahl 	/*
705*351346dbSahl 	 * A NULL rec indicates that we've processed the last record.
706*351346dbSahl 	 */
707*351346dbSahl 	if (rec == NULL)
708*351346dbSahl 		return (DTRACE_CONSUME_NEXT);
709*351346dbSahl 
710*351346dbSahl 	buf = data->dtpda_data - rec->dtrd_offset;
711*351346dbSahl 
712*351346dbSahl 	switch (rec->dtrd_action) {
713*351346dbSahl 	case DTRACEACT_DIFEXPR:
714*351346dbSahl 		(void) printf("\n%s\n\n", (char *)buf + rec->dtrd_offset);
715*351346dbSahl 		if (!g_opt_s) {
716*351346dbSahl 			print_legend();
717*351346dbSahl 			print_bar();
718*351346dbSahl 		}
719*351346dbSahl 		return (DTRACE_CONSUME_NEXT);
720*351346dbSahl 
721*351346dbSahl 	case DTRACEACT_PRINTA:
722*351346dbSahl 		for (nagv = 0, i = 0; i < epd->dtepd_nrecs - 1; i++) {
723*351346dbSahl 			const dtrace_recdesc_t *nrec = &rec[i];
724*351346dbSahl 
725*351346dbSahl 			if (nrec->dtrd_uarg != rec->dtrd_uarg)
726*351346dbSahl 				break;
727*351346dbSahl 
728*351346dbSahl 			/*LINTED - alignment*/
729*351346dbSahl 			aggvars[nagv++] = *(dtrace_aggvarid_t *)((caddr_t)buf +
730*351346dbSahl 			    nrec->dtrd_offset);
731*351346dbSahl 		}
732*351346dbSahl 
733*351346dbSahl 		if (nagv == (g_opt_s ? 1 : 2)) {
734*351346dbSahl 			uint_t nent = 0;
735*351346dbSahl 			if (dtrace_aggregate_walk_joined(g_dtp, aggvars, nagv,
736*351346dbSahl 			    process_aggregate, &nent) != 0)
737*351346dbSahl 				dfatal("failed to walk aggregate");
738*351346dbSahl 		}
739*351346dbSahl 
740*351346dbSahl 		return (DTRACE_CONSUME_NEXT);
741*351346dbSahl 	}
742*351346dbSahl 
743*351346dbSahl 	return (DTRACE_CONSUME_THIS);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7477c478bd9Sstevel@tonic-gate static void
7487c478bd9Sstevel@tonic-gate intr(int signo)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	g_intr = 1;
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate int
7547c478bd9Sstevel@tonic-gate main(int argc, char **argv)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	ucred_t *ucp;
7577c478bd9Sstevel@tonic-gate 	int err;
7587c478bd9Sstevel@tonic-gate 	int opt_C = 0, opt_H = 0, opt_p = 0, opt_v = 0;
7597c478bd9Sstevel@tonic-gate 	char c, *p, *end;
7607c478bd9Sstevel@tonic-gate 	struct sigaction act;
7617c478bd9Sstevel@tonic-gate 	int done = 0;
7627c478bd9Sstevel@tonic-gate 
763*351346dbSahl 	g_pname = basename(argv[0]);
764*351346dbSahl 	argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/*
7677c478bd9Sstevel@tonic-gate 	 * Make sure we have the required dtrace_proc privilege.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	if ((ucp = ucred_get(getpid())) != NULL) {
7707c478bd9Sstevel@tonic-gate 		const priv_set_t *psp;
7717c478bd9Sstevel@tonic-gate 		if ((psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) != NULL &&
7727c478bd9Sstevel@tonic-gate 		    !priv_ismember(psp, PRIV_DTRACE_PROC)) {
7737c478bd9Sstevel@tonic-gate 			fatal("dtrace_proc privilege required\n");
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		ucred_free(ucp);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) {
7807c478bd9Sstevel@tonic-gate 		switch (c) {
7817c478bd9Sstevel@tonic-gate 		case 'n':
7827c478bd9Sstevel@tonic-gate 			errno = 0;
7837c478bd9Sstevel@tonic-gate 			g_nent = strtoul(optarg, &end, 10);
7847c478bd9Sstevel@tonic-gate 			if (*end != '\0' || errno != 0) {
7857c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: invalid count "
7867c478bd9Sstevel@tonic-gate 				    "'%s'\n", g_pname, optarg);
7877c478bd9Sstevel@tonic-gate 				usage();
7887c478bd9Sstevel@tonic-gate 			}
7897c478bd9Sstevel@tonic-gate 			break;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		case 'p':
7927c478bd9Sstevel@tonic-gate 			opt_p = 1;
7937c478bd9Sstevel@tonic-gate 			break;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		case 'v':
7967c478bd9Sstevel@tonic-gate 			opt_v = 1;
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 		case 'A':
8007c478bd9Sstevel@tonic-gate 			opt_C = opt_H = 1;
8017c478bd9Sstevel@tonic-gate 			break;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		case 'C':
8047c478bd9Sstevel@tonic-gate 			opt_C = 1;
8057c478bd9Sstevel@tonic-gate 			break;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		case 'H':
8087c478bd9Sstevel@tonic-gate 			opt_H = 1;
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		case 'V':
8127c478bd9Sstevel@tonic-gate 			g_opt_V = 1;
8137c478bd9Sstevel@tonic-gate 			break;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		default:
8167c478bd9Sstevel@tonic-gate 			if (strchr(PLOCKSTAT_OPTSTR, c) == NULL)
8177c478bd9Sstevel@tonic-gate 				usage();
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * We need a command or at least one pid.
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 	if (argc == optind)
8257c478bd9Sstevel@tonic-gate 		usage();
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (opt_C == 0 && opt_H == 0)
8287c478bd9Sstevel@tonic-gate 		opt_C = 1;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL)
8317c478bd9Sstevel@tonic-gate 		fatal("failed to initialize dtrace: %s\n",
8327c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
8337c478bd9Sstevel@tonic-gate 
834*351346dbSahl 	/*
835*351346dbSahl 	 * The longest string we trace is 23 bytes long -- so 32 is plenty.
836*351346dbSahl 	 */
837*351346dbSahl 	if (dtrace_setopt(g_dtp, "strsize", "32") == -1)
838*351346dbSahl 		dfatal("failed to set 'strsize'");
839*351346dbSahl 
840*351346dbSahl 	/*
841*351346dbSahl 	 * 1k should be more than enough for all trace() and printa() actions.
842*351346dbSahl 	 */
843*351346dbSahl 	if (dtrace_setopt(g_dtp, "bufsize", "1k") == -1)
844*351346dbSahl 		dfatal("failed to set 'bufsize'");
845*351346dbSahl 
846*351346dbSahl 	/*
847*351346dbSahl 	 * The table we produce has the hottest locks at the top.
848*351346dbSahl 	 */
849*351346dbSahl 	if (dtrace_setopt(g_dtp, "aggsortrev", NULL) == -1)
850*351346dbSahl 		dfatal("failed to set 'aggsortrev'");
851*351346dbSahl 
852*351346dbSahl 	/*
853*351346dbSahl 	 * These are two reasonable defaults which should suffice.
854*351346dbSahl 	 */
8557c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggsize", "256k") == -1)
8567c478bd9Sstevel@tonic-gate 		dfatal("failed to set 'aggsize'");
8577c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "aggrate", "1sec") == -1)
8587c478bd9Sstevel@tonic-gate 		dfatal("failed to set 'aggrate'");
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
8617c478bd9Sstevel@tonic-gate 	 * Take a second pass through to look for options that set options now
8627c478bd9Sstevel@tonic-gate 	 * that we have an open dtrace handle.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	optind = 1;
8657c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) {
8667c478bd9Sstevel@tonic-gate 		switch (c) {
8677c478bd9Sstevel@tonic-gate 		case 's':
8687c478bd9Sstevel@tonic-gate 			g_opt_s = 1;
8697c478bd9Sstevel@tonic-gate 			if (dtrace_setopt(g_dtp, "ustackframes", optarg) == -1)
8707c478bd9Sstevel@tonic-gate 				dfatal("failed to set 'ustackframes'");
8717c478bd9Sstevel@tonic-gate 			break;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		case 'x':
8747c478bd9Sstevel@tonic-gate 			if ((p = strchr(optarg, '=')) != NULL)
8757c478bd9Sstevel@tonic-gate 				*p++ = '\0';
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 			if (dtrace_setopt(g_dtp, optarg, p) != 0)
8787c478bd9Sstevel@tonic-gate 				dfatal("failed to set -x %s", optarg);
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		case 'e':
8827c478bd9Sstevel@tonic-gate 			errno = 0;
8837c478bd9Sstevel@tonic-gate 			(void) strtoul(optarg, &end, 10);
8847c478bd9Sstevel@tonic-gate 			if (*optarg == '-' || *end != '\0' || errno != 0) {
8857c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: invalid timeout "
8867c478bd9Sstevel@tonic-gate 				    "'%s'\n", g_pname, optarg);
8877c478bd9Sstevel@tonic-gate 				usage();
8887c478bd9Sstevel@tonic-gate 			}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 			/*
8917c478bd9Sstevel@tonic-gate 			 * Construct a DTrace enabling that will exit after
8927c478bd9Sstevel@tonic-gate 			 * the specified number of seconds.
8937c478bd9Sstevel@tonic-gate 			 */
8947c478bd9Sstevel@tonic-gate 			dprog_add("BEGIN\n{\n\tend = timestamp + ");
8957c478bd9Sstevel@tonic-gate 			dprog_add(optarg);
8967c478bd9Sstevel@tonic-gate 			dprog_add(" * 1000000000;\n}\n");
8977c478bd9Sstevel@tonic-gate 			dprog_add("tick-10hz\n/timestamp >= end/\n");
8987c478bd9Sstevel@tonic-gate 			dprog_add("{\n\texit(0);\n}\n");
8997c478bd9Sstevel@tonic-gate 			break;
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	argc -= optind;
9047c478bd9Sstevel@tonic-gate 	argv += optind;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (opt_H) {
9077c478bd9Sstevel@tonic-gate 		dprog_add(g_hold_init);
9087c478bd9Sstevel@tonic-gate 		if (g_opt_s == NULL)
9097c478bd9Sstevel@tonic-gate 			dprog_add(g_hold_times);
9107c478bd9Sstevel@tonic-gate 		else
9117c478bd9Sstevel@tonic-gate 			dprog_add(g_hold_histogram);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (opt_C) {
9157c478bd9Sstevel@tonic-gate 		dprog_add(g_ctnd_init);
9167c478bd9Sstevel@tonic-gate 		if (g_opt_s == NULL)
9177c478bd9Sstevel@tonic-gate 			dprog_add(g_ctnd_times);
9187c478bd9Sstevel@tonic-gate 		else
9197c478bd9Sstevel@tonic-gate 			dprog_add(g_ctnd_histogram);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (opt_p) {
9237c478bd9Sstevel@tonic-gate 		ulong_t pid;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		if (argc > 1) {
9267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: only one pid is allowed\n",
9277c478bd9Sstevel@tonic-gate 			    g_pname);
9287c478bd9Sstevel@tonic-gate 			usage();
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		errno = 0;
9327c478bd9Sstevel@tonic-gate 		pid = strtoul(argv[0], &end, 10);
9337c478bd9Sstevel@tonic-gate 		if (*end != '\0' || errno != 0 || (pid_t)pid != pid) {
9347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: invalid pid '%s'\n",
9357c478bd9Sstevel@tonic-gate 			    g_pname, argv[0]);
9367c478bd9Sstevel@tonic-gate 			usage();
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		if ((g_pr = dtrace_proc_grab(g_dtp, (pid_t)pid, 0)) == NULL)
9407c478bd9Sstevel@tonic-gate 			dfatal(NULL);
9417c478bd9Sstevel@tonic-gate 	} else {
9427c478bd9Sstevel@tonic-gate 		if ((g_pr = dtrace_proc_create(g_dtp, argv[0], argv)) == NULL)
9437c478bd9Sstevel@tonic-gate 			dfatal(NULL);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	dprog_compile();
9477c478bd9Sstevel@tonic-gate 
948*351346dbSahl 	if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
949*351346dbSahl 		dfatal("failed to establish proc handler");
950*351346dbSahl 
9517c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
9527c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
9537c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
9547c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
9557c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
9587c478bd9Sstevel@tonic-gate 		dfatal("dtrace_go()");
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "ustackframes", &g_nframes) != 0)
9617c478bd9Sstevel@tonic-gate 		dfatal("failed to get 'ustackframes'");
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	dtrace_proc_continue(g_dtp, g_pr);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (opt_v)
9667c478bd9Sstevel@tonic-gate 		(void) printf("%s: tracing enabled for pid %d\n", g_pname,
9677c478bd9Sstevel@tonic-gate 		    (int)Pstatus(g_pr)->pr_pid);
9687c478bd9Sstevel@tonic-gate 
969*351346dbSahl 	do {
970*351346dbSahl 		if (!g_intr && !done)
971*351346dbSahl 			dtrace_sleep(g_dtp);
9727c478bd9Sstevel@tonic-gate 
973*351346dbSahl 		if (done || g_intr || g_exited) {
9747c478bd9Sstevel@tonic-gate 			done = 1;
975*351346dbSahl 			if (dtrace_stop(g_dtp) == -1)
976*351346dbSahl 				dfatal("couldn't stop tracing");
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 
979*351346dbSahl 		switch (dtrace_work(g_dtp, stdout, NULL, chewrec, NULL)) {
980*351346dbSahl 		case DTRACE_WORKSTATUS_DONE:
981*351346dbSahl 			done = 1;
982*351346dbSahl 			break;
983*351346dbSahl 		case DTRACE_WORKSTATUS_OKAY:
984*351346dbSahl 			break;
985*351346dbSahl 		default:
986*351346dbSahl 			dfatal("processing aborted");
987*351346dbSahl 		}
9887c478bd9Sstevel@tonic-gate 
989*351346dbSahl 	} while (!done);
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	dtrace_close(g_dtp);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	return (0);
9947c478bd9Sstevel@tonic-gate }
995