blob: c5dbedf25a3cedc3ff1224dc940e1bcd90a1f9df [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic select()
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
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>
19#include <common/time.h>
20
21#include <types/fd.h>
22#include <types/global.h>
23
24#include <proto/fd.h>
Willy Tarreau4f60f162007-04-08 16:39:58 +020025#include <proto/task.h>
26
27
Willy Tarreau28d86862007-04-08 17:42:27 +020028static fd_set *fd_evts[2];
29static fd_set *tmp_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020030
31
32/*
33 * Benchmarks performed on a Pentium-M notebook show that using functions
34 * instead of the usual macros improve the FD_* performance by about 80%,
35 * and that marking them regparm(2) adds another 20%.
36 */
Willy Tarreau63455a92007-04-09 15:34:49 +020037REGPRM2 static int __fd_is_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020038{
Willy Tarreau28d86862007-04-08 17:42:27 +020039 return FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020040}
41
Willy Tarreau97129b52007-04-09 00:54:46 +020042REGPRM2 static int __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020043{
Willy Tarreau28d86862007-04-08 17:42:27 +020044 FD_SET(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020045 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020046}
47
Willy Tarreau97129b52007-04-09 00:54:46 +020048REGPRM2 static int __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 Tarreau97129b52007-04-09 00:54:46 +020051 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020052}
53
Willy Tarreau97129b52007-04-09 00:54:46 +020054REGPRM2 static int __fd_cond_s(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020055{
56 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020057 ret = !FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020058 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020059 FD_SET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020060 return ret;
61}
62
Willy Tarreau97129b52007-04-09 00:54:46 +020063REGPRM2 static int __fd_cond_c(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020064{
65 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020066 ret = FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020067 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020068 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020069 return ret;
70}
71
Willy Tarreau97129b52007-04-09 00:54:46 +020072REGPRM1 static void __fd_rem(int fd)
Willy Tarreau4f60f162007-04-08 16:39:58 +020073{
Willy Tarreau28d86862007-04-08 17:42:27 +020074 FD_CLR(fd, fd_evts[DIR_RD]);
75 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020076}
77
Willy Tarreau4f60f162007-04-08 16:39:58 +020078/*
79 * Select() poller
80 */
81REGPRM2 static void select_poll(struct poller *p, int wait_time)
82{
83 int status;
84 int fd, i;
85 struct timeval delta;
86 int readnotnull, writenotnull;
87 int fds;
88 char count;
89
90 /* allow select to return immediately when needed */
91 delta.tv_sec = delta.tv_usec = 0;
92 if (wait_time > 0) { /* FIXME */
93 /* Convert to timeval */
94 /* to avoid eventual select loops due to timer precision */
95 wait_time += SCHEDULER_RESOLUTION;
96 delta.tv_sec = wait_time / 1000;
97 delta.tv_usec = (wait_time % 1000) * 1000;
98 }
99
100 /* let's restore fdset state */
101
102 readnotnull = 0; writenotnull = 0;
103 for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200104 readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0;
105 writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200106 }
107
108 // /* just a verification code, needs to be removed for performance */
109 // for (i=0; i<maxfd; i++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200110 // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200111 // abort();
Willy Tarreau28d86862007-04-08 17:42:27 +0200112 // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR]))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200113 // abort();
114 //
115 // }
116
117 status = select(maxfd,
Willy Tarreau28d86862007-04-08 17:42:27 +0200118 readnotnull ? tmp_evts[DIR_RD] : NULL,
119 writenotnull ? tmp_evts[DIR_WR] : NULL,
Willy Tarreau4f60f162007-04-08 16:39:58 +0200120 NULL,
121 (wait_time >= 0) ? &delta : NULL);
122
123 tv_now(&now);
124
125 if (status <= 0)
126 return;
127
128 for (fds = 0; (fds << INTBITS) < maxfd; fds++) {
Willy Tarreau28d86862007-04-08 17:42:27 +0200129 if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200130 continue;
131
132 for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) {
133 /* if we specify read first, the accepts and zero reads will be
134 * seen first. Moreover, system buffers will be flushed faster.
135 */
Willy Tarreau28d86862007-04-08 17:42:27 +0200136 if (FD_ISSET(fd, tmp_evts[DIR_RD])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200137 if (fdtab[fd].state == FD_STCLOSE)
138 continue;
139 fdtab[fd].cb[DIR_RD].f(fd);
140 }
141
Willy Tarreau28d86862007-04-08 17:42:27 +0200142 if (FD_ISSET(fd, tmp_evts[DIR_WR])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200143 if (fdtab[fd].state == FD_STCLOSE)
144 continue;
145 fdtab[fd].cb[DIR_WR].f(fd);
146 }
147 }
148 }
149}
150
151/*
Willy Tarreaue54e9172007-04-09 09:23:31 +0200152 * Initialization of the select() poller.
153 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
154 * disables the poller by setting its pref to 0.
155 */
156REGPRM1 static int select_init(struct poller *p)
157{
158 __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt;
159 int fd_set_bytes;
160
161 p->private = NULL;
162 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
163
164 if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
165 goto fail_revt;
166
167 if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
168 goto fail_wevt;
169
170 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
171 goto fail_srevt;
172
173 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
174 goto fail_swevt;
175
176 return 1;
177
178 fail_swevt:
179 free(fd_evts[DIR_RD]);
180 fail_srevt:
181 free(tmp_evts[DIR_WR]);
182 fail_wevt:
183 free(tmp_evts[DIR_RD]);
184 fail_revt:
185 p->pref = 0;
186 return 0;
187}
188
189/*
190 * Termination of the select() poller.
191 * Memory is released and the poller is marked as unselectable.
192 */
193REGPRM1 static void select_term(struct poller *p)
194{
195 if (fd_evts[DIR_WR])
196 free(fd_evts[DIR_WR]);
197 if (fd_evts[DIR_RD])
198 free(fd_evts[DIR_RD]);
199 if (tmp_evts[DIR_WR])
200 free(tmp_evts[DIR_WR]);
201 if (tmp_evts[DIR_RD])
202 free(tmp_evts[DIR_RD]);
203 p->private = NULL;
204 p->pref = 0;
205}
206
207/*
Willy Tarreau4f60f162007-04-08 16:39:58 +0200208 * The only exported function. Returns 1.
209 */
210int select_register(struct poller *p)
211{
212 p->name = "select";
213 p->pref = 150;
214 p->private = NULL;
215
216 p->init = select_init;
217 p->term = select_term;
218 p->poll = select_poll;
Willy Tarreau63455a92007-04-09 15:34:49 +0200219 p->is_set = __fd_is_set;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200220 p->set = __fd_set;
221 p->clr = __fd_clr;
222 p->clo = p->rem = __fd_rem;
223 p->cond_s = __fd_cond_s;
224 p->cond_c = __fd_cond_c;
225 return 1;
226}
227
228
229/*
230 * Local variables:
231 * c-indent-level: 8
232 * c-basic-offset: 8
233 * End:
234 */