blob: cf80ac856653756a2cda727546dd7269a33a4256 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic select()
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>
14#include <sys/time.h>
15#include <sys/types.h>
16
17#include <common/compat.h>
18#include <common/config.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020019#include <common/ticks.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020020#include <common/time.h>
21
Willy Tarreau4f60f162007-04-08 16:39:58 +020022#include <types/global.h>
23
24#include <proto/fd.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020025
26
Willy Tarreau28d86862007-04-08 17:42:27 +020027static fd_set *fd_evts[2];
28static fd_set *tmp_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020029
Willy Tarreau4d31fb22012-11-11 16:53:50 +010030/* Immediately remove the entry upon close() */
31REGPRM1 static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020032{
Willy Tarreau28d86862007-04-08 17:42:27 +020033 FD_CLR(fd, fd_evts[DIR_RD]);
34 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020035}
36
Willy Tarreau4f60f162007-04-08 16:39:58 +020037/*
38 * Select() poller
39 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020040REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020041{
42 int status;
43 int fd, i;
44 struct timeval delta;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020045 int delta_ms;
Willy Tarreau4f60f162007-04-08 16:39:58 +020046 int readnotnull, writenotnull;
47 int fds;
Willy Tarreau4d31fb22012-11-11 16:53:50 +010048 int updt_idx, en, eo;
Willy Tarreau4f60f162007-04-08 16:39:58 +020049 char count;
50
Willy Tarreau4d31fb22012-11-11 16:53:50 +010051 /* first, scan the update list to find changes */
52 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
53 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010054 fdtab[fd].updated = 0;
55 fdtab[fd].new = 0;
Willy Tarreau4d31fb22012-11-11 16:53:50 +010056
Willy Tarreauf817e9f2014-01-10 16:58:45 +010057 if (!fdtab[fd].owner)
58 continue;
Willy Tarreau4d31fb22012-11-11 16:53:50 +010059
Willy Tarreau25002d22014-01-25 10:32:56 +010060 eo = fdtab[fd].state;
61 en = fd_compute_new_polled_status(eo);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010062
63 if ((eo ^ en) & FD_EV_POLLED_RW) {
64 /* poll status changed, update the lists */
65 fdtab[fd].state = en;
66
67 if ((eo & ~en) & FD_EV_POLLED_R)
68 FD_CLR(fd, fd_evts[DIR_RD]);
69 else if ((en & ~eo) & FD_EV_POLLED_R)
70 FD_SET(fd, fd_evts[DIR_RD]);
71
72 if ((eo & ~en) & FD_EV_POLLED_W)
73 FD_CLR(fd, fd_evts[DIR_WR]);
74 else if ((en & ~eo) & FD_EV_POLLED_W)
75 FD_SET(fd, fd_evts[DIR_WR]);
76 }
Willy Tarreau4d31fb22012-11-11 16:53:50 +010077 }
78 fd_nbupdt = 0;
79
Willy Tarreau0c303ee2008-07-07 00:09:58 +020080 delta_ms = 0;
81 delta.tv_sec = 0;
82 delta.tv_usec = 0;
83
Willy Tarreau10146c92015-04-13 20:44:19 +020084 if (!exp) {
85 delta_ms = MAX_DELAY_MS;
86 delta.tv_sec = (MAX_DELAY_MS / 1000);
87 delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000;
88 }
89 else if (!tick_is_expired(exp, now_ms)) {
90 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION;
91 if (delta_ms > MAX_DELAY_MS)
92 delta_ms = MAX_DELAY_MS;
93 delta.tv_sec = (delta_ms / 1000);
94 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreau4f60f162007-04-08 16:39:58 +020095 }
96
97 /* let's restore fdset state */
98
99 readnotnull = 0; writenotnull = 0;
100 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200101 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
102 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200103 }
104
105 // /* just a verification code, needs to be removed for performance */
106 // for (i=0; i<maxfd; i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200107 // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200108 // abort();
Willy Tarreau28d86862007-04-08 17:42:27 +0200109 // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200110 // abort();
111 //
112 // }
113
Willy Tarreau45a12512011-09-10 16:56:42 +0200114 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200115 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200116 readnotnull ? tmp_evts[DIR_RD] : NULL,
117 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200118 NULL,
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200119 &delta);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200120
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200121 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200122 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200123
124 if (status <= 0)
125 return;
126
Willy Tarreau177e2b02008-07-15 00:36:31 +0200127 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200128 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200129 continue;
130
Willy Tarreau177e2b02008-07-15 00:36:31 +0200131 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200132 unsigned int n = 0;
133
Willy Tarreau4f60f162007-04-08 16:39:58 +0200134 /* if we specify read first, the accepts and zero reads will be
135 * seen first. Moreover, system buffers will be flushed faster.
136 */
Willy Tarreau076be252012-07-06 16:02:29 +0200137 if (!fdtab[fd].owner)
138 continue;
139
Willy Tarreau076be252012-07-06 16:02:29 +0200140 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Christopher Fauletab62f512017-08-30 10:34:36 +0200141 n |= FD_POLL_IN;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200142
Willy Tarreau076be252012-07-06 16:02:29 +0200143 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Christopher Fauletab62f512017-08-30 10:34:36 +0200144 n |= FD_POLL_OUT;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100145
Christopher Fauletab62f512017-08-30 10:34:36 +0200146 fd_update_events(fd, n);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200147 }
148 }
149}
150
151/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200152 * Initialization of the select() poller.
153 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
154 * disables the poller by setting its pref to 0.
155 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200156REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200157{
158 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
159 int fd_set_bytes;
160
161 p->private = NULL;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200162
163 if (global.maxsock > FD_SETSIZE)
164 goto fail_revt;
165
Willy Tarreaue54e9172007-04-09 09:23:31 +0200166 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
167
168 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
169 goto fail_revt;
170
171 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
172 goto fail_wevt;
173
174 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
175 goto fail_srevt;
176
177 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
178 goto fail_swevt;
179
180 return 1;
181
182 fail_swevt:
183 free(fd_evts[DIR_RD]);
184 fail_srevt:
185 free(tmp_evts[DIR_WR]);
186 fail_wevt:
187 free(tmp_evts[DIR_RD]);
188 fail_revt:
189 p->pref = 0;
190 return 0;
191}
192
193/*
194 * Termination of the select() poller.
195 * Memory is released and the poller is marked as unselectable.
196 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200197REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200198{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200199 free(fd_evts[DIR_WR]);
200 free(fd_evts[DIR_RD]);
201 free(tmp_evts[DIR_WR]);
202 free(tmp_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200203 p->private = NULL;
204 p->pref = 0;
205}
206
207/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200208 * Check that the poller works.
209 * Returns 1 if OK, otherwise 0.
210 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200211REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200212{
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200213 if (global.maxsock > FD_SETSIZE)
214 return 0;
215
Willy Tarreau2ff76222007-04-09 19:29:56 +0200216 return 1;
217}
218
219/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200220 * It is a constructor, which means that it will automatically be called before
221 * main(). This is GCC-specific but it works at least since 2.95.
222 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200223 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200224__attribute__((constructor))
225static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200226{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200227 struct poller *p;
228
229 if (nbpollers >= MAX_POLLERS)
230 return;
231 p = &pollers[nbpollers++];
232
Willy Tarreau4f60f162007-04-08 16:39:58 +0200233 p->name = "select";
234 p->pref = 150;
Willy Tarreau5a767692017-03-13 11:38:28 +0100235 p->flags = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200236 p->private = NULL;
237
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100238 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200239 p->test = _do_test;
240 p->init = _do_init;
241 p->term = _do_term;
242 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200243}
244
245
246/*
247 * Local variables:
248 * c-indent-level: 8
249 * c-basic-offset: 8
250 * End:
251 */