blob: c4ac193d4c9b72c001b96ab1c5b06917c11f7d76 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic poll()
3 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01004 * Copyright 2000-2014 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 Tarreau80da05a2013-03-31 14:06:57 +020030static unsigned int *fd_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020031
32/* private data */
33static struct pollfd *poll_events = NULL;
34
35
Willy Tarreau80da05a2013-03-31 14:06:57 +020036static inline void hap_fd_set(int fd, unsigned int *evts)
37{
38 evts[fd / (8*sizeof(*evts))] |= 1U << (fd & (8*sizeof(*evts) - 1));
39}
40
41static inline void hap_fd_clr(int fd, unsigned int *evts)
42{
43 evts[fd / (8*sizeof(*evts))] &= ~(1U << (fd & (8*sizeof(*evts) - 1)));
44}
45
46REGPRM1 static void __fd_clo(int fd)
47{
48 hap_fd_clr(fd, fd_evts[DIR_RD]);
49 hap_fd_clr(fd, fd_evts[DIR_WR]);
50}
51
Willy Tarreau4f60f162007-04-08 16:39:58 +020052/*
53 * Poll() poller
54 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020055REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020056{
57 int status;
58 int fd, nbfd;
Willy Tarreaud825eef2007-05-12 22:35:00 +020059 int wait_time;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010060 int updt_idx, en, eo;
Willy Tarreau4f60f162007-04-08 16:39:58 +020061 int fds, count;
62 int sr, sw;
63 unsigned rn, wn; /* read new, write new */
64
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010065 /* first, scan the update list to find changes */
66 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
67 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010068 fdtab[fd].updated = 0;
69 fdtab[fd].new = 0;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010070
Willy Tarreauf817e9f2014-01-10 16:58:45 +010071 if (!fdtab[fd].owner)
72 continue;
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010073
Willy Tarreau25002d22014-01-25 10:32:56 +010074 eo = fdtab[fd].state;
75 en = fd_compute_new_polled_status(eo);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010076
77 if ((eo ^ en) & FD_EV_POLLED_RW) {
78 /* poll status changed, update the lists */
79 fdtab[fd].state = en;
80
81 if ((eo & ~en) & FD_EV_POLLED_R)
82 hap_fd_clr(fd, fd_evts[DIR_RD]);
83 else if ((en & ~eo) & FD_EV_POLLED_R)
84 hap_fd_set(fd, fd_evts[DIR_RD]);
85
86 if ((eo & ~en) & FD_EV_POLLED_W)
87 hap_fd_clr(fd, fd_evts[DIR_WR]);
88 else if ((en & ~eo) & FD_EV_POLLED_W)
89 hap_fd_set(fd, fd_evts[DIR_WR]);
90 }
91
92 fd_alloc_or_release_cache_entry(fd, en);
Willy Tarreaucc7e3f72012-11-11 17:25:15 +010093 }
94 fd_nbupdt = 0;
95
Willy Tarreau4f60f162007-04-08 16:39:58 +020096 nbfd = 0;
Willy Tarreau80da05a2013-03-31 14:06:57 +020097 for (fds = 0; (fds * 8*sizeof(**fd_evts)) < maxfd; fds++) {
98 rn = fd_evts[DIR_RD][fds];
99 wn = fd_evts[DIR_WR][fds];
Willy Tarreau4f60f162007-04-08 16:39:58 +0200100
Willy Tarreau80da05a2013-03-31 14:06:57 +0200101 if (!(rn|wn))
102 continue;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200103
Willy Tarreau80da05a2013-03-31 14:06:57 +0200104 for (count = 0, fd = fds * 8*sizeof(**fd_evts); count < 8*sizeof(**fd_evts) && fd < maxfd; count++, fd++) {
105 sr = (rn >> count) & 1;
106 sw = (wn >> count) & 1;
107 if ((sr|sw)) {
108 poll_events[nbfd].fd = fd;
109 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
110 nbfd++;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200111 }
112 }
113 }
114
115 /* now let's wait for events */
Willy Tarreau16f649c2014-01-25 19:10:48 +0100116 if (fd_cache_num || run_queue || signal_queue_len)
Willy Tarreau3a628112008-06-13 21:06:56 +0200117 wait_time = 0;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200118 else if (!exp)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200119 wait_time = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200120 else if (tick_is_expired(exp, now_ms))
Willy Tarreaubdefc512007-05-14 02:02:04 +0200121 wait_time = 0;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200122 else {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200123 wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200124 if (wait_time > MAX_DELAY_MS)
125 wait_time = MAX_DELAY_MS;
126 }
Willy Tarreaud825eef2007-05-12 22:35:00 +0200127
Willy Tarreau45a12512011-09-10 16:56:42 +0200128 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200129 status = poll(poll_events, nbfd, wait_time);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200130 tv_update_date(wait_time, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200131 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200132
133 for (count = 0; status > 0 && count < nbfd; count++) {
Willy Tarreau491c4982012-07-06 11:16:01 +0200134 int e = poll_events[count].revents;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200135 fd = poll_events[count].fd;
136
Willy Tarreau491c4982012-07-06 11:16:01 +0200137 if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200138 continue;
139
Willy Tarreau076be252012-07-06 16:02:29 +0200140 /* ok, we found one active fd */
141 status--;
142
143 if (!fdtab[fd].owner)
144 continue;
145
Willy Tarreau462c7202012-12-13 22:26:37 +0100146 /* it looks complicated but gcc can optimize it away when constants
147 * have same values... In fact it depends on gcc :-(
148 */
Willy Tarreau491c4982012-07-06 11:16:01 +0200149 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau462c7202012-12-13 22:26:37 +0100150 if (POLLIN == FD_POLL_IN && POLLOUT == FD_POLL_OUT &&
151 POLLERR == FD_POLL_ERR && POLLHUP == FD_POLL_HUP) {
152 fdtab[fd].ev |= e & (POLLIN|POLLOUT|POLLERR|POLLHUP);
153 }
154 else {
155 fdtab[fd].ev |=
156 ((e & POLLIN ) ? FD_POLL_IN : 0) |
157 ((e & POLLOUT) ? FD_POLL_OUT : 0) |
158 ((e & POLLERR) ? FD_POLL_ERR : 0) |
159 ((e & POLLHUP) ? FD_POLL_HUP : 0);
160 }
Willy Tarreau491c4982012-07-06 11:16:01 +0200161
Willy Tarreaue8525452014-01-25 09:58:06 +0100162 fd_process_polled_events(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200163 }
164
Willy Tarreaue54e9172007-04-09 09:23:31 +0200165}
166
167/*
168 * Initialization of the poll() poller.
169 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
170 * disables the poller by setting its pref to 0.
171 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200172REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200173{
174 __label__ fail_swevt, fail_srevt, fail_pe;
Willy Tarreau80da05a2013-03-31 14:06:57 +0200175 int fd_evts_bytes;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200176
177 p->private = NULL;
Willy Tarreau80da05a2013-03-31 14:06:57 +0200178 fd_evts_bytes = (global.maxsock + sizeof(**fd_evts) - 1) / sizeof(**fd_evts) * sizeof(**fd_evts);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200179
Willy Tarreau80da05a2013-03-31 14:06:57 +0200180 poll_events = calloc(1, sizeof(struct pollfd) * global.maxsock);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200181
182 if (poll_events == NULL)
183 goto fail_pe;
184
Willy Tarreau80da05a2013-03-31 14:06:57 +0200185 if ((fd_evts[DIR_RD] = calloc(1, fd_evts_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200186 goto fail_srevt;
187
Willy Tarreau80da05a2013-03-31 14:06:57 +0200188 if ((fd_evts[DIR_WR] = calloc(1, fd_evts_bytes)) == NULL)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200189 goto fail_swevt;
190
191 return 1;
192
193 fail_swevt:
194 free(fd_evts[DIR_RD]);
195 fail_srevt:
196 free(poll_events);
197 fail_pe:
198 p->pref = 0;
199 return 0;
200}
201
202/*
203 * Termination of the poll() poller.
204 * Memory is released and the poller is marked as unselectable.
205 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200206REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200207{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200208 free(fd_evts[DIR_WR]);
209 free(fd_evts[DIR_RD]);
210 free(poll_events);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200211 p->private = NULL;
212 p->pref = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200213}
214
215/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200216 * Check that the poller works.
217 * Returns 1 if OK, otherwise 0.
218 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200219REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200220{
221 return 1;
222}
223
224/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200225 * It is a constructor, which means that it will automatically be called before
226 * main(). This is GCC-specific but it works at least since 2.95.
227 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200228 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200229__attribute__((constructor))
230static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200231{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200232 struct poller *p;
233
234 if (nbpollers >= MAX_POLLERS)
235 return;
236 p = &pollers[nbpollers++];
237
Willy Tarreau4f60f162007-04-08 16:39:58 +0200238 p->name = "poll";
239 p->pref = 200;
240 p->private = NULL;
241
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100242 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200243 p->test = _do_test;
244 p->init = _do_init;
245 p->term = _do_term;
246 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200247}
248
249
250/*
251 * Local variables:
252 * c-indent-level: 8
253 * c-basic-offset: 8
254 * End:
255 */