xref: /illumos-gate/usr/src/cmd/lockstat/lockstat.c (revision ef150c2b)
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
59d68b18eSck  * Common Development and Distribution License (the "License").
69d68b18eSck  * 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  */
217c478bd9Sstevel@tonic-gate /*
229d68b18eSck  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stddef.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/wait.h>
417c478bd9Sstevel@tonic-gate #include <dtrace.h>
427c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
437c478bd9Sstevel@tonic-gate #include <alloca.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <assert.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	LOCKSTAT_OPTSTR	"x:bths:n:d:i:l:f:e:ckwWgCHEATID:RpPo:V"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	LS_MAX_STACK_DEPTH	50
507c478bd9Sstevel@tonic-gate #define	LS_MAX_EVENTS		64
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate typedef struct lsrec {
537c478bd9Sstevel@tonic-gate 	struct lsrec	*ls_next;	/* next in hash chain */
547c478bd9Sstevel@tonic-gate 	uintptr_t	ls_lock;	/* lock address */
557c478bd9Sstevel@tonic-gate 	uintptr_t	ls_caller;	/* caller address */
567c478bd9Sstevel@tonic-gate 	uint32_t	ls_count;	/* cumulative event count */
577c478bd9Sstevel@tonic-gate 	uint32_t	ls_event;	/* type of event */
587c478bd9Sstevel@tonic-gate 	uintptr_t	ls_refcnt;	/* cumulative reference count */
597c478bd9Sstevel@tonic-gate 	uint64_t	ls_time;	/* cumulative event duration */
607c478bd9Sstevel@tonic-gate 	uint32_t	ls_hist[64];	/* log2(duration) histogram */
617c478bd9Sstevel@tonic-gate 	uintptr_t	ls_stack[LS_MAX_STACK_DEPTH];
627c478bd9Sstevel@tonic-gate } lsrec_t;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate typedef struct lsdata {
657c478bd9Sstevel@tonic-gate 	struct lsrec	*lsd_next;	/* next available */
667c478bd9Sstevel@tonic-gate 	int		lsd_count;	/* number of records */
677c478bd9Sstevel@tonic-gate } lsdata_t;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Definitions for the types of experiments which can be run.  They are
717c478bd9Sstevel@tonic-gate  * listed in increasing order of memory cost and processing time cost.
727c478bd9Sstevel@tonic-gate  * The numerical value of each type is the number of bytes needed per record.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #define	LS_BASIC	offsetof(lsrec_t, ls_time)
757c478bd9Sstevel@tonic-gate #define	LS_TIME		offsetof(lsrec_t, ls_hist[0])
767c478bd9Sstevel@tonic-gate #define	LS_HIST		offsetof(lsrec_t, ls_stack[0])
777c478bd9Sstevel@tonic-gate #define	LS_STACK(depth)	offsetof(lsrec_t, ls_stack[depth])
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static void report_stats(FILE *, lsrec_t **, size_t, uint64_t, uint64_t);
807c478bd9Sstevel@tonic-gate static void report_trace(FILE *, lsrec_t **);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate extern int symtab_init(void);
837c478bd9Sstevel@tonic-gate extern char *addr_to_sym(uintptr_t, uintptr_t *, size_t *);
847c478bd9Sstevel@tonic-gate extern uintptr_t sym_to_addr(char *name);
857c478bd9Sstevel@tonic-gate extern size_t sym_size(char *name);
867c478bd9Sstevel@tonic-gate extern char *strtok_r(char *, const char *, char **);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	DEFAULT_NRECS	10000
897c478bd9Sstevel@tonic-gate #define	DEFAULT_HZ	97
907c478bd9Sstevel@tonic-gate #define	MAX_HZ		1000
917c478bd9Sstevel@tonic-gate #define	MIN_AGGSIZE	(16 * 1024)
927c478bd9Sstevel@tonic-gate #define	MAX_AGGSIZE	(32 * 1024 * 1024)
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int g_stkdepth;
957c478bd9Sstevel@tonic-gate static int g_topn = INT_MAX;
967c478bd9Sstevel@tonic-gate static hrtime_t g_elapsed;
977c478bd9Sstevel@tonic-gate static int g_rates = 0;
987c478bd9Sstevel@tonic-gate static int g_pflag = 0;
997c478bd9Sstevel@tonic-gate static int g_Pflag = 0;
1007c478bd9Sstevel@tonic-gate static int g_wflag = 0;
1017c478bd9Sstevel@tonic-gate static int g_Wflag = 0;
1027c478bd9Sstevel@tonic-gate static int g_cflag = 0;
1037c478bd9Sstevel@tonic-gate static int g_kflag = 0;
1047c478bd9Sstevel@tonic-gate static int g_gflag = 0;
1057c478bd9Sstevel@tonic-gate static int g_Vflag = 0;
1067c478bd9Sstevel@tonic-gate static int g_tracing = 0;
1077c478bd9Sstevel@tonic-gate static size_t g_recsize;
1087c478bd9Sstevel@tonic-gate static size_t g_nrecs;
1097c478bd9Sstevel@tonic-gate static int g_nrecs_used;
1107c478bd9Sstevel@tonic-gate static uchar_t g_enabled[LS_MAX_EVENTS];
1117c478bd9Sstevel@tonic-gate static hrtime_t g_min_duration[LS_MAX_EVENTS];
1127c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
1137c478bd9Sstevel@tonic-gate static char *g_predicate;
1147c478bd9Sstevel@tonic-gate static char *g_ipredicate;
1157c478bd9Sstevel@tonic-gate static char *g_prog;
1167c478bd9Sstevel@tonic-gate static int g_proglen;
1177c478bd9Sstevel@tonic-gate static int g_dropped;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate typedef struct ls_event_info {
1207c478bd9Sstevel@tonic-gate 	char	ev_type;
1217c478bd9Sstevel@tonic-gate 	char	ev_lhdr[20];
1227c478bd9Sstevel@tonic-gate 	char	ev_desc[80];
1237c478bd9Sstevel@tonic-gate 	char	ev_units[10];
1247c478bd9Sstevel@tonic-gate 	char	ev_name[DTRACE_NAMELEN];
1257c478bd9Sstevel@tonic-gate 	char	*ev_predicate;
1267c478bd9Sstevel@tonic-gate 	char	*ev_acquire;
1277c478bd9Sstevel@tonic-gate } ls_event_info_t;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static ls_event_info_t g_event_info[LS_MAX_EVENTS] = {
1309d68b18eSck 	{ 'C',	"Lock",	"Adaptive mutex spin",			"nsec",
1317c478bd9Sstevel@tonic-gate 	    "lockstat:::adaptive-spin" },
1327c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Adaptive mutex block",			"nsec",
1337c478bd9Sstevel@tonic-gate 	    "lockstat:::adaptive-block" },
1349d68b18eSck 	{ 'C',	"Lock",	"Spin lock spin",			"nsec",
1357c478bd9Sstevel@tonic-gate 	    "lockstat:::spin-spin" },
1369d68b18eSck 	{ 'C',	"Lock",	"Thread lock spin",			"nsec",
1377c478bd9Sstevel@tonic-gate 	    "lockstat:::thread-spin" },
1387c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"R/W writer blocked by writer",		"nsec",
1397c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-block", "arg2 == 0 && arg3 == 1" },
1407c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"R/W writer blocked by readers",	"nsec",
1417c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-block", "arg2 == 0 && arg3 == 0 && arg4" },
1427c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"R/W reader blocked by writer",		"nsec",
1437c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-block", "arg2 != 0 && arg3 == 1" },
1447c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"R/W reader blocked by write wanted",	"nsec",
1457c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-block", "arg2 != 0 && arg3 == 0 && arg4" },
1467c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 8)",		"units"	},
1477c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 9)",		"units"	},
1487c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 10)",		"units"	},
1497c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 11)",		"units"	},
1507c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 12)",		"units"	},
1517c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 13)",		"units"	},
1527c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 14)",		"units"	},
1537c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 15)",		"units"	},
1547c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 16)",		"units"	},
1557c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 17)",		"units"	},
1567c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 18)",		"units"	},
1577c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 19)",		"units"	},
1587c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 20)",		"units"	},
1597c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 21)",		"units"	},
1607c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 22)",		"units"	},
1617c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 23)",		"units"	},
1627c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 24)",		"units"	},
1637c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 25)",		"units"	},
1647c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 26)",		"units"	},
1657c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 27)",		"units"	},
1667c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 28)",		"units"	},
1677c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 29)",		"units"	},
1687c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 30)",		"units"	},
1697c478bd9Sstevel@tonic-gate 	{ 'C',	"Lock",	"Unknown event (type 31)",		"units"	},
1707c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Adaptive mutex hold",			"nsec",
1717c478bd9Sstevel@tonic-gate 	    "lockstat:::adaptive-release", NULL,
1727c478bd9Sstevel@tonic-gate 	    "lockstat:::adaptive-acquire" },
1737c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Spin lock hold",			"nsec",
1747c478bd9Sstevel@tonic-gate 	    "lockstat:::spin-release", NULL,
1757c478bd9Sstevel@tonic-gate 	    "lockstat:::spin-acquire" },
1767c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"R/W writer hold",			"nsec",
1777c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-release", "arg1 == 0",
1787c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-acquire" },
1797c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"R/W reader hold",			"nsec",
1807c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-release", "arg1 != 0",
1817c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-acquire" },
1827c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 36)",		"units"	},
1837c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 37)",		"units"	},
1847c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 38)",		"units"	},
1857c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 39)",		"units"	},
1867c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 40)",		"units"	},
1877c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 41)",		"units"	},
1887c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 42)",		"units"	},
1897c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 43)",		"units"	},
1907c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 44)",		"units"	},
1917c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 45)",		"units"	},
1927c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 46)",		"units"	},
1937c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 47)",		"units"	},
1947c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 48)",		"units"	},
1957c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 49)",		"units"	},
1967c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 50)",		"units"	},
1977c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 51)",		"units"	},
1987c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 52)",		"units"	},
1997c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 53)",		"units"	},
2007c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 54)",		"units"	},
2017c478bd9Sstevel@tonic-gate 	{ 'H',	"Lock",	"Unknown event (type 55)",		"units"	},
2027c478bd9Sstevel@tonic-gate 	{ 'I',	"CPU+PIL", "Profiling interrupt",		"nsec",
2037c478bd9Sstevel@tonic-gate 	    "profile:::profile-97", NULL },
2047c478bd9Sstevel@tonic-gate 	{ 'I',	"Lock",	"Unknown event (type 57)",		"units"	},
2057c478bd9Sstevel@tonic-gate 	{ 'I',	"Lock",	"Unknown event (type 58)",		"units"	},
2067c478bd9Sstevel@tonic-gate 	{ 'I',	"Lock",	"Unknown event (type 59)",		"units"	},
2077c478bd9Sstevel@tonic-gate 	{ 'E',	"Lock",	"Recursive lock entry detected",	"(N/A)",
2087c478bd9Sstevel@tonic-gate 	    "lockstat:::rw-release", NULL, "lockstat:::rw-acquire" },
2097c478bd9Sstevel@tonic-gate 	{ 'E',	"Lock",	"Lockstat enter failure",		"(N/A)"	},
2107c478bd9Sstevel@tonic-gate 	{ 'E',	"Lock",	"Lockstat exit failure",		"nsec"	},
2117c478bd9Sstevel@tonic-gate 	{ 'E',	"Lock",	"Lockstat record failure",		"(N/A)"	},
2127c478bd9Sstevel@tonic-gate };
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static void
fail(int do_perror,const char * message,...)2157c478bd9Sstevel@tonic-gate fail(int do_perror, const char *message, ...)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	va_list args;
2187c478bd9Sstevel@tonic-gate 	int save_errno = errno;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	va_start(args, message);
2217c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "lockstat: ");
2227c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
2237c478bd9Sstevel@tonic-gate 	va_end(args);
2247c478bd9Sstevel@tonic-gate 	if (do_perror)
2257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", strerror(save_errno));
2267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
2277c478bd9Sstevel@tonic-gate 	exit(2);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static void
dfail(const char * message,...)2317c478bd9Sstevel@tonic-gate dfail(const char *message, ...)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	va_list args;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	va_start(args, message);
2367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "lockstat: ");
2377c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
2387c478bd9Sstevel@tonic-gate 	va_end(args);
2397c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, ": %s\n",
2407c478bd9Sstevel@tonic-gate 	    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	exit(2);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate static void
show_events(char event_type,char * desc)2467c478bd9Sstevel@tonic-gate show_events(char event_type, char *desc)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	int i, first = -1, last;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	for (i = 0; i < LS_MAX_EVENTS; i++) {
2517c478bd9Sstevel@tonic-gate 		ls_event_info_t *evp = &g_event_info[i];
2527c478bd9Sstevel@tonic-gate 		if (evp->ev_type != event_type ||
2537c478bd9Sstevel@tonic-gate 		    strncmp(evp->ev_desc, "Unknown event", 13) == 0)
2547c478bd9Sstevel@tonic-gate 			continue;
2557c478bd9Sstevel@tonic-gate 		if (first == -1)
2567c478bd9Sstevel@tonic-gate 			first = i;
2577c478bd9Sstevel@tonic-gate 		last = i;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
2617c478bd9Sstevel@tonic-gate 	    "\n%s events (lockstat -%c or lockstat -e %d-%d):\n\n",
2627c478bd9Sstevel@tonic-gate 	    desc, event_type, first, last);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	for (i = first; i <= last; i++)
2657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2667c478bd9Sstevel@tonic-gate 		    "%4d = %s\n", i, g_event_info[i].ev_desc);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static void
usage(void)2707c478bd9Sstevel@tonic-gate usage(void)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
2737c478bd9Sstevel@tonic-gate 	    "Usage: lockstat [options] command [args]\n"
2747c478bd9Sstevel@tonic-gate 	    "\nEvent selection options:\n\n"
2757c478bd9Sstevel@tonic-gate 	    "  -C              watch contention events [on by default]\n"
2767c478bd9Sstevel@tonic-gate 	    "  -E              watch error events [off by default]\n"
2777c478bd9Sstevel@tonic-gate 	    "  -H              watch hold events [off by default]\n"
2787c478bd9Sstevel@tonic-gate 	    "  -I              watch interrupt events [off by default]\n"
2797c478bd9Sstevel@tonic-gate 	    "  -A              watch all lock events [equivalent to -CH]\n"
2807c478bd9Sstevel@tonic-gate 	    "  -e event_list   only watch the specified events (shown below);\n"
2817c478bd9Sstevel@tonic-gate 	    "                  <event_list> is a comma-separated list of\n"
2827c478bd9Sstevel@tonic-gate 	    "                  events or ranges of events, e.g. 1,4-7,35\n"
2837c478bd9Sstevel@tonic-gate 	    "  -i rate         interrupt rate for -I [default: %d Hz]\n"
2847c478bd9Sstevel@tonic-gate 	    "\nData gathering options:\n\n"
2857c478bd9Sstevel@tonic-gate 	    "  -b              basic statistics (lock, caller, event count)\n"
2867c478bd9Sstevel@tonic-gate 	    "  -t              timing for all events [default]\n"
2877c478bd9Sstevel@tonic-gate 	    "  -h              histograms for event times\n"
2887c478bd9Sstevel@tonic-gate 	    "  -s depth        stack traces <depth> deep\n"
2897c478bd9Sstevel@tonic-gate 	    "  -x opt[=val]    enable or modify DTrace options\n"
2907c478bd9Sstevel@tonic-gate 	    "\nData filtering options:\n\n"
2917c478bd9Sstevel@tonic-gate 	    "  -n nrecords     maximum number of data records [default: %d]\n"
2927c478bd9Sstevel@tonic-gate 	    "  -l lock[,size]  only watch <lock>, which can be specified as a\n"
2937c478bd9Sstevel@tonic-gate 	    "                  symbolic name or hex address; <size> defaults\n"
2947c478bd9Sstevel@tonic-gate 	    "                  to the ELF symbol size if available, 1 if not\n"
2957c478bd9Sstevel@tonic-gate 	    "  -f func[,size]  only watch events generated by <func>\n"
2967c478bd9Sstevel@tonic-gate 	    "  -d duration     only watch events longer than <duration>\n"
2977c478bd9Sstevel@tonic-gate 	    "  -T              trace (rather than sample) events\n"
2987c478bd9Sstevel@tonic-gate 	    "\nData reporting options:\n\n"
2997c478bd9Sstevel@tonic-gate 	    "  -c              coalesce lock data for arrays like pse_mutex[]\n"
3007c478bd9Sstevel@tonic-gate 	    "  -k              coalesce PCs within functions\n"
3017c478bd9Sstevel@tonic-gate 	    "  -g              show total events generated by function\n"
3027c478bd9Sstevel@tonic-gate 	    "  -w              wherever: don't distinguish events by caller\n"
3037c478bd9Sstevel@tonic-gate 	    "  -W              whichever: don't distinguish events by lock\n"
3047c478bd9Sstevel@tonic-gate 	    "  -R              display rates rather than counts\n"
3057c478bd9Sstevel@tonic-gate 	    "  -p              parsable output format (awk(1)-friendly)\n"
3067c478bd9Sstevel@tonic-gate 	    "  -P              sort lock data by (count * avg_time) product\n"
3077c478bd9Sstevel@tonic-gate 	    "  -D n            only display top <n> events of each type\n"
3087c478bd9Sstevel@tonic-gate 	    "  -o filename     send output to <filename>\n",
3097c478bd9Sstevel@tonic-gate 	    DEFAULT_HZ, DEFAULT_NRECS);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	show_events('C', "Contention");
3127c478bd9Sstevel@tonic-gate 	show_events('H', "Hold-time");
3137c478bd9Sstevel@tonic-gate 	show_events('I', "Interrupt");
3147c478bd9Sstevel@tonic-gate 	show_events('E', "Error");
3157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	exit(1);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static int
lockcmp(lsrec_t * a,lsrec_t * b)3217c478bd9Sstevel@tonic-gate lockcmp(lsrec_t *a, lsrec_t *b)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	int i;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
3267c478bd9Sstevel@tonic-gate 		return (-1);
3277c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
3287c478bd9Sstevel@tonic-gate 		return (1);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	for (i = g_stkdepth - 1; i >= 0; i--) {
3317c478bd9Sstevel@tonic-gate 		if (a->ls_stack[i] < b->ls_stack[i])
3327c478bd9Sstevel@tonic-gate 			return (-1);
3337c478bd9Sstevel@tonic-gate 		if (a->ls_stack[i] > b->ls_stack[i])
3347c478bd9Sstevel@tonic-gate 			return (1);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (a->ls_caller < b->ls_caller)
3387c478bd9Sstevel@tonic-gate 		return (-1);
3397c478bd9Sstevel@tonic-gate 	if (a->ls_caller > b->ls_caller)
3407c478bd9Sstevel@tonic-gate 		return (1);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (a->ls_lock < b->ls_lock)
3437c478bd9Sstevel@tonic-gate 		return (-1);
3447c478bd9Sstevel@tonic-gate 	if (a->ls_lock > b->ls_lock)
3457c478bd9Sstevel@tonic-gate 		return (1);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static int
countcmp(lsrec_t * a,lsrec_t * b)3517c478bd9Sstevel@tonic-gate countcmp(lsrec_t *a, lsrec_t *b)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
3547c478bd9Sstevel@tonic-gate 		return (-1);
3557c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
3567c478bd9Sstevel@tonic-gate 		return (1);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	return (b->ls_count - a->ls_count);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static int
timecmp(lsrec_t * a,lsrec_t * b)3627c478bd9Sstevel@tonic-gate timecmp(lsrec_t *a, lsrec_t *b)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
3657c478bd9Sstevel@tonic-gate 		return (-1);
3667c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
3677c478bd9Sstevel@tonic-gate 		return (1);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (a->ls_time < b->ls_time)
3707c478bd9Sstevel@tonic-gate 		return (1);
3717c478bd9Sstevel@tonic-gate 	if (a->ls_time > b->ls_time)
3727c478bd9Sstevel@tonic-gate 		return (-1);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	return (0);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static int
lockcmp_anywhere(lsrec_t * a,lsrec_t * b)3787c478bd9Sstevel@tonic-gate lockcmp_anywhere(lsrec_t *a, lsrec_t *b)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
3817c478bd9Sstevel@tonic-gate 		return (-1);
3827c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
3837c478bd9Sstevel@tonic-gate 		return (1);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (a->ls_lock < b->ls_lock)
3867c478bd9Sstevel@tonic-gate 		return (-1);
3877c478bd9Sstevel@tonic-gate 	if (a->ls_lock > b->ls_lock)
3887c478bd9Sstevel@tonic-gate 		return (1);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (0);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static int
lock_and_count_cmp_anywhere(lsrec_t * a,lsrec_t * b)3947c478bd9Sstevel@tonic-gate lock_and_count_cmp_anywhere(lsrec_t *a, lsrec_t *b)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
3977c478bd9Sstevel@tonic-gate 		return (-1);
3987c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
3997c478bd9Sstevel@tonic-gate 		return (1);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (a->ls_lock < b->ls_lock)
4027c478bd9Sstevel@tonic-gate 		return (-1);
4037c478bd9Sstevel@tonic-gate 	if (a->ls_lock > b->ls_lock)
4047c478bd9Sstevel@tonic-gate 		return (1);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	return (b->ls_count - a->ls_count);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate static int
sitecmp_anylock(lsrec_t * a,lsrec_t * b)4107c478bd9Sstevel@tonic-gate sitecmp_anylock(lsrec_t *a, lsrec_t *b)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	int i;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
4157c478bd9Sstevel@tonic-gate 		return (-1);
4167c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
4177c478bd9Sstevel@tonic-gate 		return (1);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	for (i = g_stkdepth - 1; i >= 0; i--) {
4207c478bd9Sstevel@tonic-gate 		if (a->ls_stack[i] < b->ls_stack[i])
4217c478bd9Sstevel@tonic-gate 			return (-1);
4227c478bd9Sstevel@tonic-gate 		if (a->ls_stack[i] > b->ls_stack[i])
4237c478bd9Sstevel@tonic-gate 			return (1);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (a->ls_caller < b->ls_caller)
4277c478bd9Sstevel@tonic-gate 		return (-1);
4287c478bd9Sstevel@tonic-gate 	if (a->ls_caller > b->ls_caller)
4297c478bd9Sstevel@tonic-gate 		return (1);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	return (0);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static int
site_and_count_cmp_anylock(lsrec_t * a,lsrec_t * b)4357c478bd9Sstevel@tonic-gate site_and_count_cmp_anylock(lsrec_t *a, lsrec_t *b)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	int i;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (a->ls_event < b->ls_event)
4407c478bd9Sstevel@tonic-gate 		return (-1);
4417c478bd9Sstevel@tonic-gate 	if (a->ls_event > b->ls_event)
4427c478bd9Sstevel@tonic-gate 		return (1);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	for (i = g_stkdepth - 1; i >= 0; i--) {
4457c478bd9Sstevel@tonic-gate 		if (a->ls_stack[i] < b->ls_stack[i])
4467c478bd9Sstevel@tonic-gate 			return (-1);
4477c478bd9Sstevel@tonic-gate 		if (a->ls_stack[i] > b->ls_stack[i])
4487c478bd9Sstevel@tonic-gate 			return (1);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (a->ls_caller < b->ls_caller)
4527c478bd9Sstevel@tonic-gate 		return (-1);
4537c478bd9Sstevel@tonic-gate 	if (a->ls_caller > b->ls_caller)
4547c478bd9Sstevel@tonic-gate 		return (1);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (b->ls_count - a->ls_count);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate static void
mergesort(int (* cmp)(lsrec_t *,lsrec_t *),lsrec_t ** a,lsrec_t ** b,int n)4607c478bd9Sstevel@tonic-gate mergesort(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **a, lsrec_t **b, int n)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	int m = n / 2;
4637c478bd9Sstevel@tonic-gate 	int i, j;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (m > 1)
4667c478bd9Sstevel@tonic-gate 		mergesort(cmp, a, b, m);
4677c478bd9Sstevel@tonic-gate 	if (n - m > 1)
4687c478bd9Sstevel@tonic-gate 		mergesort(cmp, a + m, b + m, n - m);
4697c478bd9Sstevel@tonic-gate 	for (i = m; i > 0; i--)
4707c478bd9Sstevel@tonic-gate 		b[i - 1] = a[i - 1];
4717c478bd9Sstevel@tonic-gate 	for (j = m - 1; j < n - 1; j++)
4727c478bd9Sstevel@tonic-gate 		b[n + m - j - 2] = a[j + 1];
4737c478bd9Sstevel@tonic-gate 	while (i < j)
4747c478bd9Sstevel@tonic-gate 		*a++ = cmp(b[i], b[j]) < 0 ? b[i++] : b[j--];
4757c478bd9Sstevel@tonic-gate 	*a = b[i];
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static void
coalesce(int (* cmp)(lsrec_t *,lsrec_t *),lsrec_t ** lock,int n)4797c478bd9Sstevel@tonic-gate coalesce(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **lock, int n)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	int i, j;
4827c478bd9Sstevel@tonic-gate 	lsrec_t *target, *current;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	target = lock[0];
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	for (i = 1; i < n; i++) {
4877c478bd9Sstevel@tonic-gate 		current = lock[i];
4887c478bd9Sstevel@tonic-gate 		if (cmp(current, target) != 0) {
4897c478bd9Sstevel@tonic-gate 			target = current;
4907c478bd9Sstevel@tonic-gate 			continue;
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		current->ls_event = LS_MAX_EVENTS;
4937c478bd9Sstevel@tonic-gate 		target->ls_count += current->ls_count;
4947c478bd9Sstevel@tonic-gate 		target->ls_refcnt += current->ls_refcnt;
4957c478bd9Sstevel@tonic-gate 		if (g_recsize < LS_TIME)
4967c478bd9Sstevel@tonic-gate 			continue;
4977c478bd9Sstevel@tonic-gate 		target->ls_time += current->ls_time;
4987c478bd9Sstevel@tonic-gate 		if (g_recsize < LS_HIST)
4997c478bd9Sstevel@tonic-gate 			continue;
5007c478bd9Sstevel@tonic-gate 		for (j = 0; j < 64; j++)
5017c478bd9Sstevel@tonic-gate 			target->ls_hist[j] += current->ls_hist[j];
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate static void
coalesce_symbol(uintptr_t * addrp)5067c478bd9Sstevel@tonic-gate coalesce_symbol(uintptr_t *addrp)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	uintptr_t symoff;
5097c478bd9Sstevel@tonic-gate 	size_t symsize;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (addr_to_sym(*addrp, &symoff, &symsize) != NULL && symoff < symsize)
5127c478bd9Sstevel@tonic-gate 		*addrp -= symoff;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static void
predicate_add(char ** pred,char * what,char * cmp,uintptr_t value)5167c478bd9Sstevel@tonic-gate predicate_add(char **pred, char *what, char *cmp, uintptr_t value)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	char *new;
5197c478bd9Sstevel@tonic-gate 	int len, newlen;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (what == NULL)
5227c478bd9Sstevel@tonic-gate 		return;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (*pred == NULL) {
5257c478bd9Sstevel@tonic-gate 		*pred = malloc(1);
5267c478bd9Sstevel@tonic-gate 		*pred[0] = '\0';
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	len = strlen(*pred);
5307c478bd9Sstevel@tonic-gate 	newlen = len + strlen(what) + 32 + strlen("( && )");
5317c478bd9Sstevel@tonic-gate 	new = malloc(newlen);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (*pred[0] != '\0') {
5347c478bd9Sstevel@tonic-gate 		if (cmp != NULL) {
5357c478bd9Sstevel@tonic-gate 			(void) sprintf(new, "(%s) && (%s %s 0x%p)",
5367c478bd9Sstevel@tonic-gate 			    *pred, what, cmp, (void *)value);
5377c478bd9Sstevel@tonic-gate 		} else {
5387c478bd9Sstevel@tonic-gate 			(void) sprintf(new, "(%s) && (%s)", *pred, what);
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 	} else {
5417c478bd9Sstevel@tonic-gate 		if (cmp != NULL) {
5427c478bd9Sstevel@tonic-gate 			(void) sprintf(new, "%s %s 0x%p",
5437c478bd9Sstevel@tonic-gate 			    what, cmp, (void *)value);
5447c478bd9Sstevel@tonic-gate 		} else {
5457c478bd9Sstevel@tonic-gate 			(void) sprintf(new, "%s", what);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	free(*pred);
5507c478bd9Sstevel@tonic-gate 	*pred = new;
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate static void
predicate_destroy(char ** pred)5547c478bd9Sstevel@tonic-gate predicate_destroy(char **pred)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	free(*pred);
5577c478bd9Sstevel@tonic-gate 	*pred = NULL;
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate static void
filter_add(char ** filt,char * what,uintptr_t base,uintptr_t size)5617c478bd9Sstevel@tonic-gate filter_add(char **filt, char *what, uintptr_t base, uintptr_t size)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	char buf[256], *c = buf, *new;
5647c478bd9Sstevel@tonic-gate 	int len, newlen;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (*filt == NULL) {
5677c478bd9Sstevel@tonic-gate 		*filt = malloc(1);
5687c478bd9Sstevel@tonic-gate 		*filt[0] = '\0';
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	(void) sprintf(c, "%s(%s >= 0x%p && %s < 0x%p)", *filt[0] != '\0' ?
5727c478bd9Sstevel@tonic-gate 	    " || " : "", what, (void *)base, what, (void *)(base + size));
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	newlen = (len = strlen(*filt) + 1) + strlen(c);
5757c478bd9Sstevel@tonic-gate 	new = malloc(newlen);
5767c478bd9Sstevel@tonic-gate 	bcopy(*filt, new, len);
5777c478bd9Sstevel@tonic-gate 	(void) strcat(new, c);
5787c478bd9Sstevel@tonic-gate 	free(*filt);
5797c478bd9Sstevel@tonic-gate 	*filt = new;
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate static void
filter_destroy(char ** filt)5837c478bd9Sstevel@tonic-gate filter_destroy(char **filt)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	free(*filt);
5867c478bd9Sstevel@tonic-gate 	*filt = NULL;
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate static void
dprog_add(const char * fmt,...)5907c478bd9Sstevel@tonic-gate dprog_add(const char *fmt, ...)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	va_list args;
5937c478bd9Sstevel@tonic-gate 	int size, offs;
5947c478bd9Sstevel@tonic-gate 	char c;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
5977c478bd9Sstevel@tonic-gate 	size = vsnprintf(&c, 1, fmt, args) + 1;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (g_proglen == 0) {
6007c478bd9Sstevel@tonic-gate 		offs = 0;
6017c478bd9Sstevel@tonic-gate 	} else {
6027c478bd9Sstevel@tonic-gate 		offs = g_proglen - 1;
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	g_proglen = offs + size;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if ((g_prog = realloc(g_prog, g_proglen)) == NULL)
6087c478bd9Sstevel@tonic-gate 		fail(1, "failed to reallocate program text");
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	(void) vsnprintf(&g_prog[offs], size, fmt, args);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate  * This function may read like an open sewer, but keep in mind that programs
6157c478bd9Sstevel@tonic-gate  * that generate other programs are rarely pretty.  If one has the unenviable
6167c478bd9Sstevel@tonic-gate  * task of maintaining or -- worse -- extending this code, use the -V option
6177c478bd9Sstevel@tonic-gate  * to examine the D program as generated by this function.
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate static void
dprog_addevent(int event)6207c478bd9Sstevel@tonic-gate dprog_addevent(int event)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	ls_event_info_t *info = &g_event_info[event];
6237c478bd9Sstevel@tonic-gate 	char *pred = NULL;
6247c478bd9Sstevel@tonic-gate 	char stack[20];
6257c478bd9Sstevel@tonic-gate 	const char *arg0, *caller;
6267c478bd9Sstevel@tonic-gate 	char *arg1 = "arg1";
6277c478bd9Sstevel@tonic-gate 	char buf[80];
6287c478bd9Sstevel@tonic-gate 	hrtime_t dur;
6297c478bd9Sstevel@tonic-gate 	int depth;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (info->ev_name[0] == '\0')
6327c478bd9Sstevel@tonic-gate 		return;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (info->ev_type == 'I') {
6357c478bd9Sstevel@tonic-gate 		/*
6367c478bd9Sstevel@tonic-gate 		 * For interrupt events, arg0 (normally the lock pointer) is
6377c478bd9Sstevel@tonic-gate 		 * the CPU address plus the current pil, and arg1 (normally
6387c478bd9Sstevel@tonic-gate 		 * the number of nanoseconds) is the number of nanoseconds
6397c478bd9Sstevel@tonic-gate 		 * late -- and it's stored in arg2.
6407c478bd9Sstevel@tonic-gate 		 */
6417c478bd9Sstevel@tonic-gate 		arg0 = "(uintptr_t)curthread->t_cpu + \n"
6427c478bd9Sstevel@tonic-gate 		    "\t    curthread->t_cpu->cpu_profile_pil";
6437c478bd9Sstevel@tonic-gate 		caller = "(uintptr_t)arg0";
6447c478bd9Sstevel@tonic-gate 		arg1 = "arg2";
6457c478bd9Sstevel@tonic-gate 	} else {
6467c478bd9Sstevel@tonic-gate 		arg0 = "(uintptr_t)arg0";
6477c478bd9Sstevel@tonic-gate 		caller = "caller";
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (g_recsize > LS_HIST) {
6517c478bd9Sstevel@tonic-gate 		for (depth = 0; g_recsize > LS_STACK(depth); depth++)
6527c478bd9Sstevel@tonic-gate 			continue;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		if (g_tracing) {
6557c478bd9Sstevel@tonic-gate 			(void) sprintf(stack, "\tstack(%d);\n", depth);
6567c478bd9Sstevel@tonic-gate 		} else {
6577c478bd9Sstevel@tonic-gate 			(void) sprintf(stack, ", stack(%d)", depth);
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	} else {
6607c478bd9Sstevel@tonic-gate 		(void) sprintf(stack, "");
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (info->ev_acquire != NULL) {
6647c478bd9Sstevel@tonic-gate 		/*
6657c478bd9Sstevel@tonic-gate 		 * If this is a hold event, we need to generate an additional
6667c478bd9Sstevel@tonic-gate 		 * clause for the acquire; the clause for the release will be
6677c478bd9Sstevel@tonic-gate 		 * generated with the aggregating statement, below.
6687c478bd9Sstevel@tonic-gate 		 */
6697c478bd9Sstevel@tonic-gate 		dprog_add("%s\n", info->ev_acquire);
6707c478bd9Sstevel@tonic-gate 		predicate_add(&pred, info->ev_predicate, NULL, 0);
6717c478bd9Sstevel@tonic-gate 		predicate_add(&pred, g_predicate, NULL, 0);
6727c478bd9Sstevel@tonic-gate 		if (pred != NULL)
6737c478bd9Sstevel@tonic-gate 			dprog_add("/%s/\n", pred);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		dprog_add("{\n");
6767c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "self->ev%d[(uintptr_t)arg0]", event);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		if (info->ev_type == 'H') {
6797c478bd9Sstevel@tonic-gate 			dprog_add("\t%s = timestamp;\n", buf);
6807c478bd9Sstevel@tonic-gate 		} else {
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * If this isn't a hold event, it's the recursive
6837c478bd9Sstevel@tonic-gate 			 * error event.  For this, we simply bump the
6847c478bd9Sstevel@tonic-gate 			 * thread-local, per-lock count.
6857c478bd9Sstevel@tonic-gate 			 */
6867c478bd9Sstevel@tonic-gate 			dprog_add("\t%s++;\n", buf);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		dprog_add("}\n\n");
6907c478bd9Sstevel@tonic-gate 		predicate_destroy(&pred);
6917c478bd9Sstevel@tonic-gate 		pred = NULL;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		if (info->ev_type == 'E') {
6947c478bd9Sstevel@tonic-gate 			/*
6957c478bd9Sstevel@tonic-gate 			 * If this is the recursive lock error event, we need
6967c478bd9Sstevel@tonic-gate 			 * to generate an additional clause to decrement the
6977c478bd9Sstevel@tonic-gate 			 * thread-local, per-lock count.  This assures that we
6987c478bd9Sstevel@tonic-gate 			 * only execute the aggregating clause if we have
6997c478bd9Sstevel@tonic-gate 			 * recursive entry.
7007c478bd9Sstevel@tonic-gate 			 */
7017c478bd9Sstevel@tonic-gate 			dprog_add("%s\n", info->ev_name);
7027c478bd9Sstevel@tonic-gate 			dprog_add("/%s/\n{\n\t%s--;\n}\n\n", buf, buf);
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		predicate_add(&pred, buf, NULL, 0);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (info->ev_type == 'H') {
7087c478bd9Sstevel@tonic-gate 			(void) sprintf(buf, "timestamp -\n\t    "
7097c478bd9Sstevel@tonic-gate 			    "self->ev%d[(uintptr_t)arg0]", event);
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		arg1 = buf;
7137c478bd9Sstevel@tonic-gate 	} else {
7147c478bd9Sstevel@tonic-gate 		predicate_add(&pred, info->ev_predicate, NULL, 0);
7157c478bd9Sstevel@tonic-gate 		if (info->ev_type != 'I')
7167c478bd9Sstevel@tonic-gate 			predicate_add(&pred, g_predicate, NULL, 0);
7177c478bd9Sstevel@tonic-gate 		else
7187c478bd9Sstevel@tonic-gate 			predicate_add(&pred, g_ipredicate, NULL, 0);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if ((dur = g_min_duration[event]) != 0)
7227c478bd9Sstevel@tonic-gate 		predicate_add(&pred, arg1, ">=", dur);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	dprog_add("%s\n", info->ev_name);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (pred != NULL)
7277c478bd9Sstevel@tonic-gate 		dprog_add("/%s/\n", pred);
7287c478bd9Sstevel@tonic-gate 	predicate_destroy(&pred);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	dprog_add("{\n");
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (g_tracing) {
7337c478bd9Sstevel@tonic-gate 		dprog_add("\ttrace(%dULL);\n", event);
7347c478bd9Sstevel@tonic-gate 		dprog_add("\ttrace(%s);\n", arg0);
7357c478bd9Sstevel@tonic-gate 		dprog_add("\ttrace(%s);\n", caller);
7367c478bd9Sstevel@tonic-gate 		dprog_add(stack);
7377c478bd9Sstevel@tonic-gate 	} else {
73802198c0bSbmc 		/*
73902198c0bSbmc 		 * The ordering here is important:  when we process the
74002198c0bSbmc 		 * aggregate, we count on the fact that @avg appears before
74102198c0bSbmc 		 * @hist in program order to assure that @avg is assigned the
74202198c0bSbmc 		 * first aggregation variable ID and @hist assigned the
74302198c0bSbmc 		 * second; see the comment in process_aggregate() for details.
74402198c0bSbmc 		 */
7457c478bd9Sstevel@tonic-gate 		dprog_add("\t@avg[%dULL, %s, %s%s] = avg(%s);\n",
7467c478bd9Sstevel@tonic-gate 		    event, arg0, caller, stack, arg1);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		if (g_recsize >= LS_HIST) {
7497c478bd9Sstevel@tonic-gate 			dprog_add("\t@hist[%dULL, %s, %s%s] = quantize"
7507c478bd9Sstevel@tonic-gate 			    "(%s);\n", event, arg0, caller, stack, arg1);
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (info->ev_acquire != NULL)
7557c478bd9Sstevel@tonic-gate 		dprog_add("\tself->ev%d[arg0] = 0;\n", event);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	dprog_add("}\n\n");
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static void
dprog_compile()7617c478bd9Sstevel@tonic-gate dprog_compile()
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	dtrace_prog_t *prog;
7647c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t info;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (g_Vflag) {
7677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "lockstat: vvvv D program vvvv\n");
7687c478bd9Sstevel@tonic-gate 		(void) fputs(g_prog, stderr);
7697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "lockstat: ^^^^ D program ^^^^\n");
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
7737c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
7747c478bd9Sstevel@tonic-gate 		dfail("failed to compile program");
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (dtrace_program_exec(g_dtp, prog, &info) == -1)
7777c478bd9Sstevel@tonic-gate 		dfail("failed to enable probes");
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) != 0)
7807c478bd9Sstevel@tonic-gate 		dfail("couldn't start tracing");
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate static void
status_fire(int signal __unused)78430699046SRichard Lowe status_fire(int signal __unused)
78530699046SRichard Lowe {
78630699046SRichard Lowe }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static void
status_init(void)7897c478bd9Sstevel@tonic-gate status_init(void)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	dtrace_optval_t val, status, agg;
7927c478bd9Sstevel@tonic-gate 	struct sigaction act;
7937c478bd9Sstevel@tonic-gate 	struct itimerspec ts;
7947c478bd9Sstevel@tonic-gate 	struct sigevent ev;
7957c478bd9Sstevel@tonic-gate 	timer_t tid;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "statusrate", &status) == -1)
7987c478bd9Sstevel@tonic-gate 		dfail("failed to get 'statusrate'");
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (dtrace_getopt(g_dtp, "aggrate", &agg) == -1)
8017c478bd9Sstevel@tonic-gate 		dfail("failed to get 'statusrate'");
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/*
8047c478bd9Sstevel@tonic-gate 	 * We would want to awaken at a rate that is the GCD of the statusrate
8057c478bd9Sstevel@tonic-gate 	 * and the aggrate -- but that seems a bit absurd.  Instead, we'll
8067c478bd9Sstevel@tonic-gate 	 * simply awaken at a rate that is the more frequent of the two, which
8077c478bd9Sstevel@tonic-gate 	 * assures that we're never later than the interval implied by the
8087c478bd9Sstevel@tonic-gate 	 * more frequent rate.
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	val = status < agg ? status : agg;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
8137c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
8147c478bd9Sstevel@tonic-gate 	act.sa_handler = status_fire;
8157c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	ev.sigev_notify = SIGEV_SIGNAL;
8187c478bd9Sstevel@tonic-gate 	ev.sigev_signo = SIGUSR1;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
8217c478bd9Sstevel@tonic-gate 		dfail("cannot create CLOCK_REALTIME timer");
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	ts.it_value.tv_sec = val / NANOSEC;
8247c478bd9Sstevel@tonic-gate 	ts.it_value.tv_nsec = val % NANOSEC;
8257c478bd9Sstevel@tonic-gate 	ts.it_interval = ts.it_value;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
8287c478bd9Sstevel@tonic-gate 		dfail("cannot set time on CLOCK_REALTIME timer");
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate static void
status_check(void)8327c478bd9Sstevel@tonic-gate status_check(void)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	if (!g_tracing && dtrace_aggregate_snap(g_dtp) != 0)
8357c478bd9Sstevel@tonic-gate 		dfail("failed to snap aggregate");
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (dtrace_status(g_dtp) == -1)
8387c478bd9Sstevel@tonic-gate 		dfail("dtrace_status()");
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate static void
lsrec_fill(lsrec_t * lsrec,const dtrace_recdesc_t * rec,int nrecs,caddr_t data)842a1b5e537Sbmc lsrec_fill(lsrec_t *lsrec, const dtrace_recdesc_t *rec, int nrecs, caddr_t data)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	bzero(lsrec, g_recsize);
8457c478bd9Sstevel@tonic-gate 	lsrec->ls_count = 1;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if ((g_recsize > LS_HIST && nrecs < 4) || (nrecs < 3))
8487c478bd9Sstevel@tonic-gate 		fail(0, "truncated DTrace record");
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (rec->dtrd_size != sizeof (uint64_t))
8517c478bd9Sstevel@tonic-gate 		fail(0, "bad event size in first record");
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
8547c478bd9Sstevel@tonic-gate 	lsrec->ls_event = (uint32_t)*((uint64_t *)(data + rec->dtrd_offset));
8557c478bd9Sstevel@tonic-gate 	rec++;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (rec->dtrd_size != sizeof (uintptr_t))
8587c478bd9Sstevel@tonic-gate 		fail(0, "bad lock address size in second record");
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
8617c478bd9Sstevel@tonic-gate 	lsrec->ls_lock = *((uintptr_t *)(data + rec->dtrd_offset));
8627c478bd9Sstevel@tonic-gate 	rec++;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (rec->dtrd_size != sizeof (uintptr_t))
8657c478bd9Sstevel@tonic-gate 		fail(0, "bad caller size in third record");
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
8687c478bd9Sstevel@tonic-gate 	lsrec->ls_caller = *((uintptr_t *)(data + rec->dtrd_offset));
8697c478bd9Sstevel@tonic-gate 	rec++;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (g_recsize > LS_HIST) {
8727c478bd9Sstevel@tonic-gate 		int frames, i;
8737c478bd9Sstevel@tonic-gate 		pc_t *stack;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		frames = rec->dtrd_size / sizeof (pc_t);
8767c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
8777c478bd9Sstevel@tonic-gate 		stack = (pc_t *)(data + rec->dtrd_offset);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		for (i = 1; i < frames; i++)
8807c478bd9Sstevel@tonic-gate 			lsrec->ls_stack[i - 1] = stack[i];
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8857c478bd9Sstevel@tonic-gate static int
count_aggregate(const dtrace_aggdata_t * agg,void * arg)886a1b5e537Sbmc count_aggregate(const dtrace_aggdata_t *agg, void *arg)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	*((size_t *)arg) += 1;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate static int
process_aggregate(const dtrace_aggdata_t * agg,void * arg)894a1b5e537Sbmc process_aggregate(const dtrace_aggdata_t *agg, void *arg)
8957c478bd9Sstevel@tonic-gate {
896a1b5e537Sbmc 	const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
8977c478bd9Sstevel@tonic-gate 	caddr_t data = agg->dtada_data;
8987c478bd9Sstevel@tonic-gate 	lsdata_t *lsdata = arg;
8997c478bd9Sstevel@tonic-gate 	lsrec_t *lsrec = lsdata->lsd_next;
900a1b5e537Sbmc 	const dtrace_recdesc_t *rec;
9017c478bd9Sstevel@tonic-gate 	uint64_t *avg, *quantized;
9027c478bd9Sstevel@tonic-gate 	int i, j;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	assert(lsdata->lsd_count < g_nrecs);
9057c478bd9Sstevel@tonic-gate 
90602198c0bSbmc 	/*
90702198c0bSbmc 	 * Aggregation variable IDs are guaranteed to be generated in program
90802198c0bSbmc 	 * order, and they are guaranteed to start from DTRACE_AGGVARIDNONE
90902198c0bSbmc 	 * plus one.  As "avg" appears before "hist" in program order, we know
91002198c0bSbmc 	 * that "avg" will be allocated the first aggregation variable ID, and
91102198c0bSbmc 	 * "hist" will be allocated the second aggregation variable ID -- and
91202198c0bSbmc 	 * we therefore use the aggregation variable ID to differentiate the
91302198c0bSbmc 	 * cases.
91402198c0bSbmc 	 */
91502198c0bSbmc 	if (aggdesc->dtagd_varid > DTRACE_AGGVARIDNONE + 1) {
9167c478bd9Sstevel@tonic-gate 		/*
91702198c0bSbmc 		 * If this is the histogram entry.  We'll copy the quantized
91802198c0bSbmc 		 * data into lc_hist, and jump over the rest.
9197c478bd9Sstevel@tonic-gate 		 */
9207c478bd9Sstevel@tonic-gate 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
9217c478bd9Sstevel@tonic-gate 
92202198c0bSbmc 		if (aggdesc->dtagd_varid != DTRACE_AGGVARIDNONE + 2)
92302198c0bSbmc 			fail(0, "bad variable ID in aggregation record");
92402198c0bSbmc 
9257c478bd9Sstevel@tonic-gate 		if (rec->dtrd_size !=
9267c478bd9Sstevel@tonic-gate 		    DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
9277c478bd9Sstevel@tonic-gate 			fail(0, "bad quantize size in aggregation record");
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
9307c478bd9Sstevel@tonic-gate 		quantized = (uint64_t *)(data + rec->dtrd_offset);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		for (i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
9337c478bd9Sstevel@tonic-gate 		    i < DTRACE_QUANTIZE_NBUCKETS; i++, j++)
9347c478bd9Sstevel@tonic-gate 			lsrec->ls_hist[j] = quantized[i];
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 		goto out;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	lsrec_fill(lsrec, &aggdesc->dtagd_rec[1],
9407c478bd9Sstevel@tonic-gate 	    aggdesc->dtagd_nrecs - 1, data);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (rec->dtrd_size != 2 * sizeof (uint64_t))
9457c478bd9Sstevel@tonic-gate 		fail(0, "bad avg size in aggregation record");
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
9487c478bd9Sstevel@tonic-gate 	avg = (uint64_t *)(data + rec->dtrd_offset);
9497c478bd9Sstevel@tonic-gate 	lsrec->ls_count = (uint32_t)avg[0];
9507c478bd9Sstevel@tonic-gate 	lsrec->ls_time = (uintptr_t)avg[1];
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (g_recsize >= LS_HIST)
9537c478bd9Sstevel@tonic-gate 		return (DTRACE_AGGWALK_NEXT);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate out:
9567c478bd9Sstevel@tonic-gate 	lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
9577c478bd9Sstevel@tonic-gate 	lsdata->lsd_count++;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	return (DTRACE_AGGWALK_NEXT);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate static int
process_trace(const dtrace_probedata_t * pdata,void * arg)9637c478bd9Sstevel@tonic-gate process_trace(const dtrace_probedata_t *pdata, void *arg)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	lsdata_t *lsdata = arg;
9667c478bd9Sstevel@tonic-gate 	lsrec_t *lsrec = lsdata->lsd_next;
9677c478bd9Sstevel@tonic-gate 	dtrace_eprobedesc_t *edesc = pdata->dtpda_edesc;
9687c478bd9Sstevel@tonic-gate 	caddr_t data = pdata->dtpda_data;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (lsdata->lsd_count >= g_nrecs)
9717c478bd9Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	lsrec_fill(lsrec, edesc->dtepd_rec, edesc->dtepd_nrecs, data);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
9767c478bd9Sstevel@tonic-gate 	lsdata->lsd_count++;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	return (DTRACE_CONSUME_NEXT);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate static int
process_data(FILE * out,char * data)9827c478bd9Sstevel@tonic-gate process_data(FILE *out, char *data)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	lsdata_t lsdata;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
9877c478bd9Sstevel@tonic-gate 	lsdata.lsd_next = (lsrec_t *)data;
9887c478bd9Sstevel@tonic-gate 	lsdata.lsd_count = 0;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (g_tracing) {
9917c478bd9Sstevel@tonic-gate 		if (dtrace_consume(g_dtp, out,
9927c478bd9Sstevel@tonic-gate 		    process_trace, NULL, &lsdata) != 0)
9937c478bd9Sstevel@tonic-gate 			dfail("failed to consume buffer");
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		return (lsdata.lsd_count);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
9997c478bd9Sstevel@tonic-gate 	    process_aggregate, &lsdata) != 0)
10007c478bd9Sstevel@tonic-gate 		dfail("failed to walk aggregate");
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	return (lsdata.lsd_count);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10067c478bd9Sstevel@tonic-gate static int
drophandler(const dtrace_dropdata_t * data,void * arg)1007a1b5e537Sbmc drophandler(const dtrace_dropdata_t *data, void *arg)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	g_dropped++;
10107c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "lockstat: warning: %s", data->dtdda_msg);
10117c478bd9Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)10157c478bd9Sstevel@tonic-gate main(int argc, char **argv)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	char *data_buf;
10187c478bd9Sstevel@tonic-gate 	lsrec_t *lsp, **current, **first, **sort_buf, **merge_buf;
10197c478bd9Sstevel@tonic-gate 	FILE *out = stdout;
1020*ef150c2bSRichard Lowe 	int c;
10217c478bd9Sstevel@tonic-gate 	pid_t child;
10227c478bd9Sstevel@tonic-gate 	int status;
10237c478bd9Sstevel@tonic-gate 	int i, j;
10247c478bd9Sstevel@tonic-gate 	hrtime_t duration;
10257c478bd9Sstevel@tonic-gate 	char *addrp, *offp, *sizep, *evp, *lastp, *p;
10267c478bd9Sstevel@tonic-gate 	uintptr_t addr;
10277c478bd9Sstevel@tonic-gate 	size_t size, off;
10287c478bd9Sstevel@tonic-gate 	int events_specified = 0;
10297c478bd9Sstevel@tonic-gate 	int exec_errno = 0;
10307c478bd9Sstevel@tonic-gate 	uint32_t event;
10317c478bd9Sstevel@tonic-gate 	char *filt = NULL, *ifilt = NULL;
10327c478bd9Sstevel@tonic-gate 	static uint64_t ev_count[LS_MAX_EVENTS + 1];
10337c478bd9Sstevel@tonic-gate 	static uint64_t ev_time[LS_MAX_EVENTS + 1];
10347c478bd9Sstevel@tonic-gate 	dtrace_optval_t aggsize;
10357c478bd9Sstevel@tonic-gate 	char aggstr[10];
10367c478bd9Sstevel@tonic-gate 	long ncpus;
10377c478bd9Sstevel@tonic-gate 	int dynvar = 0;
10387c478bd9Sstevel@tonic-gate 	int err;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
10417c478bd9Sstevel@tonic-gate 		fail(0, "cannot open dtrace library: %s",
10427c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
10467c478bd9Sstevel@tonic-gate 		dfail("couldn't establish drop handler");
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (symtab_init() == -1)
10497c478bd9Sstevel@tonic-gate 		fail(1, "can't load kernel symbols");
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	g_nrecs = DEFAULT_NRECS;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != EOF) {
10547c478bd9Sstevel@tonic-gate 		switch (c) {
10557c478bd9Sstevel@tonic-gate 		case 'b':
10567c478bd9Sstevel@tonic-gate 			g_recsize = LS_BASIC;
10577c478bd9Sstevel@tonic-gate 			break;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		case 't':
10607c478bd9Sstevel@tonic-gate 			g_recsize = LS_TIME;
10617c478bd9Sstevel@tonic-gate 			break;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		case 'h':
10647c478bd9Sstevel@tonic-gate 			g_recsize = LS_HIST;
10657c478bd9Sstevel@tonic-gate 			break;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 		case 's':
10687c478bd9Sstevel@tonic-gate 			if (!isdigit(optarg[0]))
10697c478bd9Sstevel@tonic-gate 				usage();
10707c478bd9Sstevel@tonic-gate 			g_stkdepth = atoi(optarg);
10717c478bd9Sstevel@tonic-gate 			if (g_stkdepth > LS_MAX_STACK_DEPTH)
10727c478bd9Sstevel@tonic-gate 				fail(0, "max stack depth is %d",
10737c478bd9Sstevel@tonic-gate 				    LS_MAX_STACK_DEPTH);
10747c478bd9Sstevel@tonic-gate 			g_recsize = LS_STACK(g_stkdepth);
10757c478bd9Sstevel@tonic-gate 			break;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		case 'n':
10787c478bd9Sstevel@tonic-gate 			if (!isdigit(optarg[0]))
10797c478bd9Sstevel@tonic-gate 				usage();
10807c478bd9Sstevel@tonic-gate 			g_nrecs = atoi(optarg);
10817c478bd9Sstevel@tonic-gate 			break;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 		case 'd':
10847c478bd9Sstevel@tonic-gate 			if (!isdigit(optarg[0]))
10857c478bd9Sstevel@tonic-gate 				usage();
10867c478bd9Sstevel@tonic-gate 			duration = atoll(optarg);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 			/*
10897c478bd9Sstevel@tonic-gate 			 * XXX -- durations really should be per event
10907c478bd9Sstevel@tonic-gate 			 * since the units are different, but it's hard
10917c478bd9Sstevel@tonic-gate 			 * to express this nicely in the interface.
10927c478bd9Sstevel@tonic-gate 			 * Not clear yet what the cleanest solution is.
10937c478bd9Sstevel@tonic-gate 			 */
10947c478bd9Sstevel@tonic-gate 			for (i = 0; i < LS_MAX_EVENTS; i++)
10957c478bd9Sstevel@tonic-gate 				if (g_event_info[i].ev_type != 'E')
10967c478bd9Sstevel@tonic-gate 					g_min_duration[i] = duration;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 			break;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		case 'i':
11017c478bd9Sstevel@tonic-gate 			if (!isdigit(optarg[0]))
11027c478bd9Sstevel@tonic-gate 				usage();
11037c478bd9Sstevel@tonic-gate 			i = atoi(optarg);
11047c478bd9Sstevel@tonic-gate 			if (i <= 0)
11057c478bd9Sstevel@tonic-gate 				usage();
11067c478bd9Sstevel@tonic-gate 			if (i > MAX_HZ)
11077c478bd9Sstevel@tonic-gate 				fail(0, "max interrupt rate is %d Hz", MAX_HZ);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 			for (j = 0; j < LS_MAX_EVENTS; j++)
11107c478bd9Sstevel@tonic-gate 				if (strcmp(g_event_info[j].ev_desc,
11117c478bd9Sstevel@tonic-gate 				    "Profiling interrupt") == 0)
11127c478bd9Sstevel@tonic-gate 					break;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 			(void) sprintf(g_event_info[j].ev_name,
11157c478bd9Sstevel@tonic-gate 			    "profile:::profile-%d", i);
11167c478bd9Sstevel@tonic-gate 			break;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		case 'l':
11197c478bd9Sstevel@tonic-gate 		case 'f':
11207c478bd9Sstevel@tonic-gate 			addrp = strtok(optarg, ",");
11217c478bd9Sstevel@tonic-gate 			sizep = strtok(NULL, ",");
11227c478bd9Sstevel@tonic-gate 			addrp = strtok(optarg, ",+");
11237c478bd9Sstevel@tonic-gate 			offp = strtok(NULL, ",");
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 			size = sizep ? strtoul(sizep, NULL, 0) : 1;
11267c478bd9Sstevel@tonic-gate 			off = offp ? strtoul(offp, NULL, 0) : 0;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 			if (addrp[0] == '0') {
11297c478bd9Sstevel@tonic-gate 				addr = strtoul(addrp, NULL, 16) + off;
11307c478bd9Sstevel@tonic-gate 			} else {
11317c478bd9Sstevel@tonic-gate 				addr = sym_to_addr(addrp) + off;
11327c478bd9Sstevel@tonic-gate 				if (sizep == NULL)
11337c478bd9Sstevel@tonic-gate 					size = sym_size(addrp) - off;
11347c478bd9Sstevel@tonic-gate 				if (addr - off == 0)
11357c478bd9Sstevel@tonic-gate 					fail(0, "symbol '%s' not found", addrp);
11367c478bd9Sstevel@tonic-gate 				if (size == 0)
11377c478bd9Sstevel@tonic-gate 					size = 1;
11387c478bd9Sstevel@tonic-gate 			}
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 			if (c == 'l') {
11427c478bd9Sstevel@tonic-gate 				filter_add(&filt, "arg0", addr, size);
11437c478bd9Sstevel@tonic-gate 			} else {
11447c478bd9Sstevel@tonic-gate 				filter_add(&filt, "caller", addr, size);
11457c478bd9Sstevel@tonic-gate 				filter_add(&ifilt, "arg0", addr, size);
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 			break;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 		case 'e':
11507c478bd9Sstevel@tonic-gate 			evp = strtok_r(optarg, ",", &lastp);
11517c478bd9Sstevel@tonic-gate 			while (evp) {
11527c478bd9Sstevel@tonic-gate 				int ev1, ev2;
11537c478bd9Sstevel@tonic-gate 				char *evp2;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 				(void) strtok(evp, "-");
11567c478bd9Sstevel@tonic-gate 				evp2 = strtok(NULL, "-");
11577c478bd9Sstevel@tonic-gate 				ev1 = atoi(evp);
11587c478bd9Sstevel@tonic-gate 				ev2 = evp2 ? atoi(evp2) : ev1;
11597c478bd9Sstevel@tonic-gate 				if ((uint_t)ev1 >= LS_MAX_EVENTS ||
11607c478bd9Sstevel@tonic-gate 				    (uint_t)ev2 >= LS_MAX_EVENTS || ev1 > ev2)
11617c478bd9Sstevel@tonic-gate 					fail(0, "-e events out of range");
11627c478bd9Sstevel@tonic-gate 				for (i = ev1; i <= ev2; i++)
11637c478bd9Sstevel@tonic-gate 					g_enabled[i] = 1;
11647c478bd9Sstevel@tonic-gate 				evp = strtok_r(NULL, ",", &lastp);
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 			events_specified = 1;
11677c478bd9Sstevel@tonic-gate 			break;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		case 'c':
11707c478bd9Sstevel@tonic-gate 			g_cflag = 1;
11717c478bd9Sstevel@tonic-gate 			break;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		case 'k':
11747c478bd9Sstevel@tonic-gate 			g_kflag = 1;
11757c478bd9Sstevel@tonic-gate 			break;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		case 'w':
11787c478bd9Sstevel@tonic-gate 			g_wflag = 1;
11797c478bd9Sstevel@tonic-gate 			break;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		case 'W':
11827c478bd9Sstevel@tonic-gate 			g_Wflag = 1;
11837c478bd9Sstevel@tonic-gate 			break;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		case 'g':
11867c478bd9Sstevel@tonic-gate 			g_gflag = 1;
11877c478bd9Sstevel@tonic-gate 			break;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 		case 'C':
11907c478bd9Sstevel@tonic-gate 		case 'E':
11917c478bd9Sstevel@tonic-gate 		case 'H':
11927c478bd9Sstevel@tonic-gate 		case 'I':
11937c478bd9Sstevel@tonic-gate 			for (i = 0; i < LS_MAX_EVENTS; i++)
11947c478bd9Sstevel@tonic-gate 				if (g_event_info[i].ev_type == c)
11957c478bd9Sstevel@tonic-gate 					g_enabled[i] = 1;
11967c478bd9Sstevel@tonic-gate 			events_specified = 1;
11977c478bd9Sstevel@tonic-gate 			break;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		case 'A':
12007c478bd9Sstevel@tonic-gate 			for (i = 0; i < LS_MAX_EVENTS; i++)
12017c478bd9Sstevel@tonic-gate 				if (strchr("CH", g_event_info[i].ev_type))
12027c478bd9Sstevel@tonic-gate 					g_enabled[i] = 1;
12037c478bd9Sstevel@tonic-gate 			events_specified = 1;
12047c478bd9Sstevel@tonic-gate 			break;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		case 'T':
12077c478bd9Sstevel@tonic-gate 			g_tracing = 1;
12087c478bd9Sstevel@tonic-gate 			break;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		case 'D':
12117c478bd9Sstevel@tonic-gate 			if (!isdigit(optarg[0]))
12127c478bd9Sstevel@tonic-gate 				usage();
12137c478bd9Sstevel@tonic-gate 			g_topn = atoi(optarg);
12147c478bd9Sstevel@tonic-gate 			break;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		case 'R':
12177c478bd9Sstevel@tonic-gate 			g_rates = 1;
12187c478bd9Sstevel@tonic-gate 			break;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		case 'p':
12217c478bd9Sstevel@tonic-gate 			g_pflag = 1;
12227c478bd9Sstevel@tonic-gate 			break;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		case 'P':
12257c478bd9Sstevel@tonic-gate 			g_Pflag = 1;
12267c478bd9Sstevel@tonic-gate 			break;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		case 'o':
12297c478bd9Sstevel@tonic-gate 			if ((out = fopen(optarg, "w")) == NULL)
12307c478bd9Sstevel@tonic-gate 				fail(1, "error opening file");
12317c478bd9Sstevel@tonic-gate 			break;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 		case 'V':
12347c478bd9Sstevel@tonic-gate 			g_Vflag = 1;
12357c478bd9Sstevel@tonic-gate 			break;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		default:
12387c478bd9Sstevel@tonic-gate 			if (strchr(LOCKSTAT_OPTSTR, c) == NULL)
12397c478bd9Sstevel@tonic-gate 				usage();
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	if (filt != NULL) {
12447c478bd9Sstevel@tonic-gate 		predicate_add(&g_predicate, filt, NULL, 0);
12457c478bd9Sstevel@tonic-gate 		filter_destroy(&filt);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if (ifilt != NULL) {
12497c478bd9Sstevel@tonic-gate 		predicate_add(&g_ipredicate, ifilt, NULL, 0);
12507c478bd9Sstevel@tonic-gate 		filter_destroy(&ifilt);
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if (g_recsize == 0) {
12547c478bd9Sstevel@tonic-gate 		if (g_gflag) {
12557c478bd9Sstevel@tonic-gate 			g_stkdepth = LS_MAX_STACK_DEPTH;
12567c478bd9Sstevel@tonic-gate 			g_recsize = LS_STACK(g_stkdepth);
12577c478bd9Sstevel@tonic-gate 		} else {
12587c478bd9Sstevel@tonic-gate 			g_recsize = LS_TIME;
12597c478bd9Sstevel@tonic-gate 		}
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	if (g_gflag && g_recsize <= LS_STACK(0))
12637c478bd9Sstevel@tonic-gate 		fail(0, "'-g' requires at least '-s 1' data gathering");
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/*
12667c478bd9Sstevel@tonic-gate 	 * Make sure the alignment is reasonable
12677c478bd9Sstevel@tonic-gate 	 */
12687c478bd9Sstevel@tonic-gate 	g_recsize = -(-g_recsize & -sizeof (uint64_t));
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	for (i = 0; i < LS_MAX_EVENTS; i++) {
12717c478bd9Sstevel@tonic-gate 		/*
12727c478bd9Sstevel@tonic-gate 		 * If no events were specified, enable -C.
12737c478bd9Sstevel@tonic-gate 		 */
12747c478bd9Sstevel@tonic-gate 		if (!events_specified && g_event_info[i].ev_type == 'C')
12757c478bd9Sstevel@tonic-gate 			g_enabled[i] = 1;
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	for (i = 0; i < LS_MAX_EVENTS; i++) {
12797c478bd9Sstevel@tonic-gate 		if (!g_enabled[i])
12807c478bd9Sstevel@tonic-gate 			continue;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 		if (g_event_info[i].ev_acquire != NULL) {
12837c478bd9Sstevel@tonic-gate 			/*
12847c478bd9Sstevel@tonic-gate 			 * If we've enabled a hold event, we must explicitly
12857c478bd9Sstevel@tonic-gate 			 * allocate dynamic variable space.
12867c478bd9Sstevel@tonic-gate 			 */
12877c478bd9Sstevel@tonic-gate 			dynvar = 1;
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		dprog_addevent(i);
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	/*
12947c478bd9Sstevel@tonic-gate 	 * Make sure there are remaining arguments to specify a child command
12957c478bd9Sstevel@tonic-gate 	 * to execute.
12967c478bd9Sstevel@tonic-gate 	 */
12977c478bd9Sstevel@tonic-gate 	if (argc <= optind)
12987c478bd9Sstevel@tonic-gate 		usage();
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
13017c478bd9Sstevel@tonic-gate 		dfail("couldn't determine number of online CPUs");
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * By default, we set our data buffer size to be the number of records
13057c478bd9Sstevel@tonic-gate 	 * multiplied by the size of the record, doubled to account for some
13067c478bd9Sstevel@tonic-gate 	 * DTrace slop and divided by the number of CPUs.  We silently clamp
13077c478bd9Sstevel@tonic-gate 	 * the aggregation size at both a minimum and a maximum to prevent
13087c478bd9Sstevel@tonic-gate 	 * absurdly low or high values.
13097c478bd9Sstevel@tonic-gate 	 */
13107c478bd9Sstevel@tonic-gate 	if ((aggsize = (g_nrecs * g_recsize * 2) / ncpus) < MIN_AGGSIZE)
13117c478bd9Sstevel@tonic-gate 		aggsize = MIN_AGGSIZE;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if (aggsize > MAX_AGGSIZE)
13147c478bd9Sstevel@tonic-gate 		aggsize = MAX_AGGSIZE;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	(void) sprintf(aggstr, "%lld", (long long)aggsize);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (!g_tracing) {
13197c478bd9Sstevel@tonic-gate 		if (dtrace_setopt(g_dtp, "bufsize", "4k") == -1)
13207c478bd9Sstevel@tonic-gate 			dfail("failed to set 'bufsize'");
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 		if (dtrace_setopt(g_dtp, "aggsize", aggstr) == -1)
13237c478bd9Sstevel@tonic-gate 			dfail("failed to set 'aggsize'");
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 		if (dynvar) {
13267c478bd9Sstevel@tonic-gate 			/*
13277c478bd9Sstevel@tonic-gate 			 * If we're using dynamic variables, we set our
13287c478bd9Sstevel@tonic-gate 			 * dynamic variable size to be one megabyte per CPU,
13297c478bd9Sstevel@tonic-gate 			 * with a hard-limit of 32 megabytes.  This may still
13307c478bd9Sstevel@tonic-gate 			 * be too small in some cases, but it can be tuned
13317c478bd9Sstevel@tonic-gate 			 * manually via -x if need be.
13327c478bd9Sstevel@tonic-gate 			 */
13337c478bd9Sstevel@tonic-gate 			(void) sprintf(aggstr, "%ldm", ncpus < 32 ? ncpus : 32);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 			if (dtrace_setopt(g_dtp, "dynvarsize", aggstr) == -1)
13367c478bd9Sstevel@tonic-gate 				dfail("failed to set 'dynvarsize'");
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 	} else {
13397c478bd9Sstevel@tonic-gate 		if (dtrace_setopt(g_dtp, "bufsize", aggstr) == -1)
13407c478bd9Sstevel@tonic-gate 			dfail("failed to set 'bufsize'");
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(g_dtp, "statusrate", "10sec") == -1)
13447c478bd9Sstevel@tonic-gate 		dfail("failed to set 'statusrate'");
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	optind = 1;
13477c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != EOF) {
13487c478bd9Sstevel@tonic-gate 		switch (c) {
13497c478bd9Sstevel@tonic-gate 		case 'x':
13507c478bd9Sstevel@tonic-gate 			if ((p = strchr(optarg, '=')) != NULL)
13517c478bd9Sstevel@tonic-gate 				*p++ = '\0';
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 			if (dtrace_setopt(g_dtp, optarg, p) != 0)
13547c478bd9Sstevel@tonic-gate 				dfail("failed to set -x %s", optarg);
13557c478bd9Sstevel@tonic-gate 			break;
13567c478bd9Sstevel@tonic-gate 		}
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	argc -= optind;
13607c478bd9Sstevel@tonic-gate 	argv += optind;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	dprog_compile();
13637c478bd9Sstevel@tonic-gate 	status_init();
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	g_elapsed = -gethrtime();
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	/*
13687c478bd9Sstevel@tonic-gate 	 * Spawn the specified command and wait for it to complete.
13697c478bd9Sstevel@tonic-gate 	 */
13707c478bd9Sstevel@tonic-gate 	child = fork();
13717c478bd9Sstevel@tonic-gate 	if (child == -1)
13727c478bd9Sstevel@tonic-gate 		fail(1, "cannot fork");
13737c478bd9Sstevel@tonic-gate 	if (child == 0) {
13747c478bd9Sstevel@tonic-gate 		(void) dtrace_close(g_dtp);
13757c478bd9Sstevel@tonic-gate 		(void) execvp(argv[0], &argv[0]);
13767c478bd9Sstevel@tonic-gate 		exec_errno = errno;
13777c478bd9Sstevel@tonic-gate 		exit(127);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	while (waitpid(child, &status, WEXITED) != child)
13817c478bd9Sstevel@tonic-gate 		status_check();
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	g_elapsed += gethrtime();
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
13867c478bd9Sstevel@tonic-gate 		if (WEXITSTATUS(status) != 0) {
13877c478bd9Sstevel@tonic-gate 			if (exec_errno != 0) {
13887c478bd9Sstevel@tonic-gate 				errno = exec_errno;
13897c478bd9Sstevel@tonic-gate 				fail(1, "could not execute %s", argv[0]);
13907c478bd9Sstevel@tonic-gate 			}
13917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13927c478bd9Sstevel@tonic-gate 			    "lockstat: warning: %s exited with code %d\n",
13939d68b18eSck 			    argv[0], WEXITSTATUS(status));
13947c478bd9Sstevel@tonic-gate 		}
13957c478bd9Sstevel@tonic-gate 	} else {
13967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13977c478bd9Sstevel@tonic-gate 		    "lockstat: warning: %s died on signal %d\n",
13989d68b18eSck 		    argv[0], WTERMSIG(status));
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (dtrace_stop(g_dtp) == -1)
14027c478bd9Sstevel@tonic-gate 		dfail("failed to stop dtrace");
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/*
14057c478bd9Sstevel@tonic-gate 	 * Before we read out the results, we need to allocate our buffer.
14067c478bd9Sstevel@tonic-gate 	 * If we're tracing, then we'll just use the precalculated size.  If
14077c478bd9Sstevel@tonic-gate 	 * we're not, then we'll take a snapshot of the aggregate, and walk
14087c478bd9Sstevel@tonic-gate 	 * it to count the number of records.
14097c478bd9Sstevel@tonic-gate 	 */
14107c478bd9Sstevel@tonic-gate 	if (!g_tracing) {
14117c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_snap(g_dtp) != 0)
14127c478bd9Sstevel@tonic-gate 			dfail("failed to snap aggregate");
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 		g_nrecs = 0;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_walk(g_dtp,
14177c478bd9Sstevel@tonic-gate 		    count_aggregate, &g_nrecs) != 0)
14187c478bd9Sstevel@tonic-gate 			dfail("failed to walk aggregate");
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if ((data_buf = memalign(sizeof (uint64_t),
14227c478bd9Sstevel@tonic-gate 	    (g_nrecs + 1) * g_recsize)) == NULL)
14237c478bd9Sstevel@tonic-gate 		fail(1, "Memory allocation failed");
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/*
14267c478bd9Sstevel@tonic-gate 	 * Read out the DTrace data.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	g_nrecs_used = process_data(out, data_buf);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (g_nrecs_used > g_nrecs || g_dropped)
14317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "lockstat: warning: "
14327c478bd9Sstevel@tonic-gate 		    "ran out of data records (use -n for more)\n");
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
14357c478bd9Sstevel@tonic-gate 	for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
14367c478bd9Sstevel@tonic-gate 	    /* LINTED - alignment */
14377c478bd9Sstevel@tonic-gate 	    lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
14387c478bd9Sstevel@tonic-gate 		ev_count[lsp->ls_event] += lsp->ls_count;
14397c478bd9Sstevel@tonic-gate 		ev_time[lsp->ls_event] += lsp->ls_time;
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	/*
14437c478bd9Sstevel@tonic-gate 	 * If -g was specified, convert stacks into individual records.
14447c478bd9Sstevel@tonic-gate 	 */
14457c478bd9Sstevel@tonic-gate 	if (g_gflag) {
14467c478bd9Sstevel@tonic-gate 		lsrec_t *newlsp, *oldlsp;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 		newlsp = memalign(sizeof (uint64_t),
14497c478bd9Sstevel@tonic-gate 		    g_nrecs_used * LS_TIME * (g_stkdepth + 1));
14507c478bd9Sstevel@tonic-gate 		if (newlsp == NULL)
14517c478bd9Sstevel@tonic-gate 			fail(1, "Cannot allocate space for -g processing");
14527c478bd9Sstevel@tonic-gate 		lsp = newlsp;
14537c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
14547c478bd9Sstevel@tonic-gate 		for (i = 0, oldlsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
14557c478bd9Sstevel@tonic-gate 		    /* LINTED - alignment */
14567c478bd9Sstevel@tonic-gate 		    oldlsp = (lsrec_t *)((char *)oldlsp + g_recsize)) {
14577c478bd9Sstevel@tonic-gate 			int fr;
14587c478bd9Sstevel@tonic-gate 			int caller_in_stack = 0;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 			if (oldlsp->ls_count == 0)
14617c478bd9Sstevel@tonic-gate 				continue;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 			for (fr = 0; fr < g_stkdepth; fr++) {
14647c478bd9Sstevel@tonic-gate 				if (oldlsp->ls_stack[fr] == 0)
14657c478bd9Sstevel@tonic-gate 					break;
14667c478bd9Sstevel@tonic-gate 				if (oldlsp->ls_stack[fr] == oldlsp->ls_caller)
14677c478bd9Sstevel@tonic-gate 					caller_in_stack = 1;
14687c478bd9Sstevel@tonic-gate 				bcopy(oldlsp, lsp, LS_TIME);
14697c478bd9Sstevel@tonic-gate 				lsp->ls_caller = oldlsp->ls_stack[fr];
14707c478bd9Sstevel@tonic-gate 				/* LINTED - alignment */
14717c478bd9Sstevel@tonic-gate 				lsp = (lsrec_t *)((char *)lsp + LS_TIME);
14727c478bd9Sstevel@tonic-gate 			}
14737c478bd9Sstevel@tonic-gate 			if (!caller_in_stack) {
14747c478bd9Sstevel@tonic-gate 				bcopy(oldlsp, lsp, LS_TIME);
14757c478bd9Sstevel@tonic-gate 				/* LINTED - alignment */
14767c478bd9Sstevel@tonic-gate 				lsp = (lsrec_t *)((char *)lsp + LS_TIME);
14777c478bd9Sstevel@tonic-gate 			}
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 		g_nrecs = g_nrecs_used =
14807c478bd9Sstevel@tonic-gate 		    ((uintptr_t)lsp - (uintptr_t)newlsp) / LS_TIME;
14817c478bd9Sstevel@tonic-gate 		g_recsize = LS_TIME;
14827c478bd9Sstevel@tonic-gate 		g_stkdepth = 0;
14837c478bd9Sstevel@tonic-gate 		free(data_buf);
14847c478bd9Sstevel@tonic-gate 		data_buf = (char *)newlsp;
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	if ((sort_buf = calloc(2 * (g_nrecs + 1),
14887c478bd9Sstevel@tonic-gate 	    sizeof (void *))) == NULL)
14897c478bd9Sstevel@tonic-gate 		fail(1, "Sort buffer allocation failed");
14907c478bd9Sstevel@tonic-gate 	merge_buf = sort_buf + (g_nrecs + 1);
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	/*
14937c478bd9Sstevel@tonic-gate 	 * Build the sort buffer, discarding zero-count records along the way.
14947c478bd9Sstevel@tonic-gate 	 */
14957c478bd9Sstevel@tonic-gate 	/* LINTED - alignment */
14967c478bd9Sstevel@tonic-gate 	for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
14977c478bd9Sstevel@tonic-gate 	    /* LINTED - alignment */
14987c478bd9Sstevel@tonic-gate 	    lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
14997c478bd9Sstevel@tonic-gate 		if (lsp->ls_count == 0)
15007c478bd9Sstevel@tonic-gate 			lsp->ls_event = LS_MAX_EVENTS;
15017c478bd9Sstevel@tonic-gate 		sort_buf[i] = lsp;
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	if (g_nrecs_used == 0)
15057c478bd9Sstevel@tonic-gate 		exit(0);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	/*
15087c478bd9Sstevel@tonic-gate 	 * Add a sentinel after the last record
15097c478bd9Sstevel@tonic-gate 	 */
15107c478bd9Sstevel@tonic-gate 	sort_buf[i] = lsp;
15117c478bd9Sstevel@tonic-gate 	lsp->ls_event = LS_MAX_EVENTS;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	if (g_tracing) {
15147c478bd9Sstevel@tonic-gate 		report_trace(out, sort_buf);
15157c478bd9Sstevel@tonic-gate 		return (0);
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * Application of -g may have resulted in multiple records
15207c478bd9Sstevel@tonic-gate 	 * with the same signature; coalesce them.
15217c478bd9Sstevel@tonic-gate 	 */
15227c478bd9Sstevel@tonic-gate 	if (g_gflag) {
15237c478bd9Sstevel@tonic-gate 		mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
15247c478bd9Sstevel@tonic-gate 		coalesce(lockcmp, sort_buf, g_nrecs_used);
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/*
15287c478bd9Sstevel@tonic-gate 	 * Coalesce locks within the same symbol if -c option specified.
15297c478bd9Sstevel@tonic-gate 	 * Coalesce PCs within the same function if -k option specified.
15307c478bd9Sstevel@tonic-gate 	 */
15317c478bd9Sstevel@tonic-gate 	if (g_cflag || g_kflag) {
15327c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_nrecs_used; i++) {
15337c478bd9Sstevel@tonic-gate 			int fr;
15347c478bd9Sstevel@tonic-gate 			lsp = sort_buf[i];
15357c478bd9Sstevel@tonic-gate 			if (g_cflag)
15367c478bd9Sstevel@tonic-gate 				coalesce_symbol(&lsp->ls_lock);
15377c478bd9Sstevel@tonic-gate 			if (g_kflag) {
15387c478bd9Sstevel@tonic-gate 				for (fr = 0; fr < g_stkdepth; fr++)
15397c478bd9Sstevel@tonic-gate 					coalesce_symbol(&lsp->ls_stack[fr]);
15407c478bd9Sstevel@tonic-gate 				coalesce_symbol(&lsp->ls_caller);
15417c478bd9Sstevel@tonic-gate 			}
15427c478bd9Sstevel@tonic-gate 		}
15437c478bd9Sstevel@tonic-gate 		mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
15447c478bd9Sstevel@tonic-gate 		coalesce(lockcmp, sort_buf, g_nrecs_used);
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Coalesce callers if -w option specified
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	if (g_wflag) {
15517c478bd9Sstevel@tonic-gate 		mergesort(lock_and_count_cmp_anywhere,
15527c478bd9Sstevel@tonic-gate 		    sort_buf, merge_buf, g_nrecs_used);
15537c478bd9Sstevel@tonic-gate 		coalesce(lockcmp_anywhere, sort_buf, g_nrecs_used);
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/*
15577c478bd9Sstevel@tonic-gate 	 * Coalesce locks if -W option specified
15587c478bd9Sstevel@tonic-gate 	 */
15597c478bd9Sstevel@tonic-gate 	if (g_Wflag) {
15607c478bd9Sstevel@tonic-gate 		mergesort(site_and_count_cmp_anylock,
15617c478bd9Sstevel@tonic-gate 		    sort_buf, merge_buf, g_nrecs_used);
15627c478bd9Sstevel@tonic-gate 		coalesce(sitecmp_anylock, sort_buf, g_nrecs_used);
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	/*
15667c478bd9Sstevel@tonic-gate 	 * Sort data by contention count (ls_count) or total time (ls_time),
15677c478bd9Sstevel@tonic-gate 	 * depending on g_Pflag.  Override g_Pflag if time wasn't measured.
15687c478bd9Sstevel@tonic-gate 	 */
15697c478bd9Sstevel@tonic-gate 	if (g_recsize < LS_TIME)
15707c478bd9Sstevel@tonic-gate 		g_Pflag = 0;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (g_Pflag)
15737c478bd9Sstevel@tonic-gate 		mergesort(timecmp, sort_buf, merge_buf, g_nrecs_used);
15747c478bd9Sstevel@tonic-gate 	else
15757c478bd9Sstevel@tonic-gate 		mergesort(countcmp, sort_buf, merge_buf, g_nrecs_used);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	/*
15787c478bd9Sstevel@tonic-gate 	 * Display data by event type
15797c478bd9Sstevel@tonic-gate 	 */
15807c478bd9Sstevel@tonic-gate 	first = &sort_buf[0];
15817c478bd9Sstevel@tonic-gate 	while ((event = (*first)->ls_event) < LS_MAX_EVENTS) {
15827c478bd9Sstevel@tonic-gate 		current = first;
15837c478bd9Sstevel@tonic-gate 		while ((lsp = *current)->ls_event == event)
15847c478bd9Sstevel@tonic-gate 			current++;
15857c478bd9Sstevel@tonic-gate 		report_stats(out, first, current - first, ev_count[event],
15867c478bd9Sstevel@tonic-gate 		    ev_time[event]);
15877c478bd9Sstevel@tonic-gate 		first = current;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	return (0);
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate static char *
format_symbol(char * buf,uintptr_t addr,int show_size)15947c478bd9Sstevel@tonic-gate format_symbol(char *buf, uintptr_t addr, int show_size)
15957c478bd9Sstevel@tonic-gate {
15967c478bd9Sstevel@tonic-gate 	uintptr_t symoff;
15977c478bd9Sstevel@tonic-gate 	char *symname;
15987c478bd9Sstevel@tonic-gate 	size_t symsize;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	symname = addr_to_sym(addr, &symoff, &symsize);
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	if (show_size && symoff == 0)
16037c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s[%ld]", symname, (long)symsize);
16047c478bd9Sstevel@tonic-gate 	else if (symoff == 0)
16057c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s", symname);
16067c478bd9Sstevel@tonic-gate 	else if (symoff < 16 && bcmp(symname, "cpu[", 4) == 0)	/* CPU+PIL */
16077c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s+%ld", symname, (long)symoff);
16087c478bd9Sstevel@tonic-gate 	else if (symoff <= symsize || (symoff < 256 && addr != symoff))
16097c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s+0x%llx", symname,
16107c478bd9Sstevel@tonic-gate 		    (unsigned long long)symoff);
16117c478bd9Sstevel@tonic-gate 	else
16127c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "0x%llx", (unsigned long long)addr);
16137c478bd9Sstevel@tonic-gate 	return (buf);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate static void
report_stats(FILE * out,lsrec_t ** sort_buf,size_t nrecs,uint64_t total_count,uint64_t total_time)16177c478bd9Sstevel@tonic-gate report_stats(FILE *out, lsrec_t **sort_buf, size_t nrecs, uint64_t total_count,
161830699046SRichard Lowe     uint64_t total_time)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	uint32_t event = sort_buf[0]->ls_event;
16217c478bd9Sstevel@tonic-gate 	lsrec_t *lsp;
16227c478bd9Sstevel@tonic-gate 	double ptotal = 0.0;
16237c478bd9Sstevel@tonic-gate 	double percent;
16247c478bd9Sstevel@tonic-gate 	int i, j, fr;
16257c478bd9Sstevel@tonic-gate 	int displayed;
16267c478bd9Sstevel@tonic-gate 	int first_bin, last_bin, max_bin_count, total_bin_count;
16277c478bd9Sstevel@tonic-gate 	int rectype;
16287c478bd9Sstevel@tonic-gate 	char buf[256];
16297c478bd9Sstevel@tonic-gate 	char lhdr[80], chdr[80];
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	rectype = g_recsize;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	if (g_topn == 0) {
16347c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%20llu %s\n",
16357c478bd9Sstevel@tonic-gate 		    g_rates == 0 ? total_count :
16367c478bd9Sstevel@tonic-gate 		    ((unsigned long long)total_count * NANOSEC) / g_elapsed,
16377c478bd9Sstevel@tonic-gate 		    g_event_info[event].ev_desc);
16387c478bd9Sstevel@tonic-gate 		return;
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	(void) sprintf(lhdr, "%s%s",
16427c478bd9Sstevel@tonic-gate 	    g_Wflag ? "Hottest " : "", g_event_info[event].ev_lhdr);
16437c478bd9Sstevel@tonic-gate 	(void) sprintf(chdr, "%s%s",
16447c478bd9Sstevel@tonic-gate 	    g_wflag ? "Hottest " : "", "Caller");
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	if (!g_pflag)
16477c478bd9Sstevel@tonic-gate 		(void) fprintf(out,
16487c478bd9Sstevel@tonic-gate 		    "\n%s: %.0f events in %.3f seconds (%.0f events/sec)\n\n",
16497c478bd9Sstevel@tonic-gate 		    g_event_info[event].ev_desc, (double)total_count,
16507c478bd9Sstevel@tonic-gate 		    (double)g_elapsed / NANOSEC,
16517c478bd9Sstevel@tonic-gate 		    (double)total_count * NANOSEC / g_elapsed);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	if (!g_pflag && rectype < LS_HIST) {
16547c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s", g_event_info[event].ev_units);
16557c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
16567c478bd9Sstevel@tonic-gate 		    g_rates ? "ops/s" : "Count",
16577c478bd9Sstevel@tonic-gate 		    g_gflag ? "genr" : "indv",
16587c478bd9Sstevel@tonic-gate 		    "cuml", "rcnt", rectype >= LS_TIME ? buf : "", lhdr, chdr);
16597c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "---------------------------------"
16607c478bd9Sstevel@tonic-gate 		    "----------------------------------------------\n");
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	displayed = 0;
16647c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrecs; i++) {
16657c478bd9Sstevel@tonic-gate 		lsp = sort_buf[i];
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 		if (displayed++ >= g_topn)
16687c478bd9Sstevel@tonic-gate 			break;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		if (g_pflag) {
16717c478bd9Sstevel@tonic-gate 			int j;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%u %u",
16747c478bd9Sstevel@tonic-gate 			    lsp->ls_event, lsp->ls_count);
16757c478bd9Sstevel@tonic-gate 			(void) fprintf(out, " %s",
16767c478bd9Sstevel@tonic-gate 			    format_symbol(buf, lsp->ls_lock, g_cflag));
16777c478bd9Sstevel@tonic-gate 			(void) fprintf(out, " %s",
16787c478bd9Sstevel@tonic-gate 			    format_symbol(buf, lsp->ls_caller, 0));
16797c478bd9Sstevel@tonic-gate 			(void) fprintf(out, " %f",
16807c478bd9Sstevel@tonic-gate 			    (double)lsp->ls_refcnt / lsp->ls_count);
16817c478bd9Sstevel@tonic-gate 			if (rectype >= LS_TIME)
16827c478bd9Sstevel@tonic-gate 				(void) fprintf(out, " %llu",
16837c478bd9Sstevel@tonic-gate 				    (unsigned long long)lsp->ls_time);
16847c478bd9Sstevel@tonic-gate 			if (rectype >= LS_HIST) {
16857c478bd9Sstevel@tonic-gate 				for (j = 0; j < 64; j++)
16867c478bd9Sstevel@tonic-gate 					(void) fprintf(out, " %u",
16877c478bd9Sstevel@tonic-gate 					    lsp->ls_hist[j]);
16887c478bd9Sstevel@tonic-gate 			}
16897c478bd9Sstevel@tonic-gate 			for (j = 0; j < LS_MAX_STACK_DEPTH; j++) {
16907c478bd9Sstevel@tonic-gate 				if (rectype <= LS_STACK(j) ||
16917c478bd9Sstevel@tonic-gate 				    lsp->ls_stack[j] == 0)
16927c478bd9Sstevel@tonic-gate 					break;
16937c478bd9Sstevel@tonic-gate 				(void) fprintf(out, " %s",
16947c478bd9Sstevel@tonic-gate 				    format_symbol(buf, lsp->ls_stack[j], 0));
16957c478bd9Sstevel@tonic-gate 			}
16967c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "\n");
16977c478bd9Sstevel@tonic-gate 			continue;
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		if (rectype >= LS_HIST) {
17017c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "---------------------------------"
17027c478bd9Sstevel@tonic-gate 			    "----------------------------------------------\n");
17037c478bd9Sstevel@tonic-gate 			(void) sprintf(buf, "%s",
17047c478bd9Sstevel@tonic-gate 			    g_event_info[event].ev_units);
17057c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
17067c478bd9Sstevel@tonic-gate 			    g_rates ? "ops/s" : "Count",
17077c478bd9Sstevel@tonic-gate 			    g_gflag ? "genr" : "indv",
17087c478bd9Sstevel@tonic-gate 			    "cuml", "rcnt", buf, lhdr, chdr);
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 		if (g_Pflag && total_time != 0)
17127c478bd9Sstevel@tonic-gate 			percent = (lsp->ls_time * 100.00) / total_time;
17137c478bd9Sstevel@tonic-gate 		else
17147c478bd9Sstevel@tonic-gate 			percent = (lsp->ls_count * 100.00) / total_count;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 		ptotal += percent;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 		if (rectype >= LS_TIME)
17197c478bd9Sstevel@tonic-gate 			(void) sprintf(buf, "%llu",
17207c478bd9Sstevel@tonic-gate 			    (unsigned long long)(lsp->ls_time / lsp->ls_count));
17217c478bd9Sstevel@tonic-gate 		else
17227c478bd9Sstevel@tonic-gate 			buf[0] = '\0';
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%5llu ",
17257c478bd9Sstevel@tonic-gate 		    g_rates == 0 ? lsp->ls_count :
17267c478bd9Sstevel@tonic-gate 		    ((uint64_t)lsp->ls_count * NANOSEC) / g_elapsed);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%3.0f%% ", percent);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 		if (g_gflag)
17317c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "---- ");
17327c478bd9Sstevel@tonic-gate 		else
17337c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%3.0f%% ", ptotal);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%4.2f %8s ",
17367c478bd9Sstevel@tonic-gate 		    (double)lsp->ls_refcnt / lsp->ls_count, buf);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%-22s ",
17397c478bd9Sstevel@tonic-gate 		    format_symbol(buf, lsp->ls_lock, g_cflag));
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%-24s\n",
17427c478bd9Sstevel@tonic-gate 		    format_symbol(buf, lsp->ls_caller, 0));
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		if (rectype < LS_HIST)
17457c478bd9Sstevel@tonic-gate 			continue;
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "\n");
17487c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%10s %31s %-9s %-24s\n",
17499d68b18eSck 		    g_event_info[event].ev_units,
17509d68b18eSck 		    "------ Time Distribution ------",
17519d68b18eSck 		    g_rates ? "ops/s" : "count",
17529d68b18eSck 		    rectype > LS_STACK(0) ? "Stack" : "");
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 		first_bin = 0;
17557c478bd9Sstevel@tonic-gate 		while (lsp->ls_hist[first_bin] == 0)
17567c478bd9Sstevel@tonic-gate 			first_bin++;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 		last_bin = 63;
17597c478bd9Sstevel@tonic-gate 		while (lsp->ls_hist[last_bin] == 0)
17607c478bd9Sstevel@tonic-gate 			last_bin--;
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 		max_bin_count = 0;
17637c478bd9Sstevel@tonic-gate 		total_bin_count = 0;
17647c478bd9Sstevel@tonic-gate 		for (j = first_bin; j <= last_bin; j++) {
17657c478bd9Sstevel@tonic-gate 			total_bin_count += lsp->ls_hist[j];
17667c478bd9Sstevel@tonic-gate 			if (lsp->ls_hist[j] > max_bin_count)
17677c478bd9Sstevel@tonic-gate 				max_bin_count = lsp->ls_hist[j];
17687c478bd9Sstevel@tonic-gate 		}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 		/*
17717c478bd9Sstevel@tonic-gate 		 * If we went a few frames below the caller, ignore them
17727c478bd9Sstevel@tonic-gate 		 */
17737c478bd9Sstevel@tonic-gate 		for (fr = 3; fr > 0; fr--)
17747c478bd9Sstevel@tonic-gate 			if (lsp->ls_stack[fr] == lsp->ls_caller)
17757c478bd9Sstevel@tonic-gate 				break;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 		for (j = first_bin; j <= last_bin; j++) {
17787c478bd9Sstevel@tonic-gate 			uint_t depth = (lsp->ls_hist[j] * 30) / total_bin_count;
17797c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%10llu |%s%s %-9u ",
17807c478bd9Sstevel@tonic-gate 			    1ULL << j,
17817c478bd9Sstevel@tonic-gate 			    "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + 30 - depth,
17827c478bd9Sstevel@tonic-gate 			    "                              " + depth,
17837c478bd9Sstevel@tonic-gate 			    g_rates == 0 ? lsp->ls_hist[j] :
17847c478bd9Sstevel@tonic-gate 			    (uint_t)(((uint64_t)lsp->ls_hist[j] * NANOSEC) /
17857c478bd9Sstevel@tonic-gate 			    g_elapsed));
17867c478bd9Sstevel@tonic-gate 			if (rectype <= LS_STACK(fr) || lsp->ls_stack[fr] == 0) {
17877c478bd9Sstevel@tonic-gate 				(void) fprintf(out, "\n");
17887c478bd9Sstevel@tonic-gate 				continue;
17897c478bd9Sstevel@tonic-gate 			}
17907c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%-24s\n",
17917c478bd9Sstevel@tonic-gate 			    format_symbol(buf, lsp->ls_stack[fr], 0));
17927c478bd9Sstevel@tonic-gate 			fr++;
17937c478bd9Sstevel@tonic-gate 		}
17947c478bd9Sstevel@tonic-gate 		while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
17957c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%15s %-36s %-24s\n", "", "",
17967c478bd9Sstevel@tonic-gate 			    format_symbol(buf, lsp->ls_stack[fr], 0));
17977c478bd9Sstevel@tonic-gate 			fr++;
17987c478bd9Sstevel@tonic-gate 		}
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	if (!g_pflag)
18027c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "---------------------------------"
18037c478bd9Sstevel@tonic-gate 		    "----------------------------------------------\n");
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	(void) fflush(out);
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate static void
report_trace(FILE * out,lsrec_t ** sort_buf)18097c478bd9Sstevel@tonic-gate report_trace(FILE *out, lsrec_t **sort_buf)
18107c478bd9Sstevel@tonic-gate {
18117c478bd9Sstevel@tonic-gate 	lsrec_t *lsp;
18127c478bd9Sstevel@tonic-gate 	int i, fr;
18137c478bd9Sstevel@tonic-gate 	int rectype;
18147c478bd9Sstevel@tonic-gate 	char buf[256], buf2[256];
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	rectype = g_recsize;
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	if (!g_pflag) {
18197c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%5s  %7s  %11s  %-24s  %-24s\n",
18207c478bd9Sstevel@tonic-gate 		    "Event", "Time", "Owner", "Lock", "Caller");
18217c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "---------------------------------"
18227c478bd9Sstevel@tonic-gate 		    "----------------------------------------------\n");
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_nrecs_used; i++) {
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 		lsp = sort_buf[i];
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 		if (lsp->ls_event >= LS_MAX_EVENTS || lsp->ls_count == 0)
18307c478bd9Sstevel@tonic-gate 			continue;
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "%2d  %10llu  %11p  %-24s  %-24s\n",
18337c478bd9Sstevel@tonic-gate 		    lsp->ls_event, (unsigned long long)lsp->ls_time,
18347c478bd9Sstevel@tonic-gate 		    (void *)lsp->ls_next,
18357c478bd9Sstevel@tonic-gate 		    format_symbol(buf, lsp->ls_lock, 0),
18367c478bd9Sstevel@tonic-gate 		    format_symbol(buf2, lsp->ls_caller, 0));
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 		if (rectype <= LS_STACK(0))
18397c478bd9Sstevel@tonic-gate 			continue;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 		/*
18427c478bd9Sstevel@tonic-gate 		 * If we went a few frames below the caller, ignore them
18437c478bd9Sstevel@tonic-gate 		 */
18447c478bd9Sstevel@tonic-gate 		for (fr = 3; fr > 0; fr--)
18457c478bd9Sstevel@tonic-gate 			if (lsp->ls_stack[fr] == lsp->ls_caller)
18467c478bd9Sstevel@tonic-gate 				break;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
18497c478bd9Sstevel@tonic-gate 			(void) fprintf(out, "%53s  %-24s\n", "",
18507c478bd9Sstevel@tonic-gate 			    format_symbol(buf, lsp->ls_stack[fr], 0));
18517c478bd9Sstevel@tonic-gate 			fr++;
18527c478bd9Sstevel@tonic-gate 		}
18537c478bd9Sstevel@tonic-gate 		(void) fprintf(out, "\n");
18547c478bd9Sstevel@tonic-gate 	}
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	(void) fflush(out);
18577c478bd9Sstevel@tonic-gate }
1858