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 | */ |
Willy Tarreau | 26d7cfc | 2012-12-07 00:09:43 +0100 | [diff] [blame] | 155 | if (fdtab[fd].ev & FD_POLL_IN) |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 156 | fd_ev_set(fd, DIR_RD); |
| 157 | |
Willy Tarreau | 26d7cfc | 2012-12-07 00:09:43 +0100 | [diff] [blame] | 158 | if (fdtab[fd].ev & FD_POLL_OUT) |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 159 | fd_ev_set(fd, DIR_WR); |
| 160 | |
Willy Tarreau | 39ebef8 | 2012-12-14 00:17:03 +0100 | [diff] [blame] | 161 | if (fdtab[fd].spec_p) { |
| 162 | /* This fd was already scheduled for being |
| 163 | * called as a speculative I/O. |
| 164 | */ |
| 165 | continue; |
| 166 | } |
| 167 | |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 168 | fdtab[fd].iocb(fd); |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 169 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Initialization of the kqueue() poller. |
| 175 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 176 | * disables the poller by setting its pref to 0. |
| 177 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 178 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 179 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 180 | p->private = NULL; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 181 | |
| 182 | kqueue_fd = kqueue(); |
| 183 | if (kqueue_fd < 0) |
| 184 | goto fail_fd; |
| 185 | |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 186 | /* we can have up to two events per fd (*/ |
| 187 | kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 188 | if (kev == NULL) |
| 189 | goto fail_kev; |
| 190 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 191 | return 1; |
| 192 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 193 | fail_kev: |
| 194 | close(kqueue_fd); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 195 | kqueue_fd = -1; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 196 | fail_fd: |
| 197 | p->pref = 0; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Termination of the kqueue() poller. |
| 203 | * Memory is released and the poller is marked as unselectable. |
| 204 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 205 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 206 | { |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 207 | free(kev); |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 208 | |
| 209 | if (kqueue_fd >= 0) { |
| 210 | close(kqueue_fd); |
| 211 | kqueue_fd = -1; |
| 212 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 213 | |
| 214 | p->private = NULL; |
| 215 | p->pref = 0; |
| 216 | } |
| 217 | |
| 218 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 219 | * Check that the poller works. |
| 220 | * Returns 1 if OK, otherwise 0. |
| 221 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 222 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 223 | { |
| 224 | int fd; |
| 225 | |
| 226 | fd = kqueue(); |
| 227 | if (fd < 0) |
| 228 | return 0; |
| 229 | close(fd); |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 235 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 236 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 237 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 238 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 239 | { |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 240 | if (kqueue_fd >= 0) |
| 241 | close(kqueue_fd); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 242 | kqueue_fd = kqueue(); |
| 243 | if (kqueue_fd < 0) |
| 244 | return 0; |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 249 | * It is a constructor, which means that it will automatically be called before |
| 250 | * main(). This is GCC-specific but it works at least since 2.95. |
| 251 | * 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] | 252 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 253 | __attribute__((constructor)) |
| 254 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 255 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 256 | struct poller *p; |
| 257 | |
| 258 | if (nbpollers >= MAX_POLLERS) |
| 259 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 260 | |
| 261 | kqueue_fd = -1; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 262 | p = &pollers[nbpollers++]; |
| 263 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 264 | p->name = "kqueue"; |
| 265 | p->pref = 300; |
| 266 | p->private = NULL; |
| 267 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 268 | p->clo = NULL; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 269 | p->test = _do_test; |
| 270 | p->init = _do_init; |
| 271 | p->term = _do_term; |
| 272 | p->poll = _do_poll; |
| 273 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
| 277 | /* |
| 278 | * Local variables: |
| 279 | * c-indent-level: 8 |
| 280 | * c-basic-offset: 8 |
| 281 | * End: |
| 282 | */ |