1192853Ssson/*-
2192853Ssson * Copyright 2008-2009 Stacey Son <sson@FreeBSD.org>
3192853Ssson *
4192853Ssson * Redistribution and use in source and binary forms, with or without
5192853Ssson * modification, are permitted provided that the following conditions
6192853Ssson * are met:
7192853Ssson * 1. Redistributions of source code must retain the above copyright
8192853Ssson *    notice, this list of conditions and the following disclaimer.
9192853Ssson * 2. Redistributions in binary form must reproduce the above copyright
10192853Ssson *    notice, this list of conditions and the following disclaimer in the
11192853Ssson *    documentation and/or other materials provided with the distribution.
12192853Ssson *
13192853Ssson * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14192853Ssson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15192853Ssson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16192853Ssson * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17192853Ssson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18192853Ssson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19192853Ssson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20192853Ssson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21192853Ssson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22192853Ssson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23192853Ssson * SUCH DAMAGE.
24192853Ssson *
25192853Ssson * $FreeBSD$
26192853Ssson */
27192853Ssson
28192853Ssson/*
29192853Ssson * Backend for the lock tracing (lockstat) kernel support. This is required
30192853Ssson * to allow a module to load even though DTrace kernel support may not be
31192853Ssson * present.
32192853Ssson *
33192853Ssson */
34192853Ssson
35192853Ssson#include "opt_kdtrace.h"
36192853Ssson
37192853Ssson#ifdef KDTRACE_HOOKS
38192853Ssson
39192853Ssson#include <sys/types.h>
40285759Smarkj#include <sys/lock.h>
41192853Ssson#include <sys/lockstat.h>
42285759Smarkj#include <sys/time.h>
43192853Ssson
44192853Ssson/*
45192853Ssson * The following must match the type definition of dtrace_probe.  It is
46192853Ssson * defined this way to avoid having to rely on CDDL code.
47192853Ssson */
48192853Sssonuint32_t lockstat_probemap[LS_NPROBES];
49192853Sssonvoid (*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
50192853Ssson    uintptr_t, uintptr_t, uintptr_t);
51285759Smarkjint lockstat_enabled = 0;
52192853Ssson
53192853Sssonuint64_t
54285759Smarkjlockstat_nsecs(struct lock_object *lo)
55192853Ssson{
56192853Ssson	struct bintime bt;
57192853Ssson	uint64_t ns;
58192853Ssson
59285759Smarkj	if (!lockstat_enabled)
60285759Smarkj		return (0);
61285759Smarkj	if ((lo->lo_flags & LO_NOPROFILE) != 0)
62285759Smarkj		return (0);
63285759Smarkj
64192853Ssson	binuptime(&bt);
65192853Ssson	ns = bt.sec * (uint64_t)1000000000;
66192853Ssson	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
67192853Ssson	return (ns);
68192853Ssson}
69192853Ssson
70192853Ssson#endif /* KDTRACE_HOOKS */
71