MySQL 9.0.0
Source Code Documentation
mysql_thread.h
Go to the documentation of this file.
1/* Copyright (c) 2008, 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_THREAD_H
25#define MYSQL_THREAD_H
26
27/**
28 @file include/mysql/psi/mysql_thread.h
29 Instrumentation helpers for mysys threads.
30 This header file provides the necessary declarations
31 to use the mysys thread API with the performance schema instrumentation.
32 In some compilers (SunStudio), 'static inline' functions, when declared
33 but not used, are not optimized away (because they are unused) by default,
34 so that including a static inline function from a header file does
35 create unwanted dependencies, causing unresolved symbols at link time.
36 Other compilers, like gcc, optimize these dependencies by default.
37
38 Since the instrumented APIs declared here are wrapper on top
39 of my_thread / safemutex / etc APIs,
40 including mysql/psi/mysql_thread.h assumes that
41 the dependency on my_thread and safemutex already exists.
42*/
43
44/* HAVE_PSI_*_INTERFACE */
45#include "my_psi_config.h" // IWYU pragma: keep
46
47#include "my_thread.h"
48#include "my_thread_local.h"
50
51#if defined(MYSQL_SERVER) || defined(PFS_DIRECT_CALL)
52/* PSI_THREAD_CALL() as direct call. */
53#include "pfs_thread_provider.h" // IWYU pragma: keep
54#endif
55
56#ifndef PSI_THREAD_CALL
57#define PSI_THREAD_CALL(M) psi_thread_service->M
58#endif
59
60/**
61 @defgroup psi_api_thread Thread Instrumentation (API)
62 @ingroup psi_api
63 @{
64*/
65
66/**
67 @def mysql_thread_register(P1, P2, P3)
68 Thread registration.
69*/
70#define mysql_thread_register(P1, P2, P3) \
71 inline_mysql_thread_register(P1, P2, P3)
72
73/**
74 @def mysql_thread_create(K, P1, P2, P3, P4)
75 Instrumented my_thread_create.
76 This function creates both the thread instrumentation and a thread.
77 @c mysql_thread_create is a replacement for @c my_thread_create.
78 The parameter P4 (or, if it is NULL, P1) will be used as the
79 instrumented thread "identity".
80 Providing a P1 / P4 parameter with a different value for each call
81 will on average improve performances, since this thread identity value
82 is used internally to randomize access to data and prevent contention.
83 This is optional, and the improvement is not guaranteed, only statistical.
84 @param K The PSI_thread_key for this instrumented thread
85 @param P1 my_thread_create parameter 1
86 @param P2 my_thread_create parameter 2
87 @param P3 my_thread_create parameter 3
88 @param P4 my_thread_create parameter 4
89*/
90#define mysql_thread_create(K, P1, P2, P3, P4) \
91 inline_mysql_thread_create(K, 0, P1, P2, P3, P4)
92
93/**
94 @def mysql_thread_create_seq(K, S, P1, P2, P3, P4)
95 Instrumented my_thread_create.
96 @see mysql_thread_create.
97 This forms takes an additional sequence number parameter,
98 used to name threads "name-N" in the operating system.
99
100 @param K The PSI_thread_key for this instrumented thread
101 @param S The sequence number for this instrumented thread
102 @param P1 my_thread_create parameter 1
103 @param P2 my_thread_create parameter 2
104 @param P3 my_thread_create parameter 3
105 @param P4 my_thread_create parameter 4
106*/
107#define mysql_thread_create_seq(K, S, P1, P2, P3, P4) \
108 inline_mysql_thread_create(K, S, P1, P2, P3, P4)
109
110/**
111 @def mysql_thread_set_psi_id(I)
112 Set the thread identifier for the instrumentation.
113 @param I The thread identifier
114*/
115#define mysql_thread_set_psi_id(I) inline_mysql_thread_set_psi_id(I)
116
117/**
118 @def mysql_thread_set_psi_THD(T)
119 Set the thread sql session for the instrumentation.
120 @param T The thread sql session
121*/
122#define mysql_thread_set_psi_THD(T) inline_mysql_thread_set_psi_THD(T)
123
124static inline void inline_mysql_thread_register(const char *category
125 [[maybe_unused]],
126 PSI_thread_info *info
127 [[maybe_unused]],
128 int count [[maybe_unused]]) {
129#ifdef HAVE_PSI_THREAD_INTERFACE
130 PSI_THREAD_CALL(register_thread)(category, info, count);
131#endif
132}
133
135 PSI_thread_key key [[maybe_unused]],
136 unsigned int sequence_number [[maybe_unused]], my_thread_handle *thread,
137 const my_thread_attr_t *attr, my_start_routine start_routine, void *arg) {
138 int result;
139#ifdef HAVE_PSI_THREAD_INTERFACE
140 result = PSI_THREAD_CALL(spawn_thread)(key, sequence_number, thread, attr,
141 start_routine, arg);
142#else
143 result = my_thread_create(thread, attr, start_routine, arg);
144#endif
145 return result;
146}
147
149 [[maybe_unused]]) {
150#ifdef HAVE_PSI_THREAD_INTERFACE
151 struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
152 PSI_THREAD_CALL(set_thread_id)(psi, id);
153#endif
154}
155
156#ifdef __cplusplus
157class THD;
158static inline void inline_mysql_thread_set_psi_THD(THD *thd [[maybe_unused]]) {
159#ifdef HAVE_PSI_THREAD_INTERFACE
160 struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
161 PSI_THREAD_CALL(set_thread_THD)(psi, thd);
162#endif
163}
164#endif /* __cplusplus */
165
166/**
167 @def mysql_thread_set_peer_port()
168 Set the remote (peer) port for the thread instrumentation.
169 @param port peer port number
170*/
171static inline void mysql_thread_set_peer_port(uint port [[maybe_unused]]) {
172#ifdef HAVE_PSI_THREAD_INTERFACE
173 struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
174 PSI_THREAD_CALL(set_thread_peer_port)(psi, port);
175#endif
176}
177
178/**
179 @def mysql_thread_set_secondary_engine()
180 Set the EXECUTION_ENGINE attribute for the thread instrumentation.
181 @param secondary True for SECONDARY, false for PRIMARY.
182*/
183static inline void mysql_thread_set_secondary_engine(bool secondary
184 [[maybe_unused]]) {
185#ifdef HAVE_PSI_THREAD_INTERFACE
186 PSI_THREAD_CALL(set_thread_secondary_engine)(secondary);
187#endif
188}
189
190/**
191 Set the INFO attribute in the thread instrumentation.
192 @param str query string
193 @param len query length
194*/
195static inline void mysql_thread_set_info(const char *str [[maybe_unused]],
196 int len [[maybe_unused]]) {
197#ifdef HAVE_PSI_THREAD_INTERFACE
198 PSI_THREAD_CALL(set_thread_info)(str, len);
199#endif
200}
201
202/** @} (end of group psi_api_thread) */
203
204#endif
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
#define PSI_THREAD_CALL(M)
Definition: psi_thread.h:36
struct PSI_thread PSI_thread
Definition: psi_thread_bits.h:82
unsigned int PSI_thread_key
Instrumented thread key.
Definition: psi_thread_bits.h:50
static int inline_mysql_thread_create(PSI_thread_key key, unsigned int sequence_number, my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine start_routine, void *arg)
Definition: mysql_thread.h:134
static void mysql_thread_set_info(const char *str, int len)
Set the INFO attribute in the thread instrumentation.
Definition: mysql_thread.h:195
static void inline_mysql_thread_set_psi_THD(THD *thd)
Definition: mysql_thread.h:158
static void inline_mysql_thread_set_psi_id(my_thread_id id)
Definition: mysql_thread.h:148
static void mysql_thread_set_peer_port(uint port)
Definition: mysql_thread.h:171
static void inline_mysql_thread_register(const char *category, PSI_thread_info *info, int count)
Definition: mysql_thread.h:124
static void mysql_thread_set_secondary_engine(bool secondary)
Definition: mysql_thread.h:183
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:78
void *(* my_start_routine)(void *)
Definition: my_thread.h:72
pthread_attr_t my_thread_attr_t
Definition: my_thread_bits.h:49
uint32 my_thread_id
Definition: my_thread_local.h:34
static int count
Definition: myisam_ftdump.cc:45
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
static const char * category
Definition: sha2_password.cc:170
Performance schema instrumentation (declarations).
struct result result
Definition: result.h:34
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Thread instrument information.
Definition: psi_thread_bits.h:117
Definition: my_thread_bits.h:58
Definition: result.h:30
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510