blob: 2419b684a66b5101b0e1e4b2d0e7b71efe49869a [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
59#include <types/fd.h>
60#include <types/global.h>
61
62#include <proto/fd.h>
63#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
89 * indexes right now.
90 *
91 * The STOP state requires the event to be present in the spec list so that
92 * it can be detected and flushed upon next scan without having to scan the
93 * whole FD list.
94 *
95 * This translates like this :
96 *
97 * EVENT_IN_SPEC_LIST = 01
98 * EVENT_IN_POLL_LIST = 10
99 *
100 * IDLE = 0
101 * SPEC = (EVENT_IN_SPEC_LIST)
102 * WAIT = (EVENT_IN_POLL_LIST)
103 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
104 *
105 * fd_is_set() just consists in checking that the status is 01 or 10.
106 *
107 * For efficiency reasons, we will store the Read and Write bits interlaced to
108 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
109 * get what we want :
110 * 3 2 1 0
111 * Wp Rp Ws Rs
112 *
113 * The FD array has to hold a back reference to the speculative list. This
114 * reference is only valid if at least one of the directions is marked SPEC.
115 *
116 */
117
118#define FD_EV_IN_SL 1
119#define FD_EV_IN_PL 4
120
121#define FD_EV_IDLE 0
122#define FD_EV_SPEC (FD_EV_IN_SL)
123#define FD_EV_WAIT (FD_EV_IN_PL)
124#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
125
126/* Those match any of R or W for Spec list or Poll list */
127#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
128#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
129#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
130
131#define FD_EV_IDLE_R 0
132#define FD_EV_SPEC_R (FD_EV_IN_SL)
133#define FD_EV_WAIT_R (FD_EV_IN_PL)
134#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
135#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
136
137#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
138#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
139#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
140#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
141#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
142
143#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
144
Willy Tarreau6653d172007-05-13 01:52:05 +0200145/* This is the minimum number of events successfully processed in speculative
146 * mode above which we agree to return without checking epoll() (1/2 times).
147 */
148#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200149
150/* descriptor of one FD.
151 * FIXME: should be a bit field */
152struct fd_status {
153 unsigned int e:4; // read and write events status.
Willy Tarreau4eac2092007-08-31 17:01:18 +0200154 unsigned int s1:28; // Position in spec list+1. 0=not in list. Should be last.
Willy Tarreaude99e992007-04-16 00:53:59 +0200155};
156
157static int nbspec = 0; // current size of the spec list
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200158static int absmaxevents = 0; // absolute maximum amounts of polled events
Willy Tarreaude99e992007-04-16 00:53:59 +0200159
160static struct fd_status *fd_list = NULL; // list of FDs
161static unsigned int *spec_list = NULL; // speculative I/O list
162
163/* private data */
164static struct epoll_event *epoll_events;
165static int epoll_fd;
166
167/* This structure may be used for any purpose. Warning! do not use it in
168 * recursive functions !
169 */
170static struct epoll_event ev;
171
172
173REGPRM1 static void alloc_spec_entry(const int fd)
174{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200175 if (fd_list[fd].s1)
Willy Tarreaude99e992007-04-16 00:53:59 +0200176 return;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200177 fd_list[fd].s1 = nbspec + 1;
178 spec_list[nbspec] = fd;
179 nbspec++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200180}
181
Willy Tarreau4eac2092007-08-31 17:01:18 +0200182/* Removes entry used by fd <fd> from the spec list and replaces it with the
183 * last one. The fd_list is adjusted to match the back reference if needed.
184 * If the fd has no entry assigned, return immediately.
Willy Tarreaude99e992007-04-16 00:53:59 +0200185 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200186REGPRM1 static void release_spec_entry(int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200187{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200188 unsigned int pos;
189
190 pos = fd_list[fd].s1;
191 if (!pos)
192 return;
193
194 fd_list[fd].s1 = 0;
195 pos--;
196 /* we have spec_list[pos]==fd */
Willy Tarreaude99e992007-04-16 00:53:59 +0200197
198 nbspec--;
199 if (pos == nbspec)
200 return;
201
Willy Tarreau4eac2092007-08-31 17:01:18 +0200202 /* we replace current FD by the highest one, which may sometimes be the same */
Willy Tarreaude99e992007-04-16 00:53:59 +0200203 fd = spec_list[nbspec];
Willy Tarreau4eac2092007-08-31 17:01:18 +0200204 fd_list[fd].s1 = pos + 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200205 spec_list[pos] = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200206}
207
208/*
209 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
210 */
211REGPRM2 static int __fd_is_set(const int fd, int dir)
212{
213 int ret;
214
215 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
216 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
217}
218
219/*
220 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
221 * designed like this in order to reduce the number of jumps (verified).
222 */
223REGPRM2 static int __fd_set(const int fd, int dir)
224{
225 __label__ switch_state;
226 unsigned int i;
227
228 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
229
230 if (i == FD_EV_IDLE) {
231 // switch to SPEC state and allocate a SPEC entry.
232 alloc_spec_entry(fd);
233 switch_state:
234 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
235 return 1;
236 }
237 else if (i == FD_EV_STOP) {
238 // switch to WAIT state
239 goto switch_state;
240 }
241 else
242 return 0;
243}
244
245REGPRM2 static int __fd_clr(const int fd, int dir)
246{
247 __label__ switch_state;
248 unsigned int i;
249
250 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
251
252 if (i == FD_EV_SPEC) {
253 // switch to IDLE state
254 goto switch_state;
255 }
256 else if (likely(i == FD_EV_WAIT)) {
257 // switch to STOP state
258 /* We will create a queue entry for this one because we want to
259 * process it later in order to merge it with other events on
260 * the same FD.
261 */
262 alloc_spec_entry(fd);
263 switch_state:
264 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
265 return 1;
266 }
267 return 0;
268}
269
Willy Tarreau6653d172007-05-13 01:52:05 +0200270/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200271REGPRM1 static void __fd_rem(int fd)
272{
273 __fd_clr(fd, DIR_RD);
274 __fd_clr(fd, DIR_WR);
275}
276
277/*
278 * On valid epoll() implementations, a call to close() automatically removes
279 * the fds. This means that the FD will appear as previously unset.
280 */
281REGPRM1 static void __fd_clo(int fd)
282{
283 if (fd_list[fd].e & FD_EV_RW_SL)
Willy Tarreau4eac2092007-08-31 17:01:18 +0200284 release_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200285 fd_list[fd].e &= ~(FD_EV_MASK);
286}
287
Willy Tarreaudc246a72007-05-09 21:57:51 +0200288/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200289 * speculative epoll() poller
290 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200291REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreaude99e992007-04-16 00:53:59 +0200292{
293 static unsigned int last_skipped;
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200294 static unsigned int spec_processed;
Willy Tarreau6653d172007-05-13 01:52:05 +0200295 int status, eo;
Willy Tarreaude99e992007-04-16 00:53:59 +0200296 int fd, opcode;
297 int count;
298 int spec_idx;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200299 int wait_time;
Willy Tarreaude99e992007-04-16 00:53:59 +0200300
301
302 /* Here we have two options :
Willy Tarreau6653d172007-05-13 01:52:05 +0200303 * - either walk the list forwards and hope to match more events
Willy Tarreaude99e992007-04-16 00:53:59 +0200304 * - or walk it backwards to minimize the number of changes and
305 * to make better use of the cache.
306 * Tests have shown that walking backwards improves perf by 0.2%.
307 */
308
Willy Tarreau6653d172007-05-13 01:52:05 +0200309 status = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200310 spec_idx = nbspec;
311 while (likely(spec_idx > 0)) {
312 spec_idx--;
313 fd = spec_list[spec_idx];
Willy Tarreau6653d172007-05-13 01:52:05 +0200314 eo = fd_list[fd].e; /* save old events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200315
Willy Tarreau6653d172007-05-13 01:52:05 +0200316 /*
317 * Process the speculative events.
318 *
319 * Principle: events which are marked FD_EV_SPEC are processed
320 * with their assigned function. If the function returns 0, it
321 * means there is nothing doable without polling first. We will
322 * then convert the event to a pollable one by assigning them
323 * the WAIT status.
Willy Tarreaude99e992007-04-16 00:53:59 +0200324 */
325
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100326 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau6653d172007-05-13 01:52:05 +0200327 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {
328 /* The owner is interested in reading from this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200329 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200330 /* Pretend there is something to read */
Willy Tarreaude99e992007-04-16 00:53:59 +0200331 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau6653d172007-05-13 01:52:05 +0200332 if (!fdtab[fd].cb[DIR_RD].f(fd))
333 fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
334 else
335 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200336 }
337 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200338 else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {
339 /* This FD was being polled and is now being removed. */
340 fd_list[fd].e &= ~FD_EV_MASK_R;
341 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200342
Willy Tarreau6653d172007-05-13 01:52:05 +0200343 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {
344 /* The owner is interested in writing to this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200345 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200346 /* Pretend there is something to write */
Willy Tarreaude99e992007-04-16 00:53:59 +0200347 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau6653d172007-05-13 01:52:05 +0200348 if (!fdtab[fd].cb[DIR_WR].f(fd))
349 fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
350 else
351 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200352 }
353 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200354 else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {
355 /* This FD was being polled and is now being removed. */
356 fd_list[fd].e &= ~FD_EV_MASK_W;
357 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200358
Willy Tarreau6653d172007-05-13 01:52:05 +0200359 /* Now, we will adjust the event in the poll list. Indeed, it
360 * is possible that an event which was previously in the poll
361 * list now goes out, and the opposite is possible too. We can
362 * have opposite changes for READ and WRITE too.
363 */
364
365 if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) {
366 /* poll status changed*/
367 if ((fd_list[fd].e & FD_EV_RW_PL) == 0) {
368 /* fd removed from poll list */
369 opcode = EPOLL_CTL_DEL;
370 }
371 else if ((eo & FD_EV_RW_PL) == 0) {
372 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200373 opcode = EPOLL_CTL_ADD;
374 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200375 else {
376 /* fd status changed */
377 opcode = EPOLL_CTL_MOD;
378 }
379
380 /* construct the epoll events based on new state */
381 ev.events = 0;
382 if (fd_list[fd].e & FD_EV_WAIT_R)
383 ev.events |= EPOLLIN;
384
385 if (fd_list[fd].e & FD_EV_WAIT_W)
386 ev.events |= EPOLLOUT;
387
388 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200389 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200390 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200391
Willy Tarreaude99e992007-04-16 00:53:59 +0200392
Willy Tarreau6653d172007-05-13 01:52:05 +0200393 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
394 /* This fd switched to combinations of either WAIT or
395 * IDLE. It must be removed from the spec list.
396 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200397 release_spec_entry(fd);
Willy Tarreau6653d172007-05-13 01:52:05 +0200398 continue;
Willy Tarreaude99e992007-04-16 00:53:59 +0200399 }
400 }
401
Willy Tarreau6653d172007-05-13 01:52:05 +0200402 /* It may make sense to immediately return here if there are enough
403 * processed events, without passing through epoll_wait() because we
404 * have exactly done a poll.
405 * Measures have shown a great performance increase if we call the
406 * epoll_wait() only the second time after speculative accesses have
407 * succeeded. This reduces the number of unsucessful calls to
408 * epoll_wait() by a factor of about 3, and the total number of calls
409 * by about 2.
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200410 * However, when we do that after having processed too many events,
411 * events waiting in epoll() starve for too long a time and tend to
412 * become themselves eligible for speculative polling. So we try to
413 * limit this practise to reasonable situations.
Willy Tarreaude99e992007-04-16 00:53:59 +0200414 */
Willy Tarreau6653d172007-05-13 01:52:05 +0200415
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200416 spec_processed += status;
417 if (status >= MIN_RETURN_EVENTS && spec_processed < absmaxevents) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200418 /* We have processed at least MIN_RETURN_EVENTS, it's worth
419 * returning now without checking epoll_wait().
420 */
421 if (++last_skipped <= 1) {
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200422 tv_update_date(0, 1);
Willy Tarreaude99e992007-04-16 00:53:59 +0200423 return;
424 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200425 }
426 last_skipped = 0;
427
Willy Tarreau3a628112008-06-13 21:06:56 +0200428 if (nbspec || status || run_queue) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200429 /* Maybe we have processed some events that we must report, or
Willy Tarreau3a628112008-06-13 21:06:56 +0200430 * maybe we still have events in the spec list, or there are
431 * some tasks left pending in the run_queue, so we must not
Willy Tarreau6653d172007-05-13 01:52:05 +0200432 * wait in epoll() otherwise we will delay their delivery by
433 * the next timeout.
434 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200435 wait_time = 0;
436 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200437 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200438 if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200439 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200440 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200441 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200442 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200443 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200444 if (wait_time > MAX_DELAY_MS)
445 wait_time = MAX_DELAY_MS;
446 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200447 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200448
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200449 /* now let's wait for real events. We normally use maxpollevents as a
450 * high limit, unless <nbspec> is already big, in which case we need
451 * to compensate for the high number of events processed there.
452 */
453 fd = MIN(absmaxevents, spec_processed);
454 fd = MAX(global.tune.maxpollevents, fd);
455 fd = MIN(maxfd, fd);
456 spec_processed = 0;
Willy Tarreau1db37712007-06-03 17:16:49 +0200457 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200458 tv_update_date(wait_time, status);
Willy Tarreaude99e992007-04-16 00:53:59 +0200459
460 for (count = 0; count < status; count++) {
461 int e = epoll_events[count].events;
462 fd = epoll_events[count].data.fd;
463
464 /* it looks complicated but gcc can optimize it away when constants
465 * have same values.
466 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100467 DPRINTF(stderr, "%s:%d: fd=%d, ev=0x%08x, e=0x%08x\n",
468 __FUNCTION__, __LINE__,
469 fd, fdtab[fd].ev, e);
470
471 fdtab[fd].ev &= FD_POLL_STICKY;
472 fdtab[fd].ev |=
Willy Tarreaude99e992007-04-16 00:53:59 +0200473 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
474 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
475 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
476 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
477 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
478
479 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200480 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200481 continue;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100482 if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200483 fdtab[fd].cb[DIR_RD].f(fd);
484 }
485
486 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200487 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200488 continue;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100489 if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200490 fdtab[fd].cb[DIR_WR].f(fd);
491 }
492 }
493}
494
495/*
496 * Initialization of the speculative epoll() poller.
497 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
498 * disables the poller by setting its pref to 0.
499 */
500REGPRM1 static int _do_init(struct poller *p)
501{
502 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
503
504 p->private = NULL;
505
506 epoll_fd = epoll_create(global.maxsock + 1);
507 if (epoll_fd < 0)
508 goto fail_fd;
509
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200510 /* See comments at the top of the file about this formula. */
511 absmaxevents = MAX(global.tune.maxpollevents, global.maxsock/4);
Willy Tarreaude99e992007-04-16 00:53:59 +0200512 epoll_events = (struct epoll_event*)
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200513 calloc(1, sizeof(struct epoll_event) * absmaxevents);
Willy Tarreaude99e992007-04-16 00:53:59 +0200514
515 if (epoll_events == NULL)
516 goto fail_ee;
517
518 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
519 goto fail_spec;
520
521 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
522 if (fd_list == NULL)
523 goto fail_fd_list;
524
525 return 1;
526
527 fail_fd_list:
528 free(spec_list);
529 fail_spec:
530 free(epoll_events);
531 fail_ee:
532 close(epoll_fd);
533 epoll_fd = 0;
534 fail_fd:
535 p->pref = 0;
536 return 0;
537}
538
539/*
540 * Termination of the speculative epoll() poller.
541 * Memory is released and the poller is marked as unselectable.
542 */
543REGPRM1 static void _do_term(struct poller *p)
544{
545 if (fd_list)
546 free(fd_list);
547 if (spec_list)
548 free(spec_list);
549 if (epoll_events)
550 free(epoll_events);
551
552 close(epoll_fd);
553 epoll_fd = 0;
554
555 fd_list = NULL;
556 spec_list = NULL;
557 epoll_events = NULL;
558
559 p->private = NULL;
560 p->pref = 0;
561}
562
563/*
564 * Check that the poller works.
565 * Returns 1 if OK, otherwise 0.
566 */
567REGPRM1 static int _do_test(struct poller *p)
568{
569 int fd;
570
571 fd = epoll_create(global.maxsock + 1);
572 if (fd < 0)
573 return 0;
574 close(fd);
575 return 1;
576}
577
578/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200579 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
580 * otherwise 0. It will ensure that all processes will not share their
581 * epoll_fd. Some side effects were encountered because of this, such
582 * as epoll_wait() returning an FD which was previously deleted.
583 */
584REGPRM1 static int _do_fork(struct poller *p)
585{
586 close(epoll_fd);
587 epoll_fd = epoll_create(global.maxsock + 1);
588 if (epoll_fd < 0)
589 return 0;
590 return 1;
591}
592
593/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200594 * It is a constructor, which means that it will automatically be called before
595 * main(). This is GCC-specific but it works at least since 2.95.
596 * Special care must be taken so that it does not need any uninitialized data.
597 */
598__attribute__((constructor))
599static void _do_register(void)
600{
601 struct poller *p;
602
603 if (nbpollers >= MAX_POLLERS)
604 return;
605 p = &pollers[nbpollers++];
606
607 p->name = "sepoll";
608 p->pref = 400;
609 p->private = NULL;
610
611 p->test = _do_test;
612 p->init = _do_init;
613 p->term = _do_term;
614 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200615 p->fork = _do_fork;
Willy Tarreaude99e992007-04-16 00:53:59 +0200616
617 p->is_set = __fd_is_set;
618 p->cond_s = p->set = __fd_set;
619 p->cond_c = p->clr = __fd_clr;
620 p->rem = __fd_rem;
621 p->clo = __fd_clo;
622}
623
624
625/*
626 * Local variables:
627 * c-indent-level: 8
628 * c-basic-offset: 8
629 * End:
630 */