Blame ev_epoll.c

Packit aac759
/*
Packit aac759
 * libev epoll fd activity backend
Packit aac759
 *
Packit aac759
 * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
Packit aac759
 * All rights reserved.
Packit aac759
 *
Packit aac759
 * Redistribution and use in source and binary forms, with or without modifica-
Packit aac759
 * tion, are permitted provided that the following conditions are met:
Packit aac759
 *
Packit aac759
 *   1.  Redistributions of source code must retain the above copyright notice,
Packit aac759
 *       this list of conditions and the following disclaimer.
Packit aac759
 *
Packit aac759
 *   2.  Redistributions in binary form must reproduce the above copyright
Packit aac759
 *       notice, this list of conditions and the following disclaimer in the
Packit aac759
 *       documentation and/or other materials provided with the distribution.
Packit aac759
 *
Packit aac759
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
Packit aac759
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
Packit aac759
 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit aac759
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
Packit aac759
 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit aac759
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit aac759
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit aac759
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
Packit aac759
 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit aac759
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit aac759
 *
Packit aac759
 * Alternatively, the contents of this file may be used under the terms of
Packit aac759
 * the GNU General Public License ("GPL") version 2 or any later version,
Packit aac759
 * in which case the provisions of the GPL are applicable instead of
Packit aac759
 * the above. If you wish to allow the use of your version of this file
Packit aac759
 * only under the terms of the GPL and not to allow others to use your
Packit aac759
 * version of this file under the BSD license, indicate your decision
Packit aac759
 * by deleting the provisions above and replace them with the notice
Packit aac759
 * and other provisions required by the GPL. If you do not delete the
Packit aac759
 * provisions above, a recipient may use your version of this file under
Packit aac759
 * either the BSD or the GPL.
Packit aac759
 */
Packit aac759
Packit aac759
/*
Packit aac759
 * general notes about epoll:
Packit aac759
 *
Packit aac759
 * a) epoll silently removes fds from the fd set. as nothing tells us
Packit aac759
 *    that an fd has been removed otherwise, we have to continually
Packit aac759
 *    "rearm" fds that we suspect *might* have changed (same
Packit aac759
 *    problem with kqueue, but much less costly there).
Packit aac759
 * b) the fact that ADD != MOD creates a lot of extra syscalls due to a)
Packit aac759
 *    and seems not to have any advantage.
Packit aac759
 * c) the inability to handle fork or file descriptors (think dup)
Packit aac759
 *    limits the applicability over poll, so this is not a generic
Packit aac759
 *    poll replacement.
Packit aac759
 * d) epoll doesn't work the same as select with many file descriptors
Packit aac759
 *    (such as files). while not critical, no other advanced interface
Packit aac759
 *    seems to share this (rather non-unixy) limitation.
Packit aac759
 * e) epoll claims to be embeddable, but in practise you never get
Packit aac759
 *    a ready event for the epoll fd (broken: <=2.6.26, working: >=2.6.32).
Packit aac759
 * f) epoll_ctl returning EPERM means the fd is always ready.
Packit aac759
 *
Packit aac759
 * lots of "weird code" and complication handling in this file is due
Packit aac759
 * to these design problems with epoll, as we try very hard to avoid
Packit aac759
 * epoll_ctl syscalls for common usage patterns and handle the breakage
Packit aac759
 * ensuing from receiving events for closed and otherwise long gone
Packit aac759
 * file descriptors.
Packit aac759
 */
