xref: /freebsd/sys/kern/kern_lockstat.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2008-2009 Stacey Son <sson@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/lockstat.h>
35 #include <sys/sdt.h>
36 #include <sys/time.h>
37 
38 SDT_PROVIDER_DEFINE(lockstat);
39 
40 SDT_PROBE_DEFINE1(lockstat, , , adaptive__acquire, "struct mtx *");
41 SDT_PROBE_DEFINE1(lockstat, , , adaptive__release, "struct mtx *");
42 SDT_PROBE_DEFINE2(lockstat, , , adaptive__spin, "struct mtx *", "uint64_t");
43 SDT_PROBE_DEFINE2(lockstat, , , adaptive__block, "struct mtx *", "uint64_t");
44 
45 SDT_PROBE_DEFINE1(lockstat, , , spin__acquire, "struct mtx *");
46 SDT_PROBE_DEFINE1(lockstat, , , spin__release, "struct mtx *");
47 SDT_PROBE_DEFINE2(lockstat, , , spin__spin, "struct mtx *", "uint64_t");
48 
49 SDT_PROBE_DEFINE2(lockstat, , , rw__acquire, "struct rwlock *", "int");
50 SDT_PROBE_DEFINE2(lockstat, , , rw__release, "struct rwlock *", "int");
51 SDT_PROBE_DEFINE5(lockstat, , , rw__block, "struct rwlock *", "uint64_t", "int",
52     "int", "int");
53 SDT_PROBE_DEFINE2(lockstat, , , rw__spin, "struct rwlock *", "uint64_t");
54 SDT_PROBE_DEFINE1(lockstat, , , rw__upgrade, "struct rwlock *");
55 SDT_PROBE_DEFINE1(lockstat, , , rw__downgrade, "struct rwlock *");
56 
57 SDT_PROBE_DEFINE2(lockstat, , , sx__acquire, "struct sx *", "int");
58 SDT_PROBE_DEFINE2(lockstat, , , sx__release, "struct sx *", "int");
59 SDT_PROBE_DEFINE5(lockstat, , , sx__block, "struct sx *", "uint64_t", "int",
60     "int", "int");
61 SDT_PROBE_DEFINE2(lockstat, , , sx__spin, "struct sx *", "uint64_t");
62 SDT_PROBE_DEFINE1(lockstat, , , sx__upgrade, "struct sx *");
63 SDT_PROBE_DEFINE1(lockstat, , , sx__downgrade, "struct sx *");
64 
65 SDT_PROBE_DEFINE2(lockstat, , , thread__spin, "struct mtx *", "uint64_t");
66 
67 volatile bool __read_frequently lockstat_enabled;
68 
69 uint64_t
70 lockstat_nsecs(struct lock_object *lo)
71 {
72 	struct bintime bt;
73 	uint64_t ns;
74 
75 	if (!lockstat_enabled)
76 		return (0);
77 	if ((lo->lo_flags & LO_NOPROFILE) != 0)
78 		return (0);
79 
80 	binuptime(&bt);
81 	ns = bt.sec * (uint64_t)1000000000;
82 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
83 	return (ns);
84 }
85