10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51677Sdp * Common Development and Distribution License (the "License").
61677Sdp * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*8803SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <sys/open.h>
310Sstevel@tonic-gate #include <sys/file.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/modctl.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
350Sstevel@tonic-gate #include <sys/bitmap.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/lockstat.h>
410Sstevel@tonic-gate #include <sys/atomic.h>
420Sstevel@tonic-gate #include <sys/dtrace.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <sys/ddi.h>
450Sstevel@tonic-gate #include <sys/sunddi.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate typedef struct lockstat_probe {
480Sstevel@tonic-gate const char *lsp_func;
490Sstevel@tonic-gate const char *lsp_name;
500Sstevel@tonic-gate int lsp_probe;
510Sstevel@tonic-gate dtrace_id_t lsp_id;
520Sstevel@tonic-gate } lockstat_probe_t;
530Sstevel@tonic-gate
540Sstevel@tonic-gate lockstat_probe_t lockstat_probes[] =
550Sstevel@tonic-gate {
560Sstevel@tonic-gate { LS_MUTEX_ENTER, LSA_ACQUIRE, LS_MUTEX_ENTER_ACQUIRE },
570Sstevel@tonic-gate { LS_MUTEX_ENTER, LSA_BLOCK, LS_MUTEX_ENTER_BLOCK },
580Sstevel@tonic-gate { LS_MUTEX_ENTER, LSA_SPIN, LS_MUTEX_ENTER_SPIN },
590Sstevel@tonic-gate { LS_MUTEX_EXIT, LSA_RELEASE, LS_MUTEX_EXIT_RELEASE },
600Sstevel@tonic-gate { LS_MUTEX_DESTROY, LSA_RELEASE, LS_MUTEX_DESTROY_RELEASE },
610Sstevel@tonic-gate { LS_MUTEX_TRYENTER, LSA_ACQUIRE, LS_MUTEX_TRYENTER_ACQUIRE },
620Sstevel@tonic-gate { LS_LOCK_SET, LSS_ACQUIRE, LS_LOCK_SET_ACQUIRE },
630Sstevel@tonic-gate { LS_LOCK_SET, LSS_SPIN, LS_LOCK_SET_SPIN },
640Sstevel@tonic-gate { LS_LOCK_SET_SPL, LSS_ACQUIRE, LS_LOCK_SET_SPL_ACQUIRE },
650Sstevel@tonic-gate { LS_LOCK_SET_SPL, LSS_SPIN, LS_LOCK_SET_SPL_SPIN },
660Sstevel@tonic-gate { LS_LOCK_TRY, LSS_ACQUIRE, LS_LOCK_TRY_ACQUIRE },
670Sstevel@tonic-gate { LS_LOCK_CLEAR, LSS_RELEASE, LS_LOCK_CLEAR_RELEASE },
680Sstevel@tonic-gate { LS_LOCK_CLEAR_SPLX, LSS_RELEASE, LS_LOCK_CLEAR_SPLX_RELEASE },
690Sstevel@tonic-gate { LS_CLOCK_UNLOCK, LSS_RELEASE, LS_CLOCK_UNLOCK_RELEASE },
700Sstevel@tonic-gate { LS_RW_ENTER, LSR_ACQUIRE, LS_RW_ENTER_ACQUIRE },
710Sstevel@tonic-gate { LS_RW_ENTER, LSR_BLOCK, LS_RW_ENTER_BLOCK },
720Sstevel@tonic-gate { LS_RW_EXIT, LSR_RELEASE, LS_RW_EXIT_RELEASE },
730Sstevel@tonic-gate { LS_RW_TRYENTER, LSR_ACQUIRE, LS_RW_TRYENTER_ACQUIRE },
740Sstevel@tonic-gate { LS_RW_TRYUPGRADE, LSR_UPGRADE, LS_RW_TRYUPGRADE_UPGRADE },
750Sstevel@tonic-gate { LS_RW_DOWNGRADE, LSR_DOWNGRADE, LS_RW_DOWNGRADE_DOWNGRADE },
760Sstevel@tonic-gate { LS_THREAD_LOCK, LST_SPIN, LS_THREAD_LOCK_SPIN },
770Sstevel@tonic-gate { LS_THREAD_LOCK_HIGH, LST_SPIN, LS_THREAD_LOCK_HIGH_SPIN },
780Sstevel@tonic-gate { NULL }
790Sstevel@tonic-gate };
800Sstevel@tonic-gate
810Sstevel@tonic-gate static dev_info_t *lockstat_devi; /* saved in xxattach() for xxinfo() */
820Sstevel@tonic-gate static kmutex_t lockstat_test; /* for testing purposes only */
830Sstevel@tonic-gate static dtrace_provider_id_t lockstat_id;
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*ARGSUSED*/
86*8803SJonathan.Haslam@Sun.COM static int
lockstat_enable(void * arg,dtrace_id_t id,void * parg)870Sstevel@tonic-gate lockstat_enable(void *arg, dtrace_id_t id, void *parg)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate lockstat_probe_t *probe = parg;
900Sstevel@tonic-gate
910Sstevel@tonic-gate ASSERT(!lockstat_probemap[probe->lsp_probe]);
920Sstevel@tonic-gate
930Sstevel@tonic-gate lockstat_probemap[probe->lsp_probe] = id;
940Sstevel@tonic-gate membar_producer();
950Sstevel@tonic-gate
960Sstevel@tonic-gate lockstat_hot_patch();
970Sstevel@tonic-gate membar_producer();
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * Immediately generate a record for the lockstat_test mutex
1010Sstevel@tonic-gate * to verify that the mutex hot-patch code worked as expected.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate mutex_enter(&lockstat_test);
1040Sstevel@tonic-gate mutex_exit(&lockstat_test);
105*8803SJonathan.Haslam@Sun.COM return (0);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*ARGSUSED*/
1090Sstevel@tonic-gate static void
lockstat_disable(void * arg,dtrace_id_t id,void * parg)1100Sstevel@tonic-gate lockstat_disable(void *arg, dtrace_id_t id, void *parg)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate lockstat_probe_t *probe = parg;
1130Sstevel@tonic-gate int i;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate ASSERT(lockstat_probemap[probe->lsp_probe]);
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate lockstat_probemap[probe->lsp_probe] = 0;
1180Sstevel@tonic-gate lockstat_hot_patch();
1190Sstevel@tonic-gate membar_producer();
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * See if we have any probes left enabled.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate for (i = 0; i < LS_NPROBES; i++) {
1250Sstevel@tonic-gate if (lockstat_probemap[i]) {
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate * This probe is still enabled. We don't need to deal
1280Sstevel@tonic-gate * with waiting for all threads to be out of the
1290Sstevel@tonic-gate * lockstat critical sections; just return.
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate return;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * The delay() here isn't as cheesy as you might think. We don't
1370Sstevel@tonic-gate * want to busy-loop in the kernel, so we have to give up the
1380Sstevel@tonic-gate * CPU between calls to lockstat_active_threads(); that much is
1390Sstevel@tonic-gate * obvious. But the reason it's a do..while loop rather than a
1400Sstevel@tonic-gate * while loop is subtle. The memory barrier above guarantees that
1410Sstevel@tonic-gate * no threads will enter the lockstat code from this point forward.
1420Sstevel@tonic-gate * However, another thread could already be executing lockstat code
1430Sstevel@tonic-gate * without our knowledge if the update to its t_lockstat field hasn't
1440Sstevel@tonic-gate * cleared its CPU's store buffer. Delaying for one clock tick
1450Sstevel@tonic-gate * guarantees that either (1) the thread will have *ample* time to
1460Sstevel@tonic-gate * complete its work, or (2) the thread will be preempted, in which
1470Sstevel@tonic-gate * case it will have to grab and release a dispatcher lock, which
1480Sstevel@tonic-gate * will flush that CPU's store buffer. Either way we're covered.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate do {
1510Sstevel@tonic-gate delay(1);
1520Sstevel@tonic-gate } while (lockstat_active_threads());
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*ARGSUSED*/
1560Sstevel@tonic-gate static int
lockstat_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)1570Sstevel@tonic-gate lockstat_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /* ARGSUSED */
1630Sstevel@tonic-gate static int
lockstat_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1640Sstevel@tonic-gate lockstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate int error;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate switch (infocmd) {
1690Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
1700Sstevel@tonic-gate *result = (void *) lockstat_devi;
1710Sstevel@tonic-gate error = DDI_SUCCESS;
1720Sstevel@tonic-gate break;
1730Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
1740Sstevel@tonic-gate *result = (void *)0;
1750Sstevel@tonic-gate error = DDI_SUCCESS;
1760Sstevel@tonic-gate break;
1770Sstevel@tonic-gate default:
1780Sstevel@tonic-gate error = DDI_FAILURE;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate return (error);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*ARGSUSED*/
1840Sstevel@tonic-gate static void
lockstat_provide(void * arg,const dtrace_probedesc_t * desc)1850Sstevel@tonic-gate lockstat_provide(void *arg, const dtrace_probedesc_t *desc)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate int i = 0;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate for (i = 0; lockstat_probes[i].lsp_func != NULL; i++) {
1900Sstevel@tonic-gate lockstat_probe_t *probe = &lockstat_probes[i];
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate if (dtrace_probe_lookup(lockstat_id, "genunix",
1930Sstevel@tonic-gate probe->lsp_func, probe->lsp_name) != 0)
1940Sstevel@tonic-gate continue;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate ASSERT(!probe->lsp_id);
1970Sstevel@tonic-gate probe->lsp_id = dtrace_probe_create(lockstat_id,
1980Sstevel@tonic-gate "genunix", probe->lsp_func, probe->lsp_name,
1990Sstevel@tonic-gate 1, probe);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*ARGSUSED*/
2040Sstevel@tonic-gate static void
lockstat_destroy(void * arg,dtrace_id_t id,void * parg)2050Sstevel@tonic-gate lockstat_destroy(void *arg, dtrace_id_t id, void *parg)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate lockstat_probe_t *probe = parg;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate ASSERT(!lockstat_probemap[probe->lsp_probe]);
2100Sstevel@tonic-gate probe->lsp_id = 0;
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate static dtrace_pattr_t lockstat_attr = {
2140Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
2150Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
2160Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
2170Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
2180Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
2190Sstevel@tonic-gate };
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate static dtrace_pops_t lockstat_pops = {
2220Sstevel@tonic-gate lockstat_provide,
2230Sstevel@tonic-gate NULL,
2240Sstevel@tonic-gate lockstat_enable,
2250Sstevel@tonic-gate lockstat_disable,
2260Sstevel@tonic-gate NULL,
2270Sstevel@tonic-gate NULL,
2280Sstevel@tonic-gate NULL,
2290Sstevel@tonic-gate NULL,
2300Sstevel@tonic-gate NULL,
2310Sstevel@tonic-gate lockstat_destroy
2320Sstevel@tonic-gate };
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate static int
lockstat_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2350Sstevel@tonic-gate lockstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate switch (cmd) {
2380Sstevel@tonic-gate case DDI_ATTACH:
2390Sstevel@tonic-gate break;
2400Sstevel@tonic-gate case DDI_RESUME:
2410Sstevel@tonic-gate return (DDI_SUCCESS);
2420Sstevel@tonic-gate default:
2430Sstevel@tonic-gate return (DDI_FAILURE);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate if (ddi_create_minor_node(devi, "lockstat", S_IFCHR, 0,
2470Sstevel@tonic-gate DDI_PSEUDO, 0) == DDI_FAILURE ||
2481677Sdp dtrace_register("lockstat", &lockstat_attr, DTRACE_PRIV_KERNEL,
2491677Sdp NULL, &lockstat_pops, NULL, &lockstat_id) != 0) {
2500Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2510Sstevel@tonic-gate return (DDI_FAILURE);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2545876Sjhaslam lockstat_probe = dtrace_probe;
2555876Sjhaslam membar_producer();
2565876Sjhaslam
2570Sstevel@tonic-gate ddi_report_dev(devi);
2580Sstevel@tonic-gate lockstat_devi = devi;
2590Sstevel@tonic-gate return (DDI_SUCCESS);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate static int
lockstat_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2630Sstevel@tonic-gate lockstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate switch (cmd) {
2660Sstevel@tonic-gate case DDI_DETACH:
2670Sstevel@tonic-gate break;
2680Sstevel@tonic-gate case DDI_SUSPEND:
2690Sstevel@tonic-gate return (DDI_SUCCESS);
2700Sstevel@tonic-gate default:
2710Sstevel@tonic-gate return (DDI_FAILURE);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (dtrace_unregister(lockstat_id) != 0)
2750Sstevel@tonic-gate return (DDI_FAILURE);
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2780Sstevel@tonic-gate return (DDI_SUCCESS);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Configuration data structures
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate static struct cb_ops lockstat_cb_ops = {
2850Sstevel@tonic-gate lockstat_open, /* open */
2860Sstevel@tonic-gate nodev, /* close */
2870Sstevel@tonic-gate nulldev, /* strategy */
2880Sstevel@tonic-gate nulldev, /* print */
2890Sstevel@tonic-gate nodev, /* dump */
2900Sstevel@tonic-gate nodev, /* read */
2910Sstevel@tonic-gate nodev, /* write */
2920Sstevel@tonic-gate nodev, /* ioctl */
2930Sstevel@tonic-gate nodev, /* devmap */
2940Sstevel@tonic-gate nodev, /* mmap */
2950Sstevel@tonic-gate nodev, /* segmap */
2960Sstevel@tonic-gate nochpoll, /* poll */
2970Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
2980Sstevel@tonic-gate 0, /* streamtab */
2990Sstevel@tonic-gate D_MP | D_NEW /* Driver compatibility flag */
3000Sstevel@tonic-gate };
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate static struct dev_ops lockstat_ops = {
3030Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
3040Sstevel@tonic-gate 0, /* refcnt */
3050Sstevel@tonic-gate lockstat_info, /* getinfo */
3060Sstevel@tonic-gate nulldev, /* identify */
3070Sstevel@tonic-gate nulldev, /* probe */
3080Sstevel@tonic-gate lockstat_attach, /* attach */
3090Sstevel@tonic-gate lockstat_detach, /* detach */
3100Sstevel@tonic-gate nulldev, /* reset */
3110Sstevel@tonic-gate &lockstat_cb_ops, /* cb_ops */
3120Sstevel@tonic-gate NULL, /* bus_ops */
3137656SSherry.Moore@Sun.COM NULL, /* power */
3147656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
3150Sstevel@tonic-gate };
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate static struct modldrv modldrv = {
3180Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */
3197656SSherry.Moore@Sun.COM "Lock Statistics", /* name of module */
3200Sstevel@tonic-gate &lockstat_ops, /* driver ops */
3210Sstevel@tonic-gate };
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate static struct modlinkage modlinkage = {
3240Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL
3250Sstevel@tonic-gate };
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate int
_init(void)3280Sstevel@tonic-gate _init(void)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate return (mod_install(&modlinkage));
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate int
_fini(void)3340Sstevel@tonic-gate _fini(void)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate return (mod_remove(&modlinkage));
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3400Sstevel@tonic-gate _info(struct modinfo *modinfop)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
3430Sstevel@tonic-gate }
344