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 | * |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +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 | * |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 11 | * |
| 12 | * This code implements "speculative I/O" under Linux. The principle is to |
| 13 | * try to perform expected I/O before registering the events in the poller. |
| 14 | * Each time this succeeds, it saves an expensive epoll_ctl(). It generally |
| 15 | * succeeds for all reads after an accept(), and for writes after a connect(). |
| 16 | * It also improves performance for streaming connections because even if only |
| 17 | * one side is polled, the other one may react accordingly depending on the |
| 18 | * level of the buffer. |
| 19 | * |
| 20 | * It has a presents drawbacks though. If too many events are set for spec I/O, |
| 21 | * those ones can starve the polled events. Experiments show that when polled |
| 22 | * events starve, they quickly turn into spec I/O, making the situation even |
| 23 | * worse. While we can reduce the number of polled events processed at once, |
| 24 | * we cannot do this on speculative events because most of them are new ones |
| 25 | * (avg 2/3 new - 1/3 old from experiments). |
| 26 | * |
| 27 | * The solution against this problem relies on those two factors : |
| 28 | * 1) one FD registered as a spec event cannot be polled at the same time |
| 29 | * 2) even during very high loads, we will almost never be interested in |
| 30 | * simultaneous read and write streaming on the same FD. |
| 31 | * |
| 32 | * The first point implies that during starvation, we will not have more than |
| 33 | * half of our FDs in the poll list, otherwise it means there is less than that |
| 34 | * in the spec list, implying there is no starvation. |
| 35 | * |
| 36 | * The second point implies that we're statically only interested in half of |
| 37 | * the maximum number of file descriptors at once, because we will unlikely |
| 38 | * have simultaneous read and writes for a same buffer during long periods. |
| 39 | * |
| 40 | * So, if we make it possible to drain maxsock/2/2 during peak loads, then we |
| 41 | * can ensure that there will be no starvation effect. This means that we must |
| 42 | * always allocate maxsock/4 events for the poller. |
| 43 | * |
| 44 | * |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | #include <unistd.h> |
| 48 | #include <sys/time.h> |
| 49 | #include <sys/types.h> |
| 50 | |
| 51 | #include <common/compat.h> |
| 52 | #include <common/config.h> |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 53 | #include <common/debug.h> |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 54 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 55 | #include <common/ticks.h> |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 56 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 57 | #include <common/tools.h> |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 59 | #include <types/global.h> |
| 60 | |
| 61 | #include <proto/fd.h> |
Willy Tarreau | 332740d | 2009-05-10 09:57:21 +0200 | [diff] [blame] | 62 | #include <proto/signal.h> |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 63 | #include <proto/task.h> |
| 64 | |
| 65 | #if defined(USE_MY_EPOLL) |
| 66 | #include <common/epoll.h> |
| 67 | #include <errno.h> |
| 68 | #include <sys/syscall.h> |
| 69 | static _syscall1 (int, epoll_create, int, size); |
| 70 | static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event); |
| 71 | static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout); |
| 72 | #else |
| 73 | #include <sys/epoll.h> |
| 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | * We define 4 states for each direction of a file descriptor, which we store |
| 78 | * as 2 bits : |
| 79 | * |
| 80 | * 00 = IDLE : we're not interested in this event |
| 81 | * 01 = SPEC : perform speculative I/O on this FD |
| 82 | * 10 = WAIT : really wait for an availability event on this FD (poll) |
| 83 | * 11 = STOP : was marked WAIT, but disabled. It can switch back to WAIT if |
| 84 | * the application changes its mind, otherwise disable FD polling |
| 85 | * and switch back to IDLE. |
| 86 | * |
| 87 | * Since we do not want to scan all the FD list to find speculative I/O events, |
| 88 | * we store them in a list consisting in a linear array holding only the FD |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 89 | * indexes right now. Note that a closed FD cannot exist in the spec list, |
| 90 | * because it is closed by fd_delete() which in turn calls __fd_clo() which |
| 91 | * always removes it from the list. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 92 | * |
| 93 | * The STOP state requires the event to be present in the spec list so that |
| 94 | * it can be detected and flushed upon next scan without having to scan the |
| 95 | * whole FD list. |
| 96 | * |
| 97 | * This translates like this : |
| 98 | * |
| 99 | * EVENT_IN_SPEC_LIST = 01 |
| 100 | * EVENT_IN_POLL_LIST = 10 |
| 101 | * |
| 102 | * IDLE = 0 |
| 103 | * SPEC = (EVENT_IN_SPEC_LIST) |
| 104 | * WAIT = (EVENT_IN_POLL_LIST) |
| 105 | * STOP = (EVENT_IN_SPEC_LIST|EVENT_IN_POLL_LIST) |
| 106 | * |
| 107 | * fd_is_set() just consists in checking that the status is 01 or 10. |
| 108 | * |
| 109 | * For efficiency reasons, we will store the Read and Write bits interlaced to |
| 110 | * form a 4-bit field, so that we can simply shift the value right by 0/1 and |
| 111 | * get what we want : |
| 112 | * 3 2 1 0 |
| 113 | * Wp Rp Ws Rs |
| 114 | * |
| 115 | * The FD array has to hold a back reference to the speculative list. This |
| 116 | * reference is only valid if at least one of the directions is marked SPEC. |
| 117 | * |
| 118 | */ |
| 119 | |
| 120 | #define FD_EV_IN_SL 1 |
| 121 | #define FD_EV_IN_PL 4 |
| 122 | |
| 123 | #define FD_EV_IDLE 0 |
| 124 | #define FD_EV_SPEC (FD_EV_IN_SL) |
| 125 | #define FD_EV_WAIT (FD_EV_IN_PL) |
| 126 | #define FD_EV_STOP (FD_EV_IN_SL|FD_EV_IN_PL) |
| 127 | |
| 128 | /* Those match any of R or W for Spec list or Poll list */ |
| 129 | #define FD_EV_RW_SL (FD_EV_IN_SL | (FD_EV_IN_SL << 1)) |
| 130 | #define FD_EV_RW_PL (FD_EV_IN_PL | (FD_EV_IN_PL << 1)) |
| 131 | #define FD_EV_MASK_DIR (FD_EV_IN_SL|FD_EV_IN_PL) |
| 132 | |
| 133 | #define FD_EV_IDLE_R 0 |
| 134 | #define FD_EV_SPEC_R (FD_EV_IN_SL) |
| 135 | #define FD_EV_WAIT_R (FD_EV_IN_PL) |
| 136 | #define FD_EV_STOP_R (FD_EV_IN_SL|FD_EV_IN_PL) |
| 137 | #define FD_EV_MASK_R (FD_EV_IN_SL|FD_EV_IN_PL) |
| 138 | |
| 139 | #define FD_EV_IDLE_W (FD_EV_IDLE_R << 1) |
| 140 | #define FD_EV_SPEC_W (FD_EV_SPEC_R << 1) |
| 141 | #define FD_EV_WAIT_W (FD_EV_WAIT_R << 1) |
| 142 | #define FD_EV_STOP_W (FD_EV_STOP_R << 1) |
| 143 | #define FD_EV_MASK_W (FD_EV_MASK_R << 1) |
| 144 | |
| 145 | #define FD_EV_MASK (FD_EV_MASK_W | FD_EV_MASK_R) |
| 146 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 147 | /* This is the minimum number of events successfully processed in speculative |
| 148 | * mode above which we agree to return without checking epoll() (1/2 times). |
| 149 | */ |
| 150 | #define MIN_RETURN_EVENTS 25 |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 151 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 152 | static int nbspec = 0; // current size of the spec list |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 153 | static int absmaxevents = 0; // absolute maximum amounts of polled events |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 154 | static int fd_created = 0; // fd creation detector, reset upon poll() entry. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 155 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 156 | static unsigned int *spec_list = NULL; // speculative I/O list |
| 157 | |
| 158 | /* private data */ |
| 159 | static struct epoll_event *epoll_events; |
| 160 | static int epoll_fd; |
| 161 | |
| 162 | /* This structure may be used for any purpose. Warning! do not use it in |
| 163 | * recursive functions ! |
| 164 | */ |
| 165 | static struct epoll_event ev; |
| 166 | |
| 167 | |
Willy Tarreau | ff9d5ba | 2009-10-17 21:43:03 +0200 | [diff] [blame] | 168 | REGPRM1 static inline void alloc_spec_entry(const int fd) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 169 | { |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 170 | if (fdtab[fd].spec.s1) |
Willy Tarreau | ff9d5ba | 2009-10-17 21:43:03 +0200 | [diff] [blame] | 171 | /* sometimes the entry already exists for the other direction */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 172 | return; |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 173 | fdtab[fd].spec.s1 = nbspec + 1; |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 174 | spec_list[nbspec] = fd; |
| 175 | nbspec++; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 178 | /* Removes entry used by fd <fd> from the spec list and replaces it with the |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 179 | * last one. The fdtab.spec is adjusted to match the back reference if needed. |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 180 | * If the fd has no entry assigned, return immediately. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 181 | */ |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 182 | REGPRM1 static void release_spec_entry(int fd) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 183 | { |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 184 | unsigned int pos; |
| 185 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 186 | pos = fdtab[fd].spec.s1; |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 187 | if (!pos) |
| 188 | return; |
| 189 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 190 | fdtab[fd].spec.s1 = 0; |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 191 | pos--; |
| 192 | /* we have spec_list[pos]==fd */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 193 | |
| 194 | nbspec--; |
| 195 | if (pos == nbspec) |
| 196 | return; |
| 197 | |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 198 | /* 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] | 199 | fd = spec_list[nbspec]; |
| 200 | spec_list[pos] = fd; |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 201 | fdtab[fd].spec.s1 = pos + 1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Returns non-zero if <fd> is already monitored for events in direction <dir>. |
| 206 | */ |
| 207 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
| 208 | { |
| 209 | int ret; |
| 210 | |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 211 | #if DEBUG_DEV |
| 212 | if (fdtab[fd].state == FD_STCLOSE) { |
| 213 | fprintf(stderr, "sepoll.fd_isset called on closed fd #%d.\n", fd); |
| 214 | ABORT_NOW(); |
| 215 | } |
| 216 | #endif |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 217 | ret = ((unsigned)fdtab[fd].spec.e >> dir) & FD_EV_MASK_DIR; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 218 | return (ret == FD_EV_SPEC || ret == FD_EV_WAIT); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Don't worry about the strange constructs in __fd_set/__fd_clr, they are |
| 223 | * designed like this in order to reduce the number of jumps (verified). |
| 224 | */ |
| 225 | REGPRM2 static int __fd_set(const int fd, int dir) |
| 226 | { |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 227 | unsigned int i; |
| 228 | |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 229 | #if DEBUG_DEV |
| 230 | if (fdtab[fd].state == FD_STCLOSE) { |
| 231 | fprintf(stderr, "sepoll.fd_set called on closed fd #%d.\n", fd); |
| 232 | ABORT_NOW(); |
| 233 | } |
| 234 | #endif |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 235 | i = ((unsigned)fdtab[fd].spec.e >> dir) & FD_EV_MASK_DIR; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | ff9d5ba | 2009-10-17 21:43:03 +0200 | [diff] [blame] | 237 | if (i != FD_EV_STOP) { |
| 238 | if (unlikely(i != FD_EV_IDLE)) |
| 239 | return 0; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 240 | // switch to SPEC state and allocate a SPEC entry. |
Willy Tarreau | 573fd80 | 2009-03-22 19:25:46 +0100 | [diff] [blame] | 241 | fd_created++; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 242 | alloc_spec_entry(fd); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 243 | } |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 244 | fdtab[fd].spec.e ^= (unsigned int)(FD_EV_IN_SL << dir); |
Willy Tarreau | ff9d5ba | 2009-10-17 21:43:03 +0200 | [diff] [blame] | 245 | return 1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | REGPRM2 static int __fd_clr(const int fd, int dir) |
| 249 | { |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 250 | unsigned int i; |
| 251 | |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 252 | #if DEBUG_DEV |
| 253 | if (fdtab[fd].state == FD_STCLOSE) { |
| 254 | fprintf(stderr, "sepoll.fd_clr called on closed fd #%d.\n", fd); |
| 255 | ABORT_NOW(); |
| 256 | } |
| 257 | #endif |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 258 | i = ((unsigned)fdtab[fd].spec.e >> dir) & FD_EV_MASK_DIR; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 259 | |
Willy Tarreau | ff9d5ba | 2009-10-17 21:43:03 +0200 | [diff] [blame] | 260 | if (i != FD_EV_SPEC) { |
| 261 | if (unlikely(i != FD_EV_WAIT)) |
| 262 | return 0; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 263 | // switch to STOP state |
| 264 | /* We will create a queue entry for this one because we want to |
| 265 | * process it later in order to merge it with other events on |
| 266 | * the same FD. |
| 267 | */ |
| 268 | alloc_spec_entry(fd); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 269 | } |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 270 | fdtab[fd].spec.e ^= (unsigned int)(FD_EV_IN_SL << dir); |
Willy Tarreau | ff9d5ba | 2009-10-17 21:43:03 +0200 | [diff] [blame] | 271 | return 1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 274 | /* normally unused */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 275 | REGPRM1 static void __fd_rem(int fd) |
| 276 | { |
| 277 | __fd_clr(fd, DIR_RD); |
| 278 | __fd_clr(fd, DIR_WR); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * On valid epoll() implementations, a call to close() automatically removes |
| 283 | * the fds. This means that the FD will appear as previously unset. |
| 284 | */ |
| 285 | REGPRM1 static void __fd_clo(int fd) |
| 286 | { |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 287 | release_spec_entry(fd); |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 288 | fdtab[fd].spec.e &= ~(FD_EV_MASK); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Willy Tarreau | dc246a7 | 2007-05-09 21:57:51 +0200 | [diff] [blame] | 291 | /* |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 292 | * speculative epoll() poller |
| 293 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 294 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 295 | { |
| 296 | static unsigned int last_skipped; |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 297 | static unsigned int spec_processed; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 298 | int status, eo; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 299 | int fd, opcode; |
| 300 | int count; |
| 301 | int spec_idx; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 302 | int wait_time; |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 303 | int looping = 0; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 304 | |
| 305 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 306 | re_poll_once: |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 307 | /* Here we have two options : |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 308 | * - either walk the list forwards and hope to match more events |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 309 | * - or walk it backwards to minimize the number of changes and |
| 310 | * to make better use of the cache. |
| 311 | * Tests have shown that walking backwards improves perf by 0.2%. |
| 312 | */ |
| 313 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 314 | status = 0; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 315 | spec_idx = nbspec; |
| 316 | while (likely(spec_idx > 0)) { |
Willy Tarreau | 6574519 | 2009-03-28 21:10:48 +0100 | [diff] [blame] | 317 | int done; |
| 318 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 319 | spec_idx--; |
| 320 | fd = spec_list[spec_idx]; |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 321 | eo = fdtab[fd].spec.e; /* save old events */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 322 | |
Willy Tarreau | 573fd80 | 2009-03-22 19:25:46 +0100 | [diff] [blame] | 323 | if (looping && --fd_created < 0) { |
| 324 | /* we were just checking the newly created FDs */ |
| 325 | break; |
| 326 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 327 | /* |
| 328 | * Process the speculative events. |
| 329 | * |
| 330 | * Principle: events which are marked FD_EV_SPEC are processed |
| 331 | * with their assigned function. If the function returns 0, it |
| 332 | * means there is nothing doable without polling first. We will |
| 333 | * then convert the event to a pollable one by assigning them |
| 334 | * the WAIT status. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 335 | */ |
| 336 | |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 337 | #ifdef DEBUG_DEV |
| 338 | if (fdtab[fd].state == FD_STCLOSE) { |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 339 | fprintf(stderr,"fd=%d, fdtab[].ev=%x, fdtab[].spec.e=%x, .s=%d, idx=%d\n", |
| 340 | fd, fdtab[fd].ev, fdtab[fd].spec.e, fdtab[fd].spec.s1, spec_idx); |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 341 | } |
| 342 | #endif |
Willy Tarreau | 6574519 | 2009-03-28 21:10:48 +0100 | [diff] [blame] | 343 | done = 0; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 344 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 345 | if ((eo & FD_EV_MASK_R) == FD_EV_SPEC_R) { |
| 346 | /* The owner is interested in reading from this FD */ |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 347 | if (fdtab[fd].state != FD_STERROR) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 348 | /* Pretend there is something to read */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 349 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 350 | if (!fdtab[fd].cb[DIR_RD].f(fd)) |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 351 | fdtab[fd].spec.e ^= (FD_EV_WAIT_R ^ FD_EV_SPEC_R); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 352 | else |
Willy Tarreau | 6574519 | 2009-03-28 21:10:48 +0100 | [diff] [blame] | 353 | done = 1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 354 | } |
| 355 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 356 | else if ((eo & FD_EV_MASK_R) == FD_EV_STOP_R) { |
| 357 | /* This FD was being polled and is now being removed. */ |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 358 | fdtab[fd].spec.e &= ~FD_EV_MASK_R; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 359 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 361 | if ((eo & FD_EV_MASK_W) == FD_EV_SPEC_W) { |
| 362 | /* The owner is interested in writing to this FD */ |
Willy Tarreau | 7a52a5c | 2008-08-16 16:06:02 +0200 | [diff] [blame] | 363 | if (fdtab[fd].state != FD_STERROR) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 364 | /* Pretend there is something to write */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 365 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 366 | if (!fdtab[fd].cb[DIR_WR].f(fd)) |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 367 | fdtab[fd].spec.e ^= (FD_EV_WAIT_W ^ FD_EV_SPEC_W); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 368 | else |
Willy Tarreau | 6574519 | 2009-03-28 21:10:48 +0100 | [diff] [blame] | 369 | done = 1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 370 | } |
| 371 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 372 | else if ((eo & FD_EV_MASK_W) == FD_EV_STOP_W) { |
| 373 | /* This FD was being polled and is now being removed. */ |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 374 | fdtab[fd].spec.e &= ~FD_EV_MASK_W; |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 375 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 376 | |
Willy Tarreau | 6574519 | 2009-03-28 21:10:48 +0100 | [diff] [blame] | 377 | status += done; |
Willy Tarreau | 1eead50 | 2009-03-28 19:43:06 +0100 | [diff] [blame] | 378 | /* one callback might already have closed the fd by itself */ |
| 379 | if (fdtab[fd].state == FD_STCLOSE) |
| 380 | continue; |
| 381 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 382 | /* Now, we will adjust the event in the poll list. Indeed, it |
| 383 | * is possible that an event which was previously in the poll |
| 384 | * list now goes out, and the opposite is possible too. We can |
| 385 | * have opposite changes for READ and WRITE too. |
| 386 | */ |
| 387 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 388 | if ((eo ^ fdtab[fd].spec.e) & FD_EV_RW_PL) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 389 | /* poll status changed*/ |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 390 | if ((fdtab[fd].spec.e & FD_EV_RW_PL) == 0) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 391 | /* fd removed from poll list */ |
| 392 | opcode = EPOLL_CTL_DEL; |
| 393 | } |
| 394 | else if ((eo & FD_EV_RW_PL) == 0) { |
| 395 | /* new fd in the poll list */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 396 | opcode = EPOLL_CTL_ADD; |
| 397 | } |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 398 | else { |
| 399 | /* fd status changed */ |
| 400 | opcode = EPOLL_CTL_MOD; |
| 401 | } |
| 402 | |
| 403 | /* construct the epoll events based on new state */ |
| 404 | ev.events = 0; |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 405 | if (fdtab[fd].spec.e & FD_EV_WAIT_R) |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 406 | ev.events |= EPOLLIN; |
| 407 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 408 | if (fdtab[fd].spec.e & FD_EV_WAIT_W) |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 409 | ev.events |= EPOLLOUT; |
| 410 | |
| 411 | ev.data.fd = fd; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 412 | epoll_ctl(epoll_fd, opcode, fd, &ev); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 413 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 414 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 415 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 416 | if (!(fdtab[fd].spec.e & FD_EV_RW_SL)) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 417 | /* This fd switched to combinations of either WAIT or |
| 418 | * IDLE. It must be removed from the spec list. |
| 419 | */ |
Willy Tarreau | 4eac209 | 2007-08-31 17:01:18 +0200 | [diff] [blame] | 420 | release_spec_entry(fd); |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 421 | continue; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 425 | /* It may make sense to immediately return here if there are enough |
| 426 | * processed events, without passing through epoll_wait() because we |
| 427 | * have exactly done a poll. |
| 428 | * Measures have shown a great performance increase if we call the |
| 429 | * epoll_wait() only the second time after speculative accesses have |
| 430 | * succeeded. This reduces the number of unsucessful calls to |
| 431 | * epoll_wait() by a factor of about 3, and the total number of calls |
| 432 | * by about 2. |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 433 | * However, when we do that after having processed too many events, |
| 434 | * events waiting in epoll() starve for too long a time and tend to |
| 435 | * become themselves eligible for speculative polling. So we try to |
| 436 | * limit this practise to reasonable situations. |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 437 | */ |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 438 | |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 439 | spec_processed += status; |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 440 | |
| 441 | if (looping) { |
| 442 | last_skipped++; |
| 443 | return; |
| 444 | } |
| 445 | |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 446 | if (status >= MIN_RETURN_EVENTS && spec_processed < absmaxevents) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 447 | /* We have processed at least MIN_RETURN_EVENTS, it's worth |
| 448 | * returning now without checking epoll_wait(). |
| 449 | */ |
| 450 | if (++last_skipped <= 1) { |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 451 | tv_update_date(0, 1); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 452 | return; |
| 453 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 454 | } |
| 455 | last_skipped = 0; |
| 456 | |
Willy Tarreau | 332740d | 2009-05-10 09:57:21 +0200 | [diff] [blame] | 457 | if (nbspec || status || run_queue || signal_queue_len) { |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 458 | /* Maybe we have processed some events that we must report, or |
Willy Tarreau | 3a62811 | 2008-06-13 21:06:56 +0200 | [diff] [blame] | 459 | * maybe we still have events in the spec list, or there are |
| 460 | * some tasks left pending in the run_queue, so we must not |
Willy Tarreau | 6653d17 | 2007-05-13 01:52:05 +0200 | [diff] [blame] | 461 | * wait in epoll() otherwise we will delay their delivery by |
| 462 | * the next timeout. |
| 463 | */ |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 464 | wait_time = 0; |
| 465 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 466 | else { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 467 | if (!exp) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 468 | wait_time = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 469 | else if (tick_is_expired(exp, now_ms)) |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 470 | wait_time = 0; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 471 | else { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 472 | wait_time = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 473 | if (wait_time > MAX_DELAY_MS) |
| 474 | wait_time = MAX_DELAY_MS; |
| 475 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 476 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 477 | |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 478 | /* now let's wait for real events. We normally use maxpollevents as a |
| 479 | * high limit, unless <nbspec> is already big, in which case we need |
| 480 | * to compensate for the high number of events processed there. |
| 481 | */ |
| 482 | fd = MIN(absmaxevents, spec_processed); |
| 483 | fd = MAX(global.tune.maxpollevents, fd); |
| 484 | fd = MIN(maxfd, fd); |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 485 | /* we want to detect if an accept() will create new speculative FDs here */ |
| 486 | fd_created = 0; |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 487 | spec_processed = 0; |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 488 | status = epoll_wait(epoll_fd, epoll_events, fd, wait_time); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 489 | tv_update_date(wait_time, status); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 490 | |
| 491 | for (count = 0; count < status; count++) { |
| 492 | int e = epoll_events[count].events; |
| 493 | fd = epoll_events[count].data.fd; |
| 494 | |
| 495 | /* it looks complicated but gcc can optimize it away when constants |
| 496 | * have same values. |
| 497 | */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 498 | DPRINTF(stderr, "%s:%d: fd=%d, ev=0x%08x, e=0x%08x\n", |
| 499 | __FUNCTION__, __LINE__, |
| 500 | fd, fdtab[fd].ev, e); |
| 501 | |
| 502 | fdtab[fd].ev &= FD_POLL_STICKY; |
| 503 | fdtab[fd].ev |= |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 504 | ((e & EPOLLIN ) ? FD_POLL_IN : 0) | |
| 505 | ((e & EPOLLPRI) ? FD_POLL_PRI : 0) | |
| 506 | ((e & EPOLLOUT) ? FD_POLL_OUT : 0) | |
| 507 | ((e & EPOLLERR) ? FD_POLL_ERR : 0) | |
| 508 | ((e & EPOLLHUP) ? FD_POLL_HUP : 0); |
| 509 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 510 | if ((fdtab[fd].spec.e & FD_EV_MASK_R) == FD_EV_WAIT_R) { |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 511 | if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 512 | continue; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 513 | if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 514 | fdtab[fd].cb[DIR_RD].f(fd); |
| 515 | } |
| 516 | |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 517 | if ((fdtab[fd].spec.e & FD_EV_MASK_W) == FD_EV_WAIT_W) { |
Willy Tarreau | 8bb46f4 | 2007-04-30 12:56:21 +0200 | [diff] [blame] | 518 | if (fdtab[fd].state == FD_STCLOSE || fdtab[fd].state == FD_STERROR) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 519 | continue; |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 520 | if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR)) |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 521 | fdtab[fd].cb[DIR_WR].f(fd); |
| 522 | } |
| 523 | } |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 524 | |
| 525 | if (fd_created) { |
| 526 | /* we have created some fds, certainly in return of an accept(), |
| 527 | * and they're marked as speculative. If we can manage to perform |
| 528 | * a read(), we're almost sure to collect all the request at once |
| 529 | * and avoid several expensive wakeups. So let's try now. Anyway, |
| 530 | * if we fail, the tasks are still woken up, and the FD gets marked |
| 531 | * for poll mode. |
| 532 | */ |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 533 | looping = 1; |
| 534 | goto re_poll_once; |
| 535 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Initialization of the speculative epoll() poller. |
| 540 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 541 | * disables the poller by setting its pref to 0. |
| 542 | */ |
| 543 | REGPRM1 static int _do_init(struct poller *p) |
| 544 | { |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 545 | __label__ fail_spec, fail_ee, fail_fd; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 546 | |
| 547 | p->private = NULL; |
| 548 | |
| 549 | epoll_fd = epoll_create(global.maxsock + 1); |
| 550 | if (epoll_fd < 0) |
| 551 | goto fail_fd; |
| 552 | |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 553 | /* See comments at the top of the file about this formula. */ |
| 554 | absmaxevents = MAX(global.tune.maxpollevents, global.maxsock/4); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 555 | epoll_events = (struct epoll_event*) |
Willy Tarreau | f2e8ee2 | 2008-05-25 10:39:02 +0200 | [diff] [blame] | 556 | calloc(1, sizeof(struct epoll_event) * absmaxevents); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 557 | |
| 558 | if (epoll_events == NULL) |
| 559 | goto fail_ee; |
| 560 | |
| 561 | if ((spec_list = (uint32_t *)calloc(1, sizeof(uint32_t) * global.maxsock)) == NULL) |
| 562 | goto fail_spec; |
| 563 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 564 | return 1; |
| 565 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 566 | fail_spec: |
| 567 | free(epoll_events); |
| 568 | fail_ee: |
| 569 | close(epoll_fd); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 570 | epoll_fd = -1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 571 | fail_fd: |
| 572 | p->pref = 0; |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Termination of the speculative epoll() poller. |
| 578 | * Memory is released and the poller is marked as unselectable. |
| 579 | */ |
| 580 | REGPRM1 static void _do_term(struct poller *p) |
| 581 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 582 | free(spec_list); |
| 583 | free(epoll_events); |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 584 | |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 585 | if (epoll_fd >= 0) { |
| 586 | close(epoll_fd); |
| 587 | epoll_fd = -1; |
| 588 | } |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 589 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 590 | spec_list = NULL; |
| 591 | epoll_events = NULL; |
| 592 | |
| 593 | p->private = NULL; |
| 594 | p->pref = 0; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Check that the poller works. |
| 599 | * Returns 1 if OK, otherwise 0. |
| 600 | */ |
| 601 | REGPRM1 static int _do_test(struct poller *p) |
| 602 | { |
| 603 | int fd; |
| 604 | |
| 605 | fd = epoll_create(global.maxsock + 1); |
| 606 | if (fd < 0) |
| 607 | return 0; |
| 608 | close(fd); |
| 609 | return 1; |
| 610 | } |
| 611 | |
| 612 | /* |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 613 | * Recreate the epoll file descriptor after a fork(). Returns 1 if OK, |
| 614 | * otherwise 0. It will ensure that all processes will not share their |
| 615 | * epoll_fd. Some side effects were encountered because of this, such |
| 616 | * as epoll_wait() returning an FD which was previously deleted. |
| 617 | */ |
| 618 | REGPRM1 static int _do_fork(struct poller *p) |
| 619 | { |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 620 | if (epoll_fd >= 0) |
| 621 | close(epoll_fd); |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 622 | epoll_fd = epoll_create(global.maxsock + 1); |
| 623 | if (epoll_fd < 0) |
| 624 | return 0; |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | /* |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 629 | * It is a constructor, which means that it will automatically be called before |
| 630 | * main(). This is GCC-specific but it works at least since 2.95. |
| 631 | * Special care must be taken so that it does not need any uninitialized data. |
| 632 | */ |
| 633 | __attribute__((constructor)) |
| 634 | static void _do_register(void) |
| 635 | { |
| 636 | struct poller *p; |
| 637 | |
| 638 | if (nbpollers >= MAX_POLLERS) |
| 639 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 640 | |
| 641 | epoll_fd = -1; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 642 | p = &pollers[nbpollers++]; |
| 643 | |
| 644 | p->name = "sepoll"; |
| 645 | p->pref = 400; |
| 646 | p->private = NULL; |
| 647 | |
| 648 | p->test = _do_test; |
| 649 | p->init = _do_init; |
| 650 | p->term = _do_term; |
| 651 | p->poll = _do_poll; |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 652 | p->fork = _do_fork; |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 653 | |
| 654 | p->is_set = __fd_is_set; |
| 655 | p->cond_s = p->set = __fd_set; |
| 656 | p->cond_c = p->clr = __fd_clr; |
| 657 | p->rem = __fd_rem; |
| 658 | p->clo = __fd_clo; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* |
| 663 | * Local variables: |
| 664 | * c-indent-level: 8 |
| 665 | * c-basic-offset: 8 |
| 666 | * End: |
| 667 | */ |