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 | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 144 | if (!fdtab[fd].owner || !(fdtab[fd].thread_mask & tid_bit)) |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 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 | { |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 176 | free(tmp_evts[DIR_WR]); tmp_evts[DIR_WR] = NULL; |
| 177 | free(tmp_evts[DIR_RD]); tmp_evts[DIR_RD] = NULL; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 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; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 196 | |
| 197 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 198 | goto fail_srevt; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 199 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 200 | goto fail_swevt; |
| 201 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 202 | hap_register_per_thread_init(init_select_per_thread); |
| 203 | hap_register_per_thread_deinit(deinit_select_per_thread); |
| 204 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 205 | return 1; |
| 206 | |
| 207 | fail_swevt: |
| 208 | free(fd_evts[DIR_RD]); |
| 209 | fail_srevt: |
| 210 | free(tmp_evts[DIR_WR]); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 211 | free(tmp_evts[DIR_RD]); |
| 212 | fail_revt: |
| 213 | p->pref = 0; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Termination of the select() poller. |
| 219 | * Memory is released and the poller is marked as unselectable. |
| 220 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 221 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 222 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 223 | free(fd_evts[DIR_WR]); |
| 224 | free(fd_evts[DIR_RD]); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 225 | p->private = NULL; |
| 226 | p->pref = 0; |
| 227 | } |
| 228 | |
| 229 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 230 | * Check that the poller works. |
| 231 | * Returns 1 if OK, otherwise 0. |
| 232 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 233 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 234 | { |
Willy Tarreau | 3fa87b1 | 2013-03-31 14:41:15 +0200 | [diff] [blame] | 235 | if (global.maxsock > FD_SETSIZE) |
| 236 | return 0; |
| 237 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 242 | * It is a constructor, which means that it will automatically be called before |
| 243 | * main(). This is GCC-specific but it works at least since 2.95. |
| 244 | * 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] | 245 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 246 | __attribute__((constructor)) |
| 247 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 248 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 249 | struct poller *p; |
| 250 | |
| 251 | if (nbpollers >= MAX_POLLERS) |
| 252 | return; |
| 253 | p = &pollers[nbpollers++]; |
| 254 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 255 | p->name = "select"; |
| 256 | p->pref = 150; |
Willy Tarreau | 5a76769 | 2017-03-13 11:38:28 +0100 | [diff] [blame] | 257 | p->flags = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 258 | p->private = NULL; |
| 259 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 260 | p->clo = __fd_clo; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 261 | p->test = _do_test; |
| 262 | p->init = _do_init; |
| 263 | p->term = _do_term; |
| 264 | p->poll = _do_poll; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
| 268 | /* |
| 269 | * Local variables: |
| 270 | * c-indent-level: 8 |
| 271 | * c-basic-offset: 8 |
| 272 | * End: |
| 273 | */ |