blob: 852577caa6e5837ed3917e1036e6eab9d752770e [file] [log] [blame]
Willy Tarreaude99e992007-04-16 00:53:59 +02001/*
2 * FD polling functions for Speculative I/O combined with Linux epoll()
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
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 *
11 */
12
13#include <unistd.h>
14#include <sys/time.h>
15#include <sys/types.h>
16
17#include <common/compat.h>
18#include <common/config.h>
19#include <common/standard.h>
20#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020021#include <common/tools.h>
Willy Tarreaude99e992007-04-16 00:53:59 +020022
23#include <types/fd.h>
24#include <types/global.h>
25
26#include <proto/fd.h>
27#include <proto/task.h>
28
29#if defined(USE_MY_EPOLL)
30#include <common/epoll.h>
31#include <errno.h>
32#include <sys/syscall.h>
33static _syscall1 (int, epoll_create, int, size);
34static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
35static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
36#else
37#include <sys/epoll.h>
38#endif
39
40/*
41 * We define 4 states for each direction of a file descriptor, which we store
42 * as 2 bits :
43 *
44 * 00 = IDLE : we're not interested in this event
45 * 01 = SPEC : perform speculative I/O on this FD
46 * 10 = WAIT : really wait for an availability event on this FD (poll)
47 * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if
48 * the application changes its mind, otherwise disable FD polling
49 * and switch back to IDLE.
50 *
51 * Since we do not want to scan all the FD list to find speculative I/O events,
52 * we store them in a list consisting in a linear array holding only the FD
53 * indexes right now.
54 *
55 * The STOP state requires the event to be present in the spec list so that
56 * it can be detected and flushed upon next scan without having to scan the
57 * whole FD list.
58 *
59 * This translates like this :
60 *
61 * EVENT_IN_SPEC_LIST = 01
62 * EVENT_IN_POLL_LIST = 10
63 *
64 * IDLE = 0
65 * SPEC = (EVENT_IN_SPEC_LIST)
66 * WAIT = (EVENT_IN_POLL_LIST)
67 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
68 *
69 * fd_is_set() just consists in checking that the status is 01 or 10.
70 *
71 * For efficiency reasons, we will store the Read and Write bits interlaced to
72 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
73 * get what we want :
74 * 3 2 1 0
75 * Wp Rp Ws Rs
76 *
77 * The FD array has to hold a back reference to the speculative list. This
78 * reference is only valid if at least one of the directions is marked SPEC.
79 *
80 */
81
82#define FD_EV_IN_SL 1
83#define FD_EV_IN_PL 4
84
85#define FD_EV_IDLE 0
86#define FD_EV_SPEC (FD_EV_IN_SL)
87#define FD_EV_WAIT (FD_EV_IN_PL)
88#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
89
90/* Those match any of R or W for Spec list or Poll list */
91#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
92#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
93#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
94
95#define FD_EV_IDLE_R 0
96#define FD_EV_SPEC_R (FD_EV_IN_SL)
97#define FD_EV_WAIT_R (FD_EV_IN_PL)
98#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
99#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
100
101#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
102#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
103#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
104#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
105#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
106
107#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
108
Willy Tarreau6653d172007-05-13 01:52:05 +0200109/* This is the minimum number of events successfully processed in speculative
110 * mode above which we agree to return without checking epoll() (1/2 times).
111 */
112#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200113
114/* descriptor of one FD.
115 * FIXME: should be a bit field */
116struct fd_status {
117 unsigned int e:4; // read and write events status.
118 unsigned int s:28; // Position in spec list. Should be last.
119};
120
121static int nbspec = 0; // current size of the spec list
122
123static struct fd_status *fd_list = NULL; // list of FDs
124static unsigned int *spec_list = NULL; // speculative I/O list
125
126/* private data */
127static struct epoll_event *epoll_events;
128static int epoll_fd;
129
130/* This structure may be used for any purpose. Warning! do not use it in
131 * recursive functions !
132 */
133static struct epoll_event ev;
134
135
136REGPRM1 static void alloc_spec_entry(const int fd)
137{
138 if (fd_list[fd].e & FD_EV_RW_SL)
139 return;
140 fd_list[fd].s = nbspec;
141 spec_list[nbspec++] = fd;
142}
143
144/* removes entry <pos> from the spec list and replaces it with the last one.
145 * The fd_list is adjusted to match the back reference if needed.
146 */
147REGPRM1 static void delete_spec_entry(const int pos)
148{
149 int fd;
150
151 nbspec--;
152 if (pos == nbspec)
153 return;
154
155 /* we replace current FD by the highest one */
156 fd = spec_list[nbspec];
157 spec_list[pos] = fd;
158 fd_list[fd].s = pos;
159}
160
161/*
162 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
163 */
164REGPRM2 static int __fd_is_set(const int fd, int dir)
165{
166 int ret;
167
168 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
169 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
170}
171
172/*
173 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
174 * designed like this in order to reduce the number of jumps (verified).
175 */
176REGPRM2 static int __fd_set(const int fd, int dir)
177{
178 __label__ switch_state;
179 unsigned int i;
180
181 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
182
183 if (i == FD_EV_IDLE) {
184 // switch to SPEC state and allocate a SPEC entry.
185 alloc_spec_entry(fd);
186 switch_state:
187 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
188 return 1;
189 }
190 else if (i == FD_EV_STOP) {
191 // switch to WAIT state
192 goto switch_state;
193 }
194 else
195 return 0;
196}
197
198REGPRM2 static int __fd_clr(const int fd, int dir)
199{
200 __label__ switch_state;
201 unsigned int i;
202
203 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
204
205 if (i == FD_EV_SPEC) {
206 // switch to IDLE state
207 goto switch_state;
208 }
209 else if (likely(i == FD_EV_WAIT)) {
210 // switch to STOP state
211 /* We will create a queue entry for this one because we want to
212 * process it later in order to merge it with other events on
213 * the same FD.
214 */
215 alloc_spec_entry(fd);
216 switch_state:
217 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
218 return 1;
219 }
220 return 0;
221}
222
Willy Tarreau6653d172007-05-13 01:52:05 +0200223/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200224REGPRM1 static void __fd_rem(int fd)
225{
226 __fd_clr(fd, DIR_RD);
227 __fd_clr(fd, DIR_WR);
228}
229
230/*
231 * On valid epoll() implementations, a call to close() automatically removes
232 * the fds. This means that the FD will appear as previously unset.
233 */
234REGPRM1 static void __fd_clo(int fd)
235{
236 if (fd_list[fd].e & FD_EV_RW_SL)
237 delete_spec_entry(fd_list[fd].s);
238 fd_list[fd].e &= ~(FD_EV_MASK);
239}
240
Willy Tarreaudc246a72007-05-09 21:57:51 +0200241/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200242 * speculative epoll() poller
243 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200244REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp)
Willy Tarreaude99e992007-04-16 00:53:59 +0200245{
246 static unsigned int last_skipped;
Willy Tarreau6653d172007-05-13 01:52:05 +0200247 int status, eo;
Willy Tarreaude99e992007-04-16 00:53:59 +0200248 int fd, opcode;
249 int count;
250 int spec_idx;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200251 int wait_time;
Willy Tarreaude99e992007-04-16 00:53:59 +0200252
253
254 /* Here we have two options :
Willy Tarreau6653d172007-05-13 01:52:05 +0200255 * - either walk the list forwards and hope to match more events
Willy Tarreaude99e992007-04-16 00:53:59 +0200256 * - or walk it backwards to minimize the number of changes and
257 * to make better use of the cache.
258 * Tests have shown that walking backwards improves perf by 0.2%.
259 */
260
Willy Tarreau6653d172007-05-13 01:52:05 +0200261 status = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200262 spec_idx = nbspec;
263 while (likely(spec_idx > 0)) {
264 spec_idx--;
265 fd = spec_list[spec_idx];
Willy Tarreau6653d172007-05-13 01:52:05 +0200266 eo = fd_list[fd].e; /* save old events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200267
Willy Tarreau6653d172007-05-13 01:52:05 +0200268 /*
269 * Process the speculative events.
270 *
271 * Principle: events which are marked FD_EV_SPEC are processed
272 * with their assigned function. If the function returns 0, it
273 * means there is nothing doable without polling first. We will
274 * then convert the event to a pollable one by assigning them
275 * the WAIT status.
Willy Tarreaude99e992007-04-16 00:53:59 +0200276 */
277
Willy Tarreaude99e992007-04-16 00:53:59 +0200278 fdtab[fd].ev = 0;
Willy Tarreau6653d172007-05-13 01:52:05 +0200279 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {
280 /* The owner is interested in reading from this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200281 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200282 /* Pretend there is something to read */
Willy Tarreaude99e992007-04-16 00:53:59 +0200283 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau6653d172007-05-13 01:52:05 +0200284 if (!fdtab[fd].cb[DIR_RD].f(fd))
285 fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
286 else
287 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200288 }
289 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200290 else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {
291 /* This FD was being polled and is now being removed. */
292 fd_list[fd].e &= ~FD_EV_MASK_R;
293 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200294
Willy Tarreau6653d172007-05-13 01:52:05 +0200295 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {
296 /* The owner is interested in writing to this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200297 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200298 /* Pretend there is something to write */
Willy Tarreaude99e992007-04-16 00:53:59 +0200299 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau6653d172007-05-13 01:52:05 +0200300 if (!fdtab[fd].cb[DIR_WR].f(fd))
301 fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
302 else
303 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200304 }
305 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200306 else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {
307 /* This FD was being polled and is now being removed. */
308 fd_list[fd].e &= ~FD_EV_MASK_W;
309 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200310
Willy Tarreau6653d172007-05-13 01:52:05 +0200311 /* Now, we will adjust the event in the poll list. Indeed, it
312 * is possible that an event which was previously in the poll
313 * list now goes out, and the opposite is possible too. We can
314 * have opposite changes for READ and WRITE too.
315 */
316
317 if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) {
318 /* poll status changed*/
319 if ((fd_list[fd].e & FD_EV_RW_PL) == 0) {
320 /* fd removed from poll list */
321 opcode = EPOLL_CTL_DEL;
322 }
323 else if ((eo & FD_EV_RW_PL) == 0) {
324 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200325 opcode = EPOLL_CTL_ADD;
326 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200327 else {
328 /* fd status changed */
329 opcode = EPOLL_CTL_MOD;
330 }
331
332 /* construct the epoll events based on new state */
333 ev.events = 0;
334 if (fd_list[fd].e & FD_EV_WAIT_R)
335 ev.events |= EPOLLIN;
336
337 if (fd_list[fd].e & FD_EV_WAIT_W)
338 ev.events |= EPOLLOUT;
339
340 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200341 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200342 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200343
Willy Tarreaude99e992007-04-16 00:53:59 +0200344
Willy Tarreau6653d172007-05-13 01:52:05 +0200345 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
346 /* This fd switched to combinations of either WAIT or
347 * IDLE. It must be removed from the spec list.
348 */
349 delete_spec_entry(spec_idx);
350 continue;
Willy Tarreaude99e992007-04-16 00:53:59 +0200351 }
352 }
353
Willy Tarreau6653d172007-05-13 01:52:05 +0200354 /* It may make sense to immediately return here if there are enough
355 * processed events, without passing through epoll_wait() because we
356 * have exactly done a poll.
357 * Measures have shown a great performance increase if we call the
358 * epoll_wait() only the second time after speculative accesses have
359 * succeeded. This reduces the number of unsucessful calls to
360 * epoll_wait() by a factor of about 3, and the total number of calls
361 * by about 2.
Willy Tarreaude99e992007-04-16 00:53:59 +0200362 */
Willy Tarreau6653d172007-05-13 01:52:05 +0200363
364 if (status >= MIN_RETURN_EVENTS) {
365 /* We have processed at least MIN_RETURN_EVENTS, it's worth
366 * returning now without checking epoll_wait().
367 */
368 if (++last_skipped <= 1) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200369 tv_now(&now);
370 return;
371 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200372 }
373 last_skipped = 0;
374
Willy Tarreau6653d172007-05-13 01:52:05 +0200375 if (nbspec || status) {
376 /* Maybe we have processed some events that we must report, or
377 * maybe we still have events in the spec list, so we must not
378 * wait in epoll() otherwise we will delay their delivery by
379 * the next timeout.
380 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200381 wait_time = 0;
382 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200383 else {
Willy Tarreaubdefc512007-05-14 02:02:04 +0200384 if (tv_iseternity(exp))
Willy Tarreaud825eef2007-05-12 22:35:00 +0200385 wait_time = -1;
Willy Tarreaubdefc512007-05-14 02:02:04 +0200386 else if (tv_isge(&now, exp))
387 wait_time = 0;
388 else
389 wait_time = __tv_ms_elapsed(&now, exp) + 1;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200390 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200391
Willy Tarreau6653d172007-05-13 01:52:05 +0200392 /* now let's wait for real events */
Willy Tarreau1db37712007-06-03 17:16:49 +0200393 fd = MIN(maxfd, global.tune.maxpollevents);
394 status = epoll_wait(epoll_fd, epoll_events, fd, wait_time);
Willy Tarreau6653d172007-05-13 01:52:05 +0200395
Willy Tarreaude99e992007-04-16 00:53:59 +0200396 tv_now(&now);
397
398 for (count = 0; count < status; count++) {
399 int e = epoll_events[count].events;
400 fd = epoll_events[count].data.fd;
401
402 /* it looks complicated but gcc can optimize it away when constants
403 * have same values.
404 */
405 fdtab[fd].ev =
406 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
407 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
408 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
409 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
410 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
411
412 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200413 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200414 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200415 if (fdtab[fd].ev & (FD_POLL_RD|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200416 fdtab[fd].cb[DIR_RD].f(fd);
417 }
418
419 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200420 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200421 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200422 if (fdtab[fd].ev & (FD_POLL_WR|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200423 fdtab[fd].cb[DIR_WR].f(fd);
424 }
425 }
426}
427
428/*
429 * Initialization of the speculative epoll() poller.
430 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
431 * disables the poller by setting its pref to 0.
432 */
433REGPRM1 static int _do_init(struct poller *p)
434{
435 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
436
437 p->private = NULL;
438
439 epoll_fd = epoll_create(global.maxsock + 1);
440 if (epoll_fd < 0)
441 goto fail_fd;
442
443 epoll_events = (struct epoll_event*)
Willy Tarreau1db37712007-06-03 17:16:49 +0200444 calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents);
Willy Tarreaude99e992007-04-16 00:53:59 +0200445
446 if (epoll_events == NULL)
447 goto fail_ee;
448
449 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
450 goto fail_spec;
451
452 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
453 if (fd_list == NULL)
454 goto fail_fd_list;
455
456 return 1;
457
458 fail_fd_list:
459 free(spec_list);
460 fail_spec:
461 free(epoll_events);
462 fail_ee:
463 close(epoll_fd);
464 epoll_fd = 0;
465 fail_fd:
466 p->pref = 0;
467 return 0;
468}
469
470/*
471 * Termination of the speculative epoll() poller.
472 * Memory is released and the poller is marked as unselectable.
473 */
474REGPRM1 static void _do_term(struct poller *p)
475{
476 if (fd_list)
477 free(fd_list);
478 if (spec_list)
479 free(spec_list);
480 if (epoll_events)
481 free(epoll_events);
482
483 close(epoll_fd);
484 epoll_fd = 0;
485
486 fd_list = NULL;
487 spec_list = NULL;
488 epoll_events = NULL;
489
490 p->private = NULL;
491 p->pref = 0;
492}
493
494/*
495 * Check that the poller works.
496 * Returns 1 if OK, otherwise 0.
497 */
498REGPRM1 static int _do_test(struct poller *p)
499{
500 int fd;
501
502 fd = epoll_create(global.maxsock + 1);
503 if (fd < 0)
504 return 0;
505 close(fd);
506 return 1;
507}
508
509/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200510 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
511 * otherwise 0. It will ensure that all processes will not share their
512 * epoll_fd. Some side effects were encountered because of this, such
513 * as epoll_wait() returning an FD which was previously deleted.
514 */
515REGPRM1 static int _do_fork(struct poller *p)
516{
517 close(epoll_fd);
518 epoll_fd = epoll_create(global.maxsock + 1);
519 if (epoll_fd < 0)
520 return 0;
521 return 1;
522}
523
524/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200525 * It is a constructor, which means that it will automatically be called before
526 * main(). This is GCC-specific but it works at least since 2.95.
527 * Special care must be taken so that it does not need any uninitialized data.
528 */
529__attribute__((constructor))
530static void _do_register(void)
531{
532 struct poller *p;
533
534 if (nbpollers >= MAX_POLLERS)
535 return;
536 p = &pollers[nbpollers++];
537
538 p->name = "sepoll";
539 p->pref = 400;
540 p->private = NULL;
541
542 p->test = _do_test;
543 p->init = _do_init;
544 p->term = _do_term;
545 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200546 p->fork = _do_fork;
Willy Tarreaude99e992007-04-16 00:53:59 +0200547
548 p->is_set = __fd_is_set;
549 p->cond_s = p->set = __fd_set;
550 p->cond_c = p->clr = __fd_clr;
551 p->rem = __fd_rem;
552 p->clo = __fd_clo;
553}
554
555
556/*
557 * Local variables:
558 * c-indent-level: 8
559 * c-basic-offset: 8
560 * End:
561 */