xref: /titanic_41/usr/src/uts/common/dtrace/lockstat.c (revision 10db1377dafab8ba3feedef26db9c5d8539a5cd1)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <sys/open.h>
32 #include <sys/file.h>
33 #include <sys/conf.h>
34 #include <sys/modctl.h>
35 #include <sys/cmn_err.h>
36 #include <sys/bitmap.h>
37 #include <sys/debug.h>
38 #include <sys/kmem.h>
39 #include <sys/errno.h>
40 #include <sys/sysmacros.h>
41 #include <sys/lockstat.h>
42 #include <sys/atomic.h>
43 #include <sys/dtrace.h>
44 
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 
48 typedef struct lockstat_probe {
49 	const char	*lsp_func;
50 	const char	*lsp_name;
51 	int		lsp_probe;
52 	dtrace_id_t	lsp_id;
53 } lockstat_probe_t;
54 
55 lockstat_probe_t lockstat_probes[] =
56 {
57 	{ LS_MUTEX_ENTER,	LSA_ACQUIRE,	LS_MUTEX_ENTER_ACQUIRE },
58 	{ LS_MUTEX_ENTER,	LSA_BLOCK,	LS_MUTEX_ENTER_BLOCK },
59 	{ LS_MUTEX_ENTER,	LSA_SPIN,	LS_MUTEX_ENTER_SPIN },
60 	{ LS_MUTEX_EXIT,	LSA_RELEASE,	LS_MUTEX_EXIT_RELEASE },
61 	{ LS_MUTEX_DESTROY,	LSA_RELEASE,	LS_MUTEX_DESTROY_RELEASE },
62 	{ LS_MUTEX_TRYENTER,	LSA_ACQUIRE,	LS_MUTEX_TRYENTER_ACQUIRE },
63 	{ LS_LOCK_SET,		LSS_ACQUIRE,	LS_LOCK_SET_ACQUIRE },
64 	{ LS_LOCK_SET,		LSS_SPIN,	LS_LOCK_SET_SPIN },
65 	{ LS_LOCK_SET_SPL,	LSS_ACQUIRE,	LS_LOCK_SET_SPL_ACQUIRE },
66 	{ LS_LOCK_SET_SPL,	LSS_SPIN,	LS_LOCK_SET_SPL_SPIN },
67 	{ LS_LOCK_TRY,		LSS_ACQUIRE,	LS_LOCK_TRY_ACQUIRE },
68 	{ LS_LOCK_CLEAR,	LSS_RELEASE,	LS_LOCK_CLEAR_RELEASE },
69 	{ LS_LOCK_CLEAR_SPLX,	LSS_RELEASE,	LS_LOCK_CLEAR_SPLX_RELEASE },
70 	{ LS_CLOCK_UNLOCK,	LSS_RELEASE,	LS_CLOCK_UNLOCK_RELEASE },
71 	{ LS_RW_ENTER,		LSR_ACQUIRE,	LS_RW_ENTER_ACQUIRE },
72 	{ LS_RW_ENTER,		LSR_BLOCK,	LS_RW_ENTER_BLOCK },
73 	{ LS_RW_EXIT,		LSR_RELEASE,	LS_RW_EXIT_RELEASE },
74 	{ LS_RW_TRYENTER,	LSR_ACQUIRE,	LS_RW_TRYENTER_ACQUIRE },
75 	{ LS_RW_TRYUPGRADE,	LSR_UPGRADE,	LS_RW_TRYUPGRADE_UPGRADE },
76 	{ LS_RW_DOWNGRADE,	LSR_DOWNGRADE,	LS_RW_DOWNGRADE_DOWNGRADE },
77 	{ LS_THREAD_LOCK,	LST_SPIN,	LS_THREAD_LOCK_SPIN },
78 	{ LS_THREAD_LOCK_HIGH,	LST_SPIN,	LS_THREAD_LOCK_HIGH_SPIN },
79 	{ NULL }
80 };
81 
82 static dev_info_t	*lockstat_devi;	/* saved in xxattach() for xxinfo() */
83 static kmutex_t		lockstat_test;	/* for testing purposes only */
84 static dtrace_provider_id_t lockstat_id;
85 
86 /*ARGSUSED*/
87 static void
88 lockstat_enable(void *arg, dtrace_id_t id, void *parg)
89 {
90 	lockstat_probe_t *probe = parg;
91 
92 	ASSERT(!lockstat_probemap[probe->lsp_probe]);
93 
94 	lockstat_probemap[probe->lsp_probe] = id;
95 	membar_producer();
96 
97 	lockstat_probe = dtrace_probe;
98 	membar_producer();
99 
100 	lockstat_hot_patch();
101 	membar_producer();
102 
103 	/*
104 	 * Immediately generate a record for the lockstat_test mutex
105 	 * to verify that the mutex hot-patch code worked as expected.
106 	 */
107 	mutex_enter(&lockstat_test);
108 	mutex_exit(&lockstat_test);
109 }
110 
111 /*ARGSUSED*/
112 static void
113 lockstat_disable(void *arg, dtrace_id_t id, void *parg)
114 {
115 	lockstat_probe_t *probe = parg;
116 	int i;
117 
118 	ASSERT(lockstat_probemap[probe->lsp_probe]);
119 
120 	lockstat_probemap[probe->lsp_probe] = 0;
121 	lockstat_hot_patch();
122 	membar_producer();
123 
124 	/*
125 	 * See if we have any probes left enabled.
126 	 */
127 	for (i = 0; i < LS_NPROBES; i++) {
128 		if (lockstat_probemap[i]) {
129 			/*
130 			 * This probe is still enabled.  We don't need to deal
131 			 * with waiting for all threads to be out of the
132 			 * lockstat critical sections; just return.
133 			 */
134 			return;
135 		}
136 	}
137 
138 	/*
139 	 * The delay() here isn't as cheesy as you might think.  We don't
140 	 * want to busy-loop in the kernel, so we have to give up the
141 	 * CPU between calls to lockstat_active_threads(); that much is
142 	 * obvious.  But the reason it's a do..while loop rather than a
143 	 * while loop is subtle.  The memory barrier above guarantees that
144 	 * no threads will enter the lockstat code from this point forward.
145 	 * However, another thread could already be executing lockstat code
146 	 * without our knowledge if the update to its t_lockstat field hasn't
147 	 * cleared its CPU's store buffer.  Delaying for one clock tick
148 	 * guarantees that either (1) the thread will have *ample* time to
149 	 * complete its work, or (2) the thread will be preempted, in which
150 	 * case it will have to grab and release a dispatcher lock, which
151 	 * will flush that CPU's store buffer.  Either way we're covered.
152 	 */
153 	do {
154 		delay(1);
155 	} while (lockstat_active_threads());
156 }
157 
158 /*ARGSUSED*/
159 static int
160 lockstat_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
161 {
162 	return (0);
163 }
164 
165 /* ARGSUSED */
166 static int
167 lockstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
168 {
169 	int error;
170 
171 	switch (infocmd) {
172 	case DDI_INFO_DEVT2DEVINFO:
173 		*result = (void *) lockstat_devi;
174 		error = DDI_SUCCESS;
175 		break;
176 	case DDI_INFO_DEVT2INSTANCE:
177 		*result = (void *)0;
178 		error = DDI_SUCCESS;
179 		break;
180 	default:
181 		error = DDI_FAILURE;
182 	}
183 	return (error);
184 }
185 
186 /*ARGSUSED*/
187 static void
188 lockstat_provide(void *arg, const dtrace_probedesc_t *desc)
189 {
190 	int i = 0;
191 
192 	for (i = 0; lockstat_probes[i].lsp_func != NULL; i++) {
193 		lockstat_probe_t *probe = &lockstat_probes[i];
194 
195 		if (dtrace_probe_lookup(lockstat_id, "genunix",
196 		    probe->lsp_func, probe->lsp_name) != 0)
197 			continue;
198 
199 		ASSERT(!probe->lsp_id);
200 		probe->lsp_id = dtrace_probe_create(lockstat_id,
201 		    "genunix", probe->lsp_func, probe->lsp_name,
202 		    1, probe);
203 	}
204 }
205 
206 /*ARGSUSED*/
207 static void
208 lockstat_destroy(void *arg, dtrace_id_t id, void *parg)
209 {
210 	lockstat_probe_t *probe = parg;
211 
212 	ASSERT(!lockstat_probemap[probe->lsp_probe]);
213 	probe->lsp_id = 0;
214 }
215 
216 static dtrace_pattr_t lockstat_attr = {
217 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
218 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
219 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
220 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
221 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
222 };
223 
224 static dtrace_pops_t lockstat_pops = {
225 	lockstat_provide,
226 	NULL,
227 	lockstat_enable,
228 	lockstat_disable,
229 	NULL,
230 	NULL,
231 	NULL,
232 	NULL,
233 	NULL,
234 	lockstat_destroy
235 };
236 
237 static int
238 lockstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
239 {
240 	switch (cmd) {
241 	case DDI_ATTACH:
242 		break;
243 	case DDI_RESUME:
244 		return (DDI_SUCCESS);
245 	default:
246 		return (DDI_FAILURE);
247 	}
248 
249 	if (ddi_create_minor_node(devi, "lockstat", S_IFCHR, 0,
250 	    DDI_PSEUDO, 0) == DDI_FAILURE ||
251 	    dtrace_register("lockstat", &lockstat_attr, DTRACE_PRIV_KERNEL,
252 	    NULL, &lockstat_pops, NULL, &lockstat_id) != 0) {
253 		ddi_remove_minor_node(devi, NULL);
254 		return (DDI_FAILURE);
255 	}
256 
257 	ddi_report_dev(devi);
258 	lockstat_devi = devi;
259 	return (DDI_SUCCESS);
260 }
261 
262 static int
263 lockstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
264 {
265 	switch (cmd) {
266 	case DDI_DETACH:
267 		break;
268 	case DDI_SUSPEND:
269 		return (DDI_SUCCESS);
270 	default:
271 		return (DDI_FAILURE);
272 	}
273 
274 	if (dtrace_unregister(lockstat_id) != 0)
275 		return (DDI_FAILURE);
276 
277 	ddi_remove_minor_node(devi, NULL);
278 	return (DDI_SUCCESS);
279 }
280 
281 /*
282  * Configuration data structures
283  */
284 static struct cb_ops lockstat_cb_ops = {
285 	lockstat_open,		/* open */
286 	nodev,			/* close */
287 	nulldev,		/* strategy */
288 	nulldev,		/* print */
289 	nodev,			/* dump */
290 	nodev,			/* read */
291 	nodev,			/* write */
292 	nodev,			/* ioctl */
293 	nodev,			/* devmap */
294 	nodev,			/* mmap */
295 	nodev,			/* segmap */
296 	nochpoll,		/* poll */
297 	ddi_prop_op,		/* cb_prop_op */
298 	0,			/* streamtab */
299 	D_MP | D_NEW		/* Driver compatibility flag */
300 };
301 
302 static struct dev_ops lockstat_ops = {
303 	DEVO_REV,		/* devo_rev, */
304 	0,			/* refcnt */
305 	lockstat_info,		/* getinfo */
306 	nulldev,		/* identify */
307 	nulldev,		/* probe */
308 	lockstat_attach,	/* attach */
309 	lockstat_detach,	/* detach */
310 	nulldev,		/* reset */
311 	&lockstat_cb_ops,	/* cb_ops */
312 	NULL,			/* bus_ops */
313 };
314 
315 static struct modldrv modldrv = {
316 	&mod_driverops,		/* Type of module.  This one is a driver */
317 	"Lock Statistics %I%",	/* name of module */
318 	&lockstat_ops,		/* driver ops */
319 };
320 
321 static struct modlinkage modlinkage = {
322 	MODREV_1, (void *)&modldrv, NULL
323 };
324 
325 int
326 _init(void)
327 {
328 	return (mod_install(&modlinkage));
329 }
330 
331 int
332 _fini(void)
333 {
334 	return (mod_remove(&modlinkage));
335 }
336 
337 int
338 _info(struct modinfo *modinfop)
339 {
340 	return (mod_info(&modlinkage, modinfop));
341 }
342