2020-04-27 14:24:41 +03: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 20:49:46 +03:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
2012-02-17 23:27:15 +01:00
|
|
|
|
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 <sql_audit.h>
|
|
|
|
#include <debug_sync.h>
|
2012-01-15 11:17:45 +01:00
|
|
|
#include <threadpool.h>
|
2021-02-14 18:30:39 +01:00
|
|
|
#include <sql_class.h>
|
|
|
|
#include <sql_parse.h>
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2019-08-30 08:42:24 +03:00
|
|
|
#ifdef WITH_WSREP
|
|
|
|
#include "wsrep_trans_observer.h"
|
|
|
|
#endif /* WITH_WSREP */
|
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
|
|
|
#ifdef _WIN32
|
|
|
|
#include "threadpool_winsockets.h"
|
|
|
|
#endif
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/* Threadpool parameters */
|
2011-12-18 20:40:38 +01:00
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
uint threadpool_min_threads;
|
|
|
|
uint threadpool_idle_timeout;
|
|
|
|
uint threadpool_size;
|
2013-11-05 09:18:59 +04:00
|
|
|
uint threadpool_max_size;
|
2011-12-08 19:17:49 +01:00
|
|
|
uint threadpool_stall_limit;
|
|
|
|
uint threadpool_max_threads;
|
2011-12-31 05:24:11 +01:00
|
|
|
uint threadpool_oversubscribe;
|
2016-09-21 14:28:42 +00:00
|
|
|
uint threadpool_mode;
|
|
|
|
uint threadpool_prio_kickup_timer;
|
2019-05-26 13:35:07 +02:00
|
|
|
my_bool threadpool_exact_stats;
|
|
|
|
my_bool threadpool_dedicated_listener;
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2012-01-24 03:23:14 +01:00
|
|
|
/* Stats */
|
|
|
|
TP_STATISTICS tp_stats;
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static void threadpool_remove_connection(THD *thd);
|
2021-02-14 18:30:39 +01:00
|
|
|
static dispatch_command_return threadpool_process_request(THD *thd);
|
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
|
|
|
static THD* threadpool_add_connection(CONNECT *connect, TP_connection *c);
|
2016-09-21 14:28:42 +00:00
|
|
|
|
2012-01-15 11:17:45 +01:00
|
|
|
extern bool do_command(THD*);
|
2012-01-13 15:53:17 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static inline TP_connection *get_TP_connection(THD *thd)
|
|
|
|
{
|
|
|
|
return (TP_connection *)thd->event_scheduler.data;
|
|
|
|
}
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/*
|
2012-01-13 15:53:17 +01:00
|
|
|
Worker threads contexts, and THD contexts.
|
2012-01-15 11:17:45 +01:00
|
|
|
=========================================
|
2020-05-29 12:21:27 +02:00
|
|
|
|
|
|
|
Both worker threads and connections have their sets of thread local variables
|
|
|
|
At the moment it is mysys_var (this has specific data for dbug, my_error and
|
2012-01-15 11:17:45 +01:00
|
|
|
similar goodies), and PSI per-client structure.
|
2012-01-13 15:53:17 +01:00
|
|
|
|
|
|
|
Whenever query is executed following needs to be done:
|
|
|
|
|
|
|
|
1. Save worker thread context.
|
|
|
|
2. Change TLS variables to connection specific ones using thread_attach(THD*).
|
2020-05-29 12:21:27 +02:00
|
|
|
This function does some additional work , e.g setting up
|
2012-01-13 15:53:17 +01:00
|
|
|
thread_stack/thread_ends_here pointers.
|
|
|
|
3. Process query
|
|
|
|
4. Restore worker thread context.
|
|
|
|
|
2020-05-29 12:21:27 +02:00
|
|
|
Connection login and termination follows similar schema w.r.t saving and
|
|
|
|
restoring contexts.
|
2012-01-13 15:53:17 +01:00
|
|
|
|
2020-05-29 12:21:27 +02:00
|
|
|
For both worker thread, and for the connection, mysys variables are created
|
2012-01-13 15:53:17 +01:00
|
|
|
using my_thread_init() and freed with my_thread_end().
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
*/
|
2012-01-13 15:53:17 +01:00
|
|
|
struct Worker_thread_context
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2012-01-13 15:53:17 +01:00
|
|
|
PSI_thread *psi_thread;
|
|
|
|
st_my_thread_var* mysys_var;
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2022-10-11 00:07:38 +02:00
|
|
|
Worker_thread_context()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2020-04-27 14:24:41 +03:00
|
|
|
psi_thread= PSI_CALL_get_thread();
|
2020-04-07 13:14:41 +03:00
|
|
|
mysys_var= my_thread_var;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 00:07:38 +02:00
|
|
|
~Worker_thread_context()
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2018-01-03 18:51:24 +01:00
|
|
|
PSI_CALL_set_thread(psi_thread);
|
2020-04-07 13:14:41 +03:00
|
|
|
set_mysys_var(mysys_var);
|
2020-04-30 20:06:26 +03:00
|
|
|
set_current_thd(nullptr);
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2012-01-13 15:53:17 +01:00
|
|
|
};
|
2011-12-08 19:17:49 +01:00
|
|
|
|
|
|
|
|
2016-09-28 14:16:38 +00:00
|
|
|
#ifdef HAVE_PSI_INTERFACE
|
|
|
|
|
|
|
|
/*
|
|
|
|
The following fixes PSI "idle" psi instrumentation.
|
|
|
|
The server assumes that connection becomes idle
|
|
|
|
just before net_read_packet() and switches to active after it.
|
|
|
|
In out setup, server becomes idle when async socket io is made.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern void net_before_header_psi(struct st_net *net, void *user_data, size_t);
|
|
|
|
|
|
|
|
static void dummy_before_header(struct st_net *, void *, size_t)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void re_init_net_server_extension(THD *thd)
|
|
|
|
{
|
|
|
|
thd->m_net_server_extension.m_before_header = dummy_before_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define re_init_net_server_extension(thd)
|
|
|
|
|
|
|
|
#endif /* HAVE_PSI_INTERFACE */
|
|
|
|
|
2023-09-27 17:57:24 +02:00
|
|
|
static inline bool has_unread_compressed_data(const NET *net)
|
|
|
|
{
|
|
|
|
return net->compress && net->remain_in_buf;
|
|
|
|
}
|
2016-09-28 14:16:38 +00:00
|
|
|
|
|
|
|
static inline void set_thd_idle(THD *thd)
|
|
|
|
{
|
|
|
|
thd->net.reading_or_writing= 1;
|
|
|
|
#ifdef HAVE_PSI_INTERFACE
|
2023-09-27 17:57:24 +02:00
|
|
|
if (!has_unread_compressed_data(&thd->net))
|
|
|
|
net_before_header_psi(&thd->net, thd, 0);
|
2016-09-28 14:16:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:07:38 +02:00
|
|
|
/*
|
|
|
|
Per OS thread info (ID and pthread_self)
|
|
|
|
stored as TLS, because of syscall overhead
|
|
|
|
(on Linux)
|
|
|
|
*/
|
|
|
|
struct OS_thread_info
|
|
|
|
{
|
2024-11-05 18:12:05 +01:00
|
|
|
void *stack_start;
|
|
|
|
void *stack_end;
|
2022-10-11 00:07:38 +02:00
|
|
|
pthread_t self;
|
|
|
|
uint32_t thread_id;
|
2024-11-05 18:12:05 +01:00
|
|
|
inline bool initialized() { return stack_start != 0; }
|
2022-10-11 00:07:38 +02:00
|
|
|
|
2024-11-05 18:12:05 +01:00
|
|
|
void init()
|
2022-10-11 00:07:38 +02:00
|
|
|
{
|
|
|
|
#if _WIN32
|
2024-11-05 18:12:05 +01:00
|
|
|
self= thread_id= GetCurrentThreadId();
|
2022-10-11 00:07:38 +02:00
|
|
|
#else
|
|
|
|
#ifdef __NR_gettid
|
|
|
|
thread_id= (uint32) syscall(__NR_gettid);
|
|
|
|
#else
|
|
|
|
thread_id= 0;
|
|
|
|
#endif
|
|
|
|
self= pthread_self();
|
|
|
|
#endif
|
2024-11-05 18:12:05 +01:00
|
|
|
char stack_var;
|
|
|
|
my_get_stack_bounds(&stack_start, &stack_end, &stack_var, my_thread_stack_size);
|
|
|
|
DBUG_ASSERT(stack_start);
|
|
|
|
DBUG_ASSERT(stack_end);
|
2022-10-11 00:07:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
static thread_local OS_thread_info os_thread_info;
|
|
|
|
|
|
|
|
static const OS_thread_info *get_os_thread_info()
|
|
|
|
{
|
|
|
|
auto *res= &os_thread_info;
|
|
|
|
if (!res->initialized())
|
2024-11-05 18:12:05 +01:00
|
|
|
res->init();
|
2022-10-11 00:07:38 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
/*
|
2012-01-13 15:53:17 +01:00
|
|
|
Attach/associate the connection with the OS thread,
|
2011-12-08 19:17:49 +01:00
|
|
|
*/
|
2016-03-08 10:28:26 +01:00
|
|
|
static void thread_attach(THD* thd)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2019-08-30 08:42:24 +03:00
|
|
|
#ifdef WITH_WSREP
|
|
|
|
/* Wait until possible background rollback has finished before
|
|
|
|
attaching the thd. */
|
|
|
|
wsrep_wait_rollback_complete_and_acquire_ownership(thd);
|
|
|
|
#endif /* WITH_WSREP */
|
2020-04-07 13:14:41 +03:00
|
|
|
set_mysys_var(thd->mysys_var);
|
2024-10-29 14:20:03 +01:00
|
|
|
const OS_thread_info *tinfo= get_os_thread_info();
|
|
|
|
|
2022-10-11 00:07:38 +02:00
|
|
|
set_current_thd(thd);
|
2024-11-05 18:12:05 +01:00
|
|
|
thd->thread_stack= tinfo->stack_start;
|
|
|
|
thd->mysys_var->stack_ends_here= tinfo->stack_end;
|
2022-10-11 00:07:38 +02:00
|
|
|
thd->real_id= tinfo->self;
|
|
|
|
thd->os_thread_id= tinfo->thread_id;
|
2020-03-07 15:12:02 +01:00
|
|
|
PSI_CALL_set_thread(thd->get_psi());
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
/*
|
2020-05-29 12:21:27 +02:00
|
|
|
Determine connection priority , using current
|
2016-09-21 14:28:42 +00:00
|
|
|
transaction state and 'threadpool_priority' variable value.
|
|
|
|
*/
|
|
|
|
static TP_PRIORITY get_priority(TP_connection *c)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(c->thd == current_thd);
|
|
|
|
TP_PRIORITY prio= (TP_PRIORITY)c->thd->variables.threadpool_priority;
|
|
|
|
if (prio == TP_PRIORITY_AUTO)
|
2020-05-04 14:20:14 +03:00
|
|
|
prio= c->thd->transaction->is_active() ? TP_PRIORITY_HIGH : TP_PRIORITY_LOW;
|
2019-05-26 13:25:12 +02:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
return prio;
|
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
|
|
|
|
void tp_callback(TP_connection *c)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2016-09-21 14:28:42 +00:00
|
|
|
DBUG_ASSERT(c);
|
2016-03-08 10:28:26 +01:00
|
|
|
|
2012-01-13 15:53:17 +01:00
|
|
|
Worker_thread_context worker_context;
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
THD *thd= c->thd;
|
|
|
|
|
|
|
|
c->state = TP_STATE_RUNNING;
|
|
|
|
|
2018-04-04 12:16:12 +03:00
|
|
|
if (unlikely(!thd))
|
2016-09-21 14:28:42 +00:00
|
|
|
{
|
|
|
|
/* No THD, need to login first. */
|
|
|
|
DBUG_ASSERT(c->connect);
|
|
|
|
thd= c->thd= threadpool_add_connection(c->connect, c);
|
|
|
|
if (!thd)
|
|
|
|
{
|
|
|
|
/* Bail out on connect error.*/
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
c->connect= 0;
|
|
|
|
}
|
2021-02-14 18:30:39 +01:00
|
|
|
else
|
2016-09-21 14:28:42 +00:00
|
|
|
{
|
2021-02-14 18:30:39 +01:00
|
|
|
retry:
|
|
|
|
switch(threadpool_process_request(thd))
|
|
|
|
{
|
|
|
|
case DISPATCH_COMMAND_WOULDBLOCK:
|
|
|
|
if (!thd->async_state.try_suspend())
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
All async operations finished meanwhile, thus nobody is will wake up
|
|
|
|
this THD. Therefore, we'll resume "manually" here.
|
|
|
|
*/
|
|
|
|
thd->async_state.m_state = thd_async_state::enum_async_state::RESUMED;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case DISPATCH_COMMAND_CLOSE_CONNECTION:
|
|
|
|
/* QUIT or an error occurred. */
|
|
|
|
goto error;
|
|
|
|
case DISPATCH_COMMAND_SUCCESS:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
thd->async_state.m_state= thd_async_state::enum_async_state::NONE;
|
2016-09-21 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set priority */
|
|
|
|
c->priority= get_priority(c);
|
|
|
|
|
|
|
|
/* Read next command from client. */
|
2017-02-21 16:28:42 +04:00
|
|
|
c->set_io_timeout(thd->get_net_wait_timeout());
|
2016-09-21 14:28:42 +00:00
|
|
|
c->state= TP_STATE_IDLE;
|
|
|
|
if (c->start_io())
|
|
|
|
goto error;
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
c->thd= 0;
|
|
|
|
if (thd)
|
|
|
|
{
|
|
|
|
threadpool_remove_connection(thd);
|
|
|
|
}
|
2020-05-24 17:13:12 +02:00
|
|
|
delete c;
|
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
|
|
|
static THD *threadpool_add_connection(CONNECT *connect, TP_connection *c)
|
2016-09-21 14:28:42 +00:00
|
|
|
{
|
|
|
|
THD *thd= NULL;
|
|
|
|
|
2012-01-13 15:53:17 +01:00
|
|
|
/*
|
2012-01-15 11:17:45 +01:00
|
|
|
Create a new connection context: mysys_thread_var and PSI thread
|
|
|
|
Store them in THD.
|
2012-01-13 15:53:17 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-07 13:14:41 +03:00
|
|
|
set_mysys_var(NULL);
|
2012-01-13 15:53:17 +01:00
|
|
|
my_thread_init();
|
2020-04-07 13:14:41 +03:00
|
|
|
st_my_thread_var* mysys_var= my_thread_var;
|
2020-03-07 15:12:02 +01:00
|
|
|
PSI_CALL_set_thread(PSI_CALL_new_thread(key_thread_one_connection, connect, 0));
|
2016-04-07 19:51:40 +03:00
|
|
|
if (!mysys_var ||!(thd= connect->create_thd(NULL)))
|
2012-01-13 15:53:17 +01:00
|
|
|
{
|
|
|
|
/* Out of memory? */
|
2024-09-19 10:55:25 +02:00
|
|
|
connect->close_and_delete(0);
|
2016-03-08 10:28:26 +01:00
|
|
|
if (mysys_var)
|
|
|
|
my_thread_end();
|
|
|
|
return NULL;
|
2012-01-13 15:53:17 +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
|
|
|
|
2020-07-03 21:49:45 +02:00
|
|
|
thd->event_scheduler.data= c;
|
2021-07-14 18:03:53 +02:00
|
|
|
server_threads.insert(thd); // Make THD visible in show processlist
|
|
|
|
delete connect; // must be after server_threads.insert, see close_connections()
|
2017-12-07 21:28:00 +02:00
|
|
|
thd->set_mysys_var(mysys_var);
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2012-01-13 15:53:17 +01:00
|
|
|
/* Login. */
|
|
|
|
thread_attach(thd);
|
2023-09-22 13:37:07 +02:00
|
|
|
mysql_socket_set_thread_owner(thd->net.vio->mysql_socket);
|
2016-09-28 14:16:38 +00:00
|
|
|
re_init_net_server_extension(thd);
|
2012-01-13 15:53:17 +01:00
|
|
|
ulonglong now= microsecond_interval_timer();
|
|
|
|
thd->prior_thr_create_utime= now;
|
|
|
|
thd->start_utime= now;
|
|
|
|
thd->thr_create_utime= now;
|
|
|
|
|
2020-04-30 20:06:26 +03:00
|
|
|
setup_connection_thread_globals(thd);
|
2016-09-28 14:16:38 +00:00
|
|
|
|
|
|
|
if (thd_prepare_connection(thd))
|
|
|
|
goto end;
|
|
|
|
|
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
|
|
|
c->init_vio(thd->net.vio);
|
|
|
|
|
2016-09-28 14:16:38 +00:00
|
|
|
/*
|
|
|
|
Check if THD is ok, as prepare_new_connection_state()
|
|
|
|
can fail, for example if init command failed.
|
|
|
|
*/
|
|
|
|
if (!thd_is_connection_alive(thd))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
thd->skip_wait_timeout= true;
|
|
|
|
set_thd_idle(thd);
|
2016-03-08 10:28:26 +01:00
|
|
|
return thd;
|
2016-09-28 14:16:38 +00:00
|
|
|
|
|
|
|
end:
|
2016-12-29 13:23:18 +01:00
|
|
|
threadpool_remove_connection(thd);
|
|
|
|
return NULL;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2012-01-15 11:17:45 +01:00
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static void threadpool_remove_connection(THD *thd)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2012-01-13 15:53:17 +01:00
|
|
|
thread_attach(thd);
|
2016-03-09 16:42:45 +01:00
|
|
|
thd->net.reading_or_writing = 0;
|
|
|
|
end_connection(thd);
|
|
|
|
close_connection(thd, 0);
|
|
|
|
unlink_thd(thd);
|
2020-03-07 15:12:02 +01:00
|
|
|
PSI_CALL_delete_current_thread(); // before THD is destroyed
|
2016-04-07 19:51:40 +03:00
|
|
|
delete thd;
|
2016-03-09 16:42:45 +01:00
|
|
|
|
2012-01-15 11:17:45 +01:00
|
|
|
/*
|
2020-05-29 12:21:27 +02:00
|
|
|
Free resources associated with this connection:
|
2012-01-15 11:17:45 +01:00
|
|
|
mysys thread_var and PSI thread.
|
|
|
|
*/
|
2012-01-13 15:53:17 +01:00
|
|
|
my_thread_end();
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 14:37:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Ensure that proper error message is sent to client,
|
|
|
|
and "aborted" message appears in the log in case of
|
|
|
|
wait timeout.
|
|
|
|
|
|
|
|
See also timeout handling in net_serv.cc
|
|
|
|
*/
|
|
|
|
static void handle_wait_timeout(THD *thd)
|
|
|
|
{
|
|
|
|
thd->get_stmt_da()->reset_diagnostics_area();
|
|
|
|
thd->reset_killed();
|
|
|
|
my_error(ER_NET_READ_INTERRUPTED, MYF(0));
|
|
|
|
thd->net.last_errno= ER_NET_READ_INTERRUPTED;
|
|
|
|
thd->net.error= 2;
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:24:53 +02:00
|
|
|
/** Check if some client data is cached in thd->net or thd->net.vio */
|
|
|
|
static bool has_unread_data(THD* thd)
|
|
|
|
{
|
|
|
|
NET *net= &thd->net;
|
|
|
|
Vio *vio= net->vio;
|
2023-09-27 17:57:24 +02:00
|
|
|
return vio->has_data(vio) || has_unread_compressed_data(net);
|
2020-10-03 00:24:53 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 14:37:16 +00:00
|
|
|
|
2012-02-17 23:27:15 +01:00
|
|
|
/**
|
|
|
|
Process a single client request or a single batch.
|
|
|
|
*/
|
2021-02-14 18:30:39 +01:00
|
|
|
static dispatch_command_return threadpool_process_request(THD *thd)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2021-02-14 18:30:39 +01:00
|
|
|
dispatch_command_return retval= DISPATCH_COMMAND_SUCCESS;
|
|
|
|
|
2012-01-13 15:53:17 +01:00
|
|
|
thread_attach(thd);
|
2021-02-14 18:30:39 +01:00
|
|
|
if(thd->async_state.m_state == thd_async_state::enum_async_state::RESUMED)
|
|
|
|
goto resume;
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2011-12-27 16:10:34 +01:00
|
|
|
if (thd->killed >= KILL_CONNECTION)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
2020-05-29 12:21:27 +02:00
|
|
|
/*
|
|
|
|
killed flag was set by timeout handler
|
2012-01-15 11:17:45 +01:00
|
|
|
or KILL command. Return error.
|
2011-12-08 19:17:49 +01:00
|
|
|
*/
|
2021-02-14 18:30:39 +01:00
|
|
|
retval= DISPATCH_COMMAND_CLOSE_CONNECTION;
|
2017-08-30 14:37:16 +00:00
|
|
|
if(thd->killed == KILL_WAIT_TIMEOUT)
|
|
|
|
handle_wait_timeout(thd);
|
2012-02-16 16:59:04 +01:00
|
|
|
goto end;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2012-02-16 16:59:04 +01:00
|
|
|
|
2012-02-17 23:27:15 +01:00
|
|
|
/*
|
2016-02-01 12:45:39 +02:00
|
|
|
In the loop below, the flow is essentially the copy of
|
|
|
|
thead-per-connections
|
2012-02-17 23:27:15 +01:00
|
|
|
logic, see do_handle_one_connection() in sql_connect.c
|
|
|
|
|
|
|
|
The goal is to execute a single query, thus the loop is normally executed
|
|
|
|
only once. However for SSL connections, it can be executed multiple times
|
|
|
|
(SSL can preread and cache incoming data, and vio->has_data() checks if it
|
|
|
|
was the case).
|
|
|
|
*/
|
2011-12-08 19:17:49 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
thd->net.reading_or_writing= 0;
|
2019-06-15 01:02:55 +04:00
|
|
|
if (mysql_audit_release_required(thd))
|
|
|
|
mysql_audit_release(thd);
|
2011-12-08 19:17:49 +01:00
|
|
|
|
2021-02-14 18:30:39 +01:00
|
|
|
resume:
|
|
|
|
retval= do_command(thd, false);
|
|
|
|
switch(retval)
|
|
|
|
{
|
|
|
|
case DISPATCH_COMMAND_WOULDBLOCK:
|
|
|
|
case DISPATCH_COMMAND_CLOSE_CONNECTION:
|
|
|
|
goto end;
|
|
|
|
case DISPATCH_COMMAND_SUCCESS:
|
|
|
|
break;
|
|
|
|
}
|
2011-12-08 19:17:49 +01:00
|
|
|
|
|
|
|
if (!thd_is_connection_alive(thd))
|
|
|
|
{
|
2021-02-14 18:30:39 +01:00
|
|
|
retval=DISPATCH_COMMAND_CLOSE_CONNECTION;
|
2012-02-16 16:59:04 +01:00
|
|
|
goto end;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-28 14:16:38 +00:00
|
|
|
set_thd_idle(thd);
|
|
|
|
|
2020-10-03 00:24:53 +02:00
|
|
|
if (!has_unread_data(thd))
|
2021-02-14 18:30:39 +01:00
|
|
|
{
|
2012-01-15 11:17:45 +01:00
|
|
|
/* More info on this debug sync is in sql_parse.cc*/
|
2011-12-08 19:17:49 +01:00
|
|
|
DEBUG_SYNC(thd, "before_do_command_net_read");
|
2012-02-16 16:59:04 +01:00
|
|
|
goto end;
|
2011-12-08 19:17:49 +01:00
|
|
|
}
|
2012-02-16 16:59:04 +01:00
|
|
|
}
|
2012-01-13 15:53:17 +01:00
|
|
|
|
2012-02-16 16:59:04 +01:00
|
|
|
end:
|
2011-12-08 19:17:49 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-21 14:28:42 +00:00
|
|
|
static TP_pool *pool;
|
|
|
|
|
|
|
|
static bool tp_init()
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (threadpool_mode == TP_MODE_WINDOWS)
|
|
|
|
pool= new (std::nothrow) TP_pool_win;
|
|
|
|
else
|
|
|
|
pool= new (std::nothrow) TP_pool_generic;
|
|
|
|
#else
|
|
|
|
pool= new (std::nothrow) TP_pool_generic;
|
|
|
|
#endif
|
|
|
|
if (!pool)
|
|
|
|
return true;
|
|
|
|
if (pool->init())
|
|
|
|
{
|
|
|
|
delete pool;
|
|
|
|
pool= 0;
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
#ifdef _WIN32
|
|
|
|
init_win_aio_buffers(max_connections);
|
|
|
|
#endif
|
2016-09-21 14:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tp_add_connection(CONNECT *connect)
|
|
|
|
{
|
|
|
|
TP_connection *c= pool->new_connection(connect);
|
|
|
|
DBUG_EXECUTE_IF("simulate_failed_connection_1", delete c ; c= 0;);
|
|
|
|
if (c)
|
|
|
|
pool->add(c);
|
|
|
|
else
|
2024-09-19 10:55:25 +02:00
|
|
|
connect->close_and_delete(0);
|
2016-09-21 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tp_get_idle_thread_count()
|
|
|
|
{
|
|
|
|
return pool? pool->get_idle_thread_count(): 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tp_get_thread_count()
|
|
|
|
{
|
|
|
|
return pool ? pool->get_thread_count() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tp_set_min_threads(uint val)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
pool->set_min_threads(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tp_set_max_threads(uint val)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
pool->set_max_threads(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tp_set_threadpool_size(uint val)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
pool->set_pool_size(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tp_set_threadpool_stall_limit(uint val)
|
|
|
|
{
|
|
|
|
if (pool)
|
|
|
|
pool->set_stall_limit(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tp_timeout_handler(TP_connection *c)
|
|
|
|
{
|
|
|
|
if (c->state != TP_STATE_IDLE)
|
|
|
|
return;
|
2020-07-28 15:59:38 +02:00
|
|
|
THD *thd= c->thd;
|
2017-12-07 21:28:00 +02:00
|
|
|
mysql_mutex_lock(&thd->LOCK_thd_kill);
|
2020-07-28 15:59:38 +02:00
|
|
|
Vio *vio= thd->net.vio;
|
|
|
|
if (vio && (vio_pending(vio) > 0 || vio->has_data(vio)) &&
|
|
|
|
c->state == TP_STATE_IDLE)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
There is some data on that connection, i.e
|
|
|
|
i.e there was no inactivity timeout.
|
|
|
|
Don't kill.
|
|
|
|
*/
|
|
|
|
c->state= TP_STATE_PENDING;
|
|
|
|
}
|
|
|
|
else if (c->state == TP_STATE_IDLE)
|
|
|
|
{
|
|
|
|
thd->set_killed_no_mutex(KILL_WAIT_TIMEOUT);
|
|
|
|
c->priority= TP_PRIORITY_HIGH;
|
|
|
|
post_kill_notification(thd);
|
|
|
|
}
|
2017-12-07 21:28:00 +02:00
|
|
|
mysql_mutex_unlock(&thd->LOCK_thd_kill);
|
2016-09-21 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 13:25:12 +02:00
|
|
|
MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE) Atomic_counter<unsigned long long> tp_waits[THD_WAIT_LAST];
|
2016-09-21 14:28:42 +00:00
|
|
|
|
|
|
|
static void tp_wait_begin(THD *thd, int type)
|
|
|
|
{
|
|
|
|
TP_connection *c = get_TP_connection(thd);
|
|
|
|
if (c)
|
2019-05-26 13:25:12 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(type > 0 && type < THD_WAIT_LAST);
|
|
|
|
tp_waits[type]++;
|
2016-09-21 14:28:42 +00:00
|
|
|
c->wait_begin(type);
|
2019-05-26 13:25:12 +02:00
|
|
|
}
|
2016-09-21 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void tp_wait_end(THD *thd)
|
|
|
|
{
|
|
|
|
TP_connection *c = get_TP_connection(thd);
|
|
|
|
if (c)
|
|
|
|
c->wait_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void tp_end()
|
|
|
|
{
|
|
|
|
delete pool;
|
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
|
|
|
#ifdef _WIN32
|
|
|
|
destroy_win_aio_buffers();
|
|
|
|
#endif
|
2016-09-21 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tp_post_kill_notification(THD *thd)
|
|
|
|
{
|
|
|
|
TP_connection *c= get_TP_connection(thd);
|
|
|
|
if (c)
|
|
|
|
c->priority= TP_PRIORITY_HIGH;
|
|
|
|
post_kill_notification(thd);
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:30:39 +01:00
|
|
|
/* Resume previously suspended THD */
|
|
|
|
static void tp_resume(THD* thd)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(thd->async_state.m_state == thd_async_state::enum_async_state::SUSPENDED);
|
|
|
|
thd->async_state.m_state = thd_async_state::enum_async_state::RESUMED;
|
|
|
|
TP_connection* c = get_TP_connection(thd);
|
|
|
|
pool->resume(c);
|
|
|
|
}
|
|
|
|
|
2011-12-08 19:17:49 +01:00
|
|
|
static scheduler_functions tp_scheduler_functions=
|
|
|
|
{
|
|
|
|
0, // max_threads
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
tp_init, // init
|
|
|
|
tp_add_connection, // add_connection
|
|
|
|
tp_wait_begin, // thd_wait_begin
|
|
|
|
tp_wait_end, // thd_wait_end
|
2016-09-21 14:28:42 +00:00
|
|
|
tp_post_kill_notification, // post kill notification
|
2021-02-14 18:30:39 +01:00
|
|
|
tp_end, // end
|
|
|
|
tp_resume
|
2011-12-08 19:17:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void pool_of_threads_scheduler(struct scheduler_functions *func,
|
|
|
|
ulong *arg_max_connections,
|
2019-05-19 17:00:31 +04:00
|
|
|
Atomic_counter<uint> *arg_connection_count)
|
2011-12-08 19:17:49 +01:00
|
|
|
{
|
|
|
|
*func = tp_scheduler_functions;
|
2012-01-15 11:17:45 +01:00
|
|
|
func->max_threads= threadpool_max_threads;
|
2011-12-08 19:17:49 +01:00
|
|
|
func->max_connections= arg_max_connections;
|
|
|
|
func->connection_count= arg_connection_count;
|
|
|
|
scheduler_init();
|
|
|
|
}
|