Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for FreeBSD kqueue() |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +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 | * Note: not knowing much about kqueue, I had to rely on OpenBSD's detailed man |
| 12 | * page and to check how it was implemented in lighttpd to understand it better. |
| 13 | * But it is possible that I got things wrong. |
| 14 | * |
| 15 | */ |
| 16 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <sys/time.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <sys/event.h> |
| 22 | #include <sys/time.h> |
| 23 | |
| 24 | #include <common/compat.h> |
| 25 | #include <common/config.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 26 | #include <common/ticks.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 27 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 28 | #include <common/tools.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 30 | #include <types/global.h> |
| 31 | |
| 32 | #include <proto/fd.h> |
Willy Tarreau | 332740d | 2009-05-10 09:57:21 +0200 | [diff] [blame] | 33 | #include <proto/signal.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 34 | #include <proto/task.h> |
| 35 | |
| 36 | /* private data */ |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 37 | static int kqueue_fd; |
| 38 | static struct kevent *kev = NULL; |
| 39 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 40 | /* |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 41 | * kqueue() poller |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 42 | */ |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 43 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 44 | { |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 45 | int status; |
| 46 | int count, fd, delta_ms; |
| 47 | struct timespec timeout; |
| 48 | int updt_idx, en, eo; |
| 49 | int changes = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 51 | /* first, scan the update list to find changes */ |
| 52 | for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) { |
| 53 | fd = fd_updt[updt_idx]; |
| 54 | en = fdtab[fd].spec_e & 15; /* new events */ |
| 55 | eo = fdtab[fd].spec_e >> 4; /* previous events */ |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 57 | if (fdtab[fd].owner && (eo ^ en)) { |
| 58 | if ((eo ^ en) & FD_EV_POLLED_R) { |
| 59 | /* read poll status changed */ |
| 60 | if (en & FD_EV_POLLED_R) { |
| 61 | EV_SET(&kev[changes], fd, EVFILT_READ, EV_ADD, 0, 0, NULL); |
| 62 | changes++; |
| 63 | } |
| 64 | else { |
| 65 | EV_SET(&kev[changes], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
| 66 | changes++; |
| 67 | } |
| 68 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 70 | if ((eo ^ en) & FD_EV_POLLED_W) { |
| 71 | /* write poll status changed */ |
| 72 | if (en & FD_EV_POLLED_W) { |
| 73 | EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); |
| 74 | changes++; |
| 75 | } |
| 76 | else { |
| 77 | EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
| 78 | changes++; |
| 79 | } |
| 80 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 82 | fdtab[fd].spec_e = (en << 4) + en; /* save new events */ |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 84 | if (!(en & FD_EV_ACTIVE_RW)) { |
| 85 | /* This fd doesn't use any active entry anymore, we can |
| 86 | * kill its entry. |
| 87 | */ |
| 88 | release_spec_entry(fd); |
| 89 | } |
| 90 | else if ((en & ~eo) & FD_EV_ACTIVE_RW) { |
| 91 | /* we need a new spec entry now */ |
| 92 | alloc_spec_entry(fd); |
| 93 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 95 | } |
| 96 | fdtab[fd].updated = 0; |
| 97 | fdtab[fd].new = 0; |
| 98 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 99 | if (changes) |
| 100 | kevent(kqueue_fd, kev, changes, NULL, 0, NULL); |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 101 | fd_nbupdt = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 103 | delta_ms = 0; |
| 104 | timeout.tv_sec = 0; |
| 105 | timeout.tv_nsec = 0; |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 106 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 107 | if (!fd_nbspec && !run_queue && !signal_queue_len) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 108 | if (!exp) { |
| 109 | delta_ms = MAX_DELAY_MS; |
| 110 | timeout.tv_sec = (MAX_DELAY_MS / 1000); |
| 111 | timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 112 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 113 | else if (!tick_is_expired(exp, now_ms)) { |
| 114 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
| 115 | if (delta_ms > MAX_DELAY_MS) |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 116 | delta_ms = MAX_DELAY_MS; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 117 | timeout.tv_sec = (delta_ms / 1000); |
| 118 | timeout.tv_nsec = (delta_ms % 1000) * 1000000; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 119 | } |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 120 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 122 | fd = MIN(maxfd, global.tune.maxpollevents); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 123 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 124 | status = kevent(kqueue_fd, // int kq |
| 125 | NULL, // const struct kevent *changelist |
| 126 | 0, // int nchanges |
| 127 | kev, // struct kevent *eventlist |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 128 | fd, // int nevents |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 129 | &timeout); // const struct timespec *timeout |
| 130 | tv_update_date(delta_ms, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 131 | measure_idle(); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 132 | |
| 133 | for (count = 0; count < status; count++) { |
| 134 | fd = kev[count].ident; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 135 | |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 136 | if (!fdtab[fd].owner) |
| 137 | continue; |
| 138 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 139 | fdtab[fd].ev &= FD_POLL_STICKY; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 140 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 141 | if (kev[count].filter == EVFILT_READ) { |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 142 | if ((fdtab[fd].spec_e & FD_EV_STATUS_R)) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 143 | fdtab[fd].ev |= FD_POLL_IN; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 144 | } |
| 145 | else if (kev[count].filter == EVFILT_WRITE) { |
| 146 | if ((fdtab[fd].spec_e & FD_EV_STATUS_W)) |
Willy Tarreau | 491c498 | 2012-07-06 11:16:01 +0200 | [diff] [blame] | 147 | fdtab[fd].ev |= FD_POLL_OUT; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 148 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 149 | |
| 150 | if (fdtab[fd].iocb && fdtab[fd].ev) { |
| 151 | /* Mark the events as speculative before processing |
| 152 | * them so that if nothing can be done we don't need |
| 153 | * to poll again. |
| 154 | */ |
| 155 | if (fdtab[fd].ev & (FD_POLL_IN|FD_POLL_HUP|FD_POLL_ERR)) |
| 156 | fd_ev_set(fd, DIR_RD); |
| 157 | |
| 158 | if (fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR)) |
| 159 | fd_ev_set(fd, DIR_WR); |
| 160 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 161 | fdtab[fd].iocb(fd); |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 162 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Initialization of the kqueue() poller. |
| 168 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 169 | * disables the poller by setting its pref to 0. |
| 170 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 171 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 172 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 173 | p->private = NULL; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 174 | |
| 175 | kqueue_fd = kqueue(); |
| 176 | if (kqueue_fd < 0) |
| 177 | goto fail_fd; |
| 178 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 179 | /* we can have up to two events per fd (*/ |
| 180 | kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 181 | if (kev == NULL) |
| 182 | goto fail_kev; |
| 183 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 184 | return 1; |
| 185 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 186 | fail_kev: |
| 187 | close(kqueue_fd); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 188 | kqueue_fd = -1; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 189 | fail_fd: |
| 190 | p->pref = 0; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Termination of the kqueue() poller. |
| 196 | * Memory is released and the poller is marked as unselectable. |
| 197 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 198 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 199 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 200 | free(kev); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 201 | |
| 202 | if (kqueue_fd >= 0) { |
| 203 | close(kqueue_fd); |
| 204 | kqueue_fd = -1; |
| 205 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 206 | |
| 207 | p->private = NULL; |
| 208 | p->pref = 0; |
| 209 | } |
| 210 | |
| 211 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 212 | * Check that the poller works. |
| 213 | * Returns 1 if OK, otherwise 0. |
| 214 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 215 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 216 | { |
| 217 | int fd; |
| 218 | |
| 219 | fd = kqueue(); |
| 220 | if (fd < 0) |
| 221 | return 0; |
| 222 | close(fd); |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 228 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 229 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 230 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 231 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 232 | { |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 233 | if (kqueue_fd >= 0) |
| 234 | close(kqueue_fd); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 235 | kqueue_fd = kqueue(); |
| 236 | if (kqueue_fd < 0) |
| 237 | return 0; |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 242 | * It is a constructor, which means that it will automatically be called before |
| 243 | * main(). This is GCC-specific but it works at least since 2.95. |
| 244 | * Special care must be taken so that it does not need any uninitialized data. |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 245 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 246 | __attribute__((constructor)) |
| 247 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 248 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 249 | struct poller *p; |
| 250 | |
| 251 | if (nbpollers >= MAX_POLLERS) |
| 252 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 253 | |
| 254 | kqueue_fd = -1; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 255 | p = &pollers[nbpollers++]; |
| 256 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 257 | p->name = "kqueue"; |
| 258 | p->pref = 300; |
| 259 | p->private = NULL; |
| 260 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 261 | p->clo = NULL; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 262 | p->test = _do_test; |
| 263 | p->init = _do_init; |
| 264 | p->term = _do_term; |
| 265 | p->poll = _do_poll; |
| 266 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | /* |
| 271 | * Local variables: |
| 272 | * c-indent-level: 8 |
| 273 | * c-basic-offset: 8 |
| 274 | * End: |
| 275 | */ |