1 /* Copyright (C) 2012, 2020, MariaDB 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 as published by 5 the Free Software Foundation; version 2 of the License. 6 7 This program is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 GNU General Public License for more details. 11 12 You should have received a copy of the GNU General Public License 13 along with this program; if not, write to the Free Software 14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */ 15 16 #pragma once 17 #ifdef HAVE_POOL_OF_THREADS 18 #define MAX_THREAD_GROUPS 100000 19 20 /* Threadpool parameters */ 21 extern uint threadpool_min_threads; /* Minimum threads in pool */ 22 extern uint threadpool_idle_timeout; /* Shutdown idle worker threads after this timeout */ 23 extern uint threadpool_size; /* Number of parallel executing threads */ 24 extern uint threadpool_max_size; 25 extern uint threadpool_stall_limit; /* time interval in milliseconds for stall checks*/ 26 extern uint threadpool_max_threads; /* Maximum threads in pool */ 27 extern uint threadpool_oversubscribe; /* Maximum active threads in group */ 28 extern uint threadpool_prio_kickup_timer; /* Time before low prio item gets prio boost */ 29 extern my_bool threadpool_exact_stats; /* Better queueing time stats for information_schema, at small performance cost */ 30 extern my_bool threadpool_dedicated_listener; /* Listener thread does not pick up work items. */ 31 #ifdef _WIN32 32 extern uint threadpool_mode; /* Thread pool implementation , windows or generic */ 33 #define TP_MODE_WINDOWS 0 34 #define TP_MODE_GENERIC 1 35 #endif 36 37 #define DEFAULT_THREADPOOL_STALL_LIMIT 500U 38 39 struct TP_connection; 40 extern void tp_callback(TP_connection *c); 41 extern void tp_timeout_handler(TP_connection *c); 42 43 44 45 /* 46 Threadpool statistics 47 */ 48 struct TP_STATISTICS 49 { 50 /* Current number of worker thread. */ 51 Atomic_counter<uint32_t> num_worker_threads; 52 }; 53 54 extern TP_STATISTICS tp_stats; 55 56 57 /* Functions to set threadpool parameters */ 58 extern void tp_set_min_threads(uint val); 59 extern void tp_set_max_threads(uint val); 60 extern void tp_set_threadpool_size(uint val); 61 extern void tp_set_threadpool_stall_limit(uint val); 62 extern int tp_get_idle_thread_count(); 63 extern int tp_get_thread_count(); 64 65 66 enum TP_PRIORITY { 67 TP_PRIORITY_HIGH, 68 TP_PRIORITY_LOW, 69 TP_PRIORITY_AUTO 70 }; 71 72 73 enum TP_STATE 74 { 75 TP_STATE_IDLE, 76 TP_STATE_RUNNING, 77 TP_STATE_PENDING 78 }; 79 80 /* 81 Connection structure, encapsulates THD + structures for asynchronous 82 IO and pool. 83 84 Platform specific parts are specified in subclasses called connection_t, 85 inside threadpool_win.cc and threadpool_unix.cc 86 */ 87 88 class CONNECT; 89 90 struct TP_connection 91 { 92 THD* thd; 93 CONNECT* connect; 94 TP_STATE state; 95 TP_PRIORITY priority; TP_connectionTP_connection96 TP_connection(CONNECT *c) : 97 thd(0), 98 connect(c), 99 state(TP_STATE_IDLE), 100 priority(TP_PRIORITY_HIGH) 101 {} 102 ~TP_connectionTP_connection103 virtual ~TP_connection() 104 {}; 105 106 /* Initialize io structures windows threadpool, epoll etc */ 107 virtual int init() = 0; 108 109 virtual void set_io_timeout(int sec) = 0; 110 111 /* Read for the next client command (async) with specified timeout */ 112 virtual int start_io() = 0; 113 114 virtual void wait_begin(int type)= 0; 115 virtual void wait_end() = 0; 116 117 }; 118 119 120 struct TP_pool 121 { ~TP_poolTP_pool122 virtual ~TP_pool(){}; 123 virtual int init()= 0; 124 virtual TP_connection *new_connection(CONNECT *)= 0; 125 virtual void add(TP_connection *c)= 0; set_max_threadsTP_pool126 virtual int set_max_threads(uint){ return 0; } set_min_threadsTP_pool127 virtual int set_min_threads(uint){ return 0; } set_pool_sizeTP_pool128 virtual int set_pool_size(uint){ return 0; } set_idle_timeoutTP_pool129 virtual int set_idle_timeout(uint){ return 0; } set_oversubscribeTP_pool130 virtual int set_oversubscribe(uint){ return 0; } set_stall_limitTP_pool131 virtual int set_stall_limit(uint){ return 0; } get_thread_countTP_pool132 virtual int get_thread_count() { return tp_stats.num_worker_threads; } get_idle_thread_countTP_pool133 virtual int get_idle_thread_count(){ return 0; } 134 }; 135 136 #ifdef _WIN32 137 struct TP_pool_win:TP_pool 138 { 139 TP_pool_win(); 140 virtual int init(); 141 virtual ~TP_pool_win(); 142 virtual TP_connection *new_connection(CONNECT *c); 143 virtual void add(TP_connection *); 144 virtual int set_max_threads(uint); 145 virtual int set_min_threads(uint); 146 }; 147 #endif 148 149 struct TP_pool_generic :TP_pool 150 { 151 TP_pool_generic(); 152 ~TP_pool_generic(); 153 virtual int init(); 154 virtual TP_connection *new_connection(CONNECT *c); 155 virtual void add(TP_connection *); 156 virtual int set_pool_size(uint); 157 virtual int set_stall_limit(uint); 158 virtual int get_idle_thread_count(); 159 }; 160 161 #endif /* HAVE_POOL_OF_THREADS */ 162