blob: 62ee1156e22638751664b3087641c160479f1f93 [file] [log] [blame]
Willy Tarreaude99e992007-04-16 00:53:59 +02001/*
2 * FD polling functions for Speculative I/O combined with Linux epoll()
3 *
Willy Tarreau43d8fb22011-08-22 17:12:02 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaude99e992007-04-16 00:53:59 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
Willy Tarreauf2e8ee22008-05-25 10:39:02 +020011 *
12 * This code implements "speculative I/O" under Linux. The principle is to
13 * try to perform expected I/O before registering the events in the poller.
14 * Each time this succeeds, it saves an expensive epoll_ctl(). It generally
15 * succeeds for all reads after an accept(), and for writes after a connect().
16 * It also improves performance for streaming connections because even if only
17 * one side is polled, the other one may react accordingly depending on the
18 * level of the buffer.
19 *
20 * It has a presents drawbacks though. If too many events are set for spec I/O,
21 * those ones can starve the polled events. Experiments show that when polled
22 * events starve, they quickly turn into spec I/O, making the situation even
23 * worse. While we can reduce the number of polled events processed at once,
24 * we cannot do this on speculative events because most of them are new ones
25 * (avg 2/3 new - 1/3 old from experiments).
26 *
27 * The solution against this problem relies on those two factors :
28 * 1) one FD registered as a spec event cannot be polled at the same time
29 * 2) even during very high loads, we will almost never be interested in
30 * simultaneous read and write streaming on the same FD.
31 *
32 * The first point implies that during starvation, we will not have more than
33 * half of our FDs in the poll list, otherwise it means there is less than that
34 * in the spec list, implying there is no starvation.
35 *
36 * The second point implies that we're statically only interested in half of
37 * the maximum number of file descriptors at once, because we will unlikely
38 * have simultaneous read and writes for a same buffer during long periods.
39 *
40 * So, if we make it possible to drain maxsock/2/2 during peak loads, then we
41 * can ensure that there will be no starvation effect. This means that we must
42 * always allocate maxsock/4 events for the poller.
43 *
44 *
Willy Tarreaude99e992007-04-16 00:53:59 +020045 */
46
47#include <unistd.h>
48#include <sys/time.h>
49#include <sys/types.h>
50
51#include <common/compat.h>
52#include <common/config.h>
Willy Tarreaud6f087e2008-01-18 17:20:13 +010053#include <common/debug.h>
Willy Tarreau43d8fb22011-08-22 17:12:02 +020054#include <common/epoll.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020055#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020056#include <common/ticks.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020057#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020058#include <common/tools.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020059
Willy Tarreaude99e992007-04-16 00:53:59 +020060#include <types/global.h>
61
62#include <proto/fd.h>
Willy Tarreau332740d2009-05-10 09:57:21 +020063#include <proto/signal.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020064#include <proto/task.h>
65
Willy Tarreaude99e992007-04-16 00:53:59 +020066/*
67 * We define 4 states for each direction of a file descriptor, which we store
68 * as 2 bits :
69 *
70 * 00 = IDLE : we're not interested in this event
71 * 01 = SPEC : perform speculative I/O on this FD
72 * 10 = WAIT : really wait for an availability event on this FD (poll)
73 * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if
74 * the application changes its mind, otherwise disable FD polling
75 * and switch back to IDLE.
76 *
77 * Since we do not want to scan all the FD list to find speculative I/O events,
78 * we store them in a list consisting in a linear array holding only the FD
Willy Tarreau7a52a5c2008-08-16 16:06:02 +020079 * indexes right now. Note that a closed FD cannot exist in the spec list,
80 * because it is closed by fd_delete() which in turn calls __fd_clo() which
81 * always removes it from the list.
Willy Tarreaude99e992007-04-16 00:53:59 +020082 *
83 * The STOP state requires the event to be present in the spec list so that
84 * it can be detected and flushed upon next scan without having to scan the
85 * whole FD list.
86 *
87 * This translates like this :
88 *
89 * EVENT_IN_SPEC_LIST = 01
90 * EVENT_IN_POLL_LIST = 10
91 *
92 * IDLE = 0
93 * SPEC = (EVENT_IN_SPEC_LIST)
94 * WAIT = (EVENT_IN_POLL_LIST)
95 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
96 *
97 * fd_is_set() just consists in checking that the status is 01 or 10.
98 *
99 * For efficiency reasons, we will store the Read and Write bits interlaced to
100 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
101 * get what we want :
102 * 3 2 1 0
103 * Wp Rp Ws Rs
104 *
105 * The FD array has to hold a back reference to the speculative list. This
106 * reference is only valid if at least one of the directions is marked SPEC.
107 *
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200108 * We store the FD state in the 4 lower bits of fdtab[fd].spec.e, and save the
109 * previous state upon changes in the 4 higher bits, so that changes are easy
110 * to spot.
Willy Tarreaude99e992007-04-16 00:53:59 +0200111 */
112
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200113#define FD_EV_IN_SL 1U
114#define FD_EV_IN_PL 4U
Willy Tarreaude99e992007-04-16 00:53:59 +0200115
116#define FD_EV_IDLE 0
117#define FD_EV_SPEC (FD_EV_IN_SL)
118#define FD_EV_WAIT (FD_EV_IN_PL)
119#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
120
121/* Those match any of R or W for Spec list or Poll list */
122#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
123#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
124#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
125
126#define FD_EV_IDLE_R 0
127#define FD_EV_SPEC_R (FD_EV_IN_SL)
128#define FD_EV_WAIT_R (FD_EV_IN_PL)
129#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
130#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
131
132#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
133#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
134#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
135#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
136#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
137
138#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200139#define FD_EV_MASK_OLD (FD_EV_MASK << 4)
Willy Tarreaude99e992007-04-16 00:53:59 +0200140
Willy Tarreau6653d172007-05-13 01:52:05 +0200141/* This is the minimum number of events successfully processed in speculative
142 * mode above which we agree to return without checking epoll() (1/2 times).
143 */
144#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200145
Willy Tarreaude99e992007-04-16 00:53:59 +0200146static int nbspec = 0; // current size of the spec list
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200147static int absmaxevents = 0; // absolute maximum amounts of polled events
Willy Tarreaude99e992007-04-16 00:53:59 +0200148
Willy Tarreaude99e992007-04-16 00:53:59 +0200149static unsigned int *spec_list = NULL; // speculative I/O list
150
151/* private data */
152static struct epoll_event *epoll_events;
153static int epoll_fd;
154
155/* This structure may be used for any purpose. Warning! do not use it in
156 * recursive functions !
157 */
158static struct epoll_event ev;
159
160
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200161REGPRM1 static inline void alloc_spec_entry(const int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200162{
Willy Tarreaub48b3232009-10-17 22:54:17 +0200163 if (fdtab[fd].spec.s1)
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200164 /* sometimes the entry already exists for the other direction */
Willy Tarreaude99e992007-04-16 00:53:59 +0200165 return;
Willy Tarreaub48b3232009-10-17 22:54:17 +0200166 fdtab[fd].spec.s1 = nbspec + 1;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200167 spec_list[nbspec] = fd;
168 nbspec++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200169}
170
Willy Tarreau4eac2092007-08-31 17:01:18 +0200171/* Removes entry used by fd <fd> from the spec list and replaces it with the
Willy Tarreaub48b3232009-10-17 22:54:17 +0200172 * last one. The fdtab.spec is adjusted to match the back reference if needed.
Willy Tarreau4eac2092007-08-31 17:01:18 +0200173 * If the fd has no entry assigned, return immediately.
Willy Tarreaude99e992007-04-16 00:53:59 +0200174 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200175REGPRM1 static void release_spec_entry(int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200176{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200177 unsigned int pos;
178
Willy Tarreaub48b3232009-10-17 22:54:17 +0200179 pos = fdtab[fd].spec.s1;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200180 if (!pos)
181 return;
182
Willy Tarreaub48b3232009-10-17 22:54:17 +0200183 fdtab[fd].spec.s1 = 0;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200184 pos--;
185 /* we have spec_list[pos]==fd */
Willy Tarreaude99e992007-04-16 00:53:59 +0200186
187 nbspec--;
188 if (pos == nbspec)
189 return;
190
Willy Tarreau4eac2092007-08-31 17:01:18 +0200191 /* we replace current FD by the highest one, which may sometimes be the same */
Willy Tarreaude99e992007-04-16 00:53:59 +0200192 fd = spec_list[nbspec];
193 spec_list[pos] = fd;
Willy Tarreaub48b3232009-10-17 22:54:17 +0200194 fdtab[fd].spec.s1 = pos + 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200195}
196
197/*
198 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
199 */
200REGPRM2 static int __fd_is_set(const int fd, int dir)
201{
202 int ret;
203
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200204#if DEBUG_DEV
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200205 if (!fdtab[fd].owner) {
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200206 fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd);
207 ABORT_NOW();
208 }
209#endif
Willy Tarreaub48b3232009-10-17 22:54:17 +0200210 ret = ((unsigned)fdtab[fd].spec.e >> dir) & FD_EV_MASK_DIR;
Willy Tarreaude99e992007-04-16 00:53:59 +0200211 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
212}
213
214/*
215 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
216 * designed like this in order to reduce the number of jumps (verified).
217 */
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200218REGPRM2 static void __fd_set(const int fd, int dir)
Willy Tarreaude99e992007-04-16 00:53:59 +0200219{
Willy Tarreaude99e992007-04-16 00:53:59 +0200220 unsigned int i;
221
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200222#if DEBUG_DEV
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200223 if (!fdtab[fd].owner) {
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200224 fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd);
225 ABORT_NOW();
226 }
227#endif
Willy Tarreaub48b3232009-10-17 22:54:17 +0200228 i = ((unsigned)fdtab[fd].spec.e >> dir) & FD_EV_MASK_DIR;
Willy Tarreaude99e992007-04-16 00:53:59 +0200229
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200230 if (i != FD_EV_STOP) {
231 if (unlikely(i != FD_EV_IDLE))
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200232 return;
Willy Tarreaude99e992007-04-16 00:53:59 +0200233 // switch to SPEC state and allocate a SPEC entry.
234 alloc_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200235 }
Willy Tarreaub48b3232009-10-17 22:54:17 +0200236 fdtab[fd].spec.e ^= (unsigned int)(FD_EV_IN_SL << dir);
Willy Tarreaude99e992007-04-16 00:53:59 +0200237}
238
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200239REGPRM2 static void __fd_clr(const int fd, int dir)
Willy Tarreaude99e992007-04-16 00:53:59 +0200240{
Willy Tarreaude99e992007-04-16 00:53:59 +0200241 unsigned int i;
242
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200243#if DEBUG_DEV
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200244 if (!fdtab[fd].owner) {
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200245 fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd);
246 ABORT_NOW();
247 }
248#endif
Willy Tarreaub48b3232009-10-17 22:54:17 +0200249 i = ((unsigned)fdtab[fd].spec.e >> dir) & FD_EV_MASK_DIR;
Willy Tarreaude99e992007-04-16 00:53:59 +0200250
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200251 if (i != FD_EV_SPEC) {
252 if (unlikely(i != FD_EV_WAIT))
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200253 return;
Willy Tarreaude99e992007-04-16 00:53:59 +0200254 // switch to STOP state
255 /* We will create a queue entry for this one because we want to
256 * process it later in order to merge it with other events on
257 * the same FD.
258 */
259 alloc_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200260 }
Willy Tarreaub48b3232009-10-17 22:54:17 +0200261 fdtab[fd].spec.e ^= (unsigned int)(FD_EV_IN_SL << dir);
Willy Tarreaude99e992007-04-16 00:53:59 +0200262}
263
Willy Tarreau6653d172007-05-13 01:52:05 +0200264/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200265REGPRM1 static void __fd_rem(int fd)
266{
267 __fd_clr(fd, DIR_RD);
268 __fd_clr(fd, DIR_WR);
269}
270
271/*
272 * On valid epoll() implementations, a call to close() automatically removes
273 * the fds. This means that the FD will appear as previously unset.
274 */
275REGPRM1 static void __fd_clo(int fd)
276{
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200277 release_spec_entry(fd);
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200278 fdtab[fd].spec.e &= ~(FD_EV_MASK | FD_EV_MASK_OLD);
Willy Tarreaude99e992007-04-16 00:53:59 +0200279}
280
Willy Tarreaudc246a72007-05-09 21:57:51 +0200281/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200282 * speculative epoll() poller
283 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200284REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreaude99e992007-04-16 00:53:59 +0200285{
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200286 int status, eo, en;
Willy Tarreaude99e992007-04-16 00:53:59 +0200287 int fd, opcode;
288 int count;
289 int spec_idx;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200290 int wait_time;
Willy Tarreaude99e992007-04-16 00:53:59 +0200291
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200292 /* first, update the poll list according to what changed in the spec list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200293 spec_idx = nbspec;
294 while (likely(spec_idx > 0)) {
295 spec_idx--;
296 fd = spec_list[spec_idx];
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200297 en = fdtab[fd].spec.e & 15; /* new events */
298 eo = fdtab[fd].spec.e >> 4; /* previous events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200299
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200300 /* If an fd with a poll bit is present here, it means that it
301 * has last requested a poll, or is leaving from a poll. Given
302 * that an FD is fully in the poll list or in the spec list but
303 * not in both at once, we'll first ensure that if it is
304 * already being polled in one direction and requests a spec
305 * access, then we'll turn that into a polled access in order
306 * to save a syscall which will likely return EAGAIN.
Willy Tarreaude99e992007-04-16 00:53:59 +0200307 */
308
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200309 if ((en & FD_EV_RW_PL) && (en & FD_EV_RW_SL)) {
310 /* convert SPEC to WAIT if fd already being polled */
311 if ((en & FD_EV_MASK_R) == FD_EV_SPEC_R)
312 en = (en & ~FD_EV_MASK_R) | FD_EV_WAIT_R;
313
314 if ((en & FD_EV_MASK_W) == FD_EV_SPEC_W)
315 en = (en & ~FD_EV_MASK_W) | FD_EV_WAIT_W;
Willy Tarreaude99e992007-04-16 00:53:59 +0200316 }
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200317
318 if ((en & FD_EV_MASK_R) == FD_EV_STOP_R) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200319 /* This FD was being polled and is now being removed. */
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200320 en &= ~FD_EV_MASK_R;
Willy Tarreaude99e992007-04-16 00:53:59 +0200321 }
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200322
323 if ((en & FD_EV_MASK_W) == FD_EV_STOP_W) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200324 /* This FD was being polled and is now being removed. */
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200325 en &= ~FD_EV_MASK_W;
Willy Tarreau6653d172007-05-13 01:52:05 +0200326 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200327
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200328 if ((eo ^ en) & FD_EV_RW_PL) {
329 /* poll status changed */
330 if ((en & FD_EV_RW_PL) == 0) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200331 /* fd removed from poll list */
332 opcode = EPOLL_CTL_DEL;
333 }
334 else if ((eo & FD_EV_RW_PL) == 0) {
335 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200336 opcode = EPOLL_CTL_ADD;
337 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200338 else {
339 /* fd status changed */
340 opcode = EPOLL_CTL_MOD;
341 }
342
343 /* construct the epoll events based on new state */
344 ev.events = 0;
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200345 if (en & FD_EV_WAIT_R)
Willy Tarreau6653d172007-05-13 01:52:05 +0200346 ev.events |= EPOLLIN;
347
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200348 if (en & FD_EV_WAIT_W)
Willy Tarreau6653d172007-05-13 01:52:05 +0200349 ev.events |= EPOLLOUT;
350
351 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200352 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200353 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200354
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200355 fdtab[fd].spec.e = (en << 4) + en; /* save new events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200356
Willy Tarreaub48b3232009-10-17 22:54:17 +0200357 if (!(fdtab[fd].spec.e & FD_EV_RW_SL)) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200358 /* This fd switched to combinations of either WAIT or
359 * IDLE. It must be removed from the spec list.
360 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200361 release_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200362 }
363 }
364
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200365 /* compute the epoll_wait() timeout */
Willy Tarreaucb651252008-08-29 13:57:30 +0200366
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200367 if (nbspec || run_queue || signal_queue_len) {
368 /* Maybe we still have events in the spec list, or there are
Willy Tarreau3a628112008-06-13 21:06:56 +0200369 * some tasks left pending in the run_queue, so we must not
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200370 * wait in epoll() otherwise we would delay their delivery by
Willy Tarreau6653d172007-05-13 01:52:05 +0200371 * the next timeout.
372 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200373 wait_time = 0;
374 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200375 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200376 if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200377 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200378 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200379 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200380 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200381 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200382 if (wait_time > MAX_DELAY_MS)
383 wait_time = MAX_DELAY_MS;
384 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200385 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200386
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200387 /* now let's wait for real events */
388 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau45a12512011-09-10 16:56:42 +0200389 gettimeofday(&before_poll, NULL);
Willy Tarreau1db37712007-06-03 17:16:49 +0200390 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200391 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200392 measure_idle();
Willy Tarreaude99e992007-04-16 00:53:59 +0200393
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200394 /* process events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200395 for (count = 0; count < status; count++) {
396 int e = epoll_events[count].events;
397 fd = epoll_events[count].data.fd;
398
Willy Tarreau076be252012-07-06 16:02:29 +0200399 if (!fdtab[fd].owner)
400 continue;
401
Willy Tarreaude99e992007-04-16 00:53:59 +0200402 /* it looks complicated but gcc can optimize it away when constants
403 * have same values.
404 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100405 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau491c4982012-07-06 11:16:01 +0200406 fdtab[fd].ev |=
Willy Tarreaude99e992007-04-16 00:53:59 +0200407 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
408 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
409 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
410 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
411 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
Willy Tarreau491c4982012-07-06 11:16:01 +0200412
Willy Tarreau9845e752012-07-06 11:44:28 +0200413 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev)
414 fdtab[fd].iocb(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200415 }
Willy Tarreaucb651252008-08-29 13:57:30 +0200416
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200417 /* now process speculative events if any */
418
419 /* Here we have two options :
420 * - either walk the list forwards and hope to match more events
421 * - or walk it backwards to minimize the number of changes and
422 * to make better use of the cache.
423 * Tests have shown that walking backwards improves perf by 0.2%.
424 */
425
426 spec_idx = nbspec;
427 while (likely(spec_idx > 0)) {
428 spec_idx--;
429 fd = spec_list[spec_idx];
430 eo = fdtab[fd].spec.e; /* save old events */
431
432 /*
433 * Process the speculative events.
434 *
435 * Principle: events which are marked FD_EV_SPEC are processed
436 * with their assigned function. If the function returns 0, it
437 * means there is nothing doable without polling first. We will
438 * then convert the event to a pollable one by assigning them
439 * the WAIT status.
Willy Tarreaucb651252008-08-29 13:57:30 +0200440 */
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200441
442 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau076be252012-07-06 16:02:29 +0200443 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R)
Willy Tarreau5d526b72012-07-05 23:33:51 +0200444 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200445
Willy Tarreau076be252012-07-06 16:02:29 +0200446 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W)
Willy Tarreau5d526b72012-07-05 23:33:51 +0200447 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau9845e752012-07-06 11:44:28 +0200448
449 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) {
450 int wait = fdtab[fd].iocb(fd);
451
452 /* FIXME: warning, this will not work if both old and new
453 * callbacks are used at the same time ! This is only a
454 * temporary measure during the migration.
455 */
456 if (wait & FD_WAIT_READ)
457 fdtab[fd].spec.e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
458
459 if (wait & FD_WAIT_WRITE)
Willy Tarreau5d526b72012-07-05 23:33:51 +0200460 fdtab[fd].spec.e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200461 }
462
463 /* one callback might already have closed the fd by itself */
Willy Tarreaudb3b3262012-07-05 23:19:22 +0200464 if (!fdtab[fd].owner)
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200465 continue;
466
467 if (!(fdtab[fd].spec.e & (FD_EV_RW_SL|FD_EV_RW_PL))) {
468 /* This fd switched to IDLE, it can be removed from the spec list. */
469 release_spec_entry(fd);
470 continue;
471 }
Willy Tarreaucb651252008-08-29 13:57:30 +0200472 }
Willy Tarreaudbcd47e2012-05-13 09:42:26 +0200473
474 /* in the end, we have processed status + spec_processed FDs */
Willy Tarreaude99e992007-04-16 00:53:59 +0200475}
476
477/*
478 * Initialization of the speculative epoll() poller.
479 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
480 * disables the poller by setting its pref to 0.
481 */
482REGPRM1 static int _do_init(struct poller *p)
483{
Willy Tarreaub48b3232009-10-17 22:54:17 +0200484 __label__ fail_spec, fail_ee, fail_fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200485
486 p->private = NULL;
487
488 epoll_fd = epoll_create(global.maxsock + 1);
489 if (epoll_fd < 0)
490 goto fail_fd;
491
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200492 /* See comments at the top of the file about this formula. */
493 absmaxevents = MAX(global.tune.maxpollevents, global.maxsock/4);
Willy Tarreaude99e992007-04-16 00:53:59 +0200494 epoll_events = (struct epoll_event*)
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200495 calloc(1, sizeof(struct epoll_event) * absmaxevents);
Willy Tarreaude99e992007-04-16 00:53:59 +0200496
497 if (epoll_events == NULL)
498 goto fail_ee;
499
500 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
501 goto fail_spec;
502
Willy Tarreaude99e992007-04-16 00:53:59 +0200503 return 1;
504
Willy Tarreaude99e992007-04-16 00:53:59 +0200505 fail_spec:
506 free(epoll_events);
507 fail_ee:
508 close(epoll_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200509 epoll_fd = -1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200510 fail_fd:
511 p->pref = 0;
512 return 0;
513}
514
515/*
516 * Termination of the speculative epoll() poller.
517 * Memory is released and the poller is marked as unselectable.
518 */
519REGPRM1 static void _do_term(struct poller *p)
520{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200521 free(spec_list);
522 free(epoll_events);
Willy Tarreaude99e992007-04-16 00:53:59 +0200523
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200524 if (epoll_fd >= 0) {
525 close(epoll_fd);
526 epoll_fd = -1;
527 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200528
Willy Tarreaude99e992007-04-16 00:53:59 +0200529 spec_list = NULL;
530 epoll_events = NULL;
531
532 p->private = NULL;
533 p->pref = 0;
534}
535
536/*
537 * Check that the poller works.
538 * Returns 1 if OK, otherwise 0.
539 */
540REGPRM1 static int _do_test(struct poller *p)
541{
542 int fd;
543
544 fd = epoll_create(global.maxsock + 1);
545 if (fd < 0)
546 return 0;
547 close(fd);
548 return 1;
549}
550
551/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200552 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
553 * otherwise 0. It will ensure that all processes will not share their
554 * epoll_fd. Some side effects were encountered because of this, such
555 * as epoll_wait() returning an FD which was previously deleted.
556 */
557REGPRM1 static int _do_fork(struct poller *p)
558{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200559 if (epoll_fd >= 0)
560 close(epoll_fd);
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200561 epoll_fd = epoll_create(global.maxsock + 1);
562 if (epoll_fd < 0)
563 return 0;
564 return 1;
565}
566
567/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200568 * It is a constructor, which means that it will automatically be called before
569 * main(). This is GCC-specific but it works at least since 2.95.
570 * Special care must be taken so that it does not need any uninitialized data.
571 */
572__attribute__((constructor))
573static void _do_register(void)
574{
575 struct poller *p;
576
577 if (nbpollers >= MAX_POLLERS)
578 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200579
580 epoll_fd = -1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200581 p = &pollers[nbpollers++];
582
583 p->name = "sepoll";
584 p->pref = 400;
585 p->private = NULL;
586
587 p->test = _do_test;
588 p->init = _do_init;
589 p->term = _do_term;
590 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200591 p->fork = _do_fork;
Willy Tarreaude99e992007-04-16 00:53:59 +0200592
593 p->is_set = __fd_is_set;
Willy Tarreau3788e4c2012-07-30 14:29:35 +0200594 p->set = __fd_set;
595 p->clr = __fd_clr;
Willy Tarreaude99e992007-04-16 00:53:59 +0200596 p->rem = __fd_rem;
597 p->clo = __fd_clo;
598}
599
600
601/*
602 * Local variables:
603 * c-indent-level: 8
604 * c-basic-offset: 8
605 * End:
606 */