Home | History | Annotate | Line # | Download | only in lockstat
main.c revision 1.1
      1 /*	$NetBSD: main.c,v 1.1 2006/09/07 00:50:45 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 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  * - Need better analysis and tracking of events.
     43  * - Should be binary format agnostic, but given that we're likely to be using
     44  *   ELF for quite a while that's not a big problem.
     45  * - Shouldn't have to parse the namelist here.  We should use something like
     46  *   FreeBSD's libelf.
     47  * - The way the namelist is searched sucks, is it worth doing something
     48  *   better?
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 #ifndef lint
     53 __RCSID("$NetBSD: main.c,v 1.1 2006/09/07 00:50:45 ad Exp $");
     54 #endif /* not lint */
     55 
     56 #include <sys/types.h>
     57 #include <sys/param.h>
     58 #include <sys/time.h>
     59 #include <sys/fcntl.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/wait.h>
     62 #include <sys/signal.h>
     63 #include <sys/lockstat.h>
     64 #include <sys/sysctl.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 
     77 #include "extern.h"
     78 
     79 #define	_PATH_DEV_LOCKSTAT	"/dev/lockstat"
     80 
     81 #define	MILLI	1000.0
     82 #define	MICRO	1000000.0
     83 #define	NANO	1000000000.0
     84 #define	PICO	1000000000000.0
     85 
     86 TAILQ_HEAD(lock_head, lockstruct);
     87 typedef struct lock_head locklist_t;
     88 TAILQ_HEAD(buf_head, lsbuf);
     89 typedef struct buf_head buflist_t;
     90 
     91 typedef struct lockstruct {
     92 	TAILQ_ENTRY(lockstruct)	chain;
     93 	buflist_t		bufs;
     94 	uintptr_t		lock;
     95  	double			times[LB_NEVENT];
     96 	uint32_t		counts[LB_NEVENT];
     97 	u_int			flags;
     98 } lock_t;
     99 
    100 typedef struct name {
    101 	const char	*name;
    102 	int		mask;
    103 } name_t;
    104 
    105 const name_t locknames[] = {
    106 	{ "adaptive_mutex", LB_ADAPTIVE_MUTEX },
    107 	{ "adaptive_rwlock", LB_ADAPTIVE_RWLOCK },
    108 	{ "spin_mutex", LB_SPIN_MUTEX },
    109 	{ "spin_rwlock", LB_SPIN_RWLOCK },
    110 	{ "lockmgr", LB_LOCKMGR },
    111 	{ NULL, 0 }
    112 };
    113 
    114 const name_t eventnames[] = {
    115 	{ "spin", LB_SPIN },
    116 	{ "sleep", LB_SLEEP },
    117 	{ NULL, 0 },
    118 };
    119 
    120 const name_t alltypes[] = {
    121 	{ "Adaptive mutex spin", LB_ADAPTIVE_MUTEX | LB_SPIN },
    122 	{ "Adaptive mutex sleep", LB_ADAPTIVE_MUTEX | LB_SLEEP },
    123 	{ "Adaptive RW lock spin", LB_ADAPTIVE_RWLOCK | LB_SPIN },
    124 	{ "Adaptive RW lock sleep", LB_ADAPTIVE_RWLOCK | LB_SPIN },
    125 	{ "Spin mutex spin", LB_SPIN_MUTEX | LB_SPIN },
    126 	{ "Spin RW lock spin", LB_SPIN_RWLOCK | LB_SPIN },
    127 	{ "lockmgr sleep", LB_LOCKMGR | LB_SLEEP },
    128 	{ NULL, 0 }
    129 };
    130 
    131 locklist_t	locklist[LB_NLOCK];
    132 
    133 lsbuf_t		*bufs;
    134 lsdisable_t	ld;
    135 int		lflag;
    136 int		nbufs;
    137 int		cflag;
    138 int		lsfd;
    139 int		displayed;
    140 int		bin64;
    141 double		tscale;
    142 double		cscale;
    143 double		cpuscale[sizeof(ld.ld_freq) / sizeof(ld.ld_freq[0])];
    144 FILE		*outfp;
    145 
    146 void	findsym(findsym_t, char *, uintptr_t *, uintptr_t *);
    147 void	spawn(int, char **);
    148 void	display(int, const char *name);
    149 void	listnames(const name_t *);
    150 int	matchname(const name_t *, const char *);
    151 void	makelists(void);
    152 void	nullsig(int);
    153 void	usage(void);
    154 void	resort(int, int);
    155 int	ncpu(void);
    156 
    157 int
    158 main(int argc, char **argv)
    159 {
    160 	int eventtype, locktype, ch, nlfd, sflag, fd, i, pflag;
    161 	const char *nlistf, *outf;
    162 	char *lockname, *funcname;
    163 	const name_t *name;
    164 	lsenable_t le;
    165 	double ms;
    166 	char *p;
    167 
    168 	nlistf = NULL;
    169 	outf = NULL;
    170 	lockname = NULL;
    171 	funcname = NULL;
    172 	eventtype = -1;
    173 	locktype = -1;
    174 	nbufs = 0;
    175 	sflag = 0;
    176 	pflag = 0;
    177 
    178 	while ((ch = getopt(argc, argv, "E:F:L:M:N:T:b:ceflo:pst")) != -1)
    179 		switch (ch) {
    180 		case 'E':
    181 			eventtype = matchname(eventnames, optarg);
    182 			break;
    183 		case 'F':
    184 			funcname = optarg;
    185 			break;
    186 		case 'L':
    187 			lockname = optarg;
    188 			break;
    189 		case 'N':
    190 			nlistf = optarg;
    191 			break;
    192 		case 'T':
    193 			locktype = matchname(locknames, optarg);
    194 			break;
    195 		case 'b':
    196 			nbufs = (int)strtol(optarg, &p, 0);
    197 			if (!isdigit((u_int)*optarg) || *p != '\0')
    198 				usage();
    199 			break;
    200 		case 'c':
    201 			cflag = 1;
    202 			break;
    203 		case 'e':
    204 			listnames(eventnames);
    205 			break;
    206 		case 'l':
    207 			lflag = 1;
    208 			break;
    209 		case 'o':
    210 			outf = optarg;
    211 			break;
    212 		case 'p':
    213 			pflag = 1;
    214 			break;
    215 		case 's':
    216 			sflag = 1;
    217 			break;
    218 		case 't':
    219 			listnames(locknames);
    220 			break;
    221 		default:
    222 			usage();
    223 		}
    224 	argc -= optind;
    225 	argv += optind;
    226 
    227 	if (*argv == NULL)
    228 		usage();
    229 
    230 	if (outf) {
    231 		if ((fd = open(outf, O_WRONLY | O_CREAT, 0600)) == -1)
    232 			err(EXIT_FAILURE, "opening %s", outf);
    233 		outfp = fdopen(fd, "w");
    234 	} else
    235 		outfp = stdout;
    236 
    237 	/*
    238 	 * Find the name list for resolving symbol names, and load it into
    239 	 * memory.
    240 	 */
    241 	if (nlistf == NULL) {
    242 		nlfd = open(_PATH_KSYMS, O_RDONLY);
    243 		nlistf = getbootfile();
    244 	} else
    245 		nlfd = -1;
    246 	if (nlfd == -1) {
    247 		if ((nlfd = open(nlistf, O_RDONLY)) < 0)
    248 			err(EXIT_FAILURE, "cannot open " _PATH_KSYMS " or %s",
    249 			    nlistf);
    250 	}
    251 	if (loadsym32(nlfd) != 0) {
    252 		if (loadsym64(nlfd) != 0)
    253 			errx(EXIT_FAILURE, "unable to load symbol table");
    254 		bin64 = 1;
    255 	}
    256 	close(nlfd);
    257 
    258 	memset(&le, 0, sizeof(le));
    259 	le.le_nbufs = nbufs;
    260 
    261 	/*
    262 	 * Set up initial filtering.
    263 	 */
    264 	if (lockname != NULL) {
    265 		findsym(LOCK_BYNAME, lockname, &le.le_lock, NULL);
    266 		le.le_flags |= LE_ONE_LOCK;
    267 	}
    268 	if (!lflag)
    269 		le.le_flags |= LE_CALLSITE;
    270 	if (funcname != NULL) {
    271 		if (lflag)
    272 			usage();
    273 		findsym(FUNC_BYNAME, funcname, &le.le_csstart, &le.le_csend);
    274 		le.le_flags |= LE_ONE_CALLSITE;
    275 	}
    276 	le.le_mask = (eventtype & LB_EVENT_MASK) | (locktype & LB_LOCK_MASK);
    277 
    278 	/*
    279 	 * Start tracing.
    280 	 */
    281 	if ((lsfd = open(_PATH_DEV_LOCKSTAT, O_RDONLY)) < 0)
    282 		err(EXIT_FAILURE, "cannot open " _PATH_DEV_LOCKSTAT);
    283 	if (ioctl(lsfd, IOC_LOCKSTAT_GVERSION, &ch) < 0)
    284 		err(EXIT_FAILURE, "ioctl");
    285 	if (ch != LS_VERSION)
    286 		errx(EXIT_FAILURE, "incompatible lockstat interface version");
    287 	if (ioctl(lsfd, IOC_LOCKSTAT_ENABLE, &le))
    288 		err(EXIT_FAILURE, "cannot enable tracing");
    289 
    290 	/*
    291 	 * Execute the traced program.
    292 	 */
    293 	spawn(argc, argv);
    294 
    295 	/*
    296 	 * Stop tracing, and read the trace buffers from the kernel.
    297 	 */
    298 	if (ioctl(lsfd, IOC_LOCKSTAT_DISABLE, &ld) == -1) {
    299 		if (errno == EOVERFLOW) {
    300 			warnx("overflowed available kernel trace buffers");
    301 			exit(EXIT_FAILURE);
    302 		}
    303 		err(EXIT_FAILURE, "cannot disable tracing");
    304 	}
    305 	if ((bufs = malloc(ld.ld_size)) == NULL)
    306 		err(EXIT_FAILURE, "cannot allocate memory for user buffers");
    307 	if (read(lsfd, bufs, ld.ld_size) != ld.ld_size)
    308 		err(EXIT_FAILURE, "reading from " _PATH_DEV_LOCKSTAT);
    309 	if (close(lsfd))
    310 		err(EXIT_FAILURE, "close(" _PATH_DEV_LOCKSTAT ")");
    311 
    312 	/*
    313 	 * Figure out how to scale the results, and build the lists.  For
    314 	 * internal use we convert all times from CPU frequency based to
    315 	 * picoseconds, and values are eventually displayed in ms.
    316 	 */
    317 	for (i = 0; i < sizeof(ld.ld_freq) / sizeof(ld.ld_freq[0]); i++)
    318 		if (ld.ld_freq[i] != 0)
    319 			cpuscale[i] = PICO / ld.ld_freq[i];
    320 	ms = ld.ld_time.tv_sec * MILLI + ld.ld_time.tv_nsec / MICRO;
    321 	if (pflag)
    322 		cscale = 1.0 / ncpu();
    323 	else
    324 		cscale = 1.0;
    325 	cscale *= (sflag ? MILLI / ms : 1.0);
    326 	tscale = cscale / NANO;
    327 	nbufs = (int)(ld.ld_size / sizeof(lsbuf_t));
    328 	makelists();
    329 
    330 	/*
    331 	 * Display the results.
    332 	 */
    333 	fprintf(outfp, "Elapsed time: %.2f seconds.", ms / MILLI);
    334 	if (sflag || pflag) {
    335 		fprintf(outfp, " Displaying ");
    336 		if (pflag)
    337 			fprintf(outfp, "per-CPU ");
    338 		if (sflag)
    339 			fprintf(outfp, "per-second ");
    340 		fprintf(outfp, "averages.");
    341 	}
    342 	putc('\n', outfp);
    343 
    344 	for (name = alltypes; name->name != NULL; name++) {
    345 		if (eventtype != -1 &&
    346 		    (name->mask & LB_EVENT_MASK) != eventtype)
    347 			continue;
    348 		if (locktype != -1 &&
    349 		    (name->mask & LB_LOCK_MASK) != locktype)
    350 			continue;
    351 
    352 		display(name->mask, name->name);
    353 	}
    354 
    355 	if (displayed == 0)
    356 		fprintf(outfp, "None of the selected events were recorded.\n");
    357 	exit(EXIT_SUCCESS);
    358 }
    359 
    360 void
    361 usage(void)
    362 {
    363 
    364 	fprintf(stderr,
    365 	    "%s: usage:\n"
    366 	    "%s [options] <command>\n\n"
    367 	    "-F func\t\tlimit trace to one function\n"
    368 	    "-E evt\t\tdisplay only one type of event\n"
    369 	    "-L lock\t\tlimit trace to one lock (name, or address)\n"
    370 	    "-N nlist\tspecify name list file\n"
    371 	    "-T type\t\tdisplay only one type of lock\n"
    372 	    "-b nbuf\t\tset number of event buffers to allocate\n"
    373 	    "-c\t\treport percentage of total events by count, not time\n"
    374 	    "-e\t\tlist event types\n"
    375 	    "-l\t\ttrace only by lock\n"
    376 	    "-o file\t\tsend output to named file, not stdout\n"
    377 	    "-p\t\tshow average count/time per CPU, not total\n"
    378 	    "-s\t\tshow average count/time per second, not total\n"
    379 	    "-t\t\tlist lock types\n",
    380 	    getprogname(), getprogname());
    381 
    382 	exit(EXIT_FAILURE);
    383 }
    384 
    385 void
    386 nullsig(int junk)
    387 {
    388 
    389 	(void)junk;
    390 }
    391 
    392 void
    393 listnames(const name_t *name)
    394 {
    395 
    396 	for (; name->name != NULL; name++)
    397 		printf("%s\n", name->name);
    398 
    399 	exit(EXIT_SUCCESS);
    400 }
    401 
    402 int
    403 matchname(const name_t *name, const char *string)
    404 {
    405 
    406 	for (; name->name != NULL; name++)
    407 		if (strcasecmp(name->name, string) == 0)
    408 			return name->mask;
    409 
    410 	warnx("unknown type `%s'", string);
    411 	usage();
    412 	return 0;
    413 }
    414 
    415 /*
    416  * Return the number of CPUs in the running system.
    417  */
    418 int
    419 ncpu(void)
    420 {
    421 	int rv, mib[2];
    422 	size_t varlen;
    423 
    424 	mib[0] = CTL_HW;
    425 	mib[1] = HW_NCPU;
    426 	varlen = sizeof(rv);
    427 	if (sysctl(mib, 2, &rv, &varlen, NULL, (size_t)0) < 0)
    428 		rv = 1;
    429 
    430 	return (rv);
    431 }
    432 
    433 /*
    434  * Call into the ELF parser and look up a symbol by name or by address.
    435  */
    436 void
    437 findsym(findsym_t find, char *name, uintptr_t *start, uintptr_t *end)
    438 {
    439 	uintptr_t tend;
    440 	char *p;
    441 	int rv;
    442 
    443 	if (end == NULL)
    444 		end = &tend;
    445 
    446 	if (find == LOCK_BYNAME) {
    447 		if (isdigit((u_int)name[0])) {
    448 			*start = (uintptr_t)strtoul(name, &p, 0);
    449 			if (*p == '\0')
    450 				return;
    451 		}
    452 	}
    453 
    454 	if (bin64)
    455 		rv = findsym64(find, name, start, end);
    456 	else
    457 		rv = findsym32(find, name, start, end);
    458 
    459 	if (find == FUNC_BYNAME || find == LOCK_BYNAME) {
    460 		if (rv == -1)
    461 			errx(EXIT_FAILURE, "unable to find symbol `%s'", name);
    462 		return;
    463 	}
    464 
    465 	if (rv == -1)
    466 		sprintf(name, "0x%016lx", (long)*start);
    467 }
    468 
    469 /*
    470  * Fork off the child process and wait for it to complete.  We trap SIGINT
    471  * so that the caller can use Ctrl-C to stop tracing early and still get
    472  * useful results.
    473  */
    474 void
    475 spawn(int argc, char **argv)
    476 {
    477 	pid_t pid;
    478 
    479 	switch (pid = fork()) {
    480 	case 0:
    481 		close(lsfd);
    482 		if (execvp(argv[0], argv) == -1)
    483 			err(EXIT_FAILURE, "cannot exec");
    484 		break;
    485 	case -1:
    486 		err(EXIT_FAILURE, "cannot fork to exec");
    487 		break;
    488 	default:
    489 		signal(SIGINT, nullsig);
    490 		wait(NULL);
    491 		signal(SIGINT, SIG_DFL);
    492 		break;
    493 	}
    494 }
    495 
    496 /*
    497  * From the kernel supplied data, construct two dimensional lists of locks
    498  * and event buffers, indexed by lock type.
    499  */
    500 void
    501 makelists(void)
    502 {
    503 	lsbuf_t *lb, *lb2, *max;
    504 	int i, type;
    505 	lock_t *l;
    506 
    507 	for (i = 0; i < LB_NLOCK; i++)
    508 		TAILQ_INIT(&locklist[i]);
    509 
    510 	for (lb = bufs, max = bufs + nbufs; lb < max; lb++) {
    511 		if (lb->lb_flags == 0)
    512 			continue;
    513 
    514 		/*
    515 		 * Look for a record descibing this lock, and allocate a
    516 		 * new one if needed.
    517 		 */
    518 		type = ((lb->lb_flags & LB_LOCK_MASK) >> LB_LOCK_SHIFT) - 1;
    519 		TAILQ_FOREACH(l, &locklist[type], chain) {
    520 			if (l->lock == lb->lb_lock)
    521 				break;
    522 		}
    523 		if (l == NULL) {
    524 			l = (lock_t *)malloc(sizeof(*l));
    525 			l->flags = lb->lb_flags;
    526 			l->lock = lb->lb_lock;
    527 			memset(&l->counts, 0, sizeof(l->counts));
    528 			memset(&l->times, 0, sizeof(l->times));
    529 			TAILQ_INIT(&l->bufs);
    530 			TAILQ_INSERT_TAIL(&locklist[type], l, chain);
    531 		}
    532 
    533 		/*
    534 		 * Scale the time values per buffer and summarise
    535 		 * times+counts per lock.
    536 		 */
    537 		for (i = 0; i < LB_NEVENT; i++) {
    538 			lb->lb_times[i] *= cpuscale[lb->lb_cpu];
    539 			l->counts[i] += lb->lb_counts[i];
    540 			l->times[i] += lb->lb_times[i];
    541 		}
    542 
    543 		/*
    544 		 * Merge same lock+callsite pairs from multiple CPUs
    545 		 * together.
    546 		 */
    547 		TAILQ_FOREACH(lb2, &l->bufs, lb_chain.tailq) {
    548 			if (lb->lb_callsite == lb2->lb_callsite)
    549 				break;
    550 		}
    551 		if (lb2 != NULL) {
    552 			for (i = 0; i < LB_NEVENT; i++) {
    553 				lb2->lb_counts[i] += lb->lb_counts[i];
    554 				lb2->lb_times[i] += lb->lb_times[i];
    555 			}
    556 		} else
    557 			TAILQ_INSERT_HEAD(&l->bufs, lb, lb_chain.tailq);
    558 	}
    559 }
    560 
    561 /*
    562  * Re-sort one list of locks / lock buffers by event type.
    563  */
    564 void
    565 resort(int type, int event)
    566 {
    567 	lsbuf_t *lb, *lb2;
    568 	locklist_t llist;
    569 	buflist_t blist;
    570 	lock_t *l, *l2;
    571 
    572 	TAILQ_INIT(&llist);
    573 	while ((l = TAILQ_FIRST(&locklist[type])) != NULL) {
    574 		TAILQ_REMOVE(&locklist[type], l, chain);
    575 
    576 		/*
    577 		 * Sort the buffers into the per-lock list.
    578 		 */
    579 		TAILQ_INIT(&blist);
    580 		while ((lb = TAILQ_FIRST(&l->bufs)) != NULL) {
    581 			TAILQ_REMOVE(&l->bufs, lb, lb_chain.tailq);
    582 
    583 			lb2 = TAILQ_FIRST(&blist);
    584 			while (lb2 != NULL) {
    585 				if (cflag) {
    586 					if (lb->lb_counts[event] >
    587 					    lb2->lb_counts[event])
    588 						break;
    589 				} else if (lb->lb_times[event] >
    590 				    lb2->lb_times[event])
    591 					break;
    592 				lb2 = TAILQ_NEXT(lb2, lb_chain.tailq);
    593 			}
    594 			if (lb2 == NULL)
    595 				TAILQ_INSERT_TAIL(&blist, lb, lb_chain.tailq);
    596 			else
    597 				TAILQ_INSERT_BEFORE(lb2, lb, lb_chain.tailq);
    598 		}
    599 		l->bufs = blist;
    600 
    601 		/*
    602 		 * Sort this lock into the per-type list, based on the
    603 		 * totals per lock.
    604 		 */
    605 		l2 = TAILQ_FIRST(&llist);
    606 		while (l2 != NULL) {
    607 			if (cflag) {
    608 				if (l->counts[event] > l2->counts[event])
    609 					break;
    610 			} else if (l->times[event] > l2->times[event])
    611 				break;
    612 			l2 = TAILQ_NEXT(l2, chain);
    613 		}
    614 		if (l2 == NULL)
    615 			TAILQ_INSERT_TAIL(&llist, l, chain);
    616 		else
    617 			TAILQ_INSERT_BEFORE(l2, l, chain);
    618 	}
    619 	locklist[type] = llist;
    620 }
    621 
    622 /*
    623  * Display a summary table for one lock type / event type pair.
    624  */
    625 void
    626 display(int mask, const char *name)
    627 {
    628 	lock_t *l;
    629 	lsbuf_t *lb;
    630 	int event, type;
    631 	double pcscale, metric;
    632 	char lname[256], fname[256];
    633 
    634 	type = ((mask & LB_LOCK_MASK) >> LB_LOCK_SHIFT) - 1;
    635 	if (TAILQ_FIRST(&locklist[type]) == NULL)
    636 		return;
    637 
    638 	event = (mask & LB_EVENT_MASK) - 1;
    639 	resort(type, event);
    640 
    641 	fprintf(outfp, "\n-- %s\n\n"
    642 	    "Total%%  Count   Time/ms         Lock                      Caller\n"
    643 	    "------ ------- --------- ------------------ ----------------------------------\n",
    644 	    name);
    645 
    646 	/*
    647 	 * Sum up all events for this type of lock + event.
    648 	 */
    649 	pcscale = 0;
    650 	TAILQ_FOREACH(l, &locklist[type], chain) {
    651 		if (cflag)
    652 			pcscale += l->counts[event];
    653 		else
    654 			pcscale += l->times[event];
    655 		displayed++;
    656 	}
    657 	if (pcscale == 0)
    658 		pcscale = 100;
    659 	else
    660 		pcscale = (100.0 / pcscale);
    661 
    662 	/*
    663 	 * For each lock, print a summary total, followed by a breakdown by
    664 	 * caller.
    665 	 */
    666 	TAILQ_FOREACH(l, &locklist[type], chain) {
    667 		if (cflag)
    668 			metric = l->counts[event];
    669 		else
    670 			metric = l->times[event];
    671 		metric *= pcscale;
    672 
    673 		findsym(LOCK_BYADDR, lname, &l->lock, NULL);
    674 
    675 		fprintf(outfp, "%6.2f %7d %9.2f %-18s <all>\n", metric,
    676 		    (int)(l->counts[event] * cscale),
    677 		    l->times[event] * tscale, lname);
    678 
    679 		if (lflag)
    680 			continue;
    681 
    682 		TAILQ_FOREACH(lb, &l->bufs, lb_chain.tailq) {
    683 			if (cflag)
    684 				metric = lb->lb_counts[event];
    685 			else
    686 				metric = lb->lb_times[event];
    687 			metric *= pcscale;
    688 
    689 			findsym(LOCK_BYADDR, lname, &lb->lb_lock, NULL);
    690 			findsym(FUNC_BYADDR, fname, &lb->lb_callsite, NULL);
    691 			fprintf(outfp, "%6.2f %7d %9.2f %-18s %s\n", metric,
    692 			    (int)(lb->lb_counts[event] * cscale),
    693 			    lb->lb_times[event] * tscale,
    694 			    lname, fname);
    695 		}
    696 	}
    697 }
    698