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 | 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 | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 81 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 82 | { |
| 83 | int status; |
| 84 | int fd, i; |
| 85 | struct timeval delta; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 86 | int delta_ms; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 87 | int readnotnull, writenotnull; |
| 88 | int fds; |
| 89 | char count; |
| 90 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 91 | delta_ms = 0; |
| 92 | delta.tv_sec = 0; |
| 93 | delta.tv_usec = 0; |
| 94 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 95 | if (!run_queue) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 96 | if (!exp) { |
| 97 | delta_ms = MAX_DELAY_MS; |
| 98 | delta.tv_sec = (MAX_DELAY_MS / 1000); |
| 99 | delta.tv_usec = (MAX_DELAY_MS % 1000) * 1000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 100 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 101 | else if (!tick_is_expired(exp, now_ms)) { |
| 102 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + SCHEDULER_RESOLUTION; |
| 103 | if (delta_ms > MAX_DELAY_MS) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 104 | delta_ms = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 105 | delta.tv_sec = (delta_ms / 1000); |
| 106 | delta.tv_usec = (delta_ms % 1000) * 1000; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 107 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* let's restore fdset state */ |
| 111 | |
| 112 | readnotnull = 0; writenotnull = 0; |
| 113 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 114 | readnotnull |= (*(((int*)tmp_evts[DIR_RD])+i) = *(((int*)fd_evts[DIR_RD])+i)) != 0; |
| 115 | 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] | 116 | } |
| 117 | |
| 118 | // /* just a verification code, needs to be removed for performance */ |
| 119 | // for (i=0; i<maxfd; i++) { |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 120 | // 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] | 121 | // abort(); |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 122 | // 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] | 123 | // abort(); |
| 124 | // |
| 125 | // } |
| 126 | |
| 127 | status = select(maxfd, |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 128 | readnotnull ? tmp_evts[DIR_RD] : NULL, |
| 129 | writenotnull ? tmp_evts[DIR_WR] : NULL, |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 130 | NULL, |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 131 | &delta); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 132 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 133 | tv_update_date(delta_ms, status); |
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++) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 143 | /* if we specify read first, the accepts and zero reads will be |
| 144 | * seen first. Moreover, system buffers will be flushed faster. |
| 145 | */ |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 146 | if (FD_ISSET(fd, tmp_evts[DIR_RD])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 147 | if (fdtab[fd].state == FD_STCLOSE) |
| 148 | continue; |
| 149 | fdtab[fd].cb[DIR_RD].f(fd); |
| 150 | } |
| 151 | |
Willy Tarreau | 28d8686 | 2007-04-08 17:42:27 +0200 | [diff] [blame] | 152 | if (FD_ISSET(fd, tmp_evts[DIR_WR])) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 153 | if (fdtab[fd].state == FD_STCLOSE) |
| 154 | continue; |
| 155 | fdtab[fd].cb[DIR_WR].f(fd); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 162 | * Initialization of the select() poller. |
| 163 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 164 | * disables the poller by setting its pref to 0. |
| 165 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 166 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 167 | { |
| 168 | __label__ fail_swevt, fail_srevt, fail_wevt, fail_revt; |
| 169 | int fd_set_bytes; |
| 170 | |
| 171 | p->private = NULL; |
| 172 | fd_set_bytes = sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE; |
| 173 | |
| 174 | if ((tmp_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 175 | goto fail_revt; |
| 176 | |
| 177 | if ((tmp_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 178 | goto fail_wevt; |
| 179 | |
| 180 | if ((fd_evts[DIR_RD] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 181 | goto fail_srevt; |
| 182 | |
| 183 | if ((fd_evts[DIR_WR] = (fd_set *)calloc(1, fd_set_bytes)) == NULL) |
| 184 | goto fail_swevt; |
| 185 | |
| 186 | return 1; |
| 187 | |
| 188 | fail_swevt: |
| 189 | free(fd_evts[DIR_RD]); |
| 190 | fail_srevt: |
| 191 | free(tmp_evts[DIR_WR]); |
| 192 | fail_wevt: |
| 193 | free(tmp_evts[DIR_RD]); |
| 194 | fail_revt: |
| 195 | p->pref = 0; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Termination of the select() poller. |
| 201 | * Memory is released and the poller is marked as unselectable. |
| 202 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 203 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 204 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 205 | free(fd_evts[DIR_WR]); |
| 206 | free(fd_evts[DIR_RD]); |
| 207 | free(tmp_evts[DIR_WR]); |
| 208 | free(tmp_evts[DIR_RD]); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 209 | p->private = NULL; |
| 210 | p->pref = 0; |
| 211 | } |
| 212 | |
| 213 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 214 | * Check that the poller works. |
| 215 | * Returns 1 if OK, otherwise 0. |
| 216 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 217 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 218 | { |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 223 | * It is a constructor, which means that it will automatically be called before |
| 224 | * main(). This is GCC-specific but it works at least since 2.95. |
| 225 | * 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] | 226 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 227 | __attribute__((constructor)) |
| 228 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 229 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 230 | struct poller *p; |
| 231 | |
| 232 | if (nbpollers >= MAX_POLLERS) |
| 233 | return; |
| 234 | p = &pollers[nbpollers++]; |
| 235 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 236 | p->name = "select"; |
| 237 | p->pref = 150; |
| 238 | p->private = NULL; |
| 239 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 240 | p->test = _do_test; |
| 241 | p->init = _do_init; |
| 242 | p->term = _do_term; |
| 243 | p->poll = _do_poll; |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 244 | p->is_set = __fd_is_set; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 245 | p->set = __fd_set; |
| 246 | p->clr = __fd_clr; |
| 247 | p->clo = p->rem = __fd_rem; |
| 248 | p->cond_s = __fd_cond_s; |
| 249 | p->cond_c = __fd_cond_c; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | /* |
| 254 | * Local variables: |
| 255 | * c-indent-level: 8 |
| 256 | * c-basic-offset: 8 |
| 257 | * End: |
| 258 | */ |