2020-03-30 17:52:17 +02:00
|
|
|
/* Copyright(C) 2019, 2020, MariaDB
|
2019-05-26 13:25:12 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute itand /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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 - 1301 USA*/
|
|
|
|
|
|
|
|
#if defined (HAVE_POOL_OF_THREADS)
|
|
|
|
#include <my_global.h>
|
|
|
|
#include <sql_plist.h>
|
|
|
|
#include <my_pthread.h>
|
|
|
|
#include <mysqld.h>
|
2020-03-30 17:52:17 +02:00
|
|
|
#include <threadpool.h>
|
2019-05-26 13:25:12 +02:00
|
|
|
#include <violite.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
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"
|
2019-05-26 13:25:12 +02:00
|
|
|
/* AIX may define this, too ?*/
|
2020-05-29 12:21:27 +02:00
|
|
|
#define HAVE_IOCP
|
2019-05-26 13:25:12 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
typedef HANDLE TP_file_handle;
|
|
|
|
#else
|
|
|
|
typedef int TP_file_handle;
|
|
|
|
#define INVALID_HANDLE_VALUE -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
typedef struct epoll_event native_event;
|
|
|
|
#elif defined(HAVE_KQUEUE)
|
|
|
|
#include <sys/event.h>
|
|
|
|
typedef struct kevent native_event;
|
|
|
|
#elif defined (__sun)
|
|
|
|
#include <port.h>
|
|
|
|
typedef port_event_t native_event;
|
|
|
|
#elif defined (HAVE_IOCP)
|
|
|
|
typedef OVERLAPPED_ENTRY native_event;
|
|
|
|
#else
|
|
|
|
#error threadpool is not available on this platform
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct thread_group_t;
|
|
|
|
|
|
|
|
/* Per-thread structure for workers */
|
|
|
|
struct worker_thread_t
|
|
|
|
{
|
|
|
|
ulonglong event_count; /* number of request handled by this thread */
|
|
|
|
thread_group_t* thread_group;
|
|
|
|
worker_thread_t* next_in_list;
|
|
|
|
worker_thread_t** prev_in_list;
|
|
|
|
mysql_cond_t cond;
|
|
|
|
bool woken;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef I_P_List<worker_thread_t, I_P_List_adapter<worker_thread_t,
|
|
|
|
& worker_thread_t::next_in_list,
|
|
|
|
& worker_thread_t::prev_in_list>,
|
|
|
|
I_P_List_counter
|
|
|
|
>
|
|
|
|
worker_list_t;
|
|
|
|
|
|
|
|
struct TP_connection_generic :public TP_connection
|
|
|
|
{
|
|
|
|
TP_connection_generic(CONNECT* c);
|
|
|
|
~TP_connection_generic();
|
|
|
|
|
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 { return 0; }
|
|
|
|
void set_io_timeout(int sec) override;
|
|
|
|
int start_io() override;
|
|
|
|
void wait_begin(int type) override;
|
|
|
|
void wait_end() override;
|
2019-05-26 13:25:12 +02:00
|
|
|
|
|
|
|
thread_group_t* thread_group;
|
|
|
|
TP_connection_generic* next_in_queue;
|
|
|
|
TP_connection_generic** prev_in_queue;
|
|
|
|
ulonglong abs_wait_timeout;
|
|
|
|
ulonglong enqueue_time;
|
|
|
|
TP_file_handle fd;
|
|
|
|
bool bound_to_poll_descriptor;
|
|
|
|
int waiting;
|
2020-05-23 13:17:50 +02:00
|
|
|
bool fix_group;
|
2019-05-26 13:25:12 +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
|
|
|
win_aiosocket win_sock{};
|
|
|
|
void init_vio(st_vio *vio) override
|
|
|
|
{ win_sock.init(vio);}
|
2019-05-26 13:25:12 +02:00
|
|
|
#endif
|
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
|
|
|
|
2019-05-26 13:25:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef I_P_List<TP_connection_generic,
|
|
|
|
I_P_List_adapter<TP_connection_generic,
|
|
|
|
& TP_connection_generic::next_in_queue,
|
|
|
|
& TP_connection_generic::prev_in_queue>,
|
|
|
|
I_P_List_counter,
|
|
|
|
I_P_List_fast_push_back<TP_connection_generic> >
|
|
|
|
connection_queue_t;
|
|
|
|
|
|
|
|
const int NQUEUES = 2; /* We have high and low priority queues*/
|
|
|
|
|
2020-03-28 01:46:53 +01:00
|
|
|
enum class operation_origin
|
|
|
|
{
|
|
|
|
WORKER,
|
|
|
|
LISTENER
|
|
|
|
};
|
|
|
|
|
2019-05-26 13:25:12 +02:00
|
|
|
struct thread_group_counters_t
|
|
|
|
{
|
|
|
|
ulonglong thread_creations;
|
|
|
|
ulonglong thread_creations_due_to_stall;
|
|
|
|
ulonglong wakes;
|
|
|
|
ulonglong wakes_due_to_stall;
|
|
|
|
ulonglong throttles;
|
|
|
|
ulonglong stalls;
|
2020-03-28 01:46:53 +01:00
|
|
|
ulonglong dequeues[2];
|
|
|
|
ulonglong polls[2];
|
2019-05-26 13:25:12 +02:00
|
|
|
};
|
|
|
|
|
2020-05-15 16:09:13 +02:00
|
|
|
struct thread_group_t
|
2019-05-26 13:25:12 +02:00
|
|
|
{
|
|
|
|
mysql_mutex_t mutex;
|
|
|
|
connection_queue_t queues[NQUEUES];
|
|
|
|
worker_list_t waiting_threads;
|
|
|
|
worker_thread_t* listener;
|
|
|
|
pthread_attr_t* pthread_attr;
|
|
|
|
TP_file_handle pollfd;
|
|
|
|
int thread_count;
|
|
|
|
int active_thread_count;
|
|
|
|
int connection_count;
|
|
|
|
/* Stats for the deadlock detection timer routine.*/
|
|
|
|
int io_event_count;
|
|
|
|
int queue_event_count;
|
|
|
|
ulonglong last_thread_creation_time;
|
|
|
|
int shutdown_pipe[2];
|
|
|
|
bool shutdown;
|
|
|
|
bool stalled;
|
|
|
|
thread_group_counters_t counters;
|
2020-05-15 16:09:13 +02:00
|
|
|
char pad[CPU_LEVEL1_DCACHE_LINESIZE];
|
2019-05-26 13:25:12 +02:00
|
|
|
};
|
|
|
|
|
2020-03-28 01:46:53 +01:00
|
|
|
#define TP_INCREMENT_GROUP_COUNTER(group,var) do {group->counters.var++;}while(0)
|
2019-05-26 13:25:12 +02:00
|
|
|
|
|
|
|
extern thread_group_t* all_groups;
|
|
|
|
#endif
|
|
|
|
|