2020-03-30 17:52:17 +02:00
|
|
|
/* Copyright (C) 2012, 2020, MariaDB
|
2012-02-17 23:27:15 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
Update FSF address
This commit is based on the work of Michal Schorm, rebased on the
earliest MariaDB version.
Th command line used to generate this diff was:
find ./ -type f \
-exec sed -i -e 's/Foundation, Inc., 59 Temple Place, Suite 330, Boston, /Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, /g' {} \; \
-exec sed -i -e 's/Foundation, Inc. 59 Temple Place.* Suite 330, Boston, /Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, /g' {} \; \
-exec sed -i -e 's/MA.*.....-1307.*USA/MA 02110-1335 USA/g' {} \; \
-exec sed -i -e 's/Foundation, Inc., 59 Temple/Foundation, Inc., 51 Franklin/g' {} \; \
-exec sed -i -e 's/Place, Suite 330, Boston, MA.*02111-1307.*USA/Street, Fifth Floor, Boston, MA 02110-1335 USA/g' {} \; \
-exec sed -i -e 's/MA.*.....-1307/MA 02110-1335/g' {} \;
2019-05-10 19:49:46 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2020-03-30 17:52:17 +02:00
|
|
|
#pragma once
|
|
|
|
#ifdef HAVE_POOL_OF_THREADS
|
2013-11-05 06:18:59 +01:00
|
|
|
#define MAX_THREAD_GROUPS 100000
|
2011-12-19 13:28:30 +01:00
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/* Threadpool parameters */
|
|
|
|
extern uint threadpool_min_threads; /* Minimum threads in pool */
|
|
|
|
extern uint threadpool_idle_timeout; /* Shutdown idle worker threads after this timeout */
|
|
|
|
extern uint threadpool_size; /* Number of parallel executing threads */
|
2013-11-05 06:18:59 +01:00
|
|
|
extern uint threadpool_max_size;
|
2019-05-31 10:28:22 +02:00
|
|
|
extern uint threadpool_stall_limit; /* time interval in milliseconds for stall checks*/
|
2011-12-08 19:17:49 +01:00
|
|
|
extern uint threadpool_max_threads; /* Maximum threads in pool */
|
2011-12-31 05:24:11 +01:00
|
|
|
extern uint threadpool_oversubscribe; /* Maximum active threads in group */
|
2016-09-21 16:28:42 +02:00
|
|
|
extern uint threadpool_prio_kickup_timer; /* Time before low prio item gets prio boost */
|
2019-05-26 13:35:07 +02:00
|
|
|
extern my_bool threadpool_exact_stats; /* Better queueing time stats for information_schema, at small performance cost */
|
|
|
|
extern my_bool threadpool_dedicated_listener; /* Listener thread does not pick up work items. */
|
2016-09-21 16:28:42 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
extern uint threadpool_mode; /* Thread pool implementation , windows or generic */
|
|
|
|
#define TP_MODE_WINDOWS 0
|
|
|
|
#define TP_MODE_GENERIC 1
|
|
|
|
#endif
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2019-05-31 10:28:22 +02:00
|
|
|
#define DEFAULT_THREADPOOL_STALL_LIMIT 500U
|
2012-01-15 11:17:45 +01:00
|
|
|
|
2016-09-21 16:28:42 +02:00
|
|
|
struct TP_connection;
|
MDEV-22990 Threadpool : Optimize network/named pipe IO for Windows
This patch reduces the overhead of system calls prior to a query, for
threadpool. Previously, 3 system calls were done
1. WSARecv() to get notification of input data from client, asynchronous
equivalent of select() in one-thread-per-connection
2. recv(4 bytes) - reading packet header length
3. recv(packet payload)
Now there will be usually, just WSARecv(), which pre-reads user data into
a buffer, so we spared 2 syscalls
Profiler shows the most expensive call WSARecv(16%CPU) becomes 4% CPU,
after the patch, benchmark results (network heavy ones like point-select)
improve by ~20%
The buffer management was rather carefully done to keep
buffers together, as Windows would keeps the pages pinned
in memory for the duration of async calls.
At most 1MB memory is used for the buffers, and overhead per-connection is
only 256 bytes, which should cover most of the uses.
SSL does not yet use the optmization, so far it does not properly use
VIO for reads and writes. Neither one-thread-per-connection would get any
benefit, but that should be fine, it is not even default on Windows.
2020-06-26 14:43:56 +02:00
|
|
|
struct st_vio;
|
|
|
|
|
2016-09-21 16:28:42 +02:00
|
|
|
extern void tp_callback(TP_connection *c);
|
|
|
|
extern void tp_timeout_handler(TP_connection *c);
|
2012-01-24 03:23:14 +01:00
|
|
|
|
|
|
|
|
2012-01-15 15:41:25 +01:00
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/*
|
|
|
|
Threadpool statistics
|
|
|
|
*/
|
|
|
|
struct TP_STATISTICS
|
|
|
|
{
|
|
|
|
/* Current number of worker thread. */
|
2020-03-26 23:24:49 +01:00
|
|
|
Atomic_counter<uint32_t> num_worker_threads;
|
2011-12-08 19:17:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extern TP_STATISTICS tp_stats;
|
|
|
|
|
|
|
|
|
|
|
|
/* Functions to set threadpool parameters */
|
|
|
|
extern void tp_set_min_threads(uint val);
|
|
|
|
extern void tp_set_max_threads(uint val);
|
2012-02-16 16:59:04 +01:00
|
|
|
extern void tp_set_threadpool_size(uint val);
|
2011-12-29 21:11:06 +01:00
|
|
|
extern void tp_set_threadpool_stall_limit(uint val);
|
2016-09-21 16:28:42 +02:00
|
|
|
extern int tp_get_idle_thread_count();
|
|
|
|
extern int tp_get_thread_count();
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2016-09-21 16:28:42 +02:00
|
|
|
|
|
|
|
enum TP_PRIORITY {
|
|
|
|
TP_PRIORITY_HIGH,
|
|
|
|
TP_PRIORITY_LOW,
|
|
|
|
TP_PRIORITY_AUTO
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum TP_STATE
|
|
|
|
{
|
|
|
|
TP_STATE_IDLE,
|
|
|
|
TP_STATE_RUNNING,
|
2020-07-28 15:59:38 +02:00
|
|
|
TP_STATE_PENDING
|
2016-09-21 16:28:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Connection structure, encapsulates THD + structures for asynchronous
|
|
|
|
IO and pool.
|
|
|
|
|
|
|
|
Platform specific parts are specified in subclasses called connection_t,
|
|
|
|
inside threadpool_win.cc and threadpool_unix.cc
|
|
|
|
*/
|
|
|
|
|
2019-05-26 13:25:12 +02:00
|
|
|
class CONNECT;
|
|
|
|
|
2016-09-21 16:28:42 +02:00
|
|
|
struct TP_connection
|
|
|
|
{
|
|
|
|
THD* thd;
|
|
|
|
CONNECT* connect;
|
|
|
|
TP_STATE state;
|
|
|
|
TP_PRIORITY priority;
|
|
|
|
TP_connection(CONNECT *c) :
|
|
|
|
thd(0),
|
|
|
|
connect(c),
|
|
|
|
state(TP_STATE_IDLE),
|
|
|
|
priority(TP_PRIORITY_HIGH)
|
|
|
|
{}
|
|
|
|
|
2023-02-07 12:57:20 +01:00
|
|
|
virtual ~TP_connection() = default;
|
2016-09-21 16:28:42 +02:00
|
|
|
|
|
|
|
/* Initialize io structures windows threadpool, epoll etc */
|
|
|
|
virtual int init() = 0;
|
|
|
|
|
|
|
|
virtual void set_io_timeout(int sec) = 0;
|
|
|
|
|
|
|
|
/* Read for the next client command (async) with specified timeout */
|
|
|
|
virtual int start_io() = 0;
|
|
|
|
|
|
|
|
virtual void wait_begin(int type)= 0;
|
|
|
|
virtual void wait_end() = 0;
|
MDEV-22990 Threadpool : Optimize network/named pipe IO for Windows
This patch reduces the overhead of system calls prior to a query, for
threadpool. Previously, 3 system calls were done
1. WSARecv() to get notification of input data from client, asynchronous
equivalent of select() in one-thread-per-connection
2. recv(4 bytes) - reading packet header length
3. recv(packet payload)
Now there will be usually, just WSARecv(), which pre-reads user data into
a buffer, so we spared 2 syscalls
Profiler shows the most expensive call WSARecv(16%CPU) becomes 4% CPU,
after the patch, benchmark results (network heavy ones like point-select)
improve by ~20%
The buffer management was rather carefully done to keep
buffers together, as Windows would keeps the pages pinned
in memory for the duration of async calls.
At most 1MB memory is used for the buffers, and overhead per-connection is
only 256 bytes, which should cover most of the uses.
SSL does not yet use the optmization, so far it does not properly use
VIO for reads and writes. Neither one-thread-per-connection would get any
benefit, but that should be fine, it is not even default on Windows.
2020-06-26 14:43:56 +02:00
|
|
|
IF_WIN(virtual,) void init_vio(st_vio *){};
|
2016-09-21 16:28:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct TP_pool
|
|
|
|
{
|
2023-02-07 12:57:20 +01:00
|
|
|
virtual ~TP_pool() = default;
|
2016-09-21 16:28:42 +02:00
|
|
|
virtual int init()= 0;
|
|
|
|
virtual TP_connection *new_connection(CONNECT *)= 0;
|
|
|
|
virtual void add(TP_connection *c)= 0;
|
|
|
|
virtual int set_max_threads(uint){ return 0; }
|
|
|
|
virtual int set_min_threads(uint){ return 0; }
|
|
|
|
virtual int set_pool_size(uint){ return 0; }
|
|
|
|
virtual int set_idle_timeout(uint){ return 0; }
|
|
|
|
virtual int set_oversubscribe(uint){ return 0; }
|
|
|
|
virtual int set_stall_limit(uint){ return 0; }
|
|
|
|
virtual int get_thread_count() { return tp_stats.num_worker_threads; }
|
|
|
|
virtual int get_idle_thread_count(){ return 0; }
|
2021-02-14 18:30:39 +01:00
|
|
|
virtual void resume(TP_connection* c)=0;
|
2016-09-21 16:28:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
MDEV-22990 Threadpool : Optimize network/named pipe IO for Windows
This patch reduces the overhead of system calls prior to a query, for
threadpool. Previously, 3 system calls were done
1. WSARecv() to get notification of input data from client, asynchronous
equivalent of select() in one-thread-per-connection
2. recv(4 bytes) - reading packet header length
3. recv(packet payload)
Now there will be usually, just WSARecv(), which pre-reads user data into
a buffer, so we spared 2 syscalls
Profiler shows the most expensive call WSARecv(16%CPU) becomes 4% CPU,
after the patch, benchmark results (network heavy ones like point-select)
improve by ~20%
The buffer management was rather carefully done to keep
buffers together, as Windows would keeps the pages pinned
in memory for the duration of async calls.
At most 1MB memory is used for the buffers, and overhead per-connection is
only 256 bytes, which should cover most of the uses.
SSL does not yet use the optmization, so far it does not properly use
VIO for reads and writes. Neither one-thread-per-connection would get any
benefit, but that should be fine, it is not even default on Windows.
2020-06-26 14:43:56 +02:00
|
|
|
|
2016-09-21 16:28:42 +02:00
|
|
|
struct TP_pool_win:TP_pool
|
|
|
|
{
|
2020-05-29 12:21:27 +02:00
|
|
|
TP_pool_win();
|
2024-06-12 15:46:26 +02:00
|
|
|
int init() override;
|
|
|
|
~TP_pool_win() override;
|
|
|
|
TP_connection *new_connection(CONNECT *c) override;
|
|
|
|
void add(TP_connection *) override;
|
|
|
|
int set_max_threads(uint) override;
|
|
|
|
int set_min_threads(uint) override;
|
2024-06-24 12:09:47 +02:00
|
|
|
void resume(TP_connection *c) override;
|
2016-09-21 16:28:42 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct TP_pool_generic :TP_pool
|
|
|
|
{
|
|
|
|
TP_pool_generic();
|
|
|
|
~TP_pool_generic();
|
2024-06-12 15:46:26 +02:00
|
|
|
int init() override;
|
|
|
|
TP_connection *new_connection(CONNECT *c) override;
|
|
|
|
void add(TP_connection *) override;
|
|
|
|
int set_pool_size(uint) override;
|
|
|
|
int set_stall_limit(uint) override;
|
|
|
|
int get_idle_thread_count() override;
|
2024-06-24 12:09:47 +02:00
|
|
|
void resume(TP_connection* c) override;
|
2016-09-21 16:28:42 +02:00
|
|
|
};
|
2019-04-18 06:50:59 +02:00
|
|
|
|
|
|
|
#endif /* HAVE_POOL_OF_THREADS */
|