blob: df1d1615c3b9699a70c73f49f1ddef7e87c78d74 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic select()
3 *
Willy Tarreau4d31fb22012-11-11 16:53:50 +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>
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 Tarreau332740d2009-05-10 09:57:21 +020025#include <proto/signal.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];
30static fd_set *tmp_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020031
Willy Tarreau4d31fb22012-11-11 16:53:50 +010032/* Immediately remove the entry upon close() */
33REGPRM1 static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020034{
Willy Tarreau28d86862007-04-08 17:42:27 +020035 FD_CLR(fd, fd_evts[DIR_RD]);
36 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020037}
38
Willy Tarreau4f60f162007-04-08 16:39:58 +020039/*
40 * Select() poller
41 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020042REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020043{
44 int status;
45 int fd, i;
46 struct timeval delta;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020047 int delta_ms;
Willy Tarreau4f60f162007-04-08 16:39:58 +020048 int readnotnull, writenotnull;
49 int fds;
Willy Tarreau4d31fb22012-11-11 16:53:50 +010050 int updt_idx, en, eo;
Willy Tarreau4f60f162007-04-08 16:39:58 +020051 char count;
52
Willy Tarreau4d31fb22012-11-11 16:53:50 +010053 /* first, scan the update list to find changes */
54 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
55 fd = fd_updt[updt_idx];
Willy Tarreau69a41fa2014-01-20 11:02:59 +010056 en = fdtab[fd].state & 15; /* new events */
57 eo = fdtab[fd].state >> 4; /* previous events */
Willy Tarreau4d31fb22012-11-11 16:53:50 +010058
59 if (fdtab[fd].owner && (eo ^ en)) {
60 if ((eo ^ en) & FD_EV_POLLED_RW) {
61 /* poll status changed, update the lists */
62 if ((eo & ~en) & FD_EV_POLLED_R)
63 FD_CLR(fd, fd_evts[DIR_RD]);
64 else if ((en & ~eo) & FD_EV_POLLED_R)
65 FD_SET(fd, fd_evts[DIR_RD]);
66
67 if ((eo & ~en) & FD_EV_POLLED_W)
68 FD_CLR(fd, fd_evts[DIR_WR]);
69 else if ((en & ~eo) & FD_EV_POLLED_W)
70 FD_SET(fd, fd_evts[DIR_WR]);
71 }
72
Willy Tarreau69a41fa2014-01-20 11:02:59 +010073 fdtab[fd].state = (en << 4) + en; /* save new events */
Willy Tarreau4d31fb22012-11-11 16:53:50 +010074
75 if (!(en & FD_EV_ACTIVE_RW)) {
76 /* This fd doesn't use any active entry anymore, we can
77 * kill its entry.
78 */
Willy Tarreau899d9572014-01-25 19:20:35 +010079 fd_release_cache_entry(fd);
Willy Tarreau4d31fb22012-11-11 16:53:50 +010080 }
81 else if ((en & ~eo) & FD_EV_ACTIVE_RW) {
Willy Tarreau899d9572014-01-25 19:20:35 +010082 /* we need a new cache entry now */
83 fd_alloc_cache_entry(fd);
Willy Tarreau4d31fb22012-11-11 16:53:50 +010084 }
Willy Tarreau4d31fb22012-11-11 16:53:50 +010085 }
86 fdtab[fd].updated = 0;
87 fdtab[fd].new = 0;
88 }
89 fd_nbupdt = 0;
90
Willy Tarreau0c303ee2008-07-07 00:09:58 +020091 delta_ms = 0;
92 delta.tv_sec = 0;
93 delta.tv_usec = 0;
94
Willy Tarreau16f649c2014-01-25 19:10:48 +010095 if (!fd_cache_num && !run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020096 if (!exp) {
97 delta_ms = MAX_DELAY_MS;
98 delta.tv_sec = (MAX_DELAY_MS / 1000);
99 delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200100 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200101 else if (!tick_is_expired(exp, now_ms)) {
102 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION;
103 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200104 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200105 delta.tv_sec = (delta_ms / 1000);
106 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200107 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200108 }
109
110 /* let's restore fdset state */
111
112 readnotnull = 0; writenotnull = 0;
113 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200114 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
115 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200116 }
117
118 // /* just a verification code, needs to be removed for performance */
119 // for (i=0; i<maxfd; i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200120 // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200121 // abort();
Willy Tarreau28d86862007-04-08 17:42:27 +0200122 // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200123 // abort();
124 //
125 // }
126
Willy Tarreau45a12512011-09-10 16:56:42 +0200127 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200128 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200129 readnotnull ? tmp_evts[DIR_RD] : NULL,
130 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200131 NULL,
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200132 &delta);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200133
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200134 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200135 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200136
137 if (status <= 0)
138 return;
139
Willy Tarreau177e2b02008-07-15 00:36:31 +0200140 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200141 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200142 continue;
143
Willy Tarreau177e2b02008-07-15 00:36:31 +0200144 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200145 /* if we specify read first, the accepts and zero reads will be
146 * seen first. Moreover, system buffers will be flushed faster.
147 */
Willy Tarreau076be252012-07-06 16:02:29 +0200148 if (!fdtab[fd].owner)
149 continue;
150
Willy Tarreau9845e752012-07-06 11:44:28 +0200151 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau076be252012-07-06 16:02:29 +0200152 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200153 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200154
Willy Tarreau076be252012-07-06 16:02:29 +0200155 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200156 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau9845e752012-07-06 11:44:28 +0200157
Willy Tarreaudb9cb0b2012-12-13 23:41:12 +0100158 if (fdtab[fd].iocb && fdtab[fd].ev) {
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100159 /* Mark the events as speculative before processing
160 * them so that if nothing can be done we don't need
161 * to poll again.
162 */
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100163 if (fdtab[fd].ev & FD_POLL_IN)
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100164 fd_ev_set(fd, DIR_RD);
165
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100166 if (fdtab[fd].ev & FD_POLL_OUT)
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100167 fd_ev_set(fd, DIR_WR);
168
Willy Tarreau15a4dec2014-01-20 11:09:39 +0100169 if (fdtab[fd].cache) {
Willy Tarreau39ebef82012-12-14 00:17:03 +0100170 /* This fd was already scheduled for being
171 * called as a speculative I/O.
172 */
173 continue;
174 }
175
Willy Tarreau9845e752012-07-06 11:44:28 +0200176 fdtab[fd].iocb(fd);
Willy Tarreau4d31fb22012-11-11 16:53:50 +0100177 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200178 }
179 }
180}
181
182/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200183 * Initialization of the select() poller.
184 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
185 * disables the poller by setting its pref to 0.
186 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200187REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200188{
189 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
190 int fd_set_bytes;
191
192 p->private = NULL;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200193
194 if (global.maxsock > FD_SETSIZE)
195 goto fail_revt;
196
Willy Tarreaue54e9172007-04-09 09:23:31 +0200197 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
198
199 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
200 goto fail_revt;
201
202 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
203 goto fail_wevt;
204
205 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
206 goto fail_srevt;
207
208 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
209 goto fail_swevt;
210
211 return 1;
212
213 fail_swevt:
214 free(fd_evts[DIR_RD]);
215 fail_srevt:
216 free(tmp_evts[DIR_WR]);
217 fail_wevt:
218 free(tmp_evts[DIR_RD]);
219 fail_revt:
220 p->pref = 0;
221 return 0;
222}
223
224/*
225 * Termination of the select() poller.
226 * Memory is released and the poller is marked as unselectable.
227 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200228REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200229{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200230 free(fd_evts[DIR_WR]);
231 free(fd_evts[DIR_RD]);
232 free(tmp_evts[DIR_WR]);
233 free(tmp_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200234 p->private = NULL;
235 p->pref = 0;
236}
237
238/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200239 * Check that the poller works.
240 * Returns 1 if OK, otherwise 0.
241 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200242REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200243{
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200244 if (global.maxsock > FD_SETSIZE)
245 return 0;
246
Willy Tarreau2ff76222007-04-09 19:29:56 +0200247 return 1;
248}
249
250/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200251 * It is a constructor, which means that it will automatically be called before
252 * main(). This is GCC-specific but it works at least since 2.95.
253 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200254 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200255__attribute__((constructor))
256static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200257{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200258 struct poller *p;
259
260 if (nbpollers >= MAX_POLLERS)
261 return;
262 p = &pollers[nbpollers++];
263
Willy Tarreau4f60f162007-04-08 16:39:58 +0200264 p->name = "select";
265 p->pref = 150;
266 p->private = NULL;
267
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100268 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200269 p->test = _do_test;
270 p->init = _do_init;
271 p->term = _do_term;
272 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200273}
274
275
276/*
277 * Local variables:
278 * c-indent-level: 8
279 * c-basic-offset: 8
280 * End:
281 */