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