1 /* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 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 25 #include "resource_group_sql_cmd.h" 26 27 #include <functional> 28 #include <memory> 29 #include <mutex> 30 #include <set> 31 32 #include "sql/log.h" // sql_print_warning 33 #include "sql/resourcegroups/thread_resource_control.h" 34 35 namespace 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 43 class Resource_group { 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 Resource_group(const std::string & name,const Type type,bool enabled)53 Resource_group(const std::string &name, const Type type, bool enabled) 54 : m_name(name), m_type(type), m_enabled(enabled) {} 55 name()56 const std::string &name() const { return m_name; } 57 type()58 Type type() const { return m_type; } 59 enabled()60 bool enabled() const { return m_enabled; } 61 set_type(Type type)62 void set_type(Type type) { m_type = type; } 63 set_enabled(bool enabled)64 void set_enabled(bool enabled) { m_enabled = enabled; } 65 controller()66 Thread_resource_control *controller() { return &m_thread_resource_control; } 67 controller()68 const Thread_resource_control *controller() const { 69 return &m_thread_resource_control; 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 is_bound_to_threads()79 bool is_bound_to_threads() { 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 is_pfs_thread_id_exists(const ulonglong pfs_thread_id)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 add_pfs_thread_id(const ulonglong pfs_thread_id)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 remove_pfs_thread_id(const ulonglong pfs_thread_id)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 clear()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 apply_control_func(std::function<void (ulonglong)> control_func)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 ~Resource_group()139 ~Resource_group() {} 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 */ 150 Type m_type; 151 152 /** 153 bool flag whether resource is enabled or disabled. 154 */ 155 bool m_enabled; 156 157 /** 158 Thread resource controller object. 159 */ 160 Thread_resource_control m_thread_resource_control; 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 */ 175 Resource_group(const Resource_group &) = delete; 176 void operator=(const Resource_group &) = delete; 177 }; 178 } // namespace resourcegroups 179 #endif // RESOURCEGROUPS_RESOURCE_GROUP_H_ 180