blob: ec18863b44f9bc2b285f782c0caa089c9fb07f06 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic poll()
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 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.
10 *
11 */
12
13#include <unistd.h>
Willy Tarreau69801b82007-04-09 15:28:51 +020014#include <sys/poll.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020015#include <sys/time.h>
16#include <sys/types.h>
17
18#include <common/compat.h>
19#include <common/config.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020020#include <common/ticks.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020021#include <common/time.h>
22
Willy Tarreau4f60f162007-04-08 16:39:58 +020023#include <types/global.h>
24
25#include <proto/fd.h>
Willy Tarreau332740d2009-05-10 09:57:21 +020026#include <proto/signal.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020027#include <proto/task.h>
28
29
Willy Tarreau28d86862007-04-08 17:42:27 +020030static fd_set *fd_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020031
32/* private data */
33static struct pollfd *poll_events = NULL;
34
35
36/*
37 * Benchmarks performed on a Pentium-M notebook show that using functions
38 * instead of the usual macros improve the FD_* performance by about 80%,
39 * and that marking them regparm(2) adds another 20%.
40 */
Willy Tarreau63455a92007-04-09 15:34:49 +020041REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020042{
Willy Tarreau28d86862007-04-08 17:42:27 +020043 return FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020044}
45
Willy Tarreau97129b52007-04-09 00:54:46 +020046REGPRM2 static int __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020047{
Willy Tarreau28d86862007-04-08 17:42:27 +020048 FD_SET(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020049 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020050}
51
Willy Tarreau97129b52007-04-09 00:54:46 +020052REGPRM2 static int __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020053{
Willy Tarreau28d86862007-04-08 17:42:27 +020054 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020055 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020056}
57
Willy Tarreau97129b52007-04-09 00:54:46 +020058REGPRM2 static int __fd_cond_s(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020059{
60 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020061 ret = !FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020062 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020063 FD_SET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020064 return ret;
65}
66
Willy Tarreau97129b52007-04-09 00:54:46 +020067REGPRM2 static int __fd_cond_c(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020068{
69 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020070 ret = FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020071 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020072 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020073 return ret;
74}
75
76REGPRM1 static void __fd_rem(const int fd)
77{
Willy Tarreau28d86862007-04-08 17:42:27 +020078 FD_CLR(fd, fd_evts[DIR_RD]);
79 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020080}
81
Willy Tarreau4f60f162007-04-08 16:39:58 +020082/*
83 * Poll() poller
84 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020085REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020086{
87 int status;
88 int fd, nbfd;
Willy Tarreaud825eef2007-05-12 22:35:00 +020089 int wait_time;
Willy Tarreau4f60f162007-04-08 16:39:58 +020090
91 int fds, count;
92 int sr, sw;
93 unsigned rn, wn; /* read new, write new */
94
95 nbfd = 0;
Willy Tarreau177e2b02008-07-15 00:36:31 +020096 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +020097
Willy Tarreau28d86862007-04-08 17:42:27 +020098 rn = ((int*)fd_evts[DIR_RD])[fds];
99 wn = ((int*)fd_evts[DIR_WR])[fds];
Willy Tarreau4f60f162007-04-08 16:39:58 +0200100
101 if ((rn|wn)) {
Willy Tarreau177e2b02008-07-15 00:36:31 +0200102 for (count = 0, fd = fds * BITS_PER_INT; count < BITS_PER_INT && fd < maxfd; count++, fd++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200103#define FDSETS_ARE_INT_ALIGNED
104#ifdef FDSETS_ARE_INT_ALIGNED
105
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200106#define WE_REALLY_KNOW_THAT_FDSETS_ARE_INTS
107#ifdef WE_REALLY_KNOW_THAT_FDSETS_ARE_INTS
Willy Tarreau4f60f162007-04-08 16:39:58 +0200108 sr = (rn >> count) & 1;
109 sw = (wn >> count) & 1;
110#else
Willy Tarreau177e2b02008-07-15 00:36:31 +0200111 sr = FD_ISSET(fd&(BITS_PER_INT-1), (typeof(fd_set*))&rn);
112 sw = FD_ISSET(fd&(BITS_PER_INT-1), (typeof(fd_set*))&wn);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200113#endif
114#else
Willy Tarreau28d86862007-04-08 17:42:27 +0200115 sr = FD_ISSET(fd, fd_evts[DIR_RD]);
116 sw = FD_ISSET(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200117#endif
118 if ((sr|sw)) {
119 poll_events[nbfd].fd = fd;
120 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
121 nbfd++;
122 }
123 }
124 }
125 }
126
127 /* now let's wait for events */
Willy Tarreau332740d2009-05-10 09:57:21 +0200128 if (run_queue || signal_queue_len)
Willy Tarreau3a628112008-06-13 21:06:56 +0200129 wait_time = 0;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200130 else if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200131 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200132 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200133 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200134 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200135 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200136 if (wait_time > MAX_DELAY_MS)
137 wait_time = MAX_DELAY_MS;
138 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200139
Willy Tarreau45a12512011-09-10 16:56:42 +0200140 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200141 status = poll(poll_events, nbfd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200142 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200143 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200144
145 for (count = 0; status > 0 && count < nbfd; count++) {
146 fd = poll_events[count].fd;
147
148 if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
149 continue;
150
151 /* ok, we found one active fd */
152 status--;
153
Willy Tarreau28d86862007-04-08 17:42:27 +0200154 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200155 if (fdtab[fd].state == FD_STCLOSE)
156 continue;
157 if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP ))
158 fdtab[fd].cb[DIR_RD].f(fd);
159 }
160
Willy Tarreau28d86862007-04-08 17:42:27 +0200161 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200162 if (fdtab[fd].state == FD_STCLOSE)
163 continue;
164 if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP ))
165 fdtab[fd].cb[DIR_WR].f(fd);
166 }
167 }
168
Willy Tarreaue54e9172007-04-09 09:23:31 +0200169}
170
171/*
172 * Initialization of the poll() poller.
173 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
174 * disables the poller by setting its pref to 0.
175 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200176REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200177{
178 __label__ fail_swevt, fail_srevt, fail_pe;
179 int fd_set_bytes;
180
181 p->private = NULL;
182 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
183
184 poll_events = (struct pollfd*)
185 calloc(1, sizeof(struct pollfd) * global.maxsock);
186
187 if (poll_events == NULL)
188 goto fail_pe;
189
190 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
191 goto fail_srevt;
192
193 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
194 goto fail_swevt;
195
196 return 1;
197
198 fail_swevt:
199 free(fd_evts[DIR_RD]);
200 fail_srevt:
201 free(poll_events);
202 fail_pe:
203 p->pref = 0;
204 return 0;
205}
206
207/*
208 * Termination of the poll() poller.
209 * Memory is released and the poller is marked as unselectable.
210 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200211REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200212{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200213 free(fd_evts[DIR_WR]);
214 free(fd_evts[DIR_RD]);
215 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200216 p->private = NULL;
217 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200218}
219
220/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200221 * Check that the poller works.
222 * Returns 1 if OK, otherwise 0.
223 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200224REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200225{
226 return 1;
227}
228
229/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200230 * It is a constructor, which means that it will automatically be called before
231 * main(). This is GCC-specific but it works at least since 2.95.
232 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200233 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200234__attribute__((constructor))
235static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200236{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200237 struct poller *p;
238
239 if (nbpollers >= MAX_POLLERS)
240 return;
241 p = &pollers[nbpollers++];
242
Willy Tarreau4f60f162007-04-08 16:39:58 +0200243 p->name = "poll";
244 p->pref = 200;
245 p->private = NULL;
246
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200247 p->test = _do_test;
248 p->init = _do_init;
249 p->term = _do_term;
250 p->poll = _do_poll;
Willy Tarreau63455a92007-04-09 15:34:49 +0200251 p->is_set = __fd_is_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200252 p->set = __fd_set;
253 p->clr = __fd_clr;
254 p->clo = p->rem = __fd_rem;
255 p->cond_s = __fd_cond_s;
256 p->cond_c = __fd_cond_c;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200257}
258
259
260/*
261 * Local variables:
262 * c-indent-level: 8
263 * c-basic-offset: 8
264 * End:
265 */