Home | History | Annotate | Line # | Download | only in dev
lockstat.h revision 1.11
      1 /*	$NetBSD: lockstat.h,v 1.11 2015/03/08 22:45:16 christos 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _SYS_LOCKSTAT_H_
     33 #define _SYS_LOCKSTAT_H_
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_dtrace.h"
     37 #include <lockstat.h>
     38 #endif
     39 
     40 #include <sys/types.h>
     41 #include <sys/ioccom.h>
     42 #include <sys/queue.h>
     43 #include <sys/time.h>
     44 
     45 #if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER)
     46 #include <machine/cpu_counter.h>
     47 #endif
     48 
     49 /*
     50  * Interface version.  The interface is not designed to provide
     51  * compatibility across NetBSD releases.
     52  */
     53 
     54 #define	IOC_LOCKSTAT_GVERSION	_IOR('L', 0, int)
     55 
     56 #define	LS_VERSION	5
     57 
     58 /*
     59  * Enable request.  We can limit tracing by the call site and by
     60  * the lock.  We also specify the number of event buffers to
     61  * allocate up front, and what kind of events to track.
     62  */
     63 
     64 #define	IOC_LOCKSTAT_ENABLE	_IOW('L', 1, lsenable_t)
     65 
     66 #define LE_CALLSITE	0x01		/* track call sites */
     67 #define	LE_ONE_CALLSITE	0x02		/* specific call site */
     68 #define	LE_ONE_LOCK	0x04		/* specific lock */
     69 #define LE_LOCK		0x08		/* track locks */
     70 
     71 typedef struct lsenable {
     72 	uintptr_t	le_csstart;	/* callsite start */
     73 	uintptr_t	le_csend;	/* callsite end */
     74 	uintptr_t	le_lockstart;	/* lock address start */
     75 	uintptr_t	le_lockend;	/* lock address end */
     76 	uintptr_t	le_nbufs;	/* buffers to allocate, 0 = default */
     77 	u_int		le_flags;	/* request flags */
     78 	u_int		le_mask;	/* event mask (LB_*) */
     79 } lsenable_t;
     80 
     81 /*
     82  * Disable request.
     83  */
     84 
     85 #define	IOC_LOCKSTAT_DISABLE	_IOR('L', 2, lsdisable_t)
     86 
     87 typedef struct lsdisable {
     88 	size_t		ld_size;	/* buffer space allocated */
     89 	struct timespec	ld_time;	/* time spent enabled */
     90 	uint64_t	ld_freq[64];	/* counter HZ by CPU number */
     91 } lsdisable_t;
     92 
     93 /*
     94  * Event buffers returned from reading from the devices.
     95  */
     96 
     97 /*
     98  * Event types, for lockstat_event().  Stored in lb_flags but should be
     99  * meaningless to the consumer, also provided with the enable request
    100  * in le_mask.
    101  */
    102 #define	LB_SPIN			0x00000001
    103 #define	LB_SLEEP1		0x00000002
    104 #define	LB_SLEEP2		0x00000003
    105 #define	LB_NEVENT		0x00000003
    106 #define	LB_EVENT_MASK		0x000000ff
    107 
    108 /*
    109  * Lock types, the only part of lb_flags that should be inspected.  Also
    110  * provided with the enable request in le_mask.
    111  */
    112 #define	LB_ADAPTIVE_MUTEX	0x00000100
    113 #define	LB_SPIN_MUTEX		0x00000200
    114 #define	LB_RWLOCK		0x00000300
    115 #define	LB_NOPREEMPT		0x00000400
    116 #define	LB_KERNEL_LOCK		0x00000500
    117 #define	LB_MISC			0x00000600
    118 #define	LB_NLOCK		0x00000600
    119 #define	LB_LOCK_MASK		0x0000ff00
    120 #define	LB_LOCK_SHIFT		8
    121 
    122 typedef struct lsbuf {
    123 	union {
    124 		LIST_ENTRY(lsbuf) list;
    125 		SLIST_ENTRY(lsbuf) slist;
    126 		TAILQ_ENTRY(lsbuf) tailq;
    127 	} lb_chain;
    128 	uintptr_t	lb_lock;		/* lock address */
    129 	uintptr_t	lb_callsite;		/* call site */
    130 	uint64_t	lb_times[LB_NEVENT];	/* cumulative times */
    131 	uint32_t	lb_counts[LB_NEVENT];	/* count of events */
    132 	uint16_t	lb_flags;		/* lock type */
    133 	uint16_t	lb_cpu;			/* CPU number */
    134 } lsbuf_t;
    135 
    136 /*
    137  * Tracing stubs used by lock providers.
    138  */
    139 
    140 #if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER) && NLOCKSTAT > 0
    141 
    142 #define	LOCKSTAT_EVENT(flag, lock, type, count, time)			\
    143 do {									\
    144 	if (__predict_false(flag))					\
    145 		lockstat_event((uintptr_t)(lock),			\
    146 		    (uintptr_t)__builtin_return_address(0),		\
    147 		    (type), (count), (time));				\
    148 } while (/* CONSTCOND */ 0);
    149 
    150 #define	LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra)		\
    151 do {									\
    152 	if (__predict_false(flag))					\
    153 		lockstat_event((uintptr_t)(lock), (uintptr_t)ra,	\
    154 		    (type), (count), (time));				\
    155 } while (/* CONSTCOND */ 0);
    156 
    157 #define	LOCKSTAT_TIMER(name)	uint64_t name = 0
    158 #define	LOCKSTAT_COUNTER(name)	uint64_t name = 0
    159 #define	LOCKSTAT_FLAG(name)	int name
    160 #define	LOCKSTAT_ENTER(name)	name = lockstat_enabled
    161 #define	LOCKSTAT_EXIT(name)
    162 
    163 #define	LOCKSTAT_START_TIMER(flag, name)				\
    164 do {									\
    165 	if (__predict_false(flag))					\
    166 		(name) -= cpu_counter();				\
    167 } while (/* CONSTCOND */ 0)
    168 
    169 #define	LOCKSTAT_STOP_TIMER(flag, name)					\
    170 do {									\
    171 	if (__predict_false(flag))					\
    172 		(name) += cpu_counter();				\
    173 } while (/* CONSTCOND */ 0)
    174 
    175 #define	LOCKSTAT_COUNT(name, inc)					\
    176 do {									\
    177 	(name) += (inc);						\
    178 } while (/* CONSTCOND */ 0)
    179 
    180 void	lockstat_event(uintptr_t, uintptr_t, u_int, u_int, uint64_t);
    181 
    182 extern volatile u_int	lockstat_enabled;
    183 
    184 #else
    185 
    186 #define	LOCKSTAT_FLAG(name)					/* nothing */
    187 #define	LOCKSTAT_ENTER(name)					/* nothing */
    188 #define	LOCKSTAT_EXIT(name)					/* nothing */
    189 #define	LOCKSTAT_EVENT(flag, lock, type, count, time)		/* nothing */
    190 #define	LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra)	/* nothing */
    191 #define	LOCKSTAT_TIMER(void)					/* nothing */
    192 #define	LOCKSTAT_COUNTER(void)					/* nothing */
    193 #define	LOCKSTAT_START_TIMER(flag, void)			/* nothing */
    194 #define	LOCKSTAT_STOP_TIMER(flag, void)				/* nothing */
    195 #define	LOCKSTAT_COUNT(name, int)				/* nothing */
    196 
    197 #endif
    198 
    199 #ifdef KDTRACE_HOOKS
    200 #define LS_COMPRESS(f) \
    201     ((((f) & 3) | (((f) & 7) >> 6)) & (LS_NPROBES - 1))
    202 #define	LS_NPROBES	0x20	/* 5 bits */
    203 
    204 extern uint32_t	lockstat_probemap[];
    205 extern void	(*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
    206     uintptr_t, uintptr_t, uintptr_t);
    207 
    208 void		lockstat_probe_stub(uint32_t, uintptr_t, uintptr_t,
    209     uintptr_t, uintptr_t, uintptr_t);
    210 #endif
    211 
    212 #endif	/* _SYS_LOCKSTAT_H_ */
    213