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*5876Sjhaslam * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/stat.h> 310Sstevel@tonic-gate #include <sys/open.h> 320Sstevel@tonic-gate #include <sys/file.h> 330Sstevel@tonic-gate #include <sys/conf.h> 340Sstevel@tonic-gate #include <sys/modctl.h> 350Sstevel@tonic-gate #include <sys/cmn_err.h> 360Sstevel@tonic-gate #include <sys/bitmap.h> 370Sstevel@tonic-gate #include <sys/debug.h> 380Sstevel@tonic-gate #include <sys/kmem.h> 390Sstevel@tonic-gate #include <sys/errno.h> 400Sstevel@tonic-gate #include <sys/sysmacros.h> 410Sstevel@tonic-gate #include <sys/lockstat.h> 420Sstevel@tonic-gate #include <sys/atomic.h> 430Sstevel@tonic-gate #include <sys/dtrace.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include <sys/ddi.h> 460Sstevel@tonic-gate #include <sys/sunddi.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate typedef struct lockstat_probe { 490Sstevel@tonic-gate const char *lsp_func; 500Sstevel@tonic-gate const char *lsp_name; 510Sstevel@tonic-gate int lsp_probe; 520Sstevel@tonic-gate dtrace_id_t lsp_id; 530Sstevel@tonic-gate } lockstat_probe_t; 540Sstevel@tonic-gate 550Sstevel@tonic-gate lockstat_probe_t lockstat_probes[] = 560Sstevel@tonic-gate { 570Sstevel@tonic-gate { LS_MUTEX_ENTER, LSA_ACQUIRE, LS_MUTEX_ENTER_ACQUIRE }, 580Sstevel@tonic-gate { LS_MUTEX_ENTER, LSA_BLOCK, LS_MUTEX_ENTER_BLOCK }, 590Sstevel@tonic-gate { LS_MUTEX_ENTER, LSA_SPIN, LS_MUTEX_ENTER_SPIN }, 600Sstevel@tonic-gate { LS_MUTEX_EXIT, LSA_RELEASE, LS_MUTEX_EXIT_RELEASE }, 610Sstevel@tonic-gate { LS_MUTEX_DESTROY, LSA_RELEASE, LS_MUTEX_DESTROY_RELEASE }, 620Sstevel@tonic-gate { LS_MUTEX_TRYENTER, LSA_ACQUIRE, LS_MUTEX_TRYENTER_ACQUIRE }, 630Sstevel@tonic-gate { LS_LOCK_SET, LSS_ACQUIRE, LS_LOCK_SET_ACQUIRE }, 640Sstevel@tonic-gate { LS_LOCK_SET, LSS_SPIN, LS_LOCK_SET_SPIN }, 650Sstevel@tonic-gate { LS_LOCK_SET_SPL, LSS_ACQUIRE, LS_LOCK_SET_SPL_ACQUIRE }, 660Sstevel@tonic-gate { LS_LOCK_SET_SPL, LSS_SPIN, LS_LOCK_SET_SPL_SPIN }, 670Sstevel@tonic-gate { LS_LOCK_TRY, LSS_ACQUIRE, LS_LOCK_TRY_ACQUIRE }, 680Sstevel@tonic-gate { LS_LOCK_CLEAR, LSS_RELEASE, LS_LOCK_CLEAR_RELEASE }, 690Sstevel@tonic-gate { LS_LOCK_CLEAR_SPLX, LSS_RELEASE, LS_LOCK_CLEAR_SPLX_RELEASE }, 700Sstevel@tonic-gate { LS_CLOCK_UNLOCK, LSS_RELEASE, LS_CLOCK_UNLOCK_RELEASE }, 710Sstevel@tonic-gate { LS_RW_ENTER, LSR_ACQUIRE, LS_RW_ENTER_ACQUIRE }, 720Sstevel@tonic-gate { LS_RW_ENTER, LSR_BLOCK, LS_RW_ENTER_BLOCK }, 730Sstevel@tonic-gate { LS_RW_EXIT, LSR_RELEASE, LS_RW_EXIT_RELEASE }, 740Sstevel@tonic-gate { LS_RW_TRYENTER, LSR_ACQUIRE, LS_RW_TRYENTER_ACQUIRE }, 750Sstevel@tonic-gate { LS_RW_TRYUPGRADE, LSR_UPGRADE, LS_RW_TRYUPGRADE_UPGRADE }, 760Sstevel@tonic-gate { LS_RW_DOWNGRADE, LSR_DOWNGRADE, LS_RW_DOWNGRADE_DOWNGRADE }, 770Sstevel@tonic-gate { LS_THREAD_LOCK, LST_SPIN, LS_THREAD_LOCK_SPIN }, 780Sstevel@tonic-gate { LS_THREAD_LOCK_HIGH, LST_SPIN, LS_THREAD_LOCK_HIGH_SPIN }, 790Sstevel@tonic-gate { NULL } 800Sstevel@tonic-gate }; 810Sstevel@tonic-gate 820Sstevel@tonic-gate static dev_info_t *lockstat_devi; /* saved in xxattach() for xxinfo() */ 830Sstevel@tonic-gate static kmutex_t lockstat_test; /* for testing purposes only */ 840Sstevel@tonic-gate static dtrace_provider_id_t lockstat_id; 850Sstevel@tonic-gate 860Sstevel@tonic-gate /*ARGSUSED*/ 870Sstevel@tonic-gate static void 880Sstevel@tonic-gate lockstat_enable(void *arg, dtrace_id_t id, void *parg) 890Sstevel@tonic-gate { 900Sstevel@tonic-gate lockstat_probe_t *probe = parg; 910Sstevel@tonic-gate 920Sstevel@tonic-gate ASSERT(!lockstat_probemap[probe->lsp_probe]); 930Sstevel@tonic-gate 940Sstevel@tonic-gate lockstat_probemap[probe->lsp_probe] = id; 950Sstevel@tonic-gate membar_producer(); 960Sstevel@tonic-gate 970Sstevel@tonic-gate lockstat_hot_patch(); 980Sstevel@tonic-gate membar_producer(); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Immediately generate a record for the lockstat_test mutex 1020Sstevel@tonic-gate * to verify that the mutex hot-patch code worked as expected. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate mutex_enter(&lockstat_test); 1050Sstevel@tonic-gate mutex_exit(&lockstat_test); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /*ARGSUSED*/ 1090Sstevel@tonic-gate static void 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 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 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 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 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 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 254*5876Sjhaslam lockstat_probe = dtrace_probe; 255*5876Sjhaslam membar_producer(); 256*5876Sjhaslam 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 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 */ 3130Sstevel@tonic-gate }; 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate static struct modldrv modldrv = { 3160Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 3170Sstevel@tonic-gate "Lock Statistics %I%", /* name of module */ 3180Sstevel@tonic-gate &lockstat_ops, /* driver ops */ 3190Sstevel@tonic-gate }; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate static struct modlinkage modlinkage = { 3220Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 3230Sstevel@tonic-gate }; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate int 3260Sstevel@tonic-gate _init(void) 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate return (mod_install(&modlinkage)); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate int 3320Sstevel@tonic-gate _fini(void) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate return (mod_remove(&modlinkage)); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate int 3380Sstevel@tonic-gate _info(struct modinfo *modinfop) 3390Sstevel@tonic-gate { 3400Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3410Sstevel@tonic-gate } 342