blob: f5d011eea4bfe5595d8aee0a05ffe81c86ac088c [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 Tarreau4f60f162007-04-08 16:39:58 +0200140 status = poll(poll_events, nbfd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200141 tv_update_date(wait_time, status);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200142
143 for (count = 0; status > 0 && count < nbfd; count++) {
144 fd = poll_events[count].fd;
145
146 if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
147 continue;
148
149 /* ok, we found one active fd */
150 status--;
151
Willy Tarreau28d86862007-04-08 17:42:27 +0200152 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200153 if (fdtab[fd].state == FD_STCLOSE)
154 continue;
155 if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP ))
156 fdtab[fd].cb[DIR_RD].f(fd);
157 }
158
Willy Tarreau28d86862007-04-08 17:42:27 +0200159 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200160 if (fdtab[fd].state == FD_STCLOSE)
161 continue;
162 if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP ))
163 fdtab[fd].cb[DIR_WR].f(fd);
164 }
165 }
166
Willy Tarreaue54e9172007-04-09 09:23:31 +0200167}
168
169/*
170 * Initialization of the poll() poller.
171 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
172 * disables the poller by setting its pref to 0.
173 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200174REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200175{
176 __label__ fail_swevt, fail_srevt, fail_pe;
177 int fd_set_bytes;
178
179 p->private = NULL;
180 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
181
182 poll_events = (struct pollfd*)
183 calloc(1, sizeof(struct pollfd) * global.maxsock);
184
185 if (poll_events == NULL)
186 goto fail_pe;
187
188 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
189 goto fail_srevt;
190
191 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
192 goto fail_swevt;
193
194 return 1;
195
196 fail_swevt:
197 free(fd_evts[DIR_RD]);
198 fail_srevt:
199 free(poll_events);
200 fail_pe:
201 p->pref = 0;
202 return 0;
203}
204
205/*
206 * Termination of the poll() poller.
207 * Memory is released and the poller is marked as unselectable.
208 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200209REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200210{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200211 free(fd_evts[DIR_WR]);
212 free(fd_evts[DIR_RD]);
213 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200214 p->private = NULL;
215 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200216}
217
218/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219 * Check that the poller works.
220 * Returns 1 if OK, otherwise 0.
221 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200222REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200223{
224 return 1;
225}
226
227/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200228 * It is a constructor, which means that it will automatically be called before
229 * main(). This is GCC-specific but it works at least since 2.95.
230 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200231 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200232__attribute__((constructor))
233static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200234{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200235 struct poller *p;
236
237 if (nbpollers >= MAX_POLLERS)
238 return;
239 p = &pollers[nbpollers++];
240
Willy Tarreau4f60f162007-04-08 16:39:58 +0200241 p->name = "poll";
242 p->pref = 200;
243 p->private = NULL;
244
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200245 p->test = _do_test;
246 p->init = _do_init;
247 p->term = _do_term;
248 p->poll = _do_poll;
Willy Tarreau63455a92007-04-09 15:34:49 +0200249 p->is_set = __fd_is_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200250 p->set = __fd_set;
251 p->clr = __fd_clr;
252 p->clo = p->rem = __fd_rem;
253 p->cond_s = __fd_cond_s;
254 p->cond_c = __fd_cond_c;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200255}
256
257
258/*
259 * Local variables:
260 * c-indent-level: 8
261 * c-basic-offset: 8
262 * End:
263 */