Debian Codegraph @

storage/innobase/include/sync0policy.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2012, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/sync0policy.h
29 Policies for mutexes.
30
31 Created 2012-08-21 Sunny Bains.
32 ***********************************************************************/
33
34 #ifndef sync0policy_h
35 #define sync0policy_h
36
37 #include <string>
38 #include <thread>
39
40 #include "srv0mon.h"
41 #include "sync0types.h"
42 #include "univ.i"
43 #include "ut0rnd.h"
44
45 #ifndef UNIV_LIBRARY
46 #ifdef UNIV_DEBUG
47
48 constexpr uint32_t MUTEX_MAGIC_N{0xb251b04bU};
49
50 template <typename Mutex>
51 class MutexDebug {
52 public:
53 /** For passing context to SyncDebug */
54 struct Context : public latch_t {
55 /** Constructor */
56 Context()
57 : m_mutex(), m_filename(), m_line(), m_thread_id(std::thread::id{}) {
58 /* No op */
59 }
60
61 /** Create the context for SyncDebug
62 @param[in] id ID of the latch to track */
63 Context(latch_id_t id) : latch_t(id) { /* No op */
64 }
65
66 /** Set to locked state
67 @param[in] mutex The mutex to acquire
68 @param[in] filename File name from where to acquire
69 @param[in] line Line number in filename */
70 void locked(const Mutex *mutex, const char *filename,
71 ulint line) UNIV_NOTHROW {
72 m_mutex = mutex;
73
74 m_thread_id = std::this_thread::get_id();
75
76 m_filename = filename;
77
78 m_line = line;
79 }
80
81 /** Reset to unlock state */
82 void release() UNIV_NOTHROW {
83 m_mutex = nullptr;
84
85 m_thread_id = std::thread::id{};
86
87 m_filename = nullptr;
88
89 m_line = ULINT_UNDEFINED;
90 }
91
92 /** Print information about the latch
93 @return the string representation */
94 std::string to_string() const override UNIV_NOTHROW {
95 std::ostringstream msg;
96
97 msg << m_mutex->policy().to_string();
98
99 if (m_thread_id != std::thread::id{}) {
100 msg << " addr: " << m_mutex << " acquired: " << locked_from().c_str();
101
102 } else {
103 msg << "Not locked";
104 }
105
106 return (msg.str());
107 }
108
109 /** @return the name of the file and line number in the file
110 from where the mutex was acquired "filename:line" */
111 virtual std::string locked_from() const override {
112 std::ostringstream msg;
113
114 msg << sync_basename(m_filename) << ":" << m_line;
115
116 return (std::string(msg.str()));
117 }
118
119 /** Mutex to check for lock order violation */
120 const Mutex *m_mutex;
121
122 /** Filename from where enter was called */
123 const char *m_filename;
124
125 /** Line number in filename */
126 ulint m_line;
127
128 /** Thread ID of the thread that own(ed) the mutex */
129 std::thread::id m_thread_id;
130 };
131
132 /** Constructor. */
133 MutexDebug() : m_magic_n(), m_context() UNIV_NOTHROW { /* No op */
134 }
135
136 /* Destructor */
137 virtual ~MutexDebug() = default;
138
139 /** Mutex is being destroyed. */
140 void destroy() UNIV_NOTHROW {
141 ut_ad(m_context.m_thread_id == std::thread::id{});
142 ut_ad(m_magic_n == MUTEX_MAGIC_N);
143
144 m_magic_n = 0;
145
146 m_context.m_thread_id = std::thread::id{};
147 }
148
149 /** Called when the mutex is "created". Note: Not from the constructor
150 but when the mutex is initialised.
151 @param[in] id Mutex ID */
152 void init(latch_id_t id) UNIV_NOTHROW;
153
154 /** Called when an attempt is made to lock the mutex
155 @param[in] mutex Mutex instance to be locked
156 @param[in] filename Filename from where it was called
157 @param[in] line Line number from where it was called */
158 void enter(const Mutex *mutex, const char *filename, ulint line) UNIV_NOTHROW;
159
160 /** Called when the mutex is locked
161 @param[in] mutex Mutex instance that was locked
162 @param[in] filename Filename from where it was called
163 @param[in] line Line number from where it was called */
164 void locked(const Mutex *mutex, const char *filename,
165 ulint line) UNIV_NOTHROW;
166
167 /** Called when the mutex is released
168 @param[in] mutex Mutex that was released */
169 void release(const Mutex *mutex) UNIV_NOTHROW;
170
171 /** @return true if thread owns the mutex */
172 bool is_owned() const UNIV_NOTHROW {
173 return m_context.m_thread_id == std::this_thread::get_id();
174 }
175
176 /** @return the name of the file from the mutex was acquired */
177 const char *get_enter_filename() const UNIV_NOTHROW {
178 return m_context.m_filename;
179 }
180
181 /** @return the name of the file from the mutex was acquired */
182 ulint get_enter_line() const UNIV_NOTHROW { return m_context.m_line; }
183
184 /** @return id of the thread that was trying to acquire the mutex */
185 std::thread::id get_thread_id() const UNIV_NOTHROW {
186 return m_context.m_thread_id;
187 }
188
189 /** Magic number to check for memory corruption. */
190 uint32_t m_magic_n;
191
192 /** Latch state of the mutex owner */
193 Context m_context;
194 };
195 #endif /* UNIV_DEBUG */
196
197 /* Do nothing */
198 template <typename Mutex>
199 struct NoPolicy {
200 /** Default constructor. */
201 NoPolicy() = default;
202
203 void init(const Mutex &, latch_id_t, const char *, uint32_t) UNIV_NOTHROW {}
204 void destroy() UNIV_NOTHROW {}
205 void enter(const Mutex &, const char *, ulint line) UNIV_NOTHROW {}
206 void add(uint32_t, uint32_t) UNIV_NOTHROW {}
207 void locked(const Mutex &, const char *, ulint) UNIV_NOTHROW {}
208 void release(const Mutex &) UNIV_NOTHROW {}
209 std::string to_string() const { return (""); }
210 latch_id_t get_id() const;
211 };
212
213 /** Collect the metrics per mutex instance, no aggregation. */
214 template <typename Mutex>
215 struct GenericPolicy
216 #ifdef UNIV_DEBUG
217 : public MutexDebug<Mutex>
218 #endif /* UNIV_DEBUG */
219 {
220 public:
221 typedef Mutex MutexType;
222
223 /** Constructor. */
224 GenericPolicy() UNIV_NOTHROW :
225 #ifdef UNIV_DEBUG
226 MutexDebug<MutexType>(),
227 #endif /* UNIV_DEBUG */
228 m_count(),
229 m_id() {
230 }
231
232 /** Destructor */
233 ~GenericPolicy() = default;
234
235 /** Called when the mutex is "created". Note: Not from the constructor
236 but when the mutex is initialised.
237 @param[in] mutex Mutex instance to track
238 @param[in] id Mutex ID
239 @param[in] filename File where mutex was created
240 @param[in] line Line in filename */
241 void init(const MutexType &mutex [[maybe_unused]], latch_id_t id,
242 const char *filename, uint32_t line) UNIV_NOTHROW {
243 m_id = id;
244
245 latch_meta_t &meta = sync_latch_get_meta(id);
246
247 ut_ad(meta.get_id() == id);
248
249 meta.get_counter()->single_register(&m_count);
250
251 sync_file_created_register(this, filename, line);
252
253 ut_d(MutexDebug<MutexType>::init(m_id));
254 }
255
256 /** Called when the mutex is destroyed. */
257 void destroy() UNIV_NOTHROW {
258 latch_meta_t &meta = sync_latch_get_meta(m_id);
259
260 meta.get_counter()->single_deregister(&m_count);
261
262 sync_file_created_deregister(this);
263
264 ut_d(MutexDebug<MutexType>::destroy());
265 }
266
267 /** Called after a successful mutex acquire.
268 @param[in] n_spins Number of times the thread did
269 spins while trying to acquire the mutex
270 @param[in] n_waits Number of times the thread waited
271 in some type of OS queue */
272 void add(uint32_t n_spins, uint32_t n_waits) UNIV_NOTHROW {
273 /* Currently global on/off. Keeps things simple and fast */
274
275 if (!m_count.m_enabled) {
276 return;
277 }
278
279 m_count.m_spins += n_spins;
280 m_count.m_waits += n_waits;
281
282 ++m_count.m_calls;
283 }
284
285 /** Called when an attempt is made to lock the mutex
286 @param[in] mutex Mutex instance to be locked
287 @param[in] filename Filename from where it was called
288 @param[in] line Line number from where it was called */
289 void enter(const MutexType &IF_DEBUG(mutex), const char *IF_DEBUG(filename),
290 ulint IF_DEBUG(line)) UNIV_NOTHROW {
291 ut_d(MutexDebug<MutexType>::enter(&mutex, filename, line));
292 }
293
294 /** Called when the mutex is locked
295 @param[in] mutex Mutex instance that is locked
296 @param[in] filename Filename from where it was called
297 @param[in] line Line number from where it was called */
298 void locked(const MutexType &IF_DEBUG(mutex), const char *IF_DEBUG(filename),
299 ulint IF_DEBUG(line)) UNIV_NOTHROW {
300 ut_d(MutexDebug<MutexType>::locked(&mutex, filename, line));
301 }
302
303 /** Called when the mutex is released
304 @param[in] mutex Mutex instance that is released */
305 void release(const MutexType &IF_DEBUG(mutex)) UNIV_NOTHROW {
306 ut_d(MutexDebug<MutexType>::release(&mutex));
307 }
308
309 /** Print the information about the latch
310 @return the string representation */
311 std::string print() const UNIV_NOTHROW;
312
313 /** @return the latch ID */
314 latch_id_t get_id() const UNIV_NOTHROW { return (m_id); }
315
316 /** @return the string representation */
317 std::string to_string() const;
318
319 private:
320 typedef latch_meta_t::CounterType Counter;
321
322 /** The user visible counters, registered with the meta-data. */
323 Counter::Count m_count;
324
325 /** Latch meta data ID */
326 latch_id_t m_id;
327 };
328
329 /** Track aggregate metrics policy, used by the page mutex. There are just
330 too many of them to count individually. */
331 template <typename Mutex>
332 class BlockMutexPolicy
333 #ifdef UNIV_DEBUG
334 : public MutexDebug<Mutex>
335 #endif /* UNIV_DEBUG */
336 {
337 public:
338 typedef Mutex MutexType;
339 typedef typename latch_meta_t::CounterType::Count Count;
340
341 /** Default constructor. */
342 BlockMutexPolicy()
343 :
344 #ifdef UNIV_DEBUG
345 MutexDebug<MutexType>(),
346 #endif /* UNIV_DEBUG */
347 m_count(),
348 m_id() {
349 /* Do nothing */
350 }
351
352 /** Destructor */
353 ~BlockMutexPolicy() = default;
354
355 /** Called when the mutex is "created". Note: Not from the constructor
356 but when the mutex is initialised.
357 @param[in] mutex Mutex instance to track
358 @param[in] id Mutex ID
359 @param[in] filename File where mutex was created
360 @param[in] line Line in filename */
361 void init(const MutexType &mutex [[maybe_unused]], latch_id_t id,
362 const char *filename [[maybe_unused]],
363 uint32_t line [[maybe_unused]]) UNIV_NOTHROW {
364 /* It can be LATCH_ID_BUF_BLOCK_MUTEX or
365 LATCH_ID_BUF_POOL_ZIP. Unfortunately, they
366 are mapped to the same mutex type in the
367 buffer pool code. */
368
369 m_id = id;
370
371 latch_meta_t &meta = sync_latch_get_meta(m_id);
372
373 ut_ad(meta.get_id() == id);
374
375 m_count = meta.get_counter()->sum_register();
376
377 ut_d(MutexDebug<MutexType>::init(m_id));
378 }
379
380 /** Called when the mutex is destroyed. */
381 void destroy() UNIV_NOTHROW {
382 latch_meta_t &meta = sync_latch_get_meta(m_id);
383
384 ut_ad(meta.get_id() == m_id);
385
386 meta.get_counter()->sum_deregister(m_count);
387
388 m_count = nullptr;
389
390 ut_d(MutexDebug<MutexType>::destroy());
391 }
392
393 /** Called after a successful mutex acquire.
394 @param[in] n_spins Number of times the thread did
395 spins while trying to acquire the mutex
396 @param[in] n_waits Number of times the thread waited
397 in some type of OS queue */
398 void add(uint32_t n_spins, uint32_t n_waits) UNIV_NOTHROW {
399 if (!m_count->m_enabled) {
400 return;
401 }
402
403 m_count->m_spins += n_spins;
404 m_count->m_waits += n_waits;
405
406 ++m_count->m_calls;
407 }
408
409 /** Called when the mutex is locked
410 @param[in] mutex Mutex instance that is locked
411 @param[in] filename Filename from where it was called
412 @param[in] line Line number from where it was called */
413 void locked(const MutexType &IF_DEBUG(mutex), const char *IF_DEBUG(filename),
414 ulint IF_DEBUG(line)) UNIV_NOTHROW {
415 ut_d(MutexDebug<MutexType>::locked(&mutex, filename, line));
416 }
417
418 /** Called when the mutex is released
419 @param[in] mutex Mutex instance that is released */
420 void release(const MutexType &IF_DEBUG(mutex)) UNIV_NOTHROW {
421 ut_d(MutexDebug<MutexType>::release(&mutex));
422 }
423
424 /** Called when an attempt is made to lock the mutex
425 @param[in] mutex Mutex instance to be locked
426 @param[in] filename Filename from where it was called
427 @param[in] line Line number from where it was called */
428 void enter(const MutexType &IF_DEBUG(mutex), const char *IF_DEBUG(filename),
429 ulint IF_DEBUG(line)) UNIV_NOTHROW {
430 ut_d(MutexDebug<MutexType>::enter(&mutex, filename, line));
431 }
432
433 /** Print the information about the latch
434 @return the string representation */
435 std::string print() const UNIV_NOTHROW;
436
437 /** @return the latch ID */
438 latch_id_t get_id() const { return (m_id); }
439
440 /** @return the string representation */
441 std::string to_string() const;
442
443 private:
444 typedef latch_meta_t::CounterType Counter;
445
446 /** The user visible counters, registered with the meta-data. */
447 Counter::Count *m_count;
448
449 /** Latch meta data ID */
450 latch_id_t m_id;
451 };
452
453 #include "sync0policy.ic"
454
455 #endif /* !UNIV_LIBRARY */
456 #endif /* sync0policy_h */