Commit 98a64d0b authored by Andres Freund's avatar Andres Freund

Introduce WaitEventSet API.

Commit ac1d7945 ("Make idle backends exit if the postmaster dies.")
introduced a regression on, at least, large linux systems. Constantly
adding the same postmaster_alive_fds to the OSs internal datastructures
for implementing poll/select can cause significant contention; leading
to a performance regression of nearly 3x in one example.

This can be avoided by using e.g. linux' epoll, which avoids having to
add/remove file descriptors to the wait datastructures at a high rate.
Unfortunately the current latch interface makes it hard to allocate any
persistent per-backend resources.

Replace, with a backward compatibility layer, WaitLatchOrSocket with a
new WaitEventSet API. Users can allocate such a Set across multiple
calls, and add more than one file-descriptor to wait on. The latter has
been added because there's upcoming postgres features where that will be
helpful.

In addition to the previously existing poll(2), select(2),
WaitForMultipleObjects() implementations also provide an epoll_wait(2)
based implementation to address the aforementioned performance
problem. Epoll is only available on linux, but that is the most likely
OS for machines large enough (four sockets) to reproduce the problem.

To actually address the aforementioned regression, create and use a
long-lived WaitEventSet for FE/BE communication.  There are additional
places that would benefit from a long-lived set, but that's a task for
another day.

Thanks to Amit Kapila, who helped make the windows code I blindly wrote
actually work.

