2012-02-17 23:23:54 +01:00
|
|
|
/* Copyright (C) 2012 Monty Program Ab
|
|
|
|
|
|
|
|
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 20:49:46 +03:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
2012-02-17 23:23:54 +01:00
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
#ifdef _WIN32_WINNT
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define _WIN32_WINNT 0x0601
|
|
|
|
|
2017-06-18 06:42:16 +03:00
|
|
|
#include "mariadb.h"
|
2011-12-08 19:17:49 +01:00
|
|
|
#include <violite.h>
|
|
|
|
#include <sql_priv.h>
|
|
|
|
#include <sql_class.h>
|
|
|
|
#include <my_pthread.h>
|
|
|
|
#include <scheduler.h>
|
|
|
|
#include <sql_connect.h>
|
|
|
|
#include <mysqld.h>
|
|
|
|
#include <debug_sync.h>
|
|
|
|
#include <threadpool.h>
|
|
|
|
#include <windows.h>
|
2020-07-20 08:08:00 +02:00
|
|
|
#include <set_var.h>
|
2011-12-08 19:17:49 +01:00
|
|
|
|
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
|
|
|
#include "threadpool_winsockets.h"
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/* Log a warning */
|
|
|
|
static void tp_log_warning(const char *msg, const char *fct)
|
|
|
|
{
|
|
|
|
sql_print_warning("Threadpool: %s. %s failed (last error %d)",msg, fct,
|
|
|
|
GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static PTP_POOL pool;
|
|
|
|
static TP_CALLBACK_ENVIRON callback_environ;
|
|
|
|
static DWORD fls;
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2018-09-25 16:06:32 +01:00
|
|
|
PTP_CALLBACK_ENVIRON get_threadpool_win_callback_environ()
|
|
|
|
{
|
|
|
|
return pool? &callback_environ: 0;
|
|
|
|
}
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/*
|
|
|
|
Threadpool callbacks.
|
|
|
|
|
|
|
|
io_completion_callback - handle client request
|
|
|
|
timer_callback - handle wait timeout (kill connection)
|
|
|
|
login_callback - user login (submitted as threadpool work)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-05-29 12:21:27 +02:00
|
|
|
static void CALLBACK timer_callback(PTP_CALLBACK_INSTANCE instance,
|
2011-12-08 19:17:49 +01:00
|
|
|
PVOID context, PTP_TIMER timer);
|
|
|
|
|
2020-05-29 12:21:27 +02:00
|
|
|
static void CALLBACK io_completion_callback(PTP_CALLBACK_INSTANCE instance,
|
2011-12-08 19:17:49 +01:00
|
|
|
PVOID context, PVOID overlapped, ULONG io_result, ULONG_PTR nbytes, PTP_IO io);
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
|
|
|
|
static void CALLBACK work_callback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK work);
|
|
|
|
|
|
|
|
static void pre_callback(PVOID context, PTP_CALLBACK_INSTANCE instance);
|
2011-12-08 19:17:49 +01:00
|
|
|
|
|
|
|
/* Get current time as Windows time */
|
|
|
|
static ulonglong now()
|
|
|
|
{
|
|
|
|
ulonglong current_time;
|
|
|
|
GetSystemTimeAsFileTime((PFILETIME)¤t_time);
|
|
|
|
return current_time;
|
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
struct TP_connection_win:public TP_connection
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
public:
|
|
|
|
TP_connection_win(CONNECT*);
|
|
|
|
~TP_connection_win();
|
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
|
|
|
int init() override;
|
|
|
|
void init_vio(st_vio *vio) override;
|
|
|
|
int start_io() override;
|
|
|
|
void set_io_timeout(int sec) override;
|
|
|
|
void wait_begin(int type) override;
|
|
|
|
void wait_end() override;
|
|
|
|
|
|
|
|
ulonglong timeout=ULLONG_MAX;
|
|
|
|
OVERLAPPED overlapped{};
|
|
|
|
PTP_CALLBACK_INSTANCE callback_instance{};
|
|
|
|
PTP_IO io{};
|
|
|
|
PTP_TIMER timer{};
|
|
|
|
PTP_WORK work{};
|
|
|
|
bool long_callback{};
|
|
|
|
win_aiosocket sock;
|
2011-12-08 19:17:49 +01:00
|
|
|
};
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
struct TP_connection *new_TP_connection(CONNECT *connect)
|
|
|
|
{
|
|
|
|
TP_connection *c = new (std::nothrow) TP_connection_win(connect);
|
|
|
|
if (!c || c->init())
|
|
|
|
{
|
|
|
|
delete c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2012-01-26 04:35:54 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
void TP_pool_win::add(TP_connection *c)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2018-09-25 16:06:32 +01:00
|
|
|
if(FlsGetValue(fls))
|
|
|
|
{
|
|
|
|
/* Inside threadpool(), execute callback directly. */
|
|
|
|
tp_callback(c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SubmitThreadpoolWork(((TP_connection_win *)c)->work);
|
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:30:39 +01:00
|
|
|
void TP_pool_win::resume(TP_connection* c)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(c->state == TP_STATE_RUNNING);
|
|
|
|
SubmitThreadpoolWork(((TP_connection_win*)c)->work);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
#define CHECK_ALLOC_ERROR(op) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (!(op)) \
|
|
|
|
{ \
|
|
|
|
tp_log_warning("Allocation failed", #op); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2012-01-26 04:35:54 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
TP_connection_win::TP_connection_win(CONNECT *c) :
|
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
|
|
|
TP_connection(c)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
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
|
|
|
/* Assign io completion callback */
|
|
|
|
HANDLE h= c->vio_type == VIO_TYPE_NAMEDPIPE ? c->pipe
|
|
|
|
: (HANDLE)mysql_socket_getfd(c->sock);
|
2016-09-21 14:28:42 +00:00
|
|
|
|
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
|
|
|
CHECK_ALLOC_ERROR(io=CreateThreadpoolIo(h, io_completion_callback, this, &callback_environ));
|
|
|
|
CHECK_ALLOC_ERROR(timer= CreateThreadpoolTimer(timer_callback, this, &callback_environ));
|
|
|
|
CHECK_ALLOC_ERROR(work= CreateThreadpoolWork(work_callback, this, &callback_environ));
|
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
|
|
|
|
int TP_connection_win::init()
|
|
|
|
{
|
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
|
|
|
return !io || !timer || !work ;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
void TP_connection_win::init_vio(st_vio* vio)
|
|
|
|
{
|
|
|
|
sock.init(vio);
|
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Start asynchronous read
|
|
|
|
*/
|
2016-09-21 14:28:42 +00:00
|
|
|
int TP_connection_win::start_io()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
|
|
|
StartThreadpoolIo(io);
|
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 (sock.begin_read())
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
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
|
|
|
/* Some error occurred */
|
|
|
|
CancelThreadpoolIo(io);
|
|
|
|
return -1;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
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
|
|
|
return 0;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-09-21 14:28:42 +00:00
|
|
|
Recalculate wait timeout, maybe reset timer.
|
2011-12-08 19:17:49 +01:00
|
|
|
*/
|
2016-09-21 14:28:42 +00:00
|
|
|
void TP_connection_win::set_io_timeout(int timeout_sec)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
ulonglong old_timeout= timeout;
|
|
|
|
ulonglong new_timeout = now() + 10000000LL * timeout_sec;
|
2011-12-08 19:17:49 +01:00
|
|
|
|
|
|
|
if (new_timeout < old_timeout)
|
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
SetThreadpoolTimer(timer, (PFILETIME)&new_timeout, 0, 1000);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
/* new_timeout > old_timeout case is handled by expiring timer. */
|
|
|
|
timeout = new_timeout;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
TP_connection_win::~TP_connection_win()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
if (io)
|
|
|
|
CloseThreadpoolIo(io);
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
if (work)
|
|
|
|
CloseThreadpoolWork(work);
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
if (timer)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2019-09-09 13:48:36 +02:00
|
|
|
SetThreadpoolTimer(timer, 0, 0, 0);
|
2016-09-21 14:28:42 +00:00
|
|
|
WaitForThreadpoolTimerCallbacks(timer, TRUE);
|
|
|
|
CloseThreadpoolTimer(timer);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TP_connection_win::wait_begin(int type)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Signal to the threadpool whenever callback can run long. Currently, binlog
|
|
|
|
waits are a good candidate, its waits are really long
|
|
|
|
*/
|
|
|
|
if (type == THD_WAIT_BINLOG)
|
2012-01-27 00:39:23 +01:00
|
|
|
{
|
2018-09-25 16:06:32 +01:00
|
|
|
if (!long_callback && callback_instance)
|
2016-09-21 14:28:42 +00:00
|
|
|
{
|
|
|
|
CallbackMayRunLong(callback_instance);
|
|
|
|
long_callback= true;
|
|
|
|
}
|
2012-01-27 00:39:23 +01:00
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
void TP_connection_win::wait_end()
|
|
|
|
{
|
|
|
|
/* Do we need to do anything ? */
|
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2018-09-25 16:06:32 +01:00
|
|
|
/*
|
|
|
|
This function should be called first whenever a callback is invoked in the
|
2011-12-08 19:17:49 +01:00
|
|
|
threadpool, does my_thread_init() if not yet done
|
|
|
|
*/
|
2018-09-25 16:06:32 +01:00
|
|
|
void tp_win_callback_prolog()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
|
|
|
if (FlsGetValue(fls) == NULL)
|
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
/* Running in new worker thread*/
|
2011-12-08 19:17:49 +01:00
|
|
|
FlsSetValue(fls, (void *)1);
|
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
|
|
|
thread_created++;
|
2020-03-27 02:24:49 +04:00
|
|
|
tp_stats.num_worker_threads++;
|
2016-09-21 14:28:42 +00:00
|
|
|
my_thread_init();
|
2024-06-09 14:09:33 +02:00
|
|
|
my_thread_set_name("worker_thread");
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2018-09-25 16:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern ulong thread_created;
|
|
|
|
static void pre_callback(PVOID context, PTP_CALLBACK_INSTANCE instance)
|
|
|
|
{
|
|
|
|
tp_win_callback_prolog();
|
2016-09-21 14:28:42 +00:00
|
|
|
TP_connection_win *c = (TP_connection_win *)context;
|
|
|
|
c->callback_instance = instance;
|
|
|
|
c->long_callback = false;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2020-05-29 12:21:27 +02:00
|
|
|
Decrement number of threads when a thread exits.
|
2012-01-26 04:35:54 +01:00
|
|
|
On Windows, FlsAlloc() provides the thread destruction callbacks.
|
2011-12-08 19:17:49 +01:00
|
|
|
*/
|
|
|
|
static VOID WINAPI thread_destructor(void *data)
|
|
|
|
{
|
|
|
|
if(data)
|
|
|
|
{
|
2020-03-27 02:24:49 +04:00
|
|
|
tp_stats.num_worker_threads--;
|
2016-09-21 14:28:42 +00:00
|
|
|
my_thread_end();
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static inline void tp_callback(PTP_CALLBACK_INSTANCE instance, PVOID context)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
pre_callback(context, instance);
|
|
|
|
tp_callback((TP_connection *)context);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/*
|
|
|
|
Handle read completion/notification.
|
|
|
|
*/
|
2020-05-29 12:21:27 +02:00
|
|
|
static VOID CALLBACK io_completion_callback(PTP_CALLBACK_INSTANCE instance,
|
2011-12-08 19:17:49 +01:00
|
|
|
PVOID context, PVOID overlapped, ULONG io_result, ULONG_PTR nbytes, PTP_IO io)
|
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
TP_connection_win *c= (TP_connection_win *)context;
|
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
|
|
|
|
|
|
|
/* How many bytes were preread into read buffer */
|
|
|
|
c->sock.end_read((ULONG)nbytes, io_result);
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
/*
|
|
|
|
Execute high priority connections immediately.
|
|
|
|
'Yield' in case of low priority connections, i.e SubmitThreadpoolWork (with the same callback)
|
|
|
|
which makes Windows threadpool place the items at the end of its internal work queue.
|
|
|
|
*/
|
|
|
|
if (c->priority == TP_PRIORITY_HIGH)
|
|
|
|
tp_callback(instance, context);
|
|
|
|
else
|
|
|
|
SubmitThreadpoolWork(c->work);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Timer callback.
|
|
|
|
Invoked when connection times out (wait_timeout)
|
|
|
|
*/
|
2016-09-21 14:28:42 +00:00
|
|
|
static VOID CALLBACK timer_callback(PTP_CALLBACK_INSTANCE instance,
|
2011-12-08 19:17:49 +01:00
|
|
|
PVOID parameter, PTP_TIMER timer)
|
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
TP_connection_win *c = (TP_connection_win *)parameter;
|
|
|
|
if (c->timeout <= now())
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
tp_timeout_handler(c);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
else
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
/*
|
|
|
|
Reset timer.
|
|
|
|
There is a tiny possibility of a race condition, since the value of timeout
|
|
|
|
could have changed to smaller value in the thread doing io callback.
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
Given the relative unimportance of the wait timeout, we accept race
|
2011-12-08 19:17:49 +01:00
|
|
|
condition.
|
2016-09-21 14:28:42 +00:00
|
|
|
*/
|
|
|
|
SetThreadpoolTimer(timer, (PFILETIME)&c->timeout, 0, 1000);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static void CALLBACK work_callback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK work)
|
|
|
|
{
|
|
|
|
tp_callback(instance, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
TP_pool_win::TP_pool_win()
|
|
|
|
{}
|
2016-02-01 12:45:39 +02:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
int TP_pool_win::init()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
fls= FlsAlloc(thread_destructor);
|
|
|
|
pool= CreateThreadpool(NULL);
|
|
|
|
|
|
|
|
if (!pool)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
sql_print_error("Can't create threadpool. "
|
|
|
|
"CreateThreadpool() failed with %d. Likely cause is memory pressure",
|
|
|
|
GetLastError());
|
|
|
|
return -1;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2016-02-01 12:45:39 +02:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
InitializeThreadpoolEnvironment(&callback_environ);
|
|
|
|
SetThreadpoolCallbackPool(&callback_environ, pool);
|
2012-01-27 19:52:53 +01:00
|
|
|
|
2020-07-20 08:08:00 +02:00
|
|
|
if (IS_SYSVAR_AUTOSIZE(&threadpool_max_threads))
|
|
|
|
{
|
|
|
|
/*
|
2024-03-19 11:57:38 +01:00
|
|
|
Default 500 comes from Microsoft documentation,
|
|
|
|
there is no API for GetThreadpoolThreadMaxThreads().
|
|
|
|
|
|
|
|
To avoid deadlocks, allow at least max_connections + safety
|
|
|
|
margin threads in the pool.
|
2020-07-20 08:08:00 +02:00
|
|
|
*/
|
2024-03-19 11:57:38 +01:00
|
|
|
SYSVAR_AUTOSIZE(threadpool_max_threads,std::max(500U,(uint)max_connections + 10));
|
2020-07-20 08:08:00 +02:00
|
|
|
}
|
|
|
|
else
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
SetThreadpoolThreadMaximum(pool, threadpool_max_threads);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
|
2020-07-20 08:08:00 +02:00
|
|
|
if (IS_SYSVAR_AUTOSIZE(&threadpool_min_threads))
|
|
|
|
{
|
|
|
|
SYSVAR_AUTOSIZE(threadpool_min_threads,1);
|
|
|
|
}
|
|
|
|
else
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
if (!SetThreadpoolThreadMinimum(pool, threadpool_min_threads))
|
|
|
|
{
|
|
|
|
tp_log_warning("Can't set threadpool minimum threads",
|
|
|
|
"SetThreadpoolThreadMinimum");
|
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2012-02-17 23:23:54 +01:00
|
|
|
|
2020-07-20 08:08:00 +02:00
|
|
|
|
|
|
|
if (IS_SYSVAR_AUTOSIZE(&global_system_variables.threadpool_priority))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
There is a notable overhead for "auto" priority implementation,
|
|
|
|
use "high" which handles socket IO callbacks as they come
|
|
|
|
without rescheduling to work queue.
|
|
|
|
*/
|
|
|
|
SYSVAR_AUTOSIZE(global_system_variables.threadpool_priority,
|
|
|
|
TP_PRIORITY_HIGH);
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:02:52 +02:00
|
|
|
TP_POOL_STACK_INFORMATION stackinfo;
|
|
|
|
stackinfo.StackCommit = 0;
|
|
|
|
stackinfo.StackReserve = (SIZE_T)my_thread_stack_size;
|
|
|
|
if (!SetThreadpoolStackInformation(pool, &stackinfo))
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2019-06-18 09:02:52 +02:00
|
|
|
tp_log_warning("Can't set threadpool stack size",
|
|
|
|
"SetThreadpoolStackInformation");
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
return 0;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Scheduler callback : Destroy the scheduler.
|
|
|
|
*/
|
|
|
|
TP_pool_win::~TP_pool_win()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
if (!pool)
|
|
|
|
return;
|
|
|
|
DestroyThreadpoolEnvironment(&callback_environ);
|
|
|
|
SetThreadpoolThreadMaximum(pool, 0);
|
|
|
|
CloseThreadpool(pool);
|
|
|
|
if (!tp_stats.num_worker_threads)
|
|
|
|
FlsFree(fls);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2012-01-15 15:41:25 +01:00
|
|
|
/**
|
2016-09-21 14:28:42 +00:00
|
|
|
Sets the number of idle threads the thread pool maintains in anticipation of new
|
|
|
|
requests.
|
2012-01-15 15:41:25 +01:00
|
|
|
*/
|
2016-09-21 14:28:42 +00:00
|
|
|
int TP_pool_win::set_min_threads(uint val)
|
|
|
|
{
|
|
|
|
SetThreadpoolThreadMinimum(pool, val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TP_pool_win::set_max_threads(uint val)
|
2012-01-15 15:41:25 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
SetThreadpoolThreadMaximum(pool, val);
|
2012-01-15 15:41:25 +01:00
|
|
|
return 0;
|
2012-01-27 19:52:53 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
|
|
|
|
TP_connection *TP_pool_win::new_connection(CONNECT *connect)
|
|
|
|
{
|
|
|
|
TP_connection *c= new (std::nothrow) TP_connection_win(connect);
|
|
|
|
if (!c )
|
|
|
|
return 0;
|
|
|
|
if (c->init())
|
|
|
|
{
|
|
|
|
delete c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2021-02-14 18:30:39 +01:00
|
|
|
|