4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <sys/types.h>
40 #include <sys/modctl.h>
44 #include <sys/lockstat.h>
50 #define GETOPT_EOF EOF
54 #include <sys/resource.h>
56 #define mergesort(a, b, c, d) lsmergesort(a, b, c, d)
57 #define GETOPT_EOF (-1)
59 typedef uintptr_t pc_t;
60 #endif /* defined(sun) */
62 #define LOCKSTAT_OPTSTR "x:bths:n:d:i:l:f:e:ckwWgCHEATID:RpPo:V"
64 #define LS_MAX_STACK_DEPTH 50
65 #define LS_MAX_EVENTS 64
67 typedef struct lsrec {
68 struct lsrec *ls_next; /* next in hash chain */
69 uintptr_t ls_lock; /* lock address */
70 uintptr_t ls_caller; /* caller address */
71 uint32_t ls_count; /* cumulative event count */
72 uint32_t ls_event; /* type of event */
73 uintptr_t ls_refcnt; /* cumulative reference count */
74 uint64_t ls_time; /* cumulative event duration */
75 uint32_t ls_hist[64]; /* log2(duration) histogram */
76 uintptr_t ls_stack[LS_MAX_STACK_DEPTH];
79 typedef struct lsdata {
80 struct lsrec *lsd_next; /* next available */
81 int lsd_count; /* number of records */
85 * Definitions for the types of experiments which can be run. They are
86 * listed in increasing order of memory cost and processing time cost.
87 * The numerical value of each type is the number of bytes needed per record.
89 #define LS_BASIC offsetof(lsrec_t, ls_time)
90 #define LS_TIME offsetof(lsrec_t, ls_hist[0])
91 #define LS_HIST offsetof(lsrec_t, ls_stack[0])
92 #define LS_STACK(depth) offsetof(lsrec_t, ls_stack[depth])
94 static void report_stats(FILE *, lsrec_t **, size_t, uint64_t, uint64_t);
95 static void report_trace(FILE *, lsrec_t **);
97 extern int symtab_init(void);
98 extern char *addr_to_sym(uintptr_t, uintptr_t *, size_t *);
99 extern uintptr_t sym_to_addr(char *name);
100 extern size_t sym_size(char *name);
101 extern char *strtok_r(char *, const char *, char **);
103 #define DEFAULT_NRECS 10000
104 #define DEFAULT_HZ 97
106 #define MIN_AGGSIZE (16 * 1024)
107 #define MAX_AGGSIZE (32 * 1024 * 1024)
109 static int g_stkdepth;
110 static int g_topn = INT_MAX;
111 static hrtime_t g_elapsed;
112 static int g_rates = 0;
113 static int g_pflag = 0;
114 static int g_Pflag = 0;
115 static int g_wflag = 0;
116 static int g_Wflag = 0;
117 static int g_cflag = 0;
118 static int g_kflag = 0;
119 static int g_gflag = 0;
120 static int g_Vflag = 0;
121 static int g_tracing = 0;
122 static size_t g_recsize;
123 static size_t g_nrecs;
124 static int g_nrecs_used;
125 static uchar_t g_enabled[LS_MAX_EVENTS];
126 static hrtime_t g_min_duration[LS_MAX_EVENTS];
127 static dtrace_hdl_t *g_dtp;
128 static char *g_predicate;
129 static char *g_ipredicate;
131 static int g_proglen;
132 static int g_dropped;
134 typedef struct ls_event_info {
139 char ev_name[DTRACE_NAMELEN];
144 static ls_event_info_t g_event_info[LS_MAX_EVENTS] = {
145 { 'C', "Lock", "Adaptive mutex spin", "nsec",
146 "lockstat:::adaptive-spin" },
147 { 'C', "Lock", "Adaptive mutex block", "nsec",
148 "lockstat:::adaptive-block" },
149 { 'C', "Lock", "Spin lock spin", "nsec",
150 "lockstat:::spin-spin" },
151 { 'C', "Lock", "Thread lock spin", "nsec",
152 "lockstat:::thread-spin" },
153 { 'C', "Lock", "R/W writer blocked by writer", "nsec",
154 "lockstat:::rw-block", "arg2 == 0 && arg3 == 1" },
155 { 'C', "Lock", "R/W writer blocked by readers", "nsec",
156 "lockstat:::rw-block", "arg2 == 0 && arg3 == 0 && arg4" },
157 { 'C', "Lock", "R/W reader blocked by writer", "nsec",
158 "lockstat:::rw-block", "arg2 != 0 && arg3 == 1" },
159 { 'C', "Lock", "R/W reader blocked by write wanted", "nsec",
160 "lockstat:::rw-block", "arg2 != 0 && arg3 == 0 && arg4" },
161 { 'C', "Lock", "R/W writer spin on writer", "nsec",
162 "lockstat:::rw-spin", "arg2 == 0 && arg3 == 1" },
163 { 'C', "Lock", "R/W writer spin on readers", "nsec",
164 "lockstat:::rw-spin", "arg2 == 0 && arg3 == 0 && arg4" },
165 { 'C', "Lock", "R/W reader spin on writer", "nsec",
166 "lockstat:::rw-spin", "arg2 != 0 && arg3 == 1" },
167 { 'C', "Lock", "R/W reader spin on write wanted", "nsec",
168 "lockstat:::rw-spin", "arg2 != 0 && arg3 == 0 && arg4" },
169 { 'C', "Lock", "SX exclusive block", "nsec",
170 "lockstat:::sx-block", "arg2 == 0" },
171 { 'C', "Lock", "SX shared block", "nsec",
172 "lockstat:::sx-block", "arg2 != 0" },
173 { 'C', "Lock", "SX exclusive spin", "nsec",
174 "lockstat:::sx-spin", "arg2 == 0" },
175 { 'C', "Lock", "SX shared spin", "nsec",
176 "lockstat:::sx-spin", "arg2 != 0" },
177 { 'C', "Lock", "Unknown event (type 16)", "units" },
178 { 'C', "Lock", "Unknown event (type 17)", "units" },
179 { 'C', "Lock", "Unknown event (type 18)", "units" },
180 { 'C', "Lock", "Unknown event (type 19)", "units" },
181 { 'C', "Lock", "Unknown event (type 20)", "units" },
182 { 'C', "Lock", "Unknown event (type 21)", "units" },
183 { 'C', "Lock", "Unknown event (type 22)", "units" },
184 { 'C', "Lock", "Unknown event (type 23)", "units" },
185 { 'C', "Lock", "Unknown event (type 24)", "units" },
186 { 'C', "Lock", "Unknown event (type 25)", "units" },
187 { 'C', "Lock", "Unknown event (type 26)", "units" },
188 { 'C', "Lock", "Unknown event (type 27)", "units" },
189 { 'C', "Lock", "Unknown event (type 28)", "units" },
190 { 'C', "Lock", "Unknown event (type 29)", "units" },
191 { 'C', "Lock", "Unknown event (type 30)", "units" },
192 { 'C', "Lock", "Unknown event (type 31)", "units" },
193 { 'H', "Lock", "Adaptive mutex hold", "nsec",
194 "lockstat:::adaptive-release", NULL,
195 "lockstat:::adaptive-acquire" },
196 { 'H', "Lock", "Spin lock hold", "nsec",
197 "lockstat:::spin-release", NULL,
198 "lockstat:::spin-acquire" },
199 { 'H', "Lock", "R/W writer hold", "nsec",
200 "lockstat::rw_wunlock:rw-release", NULL,
201 "lockstat::rw_wlock:rw-acquire" },
202 { 'H', "Lock", "R/W reader hold", "nsec",
203 "lockstat::rw_runlock:rw-release", NULL,
204 "lockstat::rw_rlock:rw-acquire" },
205 { 'H', "Lock", "SX shared hold", "nsec",
206 "lockstat::sx_sunlock:sx-release", NULL,
207 "lockstat::sx_slock:sx-acquire" },
208 { 'H', "Lock", "SX exclusive hold", "nsec",
209 "lockstat::sx_xunlock:sx-release", NULL,
210 "lockstat::sx_xlock:sx-acquire" },
211 { 'H', "Lock", "Unknown event (type 38)", "units" },
212 { 'H', "Lock", "Unknown event (type 39)", "units" },
213 { 'H', "Lock", "Unknown event (type 40)", "units" },
214 { 'H', "Lock", "Unknown event (type 41)", "units" },
215 { 'H', "Lock", "Unknown event (type 42)", "units" },
216 { 'H', "Lock", "Unknown event (type 43)", "units" },
217 { 'H', "Lock", "Unknown event (type 44)", "units" },
218 { 'H', "Lock", "Unknown event (type 45)", "units" },
219 { 'H', "Lock", "Unknown event (type 46)", "units" },
220 { 'H', "Lock", "Unknown event (type 47)", "units" },
221 { 'H', "Lock", "Unknown event (type 48)", "units" },
222 { 'H', "Lock", "Unknown event (type 49)", "units" },
223 { 'H', "Lock", "Unknown event (type 50)", "units" },
224 { 'H', "Lock", "Unknown event (type 51)", "units" },
225 { 'H', "Lock", "Unknown event (type 52)", "units" },
226 { 'H', "Lock", "Unknown event (type 53)", "units" },
227 { 'H', "Lock", "Unknown event (type 54)", "units" },
228 { 'H', "Lock", "Unknown event (type 55)", "units" },
230 { 'I', "CPU+PIL", "Profiling interrupt", "nsec",
233 { 'I', "CPU+Pri_Class", "Profiling interrupt", "nsec",
235 "profile:::profile-97", NULL },
236 { 'I', "Lock", "Unknown event (type 57)", "units" },
237 { 'I', "Lock", "Unknown event (type 58)", "units" },
238 { 'I', "Lock", "Unknown event (type 59)", "units" },
239 { 'E', "Lock", "Recursive lock entry detected", "(N/A)",
240 "lockstat:::rw-release", NULL, "lockstat:::rw-acquire" },
241 { 'E', "Lock", "Lockstat enter failure", "(N/A)" },
242 { 'E', "Lock", "Lockstat exit failure", "nsec" },
243 { 'E', "Lock", "Lockstat record failure", "(N/A)" },
247 static char *g_pri_class[] = {
257 fail(int do_perror, const char *message, ...)
260 int save_errno = errno;
262 va_start(args, message);
263 (void) fprintf(stderr, "lockstat: ");
264 (void) vfprintf(stderr, message, args);
267 (void) fprintf(stderr, ": %s", strerror(save_errno));
268 (void) fprintf(stderr, "\n");
273 dfail(const char *message, ...)
277 va_start(args, message);
278 (void) fprintf(stderr, "lockstat: ");
279 (void) vfprintf(stderr, message, args);
281 (void) fprintf(stderr, ": %s\n",
282 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
288 show_events(char event_type, char *desc)
290 int i, first = -1, last;
292 for (i = 0; i < LS_MAX_EVENTS; i++) {
293 ls_event_info_t *evp = &g_event_info[i];
294 if (evp->ev_type != event_type ||
295 strncmp(evp->ev_desc, "Unknown event", 13) == 0)
302 (void) fprintf(stderr,
303 "\n%s events (lockstat -%c or lockstat -e %d-%d):\n\n",
304 desc, event_type, first, last);
306 for (i = first; i <= last; i++)
307 (void) fprintf(stderr,
308 "%4d = %s\n", i, g_event_info[i].ev_desc);
314 (void) fprintf(stderr,
315 "Usage: lockstat [options] command [args]\n"
316 "\nEvent selection options:\n\n"
317 " -C watch contention events [on by default]\n"
318 " -E watch error events [off by default]\n"
319 " -H watch hold events [off by default]\n"
320 " -I watch interrupt events [off by default]\n"
321 " -A watch all lock events [equivalent to -CH]\n"
322 " -e event_list only watch the specified events (shown below);\n"
323 " <event_list> is a comma-separated list of\n"
324 " events or ranges of events, e.g. 1,4-7,35\n"
325 " -i rate interrupt rate for -I [default: %d Hz]\n"
326 "\nData gathering options:\n\n"
327 " -b basic statistics (lock, caller, event count)\n"
328 " -t timing for all events [default]\n"
329 " -h histograms for event times\n"
330 " -s depth stack traces <depth> deep\n"
331 " -x opt[=val] enable or modify DTrace options\n"
332 "\nData filtering options:\n\n"
333 " -n nrecords maximum number of data records [default: %d]\n"
334 " -l lock[,size] only watch <lock>, which can be specified as a\n"
335 " symbolic name or hex address; <size> defaults\n"
336 " to the ELF symbol size if available, 1 if not\n"
337 " -f func[,size] only watch events generated by <func>\n"
338 " -d duration only watch events longer than <duration>\n"
339 " -T trace (rather than sample) events\n"
340 "\nData reporting options:\n\n"
341 " -c coalesce lock data for arrays like pse_mutex[]\n"
342 " -k coalesce PCs within functions\n"
343 " -g show total events generated by function\n"
344 " -w wherever: don't distinguish events by caller\n"
345 " -W whichever: don't distinguish events by lock\n"
346 " -R display rates rather than counts\n"
347 " -p parsable output format (awk(1)-friendly)\n"
348 " -P sort lock data by (count * avg_time) product\n"
349 " -D n only display top <n> events of each type\n"
350 " -o filename send output to <filename>\n",
351 DEFAULT_HZ, DEFAULT_NRECS);
353 show_events('C', "Contention");
354 show_events('H', "Hold-time");
355 show_events('I', "Interrupt");
356 show_events('E', "Error");
357 (void) fprintf(stderr, "\n");
363 lockcmp(lsrec_t *a, lsrec_t *b)
367 if (a->ls_event < b->ls_event)
369 if (a->ls_event > b->ls_event)
372 for (i = g_stkdepth - 1; i >= 0; i--) {
373 if (a->ls_stack[i] < b->ls_stack[i])
375 if (a->ls_stack[i] > b->ls_stack[i])
379 if (a->ls_caller < b->ls_caller)
381 if (a->ls_caller > b->ls_caller)
384 if (a->ls_lock < b->ls_lock)
386 if (a->ls_lock > b->ls_lock)
393 countcmp(lsrec_t *a, lsrec_t *b)
395 if (a->ls_event < b->ls_event)
397 if (a->ls_event > b->ls_event)
400 return (b->ls_count - a->ls_count);
404 timecmp(lsrec_t *a, lsrec_t *b)
406 if (a->ls_event < b->ls_event)
408 if (a->ls_event > b->ls_event)
411 if (a->ls_time < b->ls_time)
413 if (a->ls_time > b->ls_time)
420 lockcmp_anywhere(lsrec_t *a, lsrec_t *b)
422 if (a->ls_event < b->ls_event)
424 if (a->ls_event > b->ls_event)
427 if (a->ls_lock < b->ls_lock)
429 if (a->ls_lock > b->ls_lock)
436 lock_and_count_cmp_anywhere(lsrec_t *a, lsrec_t *b)
438 if (a->ls_event < b->ls_event)
440 if (a->ls_event > b->ls_event)
443 if (a->ls_lock < b->ls_lock)
445 if (a->ls_lock > b->ls_lock)
448 return (b->ls_count - a->ls_count);
452 sitecmp_anylock(lsrec_t *a, lsrec_t *b)
456 if (a->ls_event < b->ls_event)
458 if (a->ls_event > b->ls_event)
461 for (i = g_stkdepth - 1; i >= 0; i--) {
462 if (a->ls_stack[i] < b->ls_stack[i])
464 if (a->ls_stack[i] > b->ls_stack[i])
468 if (a->ls_caller < b->ls_caller)
470 if (a->ls_caller > b->ls_caller)
477 site_and_count_cmp_anylock(lsrec_t *a, lsrec_t *b)
481 if (a->ls_event < b->ls_event)
483 if (a->ls_event > b->ls_event)
486 for (i = g_stkdepth - 1; i >= 0; i--) {
487 if (a->ls_stack[i] < b->ls_stack[i])
489 if (a->ls_stack[i] > b->ls_stack[i])
493 if (a->ls_caller < b->ls_caller)
495 if (a->ls_caller > b->ls_caller)
498 return (b->ls_count - a->ls_count);
502 lsmergesort(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **a, lsrec_t **b, int n)
508 lsmergesort(cmp, a, b, m);
510 lsmergesort(cmp, a + m, b + m, n - m);
511 for (i = m; i > 0; i--)
513 for (j = m - 1; j < n - 1; j++)
514 b[n + m - j - 2] = a[j + 1];
516 *a++ = cmp(b[i], b[j]) < 0 ? b[i++] : b[j--];
521 coalesce(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **lock, int n)
524 lsrec_t *target, *current;
528 for (i = 1; i < n; i++) {
530 if (cmp(current, target) != 0) {
534 current->ls_event = LS_MAX_EVENTS;
535 target->ls_count += current->ls_count;
536 target->ls_refcnt += current->ls_refcnt;
537 if (g_recsize < LS_TIME)
539 target->ls_time += current->ls_time;
540 if (g_recsize < LS_HIST)
542 for (j = 0; j < 64; j++)
543 target->ls_hist[j] += current->ls_hist[j];
548 coalesce_symbol(uintptr_t *addrp)
553 if (addr_to_sym(*addrp, &symoff, &symsize) != NULL && symoff < symsize)
558 predicate_add(char **pred, char *what, char *cmp, uintptr_t value)
572 newlen = len + strlen(what) + 32 + strlen("( && )");
573 new = malloc(newlen);
575 if (*pred[0] != '\0') {
577 (void) sprintf(new, "(%s) && (%s %s 0x%p)",
578 *pred, what, cmp, (void *)value);
580 (void) sprintf(new, "(%s) && (%s)", *pred, what);
584 (void) sprintf(new, "%s %s 0x%p",
585 what, cmp, (void *)value);
587 (void) sprintf(new, "%s", what);
596 predicate_destroy(char **pred)
603 filter_add(char **filt, char *what, uintptr_t base, uintptr_t size)
605 char buf[256], *c = buf, *new;
614 (void) sprintf(c, "%s(%s >= 0x%p && %s < 0x%p)", *filt[0] != '\0' ?
615 " || " : "", what, (void *)base, what, (void *)(base + size));
617 (void) sprintf(c, "%s(%s >= %p && %s < %p)", *filt[0] != '\0' ?
618 " || " : "", what, (void *)base, what, (void *)(base + size));
621 newlen = (len = strlen(*filt) + 1) + strlen(c);
622 new = malloc(newlen);
623 bcopy(*filt, new, len);
624 (void) strcat(new, c);
630 filter_destroy(char **filt)
637 dprog_add(const char *fmt, ...)
644 size = vsnprintf(&c, 1, fmt, args) + 1;
647 if (g_proglen == 0) {
650 offs = g_proglen - 1;
653 g_proglen = offs + size;
655 if ((g_prog = realloc(g_prog, g_proglen)) == NULL)
656 fail(1, "failed to reallocate program text");
659 (void) vsnprintf(&g_prog[offs], size, fmt, args);
664 * This function may read like an open sewer, but keep in mind that programs
665 * that generate other programs are rarely pretty. If one has the unenviable
666 * task of maintaining or -- worse -- extending this code, use the -V option
667 * to examine the D program as generated by this function.
670 dprog_addevent(int event)
672 ls_event_info_t *info = &g_event_info[event];
675 const char *arg0, *caller;
681 if (info->ev_name[0] == '\0')
684 if (info->ev_type == 'I') {
686 * For interrupt events, arg0 (normally the lock pointer) is
687 * the CPU address plus the current pil, and arg1 (normally
688 * the number of nanoseconds) is the number of nanoseconds
689 * late -- and it's stored in arg2.
692 arg0 = "(uintptr_t)curthread->t_cpu + \n"
693 "\t curthread->t_cpu->cpu_profile_pil";
695 arg0 = "(uintptr_t)(curthread->td_oncpu << 16) + \n"
696 "\t 0x01000000 + curthread->td_pri_class";
698 caller = "(uintptr_t)arg0";
701 arg0 = "(uintptr_t)arg0";
705 if (g_recsize > LS_HIST) {
706 for (depth = 0; g_recsize > LS_STACK(depth); depth++)
710 (void) sprintf(stack, "\tstack(%d);\n", depth);
712 (void) sprintf(stack, ", stack(%d)", depth);
715 (void) sprintf(stack, "");
718 if (info->ev_acquire != NULL) {
720 * If this is a hold event, we need to generate an additional
721 * clause for the acquire; the clause for the release will be
722 * generated with the aggregating statement, below.
724 dprog_add("%s\n", info->ev_acquire);
725 predicate_add(&pred, info->ev_predicate, NULL, 0);
726 predicate_add(&pred, g_predicate, NULL, 0);
728 dprog_add("/%s/\n", pred);
731 (void) sprintf(buf, "self->ev%d[(uintptr_t)arg0]", event);
733 if (info->ev_type == 'H') {
734 dprog_add("\t%s = timestamp;\n", buf);
737 * If this isn't a hold event, it's the recursive
738 * error event. For this, we simply bump the
739 * thread-local, per-lock count.
741 dprog_add("\t%s++;\n", buf);
745 predicate_destroy(&pred);
748 if (info->ev_type == 'E') {
750 * If this is the recursive lock error event, we need
751 * to generate an additional clause to decrement the
752 * thread-local, per-lock count. This assures that we
753 * only execute the aggregating clause if we have
756 dprog_add("%s\n", info->ev_name);
757 dprog_add("/%s/\n{\n\t%s--;\n}\n\n", buf, buf);
760 predicate_add(&pred, buf, NULL, 0);
762 if (info->ev_type == 'H') {
763 (void) sprintf(buf, "timestamp -\n\t "
764 "self->ev%d[(uintptr_t)arg0]", event);
769 predicate_add(&pred, info->ev_predicate, NULL, 0);
770 if (info->ev_type != 'I')
771 predicate_add(&pred, g_predicate, NULL, 0);
773 predicate_add(&pred, g_ipredicate, NULL, 0);
776 if ((dur = g_min_duration[event]) != 0)
777 predicate_add(&pred, arg1, ">=", dur);
779 dprog_add("%s\n", info->ev_name);
782 dprog_add("/%s/\n", pred);
783 predicate_destroy(&pred);
788 dprog_add("\ttrace(%dULL);\n", event);
789 dprog_add("\ttrace(%s);\n", arg0);
790 dprog_add("\ttrace(%s);\n", caller);
794 * The ordering here is important: when we process the
795 * aggregate, we count on the fact that @avg appears before
796 * @hist in program order to assure that @avg is assigned the
797 * first aggregation variable ID and @hist assigned the
798 * second; see the comment in process_aggregate() for details.
800 dprog_add("\t@avg[%dULL, %s, %s%s] = avg(%s);\n",
801 event, arg0, caller, stack, arg1);
803 if (g_recsize >= LS_HIST) {
804 dprog_add("\t@hist[%dULL, %s, %s%s] = quantize"
805 "(%s);\n", event, arg0, caller, stack, arg1);
809 if (info->ev_acquire != NULL)
810 dprog_add("\tself->ev%d[arg0] = 0;\n", event);
819 dtrace_proginfo_t info;
822 (void) fprintf(stderr, "lockstat: vvvv D program vvvv\n");
823 (void) fputs(g_prog, stderr);
824 (void) fprintf(stderr, "lockstat: ^^^^ D program ^^^^\n");
827 if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
828 DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
829 dfail("failed to compile program");
831 if (dtrace_program_exec(g_dtp, prog, &info) == -1)
832 dfail("failed to enable probes");
834 if (dtrace_go(g_dtp) != 0)
835 dfail("couldn't start tracing");
849 dtrace_optval_t val, status, agg;
850 struct sigaction act;
851 struct itimerspec ts;
855 if (dtrace_getopt(g_dtp, "statusrate", &status) == -1)
856 dfail("failed to get 'statusrate'");
858 if (dtrace_getopt(g_dtp, "aggrate", &agg) == -1)
859 dfail("failed to get 'statusrate'");
862 * We would want to awaken at a rate that is the GCD of the statusrate
863 * and the aggrate -- but that seems a bit absurd. Instead, we'll
864 * simply awaken at a rate that is the more frequent of the two, which
865 * assures that we're never later than the interval implied by the
866 * more frequent rate.
868 val = status < agg ? status : agg;
870 (void) sigemptyset(&act.sa_mask);
872 act.sa_handler = status_fire;
873 (void) sigaction(SIGUSR1, &act, NULL);
875 ev.sigev_notify = SIGEV_SIGNAL;
876 ev.sigev_signo = SIGUSR1;
878 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
879 dfail("cannot create CLOCK_REALTIME timer");
881 ts.it_value.tv_sec = val / NANOSEC;
882 ts.it_value.tv_nsec = val % NANOSEC;
883 ts.it_interval = ts.it_value;
885 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
886 dfail("cannot set time on CLOCK_REALTIME timer");
892 if (!g_tracing && dtrace_aggregate_snap(g_dtp) != 0)
893 dfail("failed to snap aggregate");
895 if (dtrace_status(g_dtp) == -1)
896 dfail("dtrace_status()");
900 lsrec_fill(lsrec_t *lsrec, const dtrace_recdesc_t *rec, int nrecs, caddr_t data)
902 bzero(lsrec, g_recsize);
905 if ((g_recsize > LS_HIST && nrecs < 4) || (nrecs < 3))
906 fail(0, "truncated DTrace record");
908 if (rec->dtrd_size != sizeof (uint64_t))
909 fail(0, "bad event size in first record");
911 /* LINTED - alignment */
912 lsrec->ls_event = (uint32_t)*((uint64_t *)(data + rec->dtrd_offset));
915 if (rec->dtrd_size != sizeof (uintptr_t))
916 fail(0, "bad lock address size in second record");
918 /* LINTED - alignment */
919 lsrec->ls_lock = *((uintptr_t *)(data + rec->dtrd_offset));
922 if (rec->dtrd_size != sizeof (uintptr_t))
923 fail(0, "bad caller size in third record");
925 /* LINTED - alignment */
926 lsrec->ls_caller = *((uintptr_t *)(data + rec->dtrd_offset));
929 if (g_recsize > LS_HIST) {
933 frames = rec->dtrd_size / sizeof (pc_t);
934 /* LINTED - alignment */
935 stack = (pc_t *)(data + rec->dtrd_offset);
937 for (i = 1; i < frames; i++)
938 lsrec->ls_stack[i - 1] = stack[i];
944 count_aggregate(const dtrace_aggdata_t *agg, void *arg)
946 *((size_t *)arg) += 1;
948 return (DTRACE_AGGWALK_NEXT);
952 process_aggregate(const dtrace_aggdata_t *agg, void *arg)
954 const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
955 caddr_t data = agg->dtada_data;
956 lsdata_t *lsdata = arg;
957 lsrec_t *lsrec = lsdata->lsd_next;
958 const dtrace_recdesc_t *rec;
959 uint64_t *avg, *quantized;
962 assert(lsdata->lsd_count < g_nrecs);
965 * Aggregation variable IDs are guaranteed to be generated in program
966 * order, and they are guaranteed to start from DTRACE_AGGVARIDNONE
967 * plus one. As "avg" appears before "hist" in program order, we know
968 * that "avg" will be allocated the first aggregation variable ID, and
969 * "hist" will be allocated the second aggregation variable ID -- and
970 * we therefore use the aggregation variable ID to differentiate the
973 if (aggdesc->dtagd_varid > DTRACE_AGGVARIDNONE + 1) {
975 * If this is the histogram entry. We'll copy the quantized
976 * data into lc_hist, and jump over the rest.
978 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
980 if (aggdesc->dtagd_varid != DTRACE_AGGVARIDNONE + 2)
981 fail(0, "bad variable ID in aggregation record");
983 if (rec->dtrd_size !=
984 DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
985 fail(0, "bad quantize size in aggregation record");
987 /* LINTED - alignment */
988 quantized = (uint64_t *)(data + rec->dtrd_offset);
990 for (i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
991 i < DTRACE_QUANTIZE_NBUCKETS; i++, j++)
992 lsrec->ls_hist[j] = quantized[i];
997 lsrec_fill(lsrec, &aggdesc->dtagd_rec[1],
998 aggdesc->dtagd_nrecs - 1, data);
1000 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1002 if (rec->dtrd_size != 2 * sizeof (uint64_t))
1003 fail(0, "bad avg size in aggregation record");
1005 /* LINTED - alignment */
1006 avg = (uint64_t *)(data + rec->dtrd_offset);
1007 lsrec->ls_count = (uint32_t)avg[0];
1008 lsrec->ls_time = (uintptr_t)avg[1];
1010 if (g_recsize >= LS_HIST)
1011 return (DTRACE_AGGWALK_NEXT);
1014 lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
1015 lsdata->lsd_count++;
1017 return (DTRACE_AGGWALK_NEXT);
1021 process_trace(const dtrace_probedata_t *pdata, void *arg)
1023 lsdata_t *lsdata = arg;
1024 lsrec_t *lsrec = lsdata->lsd_next;
1025 dtrace_eprobedesc_t *edesc = pdata->dtpda_edesc;
1026 caddr_t data = pdata->dtpda_data;
1028 if (lsdata->lsd_count >= g_nrecs)
1029 return (DTRACE_CONSUME_NEXT);
1031 lsrec_fill(lsrec, edesc->dtepd_rec, edesc->dtepd_nrecs, data);
1033 lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
1034 lsdata->lsd_count++;
1036 return (DTRACE_CONSUME_NEXT);
1040 process_data(FILE *out, char *data)
1044 /* LINTED - alignment */
1045 lsdata.lsd_next = (lsrec_t *)data;
1046 lsdata.lsd_count = 0;
1049 if (dtrace_consume(g_dtp, out,
1050 process_trace, NULL, &lsdata) != 0)
1051 dfail("failed to consume buffer");
1053 return (lsdata.lsd_count);
1056 if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
1057 process_aggregate, &lsdata) != 0)
1058 dfail("failed to walk aggregate");
1060 return (lsdata.lsd_count);
1065 drophandler(const dtrace_dropdata_t *data, void *arg)
1068 (void) fprintf(stderr, "lockstat: warning: %s", data->dtdda_msg);
1069 return (DTRACE_HANDLE_OK);
1073 main(int argc, char **argv)
1076 lsrec_t *lsp, **current, **first, **sort_buf, **merge_buf;
1083 char *addrp, *offp, *sizep, *evp, *lastp, *p;
1086 int events_specified = 0;
1089 char *filt = NULL, *ifilt = NULL;
1090 static uint64_t ev_count[LS_MAX_EVENTS + 1];
1091 static uint64_t ev_time[LS_MAX_EVENTS + 1];
1092 dtrace_optval_t aggsize;
1098 if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
1099 fail(0, "cannot open dtrace library: %s",
1100 dtrace_errmsg(NULL, err));
1103 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1104 dfail("couldn't establish drop handler");
1106 if (symtab_init() == -1)
1107 fail(1, "can't load kernel symbols");
1109 g_nrecs = DEFAULT_NRECS;
1111 while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != GETOPT_EOF) {
1114 g_recsize = LS_BASIC;
1118 g_recsize = LS_TIME;
1122 g_recsize = LS_HIST;
1126 if (!isdigit(optarg[0]))
1128 g_stkdepth = atoi(optarg);
1129 if (g_stkdepth > LS_MAX_STACK_DEPTH)
1130 fail(0, "max stack depth is %d",
1131 LS_MAX_STACK_DEPTH);
1132 g_recsize = LS_STACK(g_stkdepth);
1136 if (!isdigit(optarg[0]))
1138 g_nrecs = atoi(optarg);
1142 if (!isdigit(optarg[0]))
1144 duration = atoll(optarg);
1147 * XXX -- durations really should be per event
1148 * since the units are different, but it's hard
1149 * to express this nicely in the interface.
1150 * Not clear yet what the cleanest solution is.
1152 for (i = 0; i < LS_MAX_EVENTS; i++)
1153 if (g_event_info[i].ev_type != 'E')
1154 g_min_duration[i] = duration;
1159 if (!isdigit(optarg[0]))
1165 fail(0, "max interrupt rate is %d Hz", MAX_HZ);
1167 for (j = 0; j < LS_MAX_EVENTS; j++)
1168 if (strcmp(g_event_info[j].ev_desc,
1169 "Profiling interrupt") == 0)
1172 (void) sprintf(g_event_info[j].ev_name,
1173 "profile:::profile-%d", i);
1178 addrp = strtok(optarg, ",");
1179 sizep = strtok(NULL, ",");
1180 addrp = strtok(optarg, ",+");
1181 offp = strtok(NULL, ",");
1183 size = sizep ? strtoul(sizep, NULL, 0) : 1;
1184 off = offp ? strtoul(offp, NULL, 0) : 0;
1186 if (addrp[0] == '0') {
1187 addr = strtoul(addrp, NULL, 16) + off;
1189 addr = sym_to_addr(addrp) + off;
1191 size = sym_size(addrp) - off;
1192 if (addr - off == 0)
1193 fail(0, "symbol '%s' not found", addrp);
1200 filter_add(&filt, "arg0", addr, size);
1202 filter_add(&filt, "caller", addr, size);
1203 filter_add(&ifilt, "arg0", addr, size);
1208 evp = strtok_r(optarg, ",", &lastp);
1213 (void) strtok(evp, "-");
1214 evp2 = strtok(NULL, "-");
1216 ev2 = evp2 ? atoi(evp2) : ev1;
1217 if ((uint_t)ev1 >= LS_MAX_EVENTS ||
1218 (uint_t)ev2 >= LS_MAX_EVENTS || ev1 > ev2)
1219 fail(0, "-e events out of range");
1220 for (i = ev1; i <= ev2; i++)
1222 evp = strtok_r(NULL, ",", &lastp);
1224 events_specified = 1;
1251 for (i = 0; i < LS_MAX_EVENTS; i++)
1252 if (g_event_info[i].ev_type == c)
1254 events_specified = 1;
1258 for (i = 0; i < LS_MAX_EVENTS; i++)
1259 if (strchr("CH", g_event_info[i].ev_type))
1261 events_specified = 1;
1269 if (!isdigit(optarg[0]))
1271 g_topn = atoi(optarg);
1287 if ((out = fopen(optarg, "w")) == NULL)
1288 fail(1, "error opening file");
1296 if (strchr(LOCKSTAT_OPTSTR, c) == NULL)
1302 predicate_add(&g_predicate, filt, NULL, 0);
1303 filter_destroy(&filt);
1306 if (ifilt != NULL) {
1307 predicate_add(&g_ipredicate, ifilt, NULL, 0);
1308 filter_destroy(&ifilt);
1311 if (g_recsize == 0) {
1313 g_stkdepth = LS_MAX_STACK_DEPTH;
1314 g_recsize = LS_STACK(g_stkdepth);
1316 g_recsize = LS_TIME;
1320 if (g_gflag && g_recsize <= LS_STACK(0))
1321 fail(0, "'-g' requires at least '-s 1' data gathering");
1324 * Make sure the alignment is reasonable
1326 g_recsize = -(-g_recsize & -sizeof (uint64_t));
1328 for (i = 0; i < LS_MAX_EVENTS; i++) {
1330 * If no events were specified, enable -C.
1332 if (!events_specified && g_event_info[i].ev_type == 'C')
1336 for (i = 0; i < LS_MAX_EVENTS; i++) {
1340 if (g_event_info[i].ev_acquire != NULL) {
1342 * If we've enabled a hold event, we must explicitly
1343 * allocate dynamic variable space.
1352 * Make sure there are remaining arguments to specify a child command
1358 if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
1359 dfail("couldn't determine number of online CPUs");
1362 * By default, we set our data buffer size to be the number of records
1363 * multiplied by the size of the record, doubled to account for some
1364 * DTrace slop and divided by the number of CPUs. We silently clamp
1365 * the aggregation size at both a minimum and a maximum to prevent
1366 * absurdly low or high values.
1368 if ((aggsize = (g_nrecs * g_recsize * 2) / ncpus) < MIN_AGGSIZE)
1369 aggsize = MIN_AGGSIZE;
1371 if (aggsize > MAX_AGGSIZE)
1372 aggsize = MAX_AGGSIZE;
1374 (void) sprintf(aggstr, "%lld", (long long)aggsize);
1377 if (dtrace_setopt(g_dtp, "bufsize", "4k") == -1)
1378 dfail("failed to set 'bufsize'");
1380 if (dtrace_setopt(g_dtp, "aggsize", aggstr) == -1)
1381 dfail("failed to set 'aggsize'");
1385 * If we're using dynamic variables, we set our
1386 * dynamic variable size to be one megabyte per CPU,
1387 * with a hard-limit of 32 megabytes. This may still
1388 * be too small in some cases, but it can be tuned
1389 * manually via -x if need be.
1391 (void) sprintf(aggstr, "%ldm", ncpus < 32 ? ncpus : 32);
1393 if (dtrace_setopt(g_dtp, "dynvarsize", aggstr) == -1)
1394 dfail("failed to set 'dynvarsize'");
1397 if (dtrace_setopt(g_dtp, "bufsize", aggstr) == -1)
1398 dfail("failed to set 'bufsize'");
1401 if (dtrace_setopt(g_dtp, "statusrate", "10sec") == -1)
1402 dfail("failed to set 'statusrate'");
1405 while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != GETOPT_EOF) {
1408 if ((p = strchr(optarg, '=')) != NULL)
1411 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1412 dfail("failed to set -x %s", optarg);
1423 g_elapsed = -gethrtime();
1426 * Spawn the specified command and wait for it to complete.
1430 fail(1, "cannot fork");
1432 (void) dtrace_close(g_dtp);
1433 (void) execvp(argv[0], &argv[0]);
1439 while (waitpid(child, &status, WEXITED) != child)
1441 while (waitpid(child, &status, 0) != child)
1445 g_elapsed += gethrtime();
1447 if (WIFEXITED(status)) {
1448 if (WEXITSTATUS(status) != 0) {
1449 if (exec_errno != 0) {
1451 fail(1, "could not execute %s", argv[0]);
1453 (void) fprintf(stderr,
1454 "lockstat: warning: %s exited with code %d\n",
1455 argv[0], WEXITSTATUS(status));
1458 (void) fprintf(stderr,
1459 "lockstat: warning: %s died on signal %d\n",
1460 argv[0], WTERMSIG(status));
1463 if (dtrace_stop(g_dtp) == -1)
1464 dfail("failed to stop dtrace");
1467 * Before we read out the results, we need to allocate our buffer.
1468 * If we're tracing, then we'll just use the precalculated size. If
1469 * we're not, then we'll take a snapshot of the aggregate, and walk
1470 * it to count the number of records.
1473 if (dtrace_aggregate_snap(g_dtp) != 0)
1474 dfail("failed to snap aggregate");
1478 if (dtrace_aggregate_walk(g_dtp,
1479 count_aggregate, &g_nrecs) != 0)
1480 dfail("failed to walk aggregate");
1484 if ((data_buf = memalign(sizeof (uint64_t),
1485 (g_nrecs + 1) * g_recsize)) == NULL)
1487 if (posix_memalign((void **)&data_buf, sizeof (uint64_t),
1488 (g_nrecs + 1) * g_recsize) )
1490 fail(1, "Memory allocation failed");
1493 * Read out the DTrace data.
1495 g_nrecs_used = process_data(out, data_buf);
1497 if (g_nrecs_used > g_nrecs || g_dropped)
1498 (void) fprintf(stderr, "lockstat: warning: "
1499 "ran out of data records (use -n for more)\n");
1501 /* LINTED - alignment */
1502 for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1503 /* LINTED - alignment */
1504 lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
1505 ev_count[lsp->ls_event] += lsp->ls_count;
1506 ev_time[lsp->ls_event] += lsp->ls_time;
1510 * If -g was specified, convert stacks into individual records.
1513 lsrec_t *newlsp, *oldlsp;
1516 newlsp = memalign(sizeof (uint64_t),
1517 g_nrecs_used * LS_TIME * (g_stkdepth + 1));
1519 posix_memalign((void **)&newlsp, sizeof (uint64_t),
1520 g_nrecs_used * LS_TIME * (g_stkdepth + 1));
1523 fail(1, "Cannot allocate space for -g processing");
1525 /* LINTED - alignment */
1526 for (i = 0, oldlsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1527 /* LINTED - alignment */
1528 oldlsp = (lsrec_t *)((char *)oldlsp + g_recsize)) {
1530 int caller_in_stack = 0;
1532 if (oldlsp->ls_count == 0)
1535 for (fr = 0; fr < g_stkdepth; fr++) {
1536 if (oldlsp->ls_stack[fr] == 0)
1538 if (oldlsp->ls_stack[fr] == oldlsp->ls_caller)
1539 caller_in_stack = 1;
1540 bcopy(oldlsp, lsp, LS_TIME);
1541 lsp->ls_caller = oldlsp->ls_stack[fr];
1542 /* LINTED - alignment */
1543 lsp = (lsrec_t *)((char *)lsp + LS_TIME);
1545 if (!caller_in_stack) {
1546 bcopy(oldlsp, lsp, LS_TIME);
1547 /* LINTED - alignment */
1548 lsp = (lsrec_t *)((char *)lsp + LS_TIME);
1551 g_nrecs = g_nrecs_used =
1552 ((uintptr_t)lsp - (uintptr_t)newlsp) / LS_TIME;
1553 g_recsize = LS_TIME;
1556 data_buf = (char *)newlsp;
1559 if ((sort_buf = calloc(2 * (g_nrecs + 1),
1560 sizeof (void *))) == NULL)
1561 fail(1, "Sort buffer allocation failed");
1562 merge_buf = sort_buf + (g_nrecs + 1);
1565 * Build the sort buffer, discarding zero-count records along the way.
1567 /* LINTED - alignment */
1568 for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1569 /* LINTED - alignment */
1570 lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
1571 if (lsp->ls_count == 0)
1572 lsp->ls_event = LS_MAX_EVENTS;
1576 if (g_nrecs_used == 0)
1580 * Add a sentinel after the last record
1583 lsp->ls_event = LS_MAX_EVENTS;
1586 report_trace(out, sort_buf);
1591 * Application of -g may have resulted in multiple records
1592 * with the same signature; coalesce them.
1595 mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
1596 coalesce(lockcmp, sort_buf, g_nrecs_used);
1600 * Coalesce locks within the same symbol if -c option specified.
1601 * Coalesce PCs within the same function if -k option specified.
1603 if (g_cflag || g_kflag) {
1604 for (i = 0; i < g_nrecs_used; i++) {
1608 coalesce_symbol(&lsp->ls_lock);
1610 for (fr = 0; fr < g_stkdepth; fr++)
1611 coalesce_symbol(&lsp->ls_stack[fr]);
1612 coalesce_symbol(&lsp->ls_caller);
1615 mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
1616 coalesce(lockcmp, sort_buf, g_nrecs_used);
1620 * Coalesce callers if -w option specified
1623 mergesort(lock_and_count_cmp_anywhere,
1624 sort_buf, merge_buf, g_nrecs_used);
1625 coalesce(lockcmp_anywhere, sort_buf, g_nrecs_used);
1629 * Coalesce locks if -W option specified
1632 mergesort(site_and_count_cmp_anylock,
1633 sort_buf, merge_buf, g_nrecs_used);
1634 coalesce(sitecmp_anylock, sort_buf, g_nrecs_used);
1638 * Sort data by contention count (ls_count) or total time (ls_time),
1639 * depending on g_Pflag. Override g_Pflag if time wasn't measured.
1641 if (g_recsize < LS_TIME)
1645 mergesort(timecmp, sort_buf, merge_buf, g_nrecs_used);
1647 mergesort(countcmp, sort_buf, merge_buf, g_nrecs_used);
1650 * Display data by event type
1652 first = &sort_buf[0];
1653 while ((event = (*first)->ls_event) < LS_MAX_EVENTS) {
1655 while ((lsp = *current)->ls_event == event)
1657 report_stats(out, first, current - first, ev_count[event],
1666 format_symbol(char *buf, uintptr_t addr, int show_size)
1672 symname = addr_to_sym(addr, &symoff, &symsize);
1674 if (show_size && symoff == 0)
1675 (void) sprintf(buf, "%s[%ld]", symname, (long)symsize);
1676 else if (symoff == 0)
1677 (void) sprintf(buf, "%s", symname);
1678 else if (symoff < 16 && bcmp(symname, "cpu[", 4) == 0) /* CPU+PIL */
1680 (void) sprintf(buf, "%s+%ld", symname, (long)symoff);
1682 (void) sprintf(buf, "%s+%s", symname, g_pri_class[(int)symoff]);
1684 else if (symoff <= symsize || (symoff < 256 && addr != symoff))
1685 (void) sprintf(buf, "%s+0x%llx", symname,
1686 (unsigned long long)symoff);
1688 (void) sprintf(buf, "0x%llx", (unsigned long long)addr);
1693 report_stats(FILE *out, lsrec_t **sort_buf, size_t nrecs, uint64_t total_count,
1694 uint64_t total_time)
1696 uint32_t event = sort_buf[0]->ls_event;
1698 double ptotal = 0.0;
1702 int first_bin, last_bin, max_bin_count, total_bin_count;
1705 char lhdr[80], chdr[80];
1707 rectype = g_recsize;
1710 (void) fprintf(out, "%20llu %s\n",
1711 g_rates == 0 ? total_count :
1712 ((unsigned long long)total_count * NANOSEC) / g_elapsed,
1713 g_event_info[event].ev_desc);
1717 (void) sprintf(lhdr, "%s%s",
1718 g_Wflag ? "Hottest " : "", g_event_info[event].ev_lhdr);
1719 (void) sprintf(chdr, "%s%s",
1720 g_wflag ? "Hottest " : "", "Caller");
1724 "\n%s: %.0f events in %.3f seconds (%.0f events/sec)\n\n",
1725 g_event_info[event].ev_desc, (double)total_count,
1726 (double)g_elapsed / NANOSEC,
1727 (double)total_count * NANOSEC / g_elapsed);
1729 if (!g_pflag && rectype < LS_HIST) {
1730 (void) sprintf(buf, "%s", g_event_info[event].ev_units);
1731 (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
1732 g_rates ? "ops/s" : "Count",
1733 g_gflag ? "genr" : "indv",
1734 "cuml", "rcnt", rectype >= LS_TIME ? buf : "", lhdr, chdr);
1735 (void) fprintf(out, "---------------------------------"
1736 "----------------------------------------------\n");
1740 for (i = 0; i < nrecs; i++) {
1743 if (displayed++ >= g_topn)
1749 (void) fprintf(out, "%u %u",
1750 lsp->ls_event, lsp->ls_count);
1751 (void) fprintf(out, " %s",
1752 format_symbol(buf, lsp->ls_lock, g_cflag));
1753 (void) fprintf(out, " %s",
1754 format_symbol(buf, lsp->ls_caller, 0));
1755 (void) fprintf(out, " %f",
1756 (double)lsp->ls_refcnt / lsp->ls_count);
1757 if (rectype >= LS_TIME)
1758 (void) fprintf(out, " %llu",
1759 (unsigned long long)lsp->ls_time);
1760 if (rectype >= LS_HIST) {
1761 for (j = 0; j < 64; j++)
1762 (void) fprintf(out, " %u",
1765 for (j = 0; j < LS_MAX_STACK_DEPTH; j++) {
1766 if (rectype <= LS_STACK(j) ||
1767 lsp->ls_stack[j] == 0)
1769 (void) fprintf(out, " %s",
1770 format_symbol(buf, lsp->ls_stack[j], 0));
1772 (void) fprintf(out, "\n");
1776 if (rectype >= LS_HIST) {
1777 (void) fprintf(out, "---------------------------------"
1778 "----------------------------------------------\n");
1779 (void) sprintf(buf, "%s",
1780 g_event_info[event].ev_units);
1781 (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
1782 g_rates ? "ops/s" : "Count",
1783 g_gflag ? "genr" : "indv",
1784 "cuml", "rcnt", buf, lhdr, chdr);
1787 if (g_Pflag && total_time != 0)
1788 percent = (lsp->ls_time * 100.00) / total_time;
1790 percent = (lsp->ls_count * 100.00) / total_count;
1794 if (rectype >= LS_TIME)
1795 (void) sprintf(buf, "%llu",
1796 (unsigned long long)(lsp->ls_time / lsp->ls_count));
1800 (void) fprintf(out, "%5llu ",
1801 g_rates == 0 ? lsp->ls_count :
1802 ((uint64_t)lsp->ls_count * NANOSEC) / g_elapsed);
1804 (void) fprintf(out, "%3.0f%% ", percent);
1807 (void) fprintf(out, "---- ");
1809 (void) fprintf(out, "%3.0f%% ", ptotal);
1811 (void) fprintf(out, "%4.2f %8s ",
1812 (double)lsp->ls_refcnt / lsp->ls_count, buf);
1814 (void) fprintf(out, "%-22s ",
1815 format_symbol(buf, lsp->ls_lock, g_cflag));
1817 (void) fprintf(out, "%-24s\n",
1818 format_symbol(buf, lsp->ls_caller, 0));
1820 if (rectype < LS_HIST)
1823 (void) fprintf(out, "\n");
1824 (void) fprintf(out, "%10s %31s %-9s %-24s\n",
1825 g_event_info[event].ev_units,
1826 "------ Time Distribution ------",
1827 g_rates ? "ops/s" : "count",
1828 rectype > LS_STACK(0) ? "Stack" : "");
1831 while (lsp->ls_hist[first_bin] == 0)
1835 while (lsp->ls_hist[last_bin] == 0)
1839 total_bin_count = 0;
1840 for (j = first_bin; j <= last_bin; j++) {
1841 total_bin_count += lsp->ls_hist[j];
1842 if (lsp->ls_hist[j] > max_bin_count)
1843 max_bin_count = lsp->ls_hist[j];
1847 * If we went a few frames below the caller, ignore them
1849 for (fr = 3; fr > 0; fr--)
1850 if (lsp->ls_stack[fr] == lsp->ls_caller)
1853 for (j = first_bin; j <= last_bin; j++) {
1854 uint_t depth = (lsp->ls_hist[j] * 30) / total_bin_count;
1855 (void) fprintf(out, "%10llu |%s%s %-9u ",
1857 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + 30 - depth,
1859 g_rates == 0 ? lsp->ls_hist[j] :
1860 (uint_t)(((uint64_t)lsp->ls_hist[j] * NANOSEC) /
1862 if (rectype <= LS_STACK(fr) || lsp->ls_stack[fr] == 0) {
1863 (void) fprintf(out, "\n");
1866 (void) fprintf(out, "%-24s\n",
1867 format_symbol(buf, lsp->ls_stack[fr], 0));
1870 while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
1871 (void) fprintf(out, "%15s %-36s %-24s\n", "", "",
1872 format_symbol(buf, lsp->ls_stack[fr], 0));
1878 (void) fprintf(out, "---------------------------------"
1879 "----------------------------------------------\n");
1885 report_trace(FILE *out, lsrec_t **sort_buf)
1890 char buf[256], buf2[256];
1892 rectype = g_recsize;
1895 (void) fprintf(out, "%5s %7s %11s %-24s %-24s\n",
1896 "Event", "Time", "Owner", "Lock", "Caller");
1897 (void) fprintf(out, "---------------------------------"
1898 "----------------------------------------------\n");
1901 for (i = 0; i < g_nrecs_used; i++) {
1905 if (lsp->ls_event >= LS_MAX_EVENTS || lsp->ls_count == 0)
1908 (void) fprintf(out, "%2d %10llu %11p %-24s %-24s\n",
1909 lsp->ls_event, (unsigned long long)lsp->ls_time,
1910 (void *)lsp->ls_next,
1911 format_symbol(buf, lsp->ls_lock, 0),
1912 format_symbol(buf2, lsp->ls_caller, 0));
1914 if (rectype <= LS_STACK(0))
1918 * If we went a few frames below the caller, ignore them
1920 for (fr = 3; fr > 0; fr--)
1921 if (lsp->ls_stack[fr] == lsp->ls_caller)
1924 while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
1925 (void) fprintf(out, "%53s %-24s\n", "",
1926 format_symbol(buf, lsp->ls_stack[fr], 0));
1929 (void) fprintf(out, "\n");