MySQL 8.0.32
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 <atomic>
28#include <functional>
29#include <memory>
30#include <mutex>
31#include <set>
32
33#include "sql/log.h" // sql_print_warning
35
36namespace resourcegroups {
37
38/**
39 Class that represents an abstraction of the Resource Group.
40 It has generic attributes of Resource group name, type,
41 active or inactive, a pointer to a Resource control object.
42*/
43
45 public:
46 /**
47 Construct a Resource_group object.
48
49 @param name name of the Resource_group.
50 @param type type of the Resource_group.
51 @param enabled Check if Resource_group is enabled or not.
52 */
53
54 Resource_group(const std::string &name, const Type type, bool enabled)
56
57 const std::string &name() const { return m_name; }
58
59 Type type() const { return m_type; }
60
61 bool enabled() const { return m_enabled; }
62
64
66
67 /**
68 Method to check if resource group is defunct.
69
70 @returns true if resource group is defunct else false.
71 */
72
73 bool is_defunct() const { return m_defunct; }
74
75 /**
76 Method to mark resource group defunct.
77 */
78
79 void set_defunct() { m_defunct = true; }
80
82
85 }
86
87 /**
88 Check if resource group is associated with threads.
89
90 @return true if some threads are mapped with this resource group
91 else false.
92 */
93
95 std::unique_lock<std::mutex> lock(m_set_mutex);
96 return !m_pfs_thread_id_set.empty();
97 }
98
99 /**
100 Is pfs thread id already exists in the set.
101
102 @param pfs_thread_id PFS thread id.
103
104 @return true if thread id exists in the set else false.
105 */
106
107 bool is_pfs_thread_id_exists(const ulonglong pfs_thread_id) {
108 std::unique_lock<std::mutex> lock(m_set_mutex);
109 return m_pfs_thread_id_set.find(pfs_thread_id) != m_pfs_thread_id_set.end();
110 }
111
112 /**
113 Add thread_id to the thread id set associated with this resource group.
114
115 @param pfs_thread_id PFS thread id.
116 */
117
118 void add_pfs_thread_id(const ulonglong pfs_thread_id) {
119 std::unique_lock<std::mutex> lock(m_set_mutex);
120 (void)m_pfs_thread_id_set.insert(pfs_thread_id);
122 }
123
124 /**
125 Remove the PFS thread id.
126
127 @param pfs_thread_id Remove pfs thread id.
128 */
129
130 void remove_pfs_thread_id(const ulonglong pfs_thread_id) {
131 std::unique_lock<std::mutex> lock(m_set_mutex);
132 (void)m_pfs_thread_id_set.erase(pfs_thread_id);
134 }
135
136 /**
137 Clear the thread id set associated with this resource group.
138 */
139
140 void clear() {
141 std::unique_lock<std::mutex> lock(m_set_mutex);
143 (void)m_pfs_thread_id_set.clear();
144 }
145
146 /**
147 Apply a control function on threads associated with this resource group.
148
149 @param control_func pointer to Control function.
150 */
151
152 void apply_control_func(std::function<void(ulonglong)> control_func) {
153 std::unique_lock<std::mutex> lock(m_set_mutex);
154 for (auto pfs_thread_id : m_pfs_thread_id_set) control_func(pfs_thread_id);
155 }
156
157 std::atomic<ulonglong> &reference_count() { return m_reference_count; }
158
159 uint &version() { return m_version; }
160
161 ~Resource_group() = default;
162
163 private:
164 /**
165 Name of the resource group.
166 */
167 std::string m_name;
168
169 /**
170 Type whether it is user or system resource group.
171 */
173
174 /**
175 bool flag whether resource is enabled or disabled.
176 */
178
179 /**
180 Whether resource group is defunct or operative.
181 */
182 bool m_defunct{false};
183
184 /**
185 Thread resource controller object.
186 */
188
189 /**
190 Threads associated with this resource group.
191 */
192 std::set<ulonglong> m_pfs_thread_id_set;
193
194 /**
195 Mutex protecting the resource group set.
196 */
197 std::mutex m_set_mutex;
198
199 /**
200 Count of threads referencing resource group. Count includes threads
201 associated with this resource group (i.e. threads in m_pfs_thread_id_set)
202 and other threads referencing this resource group (Only system threads
203 internally switched to refer user resource group to execute user queries
204 in some cases. User resource group maintains counter of even such
205 references.)
206 */
207 std::atomic<ulonglong> m_reference_count{0};
208
209 /**
210 Version number of a Resource group in-memory instance. Version number is
211 incremented on thread resource controls alter. If other threads (only
212 system threads internally switched to refer user resource group to execute
213 user queries for now) references this resource group, then resource group
214 is re-applied by threads on version number mismatch.
215 */
217
218 /**
219 Disable copy construction and assignment.
220 */
222 void operator=(const Resource_group &) = delete;
223};
224} // namespace resourcegroups
225#endif // RESOURCEGROUPS_RESOURCE_GROUP_H_
Class that represents an abstraction of the Resource Group.
Definition: resource_group.h:44
void clear()
Clear the thread id set associated with this resource group.
Definition: resource_group.h:140
Resource_group(const std::string &name, const Type type, bool enabled)
Construct a Resource_group object.
Definition: resource_group.h:54
std::set< ulonglong > m_pfs_thread_id_set
Threads associated with this resource group.
Definition: resource_group.h:192
std::atomic< ulonglong > m_reference_count
Count of threads referencing resource group.
Definition: resource_group.h:207
const std::string & name() const
Definition: resource_group.h:57
uint & version()
Definition: resource_group.h:159
std::mutex m_set_mutex
Mutex protecting the resource group set.
Definition: resource_group.h:197
void apply_control_func(std::function< void(ulonglong)> control_func)
Apply a control function on threads associated with this resource group.
Definition: resource_group.h:152
Thread_resource_control * controller()
Definition: resource_group.h:81
void set_defunct()
Method to mark resource group defunct.
Definition: resource_group.h:79
void add_pfs_thread_id(const ulonglong pfs_thread_id)
Add thread_id to the thread id set associated with this resource group.
Definition: resource_group.h:118
void remove_pfs_thread_id(const ulonglong pfs_thread_id)
Remove the PFS thread id.
Definition: resource_group.h:130
bool m_enabled
bool flag whether resource is enabled or disabled.
Definition: resource_group.h:177
Resource_group(const Resource_group &)=delete
Disable copy construction and assignment.
Type type() const
Definition: resource_group.h:59
bool m_defunct
Whether resource group is defunct or operative.
Definition: resource_group.h:182
Type m_type
Type whether it is user or system resource group.
Definition: resource_group.h:172
bool is_defunct() const
Method to check if resource group is defunct.
Definition: resource_group.h:73
std::string m_name
Name of the resource group.
Definition: resource_group.h:167
void operator=(const Resource_group &)=delete
uint m_version
Version number of a Resource group in-memory instance.
Definition: resource_group.h:216
bool is_bound_to_threads()
Check if resource group is associated with threads.
Definition: resource_group.h:94
void set_enabled(bool enabled)
Definition: resource_group.h:65
void set_type(Type type)
Definition: resource_group.h:63
std::atomic< ulonglong > & reference_count()
Definition: resource_group.h:157
bool is_pfs_thread_id_exists(const ulonglong pfs_thread_id)
Is pfs thread id already exists in the set.
Definition: resource_group.h:107
bool enabled() const
Definition: resource_group.h:61
const Thread_resource_control * controller() const
Definition: resource_group.h:83
Thread_resource_control m_thread_resource_control
Thread resource controller object.
Definition: resource_group.h:187
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
Definition: dd_resource_group.h:28
Type
Definition: resource_group_basic_types.h:32
static std::mutex lock
Definition: net_ns.cc:55
unsigned int uint
Definition: uca-dump.cc:29