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