Home | History | Annotate | Line # | Download | only in dev
lockstat.c revision 1.8.2.3
      1 /*	$NetBSD: lockstat.c,v 1.8.2.3 2007/10/29 00:22:43 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.8.2.3 2007/10/29 00:22:43 ad Exp $");
     51 
     52 #include <sys/types.h>
     53 #include <sys/param.h>
     54 #include <sys/lock.h>
     55 #include <sys/proc.h>
     56 #include <sys/resourcevar.h>
     57 #include <sys/systm.h>
     58 #include <sys/kernel.h>
     59 #include <sys/kmem.h>
     60 #include <sys/conf.h>
     61 #include <sys/syslog.h>
     62 
     63 #include <dev/lockstat.h>
     64 
     65 #ifndef __HAVE_CPU_COUNTER
     66 #error CPU counters not available
     67 #endif
     68 
     69 #if LONG_BIT == 64
     70 #define	LOCKSTAT_HASH_SHIFT	3
     71 #elif LONG_BIT == 32
     72 #define	LOCKSTAT_HASH_SHIFT	2
     73 #endif
     74 
     75 #define	LOCKSTAT_MINBUFS	1000
     76 #define	LOCKSTAT_DEFBUFS	10000
     77 #define	LOCKSTAT_MAXBUFS	50000
     78 
     79 #define	LOCKSTAT_HASH_SIZE	128
     80 #define	LOCKSTAT_HASH_MASK	(LOCKSTAT_HASH_SIZE - 1)
     81 #define	LOCKSTAT_HASH(key)	\
     82 	((key >> LOCKSTAT_HASH_SHIFT) & LOCKSTAT_HASH_MASK)
     83 
     84 typedef struct lscpu {
     85 	SLIST_HEAD(, lsbuf)	lc_free;
     86 	u_int			lc_overflow;
     87 	LIST_HEAD(lslist, lsbuf) lc_hash[LOCKSTAT_HASH_SIZE];
     88 } lscpu_t;
     89 
     90 typedef struct lslist lslist_t;
     91 
     92 void	lockstatattach(int);
     93 void	lockstat_start(lsenable_t *);
     94 int	lockstat_alloc(lsenable_t *);
     95 void	lockstat_init_tables(lsenable_t *);
     96 int	lockstat_stop(lsdisable_t *);
     97 void	lockstat_free(void);
     98 
     99 dev_type_open(lockstat_open);
    100 dev_type_close(lockstat_close);
    101 dev_type_read(lockstat_read);
    102 dev_type_ioctl(lockstat_ioctl);
    103 
    104 volatile u_int	lockstat_enabled;
    105 uintptr_t	lockstat_csstart;
    106 uintptr_t	lockstat_csend;
    107 uintptr_t	lockstat_csmask;
    108 uintptr_t	lockstat_lamask;
    109 uintptr_t	lockstat_lockstart;
    110 uintptr_t	lockstat_lockend;
    111 __cpu_simple_lock_t lockstat_lock;
    112 lsbuf_t		*lockstat_baseb;
    113 size_t		lockstat_sizeb;
    114 int		lockstat_busy;
    115 struct timespec	lockstat_stime;
    116 
    117 const struct cdevsw lockstat_cdevsw = {
    118 	lockstat_open, lockstat_close, lockstat_read, nowrite, lockstat_ioctl,
    119 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE
    120 };
    121 
    122 /*
    123  * Called when the pseudo-driver is attached.
    124  */
    125 void
    126 lockstatattach(int nunits)
    127 {
    128 
    129 	(void)nunits;
    130 
    131 	__cpu_simple_lock_init(&lockstat_lock);
    132 }
    133 
    134 /*
    135  * Prepare the per-CPU tables for use, or clear down tables when tracing is
    136  * stopped.
    137  */
    138 void
    139 lockstat_init_tables(lsenable_t *le)
    140 {
    141 	int i, per, slop, cpuno;
    142 	CPU_INFO_ITERATOR cii;
    143 	struct cpu_info *ci;
    144 	lscpu_t *lc;
    145 	lsbuf_t *lb;
    146 
    147 	KASSERT(!lockstat_enabled);
    148 
    149 	for (CPU_INFO_FOREACH(cii, ci)) {
    150 		if (ci->ci_lockstat != NULL) {
    151 			kmem_free(ci->ci_lockstat, sizeof(lscpu_t));
    152 			ci->ci_lockstat = NULL;
    153 		}
    154 	}
    155 
    156 	if (le == NULL)
    157 		return;
    158 
    159 	lb = lockstat_baseb;
    160 	per = le->le_nbufs / ncpu;
    161 	slop = le->le_nbufs - (per * ncpu);
    162 	cpuno = 0;
    163 	for (CPU_INFO_FOREACH(cii, ci)) {
    164 		lc = kmem_alloc(sizeof(*lc), KM_SLEEP);
    165 		lc->lc_overflow = 0;
    166 		ci->ci_lockstat = lc;
    167 
    168 		SLIST_INIT(&lc->lc_free);
    169 		for (i = 0; i < LOCKSTAT_HASH_SIZE; i++)
    170 			LIST_INIT(&lc->lc_hash[i]);
    171 
    172 		for (i = per; i != 0; i--, lb++) {
    173 			lb->lb_cpu = (uint16_t)cpuno;
    174 			SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
    175 		}
    176 		if (--slop > 0) {
    177 			lb->lb_cpu = (uint16_t)cpuno;
    178 			SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
    179 			lb++;
    180 		}
    181 		cpuno++;
    182 	}
    183 }
    184 
    185 /*
    186  * Start collecting lock statistics.
    187  */
    188 void
    189 lockstat_start(lsenable_t *le)
    190 {
    191 
    192 	KASSERT(!lockstat_enabled);
    193 
    194 	lockstat_init_tables(le);
    195 
    196 	if ((le->le_flags & LE_CALLSITE) != 0)
    197 		lockstat_csmask = (uintptr_t)-1LL;
    198 	else
    199 		lockstat_csmask = 0;
    200 
    201 	if ((le->le_flags & LE_LOCK) != 0)
    202 		lockstat_lamask = (uintptr_t)-1LL;
    203 	else
    204 		lockstat_lamask = 0;
    205 
    206 	lockstat_csstart = le->le_csstart;
    207 	lockstat_csend = le->le_csend;
    208 	lockstat_lockstart = le->le_lockstart;
    209 	lockstat_lockstart = le->le_lockstart;
    210 	lockstat_lockend = le->le_lockend;
    211 	mb_memory();
    212 	getnanotime(&lockstat_stime);
    213 	lockstat_enabled = le->le_mask;
    214 	mb_write();
    215 }
    216 
    217 /*
    218  * Stop collecting lock statistics.
    219  */
    220 int
    221 lockstat_stop(lsdisable_t *ld)
    222 {
    223 	CPU_INFO_ITERATOR cii;
    224 	struct cpu_info *ci;
    225 	u_int cpuno, overflow;
    226 	struct timespec ts;
    227 	int error;
    228 
    229 	KASSERT(lockstat_enabled);
    230 
    231 	/*
    232 	 * Set enabled false, force a write barrier, and wait for other CPUs
    233 	 * to exit lockstat_event().
    234 	 */
    235 	lockstat_enabled = 0;
    236 	mb_write();
    237 	getnanotime(&ts);
    238 	tsleep(&lockstat_stop, PPAUSE, "lockstat", mstohz(10));
    239 
    240 	/*
    241 	 * Did we run out of buffers while tracing?
    242 	 */
    243 	overflow = 0;
    244 	for (CPU_INFO_FOREACH(cii, ci))
    245 		overflow += ((lscpu_t *)ci->ci_lockstat)->lc_overflow;
    246 
    247 	if (overflow != 0) {
    248 		error = EOVERFLOW;
    249 		log(LOG_NOTICE, "lockstat: %d buffer allocations failed\n",
    250 		    overflow);
    251 	} else
    252 		error = 0;
    253 
    254 	lockstat_init_tables(NULL);
    255 
    256 	if (ld == NULL)
    257 		return error;
    258 
    259 	/*
    260 	 * Fill out the disable struct for the caller.
    261 	 */
    262 	timespecsub(&ts, &lockstat_stime, &ld->ld_time);
    263 	ld->ld_size = lockstat_sizeb;
    264 
    265 	cpuno = 0;
    266 	for (CPU_INFO_FOREACH(cii, ci)) {
    267 		if (cpuno > sizeof(ld->ld_freq) / sizeof(ld->ld_freq[0])) {
    268 			log(LOG_WARNING, "lockstat: too many CPUs\n");
    269 			break;
    270 		}
    271 		ld->ld_freq[cpuno++] = cpu_frequency(ci);
    272 	}
    273 
    274 	return error;
    275 }
    276 
    277 /*
    278  * Allocate buffers for lockstat_start().
    279  */
    280 int
    281 lockstat_alloc(lsenable_t *le)
    282 {
    283 	lsbuf_t *lb;
    284 	size_t sz;
    285 
    286 	KASSERT(!lockstat_enabled);
    287 	lockstat_free();
    288 
    289 	sz = sizeof(*lb) * le->le_nbufs;
    290 
    291 	lb = kmem_zalloc(sz, KM_SLEEP);
    292 	if (lb == NULL)
    293 		return (ENOMEM);
    294 
    295 	KASSERT(!lockstat_enabled);
    296 	KASSERT(lockstat_baseb == NULL);
    297 	lockstat_sizeb = sz;
    298 	lockstat_baseb = lb;
    299 
    300 	return (0);
    301 }
    302 
    303 /*
    304  * Free allocated buffers after tracing has stopped.
    305  */
    306 void
    307 lockstat_free(void)
    308 {
    309 
    310 	KASSERT(!lockstat_enabled);
    311 
    312 	if (lockstat_baseb != NULL) {
    313 		kmem_free(lockstat_baseb, lockstat_sizeb);
    314 		lockstat_baseb = NULL;
    315 	}
    316 }
    317 
    318 /*
    319  * Main entry point from lock primatives.
    320  */
    321 void
    322 lockstat_event(uintptr_t lock, uintptr_t callsite, u_int flags, u_int count,
    323 	       uint64_t cycles)
    324 {
    325 	lslist_t *ll;
    326 	lscpu_t *lc;
    327 	lsbuf_t *lb;
    328 	u_int event;
    329 	int s;
    330 
    331 	if ((flags & lockstat_enabled) != flags || count == 0)
    332 		return;
    333 	if (lock < lockstat_lockstart || lock > lockstat_lockend)
    334 		return;
    335 	if (callsite < lockstat_csstart || callsite > lockstat_csend)
    336 		return;
    337 
    338 	callsite &= lockstat_csmask;
    339 	lock &= lockstat_lamask;
    340 
    341 	/*
    342 	 * Find the table for this lock+callsite pair, and try to locate a
    343 	 * buffer with the same key.
    344 	 */
    345 	s = splhigh();
    346 	lc = curcpu()->ci_lockstat;
    347 	ll = &lc->lc_hash[LOCKSTAT_HASH(lock ^ callsite)];
    348 	event = (flags & LB_EVENT_MASK) - 1;
    349 
    350 	LIST_FOREACH(lb, ll, lb_chain.list) {
    351 		if (lb->lb_lock == lock && lb->lb_callsite == callsite)
    352 			break;
    353 	}
    354 
    355 	if (lb != NULL) {
    356 		/*
    357 		 * We found a record.  Move it to the front of the list, as
    358 		 * we're likely to hit it again soon.
    359 		 */
    360 		if (lb != LIST_FIRST(ll)) {
    361 			LIST_REMOVE(lb, lb_chain.list);
    362 			LIST_INSERT_HEAD(ll, lb, lb_chain.list);
    363 		}
    364 		lb->lb_counts[event] += count;
    365 		lb->lb_times[event] += cycles;
    366 	} else if ((lb = SLIST_FIRST(&lc->lc_free)) != NULL) {
    367 		/*
    368 		 * Pinch a new buffer and fill it out.
    369 		 */
    370 		SLIST_REMOVE_HEAD(&lc->lc_free, lb_chain.slist);
    371 		LIST_INSERT_HEAD(ll, lb, lb_chain.list);
    372 		lb->lb_flags = (uint16_t)flags;
    373 		lb->lb_lock = lock;
    374 		lb->lb_callsite = callsite;
    375 		lb->lb_counts[event] = count;
    376 		lb->lb_times[event] = cycles;
    377 	} else {
    378 		/*
    379 		 * We didn't find a buffer and there were none free.
    380 		 * lockstat_stop() will notice later on and report the
    381 		 * error.
    382 		 */
    383 		 lc->lc_overflow++;
    384 	}
    385 
    386 	splx(s);
    387 }
    388 
    389 /*
    390  * Accept an open() on /dev/lockstat.
    391  */
    392 int
    393 lockstat_open(dev_t dev, int flag, int mode, lwp_t *l)
    394 {
    395 
    396 	if (!__cpu_simple_lock_try(&lockstat_lock))
    397 		return EBUSY;
    398 	return 0;
    399 }
    400 
    401 /*
    402  * Accept the last close() on /dev/lockstat.
    403  */
    404 int
    405 lockstat_close(dev_t dev, int flag, int mode, lwp_t *l)
    406 {
    407 
    408 	__cpu_simple_unlock(&lockstat_lock);
    409 	return 0;
    410 }
    411 
    412 /*
    413  * Handle control operations.
    414  */
    415 int
    416 lockstat_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    417 {
    418 	lsenable_t *le;
    419 	int error;
    420 
    421 	switch (cmd) {
    422 	case IOC_LOCKSTAT_GVERSION:
    423 		*(int *)data = LS_VERSION;
    424 		error = 0;
    425 		break;
    426 
    427 	case IOC_LOCKSTAT_ENABLE:
    428 		le = (lsenable_t *)data;
    429 
    430 		if (!cpu_hascounter()) {
    431 			error = ENODEV;
    432 			break;
    433 		}
    434 		if (lockstat_enabled) {
    435 			error = EBUSY;
    436 			break;
    437 		}
    438 
    439 		/*
    440 		 * Sanitize the arguments passed in and set up filtering.
    441 		 */
    442 		if (le->le_nbufs == 0)
    443 			le->le_nbufs = LOCKSTAT_DEFBUFS;
    444 		else if (le->le_nbufs > LOCKSTAT_MAXBUFS ||
    445 		    le->le_nbufs < LOCKSTAT_MINBUFS) {
    446 			error = EINVAL;
    447 			break;
    448 		}
    449 		if ((le->le_flags & LE_ONE_CALLSITE) == 0) {
    450 			le->le_csstart = 0;
    451 			le->le_csend = le->le_csstart - 1;
    452 		}
    453 		if ((le->le_flags & LE_ONE_LOCK) == 0) {
    454 			le->le_lockstart = 0;
    455 			le->le_lockend = le->le_lockstart - 1;
    456 		}
    457 		if ((le->le_mask & LB_EVENT_MASK) == 0)
    458 			return EINVAL;
    459 		if ((le->le_mask & LB_LOCK_MASK) == 0)
    460 			return EINVAL;
    461 
    462 		/*
    463 		 * Start tracing.
    464 		 */
    465 		if ((error = lockstat_alloc(le)) == 0)
    466 			lockstat_start(le);
    467 		break;
    468 
    469 	case IOC_LOCKSTAT_DISABLE:
    470 		if (!lockstat_enabled)
    471 			error = EINVAL;
    472 		else
    473 			error = lockstat_stop((lsdisable_t *)data);
    474 		break;
    475 
    476 	default:
    477 		error = ENOTTY;
    478 		break;
    479 	}
    480 
    481 	return error;
    482 }
    483 
    484 /*
    485  * Copy buffers out to user-space.
    486  */
    487 int
    488 lockstat_read(dev_t dev, struct uio *uio, int flag)
    489 {
    490 
    491 	if (lockstat_enabled)
    492 		return EBUSY;
    493 	return uiomove(lockstat_baseb, lockstat_sizeb, uio);
    494 }
    495