blob: 74917add8e9c0d83650d94a77f8d921bced75b36 [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
235static struct ev_to_epoll {
236 char op; // epoll opcode to switch from spec to wait, 0 if none
237 char m; // inverted mask for existing events
238 char ev; // remainint epoll events after change
239 char pad;
240} ev_to_epoll[16] = {
241 [FD_EV_IDLE_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_R },
242 [FD_EV_SPEC_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_R },
243 [FD_EV_STOP_W | FD_EV_IDLE_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_W },
244 [FD_EV_STOP_W | FD_EV_SPEC_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_W },
245 [FD_EV_WAIT_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_MOD, .m=FD_EV_MASK_R, .ev=EPOLLOUT },
246 [FD_EV_STOP_W | FD_EV_WAIT_R] = { .op=EPOLL_CTL_MOD, .m=FD_EV_MASK_W, .ev=EPOLLIN },
247 [FD_EV_STOP_W | FD_EV_STOP_R] = { .op=EPOLL_CTL_DEL, .m=FD_EV_MASK_R|FD_EV_MASK_W },
248 [FD_EV_WAIT_W | FD_EV_WAIT_R] = { .ev=EPOLLIN|EPOLLOUT },
249};
250
251/*
252 * speculative epoll() poller
253 */
254REGPRM2 static void _do_poll(struct poller *p, int wait_time)
255{
256 static unsigned int last_skipped;
257 int status;
258 int fd, opcode;
259 int count;
260 int spec_idx;
261
262
263 /* Here we have two options :
264 * - either walk the list forwards and hope to atch more events
265 * - or walk it backwards to minimize the number of changes and
266 * to make better use of the cache.
267 * Tests have shown that walking backwards improves perf by 0.2%.
268 */
269
270 spec_idx = nbspec;
271 while (likely(spec_idx > 0)) {
272 spec_idx--;
273 fd = spec_list[spec_idx];
274
275 opcode = ev_to_epoll[fd_list[fd].e].op;
276 if (opcode) {
277 ev.events = ev_to_epoll[fd_list[fd].e].ev;
278 ev.data.fd = fd;
279 epoll_ctl(epoll_fd, opcode, fd, &ev);
280 fd_list[fd].e &= ~(unsigned int)ev_to_epoll[fd_list[fd].e].m;
281 }
282
283 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
284 // This one must be removed. Let's clear it now.
285 delete_spec_entry(spec_idx);
286 continue;
287 }
288
289 /* OK so now we do not have any event marked STOP anymore in
290 * the list. We can simply try to execute functions for the
291 * events we have found, and requeue them in case of EAGAIN.
292 */
293
294 status = 0;
295 fdtab[fd].ev = 0;
296
297 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_SPEC_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200298 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200299 fdtab[fd].ev |= FD_POLL_IN;
300 if (fdtab[fd].cb[DIR_RD].f(fd) == 0)
301 status |= EPOLLIN;
302 }
303 }
304
305 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_SPEC_W) {
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_OUT;
308 if (fdtab[fd].cb[DIR_WR].f(fd) == 0)
309 status |= EPOLLOUT;
310 }
311 }
312
313 if (status) {
314 /* Some speculative accesses have failed, we must
315 * switch to the WAIT state.
316 */
317 ev.events = status;
318 ev.data.fd = fd;
319 if (fd_list[fd].e & FD_EV_RW_PL) {
320 // Event already in poll list
321 ev.events |= ev_to_epoll[fd_list[fd].e].ev;
322 opcode = EPOLL_CTL_MOD;
323 } else {
324 // Event not in poll list yet
325 opcode = EPOLL_CTL_ADD;
326 }
327 epoll_ctl(epoll_fd, opcode, fd, &ev);
328
Willy Tarreaude99e992007-04-16 00:53:59 +0200329 if (status & EPOLLIN) {
330 fd_list[fd].e &= ~FD_EV_MASK_R;
331 fd_list[fd].e |= FD_EV_WAIT_R;
332 }
333 if (status & EPOLLOUT) {
334 fd_list[fd].e &= ~FD_EV_MASK_W;
335 fd_list[fd].e |= FD_EV_WAIT_W;
336 }
337
338 if ((fd_list[fd].e & FD_EV_MASK_R) != FD_EV_SPEC_R &&
339 (fd_list[fd].e & FD_EV_MASK_W) != FD_EV_SPEC_W) {
340 delete_spec_entry(spec_idx);
341 continue;
342 }
343 }
344 }
345
346 /* If some speculative events remain, we must not set the timeout in
347 * epoll_wait(). Also, if some speculative events remain, it means
348 * that some have been immediately processed, otherwise they would
349 * have been disabled.
350 */
351 if (nbspec) {
352 if (!last_skipped++) {
353 /* Measures have shown a great performance increase if
354 * we call the epoll_wait() only the second time after
355 * speculative accesses have succeeded. This reduces
356 * the number of unsucessful calls to epoll_wait() by
357 * a factor of about 3, and the total number of calls
358 * by about 2.
359 */
360 tv_now(&now);
361 return;
362 }
363 wait_time = 0;
364 }
365 last_skipped = 0;
366
367 /* now let's wait for events */
368 status = epoll_wait(epoll_fd, epoll_events, maxfd, wait_time);
369 tv_now(&now);
370
371 for (count = 0; count < status; count++) {
372 int e = epoll_events[count].events;
373 fd = epoll_events[count].data.fd;
374
375 /* it looks complicated but gcc can optimize it away when constants
376 * have same values.
377 */
378 fdtab[fd].ev =
379 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
380 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
381 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
382 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
383 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
384
385 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200386 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200387 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200388 if (fdtab[fd].ev & (FD_POLL_RD|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200389 fdtab[fd].cb[DIR_RD].f(fd);
390 }
391
392 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200393 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200394 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200395 if (fdtab[fd].ev & (FD_POLL_WR|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200396 fdtab[fd].cb[DIR_WR].f(fd);
397 }
398 }
399}
400
401/*
402 * Initialization of the speculative epoll() poller.
403 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
404 * disables the poller by setting its pref to 0.
405 */
406REGPRM1 static int _do_init(struct poller *p)
407{
408 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
409
410 p->private = NULL;
411
412 epoll_fd = epoll_create(global.maxsock + 1);
413 if (epoll_fd < 0)
414 goto fail_fd;
415
416 epoll_events = (struct epoll_event*)
417 calloc(1, sizeof(struct epoll_event) * global.maxsock);
418
419 if (epoll_events == NULL)
420 goto fail_ee;
421
422 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
423 goto fail_spec;
424
425 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
426 if (fd_list == NULL)
427 goto fail_fd_list;
428
429 return 1;
430
431 fail_fd_list:
432 free(spec_list);
433 fail_spec:
434 free(epoll_events);
435 fail_ee:
436 close(epoll_fd);
437 epoll_fd = 0;
438 fail_fd:
439 p->pref = 0;
440 return 0;
441}
442
443/*
444 * Termination of the speculative epoll() poller.
445 * Memory is released and the poller is marked as unselectable.
446 */
447REGPRM1 static void _do_term(struct poller *p)
448{
449 if (fd_list)
450 free(fd_list);
451 if (spec_list)
452 free(spec_list);
453 if (epoll_events)
454 free(epoll_events);
455
456 close(epoll_fd);
457 epoll_fd = 0;
458
459 fd_list = NULL;
460 spec_list = NULL;
461 epoll_events = NULL;
462
463 p->private = NULL;
464 p->pref = 0;
465}
466
467/*
468 * Check that the poller works.
469 * Returns 1 if OK, otherwise 0.
470 */
471REGPRM1 static int _do_test(struct poller *p)
472{
473 int fd;
474
475 fd = epoll_create(global.maxsock + 1);
476 if (fd < 0)
477 return 0;
478 close(fd);
479 return 1;
480}
481
482/*
483 * It is a constructor, which means that it will automatically be called before
484 * main(). This is GCC-specific but it works at least since 2.95.
485 * Special care must be taken so that it does not need any uninitialized data.
486 */
487__attribute__((constructor))
488static void _do_register(void)
489{
490 struct poller *p;
491
492 if (nbpollers >= MAX_POLLERS)
493 return;
494 p = &pollers[nbpollers++];
495
496 p->name = "sepoll";
497 p->pref = 400;
498 p->private = NULL;
499
500 p->test = _do_test;
501 p->init = _do_init;
502 p->term = _do_term;
503 p->poll = _do_poll;
504
505 p->is_set = __fd_is_set;
506 p->cond_s = p->set = __fd_set;
507 p->cond_c = p->clr = __fd_clr;
508 p->rem = __fd_rem;
509 p->clo = __fd_clo;
510}
511
512
513/*
514 * Local variables:
515 * c-indent-level: 8
516 * c-basic-offset: 8
517 * End:
518 */