Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for generic select() |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 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 | |
| 32 | |
| 33 | /* |
| 34 | * Benchmarks performed on a Pentium-M notebook show that using functions |
| 35 | * instead of the usual macros improve the FD_* performance by about 80%, |
| 36 | * and that marking them regparm(2) adds another 20%. |
| 37 | */ |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 38 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 39 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 40 | return FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 43 | REGPRM2 static void __fd_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 44 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 45 | FD_SET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Willy Tarreau | 3788e4c | 2012-07-30 14:29:35 +0200 | [diff] [blame] | 48 | REGPRM2 static void __fd_clr(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 49 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 50 | FD_CLR(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 53 | REGPRM1 static void __fd_rem(int fd) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 54 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 55 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 56 | FD_CLR(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 59 | /* |
| 60 | * Select() poller |
| 61 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 62 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 63 | { |
| 64 | int status; |
| 65 | int fd, i; |
| 66 | struct timeval delta; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 67 | int delta_ms; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 68 | int readnotnull, writenotnull; |
| 69 | int fds; |
| 70 | char count; |
| 71 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 72 | delta_ms = 0; |
| 73 | delta.tv_sec = 0; |
| 74 | delta.tv_usec = 0; |
| 75 | |
Willy Tarreau | 332740d | 2009-05-10 09:57:21 +0200 | [diff] [blame] | 76 | if (!run_queue && !signal_queue_len) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 77 | if (!exp) { |
| 78 | delta_ms = MAX_DELAY_MS; |
| 79 | delta.tv_sec = (MAX_DELAY_MS / 1000); |
| 80 | delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 81 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 82 | else if (!tick_is_expired(exp, now_ms)) { |
| 83 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION; |
| 84 | if (delta_ms > MAX_DELAY_MS) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 85 | delta_ms = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 86 | delta.tv_sec = (delta_ms / 1000); |
| 87 | delta.tv_usec = (delta_ms % 1000) * 1000; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 88 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* let's restore fdset state */ |
| 92 | |
| 93 | readnotnull = 0; writenotnull = 0; |
| 94 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 95 | readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0; |
| 96 | 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] | 97 | } |
| 98 | |
| 99 | // /* just a verification code, needs to be removed for performance */ |
| 100 | // for (i=0; i<maxfd; i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 101 | // 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] | 102 | // abort(); |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 103 | // 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] | 104 | // abort(); |
| 105 | // |
| 106 | // } |
| 107 | |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 108 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 109 | status = select(maxfd, |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 110 | readnotnull ? tmp_evts[DIR_RD] : NULL, |
| 111 | writenotnull ? tmp_evts[DIR_WR] : NULL, |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 112 | NULL, |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 113 | &delta); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 114 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 115 | tv_update_date(delta_ms, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 116 | measure_idle(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 117 | |
| 118 | if (status <= 0) |
| 119 | return; |
| 120 | |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 121 | for (fds = 0; (fds * BITS_PER_INT) < maxfd; fds++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 122 | 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] | 123 | continue; |
| 124 | |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 125 | 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] | 126 | /* if we specify read first, the accepts and zero reads will be |
| 127 | * seen first. Moreover, system buffers will be flushed faster. |
| 128 | */ |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 129 | if (!fdtab[fd].owner) |
| 130 | continue; |
| 131 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 132 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 133 | if (FD_ISSET(fd, tmp_evts[DIR_RD])) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 134 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 135 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 136 | if (FD_ISSET(fd, tmp_evts[DIR_WR])) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 137 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 138 | |
| 139 | if (fdtab[fd].iocb && fdtab[fd].owner && fdtab[fd].ev) |
| 140 | fdtab[fd].iocb(fd); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 146 | * Initialization of the select() poller. |
| 147 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 148 | * disables the poller by setting its pref to 0. |
| 149 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 150 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 151 | { |
| 152 | __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt; |
| 153 | int fd_set_bytes; |
| 154 | |
| 155 | p->private = NULL; |
| 156 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 157 | |
| 158 | if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 159 | goto fail_revt; |
| 160 | |
| 161 | if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 162 | goto fail_wevt; |
| 163 | |
| 164 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 165 | goto fail_srevt; |
| 166 | |
| 167 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 168 | goto fail_swevt; |
| 169 | |
| 170 | return 1; |
| 171 | |
| 172 | fail_swevt: |
| 173 | free(fd_evts[DIR_RD]); |
| 174 | fail_srevt: |
| 175 | free(tmp_evts[DIR_WR]); |
| 176 | fail_wevt: |
| 177 | free(tmp_evts[DIR_RD]); |
| 178 | fail_revt: |
| 179 | p->pref = 0; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Termination of the select() poller. |
| 185 | * Memory is released and the poller is marked as unselectable. |
| 186 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 187 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 188 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 189 | free(fd_evts[DIR_WR]); |
| 190 | free(fd_evts[DIR_RD]); |
| 191 | free(tmp_evts[DIR_WR]); |
| 192 | free(tmp_evts[DIR_RD]); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 193 | p->private = NULL; |
| 194 | p->pref = 0; |
| 195 | } |
| 196 | |
| 197 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 198 | * Check that the poller works. |
| 199 | * Returns 1 if OK, otherwise 0. |
| 200 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 201 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 202 | { |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 207 | * It is a constructor, which means that it will automatically be called before |
| 208 | * main(). This is GCC-specific but it works at least since 2.95. |
| 209 | * 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] | 210 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 211 | __attribute__((constructor)) |
| 212 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 213 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 214 | struct poller *p; |
| 215 | |
| 216 | if (nbpollers >= MAX_POLLERS) |
| 217 | return; |
| 218 | p = &pollers[nbpollers++]; |
| 219 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 220 | p->name = "select"; |
| 221 | p->pref = 150; |
| 222 | p->private = NULL; |
| 223 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 224 | p->test = _do_test; |
| 225 | p->init = _do_init; |
| 226 | p->term = _do_term; |
| 227 | p->poll = _do_poll; |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 228 | p->is_set = __fd_is_set; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 229 | p->set = __fd_set; |
Willy Tarreau | babd05a | 2012-08-09 12:14:03 +0200 | [diff] [blame] | 230 | p->wai = __fd_set; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 231 | p->clr = __fd_clr; |
| 232 | p->clo = p->rem = __fd_rem; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | * Local variables: |
| 238 | * c-indent-level: 8 |
| 239 | * c-basic-offset: 8 |
| 240 | * End: |
| 241 | */ |