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", "Unknown event (type 8)", "units" },
162 { 'C', "Lock", "Unknown event (type 9)", "units" },
163 { 'C', "Lock", "Unknown event (type 10)", "units" },
164 { 'C', "Lock", "Unknown event (type 11)", "units" },
165 { 'C', "Lock", "Unknown event (type 12)", "units" },
166 { 'C', "Lock", "Unknown event (type 13)", "units" },
167 { 'C', "Lock", "Unknown event (type 14)", "units" },
168 { 'C', "Lock", "Unknown event (type 15)", "units" },
169 { 'C', "Lock", "Unknown event (type 16)", "units" },
170 { 'C', "Lock", "Unknown event (type 17)", "units" },
171 { 'C', "Lock", "Unknown event (type 18)", "units" },
172 { 'C', "Lock", "Unknown event (type 19)", "units" },
173 { 'C', "Lock", "Unknown event (type 20)", "units" },
174 { 'C', "Lock", "Unknown event (type 21)", "units" },
175 { 'C', "Lock", "Unknown event (type 22)", "units" },
176 { 'C', "Lock", "Unknown event (type 23)", "units" },
177 { 'C', "Lock", "Unknown event (type 24)", "units" },
178 { 'C', "Lock", "Unknown event (type 25)", "units" },
179 { 'C', "Lock", "Unknown event (type 26)", "units" },
180 { 'C', "Lock", "Unknown event (type 27)", "units" },
181 { 'C', "Lock", "Unknown event (type 28)", "units" },
182 { 'C', "Lock", "Unknown event (type 29)", "units" },
183 { 'C', "Lock", "Unknown event (type 30)", "units" },
184 { 'C', "Lock", "Unknown event (type 31)", "units" },
185 { 'H', "Lock", "Adaptive mutex hold", "nsec",
186 "lockstat:::adaptive-release", NULL,
187 "lockstat:::adaptive-acquire" },
188 { 'H', "Lock", "Spin lock hold", "nsec",
189 "lockstat:::spin-release", NULL,
190 "lockstat:::spin-acquire" },
191 { 'H', "Lock", "R/W writer hold", "nsec",
192 "lockstat:::rw-release", "arg1 == 0",
193 "lockstat:::rw-acquire" },
194 { 'H', "Lock", "R/W reader hold", "nsec",
195 "lockstat:::rw-release", "arg1 != 0",
196 "lockstat:::rw-acquire" },
197 { 'H', "Lock", "Unknown event (type 36)", "units" },
198 { 'H', "Lock", "Unknown event (type 37)", "units" },
199 { 'H', "Lock", "Unknown event (type 38)", "units" },
200 { 'H', "Lock", "Unknown event (type 39)", "units" },
201 { 'H', "Lock", "Unknown event (type 40)", "units" },
202 { 'H', "Lock", "Unknown event (type 41)", "units" },
203 { 'H', "Lock", "Unknown event (type 42)", "units" },
204 { 'H', "Lock", "Unknown event (type 43)", "units" },
205 { 'H', "Lock", "Unknown event (type 44)", "units" },
206 { 'H', "Lock", "Unknown event (type 45)", "units" },
207 { 'H', "Lock", "Unknown event (type 46)", "units" },
208 { 'H', "Lock", "Unknown event (type 47)", "units" },
209 { 'H', "Lock", "Unknown event (type 48)", "units" },
210 { 'H', "Lock", "Unknown event (type 49)", "units" },
211 { 'H', "Lock", "Unknown event (type 50)", "units" },
212 { 'H', "Lock", "Unknown event (type 51)", "units" },
213 { 'H', "Lock", "Unknown event (type 52)", "units" },
214 { 'H', "Lock", "Unknown event (type 53)", "units" },
215 { 'H', "Lock", "Unknown event (type 54)", "units" },
216 { 'H', "Lock", "Unknown event (type 55)", "units" },
218 { 'I', "CPU+PIL", "Profiling interrupt", "nsec",
221 { 'I', "CPU+Pri_Class", "Profiling interrupt", "nsec",
223 "profile:::profile-97", NULL },
224 { 'I', "Lock", "Unknown event (type 57)", "units" },
225 { 'I', "Lock", "Unknown event (type 58)", "units" },
226 { 'I', "Lock", "Unknown event (type 59)", "units" },
227 { 'E', "Lock", "Recursive lock entry detected", "(N/A)",
228 "lockstat:::rw-release", NULL, "lockstat:::rw-acquire" },
229 { 'E', "Lock", "Lockstat enter failure", "(N/A)" },
230 { 'E', "Lock", "Lockstat exit failure", "nsec" },
231 { 'E', "Lock", "Lockstat record failure", "(N/A)" },
235 static char *g_pri_class[] = {
245 fail(int do_perror, const char *message, ...)
248 int save_errno = errno;
250 va_start(args, message);
251 (void) fprintf(stderr, "lockstat: ");
252 (void) vfprintf(stderr, message, args);
255 (void) fprintf(stderr, ": %s", strerror(save_errno));
256 (void) fprintf(stderr, "\n");
261 dfail(const char *message, ...)
265 va_start(args, message);
266 (void) fprintf(stderr, "lockstat: ");
267 (void) vfprintf(stderr, message, args);
269 (void) fprintf(stderr, ": %s\n",
270 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
276 show_events(char event_type, char *desc)
278 int i, first = -1, last;
280 for (i = 0; i < LS_MAX_EVENTS; i++) {
281 ls_event_info_t *evp = &g_event_info[i];
282 if (evp->ev_type != event_type ||
283 strncmp(evp->ev_desc, "Unknown event", 13) == 0)
290 (void) fprintf(stderr,
291 "\n%s events (lockstat -%c or lockstat -e %d-%d):\n\n",
292 desc, event_type, first, last);
294 for (i = first; i <= last; i++)
295 (void) fprintf(stderr,
296 "%4d = %s\n", i, g_event_info[i].ev_desc);
302 (void) fprintf(stderr,
303 "Usage: lockstat [options] command [args]\n"
304 "\nEvent selection options:\n\n"
305 " -C watch contention events [on by default]\n"
306 " -E watch error events [off by default]\n"
307 " -H watch hold events [off by default]\n"
308 " -I watch interrupt events [off by default]\n"
309 " -A watch all lock events [equivalent to -CH]\n"
310 " -e event_list only watch the specified events (shown below);\n"
311 " <event_list> is a comma-separated list of\n"
312 " events or ranges of events, e.g. 1,4-7,35\n"
313 " -i rate interrupt rate for -I [default: %d Hz]\n"
314 "\nData gathering options:\n\n"
315 " -b basic statistics (lock, caller, event count)\n"
316 " -t timing for all events [default]\n"
317 " -h histograms for event times\n"
318 " -s depth stack traces <depth> deep\n"
319 " -x opt[=val] enable or modify DTrace options\n"
320 "\nData filtering options:\n\n"
321 " -n nrecords maximum number of data records [default: %d]\n"
322 " -l lock[,size] only watch <lock>, which can be specified as a\n"
323 " symbolic name or hex address; <size> defaults\n"
324 " to the ELF symbol size if available, 1 if not\n"
325 " -f func[,size] only watch events generated by <func>\n"
326 " -d duration only watch events longer than <duration>\n"
327 " -T trace (rather than sample) events\n"
328 "\nData reporting options:\n\n"
329 " -c coalesce lock data for arrays like pse_mutex[]\n"
330 " -k coalesce PCs within functions\n"
331 " -g show total events generated by function\n"
332 " -w wherever: don't distinguish events by caller\n"
333 " -W whichever: don't distinguish events by lock\n"
334 " -R display rates rather than counts\n"
335 " -p parsable output format (awk(1)-friendly)\n"
336 " -P sort lock data by (count * avg_time) product\n"
337 " -D n only display top <n> events of each type\n"
338 " -o filename send output to <filename>\n",
339 DEFAULT_HZ, DEFAULT_NRECS);
341 show_events('C', "Contention");
342 show_events('H', "Hold-time");
343 show_events('I', "Interrupt");
344 show_events('E', "Error");
345 (void) fprintf(stderr, "\n");
351 lockcmp(lsrec_t *a, lsrec_t *b)
355 if (a->ls_event < b->ls_event)
357 if (a->ls_event > b->ls_event)
360 for (i = g_stkdepth - 1; i >= 0; i--) {
361 if (a->ls_stack[i] < b->ls_stack[i])
363 if (a->ls_stack[i] > b->ls_stack[i])
367 if (a->ls_caller < b->ls_caller)
369 if (a->ls_caller > b->ls_caller)
372 if (a->ls_lock < b->ls_lock)
374 if (a->ls_lock > b->ls_lock)
381 countcmp(lsrec_t *a, lsrec_t *b)
383 if (a->ls_event < b->ls_event)
385 if (a->ls_event > b->ls_event)
388 return (b->ls_count - a->ls_count);
392 timecmp(lsrec_t *a, lsrec_t *b)
394 if (a->ls_event < b->ls_event)
396 if (a->ls_event > b->ls_event)
399 if (a->ls_time < b->ls_time)
401 if (a->ls_time > b->ls_time)
408 lockcmp_anywhere(lsrec_t *a, lsrec_t *b)
410 if (a->ls_event < b->ls_event)
412 if (a->ls_event > b->ls_event)
415 if (a->ls_lock < b->ls_lock)
417 if (a->ls_lock > b->ls_lock)
424 lock_and_count_cmp_anywhere(lsrec_t *a, lsrec_t *b)
426 if (a->ls_event < b->ls_event)
428 if (a->ls_event > b->ls_event)
431 if (a->ls_lock < b->ls_lock)
433 if (a->ls_lock > b->ls_lock)
436 return (b->ls_count - a->ls_count);
440 sitecmp_anylock(lsrec_t *a, lsrec_t *b)
444 if (a->ls_event < b->ls_event)
446 if (a->ls_event > b->ls_event)
449 for (i = g_stkdepth - 1; i >= 0; i--) {
450 if (a->ls_stack[i] < b->ls_stack[i])
452 if (a->ls_stack[i] > b->ls_stack[i])
456 if (a->ls_caller < b->ls_caller)
458 if (a->ls_caller > b->ls_caller)
465 site_and_count_cmp_anylock(lsrec_t *a, lsrec_t *b)
469 if (a->ls_event < b->ls_event)
471 if (a->ls_event > b->ls_event)
474 for (i = g_stkdepth - 1; i >= 0; i--) {
475 if (a->ls_stack[i] < b->ls_stack[i])
477 if (a->ls_stack[i] > b->ls_stack[i])
481 if (a->ls_caller < b->ls_caller)
483 if (a->ls_caller > b->ls_caller)
486 return (b->ls_count - a->ls_count);
490 lsmergesort(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **a, lsrec_t **b, int n)
496 lsmergesort(cmp, a, b, m);
498 lsmergesort(cmp, a + m, b + m, n - m);
499 for (i = m; i > 0; i--)
501 for (j = m - 1; j < n - 1; j++)
502 b[n + m - j - 2] = a[j + 1];
504 *a++ = cmp(b[i], b[j]) < 0 ? b[i++] : b[j--];
509 coalesce(int (*cmp)(lsrec_t *, lsrec_t *), lsrec_t **lock, int n)
512 lsrec_t *target, *current;
516 for (i = 1; i < n; i++) {
518 if (cmp(current, target) != 0) {
522 current->ls_event = LS_MAX_EVENTS;
523 target->ls_count += current->ls_count;
524 target->ls_refcnt += current->ls_refcnt;
525 if (g_recsize < LS_TIME)
527 target->ls_time += current->ls_time;
528 if (g_recsize < LS_HIST)
530 for (j = 0; j < 64; j++)
531 target->ls_hist[j] += current->ls_hist[j];
536 coalesce_symbol(uintptr_t *addrp)
541 if (addr_to_sym(*addrp, &symoff, &symsize) != NULL && symoff < symsize)
546 predicate_add(char **pred, char *what, char *cmp, uintptr_t value)
560 newlen = len + strlen(what) + 32 + strlen("( && )");
561 new = malloc(newlen);
563 if (*pred[0] != '\0') {
565 (void) sprintf(new, "(%s) && (%s %s 0x%p)",
566 *pred, what, cmp, (void *)value);
568 (void) sprintf(new, "(%s) && (%s)", *pred, what);
572 (void) sprintf(new, "%s %s 0x%p",
573 what, cmp, (void *)value);
575 (void) sprintf(new, "%s", what);
584 predicate_destroy(char **pred)
591 filter_add(char **filt, char *what, uintptr_t base, uintptr_t size)
593 char buf[256], *c = buf, *new;
602 (void) sprintf(c, "%s(%s >= 0x%p && %s < 0x%p)", *filt[0] != '\0' ?
603 " || " : "", what, (void *)base, what, (void *)(base + size));
605 (void) sprintf(c, "%s(%s >= %p && %s < %p)", *filt[0] != '\0' ?
606 " || " : "", what, (void *)base, what, (void *)(base + size));
609 newlen = (len = strlen(*filt) + 1) + strlen(c);
610 new = malloc(newlen);
611 bcopy(*filt, new, len);
612 (void) strcat(new, c);
618 filter_destroy(char **filt)
625 dprog_add(const char *fmt, ...)
632 size = vsnprintf(&c, 1, fmt, args) + 1;
635 if (g_proglen == 0) {
638 offs = g_proglen - 1;
641 g_proglen = offs + size;
643 if ((g_prog = realloc(g_prog, g_proglen)) == NULL)
644 fail(1, "failed to reallocate program text");
647 (void) vsnprintf(&g_prog[offs], size, fmt, args);
652 * This function may read like an open sewer, but keep in mind that programs
653 * that generate other programs are rarely pretty. If one has the unenviable
654 * task of maintaining or -- worse -- extending this code, use the -V option
655 * to examine the D program as generated by this function.
658 dprog_addevent(int event)
660 ls_event_info_t *info = &g_event_info[event];
663 const char *arg0, *caller;
669 if (info->ev_name[0] == '\0')
672 if (info->ev_type == 'I') {
674 * For interrupt events, arg0 (normally the lock pointer) is
675 * the CPU address plus the current pil, and arg1 (normally
676 * the number of nanoseconds) is the number of nanoseconds
677 * late -- and it's stored in arg2.
680 arg0 = "(uintptr_t)curthread->t_cpu + \n"
681 "\t curthread->t_cpu->cpu_profile_pil";
683 arg0 = "(uintptr_t)(curthread->td_oncpu << 16) + \n"
684 "\t 0x01000000 + curthread->td_pri_class";
686 caller = "(uintptr_t)arg0";
689 arg0 = "(uintptr_t)arg0";
693 if (g_recsize > LS_HIST) {
694 for (depth = 0; g_recsize > LS_STACK(depth); depth++)
698 (void) sprintf(stack, "\tstack(%d);\n", depth);
700 (void) sprintf(stack, ", stack(%d)", depth);
703 (void) sprintf(stack, "");
706 if (info->ev_acquire != NULL) {
708 * If this is a hold event, we need to generate an additional
709 * clause for the acquire; the clause for the release will be
710 * generated with the aggregating statement, below.
712 dprog_add("%s\n", info->ev_acquire);
713 predicate_add(&pred, info->ev_predicate, NULL, 0);
714 predicate_add(&pred, g_predicate, NULL, 0);
716 dprog_add("/%s/\n", pred);
719 (void) sprintf(buf, "self->ev%d[(uintptr_t)arg0]", event);
721 if (info->ev_type == 'H') {
722 dprog_add("\t%s = timestamp;\n", buf);
725 * If this isn't a hold event, it's the recursive
726 * error event. For this, we simply bump the
727 * thread-local, per-lock count.
729 dprog_add("\t%s++;\n", buf);
733 predicate_destroy(&pred);
736 if (info->ev_type == 'E') {
738 * If this is the recursive lock error event, we need
739 * to generate an additional clause to decrement the
740 * thread-local, per-lock count. This assures that we
741 * only execute the aggregating clause if we have
744 dprog_add("%s\n", info->ev_name);
745 dprog_add("/%s/\n{\n\t%s--;\n}\n\n", buf, buf);
748 predicate_add(&pred, buf, NULL, 0);
750 if (info->ev_type == 'H') {
751 (void) sprintf(buf, "timestamp -\n\t "
752 "self->ev%d[(uintptr_t)arg0]", event);
757 predicate_add(&pred, info->ev_predicate, NULL, 0);
758 if (info->ev_type != 'I')
759 predicate_add(&pred, g_predicate, NULL, 0);
761 predicate_add(&pred, g_ipredicate, NULL, 0);
764 if ((dur = g_min_duration[event]) != 0)
765 predicate_add(&pred, arg1, ">=", dur);
767 dprog_add("%s\n", info->ev_name);
770 dprog_add("/%s/\n", pred);
771 predicate_destroy(&pred);
776 dprog_add("\ttrace(%dULL);\n", event);
777 dprog_add("\ttrace(%s);\n", arg0);
778 dprog_add("\ttrace(%s);\n", caller);
782 * The ordering here is important: when we process the
783 * aggregate, we count on the fact that @avg appears before
784 * @hist in program order to assure that @avg is assigned the
785 * first aggregation variable ID and @hist assigned the
786 * second; see the comment in process_aggregate() for details.
788 dprog_add("\t@avg[%dULL, %s, %s%s] = avg(%s);\n",
789 event, arg0, caller, stack, arg1);
791 if (g_recsize >= LS_HIST) {
792 dprog_add("\t@hist[%dULL, %s, %s%s] = quantize"
793 "(%s);\n", event, arg0, caller, stack, arg1);
797 if (info->ev_acquire != NULL)
798 dprog_add("\tself->ev%d[arg0] = 0;\n", event);
807 dtrace_proginfo_t info;
810 (void) fprintf(stderr, "lockstat: vvvv D program vvvv\n");
811 (void) fputs(g_prog, stderr);
812 (void) fprintf(stderr, "lockstat: ^^^^ D program ^^^^\n");
815 if ((prog = dtrace_program_strcompile(g_dtp, g_prog,
816 DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL)
817 dfail("failed to compile program");
819 if (dtrace_program_exec(g_dtp, prog, &info) == -1)
820 dfail("failed to enable probes");
822 if (dtrace_go(g_dtp) != 0)
823 dfail("couldn't start tracing");
837 dtrace_optval_t val, status, agg;
838 struct sigaction act;
839 struct itimerspec ts;
843 if (dtrace_getopt(g_dtp, "statusrate", &status) == -1)
844 dfail("failed to get 'statusrate'");
846 if (dtrace_getopt(g_dtp, "aggrate", &agg) == -1)
847 dfail("failed to get 'statusrate'");
850 * We would want to awaken at a rate that is the GCD of the statusrate
851 * and the aggrate -- but that seems a bit absurd. Instead, we'll
852 * simply awaken at a rate that is the more frequent of the two, which
853 * assures that we're never later than the interval implied by the
854 * more frequent rate.
856 val = status < agg ? status : agg;
858 (void) sigemptyset(&act.sa_mask);
860 act.sa_handler = status_fire;
861 (void) sigaction(SIGUSR1, &act, NULL);
863 ev.sigev_notify = SIGEV_SIGNAL;
864 ev.sigev_signo = SIGUSR1;
866 if (timer_create(CLOCK_REALTIME, &ev, &tid) == -1)
867 dfail("cannot create CLOCK_REALTIME timer");
869 ts.it_value.tv_sec = val / NANOSEC;
870 ts.it_value.tv_nsec = val % NANOSEC;
871 ts.it_interval = ts.it_value;
873 if (timer_settime(tid, TIMER_RELTIME, &ts, NULL) == -1)
874 dfail("cannot set time on CLOCK_REALTIME timer");
880 if (!g_tracing && dtrace_aggregate_snap(g_dtp) != 0)
881 dfail("failed to snap aggregate");
883 if (dtrace_status(g_dtp) == -1)
884 dfail("dtrace_status()");
888 lsrec_fill(lsrec_t *lsrec, const dtrace_recdesc_t *rec, int nrecs, caddr_t data)
890 bzero(lsrec, g_recsize);
893 if ((g_recsize > LS_HIST && nrecs < 4) || (nrecs < 3))
894 fail(0, "truncated DTrace record");
896 if (rec->dtrd_size != sizeof (uint64_t))
897 fail(0, "bad event size in first record");
899 /* LINTED - alignment */
900 lsrec->ls_event = (uint32_t)*((uint64_t *)(data + rec->dtrd_offset));
903 if (rec->dtrd_size != sizeof (uintptr_t))
904 fail(0, "bad lock address size in second record");
906 /* LINTED - alignment */
907 lsrec->ls_lock = *((uintptr_t *)(data + rec->dtrd_offset));
910 if (rec->dtrd_size != sizeof (uintptr_t))
911 fail(0, "bad caller size in third record");
913 /* LINTED - alignment */
914 lsrec->ls_caller = *((uintptr_t *)(data + rec->dtrd_offset));
917 if (g_recsize > LS_HIST) {
921 frames = rec->dtrd_size / sizeof (pc_t);
922 /* LINTED - alignment */
923 stack = (pc_t *)(data + rec->dtrd_offset);
925 for (i = 1; i < frames; i++)
926 lsrec->ls_stack[i - 1] = stack[i];
932 count_aggregate(const dtrace_aggdata_t *agg, void *arg)
934 *((size_t *)arg) += 1;
936 return (DTRACE_AGGWALK_NEXT);
940 process_aggregate(const dtrace_aggdata_t *agg, void *arg)
942 const dtrace_aggdesc_t *aggdesc = agg->dtada_desc;
943 caddr_t data = agg->dtada_data;
944 lsdata_t *lsdata = arg;
945 lsrec_t *lsrec = lsdata->lsd_next;
946 const dtrace_recdesc_t *rec;
947 uint64_t *avg, *quantized;
950 assert(lsdata->lsd_count < g_nrecs);
953 * Aggregation variable IDs are guaranteed to be generated in program
954 * order, and they are guaranteed to start from DTRACE_AGGVARIDNONE
955 * plus one. As "avg" appears before "hist" in program order, we know
956 * that "avg" will be allocated the first aggregation variable ID, and
957 * "hist" will be allocated the second aggregation variable ID -- and
958 * we therefore use the aggregation variable ID to differentiate the
961 if (aggdesc->dtagd_varid > DTRACE_AGGVARIDNONE + 1) {
963 * If this is the histogram entry. We'll copy the quantized
964 * data into lc_hist, and jump over the rest.
966 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
968 if (aggdesc->dtagd_varid != DTRACE_AGGVARIDNONE + 2)
969 fail(0, "bad variable ID in aggregation record");
971 if (rec->dtrd_size !=
972 DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
973 fail(0, "bad quantize size in aggregation record");
975 /* LINTED - alignment */
976 quantized = (uint64_t *)(data + rec->dtrd_offset);
978 for (i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0;
979 i < DTRACE_QUANTIZE_NBUCKETS; i++, j++)
980 lsrec->ls_hist[j] = quantized[i];
985 lsrec_fill(lsrec, &aggdesc->dtagd_rec[1],
986 aggdesc->dtagd_nrecs - 1, data);
988 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
990 if (rec->dtrd_size != 2 * sizeof (uint64_t))
991 fail(0, "bad avg size in aggregation record");
993 /* LINTED - alignment */
994 avg = (uint64_t *)(data + rec->dtrd_offset);
995 lsrec->ls_count = (uint32_t)avg[0];
996 lsrec->ls_time = (uintptr_t)avg[1];
998 if (g_recsize >= LS_HIST)
999 return (DTRACE_AGGWALK_NEXT);
1002 lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
1003 lsdata->lsd_count++;
1005 return (DTRACE_AGGWALK_NEXT);
1009 process_trace(const dtrace_probedata_t *pdata, void *arg)
1011 lsdata_t *lsdata = arg;
1012 lsrec_t *lsrec = lsdata->lsd_next;
1013 dtrace_eprobedesc_t *edesc = pdata->dtpda_edesc;
1014 caddr_t data = pdata->dtpda_data;
1016 if (lsdata->lsd_count >= g_nrecs)
1017 return (DTRACE_CONSUME_NEXT);
1019 lsrec_fill(lsrec, edesc->dtepd_rec, edesc->dtepd_nrecs, data);
1021 lsdata->lsd_next = (lsrec_t *)((uintptr_t)lsrec + g_recsize);
1022 lsdata->lsd_count++;
1024 return (DTRACE_CONSUME_NEXT);
1028 process_data(FILE *out, char *data)
1032 /* LINTED - alignment */
1033 lsdata.lsd_next = (lsrec_t *)data;
1034 lsdata.lsd_count = 0;
1037 if (dtrace_consume(g_dtp, out,
1038 process_trace, NULL, &lsdata) != 0)
1039 dfail("failed to consume buffer");
1041 return (lsdata.lsd_count);
1044 if (dtrace_aggregate_walk_keyvarsorted(g_dtp,
1045 process_aggregate, &lsdata) != 0)
1046 dfail("failed to walk aggregate");
1048 return (lsdata.lsd_count);
1053 drophandler(const dtrace_dropdata_t *data, void *arg)
1056 (void) fprintf(stderr, "lockstat: warning: %s", data->dtdda_msg);
1057 return (DTRACE_HANDLE_OK);
1061 main(int argc, char **argv)
1064 lsrec_t *lsp, **current, **first, **sort_buf, **merge_buf;
1071 char *addrp, *offp, *sizep, *evp, *lastp, *p;
1074 int events_specified = 0;
1077 char *filt = NULL, *ifilt = NULL;
1078 static uint64_t ev_count[LS_MAX_EVENTS + 1];
1079 static uint64_t ev_time[LS_MAX_EVENTS + 1];
1080 dtrace_optval_t aggsize;
1086 if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
1087 fail(0, "cannot open dtrace library: %s",
1088 dtrace_errmsg(NULL, err));
1091 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1092 dfail("couldn't establish drop handler");
1094 if (symtab_init() == -1)
1095 fail(1, "can't load kernel symbols");
1097 g_nrecs = DEFAULT_NRECS;
1099 while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != GETOPT_EOF) {
1102 g_recsize = LS_BASIC;
1106 g_recsize = LS_TIME;
1110 g_recsize = LS_HIST;
1114 if (!isdigit(optarg[0]))
1116 g_stkdepth = atoi(optarg);
1117 if (g_stkdepth > LS_MAX_STACK_DEPTH)
1118 fail(0, "max stack depth is %d",
1119 LS_MAX_STACK_DEPTH);
1120 g_recsize = LS_STACK(g_stkdepth);
1124 if (!isdigit(optarg[0]))
1126 g_nrecs = atoi(optarg);
1130 if (!isdigit(optarg[0]))
1132 duration = atoll(optarg);
1135 * XXX -- durations really should be per event
1136 * since the units are different, but it's hard
1137 * to express this nicely in the interface.
1138 * Not clear yet what the cleanest solution is.
1140 for (i = 0; i < LS_MAX_EVENTS; i++)
1141 if (g_event_info[i].ev_type != 'E')
1142 g_min_duration[i] = duration;
1147 if (!isdigit(optarg[0]))
1153 fail(0, "max interrupt rate is %d Hz", MAX_HZ);
1155 for (j = 0; j < LS_MAX_EVENTS; j++)
1156 if (strcmp(g_event_info[j].ev_desc,
1157 "Profiling interrupt") == 0)
1160 (void) sprintf(g_event_info[j].ev_name,
1161 "profile:::profile-%d", i);
1166 addrp = strtok(optarg, ",");
1167 sizep = strtok(NULL, ",");
1168 addrp = strtok(optarg, ",+");
1169 offp = strtok(NULL, ",");
1171 size = sizep ? strtoul(sizep, NULL, 0) : 1;
1172 off = offp ? strtoul(offp, NULL, 0) : 0;
1174 if (addrp[0] == '0') {
1175 addr = strtoul(addrp, NULL, 16) + off;
1177 addr = sym_to_addr(addrp) + off;
1179 size = sym_size(addrp) - off;
1180 if (addr - off == 0)
1181 fail(0, "symbol '%s' not found", addrp);
1188 filter_add(&filt, "arg0", addr, size);
1190 filter_add(&filt, "caller", addr, size);
1191 filter_add(&ifilt, "arg0", addr, size);
1196 evp = strtok_r(optarg, ",", &lastp);
1201 (void) strtok(evp, "-");
1202 evp2 = strtok(NULL, "-");
1204 ev2 = evp2 ? atoi(evp2) : ev1;
1205 if ((uint_t)ev1 >= LS_MAX_EVENTS ||
1206 (uint_t)ev2 >= LS_MAX_EVENTS || ev1 > ev2)
1207 fail(0, "-e events out of range");
1208 for (i = ev1; i <= ev2; i++)
1210 evp = strtok_r(NULL, ",", &lastp);
1212 events_specified = 1;
1239 for (i = 0; i < LS_MAX_EVENTS; i++)
1240 if (g_event_info[i].ev_type == c)
1242 events_specified = 1;
1246 for (i = 0; i < LS_MAX_EVENTS; i++)
1247 if (strchr("CH", g_event_info[i].ev_type))
1249 events_specified = 1;
1257 if (!isdigit(optarg[0]))
1259 g_topn = atoi(optarg);
1275 if ((out = fopen(optarg, "w")) == NULL)
1276 fail(1, "error opening file");
1284 if (strchr(LOCKSTAT_OPTSTR, c) == NULL)
1290 predicate_add(&g_predicate, filt, NULL, 0);
1291 filter_destroy(&filt);
1294 if (ifilt != NULL) {
1295 predicate_add(&g_ipredicate, ifilt, NULL, 0);
1296 filter_destroy(&ifilt);
1299 if (g_recsize == 0) {
1301 g_stkdepth = LS_MAX_STACK_DEPTH;
1302 g_recsize = LS_STACK(g_stkdepth);
1304 g_recsize = LS_TIME;
1308 if (g_gflag && g_recsize <= LS_STACK(0))
1309 fail(0, "'-g' requires at least '-s 1' data gathering");
1312 * Make sure the alignment is reasonable
1314 g_recsize = -(-g_recsize & -sizeof (uint64_t));
1316 for (i = 0; i < LS_MAX_EVENTS; i++) {
1318 * If no events were specified, enable -C.
1320 if (!events_specified && g_event_info[i].ev_type == 'C')
1324 for (i = 0; i < LS_MAX_EVENTS; i++) {
1328 if (g_event_info[i].ev_acquire != NULL) {
1330 * If we've enabled a hold event, we must explicitly
1331 * allocate dynamic variable space.
1340 * Make sure there are remaining arguments to specify a child command
1346 if ((ncpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
1347 dfail("couldn't determine number of online CPUs");
1350 * By default, we set our data buffer size to be the number of records
1351 * multiplied by the size of the record, doubled to account for some
1352 * DTrace slop and divided by the number of CPUs. We silently clamp
1353 * the aggregation size at both a minimum and a maximum to prevent
1354 * absurdly low or high values.
1356 if ((aggsize = (g_nrecs * g_recsize * 2) / ncpus) < MIN_AGGSIZE)
1357 aggsize = MIN_AGGSIZE;
1359 if (aggsize > MAX_AGGSIZE)
1360 aggsize = MAX_AGGSIZE;
1362 (void) sprintf(aggstr, "%lld", (long long)aggsize);
1365 if (dtrace_setopt(g_dtp, "bufsize", "4k") == -1)
1366 dfail("failed to set 'bufsize'");
1368 if (dtrace_setopt(g_dtp, "aggsize", aggstr) == -1)
1369 dfail("failed to set 'aggsize'");
1373 * If we're using dynamic variables, we set our
1374 * dynamic variable size to be one megabyte per CPU,
1375 * with a hard-limit of 32 megabytes. This may still
1376 * be too small in some cases, but it can be tuned
1377 * manually via -x if need be.
1379 (void) sprintf(aggstr, "%ldm", ncpus < 32 ? ncpus : 32);
1381 if (dtrace_setopt(g_dtp, "dynvarsize", aggstr) == -1)
1382 dfail("failed to set 'dynvarsize'");
1385 if (dtrace_setopt(g_dtp, "bufsize", aggstr) == -1)
1386 dfail("failed to set 'bufsize'");
1389 if (dtrace_setopt(g_dtp, "statusrate", "10sec") == -1)
1390 dfail("failed to set 'statusrate'");
1393 while ((c = getopt(argc, argv, LOCKSTAT_OPTSTR)) != GETOPT_EOF) {
1396 if ((p = strchr(optarg, '=')) != NULL)
1399 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1400 dfail("failed to set -x %s", optarg);
1411 g_elapsed = -gethrtime();
1414 * Spawn the specified command and wait for it to complete.
1418 fail(1, "cannot fork");
1420 (void) dtrace_close(g_dtp);
1421 (void) execvp(argv[0], &argv[0]);
1427 while (waitpid(child, &status, WEXITED) != child)
1429 while (waitpid(child, &status, 0) != child)
1433 g_elapsed += gethrtime();
1435 if (WIFEXITED(status)) {
1436 if (WEXITSTATUS(status) != 0) {
1437 if (exec_errno != 0) {
1439 fail(1, "could not execute %s", argv[0]);
1441 (void) fprintf(stderr,
1442 "lockstat: warning: %s exited with code %d\n",
1443 argv[0], WEXITSTATUS(status));
1446 (void) fprintf(stderr,
1447 "lockstat: warning: %s died on signal %d\n",
1448 argv[0], WTERMSIG(status));
1451 if (dtrace_stop(g_dtp) == -1)
1452 dfail("failed to stop dtrace");
1455 * Before we read out the results, we need to allocate our buffer.
1456 * If we're tracing, then we'll just use the precalculated size. If
1457 * we're not, then we'll take a snapshot of the aggregate, and walk
1458 * it to count the number of records.
1461 if (dtrace_aggregate_snap(g_dtp) != 0)
1462 dfail("failed to snap aggregate");
1466 if (dtrace_aggregate_walk(g_dtp,
1467 count_aggregate, &g_nrecs) != 0)
1468 dfail("failed to walk aggregate");
1472 if ((data_buf = memalign(sizeof (uint64_t),
1473 (g_nrecs + 1) * g_recsize)) == NULL)
1475 if (posix_memalign((void **)&data_buf, sizeof (uint64_t),
1476 (g_nrecs + 1) * g_recsize) )
1478 fail(1, "Memory allocation failed");
1481 * Read out the DTrace data.
1483 g_nrecs_used = process_data(out, data_buf);
1485 if (g_nrecs_used > g_nrecs || g_dropped)
1486 (void) fprintf(stderr, "lockstat: warning: "
1487 "ran out of data records (use -n for more)\n");
1489 /* LINTED - alignment */
1490 for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1491 /* LINTED - alignment */
1492 lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
1493 ev_count[lsp->ls_event] += lsp->ls_count;
1494 ev_time[lsp->ls_event] += lsp->ls_time;
1498 * If -g was specified, convert stacks into individual records.
1501 lsrec_t *newlsp, *oldlsp;
1504 newlsp = memalign(sizeof (uint64_t),
1505 g_nrecs_used * LS_TIME * (g_stkdepth + 1));
1507 posix_memalign((void **)&newlsp, sizeof (uint64_t),
1508 g_nrecs_used * LS_TIME * (g_stkdepth + 1));
1511 fail(1, "Cannot allocate space for -g processing");
1513 /* LINTED - alignment */
1514 for (i = 0, oldlsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1515 /* LINTED - alignment */
1516 oldlsp = (lsrec_t *)((char *)oldlsp + g_recsize)) {
1518 int caller_in_stack = 0;
1520 if (oldlsp->ls_count == 0)
1523 for (fr = 0; fr < g_stkdepth; fr++) {
1524 if (oldlsp->ls_stack[fr] == 0)
1526 if (oldlsp->ls_stack[fr] == oldlsp->ls_caller)
1527 caller_in_stack = 1;
1528 bcopy(oldlsp, lsp, LS_TIME);
1529 lsp->ls_caller = oldlsp->ls_stack[fr];
1530 /* LINTED - alignment */
1531 lsp = (lsrec_t *)((char *)lsp + LS_TIME);
1533 if (!caller_in_stack) {
1534 bcopy(oldlsp, lsp, LS_TIME);
1535 /* LINTED - alignment */
1536 lsp = (lsrec_t *)((char *)lsp + LS_TIME);
1539 g_nrecs = g_nrecs_used =
1540 ((uintptr_t)lsp - (uintptr_t)newlsp) / LS_TIME;
1541 g_recsize = LS_TIME;
1544 data_buf = (char *)newlsp;
1547 if ((sort_buf = calloc(2 * (g_nrecs + 1),
1548 sizeof (void *))) == NULL)
1549 fail(1, "Sort buffer allocation failed");
1550 merge_buf = sort_buf + (g_nrecs + 1);
1553 * Build the sort buffer, discarding zero-count records along the way.
1555 /* LINTED - alignment */
1556 for (i = 0, lsp = (lsrec_t *)data_buf; i < g_nrecs_used; i++,
1557 /* LINTED - alignment */
1558 lsp = (lsrec_t *)((char *)lsp + g_recsize)) {
1559 if (lsp->ls_count == 0)
1560 lsp->ls_event = LS_MAX_EVENTS;
1564 if (g_nrecs_used == 0)
1568 * Add a sentinel after the last record
1571 lsp->ls_event = LS_MAX_EVENTS;
1574 report_trace(out, sort_buf);
1579 * Application of -g may have resulted in multiple records
1580 * with the same signature; coalesce them.
1583 mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
1584 coalesce(lockcmp, sort_buf, g_nrecs_used);
1588 * Coalesce locks within the same symbol if -c option specified.
1589 * Coalesce PCs within the same function if -k option specified.
1591 if (g_cflag || g_kflag) {
1592 for (i = 0; i < g_nrecs_used; i++) {
1596 coalesce_symbol(&lsp->ls_lock);
1598 for (fr = 0; fr < g_stkdepth; fr++)
1599 coalesce_symbol(&lsp->ls_stack[fr]);
1600 coalesce_symbol(&lsp->ls_caller);
1603 mergesort(lockcmp, sort_buf, merge_buf, g_nrecs_used);
1604 coalesce(lockcmp, sort_buf, g_nrecs_used);
1608 * Coalesce callers if -w option specified
1611 mergesort(lock_and_count_cmp_anywhere,
1612 sort_buf, merge_buf, g_nrecs_used);
1613 coalesce(lockcmp_anywhere, sort_buf, g_nrecs_used);
1617 * Coalesce locks if -W option specified
1620 mergesort(site_and_count_cmp_anylock,
1621 sort_buf, merge_buf, g_nrecs_used);
1622 coalesce(sitecmp_anylock, sort_buf, g_nrecs_used);
1626 * Sort data by contention count (ls_count) or total time (ls_time),
1627 * depending on g_Pflag. Override g_Pflag if time wasn't measured.
1629 if (g_recsize < LS_TIME)
1633 mergesort(timecmp, sort_buf, merge_buf, g_nrecs_used);
1635 mergesort(countcmp, sort_buf, merge_buf, g_nrecs_used);
1638 * Display data by event type
1640 first = &sort_buf[0];
1641 while ((event = (*first)->ls_event) < LS_MAX_EVENTS) {
1643 while ((lsp = *current)->ls_event == event)
1645 report_stats(out, first, current - first, ev_count[event],
1654 format_symbol(char *buf, uintptr_t addr, int show_size)
1660 symname = addr_to_sym(addr, &symoff, &symsize);
1662 if (show_size && symoff == 0)
1663 (void) sprintf(buf, "%s[%ld]", symname, (long)symsize);
1664 else if (symoff == 0)
1665 (void) sprintf(buf, "%s", symname);
1666 else if (symoff < 16 && bcmp(symname, "cpu[", 4) == 0) /* CPU+PIL */
1668 (void) sprintf(buf, "%s+%ld", symname, (long)symoff);
1670 (void) sprintf(buf, "%s+%s", symname, g_pri_class[(int)symoff]);
1672 else if (symoff <= symsize || (symoff < 256 && addr != symoff))
1673 (void) sprintf(buf, "%s+0x%llx", symname,
1674 (unsigned long long)symoff);
1676 (void) sprintf(buf, "0x%llx", (unsigned long long)addr);
1681 report_stats(FILE *out, lsrec_t **sort_buf, size_t nrecs, uint64_t total_count,
1682 uint64_t total_time)
1684 uint32_t event = sort_buf[0]->ls_event;
1686 double ptotal = 0.0;
1690 int first_bin, last_bin, max_bin_count, total_bin_count;
1693 char lhdr[80], chdr[80];
1695 rectype = g_recsize;
1698 (void) fprintf(out, "%20llu %s\n",
1699 g_rates == 0 ? total_count :
1700 ((unsigned long long)total_count * NANOSEC) / g_elapsed,
1701 g_event_info[event].ev_desc);
1705 (void) sprintf(lhdr, "%s%s",
1706 g_Wflag ? "Hottest " : "", g_event_info[event].ev_lhdr);
1707 (void) sprintf(chdr, "%s%s",
1708 g_wflag ? "Hottest " : "", "Caller");
1712 "\n%s: %.0f events in %.3f seconds (%.0f events/sec)\n\n",
1713 g_event_info[event].ev_desc, (double)total_count,
1714 (double)g_elapsed / NANOSEC,
1715 (double)total_count * NANOSEC / g_elapsed);
1717 if (!g_pflag && rectype < LS_HIST) {
1718 (void) sprintf(buf, "%s", g_event_info[event].ev_units);
1719 (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
1720 g_rates ? "ops/s" : "Count",
1721 g_gflag ? "genr" : "indv",
1722 "cuml", "rcnt", rectype >= LS_TIME ? buf : "", lhdr, chdr);
1723 (void) fprintf(out, "---------------------------------"
1724 "----------------------------------------------\n");
1728 for (i = 0; i < nrecs; i++) {
1731 if (displayed++ >= g_topn)
1737 (void) fprintf(out, "%u %u",
1738 lsp->ls_event, lsp->ls_count);
1739 (void) fprintf(out, " %s",
1740 format_symbol(buf, lsp->ls_lock, g_cflag));
1741 (void) fprintf(out, " %s",
1742 format_symbol(buf, lsp->ls_caller, 0));
1743 (void) fprintf(out, " %f",
1744 (double)lsp->ls_refcnt / lsp->ls_count);
1745 if (rectype >= LS_TIME)
1746 (void) fprintf(out, " %llu",
1747 (unsigned long long)lsp->ls_time);
1748 if (rectype >= LS_HIST) {
1749 for (j = 0; j < 64; j++)
1750 (void) fprintf(out, " %u",
1753 for (j = 0; j < LS_MAX_STACK_DEPTH; j++) {
1754 if (rectype <= LS_STACK(j) ||
1755 lsp->ls_stack[j] == 0)
1757 (void) fprintf(out, " %s",
1758 format_symbol(buf, lsp->ls_stack[j], 0));
1760 (void) fprintf(out, "\n");
1764 if (rectype >= LS_HIST) {
1765 (void) fprintf(out, "---------------------------------"
1766 "----------------------------------------------\n");
1767 (void) sprintf(buf, "%s",
1768 g_event_info[event].ev_units);
1769 (void) fprintf(out, "%5s %4s %4s %4s %8s %-22s %-24s\n",
1770 g_rates ? "ops/s" : "Count",
1771 g_gflag ? "genr" : "indv",
1772 "cuml", "rcnt", buf, lhdr, chdr);
1775 if (g_Pflag && total_time != 0)
1776 percent = (lsp->ls_time * 100.00) / total_time;
1778 percent = (lsp->ls_count * 100.00) / total_count;
1782 if (rectype >= LS_TIME)
1783 (void) sprintf(buf, "%llu",
1784 (unsigned long long)(lsp->ls_time / lsp->ls_count));
1788 (void) fprintf(out, "%5llu ",
1789 g_rates == 0 ? lsp->ls_count :
1790 ((uint64_t)lsp->ls_count * NANOSEC) / g_elapsed);
1792 (void) fprintf(out, "%3.0f%% ", percent);
1795 (void) fprintf(out, "---- ");
1797 (void) fprintf(out, "%3.0f%% ", ptotal);
1799 (void) fprintf(out, "%4.2f %8s ",
1800 (double)lsp->ls_refcnt / lsp->ls_count, buf);
1802 (void) fprintf(out, "%-22s ",
1803 format_symbol(buf, lsp->ls_lock, g_cflag));
1805 (void) fprintf(out, "%-24s\n",
1806 format_symbol(buf, lsp->ls_caller, 0));
1808 if (rectype < LS_HIST)
1811 (void) fprintf(out, "\n");
1812 (void) fprintf(out, "%10s %31s %-9s %-24s\n",
1813 g_event_info[event].ev_units,
1814 "------ Time Distribution ------",
1815 g_rates ? "ops/s" : "count",
1816 rectype > LS_STACK(0) ? "Stack" : "");
1819 while (lsp->ls_hist[first_bin] == 0)
1823 while (lsp->ls_hist[last_bin] == 0)
1827 total_bin_count = 0;
1828 for (j = first_bin; j <= last_bin; j++) {
1829 total_bin_count += lsp->ls_hist[j];
1830 if (lsp->ls_hist[j] > max_bin_count)
1831 max_bin_count = lsp->ls_hist[j];
1835 * If we went a few frames below the caller, ignore them
1837 for (fr = 3; fr > 0; fr--)
1838 if (lsp->ls_stack[fr] == lsp->ls_caller)
1841 for (j = first_bin; j <= last_bin; j++) {
1842 uint_t depth = (lsp->ls_hist[j] * 30) / total_bin_count;
1843 (void) fprintf(out, "%10llu |%s%s %-9u ",
1845 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + 30 - depth,
1847 g_rates == 0 ? lsp->ls_hist[j] :
1848 (uint_t)(((uint64_t)lsp->ls_hist[j] * NANOSEC) /
1850 if (rectype <= LS_STACK(fr) || lsp->ls_stack[fr] == 0) {
1851 (void) fprintf(out, "\n");
1854 (void) fprintf(out, "%-24s\n",
1855 format_symbol(buf, lsp->ls_stack[fr], 0));
1858 while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
1859 (void) fprintf(out, "%15s %-36s %-24s\n", "", "",
1860 format_symbol(buf, lsp->ls_stack[fr], 0));
1866 (void) fprintf(out, "---------------------------------"
1867 "----------------------------------------------\n");
1873 report_trace(FILE *out, lsrec_t **sort_buf)
1878 char buf[256], buf2[256];
1880 rectype = g_recsize;
1883 (void) fprintf(out, "%5s %7s %11s %-24s %-24s\n",
1884 "Event", "Time", "Owner", "Lock", "Caller");
1885 (void) fprintf(out, "---------------------------------"
1886 "----------------------------------------------\n");
1889 for (i = 0; i < g_nrecs_used; i++) {
1893 if (lsp->ls_event >= LS_MAX_EVENTS || lsp->ls_count == 0)
1896 (void) fprintf(out, "%2d %10llu %11p %-24s %-24s\n",
1897 lsp->ls_event, (unsigned long long)lsp->ls_time,
1898 (void *)lsp->ls_next,
1899 format_symbol(buf, lsp->ls_lock, 0),
1900 format_symbol(buf2, lsp->ls_caller, 0));
1902 if (rectype <= LS_STACK(0))
1906 * If we went a few frames below the caller, ignore them
1908 for (fr = 3; fr > 0; fr--)
1909 if (lsp->ls_stack[fr] == lsp->ls_caller)
1912 while (rectype > LS_STACK(fr) && lsp->ls_stack[fr] != 0) {
1913 (void) fprintf(out, "%53s %-24s\n", "",
1914 format_symbol(buf, lsp->ls_stack[fr], 0));
1917 (void) fprintf(out, "\n");