Packit aac759
Packit aac759
#include <sys/epoll.h>
Packit aac759
Packit aac759
#define EV_EMASK_EPERM 0x80
Packit aac759
Packit aac759
static void
Packit aac759
epoll_modify (EV_P_ int fd, int oev, int nev)
Packit aac759
{
Packit aac759
  struct epoll_event ev;
Packit aac759
  unsigned char oldmask;
Packit aac759
Packit aac759
  /*
Packit aac759
   * we handle EPOLL_CTL_DEL by ignoring it here
Packit aac759
   * on the assumption that the fd is gone anyways
Packit aac759
   * if that is wrong, we have to handle the spurious
Packit aac759
   * event in epoll_poll.
Packit aac759
   * if the fd is added again, we try to ADD it, and, if that
Packit aac759
   * fails, we assume it still has the same eventmask.
Packit aac759
   */
Packit aac759
  if (!nev)
Packit aac759
    return;
Packit aac759
Packit aac759
  oldmask = anfds [fd].emask;
Packit aac759
  anfds [fd].emask = nev;
Packit aac759
Packit aac759
  /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */
Packit aac759
  ev.data.u64 = (uint64_t)(uint32_t)fd
Packit aac759
              | ((uint64_t)(uint32_t)++anfds [fd].egen << 32);
Packit aac759
  ev.events   = (nev & EV_READ  ? EPOLLIN  : 0)
Packit aac759
              | (nev & EV_WRITE ? EPOLLOUT : 0);
Packit aac759
Packit aac759
  if (expect_true (!epoll_ctl (backend_fd, oev && oldmask != nev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev)))
Packit aac759
    return;
Packit aac759
Packit aac759
  if (expect_true (errno == ENOENT))
Packit aac759
    {
Packit aac759
      /* if ENOENT then the fd went away, so try to do the right thing */
Packit aac759
      if (!nev)
Packit aac759
        goto dec_egen;
Packit aac759
Packit aac759
      if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev))
Packit aac759
        return;
Packit aac759
    }
Packit aac759
  else if (expect_true (errno == EEXIST))
Packit aac759
    {
Packit aac759
      /* EEXIST means we ignored a previous DEL, but the fd is still active */
Packit aac759
      /* if the kernel mask is the same as the new mask, we assume it hasn't changed */
Packit aac759
      if (oldmask == nev)
Packit aac759
        goto dec_egen;
Packit aac759
Packit aac759
      if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev))
Packit aac759
        return;
Packit aac759
    }
Packit aac759
  else if (expect_true (errno == EPERM))
Packit aac759
    {
Packit aac759
      /* EPERM means the fd is always ready, but epoll is too snobbish */
Packit aac759
      /* to handle it, unlike select or poll. */
Packit aac759
      anfds [fd].emask = EV_EMASK_EPERM;
Packit aac759
Packit aac759
      /* add fd to epoll_eperms, if not already inside */
Packit aac759
      if (!(oldmask & EV_EMASK_EPERM))
Packit aac759
        {
Packit aac759
          array_needsize (int, epoll_eperms, epoll_epermmax, epoll_epermcnt + 1, EMPTY2);
Packit aac759
          epoll_eperms [epoll_epermcnt++] = fd;
Packit aac759
        }
Packit aac759
Packit aac759
      return;
Packit aac759
    }
Packit aac759
Packit aac759
  fd_kill (EV_A_ fd);
Packit aac759
Packit aac759
dec_egen:
Packit aac759
  /* we didn't successfully call epoll_ctl, so decrement the generation counter again */
Packit aac759
  --anfds [fd].egen;
Packit aac759
}
Packit aac759
Packit aac759
static void
Packit aac759
epoll_poll (EV_P_ ev_tstamp timeout)
Packit aac759
{
Packit aac759
  int i;
Packit aac759
  int eventcnt;
Packit aac759
Packit aac759
  if (expect_false (epoll_epermcnt))
Packit aac759
    timeout = 0.;
Packit aac759
Packit aac759
  /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */
Packit aac759
  /* the default libev max wait time, however. */
Packit aac759
  EV_RELEASE_CB;
Packit aac759
  eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, timeout * 1e3);
Packit aac759
  EV_ACQUIRE_CB;
Packit aac759
Packit aac759
  if (expect_false (eventcnt < 0))
Packit aac759
    {
Packit aac759
      if (errno != EINTR)
Packit aac759
        ev_syserr ("(libev) epoll_wait");
Packit aac759
Packit aac759
      return;
Packit aac759
    }
Packit aac759
Packit aac759
  for (i = 0; i < eventcnt; ++i)
Packit aac759
    {
Packit aac759
      struct epoll_event *ev = epoll_events + i;
Packit aac759
Packit aac759
      int fd = (uint32_t)ev->data.u64; /* mask out the lower 32 bits */
Packit aac759
      int want = anfds [fd].events;
Packit aac759
      int got  = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
Packit aac759
               | (ev->events & (EPOLLIN  | EPOLLERR | EPOLLHUP) ? EV_READ  : 0);
Packit aac759
Packit aac759
      /*
Packit aac759
       * check for spurious notification.
Packit aac759
       * this only finds spurious notifications on egen updates
Packit aac759
       * other spurious notifications will be found by epoll_ctl, below
Packit aac759
       * we assume that fd is always in range, as we never shrink the anfds array
Packit aac759
       */
Packit aac759
      if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32)))
