blob: c3b777647e265ce4789152a5ce759a4860166729 [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>
21
22#include <types/fd.h>
23#include <types/global.h>
24
25#include <proto/fd.h>
26#include <proto/task.h>
27
28#if defined(USE_MY_EPOLL)
29#include <common/epoll.h>
30#include <errno.h>
31#include <sys/syscall.h>
32static _syscall1 (int, epoll_create, int, size);
33static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event);
34static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout);
35#else
36#include <sys/epoll.h>
37#endif
38
39/*
40 * We define 4 states for each direction of a file descriptor, which we store
41 * as 2 bits :
42 *
43 * 00 = IDLE : we're not interested in this event
44 * 01 = SPEC : perform speculative I/O on this FD
45 * 10 = WAIT : really wait for an availability event on this FD (poll)
46 * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if
47 * the application changes its mind, otherwise disable FD polling
48 * and switch back to IDLE.
49 *
50 * Since we do not want to scan all the FD list to find speculative I/O events,
51 * we store them in a list consisting in a linear array holding only the FD
52 * indexes right now.
53 *
54 * The STOP state requires the event to be present in the spec list so that
55 * it can be detected and flushed upon next scan without having to scan the
56 * whole FD list.
57 *
58 * This translates like this :
59 *
60 * EVENT_IN_SPEC_LIST = 01
61 * EVENT_IN_POLL_LIST = 10
62 *
63 * IDLE = 0
64 * SPEC = (EVENT_IN_SPEC_LIST)
65 * WAIT = (EVENT_IN_POLL_LIST)
66 * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST)
67 *
68 * fd_is_set() just consists in checking that the status is 01 or 10.
69 *
70 * For efficiency reasons, we will store the Read and Write bits interlaced to
71 * form a 4-bit field, so that we can simply shift the value right by 0/1 and
72 * get what we want :
73 * 3 2 1 0
74 * Wp Rp Ws Rs
75 *
76 * The FD array has to hold a back reference to the speculative list. This
77 * reference is only valid if at least one of the directions is marked SPEC.
78 *
79 */
80
81#define FD_EV_IN_SL 1
82#define FD_EV_IN_PL 4
83
84#define FD_EV_IDLE 0
85#define FD_EV_SPEC (FD_EV_IN_SL)
86#define FD_EV_WAIT (FD_EV_IN_PL)
87#define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL)
88
89/* Those match any of R or W for Spec list or Poll list */
90#define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1))
91#define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1))
92#define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL)
93
94#define FD_EV_IDLE_R 0
95#define FD_EV_SPEC_R (FD_EV_IN_SL)
96#define FD_EV_WAIT_R (FD_EV_IN_PL)
97#define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL)
98#define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL)
99
100#define FD_EV_IDLE_W (FD_EV_IDLE_R << 1)
101#define FD_EV_SPEC_W (FD_EV_SPEC_R << 1)
102#define FD_EV_WAIT_W (FD_EV_WAIT_R << 1)
103#define FD_EV_STOP_W (FD_EV_STOP_R << 1)
104#define FD_EV_MASK_W (FD_EV_MASK_R << 1)
105
106#define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R)
107
108
109/* descriptor of one FD.
110 * FIXME: should be a bit field */
111struct fd_status {
112 unsigned int e:4; // read and write events status.
113 unsigned int s:28; // Position in spec list. Should be last.
114};
115
116static int nbspec = 0; // current size of the spec list
117
118static struct fd_status *fd_list = NULL; // list of FDs
119static unsigned int *spec_list = NULL; // speculative I/O list
120
121/* private data */
122static struct epoll_event *epoll_events;
123static int epoll_fd;
124
125/* This structure may be used for any purpose. Warning! do not use it in
126 * recursive functions !
127 */
128static struct epoll_event ev;
129
130
131REGPRM1 static void alloc_spec_entry(const int fd)
132{
133 if (fd_list[fd].e & FD_EV_RW_SL)
134 return;
135 fd_list[fd].s = nbspec;
136 spec_list[nbspec++] = fd;
137}
138
139/* removes entry <pos> from the spec list and replaces it with the last one.
140 * The fd_list is adjusted to match the back reference if needed.
141 */
142REGPRM1 static void delete_spec_entry(const int pos)
143{
144 int fd;
145
146 nbspec--;
147 if (pos == nbspec)
148 return;
149
150 /* we replace current FD by the highest one */
151 fd = spec_list[nbspec];
152 spec_list[pos] = fd;
153 fd_list[fd].s = pos;
154}
155
156/*
157 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
158 */
159REGPRM2 static int __fd_is_set(const int fd, int dir)
160{
161 int ret;
162
163 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
164 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
165}
166
167/*
168 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
169 * designed like this in order to reduce the number of jumps (verified).
170 */
171REGPRM2 static int __fd_set(const int fd, int dir)
172{
173 __label__ switch_state;
174 unsigned int i;
175
176 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
177
178 if (i == FD_EV_IDLE) {
179 // switch to SPEC state and allocate a SPEC entry.
180 alloc_spec_entry(fd);
181 switch_state:
182 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
183 return 1;
184 }
185 else if (i == FD_EV_STOP) {
186 // switch to WAIT state
187 goto switch_state;
188 }
189 else
190 return 0;
191}
192
193REGPRM2 static int __fd_clr(const int fd, int dir)
194{
195 __label__ switch_state;
196 unsigned int i;
197
198 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
199
200 if (i == FD_EV_SPEC) {
201 // switch to IDLE state
202 goto switch_state;
203 }
204 else if (likely(i == FD_EV_WAIT)) {
205 // switch to STOP state
206 /* We will create a queue entry for this one because we want to
207 * process it later in order to merge it with other events on
208 * the same FD.
209 */
210 alloc_spec_entry(fd);
211 switch_state:
212 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
213 return 1;
214 }
215 return 0;
216}
217
218REGPRM1 static void __fd_rem(int fd)
219{
220 __fd_clr(fd, DIR_RD);
221 __fd_clr(fd, DIR_WR);
222}
223
224/*
225 * On valid epoll() implementations, a call to close() automatically removes
226 * the fds. This means that the FD will appear as previously unset.
227 */
228REGPRM1 static void __fd_clo(int fd)
229{
230 if (fd_list[fd].e & FD_EV_RW_SL)
231 delete_spec_entry(fd_list[fd].s);
232 fd_list[fd].e &= ~(FD_EV_MASK);
233}
234
Willy Tarreaudc246a72007-05-09 21:57:51 +0200235/*
236 * operations to perform when reaching _do_poll() for some FDs in the spec
237 * queue depending on their state. This is mainly used to cleanup some FDs
238 * which are in STOP state. It is also used to compute EPOLL* flags when
239 * switching from SPEC to WAIT.
240 */
Willy Tarreaude99e992007-04-16 00:53:59 +0200241static struct ev_to_epoll {
242 char op; // epoll opcode to switch from spec to wait, 0 if none
243 char m; // inverted mask for existing events
Willy Tarreaudc246a72007-05-09 21:57:51 +0200244 char ev; // remaining epoll events after change
Willy Tarreaude99e992007-04-16 00:53:59 +0200245 char pad;
246} ev_to_epoll[16] = {
247 [FD_EV_IDLE_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_R },
248 [FD_EV_SPEC_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_R },
249 [FD_EV_STOP_W | FD_EV_IDLE_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_W },
250 [FD_EV_STOP_W | FD_EV_SPEC_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_W },
251 [FD_EV_WAIT_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_MOD, .m=FD_EV_MASK_R, .ev=EPOLLOUT },
252 [FD_EV_STOP_W | FD_EV_WAIT_R] = { .op=EPOLL_CTL_MOD, .m=FD_EV_MASK_W, .ev=EPOLLIN },
253 [FD_EV_STOP_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_R|FD_EV_MASK_W },
Willy Tarreaudc246a72007-05-09 21:57:51 +0200254 [FD_EV_SPEC_W | FD_EV_WAIT_R] = { .ev=EPOLLIN },
255 [FD_EV_WAIT_W | FD_EV_SPEC_R] = { .ev=EPOLLOUT },
Willy Tarreaude99e992007-04-16 00:53:59 +0200256 [FD_EV_WAIT_W | FD_EV_WAIT_R] = { .ev=EPOLLIN|EPOLLOUT },
257};
258
259/*
260 * speculative epoll() poller
261 */
262REGPRM2 static void _do_poll(struct poller *p, int wait_time)
263{
264 static unsigned int last_skipped;
265 int status;
266 int fd, opcode;
267 int count;
268 int spec_idx;
269
270
271 /* Here we have two options :
272 * - either walk the list forwards and hope to atch more events
273 * - or walk it backwards to minimize the number of changes and
274 * to make better use of the cache.
275 * Tests have shown that walking backwards improves perf by 0.2%.
276 */
277
278 spec_idx = nbspec;
279 while (likely(spec_idx > 0)) {
280 spec_idx--;
281 fd = spec_list[spec_idx];
282
283 opcode = ev_to_epoll[fd_list[fd].e].op;
284 if (opcode) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200285 ev.data.fd = fd;
Willy Tarreaudc246a72007-05-09 21:57:51 +0200286 ev.events = ev_to_epoll[fd_list[fd].e].ev;
Willy Tarreaude99e992007-04-16 00:53:59 +0200287 fd_list[fd].e &= ~(unsigned int)ev_to_epoll[fd_list[fd].e].m;
Willy Tarreaudc246a72007-05-09 21:57:51 +0200288 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreaude99e992007-04-16 00:53:59 +0200289 }
290
291 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
292 // This one must be removed. Let's clear it now.
293 delete_spec_entry(spec_idx);
294 continue;
295 }
296
297 /* OK so now we do not have any event marked STOP anymore in
298 * the list. We can simply try to execute functions for the
299 * events we have found, and requeue them in case of EAGAIN.
300 */
301
302 status = 0;
303 fdtab[fd].ev = 0;
304
305 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_SPEC_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200306 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200307 fdtab[fd].ev |= FD_POLL_IN;
308 if (fdtab[fd].cb[DIR_RD].f(fd) == 0)
309 status |= EPOLLIN;
310 }
311 }
312
313 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_SPEC_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200314 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200315 fdtab[fd].ev |= FD_POLL_OUT;
316 if (fdtab[fd].cb[DIR_WR].f(fd) == 0)
317 status |= EPOLLOUT;
318 }
319 }
320
321 if (status) {
322 /* Some speculative accesses have failed, we must
323 * switch to the WAIT state.
324 */
325 ev.events = status;
326 ev.data.fd = fd;
327 if (fd_list[fd].e & FD_EV_RW_PL) {
328 // Event already in poll list
329 ev.events |= ev_to_epoll[fd_list[fd].e].ev;
330 opcode = EPOLL_CTL_MOD;
331 } else {
332 // Event not in poll list yet
333 opcode = EPOLL_CTL_ADD;
334 }
335 epoll_ctl(epoll_fd, opcode, fd, &ev);
336
Willy Tarreaude99e992007-04-16 00:53:59 +0200337 if (status & EPOLLIN) {
338 fd_list[fd].e &= ~FD_EV_MASK_R;
339 fd_list[fd].e |= FD_EV_WAIT_R;
340 }
341 if (status & EPOLLOUT) {
342 fd_list[fd].e &= ~FD_EV_MASK_W;
343 fd_list[fd].e |= FD_EV_WAIT_W;
344 }
345
346 if ((fd_list[fd].e & FD_EV_MASK_R) != FD_EV_SPEC_R &&
347 (fd_list[fd].e & FD_EV_MASK_W) != FD_EV_SPEC_W) {
348 delete_spec_entry(spec_idx);
349 continue;
350 }
351 }
352 }
353
354 /* If some speculative events remain, we must not set the timeout in
355 * epoll_wait(). Also, if some speculative events remain, it means
356 * that some have been immediately processed, otherwise they would
357 * have been disabled.
358 */
359 if (nbspec) {
360 if (!last_skipped++) {
361 /* Measures have shown a great performance increase if
362 * we call the epoll_wait() only the second time after
363 * speculative accesses have succeeded. This reduces
364 * the number of unsucessful calls to epoll_wait() by
365 * a factor of about 3, and the total number of calls
366 * by about 2.
367 */
368 tv_now(&now);
369 return;
370 }
371 wait_time = 0;
372 }
373 last_skipped = 0;
374
375 /* now let's wait for events */
376 status = epoll_wait(epoll_fd, epoll_events, maxfd, wait_time);
377 tv_now(&now);
378
379 for (count = 0; count < status; count++) {
380 int e = epoll_events[count].events;
381 fd = epoll_events[count].data.fd;
382
383 /* it looks complicated but gcc can optimize it away when constants
384 * have same values.
385 */
386 fdtab[fd].ev =
387 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
388 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
389 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
390 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
391 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
392
393 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200394 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200395 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200396 if (fdtab[fd].ev & (FD_POLL_RD|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200397 fdtab[fd].cb[DIR_RD].f(fd);
398 }
399
400 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200401 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200402 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200403 if (fdtab[fd].ev & (FD_POLL_WR|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200404 fdtab[fd].cb[DIR_WR].f(fd);
405 }
406 }
407}
408
409/*
410 * Initialization of the speculative epoll() poller.
411 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
412 * disables the poller by setting its pref to 0.
413 */
414REGPRM1 static int _do_init(struct poller *p)
415{
416 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
417
418 p->private = NULL;
419
420 epoll_fd = epoll_create(global.maxsock + 1);
421 if (epoll_fd < 0)
422 goto fail_fd;
423
424 epoll_events = (struct epoll_event*)
425 calloc(1, sizeof(struct epoll_event) * global.maxsock);
426
427 if (epoll_events == NULL)
428 goto fail_ee;
429
430 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
431 goto fail_spec;
432
433 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
434 if (fd_list == NULL)
435 goto fail_fd_list;
436
437 return 1;
438
439 fail_fd_list:
440 free(spec_list);
441 fail_spec:
442 free(epoll_events);
443 fail_ee:
444 close(epoll_fd);
445 epoll_fd = 0;
446 fail_fd:
447 p->pref = 0;
448 return 0;
449}
450
451/*
452 * Termination of the speculative epoll() poller.
453 * Memory is released and the poller is marked as unselectable.
454 */
455REGPRM1 static void _do_term(struct poller *p)
456{
457 if (fd_list)
458 free(fd_list);
459 if (spec_list)
460 free(spec_list);
461 if (epoll_events)
462 free(epoll_events);
463
464 close(epoll_fd);
465 epoll_fd = 0;
466
467 fd_list = NULL;
468 spec_list = NULL;
469 epoll_events = NULL;
470
471 p->private = NULL;
472 p->pref = 0;
473}
474
475/*
476 * Check that the poller works.
477 * Returns 1 if OK, otherwise 0.
478 */
479REGPRM1 static int _do_test(struct poller *p)
480{
481 int fd;
482
483 fd = epoll_create(global.maxsock + 1);
484 if (fd < 0)
485 return 0;
486 close(fd);
487 return 1;
488}
489
490/*
491 * It is a constructor, which means that it will automatically be called before
492 * main(). This is GCC-specific but it works at least since 2.95.
493 * Special care must be taken so that it does not need any uninitialized data.
494 */
495__attribute__((constructor))
496static void _do_register(void)
497{
498 struct poller *p;
499
500 if (nbpollers >= MAX_POLLERS)
501 return;
502 p = &pollers[nbpollers++];
503
504 p->name = "sepoll";
505 p->pref = 400;
506 p->private = NULL;
507
508 p->test = _do_test;
509 p->init = _do_init;
510 p->term = _do_term;
511 p->poll = _do_poll;
512
513 p->is_set = __fd_is_set;
514 p->cond_s = p->set = __fd_set;
515 p->cond_c = p->clr = __fd_clr;
516 p->rem = __fd_rem;
517 p->clo = __fd_clo;
518}
519
520
521/*
522 * Local variables:
523 * c-indent-level: 8
524 * c-basic-offset: 8
525 * End:
526 */