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