MySQL 8.0.31
Source Code Documentation
resource_group.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2022, 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#ifndef RESOURCEGROUPS_RESOURCE_GROUP_H_
23#define RESOURCEGROUPS_RESOURCE_GROUP_H_
24
26
27#include <functional>
28#include <memory>
29#include <mutex>
30#include <unordered_map>
31
32#include "sql/log.h" // sql_print_warning
34
35namespace resourcegroups {
36
37class Resource_group_switch_handler;
38
39/**
40 Class that represents an abstraction of the Resource Group.
41 It has generic attributes of Resource group name, type,
42 active or inactive, a pointer to a Resource control object.
43*/
44
46 public:
47 /**
48 Construct a Resource_group object.
49
50 @param name name of the Resource_group.
51 @param type type of the Resource_group.
52 @param enabled Check if Resource_group is enabled or not.
53 */
54
55 Resource_group(const std::string &name, const Type type, bool enabled)
57
58 const std::string &name() const { return m_name; }
59
60 Type type() const { return m_type; }
61
62 bool enabled() const { return m_enabled; }
63
65
67
69
72 }
73
74 /**
75 Check if resource group is associated with threads.
76
77 @return true if some threads are mapped with this resource group
78 else false.
79 */
80
82 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
83 return !m_pfs_thread_id_map.empty();
84 }
85
86 /**
87 Is pfs thread id already exists in the m_pfs_thread_id_map map.
88
89 @param pfs_thread_id PFS thread id.
90
91 @return true if thread id exists in the set else false.
92 */
93
94 bool is_pfs_thread_id_exists(const ulonglong pfs_thread_id) {
95 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
96 return m_pfs_thread_id_map.find(pfs_thread_id) != m_pfs_thread_id_map.end();
97 }
98
99 /**
100 Add pfs_thread_id to the thread id map associated with this resource group.
101
102 @param pfs_thread_id PFS thread id.
103 @param rg_switch_handler Resource_group_switch_handler instance to
104 apply new resource group to a thread on
105 resource group switch from current to some
106 other resource group.
107 */
108
109 void add_pfs_thread_id(const ulonglong pfs_thread_id,
110 Resource_group_switch_handler *rg_switch_handler) {
111 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
112 (void)m_pfs_thread_id_map.insert({pfs_thread_id, rg_switch_handler});
113 }
114
115 /**
116 Add pfs_thread_id if pfs_thread_id does not exists in the map of threads
117 associated with this thread group. Otherwise, update resource group switch
118 handler for the thread_id.
119
120 @param pfs_thread_id PFS thread id.
121 @param rg_switch_handler Resource_group_switch_handler instance to
122 apply new resource group to a thread on
123 resource group switch from current to some
124 other resource group.
125 */
126
128 const ulonglong pfs_thread_id,
129 Resource_group_switch_handler *rg_switch_handler) {
130 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
131 (void)m_pfs_thread_id_map.insert_or_assign(pfs_thread_id,
132 rg_switch_handler);
133 }
134
135 /**
136 Add pfs_thread_id of a thread which is temporarily switched this resource
137 group.
138
139 @param pfs_thread_id PFS thread id.
140 @param rg_switch_handler Resource_group_switch_handler instance to
141 apply new resource group to a thread on
142 resource group switch from current to some
143 other resource group.
144 */
145
147 const ulonglong pfs_thread_id,
148 Resource_group_switch_handler *rg_switch_handler) {
149 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
150 (void)m_temporarily_switched_pfs_thread_id_map.insert(
151 {pfs_thread_id, rg_switch_handler});
152 }
153
154 /**
155 Get Resource_group_switch_handler instance for a thread identified by
156 pfs_thread_id from the thread id maps.
157
158 @param pfs_thread_id PFS thread id.
159 */
160
162 ulonglong pfs_thread_id) {
163 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
164 auto res = m_pfs_thread_id_map.find(pfs_thread_id);
165 if (res == m_pfs_thread_id_map.end()) {
166 res = m_temporarily_switched_pfs_thread_id_map.find(pfs_thread_id);
167 if (res == m_temporarily_switched_pfs_thread_id_map.end()) return nullptr;
168 }
169 return res->second;
170 }
171
172 /**
173 Remove the PFS thread id from the thread id maps.
174
175 @param pfs_thread_id Remove pfs thread id.
176 @param skip_mutex_lock Skip locking m_thread_id_maps_mutex.
177 */
178
179 void remove_pfs_thread_id(const ulonglong pfs_thread_id,
180 bool skip_mutex_lock = false) {
181 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex, std::defer_lock);
182 if (!skip_mutex_lock) lock.lock();
183 (void)m_pfs_thread_id_map.erase(pfs_thread_id);
184 (void)m_temporarily_switched_pfs_thread_id_map.erase(pfs_thread_id);
185 }
186
187 /**
188 Clear the thread id maps associated with this resource group.
189 */
190
191 void clear() {
192 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
193 (void)m_pfs_thread_id_map.clear();
194 (void)m_temporarily_switched_pfs_thread_id_map.clear();
195 }
196
197 /**
198 Apply a control function on threads *associated* with this resource group.
199
200 @param control_func pointer to Control function.
201 */
202
204 std::function<void(ulonglong, Resource_group_switch_handler *)>
205 control_func) {
206 std::unique_lock<std::mutex> lock(m_thread_id_maps_mutex);
207 for (auto &[pfs_thread_id, resource_group_switch_handler] :
208 m_pfs_thread_id_map)
209 control_func(pfs_thread_id, resource_group_switch_handler);
210 }
211
212 ~Resource_group() = default;
213
214 private:
215 /**
216 Name of the resource group.
217 */
218 std::string m_name;
219
220 /**
221 Type whether it is user or system resource group.
222 */
224
225 /**
226 bool flag whether resource is enabled or disabled.
227 */
229
230 /**
231 Thread resource controller object.
232 */
234
235 /*
236 Contains threads associated with this resource group and resource group
237 switch handler instance pair.
238
239 Resource group switch handler instance is used to apply resource group to
240 a thread when resource group fo a thread is switched from this to some other
241 resource group.
242 */
243 std::map<ulonglong, Resource_group_switch_handler *> m_pfs_thread_id_map;
244
245 /*
246 Contains threads temporarily switched to this resource group and resource
247 group switch handler instance pair.
248 */
249 std::map<ulonglong, Resource_group_switch_handler *>
251
252 /**
253 Mutex protecting the pfs thread id maps.
254 */
256
257 /**
258 Disable copy construction and assignment.
259 */
261 void operator=(const Resource_group &) = delete;
262};
263
264/**
265 Class used to apply new resource group to a thread on resource group switch.
266*/
268 public:
270 /**
271 Apply thread resource control to thread identified by thread os id,
272
273 @param new_rg New resource group to apply to the
274 thread.
275 @param thread_os_id OS thread id.
276 @param[out] is_rg_applied_to_thread Set to "true" if resource group is
277 applied to a thread.
278
279 @retval false Success.
280 @retval true Failure.
281 */
282 virtual bool apply(Resource_group *new_rg, my_thread_os_id_t thread_os_id,
283 bool *is_rg_applied_to_thread);
284};
285
286/**
287 Default resource group switch handler instance.
288*/
289extern Resource_group_switch_handler default_rg_switch_handler;
290} // namespace resourcegroups
291#endif // RESOURCEGROUPS_RESOURCE_GROUP_H_
Class used to apply new resource group to a thread on resource group switch.
Definition: resource_group.h:267
virtual ~Resource_group_switch_handler()
Definition: resource_group.h:269
Class that represents an abstraction of the Resource Group.
Definition: resource_group.h:45
void clear()
Clear the thread id maps associated with this resource group.
Definition: resource_group.h:191
Resource_group(const std::string &name, const Type type, bool enabled)
Construct a Resource_group object.
Definition: resource_group.h:55
Resource_group_switch_handler * resource_group_switch_handler(ulonglong pfs_thread_id)
Get Resource_group_switch_handler instance for a thread identified by pfs_thread_id from the thread i...
Definition: resource_group.h:161
std::map< ulonglong, Resource_group_switch_handler * > m_temporarily_switched_pfs_thread_id_map
Definition: resource_group.h:250
void add_temporarily_switched_pfs_thread_id(const ulonglong pfs_thread_id, Resource_group_switch_handler *rg_switch_handler)
Add pfs_thread_id of a thread which is temporarily switched this resource group.
Definition: resource_group.h:146
const std::string & name() const
Definition: resource_group.h:58
std::mutex m_thread_id_maps_mutex
Mutex protecting the pfs thread id maps.
Definition: resource_group.h:255
Thread_resource_control * controller()
Definition: resource_group.h:68
bool m_enabled
bool flag whether resource is enabled or disabled.
Definition: resource_group.h:228
Resource_group(const Resource_group &)=delete
Disable copy construction and assignment.
Type type() const
Definition: resource_group.h:60
Type m_type
Type whether it is user or system resource group.
Definition: resource_group.h:223
std::string m_name
Name of the resource group.
Definition: resource_group.h:218
void add_or_update_pfs_thread_id(const ulonglong pfs_thread_id, Resource_group_switch_handler *rg_switch_handler)
Add pfs_thread_id if pfs_thread_id does not exists in the map of threads associated with this thread ...
Definition: resource_group.h:127
void operator=(const Resource_group &)=delete
void remove_pfs_thread_id(const ulonglong pfs_thread_id, bool skip_mutex_lock=false)
Remove the PFS thread id from the thread id maps.
Definition: resource_group.h:179
bool is_bound_to_threads()
Check if resource group is associated with threads.
Definition: resource_group.h:81
void set_enabled(bool enabled)
Definition: resource_group.h:66
void apply_control_func(std::function< void(ulonglong, Resource_group_switch_handler *)> control_func)
Apply a control function on threads associated with this resource group.
Definition: resource_group.h:203
void set_type(Type type)
Definition: resource_group.h:64
bool is_pfs_thread_id_exists(const ulonglong pfs_thread_id)
Is pfs thread id already exists in the m_pfs_thread_id_map map.
Definition: resource_group.h:94
std::map< ulonglong, Resource_group_switch_handler * > m_pfs_thread_id_map
Definition: resource_group.h:243
bool enabled() const
Definition: resource_group.h:62
void add_pfs_thread_id(const ulonglong pfs_thread_id, Resource_group_switch_handler *rg_switch_handler)
Add pfs_thread_id to the thread id map associated with this resource group.
Definition: resource_group.h:109
const Thread_resource_control * controller() const
Definition: resource_group.h:70
Thread_resource_control m_thread_resource_control
Thread resource controller object.
Definition: resource_group.h:233
Class that abstracts the resource control that can be applied to threads.
Definition: thread_resource_control.h:39
Error logging, slow query logging, general query logging: If it's server-internal,...
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned long long my_thread_os_id_t
Definition: my_thread_os_id.h:47
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:78
Definition: dd_resource_group.h:28
Type
Definition: resource_group_basic_types.h:32
Resource_group_switch_handler default_rg_switch_handler
Default resource group switch handler instance.
Definition: resource_group.cc:31
static std::mutex lock
Definition: net_ns.cc:55