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