Home | History | Annotate | Line # | Download | only in dev
lockstat.c revision 1.20
      1 /*	$NetBSD: lockstat.c,v 1.20 2015/03/08 22:45:16 christos 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.20 2015/03/08 22:45:16 christos 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 uintptr_t	lockstat_csstart;
    101 uintptr_t	lockstat_csend;
    102 uintptr_t	lockstat_csmask;
    103 uintptr_t	lockstat_lamask;
    104 uintptr_t	lockstat_lockstart;
    105 uintptr_t	lockstat_lockend;
    106 __cpu_simple_lock_t lockstat_lock;
    107 lwp_t		*lockstat_lwp;
    108 lsbuf_t		*lockstat_baseb;
    109 size_t		lockstat_sizeb;
    110 int		lockstat_busy;
    111 struct timespec	lockstat_stime;
    112 
    113 #ifdef KDTRACE_HOOKS
    114 CTASSERT(LB_NEVENT <= 3);
    115 CTASSERT(LB_NLOCK <= (7 << LB_LOCK_SHIFT));
    116 void
    117 lockstat_probe_stub(uint32_t id, uintptr_t lock, uintptr_t callsite,
    118     uintptr_t flags, uintptr_t count, uintptr_t cycles)
    119 {
    120 }
    121 
    122 uint32_t	lockstat_probemap[LS_NPROBES];
    123 void		(*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
    124 		    uintptr_t, uintptr_t, uintptr_t) = &lockstat_probe_stub;
    125 #endif
    126 
    127 const struct cdevsw lockstat_cdevsw = {
    128 	.d_open = lockstat_open,
    129 	.d_close = lockstat_close,
    130 	.d_read = lockstat_read,
    131 	.d_write = nowrite,
    132 	.d_ioctl = lockstat_ioctl,
    133 	.d_stop = nostop,
    134 	.d_tty = notty,
    135 	.d_poll = nopoll,
    136 	.d_mmap = nommap,
    137 	.d_kqfilter = nokqfilter,
    138 	.d_discard = nodiscard,
    139 	.d_flag = D_OTHER | D_MPSAFE
    140 };
    141 
    142 /*
    143  * Called when the pseudo-driver is attached.
    144  */
    145 void
    146 lockstatattach(int nunits)
    147 {
    148 
    149 	(void)nunits;
    150 
    151 	__cpu_simple_lock_init(&lockstat_lock);
    152 }
    153 
    154 /*
    155  * Prepare the per-CPU tables for use, or clear down tables when tracing is
    156  * stopped.
    157  */
    158 void
    159 lockstat_init_tables(lsenable_t *le)
    160 {
    161 	int i, per, slop, cpuno;
    162 	CPU_INFO_ITERATOR cii;
    163 	struct cpu_info *ci;
    164 	lscpu_t *lc;
    165 	lsbuf_t *lb;
    166 
    167 	KASSERT(!lockstat_enabled);
    168 
    169 	for (CPU_INFO_FOREACH(cii, ci)) {
    170 		if (ci->ci_lockstat != NULL) {
    171 			kmem_free(ci->ci_lockstat, sizeof(lscpu_t));
    172 			ci->ci_lockstat = NULL;
    173 		}
    174 	}
    175 
    176 	if (le == NULL)
    177 		return;
    178 
    179 	lb = lockstat_baseb;
    180 	per = le->le_nbufs / ncpu;
    181 	slop = le->le_nbufs - (per * ncpu);
    182 	cpuno = 0;
    183 	for (CPU_INFO_FOREACH(cii, ci)) {
    184 		lc = kmem_alloc(sizeof(*lc), KM_SLEEP);
    185 		lc->lc_overflow = 0;
    186 		ci->ci_lockstat = lc;
    187 
    188 		SLIST_INIT(&lc->lc_free);
    189 		for (i = 0; i < LOCKSTAT_HASH_SIZE; i++)
    190 			LIST_INIT(&lc->lc_hash[i]);
    191 
    192 		for (i = per; i != 0; i--, lb++) {
    193 			lb->lb_cpu = (uint16_t)cpuno;
    194 			SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
    195 		}
    196 		if (--slop > 0) {
    197 			lb->lb_cpu = (uint16_t)cpuno;
    198 			SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
    199 			lb++;
    200 		}
    201 		cpuno++;
    202 	}
    203 }
    204 
    205 /*
    206  * Start collecting lock statistics.
    207  */
    208 void
    209 lockstat_start(lsenable_t *le)
    210 {
    211 
    212 	KASSERT(!lockstat_enabled);
    213 
    214 	lockstat_init_tables(le);
    215 
    216 	if ((le->le_flags & LE_CALLSITE) != 0)
    217 		lockstat_csmask = (uintptr_t)-1LL;
    218 	else
    219 		lockstat_csmask = 0;
    220 
    221 	if ((le->le_flags & LE_LOCK) != 0)
    222 		lockstat_lamask = (uintptr_t)-1LL;
    223 	else
    224 		lockstat_lamask = 0;
    225 
    226 	lockstat_csstart = le->le_csstart;
    227 	lockstat_csend = le->le_csend;
    228 	lockstat_lockstart = le->le_lockstart;
    229 	lockstat_lockstart = le->le_lockstart;
    230 	lockstat_lockend = le->le_lockend;
    231 	membar_sync();
    232 	getnanotime(&lockstat_stime);
    233 	lockstat_enabled = le->le_mask;
    234 	membar_producer();
    235 }
    236 
    237 /*
    238  * Stop collecting lock statistics.
    239  */
    240 int
    241 lockstat_stop(lsdisable_t *ld)
    242 {
    243 	CPU_INFO_ITERATOR cii;
    244 	struct cpu_info *ci;
    245 	u_int cpuno, overflow;
    246 	struct timespec ts;
    247 	int error;
    248 	lwp_t *l;
    249 
    250 	KASSERT(lockstat_enabled);
    251 
    252 	/*
    253 	 * Set enabled false, force a write barrier, and wait for other CPUs
    254 	 * to exit lockstat_event().
    255 	 */
    256 	lockstat_enabled = 0;
    257 	membar_producer();
    258 	getnanotime(&ts);
    259 	tsleep(&lockstat_stop, PPAUSE, "lockstat", mstohz(10));
    260 
    261 	/*
    262 	 * Did we run out of buffers while tracing?
    263 	 */
    264 	overflow = 0;
    265 	for (CPU_INFO_FOREACH(cii, ci))
    266 		overflow += ((lscpu_t *)ci->ci_lockstat)->lc_overflow;
    267 
    268 	if (overflow != 0) {
    269 		error = EOVERFLOW;
    270 		log(LOG_NOTICE, "lockstat: %d buffer allocations failed\n",
    271 		    overflow);
    272 	} else
    273 		error = 0;
    274 
    275 	lockstat_init_tables(NULL);
    276 
    277 	/* Run through all LWPs and clear the slate for the next run. */
    278 	mutex_enter(proc_lock);
    279 	LIST_FOREACH(l, &alllwp, l_list) {
    280 		l->l_pfailaddr = 0;
    281 		l->l_pfailtime = 0;
    282 		l->l_pfaillock = 0;
    283 	}
    284 	mutex_exit(proc_lock);
    285 
    286 	if (ld == NULL)
    287 		return error;
    288 
    289 	/*
    290 	 * Fill out the disable struct for the caller.
    291 	 */
    292 	timespecsub(&ts, &lockstat_stime, &ld->ld_time);
    293 	ld->ld_size = lockstat_sizeb;
    294 
    295 	cpuno = 0;
    296 	for (CPU_INFO_FOREACH(cii, ci)) {
    297 		if (cpuno >= sizeof(ld->ld_freq) / sizeof(ld->ld_freq[0])) {
    298 			log(LOG_WARNING, "lockstat: too many CPUs\n");
    299 			break;
    300 		}
    301 		ld->ld_freq[cpuno++] = cpu_frequency(ci);
    302 	}
    303 
    304 	return error;
    305 }
    306 
    307 /*
    308  * Allocate buffers for lockstat_start().
    309  */
    310 int
    311 lockstat_alloc(lsenable_t *le)
    312 {
    313 	lsbuf_t *lb;
    314 	size_t sz;
    315 
    316 	KASSERT(!lockstat_enabled);
    317 	lockstat_free();
    318 
    319 	sz = sizeof(*lb) * le->le_nbufs;
    320 
    321 	lb = kmem_zalloc(sz, KM_SLEEP);
    322 	if (lb == NULL)
    323 		return (ENOMEM);
    324 
    325 	KASSERT(!lockstat_enabled);
    326 	KASSERT(lockstat_baseb == NULL);
    327 	lockstat_sizeb = sz;
    328 	lockstat_baseb = lb;
    329 
    330 	return (0);
    331 }
    332 
    333 /*
    334  * Free allocated buffers after tracing has stopped.
    335  */
    336 void
    337 lockstat_free(void)
    338 {
    339 
    340 	KASSERT(!lockstat_enabled);
    341 
    342 	if (lockstat_baseb != NULL) {
    343 		kmem_free(lockstat_baseb, lockstat_sizeb);
    344 		lockstat_baseb = NULL;
    345 	}
    346 }
    347 
    348 /*
    349  * Main entry point from lock primatives.
    350  */
    351 void
    352 lockstat_event(uintptr_t lock, uintptr_t callsite, u_int flags, u_int count,
    353 	       uint64_t cycles)
    354 {
    355 	lslist_t *ll;
    356 	lscpu_t *lc;
    357 	lsbuf_t *lb;
    358 	u_int event;
    359 	int s;
    360 
    361 #ifdef KDTRACE_HOOKS
    362 	uint32_t id;
    363 	CTASSERT((LS_NPROBES & (LS_NPROBES - 1)) == 0);
    364 	if ((id = lockstat_probemap[LS_COMPRESS(flags)]) != 0)
    365 		(*lockstat_probe_func)(id, lock, callsite, flags, count,
    366 		    cycles);
    367 #endif
    368 
    369 	if ((flags & lockstat_enabled) != flags || count == 0)
    370 		return;
    371 	if (lock < lockstat_lockstart || lock > lockstat_lockend)
    372 		return;
    373 	if (callsite < lockstat_csstart || callsite > lockstat_csend)
    374 		return;
    375 
    376 	callsite &= lockstat_csmask;
    377 	lock &= lockstat_lamask;
    378 
    379 	/*
    380 	 * Find the table for this lock+callsite pair, and try to locate a
    381 	 * buffer with the same key.
    382 	 */
    383 	s = splhigh();
    384 	lc = curcpu()->ci_lockstat;
    385 	ll = &lc->lc_hash[LOCKSTAT_HASH(lock ^ callsite)];
    386 	event = (flags & LB_EVENT_MASK) - 1;
    387 
    388 	LIST_FOREACH(lb, ll, lb_chain.list) {
    389 		if (lb->lb_lock == lock && lb->lb_callsite == callsite)
    390 			break;
    391 	}
    392 
    393 	if (lb != NULL) {
    394 		/*
    395 		 * We found a record.  Move it to the front of the list, as
    396 		 * we're likely to hit it again soon.
    397 		 */
    398 		if (lb != LIST_FIRST(ll)) {
    399 			LIST_REMOVE(lb, lb_chain.list);
    400 			LIST_INSERT_HEAD(ll, lb, lb_chain.list);
    401 		}
    402 		lb->lb_counts[event] += count;
    403 		lb->lb_times[event] += cycles;
    404 	} else if ((lb = SLIST_FIRST(&lc->lc_free)) != NULL) {
    405 		/*
    406 		 * Pinch a new buffer and fill it out.
    407 		 */
    408 		SLIST_REMOVE_HEAD(&lc->lc_free, lb_chain.slist);
    409 		LIST_INSERT_HEAD(ll, lb, lb_chain.list);
    410 		lb->lb_flags = (uint16_t)flags;
    411 		lb->lb_lock = lock;
    412 		lb->lb_callsite = callsite;
    413 		lb->lb_counts[event] = count;
    414 		lb->lb_times[event] = cycles;
    415 	} else {
    416 		/*
    417 		 * We didn't find a buffer and there were none free.
    418 		 * lockstat_stop() will notice later on and report the
    419 		 * error.
    420 		 */
    421 		 lc->lc_overflow++;
    422 	}
    423 
    424 	splx(s);
    425 }
    426 
    427 /*
    428  * Accept an open() on /dev/lockstat.
    429  */
    430 int
    431 lockstat_open(dev_t dev, int flag, int mode, lwp_t *l)
    432 {
    433 
    434 	if (!__cpu_simple_lock_try(&lockstat_lock))
    435 		return EBUSY;
    436 	lockstat_lwp = curlwp;
    437 	return 0;
    438 }
    439 
    440 /*
    441  * Accept the last close() on /dev/lockstat.
    442  */
    443 int
    444 lockstat_close(dev_t dev, int flag, int mode, lwp_t *l)
    445 {
    446 
    447 	lockstat_lwp = NULL;
    448 	__cpu_simple_unlock(&lockstat_lock);
    449 	return 0;
    450 }
    451 
    452 /*
    453  * Handle control operations.
    454  */
    455 int
    456 lockstat_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    457 {
    458 	lsenable_t *le;
    459 	int error;
    460 
    461 	if (lockstat_lwp != curlwp)
    462 		return EBUSY;
    463 
    464 	switch (cmd) {
    465 	case IOC_LOCKSTAT_GVERSION:
    466 		*(int *)data = LS_VERSION;
    467 		error = 0;
    468 		break;
    469 
    470 	case IOC_LOCKSTAT_ENABLE:
    471 		le = (lsenable_t *)data;
    472 
    473 		if (!cpu_hascounter()) {
    474 			error = ENODEV;
    475 			break;
    476 		}
    477 		if (lockstat_enabled) {
    478 			error = EBUSY;
    479 			break;
    480 		}
    481 
    482 		/*
    483 		 * Sanitize the arguments passed in and set up filtering.
    484 		 */
    485 		if (le->le_nbufs == 0)
    486 			le->le_nbufs = LOCKSTAT_DEFBUFS;
    487 		else if (le->le_nbufs > LOCKSTAT_MAXBUFS ||
    488 		    le->le_nbufs < LOCKSTAT_MINBUFS) {
    489 			error = EINVAL;
    490 			break;
    491 		}
    492 		if ((le->le_flags & LE_ONE_CALLSITE) == 0) {
    493 			le->le_csstart = 0;
    494 			le->le_csend = le->le_csstart - 1;
    495 		}
    496 		if ((le->le_flags & LE_ONE_LOCK) == 0) {
    497 			le->le_lockstart = 0;
    498 			le->le_lockend = le->le_lockstart - 1;
    499 		}
    500 		if ((le->le_mask & LB_EVENT_MASK) == 0)
    501 			return EINVAL;
    502 		if ((le->le_mask & LB_LOCK_MASK) == 0)
    503 			return EINVAL;
    504 
    505 		/*
    506 		 * Start tracing.
    507 		 */
    508 		if ((error = lockstat_alloc(le)) == 0)
    509 			lockstat_start(le);
    510 		break;
    511 
    512 	case IOC_LOCKSTAT_DISABLE:
    513 		if (!lockstat_enabled)
    514 			error = EINVAL;
    515 		else
    516 			error = lockstat_stop((lsdisable_t *)data);
    517 		break;
    518 
    519 	default:
    520 		error = ENOTTY;
    521 		break;
    522 	}
    523 
    524 	return error;
    525 }
    526 
    527 /*
    528  * Copy buffers out to user-space.
    529  */
    530 int
    531 lockstat_read(dev_t dev, struct uio *uio, int flag)
    532 {
    533 
    534 	if (curlwp != lockstat_lwp || lockstat_enabled)
    535 		return EBUSY;
    536 	return uiomove(lockstat_baseb, lockstat_sizeb, uio);
    537 }
    538