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