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