Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for generic select() |
| 3 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 4 | * Copyright 2000-2014 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> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | #include <common/compat.h> |
| 18 | #include <common/config.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 19 | #include <common/ticks.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 20 | #include <common/time.h> |
| 21 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 22 | #include <types/global.h> |
| 23 | |
| 24 | #include <proto/fd.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 25 | |
| 26 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 27 | static fd_set *fd_evts[2]; |
| 28 | static fd_set *tmp_evts[2]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 4d31fb2 | 2012-11-11 16:53:50 +0100 | [diff] [blame] | 30 | /* Immediately remove the entry upon close() */ |
| 31 | REGPRM1 static void __fd_clo(int fd) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 32 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 33 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 34 | FD_CLR(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Select() poller |
| 39 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 40 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 41 | { |
| 42 | int status; |
| 43 | int fd, i; |
| 44 | struct timeval delta; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 45 | int delta_ms; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 46 | int readnotnull, writenotnull; |
| 47 | int fds; |
Willy Tarreau | 4d31fb2 | 2012-11-11 16:53:50 +0100 | [diff] [blame] | 48 | int updt_idx, en, eo; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 49 | char count; |
| 50 | |
Willy Tarreau | 4d31fb2 | 2012-11-11 16:53:50 +0100 | [diff] [blame] | 51 | /* first, scan the update list to find changes */ |
| 52 | for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) { |
| 53 | fd = fd_updt[updt_idx]; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 54 | fdtab[fd].updated = 0; |
| 55 | fdtab[fd].new = 0; |
Willy Tarreau | 4d31fb2 | 2012-11-11 16:53:50 +0100 | [diff] [blame] | 56 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 57 | if (!fdtab[fd].owner) |
| 58 | continue; |
Willy Tarreau | 4d31fb2 | 2012-11-11 16:53:50 +0100 | [diff] [blame] | 59 | |
Willy Tarreau | 25002d2 | 2014-01-25 10:32:56 +0100 | [diff] [blame] | 60 | eo = fdtab[fd].state; |
| 61 | en = fd_compute_new_polled_status(eo); |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 62 | |
| 63 | if ((eo ^ en) & FD_EV_POLLED_RW) { |
| 64 | /* poll status changed, update the lists */ |
| 65 | fdtab[fd].state = en; |
| 66 | |
| 67 | if ((eo & ~en) & FD_EV_POLLED_R) |
| 68 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 69 | else if ((en & ~eo) & FD_EV_POLLED_R) |
| 70 | FD_SET(fd, fd_evts[DIR_RD]); |
| 71 | |
| 72 | if ((eo & ~en) & FD_EV_POLLED_W) |
| 73 | FD_CLR(fd, fd_evts[DIR_WR]); |
| 74 | else if ((en & ~eo) & FD_EV_POLLED_W) |
| 75 | FD_SET(fd, fd_evts[DIR_WR]); |
| 76 | } |
Willy Tarreau | 4d31fb2 | 2012-11-11 16:53:50 +0100 | [diff] [blame] | 77 | } |
| 78 | fd_nbupdt = 0; |
| 79 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 80 | delta_ms = 0; |
| 81 | delta.tv_sec = 0; |
| 82 | delta.tv_usec = 0; |
| 83 | |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 84 | if (!exp) { |
| 85 | delta_ms = MAX_DELAY_MS; |
| 86 | delta.tv_sec = (MAX_DELAY_MS / 1000); |
| 87 | delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000; |
| 88 | } |
| 89 | else if (!tick_is_expired(exp, now_ms)) { |
| 90 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION; |
| 91 | if (delta_ms > MAX_DELAY_MS) |
| 92 | delta_ms = MAX_DELAY_MS; |
| 93 | delta.tv_sec = (delta_ms / 1000); |
| 94 | delta.tv_usec = (delta_ms % 1000) * 1000; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* let's restore fdset state */ |
| 98 | |
| 99 | readnotnull = 0; writenotnull = 0; |
| 100 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 101 | readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0; |
| 102 | writenotnull |= (*(((int*)tmp_evts[DIR_WR])+i) = *(((int*)fd_evts[DIR_WR])+i)) != 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // /* just a verification code, needs to be removed for performance */ |
| 106 | // for (i=0; i<maxfd; i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 107 | // if (FD_ISSET(i, tmp_evts[DIR_RD]) != FD_ISSET(i, fd_evts[DIR_RD])) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 108 | // abort(); |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 109 | // if (FD_ISSET(i, tmp_evts[DIR_WR]) != FD_ISSET(i, fd_evts[DIR_WR])) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 110 | // abort(); |
| 111 | // |
| 112 | // } |
| 113 | |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 114 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 115 | status = select(maxfd, |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 116 | readnotnull ? tmp_evts[DIR_RD] : NULL, |
| 117 | writenotnull ? tmp_evts[DIR_WR] : NULL, |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 118 | NULL, |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 119 | &delta); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 121 | tv_update_date(delta_ms, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 122 | measure_idle(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 123 | |
| 124 | if (status <= 0) |
| 125 | return; |
| 126 | |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 127 | for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 128 | if ((((int *)(tmp_evts[DIR_RD]))[fds] | ((int *)(tmp_evts[DIR_WR]))[fds]) == 0) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 129 | continue; |
| 130 | |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 131 | for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 132 | /* if we specify read first, the accepts and zero reads will be |
| 133 | * seen first. Moreover, system buffers will be flushed faster. |
| 134 | */ |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 135 | if (!fdtab[fd].owner) |
| 136 | continue; |
| 137 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 138 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 139 | if (FD_ISSET(fd, tmp_evts[DIR_RD])) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 140 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 141 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 142 | if (FD_ISSET(fd, tmp_evts[DIR_WR])) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 143 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 144 | |
Willy Tarreau | 5be2f35 | 2014-11-19 19:43:05 +0100 | [diff] [blame] | 145 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
| 146 | fd_may_recv(fd); |
| 147 | |
| 148 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
| 149 | fd_may_send(fd); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 155 | * Initialization of the select() poller. |
| 156 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 157 | * disables the poller by setting its pref to 0. |
| 158 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 159 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 160 | { |
| 161 | __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt; |
| 162 | int fd_set_bytes; |
| 163 | |
| 164 | p->private = NULL; |
Willy Tarreau | 3fa87b1 | 2013-03-31 14:41:15 +0200 | [diff] [blame] | 165 | |
| 166 | if (global.maxsock > FD_SETSIZE) |
| 167 | goto fail_revt; |
| 168 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 169 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 170 | |
| 171 | if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 172 | goto fail_revt; |
| 173 | |
| 174 | if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 175 | goto fail_wevt; |
| 176 | |
| 177 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 178 | goto fail_srevt; |
| 179 | |
| 180 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 181 | goto fail_swevt; |
| 182 | |
| 183 | return 1; |
| 184 | |
| 185 | fail_swevt: |
| 186 | free(fd_evts[DIR_RD]); |
| 187 | fail_srevt: |
| 188 | free(tmp_evts[DIR_WR]); |
| 189 | fail_wevt: |
| 190 | free(tmp_evts[DIR_RD]); |
| 191 | fail_revt: |
| 192 | p->pref = 0; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Termination of the select() poller. |
| 198 | * Memory is released and the poller is marked as unselectable. |
| 199 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 200 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 201 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 202 | free(fd_evts[DIR_WR]); |
| 203 | free(fd_evts[DIR_RD]); |
| 204 | free(tmp_evts[DIR_WR]); |
| 205 | free(tmp_evts[DIR_RD]); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 206 | p->private = NULL; |
| 207 | p->pref = 0; |
| 208 | } |
| 209 | |
| 210 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 211 | * Check that the poller works. |
| 212 | * Returns 1 if OK, otherwise 0. |
| 213 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 214 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 215 | { |
Willy Tarreau | 3fa87b1 | 2013-03-31 14:41:15 +0200 | [diff] [blame] | 216 | if (global.maxsock > FD_SETSIZE) |
| 217 | return 0; |
| 218 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 223 | * It is a constructor, which means that it will automatically be called before |
| 224 | * main(). This is GCC-specific but it works at least since 2.95. |
| 225 | * 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] | 226 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 227 | __attribute__((constructor)) |
| 228 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 229 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 230 | struct poller *p; |
| 231 | |
| 232 | if (nbpollers >= MAX_POLLERS) |
| 233 | return; |
| 234 | p = &pollers[nbpollers++]; |
| 235 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 236 | p->name = "select"; |
| 237 | p->pref = 150; |
Willy Tarreau | 5a76769 | 2017-03-13 11:38:28 +0100 | [diff] [blame] | 238 | p->flags = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 239 | p->private = NULL; |
| 240 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 241 | p->clo = __fd_clo; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 242 | p->test = _do_test; |
| 243 | p->init = _do_init; |
| 244 | p->term = _do_term; |
| 245 | p->poll = _do_poll; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * Local variables: |
| 251 | * c-indent-level: 8 |
| 252 | * c-basic-offset: 8 |
| 253 | * End: |
| 254 | */ |