MySQL 8.4.3
Source Code Documentation
service_thd_wait.h
Go to the documentation of this file.
1/* Copyright (c) 2010, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MYSQL_SERVICE_THD_WAIT_INCLUDED
25#define MYSQL_SERVICE_THD_WAIT_INCLUDED
26
27#include <assert.h>
28
29/**
30 @file include/mysql/service_thd_wait.h
31 This service provides functions for plugins and storage engines to report
32 when they are going to sleep/stall.
33
34 SYNOPSIS
35 thd_wait_begin() - call just before a wait begins
36 thd Thread object
37 Use NULL if the thd is NOT known.
38 wait_type Type of wait
39 1 -- short wait (e.g. for mutex)
40 2 -- medium wait (e.g. for disk io)
41 3 -- large wait (e.g. for locked row/table)
42 NOTES
43 This is used by the threadpool to have better knowledge of which
44 threads that currently are actively running on CPUs. When a thread
45 reports that it's going to sleep/stall, the threadpool scheduler is
46 free to start another thread in the pool most likely. The expected wait
47 time is simply an indication of how long the wait is expected to
48 become, the real wait time could be very different.
49
50 thd_wait_end() called immediately after the wait is complete
51
52 thd_wait_end() MUST be called if thd_wait_begin() was called.
53
54 Using thd_wait_...() service is optional but recommended. Using it will
55 improve performance as the thread pool will be more active at managing the
56 thread workload.
57*/
58#ifndef __cplusplus
59#error "Not compiling as C++"
60#endif
61
62class THD;
63
64/*
65 One should only report wait events that could potentially block for a
66 long time. A mutex wait is too short of an event to report. The reason
67 is that an event which is reported leads to a new thread starts
68 executing a query and this has a negative impact of usage of CPU caches
69 and thus the expected gain of starting a new thread must be higher than
70 the expected cost of lost performance due to starting a new thread.
71
72 Good examples of events that should be reported are waiting for row locks
73 that could easily be for many milliseconds or even seconds and the same
74 holds true for global read locks, table locks and other meta data locks.
75 Another event of interest is going to sleep for an extended time.
76
77 Note that user-level locks no longer use THD_WAIT_USER_LOCK wait type.
78 Since their implementation relies on metadata locks manager it uses
79 THD_WAIT_META_DATA_LOCK instead.
80*/
81
82enum THD_wait_type : int {
95 THD_WAIT_LAST = 12
96};
97
98inline const char *THD_wait_type_str(THD_wait_type twt) {
99 switch (twt) {
100 case THD_WAIT_NONE:
101 return "Not waiting";
102
103 case THD_WAIT_SLEEP:
104 return "Waiting for sleep";
105
106 case THD_WAIT_DISKIO:
107 return "Waiting for Disk IO";
108
110 return "Waiting for row lock";
111
113 return "Waiting for global lock";
114
116 return "Waiting for metadata lock";
117
119 return "Waiting for table lock";
120
122 return "Waiting for user lock";
123
124 case THD_WAIT_BINLOG:
125 return "Waiting for binlog";
126
128 return "Waiting for group commit";
129
130 case THD_WAIT_SYNC:
131 return "Waiting for fsync";
132
134 return "Waiting for transaction delay";
135
136 case THD_WAIT_LAST:
137 return "<Unused LAST marker value>";
138 } // switch
139 assert(false);
140 return "<Invalid THD_WAIT value>";
141}
142
144 void (*thd_wait_begin_func)(THD *, int);
146};
147
148#ifdef MYSQL_DYNAMIC_PLUGIN
149// We do not support these callbacks for dynamic plugins. To enforce this we
150// provide a conflicting declaration for them, so it becomes impossible to use
151// them.
152void thd_wait_begin();
153void thd_wait_end();
154#else /* MYSQL_DYNAMIC_PLUGIN */
155// e.g. ha_innodb (static plugin)
157void thd_wait_end(THD *thd);
158#endif /* MYSQL_DYNAMIC_PLUGIN */
159#endif /* MYSQL_SERVICE_THD_WAIT_INCLUDED */
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
wait_type
Definition: socket_constants.h:86
void thd_wait_end(THD *thd)
THD_wait_type
Definition: service_thd_wait.h:82
@ THD_WAIT_SYNC
Definition: service_thd_wait.h:93
@ THD_WAIT_DISKIO
Definition: service_thd_wait.h:85
@ THD_WAIT_GLOBAL_LOCK
Definition: service_thd_wait.h:87
@ THD_WAIT_META_DATA_LOCK
Definition: service_thd_wait.h:88
@ THD_WAIT_NONE
Definition: service_thd_wait.h:83
@ THD_WAIT_TABLE_LOCK
Definition: service_thd_wait.h:89
@ THD_WAIT_BINLOG
Definition: service_thd_wait.h:91
@ THD_WAIT_SLEEP
Definition: service_thd_wait.h:84
@ THD_WAIT_LAST
Definition: service_thd_wait.h:95
@ THD_WAIT_TRX_DELAY
Definition: service_thd_wait.h:94
@ THD_WAIT_ROW_LOCK
Definition: service_thd_wait.h:86
@ THD_WAIT_GROUP_COMMIT
Definition: service_thd_wait.h:92
@ THD_WAIT_USER_LOCK
Definition: service_thd_wait.h:90
const char * THD_wait_type_str(THD_wait_type twt)
Definition: service_thd_wait.h:98
void thd_wait_begin(THD *thd, int wait_type)
Definition: service_thd_wait.h:143
void(* thd_wait_end_func)(THD *)
Definition: service_thd_wait.h:145
void(* thd_wait_begin_func)(THD *, int)
Definition: service_thd_wait.h:144