]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/lockstat.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / lockstat.c
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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /* #pragma ident "@(#)lockstat.c 1.12 08/01/16 SMI" */
27
28
29 #ifdef KERNEL
30 #ifndef _KERNEL
31 #define _KERNEL /* Solaris vs. Darwin */
32 #endif
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <miscfs/devfs/devfs.h>
43
44 #include <sys/dtrace.h>
45 #include <sys/dtrace_impl.h>
46
47 #include <sys/dtrace_glue.h>
48
49 #include <sys/lockstat.h>
50
51 #include <kern/processor.h>
52
53 #define membar_producer dtrace_membar_producer
54
55 /*
56 * Hot patch values, x86
57 */
58 #if defined(__i386__) || defined(__x86_64__)
59 #define NOP 0x90
60 #define RET 0xc3
61 #define LOCKSTAT_AFRAMES 1
62 #elif __ppc__
63 #define NOP 0x60000000
64 #define RET 0x4e800020 /* blr */
65 #define LOCKSTAT_AFRAMES 2
66 #else
67 #error "not ported to this architecture"
68 #endif
69
70
71 typedef struct lockstat_probe {
72 const char *lsp_func;
73 const char *lsp_name;
74 int lsp_probe;
75 dtrace_id_t lsp_id;
76 } lockstat_probe_t;
77
78 lockstat_probe_t lockstat_probes[] =
79 {
80 #if defined(__i386__) || defined(__x86_64__)
81 /* Not implemented yet on PPC... */
82 { LS_LCK_MTX_LOCK, LSA_ACQUIRE, LS_LCK_MTX_LOCK_ACQUIRE, DTRACE_IDNONE },
83 { LS_LCK_MTX_LOCK, LSA_SPIN, LS_LCK_MTX_LOCK_SPIN, DTRACE_IDNONE },
84 { LS_LCK_MTX_TRY_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_LOCK_ACQUIRE, DTRACE_IDNONE },
85 { LS_LCK_MTX_TRY_SPIN_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_SPIN_LOCK_ACQUIRE, DTRACE_IDNONE },
86 { LS_LCK_MTX_UNLOCK, LSA_RELEASE, LS_LCK_MTX_UNLOCK_RELEASE, DTRACE_IDNONE },
87 { LS_LCK_MTX_EXT_LOCK, LSA_ACQUIRE, LS_LCK_MTX_EXT_LOCK_ACQUIRE, DTRACE_IDNONE },
88 { LS_LCK_MTX_EXT_LOCK, LSA_SPIN, LS_LCK_MTX_EXT_LOCK_SPIN, DTRACE_IDNONE },
89 { LS_LCK_MTX_EXT_TRY_LOCK, LSA_ACQUIRE, LS_LCK_MTX_TRY_EXT_LOCK_ACQUIRE, DTRACE_IDNONE },
90 { LS_LCK_MTX_UNLOCK, LSA_RELEASE, LS_LCK_MTX_EXT_UNLOCK_RELEASE, DTRACE_IDNONE },
91 { LS_LCK_MTX_LOCK_SPIN_LOCK, LSA_ACQUIRE, LS_LCK_MTX_LOCK_SPIN_ACQUIRE, DTRACE_IDNONE },
92 #endif
93 { LS_LCK_MTX_LOCK, LSA_BLOCK, LS_LCK_MTX_LOCK_BLOCK, DTRACE_IDNONE },
94 { LS_LCK_MTX_EXT_LOCK, LSA_BLOCK, LS_LCK_MTX_EXT_LOCK_BLOCK, DTRACE_IDNONE },
95
96 { LS_LCK_RW_LOCK_SHARED, LSR_ACQUIRE, LS_LCK_RW_LOCK_SHARED_ACQUIRE, DTRACE_IDNONE },
97 { LS_LCK_RW_LOCK_SHARED, LSR_BLOCK, LS_LCK_RW_LOCK_SHARED_BLOCK, DTRACE_IDNONE },
98 { LS_LCK_RW_LOCK_SHARED, LSR_SPIN, LS_LCK_RW_LOCK_SHARED_SPIN, DTRACE_IDNONE },
99 { LS_LCK_RW_LOCK_EXCL, LSR_ACQUIRE, LS_LCK_RW_LOCK_EXCL_ACQUIRE, DTRACE_IDNONE },
100 { LS_LCK_RW_LOCK_EXCL, LSR_BLOCK, LS_LCK_RW_LOCK_EXCL_BLOCK, DTRACE_IDNONE },
101 { LS_LCK_RW_LOCK_EXCL, LSR_SPIN, LS_LCK_RW_LOCK_EXCL_SPIN, DTRACE_IDNONE },
102 { LS_LCK_RW_DONE, LSR_RELEASE, LS_LCK_RW_DONE_RELEASE, DTRACE_IDNONE },
103 { LS_LCK_RW_TRY_LOCK_SHARED, LSR_ACQUIRE, LS_LCK_RW_TRY_LOCK_SHARED_ACQUIRE, DTRACE_IDNONE },
104 { LS_LCK_RW_TRY_LOCK_EXCL, LSR_ACQUIRE, LS_LCK_RW_TRY_LOCK_EXCL_ACQUIRE, DTRACE_IDNONE },
105 { LS_LCK_RW_LOCK_SHARED_TO_EXCL, LSR_UPGRADE, LS_LCK_RW_LOCK_SHARED_TO_EXCL_UPGRADE, DTRACE_IDNONE },
106 { LS_LCK_RW_LOCK_SHARED_TO_EXCL, LSR_BLOCK, LS_LCK_RW_LOCK_SHARED_TO_EXCL_BLOCK, DTRACE_IDNONE },
107 { LS_LCK_RW_LOCK_SHARED_TO_EXCL, LSR_SPIN, LS_LCK_RW_LOCK_SHARED_TO_EXCL_SPIN, DTRACE_IDNONE },
108 { LS_LCK_RW_LOCK_EXCL_TO_SHARED, LSR_DOWNGRADE, LS_LCK_RW_LOCK_EXCL_TO_SHARED_DOWNGRADE, DTRACE_IDNONE },
109
110
111 #ifdef LATER
112 /* Interlock and spinlock measurements would be nice, but later */
113 { LS_LCK_SPIN_LOCK, LSS_ACQUIRE, LS_LCK_SPIN_LOCK_ACQUIRE, DTRACE_IDNONE },
114 { LS_LCK_SPIN_LOCK, LSS_SPIN, LS_LCK_SPIN_LOCK_SPIN, DTRACE_IDNONE },
115 { LS_LCK_SPIN_UNLOCK, LSS_RELEASE, LS_LCK_SPIN_UNLOCK_RELEASE, DTRACE_IDNONE },
116
117 { LS_LCK_RW_LOCK_EXCL_TO_SHARED, LSA_ILK_SPIN, LS_LCK_RW_LOCK_EXCL_TO_SHARED_ILK_SPIN, DTRACE_IDNONE },
118 { LS_LCK_MTX_LOCK, LSA_ILK_SPIN, LS_LCK_MTX_LOCK_ILK_SPIN, DTRACE_IDNONE },
119 { LS_LCK_MTX_EXT_LOCK, LSA_ILK_SPIN, LS_LCK_MTX_EXT_LOCK_ILK_SPIN, DTRACE_IDNONE },
120 { LS_LCK_RW_TRY_LOCK_EXCL, LSA_ILK_SPIN, LS_LCK_RW_TRY_LOCK_EXCL_ILK_SPIN, DTRACE_IDNONE },
121 { LS_LCK_RW_TRY_LOCK_SHARED, LSA_SPIN, LS_LCK_RW_TRY_LOCK_SHARED_SPIN, DTRACE_IDNONE },
122 #endif
123
124 { NULL, NULL, 0, 0 }
125 };
126
127 dtrace_id_t lockstat_probemap[LS_NPROBES];
128
129 #if CONFIG_DTRACE
130 extern void lck_mtx_lock_lockstat_patch_point(void);
131 extern void lck_mtx_try_lock_lockstat_patch_point(void);
132 extern void lck_mtx_try_lock_spin_lockstat_patch_point(void);
133 extern void lck_mtx_unlock_lockstat_patch_point(void);
134 extern void lck_mtx_lock_ext_lockstat_patch_point(void);
135 extern void lck_mtx_ext_unlock_lockstat_patch_point(void);
136
137 extern void lck_rw_lock_shared_lockstat_patch_point(void);
138 extern void lck_rw_lock_exclusive_lockstat_patch_point(void);
139 extern void lck_rw_lock_shared_to_exclusive_lockstat_patch_point(void);
140 extern void lck_rw_try_lock_shared_lockstat_patch_point(void);
141 extern void lck_rw_try_lock_exclusive_lockstat_patch_point(void);
142 extern void lck_mtx_lock_spin_lockstat_patch_point(void);
143 #endif /* CONFIG_DTRACE */
144
145 vm_offset_t *assembly_probes[] = {
146 #if CONFIG_DTRACE
147 #if defined(__i386__) || defined(__x86_64__)
148 /*
149 * On x86 these points are better done via hot patches, which ensure
150 * there is zero overhead when not in use. On x86 these patch points
151 * are swapped between the return instruction and a no-op, with the
152 * Dtrace call following the return.
153 */
154 (vm_offset_t *) lck_mtx_lock_lockstat_patch_point,
155 (vm_offset_t *) lck_mtx_try_lock_lockstat_patch_point,
156 (vm_offset_t *) lck_mtx_try_lock_spin_lockstat_patch_point,
157 (vm_offset_t *) lck_mtx_unlock_lockstat_patch_point,
158 (vm_offset_t *) lck_mtx_lock_ext_lockstat_patch_point,
159 (vm_offset_t *) lck_mtx_ext_unlock_lockstat_patch_point,
160 (vm_offset_t *) lck_rw_lock_shared_lockstat_patch_point,
161 (vm_offset_t *) lck_rw_lock_exclusive_lockstat_patch_point,
162 (vm_offset_t *) lck_rw_lock_shared_to_exclusive_lockstat_patch_point,
163 (vm_offset_t *) lck_rw_try_lock_shared_lockstat_patch_point,
164 (vm_offset_t *) lck_rw_try_lock_exclusive_lockstat_patch_point,
165 (vm_offset_t *) lck_mtx_lock_spin_lockstat_patch_point,
166 #else
167 (vm_offset_t *) lck_mtx_unlock_lockstat_patch_point,
168 #endif
169 #endif /* CONFIG_DTRACE */
170 NULL
171 };
172 /*
173 * Hot patch switches back and forth the probe points between NOP and RET.
174 * The argument indicates whether the probe point is on or off.
175 */
176 #if defined(__APPLE__)
177 static
178 #endif /* __APPLE__ */
179 void lockstat_hot_patch(boolean_t active)
180 {
181 #pragma unused(active)
182 int i;
183
184
185 for (i = 0; assembly_probes[i]; i++) {
186 #if defined(__i386__) || defined(__x86_64__)
187 uint8_t instr;
188 instr = (active ? NOP : RET );
189 (void) ml_nofault_copy( (vm_offset_t)&instr, *(assembly_probes[i]),
190 sizeof(instr));
191 #endif
192 #ifdef __ppc__
193 uint32_t instr;
194 instr = (active ? NOP : RET );
195 (void) ml_nofault_copy( (vm_offset_t)&instr, *(assembly_probes[i]), sizeof(instr));
196 #endif
197 }
198 }
199
200
201
202 void (*lockstat_probe)(dtrace_id_t, uint64_t, uint64_t,
203 uint64_t, uint64_t, uint64_t);
204
205 static dev_info_t *lockstat_devi; /* saved in xxattach() for xxinfo() */
206 static dtrace_provider_id_t lockstat_id;
207
208 /*ARGSUSED*/
209 static void
210 lockstat_enable(void *arg, dtrace_id_t id, void *parg)
211 {
212 #pragma unused(arg) /* __APPLE__ */
213
214 lockstat_probe_t *probe = parg;
215
216 ASSERT(!lockstat_probemap[probe->lsp_probe]);
217
218 lockstat_probemap[probe->lsp_probe] = id;
219 membar_producer();
220
221 lockstat_hot_patch(TRUE);
222 membar_producer();
223
224 }
225
226 /*ARGSUSED*/
227 static void
228 lockstat_disable(void *arg, dtrace_id_t id, void *parg)
229 {
230 #pragma unused(arg, id) /* __APPLE__ */
231
232 lockstat_probe_t *probe = parg;
233 int i;
234
235 ASSERT(lockstat_probemap[probe->lsp_probe]);
236
237 lockstat_probemap[probe->lsp_probe] = 0;
238 lockstat_hot_patch(FALSE);
239 membar_producer();
240
241 /*
242 * See if we have any probes left enabled.
243 */
244 for (i = 0; i < LS_NPROBES; i++) {
245 if (lockstat_probemap[i]) {
246 /*
247 * This probe is still enabled. We don't need to deal
248 * with waiting for all threads to be out of the
249 * lockstat critical sections; just return.
250 */
251 return;
252 }
253 }
254
255 }
256
257 /*ARGSUSED*/
258 static void
259 lockstat_provide(void *arg, const dtrace_probedesc_t *desc)
260 {
261 #pragma unused(arg, desc) /* __APPLE__ */
262
263 int i = 0;
264
265 for (i = 0; lockstat_probes[i].lsp_func != NULL; i++) {
266 lockstat_probe_t *probe = &lockstat_probes[i];
267
268 if (dtrace_probe_lookup(lockstat_id, "mach_kernel",
269 probe->lsp_func, probe->lsp_name) != 0)
270 continue;
271
272 ASSERT(!probe->lsp_id);
273 probe->lsp_id = dtrace_probe_create(lockstat_id,
274 "mach_kernel", probe->lsp_func, probe->lsp_name,
275 LOCKSTAT_AFRAMES, probe);
276 }
277 }
278
279
280 /*ARGSUSED*/
281 static void
282 lockstat_destroy(void *arg, dtrace_id_t id, void *parg)
283 {
284 #pragma unused(arg, id) /* __APPLE__ */
285
286 lockstat_probe_t *probe = parg;
287
288 ASSERT(!lockstat_probemap[probe->lsp_probe]);
289 probe->lsp_id = 0;
290 }
291
292 static dtrace_pattr_t lockstat_attr = {
293 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
294 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
295 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
296 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
297 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
298 };
299
300 static dtrace_pops_t lockstat_pops = {
301 lockstat_provide,
302 NULL,
303 lockstat_enable,
304 lockstat_disable,
305 NULL,
306 NULL,
307 NULL,
308 NULL,
309 NULL,
310 lockstat_destroy
311 };
312
313 static int
314 lockstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
315 {
316 switch (cmd) {
317 case DDI_ATTACH:
318 break;
319 case DDI_RESUME:
320 return (DDI_SUCCESS);
321 default:
322 return (DDI_FAILURE);
323 }
324
325 if (ddi_create_minor_node(devi, "lockstat", S_IFCHR, 0,
326 DDI_PSEUDO, 0) == DDI_FAILURE ||
327 dtrace_register("lockstat", &lockstat_attr, DTRACE_PRIV_KERNEL,
328 NULL, &lockstat_pops, NULL, &lockstat_id) != 0) {
329 ddi_remove_minor_node(devi, NULL);
330 return (DDI_FAILURE);
331 }
332
333 lockstat_probe = dtrace_probe;
334 membar_producer();
335
336 ddi_report_dev(devi);
337 lockstat_devi = devi;
338 return (DDI_SUCCESS);
339 }
340
341 d_open_t _lockstat_open;
342
343 int _lockstat_open(dev_t dev, int flags, int devtype, struct proc *p)
344 {
345 #pragma unused(dev,flags,devtype,p)
346 return 0;
347 }
348
349 #define LOCKSTAT_MAJOR -24 /* let the kernel pick the device number */
350
351 /*
352 * A struct describing which functions will get invoked for certain
353 * actions.
354 */
355 static struct cdevsw lockstat_cdevsw =
356 {
357 _lockstat_open, /* open */
358 eno_opcl, /* close */
359 eno_rdwrt, /* read */
360 eno_rdwrt, /* write */
361 eno_ioctl, /* ioctl */
362 (stop_fcn_t *)nulldev, /* stop */
363 (reset_fcn_t *)nulldev, /* reset */
364 NULL, /* tty's */
365 eno_select, /* select */
366 eno_mmap, /* mmap */
367 eno_strat, /* strategy */
368 eno_getc, /* getc */
369 eno_putc, /* putc */
370 0 /* type */
371 };
372
373 static int gLockstatInited = 0;
374
375 void lockstat_init( void );
376
377 void lockstat_init( void )
378 {
379 if (0 == gLockstatInited)
380 {
381 int majdevno = cdevsw_add(LOCKSTAT_MAJOR, &lockstat_cdevsw);
382
383 if (majdevno < 0) {
384 printf("lockstat_init: failed to allocate a major number!\n");
385 gLockstatInited = 0;
386 return;
387 }
388
389 lockstat_attach( (dev_info_t *)(uintptr_t)majdevno, DDI_ATTACH );
390 gLockstatInited = 1;
391 } else
392 panic("lockstat_init: called twice!\n");
393 }
394 #undef LOCKSTAT_MAJOR