1192853Ssson/*-
2192853Ssson * Copyright (c) 2008-2009 Stacey Son <sson@FreeBSD.org>
3192853Ssson *
4192853Ssson * Redistribution and use in source and binary forms, with or without
5192853Ssson * modification, are permitted provided that the following conditions
6192853Ssson * are met:
7192853Ssson * 1. Redistributions of source code must retain the above copyright
8192853Ssson *    notice, this list of conditions and the following disclaimer.
9192853Ssson * 2. Redistributions in binary form must reproduce the above copyright
10192853Ssson *    notice, this list of conditions and the following disclaimer in the
11192853Ssson *    documentation and/or other materials provided with the distribution.
12192853Ssson *
13192853Ssson * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14192853Ssson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15192853Ssson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16192853Ssson * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17192853Ssson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18192853Ssson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19192853Ssson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20192853Ssson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21192853Ssson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22192853Ssson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23192853Ssson * SUCH DAMAGE.
24192853Ssson *
25192853Ssson * $FreeBSD$
26192853Ssson */
27192853Ssson
28192853Ssson/*
29192853Ssson * DTrace lockstat provider definitions
30192853Ssson *
31192853Ssson */
32192853Ssson
33192853Ssson#ifndef	_SYS_LOCKSTAT_H
34192853Ssson#define	_SYS_LOCKSTAT_H
35192853Ssson
36192853Ssson#ifdef	_KERNEL
37192853Ssson
38192853Ssson/*
39192853Ssson * Spin Locks
40192853Ssson */
41192853Ssson#define	LS_MTX_SPIN_LOCK_ACQUIRE	0
42192853Ssson#define	LS_MTX_SPIN_UNLOCK_RELEASE	1
43192853Ssson#define	LS_MTX_SPIN_LOCK_SPIN		2
44192853Ssson
45192853Ssson/*
46192853Ssson * Adaptive Locks
47192853Ssson */
48192853Ssson#define	LS_MTX_LOCK_ACQUIRE		3
49192853Ssson#define	LS_MTX_UNLOCK_RELEASE		4
50192853Ssson#define	LS_MTX_LOCK_SPIN		5
51192853Ssson#define	LS_MTX_LOCK_BLOCK		6
52192853Ssson#define	LS_MTX_TRYLOCK_ACQUIRE		7
53192853Ssson
54192853Ssson/*
55192853Ssson * Reader/Writer Locks
56192853Ssson */
57192853Ssson#define	LS_RW_RLOCK_ACQUIRE		8
58192853Ssson#define	LS_RW_RUNLOCK_RELEASE		9
59192853Ssson#define	LS_RW_WLOCK_ACQUIRE		10
60192853Ssson#define	LS_RW_WUNLOCK_RELEASE		11
61192853Ssson#define	LS_RW_RLOCK_SPIN		12
62192853Ssson#define	LS_RW_RLOCK_BLOCK		13
63192853Ssson#define	LS_RW_WLOCK_SPIN		14
64192853Ssson#define	LS_RW_WLOCK_BLOCK		15
65192853Ssson#define	LS_RW_TRYUPGRADE_UPGRADE	16
66192853Ssson#define	LS_RW_DOWNGRADE_DOWNGRADE	17
67192853Ssson
68192853Ssson/*
69192853Ssson * Shared/Exclusive Locks
70192853Ssson */
71192853Ssson#define	LS_SX_SLOCK_ACQUIRE		18
72192853Ssson#define	LS_SX_SUNLOCK_RELEASE		19
73192853Ssson#define	LS_SX_XLOCK_ACQUIRE		20
74192853Ssson#define	LS_SX_XUNLOCK_RELEASE		21
75192853Ssson#define	LS_SX_SLOCK_SPIN		22
76192853Ssson#define	LS_SX_SLOCK_BLOCK		23
77192853Ssson#define	LS_SX_XLOCK_SPIN		24
78192853Ssson#define	LS_SX_XLOCK_BLOCK		25
79192853Ssson#define	LS_SX_TRYUPGRADE_UPGRADE	26
80192853Ssson#define	LS_SX_DOWNGRADE_DOWNGRADE	27
81192853Ssson
82192853Ssson/*
83192853Ssson * Thread Locks
84192853Ssson */
85192853Ssson#define	LS_THREAD_LOCK_SPIN		28
86192853Ssson
87192853Ssson/*
88192853Ssson * Lockmanager Locks
89192853Ssson *  According to locking(9) Lockmgr locks are "Largely deprecated"
90192853Ssson *  so no support for these have been added in the lockstat provider.
91192853Ssson */
92192853Ssson
93192853Ssson#define	LS_NPROBES			29
94192853Ssson
95192853Ssson#define	LS_MTX_LOCK			"mtx_lock"
96192853Ssson#define	LS_MTX_UNLOCK			"mtx_unlock"
97192853Ssson#define	LS_MTX_SPIN_LOCK		"mtx_lock_spin"
98192853Ssson#define	LS_MTX_SPIN_UNLOCK		"mtx_unlock_spin"
99192853Ssson#define	LS_MTX_TRYLOCK			"mtx_trylock"
100192853Ssson#define	LS_RW_RLOCK			"rw_rlock"
101192853Ssson#define	LS_RW_WLOCK			"rw_wlock"
102192853Ssson#define	LS_RW_RUNLOCK			"rw_runlock"
103192853Ssson#define	LS_RW_WUNLOCK			"rw_wunlock"
104192853Ssson#define	LS_RW_TRYUPGRADE		"rw_try_upgrade"
105192853Ssson#define	LS_RW_DOWNGRADE			"rw_downgrade"
106192853Ssson#define	LS_SX_SLOCK			"sx_slock"
107192853Ssson#define	LS_SX_XLOCK			"sx_xlock"
108192853Ssson#define	LS_SX_SUNLOCK			"sx_sunlock"
109192853Ssson#define	LS_SX_XUNLOCK			"sx_xunlock"
110192853Ssson#define	LS_SX_TRYUPGRADE		"sx_try_upgrade"
111192853Ssson#define	LS_SX_DOWNGRADE			"sx_downgrade"
112192853Ssson#define	LS_THREAD_LOCK			"thread_lock"
113192853Ssson
114192853Ssson#define	LS_ACQUIRE			"acquire"
115192853Ssson#define	LS_RELEASE			"release"
116192853Ssson#define	LS_SPIN				"spin"
117192853Ssson#define	LS_BLOCK			"block"
118192853Ssson#define	LS_UPGRADE			"upgrade"
119192853Ssson#define	LS_DOWNGRADE			"downgrade"
120192853Ssson
121192853Ssson#define	LS_TYPE_ADAPTIVE		"adaptive"
122192853Ssson#define	LS_TYPE_SPIN			"spin"
123192853Ssson#define	LS_TYPE_THREAD			"thread"
124192853Ssson#define	LS_TYPE_RW			"rw"
125192853Ssson#define	LS_TYPE_SX			"sx"
126192853Ssson
127192853Ssson#define	LSA_ACQUIRE			(LS_TYPE_ADAPTIVE "-" LS_ACQUIRE)
128192853Ssson#define	LSA_RELEASE			(LS_TYPE_ADAPTIVE "-" LS_RELEASE)
129192853Ssson#define	LSA_SPIN			(LS_TYPE_ADAPTIVE "-" LS_SPIN)
130192853Ssson#define	LSA_BLOCK			(LS_TYPE_ADAPTIVE "-" LS_BLOCK)
131192853Ssson#define	LSS_ACQUIRE			(LS_TYPE_SPIN "-" LS_ACQUIRE)
132192853Ssson#define	LSS_RELEASE			(LS_TYPE_SPIN "-" LS_RELEASE)
133192853Ssson#define	LSS_SPIN			(LS_TYPE_SPIN "-" LS_SPIN)
134192853Ssson#define	LSR_ACQUIRE			(LS_TYPE_RW "-" LS_ACQUIRE)
135192853Ssson#define	LSR_RELEASE			(LS_TYPE_RW "-" LS_RELEASE)
136192853Ssson#define	LSR_BLOCK			(LS_TYPE_RW "-" LS_BLOCK)
137192853Ssson#define	LSR_SPIN			(LS_TYPE_RW "-" LS_SPIN)
138192853Ssson#define	LSR_UPGRADE			(LS_TYPE_RW "-" LS_UPGRADE)
139192853Ssson#define	LSR_DOWNGRADE			(LS_TYPE_RW "-" LS_DOWNGRADE)
140192853Ssson#define	LSX_ACQUIRE			(LS_TYPE_SX "-" LS_ACQUIRE)
141192853Ssson#define	LSX_RELEASE			(LS_TYPE_SX "-" LS_RELEASE)
142192853Ssson#define	LSX_BLOCK			(LS_TYPE_SX "-" LS_BLOCK)
143192853Ssson#define	LSX_SPIN			(LS_TYPE_SX "-" LS_SPIN)
144192853Ssson#define	LSX_UPGRADE			(LS_TYPE_SX "-" LS_UPGRADE)
145192853Ssson#define	LSX_DOWNGRADE			(LS_TYPE_SX "-" LS_DOWNGRADE)
146192853Ssson#define	LST_SPIN			(LS_TYPE_THREAD "-" LS_SPIN)
147192853Ssson
148192853Ssson/*
149192853Ssson * The following must match the type definition of dtrace_probe.  It is
150192853Ssson * defined this way to avoid having to rely on CDDL code.
151192853Ssson */
152192853Sssonextern uint32_t lockstat_probemap[LS_NPROBES];
153192853Sssontypedef void (*lockstat_probe_func_t)(uint32_t, uintptr_t arg0, uintptr_t arg1,
154192853Ssson    uintptr_t arg2, uintptr_t arg3, uintptr_t arg4);
155192853Sssonextern lockstat_probe_func_t lockstat_probe_func;
156192853Sssonextern uint64_t lockstat_nsecs(void);
157192853Ssson
158192853Ssson#ifdef	KDTRACE_HOOKS
159192853Ssson/*
160192853Ssson * Macros to record lockstat probes.
161192853Ssson */
162192853Ssson#define	LOCKSTAT_RECORD4(probe, lp, arg1, arg2, arg3, arg4)  do {	\
163192853Ssson	uint32_t id;							\
164192853Ssson									\
165192853Ssson	if ((id = lockstat_probemap[(probe)])) 				\
166192853Ssson	    (*lockstat_probe_func)(id, (uintptr_t)(lp), (arg1),	(arg2),	\
167192853Ssson		(arg3), (arg4));					\
168192853Ssson} while (0)
169192853Ssson
170192853Ssson#define	LOCKSTAT_RECORD(probe, lp, arg1) \
171192853Ssson	LOCKSTAT_RECORD4(probe, lp, arg1, 0, 0, 0)
172192853Ssson
173192853Ssson#define	LOCKSTAT_RECORD0(probe, lp)     \
174192853Ssson	LOCKSTAT_RECORD4(probe, lp, 0, 0, 0, 0)
175192853Ssson
176192853Ssson#define	LOCKSTAT_RECORD1(probe, lp, arg1) \
177192853Ssson	LOCKSTAT_RECORD4(probe, lp, arg1, 0, 0, 0)
178192853Ssson
179192853Ssson#define	LOCKSTAT_RECORD2(probe, lp, arg1, arg2) \
180192853Ssson	LOCKSTAT_RECORD4(probe, lp, arg1, arg2, 0, 0)
181192853Ssson
182192853Ssson#define	LOCKSTAT_RECORD3(probe, lp, arg1, arg2, arg3) \
183192853Ssson	LOCKSTAT_RECORD4(probe, lp, arg1, arg2, arg3, 0)
184192853Ssson
185192853Ssson#define	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(probe, lp, c, wt, f, l)  do {   \
186192853Ssson	uint32_t id;							     \
187192853Ssson									     \
188228448Sattilio    	lock_profile_obtain_lock_success(&(lp)->lock_object, c, wt, f, l);   \
189228448Sattilio	if ((id = lockstat_probemap[(probe)])) 			     	     \
190228448Sattilio		(*lockstat_probe_func)(id, (uintptr_t)(lp), 0, 0, 0, 0);     \
191192853Ssson} while (0)
192192853Ssson
193192853Ssson#define	LOCKSTAT_PROFILE_RELEASE_LOCK(probe, lp)  do {			     \
194192853Ssson	uint32_t id;							     \
195192853Ssson									     \
196228448Sattilio	lock_profile_release_lock(&(lp)->lock_object);			     \
197228448Sattilio	if ((id = lockstat_probemap[(probe)])) 			     	     \
198228448Sattilio		(*lockstat_probe_func)(id, (uintptr_t)(lp), 0, 0, 0, 0);     \
199192853Ssson} while (0)
200192853Ssson
201192853Ssson#else	/* !KDTRACE_HOOKS */
202192853Ssson
203192853Ssson#define	LOCKSTAT_RECORD(probe, lp, arg1)
204192853Ssson#define	LOCKSTAT_RECORD0(probe, lp)
205192853Ssson#define	LOCKSTAT_RECORD1(probe, lp, arg1)
206192853Ssson#define	LOCKSTAT_RECORD2(probe, lp, arg1, arg2)
207192853Ssson#define	LOCKSTAT_RECORD3(probe, lp, arg1, arg2, arg3)
208192853Ssson#define	LOCKSTAT_RECORD4(probe, lp, arg1, arg2, arg3, arg4)
209192853Ssson
210192853Ssson#define	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(probe, lp, c, wt, f, l)	\
211192853Ssson	lock_profile_obtain_lock_success(&(lp)->lock_object, c, wt, f, l)
212192853Ssson
213192853Ssson#define	LOCKSTAT_PROFILE_RELEASE_LOCK(probe, lp)  			\
214192853Ssson	lock_profile_release_lock(&(lp)->lock_object)
215192853Ssson
216192853Ssson#endif	/* !KDTRACE_HOOKS */
217192853Ssson
218192853Ssson#endif	/* _KERNEL */
219192853Ssson
220192853Ssson#endif	/* _SYS_LOCKSTAT_H */
221