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 5*9d68b18eSck142721 * Common Development and Distribution License (the "License"). 6*9d68b18eSck142721 * 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 /* 22*9d68b18eSck142721 * 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 */ 131*9d68b18eSck142721 132*9d68b18eSck142721 /* used for 32 bit systems to avoid overflow */ 133*9d68b18eSck142721 #if defined(_ILP32) 134*9d68b18eSck142721 #define CLAMP32(x) ((x) > UINT_MAX ? UINT_MAX : (x)) 135*9d68b18eSck142721 #else 136*9d68b18eSck142721 #define CLAMP32(x) (x) 137*9d68b18eSck142721 #endif 138*9d68b18eSck142721 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 156*9d68b18eSck142721 /* 157*9d68b18eSck142721 * Return timestamp for start of busy-waiting (for spin probes) 158*9d68b18eSck142721 */ 159*9d68b18eSck142721 #define LOCKSTAT_START_TIME(probe) ( \ 160*9d68b18eSck142721 lockstat_probemap[(probe)] ? gethrtime() : 0 \ 161*9d68b18eSck142721 ) 162*9d68b18eSck142721 163*9d68b18eSck142721 /* 164*9d68b18eSck142721 * Record elapsed time since LOCKSTAT_START_TIME was called if the 165*9d68b18eSck142721 * probe is enabled at start and end, else return 0. t_start must 166*9d68b18eSck142721 * be the value returned by LOCKSTAT_START_TIME. 167*9d68b18eSck142721 */ 168*9d68b18eSck142721 #define LOCKSTAT_RECORD_TIME(probe, lp, t_start) \ 169*9d68b18eSck142721 if (lockstat_probemap[(probe)]) { \ 170*9d68b18eSck142721 dtrace_id_t id; \ 171*9d68b18eSck142721 hrtime_t t_spin = (t_start); \ 172*9d68b18eSck142721 curthread->t_lockstat++; \ 173*9d68b18eSck142721 membar_enter(); \ 174*9d68b18eSck142721 if ((id = lockstat_probemap[(probe)]) != 0) { \ 175*9d68b18eSck142721 if (t_spin) { \ 176*9d68b18eSck142721 t_spin = gethrtime() - t_spin; \ 177*9d68b18eSck142721 t_spin = CLAMP32(t_spin); \ 178*9d68b18eSck142721 } \ 179*9d68b18eSck142721 (*lockstat_probe)(id, (uintptr_t)(lp), t_spin, \ 180*9d68b18eSck142721 0, 0, 0); \ 181*9d68b18eSck142721 } \ 182*9d68b18eSck142721 curthread->t_lockstat--; \ 183*9d68b18eSck142721 } 184*9d68b18eSck142721 185*9d68b18eSck142721 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