xref: /freebsd/sys/kern/kern_lockstat.c (revision efe8b26b82a7fc541e2f6a112ce34bf7a3d84973)
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  * $FreeBSD$
26a5aedd68SStacey Son  */
27a5aedd68SStacey Son 
28a5aedd68SStacey Son /*
29a5aedd68SStacey Son  * Backend for the lock tracing (lockstat) kernel support. This is required
30a5aedd68SStacey Son  * to allow a module to load even though DTrace kernel support may not be
31a5aedd68SStacey Son  * present.
32a5aedd68SStacey Son  *
33a5aedd68SStacey Son  */
34a5aedd68SStacey Son 
35a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
36a5aedd68SStacey Son 
37a5aedd68SStacey Son #include <sys/time.h>
38a5aedd68SStacey Son #include <sys/types.h>
39a5aedd68SStacey Son #include <sys/lockstat.h>
40a5aedd68SStacey Son 
41a5aedd68SStacey Son /*
42a5aedd68SStacey Son  * The following must match the type definition of dtrace_probe.  It is
43a5aedd68SStacey Son  * defined this way to avoid having to rely on CDDL code.
44a5aedd68SStacey Son  */
45a5aedd68SStacey Son uint32_t lockstat_probemap[LS_NPROBES];
46a5aedd68SStacey Son void (*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
47a5aedd68SStacey Son     uintptr_t, uintptr_t, uintptr_t);
48*efe8b26bSMark Johnston int lockstat_enabled = 0;
49a5aedd68SStacey Son 
50a5aedd68SStacey Son uint64_t
51a5aedd68SStacey Son lockstat_nsecs(void)
52a5aedd68SStacey Son {
53a5aedd68SStacey Son 	struct bintime bt;
54a5aedd68SStacey Son 	uint64_t ns;
55a5aedd68SStacey Son 
56*efe8b26bSMark Johnston 	if (!lockstat_enabled)
57*efe8b26bSMark Johnston 		return (0);
58*efe8b26bSMark Johnston 
59a5aedd68SStacey Son 	binuptime(&bt);
60a5aedd68SStacey Son 	ns = bt.sec * (uint64_t)1000000000;
61a5aedd68SStacey Son 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
62a5aedd68SStacey Son 	return (ns);
63a5aedd68SStacey Son }
64a5aedd68SStacey Son 
65a5aedd68SStacey Son #endif /* KDTRACE_HOOKS */
66