1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-01 03:47:19 +03: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.
This commit is contained in:
Vladislav Vaintroub
2020-06-26 14:43:56 +02:00
parent b3acad4a48
commit d15c839c0d
9 changed files with 449 additions and 169 deletions

View File

@ -29,8 +29,8 @@
#include <sql_plist.h>
#include <threadpool.h>
#include <algorithm>
#ifdef HAVE_IOCP
#ifdef _WIN32
#include "threadpool_winsockets.h"
#define OPTIONAL_IO_POLL_READ_PARAM this
#else
#define OPTIONAL_IO_POLL_READ_PARAM 0
@ -348,7 +348,7 @@ static void* native_event_get_userdata(native_event *event)
return event->portev_user;
}
#elif defined(HAVE_IOCP)
#elif defined(_WIN32)
static TP_file_handle io_poll_create()
@ -359,29 +359,8 @@ static TP_file_handle io_poll_create()
int io_poll_start_read(TP_file_handle pollfd, TP_file_handle fd, void *, void *opt)
{
static char c;
TP_connection_generic *con= (TP_connection_generic *)opt;
OVERLAPPED *overlapped= &con->overlapped;
if (con->vio_type == VIO_TYPE_NAMEDPIPE)
{
if (ReadFile(fd, &c, 0, NULL, overlapped))
return 0;
}
else
{
WSABUF buf;
buf.buf= &c;
buf.len= 0;
DWORD flags=0;
if (WSARecv((SOCKET)fd, &buf, 1,NULL, &flags,overlapped, NULL) == 0)
return 0;
}
if (GetLastError() == ERROR_IO_PENDING)
return 0;
return 1;
auto c= (TP_connection_generic *) opt;
return (int) c->win_sock.begin_read();
}
@ -401,20 +380,33 @@ int io_poll_disassociate_fd(TP_file_handle pollfd, TP_file_handle fd)
}
int io_poll_wait(TP_file_handle pollfd, native_event *events, int maxevents, int timeout_ms)
static void *native_event_get_userdata(native_event *event)
{
return (void *) event->lpCompletionKey;
}
int io_poll_wait(TP_file_handle pollfd, native_event *events, int maxevents,
int timeout_ms)
{
ULONG n;
BOOL ok = GetQueuedCompletionStatusEx(pollfd, events,
maxevents, &n, timeout_ms, FALSE);
return ok ? (int)n : -1;
if (!GetQueuedCompletionStatusEx(pollfd, events, maxevents, &n, timeout_ms, FALSE))
return -1;
/* Update win_sock with number of bytes read.*/
for (ULONG i= 0; i < n; i++)
{
auto ev= &events[i];
auto c= (TP_connection_generic *) native_event_get_userdata(ev);
/* null userdata zero means shutdown (see PostQueuedCompletionStatus() usage*/
if (c)
{
c->win_sock.end_read(ev->dwNumberOfBytesTransferred, 0);
}
}
return (int) n;
}
static void* native_event_get_userdata(native_event *event)
{
return (void *)event->lpCompletionKey;
}
#endif
@ -969,7 +961,7 @@ void thread_group_destroy(thread_group_t *thread_group)
io_poll_close(thread_group->pollfd);
thread_group->pollfd= INVALID_HANDLE_VALUE;
}
#ifndef HAVE_IOCP
#ifndef _WIN32
for(int i=0; i < 2; i++)
{
if(thread_group->shutdown_pipe[i] != -1)
@ -1013,7 +1005,7 @@ static int wake_thread(thread_group_t *thread_group,bool due_to_stall)
*/
static int wake_listener(thread_group_t *thread_group)
{
#ifndef HAVE_IOCP
#ifndef _WIN32
if (pipe(thread_group->shutdown_pipe))
{
return -1;
@ -1354,9 +1346,6 @@ TP_connection_generic::TP_connection_generic(CONNECT *c):
abs_wait_timeout(ULONGLONG_MAX),
bound_to_poll_descriptor(false),
waiting(false)
#ifdef HAVE_IOCP
, overlapped()
#endif
{
DBUG_ASSERT(c->vio_type != VIO_CLOSED);