Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for Speculative I/O combined with Linux epoll() |
| 3 | * |
| 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
| 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/standard.h> |
| 20 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 21 | #include <common/tools.h> |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 22 | |
| 23 | #include <types/fd.h> |
| 24 | #include <types/global.h> |
| 25 | |
| 26 | #include <proto/fd.h> |
| 27 | #include <proto/task.h> |
| 28 | |
| 29 | #if defined(USE_MY_EPOLL) |
| 30 | #include <common/epoll.h> |
| 31 | #include <errno.h> |
| 32 | #include <sys/syscall.h> |
| 33 | static _syscall1 (int, epoll_create, int, size); |
| 34 | static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event); |
| 35 | static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout); |
| 36 | #else |
| 37 | #include <sys/epoll.h> |
| 38 | #endif |
| 39 | |
| 40 | /* |
| 41 | * We define 4 states for each direction of a file descriptor, which we store |
| 42 | * as 2 bits : |
| 43 | * |
| 44 | * 00 = IDLE : we're not interested in this event |
| 45 | * 01 = SPEC : perform speculative I/O on this FD |
| 46 | * 10 = WAIT : really wait for an availability event on this FD (poll) |
| 47 | * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if |
| 48 | * the application changes its mind, otherwise disable FD polling |
| 49 | * and switch back to IDLE. |
| 50 | * |
| 51 | * Since we do not want to scan all the FD list to find speculative I/O events, |
| 52 | * we store them in a list consisting in a linear array holding only the FD |
| 53 | * indexes right now. |
| 54 | * |
| 55 | * The STOP state requires the event to be present in the spec list so that |
| 56 | * it can be detected and flushed upon next scan without having to scan the |
| 57 | * whole FD list. |
| 58 | * |
| 59 | * This translates like this : |
| 60 | * |
| 61 | * EVENT_IN_SPEC_LIST = 01 |
| 62 | * EVENT_IN_POLL_LIST = 10 |
| 63 | * |
| 64 | * IDLE = 0 |
| 65 | * SPEC = (EVENT_IN_SPEC_LIST) |
| 66 | * WAIT = (EVENT_IN_POLL_LIST) |
| 67 | * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST) |
| 68 | * |
| 69 | * fd_is_set() just consists in checking that the status is 01 or 10. |
| 70 | * |
| 71 | * For efficiency reasons, we will store the Read and Write bits interlaced to |
| 72 | * form a 4-bit field, so that we can simply shift the value right by 0/1 and |
| 73 | * get what we want : |
| 74 | * 3 2 1 0 |
| 75 | * Wp Rp Ws Rs |
| 76 | * |
| 77 | * The FD array has to hold a back reference to the speculative list. This |
| 78 | * reference is only valid if at least one of the directions is marked SPEC. |
| 79 | * |
| 80 | */ |
| 81 | |
| 82 | #define FD_EV_IN_SL 1 |
| 83 | #define FD_EV_IN_PL 4 |
| 84 | |
| 85 | #define FD_EV_IDLE 0 |
| 86 | #define FD_EV_SPEC (FD_EV_IN_SL) |
| 87 | #define FD_EV_WAIT (FD_EV_IN_PL) |
| 88 | #define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL) |
| 89 | |
| 90 | /* Those match any of R or W for Spec list or Poll list */ |
| 91 | #define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1)) |
| 92 | #define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1)) |
| 93 | #define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL) |
| 94 | |
| 95 | #define FD_EV_IDLE_R 0 |
| 96 | #define FD_EV_SPEC_R (FD_EV_IN_SL) |
| 97 | #define FD_EV_WAIT_R (FD_EV_IN_PL) |
| 98 | #define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL) |
| 99 | #define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL) |
| 100 | |
| 101 | #define FD_EV_IDLE_W (FD_EV_IDLE_R << 1) |
| 102 | #define FD_EV_SPEC_W (FD_EV_SPEC_R << 1) |
| 103 | #define FD_EV_WAIT_W (FD_EV_WAIT_R << 1) |
| 104 | #define FD_EV_STOP_W (FD_EV_STOP_R << 1) |
| 105 | #define FD_EV_MASK_W (FD_EV_MASK_R << 1) |
| 106 | |
| 107 | #define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R) |
| 108 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 109 | /* This is the minimum number of events successfully processed in speculative |
| 110 | * mode above which we agree to return without checking epoll() (1/2 times). |
| 111 | */ |
| 112 | #define MIN_RETURN_EVENTS 25 |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 113 | |
| 114 | /* descriptor of one FD. |
| 115 | * FIXME: should be a bit field */ |
| 116 | struct fd_status { |
| 117 | unsigned int e:4; // read and write events status. |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 118 | unsigned int s1:28; // Position in spec list+1. 0=not in list. Should be last. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static int nbspec = 0; // current size of the spec list |
| 122 | |
| 123 | static struct fd_status *fd_list = NULL; // list of FDs |
| 124 | static unsigned int *spec_list = NULL; // speculative I/O list |
| 125 | |
| 126 | /* private data */ |
| 127 | static struct epoll_event *epoll_events; |
| 128 | static int epoll_fd; |
| 129 | |
| 130 | /* This structure may be used for any purpose. Warning! do not use it in |
| 131 | * recursive functions ! |
| 132 | */ |
| 133 | static struct epoll_event ev; |
| 134 | |
| 135 | |
| 136 | REGPRM1 static void alloc_spec_entry(const int fd) |
| 137 | { |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 138 | if (fd_list[fd].s1) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 139 | return; |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 140 | fd_list[fd].s1 = nbspec + 1; |
| 141 | spec_list[nbspec] = fd; |
| 142 | nbspec++; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 145 | /* Removes entry used by fd <fd> from the spec list and replaces it with the |
| 146 | * last one. The fd_list is adjusted to match the back reference if needed. |
| 147 | * If the fd has no entry assigned, return immediately. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 148 | */ |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 149 | REGPRM1 static void release_spec_entry(int fd) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 150 | { |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 151 | unsigned int pos; |
| 152 | |
| 153 | pos = fd_list[fd].s1; |
| 154 | if (!pos) |
| 155 | return; |
| 156 | |
| 157 | fd_list[fd].s1 = 0; |
| 158 | pos--; |
| 159 | /* we have spec_list[pos]==fd */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 160 | |
| 161 | nbspec--; |
| 162 | if (pos == nbspec) |
| 163 | return; |
| 164 | |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 165 | /* we replace current FD by the highest one, which may sometimes be the same */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 166 | fd = spec_list[nbspec]; |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 167 | fd_list[fd].s1 = pos + 1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 168 | spec_list[pos] = fd; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Returns non-zero if <fd> is already monitored for events in direction <dir>. |
| 173 | */ |
| 174 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
| 175 | { |
| 176 | int ret; |
| 177 | |
| 178 | ret = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR; |
| 179 | return (ret == FD_EV_SPEC || ret == FD_EV_WAIT); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Don't worry about the strange constructs in __fd_set/__fd_clr, they are |
| 184 | * designed like this in order to reduce the number of jumps (verified). |
| 185 | */ |
| 186 | REGPRM2 static int __fd_set(const int fd, int dir) |
| 187 | { |
| 188 | __label__ switch_state; |
| 189 | unsigned int i; |
| 190 | |
| 191 | i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR; |
| 192 | |
| 193 | if (i == FD_EV_IDLE) { |
| 194 | // switch to SPEC state and allocate a SPEC entry. |
| 195 | alloc_spec_entry(fd); |
| 196 | switch_state: |
| 197 | fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir); |
| 198 | return 1; |
| 199 | } |
| 200 | else if (i == FD_EV_STOP) { |
| 201 | // switch to WAIT state |
| 202 | goto switch_state; |
| 203 | } |
| 204 | else |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | REGPRM2 static int __fd_clr(const int fd, int dir) |
| 209 | { |
| 210 | __label__ switch_state; |
| 211 | unsigned int i; |
| 212 | |
| 213 | i = ((unsigned)fd_list[fd].e >> dir) & FD_EV_MASK_DIR; |
| 214 | |
| 215 | if (i == FD_EV_SPEC) { |
| 216 | // switch to IDLE state |
| 217 | goto switch_state; |
| 218 | } |
| 219 | else if (likely(i == FD_EV_WAIT)) { |
| 220 | // switch to STOP state |
| 221 | /* We will create a queue entry for this one because we want to |
| 222 | * process it later in order to merge it with other events on |
| 223 | * the same FD. |
| 224 | */ |
| 225 | alloc_spec_entry(fd); |
| 226 | switch_state: |
| 227 | fd_list[fd].e ^= (unsigned int)(FD_EV_IN_SL << dir); |
| 228 | return 1; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 233 | /* normally unused */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 234 | REGPRM1 static void __fd_rem(int fd) |
| 235 | { |
| 236 | __fd_clr(fd, DIR_RD); |
| 237 | __fd_clr(fd, DIR_WR); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * On valid epoll() implementations, a call to close() automatically removes |
| 242 | * the fds. This means that the FD will appear as previously unset. |
| 243 | */ |
| 244 | REGPRM1 static void __fd_clo(int fd) |
| 245 | { |
| 246 | if (fd_list[fd].e & FD_EV_RW_SL) |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 247 | release_spec_entry(fd); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 248 | fd_list[fd].e &= ~(FD_EV_MASK); |
| 249 | } |
| 250 | |
Willy Tarreau | dc246a7 | 2007-05-09 21:57:51 +0200 | [diff] [blame] | 251 | /* |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 252 | * speculative epoll() poller |
| 253 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 254 | REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 255 | { |
| 256 | static unsigned int last_skipped; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 257 | int status, eo; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 258 | int fd, opcode; |
| 259 | int count; |
| 260 | int spec_idx; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 261 | int wait_time; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 262 | |
| 263 | |
| 264 | /* Here we have two options : |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 265 | * - either walk the list forwards and hope to match more events |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 266 | * - or walk it backwards to minimize the number of changes and |
| 267 | * to make better use of the cache. |
| 268 | * Tests have shown that walking backwards improves perf by 0.2%. |
| 269 | */ |
| 270 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 271 | status = 0; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 272 | spec_idx = nbspec; |
| 273 | while (likely(spec_idx > 0)) { |
| 274 | spec_idx--; |
| 275 | fd = spec_list[spec_idx]; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 276 | eo = fd_list[fd].e; /* save old events */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 277 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 278 | /* |
| 279 | * Process the speculative events. |
| 280 | * |
| 281 | * Principle: events which are marked FD_EV_SPEC are processed |
| 282 | * with their assigned function. If the function returns 0, it |
| 283 | * means there is nothing doable without polling first. We will |
| 284 | * then convert the event to a pollable one by assigning them |
| 285 | * the WAIT status. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 286 | */ |
| 287 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 288 | fdtab[fd].ev = 0; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 289 | if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) { |
| 290 | /* The owner is interested in reading from this FD */ |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 291 | if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 292 | /* Pretend there is something to read */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 293 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 294 | if (!fdtab[fd].cb[DIR_RD].f(fd)) |
| 295 | fd_list[fd].e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R); |
| 296 | else |
| 297 | status++; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 298 | } |
| 299 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 300 | else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) { |
| 301 | /* This FD was being polled and is now being removed. */ |
| 302 | fd_list[fd].e &= ~FD_EV_MASK_R; |
| 303 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 304 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 305 | if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) { |
| 306 | /* The owner is interested in writing to this FD */ |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 307 | if (fdtab[fd].state != FD_STCLOSE && fdtab[fd].state != FD_STERROR) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 308 | /* Pretend there is something to write */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 309 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 310 | if (!fdtab[fd].cb[DIR_WR].f(fd)) |
| 311 | fd_list[fd].e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W); |
| 312 | else |
| 313 | status++; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 316 | else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) { |
| 317 | /* This FD was being polled and is now being removed. */ |
| 318 | fd_list[fd].e &= ~FD_EV_MASK_W; |
| 319 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 320 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 321 | /* Now, we will adjust the event in the poll list. Indeed, it |
| 322 | * is possible that an event which was previously in the poll |
| 323 | * list now goes out, and the opposite is possible too. We can |
| 324 | * have opposite changes for READ and WRITE too. |
| 325 | */ |
| 326 | |
| 327 | if ((eo ^ fd_list[fd].e) & FD_EV_RW_PL) { |
| 328 | /* poll status changed*/ |
| 329 | if ((fd_list[fd].e & FD_EV_RW_PL) == 0) { |
| 330 | /* fd removed from poll list */ |
| 331 | opcode = EPOLL_CTL_DEL; |
| 332 | } |
| 333 | else if ((eo & FD_EV_RW_PL) == 0) { |
| 334 | /* new fd in the poll list */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 335 | opcode = EPOLL_CTL_ADD; |
| 336 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 337 | else { |
| 338 | /* fd status changed */ |
| 339 | opcode = EPOLL_CTL_MOD; |
| 340 | } |
| 341 | |
| 342 | /* construct the epoll events based on new state */ |
| 343 | ev.events = 0; |
| 344 | if (fd_list[fd].e & FD_EV_WAIT_R) |
| 345 | ev.events |= EPOLLIN; |
| 346 | |
| 347 | if (fd_list[fd].e & FD_EV_WAIT_W) |
| 348 | ev.events |= EPOLLOUT; |
| 349 | |
| 350 | ev.data.fd = fd; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 351 | epoll_ctl(epoll_fd, opcode, fd, &ev); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 352 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 353 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 354 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 355 | if (!(fd_list[fd].e & FD_EV_RW_SL)) { |
| 356 | /* This fd switched to combinations of either WAIT or |
| 357 | * IDLE. It must be removed from the spec list. |
| 358 | */ |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 359 | release_spec_entry(fd); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 360 | continue; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 364 | /* It may make sense to immediately return here if there are enough |
| 365 | * processed events, without passing through epoll_wait() because we |
| 366 | * have exactly done a poll. |
| 367 | * Measures have shown a great performance increase if we call the |
| 368 | * epoll_wait() only the second time after speculative accesses have |
| 369 | * succeeded. This reduces the number of unsucessful calls to |
| 370 | * epoll_wait() by a factor of about 3, and the total number of calls |
| 371 | * by about 2. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 372 | */ |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 373 | |
| 374 | if (status >= MIN_RETURN_EVENTS) { |
| 375 | /* We have processed at least MIN_RETURN_EVENTS, it's worth |
| 376 | * returning now without checking epoll_wait(). |
| 377 | */ |
| 378 | if (++last_skipped <= 1) { |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 379 | tv_now(&now); |
| 380 | return; |
| 381 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 382 | } |
| 383 | last_skipped = 0; |
| 384 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 385 | if (nbspec || status) { |
| 386 | /* Maybe we have processed some events that we must report, or |
| 387 | * maybe we still have events in the spec list, so we must not |
| 388 | * wait in epoll() otherwise we will delay their delivery by |
| 389 | * the next timeout. |
| 390 | */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 391 | wait_time = 0; |
| 392 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 393 | else { |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 394 | if (tv_iseternity(exp)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 395 | wait_time = -1; |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 396 | else if (tv_isge(&now, exp)) |
| 397 | wait_time = 0; |
| 398 | else |
| 399 | wait_time = __tv_ms_elapsed(&now, exp) + 1; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 400 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 401 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 402 | /* now let's wait for real events */ |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 403 | fd = MIN(maxfd, global.tune.maxpollevents); |
| 404 | status = epoll_wait(epoll_fd, epoll_events, fd, wait_time); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 405 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 406 | tv_now(&now); |
| 407 | |
| 408 | for (count = 0; count < status; count++) { |
| 409 | int e = epoll_events[count].events; |
| 410 | fd = epoll_events[count].data.fd; |
| 411 | |
| 412 | /* it looks complicated but gcc can optimize it away when constants |
| 413 | * have same values. |
| 414 | */ |
| 415 | fdtab[fd].ev = |
| 416 | ((e & EPOLLIN ) ? FD_POLL_IN : 0) | |
| 417 | ((e & EPOLLPRI) ? FD_POLL_PRI : 0) | |
| 418 | ((e & EPOLLOUT) ? FD_POLL_OUT : 0) | |
| 419 | ((e & EPOLLERR) ? FD_POLL_ERR : 0) | |
| 420 | ((e & EPOLLHUP) ? FD_POLL_HUP : 0); |
| 421 | |
| 422 | if ((fd_list[fd].e & FD_EV_MASK_R) == FD_EV_WAIT_R) { |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 423 | if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 424 | continue; |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 425 | if (fdtab[fd].ev & (FD_POLL_RD|FD_POLL_HUP|FD_POLL_ERR)) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 426 | fdtab[fd].cb[DIR_RD].f(fd); |
| 427 | } |
| 428 | |
| 429 | if ((fd_list[fd].e & FD_EV_MASK_W) == FD_EV_WAIT_W) { |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 430 | if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 431 | continue; |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 432 | if (fdtab[fd].ev & (FD_POLL_WR|FD_POLL_ERR)) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 433 | fdtab[fd].cb[DIR_WR].f(fd); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Initialization of the speculative epoll() poller. |
| 440 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 441 | * disables the poller by setting its pref to 0. |
| 442 | */ |
| 443 | REGPRM1 static int _do_init(struct poller *p) |
| 444 | { |
| 445 | __label__ fail_fd_list, fail_spec, fail_ee, fail_fd; |
| 446 | |
| 447 | p->private = NULL; |
| 448 | |
| 449 | epoll_fd = epoll_create(global.maxsock + 1); |
| 450 | if (epoll_fd < 0) |
| 451 | goto fail_fd; |
| 452 | |
| 453 | epoll_events = (struct epoll_event*) |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 454 | calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 455 | |
| 456 | if (epoll_events == NULL) |
| 457 | goto fail_ee; |
| 458 | |
| 459 | if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL) |
| 460 | goto fail_spec; |
| 461 | |
| 462 | fd_list = (struct fd_status *)calloc(1, sizeof(struct fd_status) * global.maxsock); |
| 463 | if (fd_list == NULL) |
| 464 | goto fail_fd_list; |
| 465 | |
| 466 | return 1; |
| 467 | |
| 468 | fail_fd_list: |
| 469 | free(spec_list); |
| 470 | fail_spec: |
| 471 | free(epoll_events); |
| 472 | fail_ee: |
| 473 | close(epoll_fd); |
| 474 | epoll_fd = 0; |
| 475 | fail_fd: |
| 476 | p->pref = 0; |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Termination of the speculative epoll() poller. |
| 482 | * Memory is released and the poller is marked as unselectable. |
| 483 | */ |
| 484 | REGPRM1 static void _do_term(struct poller *p) |
| 485 | { |
| 486 | if (fd_list) |
| 487 | free(fd_list); |
| 488 | if (spec_list) |
| 489 | free(spec_list); |
| 490 | if (epoll_events) |
| 491 | free(epoll_events); |
| 492 | |
| 493 | close(epoll_fd); |
| 494 | epoll_fd = 0; |
| 495 | |
| 496 | fd_list = NULL; |
| 497 | spec_list = NULL; |
| 498 | epoll_events = NULL; |
| 499 | |
| 500 | p->private = NULL; |
| 501 | p->pref = 0; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Check that the poller works. |
| 506 | * Returns 1 if OK, otherwise 0. |
| 507 | */ |
| 508 | REGPRM1 static int _do_test(struct poller *p) |
| 509 | { |
| 510 | int fd; |
| 511 | |
| 512 | fd = epoll_create(global.maxsock + 1); |
| 513 | if (fd < 0) |
| 514 | return 0; |
| 515 | close(fd); |
| 516 | return 1; |
| 517 | } |
| 518 | |
| 519 | /* |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 520 | * Recreate the epoll file descriptor after a fork(). Returns 1 if OK, |
| 521 | * otherwise 0. It will ensure that all processes will not share their |
| 522 | * epoll_fd. Some side effects were encountered because of this, such |
| 523 | * as epoll_wait() returning an FD which was previously deleted. |
| 524 | */ |
| 525 | REGPRM1 static int _do_fork(struct poller *p) |
| 526 | { |
| 527 | close(epoll_fd); |
| 528 | epoll_fd = epoll_create(global.maxsock + 1); |
| 529 | if (epoll_fd < 0) |
| 530 | return 0; |
| 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | /* |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 535 | * It is a constructor, which means that it will automatically be called before |
| 536 | * main(). This is GCC-specific but it works at least since 2.95. |
| 537 | * Special care must be taken so that it does not need any uninitialized data. |
| 538 | */ |
| 539 | __attribute__((constructor)) |
| 540 | static void _do_register(void) |
| 541 | { |
| 542 | struct poller *p; |
| 543 | |
| 544 | if (nbpollers >= MAX_POLLERS) |
| 545 | return; |
| 546 | p = &pollers[nbpollers++]; |
| 547 | |
| 548 | p->name = "sepoll"; |
| 549 | p->pref = 400; |
| 550 | p->private = NULL; |
| 551 | |
| 552 | p->test = _do_test; |
| 553 | p->init = _do_init; |
| 554 | p->term = _do_term; |
| 555 | p->poll = _do_poll; |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 556 | p->fork = _do_fork; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 557 | |
| 558 | p->is_set = __fd_is_set; |
| 559 | p->cond_s = p->set = __fd_set; |
| 560 | p->cond_c = p->clr = __fd_clr; |
| 561 | p->rem = __fd_rem; |
| 562 | p->clo = __fd_clo; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | /* |
| 567 | * Local variables: |
| 568 | * c-indent-level: 8 |
| 569 | * c-basic-offset: 8 |
| 570 | * End: |
| 571 | */ |