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