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