Home | History | Annotate | Line # | Download | only in lockstat
main.c revision 1.13
      1 /*	$NetBSD: main.c,v 1.13 2008/04/28 15:36:01 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * TODO:
     41  *
     42  * - Tracking of times for sleep locks is broken.
     43  * - Need better analysis and tracking of events.
     44  * - Shouldn't have to parse the namelist here.  We should use something like
     45  *   FreeBSD's libelf.
     46  * - The way the namelist is searched sucks, is it worth doing something
     47  *   better?
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 #ifndef lint
     52 __RCSID("$NetBSD: main.c,v 1.13 2008/04/28 15:36:01 ad Exp $");
     53 #endif /* not lint */
     54 
     55 #include <sys/types.h>
     56 #include <sys/param.h>
     57 #include <sys/time.h>
     58 #include <sys/fcntl.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/wait.h>
     61 #include <sys/signal.h>
     62 #include <sys/sysctl.h>
     63 
     64 #include <dev/lockstat.h>
     65 
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <limits.h>
     70 #include <unistd.h>
     71 #include <err.h>
     72 #include <paths.h>
     73 #include <util.h>
     74 #include <ctype.h>
     75 #include <errno.h>
     76 #include <stdbool.h>
     77 
     78 #include "extern.h"
     79 
     80 #define	_PATH_DEV_LOCKSTAT	"/dev/lockstat"
     81 
     82 #define	MILLI	1000.0
     83 #define	MICRO	1000000.0
     84 #define	NANO	1000000000.0
     85 #define	PICO	1000000000000.0
     86 
     87 TAILQ_HEAD(lock_head, lockstruct);
     88 typedef struct lock_head locklist_t;
     89 TAILQ_HEAD(buf_head, lsbuf);
     90 typedef struct buf_head buflist_t;
     91 
     92 typedef struct lockstruct {
     93 	TAILQ_ENTRY(lockstruct)	chain;
     94 	buflist_t		bufs;
     95 	buflist_t		tosort;
     96 	uintptr_t		lock;
     97  	double			time;
     98 	uint32_t		count;
     99 	u_int			flags;
    100 	u_int			nbufs;
    101 	char			name[NAME_SIZE];
    102 } lock_t;
    103 
    104 typedef struct name {
    105 	const char	*name;
    106 	int		mask;
    107 } name_t;
    108 
    109 const name_t locknames[] = {
    110 	{ "adaptive_mutex", LB_ADAPTIVE_MUTEX },
    111 	{ "spin_mutex", LB_SPIN_MUTEX },
    112 	{ "rwlock", LB_RWLOCK },
    113 	{ "kernel_lock", LB_KERNEL_LOCK },
    114 	{ "preemption", LB_NOPREEMPT },
    115 	{ NULL, 0 }
    116 };
    117 
    118 const name_t eventnames[] = {
    119 	{ "spin", LB_SPIN },
    120 	{ "sleep_exclusive", LB_SLEEP1 },
    121 	{ "sleep_shared", LB_SLEEP2 },
    122 	{ NULL, 0 },
    123 };
    124 
    125 const name_t alltypes[] = {
    126 	{ "Adaptive mutex spin", LB_ADAPTIVE_MUTEX | LB_SPIN },
    127 	{ "Adaptive mutex sleep", LB_ADAPTIVE_MUTEX | LB_SLEEP1 },
    128 	{ "Spin mutex spin", LB_SPIN_MUTEX | LB_SPIN },
    129 	{ "RW lock sleep (writer)", LB_RWLOCK | LB_SLEEP1 },
    130 	{ "RW lock sleep (reader)", LB_RWLOCK | LB_SLEEP2 },
    131 	{ "RW lock spin", LB_RWLOCK | LB_SPIN },
    132 	{ "Kernel lock spin", LB_KERNEL_LOCK | LB_SPIN },
    133 	{ "Kernel preemption defer", LB_NOPREEMPT | LB_SPIN },
    134 	{ NULL, 0 }
    135 };
    136 
    137 locklist_t	locklist;
    138 locklist_t	freelist;
    139 locklist_t	sortlist;
    140 
    141 lsbuf_t		*bufs;
    142 lsdisable_t	ld;
    143 bool		lflag;
    144 bool		fflag;
    145 int		nbufs;
    146 bool		cflag;
    147 int		lsfd;
    148 int		displayed;
    149 int		bin64;
    150 double		tscale;
    151 double		cscale;
    152 double		cpuscale[sizeof(ld.ld_freq) / sizeof(ld.ld_freq[0])];
    153 FILE		*outfp;
    154 
    155 void	findsym(findsym_t, char *, uintptr_t *, uintptr_t *, bool);
    156 void	spawn(int, char **);
    157 void	display(int, const char *name);
    158 void	listnames(const name_t *);
    159 void	collapse(bool, bool);
    160 int	matchname(const name_t *, char *);
    161 void	makelists(int, int);
    162 void	nullsig(int);
    163 void	usage(void);
    164 int	ncpu(void);
    165 lock_t	*morelocks(void);
    166 
    167 int
    168 main(int argc, char **argv)
    169 {
    170 	int eventtype, locktype, ch, nlfd, fd, i;
    171 	bool sflag, pflag, mflag, Mflag;
    172 	const char *nlistf, *outf;
    173 	char *lockname, *funcname;
    174 	const name_t *name;
    175 	lsenable_t le;
    176 	double ms;
    177 	char *p;
    178 
    179 	nlistf = NULL;
    180 	outf = NULL;
    181 	lockname = NULL;
    182 	funcname = NULL;
    183 	eventtype = -1;
    184 	locktype = -1;
    185 	nbufs = 0;
    186 	sflag = false;
    187 	pflag = false;
    188 	mflag = false;
    189 	Mflag = false;
    190 
    191 	while ((ch = getopt(argc, argv, "E:F:L:MN:T:b:ceflmo:pst")) != -1)
    192 		switch (ch) {
    193 		case 'E':
    194 			eventtype = matchname(eventnames, optarg);
    195 			break;
    196 		case 'F':
    197 			funcname = optarg;
    198 			break;
    199 		case 'L':
    200 			lockname = optarg;
    201 			break;
    202 		case 'N':
    203 			nlistf = optarg;
    204 			break;
    205 		case 'T':
    206 			locktype = matchname(locknames, optarg);
    207 			break;
    208 		case 'b':
    209 			nbufs = (int)strtol(optarg, &p, 0);
    210 			if (!isdigit((u_int)*optarg) || *p != '\0')
    211 				usage();
    212 			break;
    213 		case 'c':
    214 			cflag = true;
    215 			break;
    216 		case 'e':
    217 			listnames(eventnames);
    218 			break;
    219 		case 'f':
    220 			fflag = true;
    221 			break;
    222 		case 'l':
    223 			lflag = true;
    224 			break;
    225 		case 'm':
    226 			mflag = true;
    227 			break;
    228 		case 'M':
    229 			Mflag = true;
    230 			break;
    231 		case 'o':
    232 			outf = optarg;
    233 			break;
    234 		case 'p':
    235 			pflag = true;
    236 			break;
    237 		case 's':
    238 			sflag = true;
    239 			break;
    240 		case 't':
    241 			listnames(locknames);
    242 			break;
    243 		default:
    244 			usage();
    245 		}
    246 	argc -= optind;
    247 	argv += optind;
    248 
    249 	if (*argv == NULL)
    250 		usage();
    251 
    252 	if (outf) {
    253 		fd = open(outf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    254 		if (fd == -1)
    255 			err(EXIT_FAILURE, "opening %s", outf);
    256 		outfp = fdopen(fd, "w");
    257 	} else
    258 		outfp = stdout;
    259 
    260 	/*
    261 	 * Find the name list for resolving symbol names, and load it into
    262 	 * memory.
    263 	 */
    264 	if (nlistf == NULL) {
    265 		nlfd = open(_PATH_KSYMS, O_RDONLY);
    266 		nlistf = getbootfile();
    267 	} else
    268 		nlfd = -1;
    269 	if (nlfd == -1) {
    270 		if ((nlfd = open(nlistf, O_RDONLY)) < 0)
    271 			err(EXIT_FAILURE, "cannot open " _PATH_KSYMS " or %s",
    272 			    nlistf);
    273 	}
    274 	if (loadsym32(nlfd) != 0) {
    275 		if (loadsym64(nlfd) != 0)
    276 			errx(EXIT_FAILURE, "unable to load symbol table");
    277 		bin64 = 1;
    278 	}
    279 	close(nlfd);
    280 
    281 	memset(&le, 0, sizeof(le));
    282 	le.le_nbufs = nbufs;
    283 
    284 	/*
    285 	 * Set up initial filtering.
    286 	 */
    287 	if (lockname != NULL) {
    288 		findsym(LOCK_BYNAME, lockname, &le.le_lockstart,
    289 		    &le.le_lockend, true);
    290 		le.le_flags |= LE_ONE_LOCK;
    291 	}
    292 	if (!lflag)
    293 		le.le_flags |= LE_CALLSITE;
    294 	if (!fflag)
    295 		le.le_flags |= LE_LOCK;
    296 	if (funcname != NULL) {
    297 		if (lflag)
    298 			usage();
    299 		findsym(FUNC_BYNAME, funcname, &le.le_csstart, &le.le_csend, true);
    300 		le.le_flags |= LE_ONE_CALLSITE;
    301 	}
    302 	le.le_mask = (eventtype & LB_EVENT_MASK) | (locktype & LB_LOCK_MASK);
    303 
    304 	/*
    305 	 * Start tracing.
    306 	 */
    307 	if ((lsfd = open(_PATH_DEV_LOCKSTAT, O_RDONLY)) < 0)
    308 		err(EXIT_FAILURE, "cannot open " _PATH_DEV_LOCKSTAT);
    309 	if (ioctl(lsfd, IOC_LOCKSTAT_GVERSION, &ch) < 0)
    310 		err(EXIT_FAILURE, "ioctl");
    311 	if (ch != LS_VERSION)
    312 		errx(EXIT_FAILURE,
    313 		    "incompatible lockstat interface version (%d, kernel %d)",
    314 			LS_VERSION, ch);
    315 	if (ioctl(lsfd, IOC_LOCKSTAT_ENABLE, &le))
    316 		err(EXIT_FAILURE, "cannot enable tracing");
    317 
    318 	/*
    319 	 * Execute the traced program.
    320 	 */
    321 	spawn(argc, argv);
    322 
    323 	/*
    324 	 * Stop tracing, and read the trace buffers from the kernel.
    325 	 */
    326 	if (ioctl(lsfd, IOC_LOCKSTAT_DISABLE, &ld) == -1) {
    327 		if (errno == EOVERFLOW) {
    328 			warnx("overflowed available kernel trace buffers");
    329 			exit(EXIT_FAILURE);
    330 		}
    331 		err(EXIT_FAILURE, "cannot disable tracing");
    332 	}
    333 	if ((bufs = malloc(ld.ld_size)) == NULL)
    334 		err(EXIT_FAILURE, "cannot allocate memory for user buffers");
    335 	if (read(lsfd, bufs, ld.ld_size) != ld.ld_size)
    336 		err(EXIT_FAILURE, "reading from " _PATH_DEV_LOCKSTAT);
    337 	if (close(lsfd))
    338 		err(EXIT_FAILURE, "close(" _PATH_DEV_LOCKSTAT ")");
    339 
    340 	/*
    341 	 * Figure out how to scale the results.  For internal use we convert
    342 	 * all times from CPU frequency based to picoseconds, and values are
    343 	 * eventually displayed in ms.
    344 	 */
    345 	for (i = 0; i < sizeof(ld.ld_freq) / sizeof(ld.ld_freq[0]); i++)
    346 		if (ld.ld_freq[i] != 0)
    347 			cpuscale[i] = PICO / ld.ld_freq[i];
    348 	ms = ld.ld_time.tv_sec * MILLI + ld.ld_time.tv_nsec / MICRO;
    349 	if (pflag)
    350 		cscale = 1.0 / ncpu();
    351 	else
    352 		cscale = 1.0;
    353 	cscale *= (sflag ? MILLI / ms : 1.0);
    354 	tscale = cscale / NANO;
    355 	nbufs = (int)(ld.ld_size / sizeof(lsbuf_t));
    356 
    357 	TAILQ_INIT(&locklist);
    358 	TAILQ_INIT(&sortlist);
    359 	TAILQ_INIT(&freelist);
    360 
    361 	if ((mflag | Mflag) != 0)
    362 		collapse(mflag, Mflag);
    363 
    364 	/*
    365 	 * Display the results.
    366 	 */
    367 	fprintf(outfp, "Elapsed time: %.2f seconds.", ms / MILLI);
    368 	if (sflag || pflag) {
    369 		fprintf(outfp, " Displaying ");
    370 		if (pflag)
    371 			fprintf(outfp, "per-CPU ");
    372 		if (sflag)
    373 			fprintf(outfp, "per-second ");
    374 		fprintf(outfp, "averages.");
    375 	}
    376 	putc('\n', outfp);
    377 
    378 	for (name = alltypes; name->name != NULL; name++) {
    379 		if (eventtype != -1 &&
    380 		    (name->mask & LB_EVENT_MASK) != eventtype)
    381 			continue;
    382 		if (locktype != -1 &&
    383 		    (name->mask & LB_LOCK_MASK) != locktype)
    384 			continue;
    385 
    386 		display(name->mask, name->name);
    387 	}
    388 
    389 	if (displayed == 0)
    390 		fprintf(outfp, "None of the selected events were recorded.\n");
    391 	exit(EXIT_SUCCESS);
    392 }
    393 
    394 void
    395 usage(void)
    396 {
    397 
    398 	fprintf(stderr,
    399 	    "%s: usage:\n"
    400 	    "%s [options] <command>\n\n"
    401 	    "-b nbuf\t\tset number of event buffers to allocate\n"
    402 	    "-c\t\treport percentage of total events by count, not time\n"
    403 	    "-E event\t\tdisplay only one type of event\n"
    404 	    "-e\t\tlist event types\n"
    405 	    "-F func\t\tlimit trace to one function\n"
    406 	    "-f\t\ttrace only by function\n"
    407 	    "-L lock\t\tlimit trace to one lock (name, or address)\n"
    408 	    "-l\t\ttrace only by lock\n"
    409 	    "-M\t\tmerge lock addresses within unique objects\n"
    410 	    "-m\t\tmerge call sites within unique functions\n"
    411 	    "-N nlist\tspecify name list file\n"
    412 	    "-o file\t\tsend output to named file, not stdout\n"
    413 	    "-p\t\tshow average count/time per CPU, not total\n"
    414 	    "-s\t\tshow average count/time per second, not total\n"
    415 	    "-T type\t\tdisplay only one type of lock\n"
    416 	    "-t\t\tlist lock types\n",
    417 	    getprogname(), getprogname());
    418 
    419 	exit(EXIT_FAILURE);
    420 }
    421 
    422 void
    423 nullsig(int junk)
    424 {
    425 
    426 	(void)junk;
    427 }
    428 
    429 void
    430 listnames(const name_t *name)
    431 {
    432 
    433 	for (; name->name != NULL; name++)
    434 		printf("%s\n", name->name);
    435 
    436 	exit(EXIT_SUCCESS);
    437 }
    438 
    439 int
    440 matchname(const name_t *name, char *string)
    441 {
    442 	int empty, mask;
    443 	char *sp;
    444 
    445 	empty = 1;
    446 	mask = 0;
    447 
    448 	while ((sp = strsep(&string, ",")) != NULL) {
    449 		if (*sp == '\0')
    450 			usage();
    451 
    452 		for (; name->name != NULL; name++) {
    453 			if (strcasecmp(name->name, sp) == 0) {
    454 				mask |= name->mask;
    455 				break;
    456 			}
    457 		}
    458 		if (name->name == NULL)
    459 			errx(EXIT_FAILURE, "unknown identifier `%s'", sp);
    460 		empty = 0;
    461 	}
    462 
    463 	if (empty)
    464 		usage();
    465 
    466 	return mask;
    467 }
    468 
    469 /*
    470  * Return the number of CPUs in the running system.
    471  */
    472 int
    473 ncpu(void)
    474 {
    475 	int rv, mib[2];
    476 	size_t varlen;
    477 
    478 	mib[0] = CTL_HW;
    479 	mib[1] = HW_NCPU;
    480 	varlen = sizeof(rv);
    481 	if (sysctl(mib, 2, &rv, &varlen, NULL, (size_t)0) < 0)
    482 		rv = 1;
    483 
    484 	return (rv);
    485 }
    486 
    487 /*
    488  * Call into the ELF parser and look up a symbol by name or by address.
    489  */
    490 void
    491 findsym(findsym_t find, char *name, uintptr_t *start, uintptr_t *end, bool chg)
    492 {
    493 	uintptr_t tend, sa, ea;
    494 	char *p;
    495 	int rv;
    496 
    497 	if (!chg) {
    498 		sa = *start;
    499 		start = &sa;
    500 		end = &ea;
    501 	}
    502 
    503 	if (end == NULL)
    504 		end = &tend;
    505 
    506 	if (find == LOCK_BYNAME) {
    507 		if (isdigit((u_int)name[0])) {
    508 			*start = (uintptr_t)strtoul(name, &p, 0);
    509 			if (*p == '\0')
    510 				return;
    511 		}
    512 	}
    513 
    514 	if (bin64)
    515 		rv = findsym64(find, name, start, end);
    516 	else
    517 		rv = findsym32(find, name, start, end);
    518 
    519 	if (find == FUNC_BYNAME || find == LOCK_BYNAME) {
    520 		if (rv == -1)
    521 			errx(EXIT_FAILURE, "unable to find symbol `%s'", name);
    522 		return;
    523 	}
    524 
    525 	if (rv == -1)
    526 		snprintf(name, NAME_SIZE, "%016lx", (long)*start);
    527 }
    528 
    529 /*
    530  * Fork off the child process and wait for it to complete.  We trap SIGINT
    531  * so that the caller can use Ctrl-C to stop tracing early and still get
    532  * useful results.
    533  */
    534 void
    535 spawn(int argc, char **argv)
    536 {
    537 	pid_t pid;
    538 
    539 	switch (pid = fork()) {
    540 	case 0:
    541 		close(lsfd);
    542 		if (execvp(argv[0], argv) == -1)
    543 			err(EXIT_FAILURE, "cannot exec");
    544 		break;
    545 	case -1:
    546 		err(EXIT_FAILURE, "cannot fork to exec");
    547 		break;
    548 	default:
    549 		signal(SIGINT, nullsig);
    550 		wait(NULL);
    551 		signal(SIGINT, SIG_DFL);
    552 		break;
    553 	}
    554 }
    555 
    556 /*
    557  * Allocate a new block of lock_t structures.
    558  */
    559 lock_t *
    560 morelocks(void)
    561 {
    562 	const static int batch = 32;
    563 	lock_t *l, *lp, *max;
    564 
    565 	l = (lock_t *)malloc(sizeof(*l) * batch);
    566 
    567 	for (lp = l, max = l + batch; lp < max; lp++)
    568 		TAILQ_INSERT_TAIL(&freelist, lp, chain);
    569 
    570 	return l;
    571 }
    572 
    573 /*
    574  * Collapse addresses from unique objects.
    575  */
    576 void
    577 collapse(bool func, bool lock)
    578 {
    579 	lsbuf_t *lb, *max;
    580 
    581 	for (lb = bufs, max = bufs + nbufs; lb < max; lb++) {
    582 		if (func && lb->lb_callsite != 0) {
    583 			findsym(FUNC_BYADDR, NULL, &lb->lb_callsite, NULL,
    584 			    true);
    585 		}
    586 		if (lock && lb->lb_lock != 0) {
    587 			findsym(LOCK_BYADDR, NULL, &lb->lb_lock, NULL,
    588 			    true);
    589 		}
    590 	}
    591 }
    592 
    593 /*
    594  * From the kernel supplied data, construct two dimensional lists of locks
    595  * and event buffers, indexed by lock type and sorted by event type.
    596  */
    597 void
    598 makelists(int mask, int event)
    599 {
    600 	lsbuf_t *lb, *lb2, *max;
    601 	lock_t *l, *l2;
    602 	int type;
    603 
    604 	/*
    605 	 * Recycle lock_t structures from the last run.
    606 	 */
    607 	while ((l = TAILQ_FIRST(&locklist)) != NULL) {
    608 		TAILQ_REMOVE(&locklist, l, chain);
    609 		TAILQ_INSERT_HEAD(&freelist, l, chain);
    610 	}
    611 
    612 	type = mask & LB_LOCK_MASK;
    613 
    614 	for (lb = bufs, max = bufs + nbufs; lb < max; lb++) {
    615 		if ((lb->lb_flags & LB_LOCK_MASK) != type ||
    616 		    lb->lb_counts[event] == 0)
    617 			continue;
    618 
    619 		/*
    620 		 * Look for a record descibing this lock, and allocate a
    621 		 * new one if needed.
    622 		 */
    623 		TAILQ_FOREACH(l, &sortlist, chain) {
    624 			if (l->lock == lb->lb_lock)
    625 				break;
    626 		}
    627 		if (l == NULL) {
    628 			if ((l = TAILQ_FIRST(&freelist)) == NULL)
    629 				l = morelocks();
    630 			TAILQ_REMOVE(&freelist, l, chain);
    631 			l->flags = lb->lb_flags;
    632 			l->lock = lb->lb_lock;
    633 			l->nbufs = 0;
    634 			l->name[0] = '\0';
    635 			l->count = 0;
    636 			l->time = 0;
    637 			TAILQ_INIT(&l->tosort);
    638 			TAILQ_INIT(&l->bufs);
    639 			TAILQ_INSERT_TAIL(&sortlist, l, chain);
    640 		}
    641 
    642 		/*
    643 		 * Scale the time values per buffer and summarise
    644 		 * times+counts per lock.
    645 		 */
    646 		lb->lb_times[event] *= cpuscale[lb->lb_cpu];
    647 		l->count += lb->lb_counts[event];
    648 		l->time += lb->lb_times[event];
    649 
    650 		/*
    651 		 * Merge same lock+callsite pairs from multiple CPUs
    652 		 * together.
    653 		 */
    654 		TAILQ_FOREACH(lb2, &l->tosort, lb_chain.tailq) {
    655 			if (lb->lb_callsite == lb2->lb_callsite)
    656 				break;
    657 		}
    658 		if (lb2 != NULL) {
    659 			lb2->lb_counts[event] += lb->lb_counts[event];
    660 			lb2->lb_times[event] += lb->lb_times[event];
    661 		} else {
    662 			TAILQ_INSERT_HEAD(&l->tosort, lb, lb_chain.tailq);
    663 			l->nbufs++;
    664 		}
    665 	}
    666 
    667 	/*
    668 	 * Now sort the lists.
    669 	 */
    670 	while ((l = TAILQ_FIRST(&sortlist)) != NULL) {
    671 		TAILQ_REMOVE(&sortlist, l, chain);
    672 
    673 		/*
    674 		 * Sort the buffers into the per-lock list.
    675 		 */
    676 		while ((lb = TAILQ_FIRST(&l->tosort)) != NULL) {
    677 			TAILQ_REMOVE(&l->tosort, lb, lb_chain.tailq);
    678 
    679 			lb2 = TAILQ_FIRST(&l->bufs);
    680 			while (lb2 != NULL) {
    681 				if (cflag) {
    682 					if (lb->lb_counts[event] >
    683 					    lb2->lb_counts[event])
    684 						break;
    685 				} else if (lb->lb_times[event] >
    686 				    lb2->lb_times[event])
    687 					break;
    688 				lb2 = TAILQ_NEXT(lb2, lb_chain.tailq);
    689 			}
    690 			if (lb2 == NULL)
    691 				TAILQ_INSERT_TAIL(&l->bufs, lb,
    692 				    lb_chain.tailq);
    693 			else
    694 				TAILQ_INSERT_BEFORE(lb2, lb, lb_chain.tailq);
    695 		}
    696 
    697 		/*
    698 		 * Sort this lock into the per-type list, based on the
    699 		 * totals per lock.
    700 		 */
    701 		l2 = TAILQ_FIRST(&locklist);
    702 		while (l2 != NULL) {
    703 			if (cflag) {
    704 				if (l->count > l2->count)
    705 					break;
    706 			} else if (l->time > l2->time)
    707 				break;
    708 			l2 = TAILQ_NEXT(l2, chain);
    709 		}
    710 		if (l2 == NULL)
    711 			TAILQ_INSERT_TAIL(&locklist, l, chain);
    712 		else
    713 			TAILQ_INSERT_BEFORE(l2, l, chain);
    714 	}
    715 }
    716 
    717 /*
    718  * Display a summary table for one lock type / event type pair.
    719  */
    720 void
    721 display(int mask, const char *name)
    722 {
    723 	lock_t *l;
    724 	lsbuf_t *lb;
    725 	double pcscale, metric;
    726 	char fname[NAME_SIZE];
    727 	int event;
    728 
    729 	event = (mask & LB_EVENT_MASK) - 1;
    730 	makelists(mask, event);
    731 
    732 	if (TAILQ_EMPTY(&locklist))
    733 		return;
    734 
    735 	fprintf(outfp, "\n-- %s\n\n"
    736 	    "Total%%  Count   Time/ms          Lock                       Caller\n"
    737 	    "------ ------- --------- ---------------------- ------------------------------\n",
    738 	    name);
    739 
    740 	/*
    741 	 * Sum up all events for this type of lock + event.
    742 	 */
    743 	pcscale = 0;
    744 	TAILQ_FOREACH(l, &locklist, chain) {
    745 		if (cflag)
    746 			pcscale += l->count;
    747 		else
    748 			pcscale += l->time;
    749 		displayed++;
    750 	}
    751 	if (pcscale == 0)
    752 		pcscale = 100;
    753 	else
    754 		pcscale = (100.0 / pcscale);
    755 
    756 	/*
    757 	 * For each lock, print a summary total, followed by a breakdown by
    758 	 * caller.
    759 	 */
    760 	TAILQ_FOREACH(l, &locklist, chain) {
    761 		if (cflag)
    762 			metric = l->count;
    763 		else
    764 			metric = l->time;
    765 		metric *= pcscale;
    766 
    767 		if (l->name[0] == '\0')
    768 			findsym(LOCK_BYADDR, l->name, &l->lock, NULL, false);
    769 
    770 		if (lflag || l->nbufs > 1)
    771 			fprintf(outfp, "%6.2f %7d %9.2f %-22s <all>\n",
    772 			    metric, (int)(l->count * cscale),
    773 			    l->time * tscale, l->name);
    774 
    775 		if (lflag)
    776 			continue;
    777 
    778 		TAILQ_FOREACH(lb, &l->bufs, lb_chain.tailq) {
    779 			if (cflag)
    780 				metric = lb->lb_counts[event];
    781 			else
    782 				metric = lb->lb_times[event];
    783 			metric *= pcscale;
    784 
    785 			findsym(FUNC_BYADDR, fname, &lb->lb_callsite, NULL,
    786 			    false);
    787 			fprintf(outfp, "%6.2f %7d %9.2f %-22s %s\n",
    788 			    metric, (int)(lb->lb_counts[event] * cscale),
    789 			    lb->lb_times[event] * tscale, l->name, fname);
    790 		}
    791 	}
    792 }
    793