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