xref: /titanic_51/usr/src/uts/common/sys/lockstat.h (revision fb81f5532aeec22da92ae11861736f38ef47528a)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
347c478bd9Sstevel@tonic-gate extern "C" {
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER_ACQUIRE		0
387c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER_BLOCK		1
397c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER_SPIN		2
407c478bd9Sstevel@tonic-gate #define	LS_MUTEX_EXIT_RELEASE		3
417c478bd9Sstevel@tonic-gate #define	LS_MUTEX_DESTROY_RELEASE	4
427c478bd9Sstevel@tonic-gate #define	LS_MUTEX_TRYENTER_ACQUIRE	5
437c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_ACQUIRE		6
447c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPIN		7
457c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPL_ACQUIRE		8
467c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPL_SPIN		9
477c478bd9Sstevel@tonic-gate #define	LS_LOCK_TRY_ACQUIRE		10
487c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR_RELEASE		11
497c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR_SPLX_RELEASE	12
507c478bd9Sstevel@tonic-gate #define	LS_CLOCK_UNLOCK_RELEASE		13
517c478bd9Sstevel@tonic-gate #define	LS_RW_ENTER_ACQUIRE		14
527c478bd9Sstevel@tonic-gate #define	LS_RW_ENTER_BLOCK		15
537c478bd9Sstevel@tonic-gate #define	LS_RW_EXIT_RELEASE		16
547c478bd9Sstevel@tonic-gate #define	LS_RW_TRYENTER_ACQUIRE		17
557c478bd9Sstevel@tonic-gate #define	LS_RW_TRYUPGRADE_UPGRADE	18
567c478bd9Sstevel@tonic-gate #define	LS_RW_DOWNGRADE_DOWNGRADE	19
577c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_ACQUIRE		20
587c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_SPIN		21
597c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_HIGH_ACQUIRE	22
607c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_HIGH_SPIN	23
617c478bd9Sstevel@tonic-gate #define	LS_TURNSTILE_INTERLOCK_SPIN	24
627c478bd9Sstevel@tonic-gate #define	LS_NPROBES			25
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	LS_MUTEX_ENTER			"mutex_enter"
657c478bd9Sstevel@tonic-gate #define	LS_MUTEX_EXIT			"mutex_exit"
667c478bd9Sstevel@tonic-gate #define	LS_MUTEX_DESTROY		"mutex_destroy"
677c478bd9Sstevel@tonic-gate #define	LS_MUTEX_TRYENTER		"mutex_tryenter"
687c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET			"lock_set"
697c478bd9Sstevel@tonic-gate #define	LS_LOCK_SET_SPL			"lock_set_spl"
707c478bd9Sstevel@tonic-gate #define	LS_LOCK_TRY			"lock_try"
717c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR			"lock_clear"
727c478bd9Sstevel@tonic-gate #define	LS_LOCK_CLEAR_SPLX		"lock_clear_splx"
737c478bd9Sstevel@tonic-gate #define	LS_CLOCK_UNLOCK			"CLOCK_UNLOCK"
747c478bd9Sstevel@tonic-gate #define	LS_RW_ENTER			"rw_enter"
757c478bd9Sstevel@tonic-gate #define	LS_RW_EXIT			"rw_exit"
767c478bd9Sstevel@tonic-gate #define	LS_RW_TRYENTER			"rw_tryenter"
777c478bd9Sstevel@tonic-gate #define	LS_RW_TRYUPGRADE		"rw_tryupgrade"
787c478bd9Sstevel@tonic-gate #define	LS_RW_DOWNGRADE			"rw_downgrade"
797c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK			"thread_lock"
807c478bd9Sstevel@tonic-gate #define	LS_THREAD_LOCK_HIGH		"thread_lock_high"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	LS_ACQUIRE			"acquire"
837c478bd9Sstevel@tonic-gate #define	LS_RELEASE			"release"
847c478bd9Sstevel@tonic-gate #define	LS_SPIN				"spin"
857c478bd9Sstevel@tonic-gate #define	LS_BLOCK			"block"
867c478bd9Sstevel@tonic-gate #define	LS_UPGRADE			"upgrade"
877c478bd9Sstevel@tonic-gate #define	LS_DOWNGRADE			"downgrade"
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	LS_TYPE_ADAPTIVE		"adaptive"
907c478bd9Sstevel@tonic-gate #define	LS_TYPE_SPIN			"spin"
917c478bd9Sstevel@tonic-gate #define	LS_TYPE_THREAD			"thread"
927c478bd9Sstevel@tonic-gate #define	LS_TYPE_RW			"rw"
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	LSA_ACQUIRE			(LS_TYPE_ADAPTIVE "-" LS_ACQUIRE)
957c478bd9Sstevel@tonic-gate #define	LSA_RELEASE			(LS_TYPE_ADAPTIVE "-" LS_RELEASE)
967c478bd9Sstevel@tonic-gate #define	LSA_SPIN			(LS_TYPE_ADAPTIVE "-" LS_SPIN)
977c478bd9Sstevel@tonic-gate #define	LSA_BLOCK			(LS_TYPE_ADAPTIVE "-" LS_BLOCK)
987c478bd9Sstevel@tonic-gate #define	LSS_ACQUIRE			(LS_TYPE_SPIN "-" LS_ACQUIRE)
997c478bd9Sstevel@tonic-gate #define	LSS_RELEASE			(LS_TYPE_SPIN "-" LS_RELEASE)
1007c478bd9Sstevel@tonic-gate #define	LSS_SPIN			(LS_TYPE_SPIN "-" LS_SPIN)
1017c478bd9Sstevel@tonic-gate #define	LSR_ACQUIRE			(LS_TYPE_RW "-" LS_ACQUIRE)
1027c478bd9Sstevel@tonic-gate #define	LSR_RELEASE			(LS_TYPE_RW "-" LS_RELEASE)
1037c478bd9Sstevel@tonic-gate #define	LSR_BLOCK			(LS_TYPE_RW "-" LS_BLOCK)
1047c478bd9Sstevel@tonic-gate #define	LSR_UPGRADE			(LS_TYPE_RW "-" LS_UPGRADE)
1057c478bd9Sstevel@tonic-gate #define	LSR_DOWNGRADE			(LS_TYPE_RW "-" LS_DOWNGRADE)
1067c478bd9Sstevel@tonic-gate #define	LST_SPIN			(LS_TYPE_THREAD "-" LS_SPIN)
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #ifndef _ASM
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #include <sys/types.h>
1117c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
1127c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1137c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * Platform-independent kernel support for the lockstat driver.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate extern dtrace_id_t lockstat_probemap[LS_NPROBES];
1217c478bd9Sstevel@tonic-gate extern void (*lockstat_probe)(dtrace_id_t, uintptr_t, uintptr_t,
1227c478bd9Sstevel@tonic-gate     uintptr_t, uintptr_t, uintptr_t);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate extern int lockstat_active_threads(void);
1257c478bd9Sstevel@tonic-gate extern int lockstat_depth(void);
1267c478bd9Sstevel@tonic-gate extern void lockstat_hot_patch(void);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Macros to record lockstat probes.
1307c478bd9Sstevel@tonic-gate  */
1319d68b18eSck142721 
1329d68b18eSck142721 /* used for 32 bit systems to avoid overflow */
1339d68b18eSck142721 #if defined(_ILP32)
1349d68b18eSck142721 #define	CLAMP32(x)	((x) > UINT_MAX ? UINT_MAX : (x))
1359d68b18eSck142721 #else
1369d68b18eSck142721 #define	CLAMP32(x)	(x)
1379d68b18eSck142721 #endif
1389d68b18eSck142721 
1397c478bd9Sstevel@tonic-gate #define	LOCKSTAT_RECORD4(probe, lp, arg0, arg1, arg2, arg3)		\
1407c478bd9Sstevel@tonic-gate 	if (lockstat_probemap[(probe)]) {				\
1417c478bd9Sstevel@tonic-gate 		dtrace_id_t id;						\
1427c478bd9Sstevel@tonic-gate 		curthread->t_lockstat++;				\
1437c478bd9Sstevel@tonic-gate 		membar_enter();						\
1447c478bd9Sstevel@tonic-gate 		if ((id = lockstat_probemap[(probe)]) != 0)		\
1457c478bd9Sstevel@tonic-gate 			(*lockstat_probe)(id, (uintptr_t)(lp), (arg0),	\
1467c478bd9Sstevel@tonic-gate 			    (arg1), (arg2), (arg3));			\
1477c478bd9Sstevel@tonic-gate 		curthread->t_lockstat--;				\
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define	LOCKSTAT_RECORD(probe, lp, arg)	\
1517c478bd9Sstevel@tonic-gate 	LOCKSTAT_RECORD4(probe, lp, arg, 0, 0, 0)
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #define	LOCKSTAT_RECORD0(probe, lp)	\
1547c478bd9Sstevel@tonic-gate 	LOCKSTAT_RECORD4(probe, lp, 0, 0, 0, 0)
1557c478bd9Sstevel@tonic-gate 
1569d68b18eSck142721 /*
1579d68b18eSck142721  * Return timestamp for start of busy-waiting (for spin probes)
1589d68b18eSck142721  */
1599d68b18eSck142721 #define	LOCKSTAT_START_TIME(probe)	(			\
160*fb81f553Sck142721 	lockstat_probemap[(probe)] ? gethrtime_waitfree() : 0	\
1619d68b18eSck142721 )
1629d68b18eSck142721 
1639d68b18eSck142721 /*
1649d68b18eSck142721  * Record elapsed time since LOCKSTAT_START_TIME was called if the
1659d68b18eSck142721  * probe is enabled at start and end, else return 0. t_start must
1669d68b18eSck142721  * be the value returned by LOCKSTAT_START_TIME.
1679d68b18eSck142721  */
1689d68b18eSck142721 #define	LOCKSTAT_RECORD_TIME(probe, lp, t_start)		\
1699d68b18eSck142721 	if (lockstat_probemap[(probe)]) {				\
1709d68b18eSck142721 		dtrace_id_t id;						\
1719d68b18eSck142721 		hrtime_t t_spin = (t_start);				\
1729d68b18eSck142721 		curthread->t_lockstat++;				\
1739d68b18eSck142721 		membar_enter();						\
1749d68b18eSck142721 		if ((id = lockstat_probemap[(probe)]) != 0) {		\
1759d68b18eSck142721 			if (t_spin) {					\
176*fb81f553Sck142721 				t_spin = gethrtime_waitfree() - t_spin;	\
1779d68b18eSck142721 				t_spin = CLAMP32(t_spin);		\
1789d68b18eSck142721 			} 						\
1799d68b18eSck142721 			(*lockstat_probe)(id, (uintptr_t)(lp), t_spin,	\
1809d68b18eSck142721 			0, 0, 0);					\
1819d68b18eSck142721 		} 							\
1829d68b18eSck142721 		curthread->t_lockstat--;				\
1839d68b18eSck142721 	}
1849d68b18eSck142721 
1859d68b18eSck142721 
1867c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #endif /* _ASM */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate #endif
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #endif	/* _SYS_LOCKSTAT_H */
195