lockstat.h revision 1.2.2.3 1 1.2.2.3 yamt /* $NetBSD: lockstat.h,v 1.2.2.3 2007/02/26 09:09:55 yamt Exp $ */
2 1.2.2.2 yamt
3 1.2.2.2 yamt /*-
4 1.2.2.2 yamt * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.2.2.2 yamt * All rights reserved.
6 1.2.2.2 yamt *
7 1.2.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 yamt * by Andrew Doran.
9 1.2.2.2 yamt *
10 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 yamt * modification, are permitted provided that the following conditions
12 1.2.2.2 yamt * are met:
13 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.2.2.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.2.2.2 yamt * must display the following acknowledgement:
20 1.2.2.2 yamt * This product includes software developed by the NetBSD
21 1.2.2.2 yamt * Foundation, Inc. and its contributors.
22 1.2.2.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.2.2 yamt * contributors may be used to endorse or promote products derived
24 1.2.2.2 yamt * from this software without specific prior written permission.
25 1.2.2.2 yamt *
26 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.2.2.2 yamt */
38 1.2.2.2 yamt
39 1.2.2.2 yamt #ifndef _SYS_LOCKSTAT_H_
40 1.2.2.2 yamt #define _SYS_LOCKSTAT_H_
41 1.2.2.2 yamt
42 1.2.2.2 yamt #ifdef _KERNEL_OPT
43 1.2.2.2 yamt #include <lockstat.h>
44 1.2.2.2 yamt #endif
45 1.2.2.2 yamt
46 1.2.2.2 yamt #include <sys/types.h>
47 1.2.2.2 yamt #include <sys/ioccom.h>
48 1.2.2.2 yamt #include <sys/queue.h>
49 1.2.2.2 yamt #include <sys/time.h>
50 1.2.2.2 yamt
51 1.2.2.2 yamt #if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER)
52 1.2.2.2 yamt #include <machine/cpu_counter.h>
53 1.2.2.2 yamt #endif
54 1.2.2.2 yamt
55 1.2.2.2 yamt /*
56 1.2.2.2 yamt * Interface version. The interface is not designed to provide
57 1.2.2.2 yamt * compatibility across NetBSD releases.
58 1.2.2.2 yamt */
59 1.2.2.2 yamt
60 1.2.2.2 yamt #define IOC_LOCKSTAT_GVERSION _IOR('L', 0, int)
61 1.2.2.2 yamt
62 1.2.2.2 yamt #define LS_VERSION 3
63 1.2.2.2 yamt
64 1.2.2.2 yamt /*
65 1.2.2.2 yamt * Enable request. We can limit tracing by the call site and by
66 1.2.2.2 yamt * the lock. We also specify the number of event buffers to
67 1.2.2.2 yamt * allocate up front, and what kind of events to track.
68 1.2.2.2 yamt */
69 1.2.2.2 yamt
70 1.2.2.2 yamt #define IOC_LOCKSTAT_ENABLE _IOW('L', 1, lsenable_t)
71 1.2.2.2 yamt
72 1.2.2.2 yamt #define LE_CALLSITE 0x01 /* track call sites */
73 1.2.2.2 yamt #define LE_ONE_CALLSITE 0x02 /* specific call site */
74 1.2.2.2 yamt #define LE_ONE_LOCK 0x04 /* specific lock */
75 1.2.2.2 yamt
76 1.2.2.2 yamt typedef struct lsenable {
77 1.2.2.2 yamt uintptr_t le_csstart; /* callsite start */
78 1.2.2.2 yamt uintptr_t le_csend; /* callsite end */
79 1.2.2.2 yamt uintptr_t le_lockstart; /* lock address start */
80 1.2.2.2 yamt uintptr_t le_lockend; /* lock address end */
81 1.2.2.2 yamt uintptr_t le_nbufs; /* buffers to allocate, 0 = default */
82 1.2.2.2 yamt u_int le_flags; /* request flags */
83 1.2.2.2 yamt u_int le_mask; /* event mask (LB_*) */
84 1.2.2.2 yamt } lsenable_t;
85 1.2.2.2 yamt
86 1.2.2.2 yamt /*
87 1.2.2.2 yamt * Disable request.
88 1.2.2.2 yamt */
89 1.2.2.2 yamt
90 1.2.2.2 yamt #define IOC_LOCKSTAT_DISABLE _IOR('L', 2, lsdisable_t)
91 1.2.2.2 yamt
92 1.2.2.2 yamt typedef struct lsdisable {
93 1.2.2.2 yamt size_t ld_size; /* buffer space allocated */
94 1.2.2.2 yamt struct timespec ld_time; /* time spent enabled */
95 1.2.2.2 yamt uint64_t ld_freq[64]; /* counter HZ by CPU number */
96 1.2.2.2 yamt } lsdisable_t;
97 1.2.2.2 yamt
98 1.2.2.2 yamt /*
99 1.2.2.2 yamt * Event buffers returned from reading from the devices.
100 1.2.2.2 yamt */
101 1.2.2.2 yamt
102 1.2.2.2 yamt /*
103 1.2.2.2 yamt * Event types, for lockstat_event(). Stored in lb_flags but should be
104 1.2.2.2 yamt * meaningless to the consumer, also provided with the enable request
105 1.2.2.2 yamt * in le_mask.
106 1.2.2.2 yamt */
107 1.2.2.2 yamt #define LB_SPIN 0x00000001
108 1.2.2.2 yamt #define LB_SLEEP1 0x00000002
109 1.2.2.2 yamt #define LB_SLEEP2 0x00000003
110 1.2.2.2 yamt #define LB_NEVENT 0x00000003
111 1.2.2.2 yamt #define LB_EVENT_MASK 0x000000ff
112 1.2.2.2 yamt
113 1.2.2.2 yamt /*
114 1.2.2.2 yamt * Lock types, the only part of lb_flags that should be inspected. Also
115 1.2.2.2 yamt * provided with the enable request in le_mask.
116 1.2.2.2 yamt */
117 1.2.2.2 yamt #define LB_ADAPTIVE_MUTEX 0x00000100
118 1.2.2.2 yamt #define LB_SPIN_MUTEX 0x00000200
119 1.2.2.2 yamt #define LB_RWLOCK 0x00000300
120 1.2.2.2 yamt #define LB_LOCKMGR 0x00000400
121 1.2.2.2 yamt #define LB_KERNEL_LOCK 0x00000500
122 1.2.2.3 yamt #define LB_MISC 0x00000600
123 1.2.2.3 yamt #define LB_NLOCK 0x00000600
124 1.2.2.2 yamt #define LB_LOCK_MASK 0x0000ff00
125 1.2.2.2 yamt #define LB_LOCK_SHIFT 8
126 1.2.2.2 yamt
127 1.2.2.2 yamt typedef struct lsbuf {
128 1.2.2.2 yamt union {
129 1.2.2.2 yamt LIST_ENTRY(lsbuf) list;
130 1.2.2.2 yamt SLIST_ENTRY(lsbuf) slist;
131 1.2.2.2 yamt TAILQ_ENTRY(lsbuf) tailq;
132 1.2.2.2 yamt } lb_chain;
133 1.2.2.2 yamt uintptr_t lb_lock; /* lock address */
134 1.2.2.2 yamt uintptr_t lb_callsite; /* call site */
135 1.2.2.2 yamt uint64_t lb_times[LB_NEVENT]; /* cumulative times */
136 1.2.2.2 yamt uint32_t lb_counts[LB_NEVENT]; /* count of events */
137 1.2.2.2 yamt uint16_t lb_flags; /* lock type */
138 1.2.2.2 yamt uint16_t lb_cpu; /* CPU number */
139 1.2.2.2 yamt } lsbuf_t;
140 1.2.2.2 yamt
141 1.2.2.2 yamt /*
142 1.2.2.2 yamt * Tracing stubs used by lock providers, e.g. lockmgr().
143 1.2.2.2 yamt */
144 1.2.2.2 yamt
145 1.2.2.2 yamt #if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER) && NLOCKSTAT > 0
146 1.2.2.2 yamt
147 1.2.2.3 yamt #define LOCKSTAT_EVENT(flag, lock, type, count, time) \
148 1.2.2.2 yamt do { \
149 1.2.2.3 yamt if (__predict_false(flag)) \
150 1.2.2.2 yamt lockstat_event((uintptr_t)(lock), \
151 1.2.2.2 yamt (uintptr_t)__builtin_return_address(0), \
152 1.2.2.2 yamt (type), (count), (time)); \
153 1.2.2.2 yamt } while (/* CONSTCOND */ 0);
154 1.2.2.2 yamt
155 1.2.2.3 yamt #define LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra) \
156 1.2.2.2 yamt do { \
157 1.2.2.3 yamt if (__predict_false(flag)) \
158 1.2.2.2 yamt lockstat_event((uintptr_t)(lock), (uintptr_t)ra, \
159 1.2.2.2 yamt (type), (count), (time)); \
160 1.2.2.2 yamt } while (/* CONSTCOND */ 0);
161 1.2.2.2 yamt
162 1.2.2.2 yamt #define LOCKSTAT_TIMER(name) uint64_t name = 0
163 1.2.2.2 yamt #define LOCKSTAT_COUNTER(name) uint64_t name = 0
164 1.2.2.3 yamt #define LOCKSTAT_FLAG(name) int name
165 1.2.2.3 yamt #define LOCKSTAT_ENTER(name) name = lockstat_enabled
166 1.2.2.3 yamt #define LOCKSTAT_EXIT(name)
167 1.2.2.2 yamt
168 1.2.2.3 yamt #define LOCKSTAT_START_TIMER(flag, name) \
169 1.2.2.2 yamt do { \
170 1.2.2.3 yamt if (__predict_false(flag)) \
171 1.2.2.3 yamt (name) -= cpu_counter(); \
172 1.2.2.2 yamt } while (/* CONSTCOND */ 0)
173 1.2.2.2 yamt
174 1.2.2.3 yamt #define LOCKSTAT_STOP_TIMER(flag, name) \
175 1.2.2.2 yamt do { \
176 1.2.2.3 yamt if (__predict_false(flag)) \
177 1.2.2.3 yamt (name) += cpu_counter(); \
178 1.2.2.2 yamt } while (/* CONSTCOND */ 0)
179 1.2.2.2 yamt
180 1.2.2.2 yamt #define LOCKSTAT_COUNT(name, inc) \
181 1.2.2.2 yamt do { \
182 1.2.2.2 yamt (name) += (inc); \
183 1.2.2.2 yamt } while (/* CONSTCOND */ 0)
184 1.2.2.2 yamt
185 1.2.2.2 yamt void lockstat_event(uintptr_t, uintptr_t, u_int, u_int, uint64_t);
186 1.2.2.2 yamt
187 1.2.2.2 yamt extern volatile u_int lockstat_enabled;
188 1.2.2.2 yamt
189 1.2.2.2 yamt #else
190 1.2.2.2 yamt
191 1.2.2.3 yamt #define LOCKSTAT_FLAG(name) /* nothing */
192 1.2.2.3 yamt #define LOCKSTAT_ENTER(name) /* nothing */
193 1.2.2.3 yamt #define LOCKSTAT_EXIT(name) /* nothing */
194 1.2.2.3 yamt #define LOCKSTAT_EVENT(flag, lock, type, count, time) /* nothing */
195 1.2.2.3 yamt #define LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra) /* nothing */
196 1.2.2.3 yamt #define LOCKSTAT_TIMER(void) /* nothing */
197 1.2.2.3 yamt #define LOCKSTAT_COUNTER(void) /* nothing */
198 1.2.2.3 yamt #define LOCKSTAT_START_TIMER(flag, void) /* nothing */
199 1.2.2.3 yamt #define LOCKSTAT_STOP_TIMER(flag, void) /* nothing */
200 1.2.2.3 yamt #define LOCKSTAT_COUNT(name, int) /* nothing */
201 1.2.2.2 yamt
202 1.2.2.2 yamt #endif
203 1.2.2.2 yamt
204 1.2.2.2 yamt #endif /* _SYS_LOCKSTAT_H_ */
205