Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for generic poll() |
| 3 | * |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 4 | * Copyright 2000-2012 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 | |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 36 | REGPRM1 static void __fd_clo(const int fd) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 37 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 38 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 39 | FD_CLR(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Poll() poller |
| 44 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 45 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 46 | { |
| 47 | int status; |
| 48 | int fd, nbfd; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 49 | int wait_time; |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 50 | int updt_idx, en, eo; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 51 | int fds, count; |
| 52 | int sr, sw; |
| 53 | unsigned rn, wn; /* read new, write new */ |
| 54 | |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 55 | /* first, scan the update list to find changes */ |
| 56 | for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) { |
| 57 | fd = fd_updt[updt_idx]; |
| 58 | en = fdtab[fd].spec_e & 15; /* new events */ |
| 59 | eo = fdtab[fd].spec_e >> 4; /* previous events */ |
| 60 | |
| 61 | if (fdtab[fd].owner && (eo ^ en)) { |
| 62 | if ((eo ^ en) & FD_EV_POLLED_RW) { |
| 63 | /* poll status changed, update the lists */ |
| 64 | if ((eo & ~en) & FD_EV_POLLED_R) |
| 65 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 66 | else if ((en & ~eo) & FD_EV_POLLED_R) |
| 67 | FD_SET(fd, fd_evts[DIR_RD]); |
| 68 | |
| 69 | if ((eo & ~en) & FD_EV_POLLED_W) |
| 70 | FD_CLR(fd, fd_evts[DIR_WR]); |
| 71 | else if ((en & ~eo) & FD_EV_POLLED_W) |
| 72 | FD_SET(fd, fd_evts[DIR_WR]); |
| 73 | } |
| 74 | |
| 75 | fdtab[fd].spec_e = (en << 4) + en; /* save new events */ |
| 76 | |
| 77 | if (!(en & FD_EV_ACTIVE_RW)) { |
| 78 | /* This fd doesn't use any active entry anymore, we can |
| 79 | * kill its entry. |
| 80 | */ |
| 81 | release_spec_entry(fd); |
| 82 | } |
| 83 | else if ((en & ~eo) & FD_EV_ACTIVE_RW) { |
| 84 | /* we need a new spec entry now */ |
| 85 | alloc_spec_entry(fd); |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | fdtab[fd].updated = 0; |
| 90 | fdtab[fd].new = 0; |
| 91 | } |
| 92 | fd_nbupdt = 0; |
| 93 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 94 | nbfd = 0; |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 95 | for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 97 | rn = ((int*)fd_evts[DIR_RD])[fds]; |
| 98 | wn = ((int*)fd_evts[DIR_WR])[fds]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 99 | |
| 100 | if ((rn|wn)) { |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 101 | 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] | 102 | #define FDSETS_ARE_INT_ALIGNED |
| 103 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 104 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 105 | #define WE_REALLY_KNOW_THAT_FDSETS_ARE_INTS |
| 106 | #ifdef WE_REALLY_KNOW_THAT_FDSETS_ARE_INTS |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 107 | sr = (rn >> count) & 1; |
| 108 | sw = (wn >> count) & 1; |
| 109 | #else |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 110 | sr = FD_ISSET(fd&(BITS_PER_INT-1), (typeof(fd_set*))&rn); |
| 111 | sw = FD_ISSET(fd&(BITS_PER_INT-1), (typeof(fd_set*))&wn); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 112 | #endif |
| 113 | #else |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 114 | sr = FD_ISSET(fd, fd_evts[DIR_RD]); |
| 115 | sw = FD_ISSET(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 116 | #endif |
| 117 | if ((sr|sw)) { |
| 118 | poll_events[nbfd].fd = fd; |
| 119 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 120 | nbfd++; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* now let's wait for events */ |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 127 | if (fd_nbspec || run_queue || signal_queue_len) |
Willy Tarreau | 3a62811 | 2008-06-13 21:06:56 +0200 | [diff] [blame] | 128 | wait_time = 0; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 129 | else if (!exp) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 130 | wait_time = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 131 | else if (tick_is_expired(exp, now_ms)) |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 132 | wait_time = 0; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 133 | else { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 134 | wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 135 | if (wait_time > MAX_DELAY_MS) |
| 136 | wait_time = MAX_DELAY_MS; |
| 137 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 138 | |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 139 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 140 | status = poll(poll_events, nbfd, wait_time); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 141 | tv_update_date(wait_time, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 142 | measure_idle(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 143 | |
| 144 | for (count = 0; status > 0 && count < nbfd; count++) { |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 145 | int e = poll_events[count].revents; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 146 | fd = poll_events[count].fd; |
| 147 | |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 148 | if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP ))) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 149 | continue; |
| 150 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 151 | /* ok, we found one active fd */ |
| 152 | status--; |
| 153 | |
| 154 | if (!fdtab[fd].owner) |
| 155 | continue; |
| 156 | |
Willy Tarreau | 462c720 | 2012-12-13 22:26:37 +0100 | [diff] [blame] | 157 | /* it looks complicated but gcc can optimize it away when constants |
| 158 | * have same values... In fact it depends on gcc :-( |
| 159 | */ |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 160 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 462c720 | 2012-12-13 22:26:37 +0100 | [diff] [blame] | 161 | if (POLLIN == FD_POLL_IN && POLLOUT == FD_POLL_OUT && |
| 162 | POLLERR == FD_POLL_ERR && POLLHUP == FD_POLL_HUP) { |
| 163 | fdtab[fd].ev |= e & (POLLIN|POLLOUT|POLLERR|POLLHUP); |
| 164 | } |
| 165 | else { |
| 166 | fdtab[fd].ev |= |
| 167 | ((e & POLLIN ) ? FD_POLL_IN : 0) | |
| 168 | ((e & POLLOUT) ? FD_POLL_OUT : 0) | |
| 169 | ((e & POLLERR) ? FD_POLL_ERR : 0) | |
| 170 | ((e & POLLHUP) ? FD_POLL_HUP : 0); |
| 171 | } |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 172 | |
Willy Tarreau | db9cb0b | 2012-12-13 23:41:12 +0100 | [diff] [blame] | 173 | if (fdtab[fd].iocb && fdtab[fd].ev) { |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 174 | /* Mark the events as speculative before processing |
| 175 | * them so that if nothing can be done we don't need |
| 176 | * to poll again. |
| 177 | */ |
Willy Tarreau | 26d7cfc | 2012-12-07 00:09:43 +0100 | [diff] [blame] | 178 | if (fdtab[fd].ev & FD_POLL_IN) |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 179 | fd_ev_set(fd, DIR_RD); |
| 180 | |
Willy Tarreau | 26d7cfc | 2012-12-07 00:09:43 +0100 | [diff] [blame] | 181 | if (fdtab[fd].ev & FD_POLL_OUT) |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 182 | fd_ev_set(fd, DIR_WR); |
| 183 | |
Willy Tarreau | 39ebef8 | 2012-12-14 00:17:03 +0100 | [diff] [blame] | 184 | if (fdtab[fd].spec_p) { |
| 185 | /* This fd was already scheduled for being |
| 186 | * called as a speculative I/O |
| 187 | */ |
| 188 | continue; |
| 189 | } |
| 190 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 191 | fdtab[fd].iocb(fd); |
Willy Tarreau | cc7e3f7 | 2012-11-11 17:25:15 +0100 | [diff] [blame] | 192 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Initialization of the poll() poller. |
| 199 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 200 | * disables the poller by setting its pref to 0. |
| 201 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 202 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 203 | { |
| 204 | __label__ fail_swevt, fail_srevt, fail_pe; |
| 205 | int fd_set_bytes; |
| 206 | |
| 207 | p->private = NULL; |
| 208 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 209 | |
| 210 | poll_events = (struct pollfd*) |
| 211 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 212 | |
| 213 | if (poll_events == NULL) |
| 214 | goto fail_pe; |
| 215 | |
| 216 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 217 | goto fail_srevt; |
| 218 | |
| 219 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 220 | goto fail_swevt; |
| 221 | |
| 222 | return 1; |
| 223 | |
| 224 | fail_swevt: |
| 225 | free(fd_evts[DIR_RD]); |
| 226 | fail_srevt: |
| 227 | free(poll_events); |
| 228 | fail_pe: |
| 229 | p->pref = 0; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Termination of the poll() poller. |
| 235 | * Memory is released and the poller is marked as unselectable. |
| 236 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 237 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 238 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 239 | free(fd_evts[DIR_WR]); |
| 240 | free(fd_evts[DIR_RD]); |
| 241 | free(poll_events); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 242 | p->private = NULL; |
| 243 | p->pref = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 247 | * Check that the poller works. |
| 248 | * Returns 1 if OK, otherwise 0. |
| 249 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 250 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 251 | { |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 256 | * It is a constructor, which means that it will automatically be called before |
| 257 | * main(). This is GCC-specific but it works at least since 2.95. |
| 258 | * 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] | 259 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 260 | __attribute__((constructor)) |
| 261 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 262 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 263 | struct poller *p; |
| 264 | |
| 265 | if (nbpollers >= MAX_POLLERS) |
| 266 | return; |
| 267 | p = &pollers[nbpollers++]; |
| 268 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 269 | p->name = "poll"; |
| 270 | p->pref = 200; |
| 271 | p->private = NULL; |
| 272 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 273 | p->clo = __fd_clo; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 274 | p->test = _do_test; |
| 275 | p->init = _do_init; |
| 276 | p->term = _do_term; |
| 277 | p->poll = _do_poll; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | /* |
| 282 | * Local variables: |
| 283 | * c-indent-level: 8 |
| 284 | * c-basic-offset: 8 |
| 285 | * End: |
| 286 | */ |