1 /* Copyright (C) 2012 Monty Program Ab
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 #define MAX_THREAD_GROUPS 100000
17 
18 /* Threadpool parameters */
19 extern uint threadpool_min_threads;  /* Minimum threads in pool */
20 extern uint threadpool_idle_timeout; /* Shutdown idle worker threads  after this timeout */
21 extern uint threadpool_size; /* Number of parallel executing threads */
22 extern uint threadpool_max_size;
23 extern uint threadpool_stall_limit;  /* time interval in 10 ms units for stall checks*/
24 extern uint threadpool_max_threads;  /* Maximum threads in pool */
25 extern uint threadpool_oversubscribe;  /* Maximum active threads in group */
26 extern uint threadpool_prio_kickup_timer;  /* Time before low prio item gets prio boost */
27 #ifdef _WIN32
28 extern uint threadpool_mode; /* Thread pool implementation , windows or generic */
29 #define TP_MODE_WINDOWS 0
30 #define TP_MODE_GENERIC 1
31 #endif
32 
33 
34 struct TP_connection;
35 extern void tp_callback(TP_connection *c);
36 extern void tp_timeout_handler(TP_connection *c);
37 
38 
39 
40 /*
41   Threadpool statistics
42 */
43 struct TP_STATISTICS
44 {
45   /* Current number of worker thread. */
46   volatile int32 num_worker_threads;
47 };
48 
49 extern TP_STATISTICS tp_stats;
50 
51 
52 /* Functions to set threadpool parameters */
53 extern void tp_set_min_threads(uint val);
54 extern void tp_set_max_threads(uint val);
55 extern void tp_set_threadpool_size(uint val);
56 extern void tp_set_threadpool_stall_limit(uint val);
57 extern int tp_get_idle_thread_count();
58 extern int tp_get_thread_count();
59 
60 /* Activate threadpool scheduler */
61 extern void tp_scheduler(void);
62 
63 extern int show_threadpool_idle_threads(THD *thd, SHOW_VAR *var, char *buff,
64                                         enum enum_var_type scope);
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 struct TP_connection
89 {
90   THD*        thd;
91   CONNECT*    connect;
92   TP_STATE    state;
93   TP_PRIORITY priority;
TP_connectionTP_connection94   TP_connection(CONNECT *c) :
95     thd(0),
96     connect(c),
97     state(TP_STATE_IDLE),
98     priority(TP_PRIORITY_HIGH)
99   {}
100 
~TP_connectionTP_connection101   virtual ~TP_connection()
102   {};
103 
104   /* Initialize io structures windows threadpool, epoll etc */
105   virtual int init() = 0;
106 
107   virtual void set_io_timeout(int sec) = 0;
108 
109   /* Read for the next client command (async) with specified timeout */
110   virtual int start_io() = 0;
111 
112   virtual void wait_begin(int type)= 0;
113   virtual void wait_end() = 0;
114 
115 };
116 
117 
118 struct TP_pool
119 {
~TP_poolTP_pool120   virtual ~TP_pool(){};
121   virtual int init()= 0;
122   virtual TP_connection *new_connection(CONNECT *)= 0;
123   virtual void add(TP_connection *c)= 0;
set_max_threadsTP_pool124   virtual int set_max_threads(uint){ return 0; }
set_min_threadsTP_pool125   virtual int set_min_threads(uint){ return 0; }
set_pool_sizeTP_pool126   virtual int set_pool_size(uint){ return 0; }
set_idle_timeoutTP_pool127   virtual int set_idle_timeout(uint){ return 0; }
set_oversubscribeTP_pool128   virtual int set_oversubscribe(uint){ return 0; }
set_stall_limitTP_pool129   virtual int set_stall_limit(uint){ return 0; }
get_thread_countTP_pool130   virtual int get_thread_count() { return tp_stats.num_worker_threads; }
get_idle_thread_countTP_pool131   virtual int get_idle_thread_count(){ return 0; }
132 };
133 
134 #ifdef _WIN32
135 struct TP_pool_win:TP_pool
136 {
137   TP_pool_win();
138   virtual int init();
139   virtual ~TP_pool_win();
140   virtual TP_connection *new_connection(CONNECT *c);
141   virtual void add(TP_connection *);
142   virtual int set_max_threads(uint);
143   virtual int set_min_threads(uint);
144 };
145 #endif
146 
147 struct TP_pool_generic :TP_pool
148 {
149   TP_pool_generic();
150   ~TP_pool_generic();
151   virtual int init();
152   virtual TP_connection *new_connection(CONNECT *c);
153   virtual void add(TP_connection *);
154   virtual int set_pool_size(uint);
155   virtual int set_stall_limit(uint);
156   virtual int get_idle_thread_count();
157 };
158