xref: /freebsd/sys/kern/kern_lockstat.c (revision de2c95cc007982be33371947aade23e3b045776d)
1a5aedd68SStacey Son /*-
2a5aedd68SStacey Son  * Copyright 2008-2009 Stacey Son <sson@FreeBSD.org>
3a5aedd68SStacey Son  *
4a5aedd68SStacey Son  * Redistribution and use in source and binary forms, with or without
5a5aedd68SStacey Son  * modification, are permitted provided that the following conditions
6a5aedd68SStacey Son  * are met:
7a5aedd68SStacey Son  * 1. Redistributions of source code must retain the above copyright
8a5aedd68SStacey Son  *    notice, this list of conditions and the following disclaimer.
9a5aedd68SStacey Son  * 2. Redistributions in binary form must reproduce the above copyright
10a5aedd68SStacey Son  *    notice, this list of conditions and the following disclaimer in the
11a5aedd68SStacey Son  *    documentation and/or other materials provided with the distribution.
12a5aedd68SStacey Son  *
13a5aedd68SStacey Son  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14a5aedd68SStacey Son  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15a5aedd68SStacey Son  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16a5aedd68SStacey Son  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17a5aedd68SStacey Son  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18a5aedd68SStacey Son  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19a5aedd68SStacey Son  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20a5aedd68SStacey Son  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21a5aedd68SStacey Son  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22a5aedd68SStacey Son  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23a5aedd68SStacey Son  * SUCH DAMAGE.
24a5aedd68SStacey Son  */
25a5aedd68SStacey Son 
2632cd0147SMark Johnston #include <sys/cdefs.h>
2732cd0147SMark Johnston __FBSDID("$FreeBSD$");
28a5aedd68SStacey Son 
2932cd0147SMark Johnston #include <sys/param.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 
46*de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , rw__acquire, "struct rwlock *", "int");
47*de2c95ccSMark 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 
54*de2c95ccSMark Johnston SDT_PROBE_DEFINE2(lockstat, , , sx__acquire, "struct sx *", "int");
55*de2c95ccSMark 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 
6232cd0147SMark Johnston SDT_PROBE_DEFINE2(lockstat, , , thread__spin, "struct mtx *", "uint64_t");
6332cd0147SMark Johnston 
64efe8b26bSMark Johnston int lockstat_enabled = 0;
65a5aedd68SStacey Son 
66a5aedd68SStacey Son uint64_t
67e2b25737SMark Johnston lockstat_nsecs(struct lock_object *lo)
68a5aedd68SStacey Son {
69a5aedd68SStacey Son 	struct bintime bt;
70a5aedd68SStacey Son 	uint64_t ns;
71a5aedd68SStacey Son 
72efe8b26bSMark Johnston 	if (!lockstat_enabled)
73efe8b26bSMark Johnston 		return (0);
74e2b25737SMark Johnston 	if ((lo->lo_flags & LO_NOPROFILE) != 0)
75e2b25737SMark Johnston 		return (0);
76efe8b26bSMark Johnston 
77a5aedd68SStacey Son 	binuptime(&bt);
78a5aedd68SStacey Son 	ns = bt.sec * (uint64_t)1000000000;
79a5aedd68SStacey Son 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
80a5aedd68SStacey Son 	return (ns);
81a5aedd68SStacey Son }
82