blob: 100bc575659592e73beb8977097b8ad059b62875 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic select()
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 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
32
33/*
34 * Benchmarks performed on a Pentium-M notebook show that using functions
35 * instead of the usual macros improve the FD_* performance by about 80%,
36 * and that marking them regparm(2) adds another 20%.
37 */
Willy Tarreau63455a92007-04-09 15:34:49 +020038REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020039{
Willy Tarreau28d86862007-04-08 17:42:27 +020040 return FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020041}
42
Willy Tarreau3788e4c2012-07-30 14:29:35 +020043REGPRM2 static void __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020044{
Willy Tarreau28d86862007-04-08 17:42:27 +020045 FD_SET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020046}
47
Willy Tarreau3788e4c2012-07-30 14:29:35 +020048REGPRM2 static void __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020049{
Willy Tarreau28d86862007-04-08 17:42:27 +020050 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020051}
52
Willy Tarreau97129b52007-04-09 00:54:46 +020053REGPRM1 static void __fd_rem(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020054{
Willy Tarreau28d86862007-04-08 17:42:27 +020055 FD_CLR(fd, fd_evts[DIR_RD]);
56 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020057}
58
Willy Tarreau4f60f162007-04-08 16:39:58 +020059/*
60 * Select() poller
61 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020062REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020063{
64 int status;
65 int fd, i;
66 struct timeval delta;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020067 int delta_ms;
Willy Tarreau4f60f162007-04-08 16:39:58 +020068 int readnotnull, writenotnull;
69 int fds;
70 char count;
71
Willy Tarreau0c303ee2008-07-07 00:09:58 +020072 delta_ms = 0;
73 delta.tv_sec = 0;
74 delta.tv_usec = 0;
75
Willy Tarreau332740d2009-05-10 09:57:21 +020076 if (!run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020077 if (!exp) {
78 delta_ms = MAX_DELAY_MS;
79 delta.tv_sec = (MAX_DELAY_MS / 1000);
80 delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020081 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +020082 else if (!tick_is_expired(exp, now_ms)) {
83 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION;
84 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020085 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +020086 delta.tv_sec = (delta_ms / 1000);
87 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreaud825eef2007-05-12 22:35:00 +020088 }
Willy Tarreau4f60f162007-04-08 16:39:58 +020089 }
90
91 /* let's restore fdset state */
92
93 readnotnull = 0; writenotnull = 0;
94 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +020095 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
96 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020097 }
98
99 // /* just a verification code, needs to be removed for performance */
100 // for (i=0; i<maxfd; i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200101 // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200102 // abort();
Willy Tarreau28d86862007-04-08 17:42:27 +0200103 // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200104 // abort();
105 //
106 // }
107
Willy Tarreau45a12512011-09-10 16:56:42 +0200108 gettimeofday(&before_poll, NULL);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200109 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200110 readnotnull ? tmp_evts[DIR_RD] : NULL,
111 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200112 NULL,
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200113 &delta);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200114
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200115 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200116 measure_idle();
Willy Tarreau4f60f162007-04-08 16:39:58 +0200117
118 if (status <= 0)
119 return;
120
Willy Tarreau177e2b02008-07-15 00:36:31 +0200121 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200122 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200123 continue;
124
Willy Tarreau177e2b02008-07-15 00:36:31 +0200125 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200126 /* if we specify read first, the accepts and zero reads will be
127 * seen first. Moreover, system buffers will be flushed faster.
128 */
Willy Tarreau076be252012-07-06 16:02:29 +0200129 if (!fdtab[fd].owner)
130 continue;
131
Willy Tarreau9845e752012-07-06 11:44:28 +0200132 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau076be252012-07-06 16:02:29 +0200133 if (FD_ISSET(fd, tmp_evts[DIR_RD]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200134 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200135
Willy Tarreau076be252012-07-06 16:02:29 +0200136 if (FD_ISSET(fd, tmp_evts[DIR_WR]))
Willy Tarreau491c4982012-07-06 11:16:01 +0200137 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau9845e752012-07-06 11:44:28 +0200138
139 if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev)
140 fdtab[fd].iocb(fd);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200141 }
142 }
143}
144
145/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200146 * Initialization of the select() poller.
147 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
148 * disables the poller by setting its pref to 0.
149 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200150REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200151{
152 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
153 int fd_set_bytes;
154
155 p->private = NULL;
156 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
157
158 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
159 goto fail_revt;
160
161 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
162 goto fail_wevt;
163
164 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
165 goto fail_srevt;
166
167 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
168 goto fail_swevt;
169
170 return 1;
171
172 fail_swevt:
173 free(fd_evts[DIR_RD]);
174 fail_srevt:
175 free(tmp_evts[DIR_WR]);
176 fail_wevt:
177 free(tmp_evts[DIR_RD]);
178 fail_revt:
179 p->pref = 0;
180 return 0;
181}
182
183/*
184 * Termination of the select() poller.
185 * Memory is released and the poller is marked as unselectable.
186 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200187REGPRM1 static void _do_term(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200188{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200189 free(fd_evts[DIR_WR]);
190 free(fd_evts[DIR_RD]);
191 free(tmp_evts[DIR_WR]);
192 free(tmp_evts[DIR_RD]);
Willy Tarreaue54e9172007-04-09 09:23:31 +0200193 p->private = NULL;
194 p->pref = 0;
195}
196
197/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200198 * Check that the poller works.
199 * Returns 1 if OK, otherwise 0.
200 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200201REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200202{
203 return 1;
204}
205
206/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200207 * It is a constructor, which means that it will automatically be called before
208 * main(). This is GCC-specific but it works at least since 2.95.
209 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200210 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200211__attribute__((constructor))
212static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200213{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200214 struct poller *p;
215
216 if (nbpollers >= MAX_POLLERS)
217 return;
218 p = &pollers[nbpollers++];
219
Willy Tarreau4f60f162007-04-08 16:39:58 +0200220 p->name = "select";
221 p->pref = 150;
222 p->private = NULL;
223
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200224 p->test = _do_test;
225 p->init = _do_init;
226 p->term = _do_term;
227 p->poll = _do_poll;
Willy Tarreau63455a92007-04-09 15:34:49 +0200228 p->is_set = __fd_is_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200229 p->set = __fd_set;
Willy Tarreaubabd05a2012-08-09 12:14:03 +0200230 p->wai = __fd_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200231 p->clr = __fd_clr;
232 p->clo = p->rem = __fd_rem;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200233}
234
235
236/*
237 * Local variables:
238 * c-indent-level: 8
239 * c-basic-offset: 8
240 * End:
241 */