1a5aedd68SStacey Son /*- 2*8a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*8a36da99SPedro F. Giffuni * 4a5aedd68SStacey Son * Copyright 2008-2009 Stacey Son <sson@FreeBSD.org> 5a5aedd68SStacey Son * 6a5aedd68SStacey Son * Redistribution and use in source and binary forms, with or without 7a5aedd68SStacey Son * modification, are permitted provided that the following conditions 8a5aedd68SStacey Son * are met: 9a5aedd68SStacey Son * 1. Redistributions of source code must retain the above copyright 10a5aedd68SStacey Son * notice, this list of conditions and the following disclaimer. 11a5aedd68SStacey Son * 2. Redistributions in binary form must reproduce the above copyright 12a5aedd68SStacey Son * notice, this list of conditions and the following disclaimer in the 13a5aedd68SStacey Son * documentation and/or other materials provided with the distribution. 14a5aedd68SStacey Son * 15a5aedd68SStacey Son * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16a5aedd68SStacey Son * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17a5aedd68SStacey Son * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18a5aedd68SStacey Son * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 19a5aedd68SStacey Son * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20a5aedd68SStacey Son * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21a5aedd68SStacey Son * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22a5aedd68SStacey Son * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23a5aedd68SStacey Son * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24a5aedd68SStacey Son * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25a5aedd68SStacey Son * SUCH DAMAGE. 26a5aedd68SStacey Son */ 27a5aedd68SStacey Son 2832cd0147SMark Johnston #include <sys/cdefs.h> 2932cd0147SMark Johnston __FBSDID("$FreeBSD$"); 30a5aedd68SStacey Son 3132cd0147SMark Johnston #include <sys/param.h> 3229051116SMateusz Guzik #include <sys/systm.h> 33e2b25737SMark Johnston #include <sys/lock.h> 34a5aedd68SStacey Son #include <sys/lockstat.h> 3532cd0147SMark Johnston #include <sys/sdt.h> 36e2b25737SMark Johnston #include <sys/time.h> 37a5aedd68SStacey Son 3832cd0147SMark Johnston SDT_PROVIDER_DEFINE(lockstat); 3932cd0147SMark Johnston 4032cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , adaptive__acquire, "struct mtx *"); 4132cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , adaptive__release, "struct mtx *"); 4232cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , adaptive__spin, "struct mtx *", "uint64_t"); 4332cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , adaptive__block, "struct mtx *", "uint64_t"); 4432cd0147SMark Johnston 4532cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , spin__acquire, "struct mtx *"); 4632cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , spin__release, "struct mtx *"); 4732cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , spin__spin, "struct mtx *", "uint64_t"); 4832cd0147SMark Johnston 49de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__acquire, "struct rwlock *", "int"); 50de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__release, "struct rwlock *", "int"); 5132cd0147SMark Johnston SDT_PROBE_DEFINE5(lockstat, , , rw__block, "struct rwlock *", "uint64_t", "int", 5232cd0147SMark Johnston "int", "int"); 5332cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__spin, "struct rwlock *", "uint64_t"); 5432cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , rw__upgrade, "struct rwlock *"); 5532cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , rw__downgrade, "struct rwlock *"); 5632cd0147SMark Johnston 57de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__acquire, "struct sx *", "int"); 58de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__release, "struct sx *", "int"); 5932cd0147SMark Johnston SDT_PROBE_DEFINE5(lockstat, , , sx__block, "struct sx *", "uint64_t", "int", 6032cd0147SMark Johnston "int", "int"); 6132cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__spin, "struct sx *", "uint64_t"); 6232cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , sx__upgrade, "struct sx *"); 6332cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , sx__downgrade, "struct sx *"); 6432cd0147SMark Johnston 6532cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , thread__spin, "struct mtx *", "uint64_t"); 6632cd0147SMark Johnston 67574adb65SMateusz Guzik volatile int __read_frequently lockstat_enabled; 68a5aedd68SStacey Son 69a5aedd68SStacey Son uint64_t 70e2b25737SMark Johnston lockstat_nsecs(struct lock_object *lo) 71a5aedd68SStacey Son { 72a5aedd68SStacey Son struct bintime bt; 73a5aedd68SStacey Son uint64_t ns; 74a5aedd68SStacey Son 75efe8b26bSMark Johnston if (!lockstat_enabled) 76efe8b26bSMark Johnston return (0); 77e2b25737SMark Johnston if ((lo->lo_flags & LO_NOPROFILE) != 0) 78e2b25737SMark Johnston return (0); 79efe8b26bSMark Johnston 80a5aedd68SStacey Son binuptime(&bt); 81a5aedd68SStacey Son ns = bt.sec * (uint64_t)1000000000; 82a5aedd68SStacey Son ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32; 83a5aedd68SStacey Son return (ns); 84a5aedd68SStacey Son } 85