blob: f42a97f296b89c3fded21082813db4c7dc562af5 [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>
55#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020056#include <common/tools.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020057
58#include <types/fd.h>
59#include <types/global.h>
60
61#include <proto/fd.h>
62#include <proto/task.h>
63
64#if defined(USE_MY_EPOLL)
65#include <common/epoll.h>
66#include <errno.h>
67#include <sys/syscall.h>
68static _syscall1 (int, epoll_create, int, size);
69static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
70static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
71#else
72#include <sys/epoll.h>
73#endif
74
75/*
76 * We define 4 states for each direction of a file descriptor, which we store
77 * as 2 bits :
78 *
79 * 00 = IDLE : we're not interested in this event
80 * 01 = SPEC : perform speculative I/O on this FD
81 * 10 = WAIT : really wait for an availability event on this FD (poll)
82 * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if
83 * the application changes its mind, otherwise disable FD polling
84 * and switch back to IDLE.
85 *
86 * Since we do not want to scan all the FD list to find speculative I/O events,
87 * we store them in a list consisting in a linear array holding only the FD
88 * indexes right now.
89 *
90 * The STOP state requires the event to be present in the spec list so that
91 * it can be detected and flushed upon next scan without having to scan the
92 * whole FD list.
93 *
94 * This translates like this :
95 *
96 * EVENT_IN_SPEC_LIST = 01
97 * EVENT_IN_POLL_LIST = 10
98 *
99 * IDLE = 0
100 * SPEC = (EVENT_IN_SPEC_LIST)
101 * WAIT = (EVENT_IN_POLL_LIST)
102 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
103 *
104 * fd_is_set() just consists in checking that the status is 01 or 10.
105 *
106 * For efficiency reasons, we will store the Read and Write bits interlaced to
107 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
108 * get what we want :
109 * 3 2 1 0
110 * Wp Rp Ws Rs
111 *
112 * The FD array has to hold a back reference to the speculative list. This
113 * reference is only valid if at least one of the directions is marked SPEC.
114 *
115 */
116
117#define FD_EV_IN_SL 1
118#define FD_EV_IN_PL 4
119
120#define FD_EV_IDLE 0
121#define FD_EV_SPEC (FD_EV_IN_SL)
122#define FD_EV_WAIT (FD_EV_IN_PL)
123#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
124
125/* Those match any of R or W for Spec list or Poll list */
126#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
127#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
128#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
129
130#define FD_EV_IDLE_R 0
131#define FD_EV_SPEC_R (FD_EV_IN_SL)
132#define FD_EV_WAIT_R (FD_EV_IN_PL)
133#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
134#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
135
136#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
137#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
138#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
139#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
140#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
141
142#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
143
Willy Tarreau6653d172007-05-13 01:52:05 +0200144/* This is the minimum number of events successfully processed in speculative
145 * mode above which we agree to return without checking epoll() (1/2 times).
146 */
147#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200148
149/* descriptor of one FD.
150 * FIXME: should be a bit field */
151struct fd_status {
152 unsigned int e:4; // read and write events status.
Willy Tarreau4eac2092007-08-31 17:01:18 +0200153 unsigned int s1:28; // Position in spec list+1. 0=not in list. Should be last.
Willy Tarreaude99e992007-04-16 00:53:59 +0200154};
155
156static int nbspec = 0; // current size of the spec list
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200157static int absmaxevents = 0; // absolute maximum amounts of polled events
Willy Tarreaude99e992007-04-16 00:53:59 +0200158
159static struct fd_status *fd_list = NULL; // list of FDs
160static unsigned int *spec_list = NULL; // speculative I/O list
161
162/* private data */
163static struct epoll_event *epoll_events;
164static int epoll_fd;
165
166/* This structure may be used for any purpose. Warning! do not use it in
167 * recursive functions !
168 */
169static struct epoll_event ev;
170
171
172REGPRM1 static void alloc_spec_entry(const int fd)
173{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200174 if (fd_list[fd].s1)
Willy Tarreaude99e992007-04-16 00:53:59 +0200175 return;
Willy Tarreau4eac2092007-08-31 17:01:18 +0200176 fd_list[fd].s1 = nbspec + 1;
177 spec_list[nbspec] = fd;
178 nbspec++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200179}
180
Willy Tarreau4eac2092007-08-31 17:01:18 +0200181/* Removes entry used by fd <fd> from the spec list and replaces it with the
182 * last one. The fd_list is adjusted to match the back reference if needed.
183 * If the fd has no entry assigned, return immediately.
Willy Tarreaude99e992007-04-16 00:53:59 +0200184 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200185REGPRM1 static void release_spec_entry(int fd)
Willy Tarreaude99e992007-04-16 00:53:59 +0200186{
Willy Tarreau4eac2092007-08-31 17:01:18 +0200187 unsigned int pos;
188
189 pos = fd_list[fd].s1;
190 if (!pos)
191 return;
192
193 fd_list[fd].s1 = 0;
194 pos--;
195 /* we have spec_list[pos]==fd */
Willy Tarreaude99e992007-04-16 00:53:59 +0200196
197 nbspec--;
198 if (pos == nbspec)
199 return;
200
Willy Tarreau4eac2092007-08-31 17:01:18 +0200201 /* we replace current FD by the highest one, which may sometimes be the same */
Willy Tarreaude99e992007-04-16 00:53:59 +0200202 fd = spec_list[nbspec];
Willy Tarreau4eac2092007-08-31 17:01:18 +0200203 fd_list[fd].s1 = pos + 1;
Willy Tarreaude99e992007-04-16 00:53:59 +0200204 spec_list[pos] = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200205}
206
207/*
208 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
209 */
210REGPRM2 static int __fd_is_set(const int fd, int dir)
211{
212 int ret;
213
214 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
215 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
216}
217
218/*
219 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
220 * designed like this in order to reduce the number of jumps (verified).
221 */
222REGPRM2 static int __fd_set(const int fd, int dir)
223{
224 __label__ switch_state;
225 unsigned int i;
226
227 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
228
229 if (i == FD_EV_IDLE) {
230 // switch to SPEC state and allocate a SPEC entry.
231 alloc_spec_entry(fd);
232 switch_state:
233 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
234 return 1;
235 }
236 else if (i == FD_EV_STOP) {
237 // switch to WAIT state
238 goto switch_state;
239 }
240 else
241 return 0;
242}
243
244REGPRM2 static int __fd_clr(const int fd, int dir)
245{
246 __label__ switch_state;
247 unsigned int i;
248
249 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
250
251 if (i == FD_EV_SPEC) {
252 // switch to IDLE state
253 goto switch_state;
254 }
255 else if (likely(i == FD_EV_WAIT)) {
256 // switch to STOP state
257 /* We will create a queue entry for this one because we want to
258 * process it later in order to merge it with other events on
259 * the same FD.
260 */
261 alloc_spec_entry(fd);
262 switch_state:
263 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
264 return 1;
265 }
266 return 0;
267}
268
Willy Tarreau6653d172007-05-13 01:52:05 +0200269/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200270REGPRM1 static void __fd_rem(int fd)
271{
272 __fd_clr(fd, DIR_RD);
273 __fd_clr(fd, DIR_WR);
274}
275
276/*
277 * On valid epoll() implementations, a call to close() automatically removes
278 * the fds. This means that the FD will appear as previously unset.
279 */
280REGPRM1 static void __fd_clo(int fd)
281{
282 if (fd_list[fd].e & FD_EV_RW_SL)
Willy Tarreau4eac2092007-08-31 17:01:18 +0200283 release_spec_entry(fd);
Willy Tarreaude99e992007-04-16 00:53:59 +0200284 fd_list[fd].e &= ~(FD_EV_MASK);
285}
286
Willy Tarreaudc246a72007-05-09 21:57:51 +0200287/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200288 * speculative epoll() poller
289 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200290REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp)
Willy Tarreaude99e992007-04-16 00:53:59 +0200291{
292 static unsigned int last_skipped;
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200293 static unsigned int spec_processed;
Willy Tarreau6653d172007-05-13 01:52:05 +0200294 int status, eo;
Willy Tarreaude99e992007-04-16 00:53:59 +0200295 int fd, opcode;
296 int count;
297 int spec_idx;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200298 int wait_time;
Willy Tarreaude99e992007-04-16 00:53:59 +0200299
300
301 /* Here we have two options :
Willy Tarreau6653d172007-05-13 01:52:05 +0200302 * - either walk the list forwards and hope to match more events
Willy Tarreaude99e992007-04-16 00:53:59 +0200303 * - or walk it backwards to minimize the number of changes and
304 * to make better use of the cache.
305 * Tests have shown that walking backwards improves perf by 0.2%.
306 */
307
Willy Tarreau6653d172007-05-13 01:52:05 +0200308 status = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200309 spec_idx = nbspec;
310 while (likely(spec_idx > 0)) {
311 spec_idx--;
312 fd = spec_list[spec_idx];
Willy Tarreau6653d172007-05-13 01:52:05 +0200313 eo = fd_list[fd].e; /* save old events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200314
Willy Tarreau6653d172007-05-13 01:52:05 +0200315 /*
316 * Process the speculative events.
317 *
318 * Principle: events which are marked FD_EV_SPEC are processed
319 * with their assigned function. If the function returns 0, it
320 * means there is nothing doable without polling first. We will
321 * then convert the event to a pollable one by assigning them
322 * the WAIT status.
Willy Tarreaude99e992007-04-16 00:53:59 +0200323 */
324
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100325 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau6653d172007-05-13 01:52:05 +0200326 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {
327 /* The owner is interested in reading from this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200328 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200329 /* Pretend there is something to read */
Willy Tarreaude99e992007-04-16 00:53:59 +0200330 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau6653d172007-05-13 01:52:05 +0200331 if (!fdtab[fd].cb[DIR_RD].f(fd))
332 fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
333 else
334 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200335 }
336 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200337 else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {
338 /* This FD was being polled and is now being removed. */
339 fd_list[fd].e &= ~FD_EV_MASK_R;
340 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200341
Willy Tarreau6653d172007-05-13 01:52:05 +0200342 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {
343 /* The owner is interested in writing to this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200344 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200345 /* Pretend there is something to write */
Willy Tarreaude99e992007-04-16 00:53:59 +0200346 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau6653d172007-05-13 01:52:05 +0200347 if (!fdtab[fd].cb[DIR_WR].f(fd))
348 fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
349 else
350 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200351 }
352 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200353 else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {
354 /* This FD was being polled and is now being removed. */
355 fd_list[fd].e &= ~FD_EV_MASK_W;
356 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200357
Willy Tarreau6653d172007-05-13 01:52:05 +0200358 /* Now, we will adjust the event in the poll list. Indeed, it
359 * is possible that an event which was previously in the poll
360 * list now goes out, and the opposite is possible too. We can
361 * have opposite changes for READ and WRITE too.
362 */
363
364 if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) {
365 /* poll status changed*/
366 if ((fd_list[fd].e & FD_EV_RW_PL) == 0) {
367 /* fd removed from poll list */
368 opcode = EPOLL_CTL_DEL;
369 }
370 else if ((eo & FD_EV_RW_PL) == 0) {
371 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200372 opcode = EPOLL_CTL_ADD;
373 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200374 else {
375 /* fd status changed */
376 opcode = EPOLL_CTL_MOD;
377 }
378
379 /* construct the epoll events based on new state */
380 ev.events = 0;
381 if (fd_list[fd].e & FD_EV_WAIT_R)
382 ev.events |= EPOLLIN;
383
384 if (fd_list[fd].e & FD_EV_WAIT_W)
385 ev.events |= EPOLLOUT;
386
387 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200388 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200389 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200390
Willy Tarreaude99e992007-04-16 00:53:59 +0200391
Willy Tarreau6653d172007-05-13 01:52:05 +0200392 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
393 /* This fd switched to combinations of either WAIT or
394 * IDLE. It must be removed from the spec list.
395 */
Willy Tarreau4eac2092007-08-31 17:01:18 +0200396 release_spec_entry(fd);
Willy Tarreau6653d172007-05-13 01:52:05 +0200397 continue;
Willy Tarreaude99e992007-04-16 00:53:59 +0200398 }
399 }
400
Willy Tarreau6653d172007-05-13 01:52:05 +0200401 /* It may make sense to immediately return here if there are enough
402 * processed events, without passing through epoll_wait() because we
403 * have exactly done a poll.
404 * Measures have shown a great performance increase if we call the
405 * epoll_wait() only the second time after speculative accesses have
406 * succeeded. This reduces the number of unsucessful calls to
407 * epoll_wait() by a factor of about 3, and the total number of calls
408 * by about 2.
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200409 * However, when we do that after having processed too many events,
410 * events waiting in epoll() starve for too long a time and tend to
411 * become themselves eligible for speculative polling. So we try to
412 * limit this practise to reasonable situations.
Willy Tarreaude99e992007-04-16 00:53:59 +0200413 */
Willy Tarreau6653d172007-05-13 01:52:05 +0200414
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200415 spec_processed += status;
416 if (status >= MIN_RETURN_EVENTS && spec_processed < absmaxevents) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200417 /* We have processed at least MIN_RETURN_EVENTS, it's worth
418 * returning now without checking epoll_wait().
419 */
420 if (++last_skipped <= 1) {
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200421 tv_update_date(0, 1);
Willy Tarreaude99e992007-04-16 00:53:59 +0200422 return;
423 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200424 }
425 last_skipped = 0;
426
Willy Tarreau3a628112008-06-13 21:06:56 +0200427 if (nbspec || status || run_queue) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200428 /* Maybe we have processed some events that we must report, or
Willy Tarreau3a628112008-06-13 21:06:56 +0200429 * maybe we still have events in the spec list, or there are
430 * some tasks left pending in the run_queue, so we must not
Willy Tarreau6653d172007-05-13 01:52:05 +0200431 * wait in epoll() otherwise we will delay their delivery by
432 * the next timeout.
433 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200434 wait_time = 0;
435 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200436 else {
Willy Tarreaubdefc512007-05-14 02:02:04 +0200437 if (tv_iseternity(exp))
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200438 wait_time = MAX_DELAY_MS;
Willy Tarreaubdefc512007-05-14 02:02:04 +0200439 else if (tv_isge(&now, exp))
440 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200441 else {
Willy Tarreaubdefc512007-05-14 02:02:04 +0200442 wait_time = __tv_ms_elapsed(&now, exp) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200443 if (wait_time > MAX_DELAY_MS)
444 wait_time = MAX_DELAY_MS;
445 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200446 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200447
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200448 /* now let's wait for real events. We normally use maxpollevents as a
449 * high limit, unless <nbspec> is already big, in which case we need
450 * to compensate for the high number of events processed there.
451 */
452 fd = MIN(absmaxevents, spec_processed);
453 fd = MAX(global.tune.maxpollevents, fd);
454 fd = MIN(maxfd, fd);
455 spec_processed = 0;
Willy Tarreau1db37712007-06-03 17:16:49 +0200456 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200457 tv_update_date(wait_time, status);
Willy Tarreaude99e992007-04-16 00:53:59 +0200458
459 for (count = 0; count < status; count++) {
460 int e = epoll_events[count].events;
461 fd = epoll_events[count].data.fd;
462
463 /* it looks complicated but gcc can optimize it away when constants
464 * have same values.
465 */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100466 DPRINTF(stderr, "%s:%d: fd=%d, ev=0x%08x, e=0x%08x\n",
467 __FUNCTION__, __LINE__,
468 fd, fdtab[fd].ev, e);
469
470 fdtab[fd].ev &= FD_POLL_STICKY;
471 fdtab[fd].ev |=
Willy Tarreaude99e992007-04-16 00:53:59 +0200472 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
473 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
474 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
475 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
476 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
477
478 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200479 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200480 continue;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100481 if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200482 fdtab[fd].cb[DIR_RD].f(fd);
483 }
484
485 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200486 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200487 continue;
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100488 if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200489 fdtab[fd].cb[DIR_WR].f(fd);
490 }
491 }
492}
493
494/*
495 * Initialization of the speculative epoll() poller.
496 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
497 * disables the poller by setting its pref to 0.
498 */
499REGPRM1 static int _do_init(struct poller *p)
500{
501 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
502
503 p->private = NULL;
504
505 epoll_fd = epoll_create(global.maxsock + 1);
506 if (epoll_fd < 0)
507 goto fail_fd;
508
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200509 /* See comments at the top of the file about this formula. */
510 absmaxevents = MAX(global.tune.maxpollevents, global.maxsock/4);
Willy Tarreaude99e992007-04-16 00:53:59 +0200511 epoll_events = (struct epoll_event*)
Willy Tarreauf2e8ee22008-05-25 10:39:02 +0200512 calloc(1, sizeof(struct epoll_event) * absmaxevents);
Willy Tarreaude99e992007-04-16 00:53:59 +0200513
514 if (epoll_events == NULL)
515 goto fail_ee;
516
517 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
518 goto fail_spec;
519
520 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
521 if (fd_list == NULL)
522 goto fail_fd_list;
523
524 return 1;
525
526 fail_fd_list:
527 free(spec_list);
528 fail_spec:
529 free(epoll_events);
530 fail_ee:
531 close(epoll_fd);
532 epoll_fd = 0;
533 fail_fd:
534 p->pref = 0;
535 return 0;
536}
537
538/*
539 * Termination of the speculative epoll() poller.
540 * Memory is released and the poller is marked as unselectable.
541 */
542REGPRM1 static void _do_term(struct poller *p)
543{
544 if (fd_list)
545 free(fd_list);
546 if (spec_list)
547 free(spec_list);
548 if (epoll_events)
549 free(epoll_events);
550
551 close(epoll_fd);
552 epoll_fd = 0;
553
554 fd_list = NULL;
555 spec_list = NULL;
556 epoll_events = NULL;
557
558 p->private = NULL;
559 p->pref = 0;
560}
561
562/*
563 * Check that the poller works.
564 * Returns 1 if OK, otherwise 0.
565 */
566REGPRM1 static int _do_test(struct poller *p)
567{
568 int fd;
569
570 fd = epoll_create(global.maxsock + 1);
571 if (fd < 0)
572 return 0;
573 close(fd);
574 return 1;
575}
576
577/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200578 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
579 * otherwise 0. It will ensure that all processes will not share their
580 * epoll_fd. Some side effects were encountered because of this, such
581 * as epoll_wait() returning an FD which was previously deleted.
582 */
583REGPRM1 static int _do_fork(struct poller *p)
584{
585 close(epoll_fd);
586 epoll_fd = epoll_create(global.maxsock + 1);
587 if (epoll_fd < 0)
588 return 0;
589 return 1;
590}
591
592/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200593 * It is a constructor, which means that it will automatically be called before
594 * main(). This is GCC-specific but it works at least since 2.95.
595 * Special care must be taken so that it does not need any uninitialized data.
596 */
597__attribute__((constructor))
598static void _do_register(void)
599{
600 struct poller *p;
601
602 if (nbpollers >= MAX_POLLERS)
603 return;
604 p = &pollers[nbpollers++];
605
606 p->name = "sepoll";
607 p->pref = 400;
608 p->private = NULL;
609
610 p->test = _do_test;
611 p->init = _do_init;
612 p->term = _do_term;
613 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200614 p->fork = _do_fork;
Willy Tarreaude99e992007-04-16 00:53:59 +0200615
616 p->is_set = __fd_is_set;
617 p->cond_s = p->set = __fd_set;
618 p->cond_c = p->clr = __fd_clr;
619 p->rem = __fd_rem;
620 p->clo = __fd_clo;
621}
622
623
624/*
625 * Local variables:
626 * c-indent-level: 8
627 * c-basic-offset: 8
628 * End:
629 */