blob: 5a87282d29f5d196353ad1edf9ebeb9fffdbd243 [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 Tarreau97129b52007-04-09 00:54:46 +020043REGPRM2 static int __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 Tarreau97129b52007-04-09 00:54:46 +020046 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020047}
48
Willy Tarreau97129b52007-04-09 00:54:46 +020049REGPRM2 static int __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020050{
Willy Tarreau28d86862007-04-08 17:42:27 +020051 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020052 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020053}
54
Willy Tarreau97129b52007-04-09 00:54:46 +020055REGPRM2 static int __fd_cond_s(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020056{
57 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020058 ret = !FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020059 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020060 FD_SET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020061 return ret;
62}
63
Willy Tarreau97129b52007-04-09 00:54:46 +020064REGPRM2 static int __fd_cond_c(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020065{
66 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020067 ret = FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020068 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020069 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020070 return ret;
71}
72
Willy Tarreau97129b52007-04-09 00:54:46 +020073REGPRM1 static void __fd_rem(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020074{
Willy Tarreau28d86862007-04-08 17:42:27 +020075 FD_CLR(fd, fd_evts[DIR_RD]);
76 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020077}
78
Willy Tarreau4f60f162007-04-08 16:39:58 +020079/*
80 * Select() poller
81 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +020082REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau4f60f162007-04-08 16:39:58 +020083{
84 int status;
85 int fd, i;
86 struct timeval delta;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020087 int delta_ms;
Willy Tarreau4f60f162007-04-08 16:39:58 +020088 int readnotnull, writenotnull;
89 int fds;
90 char count;
91
Willy Tarreau0c303ee2008-07-07 00:09:58 +020092 delta_ms = 0;
93 delta.tv_sec = 0;
94 delta.tv_usec = 0;
95
Willy Tarreau332740d2009-05-10 09:57:21 +020096 if (!run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020097 if (!exp) {
98 delta_ms = MAX_DELAY_MS;
99 delta.tv_sec = (MAX_DELAY_MS / 1000);
100 delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200101 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200102 else if (!tick_is_expired(exp, now_ms)) {
103 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION;
104 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200105 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200106 delta.tv_sec = (delta_ms / 1000);
107 delta.tv_usec = (delta_ms % 1000) * 1000;
Willy Tarreaud825eef2007-05-12 22:35:00 +0200108 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200109 }
110
111 /* let's restore fdset state */
112
113 readnotnull = 0; writenotnull = 0;
114 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200115 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
116 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200117 }
118
119 // /* just a verification code, needs to be removed for performance */
120 // for (i=0; i<maxfd; i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200121 // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200122 // abort();
Willy Tarreau28d86862007-04-08 17:42:27 +0200123 // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200124 // abort();
125 //
126 // }
127
128 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 Tarreau4f60f162007-04-08 16:39:58 +0200135
136 if (status <= 0)
137 return;
138
Willy Tarreau177e2b02008-07-15 00:36:31 +0200139 for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200140 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200141 continue;
142
Willy Tarreau177e2b02008-07-15 00:36:31 +0200143 for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200144 /* if we specify read first, the accepts and zero reads will be
145 * seen first. Moreover, system buffers will be flushed faster.
146 */
Willy Tarreau28d86862007-04-08 17:42:27 +0200147 if (FD_ISSET(fd, tmp_evts[DIR_RD])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200148 if (fdtab[fd].state == FD_STCLOSE)
149 continue;
150 fdtab[fd].cb[DIR_RD].f(fd);
151 }
152
Willy Tarreau28d86862007-04-08 17:42:27 +0200153 if (FD_ISSET(fd, tmp_evts[DIR_WR])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200154 if (fdtab[fd].state == FD_STCLOSE)
155 continue;
156 fdtab[fd].cb[DIR_WR].f(fd);
157 }
158 }
159 }
160}
161
162/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200163 * Initialization of the select() poller.
164 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
165 * disables the poller by setting its pref to 0.
166 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200167REGPRM1 static int _do_init(struct poller *p)
Willy Tarreaue54e9172007-04-09 09:23:31 +0200168{
169 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
170 int fd_set_bytes;
171
172 p->private = NULL;
173 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{
220 return 1;
221}
222
223/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200224 * It is a constructor, which means that it will automatically be called before
225 * main(). This is GCC-specific but it works at least since 2.95.
226 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau4f60f162007-04-08 16:39:58 +0200227 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200228__attribute__((constructor))
229static void _do_register(void)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200230{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200231 struct poller *p;
232
233 if (nbpollers >= MAX_POLLERS)
234 return;
235 p = &pollers[nbpollers++];
236
Willy Tarreau4f60f162007-04-08 16:39:58 +0200237 p->name = "select";
238 p->pref = 150;
239 p->private = NULL;
240
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 Tarreau63455a92007-04-09 15:34:49 +0200245 p->is_set = __fd_is_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200246 p->set = __fd_set;
247 p->clr = __fd_clr;
248 p->clo = p->rem = __fd_rem;
249 p->cond_s = __fd_cond_s;
250 p->cond_c = __fd_cond_c;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200251}
252
253
254/*
255 * Local variables:
256 * c-indent-level: 8
257 * c-basic-offset: 8
258 * End:
259 */