xref: /illumos-gate/usr/src/uts/common/sys/lockstat.h (revision ee6ee36a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59d68b18eSck142721  * Common Development and Distribution License (the "License").
69d68b18eSck142721  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229d68b18eSck142721  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef _SYS_LOCKSTAT_H
277c478bd9Sstevel@tonic-gate #define	_SYS_LOCKSTAT_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
327c478bd9Sstevel@tonic-gate extern "C" {
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER_ACQUIRE		0
367c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER_BLOCK		1
377c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER_SPIN		2
387c478bd9Sstevel@tonic-gate #define	LS_MUTEX_EXIT_RELEASE		3
397c478bd9Sstevel@tonic-gate #define	LS_MUTEX_DESTROY_RELEASE	4
407c478bd9Sstevel@tonic-gate #define	LS_MUTEX_TRYENTER_ACQUIRE	5
417c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_ACQUIRE		6
427c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPIN		7
437c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPL_ACQUIRE		8
447c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPL_SPIN		9
457c478bd9Sstevel@tonic-gate #define	LS_LOCK_TRY_ACQUIRE		10
467c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR_RELEASE		11
477c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR_SPLX_RELEASE	12
487c478bd9Sstevel@tonic-gate #define	LS_CLOCK_UNLOCK_RELEASE		13
497c478bd9Sstevel@tonic-gate #define	LS_RW_ENTER_ACQUIRE		14
507c478bd9Sstevel@tonic-gate #define	LS_RW_ENTER_BLOCK		15
517c478bd9Sstevel@tonic-gate #define	LS_RW_EXIT_RELEASE		16
527c478bd9Sstevel@tonic-gate #define	LS_RW_TRYENTER_ACQUIRE		17
537c478bd9Sstevel@tonic-gate #define	LS_RW_TRYUPGRADE_UPGRADE	18
547c478bd9Sstevel@tonic-gate #define	LS_RW_DOWNGRADE_DOWNGRADE	19
557c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_ACQUIRE		20
567c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_SPIN		21
577c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_HIGH_ACQUIRE	22
587c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_HIGH_SPIN	23
597c478bd9Sstevel@tonic-gate #define	LS_TURNSTILE_INTERLOCK_SPIN	24
607c478bd9Sstevel@tonic-gate #define	LS_NPROBES			25
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER			"mutex_enter"
637c478bd9Sstevel@tonic-gate #define	LS_MUTEX_EXIT			"mutex_exit"
647c478bd9Sstevel@tonic-gate #define	LS_MUTEX_DESTROY		"mutex_destroy"
657c478bd9Sstevel@tonic-gate #define	LS_MUTEX_TRYENTER		"mutex_tryenter"
667c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET			"lock_set"
677c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPL			"lock_set_spl"
687c478bd9Sstevel@tonic-gate #define	LS_LOCK_TRY			"lock_try"
697c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR			"lock_clear"
707c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR_SPLX		"lock_clear_splx"
717c478bd9Sstevel@tonic-gate #define	LS_CLOCK_UNLOCK			"CLOCK_UNLOCK"
727c478bd9Sstevel@tonic-gate #define	LS_RW_ENTER			"rw_enter"
737c478bd9Sstevel@tonic-gate #define	LS_RW_EXIT			"rw_exit"
747c478bd9Sstevel@tonic-gate #define	LS_RW_TRYENTER			"rw_tryenter"
757c478bd9Sstevel@tonic-gate #define	LS_RW_TRYUPGRADE		"rw_tryupgrade"
767c478bd9Sstevel@tonic-gate #define	LS_RW_DOWNGRADE			"rw_downgrade"
777c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK			"thread_lock"
787c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_HIGH		"thread_lock_high"
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	LS_ACQUIRE			"acquire"
817c478bd9Sstevel@tonic-gate #define	LS_RELEASE			"release"
827c478bd9Sstevel@tonic-gate #define	LS_SPIN				"spin"
837c478bd9Sstevel@tonic-gate #define	LS_BLOCK			"block"
847c478bd9Sstevel@tonic-gate #define	LS_UPGRADE			"upgrade"
857c478bd9Sstevel@tonic-gate #define	LS_DOWNGRADE			"downgrade"
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	LS_TYPE_ADAPTIVE		"adaptive"
887c478bd9Sstevel@tonic-gate #define	LS_TYPE_SPIN			"spin"
897c478bd9Sstevel@tonic-gate #define	LS_TYPE_THREAD			"thread"
907c478bd9Sstevel@tonic-gate #define	LS_TYPE_RW			"rw"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	LSA_ACQUIRE			(LS_TYPE_ADAPTIVE "-" LS_ACQUIRE)
937c478bd9Sstevel@tonic-gate #define	LSA_RELEASE			(LS_TYPE_ADAPTIVE "-" LS_RELEASE)
947c478bd9Sstevel@tonic-gate #define	LSA_SPIN			(LS_TYPE_ADAPTIVE "-" LS_SPIN)
957c478bd9Sstevel@tonic-gate #define	LSA_BLOCK			(LS_TYPE_ADAPTIVE "-" LS_BLOCK)
967c478bd9Sstevel@tonic-gate #define	LSS_ACQUIRE			(LS_TYPE_SPIN "-" LS_ACQUIRE)
977c478bd9Sstevel@tonic-gate #define	LSS_RELEASE			(LS_TYPE_SPIN "-" LS_RELEASE)
987c478bd9Sstevel@tonic-gate #define	LSS_SPIN			(LS_TYPE_SPIN "-" LS_SPIN)
997c478bd9Sstevel@tonic-gate #define	LSR_ACQUIRE			(LS_TYPE_RW "-" LS_ACQUIRE)
1007c478bd9Sstevel@tonic-gate #define	LSR_RELEASE			(LS_TYPE_RW "-" LS_RELEASE)
1017c478bd9Sstevel@tonic-gate #define	LSR_BLOCK			(LS_TYPE_RW "-" LS_BLOCK)
1027c478bd9Sstevel@tonic-gate #define	LSR_UPGRADE			(LS_TYPE_RW "-" LS_UPGRADE)
1037c478bd9Sstevel@tonic-gate #define	LSR_DOWNGRADE			(LS_TYPE_RW "-" LS_DOWNGRADE)
1047c478bd9Sstevel@tonic-gate #define	LST_SPIN			(LS_TYPE_THREAD "-" LS_SPIN)
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #ifndef _ASM
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #include <sys/types.h>
1097c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
1107c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1117c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Platform-independent kernel support for the lockstat driver.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate extern dtrace_id_t lockstat_probemap[LS_NPROBES];
1197c478bd9Sstevel@tonic-gate extern void (*lockstat_probe)(dtrace_id_t, uintptr_t, uintptr_t,
1207c478bd9Sstevel@tonic-gate     uintptr_t, uintptr_t, uintptr_t);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate extern int lockstat_active_threads(void);
1237c478bd9Sstevel@tonic-gate extern int lockstat_depth(void);
124*ee6ee36aSPatrick Mooney extern void lockstat_hotpatch_probe(int);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Macros to record lockstat probes.
1287c478bd9Sstevel@tonic-gate  */
1299d68b18eSck142721 
1309d68b18eSck142721 /* used for 32 bit systems to avoid overflow */
1319d68b18eSck142721 #if defined(_ILP32)
1329d68b18eSck142721 #define	CLAMP32(x)	((x) > UINT_MAX ? UINT_MAX : (x))
1339d68b18eSck142721 #else
1349d68b18eSck142721 #define	CLAMP32(x)	(x)
1359d68b18eSck142721 #endif
1369d68b18eSck142721 
1377c478bd9Sstevel@tonic-gate #define	LOCKSTAT_RECORD4(probe, lp, arg0, arg1, arg2, arg3)		\
1387c478bd9Sstevel@tonic-gate 	if (lockstat_probemap[(probe)]) {				\
1397c478bd9Sstevel@tonic-gate 		dtrace_id_t id;						\
1407c478bd9Sstevel@tonic-gate 		curthread->t_lockstat++;				\
1417c478bd9Sstevel@tonic-gate 		membar_enter();						\
1427c478bd9Sstevel@tonic-gate 		if ((id = lockstat_probemap[(probe)]) != 0)		\
1437c478bd9Sstevel@tonic-gate 			(*lockstat_probe)(id, (uintptr_t)(lp), (arg0),	\
1447c478bd9Sstevel@tonic-gate 			    (arg1), (arg2), (arg3));			\
1457c478bd9Sstevel@tonic-gate 		curthread->t_lockstat--;				\
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #define	LOCKSTAT_RECORD(probe, lp, arg)	\
1497c478bd9Sstevel@tonic-gate 	LOCKSTAT_RECORD4(probe, lp, arg, 0, 0, 0)
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #define	LOCKSTAT_RECORD0(probe, lp)	\
1527c478bd9Sstevel@tonic-gate 	LOCKSTAT_RECORD4(probe, lp, 0, 0, 0, 0)
1537c478bd9Sstevel@tonic-gate 
1549d68b18eSck142721 /*
1559d68b18eSck142721  * Return timestamp for start of busy-waiting (for spin probes)
1569d68b18eSck142721  */
1579d68b18eSck142721 #define	LOCKSTAT_START_TIME(probe)	(			\
158fb81f553Sck142721 	lockstat_probemap[(probe)] ? gethrtime_waitfree() : 0	\
1599d68b18eSck142721 )
1609d68b18eSck142721 
1619d68b18eSck142721 /*
1629d68b18eSck142721  * Record elapsed time since LOCKSTAT_START_TIME was called if the
1639d68b18eSck142721  * probe is enabled at start and end, else return 0. t_start must
1649d68b18eSck142721  * be the value returned by LOCKSTAT_START_TIME.
1659d68b18eSck142721  */
1669d68b18eSck142721 #define	LOCKSTAT_RECORD_TIME(probe, lp, t_start)		\
1679d68b18eSck142721 	if (lockstat_probemap[(probe)]) {				\
1689d68b18eSck142721 		dtrace_id_t id;						\
1699d68b18eSck142721 		hrtime_t t_spin = (t_start);				\
1709d68b18eSck142721 		curthread->t_lockstat++;				\
1719d68b18eSck142721 		membar_enter();						\
1729d68b18eSck142721 		if ((id = lockstat_probemap[(probe)]) != 0) {		\
1739d68b18eSck142721 			if (t_spin) {					\
174fb81f553Sck142721 				t_spin = gethrtime_waitfree() - t_spin;	\
1759d68b18eSck142721 				t_spin = CLAMP32(t_spin);		\
1769d68b18eSck142721 			}						\
1779d68b18eSck142721 			(*lockstat_probe)(id, (uintptr_t)(lp), t_spin,	\
1789d68b18eSck142721 			0, 0, 0);					\
1799d68b18eSck142721 		}							\
1809d68b18eSck142721 		curthread->t_lockstat--;				\
1819d68b18eSck142721 	}
1829d68b18eSck142721 
1839d68b18eSck142721 
1847c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #endif /* _ASM */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #endif	/* _SYS_LOCKSTAT_H */
193