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