blob: de92c5d95bc9635574a9eba3e2457942c28d5328 [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
Willy Tarreau6653d172007-05-13 01:52:05 +0200108/* This is the minimum number of events successfully processed in speculative
109 * mode above which we agree to return without checking epoll() (1/2 times).
110 */
111#define MIN_RETURN_EVENTS 25
Willy Tarreaude99e992007-04-16 00:53:59 +0200112
113/* descriptor of one FD.
114 * FIXME: should be a bit field */
115struct fd_status {
116 unsigned int e:4; // read and write events status.
117 unsigned int s:28; // Position in spec list. Should be last.
118};
119
120static int nbspec = 0; // current size of the spec list
121
122static struct fd_status *fd_list = NULL; // list of FDs
123static unsigned int *spec_list = NULL; // speculative I/O list
124
125/* private data */
126static struct epoll_event *epoll_events;
127static int epoll_fd;
128
129/* This structure may be used for any purpose. Warning! do not use it in
130 * recursive functions !
131 */
132static struct epoll_event ev;
133
134
135REGPRM1 static void alloc_spec_entry(const int fd)
136{
137 if (fd_list[fd].e & FD_EV_RW_SL)
138 return;
139 fd_list[fd].s = nbspec;
140 spec_list[nbspec++] = fd;
141}
142
143/* removes entry <pos> from the spec list and replaces it with the last one.
144 * The fd_list is adjusted to match the back reference if needed.
145 */
146REGPRM1 static void delete_spec_entry(const int pos)
147{
148 int fd;
149
150 nbspec--;
151 if (pos == nbspec)
152 return;
153
154 /* we replace current FD by the highest one */
155 fd = spec_list[nbspec];
156 spec_list[pos] = fd;
157 fd_list[fd].s = pos;
158}
159
160/*
161 * Returns non-zero if <fd> is already monitored for events in direction <dir>.
162 */
163REGPRM2 static int __fd_is_set(const int fd, int dir)
164{
165 int ret;
166
167 ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
168 return (ret == FD_EV_SPEC || ret == FD_EV_WAIT);
169}
170
171/*
172 * Don't worry about the strange constructs in __fd_set/__fd_clr, they are
173 * designed like this in order to reduce the number of jumps (verified).
174 */
175REGPRM2 static int __fd_set(const int fd, int dir)
176{
177 __label__ switch_state;
178 unsigned int i;
179
180 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
181
182 if (i == FD_EV_IDLE) {
183 // switch to SPEC state and allocate a SPEC entry.
184 alloc_spec_entry(fd);
185 switch_state:
186 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
187 return 1;
188 }
189 else if (i == FD_EV_STOP) {
190 // switch to WAIT state
191 goto switch_state;
192 }
193 else
194 return 0;
195}
196
197REGPRM2 static int __fd_clr(const int fd, int dir)
198{
199 __label__ switch_state;
200 unsigned int i;
201
202 i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR;
203
204 if (i == FD_EV_SPEC) {
205 // switch to IDLE state
206 goto switch_state;
207 }
208 else if (likely(i == FD_EV_WAIT)) {
209 // switch to STOP state
210 /* We will create a queue entry for this one because we want to
211 * process it later in order to merge it with other events on
212 * the same FD.
213 */
214 alloc_spec_entry(fd);
215 switch_state:
216 fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir);
217 return 1;
218 }
219 return 0;
220}
221
Willy Tarreau6653d172007-05-13 01:52:05 +0200222/* normally unused */
Willy Tarreaude99e992007-04-16 00:53:59 +0200223REGPRM1 static void __fd_rem(int fd)
224{
225 __fd_clr(fd, DIR_RD);
226 __fd_clr(fd, DIR_WR);
227}
228
229/*
230 * On valid epoll() implementations, a call to close() automatically removes
231 * the fds. This means that the FD will appear as previously unset.
232 */
233REGPRM1 static void __fd_clo(int fd)
234{
235 if (fd_list[fd].e & FD_EV_RW_SL)
236 delete_spec_entry(fd_list[fd].s);
237 fd_list[fd].e &= ~(FD_EV_MASK);
238}
239
Willy Tarreaudc246a72007-05-09 21:57:51 +0200240/*
Willy Tarreaude99e992007-04-16 00:53:59 +0200241 * speculative epoll() poller
242 */
243REGPRM2 static void _do_poll(struct poller *p, int wait_time)
244{
245 static unsigned int last_skipped;
Willy Tarreau6653d172007-05-13 01:52:05 +0200246 int status, eo;
Willy Tarreaude99e992007-04-16 00:53:59 +0200247 int fd, opcode;
248 int count;
249 int spec_idx;
250
251
252 /* Here we have two options :
Willy Tarreau6653d172007-05-13 01:52:05 +0200253 * - either walk the list forwards and hope to match more events
Willy Tarreaude99e992007-04-16 00:53:59 +0200254 * - or walk it backwards to minimize the number of changes and
255 * to make better use of the cache.
256 * Tests have shown that walking backwards improves perf by 0.2%.
257 */
258
Willy Tarreau6653d172007-05-13 01:52:05 +0200259 status = 0;
Willy Tarreaude99e992007-04-16 00:53:59 +0200260 spec_idx = nbspec;
261 while (likely(spec_idx > 0)) {
262 spec_idx--;
263 fd = spec_list[spec_idx];
Willy Tarreau6653d172007-05-13 01:52:05 +0200264 eo = fd_list[fd].e; /* save old events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200265
Willy Tarreau6653d172007-05-13 01:52:05 +0200266 /*
267 * Process the speculative events.
268 *
269 * Principle: events which are marked FD_EV_SPEC are processed
270 * with their assigned function. If the function returns 0, it
271 * means there is nothing doable without polling first. We will
272 * then convert the event to a pollable one by assigning them
273 * the WAIT status.
Willy Tarreaude99e992007-04-16 00:53:59 +0200274 */
275
Willy Tarreaude99e992007-04-16 00:53:59 +0200276 fdtab[fd].ev = 0;
Willy Tarreau6653d172007-05-13 01:52:05 +0200277 if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) {
278 /* The owner is interested in reading from this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200279 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200280 /* Pretend there is something to read */
Willy Tarreaude99e992007-04-16 00:53:59 +0200281 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau6653d172007-05-13 01:52:05 +0200282 if (!fdtab[fd].cb[DIR_RD].f(fd))
283 fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R);
284 else
285 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200286 }
287 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200288 else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) {
289 /* This FD was being polled and is now being removed. */
290 fd_list[fd].e &= ~FD_EV_MASK_R;
291 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200292
Willy Tarreau6653d172007-05-13 01:52:05 +0200293 if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) {
294 /* The owner is interested in writing to this FD */
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200295 if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) {
Willy Tarreau6653d172007-05-13 01:52:05 +0200296 /* Pretend there is something to write */
Willy Tarreaude99e992007-04-16 00:53:59 +0200297 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau6653d172007-05-13 01:52:05 +0200298 if (!fdtab[fd].cb[DIR_WR].f(fd))
299 fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W);
300 else
301 status++;
Willy Tarreaude99e992007-04-16 00:53:59 +0200302 }
303 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200304 else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) {
305 /* This FD was being polled and is now being removed. */
306 fd_list[fd].e &= ~FD_EV_MASK_W;
307 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200308
Willy Tarreau6653d172007-05-13 01:52:05 +0200309 /* Now, we will adjust the event in the poll list. Indeed, it
310 * is possible that an event which was previously in the poll
311 * list now goes out, and the opposite is possible too. We can
312 * have opposite changes for READ and WRITE too.
313 */
314
315 if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) {
316 /* poll status changed*/
317 if ((fd_list[fd].e & FD_EV_RW_PL) == 0) {
318 /* fd removed from poll list */
319 opcode = EPOLL_CTL_DEL;
320 }
321 else if ((eo & FD_EV_RW_PL) == 0) {
322 /* new fd in the poll list */
Willy Tarreaude99e992007-04-16 00:53:59 +0200323 opcode = EPOLL_CTL_ADD;
324 }
Willy Tarreau6653d172007-05-13 01:52:05 +0200325 else {
326 /* fd status changed */
327 opcode = EPOLL_CTL_MOD;
328 }
329
330 /* construct the epoll events based on new state */
331 ev.events = 0;
332 if (fd_list[fd].e & FD_EV_WAIT_R)
333 ev.events |= EPOLLIN;
334
335 if (fd_list[fd].e & FD_EV_WAIT_W)
336 ev.events |= EPOLLOUT;
337
338 ev.data.fd = fd;
Willy Tarreaude99e992007-04-16 00:53:59 +0200339 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau6653d172007-05-13 01:52:05 +0200340 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200341
Willy Tarreaude99e992007-04-16 00:53:59 +0200342
Willy Tarreau6653d172007-05-13 01:52:05 +0200343 if (!(fd_list[fd].e & FD_EV_RW_SL)) {
344 /* This fd switched to combinations of either WAIT or
345 * IDLE. It must be removed from the spec list.
346 */
347 delete_spec_entry(spec_idx);
348 continue;
Willy Tarreaude99e992007-04-16 00:53:59 +0200349 }
350 }
351
Willy Tarreau6653d172007-05-13 01:52:05 +0200352 /* It may make sense to immediately return here if there are enough
353 * processed events, without passing through epoll_wait() because we
354 * have exactly done a poll.
355 * Measures have shown a great performance increase if we call the
356 * epoll_wait() only the second time after speculative accesses have
357 * succeeded. This reduces the number of unsucessful calls to
358 * epoll_wait() by a factor of about 3, and the total number of calls
359 * by about 2.
Willy Tarreaude99e992007-04-16 00:53:59 +0200360 */
Willy Tarreau6653d172007-05-13 01:52:05 +0200361
362 if (status >= MIN_RETURN_EVENTS) {
363 /* We have processed at least MIN_RETURN_EVENTS, it's worth
364 * returning now without checking epoll_wait().
365 */
366 if (++last_skipped <= 1) {
Willy Tarreaude99e992007-04-16 00:53:59 +0200367 tv_now(&now);
368 return;
369 }
Willy Tarreaude99e992007-04-16 00:53:59 +0200370 }
371 last_skipped = 0;
372
Willy Tarreau6653d172007-05-13 01:52:05 +0200373 if (nbspec || status) {
374 /* Maybe we have processed some events that we must report, or
375 * maybe we still have events in the spec list, so we must not
376 * wait in epoll() otherwise we will delay their delivery by
377 * the next timeout.
378 */
379 wait_time = 0;
380 }
381
382 /* now let's wait for real events */
Willy Tarreaude99e992007-04-16 00:53:59 +0200383 status = epoll_wait(epoll_fd, epoll_events, maxfd, wait_time);
Willy Tarreau6653d172007-05-13 01:52:05 +0200384
Willy Tarreaude99e992007-04-16 00:53:59 +0200385 tv_now(&now);
386
387 for (count = 0; count < status; count++) {
388 int e = epoll_events[count].events;
389 fd = epoll_events[count].data.fd;
390
391 /* it looks complicated but gcc can optimize it away when constants
392 * have same values.
393 */
394 fdtab[fd].ev =
395 ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
396 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
397 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
398 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
399 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
400
401 if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200402 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200403 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200404 if (fdtab[fd].ev & (FD_POLL_RD|FD_POLL_HUP|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200405 fdtab[fd].cb[DIR_RD].f(fd);
406 }
407
408 if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) {
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200409 if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR)
Willy Tarreaude99e992007-04-16 00:53:59 +0200410 continue;
Willy Tarreau8bb46f42007-04-30 12:56:21 +0200411 if (fdtab[fd].ev & (FD_POLL_WR|FD_POLL_ERR))
Willy Tarreaude99e992007-04-16 00:53:59 +0200412 fdtab[fd].cb[DIR_WR].f(fd);
413 }
414 }
415}
416
417/*
418 * Initialization of the speculative epoll() poller.
419 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
420 * disables the poller by setting its pref to 0.
421 */
422REGPRM1 static int _do_init(struct poller *p)
423{
424 __label__ fail_fd_list, fail_spec, fail_ee, fail_fd;
425
426 p->private = NULL;
427
428 epoll_fd = epoll_create(global.maxsock + 1);
429 if (epoll_fd < 0)
430 goto fail_fd;
431
432 epoll_events = (struct epoll_event*)
433 calloc(1, sizeof(struct epoll_event) * global.maxsock);
434
435 if (epoll_events == NULL)
436 goto fail_ee;
437
438 if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL)
439 goto fail_spec;
440
441 fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock);
442 if (fd_list == NULL)
443 goto fail_fd_list;
444
445 return 1;
446
447 fail_fd_list:
448 free(spec_list);
449 fail_spec:
450 free(epoll_events);
451 fail_ee:
452 close(epoll_fd);
453 epoll_fd = 0;
454 fail_fd:
455 p->pref = 0;
456 return 0;
457}
458
459/*
460 * Termination of the speculative epoll() poller.
461 * Memory is released and the poller is marked as unselectable.
462 */
463REGPRM1 static void _do_term(struct poller *p)
464{
465 if (fd_list)
466 free(fd_list);
467 if (spec_list)
468 free(spec_list);
469 if (epoll_events)
470 free(epoll_events);
471
472 close(epoll_fd);
473 epoll_fd = 0;
474
475 fd_list = NULL;
476 spec_list = NULL;
477 epoll_events = NULL;
478
479 p->private = NULL;
480 p->pref = 0;
481}
482
483/*
484 * Check that the poller works.
485 * Returns 1 if OK, otherwise 0.
486 */
487REGPRM1 static int _do_test(struct poller *p)
488{
489 int fd;
490
491 fd = epoll_create(global.maxsock + 1);
492 if (fd < 0)
493 return 0;
494 close(fd);
495 return 1;
496}
497
498/*
499 * It is a constructor, which means that it will automatically be called before
500 * main(). This is GCC-specific but it works at least since 2.95.
501 * Special care must be taken so that it does not need any uninitialized data.
502 */
503__attribute__((constructor))
504static void _do_register(void)
505{
506 struct poller *p;
507
508 if (nbpollers >= MAX_POLLERS)
509 return;
510 p = &pollers[nbpollers++];
511
512 p->name = "sepoll";
513 p->pref = 400;
514 p->private = NULL;
515
516 p->test = _do_test;
517 p->init = _do_init;
518 p->term = _do_term;
519 p->poll = _do_poll;
520
521 p->is_set = __fd_is_set;
522 p->cond_s = p->set = __fd_set;
523 p->cond_c = p->clr = __fd_clr;
524 p->rem = __fd_rem;
525 p->clo = __fd_clo;
526}
527
528
529/*
530 * Local variables:
531 * c-indent-level: 8
532 * c-basic-offset: 8
533 * End:
534 */