4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/errno.h>
30 #include <sys/ioctl.h>
32 #include <sys/fcntl.h>
33 #include <miscfs/devfs/devfs.h>
35 #include <sys/dtrace.h>
36 #include <sys/dtrace_impl.h>
38 #include <sys/dtrace_glue.h>
40 #include <sys/lockstat.h>
42 #include <kern/lock_stat.h>
44 #include <kern/processor.h>
46 #define membar_producer dtrace_membar_producer
48 #define PROBE_ARGS0(a, b, c, d, e) "\000"
49 #define PROBE_ARGS1(a, b, c, d, e) a "\000"
50 #define PROBE_ARGS2(a, b, c, d, e) a "\000" b "\000"
51 #define PROBE_ARGS3(a, b, c, d, e) a "\000" b "\000" c "\000"
52 #define PROBE_ARGS4(a, b, c, d, e) a "\000" b "\000" c "\000" d "\000"
53 #define PROBE_ARGS5(a, b, c, d, e) a "\000" b "\000" c "\000" d "\000" e "\000"
54 #define PROBE_ARGS_(a, b, c, d, e, n, ...) PROBE_ARGS##n(a, b, c, d, e)
55 #define PROBE_ARGS(...) PROBE_ARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
57 #define LOCKSTAT_PROBE(func, name, probe, ...) \
58 {func, name, probe, DTRACE_IDNONE, PROBE_ARGS(__VA_ARGS__)}
61 * Hot patch values, x86
63 #if defined(__x86_64__)
66 #define LOCKSTAT_AFRAMES 1
67 #elif defined(__arm__)
68 #define NOP 0xE1A00000
69 #define BXLR 0xE12FFF1E
70 #define LOCKSTAT_AFRAMES 2
71 #elif defined(__arm64__)
72 #define NOP 0xD503201F
73 #define RET 0xD65f03c0
74 #define LOCKSTAT_AFRAMES 2
76 #error "not ported to this architecture"
79 typedef struct lockstat_probe
{
87 lockstat_probe_t lockstat_probes
[] =
89 #if defined(__x86_64__)
90 /* Only provide implemented probes for each architecture */
91 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_LOCK_ACQUIRE
, "lck_mtx_t"),
92 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK
, LSA_SPIN
, LS_LCK_MTX_LOCK_SPIN
, "lck_mtx_t", "uint64_t"),
93 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK
, LSA_BLOCK
, LS_LCK_MTX_LOCK_BLOCK
, "lck_mtx_t", "uint64_t"),
94 LOCKSTAT_PROBE(LS_LCK_MTX_TRY_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_TRY_LOCK_ACQUIRE
, "lck_mtx_t"),
95 LOCKSTAT_PROBE(LS_LCK_MTX_TRY_SPIN_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE
, "lck_mtx_t"),
96 LOCKSTAT_PROBE(LS_LCK_MTX_UNLOCK
, LSA_RELEASE
, LS_LCK_MTX_UNLOCK_RELEASE
, "lck_mtx_t"),
97 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_EXT_LOCK_ACQUIRE
, "lck_mtx_t"),
98 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK
, LSA_SPIN
, LS_LCK_MTX_EXT_LOCK_SPIN
, "lck_mtx_t", "uint64_t"),
99 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK
, LSA_BLOCK
, LS_LCK_MTX_EXT_LOCK_BLOCK
, "lck_mtx_t", "uint64_t"),
100 // LOCKSTAT_PROBE(LS_LCK_MTX_EXT_TRY_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_EXT_LOCK_ACQUIRE)
101 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_UNLOCK
, LSA_RELEASE
, LS_LCK_MTX_EXT_UNLOCK_RELEASE
, "lck_mtx_t"),
102 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK_SPIN_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_LOCK_SPIN_ACQUIRE
, "lck_mtx_t"),
103 // TODO: This should not be a uint64_t !
104 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED
, LSR_ACQUIRE
, LS_LCK_RW_LOCK_SHARED_ACQUIRE
, "lck_rw_t", "uint64_t"),
105 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED
, LSR_BLOCK
, LS_LCK_RW_LOCK_SHARED_BLOCK
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
106 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED
, LSR_SPIN
, LS_LCK_RW_LOCK_SHARED_SPIN
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
107 // TODO: This should NOT be a uint64_t
108 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL
, LSR_ACQUIRE
, LS_LCK_RW_LOCK_EXCL_ACQUIRE
, "lck_rw_t", "uint64_t"),
109 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL
, LSR_BLOCK
, LS_LCK_RW_LOCK_EXCL_BLOCK
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
110 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL
, LSR_SPIN
, LS_LCK_RW_LOCK_EXCL_SPIN
, "lck_rw_t", "uint64_t", "int"),
111 LOCKSTAT_PROBE(LS_LCK_RW_DONE
, LSR_RELEASE
, LS_LCK_RW_DONE_RELEASE
, "lck_rw_t", "_Bool"),
112 // TODO : This should NOT be a uint64_t
113 LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_SHARED
, LSR_ACQUIRE
, LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE
, "lck_rw_t", "uint64_t"),
115 LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_EXCL
, LSR_ACQUIRE
, LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE
, "lck_rw_t", "uint64_t"),
116 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL
, LSR_UPGRADE
, LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
, "lck_rw_t", "_Bool"),
117 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL
, LSR_SPIN
, LS_LCK_RW_LOCK_SHARED_TO_EXCL_SPIN
, "lck_rw_t", "uint64_t"),
118 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL
, LSR_BLOCK
, LS_LCK_RW_LOCK_SHARED_TO_EXCL_BLOCK
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
119 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL_TO_SHARED
, LSR_DOWNGRADE
, LS_LCK_RW_LOCK_EXCL_TO_SHARED_DOWNGRADE
, "lck_rw_t"),
120 //TODO : Separate the probes for the hw_bit from the probe for the normal hw locks
121 LOCKSTAT_PROBE(LS_LCK_SPIN_LOCK
, LSS_ACQUIRE
, LS_LCK_SPIN_LOCK_ACQUIRE
, "hw_lock_t"),
122 LOCKSTAT_PROBE(LS_LCK_SPIN_LOCK
, LSS_SPIN
, LS_LCK_SPIN_LOCK_SPIN
, "hw_lock_t", "uint64_t", "uint64_t"),
123 LOCKSTAT_PROBE(LS_LCK_SPIN_UNLOCK
, LSS_RELEASE
, LS_LCK_SPIN_UNLOCK_RELEASE
, "hw_lock_t"),
124 #elif defined(__arm__) || defined(__arm64__)
125 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_LOCK_ACQUIRE
, "lck_mtx_t"),
126 // LOCKSTAT_PROBE(LS_LCK_MTX_LOCK, LSA_SPIN, LS_LCK_MTX_LOCK_SPIN, "lck_mtx_t", "uint64_t"),
127 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK
, LSA_BLOCK
, LS_LCK_MTX_LOCK_BLOCK
, "lck_mtx_t", "uint64_t"),
128 LOCKSTAT_PROBE(LS_LCK_MTX_TRY_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_TRY_LOCK_ACQUIRE
, "lck_mtx_t"),
129 // LOCKSTAT_PROBE(LS_LCK_MTX_TRY_SPIN_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, "lck_mtx_t"),
130 LOCKSTAT_PROBE(LS_LCK_MTX_UNLOCK
, LSA_RELEASE
, LS_LCK_MTX_UNLOCK_RELEASE
, "lck_mtx_t"),
131 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK
, LSA_ACQUIRE
, LS_LCK_MTX_EXT_LOCK_ACQUIRE
, "lck_mtx_t"),
132 // LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK, LSA_SPIN, LS_LCK_MTX_EXT_LOCK_SPIN, "lck_mtx_t", "uint64_t"),
133 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK
, LSA_BLOCK
, LS_LCK_MTX_EXT_LOCK_BLOCK
, "lck_mtx_t", "uint64_t"),
134 // LOCKSTAT_PROBE(LS_LCK_MTX_EXT_TRY_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_EXT_LOCK_ACQUIRE)
135 // LOCKSTAT_PROBE(LS_LCK_MTX_EXT_UNLOCK, LSA_RELEASE, LS_LCK_MTX_EXT_UNLOCK_RELEASE, "lck_mtx_t"),
136 // LOCKSTAT_PROBE(LS_LCK_MTX_LOCK_SPIN_LOCK, LSA_ACQUIRE, LS_LCK_MTX_LOCK_SPIN_ACQUIRE, "lck_mtx_t"),
137 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED
, LSR_ACQUIRE
, LS_LCK_RW_LOCK_SHARED_ACQUIRE
, "lck_rw_t", "uint64_t"),
138 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED
, LSR_BLOCK
, LS_LCK_RW_LOCK_SHARED_BLOCK
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
139 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED
, LSR_SPIN
, LS_LCK_RW_LOCK_SHARED_SPIN
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
140 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL
, LSR_ACQUIRE
, LS_LCK_RW_LOCK_EXCL_ACQUIRE
, "lck_rw_t", "uint64_t"),
141 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL
, LSR_BLOCK
, LS_LCK_RW_LOCK_EXCL_BLOCK
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
142 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL
, LSR_SPIN
, LS_LCK_RW_LOCK_EXCL_SPIN
, "lck_rw_t", "uint64_t", "int"),
143 LOCKSTAT_PROBE(LS_LCK_RW_DONE
, LSR_RELEASE
, LS_LCK_RW_DONE_RELEASE
, "lck_rw_t", "_Bool"),
144 // TODO : This should NOT be a uint64_t
145 LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_SHARED
, LSR_ACQUIRE
, LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE
, "lck_rw_t", "uint64_t"),
147 LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_EXCL
, LSR_ACQUIRE
, LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE
, "lck_rw_t", "uint64_t"),
148 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL
, LSR_UPGRADE
, LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE
, "lck_rw_t", "_Bool"),
149 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL
, LSR_SPIN
, LS_LCK_RW_LOCK_SHARED_TO_EXCL_SPIN
, "lck_rw_t", "uint64_t"),
150 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL
, LSR_BLOCK
, LS_LCK_RW_LOCK_SHARED_TO_EXCL_BLOCK
, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
151 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL_TO_SHARED
, LSR_DOWNGRADE
, LS_LCK_RW_LOCK_EXCL_TO_SHARED_DOWNGRADE
, "lck_rw_t"),
152 //TODO : Separate the probes for the hw_bit from the probe for the normal hw locks
153 LOCKSTAT_PROBE(LS_LCK_SPIN_LOCK
, LSS_ACQUIRE
, LS_LCK_SPIN_LOCK_ACQUIRE
, "hw_lock_t"),
154 LOCKSTAT_PROBE(LS_LCK_SPIN_LOCK
, LSS_SPIN
, LS_LCK_SPIN_LOCK_SPIN
, "hw_lock_t", "uint64_t", "uint64_t"),
155 LOCKSTAT_PROBE(LS_LCK_SPIN_UNLOCK
, LSS_RELEASE
, LS_LCK_SPIN_UNLOCK_RELEASE
, "hw_lock_t"),
157 /* Interlock measurements would be nice, but later */
160 LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL_TO_SHARED
, LSA_ILK_SPIN
, LS_LCK_RW_LOCK_EXCL_TO_SHARED_ILK_SPIN
),
161 LOCKSTAT_PROBE(LS_LCK_MTX_LOCK
, LSA_ILK_SPIN
, LS_LCK_MTX_LOCK_ILK_SPIN
),
162 LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK
, LSA_ILK_SPIN
, LS_LCK_MTX_EXT_LOCK_ILK_SPIN
),
163 LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_EXCL
, LSA_ILK_SPIN
, LS_LCK_RW_TRY_LOCK_EXCL_ILK_SPIN
),
164 LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_SHARED
, LSA_SPIN
, LS_LCK_RW_TRY_LOCK_SHARED_SPIN
)
168 NULL
, NULL
, 0, 0, NULL
172 dtrace_id_t lockstat_probemap
[LS_NPROBES
];
173 void (*lockstat_probe
)(dtrace_id_t
, uint64_t, uint64_t,
174 uint64_t, uint64_t, uint64_t);
176 static dtrace_provider_id_t lockstat_id
;
180 lockstat_enable(void *arg
, dtrace_id_t id
, void *parg
)
182 #pragma unused(arg) /* __APPLE__ */
184 lockstat_probe_t
*probe
= parg
;
186 ASSERT(!lockstat_probemap
[probe
->lsp_probe
]);
188 lockstat_probemap
[probe
->lsp_probe
] = id
;
197 lockstat_disable(void *arg
, dtrace_id_t id
, void *parg
)
199 #pragma unused(arg, id) /* __APPLE__ */
201 lockstat_probe_t
*probe
= parg
;
204 ASSERT(lockstat_probemap
[probe
->lsp_probe
]);
206 lockstat_probemap
[probe
->lsp_probe
] = 0;
210 * See if we have any probes left enabled.
212 for (i
= 0; i
< LS_NPROBES
; i
++) {
213 if (lockstat_probemap
[i
]) {
215 * This probe is still enabled. We don't need to deal
216 * with waiting for all threads to be out of the
217 * lockstat critical sections; just return.
226 lockstat_provide(void *arg
, const dtrace_probedesc_t
*desc
)
228 #pragma unused(arg, desc) /* __APPLE__ */
232 for (i
= 0; lockstat_probes
[i
].lsp_func
!= NULL
; i
++) {
233 lockstat_probe_t
*probe
= &lockstat_probes
[i
];
235 if (dtrace_probe_lookup(lockstat_id
, "mach_kernel",
236 probe
->lsp_func
, probe
->lsp_name
) != 0) {
240 ASSERT(!probe
->lsp_id
);
241 probe
->lsp_id
= dtrace_probe_create(lockstat_id
,
242 "mach_kernel", probe
->lsp_func
, probe
->lsp_name
,
243 LOCKSTAT_AFRAMES
, probe
);
250 lockstat_destroy(void *arg
, dtrace_id_t id
, void *parg
)
252 #pragma unused(arg, id) /* __APPLE__ */
254 lockstat_probe_t
*probe
= parg
;
256 ASSERT(!lockstat_probemap
[probe
->lsp_probe
]);
261 lockstat_getargdesc(void *arg
, dtrace_id_t id
, void *parg
, dtrace_argdesc_t
*desc
)
263 #pragma unused(arg, id)
264 lockstat_probe_t
*probe
= parg
;
265 const char* argdesc
= probe
->lsp_args
;
268 desc
->dtargd_native
[0] = '\0';
269 desc
->dtargd_xlate
[0] = '\0';
271 while (argdesc
[0] != '\0') {
272 if (narg
== desc
->dtargd_ndx
) {
273 strlcpy(desc
->dtargd_native
, argdesc
, DTRACE_ARGTYPELEN
);
276 argdesc
+= strlen(argdesc
) + 1;
280 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
283 static dtrace_pattr_t lockstat_attr
= {
284 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
285 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
286 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
287 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
288 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
291 static dtrace_pops_t lockstat_pops
= {
292 .dtps_provide
= lockstat_provide
,
293 .dtps_provide_module
= NULL
,
294 .dtps_enable
= lockstat_enable
,
295 .dtps_disable
= lockstat_disable
,
296 .dtps_suspend
= NULL
,
298 .dtps_getargdesc
= lockstat_getargdesc
,
299 .dtps_getargval
= NULL
,
300 .dtps_usermode
= NULL
,
301 .dtps_destroy
= lockstat_destroy
305 lockstat_attach(dev_info_t
*devi
)
307 if (ddi_create_minor_node(devi
, "lockstat", S_IFCHR
, 0,
308 DDI_PSEUDO
, 0) == DDI_FAILURE
||
309 dtrace_register("lockstat", &lockstat_attr
, DTRACE_PRIV_KERNEL
,
310 NULL
, &lockstat_pops
, NULL
, &lockstat_id
) != 0) {
311 ddi_remove_minor_node(devi
, NULL
);
315 lockstat_probe
= dtrace_probe
;
321 d_open_t _lockstat_open
;
324 _lockstat_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
326 #pragma unused(dev,flags,devtype,p)
330 #define LOCKSTAT_MAJOR -24 /* let the kernel pick the device number */
333 * A struct describing which functions will get invoked for certain
336 static struct cdevsw lockstat_cdevsw
=
338 _lockstat_open
, /* open */
339 eno_opcl
, /* close */
340 eno_rdwrt
, /* read */
341 eno_rdwrt
, /* write */
342 eno_ioctl
, /* ioctl */
343 (stop_fcn_t
*)nulldev
, /* stop */
344 (reset_fcn_t
*)nulldev
, /* reset */
346 eno_select
, /* select */
348 eno_strat
, /* strategy */
354 void lockstat_init( void );
357 lockstat_init( void )
359 int majdevno
= cdevsw_add(LOCKSTAT_MAJOR
, &lockstat_cdevsw
);
362 printf("lockstat_init: failed to allocate a major number!\n");
366 lockstat_attach((dev_info_t
*)(uintptr_t)majdevno
);
368 #undef LOCKSTAT_MAJOR