blob: 2ce4815d6eb8d351cc74bd30305a4cf7d7def344 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic poll()
3 *
Willy Tarreaucc7e3f72012-11-11 17:25:15 +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.
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
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010036REGPRM1 static void __fd_clo(const int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020037{
Willy Tarreau28d86862007-04-08 17:42:27 +020038 FD_CLR(fd, fd_evts[DIR_RD]);
39 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020040}
41
Willy Tarreau4f60f162007-04-08 16:39:58 +020042/*
43 * Poll() poller
44 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020045REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020046{
47 int status;
48 int fd, nbfd;
Willy Tarreaud825eef2007-05-12 22:35:00 +020049 int wait_time;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010050 int updt_idx, en, eo;
Willy Tarreau4f60f162007-04-08 16:39:58 +020051 int fds, count;
52 int sr, sw;
53 unsigned rn, wn; /* read new, write new */
54
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010055 /* first, scan the update list to find changes */
56 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
57 fd = fd_updt[updt_idx];
58 en = fdtab[fd].spec_e & 15; /* new events */
59 eo = fdtab[fd].spec_e >> 4; /* previous events */
60
61 if (fdtab[fd].owner && (eo ^ en)) {
62 if ((eo ^ en) & FD_EV_POLLED_RW) {
63 /* poll status changed, update the lists */
64 if ((eo & ~en) & FD_EV_POLLED_R)
65 FD_CLR(fd, fd_evts[DIR_RD]);
66 else if ((en & ~eo) & FD_EV_POLLED_R)
67 FD_SET(fd, fd_evts[DIR_RD]);
68
69 if ((eo & ~en) & FD_EV_POLLED_W)
70 FD_CLR(fd, fd_evts[DIR_WR]);
71 else if ((en & ~eo) & FD_EV_POLLED_W)
72 FD_SET(fd, fd_evts[DIR_WR]);
73 }
74
75 fdtab[fd].spec_e = (en << 4) + en; /* save new events */
76
77 if (!(en & FD_EV_ACTIVE_RW)) {
78 /* This fd doesn't use any active entry anymore, we can
79 * kill its entry.
80 */
81 release_spec_entry(fd);
82 }
83 else if ((en & ~eo) & FD_EV_ACTIVE_RW) {
84 /* we need a new spec entry now */
85 alloc_spec_entry(fd);
86 }
87
88 }
89 fdtab[fd].updated = 0;
90 fdtab[fd].new = 0;
91 }
92 fd_nbupdt = 0;
93
Willy Tarreau4f60f162007-04-08 16:39:58 +020094 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 Tarreaucc7e3f72012-11-11 17:25:15 +0100127 if (fd_nbspec || run_queue || signal_queue_len)
Willy Tarreau3a628112008-06-13 21:06:56 +0200128 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 Tarreau45a12512011-09-10 16:56:42 +0200139 gettimeofday(&before_poll, NULL);
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 Tarreau45a12512011-09-10 16:56:42 +0200142 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200143
144 for (count = 0; status > 0 && count < nbfd; count++) {
Willy Tarreau491c4982012-07-06 11:16:01 +0200145 int e = poll_events[count].revents;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200146 fd = poll_events[count].fd;
147
Willy Tarreau491c4982012-07-06 11:16:01 +0200148 if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200149 continue;
150
Willy Tarreau076be252012-07-06 16:02:29 +0200151 /* ok, we found one active fd */
152 status--;
153
154 if (!fdtab[fd].owner)
155 continue;
156
Willy Tarreau491c4982012-07-06 11:16:01 +0200157 fdtab[fd].ev &= FD_POLL_STICKY;
158 fdtab[fd].ev |=
159 ((e & POLLIN ) ? FD_POLL_IN : 0) |
160 ((e & POLLOUT) ? FD_POLL_OUT : 0) |
161 ((e & POLLERR) ? FD_POLL_ERR : 0) |
162 ((e & POLLHUP) ? FD_POLL_HUP : 0);
163
Willy Tarreaucc7e3f72012-11-11 17:25:15 +0100164 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) {
165 /* Mark the events as speculative before processing
166 * them so that if nothing can be done we don't need
167 * to poll again.
168 */
169 if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR))
170 fd_ev_set(fd, DIR_RD);
171
172 if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR))
173 fd_ev_set(fd, DIR_WR);
174
Willy Tarreau9845e752012-07-06 11:44:28 +0200175 fdtab[fd].iocb(fd);
Willy Tarreaucc7e3f72012-11-11 17:25:15 +0100176 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200177 }
178
Willy Tarreaue54e9172007-04-09 09:23:31 +0200179}
180
181/*
182 * Initialization of the poll() poller.
183 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
184 * disables the poller by setting its pref to 0.
185 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200186REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200187{
188 __label__ fail_swevt, fail_srevt, fail_pe;
189 int fd_set_bytes;
190
191 p->private = NULL;
192 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
193
194 poll_events = (struct pollfd*)
195 calloc(1, sizeof(struct pollfd) * global.maxsock);
196
197 if (poll_events == NULL)
198 goto fail_pe;
199
200 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
201 goto fail_srevt;
202
203 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
204 goto fail_swevt;
205
206 return 1;
207
208 fail_swevt:
209 free(fd_evts[DIR_RD]);
210 fail_srevt:
211 free(poll_events);
212 fail_pe:
213 p->pref = 0;
214 return 0;
215}
216
217/*
218 * Termination of the poll() poller.
219 * Memory is released and the poller is marked as unselectable.
220 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200221REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200222{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200223 free(fd_evts[DIR_WR]);
224 free(fd_evts[DIR_RD]);
225 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200226 p->private = NULL;
227 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200228}
229
230/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200231 * Check that the poller works.
232 * Returns 1 if OK, otherwise 0.
233 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200234REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200235{
236 return 1;
237}
238
239/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200240 * It is a constructor, which means that it will automatically be called before
241 * main(). This is GCC-specific but it works at least since 2.95.
242 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200243 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200244__attribute__((constructor))
245static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200246{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200247 struct poller *p;
248
249 if (nbpollers >= MAX_POLLERS)
250 return;
251 p = &pollers[nbpollers++];
252
Willy Tarreau4f60f162007-04-08 16:39:58 +0200253 p->name = "poll";
254 p->pref = 200;
255 p->private = NULL;
256
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100257 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200258 p->test = _do_test;
259 p->init = _do_init;
260 p->term = _do_term;
261 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200262}
263
264
265/*
266 * Local variables:
267 * c-indent-level: 8
268 * c-basic-offset: 8
269 * End:
270 */