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