blob: 2d6d98474c4813f076bb570bb10465b23395da88 [file] [log] [blame]
Willy Tarreau4f60f162007-04-08 16:39:58 +02001/*
2 * FD polling functions for generic poll()
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>
25#include <proto/polling.h>
26#include <proto/task.h>
27
28
29static fd_set *StaticReadEvent, *StaticWriteEvent;
30
31/* private data */
32static struct pollfd *poll_events = NULL;
33
34
35/*
36 * Benchmarks performed on a Pentium-M notebook show that using functions
37 * instead of the usual macros improve the FD_* performance by about 80%,
38 * and that marking them regparm(2) adds another 20%.
39 */
40REGPRM2 static int __fd_isset(const int fd, const int dir)
41{
42 fd_set *ev;
43 if (dir == DIR_RD)
44 ev = StaticReadEvent;
45 else
46 ev = StaticWriteEvent;
47
48 return FD_ISSET(fd, ev);
49}
50
51REGPRM2 static void __fd_set(const int fd, const int dir)
52{
53 fd_set *ev;
54 if (dir == DIR_RD)
55 ev = StaticReadEvent;
56 else
57 ev = StaticWriteEvent;
58
59 FD_SET(fd, ev);
60}
61
62REGPRM2 static void __fd_clr(const int fd, const int dir)
63{
64 fd_set *ev;
65 if (dir == DIR_RD)
66 ev = StaticReadEvent;
67 else
68 ev = StaticWriteEvent;
69
70 FD_CLR(fd, ev);
71}
72
73REGPRM2 static int __fd_cond_s(const int fd, const int dir)
74{
75 int ret;
76 fd_set *ev;
77 if (dir == DIR_RD)
78 ev = StaticReadEvent;
79 else
80 ev = StaticWriteEvent;
81
82 ret = !FD_ISSET(fd, ev);
83 if (ret)
84 FD_SET(fd, ev);
85 return ret;
86}
87
88REGPRM2 static int __fd_cond_c(const int fd, const int dir)
89{
90 int ret;
91 fd_set *ev;
92 if (dir == DIR_RD)
93 ev = StaticReadEvent;
94 else
95 ev = StaticWriteEvent;
96
97 ret = FD_ISSET(fd, ev);
98 if (ret)
99 FD_CLR(fd, ev);
100 return ret;
101}
102
103REGPRM1 static void __fd_rem(const int fd)
104{
105 FD_CLR(fd, StaticReadEvent);
106 FD_CLR(fd, StaticWriteEvent);
107}
108
109
110
111/*
112 * Initialization of the poll() poller.
113 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
114 * disables the poller by setting its pref to 0.
115 */
116REGPRM1 static int poll_init(struct poller *p)
117{
118 __label__ fail_swevt, fail_srevt, fail_pe;
119 int fd_set_bytes;
120
121 p->private = NULL;
122 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
123
124 poll_events = (struct pollfd*)
125 calloc(1, sizeof(struct pollfd) * global.maxsock);
126
127 if (poll_events == NULL)
128 goto fail_pe;
129
130 if ((StaticReadEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
131 goto fail_srevt;
132
133 if ((StaticWriteEvent = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
134 goto fail_swevt;
135
136 return 1;
137
138 fail_swevt:
139 free(StaticReadEvent);
140 fail_srevt:
141 free(poll_events);
142 fail_pe:
143 p->pref = 0;
144 return 0;
145}
146
147/*
148 * Termination of the poll() poller.
149 * Memory is released and the poller is marked as unselectable.
150 */
151REGPRM1 static void poll_term(struct poller *p)
152{
153 if (StaticWriteEvent)
154 free(StaticWriteEvent);
155 if (StaticReadEvent)
156 free(StaticReadEvent);
157 if (poll_events)
158 free(poll_events);
159 p->private = NULL;
160 p->pref = 0;
161}
162
163/*
164 * Poll() poller
165 */
166REGPRM2 static void poll_poll(struct poller *p, int wait_time)
167{
168 int status;
169 int fd, nbfd;
170
171 int fds, count;
172 int sr, sw;
173 unsigned rn, wn; /* read new, write new */
174
175 nbfd = 0;
176 for (fds = 0; (fds << INTBITS) < maxfd; fds++) {
177
178 rn = ((int*)StaticReadEvent)[fds];
179 wn = ((int*)StaticWriteEvent)[fds];
180
181 if ((rn|wn)) {
182 for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) {
183#define FDSETS_ARE_INT_ALIGNED
184#ifdef FDSETS_ARE_INT_ALIGNED
185
186#define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS
187#ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS
188 sr = (rn >> count) & 1;
189 sw = (wn >> count) & 1;
190#else
191 sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn);
192 sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn);
193#endif
194#else
195 sr = FD_ISSET(fd, StaticReadEvent);
196 sw = FD_ISSET(fd, StaticWriteEvent);
197#endif
198 if ((sr|sw)) {
199 poll_events[nbfd].fd = fd;
200 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
201 nbfd++;
202 }
203 }
204 }
205 }
206
207 /* now let's wait for events */
208 status = poll(poll_events, nbfd, wait_time);
209 tv_now(&now);
210
211 for (count = 0; status > 0 && count < nbfd; count++) {
212 fd = poll_events[count].fd;
213
214 if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
215 continue;
216
217 /* ok, we found one active fd */
218 status--;
219
220 if (FD_ISSET(fd, StaticReadEvent)) {
221 if (fdtab[fd].state == FD_STCLOSE)
222 continue;
223 if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP ))
224 fdtab[fd].cb[DIR_RD].f(fd);
225 }
226
227 if (FD_ISSET(fd, StaticWriteEvent)) {
228 if (fdtab[fd].state == FD_STCLOSE)
229 continue;
230 if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP ))
231 fdtab[fd].cb[DIR_WR].f(fd);
232 }
233 }
234
235}
236
237/*
238 * The only exported function. Returns 1.
239 */
240int poll_register(struct poller *p)
241{
242 p->name = "poll";
243 p->pref = 200;
244 p->private = NULL;
245
246 p->init = poll_init;
247 p->term = poll_term;
248 p->poll = poll_poll;
249 p->isset = __fd_isset;
250 p->set = __fd_set;
251 p->clr = __fd_clr;
252 p->clo = p->rem = __fd_rem;
253 p->cond_s = __fd_cond_s;
254 p->cond_c = __fd_cond_c;
255 return 1;
256}
257
258
259/*
260 * Local variables:
261 * c-indent-level: 8
262 * c-basic-offset: 8
263 * End:
264 */