mirror of
https://github.com/MariaDB/server.git
synced 2025-08-29 00:08:14 +03:00
This is a backport of code from MySQL 6.0 with cleanups and extensions The following new options are supported configure options: --with-libevent ; Enable use of libevent, which is needed for pool of threads mysqld options: --thread-handling=pool-of-threads ; Use a pool of threads to handle queries --thread-pool-size=# ; Define how many threads should be created to handle all queries --extra-port=# ; Extra tcp port that uses the old one-thread-per-connection method --extra-max-connections=# ; Number of connections to accept to 'extra-port' --test-ignore-wrong-options ; Ignore setting an enum value to a wrong option (for mysql-test-run) BUILD/SETUP.sh: Added libevents (and thus pool-of-threads) to max builds CMakeLists.txt: Added libevent Makefile.am: Added libevents config/ac-macros/libevent.m4: Libevent code for configure config/ac-macros/libevent_configure.m4: Libevent code for configure configure.in: Added libevents dbug/dbug.c: Added _db_is_pushed(); Needed for pool-of-threads code extra/Makefile.am: Added libevents extra/libevent: Libevent initial code extra/libevent/CMakeLists.txt: Libevent initial code extra/libevent/Makefile.am: Libevent initial code extra/libevent/README: Libevent initial code extra/libevent/WIN32-Code: Libevent initial code extra/libevent/WIN32-Code/config.h: Libevent initial code extra/libevent/WIN32-Code/misc.c: Libevent initial code extra/libevent/WIN32-Code/misc.h: Libevent initial code extra/libevent/WIN32-Code/tree.h: Libevent initial code extra/libevent/WIN32-Code/win32.c: Libevent initial code extra/libevent/buffer.c: Libevent initial code extra/libevent/compat: Libevent initial code extra/libevent/compat/sys: Libevent initial code extra/libevent/compat/sys/_time.h: Libevent initial code extra/libevent/compat/sys/queue.h: Libevent initial code extra/libevent/compat/sys/tree.h: Libevent initial code extra/libevent/devpoll.c: Libevent initial code extra/libevent/epoll.c: Libevent initial code extra/libevent/epoll_sub.c: Libevent initial code extra/libevent/evbuffer.c: Libevent initial code extra/libevent/evdns.c: Libevent initial code extra/libevent/evdns.h: Libevent initial code extra/libevent/event-config.h: Libevent initial code extra/libevent/event-internal.h: Libevent initial code extra/libevent/event.c: Libevent initial code extra/libevent/event.h: Libevent initial code extra/libevent/event_tagging.c: Libevent initial code extra/libevent/evhttp.h: Libevent initial code extra/libevent/evport.c: Libevent initial code extra/libevent/evrpc-internal.h: Libevent initial code extra/libevent/evrpc.c: Libevent initial code extra/libevent/evrpc.h: Libevent initial code extra/libevent/evsignal.h: Libevent initial code extra/libevent/evutil.c: Libevent initial code extra/libevent/evutil.h: Libevent initial code extra/libevent/http-internal.h: Libevent initial code extra/libevent/http.c: Libevent initial code extra/libevent/kqueue.c: Libevent initial code extra/libevent/log.c: Libevent initial code extra/libevent/log.h: Libevent initial code extra/libevent/min_heap.h: Libevent initial code extra/libevent/poll.c: Libevent initial code extra/libevent/select.c: Libevent initial code extra/libevent/signal.c: Libevent initial code extra/libevent/strlcpy-internal.h: Libevent initial code extra/libevent/strlcpy.c: Libevent initial code include/config-win.h: Libevent support include/my_dbug.h: ADded _db_is_pushed include/mysql.h.pp: Update to handle new prototypes include/typelib.h: Split find_type_or_exit() into two functions include/violite.h: Added vio_is_pending() libmysqld/Makefile.am: Added libevent mysql-test/include/have_pool_of_threads.inc: Added test for pool-of-threads mysql-test/mysql-test-run.pl: Don't abort based on time and don't retry test cases when run under --gdb or --debug mysql-test/r/crash_commit_before.result: USE GLOBAL for debug variable mysql-test/r/have_pool_of_threads.require: Added test for pool-of-threads mysql-test/r/pool_of_threads.result: Added test for pool-of-threads mysql-test/r/subselect_debug.result: USE GLOBAL for debug variable mysql-test/t/crash_commit_before.test: USE GLOBAL for debug variable mysql-test/t/merge-big.test: USE GLOBAL for debug variable mysql-test/t/pool_of_threads-master.opt: Added test for pool-of-threads mysql-test/t/pool_of_threads.test: Added test for pool-of-threads mysys/typelib.c: Split find_type_or_exit() into find_type_with_warning() sql/Makefile.am: Added libevent sql/handler.cc: Indentation fix. Fixed memory loss bug Fixed crash on exit when handler plugin failed sql/mysql_priv.h: Added extra_max_connections and mysqld_extra_port Added extern functions from sql_connect.cc sql/mysqld.cc: Added support for new mysqld options Added code for 'extra-port' and 'extra-max-connections' Split some functions into smaller pieces to be able to reuse code Added code for test-ignore-wrong-options sql/scheduler.cc: Updated schduler code from MySQL 6.0 sql/scheduler.h: Updated schduler code from MySQL 6.0 sql/set_var.cc: Added support for changing "extra_max_connections" sql/sql_class.cc: Iniitalize thread schduler options in THD sql/sql_class.h: Added to extra_port and scheduler to 'THD' sql/sql_connect.cc: Use thd->schduler to check number of connections and terminate connection Made some local functions global (for scheduler.cc) vio/viosocket.c: Added 'vio_pending', needed for scheduler..c
357 lines
8.7 KiB
C
357 lines
8.7 KiB
C
/* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
|
|
|
|
/*
|
|
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
* derived from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h"
|
|
#endif
|
|
|
|
#ifdef HAVE_SELECT
|
|
|
|
#include <sys/types.h>
|
|
#ifdef HAVE_SYS_TIME_H
|
|
#include <sys/time.h>
|
|
#else
|
|
#include <sys/_time.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
#include <sys/select.h>
|
|
#endif
|
|
#include <sys/queue.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#ifdef CHECK_INVARIANTS
|
|
#include <assert.h>
|
|
#endif
|
|
|
|
#include "event.h"
|
|
#include "event-internal.h"
|
|
#include "evsignal.h"
|
|
#include "log.h"
|
|
|
|
#ifndef howmany
|
|
#define howmany(x, y) (((x)+((y)-1))/(y))
|
|
#endif
|
|
|
|
struct selectop {
|
|
int event_fds; /* Highest fd in fd set */
|
|
int event_fdsz;
|
|
fd_set *event_readset_in;
|
|
fd_set *event_writeset_in;
|
|
fd_set *event_readset_out;
|
|
fd_set *event_writeset_out;
|
|
struct event **event_r_by_fd;
|
|
struct event **event_w_by_fd;
|
|
};
|
|
|
|
static void *select_init (struct event_base *);
|
|
static int select_add (void *, struct event *);
|
|
static int select_del (void *, struct event *);
|
|
static int select_dispatch (struct event_base *, void *, struct timeval *);
|
|
static void select_dealloc (struct event_base *, void *);
|
|
|
|
const struct eventop selectops = {
|
|
"select",
|
|
select_init,
|
|
select_add,
|
|
select_del,
|
|
select_dispatch,
|
|
select_dealloc,
|
|
0
|
|
};
|
|
|
|
static int select_resize(struct selectop *sop, int fdsz);
|
|
|
|
static void *
|
|
select_init(struct event_base *base)
|
|
{
|
|
struct selectop *sop;
|
|
|
|
/* Disable select when this environment variable is set */
|
|
if (getenv("EVENT_NOSELECT"))
|
|
return (NULL);
|
|
|
|
if (!(sop = calloc(1, sizeof(struct selectop))))
|
|
return (NULL);
|
|
|
|
select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
|
|
|
|
evsignal_init(base);
|
|
|
|
return (sop);
|
|
}
|
|
|
|
#ifdef CHECK_INVARIANTS
|
|
static void
|
|
check_selectop(struct selectop *sop)
|
|
{
|
|
int i;
|
|
for (i = 0; i <= sop->event_fds; ++i) {
|
|
if (FD_ISSET(i, sop->event_readset_in)) {
|
|
assert(sop->event_r_by_fd[i]);
|
|
assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
|
|
assert(sop->event_r_by_fd[i]->ev_fd == i);
|
|
} else {
|
|
assert(! sop->event_r_by_fd[i]);
|
|
}
|
|
if (FD_ISSET(i, sop->event_writeset_in)) {
|
|
assert(sop->event_w_by_fd[i]);
|
|
assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
|
|
assert(sop->event_w_by_fd[i]->ev_fd == i);
|
|
} else {
|
|
assert(! sop->event_w_by_fd[i]);
|
|
}
|
|
}
|
|
|
|
}
|
|
#else
|
|
#define check_selectop(sop) do { (void) sop; } while (0)
|
|
#endif
|
|
|
|
static int
|
|
select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
|
{
|
|
int res, i;
|
|
struct selectop *sop = arg;
|
|
|
|
check_selectop(sop);
|
|
|
|
memcpy(sop->event_readset_out, sop->event_readset_in,
|
|
sop->event_fdsz);
|
|
memcpy(sop->event_writeset_out, sop->event_writeset_in,
|
|
sop->event_fdsz);
|
|
|
|
res = select(sop->event_fds + 1, sop->event_readset_out,
|
|
sop->event_writeset_out, NULL, tv);
|
|
|
|
check_selectop(sop);
|
|
|
|
if (res == -1) {
|
|
if (errno != EINTR) {
|
|
event_warn("select");
|
|
return (-1);
|
|
}
|
|
|
|
evsignal_process(base);
|
|
return (0);
|
|
} else if (base->sig.evsignal_caught) {
|
|
evsignal_process(base);
|
|
}
|
|
|
|
event_debug(("%s: select reports %d", __func__, res));
|
|
|
|
check_selectop(sop);
|
|
for (i = 0; i <= sop->event_fds; ++i) {
|
|
struct event *r_ev = NULL, *w_ev = NULL;
|
|
res = 0;
|
|
if (FD_ISSET(i, sop->event_readset_out)) {
|
|
r_ev = sop->event_r_by_fd[i];
|
|
res |= EV_READ;
|
|
}
|
|
if (FD_ISSET(i, sop->event_writeset_out)) {
|
|
w_ev = sop->event_w_by_fd[i];
|
|
res |= EV_WRITE;
|
|
}
|
|
if (r_ev && (res & r_ev->ev_events)) {
|
|
event_active(r_ev, res & r_ev->ev_events, 1);
|
|
}
|
|
if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
|
|
event_active(w_ev, res & w_ev->ev_events, 1);
|
|
}
|
|
}
|
|
check_selectop(sop);
|
|
|
|
return (0);
|
|
}
|
|
|
|
|
|
static int
|
|
select_resize(struct selectop *sop, int fdsz)
|
|
{
|
|
int n_events, n_events_old;
|
|
|
|
fd_set *readset_in = NULL;
|
|
fd_set *writeset_in = NULL;
|
|
fd_set *readset_out = NULL;
|
|
fd_set *writeset_out = NULL;
|
|
struct event **r_by_fd = NULL;
|
|
struct event **w_by_fd = NULL;
|
|
|
|
n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
|
|
n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
|
|
|
|
if (sop->event_readset_in)
|
|
check_selectop(sop);
|
|
|
|
if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
|
|
goto error;
|
|
sop->event_readset_in = readset_in;
|
|
if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
|
|
goto error;
|
|
sop->event_readset_out = readset_out;
|
|
if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
|
|
goto error;
|
|
sop->event_writeset_in = writeset_in;
|
|
if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
|
|
goto error;
|
|
sop->event_writeset_out = writeset_out;
|
|
if ((r_by_fd = realloc(sop->event_r_by_fd,
|
|
n_events*sizeof(struct event*))) == NULL)
|
|
goto error;
|
|
sop->event_r_by_fd = r_by_fd;
|
|
if ((w_by_fd = realloc(sop->event_w_by_fd,
|
|
n_events * sizeof(struct event*))) == NULL)
|
|
goto error;
|
|
sop->event_w_by_fd = w_by_fd;
|
|
|
|
memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
|
|
fdsz - sop->event_fdsz);
|
|
memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
|
|
fdsz - sop->event_fdsz);
|
|
memset(sop->event_r_by_fd + n_events_old, 0,
|
|
(n_events-n_events_old) * sizeof(struct event*));
|
|
memset(sop->event_w_by_fd + n_events_old, 0,
|
|
(n_events-n_events_old) * sizeof(struct event*));
|
|
|
|
sop->event_fdsz = fdsz;
|
|
check_selectop(sop);
|
|
|
|
return (0);
|
|
|
|
error:
|
|
event_warn("malloc");
|
|
return (-1);
|
|
}
|
|
|
|
|
|
static int
|
|
select_add(void *arg, struct event *ev)
|
|
{
|
|
struct selectop *sop = arg;
|
|
|
|
if (ev->ev_events & EV_SIGNAL)
|
|
return (evsignal_add(ev));
|
|
|
|
check_selectop(sop);
|
|
/*
|
|
* Keep track of the highest fd, so that we can calculate the size
|
|
* of the fd_sets for select(2)
|
|
*/
|
|
if (sop->event_fds < ev->ev_fd) {
|
|
int fdsz = sop->event_fdsz;
|
|
|
|
if (fdsz < sizeof(fd_mask))
|
|
fdsz = sizeof(fd_mask);
|
|
|
|
while (fdsz <
|
|
(howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
|
|
fdsz *= 2;
|
|
|
|
if (fdsz != sop->event_fdsz) {
|
|
if (select_resize(sop, fdsz)) {
|
|
check_selectop(sop);
|
|
return (-1);
|
|
}
|
|
}
|
|
|
|
sop->event_fds = ev->ev_fd;
|
|
}
|
|
|
|
if (ev->ev_events & EV_READ) {
|
|
FD_SET(ev->ev_fd, sop->event_readset_in);
|
|
sop->event_r_by_fd[ev->ev_fd] = ev;
|
|
}
|
|
if (ev->ev_events & EV_WRITE) {
|
|
FD_SET(ev->ev_fd, sop->event_writeset_in);
|
|
sop->event_w_by_fd[ev->ev_fd] = ev;
|
|
}
|
|
check_selectop(sop);
|
|
|
|
return (0);
|
|
}
|
|
|
|
/*
|
|
* Nothing to be done here.
|
|
*/
|
|
|
|
static int
|
|
select_del(void *arg, struct event *ev)
|
|
{
|
|
struct selectop *sop = arg;
|
|
|
|
check_selectop(sop);
|
|
if (ev->ev_events & EV_SIGNAL)
|
|
return (evsignal_del(ev));
|
|
|
|
if (sop->event_fds < ev->ev_fd) {
|
|
check_selectop(sop);
|
|
return (0);
|
|
}
|
|
|
|
if (ev->ev_events & EV_READ) {
|
|
FD_CLR(ev->ev_fd, sop->event_readset_in);
|
|
sop->event_r_by_fd[ev->ev_fd] = NULL;
|
|
}
|
|
|
|
if (ev->ev_events & EV_WRITE) {
|
|
FD_CLR(ev->ev_fd, sop->event_writeset_in);
|
|
sop->event_w_by_fd[ev->ev_fd] = NULL;
|
|
}
|
|
|
|
check_selectop(sop);
|
|
return (0);
|
|
}
|
|
|
|
static void
|
|
select_dealloc(struct event_base *base, void *arg)
|
|
{
|
|
struct selectop *sop = arg;
|
|
|
|
evsignal_dealloc(base);
|
|
if (sop->event_readset_in)
|
|
free(sop->event_readset_in);
|
|
if (sop->event_writeset_in)
|
|
free(sop->event_writeset_in);
|
|
if (sop->event_readset_out)
|
|
free(sop->event_readset_out);
|
|
if (sop->event_writeset_out)
|
|
free(sop->event_writeset_out);
|
|
if (sop->event_r_by_fd)
|
|
free(sop->event_r_by_fd);
|
|
if (sop->event_w_by_fd)
|
|
free(sop->event_w_by_fd);
|
|
|
|
memset(sop, 0, sizeof(struct selectop));
|
|
free(sop);
|
|
}
|
|
|
|
#endif /* HAVE_SELECT */
|