Home | History | Annotate | Line # | Download | only in dev
lockstat.c revision 1.19.4.1
      1 /*	$NetBSD: lockstat.c,v 1.19.4.1 2015/04/06 15:18:08 skrll 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Lock statistics driver, providing kernel support for the lockstat(8)
     34  * command.
     35  *
     36  * We use a global lock word (lockstat_lock) to track device opens.
     37  * Only one thread can hold the device at a time, providing a global lock.
     38  *
     39  * XXX Timings for contention on sleep locks are currently incorrect.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: lockstat.c,v 1.19.4.1 2015/04/06 15:18:08 skrll Exp $");
     44 
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/proc.h>
     48 #include <sys/resourcevar.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/kmem.h>
     52 #include <sys/conf.h>
     53 #include <sys/syslog.h>
     54 #include <sys/atomic.h>
     55 
     56 #include <dev/lockstat.h>
     57 
     58 #include <machine/lock.h>
     59 
     60 #ifndef __HAVE_CPU_COUNTER
     61 #error CPU counters not available
     62 #endif
     63 
     64 #if LONG_BIT == 64
     65 #define	LOCKSTAT_HASH_SHIFT	3
     66 #elif LONG_BIT == 32
     67 #define	LOCKSTAT_HASH_SHIFT	2
     68 #endif
     69 
     70 #define	LOCKSTAT_MINBUFS	1000
     71 #define	LOCKSTAT_DEFBUFS	10000
     72 #define	LOCKSTAT_MAXBUFS	1000000
     73 
     74 #define	LOCKSTAT_HASH_SIZE	128
     75 #define	LOCKSTAT_HASH_MASK	(LOCKSTAT_HASH_SIZE - 1)
     76 #define	LOCKSTAT_HASH(key)	\
     77 	((key >> LOCKSTAT_HASH_SHIFT) & LOCKSTAT_HASH_MASK)
     78 
     79 typedef struct lscpu {
     80 	SLIST_HEAD(, lsbuf)	lc_free;
     81 	u_int			lc_overflow;
     82 	LIST_HEAD(lslist, lsbuf) lc_hash[LOCKSTAT_HASH_SIZE];
     83 } lscpu_t;
     84 
     85 typedef struct lslist lslist_t;
     86 
     87 void	lockstatattach(int);
     88 void	lockstat_start(lsenable_t *);
     89 int	lockstat_alloc(lsenable_t *);
     90 void	lockstat_init_tables(lsenable_t *);
     91 int	lockstat_stop(lsdisable_t *);
     92 void	lockstat_free(void);
     93 
     94 dev_type_open(lockstat_open);
     95 dev_type_close(lockstat_close);
     96 dev_type_read(lockstat_read);
     97 dev_type_ioctl(lockstat_ioctl);
     98 
     99 volatile u_int	lockstat_enabled;
    100 volatile u_int	lockstat_dev_enabled;
    101 uintptr_t	lockstat_csstart;
    102 uintptr_t	lockstat_csend;
    103 uintptr_t	lockstat_csmask;
    104 uintptr_t	lockstat_lamask;
    105 uintptr_t	lockstat_lockstart;
    106 uintptr_t	lockstat_lockend;
    107 __cpu_simple_lock_t lockstat_lock;
    108 lwp_t		*lockstat_lwp;
    109 lsbuf_t		*lockstat_baseb;
    110 size_t		lockstat_sizeb;
    111 int		lockstat_busy;
    112 struct timespec	lockstat_stime;
    113 
    114 #ifdef KDTRACE_HOOKS
    115 volatile u_int lockstat_dtrace_enabled;
    116 CTASSERT(LB_NEVENT <= 3);
    117 CTASSERT(LB_NLOCK <= (7 << LB_LOCK_SHIFT));
    118 void
    119 lockstat_probe_stub(uint32_t id, uintptr_t lock, uintptr_t callsite,
    120     uintptr_t flags, uintptr_t count, uintptr_t cycles)
    121 {
    122 }
    123 
    124 uint32_t	lockstat_probemap[LS_NPROBES];
    125 void		(*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
    126 		    uintptr_t, uintptr_t, uintptr_t) = &lockstat_probe_stub;
    127 #endif
    128 
    129 const struct cdevsw lockstat_cdevsw = {
    130 	.d_open = lockstat_open,
    131 	.d_close = lockstat_close,
    132 	.d_read = lockstat_read,
    133 	.d_write = nowrite,
    134 	.d_ioctl = lockstat_ioctl,
    135 	.d_stop = nostop,
    136 	.d_tty = notty,
    137 	.d_poll = nopoll,
    138 	.d_mmap = nommap,
    139 	.d_kqfilter = nokqfilter,
    140 	.d_discard = nodiscard,
    141 	.d_flag = D_OTHER | D_MPSAFE
    142 };
    143 
    144 /*
    145  * Called when the pseudo-driver is attached.
    146  */
    147 void
    148 lockstatattach(int nunits)
    149 {
    150 
    151 	(void)nunits;
    152 
    153 	__cpu_simple_lock_init(&lockstat_lock);
    154 }
    155 
    156 /*
    157  * Prepare the per-CPU tables for use, or clear down tables when tracing is
    158  * stopped.
    159  */
    160 void
    161 lockstat_init_tables(lsenable_t *le)
    162 {
    163 	int i, per, slop, cpuno;
    164 	CPU_INFO_ITERATOR cii;
    165 	struct cpu_info *ci;
    166 	lscpu_t *lc;
    167 	lsbuf_t *lb;
    168 
    169 	/* coverity[assert_side_effect] */
    170 	KASSERT(!lockstat_dev_enabled);
    171 
    172 	for (CPU_INFO_FOREACH(cii, ci)) {
    173 		if (ci->ci_lockstat != NULL) {
    174 			kmem_free(ci->ci_lockstat, sizeof(lscpu_t));
    175 			ci->ci_lockstat = NULL;
    176 		}
    177 	}
    178 
    179 	if (le == NULL)
    180 		return;
    181 
    182 	lb = lockstat_baseb;
    183 	per = le->le_nbufs / ncpu;
    184 	slop = le->le_nbufs - (per * ncpu);
    185 	cpuno = 0;
    186 	for (CPU_INFO_FOREACH(cii, ci)) {
    187 		lc = kmem_alloc(sizeof(*lc), KM_SLEEP);
    188 		lc->lc_overflow = 0;
    189 		ci->ci_lockstat = lc;
    190 
    191 		SLIST_INIT(&lc->lc_free);
    192 		for (i = 0; i < LOCKSTAT_HASH_SIZE; i++)
    193 			LIST_INIT(&lc->lc_hash[i]);
    194 
    195 		for (i = per; i != 0; i--, lb++) {
    196 			lb->lb_cpu = (uint16_t)cpuno;
    197 			SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
    198 		}
    199 		if (--slop > 0) {
    200 			lb->lb_cpu = (uint16_t)cpuno;
    201 			SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
    202 			lb++;
    203 		}
    204 		cpuno++;
    205 	}
    206 }
    207 
    208 /*
    209  * Start collecting lock statistics.
    210  */
    211 void
    212 lockstat_start(lsenable_t *le)
    213 {
    214 
    215 	/* coverity[assert_side_effect] */
    216 	KASSERT(!lockstat_dev_enabled);
    217 
    218 	lockstat_init_tables(le);
    219 
    220 	if ((le->le_flags & LE_CALLSITE) != 0)
    221 		lockstat_csmask = (uintptr_t)-1LL;
    222 	else
    223 		lockstat_csmask = 0;
    224 
    225 	if ((le->le_flags & LE_LOCK) != 0)
    226 		lockstat_lamask = (uintptr_t)-1LL;
    227 	else
    228 		lockstat_lamask = 0;
    229 
    230 	lockstat_csstart = le->le_csstart;
    231 	lockstat_csend = le->le_csend;
    232 	lockstat_lockstart = le->le_lockstart;
    233 	lockstat_lockstart = le->le_lockstart;
    234 	lockstat_lockend = le->le_lockend;
    235 	membar_sync();
    236 	getnanotime(&lockstat_stime);
    237 	lockstat_dev_enabled = le->le_mask;
    238 	LOCKSTAT_ENABLED_UPDATE();
    239 }
    240 
    241 /*
    242  * Stop collecting lock statistics.
    243  */
    244 int
    245 lockstat_stop(lsdisable_t *ld)
    246 {
    247 	CPU_INFO_ITERATOR cii;
    248 	struct cpu_info *ci;
    249 	u_int cpuno, overflow;
    250 	struct timespec ts;
    251 	int error;
    252 	lwp_t *l;
    253 
    254 	/* coverity[assert_side_effect] */
    255 	KASSERT(lockstat_dev_enabled);
    256 
    257 	/*
    258 	 * Set enabled false, force a write barrier, and wait for other CPUs
    259 	 * to exit lockstat_event().
    260 	 */
    261 	lockstat_dev_enabled = 0;
    262 	LOCKSTAT_ENABLED_UPDATE();
    263 	getnanotime(&ts);
    264 	tsleep(&lockstat_stop, PPAUSE, "lockstat", mstohz(10));
    265 
    266 	/*
    267 	 * Did we run out of buffers while tracing?
    268 	 */
    269 	overflow = 0;
    270 	for (CPU_INFO_FOREACH(cii, ci))
    271 		overflow += ((lscpu_t *)ci->ci_lockstat)->lc_overflow;
    272 
    273 	if (overflow != 0) {
    274 		error = EOVERFLOW;
    275 		log(LOG_NOTICE, "lockstat: %d buffer allocations failed\n",
    276 		    overflow);
    277 	} else
    278 		error = 0;
    279 
    280 	lockstat_init_tables(NULL);
    281 
    282 	/* Run through all LWPs and clear the slate for the next run. */
    283 	mutex_enter(proc_lock);
    284 	LIST_FOREACH(l, &alllwp, l_list) {
    285 		l->l_pfailaddr = 0;
    286 		l->l_pfailtime = 0;
    287 		l->l_pfaillock = 0;
    288 	}
    289 	mutex_exit(proc_lock);
    290 
    291 	if (ld == NULL)
    292 		return error;
    293 
    294 	/*
    295 	 * Fill out the disable struct for the caller.
    296 	 */
    297 	timespecsub(&ts, &lockstat_stime, &ld->ld_time);
    298 	ld->ld_size = lockstat_sizeb;
    299 
    300 	cpuno = 0;
    301 	for (CPU_INFO_FOREACH(cii, ci)) {
    302 		if (cpuno >= sizeof(ld->ld_freq) / sizeof(ld->ld_freq[0])) {
    303 			log(LOG_WARNING, "lockstat: too many CPUs\n");
    304 			break;
    305 		}
    306 		ld->ld_freq[cpuno++] = cpu_frequency(ci);
    307 	}
    308 
    309 	return error;
    310 }
    311 
    312 /*
    313  * Allocate buffers for lockstat_start().
    314  */
    315 int
    316 lockstat_alloc(lsenable_t *le)
    317 {
    318 	lsbuf_t *lb;
    319 	size_t sz;
    320 
    321 	/* coverity[assert_side_effect] */
    322 	KASSERT(!lockstat_dev_enabled);
    323 	lockstat_free();
    324 
    325 	sz = sizeof(*lb) * le->le_nbufs;
    326 
    327 	lb = kmem_zalloc(sz, KM_SLEEP);
    328 	if (lb == NULL)
    329 		return (ENOMEM);
    330 
    331 	/* coverity[assert_side_effect] */
    332 	KASSERT(!lockstat_dev_enabled);
    333 	KASSERT(lockstat_baseb == NULL);
    334 	lockstat_sizeb = sz;
    335 	lockstat_baseb = lb;
    336 
    337 	return (0);
    338 }
    339 
    340 /*
    341  * Free allocated buffers after tracing has stopped.
    342  */
    343 void
    344 lockstat_free(void)
    345 {
    346 
    347 	/* coverity[assert_side_effect] */
    348 	KASSERT(!lockstat_dev_enabled);
    349 
    350 	if (lockstat_baseb != NULL) {
    351 		kmem_free(lockstat_baseb, lockstat_sizeb);
    352 		lockstat_baseb = NULL;
    353 	}
    354 }
    355 
    356 /*
    357  * Main entry point from lock primatives.
    358  */
    359 void
    360 lockstat_event(uintptr_t lock, uintptr_t callsite, u_int flags, u_int count,
    361 	       uint64_t cycles)
    362 {
    363 	lslist_t *ll;
    364 	lscpu_t *lc;
    365 	lsbuf_t *lb;
    366 	u_int event;
    367 	int s;
    368 
    369 #ifdef KDTRACE_HOOKS
    370 	uint32_t id;
    371 	CTASSERT((LS_NPROBES & (LS_NPROBES - 1)) == 0);
    372 	if ((id = lockstat_probemap[LS_COMPRESS(flags)]) != 0)
    373 		(*lockstat_probe_func)(id, lock, callsite, flags, count,
    374 		    cycles);
    375 #endif
    376 
    377 	if ((flags & lockstat_dev_enabled) != flags || count == 0)
    378 		return;
    379 	if (lock < lockstat_lockstart || lock > lockstat_lockend)
    380 		return;
    381 	if (callsite < lockstat_csstart || callsite > lockstat_csend)
    382 		return;
    383 
    384 	callsite &= lockstat_csmask;
    385 	lock &= lockstat_lamask;
    386 
    387 	/*
    388 	 * Find the table for this lock+callsite pair, and try to locate a
    389 	 * buffer with the same key.
    390 	 */
    391 	s = splhigh();
    392 	lc = curcpu()->ci_lockstat;
    393 	ll = &lc->lc_hash[LOCKSTAT_HASH(lock ^ callsite)];
    394 	event = (flags & LB_EVENT_MASK) - 1;
    395 
    396 	LIST_FOREACH(lb, ll, lb_chain.list) {
    397 		if (lb->lb_lock == lock && lb->lb_callsite == callsite)
    398 			break;
    399 	}
    400 
    401 	if (lb != NULL) {
    402 		/*
    403 		 * We found a record.  Move it to the front of the list, as
    404 		 * we're likely to hit it again soon.
    405 		 */
    406 		if (lb != LIST_FIRST(ll)) {
    407 			LIST_REMOVE(lb, lb_chain.list);
    408 			LIST_INSERT_HEAD(ll, lb, lb_chain.list);
    409 		}
    410 		lb->lb_counts[event] += count;
    411 		lb->lb_times[event] += cycles;
    412 	} else if ((lb = SLIST_FIRST(&lc->lc_free)) != NULL) {
    413 		/*
    414 		 * Pinch a new buffer and fill it out.
    415 		 */
    416 		SLIST_REMOVE_HEAD(&lc->lc_free, lb_chain.slist);
    417 		LIST_INSERT_HEAD(ll, lb, lb_chain.list);
    418 		lb->lb_flags = (uint16_t)flags;
    419 		lb->lb_lock = lock;
    420 		lb->lb_callsite = callsite;
    421 		lb->lb_counts[event] = count;
    422 		lb->lb_times[event] = cycles;
    423 	} else {
    424 		/*
    425 		 * We didn't find a buffer and there were none free.
    426 		 * lockstat_stop() will notice later on and report the
    427 		 * error.
    428 		 */
    429 		 lc->lc_overflow++;
    430 	}
    431 
    432 	splx(s);
    433 }
    434 
    435 /*
    436  * Accept an open() on /dev/lockstat.
    437  */
    438 int
    439 lockstat_open(dev_t dev, int flag, int mode, lwp_t *l)
    440 {
    441 
    442 	if (!__cpu_simple_lock_try(&lockstat_lock))
    443 		return EBUSY;
    444 	lockstat_lwp = curlwp;
    445 	return 0;
    446 }
    447 
    448 /*
    449  * Accept the last close() on /dev/lockstat.
    450  */
    451 int
    452 lockstat_close(dev_t dev, int flag, int mode, lwp_t *l)
    453 {
    454 
    455 	lockstat_lwp = NULL;
    456 	__cpu_simple_unlock(&lockstat_lock);
    457 	return 0;
    458 }
    459 
    460 /*
    461  * Handle control operations.
    462  */
    463 int
    464 lockstat_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    465 {
    466 	lsenable_t *le;
    467 	int error;
    468 
    469 	if (lockstat_lwp != curlwp)
    470 		return EBUSY;
    471 
    472 	switch (cmd) {
    473 	case IOC_LOCKSTAT_GVERSION:
    474 		*(int *)data = LS_VERSION;
    475 		error = 0;
    476 		break;
    477 
    478 	case IOC_LOCKSTAT_ENABLE:
    479 		le = (lsenable_t *)data;
    480 
    481 		if (!cpu_hascounter()) {
    482 			error = ENODEV;
    483 			break;
    484 		}
    485 		if (lockstat_dev_enabled) {
    486 			error = EBUSY;
    487 			break;
    488 		}
    489 
    490 		/*
    491 		 * Sanitize the arguments passed in and set up filtering.
    492 		 */
    493 		if (le->le_nbufs == 0)
    494 			le->le_nbufs = LOCKSTAT_DEFBUFS;
    495 		else if (le->le_nbufs > LOCKSTAT_MAXBUFS ||
    496 		    le->le_nbufs < LOCKSTAT_MINBUFS) {
    497 			error = EINVAL;
    498 			break;
    499 		}
    500 		if ((le->le_flags & LE_ONE_CALLSITE) == 0) {
    501 			le->le_csstart = 0;
    502 			le->le_csend = le->le_csstart - 1;
    503 		}
    504 		if ((le->le_flags & LE_ONE_LOCK) == 0) {
    505 			le->le_lockstart = 0;
    506 			le->le_lockend = le->le_lockstart - 1;
    507 		}
    508 		if ((le->le_mask & LB_EVENT_MASK) == 0)
    509 			return EINVAL;
    510 		if ((le->le_mask & LB_LOCK_MASK) == 0)
    511 			return EINVAL;
    512 
    513 		/*
    514 		 * Start tracing.
    515 		 */
    516 		if ((error = lockstat_alloc(le)) == 0)
    517 			lockstat_start(le);
    518 		break;
    519 
    520 	case IOC_LOCKSTAT_DISABLE:
    521 		if (!lockstat_dev_enabled)
    522 			error = EINVAL;
    523 		else
    524 			error = lockstat_stop((lsdisable_t *)data);
    525 		break;
    526 
    527 	default:
    528 		error = ENOTTY;
    529 		break;
    530 	}
    531 
    532 	return error;
    533 }
    534 
    535 /*
    536  * Copy buffers out to user-space.
    537  */
    538 int
    539 lockstat_read(dev_t dev, struct uio *uio, int flag)
    540 {
    541 
    542 	if (curlwp != lockstat_lwp || lockstat_dev_enabled)
    543 		return EBUSY;
    544 	return uiomove(lockstat_baseb, lockstat_sizeb, uio);
    545 }
    546