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
53 #include <sys/resource.h>
55 #define mergesort(a, b, c, d) lsmergesort(a, b, c, d)
56 #define GETOPT_EOF (-1)
58 typedef uintptr_t pc_t;
61 #define LOCKSTAT_OPTSTR "x:bths:n:d:i:l:f:e:ckwWgCHEATID:RpPo:V"
63 #define LS_MAX_STACK_DEPTH 50
64 #define LS_MAX_EVENTS 64
66 typedef struct lsrec {
67 struct lsrec *ls_next; /* next in hash chain */
68 uintptr_t ls_lock; /* lock address */
69 uintptr_t ls_caller; /* caller address */
70 uint32_t ls_count; /* cumulative event count */
71 uint32_t ls_event; /* type of event */
72 uintptr_t ls_refcnt; /* cumulative reference count */
73 uint64_t ls_time; /* cumulative event duration */
74 uint32_t ls_hist[64]; /* log2(duration) histogram */
75 uintptr_t ls_stack[LS_MAX_STACK_DEPTH];
78 typedef struct lsdata {
79 struct lsrec *lsd_next; /* next available */
80 int lsd_count; /* number of records */
84 * Definitions for the types of experiments which can be run. They are
85 * listed in increasing order of memory cost and processing time cost.
86 * The numerical value of each type is the number of bytes needed per record.
88 #define LS_BASIC offsetof(lsrec_t, ls_time)
89 #define LS_TIME offsetof(lsrec_t, ls_hist[0])
90 #define LS_HIST offsetof(lsrec_t, ls_stack[0])
91 #define LS_STACK(depth) offsetof(lsrec_t, ls_stack[depth])
93 static void report_stats(FILE *, lsrec_t **, size_t, uint64_t, uint64_t);
94 static void report_trace(FILE *, lsrec_t **);
96 extern int symtab_init(void);
97 extern char *addr_to_sym(uintptr_t, uintptr_t *, size_t *);
98 extern uintptr_t sym_to_addr(char *name);
99 extern size_t sym_size(char *name);
100 extern char *strtok_r(char *, const char *, char **);
102 #define DEFAULT_NRECS 10000
103 #define DEFAULT_HZ 97
105 #define MIN_AGGSIZE (16 * 1024)
106 #define MAX_AGGSIZE (32 * 1024 * 1024)
108 static int g_stkdepth;
109 static int g_topn = INT_MAX;
110 static hrtime_t g_elapsed;
111 static int g_rates = 0;
112 static int g_pflag = 0;
113 static int g_Pflag = 0;
114 static int g_wflag = 0;
115 static int g_Wflag = 0;
116 static int g_cflag = 0;
117 static int g_kflag = 0;
118 static int g_gflag = 0;
119 static int g_Vflag = 0;
120 static int g_tracing = 0;
121 static size_t g_recsize;
122 static size_t g_nrecs;
123 static int g_nrecs_used;
124 static uchar_t g_enabled[LS_MAX_EVENTS];
125 static hrtime_t g_min_duration[LS_MAX_EVENTS];
126 static dtrace_hdl_t *g_dtp;
127 static char *g_predicate;
128 static char *g_ipredicate;
130 static int g_proglen;
131 static int g_dropped;
133 typedef struct ls_event_info {
138 char ev_name[DTRACE_NAMELEN];
143 static ls_event_info_t g_event_info[LS_MAX_EVENTS] = {
144 { 'C', "Lock", "Adaptive mutex spin", "nsec",
145 "lockstat:::adaptive-spin" },
146 { 'C', "Lock", "Adaptive mutex block", "nsec",
147 "lockstat:::adaptive-block" },
148 { 'C', "Lock", "Spin lock spin", "nsec",
149 "lockstat:::spin-spin" },
150 { 'C', "Lock", "Thread lock spin", "nsec",
151 "lockstat:::thread-spin" },
152 { 'C', "Lock", "R/W writer blocked by writer", "nsec",
153 "lockstat:::rw-block", "arg2 == 0 && arg3 == 1" },
154 { 'C', "Lock", "R/W writer blocked by readers", "nsec",
155 "lockstat:::rw-block", "arg2 == 0 && arg3 == 0 && arg4" },
156 { 'C', "Lock", "R/W reader blocked by writer", "nsec",
157 "lockstat:::rw-block", "arg2 != 0 && arg3 == 1" },
158 { 'C', "Lock", "R/W reader blocked by write wanted", "nsec",
159 "lockstat:::rw-block", "arg2 != 0 && arg3 == 0 && arg4" },
160 { 'C', "Lock", "R/W writer spin on writer", "nsec",
161 "lockstat:::rw-spin", "arg2 == 0 && arg3 == 1" },
162 { 'C', "Lock", "R/W writer spin on readers", "nsec",
163 "lockstat:::rw-spin", "arg2 == 0 && arg3 == 0 && arg4" },
164 { 'C', "Lock", "R/W reader spin on writer", "nsec",
165 "lockstat:::rw-spin", "arg2 != 0 && arg3 == 1" },
166 { 'C', "Lock", "R/W reader spin on write wanted", "nsec",
167 "lockstat:::rw-spin", "arg2 != 0 && arg3 == 0 && arg4" },
168 { 'C', "Lock", "SX exclusive block", "nsec",
169 "lockstat:::sx-block", "arg2 == 0" },
170 { 'C', "Lock", "SX shared block", "nsec",
171 "lockstat:::sx-block", "arg2 != 0" },
172 { 'C', "Lock", "SX exclusive spin", "nsec",
173 "lockstat:::sx-spin", "arg2 == 0" },
174 { 'C', "Lock", "SX shared spin", "nsec",
175 "lockstat:::sx-spin", "arg2 != 0" },
176 { 'C', "Lock", "Unknown event (type 16)", "units" },
177 { 'C', "Lock", "Unknown event (type 17)", "units" },
178 { 'C', "Lock", "Unknown event (type 18)", "units" },
179 { 'C', "Lock", "Unknown event (type 19)", "units" },
180 { 'C', "Lock", "Unknown event (type 20)", "units" },
181 { 'C', "Lock", "Unknown event (type 21)", "units" },
182 { 'C', "Lock", "Unknown event (type 22)", "units" },
183 { 'C', "Lock", "Unknown event (type 23)", "units" },
184 { 'C', "Lock", "Unknown event (type 24)", "units" },
185 { 'C', "Lock", "Unknown event (type 25)", "units" },
186 { 'C', "Lock", "Unknown event (type 26)", "units" },
187 { 'C', "Lock", "Unknown event (type 27)", "units" },
188 { 'C', "Lock", "Unknown event (type 28)", "units" },
189 { 'C', "Lock", "Unknown event (type 29)", "units" },
190 { 'C', "Lock", "Unknown event (type 30)", "units" },
191 { 'C', "Lock", "Unknown event (type 31)", "units" },
192 { 'H', "Lock", "Adaptive mutex hold", "nsec",
193 "lockstat:::adaptive-release", NULL,
194 "lockstat:::adaptive-acquire" },
195 { 'H', "Lock", "Spin lock hold", "nsec",
196 "lockstat:::spin-release", NULL,
197 "lockstat:::spin-acquire" },
198 { 'H', "Lock", "R/W writer hold", "nsec",
199 "lockstat::rw_wunlock:rw-release", NULL,
200 "lockstat::rw_wlock:rw-acquire" },
201 { 'H', "Lock", "R/W reader hold", "nsec",
202 "lockstat::rw_runlock:rw-release", NULL,
203 "lockstat::rw_rlock:rw-acquire" },
204 { 'H', "Lock", "SX shared hold", "nsec",
205 "lockstat::sx_sunlock:sx-release", NULL,
206 "lockstat::sx_slock:sx-acquire" },
207 { 'H', "Lock", "SX exclusive hold", "nsec",
208 "lockstat::sx_xunlock:sx-release", NULL,
209 "lockstat::sx_xlock:sx-acquire" },
210 { 'H', "Lock", "Unknown event (type 38)", "units" },
211 { 'H', "Lock", "Unknown event (type 39)", "units" },
212 { 'H', "Lock", "Unknown event (type 40)", "units" },
213 { 'H', "Lock", "Unknown event (type 41)", "units" },
214 { 'H', "Lock", "Unknown event (type 42)", "units" },
215 { 'H', "Lock", "Unknown event (type 43)", "units" },
216 { 'H', "Lock", "Unknown event (type 44)", "units" },
217 { 'H', "Lock", "Unknown event (type 45)", "units" },
218 { 'H', "Lock", "Unknown event (type 46)", "units" },
219 { 'H', "Lock", "Unknown event (type 47)", "units" },
220 { 'H', "Lock", "Unknown event (type 48)", "units" },
221 { 'H', "Lock", "Unknown event (type 49)", "units" },
222 { 'H', "Lock", "Unknown event (type 50)", "units" },
223 { 'H', "Lock", "Unknown event (type 51)", "units" },
224 { 'H', "Lock", "Unknown event (type 52)", "units" },
225 { 'H', "Lock", "Unknown event (type 53)", "units" },
226 { 'H', "Lock", "Unknown event (type 54)", "units" },
227 { 'H', "Lock", "Unknown event (type 55)", "units" },
229 { 'I', "CPU+PIL", "Profiling interrupt", "nsec",
231 { 'I', "CPU+Pri_Class", "Profiling interrupt", "nsec",
233 "profile:::profile-97", NULL },
234 { 'I', "Lock", "Unknown event (type 57)", "units" },
235 { 'I', "Lock", "Unknown event (type 58)", "units" },
236 { 'I', "Lock", "Unknown event (type 59)", "units" },
237 { 'E', "Lock", "Recursive lock entry detected", "(N/A)",
238 "lockstat:::rw-release", NULL, "lockstat:::rw-acquire" },
239 { 'E', "Lock", "Lockstat enter failure", "(N/A)" },
240 { 'E', "Lock", "Lockstat exit failure", "nsec" },
241 { 'E', "Lock", "Lockstat record failure", "(N/A)" },
245 static char *g_pri_class[] = {
255 fail(int do_perror, const char *message, ...)
258 int save_errno = errno;
260 va_start(args, message);
261 (void) fprintf(stderr, "lockstat: ");
262 (void) vfprintf(stderr, message, args);
265 (void) fprintf(stderr, ": %s", strerror(save_errno));
266 (void) fprintf(stderr, "\n");
271 dfail(const char *message, ...)
275 va_start(args, message);
276 (void) fprintf(stderr, "lockstat: ");
277 (void) vfprintf(stderr, message, args);
279 (void) fprintf(stderr, ": %s\n",
280 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
286 show_events(char event_type, char *desc)
288 int i, first = -1, last;
290 for (i = 0; i < LS_MAX_EVENTS; i++) {
291 ls_event_info_t *evp = &g_event_info[i];
292 if (evp->ev_type != event_type ||
293 strncmp(evp->ev_desc, "Unknown event", 13) == 0)
300 (void) fprintf(stderr,
301 "\n%s events (lockstat -%c or lockstat -e %d-%d):\n\n",
302 desc, event_type, first, last);
304 for (i = first; i <= last; i++)
305 (void) fprintf(stderr,
306 "%4d = %s\n", i, g_event_info[i].ev_desc);
312 (void) fprintf(stderr,
313 "Usage: lockstat [options] command [args]\n"
314 "\nEvent selection options:\n\n"
315 " -C watch contention events [on by default]\n"
316 " -E watch error events [off by default]\n"
317 " -H watch hold events [off by default]\n"
318 " -I watch interrupt events [off by default]\n"
319 " -A watch all lock events [equivalent to -CH]\n"
320 " -e event_list only watch the specified events (shown below);\n"
321 " <event_list> is a comma-separated list of\n"
322 " events or ranges of events, e.g. 1,4-7,35\n"
323 " -i rate interrupt rate for -I [default: %d Hz]\n"
324 "\nData gathering options:\n\n"
325 " -b basic statistics (lock, caller, event count)\n"
326 " -t timing for all events [default]\n"
327 " -h histograms for event times\n"
328 " -s depth stack traces <depth> deep\n"
329 " -x opt[=val] enable or modify DTrace options\n"
330 "\nData filtering options:\n\n"
331 " -n nrecords maximum number of data records [default: %d]\n"
332 " -l lock[,size] only watch <lock>, which can be specified as a\n"
333 " symbolic name or hex address; <size> defaults\n"
334 " to the ELF symbol size if available, 1 if not\n"
335 " -f func[,size] only watch events generated by <func>\n"
336 " -d duration only watch events longer than <duration>\n"
337 " -T trace (rather than sample) events\n"
338 "\nData reporting options:\n\n"
339 " -c coalesce lock data for arrays like pse_mutex[]\n"
340 " -k coalesce PCs within functions\n"
341 " -g show total events generated by function\n"
342 " -w wherever: don't distinguish events by caller\n"
343 " -W whichever: don't distinguish events by lock\n"
344 " -R display rates rather than counts\n"
345 " -p parsable output format (awk(1)-friendly)\n"
346 " -P sort lock data by (count * avg_time) product\n"
347 " -D n only display top <n> events of each type\n"
348 " -o filename send output to <filename>\n",
349 DEFAULT_HZ, DEFAULT_NRECS);
351 show_events('C', "Contention");
352 show_events('H', "Hold-time");
353 show_events('I', "Interrupt");
354 show_events('E', "Error");
355 (void) fprintf(stderr, "\n");
361 lockcmp(lsrec_t *a, lsrec_t *b)
365 if (a->ls_event < b->ls_event)
367 if (a->ls_event > b->ls_event)
370 for (i = g_stkdepth - 1; i >= 0; i--) {
371 if (a->ls_stack[i] < b->ls_stack[i])
373 if (a->ls_stack[i] > b->ls_stack[i])
377 if (a->ls_caller < b->ls_caller)
379 if (a->ls_caller > b->ls_caller)
382 if (a->ls_lock < b->ls_lock)
384 if (a->ls_lock > b->ls_lock)
391 countcmp(lsrec_t *a, lsrec_t *b)
393 if (a->ls_event < b->ls_event)
395 if (a->ls_event > b->ls_event)
398 return (b->ls_count - a->ls_count);
402 timecmp(lsrec_t *a, lsrec_t *b)
404 if (a->ls_event < b->ls_event)
406 if (a->ls_event > b->ls_event)
409 if (a->ls_time < b->ls_time)
411 if (a->ls_time > b->ls_time)
418 lockcmp_anywhere(lsrec_t *a, lsrec_t *b)
420 if (a->ls_event < b->ls_event)
422 if (a->ls_event > b->ls_event)
425 if (a->ls_lock < b->ls_lock)
427 if (a->ls_lock > b->ls_lock)
434 lock_and_count_cmp_anywhere(lsrec_t *a, lsrec_t *b)
436 if (a->ls_event < b->ls_event)
438 if (a->ls_event > b->ls_event)
441 if (a->ls_lock < b->ls_lock)
443 if (a->ls_lock > b->ls_lock)
446 return (b->ls_count - a->ls_count);
450 sitecmp_anylock(lsrec_t *a, lsrec_t *b)
454 if (a->ls_event < b->ls_event)
456 if (a->ls_event > b->ls_event)
459 for (i = g_stkdepth - 1; i >= 0; i--) {
460 if (a->ls_stack[i] < b->ls_stack[i])
462 if (a->ls_stack[i] > b->ls_stack[i])
466 if (a->ls_caller < b->ls_caller)
468 if (a->ls_caller > b->ls_caller)
475 site_and_count_cmp_anylock(lsrec_t *a, lsrec_t *b)
479 if (a->ls_event < b->ls_event)
481 if (a->ls_event > b->ls_event)
484 for (i = g_stkdepth - 1; i >= 0; i--) {
485 if (a->ls_stack[i] < b->ls_stack[i])
487 if (a->ls_stack[i] > b->ls_stack[i])
491 if (a->ls_caller < b->ls_caller)
493 if (a->ls_caller > b->ls_caller)
496 return (b->ls_count - a->ls_count);
500 lsmergesort(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **a, lsrec_t **b, int n)
506 lsmergesort(cmp, a, b, m);
508 lsmergesort(cmp, a + m, b + m, n - m);
509 for (i = m; i > 0; i--)
511 for (j = m - 1; j < n - 1; j++)
512 b[n + m - j - 2] = a[j + 1];
514 *a++ = cmp(b[i], b[j]) < 0 ? b[i++] : b[j--];
519 coalesce(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **lock, int n)
522 lsrec_t *target, *current;
526 for (i = 1; i < n; i++) {
528 if (cmp(current, target) != 0) {
532 current->ls_event = LS_MAX_EVENTS;
533 target->ls_count += current->ls_count;
534 target->ls_refcnt += current->ls_refcnt;
535 if (g_recsize < LS_TIME)
537 target->ls_time += current->ls_time;
538 if (g_recsize < LS_HIST)
540 for (j = 0; j < 64; j++)
541 target->ls_hist[j] += current->ls_hist[j];
546 coalesce_symbol(uintptr_t *addrp)
551 if (addr_to_sym(*addrp, &symoff, &symsize) != NULL && symoff < symsize)
556 predicate_add(char **pred, char *what, char *cmp, uintptr_t value)
570 newlen = len + strlen(what) + 32 + strlen("( && )");
571 new = malloc(newlen);
573 if (*pred[0] != '\0') {
575 (void) sprintf(new, "(%s) && (%s %s 0x%p)",
576 *pred, what, cmp, (void *)value);
578 (void) sprintf(new, "(%s) && (%s)", *pred, what);
582 (void) sprintf(new, "%s %s 0x%p",
583 what, cmp, (void *)value);
585 (void) sprintf(new, "%s", what);
594 predicate_destroy(char **pred)
601 filter_add(char **filt, char *what, uintptr_t base, uintptr_t size)
603 char buf[256], *c = buf, *new;
612 (void) sprintf(c, "%s(%s >= 0x%p && %s < 0x%p)", *filt[0] != '\0' ?
613 " || " : "", what, (void *)base, what, (void *)(base + size));
615 (void) sprintf(c, "%s(%s >= %p && %s < %p)", *filt[0] != '\0' ?
616 " || " : "", what, (void *)base, what, (void *)(base + size));
619 newlen = (len = strlen(*filt) + 1) + strlen(c);
620 new = malloc(newlen);
621 bcopy(*filt, new, len);
622 (void) strcat(new, c);
628 filter_destroy(char **filt)
635 dprog_add(const char *fmt, ...)
642 size = vsnprintf(&c, 1, fmt, args) + 1;
645 if (g_proglen == 0) {
648 offs = g_proglen - 1;
651 g_proglen = offs + size;
653 if ((g_prog = realloc(g_prog, g_proglen)) == NULL)
654 fail(1, "failed to reallocate program text");
657 (void) vsnprintf(&g_prog[offs], size, fmt, args);
662 * This function may read like an open sewer, but keep in mind that programs
663 * that generate other programs are rarely pretty. If one has the unenviable
664 * task of maintaining or -- worse -- extending this code, use the -V option
665 * to examine the D program as generated by this function.
668 dprog_addevent(int event)
670 ls_event_info_t *info = &g_event_info[event];
673 const char *arg0, *caller;
679 if (info->ev_name[0] == '\0')
682 if (info->ev_type == 'I') {
684 * For interrupt events, arg0 (normally the lock pointer) is
685 * the CPU address plus the current pil, and arg1 (normally
686 * the number of nanoseconds) is the number of nanoseconds
687 * late -- and it's stored in arg2.
690 arg0 = "(uintptr_t)curthread->t_cpu + \n"
691 "\t curthread->t_cpu->cpu_profile_pil";
693 arg0 = "(uintptr_t)(curthread->td_oncpu << 16) + \n"
694 "\t 0x01000000 + curthread->td_pri_class";
696 caller = "(uintptr_t)arg0";
699 arg0 = "(uintptr_t)arg0";
703 if (g_recsize > LS_HIST) {
704 for (depth = 0; g_recsize > LS_STACK(depth); depth++)
708 (void) sprintf(stack, "\tstack(%d);\n", depth);
710 (void) sprintf(stack, ", stack(%d)", depth);
713 (void) sprintf(stack, "");
716 if (info->ev_acquire != NULL) {
718 * If this is a hold event, we need to generate an additional
719 * clause for the acquire; the clause for the release will be
720 * generated with the aggregating statement, below.
722 dprog_add("%s\n", info->ev_acquire);
723 predicate_add(&pred, info->ev_predicate, NULL, 0);
724 predicate_add(&pred, g_predicate, NULL, 0);
726 dprog_add("/%s/\n", pred);
729 (void) sprintf(buf, "self->ev%d[(uintptr_t)arg0]", event);
731 if (info->ev_type == 'H') {
732 dprog_add("\t%s = timestamp;\n", buf);
735 * If this isn't a hold event, it's the recursive
736 * error event. For this, we simply bump the
737 * thread-local, per-lock count.
739 dprog_add("\t%s++;\n", buf);
743 predicate_destroy(&pred);
746 if (info->ev_type == 'E') {
748 * If this is the recursive lock error event, we need
749 * to generate an additional clause to decrement the
750 * thread-local, per-lock count. This assures that we
751 * only execute the aggregating clause if we have
754 dprog_add("%s\n", info->ev_name);
755 dprog_add("/%s/\n{\n\t%s--;\n}\n\n", buf, buf);
758 predicate_add(&pred, buf, NULL, 0);
760 if (info->ev_type == 'H') {
761 (void) sprintf(buf, "timestamp -\n\t "
762 "self->ev%d[(uintptr_t)arg0]", event);
767 predicate_add(&pred, info->ev_predicate, NULL, 0);
768 if (info->ev_type != 'I')
769 predicate_add(&pred, g_predicate, NULL, 0);
771 predicate_add(&pred, g_ipredicate, NULL, 0);
774 if ((dur = g_min_duration[event]) != 0)
775 predicate_add(&pred, arg1, ">=", dur);
777 dprog_add("%s\n", info->ev_name);
780 dprog_add("/%s/\n", pred);
781 predicate_destroy(&pred);
786 dprog_add("\ttrace(%dULL);\n", event);
787 dprog_add("\ttrace(%s);\n", arg0);
788 dprog_add("\ttrace(%s);\n", caller);
792 * The ordering here is important: when we process the
793 * aggregate, we count on the fact that @avg appears before
794 * @hist in program order to assure that @avg is assigned the
795 * first aggregation variable ID and @hist assigned the
796 * second; see the comment in process_aggregate() for details.
798 dprog_add("\t@avg[%dULL, %s, %s%s] = avg(%s);\n",
799 event, arg0, caller, stack, arg1);
801 if (g_recsize >= LS_HIST) {
802 dprog_add("\t@hist[%dULL, %s, %s%s] = quantize"
803 "(%s);\n", event, arg0, caller, stack, arg1);
807 if (info->ev_acquire != NULL)
808 dprog_add("\tself->ev%d[arg0] = 0;\n", event);
817 dtrace_proginfo_t info;
820 (void) fprintf(stderr, "lockstat: vvvv D program vvvv\n");
821 (void) fputs(g_prog, stderr);
822 (void) fprintf(stderr, "lockstat: ^^^^ D program ^^^^\n");
825 if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
826 DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
827 dfail("failed to compile program");
829 if (dtrace_program_exec(g_dtp, prog, &info) == -1)
830 dfail("failed to enable probes");
832 if (dtrace_go(g_dtp) != 0)
833 dfail("couldn't start tracing");
847 dtrace_optval_t val, status, agg;
848 struct sigaction act;
849 struct itimerspec ts;
853 if (dtrace_getopt(g_dtp, "statusrate", &status) == -1)
854 dfail("failed to get 'statusrate'");
856 if (dtrace_getopt(g_dtp, "aggrate", &agg) == -1)
857 dfail("failed to get 'statusrate'");
860 * We would want to awaken at a rate that is the GCD of the statusrate
861 * and the aggrate -- but that seems a bit absurd. Instead, we'll
862 * simply awaken at a rate that is the more frequent of the two, which
863 * assures that we're never later than the interval implied by the
864 * more frequent rate.
866 val = status < agg ? status : agg;
868 (void) sigemptyset(&act.sa_mask);
870 act.sa_handler = status_fire;
871 (void) sigaction(SIGUSR1, &act, NULL);
873 ev.sigev_notify = SIGEV_SIGNAL;
874 ev.sigev_signo = SIGUSR1;
876 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
877 dfail("cannot create CLOCK_REALTIME timer");
879 ts.it_value.tv_sec = val / NANOSEC;
880 ts.it_value.tv_nsec = val % NANOSEC;
881 ts.it_interval = ts.it_value;
883 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
884 dfail("cannot set time on CLOCK_REALTIME timer");
890 if (!g_tracing && dtrace_aggregate_snap(g_dtp) != 0)
891 dfail("failed to snap aggregate");
893 if (dtrace_status(g_dtp) == -1)
894 dfail("dtrace_status()");
898 lsrec_fill(lsrec_t *lsrec, const dtrace_recdesc_t *rec, int nrecs, caddr_t data)
900 bzero(lsrec, g_recsize);
903 if ((g_recsize > LS_HIST && nrecs < 4) || (nrecs < 3))
904 fail(0, "truncated DTrace record");
906 if (rec->dtrd_size != sizeof (uint64_t))
907 fail(0, "bad event size in first record");
909 /* LINTED - alignment */
910 lsrec->ls_event = (uint32_t)*((uint64_t *)(data + rec->dtrd_offset));
913 if (rec->dtrd_size != sizeof (uintptr_t))
914 fail(0, "bad lock address size in second record");
916 /* LINTED - alignment */
917 lsrec->ls_lock = *((uintptr_t *)(data + rec->dtrd_offset));
920 if (rec->dtrd_size != sizeof (uintptr_t))
921 fail(0, "bad caller size in third record");
923 /* LINTED - alignment */
924 lsrec->ls_caller = *((uintptr_t *)(data + rec->dtrd_offset));
927 if (g_recsize > LS_HIST) {
931 frames = rec->dtrd_size / sizeof (pc_t);
932 /* LINTED - alignment */
933 stack = (pc_t *)(data + rec->dtrd_offset);
935 for (i = 1; i < frames; i++)
936 lsrec->ls_stack[i - 1] = stack[i];
942 count_aggregate(const dtrace_aggdata_t *agg, void *arg)
944 *((size_t *)arg) += 1;
946 return (DTRACE_AGGWALK_NEXT);
950 process_aggregate(const dtrace_aggdata_t *agg, void *arg)
952 const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
953 caddr_t data = agg->dtada_data;
954 lsdata_t *lsdata = arg;
955 lsrec_t *lsrec = lsdata->lsd_next;
956 const dtrace_recdesc_t *rec;
957 uint64_t *avg, *quantized;
960 assert(lsdata->lsd_count < g_nrecs);
963 * Aggregation variable IDs are guaranteed to be generated in program
964 * order, and they are guaranteed to start from DTRACE_AGGVARIDNONE
965 * plus one. As "avg" appears before "hist" in program order, we know
966 * that "avg" will be allocated the first aggregation variable ID, and
967 * "hist" will be allocated the second aggregation variable ID -- and
968 * we therefore use the aggregation variable ID to differentiate the
971 if (aggdesc->dtagd_varid > DTRACE_AGGVARIDNONE + 1) {
973 * If this is the histogram entry. We'll copy the quantized
974 * data into lc_hist, and jump over the rest.
976 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
978 if (aggdesc->dtagd_varid != DTRACE_AGGVARIDNONE + 2)
979 fail(0, "bad variable ID in aggregation record");
981 if (rec->dtrd_size !=
982 DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
983 fail(0, "bad quantize size in aggregation record");
985 /* LINTED - alignment */
986 quantized = (uint64_t *)(data + rec->dtrd_offset);
988 for (i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
989 i < DTRACE_QUANTIZE_NBUCKETS; i++, j++)
990 lsrec->ls_hist[j] = quantized[i];
995 lsrec_fill(lsrec, &aggdesc->dtagd_rec[1],
996 aggdesc->dtagd_nrecs - 1, data);
998 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1000 if (rec->dtrd_size != 2 * sizeof (uint64_t))
1001 fail(0, "bad avg size in aggregation record");
1003 /* LINTED - alignment */
1004 avg = (uint64_t *)(data + rec->dtrd_offset);
1005 lsrec->ls_count = (uint32_t)avg[0];
1006 lsrec->ls_time = (uintptr_t)avg[1];
1008 if (g_recsize >= LS_HIST)
1009 return (DTRACE_AGGWALK_NEXT);
1012 lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
1013 lsdata->lsd_count++;
1015 return (DTRACE_AGGWALK_NEXT);
1019 process_trace(const dtrace_probedata_t *pdata, void *arg)
1021 lsdata_t *lsdata = arg;
1022 lsrec_t *lsrec = lsdata->lsd_next;
1023 dtrace_eprobedesc_t *edesc = pdata->dtpda_edesc;
1024 caddr_t data = pdata->dtpda_data;
1026 if (lsdata->lsd_count >= g_nrecs)
1027 return (DTRACE_CONSUME_NEXT);
1029 lsrec_fill(lsrec, edesc->dtepd_rec, edesc->dtepd_nrecs, data);
1031 lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
1032 lsdata->lsd_count++;
1034 return (DTRACE_CONSUME_NEXT);
1038 process_data(FILE *out, char *data)
1042 /* LINTED - alignment */
1043 lsdata.lsd_next = (lsrec_t *)data;
1044 lsdata.lsd_count = 0;
1047 if (dtrace_consume(g_dtp, out,
1048 process_trace, NULL, &lsdata) != 0)
1049 dfail("failed to consume buffer");
1051 return (lsdata.lsd_count);
1054 if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
1055 process_aggregate, &lsdata) != 0)
1056 dfail("failed to walk aggregate");
1058 return (lsdata.lsd_count);
1063 drophandler(const dtrace_dropdata_t *data, void *arg)
1066 (void) fprintf(stderr, "lockstat: warning: %s", data->dtdda_msg);
1067 return (DTRACE_HANDLE_OK);
1071 main(int argc, char **argv)
1074 lsrec_t *lsp, **current, **first, **sort_buf, **merge_buf;
1081 char *addrp, *offp, *sizep, *evp, *lastp, *p;
1084 int events_specified = 0;
1087 char *filt = NULL, *ifilt = NULL;
1088 static uint64_t ev_count[LS_MAX_EVENTS + 1];
1089 static uint64_t ev_time[LS_MAX_EVENTS + 1];
1090 dtrace_optval_t aggsize;
1096 if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
1097 fail(0, "cannot open dtrace library: %s",
1098 dtrace_errmsg(NULL, err));
1101 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1102 dfail("couldn't establish drop handler");
1104 if (symtab_init() == -1)
1105 fail(1, "can't load kernel symbols");
1107 g_nrecs = DEFAULT_NRECS;
1109 while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != GETOPT_EOF) {
1112 g_recsize = LS_BASIC;
1116 g_recsize = LS_TIME;
1120 g_recsize = LS_HIST;
1124 if (!isdigit(optarg[0]))
1126 g_stkdepth = atoi(optarg);
1127 if (g_stkdepth > LS_MAX_STACK_DEPTH)
1128 fail(0, "max stack depth is %d",
1129 LS_MAX_STACK_DEPTH);
1130 g_recsize = LS_STACK(g_stkdepth);
1134 if (!isdigit(optarg[0]))
1136 g_nrecs = atoi(optarg);
1140 if (!isdigit(optarg[0]))
1142 duration = atoll(optarg);
1145 * XXX -- durations really should be per event
1146 * since the units are different, but it's hard
1147 * to express this nicely in the interface.
1148 * Not clear yet what the cleanest solution is.
1150 for (i = 0; i < LS_MAX_EVENTS; i++)
1151 if (g_event_info[i].ev_type != 'E')
1152 g_min_duration[i] = duration;
1157 if (!isdigit(optarg[0]))
1163 fail(0, "max interrupt rate is %d Hz", MAX_HZ);
1165 for (j = 0; j < LS_MAX_EVENTS; j++)
1166 if (strcmp(g_event_info[j].ev_desc,
1167 "Profiling interrupt") == 0)
1170 (void) sprintf(g_event_info[j].ev_name,
1171 "profile:::profile-%d", i);
1176 addrp = strtok(optarg, ",");
1177 sizep = strtok(NULL, ",");
1178 addrp = strtok(optarg, ",+");
1179 offp = strtok(NULL, ",");
1181 size = sizep ? strtoul(sizep, NULL, 0) : 1;
1182 off = offp ? strtoul(offp, NULL, 0) : 0;
1184 if (addrp[0] == '0') {
1185 addr = strtoul(addrp, NULL, 16) + off;
1187 addr = sym_to_addr(addrp) + off;
1189 size = sym_size(addrp) - off;
1190 if (addr - off == 0)
1191 fail(0, "symbol '%s' not found", addrp);
1198 filter_add(&filt, "arg0", addr, size);
1200 filter_add(&filt, "caller", addr, size);
1201 filter_add(&ifilt, "arg0", addr, size);
1206 evp = strtok_r(optarg, ",", &lastp);
1211 (void) strtok(evp, "-");
1212 evp2 = strtok(NULL, "-");
1214 ev2 = evp2 ? atoi(evp2) : ev1;
1215 if ((uint_t)ev1 >= LS_MAX_EVENTS ||
1216 (uint_t)ev2 >= LS_MAX_EVENTS || ev1 > ev2)
1217 fail(0, "-e events out of range");
1218 for (i = ev1; i <= ev2; i++)
1220 evp = strtok_r(NULL, ",", &lastp);
1222 events_specified = 1;
1249 for (i = 0; i < LS_MAX_EVENTS; i++)
1250 if (g_event_info[i].ev_type == c)
1252 events_specified = 1;
1256 for (i = 0; i < LS_MAX_EVENTS; i++)
1257 if (strchr("CH", g_event_info[i].ev_type))
1259 events_specified = 1;
1267 if (!isdigit(optarg[0]))
1269 g_topn = atoi(optarg);
1285 if ((out = fopen(optarg, "w")) == NULL)
1286 fail(1, "error opening file");
1294 if (strchr(LOCKSTAT_OPTSTR, c) == NULL)
1300 predicate_add(&g_predicate, filt, NULL, 0);
1301 filter_destroy(&filt);
1304 if (ifilt != NULL) {
1305 predicate_add(&g_ipredicate, ifilt, NULL, 0);
1306 filter_destroy(&ifilt);
1309 if (g_recsize == 0) {
1311 g_stkdepth = LS_MAX_STACK_DEPTH;
1312 g_recsize = LS_STACK(g_stkdepth);
1314 g_recsize = LS_TIME;
1318 if (g_gflag && g_recsize <= LS_STACK(0))
1319 fail(0, "'-g' requires at least '-s 1' data gathering");
1322 * Make sure the alignment is reasonable
1324 g_recsize = -(-g_recsize & -sizeof (uint64_t));
1326 for (i = 0; i < LS_MAX_EVENTS; i++) {
1328 * If no events were specified, enable -C.
1330 if (!events_specified && g_event_info[i].ev_type == 'C')
1334 for (i = 0; i < LS_MAX_EVENTS; i++) {
1338 if (g_event_info[i].ev_acquire != NULL) {
1340 * If we've enabled a hold event, we must explicitly
1341 * allocate dynamic variable space.
1350 * Make sure there are remaining arguments to specify a child command
1356 if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
1357 dfail("couldn't determine number of online CPUs");
1360 * By default, we set our data buffer size to be the number of records
1361 * multiplied by the size of the record, doubled to account for some
1362 * DTrace slop and divided by the number of CPUs. We silently clamp
1363 * the aggregation size at both a minimum and a maximum to prevent
1364 * absurdly low or high values.
1366 if ((aggsize = (g_nrecs * g_recsize * 2) / ncpus) < MIN_AGGSIZE)
1367 aggsize = MIN_AGGSIZE;
1369 if (aggsize > MAX_AGGSIZE)
1370 aggsize = MAX_AGGSIZE;
1372 (void) sprintf(aggstr, "%lld", (long long)aggsize);
1375 if (dtrace_setopt(g_dtp, "bufsize", "4k") == -1)
1376 dfail("failed to set 'bufsize'");
1378 if (dtrace_setopt(g_dtp, "aggsize", aggstr) == -1)
1379 dfail("failed to set 'aggsize'");
1383 * If we're using dynamic variables, we set our
1384 * dynamic variable size to be one megabyte per CPU,
1385 * with a hard-limit of 32 megabytes. This may still
1386 * be too small in some cases, but it can be tuned
1387 * manually via -x if need be.
1389 (void) sprintf(aggstr, "%ldm", ncpus < 32 ? ncpus : 32);
1391 if (dtrace_setopt(g_dtp, "dynvarsize", aggstr) == -1)
1392 dfail("failed to set 'dynvarsize'");
1395 if (dtrace_setopt(g_dtp, "bufsize", aggstr) == -1)
1396 dfail("failed to set 'bufsize'");
1399 if (dtrace_setopt(g_dtp, "statusrate", "10sec") == -1)
1400 dfail("failed to set 'statusrate'");
1403 while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != GETOPT_EOF) {
1406 if ((p = strchr(optarg, '=')) != NULL)
1409 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1410 dfail("failed to set -x %s", optarg);
1421 g_elapsed = -gethrtime();
1424 * Spawn the specified command and wait for it to complete.
1428 fail(1, "cannot fork");
1430 (void) dtrace_close(g_dtp);
1431 (void) execvp(argv[0], &argv[0]);
1437 while (waitpid(child, &status, WEXITED) != child)
1439 while (waitpid(child, &status, 0) != child)
1443 g_elapsed += gethrtime();
1445 if (WIFEXITED(status)) {
1446 if (WEXITSTATUS(status) != 0) {
1447 if (exec_errno != 0) {
1449 fail(1, "could not execute %s", argv[0]);
1451 (void) fprintf(stderr,
1452 "lockstat: warning: %s exited with code %d\n",
1453 argv[0], WEXITSTATUS(status));
1456 (void) fprintf(stderr,
1457 "lockstat: warning: %s died on signal %d\n",
1458 argv[0], WTERMSIG(status));
1461 if (dtrace_stop(g_dtp) == -1)
1462 dfail("failed to stop dtrace");
1465 * Before we read out the results, we need to allocate our buffer.
1466 * If we're tracing, then we'll just use the precalculated size. If
1467 * we're not, then we'll take a snapshot of the aggregate, and walk
1468 * it to count the number of records.
1471 if (dtrace_aggregate_snap(g_dtp) != 0)
1472 dfail("failed to snap aggregate");
1476 if (dtrace_aggregate_walk(g_dtp,
1477 count_aggregate, &g_nrecs) != 0)
1478 dfail("failed to walk aggregate");
1482 if ((data_buf = memalign(sizeof (uint64_t),
1483 (g_nrecs + 1) * g_recsize)) == NULL)
1485 if (posix_memalign((void **)&data_buf, sizeof (uint64_t),
1486 (g_nrecs + 1) * g_recsize) )
1488 fail(1, "Memory allocation failed");
1491 * Read out the DTrace data.
1493 g_nrecs_used = process_data(out, data_buf);
1495 if (g_nrecs_used > g_nrecs || g_dropped)
1496 (void) fprintf(stderr, "lockstat: warning: "
1497 "ran out of data records (use -n for more)\n");
1499 /* LINTED - alignment */
1500 for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1501 /* LINTED - alignment */
1502 lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
1503 ev_count[lsp->ls_event] += lsp->ls_count;
1504 ev_time[lsp->ls_event] += lsp->ls_time;
1508 * If -g was specified, convert stacks into individual records.
1511 lsrec_t *newlsp, *oldlsp;
1514 newlsp = memalign(sizeof (uint64_t),
1515 g_nrecs_used * LS_TIME * (g_stkdepth + 1));
1517 posix_memalign((void **)&newlsp, sizeof (uint64_t),
1518 g_nrecs_used * LS_TIME * (g_stkdepth + 1));
1521 fail(1, "Cannot allocate space for -g processing");
1523 /* LINTED - alignment */
1524 for (i = 0, oldlsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1525 /* LINTED - alignment */
1526 oldlsp = (lsrec_t *)((char *)oldlsp + g_recsize)) {
1528 int caller_in_stack = 0;
1530 if (oldlsp->ls_count == 0)
1533 for (fr = 0; fr < g_stkdepth; fr++) {
1534 if (oldlsp->ls_stack[fr] == 0)
1536 if (oldlsp->ls_stack[fr] == oldlsp->ls_caller)
1537 caller_in_stack = 1;
1538 bcopy(oldlsp, lsp, LS_TIME);
1539 lsp->ls_caller = oldlsp->ls_stack[fr];
1540 /* LINTED - alignment */
1541 lsp = (lsrec_t *)((char *)lsp + LS_TIME);
1543 if (!caller_in_stack) {
1544 bcopy(oldlsp, lsp, LS_TIME);
1545 /* LINTED - alignment */
1546 lsp = (lsrec_t *)((char *)lsp + LS_TIME);
1549 g_nrecs = g_nrecs_used =
1550 ((uintptr_t)lsp - (uintptr_t)newlsp) / LS_TIME;
1551 g_recsize = LS_TIME;
1554 data_buf = (char *)newlsp;
1557 if ((sort_buf = calloc(2 * (g_nrecs + 1),
1558 sizeof (void *))) == NULL)
1559 fail(1, "Sort buffer allocation failed");
1560 merge_buf = sort_buf + (g_nrecs + 1);
1563 * Build the sort buffer, discarding zero-count records along the way.
1565 /* LINTED - alignment */
1566 for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1567 /* LINTED - alignment */
1568 lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
1569 if (lsp->ls_count == 0)
1570 lsp->ls_event = LS_MAX_EVENTS;
1574 if (g_nrecs_used == 0)
1578 * Add a sentinel after the last record
1581 lsp->ls_event = LS_MAX_EVENTS;
1584 report_trace(out, sort_buf);
1589 * Application of -g may have resulted in multiple records
1590 * with the same signature; coalesce them.
1593 mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
1594 coalesce(lockcmp, sort_buf, g_nrecs_used);
1598 * Coalesce locks within the same symbol if -c option specified.
1599 * Coalesce PCs within the same function if -k option specified.
1601 if (g_cflag || g_kflag) {
1602 for (i = 0; i < g_nrecs_used; i++) {
1606 coalesce_symbol(&lsp->ls_lock);
1608 for (fr = 0; fr < g_stkdepth; fr++)
1609 coalesce_symbol(&lsp->ls_stack[fr]);
1610 coalesce_symbol(&lsp->ls_caller);
1613 mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
1614 coalesce(lockcmp, sort_buf, g_nrecs_used);
1618 * Coalesce callers if -w option specified
1621 mergesort(lock_and_count_cmp_anywhere,
1622 sort_buf, merge_buf, g_nrecs_used);
1623 coalesce(lockcmp_anywhere, sort_buf, g_nrecs_used);
1627 * Coalesce locks if -W option specified
1630 mergesort(site_and_count_cmp_anylock,
1631 sort_buf, merge_buf, g_nrecs_used);
1632 coalesce(sitecmp_anylock, sort_buf, g_nrecs_used);
1636 * Sort data by contention count (ls_count) or total time (ls_time),
1637 * depending on g_Pflag. Override g_Pflag if time wasn't measured.
1639 if (g_recsize < LS_TIME)
1643 mergesort(timecmp, sort_buf, merge_buf, g_nrecs_used);
1645 mergesort(countcmp, sort_buf, merge_buf, g_nrecs_used);
1648 * Display data by event type
1650 first = &sort_buf[0];
1651 while ((event = (*first)->ls_event) < LS_MAX_EVENTS) {
1653 while ((lsp = *current)->ls_event == event)
1655 report_stats(out, first, current - first, ev_count[event],
1664 format_symbol(char *buf, uintptr_t addr, int show_size)
1670 symname = addr_to_sym(addr, &symoff, &symsize);
1672 if (show_size && symoff == 0)
1673 (void) sprintf(buf, "%s[%ld]", symname, (long)symsize);
1674 else if (symoff == 0)
1675 (void) sprintf(buf, "%s", symname);
1676 else if (symoff < 16 && bcmp(symname, "cpu[", 4) == 0) /* CPU+PIL */
1678 (void) sprintf(buf, "%s+%ld", symname, (long)symoff);
1680 (void) sprintf(buf, "%s+%s", symname, g_pri_class[(int)symoff]);
1682 else if (symoff <= symsize || (symoff < 256 && addr != symoff))
1683 (void) sprintf(buf, "%s+0x%llx", symname,
1684 (unsigned long long)symoff);
1686 (void) sprintf(buf, "0x%llx", (unsigned long long)addr);
1691 report_stats(FILE *out, lsrec_t **sort_buf, size_t nrecs, uint64_t total_count,
1692 uint64_t total_time)
1694 uint32_t event = sort_buf[0]->ls_event;
1696 double ptotal = 0.0;
1700 int first_bin, last_bin, max_bin_count, total_bin_count;
1703 char lhdr[80], chdr[80];
1705 rectype = g_recsize;
1708 (void) fprintf(out, "%20llu %s\n",
1709 g_rates == 0 ? total_count :
1710 ((unsigned long long)total_count * NANOSEC) / g_elapsed,
1711 g_event_info[event].ev_desc);
1715 (void) sprintf(lhdr, "%s%s",
1716 g_Wflag ? "Hottest " : "", g_event_info[event].ev_lhdr);
1717 (void) sprintf(chdr, "%s%s",
1718 g_wflag ? "Hottest " : "", "Caller");
1722 "\n%s: %.0f events in %.3f seconds (%.0f events/sec)\n\n",
1723 g_event_info[event].ev_desc, (double)total_count,
1724 (double)g_elapsed / NANOSEC,
1725 (double)total_count * NANOSEC / g_elapsed);
1727 if (!g_pflag && rectype < LS_HIST) {
1728 (void) sprintf(buf, "%s", g_event_info[event].ev_units);
1729 (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
1730 g_rates ? "ops/s" : "Count",
1731 g_gflag ? "genr" : "indv",
1732 "cuml", "rcnt", rectype >= LS_TIME ? buf : "", lhdr, chdr);
1733 (void) fprintf(out, "---------------------------------"
1734 "----------------------------------------------\n");
1738 for (i = 0; i < nrecs; i++) {
1741 if (displayed++ >= g_topn)
1747 (void) fprintf(out, "%u %u",
1748 lsp->ls_event, lsp->ls_count);
1749 (void) fprintf(out, " %s",
1750 format_symbol(buf, lsp->ls_lock, g_cflag));
1751 (void) fprintf(out, " %s",
1752 format_symbol(buf, lsp->ls_caller, 0));
1753 (void) fprintf(out, " %f",
1754 (double)lsp->ls_refcnt / lsp->ls_count);
1755 if (rectype >= LS_TIME)
1756 (void) fprintf(out, " %llu",
1757 (unsigned long long)lsp->ls_time);
1758 if (rectype >= LS_HIST) {
1759 for (j = 0; j < 64; j++)
1760 (void) fprintf(out, " %u",
1763 for (j = 0; j < LS_MAX_STACK_DEPTH; j++) {
1764 if (rectype <= LS_STACK(j) ||
1765 lsp->ls_stack[j] == 0)
1767 (void) fprintf(out, " %s",
1768 format_symbol(buf, lsp->ls_stack[j], 0));
1770 (void) fprintf(out, "\n");
1774 if (rectype >= LS_HIST) {
1775 (void) fprintf(out, "---------------------------------"
1776 "----------------------------------------------\n");
1777 (void) sprintf(buf, "%s",
1778 g_event_info[event].ev_units);
1779 (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
1780 g_rates ? "ops/s" : "Count",
1781 g_gflag ? "genr" : "indv",
1782 "cuml", "rcnt", buf, lhdr, chdr);
1785 if (g_Pflag && total_time != 0)
1786 percent = (lsp->ls_time * 100.00) / total_time;
1788 percent = (lsp->ls_count * 100.00) / total_count;
1792 if (rectype >= LS_TIME)
1793 (void) sprintf(buf, "%llu",
1794 (unsigned long long)(lsp->ls_time / lsp->ls_count));
1798 (void) fprintf(out, "%5llu ",
1799 g_rates == 0 ? lsp->ls_count :
1800 ((uint64_t)lsp->ls_count * NANOSEC) / g_elapsed);
1802 (void) fprintf(out, "%3.0f%% ", percent);
1805 (void) fprintf(out, "---- ");
1807 (void) fprintf(out, "%3.0f%% ", ptotal);
1809 (void) fprintf(out, "%4.2f %8s ",
1810 (double)lsp->ls_refcnt / lsp->ls_count, buf);
1812 (void) fprintf(out, "%-22s ",
1813 format_symbol(buf, lsp->ls_lock, g_cflag));
1815 (void) fprintf(out, "%-24s\n",
1816 format_symbol(buf, lsp->ls_caller, 0));
1818 if (rectype < LS_HIST)
1821 (void) fprintf(out, "\n");
1822 (void) fprintf(out, "%10s %31s %-9s %-24s\n",
1823 g_event_info[event].ev_units,
1824 "------ Time Distribution ------",
1825 g_rates ? "ops/s" : "count",
1826 rectype > LS_STACK(0) ? "Stack" : "");
1829 while (lsp->ls_hist[first_bin] == 0)
1833 while (lsp->ls_hist[last_bin] == 0)
1837 total_bin_count = 0;
1838 for (j = first_bin; j <= last_bin; j++) {
1839 total_bin_count += lsp->ls_hist[j];
1840 if (lsp->ls_hist[j] > max_bin_count)
1841 max_bin_count = lsp->ls_hist[j];
1845 * If we went a few frames below the caller, ignore them
1847 for (fr = 3; fr > 0; fr--)
1848 if (lsp->ls_stack[fr] == lsp->ls_caller)
1851 for (j = first_bin; j <= last_bin; j++) {
1852 uint_t depth = (lsp->ls_hist[j] * 30) / total_bin_count;
1853 (void) fprintf(out, "%10llu |%s%s %-9u ",
1855 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + 30 - depth,
1857 g_rates == 0 ? lsp->ls_hist[j] :
1858 (uint_t)(((uint64_t)lsp->ls_hist[j] * NANOSEC) /
1860 if (rectype <= LS_STACK(fr) || lsp->ls_stack[fr] == 0) {
1861 (void) fprintf(out, "\n");
1864 (void) fprintf(out, "%-24s\n",
1865 format_symbol(buf, lsp->ls_stack[fr], 0));
1868 while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
1869 (void) fprintf(out, "%15s %-36s %-24s\n", "", "",
1870 format_symbol(buf, lsp->ls_stack[fr], 0));
1876 (void) fprintf(out, "---------------------------------"
1877 "----------------------------------------------\n");
1883 report_trace(FILE *out, lsrec_t **sort_buf)
1888 char buf[256], buf2[256];
1890 rectype = g_recsize;
1893 (void) fprintf(out, "%5s %7s %11s %-24s %-24s\n",
1894 "Event", "Time", "Owner", "Lock", "Caller");
1895 (void) fprintf(out, "---------------------------------"
1896 "----------------------------------------------\n");
1899 for (i = 0; i < g_nrecs_used; i++) {
1903 if (lsp->ls_event >= LS_MAX_EVENTS || lsp->ls_count == 0)
1906 (void) fprintf(out, "%2d %10llu %11p %-24s %-24s\n",
1907 lsp->ls_event, (unsigned long long)lsp->ls_time,
1908 (void *)lsp->ls_next,
1909 format_symbol(buf, lsp->ls_lock, 0),
1910 format_symbol(buf2, lsp->ls_caller, 0));
1912 if (rectype <= LS_STACK(0))
1916 * If we went a few frames below the caller, ignore them
1918 for (fr = 3; fr > 0; fr--)
1919 if (lsp->ls_stack[fr] == lsp->ls_caller)
1922 while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
1923 (void) fprintf(out, "%53s %-24s\n", "",
1924 format_symbol(buf, lsp->ls_stack[fr], 0));
1927 (void) fprintf(out, "\n");