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> |
| 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 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 | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame^] | 40 | REGPRM2 static int __fd_isset(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 | |
| 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 | */ |
| 88 | REGPRM1 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 Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 102 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 103 | goto fail_srevt; |
| 104 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 105 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 106 | goto fail_swevt; |
| 107 | |
| 108 | return 1; |
| 109 | |
| 110 | fail_swevt: |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 111 | free(fd_evts[DIR_RD]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 112 | 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 | */ |
| 123 | REGPRM1 static void poll_term(struct poller *p) |
| 124 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 125 | if (fd_evts[DIR_WR]) |
| 126 | free(fd_evts[DIR_WR]); |
| 127 | if (fd_evts[DIR_RD]) |
| 128 | free(fd_evts[DIR_RD]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 129 | if (poll_events) |
| 130 | free(poll_events); |
| 131 | p->private = NULL; |
| 132 | p->pref = 0; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Poll() poller |
| 137 | */ |
| 138 | REGPRM2 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 Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 150 | rn = ((int*)fd_evts[DIR_RD])[fds]; |
| 151 | wn = ((int*)fd_evts[DIR_WR])[fds]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 152 | |
| 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 Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 167 | sr = FD_ISSET(fd, fd_evts[DIR_RD]); |
| 168 | sw = FD_ISSET(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 169 | #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 Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 192 | if (FD_ISSET(fd, fd_evts[DIR_RD])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 193 | 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 Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 199 | if (FD_ISSET(fd, fd_evts[DIR_WR])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 200 | 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 | */ |
| 212 | int 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 | */ |