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