blob: a86c59991669663d181d228eb59f65d2aaf8f5d4 [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 Tarreau4f60f162007-04-08 16:39:58 +020026#include <proto/task.h>
27
28
Willy Tarreau28d86862007-04-08 17:42:27 +020029static fd_set *fd_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020030
31/* private data */
32static struct pollfd *poll_events = NULL;
33
34
35/*
36 * Benchmarks performed on a Pentium-M notebook show that using functions
37 * instead of the usual macros improve the FD_* performance by about 80%,
38 * and that marking them regparm(2) adds another 20%.
39 */
Willy Tarreau63455a92007-04-09 15:34:49 +020040REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020041{
Willy Tarreau28d86862007-04-08 17:42:27 +020042 return FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020043}
44
Willy Tarreau97129b52007-04-09 00:54:46 +020045REGPRM2 static int __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020046{
Willy Tarreau28d86862007-04-08 17:42:27 +020047 FD_SET(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020048 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020049}
50
Willy Tarreau97129b52007-04-09 00:54:46 +020051REGPRM2 static int __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020052{
Willy Tarreau28d86862007-04-08 17:42:27 +020053 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020054 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020055}
56
Willy Tarreau97129b52007-04-09 00:54:46 +020057REGPRM2 static int __fd_cond_s(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020058{
59 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020060 ret = !FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020061 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020062 FD_SET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020063 return ret;
64}
65
Willy Tarreau97129b52007-04-09 00:54:46 +020066REGPRM2 static int __fd_cond_c(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020067{
68 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020069 ret = FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020070 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020071 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020072 return ret;
73}
74
75REGPRM1 static void __fd_rem(const int fd)
76{
Willy Tarreau28d86862007-04-08 17:42:27 +020077 FD_CLR(fd, fd_evts[DIR_RD]);
78 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020079}
80
Willy Tarreau4f60f162007-04-08 16:39:58 +020081/*
82 * Poll() poller
83 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020084REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020085{
86 int status;
87 int fd, nbfd;
Willy Tarreaud825eef2007-05-12 22:35:00 +020088 int wait_time;
Willy Tarreau4f60f162007-04-08 16:39:58 +020089
90 int fds, count;
91 int sr, sw;
92 unsigned rn, wn; /* read new, write new */
93
94 nbfd = 0;
Willy Tarreau177e2b02008-07-15 00:36:31 +020095 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +020096
Willy Tarreau28d86862007-04-08 17:42:27 +020097 rn = ((int*)fd_evts[DIR_RD])[fds];
98 wn = ((int*)fd_evts[DIR_WR])[fds];
Willy Tarreau4f60f162007-04-08 16:39:58 +020099
100 if ((rn|wn)) {
Willy Tarreau177e2b02008-07-15 00:36:31 +0200101 for (count = 0, fd = fds * BITS_PER_INT; count < BITS_PER_INT && fd < maxfd; count++, fd++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200102#define FDSETS_ARE_INT_ALIGNED
103#ifdef FDSETS_ARE_INT_ALIGNED
104
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200105#define WE_REALLY_KNOW_THAT_FDSETS_ARE_INTS
106#ifdef WE_REALLY_KNOW_THAT_FDSETS_ARE_INTS
Willy Tarreau4f60f162007-04-08 16:39:58 +0200107 sr = (rn >> count) & 1;
108 sw = (wn >> count) & 1;
109#else
Willy Tarreau177e2b02008-07-15 00:36:31 +0200110 sr = FD_ISSET(fd&(BITS_PER_INT-1), (typeof(fd_set*))&rn);
111 sw = FD_ISSET(fd&(BITS_PER_INT-1), (typeof(fd_set*))&wn);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200112#endif
113#else
Willy Tarreau28d86862007-04-08 17:42:27 +0200114 sr = FD_ISSET(fd, fd_evts[DIR_RD]);
115 sw = FD_ISSET(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200116#endif
117 if ((sr|sw)) {
118 poll_events[nbfd].fd = fd;
119 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
120 nbfd++;
121 }
122 }
123 }
124 }
125
126 /* now let's wait for events */
Willy Tarreau3a628112008-06-13 21:06:56 +0200127 if (run_queue)
128 wait_time = 0;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200129 else if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200130 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200131 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200132 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200133 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200134 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200135 if (wait_time > MAX_DELAY_MS)
136 wait_time = MAX_DELAY_MS;
137 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200138
Willy Tarreau4f60f162007-04-08 16:39:58 +0200139 status = poll(poll_events, nbfd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200140 tv_update_date(wait_time, status);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200141
142 for (count = 0; status > 0 && count < nbfd; count++) {
143 fd = poll_events[count].fd;
144
145 if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
146 continue;
147
148 /* ok, we found one active fd */
149 status--;
150
Willy Tarreau28d86862007-04-08 17:42:27 +0200151 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200152 if (fdtab[fd].state == FD_STCLOSE)
153 continue;
154 if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP ))
155 fdtab[fd].cb[DIR_RD].f(fd);
156 }
157
Willy Tarreau28d86862007-04-08 17:42:27 +0200158 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200159 if (fdtab[fd].state == FD_STCLOSE)
160 continue;
161 if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP ))
162 fdtab[fd].cb[DIR_WR].f(fd);
163 }
164 }
165
Willy Tarreaue54e9172007-04-09 09:23:31 +0200166}
167
168/*
169 * Initialization of the poll() poller.
170 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
171 * disables the poller by setting its pref to 0.
172 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200173REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200174{
175 __label__ fail_swevt, fail_srevt, fail_pe;
176 int fd_set_bytes;
177
178 p->private = NULL;
179 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
180
181 poll_events = (struct pollfd*)
182 calloc(1, sizeof(struct pollfd) * global.maxsock);
183
184 if (poll_events == NULL)
185 goto fail_pe;
186
187 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
188 goto fail_srevt;
189
190 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
191 goto fail_swevt;
192
193 return 1;
194
195 fail_swevt:
196 free(fd_evts[DIR_RD]);
197 fail_srevt:
198 free(poll_events);
199 fail_pe:
200 p->pref = 0;
201 return 0;
202}
203
204/*
205 * Termination of the poll() poller.
206 * Memory is released and the poller is marked as unselectable.
207 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200208REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200209{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200210 free(fd_evts[DIR_WR]);
211 free(fd_evts[DIR_RD]);
212 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200213 p->private = NULL;
214 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200215}
216
217/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200218 * Check that the poller works.
219 * Returns 1 if OK, otherwise 0.
220 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200221REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200222{
223 return 1;
224}
225
226/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200227 * It is a constructor, which means that it will automatically be called before
228 * main(). This is GCC-specific but it works at least since 2.95.
229 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200230 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200231__attribute__((constructor))
232static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200233{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200234 struct poller *p;
235
236 if (nbpollers >= MAX_POLLERS)
237 return;
238 p = &pollers[nbpollers++];
239
Willy Tarreau4f60f162007-04-08 16:39:58 +0200240 p->name = "poll";
241 p->pref = 200;
242 p->private = NULL;
243
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200244 p->test = _do_test;
245 p->init = _do_init;
246 p->term = _do_term;
247 p->poll = _do_poll;
Willy Tarreau63455a92007-04-09 15:34:49 +0200248 p->is_set = __fd_is_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200249 p->set = __fd_set;
250 p->clr = __fd_clr;
251 p->clo = p->rem = __fd_rem;
252 p->cond_s = __fd_cond_s;
253 p->cond_c = __fd_cond_c;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200254}
255
256
257/*
258 * Local variables:
259 * c-indent-level: 8
260 * c-basic-offset: 8
261 * End:
262 */