blob: d53e0a901754fa175b6f10877343a20cd2eac9a5 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
Willy Tarreaue9f49e72012-11-11 17:42:00 +01002 * FD polling functions for Linux epoll
Willy Tarreau4f60f162007-04-08 16:39:58 +02003 *
Willy Tarreaue9f49e72012-11-11 17:42:00 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreau4f60f162007-04-08 16:39:58 +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.
Willy Tarreau4f60f162007-04-08 16:39:58 +020010 */
11
12#include <unistd.h>
13#include <sys/time.h>
14#include <sys/types.h>
15
16#include <common/compat.h>
17#include <common/config.h>
Willy Tarreaue9f49e72012-11-11 17:42:00 +010018#include <common/debug.h>
Willy Tarreau43d8fb22011-08-22 17:12:02 +020019#include <common/epoll.h>
Willy Tarreau58094f22007-04-10 01:33:20 +020020#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020021#include <common/ticks.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020022#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020023#include <common/tools.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020024
Willy Tarreau4f60f162007-04-08 16:39:58 +020025#include <types/global.h>
26
Willy Tarreaue9f49e72012-11-11 17:42:00 +010027#include <proto/fd.h>
Willy Tarreau332740d2009-05-10 09:57:21 +020028#include <proto/signal.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020029#include <proto/task.h>
30
Willy Tarreau58094f22007-04-10 01:33:20 +020031
Willy Tarreaue9f49e72012-11-11 17:42:00 +010032static int absmaxevents = 0; // absolute maximum amounts of polled events
Willy Tarreau4f60f162007-04-08 16:39:58 +020033
34/* private data */
35static struct epoll_event *epoll_events;
36static int epoll_fd;
37
Willy Tarreau58094f22007-04-10 01:33:20 +020038/* This structure may be used for any purpose. Warning! do not use it in
39 * recursive functions !
40 */
41static struct epoll_event ev;
42
Willy Tarreau1c07b072013-01-07 16:19:18 +010043#ifndef EPOLLRDHUP
44/* EPOLLRDHUP was defined late in libc, and it appeared in kernel 2.6.17 */
45#define EPOLLRDHUP 0x2000
46#endif
47
Willy Tarreau4f60f162007-04-08 16:39:58 +020048/*
Willy Tarreaue9f49e72012-11-11 17:42:00 +010049 * speculative epoll() poller
Willy Tarreau4f60f162007-04-08 16:39:58 +020050 */
Willy Tarreaue9f49e72012-11-11 17:42:00 +010051REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020052{
Willy Tarreaue9f49e72012-11-11 17:42:00 +010053 int status, eo, en;
54 int fd, opcode;
55 int count;
56 int updt_idx;
57 int wait_time;
Willy Tarreau58094f22007-04-10 01:33:20 +020058
Willy Tarreaue9f49e72012-11-11 17:42:00 +010059 /* first, scan the update list to find changes */
60 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
61 fd = fd_updt[updt_idx];
62 en = fdtab[fd].spec_e & 15; /* new events */
63 eo = fdtab[fd].spec_e >> 4; /* previous events */
Willy Tarreau58094f22007-04-10 01:33:20 +020064
Willy Tarreaue9f49e72012-11-11 17:42:00 +010065 if (fdtab[fd].owner && (eo ^ en)) {
66 if ((eo ^ en) & FD_EV_POLLED_RW) {
Willy Tarreau2f877302013-11-15 22:48:31 +010067 /* poll status changed. We'll have to run some syscalls
68 * for this, so let's merge any pending speculative events
69 * into them in order to avoid possible future failed calls
70 * (typically recv()). In practice on a slow connection
71 * establishment, this saves one epoll_ctl() and one recv().
72 */
73 en = (en & FD_EV_POLLED_RW) | ((en & FD_EV_ACTIVE_RW) * FD_EV_POLLED / FD_EV_ACTIVE);
74
Willy Tarreaue9f49e72012-11-11 17:42:00 +010075 if ((en & FD_EV_POLLED_RW) == 0) {
76 /* fd removed from poll list */
77 opcode = EPOLL_CTL_DEL;
78 }
79 else if ((eo & FD_EV_POLLED_RW) == 0) {
80 /* new fd in the poll list */
81 opcode = EPOLL_CTL_ADD;
82 }
83 else {
84 /* fd status changed */
85 opcode = EPOLL_CTL_MOD;
86 }
Willy Tarreau58094f22007-04-10 01:33:20 +020087
Willy Tarreaue9f49e72012-11-11 17:42:00 +010088 /* construct the epoll events based on new state */
89 ev.events = 0;
90 if (en & FD_EV_POLLED_R)
Willy Tarreau1c07b072013-01-07 16:19:18 +010091 ev.events |= EPOLLIN | EPOLLRDHUP;
Willy Tarreau58094f22007-04-10 01:33:20 +020092
Willy Tarreaue9f49e72012-11-11 17:42:00 +010093 if (en & FD_EV_POLLED_W)
94 ev.events |= EPOLLOUT;
Willy Tarreau58094f22007-04-10 01:33:20 +020095
Willy Tarreaue9f49e72012-11-11 17:42:00 +010096 ev.data.fd = fd;
97 epoll_ctl(epoll_fd, opcode, fd, &ev);
Willy Tarreau58094f22007-04-10 01:33:20 +020098 }
Willy Tarreau58094f22007-04-10 01:33:20 +020099
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100100 fdtab[fd].spec_e = (en << 4) + en; /* save new events */
Willy Tarreauf8cfa442012-10-04 21:54:41 +0200101
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100102 if (!(en & FD_EV_ACTIVE_RW)) {
103 /* This fd doesn't use any active entry anymore, we can
104 * kill its entry.
105 */
106 release_spec_entry(fd);
107 }
108 else if ((en & ~eo) & FD_EV_ACTIVE_RW) {
109 /* we need a new spec entry now */
110 alloc_spec_entry(fd);
111 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200112
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100113 }
114 fdtab[fd].updated = 0;
115 fdtab[fd].new = 0;
Willy Tarreau58094f22007-04-10 01:33:20 +0200116 }
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100117 fd_nbupdt = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200118
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100119 /* compute the epoll_wait() timeout */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200120
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100121 if (fd_nbspec || run_queue || signal_queue_len) {
122 /* Maybe we still have events in the spec list, or there are
123 * some tasks left pending in the run_queue, so we must not
124 * wait in epoll() otherwise we would delay their delivery by
125 * the next timeout.
126 */
Willy Tarreaubdefc512007-05-14 02:02:04 +0200127 wait_time = 0;
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100128 }
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200129 else {
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100130 if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200131 wait_time = MAX_DELAY_MS;
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100132 else if (tick_is_expired(exp, now_ms))
133 wait_time = 0;
134 else {
135 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
136 if (wait_time > MAX_DELAY_MS)
137 wait_time = MAX_DELAY_MS;
138 }
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200139 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200140
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100141 /* now let's wait for polled events */
142
Willy Tarreau45a12512011-09-10 16:56:42 +0200143 gettimeofday(&before_poll, NULL);
Willy Tarreaucf181c92013-01-18 15:22:41 +0100144 status = epoll_wait(epoll_fd, epoll_events, global.tune.maxpollevents, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200145 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200146 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200147
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100148 /* process polled events */
149
Willy Tarreau4f60f162007-04-08 16:39:58 +0200150 for (count = 0; count < status; count++) {
Willy Tarreau1c07b072013-01-07 16:19:18 +0100151 unsigned int n;
152 unsigned int e = epoll_events[count].events;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200153 fd = epoll_events[count].data.fd;
154
Willy Tarreau076be252012-07-06 16:02:29 +0200155 if (!fdtab[fd].owner)
156 continue;
157
Willy Tarreau491c4982012-07-06 11:16:01 +0200158 /* it looks complicated but gcc can optimize it away when constants
Willy Tarreau462c7202012-12-13 22:26:37 +0100159 * have same values... In fact it depends on gcc :-(
Willy Tarreau491c4982012-07-06 11:16:01 +0200160 */
161 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau462c7202012-12-13 22:26:37 +0100162 if (EPOLLIN == FD_POLL_IN && EPOLLOUT == FD_POLL_OUT &&
163 EPOLLPRI == FD_POLL_PRI && EPOLLERR == FD_POLL_ERR &&
164 EPOLLHUP == FD_POLL_HUP) {
Willy Tarreau6320c3c2012-12-13 23:52:58 +0100165 n = e & (EPOLLIN|EPOLLOUT|EPOLLPRI|EPOLLERR|EPOLLHUP);
Willy Tarreau462c7202012-12-13 22:26:37 +0100166 }
167 else {
Willy Tarreau6320c3c2012-12-13 23:52:58 +0100168 n = ((e & EPOLLIN ) ? FD_POLL_IN : 0) |
Willy Tarreau462c7202012-12-13 22:26:37 +0100169 ((e & EPOLLPRI) ? FD_POLL_PRI : 0) |
170 ((e & EPOLLOUT) ? FD_POLL_OUT : 0) |
171 ((e & EPOLLERR) ? FD_POLL_ERR : 0) |
172 ((e & EPOLLHUP) ? FD_POLL_HUP : 0);
173 }
Willy Tarreau491c4982012-07-06 11:16:01 +0200174
Willy Tarreau1c07b072013-01-07 16:19:18 +0100175 /* always remap RDHUP to HUP as they're used similarly */
176 if (e & EPOLLRDHUP)
177 n |= FD_POLL_HUP;
178
Willy Tarreau6320c3c2012-12-13 23:52:58 +0100179 if (!n)
180 continue;
181
182 fdtab[fd].ev |= n;
183
184 if (fdtab[fd].iocb) {
Willy Tarreaufb5470d2012-12-14 00:02:33 +0100185 int new_updt, old_updt;
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100186
187 /* Mark the events as speculative before processing
188 * them so that if nothing can be done we don't need
189 * to poll again.
190 */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100191 if (fdtab[fd].ev & FD_POLL_IN)
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100192 fd_ev_set(fd, DIR_RD);
193
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100194 if (fdtab[fd].ev & FD_POLL_OUT)
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100195 fd_ev_set(fd, DIR_WR);
196
Willy Tarreau39ebef82012-12-14 00:17:03 +0100197 if (fdtab[fd].spec_p) {
198 /* This fd was already scheduled for being called as a speculative I/O */
199 continue;
200 }
201
Willy Tarreaufb5470d2012-12-14 00:02:33 +0100202 /* Save number of updates to detect creation of new FDs. */
203 old_updt = fd_nbupdt;
Willy Tarreau9845e752012-07-06 11:44:28 +0200204 fdtab[fd].iocb(fd);
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100205
206 /* One or more fd might have been created during the iocb().
207 * This mainly happens with new incoming connections that have
208 * just been accepted, so we'd like to process them immediately
209 * for better efficiency. Second benefit, if at the end the fds
210 * are disabled again, we can safely destroy their update entry
211 * to reduce the scope of later scans. This is the reason we
212 * scan the new entries backwards.
213 */
214
215 for (new_updt = fd_nbupdt; new_updt > old_updt; new_updt--) {
216 fd = fd_updt[new_updt - 1];
217 if (!fdtab[fd].new)
218 continue;
219
220 fdtab[fd].new = 0;
221 fdtab[fd].ev &= FD_POLL_STICKY;
222
223 if ((fdtab[fd].spec_e & FD_EV_STATUS_R) == FD_EV_ACTIVE_R)
224 fdtab[fd].ev |= FD_POLL_IN;
225
226 if ((fdtab[fd].spec_e & FD_EV_STATUS_W) == FD_EV_ACTIVE_W)
227 fdtab[fd].ev |= FD_POLL_OUT;
228
229 if (fdtab[fd].ev && fdtab[fd].iocb && fdtab[fd].owner)
230 fdtab[fd].iocb(fd);
231
232 /* we can remove this update entry if it's the last one and is
233 * unused, otherwise we don't touch anything.
234 */
235 if (new_updt == fd_nbupdt && fdtab[fd].spec_e == 0) {
236 fdtab[fd].updated = 0;
237 fd_nbupdt--;
238 }
239 }
240 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200241 }
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100242
243 /* the caller will take care of speculative events */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200244}
245
246/*
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100247 * Initialization of the speculative epoll() poller.
Willy Tarreaue54e9172007-04-09 09:23:31 +0200248 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
249 * disables the poller by setting its pref to 0.
250 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200251REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200252{
Willy Tarreaue54e9172007-04-09 09:23:31 +0200253 p->private = NULL;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200254
255 epoll_fd = epoll_create(global.maxsock + 1);
256 if (epoll_fd < 0)
257 goto fail_fd;
258
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100259 /* See comments at the top of the file about this formula. */
260 absmaxevents = MAX(global.tune.maxpollevents, global.maxsock);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200261 epoll_events = (struct epoll_event*)
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100262 calloc(1, sizeof(struct epoll_event) * absmaxevents);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200263
264 if (epoll_events == NULL)
265 goto fail_ee;
266
Willy Tarreaue54e9172007-04-09 09:23:31 +0200267 return 1;
268
Willy Tarreaue54e9172007-04-09 09:23:31 +0200269 fail_ee:
270 close(epoll_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200271 epoll_fd = -1;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200272 fail_fd:
273 p->pref = 0;
274 return 0;
275}
276
277/*
Willy Tarreaue9f49e72012-11-11 17:42:00 +0100278 * Termination of the speculative epoll() poller.
Willy Tarreaue54e9172007-04-09 09:23:31 +0200279 * Memory is released and the poller is marked as unselectable.
280 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200281REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200282{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200283 free(epoll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200284
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200285 if (epoll_fd >= 0) {
286 close(epoll_fd);
287 epoll_fd = -1;
288 }
Willy Tarreaue54e9172007-04-09 09:23:31 +0200289
Willy Tarreau58094f22007-04-10 01:33:20 +0200290 epoll_events = NULL;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200291 p->private = NULL;
292 p->pref = 0;
293}
294
295/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200296 * Check that the poller works.
297 * Returns 1 if OK, otherwise 0.
298 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200299REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200300{
301 int fd;
302
303 fd = epoll_create(global.maxsock + 1);
304 if (fd < 0)
305 return 0;
306 close(fd);
307 return 1;
308}
309
310/*
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200311 * Recreate the epoll file descriptor after a fork(). Returns 1 if OK,
312 * otherwise 0. It will ensure that all processes will not share their
313 * epoll_fd. Some side effects were encountered because of this, such
314 * as epoll_wait() returning an FD which was previously deleted.
315 */
316REGPRM1 static int _do_fork(struct poller *p)
317{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200318 if (epoll_fd >= 0)
319 close(epoll_fd);
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200320 epoll_fd = epoll_create(global.maxsock + 1);
321 if (epoll_fd < 0)
322 return 0;
323 return 1;
324}
325
326/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200327 * It is a constructor, which means that it will automatically be called before
328 * main(). This is GCC-specific but it works at least since 2.95.
329 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200330 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200331__attribute__((constructor))
332static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200333{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200334 struct poller *p;
335
336 if (nbpollers >= MAX_POLLERS)
337 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200338
339 epoll_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200340 p = &pollers[nbpollers++];
341
Willy Tarreau4f60f162007-04-08 16:39:58 +0200342 p->name = "epoll";
343 p->pref = 300;
344 p->private = NULL;
345
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100346 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200347 p->test = _do_test;
348 p->init = _do_init;
349 p->term = _do_term;
350 p->poll = _do_poll;
Willy Tarreaufb8983f2007-06-03 16:40:44 +0200351 p->fork = _do_fork;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200352}
353
354
355/*
356 * Local variables:
357 * c-indent-level: 8
358 * c-basic-offset: 8
359 * End:
360 */