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> |
| 19 | #include <common/time.h> |
| 20 | |
| 21 | #include <types/fd.h> |
| 22 | #include <types/global.h> |
| 23 | |
| 24 | #include <proto/fd.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 25 | #include <proto/task.h> |
| 26 | |
| 27 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 28 | static fd_set *fd_evts[2]; |
| 29 | static fd_set *tmp_evts[2]; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /* |
| 33 | * Benchmarks performed on a Pentium-M notebook show that using functions |
| 34 | * instead of the usual macros improve the FD_* performance by about 80%, |
| 35 | * and that marking them regparm(2) adds another 20%. |
| 36 | */ |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 37 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 38 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 39 | return FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 42 | REGPRM2 static int __fd_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 43 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 44 | FD_SET(fd, fd_evts[dir]); |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 45 | return 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 48 | REGPRM2 static int __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 | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 51 | return 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 54 | REGPRM2 static int __fd_cond_s(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 55 | { |
| 56 | int ret; |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 57 | ret = !FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 58 | if (ret) |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 59 | FD_SET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 60 | return ret; |
| 61 | } |
| 62 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 63 | REGPRM2 static int __fd_cond_c(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 64 | { |
| 65 | int ret; |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 66 | ret = FD_ISSET(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 67 | if (ret) |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 68 | FD_CLR(fd, fd_evts[dir]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 72 | REGPRM1 static void __fd_rem(int fd) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 73 | { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 74 | FD_CLR(fd, fd_evts[DIR_RD]); |
| 75 | FD_CLR(fd, fd_evts[DIR_WR]); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Select() poller |
| 80 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 81 | REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 82 | { |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 83 | const struct timeval max_delay = { |
| 84 | .tv_sec = MAX_DELAY_MS / 1000, |
| 85 | .tv_usec = (MAX_DELAY_MS % 1000) * 1000 |
| 86 | }; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 87 | int status; |
| 88 | int fd, i; |
| 89 | struct timeval delta; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 90 | int delta_ms; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 91 | int readnotnull, writenotnull; |
| 92 | int fds; |
| 93 | char count; |
| 94 | |
| 95 | /* allow select to return immediately when needed */ |
| 96 | delta.tv_sec = delta.tv_usec = 0; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 97 | delta_ms = 0; |
| 98 | if (!run_queue) { |
| 99 | if (!tv_isset(exp)) { |
| 100 | delta = max_delay; |
| 101 | delta_ms = MAX_DELAY_MS; |
| 102 | } |
| 103 | else if (tv_islt(&now, exp)) { |
Willy Tarreau | d9b7441 | 2007-05-14 03:16:06 +0200 | [diff] [blame] | 104 | tv_remain(&now, exp, &delta); |
| 105 | /* To avoid eventual select loops due to timer precision */ |
| 106 | delta.tv_usec += SCHEDULER_RESOLUTION * 1000; |
| 107 | if (delta.tv_usec >= 1000000) { |
| 108 | delta.tv_usec -= 1000000; |
| 109 | delta.tv_sec ++; |
| 110 | } |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 111 | if (__tv_isge(&delta, &max_delay)) { |
| 112 | delta = max_delay; |
| 113 | delta_ms = MAX_DELAY_MS; |
| 114 | } else { |
| 115 | delta_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000; |
| 116 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 117 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* let's restore fdset state */ |
| 121 | |
| 122 | readnotnull = 0; writenotnull = 0; |
| 123 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 124 | readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0; |
| 125 | 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] | 126 | } |
| 127 | |
| 128 | // /* just a verification code, needs to be removed for performance */ |
| 129 | // for (i=0; i<maxfd; i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 130 | // 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] | 131 | // abort(); |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 132 | // 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] | 133 | // abort(); |
| 134 | // |
| 135 | // } |
| 136 | |
| 137 | status = select(maxfd, |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 138 | readnotnull ? tmp_evts[DIR_RD] : NULL, |
| 139 | writenotnull ? tmp_evts[DIR_WR] : NULL, |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 140 | NULL, |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 141 | &delta); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 142 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 143 | tv_update_date(delta_ms, status); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 144 | |
| 145 | if (status <= 0) |
| 146 | return; |
| 147 | |
| 148 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 149 | 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] | 150 | continue; |
| 151 | |
| 152 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 153 | /* if we specify read first, the accepts and zero reads will be |
| 154 | * seen first. Moreover, system buffers will be flushed faster. |
| 155 | */ |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 156 | if (FD_ISSET(fd, tmp_evts[DIR_RD])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 157 | if (fdtab[fd].state == FD_STCLOSE) |
| 158 | continue; |
| 159 | fdtab[fd].cb[DIR_RD].f(fd); |
| 160 | } |
| 161 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 162 | if (FD_ISSET(fd, tmp_evts[DIR_WR])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 163 | if (fdtab[fd].state == FD_STCLOSE) |
| 164 | continue; |
| 165 | fdtab[fd].cb[DIR_WR].f(fd); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 172 | * Initialization of the select() poller. |
| 173 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 174 | * disables the poller by setting its pref to 0. |
| 175 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 176 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 177 | { |
| 178 | __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt; |
| 179 | int fd_set_bytes; |
| 180 | |
| 181 | p->private = NULL; |
| 182 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 183 | |
| 184 | if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 185 | goto fail_revt; |
| 186 | |
| 187 | if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 188 | goto fail_wevt; |
| 189 | |
| 190 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 191 | goto fail_srevt; |
| 192 | |
| 193 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 194 | goto fail_swevt; |
| 195 | |
| 196 | return 1; |
| 197 | |
| 198 | fail_swevt: |
| 199 | free(fd_evts[DIR_RD]); |
| 200 | fail_srevt: |
| 201 | free(tmp_evts[DIR_WR]); |
| 202 | fail_wevt: |
| 203 | free(tmp_evts[DIR_RD]); |
| 204 | fail_revt: |
| 205 | p->pref = 0; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Termination of the select() poller. |
| 211 | * Memory is released and the poller is marked as unselectable. |
| 212 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 213 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 214 | { |
| 215 | if (fd_evts[DIR_WR]) |
| 216 | free(fd_evts[DIR_WR]); |
| 217 | if (fd_evts[DIR_RD]) |
| 218 | free(fd_evts[DIR_RD]); |
| 219 | if (tmp_evts[DIR_WR]) |
| 220 | free(tmp_evts[DIR_WR]); |
| 221 | if (tmp_evts[DIR_RD]) |
| 222 | free(tmp_evts[DIR_RD]); |
| 223 | p->private = NULL; |
| 224 | p->pref = 0; |
| 225 | } |
| 226 | |
| 227 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 228 | * Check that the poller works. |
| 229 | * Returns 1 if OK, otherwise 0. |
| 230 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 231 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 232 | { |
| 233 | return 1; |
| 234 | } |
| 235 | |
| 236 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 237 | * It is a constructor, which means that it will automatically be called before |
| 238 | * main(). This is GCC-specific but it works at least since 2.95. |
| 239 | * 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] | 240 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 241 | __attribute__((constructor)) |
| 242 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 243 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 244 | struct poller *p; |
| 245 | |
| 246 | if (nbpollers >= MAX_POLLERS) |
| 247 | return; |
| 248 | p = &pollers[nbpollers++]; |
| 249 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 250 | p->name = "select"; |
| 251 | p->pref = 150; |
| 252 | p->private = NULL; |
| 253 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 254 | p->test = _do_test; |
| 255 | p->init = _do_init; |
| 256 | p->term = _do_term; |
| 257 | p->poll = _do_poll; |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 258 | p->is_set = __fd_is_set; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 259 | p->set = __fd_set; |
| 260 | p->clr = __fd_clr; |
| 261 | p->clo = p->rem = __fd_rem; |
| 262 | p->cond_s = __fd_cond_s; |
| 263 | p->cond_c = __fd_cond_c; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * Local variables: |
| 269 | * c-indent-level: 8 |
| 270 | * c-basic-offset: 8 |
| 271 | * End: |
| 272 | */ |