Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1 | /* |
| 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> |
Willy Tarreau | 69801b8 | 2007-04-09 15:28:51 +0200 | [diff] [blame] | 14 | #include <sys/poll.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 15 | #include <sys/time.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | #include <common/compat.h> |
| 19 | #include <common/config.h> |
| 20 | #include <common/time.h> |
| 21 | |
| 22 | #include <types/fd.h> |
| 23 | #include <types/global.h> |
| 24 | |
| 25 | #include <proto/fd.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 26 | #include <proto/task.h> |
| 27 | |
| 28 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 29 | static fd_set *fd_evts[2]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 30 | |
| 31 | /* private data */ |
| 32 | static 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 Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 40 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 41 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 42 | return FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 45 | REGPRM2 static int __fd_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 46 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 47 | FD_SET(fd, fd_evts[dir]); |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 48 | return 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 51 | REGPRM2 static int __fd_clr(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 52 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 53 | FD_CLR(fd, fd_evts[dir]); |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 54 | return 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 57 | REGPRM2 static int __fd_cond_s(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 58 | { |
| 59 | int ret; |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 60 | ret = !FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 61 | if (ret) |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 62 | FD_SET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 63 | return ret; |
| 64 | } |
| 65 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 66 | REGPRM2 static int __fd_cond_c(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 67 | { |
| 68 | int ret; |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 69 | ret = FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 70 | if (ret) |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 71 | FD_CLR(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | REGPRM1 static void __fd_rem(const int fd) |
| 76 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 77 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 78 | FD_CLR(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Poll() poller |
| 83 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 84 | REGPRM2 static void _do_poll(struct poller *p, int wait_time) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 85 | { |
| 86 | int status; |
| 87 | int fd, nbfd; |
| 88 | |
| 89 | int fds, count; |
| 90 | int sr, sw; |
| 91 | unsigned rn, wn; /* read new, write new */ |
| 92 | |
| 93 | nbfd = 0; |
| 94 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 95 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 96 | rn = ((int*)fd_evts[DIR_RD])[fds]; |
| 97 | wn = ((int*)fd_evts[DIR_WR])[fds]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 98 | |
| 99 | if ((rn|wn)) { |
| 100 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 101 | #define FDSETS_ARE_INT_ALIGNED |
| 102 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 103 | |
| 104 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 105 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 106 | sr = (rn >> count) & 1; |
| 107 | sw = (wn >> count) & 1; |
| 108 | #else |
| 109 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 110 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
| 111 | #endif |
| 112 | #else |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 113 | sr = FD_ISSET(fd, fd_evts[DIR_RD]); |
| 114 | sw = FD_ISSET(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 115 | #endif |
| 116 | if ((sr|sw)) { |
| 117 | poll_events[nbfd].fd = fd; |
| 118 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 119 | nbfd++; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* now let's wait for events */ |
| 126 | status = poll(poll_events, nbfd, wait_time); |
| 127 | tv_now(&now); |
| 128 | |
| 129 | for (count = 0; status > 0 && count < nbfd; count++) { |
| 130 | fd = poll_events[count].fd; |
| 131 | |
| 132 | if (!(poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP ))) |
| 133 | continue; |
| 134 | |
| 135 | /* ok, we found one active fd */ |
| 136 | status--; |
| 137 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 138 | if (FD_ISSET(fd, fd_evts[DIR_RD])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 139 | if (fdtab[fd].state == FD_STCLOSE) |
| 140 | continue; |
| 141 | if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP )) |
| 142 | fdtab[fd].cb[DIR_RD].f(fd); |
| 143 | } |
| 144 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 145 | if (FD_ISSET(fd, fd_evts[DIR_WR])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 146 | if (fdtab[fd].state == FD_STCLOSE) |
| 147 | continue; |
| 148 | if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP )) |
| 149 | fdtab[fd].cb[DIR_WR].f(fd); |
| 150 | } |
| 151 | } |
| 152 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Initialization of the poll() poller. |
| 157 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 158 | * disables the poller by setting its pref to 0. |
| 159 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 160 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 161 | { |
| 162 | __label__ fail_swevt, fail_srevt, fail_pe; |
| 163 | int fd_set_bytes; |
| 164 | |
| 165 | p->private = NULL; |
| 166 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 167 | |
| 168 | poll_events = (struct pollfd*) |
| 169 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 170 | |
| 171 | if (poll_events == NULL) |
| 172 | goto fail_pe; |
| 173 | |
| 174 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 175 | goto fail_srevt; |
| 176 | |
| 177 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 178 | goto fail_swevt; |
| 179 | |
| 180 | return 1; |
| 181 | |
| 182 | fail_swevt: |
| 183 | free(fd_evts[DIR_RD]); |
| 184 | fail_srevt: |
| 185 | free(poll_events); |
| 186 | fail_pe: |
| 187 | p->pref = 0; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Termination of the poll() poller. |
| 193 | * Memory is released and the poller is marked as unselectable. |
| 194 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 195 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 196 | { |
| 197 | if (fd_evts[DIR_WR]) |
| 198 | free(fd_evts[DIR_WR]); |
| 199 | if (fd_evts[DIR_RD]) |
| 200 | free(fd_evts[DIR_RD]); |
| 201 | if (poll_events) |
| 202 | free(poll_events); |
| 203 | p->private = NULL; |
| 204 | p->pref = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 208 | * Check that the poller works. |
| 209 | * Returns 1 if OK, otherwise 0. |
| 210 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 211 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 212 | { |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 217 | * It is a constructor, which means that it will automatically be called before |
| 218 | * main(). This is GCC-specific but it works at least since 2.95. |
| 219 | * Special care must be taken so that it does not need any uninitialized data. |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 220 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 221 | __attribute__((constructor)) |
| 222 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 223 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 224 | struct poller *p; |
| 225 | |
| 226 | if (nbpollers >= MAX_POLLERS) |
| 227 | return; |
| 228 | p = &pollers[nbpollers++]; |
| 229 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 230 | p->name = "poll"; |
| 231 | p->pref = 200; |
| 232 | p->private = NULL; |
| 233 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 234 | p->test = _do_test; |
| 235 | p->init = _do_init; |
| 236 | p->term = _do_term; |
| 237 | p->poll = _do_poll; |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 238 | p->is_set = __fd_is_set; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 239 | p->set = __fd_set; |
| 240 | p->clr = __fd_clr; |
| 241 | p->clo = p->rem = __fd_rem; |
| 242 | p->cond_s = __fd_cond_s; |
| 243 | p->cond_c = __fd_cond_c; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | /* |
| 248 | * Local variables: |
| 249 | * c-indent-level: 8 |
| 250 | * c-basic-offset: 8 |
| 251 | * End: |
| 252 | */ |