MySQL  8.0.27
Source Code Documentation
mysql_thread.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, 2021, 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 also distributed 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 included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef MYSQL_THREAD_H
24 #define MYSQL_THREAD_H
25 
26 /**
27  @file include/mysql/psi/mysql_thread.h
28  Instrumentation helpers for mysys threads.
29  This header file provides the necessary declarations
30  to use the mysys thread API with the performance schema instrumentation.
31  In some compilers (SunStudio), 'static inline' functions, when declared
32  but not used, are not optimized away (because they are unused) by default,
33  so that including a static inline function from a header file does
34  create unwanted dependencies, causing unresolved symbols at link time.
35  Other compilers, like gcc, optimize these dependencies by default.
36 
37  Since the instrumented APIs declared here are wrapper on top
38  of my_thread / safemutex / etc APIs,
39  including mysql/psi/mysql_thread.h assumes that
40  the dependency on my_thread and safemutex already exists.
41 */
42 
43 /* HAVE_PSI_*_INTERFACE */
44 #include "my_psi_config.h" // IWYU pragma: keep
45 
46 #include "my_thread.h"
47 #include "my_thread_local.h"
48 #include "mysql/psi/psi_thread.h"
49 
50 #if defined(MYSQL_SERVER) || defined(PFS_DIRECT_CALL)
51 /* PSI_THREAD_CALL() as direct call. */
52 #include "pfs_thread_provider.h" // IWYU pragma: keep
53 #endif
54 
55 #ifndef PSI_THREAD_CALL
56 #define PSI_THREAD_CALL(M) psi_thread_service->M
57 #endif
58 
59 /**
60  @defgroup psi_api_thread Thread Instrumentation (API)
61  @ingroup psi_api
62  @{
63 */
64 
65 /**
66  @def mysql_thread_register(P1, P2, P3)
67  Thread registration.
68 */
69 #define mysql_thread_register(P1, P2, P3) \
70  inline_mysql_thread_register(P1, P2, P3)
71 
72 /**
73  @def mysql_thread_create(K, P1, P2, P3, P4)
74  Instrumented my_thread_create.
75  This function creates both the thread instrumentation and a thread.
76  @c mysql_thread_create is a replacement for @c my_thread_create.
77  The parameter P4 (or, if it is NULL, P1) will be used as the
78  instrumented thread "identity".
79  Providing a P1 / P4 parameter with a different value for each call
80  will on average improve performances, since this thread identity value
81  is used internally to randomize access to data and prevent contention.
82  This is optional, and the improvement is not guaranteed, only statistical.
83  @param K The PSI_thread_key for this instrumented thread
84  @param P1 my_thread_create parameter 1
85  @param P2 my_thread_create parameter 2
86  @param P3 my_thread_create parameter 3
87  @param P4 my_thread_create parameter 4
88 */
89 #define mysql_thread_create(K, P1, P2, P3, P4) \
90  inline_mysql_thread_create(K, 0, P1, P2, P3, P4)
91 
92 /**
93  @def mysql_thread_create_seq(K, S, P1, P2, P3, P4)
94  Instrumented my_thread_create.
95  @see mysql_thread_create.
96  This forms takes an additional sequence number parameter,
97  used to name threads "name-N" in the operating system.
98 
99  @param K The PSI_thread_key for this instrumented thread
100  @param S The sequence number for this instrumented thread
101  @param P1 my_thread_create parameter 1
102  @param P2 my_thread_create parameter 2
103  @param P3 my_thread_create parameter 3
104  @param P4 my_thread_create parameter 4
105 */
106 #define mysql_thread_create_seq(K, S, P1, P2, P3, P4) \
107  inline_mysql_thread_create(K, S, P1, P2, P3, P4)
108 
109 /**
110  @def mysql_thread_set_psi_id(I)
111  Set the thread identifier for the instrumentation.
112  @param I The thread identifier
113 */
114 #define mysql_thread_set_psi_id(I) inline_mysql_thread_set_psi_id(I)
115 
116 /**
117  @def mysql_thread_set_psi_THD(T)
118  Set the thread sql session for the instrumentation.
119  @param T The thread sql session
120 */
121 #define mysql_thread_set_psi_THD(T) inline_mysql_thread_set_psi_THD(T)
122 
123 static inline void inline_mysql_thread_register(const char *category
124  [[maybe_unused]],
126  [[maybe_unused]],
127  int count [[maybe_unused]]) {
128 #ifdef HAVE_PSI_THREAD_INTERFACE
129  PSI_THREAD_CALL(register_thread)(category, info, count);
130 #endif
131 }
132 
133 static inline int inline_mysql_thread_create(
134  PSI_thread_key key [[maybe_unused]],
135  unsigned int sequence_number [[maybe_unused]], my_thread_handle *thread,
136  const my_thread_attr_t *attr, my_start_routine start_routine, void *arg) {
137  int result;
138 #ifdef HAVE_PSI_THREAD_INTERFACE
139  result = PSI_THREAD_CALL(spawn_thread)(key, sequence_number, thread, attr,
140  start_routine, arg);
141 #else
142  result = my_thread_create(thread, attr, start_routine, arg);
143 #endif
144  return result;
145 }
146 
148  [[maybe_unused]]) {
149 #ifdef HAVE_PSI_THREAD_INTERFACE
150  struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
151  PSI_THREAD_CALL(set_thread_id)(psi, id);
152 #endif
153 }
154 
155 #ifdef __cplusplus
156 class THD;
157 static inline void inline_mysql_thread_set_psi_THD(THD *thd [[maybe_unused]]) {
158 #ifdef HAVE_PSI_THREAD_INTERFACE
159  struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
160  PSI_THREAD_CALL(set_thread_THD)(psi, thd);
161 #endif
162 }
163 #endif /* __cplusplus */
164 
165 /**
166  @def mysql_thread_set_peer_port()
167  Set the remote (peer) port for the thread instrumentation.
168  @param port peer port number
169 */
170 static inline void mysql_thread_set_peer_port(uint port [[maybe_unused]]) {
171 #ifdef HAVE_PSI_THREAD_INTERFACE
172  struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
173  PSI_THREAD_CALL(set_thread_peer_port)(psi, port);
174 #endif
175 }
176 
177 /** @} (end of group psi_api_thread) */
178 
179 #endif
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
#define PSI_THREAD_CALL(M)
Definition: psi_thread.h:35
struct PSI_thread PSI_thread
Definition: psi_thread_bits.h:81
unsigned int PSI_thread_key
Instrumented thread key.
Definition: psi_thread_bits.h:49
static void inline_mysql_thread_register(const char *category[[maybe_unused]], PSI_thread_info *info[[maybe_unused]], int count[[maybe_unused]])
Definition: mysql_thread.h:123
static void inline_mysql_thread_set_psi_id(my_thread_id id[[maybe_unused]])
Definition: mysql_thread.h:147
static void mysql_thread_set_peer_port(uint port[[maybe_unused]])
Definition: mysql_thread.h:170
static void inline_mysql_thread_set_psi_THD(THD *thd[[maybe_unused]])
Definition: mysql_thread.h:157
static int inline_mysql_thread_create(PSI_thread_key key[[maybe_unused]], unsigned int sequence_number[[maybe_unused]], my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine start_routine, void *arg)
Definition: mysql_thread.h:133
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Defines to make different thread packages compatible.
int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine func, void *arg)
Definition: my_thread.cc:77
void *(* my_start_routine)(void *)
Definition: my_thread.h:71
pthread_attr_t my_thread_attr_t
Definition: my_thread_bits.h:48
uint32 my_thread_id
Definition: my_thread_local.h:33
static int count
Definition: myisam_ftdump.cc:42
Log info(cout, "NOTE")
static const char * category
Definition: sha2_password.cc:168
Performance schema instrumentation (declarations).
struct result result
Definition: result.h:33
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:59
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
Thread instrument information.
Definition: psi_thread_bits.h:116
Definition: my_thread_bits.h:51
Definition: result.h:29
unsigned int uint
Definition: uca-dump.cc:29
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:506