blob: 35d3c77db7a604a95d8e8246faf99e34833e5637 [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++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200132 /* if we specify read first, the accepts and zero reads will be
133 * seen first. Moreover, system buffers will be flushed faster.
134 */
Willy Tarreau076be252012-07-06 16:02:29 +0200135 if (!fdtab[fd].owner)
136 continue;
137
Willy Tarreau9845e752012-07-06 11:44:28 +0200138 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau076be252012-07-06 16:02:29 +0200139 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200140 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200141
Willy Tarreau076be252012-07-06 16:02:29 +0200142 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200143 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau9845e752012-07-06 11:44:28 +0200144
Willy Tarreau5be2f352014-11-19 19:43:05 +0100145 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
146 fd_may_recv(fd);
147
148 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
149 fd_may_send(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200150 }
151 }
152}
153
154/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200155 * Initialization of the select() poller.
156 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
157 * disables the poller by setting its pref to 0.
158 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200159REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200160{
161 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
162 int fd_set_bytes;
163
164 p->private = NULL;
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200165
166 if (global.maxsock > FD_SETSIZE)
167 goto fail_revt;
168
Willy Tarreaue54e9172007-04-09 09:23:31 +0200169 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
170
171 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
172 goto fail_revt;
173
174 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
175 goto fail_wevt;
176
177 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
178 goto fail_srevt;
179
180 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
181 goto fail_swevt;
182
183 return 1;
184
185 fail_swevt:
186 free(fd_evts[DIR_RD]);
187 fail_srevt:
188 free(tmp_evts[DIR_WR]);
189 fail_wevt:
190 free(tmp_evts[DIR_RD]);
191 fail_revt:
192 p->pref = 0;
193 return 0;
194}
195
196/*
197 * Termination of the select() poller.
198 * Memory is released and the poller is marked as unselectable.
199 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200200REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200201{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200202 free(fd_evts[DIR_WR]);
203 free(fd_evts[DIR_RD]);
204 free(tmp_evts[DIR_WR]);
205 free(tmp_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200206 p->private = NULL;
207 p->pref = 0;
208}
209
210/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200211 * Check that the poller works.
212 * Returns 1 if OK, otherwise 0.
213 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200214REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200215{
Willy Tarreau3fa87b12013-03-31 14:41:15 +0200216 if (global.maxsock > FD_SETSIZE)
217 return 0;
218
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219 return 1;
220}
221
222/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200223 * It is a constructor, which means that it will automatically be called before
224 * main(). This is GCC-specific but it works at least since 2.95.
225 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200226 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200227__attribute__((constructor))
228static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200229{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200230 struct poller *p;
231
232 if (nbpollers >= MAX_POLLERS)
233 return;
234 p = &pollers[nbpollers++];
235
Willy Tarreau4f60f162007-04-08 16:39:58 +0200236 p->name = "select";
237 p->pref = 150;
238 p->private = NULL;
239
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100240 p->clo = __fd_clo;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200241 p->test = _do_test;
242 p->init = _do_init;
243 p->term = _do_term;
244 p->poll = _do_poll;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200245}
246
247
248/*
249 * Local variables:
250 * c-indent-level: 8
251 * c-basic-offset: 8
252 * End:
253 */