blob: 52c4454735deb71d5575219cb782e45529ca4c69 [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
Christopher Fauletd4604ad2017-05-29 10:40:41 +020027/* private data */
Willy Tarreau28d86862007-04-08 17:42:27 +020028static fd_set *fd_evts[2];
Christopher Fauletd4604ad2017-05-29 10:40:41 +020029static THREAD_LOCAL fd_set *tmp_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020030
Willy Tarreau4d31fb22012-11-11 16:53:50 +010031/* Immediately remove the entry upon close() */
32REGPRM1 static void __fd_clo(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020033{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010034 HA_SPIN_LOCK(POLL_LOCK, &poll_lock);
Willy Tarreau28d86862007-04-08 17:42:27 +020035 FD_CLR(fd, fd_evts[DIR_RD]);
36 FD_CLR(fd, fd_evts[DIR_WR]);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010037 HA_SPIN_UNLOCK(POLL_LOCK, &poll_lock);
Willy Tarreau4f60f162007-04-08 16:39:58 +020038}
39
Willy Tarreau4f60f162007-04-08 16:39:58 +020040/*
41 * Select() poller
42 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020043REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020044{
45 int status;
46 int fd, i;
47 struct timeval delta;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020048 int delta_ms;
Willy Tarreau4f60f162007-04-08 16:39:58 +020049 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;
Christopher Fauletd4604ad2017-05-29 10:40:41 +020052 int readnotnull, writenotnull;
53
Willy Tarreau4d31fb22012-11-11 16:53:50 +010054 /* first, scan the update list to find changes */
55 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
56 fd = fd_updt[updt_idx];
Willy Tarreau4d31fb22012-11-11 16:53:50 +010057
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010058 if (!fdtab[fd].owner) {
59 activity[tid].poll_drop++;
Willy Tarreauf817e9f2014-01-10 16:58:45 +010060 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +010061 }
Willy Tarreau4d31fb22012-11-11 16:53:50 +010062
Christopher Faulet2a944ee2017-11-07 10:42:54 +010063 HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock);
Christopher Fauletd4604ad2017-05-29 10:40:41 +020064 fdtab[fd].updated = 0;
65 fdtab[fd].new = 0;
66
Willy Tarreau25002d22014-01-25 10:32:56 +010067 eo = fdtab[fd].state;
68 en = fd_compute_new_polled_status(eo);
Christopher Fauletd4604ad2017-05-29 10:40:41 +020069 fdtab[fd].state = en;
Christopher Faulet2a944ee2017-11-07 10:42:54 +010070 HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010071
72 if ((eo ^ en) & FD_EV_POLLED_RW) {
73 /* poll status changed, update the lists */
Christopher Faulet2a944ee2017-11-07 10:42:54 +010074 HA_SPIN_LOCK(POLL_LOCK, &poll_lock);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010075 if ((eo & ~en) & FD_EV_POLLED_R)
76 FD_CLR(fd, fd_evts[DIR_RD]);
77 else if ((en & ~eo) & FD_EV_POLLED_R)
78 FD_SET(fd, fd_evts[DIR_RD]);
79
80 if ((eo & ~en) & FD_EV_POLLED_W)
81 FD_CLR(fd, fd_evts[DIR_WR]);
82 else if ((en & ~eo) & FD_EV_POLLED_W)
83 FD_SET(fd, fd_evts[DIR_WR]);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010084 HA_SPIN_UNLOCK(POLL_LOCK, &poll_lock);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010085 }
Willy Tarreau4d31fb22012-11-11 16:53:50 +010086 }
87 fd_nbupdt = 0;
88
Christopher Fauletd4604ad2017-05-29 10:40:41 +020089 /* let's restore fdset state */
90 readnotnull = 0; writenotnull = 0;
91 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
92 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
93 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
94 }
95
96#if 0
97 /* just a verification code, needs to be removed for performance */
98 for (i=0; i<maxfd; i++) {
99 if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
100 abort();
101 if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
102 abort();
103 }
104#endif
105
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200106 delta_ms = 0;
107 delta.tv_sec = 0;
108 delta.tv_usec = 0;
109
Willy Tarreau10146c92015-04-13 20:44:19 +0200110 if (!exp) {
111 delta_ms = MAX_DELAY_MS;
112 delta.tv_sec = (MAX_DELAY_MS / 1000);
113 delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000;
114 }
115 else if (!tick_is_expired(exp, now_ms)) {
116 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION;
117 if (delta_ms > MAX_DELAY_MS)
118 delta_ms = MAX_DELAY_MS;
119 delta.tv_sec = (delta_ms / 1000);
120 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200121 }
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100122 else
123 activity[tid].poll_exp++;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200124
Willy Tarreau45a12512011-09-10 16:56:42 +0200125 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200126 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200127 readnotnull ? tmp_evts[DIR_RD] : NULL,
128 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200129 NULL,
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200130 &delta);
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200131
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200132 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200133 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200134
135 if (status <= 0)
136 return;
137
Willy Tarreau177e2b02008-07-15 00:36:31 +0200138 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200139 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200140 continue;
141
Willy Tarreau177e2b02008-07-15 00:36:31 +0200142 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Christopher Fauletab62f512017-08-30 10:34:36 +0200143 unsigned int n = 0;
144
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100145 if (!fdtab[fd].owner) {
146 activity[tid].poll_dead++;
147 continue;
148 }
149
150 if (!(fdtab[fd].thread_mask & tid_bit)) {
151 activity[tid].poll_skip++;
Willy Tarreau076be252012-07-06 16:02:29 +0200152 continue;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +0100153 }
Willy Tarreau076be252012-07-06 16:02:29 +0200154
Willy Tarreau076be252012-07-06 16:02:29 +0200155 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Christopher Fauletab62f512017-08-30 10:34:36 +0200156 n |= FD_POLL_IN;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200157
Willy Tarreau076be252012-07-06 16:02:29 +0200158 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Christopher Fauletab62f512017-08-30 10:34:36 +0200159 n |= FD_POLL_OUT;
Willy Tarreau5be2f352014-11-19 19:43:05 +0100160
Christopher Fauletab62f512017-08-30 10:34:36 +0200161 fd_update_events(fd, n);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200162 }
163 }
164}
165
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200166static int init_select_per_thread()
167{
168 int fd_set_bytes;
169
170 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
171 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
172 goto fail;
173 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
174 goto fail;
175 return 1;
176 fail:
177 free(tmp_evts[DIR_RD]);
178 free(tmp_evts[DIR_WR]);
179 return 0;
180}
181
182static void deinit_select_per_thread()
183{
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200184 free(tmp_evts[DIR_WR]); tmp_evts[DIR_WR] = NULL;
185 free(tmp_evts[DIR_RD]); tmp_evts[DIR_RD] = NULL;
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200186}
187
Willy Tarreau4f60f162007-04-08 16:39:58 +0200188/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200189 * Initialization of the select() poller.
190 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
191 * disables the poller by setting its pref to 0.
192 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200193REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200194{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200195 __label__ fail_swevt, fail_srevt, fail_revt;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200196 int fd_set_bytes;
197
198 p->private = NULL;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200199
200 if (global.maxsock > FD_SETSIZE)
201 goto fail_revt;
202
Willy Tarreaue54e9172007-04-09 09:23:31 +0200203 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200204
205 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
206 goto fail_srevt;
Willy Tarreaue54e9172007-04-09 09:23:31 +0200207 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
208 goto fail_swevt;
209
Christopher Fauletcd7879a2017-10-27 13:53:47 +0200210 hap_register_per_thread_init(init_select_per_thread);
211 hap_register_per_thread_deinit(deinit_select_per_thread);
212
Willy Tarreaue54e9172007-04-09 09:23:31 +0200213 return 1;
214
215 fail_swevt:
216 free(fd_evts[DIR_RD]);
217 fail_srevt:
218 free(tmp_evts[DIR_WR]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200219 free(tmp_evts[DIR_RD]);
220 fail_revt:
221 p->pref = 0;
222 return 0;
223}
224
225/*
226 * Termination of the select() poller.
227 * Memory is released and the poller is marked as unselectable.
228 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200229REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200230{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200231 free(fd_evts[DIR_WR]);
232 free(fd_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200233 p->private = NULL;
234 p->pref = 0;
235}
236
237/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200238 * Check that the poller works.
239 * Returns 1 if OK, otherwise 0.
240 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200241REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200242{
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200243 if (global.maxsock > FD_SETSIZE)
244 return 0;
245
Willy Tarreau2ff76222007-04-09 19:29:56 +0200246 return 1;
247}
248
249/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200250 * It is a constructor, which means that it will automatically be called before
251 * main(). This is GCC-specific but it works at least since 2.95.
252 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200253 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200254__attribute__((constructor))
255static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200256{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200257 struct poller *p;
258
259 if (nbpollers >= MAX_POLLERS)
260 return;
261 p = &pollers[nbpollers++];
262
Willy Tarreau4f60f162007-04-08 16:39:58 +0200263 p->name = "select";
264 p->pref = 150;
Willy Tarreau5a767692017-03-13 11:38:28 +0100265 p->flags = 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200266 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 */