blob: 73fe327aa838413632d08de5f1546ff8d1659549 [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 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 Tarreauf817e9f2014-01-10 16:58:45 +010056 fdtab[fd].updated = 0;
57 fdtab[fd].new = 0;
Willy Tarreau4d31fb22012-11-11 16:53:50 +010058
Willy Tarreauf817e9f2014-01-10 16:58:45 +010059 if (!fdtab[fd].owner)
60 continue;
Willy Tarreau4d31fb22012-11-11 16:53:50 +010061
Willy Tarreau25002d22014-01-25 10:32:56 +010062 eo = fdtab[fd].state;
63 en = fd_compute_new_polled_status(eo);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010064
65 if ((eo ^ en) & FD_EV_POLLED_RW) {
66 /* poll status changed, update the lists */
67 fdtab[fd].state = en;
68
69 if ((eo & ~en) & FD_EV_POLLED_R)
70 FD_CLR(fd, fd_evts[DIR_RD]);
71 else if ((en & ~eo) & FD_EV_POLLED_R)
72 FD_SET(fd, fd_evts[DIR_RD]);
73
74 if ((eo & ~en) & FD_EV_POLLED_W)
75 FD_CLR(fd, fd_evts[DIR_WR]);
76 else if ((en & ~eo) & FD_EV_POLLED_W)
77 FD_SET(fd, fd_evts[DIR_WR]);
78 }
Willy Tarreau4d31fb22012-11-11 16:53:50 +010079 }
80 fd_nbupdt = 0;
81
Willy Tarreau0c303ee2008-07-07 00:09:58 +020082 delta_ms = 0;
83 delta.tv_sec = 0;
84 delta.tv_usec = 0;
85
Willy Tarreau16f649c2014-01-25 19:10:48 +010086 if (!fd_cache_num && !run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020087 if (!exp) {
88 delta_ms = MAX_DELAY_MS;
89 delta.tv_sec = (MAX_DELAY_MS / 1000);
90 delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020091 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +020092 else if (!tick_is_expired(exp, now_ms)) {
93 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION;
94 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020095 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +020096 delta.tv_sec = (delta_ms / 1000);
97 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreaud825eef2007-05-12 22:35:00 +020098 }
Willy Tarreau4f60f162007-04-08 16:39:58 +020099 }
100
101 /* let's restore fdset state */
102
103 readnotnull = 0; writenotnull = 0;
104 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200105 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
106 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200107 }
108
109 // /* just a verification code, needs to be removed for performance */
110 // for (i=0; i<maxfd; i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200111 // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200112 // abort();
Willy Tarreau28d86862007-04-08 17:42:27 +0200113 // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200114 // abort();
115 //
116 // }
117
Willy Tarreau45a12512011-09-10 16:56:42 +0200118 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200119 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200120 readnotnull ? tmp_evts[DIR_RD] : NULL,
121 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200122 NULL,
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200123 &delta);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200124
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200125 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200126 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200127
128 if (status <= 0)
129 return;
130
Willy Tarreau177e2b02008-07-15 00:36:31 +0200131 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200132 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200133 continue;
134
Willy Tarreau177e2b02008-07-15 00:36:31 +0200135 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200136 /* if we specify read first, the accepts and zero reads will be
137 * seen first. Moreover, system buffers will be flushed faster.
138 */
Willy Tarreau076be252012-07-06 16:02:29 +0200139 if (!fdtab[fd].owner)
140 continue;
141
Willy Tarreau9845e752012-07-06 11:44:28 +0200142 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau076be252012-07-06 16:02:29 +0200143 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200144 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200145
Willy Tarreau076be252012-07-06 16:02:29 +0200146 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200147 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau9845e752012-07-06 11:44:28 +0200148
Willy Tarreau5be2f352014-11-19 19:43:05 +0100149 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
150 fd_may_recv(fd);
151
152 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
153 fd_may_send(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200154 }
155 }
156}
157
158/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200159 * Initialization of the select() poller.
160 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
161 * disables the poller by setting its pref to 0.
162 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200163REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200164{
165 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
166 int fd_set_bytes;
167
168 p->private = NULL;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200169
170 if (global.maxsock > FD_SETSIZE)
171 goto fail_revt;
172
Willy Tarreaue54e9172007-04-09 09:23:31 +0200173 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
174
175 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
176 goto fail_revt;
177
178 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
179 goto fail_wevt;
180
181 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
182 goto fail_srevt;
183
184 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
185 goto fail_swevt;
186
187 return 1;
188
189 fail_swevt:
190 free(fd_evts[DIR_RD]);
191 fail_srevt:
192 free(tmp_evts[DIR_WR]);
193 fail_wevt:
194 free(tmp_evts[DIR_RD]);
195 fail_revt:
196 p->pref = 0;
197 return 0;
198}
199
200/*
201 * Termination of the select() poller.
202 * Memory is released and the poller is marked as unselectable.
203 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200204REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200205{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200206 free(fd_evts[DIR_WR]);
207 free(fd_evts[DIR_RD]);
208 free(tmp_evts[DIR_WR]);
209 free(tmp_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200210 p->private = NULL;
211 p->pref = 0;
212}
213
214/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200215 * Check that the poller works.
216 * Returns 1 if OK, otherwise 0.
217 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200218REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219{
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200220 if (global.maxsock > FD_SETSIZE)
221 return 0;
222
Willy Tarreau2ff76222007-04-09 19:29:56 +0200223 return 1;
224}
225
226/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200227 * It is a constructor, which means that it will automatically be called before
228 * main(). This is GCC-specific but it works at least since 2.95.
229 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200230 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200231__attribute__((constructor))
232static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200233{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200234 struct poller *p;
235
236 if (nbpollers >= MAX_POLLERS)
237 return;
238 p = &pollers[nbpollers++];
239
Willy Tarreau4f60f162007-04-08 16:39:58 +0200240 p->name = "select";
241 p->pref = 150;
242 p->private = NULL;
243
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100244 p->clo = __fd_clo;
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 Tarreau4f60f162007-04-08 16:39:58 +0200249}
250
251
252/*
253 * Local variables:
254 * c-indent-level: 8
255 * c-basic-offset: 8
256 * End:
257 */