xref: /xnu-8020.140.41/bsd/dev/dtrace/lockstat.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/errno.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <sys/conf.h>
32 #include <sys/fcntl.h>
33 #include <miscfs/devfs/devfs.h>
34 
35 #include <sys/dtrace.h>
36 #include <sys/dtrace_impl.h>
37 
38 #include <sys/dtrace_glue.h>
39 
40 #include <sys/lockstat.h>
41 
42 #include <kern/lock_stat.h>
43 
44 #define membar_producer dtrace_membar_producer
45 
46 #define PROBE_ARGS0(a, b, c, d, e) "\000"
47 #define PROBE_ARGS1(a, b, c, d, e) a "\000"
48 #define PROBE_ARGS2(a, b, c, d, e) a "\000" b "\000"
49 #define PROBE_ARGS3(a, b, c, d, e) a "\000" b "\000" c "\000"
50 #define PROBE_ARGS4(a, b, c, d, e) a "\000" b "\000" c "\000" d "\000"
51 #define PROBE_ARGS5(a, b, c, d, e) a "\000" b "\000" c "\000" d "\000" e "\000"
52 #define PROBE_ARGS_(a, b, c, d, e, n, ...) PROBE_ARGS##n(a, b, c, d, e)
53 #define PROBE_ARGS(...) PROBE_ARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
54 
55 #define LOCKSTAT_PROBE(func, name, probe, ...) \
56 	{func, name, probe, DTRACE_IDNONE, PROBE_ARGS(__VA_ARGS__)}
57 
58 #if defined(__x86_64__)
59 #define LOCKSTAT_AFRAMES 1
60 #elif   defined(__arm__) || defined(__arm64__)
61 #define LOCKSTAT_AFRAMES 2
62 #else
63 #error "architecture not supported"
64 #endif
65 
66 typedef struct lockstat_probe {
67 	const char      *lsp_func;
68 	const char      *lsp_name;
69 	int             lsp_probe;
70 	dtrace_id_t     lsp_id;
71 	const char      *lsp_args;
72 } lockstat_probe_t;
73 
74 lockstat_probe_t lockstat_probes[] =
75 {
76 	// Mutex probes
77 	LOCKSTAT_PROBE(LS_LCK_MTX_LOCK, LSA_ACQUIRE, LS_LCK_MTX_LOCK_ACQUIRE, "lck_mtx_t"),
78 	LOCKSTAT_PROBE(LS_LCK_MTX_LOCK, LSA_SPIN, LS_LCK_MTX_LOCK_ADAPTIVE_SPIN, "lck_mtx_t", "uint64_t"),
79 	LOCKSTAT_PROBE(LS_LCK_MTX_LOCK, LSA_BLOCK, LS_LCK_MTX_LOCK_BLOCK, "lck_mtx_t", "uint64_t"),
80 	LOCKSTAT_PROBE(LS_LCK_MTX_LOCK_SPIN, LSA_SPIN, LS_LCK_MTX_LOCK_SPIN_SPIN, "lck_mtx_t", "uint64_t"),
81 
82 	LOCKSTAT_PROBE(LS_LCK_MTX_LOCK_SPIN, LSA_ACQUIRE, LS_LCK_MTX_LOCK_SPIN_ACQUIRE, "lck_mtx_t"),
83 
84 	LOCKSTAT_PROBE(LS_LCK_MTX_TRY_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_LOCK_ACQUIRE, "lck_mtx_t"),
85 	LOCKSTAT_PROBE(LS_LCK_MTX_TRY_LOCK_SPIN, LSA_ACQUIRE, LS_LCK_MTX_TRY_LOCK_SPIN_ACQUIRE, "lck_mtx_t"),
86 	LOCKSTAT_PROBE(LS_LCK_MTX_UNLOCK, LSA_RELEASE, LS_LCK_MTX_UNLOCK_RELEASE, "lck_mtx_t"),
87 	// Extended mutexes are only implemented on Intel
88 #if defined(__x86_64__)
89 	LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK, LSA_SPIN, LS_LCK_MTX_EXT_LOCK_SPIN_SPIN, "lck_mtx_t", "uint64_t"),
90 	LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK, LSA_SPIN, LS_LCK_MTX_EXT_LOCK_ADAPTIVE_SPIN, "lck_mtx_t", "uint64_t"),
91 	LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK, LSA_ACQUIRE, LS_LCK_MTX_EXT_LOCK_ACQUIRE, "lck_mtx_t"),
92 	LOCKSTAT_PROBE(LS_LCK_MTX_EXT_LOCK, LSA_BLOCK, LS_LCK_MTX_EXT_LOCK_BLOCK, "lck_mtx_t", "uint64_t"),
93 	LOCKSTAT_PROBE(LS_LCK_MTX_EXT_UNLOCK, LSA_RELEASE, LS_LCK_MTX_EXT_UNLOCK_RELEASE, "lck_mtx_t"),
94 #endif
95 
96 	// RW lock probes
97 	// TODO: This should not be a uint64_t !
98 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED, LSR_ACQUIRE, LS_LCK_RW_LOCK_SHARED_ACQUIRE, "lck_rw_t", "uint64_t"),
99 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED, LSR_BLOCK, LS_LCK_RW_LOCK_SHARED_BLOCK, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
100 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED, LSR_SPIN, LS_LCK_RW_LOCK_SHARED_SPIN, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
101 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL, LSR_ACQUIRE, LS_LCK_RW_LOCK_EXCL_ACQUIRE, "lck_rw_t", "uint64_t"),
102 	// TODO: This should NOT be a uint64_t
103 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL, LSR_BLOCK, LS_LCK_RW_LOCK_EXCL_BLOCK, "lck_rw_t", "uint64_t", "_Bool", "_Bool", "int"),
104 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL, LSR_SPIN, LS_LCK_RW_LOCK_EXCL_SPIN, "lck_rw_t", "uint64_t", "int"),
105 	LOCKSTAT_PROBE(LS_LCK_RW_DONE, LSR_RELEASE, LS_LCK_RW_DONE_RELEASE, "lck_rw_t", "_Bool"),
106 	// TODO : This should NOT be a uint64_t
107 	LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_SHARED, LSR_ACQUIRE, LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE, "lck_rw_t", "uint64_t"),
108 	LOCKSTAT_PROBE(LS_LCK_RW_TRY_LOCK_EXCL, LSR_ACQUIRE, LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE, "lck_rw_t", "uint64_t"),
109 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL, LSR_UPGRADE, LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE, "lck_rw_t", "_Bool"),
110 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_SHARED_TO_EXCL, LSR_SPIN, LS_LCK_RW_LOCK_SHARED_TO_EXCL_SPIN, "lck_rw_t", "uint64_t"),
111 	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"),
112 
113 	// Spin lock probes
114 	//TODO : Separate the probes for the hw_bit from the probe for the normal hw locks
115 	LOCKSTAT_PROBE(LS_LCK_SPIN_LOCK, LSS_ACQUIRE, LS_LCK_SPIN_LOCK_ACQUIRE, "hw_lock_t"),
116 	LOCKSTAT_PROBE(LS_LCK_SPIN_LOCK, LSS_SPIN, LS_LCK_SPIN_LOCK_SPIN, "hw_lock_t", "uint64_t", "uint64_t"),
117 	LOCKSTAT_PROBE(LS_LCK_SPIN_UNLOCK, LSS_RELEASE, LS_LCK_SPIN_UNLOCK_RELEASE, "hw_lock_t"),
118 	LOCKSTAT_PROBE(LS_LCK_RW_LOCK_EXCL_TO_SHARED, LSR_DOWNGRADE, LS_LCK_RW_LOCK_EXCL_TO_SHARED_DOWNGRADE, "lck_rw_t"),
119 	// Ticket lock probes
120 	LOCKSTAT_PROBE(LS_LCK_TICKET_LOCK, LST_ACQUIRE, LS_LCK_TICKET_LOCK_ACQUIRE, "lck_ticket_t"),
121 	LOCKSTAT_PROBE(LS_LCK_TICKET_LOCK, LST_RELEASE, LS_LCK_TICKET_LOCK_RELEASE, "lck_ticket_t"),
122 	LOCKSTAT_PROBE(LS_LCK_TICKET_LOCK, LST_SPIN, LS_LCK_TICKET_LOCK_SPIN, "lck_ticket_t"),
123 	{
124 		NULL, NULL, 0, 0, NULL
125 	}
126 };
127 
128 dtrace_id_t lockstat_probemap[LS_NPROBES];
129 
130 static dtrace_provider_id_t lockstat_id;
131 
132 /*ARGSUSED*/
133 static int
lockstat_enable(void * arg,dtrace_id_t id,void * parg)134 lockstat_enable(void *arg, dtrace_id_t id, void *parg)
135 {
136 #pragma unused(arg) /* __APPLE__ */
137 
138 	lockstat_probe_t *probe = parg;
139 
140 	ASSERT(!lockstat_probemap[probe->lsp_probe]);
141 
142 	lockstat_probemap[probe->lsp_probe] = id;
143 	membar_producer();
144 
145 	return 0;
146 }
147 
148 /*ARGSUSED*/
149 static void
lockstat_disable(void * arg,dtrace_id_t id,void * parg)150 lockstat_disable(void *arg, dtrace_id_t id, void *parg)
151 {
152 #pragma unused(arg, id) /* __APPLE__ */
153 
154 	lockstat_probe_t *probe = parg;
155 
156 	ASSERT(lockstat_probemap[probe->lsp_probe]);
157 
158 	lockstat_probemap[probe->lsp_probe] = 0;
159 	membar_producer();
160 }
161 
162 /*ARGSUSED*/
163 static void
lockstat_provide(void * arg,const dtrace_probedesc_t * desc)164 lockstat_provide(void *arg, const dtrace_probedesc_t *desc)
165 {
166 #pragma unused(arg, desc) /* __APPLE__ */
167 
168 	int i = 0;
169 
170 	for (i = 0; lockstat_probes[i].lsp_func != NULL; i++) {
171 		lockstat_probe_t *probe = &lockstat_probes[i];
172 
173 		if (dtrace_probe_lookup(lockstat_id, "mach_kernel",
174 		    probe->lsp_func, probe->lsp_name) != 0) {
175 			continue;
176 		}
177 
178 		ASSERT(!probe->lsp_id);
179 		probe->lsp_id = dtrace_probe_create(lockstat_id,
180 		    "mach_kernel", probe->lsp_func, probe->lsp_name,
181 		    LOCKSTAT_AFRAMES, probe);
182 	}
183 }
184 
185 
186 /*ARGSUSED*/
187 static void
lockstat_destroy(void * arg,dtrace_id_t id,void * parg)188 lockstat_destroy(void *arg, dtrace_id_t id, void *parg)
189 {
190 #pragma unused(arg, id)
191 
192 	lockstat_probe_t *probe = parg;
193 
194 	ASSERT(!lockstat_probemap[probe->lsp_probe]);
195 	probe->lsp_id = 0;
196 }
197 
198 static void
lockstat_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)199 lockstat_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
200 {
201 #pragma unused(arg, id)
202 	lockstat_probe_t *probe = parg;
203 	const char* argdesc = probe->lsp_args;
204 	int narg = 0;
205 
206 	desc->dtargd_native[0] = '\0';
207 	desc->dtargd_xlate[0] = '\0';
208 
209 	while (argdesc[0] != '\0') {
210 		if (narg == desc->dtargd_ndx) {
211 			strlcpy(desc->dtargd_native, argdesc, DTRACE_ARGTYPELEN);
212 			return;
213 		}
214 		argdesc += strlen(argdesc) + 1;
215 		narg++;
216 	}
217 
218 	desc->dtargd_ndx = DTRACE_ARGNONE;
219 }
220 
221 static dtrace_pattr_t lockstat_attr = {
222 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
223 	{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
224 	{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
225 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
226 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
227 };
228 
229 static dtrace_pops_t lockstat_pops = {
230 	.dtps_provide =         lockstat_provide,
231 	.dtps_provide_module =  NULL,
232 	.dtps_enable =          lockstat_enable,
233 	.dtps_disable =         lockstat_disable,
234 	.dtps_suspend =         NULL,
235 	.dtps_resume =          NULL,
236 	.dtps_getargdesc =      lockstat_getargdesc,
237 	.dtps_getargval =       NULL,
238 	.dtps_usermode =        NULL,
239 	.dtps_destroy =         lockstat_destroy
240 };
241 
242 static int
lockstat_attach(dev_info_t * devi)243 lockstat_attach(dev_info_t *devi)
244 {
245 	if (ddi_create_minor_node(devi, "lockstat", S_IFCHR, 0,
246 	    DDI_PSEUDO, 0) == DDI_FAILURE ||
247 	    dtrace_register("lockstat", &lockstat_attr, DTRACE_PRIV_KERNEL,
248 	    NULL, &lockstat_pops, NULL, &lockstat_id) != 0) {
249 		ddi_remove_minor_node(devi, NULL);
250 		return DDI_FAILURE;
251 	}
252 
253 	return DDI_SUCCESS;
254 }
255 
256 static int
_lockstat_open(dev_t dev,int flags,int devtype,struct proc * p)257 _lockstat_open(dev_t dev, int flags, int devtype, struct proc *p)
258 {
259 #pragma unused(dev,flags,devtype,p)
260 	return 0;
261 }
262 
263 
264 static const struct cdevsw lockstat_cdevsw =
265 {
266 	.d_open = _lockstat_open,
267 	.d_close = eno_opcl,
268 	.d_read = eno_rdwrt,
269 	.d_write = eno_rdwrt,
270 	.d_ioctl = eno_ioctl,
271 	.d_stop = (stop_fcn_t *)nulldev,
272 	.d_reset = (reset_fcn_t *)nulldev,
273 	.d_select = eno_select,
274 	.d_mmap = eno_mmap,
275 	.d_strategy = eno_strat,
276 	.d_reserved_1 = eno_getc,
277 	.d_reserved_2 = eno_putc,
278 };
279 
280 void lockstat_init( void );
281 
282 void
lockstat_init(void)283 lockstat_init( void )
284 {
285 	int majdevno = cdevsw_add(-1, &lockstat_cdevsw);
286 
287 	if (majdevno < 0) {
288 		printf("lockstat_init: failed to allocate a major number!\n");
289 		return;
290 	}
291 
292 	lockstat_attach((dev_info_t*)(uintptr_t)majdevno);
293 }
294