blob: 542742a0c10e710f74acfe163eafeb10553c0ff1 [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
Willy Tarreau28d86862007-04-08 17:42:27 +020029static fd_set *fd_evts[2];
Willy Tarreau4f60f162007-04-08 16:39:58 +020030
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 */
Willy Tarreau97129b52007-04-09 00:54:46 +020040REGPRM2 static int __fd_isset(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020041{
Willy Tarreau28d86862007-04-08 17:42:27 +020042 return FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020043}
44
Willy Tarreau97129b52007-04-09 00:54:46 +020045REGPRM2 static int __fd_set(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020046{
Willy Tarreau28d86862007-04-08 17:42:27 +020047 FD_SET(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020048 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020049}
50
Willy Tarreau97129b52007-04-09 00:54:46 +020051REGPRM2 static int __fd_clr(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020052{
Willy Tarreau28d86862007-04-08 17:42:27 +020053 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau97129b52007-04-09 00:54:46 +020054 return 0;
Willy Tarreau4f60f162007-04-08 16:39:58 +020055}
56
Willy Tarreau97129b52007-04-09 00:54:46 +020057REGPRM2 static int __fd_cond_s(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020058{
59 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020060 ret = !FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020061 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020062 FD_SET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020063 return ret;
64}
65
Willy Tarreau97129b52007-04-09 00:54:46 +020066REGPRM2 static int __fd_cond_c(const int fd, int dir)
Willy Tarreau4f60f162007-04-08 16:39:58 +020067{
68 int ret;
Willy Tarreau28d86862007-04-08 17:42:27 +020069 ret = FD_ISSET(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020070 if (ret)
Willy Tarreau28d86862007-04-08 17:42:27 +020071 FD_CLR(fd, fd_evts[dir]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020072 return ret;
73}
74
75REGPRM1 static void __fd_rem(const int fd)
76{
Willy Tarreau28d86862007-04-08 17:42:27 +020077 FD_CLR(fd, fd_evts[DIR_RD]);
78 FD_CLR(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +020079}
80
81
82
83/*
84 * Initialization of the poll() poller.
85 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
86 * disables the poller by setting its pref to 0.
87 */
88REGPRM1 static int poll_init(struct poller *p)
89{
90 __label__ fail_swevt, fail_srevt, fail_pe;
91 int fd_set_bytes;
92
93 p->private = NULL;
94 fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE;
95
96 poll_events = (struct pollfd*)
97 calloc(1, sizeof(struct pollfd) * global.maxsock);
98
99 if (poll_events == NULL)
100 goto fail_pe;
101
Willy Tarreau28d86862007-04-08 17:42:27 +0200102 if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200103 goto fail_srevt;
104
Willy Tarreau28d86862007-04-08 17:42:27 +0200105 if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200106 goto fail_swevt;
107
108 return 1;
109
110 fail_swevt:
Willy Tarreau28d86862007-04-08 17:42:27 +0200111 free(fd_evts[DIR_RD]);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200112 fail_srevt:
113 free(poll_events);
114 fail_pe:
115 p->pref = 0;
116 return 0;
117}
118
119/*
120 * Termination of the poll() poller.
121 * Memory is released and the poller is marked as unselectable.
122 */
123REGPRM1 static void poll_term(struct poller *p)
124{
Willy Tarreau28d86862007-04-08 17:42:27 +0200125 if (fd_evts[DIR_WR])
126 free(fd_evts[DIR_WR]);
127 if (fd_evts[DIR_RD])
128 free(fd_evts[DIR_RD]);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200129 if (poll_events)
130 free(poll_events);
131 p->private = NULL;
132 p->pref = 0;
133}
134
135/*
136 * Poll() poller
137 */
138REGPRM2 static void poll_poll(struct poller *p, int wait_time)
139{
140 int status;
141 int fd, nbfd;
142
143 int fds, count;
144 int sr, sw;
145 unsigned rn, wn; /* read new, write new */
146
147 nbfd = 0;
148 for (fds = 0; (fds << INTBITS) < maxfd; fds++) {
149
Willy Tarreau28d86862007-04-08 17:42:27 +0200150 rn = ((int*)fd_evts[DIR_RD])[fds];
151 wn = ((int*)fd_evts[DIR_WR])[fds];
Willy Tarreau4f60f162007-04-08 16:39:58 +0200152
153 if ((rn|wn)) {
154 for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) {
155#define FDSETS_ARE_INT_ALIGNED
156#ifdef FDSETS_ARE_INT_ALIGNED
157
158#define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS
159#ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS
160 sr = (rn >> count) & 1;
161 sw = (wn >> count) & 1;
162#else
163 sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn);
164 sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn);
165#endif
166#else
Willy Tarreau28d86862007-04-08 17:42:27 +0200167 sr = FD_ISSET(fd, fd_evts[DIR_RD]);
168 sw = FD_ISSET(fd, fd_evts[DIR_WR]);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200169#endif
170 if ((sr|sw)) {
171 poll_events[nbfd].fd = fd;
172 poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0);
173 nbfd++;
174 }
175 }
176 }
177 }
178
179 /* now let's wait for events */
180 status = poll(poll_events, nbfd, wait_time);
181 tv_now(&now);
182
183 for (count = 0; status > 0 && count < nbfd; count++) {
184 fd = poll_events[count].fd;
185
186 if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )))
187 continue;
188
189 /* ok, we found one active fd */
190 status--;
191
Willy Tarreau28d86862007-04-08 17:42:27 +0200192 if (FD_ISSET(fd, fd_evts[DIR_RD])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200193 if (fdtab[fd].state == FD_STCLOSE)
194 continue;
195 if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP ))
196 fdtab[fd].cb[DIR_RD].f(fd);
197 }
198
Willy Tarreau28d86862007-04-08 17:42:27 +0200199 if (FD_ISSET(fd, fd_evts[DIR_WR])) {
Willy Tarreau4f60f162007-04-08 16:39:58 +0200200 if (fdtab[fd].state == FD_STCLOSE)
201 continue;
202 if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP ))
203 fdtab[fd].cb[DIR_WR].f(fd);
204 }
205 }
206
207}
208
209/*
210 * The only exported function. Returns 1.
211 */
212int poll_register(struct poller *p)
213{
214 p->name = "poll";
215 p->pref = 200;
216 p->private = NULL;
217
218 p->init = poll_init;
219 p->term = poll_term;
220 p->poll = poll_poll;
221 p->isset = __fd_isset;
222 p->set = __fd_set;
223 p->clr = __fd_clr;
224 p->clo = p->rem = __fd_rem;
225 p->cond_s = __fd_cond_s;
226 p->cond_c = __fd_cond_c;
227 return 1;
228}
229
230
231/*
232 * Local variables:
233 * c-indent-level: 8
234 * c-basic-offset: 8
235 * End:
236 */