Packit aac759
        {
Packit aac759
          /* recreate kernel state */
Packit aac759
          postfork |= 2;
Packit aac759
          continue;
Packit aac759
        }
Packit aac759
Packit aac759
      if (expect_false (got & ~want))
Packit aac759
        {
Packit aac759
          anfds [fd].emask = want;
Packit aac759
Packit aac759
          /*
Packit aac759
           * we received an event but are not interested in it, try mod or del
Packit aac759
           * this often happens because we optimistically do not unregister fds
Packit aac759
           * when we are no longer interested in them, but also when we get spurious
Packit aac759
           * notifications for fds from another process. this is partially handled
Packit aac759
           * above with the gencounter check (== our fd is not the event fd), and
Packit aac759
           * partially here, when epoll_ctl returns an error (== a child has the fd
Packit aac759
           * but we closed it).
Packit aac759
           */
Packit aac759
          ev->events = (want & EV_READ  ? EPOLLIN  : 0)
Packit aac759
                     | (want & EV_WRITE ? EPOLLOUT : 0);
Packit aac759
Packit aac759
          /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */
Packit aac759
          /* which is fortunately easy to do for us. */
Packit aac759
          if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev))
Packit aac759
            {
Packit aac759
              postfork |= 2; /* an error occurred, recreate kernel state */
Packit aac759
              continue;
Packit aac759
            }
Packit aac759
        }
Packit aac759
Packit aac759
      fd_event (EV_A_ fd, got);
Packit aac759
    }
Packit aac759
Packit aac759
  /* if the receive array was full, increase its size */
Packit aac759
  if (expect_false (eventcnt == epoll_eventmax))
Packit aac759
    {
Packit aac759
      ev_free (epoll_events);
Packit aac759
      epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1);
Packit aac759
      epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
Packit aac759
    }
Packit aac759
Packit aac759
  /* now synthesize events for all fds where epoll fails, while select works... */
Packit aac759
  for (i = epoll_epermcnt; i--; )
Packit aac759
    {
Packit aac759
      int fd = epoll_eperms [i];
Packit aac759
      unsigned char events = anfds [fd].events & (EV_READ | EV_WRITE);
Packit aac759
Packit aac759
      if (anfds [fd].emask & EV_EMASK_EPERM && events)
Packit aac759
        fd_event (EV_A_ fd, events);
Packit aac759
      else
Packit aac759
        {
Packit aac759
          epoll_eperms [i] = epoll_eperms [--epoll_epermcnt];
Packit aac759
          anfds [fd].emask = 0;
Packit aac759
        }
Packit aac759
    }
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
int
Packit aac759
epoll_init (EV_P_ int flags)
Packit aac759
{
Packit aac759
#ifdef EPOLL_CLOEXEC
Packit aac759
  backend_fd = epoll_create1 (EPOLL_CLOEXEC);
Packit aac759
Packit aac759
  if (backend_fd < 0 && (errno == EINVAL || errno == ENOSYS))
Packit aac759
#endif
Packit aac759
    backend_fd = epoll_create (256);
Packit aac759
Packit aac759
  if (backend_fd < 0)
Packit aac759
    return 0;
Packit aac759
Packit aac759
  fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
Packit aac759
Packit aac759
  backend_mintime = 1e-3; /* epoll does sometimes return early, this is just to avoid the worst */
Packit aac759
  backend_modify  = epoll_modify;
Packit aac759
  backend_poll    = epoll_poll;
Packit aac759
Packit aac759
  epoll_eventmax = 64; /* initial number of events receivable per poll */
Packit aac759
  epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
Packit aac759
Packit aac759
  return EVBACKEND_EPOLL;
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
void
Packit aac759
epoll_destroy (EV_P)
Packit aac759
{
Packit aac759
  ev_free (epoll_events);
Packit aac759
  array_free (epoll_eperm, EMPTY);
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
void
Packit aac759
epoll_fork (EV_P)
Packit aac759
{
Packit aac759
  close (backend_fd);
Packit aac759
Packit aac759
  while ((backend_fd = epoll_create (256)) < 0)
Packit aac759
    ev_syserr ("(libev) epoll_create");
Packit aac759
Packit aac759
  fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
Packit aac759
Packit aac759
  fd_rearm_all (EV_A);
Packit aac759
}
Packit aac759