MySQL 8.1.0
Source Code Documentation
resource_group.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2023, 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 <set>
31
32#include "sql/log.h" // sql_print_warning
34
35namespace resourcegroups {
36
37/**
38 Class that represents an abstraction of the Resource Group.
39 It has generic attributes of Resource group name, type,
40 active or inactive, a pointer to a Resource control object.
41*/
42
44 public:
45 /**
46 Construct a Resource_group object.
47
48 @param name name of the Resource_group.
49 @param type type of the Resource_group.
50 @param enabled Check if Resource_group is enabled or not.
51 */
52
53 Resource_group(const std::string &name, const Type type, bool enabled)
55
56 const std::string &name() const { return m_name; }
57
58 Type type() const { return m_type; }
59
60 bool enabled() const { return m_enabled; }
61
63
65
67
70 }
71
72 /**
73 Check if resource group is associated with threads.
74
75 @return true if some threads are mapped with this resource group
76 else false.
77 */
78
80 std::unique_lock<std::mutex> lock(m_set_mutex);
81 return !m_pfs_thread_id_set.empty();
82 }
83
84 /**
85 Is pfs thread id already exists in the set.
86
87 @param pfs_thread_id PFS thread id.
88
89 @return true if thread id exists in the set else false.
90 */
91
92 bool is_pfs_thread_id_exists(const ulonglong pfs_thread_id) {
93 std::unique_lock<std::mutex> lock(m_set_mutex);
94 return m_pfs_thread_id_set.find(pfs_thread_id) != m_pfs_thread_id_set.end();
95 }
96
97 /**
98 Add thread_id to the thread id set associated with this resource group.
99
100 @param pfs_thread_id PFS thread id.
101 */
102
103 void add_pfs_thread_id(const ulonglong pfs_thread_id) {
104 std::unique_lock<std::mutex> lock(m_set_mutex);
105 (void)m_pfs_thread_id_set.insert(pfs_thread_id);
106 }
107
108 /**
109 Remove the PFS thread id.
110
111 @param pfs_thread_id Remove pfs thread id.
112 */
113
114 void remove_pfs_thread_id(const ulonglong pfs_thread_id) {
115 std::unique_lock<std::mutex> lock(m_set_mutex);
116 (void)m_pfs_thread_id_set.erase(pfs_thread_id);
117 }
118
119 /**
120 Clear the thread id set associated with this resource group.
121 */
122
123 void clear() {
124 std::unique_lock<std::mutex> lock(m_set_mutex);
125 (void)m_pfs_thread_id_set.clear();
126 }
127
128 /**
129 Apply a control function on threads associated with this resource group.
130
131 @param control_func pointer to Control function.
132 */
133
134 void apply_control_func(std::function<void(ulonglong)> control_func) {
135 std::unique_lock<std::mutex> lock(m_set_mutex);
136 for (auto pfs_thread_id : m_pfs_thread_id_set) control_func(pfs_thread_id);
137 }
138
139 ~Resource_group() = default;
140
141 private:
142 /**
143 Name of the resource group.
144 */
145 std::string m_name;
146
147 /**
148 Type whether it is user or system resource group.
149 */
151
152 /**
153 bool flag whether resource is enabled or disabled.
154 */
156
157 /**
158 Thread resource controller object.
159 */
161
162 /**
163 Threads associated with this resource group.
164 */
165 std::set<ulonglong> m_pfs_thread_id_set;
166
167 /**
168 Mutex protecting the resource group set.
169 */
170 std::mutex m_set_mutex;
171
172 /**
173 Disable copy construction and assignment.
174 */
176 void operator=(const Resource_group &) = delete;
177};
178} // namespace resourcegroups
179#endif // RESOURCEGROUPS_RESOURCE_GROUP_H_
Class that represents an abstraction of the Resource Group.
Definition: resource_group.h:43
void clear()
Clear the thread id set associated with this resource group.
Definition: resource_group.h:123
Resource_group(const std::string &name, const Type type, bool enabled)
Construct a Resource_group object.
Definition: resource_group.h:53
std::set< ulonglong > m_pfs_thread_id_set
Threads associated with this resource group.
Definition: resource_group.h:165
const std::string & name() const
Definition: resource_group.h:56
std::mutex m_set_mutex
Mutex protecting the resource group set.
Definition: resource_group.h:170
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:134
Thread_resource_control * controller()
Definition: resource_group.h:66
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:103
void remove_pfs_thread_id(const ulonglong pfs_thread_id)
Remove the PFS thread id.
Definition: resource_group.h:114
bool m_enabled
bool flag whether resource is enabled or disabled.
Definition: resource_group.h:155
Resource_group(const Resource_group &)=delete
Disable copy construction and assignment.
Type type() const
Definition: resource_group.h:58
Type m_type
Type whether it is user or system resource group.
Definition: resource_group.h:150
std::string m_name
Name of the resource group.
Definition: resource_group.h:145
void operator=(const Resource_group &)=delete
bool is_bound_to_threads()
Check if resource group is associated with threads.
Definition: resource_group.h:79
void set_enabled(bool enabled)
Definition: resource_group.h:64
void set_type(Type type)
Definition: resource_group.h:62
bool is_pfs_thread_id_exists(const ulonglong pfs_thread_id)
Is pfs thread id already exists in the set.
Definition: resource_group.h:92
bool enabled() const
Definition: resource_group.h:60
const Thread_resource_control * controller() const
Definition: resource_group.h:68
Thread_resource_control m_thread_resource_control
Thread resource controller object.
Definition: resource_group.h:160
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