blob: 5c27dbd83b7f80a2c0c086082853fb7581297def [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 Tarreauf2e8ee22008-05-25 10:39:02 +02004 * Copyright 2000-2008 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 Tarreaude99e992007-04-16 00:53:59 +020054#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020055#include <common/ticks.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020056#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020057#include <common/tools.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020058
Willy Tarreaude99e992007-04-16 00:53:59 +020059#include <types/global.h>
60
61#include <proto/fd.h>
Willy Tarreau332740d2009-05-10 09:57:21 +020062#include <proto/signal.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020063#include <proto/task.h>
64
65#if defined(USE_MY_EPOLL)
66#include <common/epoll.h>
67#include <errno.h>
68#include <sys/syscall.h>
69static _syscall1 (int, epoll_create, int, size);
70static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
71static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
72#else
73#include <sys/epoll.h>
74#endif
75
76/*
77 * We define 4 states for each direction of a file descriptor, which we store
78 * as 2 bits :
79 *
80 * 00 = IDLE : we're not interested in this event
81 * 01 = SPEC : perform speculative I/O on this FD
82 * 10 = WAIT : really wait for an availability event on this FD (poll)
83 * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if
84 * the application changes its mind, otherwise disable FD polling
85 * and switch back to IDLE.
86 *
87 * Since we do not want to scan all the FD list to find speculative I/O events,
88 * we store them in a list consisting in a linear array holding only the FD
Willy Tarreau7a52a5c2008-08-16 16:06:02 +020089 * indexes right now. Note that a closed FD cannot exist in the spec list,
90 * because it is closed by fd_delete() which in turn calls __fd_clo() which
91 * always removes it from the list.
Willy Tarreaude99e992007-04-16 00:53:59 +020092 *
93 * The STOP state requires the event to be present in the spec list so that
94 * it can be detected and flushed upon next scan without having to scan the
95 * whole FD list.
96 *
97 * This translates like this :
98 *
99 * EVENT_IN_SPEC_LIST = 01
100 * EVENT_IN_POLL_LIST = 10
101 *
102 * IDLE = 0
103 * SPEC = (EVENT_IN_SPEC_LIST)
104 * WAIT = (EVENT_IN_POLL_LIST)
105 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
106 *
107 * fd_is_set() just consists in checking that the status is 01 or 10.
108 *
109 * For efficiency reasons, we will store the Read and Write bits interlaced to
110 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
111 * get what we want :
112 * 3 2 1 0
113 * Wp Rp Ws Rs
114 *
115 * The FD array has to hold a back reference to the speculative list. This
116 * reference is only valid if at least one of the directions is marked SPEC.
117 *
118 */
119
120#define FD_EV_IN_SL 1
121#define FD_EV_IN_PL 4
122
123#define FD_EV_IDLE 0
124#define FD_EV_SPEC (FD_EV_IN_SL)
125#define FD_EV_WAIT (FD_EV_IN_PL)
126#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
127
128/* Those match any of R or W for Spec list or Poll list */
129#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
130#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
131#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
132
133#define FD_EV_IDLE_R 0
134#define FD_EV_SPEC_R (FD_EV_IN_SL)
135#define FD_EV_WAIT_R (FD_EV_IN_PL)
136#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
137#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
138
139#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
140#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
141#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
142#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
143#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
144
145#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
146
Willy Tarreau6653d172007-05-13 01:52:05 +0200147/* This is the minimum number of events successfully processed in speculative
148 * mode above which we agree to return without checking epoll() (1/2 times).
149 */
150#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200151
152/* descriptor of one FD.
153 * FIXME: should be a bit field */
154struct fd_status {
155 unsigned int e:4; // read and write events status.
Willy Tarreau4eac2092007-08-31 17:01:18 +0200156 unsigned int s1:28; // Position in spec list+1. 0=not in list. Should be last.
Willy Tarreaude99e992007-04-16 00:53:59 +0200157};
158
159static int nbspec = 0; // current size of the spec list
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200160static int absmaxevents = 0; // absolute maximum amounts of polled events
Willy Tarreaucb651252008-08-29 13:57:30 +0200161static int fd_created = 0; // fd creation detector, reset upon poll() entry.
Willy Tarreaude99e992007-04-16 00:53:59 +0200162
163static struct fd_status *fd_list = NULL; // list of FDs
164static unsigned int *spec_list = NULL; // speculative I/O list
165
166/* private data */
167static struct epoll_event *epoll_events;
168static int epoll_fd;
169
170/* This structure may be used for any purpose. Warning! do not use it in
171 * recursive functions !
172 */
173static struct epoll_event ev;
174
175
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200176REGPRM1 static inline void alloc_spec_entry(const int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200177{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200178 if (fd_list[fd].s1)
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200179 /* sometimes the entry already exists for the other direction */
Willy Tarreaude99e992007-04-16 00:53:59 +0200180 return;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200181 fd_list[fd].s1 = nbspec + 1;
182 spec_list[nbspec] = fd;
183 nbspec++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200184}
185
Willy Tarreau4eac2092007-08-31 17:01:18 +0200186/* Removes entry used by fd <fd> from the spec list and replaces it with the
187 * last one. The fd_list is adjusted to match the back reference if needed.
188 * If the fd has no entry assigned, return immediately.
Willy Tarreaude99e992007-04-16 00:53:59 +0200189 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200190REGPRM1 static void release_spec_entry(int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200191{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200192 unsigned int pos;
193
194 pos = fd_list[fd].s1;
195 if (!pos)
196 return;
197
198 fd_list[fd].s1 = 0;
199 pos--;
200 /* we have spec_list[pos]==fd */
Willy Tarreaude99e992007-04-16 00:53:59 +0200201
202 nbspec--;
203 if (pos == nbspec)
204 return;
205
Willy Tarreau4eac2092007-08-31 17:01:18 +0200206 /* we replace current FD by the highest one, which may sometimes be the same */
Willy Tarreaude99e992007-04-16 00:53:59 +0200207 fd = spec_list[nbspec];
Willy Tarreau4eac2092007-08-31 17:01:18 +0200208 fd_list[fd].s1 = pos + 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200209 spec_list[pos] = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200210}
211
212/*
213 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
214 */
215REGPRM2 static int __fd_is_set(const int fd, int dir)
216{
217 int ret;
218
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200219#if DEBUG_DEV
220 if (fdtab[fd].state == FD_STCLOSE) {
221 fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd);
222 ABORT_NOW();
223 }
224#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200225 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
226 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
227}
228
229/*
230 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
231 * designed like this in order to reduce the number of jumps (verified).
232 */
233REGPRM2 static int __fd_set(const int fd, int dir)
234{
Willy Tarreaude99e992007-04-16 00:53:59 +0200235 unsigned int i;
236
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200237#if DEBUG_DEV
238 if (fdtab[fd].state == FD_STCLOSE) {
239 fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd);
240 ABORT_NOW();
241 }
242#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200243 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
244
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200245 if (i != FD_EV_STOP) {
246 if (unlikely(i != FD_EV_IDLE))
247 return 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200248 // switch to SPEC state and allocate a SPEC entry.
Willy Tarreau573fd802009-03-22 19:25:46 +0100249 fd_created++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200250 alloc_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200251 }
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200252 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
253 return 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200254}
255
256REGPRM2 static int __fd_clr(const int fd, int dir)
257{
Willy Tarreaude99e992007-04-16 00:53:59 +0200258 unsigned int i;
259
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200260#if DEBUG_DEV
261 if (fdtab[fd].state == FD_STCLOSE) {
262 fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd);
263 ABORT_NOW();
264 }
265#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200266 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
267
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200268 if (i != FD_EV_SPEC) {
269 if (unlikely(i != FD_EV_WAIT))
270 return 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200271 // switch to STOP state
272 /* We will create a queue entry for this one because we want to
273 * process it later in order to merge it with other events on
274 * the same FD.
275 */
276 alloc_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200277 }
Willy Tarreauff9d5ba2009-10-17 21:43:03 +0200278 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
279 return 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200280}
281
Willy Tarreau6653d172007-05-13 01:52:05 +0200282/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200283REGPRM1 static void __fd_rem(int fd)
284{
285 __fd_clr(fd, DIR_RD);
286 __fd_clr(fd, DIR_WR);
287}
288
289/*
290 * On valid epoll() implementations, a call to close() automatically removes
291 * the fds. This means that the FD will appear as previously unset.
292 */
293REGPRM1 static void __fd_clo(int fd)
294{
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200295 release_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200296 fd_list[fd].e &= ~(FD_EV_MASK);
297}
298
Willy Tarreaudc246a72007-05-09 21:57:51 +0200299/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200300 * speculative epoll() poller
301 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200302REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreaude99e992007-04-16 00:53:59 +0200303{
304 static unsigned int last_skipped;
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200305 static unsigned int spec_processed;
Willy Tarreau6653d172007-05-13 01:52:05 +0200306 int status, eo;
Willy Tarreaude99e992007-04-16 00:53:59 +0200307 int fd, opcode;
308 int count;
309 int spec_idx;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200310 int wait_time;
Willy Tarreaucb651252008-08-29 13:57:30 +0200311 int looping = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200312
313
Willy Tarreaucb651252008-08-29 13:57:30 +0200314 re_poll_once:
Willy Tarreaude99e992007-04-16 00:53:59 +0200315 /* Here we have two options :
Willy Tarreau6653d172007-05-13 01:52:05 +0200316 * - either walk the list forwards and hope to match more events
Willy Tarreaude99e992007-04-16 00:53:59 +0200317 * - or walk it backwards to minimize the number of changes and
318 * to make better use of the cache.
319 * Tests have shown that walking backwards improves perf by 0.2%.
320 */
321
Willy Tarreau6653d172007-05-13 01:52:05 +0200322 status = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200323 spec_idx = nbspec;
324 while (likely(spec_idx > 0)) {
Willy Tarreau65745192009-03-28 21:10:48 +0100325 int done;
326
Willy Tarreaude99e992007-04-16 00:53:59 +0200327 spec_idx--;
328 fd = spec_list[spec_idx];
Willy Tarreau6653d172007-05-13 01:52:05 +0200329 eo = fd_list[fd].e; /* save old events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200330
Willy Tarreau573fd802009-03-22 19:25:46 +0100331 if (looping && --fd_created < 0) {
332 /* we were just checking the newly created FDs */
333 break;
334 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200335 /*
336 * Process the speculative events.
337 *
338 * Principle: events which are marked FD_EV_SPEC are processed
339 * with their assigned function. If the function returns 0, it
340 * means there is nothing doable without polling first. We will
341 * then convert the event to a pollable one by assigning them
342 * the WAIT status.
Willy Tarreaude99e992007-04-16 00:53:59 +0200343 */
344
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200345#ifdef DEBUG_DEV
346 if (fdtab[fd].state == FD_STCLOSE) {
347 fprintf(stderr,"fd=%d, fdtab[].ev=%x, fd_list[].e=%x, .s=%d, idx=%d\n",
348 fd, fdtab[fd].ev, fd_list[fd].e, fd_list[fd].s1, spec_idx);
349 }
350#endif
Willy Tarreau65745192009-03-28 21:10:48 +0100351 done = 0;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100352 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau6653d172007-05-13 01:52:05 +0200353 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {
354 /* The owner is interested in reading from this FD */
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200355 if (fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200356 /* Pretend there is something to read */
Willy Tarreaude99e992007-04-16 00:53:59 +0200357 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau6653d172007-05-13 01:52:05 +0200358 if (!fdtab[fd].cb[DIR_RD].f(fd))
359 fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
360 else
Willy Tarreau65745192009-03-28 21:10:48 +0100361 done = 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200362 }
363 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200364 else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {
365 /* This FD was being polled and is now being removed. */
366 fd_list[fd].e &= ~FD_EV_MASK_R;
367 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200368
Willy Tarreau6653d172007-05-13 01:52:05 +0200369 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {
370 /* The owner is interested in writing to this FD */
Willy Tarreau7a52a5c2008-08-16 16:06:02 +0200371 if (fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200372 /* Pretend there is something to write */
Willy Tarreaude99e992007-04-16 00:53:59 +0200373 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau6653d172007-05-13 01:52:05 +0200374 if (!fdtab[fd].cb[DIR_WR].f(fd))
375 fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
376 else
Willy Tarreau65745192009-03-28 21:10:48 +0100377 done = 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200378 }
379 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200380 else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {
381 /* This FD was being polled and is now being removed. */
382 fd_list[fd].e &= ~FD_EV_MASK_W;
383 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200384
Willy Tarreau65745192009-03-28 21:10:48 +0100385 status += done;
Willy Tarreau1eead502009-03-28 19:43:06 +0100386 /* one callback might already have closed the fd by itself */
387 if (fdtab[fd].state == FD_STCLOSE)
388 continue;
389
Willy Tarreau6653d172007-05-13 01:52:05 +0200390 /* Now, we will adjust the event in the poll list. Indeed, it
391 * is possible that an event which was previously in the poll
392 * list now goes out, and the opposite is possible too. We can
393 * have opposite changes for READ and WRITE too.
394 */
395
396 if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) {
397 /* poll status changed*/
398 if ((fd_list[fd].e & FD_EV_RW_PL) == 0) {
399 /* fd removed from poll list */
400 opcode = EPOLL_CTL_DEL;
401 }
402 else if ((eo & FD_EV_RW_PL) == 0) {
403 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200404 opcode = EPOLL_CTL_ADD;
405 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200406 else {
407 /* fd status changed */
408 opcode = EPOLL_CTL_MOD;
409 }
410
411 /* construct the epoll events based on new state */
412 ev.events = 0;
413 if (fd_list[fd].e & FD_EV_WAIT_R)
414 ev.events |= EPOLLIN;
415
416 if (fd_list[fd].e & FD_EV_WAIT_W)
417 ev.events |= EPOLLOUT;
418
419 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200420 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200421 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200422
Willy Tarreaude99e992007-04-16 00:53:59 +0200423
Willy Tarreau6653d172007-05-13 01:52:05 +0200424 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
425 /* This fd switched to combinations of either WAIT or
426 * IDLE. It must be removed from the spec list.
427 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200428 release_spec_entry(fd);
Willy Tarreau6653d172007-05-13 01:52:05 +0200429 continue;
Willy Tarreaude99e992007-04-16 00:53:59 +0200430 }
431 }
432
Willy Tarreau6653d172007-05-13 01:52:05 +0200433 /* It may make sense to immediately return here if there are enough
434 * processed events, without passing through epoll_wait() because we
435 * have exactly done a poll.
436 * Measures have shown a great performance increase if we call the
437 * epoll_wait() only the second time after speculative accesses have
438 * succeeded. This reduces the number of unsucessful calls to
439 * epoll_wait() by a factor of about 3, and the total number of calls
440 * by about 2.
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200441 * However, when we do that after having processed too many events,
442 * events waiting in epoll() starve for too long a time and tend to
443 * become themselves eligible for speculative polling. So we try to
444 * limit this practise to reasonable situations.
Willy Tarreaude99e992007-04-16 00:53:59 +0200445 */
Willy Tarreau6653d172007-05-13 01:52:05 +0200446
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200447 spec_processed += status;
Willy Tarreaucb651252008-08-29 13:57:30 +0200448
449 if (looping) {
450 last_skipped++;
451 return;
452 }
453
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200454 if (status >= MIN_RETURN_EVENTS && spec_processed < absmaxevents) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200455 /* We have processed at least MIN_RETURN_EVENTS, it's worth
456 * returning now without checking epoll_wait().
457 */
458 if (++last_skipped <= 1) {
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200459 tv_update_date(0, 1);
Willy Tarreaude99e992007-04-16 00:53:59 +0200460 return;
461 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200462 }
463 last_skipped = 0;
464
Willy Tarreau332740d2009-05-10 09:57:21 +0200465 if (nbspec || status || run_queue || signal_queue_len) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200466 /* Maybe we have processed some events that we must report, or
Willy Tarreau3a628112008-06-13 21:06:56 +0200467 * maybe we still have events in the spec list, or there are
468 * some tasks left pending in the run_queue, so we must not
Willy Tarreau6653d172007-05-13 01:52:05 +0200469 * wait in epoll() otherwise we will delay their delivery by
470 * the next timeout.
471 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200472 wait_time = 0;
473 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200474 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200475 if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200476 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200477 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200478 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200479 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200480 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200481 if (wait_time > MAX_DELAY_MS)
482 wait_time = MAX_DELAY_MS;
483 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200484 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200485
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200486 /* now let's wait for real events. We normally use maxpollevents as a
487 * high limit, unless <nbspec> is already big, in which case we need
488 * to compensate for the high number of events processed there.
489 */
490 fd = MIN(absmaxevents, spec_processed);
491 fd = MAX(global.tune.maxpollevents, fd);
492 fd = MIN(maxfd, fd);
Willy Tarreaucb651252008-08-29 13:57:30 +0200493 /* we want to detect if an accept() will create new speculative FDs here */
494 fd_created = 0;
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200495 spec_processed = 0;
Willy Tarreau1db37712007-06-03 17:16:49 +0200496 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200497 tv_update_date(wait_time, status);
Willy Tarreaude99e992007-04-16 00:53:59 +0200498
499 for (count = 0; count < status; count++) {
500 int e = epoll_events[count].events;
501 fd = epoll_events[count].data.fd;
502
503 /* it looks complicated but gcc can optimize it away when constants
504 * have same values.
505 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100506 DPRINTF(stderr, "%s:%d: fd=%d, ev=0x%08x, e=0x%08x\n",
507 __FUNCTION__, __LINE__,
508 fd, fdtab[fd].ev, e);
509
510 fdtab[fd].ev &= FD_POLL_STICKY;
511 fdtab[fd].ev |=
Willy Tarreaude99e992007-04-16 00:53:59 +0200512 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
513 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
514 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
515 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
516 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
517
518 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200519 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200520 continue;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100521 if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200522 fdtab[fd].cb[DIR_RD].f(fd);
523 }
524
525 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200526 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200527 continue;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100528 if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200529 fdtab[fd].cb[DIR_WR].f(fd);
530 }
531 }
Willy Tarreaucb651252008-08-29 13:57:30 +0200532
533 if (fd_created) {
534 /* we have created some fds, certainly in return of an accept(),
535 * and they're marked as speculative. If we can manage to perform
536 * a read(), we're almost sure to collect all the request at once
537 * and avoid several expensive wakeups. So let's try now. Anyway,
538 * if we fail, the tasks are still woken up, and the FD gets marked
539 * for poll mode.
540 */
Willy Tarreaucb651252008-08-29 13:57:30 +0200541 looping = 1;
542 goto re_poll_once;
543 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200544}
545
546/*
547 * Initialization of the speculative epoll() poller.
548 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
549 * disables the poller by setting its pref to 0.
550 */
551REGPRM1 static int _do_init(struct poller *p)
552{
553 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
554
555 p->private = NULL;
556
557 epoll_fd = epoll_create(global.maxsock + 1);
558 if (epoll_fd < 0)
559 goto fail_fd;
560
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200561 /* See comments at the top of the file about this formula. */
562 absmaxevents = MAX(global.tune.maxpollevents, global.maxsock/4);
Willy Tarreaude99e992007-04-16 00:53:59 +0200563 epoll_events = (struct epoll_event*)
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200564 calloc(1, sizeof(struct epoll_event) * absmaxevents);
Willy Tarreaude99e992007-04-16 00:53:59 +0200565
566 if (epoll_events == NULL)
567 goto fail_ee;
568
569 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
570 goto fail_spec;
571
572 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
573 if (fd_list == NULL)
574 goto fail_fd_list;
575
576 return 1;
577
578 fail_fd_list:
579 free(spec_list);
580 fail_spec:
581 free(epoll_events);
582 fail_ee:
583 close(epoll_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200584 epoll_fd = -1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200585 fail_fd:
586 p->pref = 0;
587 return 0;
588}
589
590/*
591 * Termination of the speculative epoll() poller.
592 * Memory is released and the poller is marked as unselectable.
593 */
594REGPRM1 static void _do_term(struct poller *p)
595{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200596 free(fd_list);
597 free(spec_list);
598 free(epoll_events);
Willy Tarreaude99e992007-04-16 00:53:59 +0200599
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200600 if (epoll_fd >= 0) {
601 close(epoll_fd);
602 epoll_fd = -1;
603 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200604
605 fd_list = NULL;
606 spec_list = NULL;
607 epoll_events = NULL;
608
609 p->private = NULL;
610 p->pref = 0;
611}
612
613/*
614 * Check that the poller works.
615 * Returns 1 if OK, otherwise 0.
616 */
617REGPRM1 static int _do_test(struct poller *p)
618{
619 int fd;
620
621 fd = epoll_create(global.maxsock + 1);
622 if (fd < 0)
623 return 0;
624 close(fd);
625 return 1;
626}
627
628/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200629 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
630 * otherwise 0. It will ensure that all processes will not share their
631 * epoll_fd. Some side effects were encountered because of this, such
632 * as epoll_wait() returning an FD which was previously deleted.
633 */
634REGPRM1 static int _do_fork(struct poller *p)
635{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200636 if (epoll_fd >= 0)
637 close(epoll_fd);
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200638 epoll_fd = epoll_create(global.maxsock + 1);
639 if (epoll_fd < 0)
640 return 0;
641 return 1;
642}
643
644/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200645 * It is a constructor, which means that it will automatically be called before
646 * main(). This is GCC-specific but it works at least since 2.95.
647 * Special care must be taken so that it does not need any uninitialized data.
648 */
649__attribute__((constructor))
650static void _do_register(void)
651{
652 struct poller *p;
653
654 if (nbpollers >= MAX_POLLERS)
655 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200656
657 epoll_fd = -1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200658 p = &pollers[nbpollers++];
659
660 p->name = "sepoll";
661 p->pref = 400;
662 p->private = NULL;
663
664 p->test = _do_test;
665 p->init = _do_init;
666 p->term = _do_term;
667 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200668 p->fork = _do_fork;
Willy Tarreaude99e992007-04-16 00:53:59 +0200669
670 p->is_set = __fd_is_set;
671 p->cond_s = p->set = __fd_set;
672 p->cond_c = p->clr = __fd_clr;
673 p->rem = __fd_rem;
674 p->clo = __fd_clo;
675}
676
677
678/*
679 * Local variables:
680 * c-indent-level: 8
681 * c-basic-offset: 8
682 * End:
683 */