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