Home | History | Annotate | Line # | Download | only in dev
lockstat.h revision 1.8
      1 /*	$NetBSD: lockstat.h,v 1.8 2008/04/28 15:36:01 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 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 #ifndef _SYS_LOCKSTAT_H_
     40 #define _SYS_LOCKSTAT_H_
     41 
     42 #ifdef _KERNEL_OPT
     43 #include <lockstat.h>
     44 #endif
     45 
     46 #include <sys/types.h>
     47 #include <sys/ioccom.h>
     48 #include <sys/queue.h>
     49 #include <sys/time.h>
     50 
     51 #if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER)
     52 #include <machine/cpu_counter.h>
     53 #endif
     54 
     55 /*
     56  * Interface version.  The interface is not designed to provide
     57  * compatibility across NetBSD releases.
     58  */
     59 
     60 #define	IOC_LOCKSTAT_GVERSION	_IOR('L', 0, int)
     61 
     62 #define	LS_VERSION	4
     63 
     64 /*
     65  * Enable request.  We can limit tracing by the call site and by
     66  * the lock.  We also specify the number of event buffers to
     67  * allocate up front, and what kind of events to track.
     68  */
     69 
     70 #define	IOC_LOCKSTAT_ENABLE	_IOW('L', 1, lsenable_t)
     71 
     72 #define LE_CALLSITE	0x01		/* track call sites */
     73 #define	LE_ONE_CALLSITE	0x02		/* specific call site */
     74 #define	LE_ONE_LOCK	0x04		/* specific lock */
     75 #define LE_LOCK		0x08		/* track locks */
     76 
     77 typedef struct lsenable {
     78 	uintptr_t	le_csstart;	/* callsite start */
     79 	uintptr_t	le_csend;	/* callsite end */
     80 	uintptr_t	le_lockstart;	/* lock address start */
     81 	uintptr_t	le_lockend;	/* lock address end */
     82 	uintptr_t	le_nbufs;	/* buffers to allocate, 0 = default */
     83 	u_int		le_flags;	/* request flags */
     84 	u_int		le_mask;	/* event mask (LB_*) */
     85 } lsenable_t;
     86 
     87 /*
     88  * Disable request.
     89  */
     90 
     91 #define	IOC_LOCKSTAT_DISABLE	_IOR('L', 2, lsdisable_t)
     92 
     93 typedef struct lsdisable {
     94 	size_t		ld_size;	/* buffer space allocated */
     95 	struct timespec	ld_time;	/* time spent enabled */
     96 	uint64_t	ld_freq[64];	/* counter HZ by CPU number */
     97 } lsdisable_t;
     98 
     99 /*
    100  * Event buffers returned from reading from the devices.
    101  */
    102 
    103 /*
    104  * Event types, for lockstat_event().  Stored in lb_flags but should be
    105  * meaningless to the consumer, also provided with the enable request
    106  * in le_mask.
    107  */
    108 #define	LB_SPIN			0x00000001
    109 #define	LB_SLEEP1		0x00000002
    110 #define	LB_SLEEP2		0x00000003
    111 #define	LB_NEVENT		0x00000003
    112 #define	LB_EVENT_MASK		0x000000ff
    113 
    114 /*
    115  * Lock types, the only part of lb_flags that should be inspected.  Also
    116  * provided with the enable request in le_mask.
    117  */
    118 #define	LB_ADAPTIVE_MUTEX	0x00000100
    119 #define	LB_SPIN_MUTEX		0x00000200
    120 #define	LB_RWLOCK		0x00000300
    121 #define	LB_NOPREEMPT		0x00000400
    122 #define	LB_KERNEL_LOCK		0x00000500
    123 #define	LB_MISC			0x00000600
    124 #define	LB_NLOCK		0x00000600
    125 #define	LB_LOCK_MASK		0x0000ff00
    126 #define	LB_LOCK_SHIFT		8
    127 
    128 typedef struct lsbuf {
    129 	union {
    130 		LIST_ENTRY(lsbuf) list;
    131 		SLIST_ENTRY(lsbuf) slist;
    132 		TAILQ_ENTRY(lsbuf) tailq;
    133 	} lb_chain;
    134 	uintptr_t	lb_lock;		/* lock address */
    135 	uintptr_t	lb_callsite;		/* call site */
    136 	uint64_t	lb_times[LB_NEVENT];	/* cumulative times */
    137 	uint32_t	lb_counts[LB_NEVENT];	/* count of events */
    138 	uint16_t	lb_flags;		/* lock type */
    139 	uint16_t	lb_cpu;			/* CPU number */
    140 } lsbuf_t;
    141 
    142 /*
    143  * Tracing stubs used by lock providers.
    144  */
    145 
    146 #if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER) && NLOCKSTAT > 0
    147 
    148 #define	LOCKSTAT_EVENT(flag, lock, type, count, time)			\
    149 do {									\
    150 	if (__predict_false(flag))					\
    151 		lockstat_event((uintptr_t)(lock),			\
    152 		    (uintptr_t)__builtin_return_address(0),		\
    153 		    (type), (count), (time));				\
    154 } while (/* CONSTCOND */ 0);
    155 
    156 #define	LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra)		\
    157 do {									\
    158 	if (__predict_false(flag))					\
    159 		lockstat_event((uintptr_t)(lock), (uintptr_t)ra,	\
    160 		    (type), (count), (time));				\
    161 } while (/* CONSTCOND */ 0);
    162 
    163 #define	LOCKSTAT_TIMER(name)	uint64_t name = 0
    164 #define	LOCKSTAT_COUNTER(name)	uint64_t name = 0
    165 #define	LOCKSTAT_FLAG(name)	int name
    166 #define	LOCKSTAT_ENTER(name)	name = lockstat_enabled
    167 #define	LOCKSTAT_EXIT(name)
    168 
    169 #define	LOCKSTAT_START_TIMER(flag, name)				\
    170 do {									\
    171 	if (__predict_false(flag))					\
    172 		(name) -= cpu_counter();				\
    173 } while (/* CONSTCOND */ 0)
    174 
    175 #define	LOCKSTAT_STOP_TIMER(flag, name)					\
    176 do {									\
    177 	if (__predict_false(flag))					\
    178 		(name) += cpu_counter();				\
    179 } while (/* CONSTCOND */ 0)
    180 
    181 #define	LOCKSTAT_COUNT(name, inc)					\
    182 do {									\
    183 	(name) += (inc);						\
    184 } while (/* CONSTCOND */ 0)
    185 
    186 void	lockstat_event(uintptr_t, uintptr_t, u_int, u_int, uint64_t);
    187 
    188 extern volatile u_int	lockstat_enabled;
    189 
    190 #else
    191 
    192 #define	LOCKSTAT_FLAG(name)					/* nothing */
    193 #define	LOCKSTAT_ENTER(name)					/* nothing */
    194 #define	LOCKSTAT_EXIT(name)					/* nothing */
    195 #define	LOCKSTAT_EVENT(flag, lock, type, count, time)		/* nothing */
    196 #define	LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra)	/* nothing */
    197 #define	LOCKSTAT_TIMER(void)					/* nothing */
    198 #define	LOCKSTAT_COUNTER(void)					/* nothing */
    199 #define	LOCKSTAT_START_TIMER(flag, void)			/* nothing */
    200 #define	LOCKSTAT_STOP_TIMER(flag, void)				/* nothing */
    201 #define	LOCKSTAT_COUNT(name, int)				/* nothing */
    202 
    203 #endif
    204 
    205 #endif	/* _SYS_LOCKSTAT_H_ */
    206