Reported-By: Dmitry Vasilyev Discussion:
CAB-SwXZh44_2ybvS5Z67p_CDz=XFn4hNAD=CnMEF+QqkXwFrGg@mail.gmail.com
20160114143931.GG10941@awork2.anarazel.de
parent 72e2d21c
...@@ -10193,7 +10193,7 @@ fi ...@@ -10193,7 +10193,7 @@ fi
## Header files ## Header files
## ##
for ac_header in atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h pwd.h sys/ioctl.h sys/ipc.h sys/poll.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/socket.h sys/sockio.h sys/tas.h sys/time.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h for ac_header in atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h pwd.h sys/epoll.h sys/ioctl.h sys/ipc.h sys/poll.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/socket.h sys/sockio.h sys/tas.h sys/time.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h
do : do :
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
......
...@@ -1183,7 +1183,7 @@ AC_SUBST(UUID_LIBS) ...@@ -1183,7 +1183,7 @@ AC_SUBST(UUID_LIBS)
## ##
dnl sys/socket.h is required by AC_FUNC_ACCEPT_ARGTYPES dnl sys/socket.h is required by AC_FUNC_ACCEPT_ARGTYPES
AC_CHECK_HEADERS([atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h pwd.h sys/ioctl.h sys/ipc.h sys/poll.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/socket.h sys/sockio.h sys/tas.h sys/time.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h]) AC_CHECK_HEADERS([atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h pwd.h sys/epoll.h sys/ioctl.h sys/ipc.h sys/poll.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/socket.h sys/sockio.h sys/tas.h sys/time.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h])
# On BSD, test for net/if.h will fail unless sys/socket.h # On BSD, test for net/if.h will fail unless sys/socket.h
# is included first. # is included first.
......
...@@ -140,13 +140,13 @@ retry: ...@@ -140,13 +140,13 @@ retry:
/* In blocking mode, wait until the socket is ready */ /* In blocking mode, wait until the socket is ready */
if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN)) if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
{ {
int w; WaitEvent event;
Assert(waitfor); Assert(waitfor);
w = WaitLatchOrSocket(MyLatch, ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
WL_LATCH_SET | WL_POSTMASTER_DEATH | waitfor,
port->sock, 0); WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */, &event, 1);
/* /*
* If the postmaster has died, it's not safe to continue running, * If the postmaster has died, it's not safe to continue running,
...@@ -165,13 +165,13 @@ retry: ...@@ -165,13 +165,13 @@ retry:
* cycles checking for this very rare condition, and this should cause * cycles checking for this very rare condition, and this should cause
* us to exit quickly in most cases.) * us to exit quickly in most cases.)
*/ */
if (w & WL_POSTMASTER_DEATH) if (event.events & WL_POSTMASTER_DEATH)
ereport(FATAL, ereport(FATAL,
(errcode(ERRCODE_ADMIN_SHUTDOWN), (errcode(ERRCODE_ADMIN_SHUTDOWN),
errmsg("terminating connection due to unexpected postmaster exit"))); errmsg("terminating connection due to unexpected postmaster exit")));
/* Handle interrupt. */ /* Handle interrupt. */
if (w & WL_LATCH_SET) if (event.events & WL_LATCH_SET)
{ {
ResetLatch(MyLatch); ResetLatch(MyLatch);
ProcessClientReadInterrupt(true); ProcessClientReadInterrupt(true);
...@@ -241,22 +241,22 @@ retry: ...@@ -241,22 +241,22 @@ retry:
if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN)) if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
{ {
int w; WaitEvent event;
Assert(waitfor); Assert(waitfor);
w = WaitLatchOrSocket(MyLatch, ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
WL_LATCH_SET | WL_POSTMASTER_DEATH | waitfor,
port->sock, 0); WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */, &event, 1);
/* See comments in secure_read. */ /* See comments in secure_read. */
if (w & WL_POSTMASTER_DEATH) if (event.events & WL_POSTMASTER_DEATH)
ereport(FATAL, ereport(FATAL,
(errcode(ERRCODE_ADMIN_SHUTDOWN), (errcode(ERRCODE_ADMIN_SHUTDOWN),
errmsg("terminating connection due to unexpected postmaster exit"))); errmsg("terminating connection due to unexpected postmaster exit")));
/* Handle interrupt. */ /* Handle interrupt. */
if (w & WL_LATCH_SET) if (event.events & WL_LATCH_SET)
{ {
ResetLatch(MyLatch); ResetLatch(MyLatch);
ProcessClientWriteInterrupt(true); ProcessClientWriteInterrupt(true);
......
...@@ -201,6 +201,11 @@ pq_init(void) ...@@ -201,6 +201,11 @@ pq_init(void)
(errmsg("could not set socket to nonblocking mode: %m"))); (errmsg("could not set socket to nonblocking mode: %m")));
#endif #endif
FeBeWaitSet = CreateWaitEventSet(TopMemoryContext, 3);
AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE, MyProcPort->sock,
NULL, NULL);
AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, -1, MyLatch, NULL);
AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, -1, NULL, NULL);
} }
/* -------------------------------- /* --------------------------------
......
...@@ -14,8 +14,8 @@ ...@@ -14,8 +14,8 @@
* however reliably interrupts the sleep, and causes select() to return * however reliably interrupts the sleep, and causes select() to return
* immediately even if the signal arrives before select() begins. * immediately even if the signal arrives before select() begins.
* *
* (Actually, we prefer poll() over select() where available, but the * (Actually, we prefer epoll_wait() over poll() over select() where
* same comments apply to it.) * available, but the same comments apply.)
* *
* When SetLatch is called from the same process that owns the latch, * When SetLatch is called from the same process that owns the latch,
* SetLatch writes the byte directly to the pipe. If it's owned by another * SetLatch writes the byte directly to the pipe. If it's owned by another
...@@ -41,6 +41,9 @@ ...@@ -41,6 +41,9 @@
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef HAVE_SYS_EPOLL_H
#include <sys/epoll.h>
#endif
#ifdef HAVE_POLL_H #ifdef HAVE_POLL_H
#include <poll.h> #include <poll.h>
#endif #endif
...@@ -65,18 +68,59 @@ ...@@ -65,18 +68,59 @@
* useful to manually specify the used primitive. If desired, just add a * useful to manually specify the used primitive. If desired, just add a
* define somewhere before this block. * define somewhere before this block.
*/ */
#if defined(LATCH_USE_POLL) || defined(LATCH_USE_SELECT) \ #if defined(WAIT_USE_EPOLL) || defined(WAIT_USE_POLL) || \
|| defined(LATCH_USE_WIN32) defined(WAIT_USE_SELECT) || defined(WAIT_USE_WIN32)
/* don't overwrite manual choice */ /* don't overwrite manual choice */
#elif defined(HAVE_SYS_EPOLL_H)
#define WAIT_USE_EPOLL
#elif defined(HAVE_POLL) #elif defined(HAVE_POLL)
#define LATCH_USE_POLL #define WAIT_USE_POLL
#elif HAVE_SYS_SELECT_H #elif HAVE_SYS_SELECT_H
#define LATCH_USE_SELECT #define WAIT_USE_SELECT
#elif WIN32 #elif WIN32
#define LATCH_USE_WIN32 #define WAIT_USE_WIN32
#else #else
#error "no latch implementation available" #error "no wait set implementation available"
#endif
/* typedef in latch.h */
struct WaitEventSet
{
int nevents; /* number of registered events */
int nevents_space; /* maximum number of events in this set */
/*
* Array, of nevents_space length, storing the definition of events this
* set is waiting for.
*/
WaitEvent *events;
/*
* If WL_LATCH_SET is specified in any wait event, latch is a pointer to
* said latch, and latch_pos the offset in the ->events array. This is
* useful because we check the state of the latch before performing doing
* syscalls related to waiting.
*/
Latch *latch;
int latch_pos;
#if defined(WAIT_USE_EPOLL)
int epoll_fd;
/* epoll_wait returns events in a user provided arrays, allocate once */
struct epoll_event *epoll_ret_events;
#elif defined(WAIT_USE_POLL)
/* poll expects events to be waited on every poll() call, prepare once */
struct pollfd *pollfds;
#elif defined(WAIT_USE_WIN32)
/*
* Array of windows events. The first element always contains
* pgwin32_signal_event, so the remaining elements are offset by one (i.e.
* event->pos + 1).
*/
HANDLE *handles;
#endif #endif
};
#ifndef WIN32 #ifndef WIN32
/* Are we currently in WaitLatch? The signal handler would like to know. */ /* Are we currently in WaitLatch? The signal handler would like to know. */
...@@ -91,6 +135,16 @@ static void sendSelfPipeByte(void); ...@@ -91,6 +135,16 @@ static void sendSelfPipeByte(void);
static void drainSelfPipe(void); static void drainSelfPipe(void);
#endif /* WIN32 */ #endif /* WIN32 */
#if defined(WAIT_USE_EPOLL)
static void WaitEventAdjustEpoll(WaitEventSet *set, WaitEvent *event, int action);
#elif defined(WAIT_USE_POLL)
static void WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event);
#elif defined(WAIT_USE_WIN32)
static void WaitEventAdjustWin32(WaitEventSet *set, WaitEvent *event);
#endif
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
WaitEvent *occurred_events, int nevents);
/* /*
* Initialize the process-local latch infrastructure. * Initialize the process-local latch infrastructure.
...@@ -255,642 +309,1181 @@ WaitLatch(volatile Latch *latch, int wakeEvents, long timeout) ...@@ -255,642 +309,1181 @@ WaitLatch(volatile Latch *latch, int wakeEvents, long timeout)
* When waiting on a socket, EOF and error conditions are reported by * When waiting on a socket, EOF and error conditions are reported by
* returning the socket as readable/writable or both, depending on * returning the socket as readable/writable or both, depending on
* WL_SOCKET_READABLE/WL_SOCKET_WRITEABLE being specified. * WL_SOCKET_READABLE/WL_SOCKET_WRITEABLE being specified.
*
* NB: These days this is just a wrapper around the WaitEventSet API. When
* using a latch very frequently, consider creating a longer living
* WaitEventSet instead; that's more efficient.
*/ */
#ifndef LATCH_USE_WIN32
int int
WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
long timeout) long timeout)
{ {
int result = 0; int ret = 0;
int rc; int rc;
instr_time start_time, WaitEvent event;
cur_time; WaitEventSet *set = CreateWaitEventSet(CurrentMemoryContext, 3);
long cur_timeout;
#if defined(LATCH_USE_POLL)
struct pollfd pfds[3];
int nfds;
#elif defined(LATCH_USE_SELECT)
struct timeval tv,
*tvp;
fd_set input_mask;
fd_set output_mask;
int hifd;
#endif
Assert(wakeEvents != 0); /* must have at least one wake event */ if (wakeEvents & WL_TIMEOUT)
Assert(timeout >= 0);
else
timeout = -1;
/* waiting for socket readiness without a socket indicates a bug */ if (wakeEvents & WL_LATCH_SET)
if (sock == PGINVALID_SOCKET && AddWaitEventToSet(set, WL_LATCH_SET, PGINVALID_SOCKET,
(wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) != 0) (Latch *) latch, NULL);
elog(ERROR, "cannot wait on socket event without a socket");
if ((wakeEvents & WL_LATCH_SET) && latch->owner_pid != MyProcPid) if (wakeEvents & WL_POSTMASTER_DEATH)
elog(ERROR, "cannot wait on a latch owned by another process"); AddWaitEventToSet(set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
NULL, NULL);
/* if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
* Initialize timeout if requested. We must record the current time so
* that we can determine the remaining timeout if the poll() or select()
* is interrupted. (On some platforms, select() will update the contents
* of "tv" for us, but unfortunately we can't rely on that.)
*/
if (wakeEvents & WL_TIMEOUT)
{ {
INSTR_TIME_SET_CURRENT(start_time); int ev;
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
#ifdef LATCH_USE_SELECT ev = wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE);
tv.tv_sec = cur_timeout / 1000L; AddWaitEventToSet(set, ev, sock, NULL, NULL);
tv.tv_usec = (cur_timeout % 1000L) * 1000L;
tvp = &tv;
#endif
} }
rc = WaitEventSetWait(set, timeout, &event, 1);
if (rc == 0)
ret |= WL_TIMEOUT;
else else
{ {
cur_timeout = -1; ret |= event.events & (WL_LATCH_SET |
WL_POSTMASTER_DEATH |
WL_SOCKET_READABLE |
WL_SOCKET_WRITEABLE);
}
FreeWaitEventSet(set);
return ret;
}
#ifdef LATCH_USE_SELECT /*
tvp = NULL; * Sets a latch and wakes up anyone waiting on it.
*
* This is cheap if the latch is already set, otherwise not so much.
*
* NB: when calling this in a signal handler, be sure to save and restore
* errno around it. (That's standard practice in most signal handlers, of
* course, but we used to omit it in handlers that only set a flag.)
*
* NB: this function is called from critical sections and signal handlers so
* throwing an error is not a good idea.
*/
void
SetLatch(volatile Latch *latch)
{
#ifndef WIN32
pid_t owner_pid;
#else
HANDLE handle;
#endif #endif
}
waiting = true; /*
do * The memory barrier has be to be placed here to ensure that any flag
* variables possibly changed by this process have been flushed to main
* memory, before we check/set is_set.
*/
pg_memory_barrier();
/* Quick exit if already set */
if (latch->is_set)
return;
latch->is_set = true;
#ifndef WIN32
/*
* See if anyone's waiting for the latch. It can be the current process if
* we're in a signal handler. We use the self-pipe to wake up the select()
* in that case. If it's another process, send a signal.
*
* Fetch owner_pid only once, in case the latch is concurrently getting
* owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
* guaranteed to be true! In practice, the effective range of pid_t fits
* in a 32 bit integer, and so should be atomic. In the worst case, we
* might end up signaling the wrong process. Even then, you're very
* unlucky if a process with that bogus pid exists and belongs to
* Postgres; and PG database processes should handle excess SIGUSR1
* interrupts without a problem anyhow.
*
* Another sort of race condition that's possible here is for a new
* process to own the latch immediately after we look, so we don't signal
* it. This is okay so long as all callers of ResetLatch/WaitLatch follow
* the standard coding convention of waiting at the bottom of their loops,
* not the top, so that they'll correctly process latch-setting events
* that happen before they enter the loop.
*/
owner_pid = latch->owner_pid;
if (owner_pid == 0)
return;
else if (owner_pid == MyProcPid)
{ {
if (waiting)
sendSelfPipeByte();
}
else
kill(owner_pid, SIGUSR1);
#else
/* /*
* Check if the latch is set already. If so, leave loop immediately, * See if anyone's waiting for the latch. It can be the current process if
* avoid blocking again. We don't attempt to report any other events * we're in a signal handler.
* that might also be satisfied.
*
* If someone sets the latch between this and the poll()/select()
* below, the setter will write a byte to the pipe (or signal us and
* the signal handler will do that), and the poll()/select() will
* return immediately.
*
* If there's a pending byte in the self pipe, we'll notice whenever
* blocking. Only clearing the pipe in that case avoids having to
* drain it every time WaitLatchOrSocket() is used. Should the
* pipe-buffer fill up we're still ok, because the pipe is in
* nonblocking mode. It's unlikely for that to happen, because the
* self pipe isn't filled unless we're blocking (waiting = true), or
* from inside a signal handler in latch_sigusr1_handler().
* *
* Note: we assume that the kernel calls involved in drainSelfPipe() * Use a local variable here just in case somebody changes the event field
* and SetLatch() will provide adequate synchronization on machines * concurrently (which really should not happen).
* with weak memory ordering, so that we cannot miss seeing is_set if
* the signal byte is already in the pipe when we drain it.
*/ */
if ((wakeEvents & WL_LATCH_SET) && latch->is_set) handle = latch->event;
if (handle)
{ {
result |= WL_LATCH_SET; SetEvent(handle);
break;
/*
* Note that we silently ignore any errors. We might be in a signal
* handler or other critical path where it's not safe to call elog().
*/
} }
#endif
}
/*
* Clear the latch. Calling WaitLatch after this will sleep, unless
* the latch is set again before the WaitLatch call.
*/
void
ResetLatch(volatile Latch *latch)
{
/* Only the owner should reset the latch */
Assert(latch->owner_pid == MyProcPid);
latch->is_set = false;
/* /*
* Must wait ... we use the polling interface determined at the top of * Ensure that the write to is_set gets flushed to main memory before we
* this file to do so. * examine any flag variables. Otherwise a concurrent SetLatch might
* falsely conclude that it needn't signal us, even though we have missed
* seeing some flag updates that SetLatch was supposed to inform us of.
*/ */
#if defined(LATCH_USE_POLL) pg_memory_barrier();
nfds = 0; }
/* selfpipe is always in pfds[0] */ /*
pfds[0].fd = selfpipe_readfd; * Create a WaitEventSet with space for nevents different events to wait for.
pfds[0].events = POLLIN; *
pfds[0].revents = 0; * These events can then efficiently waited upon together, using
nfds++; * WaitEventSetWait().
*/
WaitEventSet *
CreateWaitEventSet(MemoryContext context, int nevents)
{
WaitEventSet *set;
char *data;
Size sz = 0;
sz += sizeof(WaitEventSet);
sz += sizeof(WaitEvent) * nevents;
#if defined(WAIT_USE_EPOLL)
sz += sizeof(struct epoll_event) * nevents;
#elif defined(WAIT_USE_POLL)
sz += sizeof(struct pollfd) * nevents;
#elif defined(WAIT_USE_WIN32)
/* need space for the pgwin32_signal_event */
sz += sizeof(HANDLE) * (nevents + 1);
#endif
if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) data = (char *) MemoryContextAllocZero(context, sz);
{
/* socket, if used, is always in pfds[1] */
pfds[1].fd = sock;
pfds[1].events = 0;
if (wakeEvents & WL_SOCKET_READABLE)
pfds[1].events |= POLLIN;
if (wakeEvents & WL_SOCKET_WRITEABLE)
pfds[1].events |= POLLOUT;
pfds[1].revents = 0;
nfds++;
}
if (wakeEvents & WL_POSTMASTER_DEATH) set = (WaitEventSet *) data;
{ data += sizeof(WaitEventSet);
/* postmaster fd, if used, is always in pfds[nfds - 1] */
pfds[nfds].fd = postmaster_alive_fds[POSTMASTER_FD_WATCH];
pfds[nfds].events = POLLIN;
pfds[nfds].revents = 0;
nfds++;
}
/* Sleep */ set->events = (WaitEvent *) data;
rc = poll(pfds, nfds, (int) cur_timeout); data += sizeof(WaitEvent) * nevents;
/* Check return code */ #if defined(WAIT_USE_EPOLL)
if (rc < 0) set->epoll_ret_events = (struct epoll_event *) data;
data += sizeof(struct epoll_event) * nevents;
#elif defined(WAIT_USE_POLL)
set->pollfds = (struct pollfd *) data;
data += sizeof(struct pollfd) * nevents;
#elif defined(WAIT_USE_WIN32)
set->handles = (HANDLE) data;
data += sizeof(HANDLE) * nevents;
#endif
set->latch = NULL;
set->nevents_space = nevents;
#if defined(WAIT_USE_EPOLL)
set->epoll_fd = epoll_create(nevents);
if (set->epoll_fd < 0)
elog(ERROR, "epoll_create failed: %m");
#elif defined(WAIT_USE_WIN32)
/*
* To handle signals while waiting, we need to add a win32 specific event.
* We accounted for the additional event at the top of this routine. See
* port/win32/signal.c for more details.
*
* Note: pgwin32_signal_event should be first to ensure that it will be
* reported when multiple events are set. We want to guarantee that
* pending signals are serviced.
*/
set->handles[0] = pgwin32_signal_event;
StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
#endif
return set;
}
/*
* Free a previously created WaitEventSet.
*/
void
FreeWaitEventSet(WaitEventSet *set)
{
#if defined(WAIT_USE_EPOLL)
close(set->epoll_fd);
#elif defined(WAIT_USE_WIN32)
WaitEvent *cur_event;
for (cur_event = set->events;
cur_event < (set->events + set->nevents);
cur_event++)
{ {
/* EINTR is okay, otherwise complain */ if (cur_event->events & WL_LATCH_SET)
if (errno != EINTR)
{ {
waiting = false; /* uses the latch's HANDLE */
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("poll() failed: %m")));
}
} }
else if (rc == 0) else if (cur_event->events & WL_POSTMASTER_DEATH)
{ {
/* timeout exceeded */ /* uses PostmasterHandle */
if (wakeEvents & WL_TIMEOUT)
result |= WL_TIMEOUT;
} }
else else
{ {
/* at least one event occurred, so check revents values */ /* Clean up the event object we created for the socket */
WSAEventSelect(cur_event->fd, NULL, 0);
WSACloseEvent(set->handles[cur_event->pos + 1]);
}
}
#endif
pfree(set);
}
/* ---
* Add an event to the set. Possible events are:
* - WL_LATCH_SET: Wait for the latch to be set
* - WL_POSTMASTER_DEATH: Wait for postmaster to die
* - WL_SOCKET_READABLE: Wait for socket to become readable
* can be combined in one event with WL_SOCKET_WRITEABLE
* - WL_SOCKET_WRITEABLE: Wait for socket to become writeable
* can be combined with WL_SOCKET_READABLE
*
* Returns the offset in WaitEventSet->events (starting from 0), which can be
* used to modify previously added wait events using ModifyWaitEvent().
*
* In the WL_LATCH_SET case the latch must be owned by the current process,
* i.e. it must be a backend-local latch initialized with InitLatch, or a
* shared latch associated with the current process by calling OwnLatch.
*
* In the WL_SOCKET_READABLE/WRITEABLE case, EOF and error conditions are
* reported by returning the socket as readable/writable or both, depending on
* WL_SOCKET_READABLE/WRITEABLE being specified.
*
* The user_data pointer specified here will be set for the events returned
* by WaitEventSetWait(), allowing to easily associate additional data with
* events.
*/
int
AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch,
void *user_data)
{
WaitEvent *event;
/* not enough space */
Assert(set->nevents < set->nevents_space);
if (pfds[0].revents & POLLIN) if (latch)
{ {
/* There's data in the self-pipe, clear it. */ if (latch->owner_pid != MyProcPid)
drainSelfPipe(); elog(ERROR, "cannot wait on a latch owned by another process");
if (set->latch)
elog(ERROR, "cannot wait on more than one latch");
if ((events & WL_LATCH_SET) != WL_LATCH_SET)
elog(ERROR, "latch events only spuport being set");
} }
else
if ((wakeEvents & WL_SOCKET_READABLE) &&
(pfds[1].revents & POLLIN))
{ {
/* data available in socket, or EOF/error condition */ if (events & WL_LATCH_SET)
result |= WL_SOCKET_READABLE; elog(ERROR, "cannot wait on latch without a specified latch");
} }
if ((wakeEvents & WL_SOCKET_WRITEABLE) &&
(pfds[1].revents & POLLOUT)) /* waiting for socket readiness without a socket indicates a bug */
if (fd == PGINVALID_SOCKET &&
(events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)))
elog(ERROR, "cannot wait on socket event without a socket");
event = &set->events[set->nevents];
event->pos = set->nevents++;
event->fd = fd;
event->events = events;
event->user_data = user_data;
if (events == WL_LATCH_SET)
{ {
/* socket is writable */ set->latch = latch;
result |= WL_SOCKET_WRITEABLE; set->latch_pos = event->pos;
#ifndef WIN32
event->fd = selfpipe_readfd;
#endif
} }
if ((wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) && else if (events == WL_POSTMASTER_DEATH)
(pfds[1].revents & (POLLHUP | POLLERR | POLLNVAL)))
{ {
/* EOF/error condition */ #ifndef WIN32
if (wakeEvents & WL_SOCKET_READABLE) event->fd = postmaster_alive_fds[POSTMASTER_FD_WATCH];
result |= WL_SOCKET_READABLE; #endif
if (wakeEvents & WL_SOCKET_WRITEABLE)
result |= WL_SOCKET_WRITEABLE;
} }
/* /* perform wait primitive specific initialization, if needed */
* We expect a POLLHUP when the remote end is closed, but because #if defined(WAIT_USE_EPOLL)
* we don't expect the pipe to become readable or to have any WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
* errors either, treat those cases as postmaster death, too. #elif defined(WAIT_USE_POLL)
*/ WaitEventAdjustPoll(set, event);
if ((wakeEvents & WL_POSTMASTER_DEATH) && #elif defined(WAIT_USE_SELECT)
(pfds[nfds - 1].revents & (POLLHUP | POLLIN | POLLERR | POLLNVAL))) /* nothing to do */
{ #elif defined(WAIT_USE_WIN32)
/* WaitEventAdjustWin32(set, event);
* According to the select(2) man page on Linux, select(2) may #endif
* spuriously return and report a file descriptor as readable,
* when it's not; and presumably so can poll(2). It's not return event->pos;
* clear that the relevant cases would ever apply to the }
* postmaster pipe, but since the consequences of falsely
* returning WL_POSTMASTER_DEATH could be pretty unpleasant, /*
* we take the trouble to positively verify EOF with * Change the event mask and, in the WL_LATCH_SET case, the latch associated
* PostmasterIsAlive(). * with the WaitEvent.
*
* 'pos' is the id returned by AddWaitEventToSet.
*/ */
if (!PostmasterIsAlive()) void
result |= WL_POSTMASTER_DEATH; ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
} {
} WaitEvent *event;
#elif defined(LATCH_USE_SELECT)
Assert(pos < set->nevents);
event = &set->events[pos];
/* /*
* On at least older linux kernels select(), in violation of POSIX, * If neither the event mask nor the associated latch changes, return
* doesn't reliably return a socket as writable if closed - but we * early. That's an important optimization for some sockets, where
* rely on that. So far all the known cases of this problem are on * ModifyWaitEvent is frequently used to switch from waiting for reads to
* platforms that also provide a poll() implementation without that * waiting on writes.
* bug. If we find one where that's not the case, we'll need to add a
* workaround.
*/ */
FD_ZERO(&input_mask); if (events == event->events &&
FD_ZERO(&output_mask); (!(event->events & WL_LATCH_SET) || set->latch == latch))
return;
FD_SET(selfpipe_readfd, &input_mask);
hifd = selfpipe_readfd;
if (wakeEvents & WL_POSTMASTER_DEATH) if (event->events & WL_LATCH_SET &&
events != event->events)
{ {
FD_SET(postmaster_alive_fds[POSTMASTER_FD_WATCH], &input_mask); /* we could allow to disable latch events for a while */
if (postmaster_alive_fds[POSTMASTER_FD_WATCH] > hifd) elog(ERROR, "cannot modify latch event");
hifd = postmaster_alive_fds[POSTMASTER_FD_WATCH];
} }
if (wakeEvents & WL_SOCKET_READABLE) if (event->events & WL_POSTMASTER_DEATH)
{ {
FD_SET(sock, &input_mask); elog(ERROR, "cannot modify postmaster death event");
if (sock > hifd)
hifd = sock;
} }
if (wakeEvents & WL_SOCKET_WRITEABLE) /* FIXME: validate event mask */
event->events = events;
if (events == WL_LATCH_SET)
{ {
FD_SET(sock, &output_mask); set->latch = latch;
if (sock > hifd)
hifd = sock;
} }
/* Sleep */ #if defined(WAIT_USE_EPOLL)
rc = select(hifd + 1, &input_mask, &output_mask, NULL, tvp); WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
#elif defined(WAIT_USE_POLL)
WaitEventAdjustPoll(set, event);
#elif defined(WAIT_USE_SELECT)
/* nothing to do */
#elif defined(WAIT_USE_WIN32)
WaitEventAdjustWin32(set, event);
#endif
}
/* Check return code */ #if defined(WAIT_USE_EPOLL)
if (rc < 0) /*
{ * action can be one of EPOLL_CTL_ADD | EPOLL_CTL_MOD | EPOLL_CTL_DEL
/* EINTR is okay, otherwise complain */ */
if (errno != EINTR) static void
WaitEventAdjustEpoll(WaitEventSet *set, WaitEvent *event, int action)
{
struct epoll_event epoll_ev;
int rc;
/* pointer to our event, returned by epoll_wait */
epoll_ev.data.ptr = event;
/* always wait for errors */
epoll_ev.events = EPOLLERR | EPOLLHUP;
/* prepare pollfd entry once */
if (event->events == WL_LATCH_SET)
{ {
waiting = false; Assert(set->latch != NULL);
epoll_ev.events |= EPOLLIN;
}
else if (event->events == WL_POSTMASTER_DEATH)
{
epoll_ev.events |= EPOLLIN;
}
else
{
Assert(event->fd != PGINVALID_SOCKET);
Assert(event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE));
if (event->events & WL_SOCKET_READABLE)
epoll_ev.events |= EPOLLIN;
if (event->events & WL_SOCKET_WRITEABLE)
epoll_ev.events |= EPOLLOUT;
}
/*
* Even though unused, we also pass epoll_ev as the data argument if
* EPOLL_CTL_DEL is passed as action. There used to be an epoll bug
* requiring that, and actually it makes the code simpler...
*/
rc = epoll_ctl(set->epoll_fd, action, event->fd, &epoll_ev);
if (rc < 0)
ereport(ERROR, ereport(ERROR,
(errcode_for_socket_access(), (errcode_for_socket_access(),
errmsg("select() failed: %m"))); errmsg("epoll_ctl() failed: %m")));
} }
#endif
#if defined(WAIT_USE_POLL)
static void
WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event)
{
struct pollfd *pollfd = &set->pollfds[event->pos];
pollfd->revents = 0;
pollfd->fd = event->fd;
/* prepare pollfd entry once */
if (event->events == WL_LATCH_SET)
{
Assert(set->latch != NULL);
pollfd->events = POLLIN;
} }
else if (rc == 0) else if (event->events == WL_POSTMASTER_DEATH)
{ {
/* timeout exceeded */ pollfd->events = POLLIN;
if (wakeEvents & WL_TIMEOUT)
result |= WL_TIMEOUT;
} }
else else
{ {
/* at least one event occurred, so check masks */ Assert(event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE));
if (FD_ISSET(selfpipe_readfd, &input_mask)) pollfd->events = 0;
{ if (event->events & WL_SOCKET_READABLE)
/* There's data in the self-pipe, clear it. */ pollfd->events |= POLLIN;
drainSelfPipe(); if (event->events & WL_SOCKET_WRITEABLE)
pollfd->events |= POLLOUT;
} }
if ((wakeEvents & WL_SOCKET_READABLE) && FD_ISSET(sock, &input_mask))
Assert(event->fd != PGINVALID_SOCKET);
}
#endif
#if defined(WAIT_USE_WIN32)
static void
WaitEventAdjustWin32(WaitEventSet *set, WaitEvent *event)
{
HANDLE *handle = &set->handles[event->pos + 1];
if (event->events == WL_LATCH_SET)
{ {
/* data available in socket, or EOF */ Assert(set->latch != NULL);
result |= WL_SOCKET_READABLE; *handle = set->latch->event;
} }
if ((wakeEvents & WL_SOCKET_WRITEABLE) && FD_ISSET(sock, &output_mask)) else if (event->events == WL_POSTMASTER_DEATH)
{ {
/* socket is writable, or EOF */ *handle = PostmasterHandle;
result |= WL_SOCKET_WRITEABLE;
} }
if ((wakeEvents & WL_POSTMASTER_DEATH) && else
FD_ISSET(postmaster_alive_fds[POSTMASTER_FD_WATCH], {
&input_mask)) int flags = FD_CLOSE; /* always check for errors/EOF */
if (event->events & WL_SOCKET_READABLE)
flags |= FD_READ;
if (event->events & WL_SOCKET_WRITEABLE)
flags |= FD_WRITE;
if (*handle == WSA_INVALID_EVENT)
{ {
*handle = WSACreateEvent();
if (*handle == WSA_INVALID_EVENT)
elog(ERROR, "failed to create event for socket: error code %u",
WSAGetLastError());
}
if (WSAEventSelect(event->fd, *handle, flags) != 0)
elog(ERROR, "failed to set up event for socket: error code %u",
WSAGetLastError());
Assert(event->fd != PGINVALID_SOCKET);
}
}
#endif
/*
* Wait for events added to the set to happen, or until the timeout is
* reached. At most nevents occurred events are returned.
*
* If timeout = -1, block until an event occurs; if 0, check sockets for
* readiness, but don't block; if > 0, block for at most timeout miliseconds.
*
* Returns the number of events occurred, or 0 if the timeout was reached.
*
* Returned events will have the fd, pos, user_data fields set to the
* values associated with the registered event.
*/
int
WaitEventSetWait(WaitEventSet *set, long timeout,
WaitEvent *occurred_events, int nevents)
{
int returned_events = 0;
instr_time start_time;
instr_time cur_time;
long cur_timeout = -1;
Assert(nevents > 0);
/* /*
* According to the select(2) man page on Linux, select(2) may * Initialize timeout if requested. We must record the current time so
* spuriously return and report a file descriptor as readable, * that we can determine the remaining timeout if interrupted.
* when it's not; and presumably so can poll(2). It's not
* clear that the relevant cases would ever apply to the
* postmaster pipe, but since the consequences of falsely
* returning WL_POSTMASTER_DEATH could be pretty unpleasant,
* we take the trouble to positively verify EOF with
* PostmasterIsAlive().
*/ */
if (!PostmasterIsAlive()) if (timeout >= 0)
result |= WL_POSTMASTER_DEATH; {
INSTR_TIME_SET_CURRENT(start_time);
Assert(timeout >= 0 && timeout <= INT_MAX);
cur_timeout = timeout;
} }
#ifndef WIN32
waiting = true;
#else
/* Ensure that signals are serviced even if latch is already set */
pgwin32_dispatch_queued_signals();
#endif
while (returned_events == 0)
{
int rc;
/*
* Check if the latch is set already. If so, leave the loop
* immediately, avoid blocking again. We don't attempt to report any
* other events that might also be satisfied.
*
* If someone sets the latch between this and the
* WaitEventSetWaitBlock() below, the setter will write a byte to the
* pipe (or signal us and the signal handler will do that), and the
* readiness routine will return immediately.
*
* On unix, If there's a pending byte in the self pipe, we'll notice
* whenever blocking. Only clearing the pipe in that case avoids
* having to drain it every time WaitLatchOrSocket() is used. Should
* the pipe-buffer fill up we're still ok, because the pipe is in
* nonblocking mode. It's unlikely for that to happen, because the
* self pipe isn't filled unless we're blocking (waiting = true), or
* from inside a signal handler in latch_sigusr1_handler().
*
* On windows, we'll also notice if there's a pending event for the
* latch when blocking, but there's no danger of anything filling up,
* as "Setting an event that is already set has no effect.".
*
* Note: we assume that the kernel calls involved in latch management
* will provide adequate synchronization on machines with weak memory
* ordering, so that we cannot miss seeing is_set if a notification
* has already been queued.
*/
if (set->latch && set->latch->is_set)
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->pos = set->latch_pos;
occurred_events->user_data =
set->events[set->latch_pos].user_data;
occurred_events->events = WL_LATCH_SET;
occurred_events++;
returned_events++;
break;
} }
#endif /* LATCH_USE_SELECT */
/* /*
* Check again whether latch is set, the arrival of a signal/self-byte * Wait for events using the readiness primitive chosen at the top of
* might be what stopped our sleep. It's not required for correctness * this file. If -1 is returned, a timeout has occurred, if 0 we have
* to signal the latch as being set (we'd just loop if there's no * to retry, everything >= 1 is the number of returned events.
* other event), but it seems good to report an arrived latch asap.
* This way we also don't have to compute the current timestamp again.
*/ */
if ((wakeEvents & WL_LATCH_SET) && latch->is_set) rc = WaitEventSetWaitBlock(set, cur_timeout,
result |= WL_LATCH_SET; occurred_events, nevents);
if (rc == -1)
break; /* timeout occurred */
else
returned_events = rc;
/* If we're not done, update cur_timeout for next iteration */ /* If we're not done, update cur_timeout for next iteration */
if (result == 0 && (wakeEvents & WL_TIMEOUT)) if (returned_events == 0 && timeout >= 0)
{ {
INSTR_TIME_SET_CURRENT(cur_time); INSTR_TIME_SET_CURRENT(cur_time);
INSTR_TIME_SUBTRACT(cur_time, start_time); INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
if (cur_timeout <= 0) if (cur_timeout <= 0)
{ break;
/* Timeout has expired, no need to continue looping */
result |= WL_TIMEOUT;
}
#ifdef LATCH_USE_SELECT
else
{
tv.tv_sec = cur_timeout / 1000L;
tv.tv_usec = (cur_timeout % 1000L) * 1000L;
} }
#endif
} }
} while (result == 0); #ifndef WIN32
waiting = false; waiting = false;
#endif
return result; return returned_events;
} }
#else /* LATCH_USE_WIN32 */
int
WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, #if defined(WAIT_USE_EPOLL)
long timeout)
/*
* Wait using linux's epoll_wait(2).
*
* This is the preferrable wait method, as several readiness notifications are
* delivered, without having to iterate through all of set->events. The return
* epoll_event struct contain a pointer to our events, making association
* easy.
*/
static int
WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
WaitEvent *occurred_events, int nevents)
{ {
DWORD rc; int returned_events = 0;
instr_time start_time, int rc;
cur_time; WaitEvent *cur_event;
long cur_timeout; struct epoll_event *cur_epoll_event;
HANDLE events[4];
HANDLE latchevent;
HANDLE sockevent = WSA_INVALID_EVENT;
int numevents;
int result = 0;
int pmdeath_eventno = 0;
Assert(wakeEvents != 0); /* must have at least one wake event */
/* waiting for socket readiness without a socket indicates a bug */ /* Sleep */
if (sock == PGINVALID_SOCKET && rc = epoll_wait(set->epoll_fd, set->epoll_ret_events,
(wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) != 0) nevents, cur_timeout);
elog(ERROR, "cannot wait on socket event without a socket");
if ((wakeEvents & WL_LATCH_SET) && latch->owner_pid != MyProcPid) /* Check return code */
elog(ERROR, "cannot wait on a latch owned by another process"); if (rc < 0)
{
/* EINTR is okay, otherwise complain */
if (errno != EINTR)
{
waiting = false;
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("epoll_wait() failed: %m")));
}
return 0;
}
else if (rc == 0)
{
/* timeout exceeded */
return -1;
}
/* /*
* Initialize timeout if requested. We must record the current time so * At least one event occurred, iterate over the returned epoll events
* that we can determine the remaining timeout if WaitForMultipleObjects * until they're either all processed, or we've returned all the events
* is interrupted. * the caller desired.
*/ */
if (wakeEvents & WL_TIMEOUT) for (cur_epoll_event = set->epoll_ret_events;
cur_epoll_event < (set->epoll_ret_events + rc) &&
returned_events < nevents;
cur_epoll_event++)
{ {
INSTR_TIME_SET_CURRENT(start_time); /* epoll's data pointer is set to the associated WaitEvent */
Assert(timeout >= 0 && timeout <= INT_MAX); cur_event = (WaitEvent *) cur_epoll_event->data.ptr;
cur_timeout = timeout;
} occurred_events->pos = cur_event->pos;
else occurred_events->user_data = cur_event->user_data;
cur_timeout = INFINITE; occurred_events->events = 0;
if (cur_event->events == WL_LATCH_SET &&
cur_epoll_event->events & (EPOLLIN | EPOLLERR | EPOLLHUP))
{
/* There's data in the self-pipe, clear it. */
drainSelfPipe();
if (set->latch->is_set)
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_LATCH_SET;
occurred_events++;
returned_events++;
}
}
else if (cur_event->events == WL_POSTMASTER_DEATH &&
cur_epoll_event->events & (EPOLLIN | EPOLLERR | EPOLLHUP))
{
/* /*
* Construct an array of event handles for WaitforMultipleObjects(). * We expect an EPOLLHUP when the remote end is closed, but
* because we don't expect the pipe to become readable or to have
* any errors either, treat those cases as postmaster death, too.
* *
* Note: pgwin32_signal_event should be first to ensure that it will be * As explained in the WAIT_USE_SELECT implementation, select(2)
* reported when multiple events are set. We want to guarantee that * may spuriously return. Be paranoid about that here too, a
* pending signals are serviced. * spurious WL_POSTMASTER_DEATH would be painful.
*/ */
latchevent = latch->event; if (!PostmasterIsAlive())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
occurred_events++;
returned_events++;
}
}
else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
{
Assert(cur_event->fd != PGINVALID_SOCKET);
events[0] = pgwin32_signal_event; if ((cur_event->events & WL_SOCKET_READABLE) &&
events[1] = latchevent; (cur_epoll_event->events & (EPOLLIN | EPOLLERR | EPOLLHUP)))
numevents = 2;
if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
{ {
/* Need an event object to represent events on the socket */ /* data available in socket, or EOF */
int flags = FD_CLOSE; /* always check for errors/EOF */ occurred_events->events |= WL_SOCKET_READABLE;
}
if (wakeEvents & WL_SOCKET_READABLE) if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
flags |= FD_READ; (cur_epoll_event->events & (EPOLLOUT | EPOLLERR | EPOLLHUP)))
if (wakeEvents & WL_SOCKET_WRITEABLE) {
flags |= FD_WRITE; /* writable, or EOF */
occurred_events->events |= WL_SOCKET_WRITEABLE;
}
sockevent = WSACreateEvent(); if (occurred_events->events != 0)
if (sockevent == WSA_INVALID_EVENT) {
elog(ERROR, "failed to create event for socket: error code %u", occurred_events->fd = cur_event->fd;
WSAGetLastError()); occurred_events++;
if (WSAEventSelect(sock, sockevent, flags) != 0) returned_events++;
elog(ERROR, "failed to set up event for socket: error code %u", }
WSAGetLastError()); }
}
events[numevents++] = sockevent; return returned_events;
}
#elif defined(WAIT_USE_POLL)
/*
* Wait using poll(2).
*
* This allows to receive readiness notifications for several events at once,
* but requires iterating through all of set->pollfds.
*/
static inline int
WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
WaitEvent *occurred_events, int nevents)
{
int returned_events = 0;
int rc;
WaitEvent *cur_event;
struct pollfd *cur_pollfd;
/* Sleep */
rc = poll(set->pollfds, set->nevents, (int) cur_timeout);
/* Check return code */
if (rc < 0)
{
/* EINTR is okay, otherwise complain */
if (errno != EINTR)
{
waiting = false;
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("poll() failed: %m")));
} }
if (wakeEvents & WL_POSTMASTER_DEATH) return 0;
}
else if (rc == 0)
{ {
pmdeath_eventno = numevents; /* timeout exceeded */
events[numevents++] = PostmasterHandle; return -1;
} }
/* Ensure that signals are serviced even if latch is already set */ for (cur_event = set->events, cur_pollfd = set->pollfds;
pgwin32_dispatch_queued_signals(); cur_event < (set->events + set->nevents) &&
returned_events < nevents;
do cur_event++, cur_pollfd++)
{ {
/* /* no activity on this FD, skip */
* The comment in the unix version above applies here as well. At if (cur_pollfd->revents == 0)
* least after mentally replacing self-pipe with windows event. continue;
* There's no danger of overflowing, as "Setting an event that is
* already set has no effect.". occurred_events->pos = cur_event->pos;
*/ occurred_events->user_data = cur_event->user_data;
if ((wakeEvents & WL_LATCH_SET) && latch->is_set) occurred_events->events = 0;
if (cur_event->events == WL_LATCH_SET &&
(cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
{ {
result |= WL_LATCH_SET; /* There's data in the self-pipe, clear it. */
drainSelfPipe();
if (set->latch->is_set)
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_LATCH_SET;
occurred_events++;
returned_events++;
}
}
else if (cur_event->events == WL_POSTMASTER_DEATH &&
(cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
{
/* /*
* Leave loop immediately, avoid blocking again. We don't attempt * We expect an POLLHUP when the remote end is closed, but because
* to report any other events that might also be satisfied. * we don't expect the pipe to become readable or to have any
* errors either, treat those cases as postmaster death, too.
*
* As explained in the WAIT_USE_SELECT implementation, select(2)
* may spuriously return. Be paranoid about that here too, a
* spurious WL_POSTMASTER_DEATH would be painful.
*/ */
break; if (!PostmasterIsAlive())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
occurred_events++;
returned_events++;
} }
}
else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
{
int errflags = POLLHUP | POLLERR | POLLNVAL;
rc = WaitForMultipleObjects(numevents, events, FALSE, cur_timeout); Assert(cur_event->fd >= PGINVALID_SOCKET);
if (rc == WAIT_FAILED) if ((cur_event->events & WL_SOCKET_READABLE) &&
elog(ERROR, "WaitForMultipleObjects() failed: error code %lu", (cur_pollfd->revents & (POLLIN | errflags)))
GetLastError());
else if (rc == WAIT_TIMEOUT)
{ {
result |= WL_TIMEOUT; /* data available in socket, or EOF */
occurred_events->events |= WL_SOCKET_READABLE;
} }
else if (rc == WAIT_OBJECT_0)
if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
(cur_pollfd->revents & (POLLOUT | errflags)))
{ {
/* Service newly-arrived signals */ /* writeable, or EOF */
pgwin32_dispatch_queued_signals(); occurred_events->events |= WL_SOCKET_WRITEABLE;
} }
else if (rc == WAIT_OBJECT_0 + 1)
if (occurred_events->events != 0)
{ {
occurred_events->fd = cur_event->fd;
occurred_events++;
returned_events++;
}
}
}
return returned_events;
}
#elif defined(WAIT_USE_SELECT)
/*
* Wait using select(2).
*
* XXX: On at least older linux kernels select(), in violation of POSIX,
* doesn't reliably return a socket as writable if closed - but we rely on
* that. So far all the known cases of this problem are on platforms that also
* provide a poll() implementation without that bug. If we find one where
* that's not the case, we'll need to add a workaround.
*/
static inline int
WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
WaitEvent *occurred_events, int nevents)
{
int returned_events = 0;
int rc;
WaitEvent *cur_event;
fd_set input_mask;
fd_set output_mask;
int hifd;
struct timeval tv;
struct timeval *tvp = NULL;
FD_ZERO(&input_mask);
FD_ZERO(&output_mask);
/* /*
* Reset the event. We'll re-check the, potentially, set latch on * Prepare input/output masks. We do so every loop iteration as there's no
* next iteration of loop, but let's not waste the cycles to * entirely portable way to copy fd_sets.
* update cur_timeout below.
*/ */
if (!ResetEvent(latchevent)) for (cur_event = set->events;
elog(ERROR, "ResetEvent failed: error code %lu", GetLastError()); cur_event < (set->events + set->nevents);
cur_event++)
{
if (cur_event->events == WL_LATCH_SET)
FD_SET(cur_event->fd, &input_mask);
else if (cur_event->events == WL_POSTMASTER_DEATH)
FD_SET(cur_event->fd, &input_mask);
else
{
Assert(cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE));
if (cur_event->events == WL_SOCKET_READABLE)
FD_SET(cur_event->fd, &input_mask);
else if (cur_event->events == WL_SOCKET_WRITEABLE)
FD_SET(cur_event->fd, &output_mask);
}
continue; if (cur_event->fd > hifd)
hifd = cur_event->fd;
} }
else if ((wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) &&
rc == WAIT_OBJECT_0 + 2) /* socket is at event slot 2 */ /* Sleep */
if (cur_timeout >= 0)
{ {
WSANETWORKEVENTS resEvents; tv.tv_sec = cur_timeout / 1000L;
tv.tv_usec = (cur_timeout % 1000L) * 1000L;
tvp = &tv;
}
rc = select(hifd + 1, &input_mask, &output_mask, NULL, tvp);
ZeroMemory(&resEvents, sizeof(resEvents)); /* Check return code */
if (WSAEnumNetworkEvents(sock, sockevent, &resEvents) != 0) if (rc < 0)
elog(ERROR, "failed to enumerate network events: error code %u", {
WSAGetLastError()); /* EINTR is okay, otherwise complain */
if ((wakeEvents & WL_SOCKET_READABLE) && if (errno != EINTR)
(resEvents.lNetworkEvents & FD_READ))
{ {
result |= WL_SOCKET_READABLE; waiting = false;
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("select() failed: %m")));
} }
if ((wakeEvents & WL_SOCKET_WRITEABLE) && return 0; /* retry */
(resEvents.lNetworkEvents & FD_WRITE)) }
else if (rc == 0)
{ {
result |= WL_SOCKET_WRITEABLE; /* timeout exceeded */
return -1;
} }
if (resEvents.lNetworkEvents & FD_CLOSE)
/*
* To associate events with select's masks, we have to check the status of
* the file descriptors associated with an event; by looping through all
* events.
*/
for (cur_event = set->events;
cur_event < (set->events + set->nevents)
&& returned_events < nevents;
cur_event++)
{
occurred_events->pos = cur_event->pos;
occurred_events->user_data = cur_event->user_data;
occurred_events->events = 0;
if (cur_event->events == WL_LATCH_SET &&
FD_ISSET(cur_event->fd, &input_mask))
{ {
if (wakeEvents & WL_SOCKET_READABLE) /* There's data in the self-pipe, clear it. */
result |= WL_SOCKET_READABLE; drainSelfPipe();
if (wakeEvents & WL_SOCKET_WRITEABLE)
result |= WL_SOCKET_WRITEABLE; if (set->latch->is_set)
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_LATCH_SET;
occurred_events++;
returned_events++;
} }
} }
else if ((wakeEvents & WL_POSTMASTER_DEATH) && else if (cur_event->events == WL_POSTMASTER_DEATH &&
rc == WAIT_OBJECT_0 + pmdeath_eventno) FD_ISSET(cur_event->fd, &input_mask))
{ {
/* /*
* Postmaster apparently died. Since the consequences of falsely * According to the select(2) man page on Linux, select(2) may
* returning WL_POSTMASTER_DEATH could be pretty unpleasant, we * spuriously return and report a file descriptor as readable,
* take the trouble to positively verify this with * when it's not; and presumably so can poll(2). It's not clear
* PostmasterIsAlive(), even though there is no known reason to * that the relevant cases would ever apply to the postmaster
* think that the event could be falsely set on Windows. * pipe, but since the consequences of falsely returning
* WL_POSTMASTER_DEATH could be pretty unpleasant, we take the
* trouble to positively verify EOF with PostmasterIsAlive().
*/ */
if (!PostmasterIsAlive()) if (!PostmasterIsAlive())
result |= WL_POSTMASTER_DEATH; {
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
occurred_events++;
returned_events++;
} }
else }
elog(ERROR, "unexpected return code from WaitForMultipleObjects(): %lu", rc); else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
/* If we're not done, update cur_timeout for next iteration */
if (result == 0 && (wakeEvents & WL_TIMEOUT))
{ {
INSTR_TIME_SET_CURRENT(cur_time); Assert(cur_event->fd != PGINVALID_SOCKET);
INSTR_TIME_SUBTRACT(cur_time, start_time);
cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); if ((cur_event->events & WL_SOCKET_READABLE) &&
if (cur_timeout <= 0) FD_ISSET(cur_event->fd, &input_mask))
{ {
/* Timeout has expired, no need to continue looping */ /* data available in socket, or EOF */
result |= WL_TIMEOUT; occurred_events->events |= WL_SOCKET_READABLE;
}
} }
} while (result == 0);
/* Clean up the event object we created for the socket */ if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
if (sockevent != WSA_INVALID_EVENT) FD_ISSET(cur_event->fd, &output_mask))
{ {
WSAEventSelect(sock, NULL, 0); /* socket is writeable, or EOF */
WSACloseEvent(sockevent); occurred_events->events |= WL_SOCKET_WRITEABLE;
} }
return result; if (occurred_events->events != 0)
{
occurred_events->fd = cur_event->fd;
occurred_events++;
returned_events++;
}
}
}
return returned_events;
} }
#endif /* LATCH_USE_WIN32 */
#elif defined(WAIT_USE_WIN32)
/* /*
* Sets a latch and wakes up anyone waiting on it. * Wait using Windows' WaitForMultipleObjects().
*
* This is cheap if the latch is already set, otherwise not so much.
* *
* NB: when calling this in a signal handler, be sure to save and restore * Unfortunately this will only ever return a single readiness notification at
* errno around it. (That's standard practice in most signal handlers, of * a time. Note that while the official documentation for
* course, but we used to omit it in handlers that only set a flag.) * WaitForMultipleObjects is ambiguous about multiple events being "consumed"
* * with a single bWaitAll = FALSE call,
* NB: this function is called from critical sections and signal handlers so * https://blogs.msdn.microsoft.com/oldnewthing/20150409-00/?p=44273 confirms
* throwing an error is not a good idea. * that only one event is "consumed".
*/ */
void static inline int
SetLatch(volatile Latch *latch) WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
WaitEvent *occurred_events, int nevents)
{ {
#ifndef WIN32 int returned_events = 0;
pid_t owner_pid; DWORD rc;
#else WaitEvent *cur_event;
HANDLE handle;
#endif
/* /*
* The memory barrier has be to be placed here to ensure that any flag * Sleep.
* variables possibly changed by this process have been flushed to main *
* memory, before we check/set is_set. * Need to wait for ->nevents + 1, because signal handle is in [0].
*/ */
pg_memory_barrier(); rc = WaitForMultipleObjects(set->nevents + 1, set->handles, FALSE,
cur_timeout);
/* Quick exit if already set */
if (latch->is_set)
return;
latch->is_set = true;
#ifndef WIN32 /* Check return code */
if (rc == WAIT_FAILED)
elog(ERROR, "WaitForMultipleObjects() failed: error code %lu",
GetLastError());
else if (rc == WAIT_TIMEOUT)
{
/* timeout exceeded */
return -1;
}
/* if (rc == WAIT_OBJECT_0)
* See if anyone's waiting for the latch. It can be the current process if
* we're in a signal handler. We use the self-pipe to wake up the select()
* in that case. If it's another process, send a signal.
*
* Fetch owner_pid only once, in case the latch is concurrently getting
* owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
* guaranteed to be true! In practice, the effective range of pid_t fits
* in a 32 bit integer, and so should be atomic. In the worst case, we
* might end up signaling the wrong process. Even then, you're very
* unlucky if a process with that bogus pid exists and belongs to
* Postgres; and PG database processes should handle excess SIGUSR1
* interrupts without a problem anyhow.
*
* Another sort of race condition that's possible here is for a new
* process to own the latch immediately after we look, so we don't signal
* it. This is okay so long as all callers of ResetLatch/WaitLatch follow
* the standard coding convention of waiting at the bottom of their loops,
* not the top, so that they'll correctly process latch-setting events
* that happen before they enter the loop.
*/
owner_pid = latch->owner_pid;
if (owner_pid == 0)
return;
else if (owner_pid == MyProcPid)
{ {
if (waiting) /* Service newly-arrived signals */
sendSelfPipeByte(); pgwin32_dispatch_queued_signals();
return 0; /* retry */
} }
else
kill(owner_pid, SIGUSR1);
#else
/* /*
* See if anyone's waiting for the latch. It can be the current process if * With an offset of one, due to the always present pgwin32_signal_event,
* we're in a signal handler. * the handle offset directly corresponds to a wait event.
*
* Use a local variable here just in case somebody changes the event field
* concurrently (which really should not happen).
*/ */
handle = latch->event; cur_event = (WaitEvent *) &set->events[rc - WAIT_OBJECT_0 - 1];
if (handle)
occurred_events->pos = cur_event->pos;
occurred_events->user_data = cur_event->user_data;
occurred_events->events = 0;
if (cur_event->events == WL_LATCH_SET)
{ {
SetEvent(handle); if (!ResetEvent(set->latch->event))
elog(ERROR, "ResetEvent failed: error code %lu", GetLastError());
if (set->latch->is_set)
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_LATCH_SET;
occurred_events++;
returned_events++;
}
}
else if (cur_event->events == WL_POSTMASTER_DEATH)
{
/* /*
* Note that we silently ignore any errors. We might be in a signal * Postmaster apparently died. Since the consequences of falsely
* handler or other critical path where it's not safe to call elog(). * returning WL_POSTMASTER_DEATH could be pretty unpleasant, we take
* the trouble to positively verify this with PostmasterIsAlive(),
* even though there is no known reason to think that the event could
* be falsely set on Windows.
*/ */
if (!PostmasterIsAlive())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
occurred_events++;
returned_events++;
} }
#endif }
else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
{
WSANETWORKEVENTS resEvents;
HANDLE handle = set->handles[cur_event->pos + 1];
} Assert(cur_event->fd);
/* occurred_events->fd = cur_event->fd;
* Clear the latch. Calling WaitLatch after this will sleep, unless
* the latch is set again before the WaitLatch call.
*/
void
ResetLatch(volatile Latch *latch)
{
/* Only the owner should reset the latch */
Assert(latch->owner_pid == MyProcPid);
latch->is_set = false; ZeroMemory(&resEvents, sizeof(resEvents));
if (WSAEnumNetworkEvents(cur_event->fd, handle, &resEvents) != 0)
elog(ERROR, "failed to enumerate network events: error code %u",
WSAGetLastError());
if ((cur_event->events & WL_SOCKET_READABLE) &&
(resEvents.lNetworkEvents & FD_READ))
{
/* data available in socket */
occurred_events->events |= WL_SOCKET_READABLE;
}
if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
(resEvents.lNetworkEvents & FD_WRITE))
{
/* writeable */
occurred_events->events |= WL_SOCKET_WRITEABLE;
}
if (resEvents.lNetworkEvents & FD_CLOSE)
{
/* EOF */
if (cur_event->events & WL_SOCKET_READABLE)
occurred_events->events |= WL_SOCKET_READABLE;
if (cur_event->events & WL_SOCKET_WRITEABLE)
occurred_events->events |= WL_SOCKET_WRITEABLE;
}
/* if (occurred_events->events != 0)
* Ensure that the write to is_set gets flushed to main memory before we {
* examine any flag variables. Otherwise a concurrent SetLatch might occurred_events++;
* falsely conclude that it needn't signal us, even though we have missed returned_events++;
* seeing some flag updates that SetLatch was supposed to inform us of. }
*/ }
pg_memory_barrier();
return returned_events;
} }
#endif
/* /*
* SetLatch uses SIGUSR1 to wake up the process waiting on the latch. * SetLatch uses SIGUSR1 to wake up the process waiting on the latch.
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include "access/htup_details.h" #include "access/htup_details.h"
#include "catalog/pg_authid.h" #include "catalog/pg_authid.h"
#include "libpq/libpq.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "postmaster/autovacuum.h" #include "postmaster/autovacuum.h"
...@@ -247,6 +248,9 @@ SwitchToSharedLatch(void) ...@@ -247,6 +248,9 @@ SwitchToSharedLatch(void)
MyLatch = &MyProc->procLatch; MyLatch = &MyProc->procLatch;
if (FeBeWaitSet)
ModifyWaitEvent(FeBeWaitSet, 1, WL_LATCH_SET, MyLatch);
/* /*
* Set the shared latch as the local one might have been set. This * Set the shared latch as the local one might have been set. This
* shouldn't normally be necessary as code is supposed to check the * shouldn't normally be necessary as code is supposed to check the
...@@ -262,6 +266,10 @@ SwitchBackToLocalLatch(void) ...@@ -262,6 +266,10 @@ SwitchBackToLocalLatch(void)
Assert(MyProc != NULL && MyLatch == &MyProc->procLatch); Assert(MyProc != NULL && MyLatch == &MyProc->procLatch);
MyLatch = &LocalLatchData; MyLatch = &LocalLatchData;
if (FeBeWaitSet)
ModifyWaitEvent(FeBeWaitSet, 1, WL_LATCH_SET, MyLatch);
SetLatch(MyLatch); SetLatch(MyLatch);
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "lib/stringinfo.h" #include "lib/stringinfo.h"
#include "libpq/libpq-be.h" #include "libpq/libpq-be.h"
#include "storage/latch.h"
typedef struct typedef struct
...@@ -95,6 +96,8 @@ extern ssize_t secure_raw_write(Port *port, const void *ptr, size_t len); ...@@ -95,6 +96,8 @@ extern ssize_t secure_raw_write(Port *port, const void *ptr, size_t len);
extern bool ssl_loaded_verify_locations; extern bool ssl_loaded_verify_locations;
WaitEventSet *FeBeWaitSet;
/* GUCs */ /* GUCs */
extern char *SSLCipherSuites; extern char *SSLCipherSuites;
extern char *SSLECDHCurve; extern char *SSLECDHCurve;
......
...@@ -530,6 +530,9 @@ ...@@ -530,6 +530,9 @@
/* Define to 1 if you have the syslog interface. */ /* Define to 1 if you have the syslog interface. */
#undef HAVE_SYSLOG #undef HAVE_SYSLOG
/* Define to 1 if you have the <sys/epoll.h> header file. */
#undef HAVE_SYS_EPOLL_H
/* Define to 1 if you have the <sys/ioctl.h> header file. */ /* Define to 1 if you have the <sys/ioctl.h> header file. */
#undef HAVE_SYS_IOCTL_H #undef HAVE_SYS_IOCTL_H
......
...@@ -68,6 +68,12 @@ ...@@ -68,6 +68,12 @@
* use of any generic handler. * use of any generic handler.
* *
* *
* WaitEventSets allow to wait for latches being set and additional events -
* postmaster dying and socket readiness of several sockets currently - at the
* same time. On many platforms using a long lived event set is more
* efficient than using WaitLatch or WaitLatchOrSocket.
*
*
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
...@@ -95,13 +101,27 @@ typedef struct Latch ...@@ -95,13 +101,27 @@ typedef struct Latch
#endif #endif
} Latch; } Latch;
/* Bitmasks for events that may wake-up WaitLatch() clients */ /*
* Bitmasks for events that may wake-up WaitLatch(), WaitLatchOrSocket(), or
* WaitEventSetWait().
*/
#define WL_LATCH_SET (1 << 0) #define WL_LATCH_SET (1 << 0)
#define WL_SOCKET_READABLE (1 << 1) #define WL_SOCKET_READABLE (1 << 1)
#define WL_SOCKET_WRITEABLE (1 << 2) #define WL_SOCKET_WRITEABLE (1 << 2)
#define WL_TIMEOUT (1 << 3) #define WL_TIMEOUT (1 << 3) /* not for WaitEventSetWait() */
#define WL_POSTMASTER_DEATH (1 << 4) #define WL_POSTMASTER_DEATH (1 << 4)
typedef struct WaitEvent
{
int pos; /* position in the event data structure */
uint32 events; /* triggered events */
pgsocket fd; /* socket fd associated with event */
void *user_data; /* pointer provided in AddWaitEventToSet */
} WaitEvent;
/* forward declaration to avoid exposing latch.c implementation details */
typedef struct WaitEventSet WaitEventSet;
/* /*
* prototypes for functions in latch.c * prototypes for functions in latch.c
*/ */
...@@ -110,12 +130,19 @@ extern void InitLatch(volatile Latch *latch); ...@@ -110,12 +130,19 @@ extern void InitLatch(volatile Latch *latch);
extern void InitSharedLatch(volatile Latch *latch); extern void InitSharedLatch(volatile Latch *latch);
extern void OwnLatch(volatile Latch *latch); extern void OwnLatch(volatile Latch *latch);
extern void DisownLatch(volatile Latch *latch); extern void DisownLatch(volatile Latch *latch);
extern int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout);
extern int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents,
pgsocket sock, long timeout);
extern void SetLatch(volatile Latch *latch); extern void SetLatch(volatile Latch *latch);
extern void ResetLatch(volatile Latch *latch); extern void ResetLatch(volatile Latch *latch);
extern WaitEventSet *CreateWaitEventSet(MemoryContext context, int nevents);
extern void FreeWaitEventSet(WaitEventSet *set);
extern int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd,
Latch *latch, void *user_data);
extern void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch);
extern int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents);
extern int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout);
extern int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents,
pgsocket sock, long timeout);
/* /*
* Unix implementation uses SIGUSR1 for inter-process signaling. * Unix implementation uses SIGUSR1 for inter-process signaling.
......
...@@ -2113,6 +2113,8 @@ WalSnd ...@@ -2113,6 +2113,8 @@ WalSnd
WalSndCtlData WalSndCtlData
WalSndSendDataCallback WalSndSendDataCallback
WalSndState WalSndState
WaitEvent
WaitEventSet
WholeRowVarExprState WholeRowVarExprState
WindowAgg WindowAgg
WindowAggState WindowAggState
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment