1a5aedd68SStacey Son /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro 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/param.h>
2929051116SMateusz Guzik #include <sys/systm.h>
30e2b25737SMark Johnston #include <sys/lock.h>
31a5aedd68SStacey Son #include <sys/lockstat.h>
3232cd0147SMark Johnston #include <sys/sdt.h>
33e2b25737SMark Johnston #include <sys/time.h>
34a5aedd68SStacey Son
3532cd0147SMark Johnston SDT_PROVIDER_DEFINE(lockstat);
3632cd0147SMark Johnston
3732cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , adaptive__acquire, "struct mtx *");
3832cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , adaptive__release, "struct mtx *");
3932cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , adaptive__spin, "struct mtx *", "uint64_t");
4032cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , adaptive__block, "struct mtx *", "uint64_t");
4132cd0147SMark Johnston
4232cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , spin__acquire, "struct mtx *");
4332cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , spin__release, "struct mtx *");
4432cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , spin__spin, "struct mtx *", "uint64_t");
4532cd0147SMark Johnston
46de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__acquire, "struct rwlock *", "int");
47de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__release, "struct rwlock *", "int");
4832cd0147SMark Johnston SDT_PROBE_DEFINE5(lockstat, , , rw__block, "struct rwlock *", "uint64_t", "int",
4932cd0147SMark Johnston "int", "int");
5032cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__spin, "struct rwlock *", "uint64_t");
5132cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , rw__upgrade, "struct rwlock *");
5232cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , rw__downgrade, "struct rwlock *");
5332cd0147SMark Johnston
54de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__acquire, "struct sx *", "int");
55de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__release, "struct sx *", "int");
5632cd0147SMark Johnston SDT_PROBE_DEFINE5(lockstat, , , sx__block, "struct sx *", "uint64_t", "int",
5732cd0147SMark Johnston "int", "int");
5832cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__spin, "struct sx *", "uint64_t");
5932cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , sx__upgrade, "struct sx *");
6032cd0147SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , sx__downgrade, "struct sx *");
6132cd0147SMark Johnston
625b699f16SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , lockmgr__acquire, "struct lock *", "int");
635b699f16SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , lockmgr__release, "struct lock *", "int");
645b699f16SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , lockmgr__disown, "struct lock *", "int");
655b699f16SMark Johnston SDT_PROBE_DEFINE5(lockstat, , , lockmgr__block, "struct lock *", "uint64_t",
665b699f16SMark Johnston "int", "int", "int");
675b699f16SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , lockmgr__upgrade, "struct lock *");
685b699f16SMark Johnston SDT_PROBE_DEFINE1(lockstat, , , lockmgr__downgrade, "struct lock *");
695b699f16SMark Johnston
7032cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , thread__spin, "struct mtx *", "uint64_t");
7132cd0147SMark Johnston
724c5209cbSMateusz Guzik volatile bool __read_frequently lockstat_enabled;
73a5aedd68SStacey Son
74a5aedd68SStacey Son uint64_t
lockstat_nsecs(struct lock_object * lo)75e2b25737SMark Johnston lockstat_nsecs(struct lock_object *lo)
76a5aedd68SStacey Son {
77a5aedd68SStacey Son struct bintime bt;
78a5aedd68SStacey Son uint64_t ns;
79a5aedd68SStacey Son
80efe8b26bSMark Johnston if (!lockstat_enabled)
81efe8b26bSMark Johnston return (0);
82e2b25737SMark Johnston if ((lo->lo_flags & LO_NOPROFILE) != 0)
83e2b25737SMark Johnston return (0);
84efe8b26bSMark Johnston
85a5aedd68SStacey Son binuptime(&bt);
86a5aedd68SStacey Son ns = bt.sec * (uint64_t)1000000000;
87a5aedd68SStacey Son ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
88a5aedd68SStacey Son return (ns);
89a5aedd68SStacey Son }
90