10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6103Sck142721 * Common Development and Distribution License (the "License").
6*6103Sck142721 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6103Sck142721 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stddef.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <fcntl.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <limits.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/modctl.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/wait.h>
430Sstevel@tonic-gate #include <dtrace.h>
440Sstevel@tonic-gate #include <sys/lockstat.h>
450Sstevel@tonic-gate #include <alloca.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include <assert.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define LOCKSTAT_OPTSTR "x:bths:n:d:i:l:f:e:ckwWgCHEATID:RpPo:V"
500Sstevel@tonic-gate
510Sstevel@tonic-gate #define LS_MAX_STACK_DEPTH 50
520Sstevel@tonic-gate #define LS_MAX_EVENTS 64
530Sstevel@tonic-gate
540Sstevel@tonic-gate typedef struct lsrec {
550Sstevel@tonic-gate struct lsrec *ls_next; /* next in hash chain */
560Sstevel@tonic-gate uintptr_t ls_lock; /* lock address */
570Sstevel@tonic-gate uintptr_t ls_caller; /* caller address */
580Sstevel@tonic-gate uint32_t ls_count; /* cumulative event count */
590Sstevel@tonic-gate uint32_t ls_event; /* type of event */
600Sstevel@tonic-gate uintptr_t ls_refcnt; /* cumulative reference count */
610Sstevel@tonic-gate uint64_t ls_time; /* cumulative event duration */
620Sstevel@tonic-gate uint32_t ls_hist[64]; /* log2(duration) histogram */
630Sstevel@tonic-gate uintptr_t ls_stack[LS_MAX_STACK_DEPTH];
640Sstevel@tonic-gate } lsrec_t;
650Sstevel@tonic-gate
660Sstevel@tonic-gate typedef struct lsdata {
670Sstevel@tonic-gate struct lsrec *lsd_next; /* next available */
680Sstevel@tonic-gate int lsd_count; /* number of records */
690Sstevel@tonic-gate } lsdata_t;
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * Definitions for the types of experiments which can be run. They are
730Sstevel@tonic-gate * listed in increasing order of memory cost and processing time cost.
740Sstevel@tonic-gate * The numerical value of each type is the number of bytes needed per record.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate #define LS_BASIC offsetof(lsrec_t, ls_time)
770Sstevel@tonic-gate #define LS_TIME offsetof(lsrec_t, ls_hist[0])
780Sstevel@tonic-gate #define LS_HIST offsetof(lsrec_t, ls_stack[0])
790Sstevel@tonic-gate #define LS_STACK(depth) offsetof(lsrec_t, ls_stack[depth])
800Sstevel@tonic-gate
810Sstevel@tonic-gate static void report_stats(FILE *, lsrec_t **, size_t, uint64_t, uint64_t);
820Sstevel@tonic-gate static void report_trace(FILE *, lsrec_t **);
830Sstevel@tonic-gate
840Sstevel@tonic-gate extern int symtab_init(void);
850Sstevel@tonic-gate extern char *addr_to_sym(uintptr_t, uintptr_t *, size_t *);
860Sstevel@tonic-gate extern uintptr_t sym_to_addr(char *name);
870Sstevel@tonic-gate extern size_t sym_size(char *name);
880Sstevel@tonic-gate extern char *strtok_r(char *, const char *, char **);
890Sstevel@tonic-gate
900Sstevel@tonic-gate #define DEFAULT_NRECS 10000
910Sstevel@tonic-gate #define DEFAULT_HZ 97
920Sstevel@tonic-gate #define MAX_HZ 1000
930Sstevel@tonic-gate #define MIN_AGGSIZE (16 * 1024)
940Sstevel@tonic-gate #define MAX_AGGSIZE (32 * 1024 * 1024)
950Sstevel@tonic-gate
960Sstevel@tonic-gate static int g_stkdepth;
970Sstevel@tonic-gate static int g_topn = INT_MAX;
980Sstevel@tonic-gate static hrtime_t g_elapsed;
990Sstevel@tonic-gate static int g_rates = 0;
1000Sstevel@tonic-gate static int g_pflag = 0;
1010Sstevel@tonic-gate static int g_Pflag = 0;
1020Sstevel@tonic-gate static int g_wflag = 0;
1030Sstevel@tonic-gate static int g_Wflag = 0;
1040Sstevel@tonic-gate static int g_cflag = 0;
1050Sstevel@tonic-gate static int g_kflag = 0;
1060Sstevel@tonic-gate static int g_gflag = 0;
1070Sstevel@tonic-gate static int g_Vflag = 0;
1080Sstevel@tonic-gate static int g_tracing = 0;
1090Sstevel@tonic-gate static size_t g_recsize;
1100Sstevel@tonic-gate static size_t g_nrecs;
1110Sstevel@tonic-gate static int g_nrecs_used;
1120Sstevel@tonic-gate static uchar_t g_enabled[LS_MAX_EVENTS];
1130Sstevel@tonic-gate static hrtime_t g_min_duration[LS_MAX_EVENTS];
1140Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
1150Sstevel@tonic-gate static char *g_predicate;
1160Sstevel@tonic-gate static char *g_ipredicate;
1170Sstevel@tonic-gate static char *g_prog;
1180Sstevel@tonic-gate static int g_proglen;
1190Sstevel@tonic-gate static int g_dropped;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate typedef struct ls_event_info {
1220Sstevel@tonic-gate char ev_type;
1230Sstevel@tonic-gate char ev_lhdr[20];
1240Sstevel@tonic-gate char ev_desc[80];
1250Sstevel@tonic-gate char ev_units[10];
1260Sstevel@tonic-gate char ev_name[DTRACE_NAMELEN];
1270Sstevel@tonic-gate char *ev_predicate;
1280Sstevel@tonic-gate char *ev_acquire;
1290Sstevel@tonic-gate } ls_event_info_t;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static ls_event_info_t g_event_info[LS_MAX_EVENTS] = {
132*6103Sck142721 { 'C', "Lock", "Adaptive mutex spin", "nsec",
1330Sstevel@tonic-gate "lockstat:::adaptive-spin" },
1340Sstevel@tonic-gate { 'C', "Lock", "Adaptive mutex block", "nsec",
1350Sstevel@tonic-gate "lockstat:::adaptive-block" },
136*6103Sck142721 { 'C', "Lock", "Spin lock spin", "nsec",
1370Sstevel@tonic-gate "lockstat:::spin-spin" },
138*6103Sck142721 { 'C', "Lock", "Thread lock spin", "nsec",
1390Sstevel@tonic-gate "lockstat:::thread-spin" },
1400Sstevel@tonic-gate { 'C', "Lock", "R/W writer blocked by writer", "nsec",
1410Sstevel@tonic-gate "lockstat:::rw-block", "arg2 == 0 && arg3 == 1" },
1420Sstevel@tonic-gate { 'C', "Lock", "R/W writer blocked by readers", "nsec",
1430Sstevel@tonic-gate "lockstat:::rw-block", "arg2 == 0 && arg3 == 0 && arg4" },
1440Sstevel@tonic-gate { 'C', "Lock", "R/W reader blocked by writer", "nsec",
1450Sstevel@tonic-gate "lockstat:::rw-block", "arg2 != 0 && arg3 == 1" },
1460Sstevel@tonic-gate { 'C', "Lock", "R/W reader blocked by write wanted", "nsec",
1470Sstevel@tonic-gate "lockstat:::rw-block", "arg2 != 0 && arg3 == 0 && arg4" },
1480Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 8)", "units" },
1490Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 9)", "units" },
1500Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 10)", "units" },
1510Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 11)", "units" },
1520Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 12)", "units" },
1530Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 13)", "units" },
1540Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 14)", "units" },
1550Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 15)", "units" },
1560Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 16)", "units" },
1570Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 17)", "units" },
1580Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 18)", "units" },
1590Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 19)", "units" },
1600Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 20)", "units" },
1610Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 21)", "units" },
1620Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 22)", "units" },
1630Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 23)", "units" },
1640Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 24)", "units" },
1650Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 25)", "units" },
1660Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 26)", "units" },
1670Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 27)", "units" },
1680Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 28)", "units" },
1690Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 29)", "units" },
1700Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 30)", "units" },
1710Sstevel@tonic-gate { 'C', "Lock", "Unknown event (type 31)", "units" },
1720Sstevel@tonic-gate { 'H', "Lock", "Adaptive mutex hold", "nsec",
1730Sstevel@tonic-gate "lockstat:::adaptive-release", NULL,
1740Sstevel@tonic-gate "lockstat:::adaptive-acquire" },
1750Sstevel@tonic-gate { 'H', "Lock", "Spin lock hold", "nsec",
1760Sstevel@tonic-gate "lockstat:::spin-release", NULL,
1770Sstevel@tonic-gate "lockstat:::spin-acquire" },
1780Sstevel@tonic-gate { 'H', "Lock", "R/W writer hold", "nsec",
1790Sstevel@tonic-gate "lockstat:::rw-release", "arg1 == 0",
1800Sstevel@tonic-gate "lockstat:::rw-acquire" },
1810Sstevel@tonic-gate { 'H', "Lock", "R/W reader hold", "nsec",
1820Sstevel@tonic-gate "lockstat:::rw-release", "arg1 != 0",
1830Sstevel@tonic-gate "lockstat:::rw-acquire" },
1840Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 36)", "units" },
1850Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 37)", "units" },
1860Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 38)", "units" },
1870Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 39)", "units" },
1880Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 40)", "units" },
1890Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 41)", "units" },
1900Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 42)", "units" },
1910Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 43)", "units" },
1920Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 44)", "units" },
1930Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 45)", "units" },
1940Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 46)", "units" },
1950Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 47)", "units" },
1960Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 48)", "units" },
1970Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 49)", "units" },
1980Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 50)", "units" },
1990Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 51)", "units" },
2000Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 52)", "units" },
2010Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 53)", "units" },
2020Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 54)", "units" },
2030Sstevel@tonic-gate { 'H', "Lock", "Unknown event (type 55)", "units" },
2040Sstevel@tonic-gate { 'I', "CPU+PIL", "Profiling interrupt", "nsec",
2050Sstevel@tonic-gate "profile:::profile-97", NULL },
2060Sstevel@tonic-gate { 'I', "Lock", "Unknown event (type 57)", "units" },
2070Sstevel@tonic-gate { 'I', "Lock", "Unknown event (type 58)", "units" },
2080Sstevel@tonic-gate { 'I', "Lock", "Unknown event (type 59)", "units" },
2090Sstevel@tonic-gate { 'E', "Lock", "Recursive lock entry detected", "(N/A)",
2100Sstevel@tonic-gate "lockstat:::rw-release", NULL, "lockstat:::rw-acquire" },
2110Sstevel@tonic-gate { 'E', "Lock", "Lockstat enter failure", "(N/A)" },
2120Sstevel@tonic-gate { 'E', "Lock", "Lockstat exit failure", "nsec" },
2130Sstevel@tonic-gate { 'E', "Lock", "Lockstat record failure", "(N/A)" },
2140Sstevel@tonic-gate };
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate static void
fail(int do_perror,const char * message,...)2170Sstevel@tonic-gate fail(int do_perror, const char *message, ...)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate va_list args;
2200Sstevel@tonic-gate int save_errno = errno;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate va_start(args, message);
2230Sstevel@tonic-gate (void) fprintf(stderr, "lockstat: ");
2240Sstevel@tonic-gate (void) vfprintf(stderr, message, args);
2250Sstevel@tonic-gate va_end(args);
2260Sstevel@tonic-gate if (do_perror)
2270Sstevel@tonic-gate (void) fprintf(stderr, ": %s", strerror(save_errno));
2280Sstevel@tonic-gate (void) fprintf(stderr, "\n");
2290Sstevel@tonic-gate exit(2);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate static void
dfail(const char * message,...)2330Sstevel@tonic-gate dfail(const char *message, ...)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate va_list args;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate va_start(args, message);
2380Sstevel@tonic-gate (void) fprintf(stderr, "lockstat: ");
2390Sstevel@tonic-gate (void) vfprintf(stderr, message, args);
2400Sstevel@tonic-gate va_end(args);
2410Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n",
2420Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate exit(2);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate static void
show_events(char event_type,char * desc)2480Sstevel@tonic-gate show_events(char event_type, char *desc)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate int i, first = -1, last;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate for (i = 0; i < LS_MAX_EVENTS; i++) {
2530Sstevel@tonic-gate ls_event_info_t *evp = &g_event_info[i];
2540Sstevel@tonic-gate if (evp->ev_type != event_type ||
2550Sstevel@tonic-gate strncmp(evp->ev_desc, "Unknown event", 13) == 0)
2560Sstevel@tonic-gate continue;
2570Sstevel@tonic-gate if (first == -1)
2580Sstevel@tonic-gate first = i;
2590Sstevel@tonic-gate last = i;
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate (void) fprintf(stderr,
2630Sstevel@tonic-gate "\n%s events (lockstat -%c or lockstat -e %d-%d):\n\n",
2640Sstevel@tonic-gate desc, event_type, first, last);
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate for (i = first; i <= last; i++)
2670Sstevel@tonic-gate (void) fprintf(stderr,
2680Sstevel@tonic-gate "%4d = %s\n", i, g_event_info[i].ev_desc);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate static void
usage(void)2720Sstevel@tonic-gate usage(void)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate (void) fprintf(stderr,
2750Sstevel@tonic-gate "Usage: lockstat [options] command [args]\n"
2760Sstevel@tonic-gate "\nEvent selection options:\n\n"
2770Sstevel@tonic-gate " -C watch contention events [on by default]\n"
2780Sstevel@tonic-gate " -E watch error events [off by default]\n"
2790Sstevel@tonic-gate " -H watch hold events [off by default]\n"
2800Sstevel@tonic-gate " -I watch interrupt events [off by default]\n"
2810Sstevel@tonic-gate " -A watch all lock events [equivalent to -CH]\n"
2820Sstevel@tonic-gate " -e event_list only watch the specified events (shown below);\n"
2830Sstevel@tonic-gate " <event_list> is a comma-separated list of\n"
2840Sstevel@tonic-gate " events or ranges of events, e.g. 1,4-7,35\n"
2850Sstevel@tonic-gate " -i rate interrupt rate for -I [default: %d Hz]\n"
2860Sstevel@tonic-gate "\nData gathering options:\n\n"
2870Sstevel@tonic-gate " -b basic statistics (lock, caller, event count)\n"
2880Sstevel@tonic-gate " -t timing for all events [default]\n"
2890Sstevel@tonic-gate " -h histograms for event times\n"
2900Sstevel@tonic-gate " -s depth stack traces <depth> deep\n"
2910Sstevel@tonic-gate " -x opt[=val] enable or modify DTrace options\n"
2920Sstevel@tonic-gate "\nData filtering options:\n\n"
2930Sstevel@tonic-gate " -n nrecords maximum number of data records [default: %d]\n"
2940Sstevel@tonic-gate " -l lock[,size] only watch <lock>, which can be specified as a\n"
2950Sstevel@tonic-gate " symbolic name or hex address; <size> defaults\n"
2960Sstevel@tonic-gate " to the ELF symbol size if available, 1 if not\n"
2970Sstevel@tonic-gate " -f func[,size] only watch events generated by <func>\n"
2980Sstevel@tonic-gate " -d duration only watch events longer than <duration>\n"
2990Sstevel@tonic-gate " -T trace (rather than sample) events\n"
3000Sstevel@tonic-gate "\nData reporting options:\n\n"
3010Sstevel@tonic-gate " -c coalesce lock data for arrays like pse_mutex[]\n"
3020Sstevel@tonic-gate " -k coalesce PCs within functions\n"
3030Sstevel@tonic-gate " -g show total events generated by function\n"
3040Sstevel@tonic-gate " -w wherever: don't distinguish events by caller\n"
3050Sstevel@tonic-gate " -W whichever: don't distinguish events by lock\n"
3060Sstevel@tonic-gate " -R display rates rather than counts\n"
3070Sstevel@tonic-gate " -p parsable output format (awk(1)-friendly)\n"
3080Sstevel@tonic-gate " -P sort lock data by (count * avg_time) product\n"
3090Sstevel@tonic-gate " -D n only display top <n> events of each type\n"
3100Sstevel@tonic-gate " -o filename send output to <filename>\n",
3110Sstevel@tonic-gate DEFAULT_HZ, DEFAULT_NRECS);
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate show_events('C', "Contention");
3140Sstevel@tonic-gate show_events('H', "Hold-time");
3150Sstevel@tonic-gate show_events('I', "Interrupt");
3160Sstevel@tonic-gate show_events('E', "Error");
3170Sstevel@tonic-gate (void) fprintf(stderr, "\n");
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate exit(1);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate static int
lockcmp(lsrec_t * a,lsrec_t * b)3230Sstevel@tonic-gate lockcmp(lsrec_t *a, lsrec_t *b)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate int i;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate if (a->ls_event < b->ls_event)
3280Sstevel@tonic-gate return (-1);
3290Sstevel@tonic-gate if (a->ls_event > b->ls_event)
3300Sstevel@tonic-gate return (1);
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate for (i = g_stkdepth - 1; i >= 0; i--) {
3330Sstevel@tonic-gate if (a->ls_stack[i] < b->ls_stack[i])
3340Sstevel@tonic-gate return (-1);
3350Sstevel@tonic-gate if (a->ls_stack[i] > b->ls_stack[i])
3360Sstevel@tonic-gate return (1);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate if (a->ls_caller < b->ls_caller)
3400Sstevel@tonic-gate return (-1);
3410Sstevel@tonic-gate if (a->ls_caller > b->ls_caller)
3420Sstevel@tonic-gate return (1);
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if (a->ls_lock < b->ls_lock)
3450Sstevel@tonic-gate return (-1);
3460Sstevel@tonic-gate if (a->ls_lock > b->ls_lock)
3470Sstevel@tonic-gate return (1);
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate return (0);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate static int
countcmp(lsrec_t * a,lsrec_t * b)3530Sstevel@tonic-gate countcmp(lsrec_t *a, lsrec_t *b)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate if (a->ls_event < b->ls_event)
3560Sstevel@tonic-gate return (-1);
3570Sstevel@tonic-gate if (a->ls_event > b->ls_event)
3580Sstevel@tonic-gate return (1);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate return (b->ls_count - a->ls_count);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate static int
timecmp(lsrec_t * a,lsrec_t * b)3640Sstevel@tonic-gate timecmp(lsrec_t *a, lsrec_t *b)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate if (a->ls_event < b->ls_event)
3670Sstevel@tonic-gate return (-1);
3680Sstevel@tonic-gate if (a->ls_event > b->ls_event)
3690Sstevel@tonic-gate return (1);
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (a->ls_time < b->ls_time)
3720Sstevel@tonic-gate return (1);
3730Sstevel@tonic-gate if (a->ls_time > b->ls_time)
3740Sstevel@tonic-gate return (-1);
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate return (0);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate static int
lockcmp_anywhere(lsrec_t * a,lsrec_t * b)3800Sstevel@tonic-gate lockcmp_anywhere(lsrec_t *a, lsrec_t *b)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate if (a->ls_event < b->ls_event)
3830Sstevel@tonic-gate return (-1);
3840Sstevel@tonic-gate if (a->ls_event > b->ls_event)
3850Sstevel@tonic-gate return (1);
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (a->ls_lock < b->ls_lock)
3880Sstevel@tonic-gate return (-1);
3890Sstevel@tonic-gate if (a->ls_lock > b->ls_lock)
3900Sstevel@tonic-gate return (1);
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate static int
lock_and_count_cmp_anywhere(lsrec_t * a,lsrec_t * b)3960Sstevel@tonic-gate lock_and_count_cmp_anywhere(lsrec_t *a, lsrec_t *b)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate if (a->ls_event < b->ls_event)
3990Sstevel@tonic-gate return (-1);
4000Sstevel@tonic-gate if (a->ls_event > b->ls_event)
4010Sstevel@tonic-gate return (1);
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate if (a->ls_lock < b->ls_lock)
4040Sstevel@tonic-gate return (-1);
4050Sstevel@tonic-gate if (a->ls_lock > b->ls_lock)
4060Sstevel@tonic-gate return (1);
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate return (b->ls_count - a->ls_count);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate static int
sitecmp_anylock(lsrec_t * a,lsrec_t * b)4120Sstevel@tonic-gate sitecmp_anylock(lsrec_t *a, lsrec_t *b)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate int i;
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate if (a->ls_event < b->ls_event)
4170Sstevel@tonic-gate return (-1);
4180Sstevel@tonic-gate if (a->ls_event > b->ls_event)
4190Sstevel@tonic-gate return (1);
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate for (i = g_stkdepth - 1; i >= 0; i--) {
4220Sstevel@tonic-gate if (a->ls_stack[i] < b->ls_stack[i])
4230Sstevel@tonic-gate return (-1);
4240Sstevel@tonic-gate if (a->ls_stack[i] > b->ls_stack[i])
4250Sstevel@tonic-gate return (1);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate if (a->ls_caller < b->ls_caller)
4290Sstevel@tonic-gate return (-1);
4300Sstevel@tonic-gate if (a->ls_caller > b->ls_caller)
4310Sstevel@tonic-gate return (1);
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate return (0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate static int
site_and_count_cmp_anylock(lsrec_t * a,lsrec_t * b)4370Sstevel@tonic-gate site_and_count_cmp_anylock(lsrec_t *a, lsrec_t *b)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate int i;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate if (a->ls_event < b->ls_event)
4420Sstevel@tonic-gate return (-1);
4430Sstevel@tonic-gate if (a->ls_event > b->ls_event)
4440Sstevel@tonic-gate return (1);
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate for (i = g_stkdepth - 1; i >= 0; i--) {
4470Sstevel@tonic-gate if (a->ls_stack[i] < b->ls_stack[i])
4480Sstevel@tonic-gate return (-1);
4490Sstevel@tonic-gate if (a->ls_stack[i] > b->ls_stack[i])
4500Sstevel@tonic-gate return (1);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate if (a->ls_caller < b->ls_caller)
4540Sstevel@tonic-gate return (-1);
4550Sstevel@tonic-gate if (a->ls_caller > b->ls_caller)
4560Sstevel@tonic-gate return (1);
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate return (b->ls_count - a->ls_count);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate static void
mergesort(int (* cmp)(lsrec_t *,lsrec_t *),lsrec_t ** a,lsrec_t ** b,int n)4620Sstevel@tonic-gate mergesort(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **a, lsrec_t **b, int n)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate int m = n / 2;
4650Sstevel@tonic-gate int i, j;
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate if (m > 1)
4680Sstevel@tonic-gate mergesort(cmp, a, b, m);
4690Sstevel@tonic-gate if (n - m > 1)
4700Sstevel@tonic-gate mergesort(cmp, a + m, b + m, n - m);
4710Sstevel@tonic-gate for (i = m; i > 0; i--)
4720Sstevel@tonic-gate b[i - 1] = a[i - 1];
4730Sstevel@tonic-gate for (j = m - 1; j < n - 1; j++)
4740Sstevel@tonic-gate b[n + m - j - 2] = a[j + 1];
4750Sstevel@tonic-gate while (i < j)
4760Sstevel@tonic-gate *a++ = cmp(b[i], b[j]) < 0 ? b[i++] : b[j--];
4770Sstevel@tonic-gate *a = b[i];
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate static void
coalesce(int (* cmp)(lsrec_t *,lsrec_t *),lsrec_t ** lock,int n)4810Sstevel@tonic-gate coalesce(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **lock, int n)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate int i, j;
4840Sstevel@tonic-gate lsrec_t *target, *current;
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate target = lock[0];
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate for (i = 1; i < n; i++) {
4890Sstevel@tonic-gate current = lock[i];
4900Sstevel@tonic-gate if (cmp(current, target) != 0) {
4910Sstevel@tonic-gate target = current;
4920Sstevel@tonic-gate continue;
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate current->ls_event = LS_MAX_EVENTS;
4950Sstevel@tonic-gate target->ls_count += current->ls_count;
4960Sstevel@tonic-gate target->ls_refcnt += current->ls_refcnt;
4970Sstevel@tonic-gate if (g_recsize < LS_TIME)
4980Sstevel@tonic-gate continue;
4990Sstevel@tonic-gate target->ls_time += current->ls_time;
5000Sstevel@tonic-gate if (g_recsize < LS_HIST)
5010Sstevel@tonic-gate continue;
5020Sstevel@tonic-gate for (j = 0; j < 64; j++)
5030Sstevel@tonic-gate target->ls_hist[j] += current->ls_hist[j];
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate static void
coalesce_symbol(uintptr_t * addrp)5080Sstevel@tonic-gate coalesce_symbol(uintptr_t *addrp)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate uintptr_t symoff;
5110Sstevel@tonic-gate size_t symsize;
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate if (addr_to_sym(*addrp, &symoff, &symsize) != NULL && symoff < symsize)
5140Sstevel@tonic-gate *addrp -= symoff;
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate static void
predicate_add(char ** pred,char * what,char * cmp,uintptr_t value)5180Sstevel@tonic-gate predicate_add(char **pred, char *what, char *cmp, uintptr_t value)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate char *new;
5210Sstevel@tonic-gate int len, newlen;
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate if (what == NULL)
5240Sstevel@tonic-gate return;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate if (*pred == NULL) {
5270Sstevel@tonic-gate *pred = malloc(1);
5280Sstevel@tonic-gate *pred[0] = '\0';
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate len = strlen(*pred);
5320Sstevel@tonic-gate newlen = len + strlen(what) + 32 + strlen("( && )");
5330Sstevel@tonic-gate new = malloc(newlen);
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate if (*pred[0] != '\0') {
5360Sstevel@tonic-gate if (cmp != NULL) {
5370Sstevel@tonic-gate (void) sprintf(new, "(%s) && (%s %s 0x%p)",
5380Sstevel@tonic-gate *pred, what, cmp, (void *)value);
5390Sstevel@tonic-gate } else {
5400Sstevel@tonic-gate (void) sprintf(new, "(%s) && (%s)", *pred, what);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate } else {
5430Sstevel@tonic-gate if (cmp != NULL) {
5440Sstevel@tonic-gate (void) sprintf(new, "%s %s 0x%p",
5450Sstevel@tonic-gate what, cmp, (void *)value);
5460Sstevel@tonic-gate } else {
5470Sstevel@tonic-gate (void) sprintf(new, "%s", what);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate free(*pred);
5520Sstevel@tonic-gate *pred = new;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate static void
predicate_destroy(char ** pred)5560Sstevel@tonic-gate predicate_destroy(char **pred)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate free(*pred);
5590Sstevel@tonic-gate *pred = NULL;
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate static void
filter_add(char ** filt,char * what,uintptr_t base,uintptr_t size)5630Sstevel@tonic-gate filter_add(char **filt, char *what, uintptr_t base, uintptr_t size)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate char buf[256], *c = buf, *new;
5660Sstevel@tonic-gate int len, newlen;
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate if (*filt == NULL) {
5690Sstevel@tonic-gate *filt = malloc(1);
5700Sstevel@tonic-gate *filt[0] = '\0';
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate (void) sprintf(c, "%s(%s >= 0x%p && %s < 0x%p)", *filt[0] != '\0' ?
5740Sstevel@tonic-gate " || " : "", what, (void *)base, what, (void *)(base + size));
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate newlen = (len = strlen(*filt) + 1) + strlen(c);
5770Sstevel@tonic-gate new = malloc(newlen);
5780Sstevel@tonic-gate bcopy(*filt, new, len);
5790Sstevel@tonic-gate (void) strcat(new, c);
5800Sstevel@tonic-gate free(*filt);
5810Sstevel@tonic-gate *filt = new;
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate static void
filter_destroy(char ** filt)5850Sstevel@tonic-gate filter_destroy(char **filt)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate free(*filt);
5880Sstevel@tonic-gate *filt = NULL;
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate static void
dprog_add(const char * fmt,...)5920Sstevel@tonic-gate dprog_add(const char *fmt, ...)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate va_list args;
5950Sstevel@tonic-gate int size, offs;
5960Sstevel@tonic-gate char c;
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate va_start(args, fmt);
5990Sstevel@tonic-gate size = vsnprintf(&c, 1, fmt, args) + 1;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate if (g_proglen == 0) {
6020Sstevel@tonic-gate offs = 0;
6030Sstevel@tonic-gate } else {
6040Sstevel@tonic-gate offs = g_proglen - 1;
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate g_proglen = offs + size;
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate if ((g_prog = realloc(g_prog, g_proglen)) == NULL)
6100Sstevel@tonic-gate fail(1, "failed to reallocate program text");
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate (void) vsnprintf(&g_prog[offs], size, fmt, args);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate * This function may read like an open sewer, but keep in mind that programs
6170Sstevel@tonic-gate * that generate other programs are rarely pretty. If one has the unenviable
6180Sstevel@tonic-gate * task of maintaining or -- worse -- extending this code, use the -V option
6190Sstevel@tonic-gate * to examine the D program as generated by this function.
6200Sstevel@tonic-gate */
6210Sstevel@tonic-gate static void
dprog_addevent(int event)6220Sstevel@tonic-gate dprog_addevent(int event)
6230Sstevel@tonic-gate {
6240Sstevel@tonic-gate ls_event_info_t *info = &g_event_info[event];
6250Sstevel@tonic-gate char *pred = NULL;
6260Sstevel@tonic-gate char stack[20];
6270Sstevel@tonic-gate const char *arg0, *caller;
6280Sstevel@tonic-gate char *arg1 = "arg1";
6290Sstevel@tonic-gate char buf[80];
6300Sstevel@tonic-gate hrtime_t dur;
6310Sstevel@tonic-gate int depth;
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate if (info->ev_name[0] == '\0')
6340Sstevel@tonic-gate return;
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate if (info->ev_type == 'I') {
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate * For interrupt events, arg0 (normally the lock pointer) is
6390Sstevel@tonic-gate * the CPU address plus the current pil, and arg1 (normally
6400Sstevel@tonic-gate * the number of nanoseconds) is the number of nanoseconds
6410Sstevel@tonic-gate * late -- and it's stored in arg2.
6420Sstevel@tonic-gate */
6430Sstevel@tonic-gate arg0 = "(uintptr_t)curthread->t_cpu + \n"
6440Sstevel@tonic-gate "\t curthread->t_cpu->cpu_profile_pil";
6450Sstevel@tonic-gate caller = "(uintptr_t)arg0";
6460Sstevel@tonic-gate arg1 = "arg2";
6470Sstevel@tonic-gate } else {
6480Sstevel@tonic-gate arg0 = "(uintptr_t)arg0";
6490Sstevel@tonic-gate caller = "caller";
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate if (g_recsize > LS_HIST) {
6530Sstevel@tonic-gate for (depth = 0; g_recsize > LS_STACK(depth); depth++)
6540Sstevel@tonic-gate continue;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate if (g_tracing) {
6570Sstevel@tonic-gate (void) sprintf(stack, "\tstack(%d);\n", depth);
6580Sstevel@tonic-gate } else {
6590Sstevel@tonic-gate (void) sprintf(stack, ", stack(%d)", depth);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate } else {
6620Sstevel@tonic-gate (void) sprintf(stack, "");
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate if (info->ev_acquire != NULL) {
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate * If this is a hold event, we need to generate an additional
6680Sstevel@tonic-gate * clause for the acquire; the clause for the release will be
6690Sstevel@tonic-gate * generated with the aggregating statement, below.
6700Sstevel@tonic-gate */
6710Sstevel@tonic-gate dprog_add("%s\n", info->ev_acquire);
6720Sstevel@tonic-gate predicate_add(&pred, info->ev_predicate, NULL, 0);
6730Sstevel@tonic-gate predicate_add(&pred, g_predicate, NULL, 0);
6740Sstevel@tonic-gate if (pred != NULL)
6750Sstevel@tonic-gate dprog_add("/%s/\n", pred);
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate dprog_add("{\n");
6780Sstevel@tonic-gate (void) sprintf(buf, "self->ev%d[(uintptr_t)arg0]", event);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate if (info->ev_type == 'H') {
6810Sstevel@tonic-gate dprog_add("\t%s = timestamp;\n", buf);
6820Sstevel@tonic-gate } else {
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate * If this isn't a hold event, it's the recursive
6850Sstevel@tonic-gate * error event. For this, we simply bump the
6860Sstevel@tonic-gate * thread-local, per-lock count.
6870Sstevel@tonic-gate */
6880Sstevel@tonic-gate dprog_add("\t%s++;\n", buf);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate dprog_add("}\n\n");
6920Sstevel@tonic-gate predicate_destroy(&pred);
6930Sstevel@tonic-gate pred = NULL;
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate if (info->ev_type == 'E') {
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate * If this is the recursive lock error event, we need
6980Sstevel@tonic-gate * to generate an additional clause to decrement the
6990Sstevel@tonic-gate * thread-local, per-lock count. This assures that we
7000Sstevel@tonic-gate * only execute the aggregating clause if we have
7010Sstevel@tonic-gate * recursive entry.
7020Sstevel@tonic-gate */
7030Sstevel@tonic-gate dprog_add("%s\n", info->ev_name);
7040Sstevel@tonic-gate dprog_add("/%s/\n{\n\t%s--;\n}\n\n", buf, buf);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate predicate_add(&pred, buf, NULL, 0);
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate if (info->ev_type == 'H') {
7100Sstevel@tonic-gate (void) sprintf(buf, "timestamp -\n\t "
7110Sstevel@tonic-gate "self->ev%d[(uintptr_t)arg0]", event);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate arg1 = buf;
7150Sstevel@tonic-gate } else {
7160Sstevel@tonic-gate predicate_add(&pred, info->ev_predicate, NULL, 0);
7170Sstevel@tonic-gate if (info->ev_type != 'I')
7180Sstevel@tonic-gate predicate_add(&pred, g_predicate, NULL, 0);
7190Sstevel@tonic-gate else
7200Sstevel@tonic-gate predicate_add(&pred, g_ipredicate, NULL, 0);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate if ((dur = g_min_duration[event]) != 0)
7240Sstevel@tonic-gate predicate_add(&pred, arg1, ">=", dur);
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate dprog_add("%s\n", info->ev_name);
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate if (pred != NULL)
7290Sstevel@tonic-gate dprog_add("/%s/\n", pred);
7300Sstevel@tonic-gate predicate_destroy(&pred);
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate dprog_add("{\n");
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate if (g_tracing) {
7350Sstevel@tonic-gate dprog_add("\ttrace(%dULL);\n", event);
7360Sstevel@tonic-gate dprog_add("\ttrace(%s);\n", arg0);
7370Sstevel@tonic-gate dprog_add("\ttrace(%s);\n", caller);
7380Sstevel@tonic-gate dprog_add(stack);
7390Sstevel@tonic-gate } else {
7401028Sbmc /*
7411028Sbmc * The ordering here is important: when we process the
7421028Sbmc * aggregate, we count on the fact that @avg appears before
7431028Sbmc * @hist in program order to assure that @avg is assigned the
7441028Sbmc * first aggregation variable ID and @hist assigned the
7451028Sbmc * second; see the comment in process_aggregate() for details.
7461028Sbmc */
7470Sstevel@tonic-gate dprog_add("\t@avg[%dULL, %s, %s%s] = avg(%s);\n",
7480Sstevel@tonic-gate event, arg0, caller, stack, arg1);
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate if (g_recsize >= LS_HIST) {
7510Sstevel@tonic-gate dprog_add("\t@hist[%dULL, %s, %s%s] = quantize"
7520Sstevel@tonic-gate "(%s);\n", event, arg0, caller, stack, arg1);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate if (info->ev_acquire != NULL)
7570Sstevel@tonic-gate dprog_add("\tself->ev%d[arg0] = 0;\n", event);
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate dprog_add("}\n\n");
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate static void
dprog_compile()7630Sstevel@tonic-gate dprog_compile()
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate dtrace_prog_t *prog;
7660Sstevel@tonic-gate dtrace_proginfo_t info;
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate if (g_Vflag) {
7690Sstevel@tonic-gate (void) fprintf(stderr, "lockstat: vvvv D program vvvv\n");
7700Sstevel@tonic-gate (void) fputs(g_prog, stderr);
7710Sstevel@tonic-gate (void) fprintf(stderr, "lockstat: ^^^^ D program ^^^^\n");
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate
7740Sstevel@tonic-gate if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
7750Sstevel@tonic-gate DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
7760Sstevel@tonic-gate dfail("failed to compile program");
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate if (dtrace_program_exec(g_dtp, prog, &info) == -1)
7790Sstevel@tonic-gate dfail("failed to enable probes");
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate if (dtrace_go(g_dtp) != 0)
7820Sstevel@tonic-gate dfail("couldn't start tracing");
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate static void
status_fire(void)7860Sstevel@tonic-gate status_fire(void)
7870Sstevel@tonic-gate {}
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate static void
status_init(void)7900Sstevel@tonic-gate status_init(void)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate dtrace_optval_t val, status, agg;
7930Sstevel@tonic-gate struct sigaction act;
7940Sstevel@tonic-gate struct itimerspec ts;
7950Sstevel@tonic-gate struct sigevent ev;
7960Sstevel@tonic-gate timer_t tid;
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate if (dtrace_getopt(g_dtp, "statusrate", &status) == -1)
7990Sstevel@tonic-gate dfail("failed to get 'statusrate'");
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate if (dtrace_getopt(g_dtp, "aggrate", &agg) == -1)
8020Sstevel@tonic-gate dfail("failed to get 'statusrate'");
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate * We would want to awaken at a rate that is the GCD of the statusrate
8060Sstevel@tonic-gate * and the aggrate -- but that seems a bit absurd. Instead, we'll
8070Sstevel@tonic-gate * simply awaken at a rate that is the more frequent of the two, which
8080Sstevel@tonic-gate * assures that we're never later than the interval implied by the
8090Sstevel@tonic-gate * more frequent rate.
8100Sstevel@tonic-gate */
8110Sstevel@tonic-gate val = status < agg ? status : agg;
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask);
8140Sstevel@tonic-gate act.sa_flags = 0;
8150Sstevel@tonic-gate act.sa_handler = status_fire;
8160Sstevel@tonic-gate (void) sigaction(SIGUSR1, &act, NULL);
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate ev.sigev_notify = SIGEV_SIGNAL;
8190Sstevel@tonic-gate ev.sigev_signo = SIGUSR1;
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
8220Sstevel@tonic-gate dfail("cannot create CLOCK_REALTIME timer");
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate ts.it_value.tv_sec = val / NANOSEC;
8250Sstevel@tonic-gate ts.it_value.tv_nsec = val % NANOSEC;
8260Sstevel@tonic-gate ts.it_interval = ts.it_value;
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
8290Sstevel@tonic-gate dfail("cannot set time on CLOCK_REALTIME timer");
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate static void
status_check(void)8330Sstevel@tonic-gate status_check(void)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate if (!g_tracing && dtrace_aggregate_snap(g_dtp) != 0)
8360Sstevel@tonic-gate dfail("failed to snap aggregate");
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate if (dtrace_status(g_dtp) == -1)
8390Sstevel@tonic-gate dfail("dtrace_status()");
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate static void
lsrec_fill(lsrec_t * lsrec,const dtrace_recdesc_t * rec,int nrecs,caddr_t data)843457Sbmc lsrec_fill(lsrec_t *lsrec, const dtrace_recdesc_t *rec, int nrecs, caddr_t data)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate bzero(lsrec, g_recsize);
8460Sstevel@tonic-gate lsrec->ls_count = 1;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate if ((g_recsize > LS_HIST && nrecs < 4) || (nrecs < 3))
8490Sstevel@tonic-gate fail(0, "truncated DTrace record");
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate if (rec->dtrd_size != sizeof (uint64_t))
8520Sstevel@tonic-gate fail(0, "bad event size in first record");
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate /* LINTED - alignment */
8550Sstevel@tonic-gate lsrec->ls_event = (uint32_t)*((uint64_t *)(data + rec->dtrd_offset));
8560Sstevel@tonic-gate rec++;
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate if (rec->dtrd_size != sizeof (uintptr_t))
8590Sstevel@tonic-gate fail(0, "bad lock address size in second record");
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate /* LINTED - alignment */
8620Sstevel@tonic-gate lsrec->ls_lock = *((uintptr_t *)(data + rec->dtrd_offset));
8630Sstevel@tonic-gate rec++;
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate if (rec->dtrd_size != sizeof (uintptr_t))
8660Sstevel@tonic-gate fail(0, "bad caller size in third record");
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate /* LINTED - alignment */
8690Sstevel@tonic-gate lsrec->ls_caller = *((uintptr_t *)(data + rec->dtrd_offset));
8700Sstevel@tonic-gate rec++;
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate if (g_recsize > LS_HIST) {
8730Sstevel@tonic-gate int frames, i;
8740Sstevel@tonic-gate pc_t *stack;
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate frames = rec->dtrd_size / sizeof (pc_t);
8770Sstevel@tonic-gate /* LINTED - alignment */
8780Sstevel@tonic-gate stack = (pc_t *)(data + rec->dtrd_offset);
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate for (i = 1; i < frames; i++)
8810Sstevel@tonic-gate lsrec->ls_stack[i - 1] = stack[i];
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate /*ARGSUSED*/
8860Sstevel@tonic-gate static int
count_aggregate(const dtrace_aggdata_t * agg,void * arg)887457Sbmc count_aggregate(const dtrace_aggdata_t *agg, void *arg)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate *((size_t *)arg) += 1;
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate return (DTRACE_AGGWALK_NEXT);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate static int
process_aggregate(const dtrace_aggdata_t * agg,void * arg)895457Sbmc process_aggregate(const dtrace_aggdata_t *agg, void *arg)
8960Sstevel@tonic-gate {
897457Sbmc const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
8980Sstevel@tonic-gate caddr_t data = agg->dtada_data;
8990Sstevel@tonic-gate lsdata_t *lsdata = arg;
9000Sstevel@tonic-gate lsrec_t *lsrec = lsdata->lsd_next;
901457Sbmc const dtrace_recdesc_t *rec;
9020Sstevel@tonic-gate uint64_t *avg, *quantized;
9030Sstevel@tonic-gate int i, j;
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate assert(lsdata->lsd_count < g_nrecs);
9060Sstevel@tonic-gate
9071028Sbmc /*
9081028Sbmc * Aggregation variable IDs are guaranteed to be generated in program
9091028Sbmc * order, and they are guaranteed to start from DTRACE_AGGVARIDNONE
9101028Sbmc * plus one. As "avg" appears before "hist" in program order, we know
9111028Sbmc * that "avg" will be allocated the first aggregation variable ID, and
9121028Sbmc * "hist" will be allocated the second aggregation variable ID -- and
9131028Sbmc * we therefore use the aggregation variable ID to differentiate the
9141028Sbmc * cases.
9151028Sbmc */
9161028Sbmc if (aggdesc->dtagd_varid > DTRACE_AGGVARIDNONE + 1) {
9170Sstevel@tonic-gate /*
9181028Sbmc * If this is the histogram entry. We'll copy the quantized
9191028Sbmc * data into lc_hist, and jump over the rest.
9200Sstevel@tonic-gate */
9210Sstevel@tonic-gate rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
9220Sstevel@tonic-gate
9231028Sbmc if (aggdesc->dtagd_varid != DTRACE_AGGVARIDNONE + 2)
9241028Sbmc fail(0, "bad variable ID in aggregation record");
9251028Sbmc
9260Sstevel@tonic-gate if (rec->dtrd_size !=
9270Sstevel@tonic-gate DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
9280Sstevel@tonic-gate fail(0, "bad quantize size in aggregation record");
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate /* LINTED - alignment */
9310Sstevel@tonic-gate quantized = (uint64_t *)(data + rec->dtrd_offset);
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate for (i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
9340Sstevel@tonic-gate i < DTRACE_QUANTIZE_NBUCKETS; i++, j++)
9350Sstevel@tonic-gate lsrec->ls_hist[j] = quantized[i];
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate goto out;
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate lsrec_fill(lsrec, &aggdesc->dtagd_rec[1],
9410Sstevel@tonic-gate aggdesc->dtagd_nrecs - 1, data);
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate if (rec->dtrd_size != 2 * sizeof (uint64_t))
9460Sstevel@tonic-gate fail(0, "bad avg size in aggregation record");
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate /* LINTED - alignment */
9490Sstevel@tonic-gate avg = (uint64_t *)(data + rec->dtrd_offset);
9500Sstevel@tonic-gate lsrec->ls_count = (uint32_t)avg[0];
9510Sstevel@tonic-gate lsrec->ls_time = (uintptr_t)avg[1];
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate if (g_recsize >= LS_HIST)
9540Sstevel@tonic-gate return (DTRACE_AGGWALK_NEXT);
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate out:
9570Sstevel@tonic-gate lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
9580Sstevel@tonic-gate lsdata->lsd_count++;
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate return (DTRACE_AGGWALK_NEXT);
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate static int
process_trace(const dtrace_probedata_t * pdata,void * arg)9640Sstevel@tonic-gate process_trace(const dtrace_probedata_t *pdata, void *arg)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate lsdata_t *lsdata = arg;
9670Sstevel@tonic-gate lsrec_t *lsrec = lsdata->lsd_next;
9680Sstevel@tonic-gate dtrace_eprobedesc_t *edesc = pdata->dtpda_edesc;
9690Sstevel@tonic-gate caddr_t data = pdata->dtpda_data;
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate if (lsdata->lsd_count >= g_nrecs)
9720Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT);
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate lsrec_fill(lsrec, edesc->dtepd_rec, edesc->dtepd_nrecs, data);
9750Sstevel@tonic-gate
9760Sstevel@tonic-gate lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
9770Sstevel@tonic-gate lsdata->lsd_count++;
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT);
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate static int
process_data(FILE * out,char * data)9830Sstevel@tonic-gate process_data(FILE *out, char *data)
9840Sstevel@tonic-gate {
9850Sstevel@tonic-gate lsdata_t lsdata;
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate /* LINTED - alignment */
9880Sstevel@tonic-gate lsdata.lsd_next = (lsrec_t *)data;
9890Sstevel@tonic-gate lsdata.lsd_count = 0;
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate if (g_tracing) {
9920Sstevel@tonic-gate if (dtrace_consume(g_dtp, out,
9930Sstevel@tonic-gate process_trace, NULL, &lsdata) != 0)
9940Sstevel@tonic-gate dfail("failed to consume buffer");
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate return (lsdata.lsd_count);
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
10000Sstevel@tonic-gate process_aggregate, &lsdata) != 0)
10010Sstevel@tonic-gate dfail("failed to walk aggregate");
10020Sstevel@tonic-gate
10030Sstevel@tonic-gate return (lsdata.lsd_count);
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate
10060Sstevel@tonic-gate /*ARGSUSED*/
10070Sstevel@tonic-gate static int
drophandler(const dtrace_dropdata_t * data,void * arg)1008457Sbmc drophandler(const dtrace_dropdata_t *data, void *arg)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate g_dropped++;
10110Sstevel@tonic-gate (void) fprintf(stderr, "lockstat: warning: %s", data->dtdda_msg);
10120Sstevel@tonic-gate return (DTRACE_HANDLE_OK);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate int
main(int argc,char ** argv)10160Sstevel@tonic-gate main(int argc, char **argv)
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate char *data_buf;
10190Sstevel@tonic-gate lsrec_t *lsp, **current, **first, **sort_buf, **merge_buf;
10200Sstevel@tonic-gate FILE *out = stdout;
10210Sstevel@tonic-gate char c;
10220Sstevel@tonic-gate pid_t child;
10230Sstevel@tonic-gate int status;
10240Sstevel@tonic-gate int i, j;
10250Sstevel@tonic-gate hrtime_t duration;
10260Sstevel@tonic-gate char *addrp, *offp, *sizep, *evp, *lastp, *p;
10270Sstevel@tonic-gate uintptr_t addr;
10280Sstevel@tonic-gate size_t size, off;
10290Sstevel@tonic-gate int events_specified = 0;
10300Sstevel@tonic-gate int exec_errno = 0;
10310Sstevel@tonic-gate uint32_t event;
10320Sstevel@tonic-gate char *filt = NULL, *ifilt = NULL;
10330Sstevel@tonic-gate static uint64_t ev_count[LS_MAX_EVENTS + 1];
10340Sstevel@tonic-gate static uint64_t ev_time[LS_MAX_EVENTS + 1];
10350Sstevel@tonic-gate dtrace_optval_t aggsize;
10360Sstevel@tonic-gate char aggstr[10];
10370Sstevel@tonic-gate long ncpus;
10380Sstevel@tonic-gate int dynvar = 0;
10390Sstevel@tonic-gate int err;
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
10420Sstevel@tonic-gate fail(0, "cannot open dtrace library: %s",
10430Sstevel@tonic-gate dtrace_errmsg(NULL, err));
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
10470Sstevel@tonic-gate dfail("couldn't establish drop handler");
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate if (symtab_init() == -1)
10500Sstevel@tonic-gate fail(1, "can't load kernel symbols");
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate g_nrecs = DEFAULT_NRECS;
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != EOF) {
10550Sstevel@tonic-gate switch (c) {
10560Sstevel@tonic-gate case 'b':
10570Sstevel@tonic-gate g_recsize = LS_BASIC;
10580Sstevel@tonic-gate break;
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate case 't':
10610Sstevel@tonic-gate g_recsize = LS_TIME;
10620Sstevel@tonic-gate break;
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate case 'h':
10650Sstevel@tonic-gate g_recsize = LS_HIST;
10660Sstevel@tonic-gate break;
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate case 's':
10690Sstevel@tonic-gate if (!isdigit(optarg[0]))
10700Sstevel@tonic-gate usage();
10710Sstevel@tonic-gate g_stkdepth = atoi(optarg);
10720Sstevel@tonic-gate if (g_stkdepth > LS_MAX_STACK_DEPTH)
10730Sstevel@tonic-gate fail(0, "max stack depth is %d",
10740Sstevel@tonic-gate LS_MAX_STACK_DEPTH);
10750Sstevel@tonic-gate g_recsize = LS_STACK(g_stkdepth);
10760Sstevel@tonic-gate break;
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate case 'n':
10790Sstevel@tonic-gate if (!isdigit(optarg[0]))
10800Sstevel@tonic-gate usage();
10810Sstevel@tonic-gate g_nrecs = atoi(optarg);
10820Sstevel@tonic-gate break;
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate case 'd':
10850Sstevel@tonic-gate if (!isdigit(optarg[0]))
10860Sstevel@tonic-gate usage();
10870Sstevel@tonic-gate duration = atoll(optarg);
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate * XXX -- durations really should be per event
10910Sstevel@tonic-gate * since the units are different, but it's hard
10920Sstevel@tonic-gate * to express this nicely in the interface.
10930Sstevel@tonic-gate * Not clear yet what the cleanest solution is.
10940Sstevel@tonic-gate */
10950Sstevel@tonic-gate for (i = 0; i < LS_MAX_EVENTS; i++)
10960Sstevel@tonic-gate if (g_event_info[i].ev_type != 'E')
10970Sstevel@tonic-gate g_min_duration[i] = duration;
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate break;
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate case 'i':
11020Sstevel@tonic-gate if (!isdigit(optarg[0]))
11030Sstevel@tonic-gate usage();
11040Sstevel@tonic-gate i = atoi(optarg);
11050Sstevel@tonic-gate if (i <= 0)
11060Sstevel@tonic-gate usage();
11070Sstevel@tonic-gate if (i > MAX_HZ)
11080Sstevel@tonic-gate fail(0, "max interrupt rate is %d Hz", MAX_HZ);
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate for (j = 0; j < LS_MAX_EVENTS; j++)
11110Sstevel@tonic-gate if (strcmp(g_event_info[j].ev_desc,
11120Sstevel@tonic-gate "Profiling interrupt") == 0)
11130Sstevel@tonic-gate break;
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate (void) sprintf(g_event_info[j].ev_name,
11160Sstevel@tonic-gate "profile:::profile-%d", i);
11170Sstevel@tonic-gate break;
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate case 'l':
11200Sstevel@tonic-gate case 'f':
11210Sstevel@tonic-gate addrp = strtok(optarg, ",");
11220Sstevel@tonic-gate sizep = strtok(NULL, ",");
11230Sstevel@tonic-gate addrp = strtok(optarg, ",+");
11240Sstevel@tonic-gate offp = strtok(NULL, ",");
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate size = sizep ? strtoul(sizep, NULL, 0) : 1;
11270Sstevel@tonic-gate off = offp ? strtoul(offp, NULL, 0) : 0;
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate if (addrp[0] == '0') {
11300Sstevel@tonic-gate addr = strtoul(addrp, NULL, 16) + off;
11310Sstevel@tonic-gate } else {
11320Sstevel@tonic-gate addr = sym_to_addr(addrp) + off;
11330Sstevel@tonic-gate if (sizep == NULL)
11340Sstevel@tonic-gate size = sym_size(addrp) - off;
11350Sstevel@tonic-gate if (addr - off == 0)
11360Sstevel@tonic-gate fail(0, "symbol '%s' not found", addrp);
11370Sstevel@tonic-gate if (size == 0)
11380Sstevel@tonic-gate size = 1;
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate if (c == 'l') {
11430Sstevel@tonic-gate filter_add(&filt, "arg0", addr, size);
11440Sstevel@tonic-gate } else {
11450Sstevel@tonic-gate filter_add(&filt, "caller", addr, size);
11460Sstevel@tonic-gate filter_add(&ifilt, "arg0", addr, size);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate break;
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate case 'e':
11510Sstevel@tonic-gate evp = strtok_r(optarg, ",", &lastp);
11520Sstevel@tonic-gate while (evp) {
11530Sstevel@tonic-gate int ev1, ev2;
11540Sstevel@tonic-gate char *evp2;
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate (void) strtok(evp, "-");
11570Sstevel@tonic-gate evp2 = strtok(NULL, "-");
11580Sstevel@tonic-gate ev1 = atoi(evp);
11590Sstevel@tonic-gate ev2 = evp2 ? atoi(evp2) : ev1;
11600Sstevel@tonic-gate if ((uint_t)ev1 >= LS_MAX_EVENTS ||
11610Sstevel@tonic-gate (uint_t)ev2 >= LS_MAX_EVENTS || ev1 > ev2)
11620Sstevel@tonic-gate fail(0, "-e events out of range");
11630Sstevel@tonic-gate for (i = ev1; i <= ev2; i++)
11640Sstevel@tonic-gate g_enabled[i] = 1;
11650Sstevel@tonic-gate evp = strtok_r(NULL, ",", &lastp);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate events_specified = 1;
11680Sstevel@tonic-gate break;
11690Sstevel@tonic-gate
11700Sstevel@tonic-gate case 'c':
11710Sstevel@tonic-gate g_cflag = 1;
11720Sstevel@tonic-gate break;
11730Sstevel@tonic-gate
11740Sstevel@tonic-gate case 'k':
11750Sstevel@tonic-gate g_kflag = 1;
11760Sstevel@tonic-gate break;
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate case 'w':
11790Sstevel@tonic-gate g_wflag = 1;
11800Sstevel@tonic-gate break;
11810Sstevel@tonic-gate
11820Sstevel@tonic-gate case 'W':
11830Sstevel@tonic-gate g_Wflag = 1;
11840Sstevel@tonic-gate break;
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate case 'g':
11870Sstevel@tonic-gate g_gflag = 1;
11880Sstevel@tonic-gate break;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate case 'C':
11910Sstevel@tonic-gate case 'E':
11920Sstevel@tonic-gate case 'H':
11930Sstevel@tonic-gate case 'I':
11940Sstevel@tonic-gate for (i = 0; i < LS_MAX_EVENTS; i++)
11950Sstevel@tonic-gate if (g_event_info[i].ev_type == c)
11960Sstevel@tonic-gate g_enabled[i] = 1;
11970Sstevel@tonic-gate events_specified = 1;
11980Sstevel@tonic-gate break;
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate case 'A':
12010Sstevel@tonic-gate for (i = 0; i < LS_MAX_EVENTS; i++)
12020Sstevel@tonic-gate if (strchr("CH", g_event_info[i].ev_type))
12030Sstevel@tonic-gate g_enabled[i] = 1;
12040Sstevel@tonic-gate events_specified = 1;
12050Sstevel@tonic-gate break;
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate case 'T':
12080Sstevel@tonic-gate g_tracing = 1;
12090Sstevel@tonic-gate break;
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate case 'D':
12120Sstevel@tonic-gate if (!isdigit(optarg[0]))
12130Sstevel@tonic-gate usage();
12140Sstevel@tonic-gate g_topn = atoi(optarg);
12150Sstevel@tonic-gate break;
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate case 'R':
12180Sstevel@tonic-gate g_rates = 1;
12190Sstevel@tonic-gate break;
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate case 'p':
12220Sstevel@tonic-gate g_pflag = 1;
12230Sstevel@tonic-gate break;
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate case 'P':
12260Sstevel@tonic-gate g_Pflag = 1;
12270Sstevel@tonic-gate break;
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate case 'o':
12300Sstevel@tonic-gate if ((out = fopen(optarg, "w")) == NULL)
12310Sstevel@tonic-gate fail(1, "error opening file");
12320Sstevel@tonic-gate break;
12330Sstevel@tonic-gate
12340Sstevel@tonic-gate case 'V':
12350Sstevel@tonic-gate g_Vflag = 1;
12360Sstevel@tonic-gate break;
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate default:
12390Sstevel@tonic-gate if (strchr(LOCKSTAT_OPTSTR, c) == NULL)
12400Sstevel@tonic-gate usage();
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate if (filt != NULL) {
12450Sstevel@tonic-gate predicate_add(&g_predicate, filt, NULL, 0);
12460Sstevel@tonic-gate filter_destroy(&filt);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate
12490Sstevel@tonic-gate if (ifilt != NULL) {
12500Sstevel@tonic-gate predicate_add(&g_ipredicate, ifilt, NULL, 0);
12510Sstevel@tonic-gate filter_destroy(&ifilt);
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate if (g_recsize == 0) {
12550Sstevel@tonic-gate if (g_gflag) {
12560Sstevel@tonic-gate g_stkdepth = LS_MAX_STACK_DEPTH;
12570Sstevel@tonic-gate g_recsize = LS_STACK(g_stkdepth);
12580Sstevel@tonic-gate } else {
12590Sstevel@tonic-gate g_recsize = LS_TIME;
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate if (g_gflag && g_recsize <= LS_STACK(0))
12640Sstevel@tonic-gate fail(0, "'-g' requires at least '-s 1' data gathering");
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate * Make sure the alignment is reasonable
12680Sstevel@tonic-gate */
12690Sstevel@tonic-gate g_recsize = -(-g_recsize & -sizeof (uint64_t));
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate for (i = 0; i < LS_MAX_EVENTS; i++) {
12720Sstevel@tonic-gate /*
12730Sstevel@tonic-gate * If no events were specified, enable -C.
12740Sstevel@tonic-gate */
12750Sstevel@tonic-gate if (!events_specified && g_event_info[i].ev_type == 'C')
12760Sstevel@tonic-gate g_enabled[i] = 1;
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate for (i = 0; i < LS_MAX_EVENTS; i++) {
12800Sstevel@tonic-gate if (!g_enabled[i])
12810Sstevel@tonic-gate continue;
12820Sstevel@tonic-gate
12830Sstevel@tonic-gate if (g_event_info[i].ev_acquire != NULL) {
12840Sstevel@tonic-gate /*
12850Sstevel@tonic-gate * If we've enabled a hold event, we must explicitly
12860Sstevel@tonic-gate * allocate dynamic variable space.
12870Sstevel@tonic-gate */
12880Sstevel@tonic-gate dynvar = 1;
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate dprog_addevent(i);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate /*
12950Sstevel@tonic-gate * Make sure there are remaining arguments to specify a child command
12960Sstevel@tonic-gate * to execute.
12970Sstevel@tonic-gate */
12980Sstevel@tonic-gate if (argc <= optind)
12990Sstevel@tonic-gate usage();
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
13020Sstevel@tonic-gate dfail("couldn't determine number of online CPUs");
13030Sstevel@tonic-gate
13040Sstevel@tonic-gate /*
13050Sstevel@tonic-gate * By default, we set our data buffer size to be the number of records
13060Sstevel@tonic-gate * multiplied by the size of the record, doubled to account for some
13070Sstevel@tonic-gate * DTrace slop and divided by the number of CPUs. We silently clamp
13080Sstevel@tonic-gate * the aggregation size at both a minimum and a maximum to prevent
13090Sstevel@tonic-gate * absurdly low or high values.
13100Sstevel@tonic-gate */
13110Sstevel@tonic-gate if ((aggsize = (g_nrecs * g_recsize * 2) / ncpus) < MIN_AGGSIZE)
13120Sstevel@tonic-gate aggsize = MIN_AGGSIZE;
13130Sstevel@tonic-gate
13140Sstevel@tonic-gate if (aggsize > MAX_AGGSIZE)
13150Sstevel@tonic-gate aggsize = MAX_AGGSIZE;
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate (void) sprintf(aggstr, "%lld", (long long)aggsize);
13180Sstevel@tonic-gate
13190Sstevel@tonic-gate if (!g_tracing) {
13200Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "bufsize", "4k") == -1)
13210Sstevel@tonic-gate dfail("failed to set 'bufsize'");
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "aggsize", aggstr) == -1)
13240Sstevel@tonic-gate dfail("failed to set 'aggsize'");
13250Sstevel@tonic-gate
13260Sstevel@tonic-gate if (dynvar) {
13270Sstevel@tonic-gate /*
13280Sstevel@tonic-gate * If we're using dynamic variables, we set our
13290Sstevel@tonic-gate * dynamic variable size to be one megabyte per CPU,
13300Sstevel@tonic-gate * with a hard-limit of 32 megabytes. This may still
13310Sstevel@tonic-gate * be too small in some cases, but it can be tuned
13320Sstevel@tonic-gate * manually via -x if need be.
13330Sstevel@tonic-gate */
13340Sstevel@tonic-gate (void) sprintf(aggstr, "%ldm", ncpus < 32 ? ncpus : 32);
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "dynvarsize", aggstr) == -1)
13370Sstevel@tonic-gate dfail("failed to set 'dynvarsize'");
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate } else {
13400Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "bufsize", aggstr) == -1)
13410Sstevel@tonic-gate dfail("failed to set 'bufsize'");
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate
13440Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "statusrate", "10sec") == -1)
13450Sstevel@tonic-gate dfail("failed to set 'statusrate'");
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate optind = 1;
13480Sstevel@tonic-gate while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != EOF) {
13490Sstevel@tonic-gate switch (c) {
13500Sstevel@tonic-gate case 'x':
13510Sstevel@tonic-gate if ((p = strchr(optarg, '=')) != NULL)
13520Sstevel@tonic-gate *p++ = '\0';
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate if (dtrace_setopt(g_dtp, optarg, p) != 0)
13550Sstevel@tonic-gate dfail("failed to set -x %s", optarg);
13560Sstevel@tonic-gate break;
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate argc -= optind;
13610Sstevel@tonic-gate argv += optind;
13620Sstevel@tonic-gate
13630Sstevel@tonic-gate dprog_compile();
13640Sstevel@tonic-gate status_init();
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate g_elapsed = -gethrtime();
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate /*
13690Sstevel@tonic-gate * Spawn the specified command and wait for it to complete.
13700Sstevel@tonic-gate */
13710Sstevel@tonic-gate child = fork();
13720Sstevel@tonic-gate if (child == -1)
13730Sstevel@tonic-gate fail(1, "cannot fork");
13740Sstevel@tonic-gate if (child == 0) {
13750Sstevel@tonic-gate (void) dtrace_close(g_dtp);
13760Sstevel@tonic-gate (void) execvp(argv[0], &argv[0]);
13770Sstevel@tonic-gate exec_errno = errno;
13780Sstevel@tonic-gate exit(127);
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate while (waitpid(child, &status, WEXITED) != child)
13820Sstevel@tonic-gate status_check();
13830Sstevel@tonic-gate
13840Sstevel@tonic-gate g_elapsed += gethrtime();
13850Sstevel@tonic-gate
13860Sstevel@tonic-gate if (WIFEXITED(status)) {
13870Sstevel@tonic-gate if (WEXITSTATUS(status) != 0) {
13880Sstevel@tonic-gate if (exec_errno != 0) {
13890Sstevel@tonic-gate errno = exec_errno;
13900Sstevel@tonic-gate fail(1, "could not execute %s", argv[0]);
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate (void) fprintf(stderr,
13930Sstevel@tonic-gate "lockstat: warning: %s exited with code %d\n",
1394*6103Sck142721 argv[0], WEXITSTATUS(status));
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate } else {
13970Sstevel@tonic-gate (void) fprintf(stderr,
13980Sstevel@tonic-gate "lockstat: warning: %s died on signal %d\n",
1399*6103Sck142721 argv[0], WTERMSIG(status));
14000Sstevel@tonic-gate }
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate if (dtrace_stop(g_dtp) == -1)
14030Sstevel@tonic-gate dfail("failed to stop dtrace");
14040Sstevel@tonic-gate
14050Sstevel@tonic-gate /*
14060Sstevel@tonic-gate * Before we read out the results, we need to allocate our buffer.
14070Sstevel@tonic-gate * If we're tracing, then we'll just use the precalculated size. If
14080Sstevel@tonic-gate * we're not, then we'll take a snapshot of the aggregate, and walk
14090Sstevel@tonic-gate * it to count the number of records.
14100Sstevel@tonic-gate */
14110Sstevel@tonic-gate if (!g_tracing) {
14120Sstevel@tonic-gate if (dtrace_aggregate_snap(g_dtp) != 0)
14130Sstevel@tonic-gate dfail("failed to snap aggregate");
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate g_nrecs = 0;
14160Sstevel@tonic-gate
14170Sstevel@tonic-gate if (dtrace_aggregate_walk(g_dtp,
14180Sstevel@tonic-gate count_aggregate, &g_nrecs) != 0)
14190Sstevel@tonic-gate dfail("failed to walk aggregate");
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate if ((data_buf = memalign(sizeof (uint64_t),
14230Sstevel@tonic-gate (g_nrecs + 1) * g_recsize)) == NULL)
14240Sstevel@tonic-gate fail(1, "Memory allocation failed");
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate * Read out the DTrace data.
14280Sstevel@tonic-gate */
14290Sstevel@tonic-gate g_nrecs_used = process_data(out, data_buf);
14300Sstevel@tonic-gate
14310Sstevel@tonic-gate if (g_nrecs_used > g_nrecs || g_dropped)
14320Sstevel@tonic-gate (void) fprintf(stderr, "lockstat: warning: "
14330Sstevel@tonic-gate "ran out of data records (use -n for more)\n");
14340Sstevel@tonic-gate
14350Sstevel@tonic-gate /* LINTED - alignment */
14360Sstevel@tonic-gate for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
14370Sstevel@tonic-gate /* LINTED - alignment */
14380Sstevel@tonic-gate lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
14390Sstevel@tonic-gate ev_count[lsp->ls_event] += lsp->ls_count;
14400Sstevel@tonic-gate ev_time[lsp->ls_event] += lsp->ls_time;
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate /*
14440Sstevel@tonic-gate * If -g was specified, convert stacks into individual records.
14450Sstevel@tonic-gate */
14460Sstevel@tonic-gate if (g_gflag) {
14470Sstevel@tonic-gate lsrec_t *newlsp, *oldlsp;
14480Sstevel@tonic-gate
14490Sstevel@tonic-gate newlsp = memalign(sizeof (uint64_t),
14500Sstevel@tonic-gate g_nrecs_used * LS_TIME * (g_stkdepth + 1));
14510Sstevel@tonic-gate if (newlsp == NULL)
14520Sstevel@tonic-gate fail(1, "Cannot allocate space for -g processing");
14530Sstevel@tonic-gate lsp = newlsp;
14540Sstevel@tonic-gate /* LINTED - alignment */
14550Sstevel@tonic-gate for (i = 0, oldlsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
14560Sstevel@tonic-gate /* LINTED - alignment */
14570Sstevel@tonic-gate oldlsp = (lsrec_t *)((char *)oldlsp + g_recsize)) {
14580Sstevel@tonic-gate int fr;
14590Sstevel@tonic-gate int caller_in_stack = 0;
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate if (oldlsp->ls_count == 0)
14620Sstevel@tonic-gate continue;
14630Sstevel@tonic-gate
14640Sstevel@tonic-gate for (fr = 0; fr < g_stkdepth; fr++) {
14650Sstevel@tonic-gate if (oldlsp->ls_stack[fr] == 0)
14660Sstevel@tonic-gate break;
14670Sstevel@tonic-gate if (oldlsp->ls_stack[fr] == oldlsp->ls_caller)
14680Sstevel@tonic-gate caller_in_stack = 1;
14690Sstevel@tonic-gate bcopy(oldlsp, lsp, LS_TIME);
14700Sstevel@tonic-gate lsp->ls_caller = oldlsp->ls_stack[fr];
14710Sstevel@tonic-gate /* LINTED - alignment */
14720Sstevel@tonic-gate lsp = (lsrec_t *)((char *)lsp + LS_TIME);
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate if (!caller_in_stack) {
14750Sstevel@tonic-gate bcopy(oldlsp, lsp, LS_TIME);
14760Sstevel@tonic-gate /* LINTED - alignment */
14770Sstevel@tonic-gate lsp = (lsrec_t *)((char *)lsp + LS_TIME);
14780Sstevel@tonic-gate }
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate g_nrecs = g_nrecs_used =
14810Sstevel@tonic-gate ((uintptr_t)lsp - (uintptr_t)newlsp) / LS_TIME;
14820Sstevel@tonic-gate g_recsize = LS_TIME;
14830Sstevel@tonic-gate g_stkdepth = 0;
14840Sstevel@tonic-gate free(data_buf);
14850Sstevel@tonic-gate data_buf = (char *)newlsp;
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate
14880Sstevel@tonic-gate if ((sort_buf = calloc(2 * (g_nrecs + 1),
14890Sstevel@tonic-gate sizeof (void *))) == NULL)
14900Sstevel@tonic-gate fail(1, "Sort buffer allocation failed");
14910Sstevel@tonic-gate merge_buf = sort_buf + (g_nrecs + 1);
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate /*
14940Sstevel@tonic-gate * Build the sort buffer, discarding zero-count records along the way.
14950Sstevel@tonic-gate */
14960Sstevel@tonic-gate /* LINTED - alignment */
14970Sstevel@tonic-gate for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
14980Sstevel@tonic-gate /* LINTED - alignment */
14990Sstevel@tonic-gate lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
15000Sstevel@tonic-gate if (lsp->ls_count == 0)
15010Sstevel@tonic-gate lsp->ls_event = LS_MAX_EVENTS;
15020Sstevel@tonic-gate sort_buf[i] = lsp;
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate
15050Sstevel@tonic-gate if (g_nrecs_used == 0)
15060Sstevel@tonic-gate exit(0);
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate /*
15090Sstevel@tonic-gate * Add a sentinel after the last record
15100Sstevel@tonic-gate */
15110Sstevel@tonic-gate sort_buf[i] = lsp;
15120Sstevel@tonic-gate lsp->ls_event = LS_MAX_EVENTS;
15130Sstevel@tonic-gate
15140Sstevel@tonic-gate if (g_tracing) {
15150Sstevel@tonic-gate report_trace(out, sort_buf);
15160Sstevel@tonic-gate return (0);
15170Sstevel@tonic-gate }
15180Sstevel@tonic-gate
15190Sstevel@tonic-gate /*
15200Sstevel@tonic-gate * Application of -g may have resulted in multiple records
15210Sstevel@tonic-gate * with the same signature; coalesce them.
15220Sstevel@tonic-gate */
15230Sstevel@tonic-gate if (g_gflag) {
15240Sstevel@tonic-gate mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
15250Sstevel@tonic-gate coalesce(lockcmp, sort_buf, g_nrecs_used);
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate
15280Sstevel@tonic-gate /*
15290Sstevel@tonic-gate * Coalesce locks within the same symbol if -c option specified.
15300Sstevel@tonic-gate * Coalesce PCs within the same function if -k option specified.
15310Sstevel@tonic-gate */
15320Sstevel@tonic-gate if (g_cflag || g_kflag) {
15330Sstevel@tonic-gate for (i = 0; i < g_nrecs_used; i++) {
15340Sstevel@tonic-gate int fr;
15350Sstevel@tonic-gate lsp = sort_buf[i];
15360Sstevel@tonic-gate if (g_cflag)
15370Sstevel@tonic-gate coalesce_symbol(&lsp->ls_lock);
15380Sstevel@tonic-gate if (g_kflag) {
15390Sstevel@tonic-gate for (fr = 0; fr < g_stkdepth; fr++)
15400Sstevel@tonic-gate coalesce_symbol(&lsp->ls_stack[fr]);
15410Sstevel@tonic-gate coalesce_symbol(&lsp->ls_caller);
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
15450Sstevel@tonic-gate coalesce(lockcmp, sort_buf, g_nrecs_used);
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate
15480Sstevel@tonic-gate /*
15490Sstevel@tonic-gate * Coalesce callers if -w option specified
15500Sstevel@tonic-gate */
15510Sstevel@tonic-gate if (g_wflag) {
15520Sstevel@tonic-gate mergesort(lock_and_count_cmp_anywhere,
15530Sstevel@tonic-gate sort_buf, merge_buf, g_nrecs_used);
15540Sstevel@tonic-gate coalesce(lockcmp_anywhere, sort_buf, g_nrecs_used);
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate * Coalesce locks if -W option specified
15590Sstevel@tonic-gate */
15600Sstevel@tonic-gate if (g_Wflag) {
15610Sstevel@tonic-gate mergesort(site_and_count_cmp_anylock,
15620Sstevel@tonic-gate sort_buf, merge_buf, g_nrecs_used);
15630Sstevel@tonic-gate coalesce(sitecmp_anylock, sort_buf, g_nrecs_used);
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate
15660Sstevel@tonic-gate /*
15670Sstevel@tonic-gate * Sort data by contention count (ls_count) or total time (ls_time),
15680Sstevel@tonic-gate * depending on g_Pflag. Override g_Pflag if time wasn't measured.
15690Sstevel@tonic-gate */
15700Sstevel@tonic-gate if (g_recsize < LS_TIME)
15710Sstevel@tonic-gate g_Pflag = 0;
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate if (g_Pflag)
15740Sstevel@tonic-gate mergesort(timecmp, sort_buf, merge_buf, g_nrecs_used);
15750Sstevel@tonic-gate else
15760Sstevel@tonic-gate mergesort(countcmp, sort_buf, merge_buf, g_nrecs_used);
15770Sstevel@tonic-gate
15780Sstevel@tonic-gate /*
15790Sstevel@tonic-gate * Display data by event type
15800Sstevel@tonic-gate */
15810Sstevel@tonic-gate first = &sort_buf[0];
15820Sstevel@tonic-gate while ((event = (*first)->ls_event) < LS_MAX_EVENTS) {
15830Sstevel@tonic-gate current = first;
15840Sstevel@tonic-gate while ((lsp = *current)->ls_event == event)
15850Sstevel@tonic-gate current++;
15860Sstevel@tonic-gate report_stats(out, first, current - first, ev_count[event],
15870Sstevel@tonic-gate ev_time[event]);
15880Sstevel@tonic-gate first = current;
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate return (0);
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate
15940Sstevel@tonic-gate static char *
format_symbol(char * buf,uintptr_t addr,int show_size)15950Sstevel@tonic-gate format_symbol(char *buf, uintptr_t addr, int show_size)
15960Sstevel@tonic-gate {
15970Sstevel@tonic-gate uintptr_t symoff;
15980Sstevel@tonic-gate char *symname;
15990Sstevel@tonic-gate size_t symsize;
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate symname = addr_to_sym(addr, &symoff, &symsize);
16020Sstevel@tonic-gate
16030Sstevel@tonic-gate if (show_size && symoff == 0)
16040Sstevel@tonic-gate (void) sprintf(buf, "%s[%ld]", symname, (long)symsize);
16050Sstevel@tonic-gate else if (symoff == 0)
16060Sstevel@tonic-gate (void) sprintf(buf, "%s", symname);
16070Sstevel@tonic-gate else if (symoff < 16 && bcmp(symname, "cpu[", 4) == 0) /* CPU+PIL */
16080Sstevel@tonic-gate (void) sprintf(buf, "%s+%ld", symname, (long)symoff);
16090Sstevel@tonic-gate else if (symoff <= symsize || (symoff < 256 && addr != symoff))
16100Sstevel@tonic-gate (void) sprintf(buf, "%s+0x%llx", symname,
16110Sstevel@tonic-gate (unsigned long long)symoff);
16120Sstevel@tonic-gate else
16130Sstevel@tonic-gate (void) sprintf(buf, "0x%llx", (unsigned long long)addr);
16140Sstevel@tonic-gate return (buf);
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate
16170Sstevel@tonic-gate static void
report_stats(FILE * out,lsrec_t ** sort_buf,size_t nrecs,uint64_t total_count,uint64_t total_time)16180Sstevel@tonic-gate report_stats(FILE *out, lsrec_t **sort_buf, size_t nrecs, uint64_t total_count,
16190Sstevel@tonic-gate uint64_t total_time)
16200Sstevel@tonic-gate {
16210Sstevel@tonic-gate uint32_t event = sort_buf[0]->ls_event;
16220Sstevel@tonic-gate lsrec_t *lsp;
16230Sstevel@tonic-gate double ptotal = 0.0;
16240Sstevel@tonic-gate double percent;
16250Sstevel@tonic-gate int i, j, fr;
16260Sstevel@tonic-gate int displayed;
16270Sstevel@tonic-gate int first_bin, last_bin, max_bin_count, total_bin_count;
16280Sstevel@tonic-gate int rectype;
16290Sstevel@tonic-gate char buf[256];
16300Sstevel@tonic-gate char lhdr[80], chdr[80];
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate rectype = g_recsize;
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate if (g_topn == 0) {
16350Sstevel@tonic-gate (void) fprintf(out, "%20llu %s\n",
16360Sstevel@tonic-gate g_rates == 0 ? total_count :
16370Sstevel@tonic-gate ((unsigned long long)total_count * NANOSEC) / g_elapsed,
16380Sstevel@tonic-gate g_event_info[event].ev_desc);
16390Sstevel@tonic-gate return;
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate (void) sprintf(lhdr, "%s%s",
16430Sstevel@tonic-gate g_Wflag ? "Hottest " : "", g_event_info[event].ev_lhdr);
16440Sstevel@tonic-gate (void) sprintf(chdr, "%s%s",
16450Sstevel@tonic-gate g_wflag ? "Hottest " : "", "Caller");
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate if (!g_pflag)
16480Sstevel@tonic-gate (void) fprintf(out,
16490Sstevel@tonic-gate "\n%s: %.0f events in %.3f seconds (%.0f events/sec)\n\n",
16500Sstevel@tonic-gate g_event_info[event].ev_desc, (double)total_count,
16510Sstevel@tonic-gate (double)g_elapsed / NANOSEC,
16520Sstevel@tonic-gate (double)total_count * NANOSEC / g_elapsed);
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate if (!g_pflag && rectype < LS_HIST) {
16550Sstevel@tonic-gate (void) sprintf(buf, "%s", g_event_info[event].ev_units);
16560Sstevel@tonic-gate (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
16570Sstevel@tonic-gate g_rates ? "ops/s" : "Count",
16580Sstevel@tonic-gate g_gflag ? "genr" : "indv",
16590Sstevel@tonic-gate "cuml", "rcnt", rectype >= LS_TIME ? buf : "", lhdr, chdr);
16600Sstevel@tonic-gate (void) fprintf(out, "---------------------------------"
16610Sstevel@tonic-gate "----------------------------------------------\n");
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate
16640Sstevel@tonic-gate displayed = 0;
16650Sstevel@tonic-gate for (i = 0; i < nrecs; i++) {
16660Sstevel@tonic-gate lsp = sort_buf[i];
16670Sstevel@tonic-gate
16680Sstevel@tonic-gate if (displayed++ >= g_topn)
16690Sstevel@tonic-gate break;
16700Sstevel@tonic-gate
16710Sstevel@tonic-gate if (g_pflag) {
16720Sstevel@tonic-gate int j;
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate (void) fprintf(out, "%u %u",
16750Sstevel@tonic-gate lsp->ls_event, lsp->ls_count);
16760Sstevel@tonic-gate (void) fprintf(out, " %s",
16770Sstevel@tonic-gate format_symbol(buf, lsp->ls_lock, g_cflag));
16780Sstevel@tonic-gate (void) fprintf(out, " %s",
16790Sstevel@tonic-gate format_symbol(buf, lsp->ls_caller, 0));
16800Sstevel@tonic-gate (void) fprintf(out, " %f",
16810Sstevel@tonic-gate (double)lsp->ls_refcnt / lsp->ls_count);
16820Sstevel@tonic-gate if (rectype >= LS_TIME)
16830Sstevel@tonic-gate (void) fprintf(out, " %llu",
16840Sstevel@tonic-gate (unsigned long long)lsp->ls_time);
16850Sstevel@tonic-gate if (rectype >= LS_HIST) {
16860Sstevel@tonic-gate for (j = 0; j < 64; j++)
16870Sstevel@tonic-gate (void) fprintf(out, " %u",
16880Sstevel@tonic-gate lsp->ls_hist[j]);
16890Sstevel@tonic-gate }
16900Sstevel@tonic-gate for (j = 0; j < LS_MAX_STACK_DEPTH; j++) {
16910Sstevel@tonic-gate if (rectype <= LS_STACK(j) ||
16920Sstevel@tonic-gate lsp->ls_stack[j] == 0)
16930Sstevel@tonic-gate break;
16940Sstevel@tonic-gate (void) fprintf(out, " %s",
16950Sstevel@tonic-gate format_symbol(buf, lsp->ls_stack[j], 0));
16960Sstevel@tonic-gate }
16970Sstevel@tonic-gate (void) fprintf(out, "\n");
16980Sstevel@tonic-gate continue;
16990Sstevel@tonic-gate }
17000Sstevel@tonic-gate
17010Sstevel@tonic-gate if (rectype >= LS_HIST) {
17020Sstevel@tonic-gate (void) fprintf(out, "---------------------------------"
17030Sstevel@tonic-gate "----------------------------------------------\n");
17040Sstevel@tonic-gate (void) sprintf(buf, "%s",
17050Sstevel@tonic-gate g_event_info[event].ev_units);
17060Sstevel@tonic-gate (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
17070Sstevel@tonic-gate g_rates ? "ops/s" : "Count",
17080Sstevel@tonic-gate g_gflag ? "genr" : "indv",
17090Sstevel@tonic-gate "cuml", "rcnt", buf, lhdr, chdr);
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate if (g_Pflag && total_time != 0)
17130Sstevel@tonic-gate percent = (lsp->ls_time * 100.00) / total_time;
17140Sstevel@tonic-gate else
17150Sstevel@tonic-gate percent = (lsp->ls_count * 100.00) / total_count;
17160Sstevel@tonic-gate
17170Sstevel@tonic-gate ptotal += percent;
17180Sstevel@tonic-gate
17190Sstevel@tonic-gate if (rectype >= LS_TIME)
17200Sstevel@tonic-gate (void) sprintf(buf, "%llu",
17210Sstevel@tonic-gate (unsigned long long)(lsp->ls_time / lsp->ls_count));
17220Sstevel@tonic-gate else
17230Sstevel@tonic-gate buf[0] = '\0';
17240Sstevel@tonic-gate
17250Sstevel@tonic-gate (void) fprintf(out, "%5llu ",
17260Sstevel@tonic-gate g_rates == 0 ? lsp->ls_count :
17270Sstevel@tonic-gate ((uint64_t)lsp->ls_count * NANOSEC) / g_elapsed);
17280Sstevel@tonic-gate
17290Sstevel@tonic-gate (void) fprintf(out, "%3.0f%% ", percent);
17300Sstevel@tonic-gate
17310Sstevel@tonic-gate if (g_gflag)
17320Sstevel@tonic-gate (void) fprintf(out, "---- ");
17330Sstevel@tonic-gate else
17340Sstevel@tonic-gate (void) fprintf(out, "%3.0f%% ", ptotal);
17350Sstevel@tonic-gate
17360Sstevel@tonic-gate (void) fprintf(out, "%4.2f %8s ",
17370Sstevel@tonic-gate (double)lsp->ls_refcnt / lsp->ls_count, buf);
17380Sstevel@tonic-gate
17390Sstevel@tonic-gate (void) fprintf(out, "%-22s ",
17400Sstevel@tonic-gate format_symbol(buf, lsp->ls_lock, g_cflag));
17410Sstevel@tonic-gate
17420Sstevel@tonic-gate (void) fprintf(out, "%-24s\n",
17430Sstevel@tonic-gate format_symbol(buf, lsp->ls_caller, 0));
17440Sstevel@tonic-gate
17450Sstevel@tonic-gate if (rectype < LS_HIST)
17460Sstevel@tonic-gate continue;
17470Sstevel@tonic-gate
17480Sstevel@tonic-gate (void) fprintf(out, "\n");
17490Sstevel@tonic-gate (void) fprintf(out, "%10s %31s %-9s %-24s\n",
1750*6103Sck142721 g_event_info[event].ev_units,
1751*6103Sck142721 "------ Time Distribution ------",
1752*6103Sck142721 g_rates ? "ops/s" : "count",
1753*6103Sck142721 rectype > LS_STACK(0) ? "Stack" : "");
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate first_bin = 0;
17560Sstevel@tonic-gate while (lsp->ls_hist[first_bin] == 0)
17570Sstevel@tonic-gate first_bin++;
17580Sstevel@tonic-gate
17590Sstevel@tonic-gate last_bin = 63;
17600Sstevel@tonic-gate while (lsp->ls_hist[last_bin] == 0)
17610Sstevel@tonic-gate last_bin--;
17620Sstevel@tonic-gate
17630Sstevel@tonic-gate max_bin_count = 0;
17640Sstevel@tonic-gate total_bin_count = 0;
17650Sstevel@tonic-gate for (j = first_bin; j <= last_bin; j++) {
17660Sstevel@tonic-gate total_bin_count += lsp->ls_hist[j];
17670Sstevel@tonic-gate if (lsp->ls_hist[j] > max_bin_count)
17680Sstevel@tonic-gate max_bin_count = lsp->ls_hist[j];
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate
17710Sstevel@tonic-gate /*
17720Sstevel@tonic-gate * If we went a few frames below the caller, ignore them
17730Sstevel@tonic-gate */
17740Sstevel@tonic-gate for (fr = 3; fr > 0; fr--)
17750Sstevel@tonic-gate if (lsp->ls_stack[fr] == lsp->ls_caller)
17760Sstevel@tonic-gate break;
17770Sstevel@tonic-gate
17780Sstevel@tonic-gate for (j = first_bin; j <= last_bin; j++) {
17790Sstevel@tonic-gate uint_t depth = (lsp->ls_hist[j] * 30) / total_bin_count;
17800Sstevel@tonic-gate (void) fprintf(out, "%10llu |%s%s %-9u ",
17810Sstevel@tonic-gate 1ULL << j,
17820Sstevel@tonic-gate "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + 30 - depth,
17830Sstevel@tonic-gate " " + depth,
17840Sstevel@tonic-gate g_rates == 0 ? lsp->ls_hist[j] :
17850Sstevel@tonic-gate (uint_t)(((uint64_t)lsp->ls_hist[j] * NANOSEC) /
17860Sstevel@tonic-gate g_elapsed));
17870Sstevel@tonic-gate if (rectype <= LS_STACK(fr) || lsp->ls_stack[fr] == 0) {
17880Sstevel@tonic-gate (void) fprintf(out, "\n");
17890Sstevel@tonic-gate continue;
17900Sstevel@tonic-gate }
17910Sstevel@tonic-gate (void) fprintf(out, "%-24s\n",
17920Sstevel@tonic-gate format_symbol(buf, lsp->ls_stack[fr], 0));
17930Sstevel@tonic-gate fr++;
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
17960Sstevel@tonic-gate (void) fprintf(out, "%15s %-36s %-24s\n", "", "",
17970Sstevel@tonic-gate format_symbol(buf, lsp->ls_stack[fr], 0));
17980Sstevel@tonic-gate fr++;
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate
18020Sstevel@tonic-gate if (!g_pflag)
18030Sstevel@tonic-gate (void) fprintf(out, "---------------------------------"
18040Sstevel@tonic-gate "----------------------------------------------\n");
18050Sstevel@tonic-gate
18060Sstevel@tonic-gate (void) fflush(out);
18070Sstevel@tonic-gate }
18080Sstevel@tonic-gate
18090Sstevel@tonic-gate static void
report_trace(FILE * out,lsrec_t ** sort_buf)18100Sstevel@tonic-gate report_trace(FILE *out, lsrec_t **sort_buf)
18110Sstevel@tonic-gate {
18120Sstevel@tonic-gate lsrec_t *lsp;
18130Sstevel@tonic-gate int i, fr;
18140Sstevel@tonic-gate int rectype;
18150Sstevel@tonic-gate char buf[256], buf2[256];
18160Sstevel@tonic-gate
18170Sstevel@tonic-gate rectype = g_recsize;
18180Sstevel@tonic-gate
18190Sstevel@tonic-gate if (!g_pflag) {
18200Sstevel@tonic-gate (void) fprintf(out, "%5s %7s %11s %-24s %-24s\n",
18210Sstevel@tonic-gate "Event", "Time", "Owner", "Lock", "Caller");
18220Sstevel@tonic-gate (void) fprintf(out, "---------------------------------"
18230Sstevel@tonic-gate "----------------------------------------------\n");
18240Sstevel@tonic-gate }
18250Sstevel@tonic-gate
18260Sstevel@tonic-gate for (i = 0; i < g_nrecs_used; i++) {
18270Sstevel@tonic-gate
18280Sstevel@tonic-gate lsp = sort_buf[i];
18290Sstevel@tonic-gate
18300Sstevel@tonic-gate if (lsp->ls_event >= LS_MAX_EVENTS || lsp->ls_count == 0)
18310Sstevel@tonic-gate continue;
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate (void) fprintf(out, "%2d %10llu %11p %-24s %-24s\n",
18340Sstevel@tonic-gate lsp->ls_event, (unsigned long long)lsp->ls_time,
18350Sstevel@tonic-gate (void *)lsp->ls_next,
18360Sstevel@tonic-gate format_symbol(buf, lsp->ls_lock, 0),
18370Sstevel@tonic-gate format_symbol(buf2, lsp->ls_caller, 0));
18380Sstevel@tonic-gate
18390Sstevel@tonic-gate if (rectype <= LS_STACK(0))
18400Sstevel@tonic-gate continue;
18410Sstevel@tonic-gate
18420Sstevel@tonic-gate /*
18430Sstevel@tonic-gate * If we went a few frames below the caller, ignore them
18440Sstevel@tonic-gate */
18450Sstevel@tonic-gate for (fr = 3; fr > 0; fr--)
18460Sstevel@tonic-gate if (lsp->ls_stack[fr] == lsp->ls_caller)
18470Sstevel@tonic-gate break;
18480Sstevel@tonic-gate
18490Sstevel@tonic-gate while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
18500Sstevel@tonic-gate (void) fprintf(out, "%53s %-24s\n", "",
18510Sstevel@tonic-gate format_symbol(buf, lsp->ls_stack[fr], 0));
18520Sstevel@tonic-gate fr++;
18530Sstevel@tonic-gate }
18540Sstevel@tonic-gate (void) fprintf(out, "\n");
18550Sstevel@tonic-gate }
18560Sstevel@tonic-gate
18570Sstevel@tonic-gate (void) fflush(out);
18580Sstevel@tonic-gate }
1859