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