blob: 36a7b7f41f94eaaa5019e551e5dbb09502db919c [file] [log] [blame]
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001/*
2 * FD polling functions for FreeBSD kqueue()
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreau1e63130a2007-04-09 12:03:06 +02005 *
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 Tarreau1e63130a2007-04-09 12:03:06 +020017#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 Tarreau0c303ee2008-07-07 00:09:58 +020026#include <common/ticks.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020027#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020028#include <common/tools.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020029
Willy Tarreau1e63130a2007-04-09 12:03:06 +020030#include <types/global.h>
31
32#include <proto/fd.h>
Willy Tarreau332740d2009-05-10 09:57:21 +020033#include <proto/signal.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020034#include <proto/task.h>
35
36/* private data */
Willy Tarreau1e63130a2007-04-09 12:03:06 +020037static int kqueue_fd;
38static struct kevent *kev = NULL;
39
Willy Tarreau1e63130a2007-04-09 12:03:06 +020040/*
Willy Tarreau4a226272012-11-11 20:49:49 +010041 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020042 */
Willy Tarreau4a226272012-11-11 20:49:49 +010043REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020044{
Willy Tarreau4a226272012-11-11 20:49:49 +010045 int status;
46 int count, fd, delta_ms;
47 struct timespec timeout;
48 int updt_idx, en, eo;
49 int changes = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020050
Willy Tarreau4a226272012-11-11 20:49:49 +010051 /* 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 Tarreau1e63130a2007-04-09 12:03:06 +020056
Willy Tarreau4a226272012-11-11 20:49:49 +010057 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 Tarreau1e63130a2007-04-09 12:03:06 +020069
Willy Tarreau4a226272012-11-11 20:49:49 +010070 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 Tarreau1e63130a2007-04-09 12:03:06 +020081
Willy Tarreau4a226272012-11-11 20:49:49 +010082 fdtab[fd].spec_e = (en << 4) + en; /* save new events */
Willy Tarreau1e63130a2007-04-09 12:03:06 +020083
Willy Tarreau4a226272012-11-11 20:49:49 +010084 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 Tarreau1e63130a2007-04-09 12:03:06 +020094
Willy Tarreau4a226272012-11-11 20:49:49 +010095 }
96 fdtab[fd].updated = 0;
97 fdtab[fd].new = 0;
98 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020099 if (changes)
100 kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
Willy Tarreau4a226272012-11-11 20:49:49 +0100101 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200102
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200103 delta_ms = 0;
104 timeout.tv_sec = 0;
105 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +0200106
Willy Tarreau4a226272012-11-11 20:49:49 +0100107 if (!fd_nbspec && !run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200108 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 Tarreaub0b37bc2008-06-23 14:00:57 +0200112 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200113 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 Tarreaub0b37bc2008-06-23 14:00:57 +0200116 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200117 timeout.tv_sec = (delta_ms / 1000);
118 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200119 }
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200120 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200121
Willy Tarreau1db37712007-06-03 17:16:49 +0200122 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau45a12512011-09-10 16:56:42 +0200123 gettimeofday(&before_poll, NULL);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200124 status = kevent(kqueue_fd, // int kq
125 NULL, // const struct kevent *changelist
126 0, // int nchanges
127 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200128 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200129 &timeout); // const struct timespec *timeout
130 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200131 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200132
133 for (count = 0; count < status; count++) {
134 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200135
Willy Tarreau076be252012-07-06 16:02:29 +0200136 if (!fdtab[fd].owner)
137 continue;
138
Willy Tarreau9845e752012-07-06 11:44:28 +0200139 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau4a226272012-11-11 20:49:49 +0100140
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200141 if (kev[count].filter == EVFILT_READ) {
Willy Tarreau4a226272012-11-11 20:49:49 +0100142 if ((fdtab[fd].spec_e & FD_EV_STATUS_R))
Willy Tarreau491c4982012-07-06 11:16:01 +0200143 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau4a226272012-11-11 20:49:49 +0100144 }
145 else if (kev[count].filter == EVFILT_WRITE) {
146 if ((fdtab[fd].spec_e & FD_EV_STATUS_W))
Willy Tarreau491c4982012-07-06 11:16:01 +0200147 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200148 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100149
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 Tarreau26d7cfc2012-12-07 00:09:43 +0100155 if (fdtab[fd].ev & FD_POLL_IN)
Willy Tarreau4a226272012-11-11 20:49:49 +0100156 fd_ev_set(fd, DIR_RD);
157
Willy Tarreau26d7cfc2012-12-07 00:09:43 +0100158 if (fdtab[fd].ev & FD_POLL_OUT)
Willy Tarreau4a226272012-11-11 20:49:49 +0100159 fd_ev_set(fd, DIR_WR);
160
Willy Tarreau9845e752012-07-06 11:44:28 +0200161 fdtab[fd].iocb(fd);
Willy Tarreau4a226272012-11-11 20:49:49 +0100162 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200163 }
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 Tarreauef1d1f82007-04-16 00:25:25 +0200171REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200172{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200173 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200174
175 kqueue_fd = kqueue();
176 if (kqueue_fd < 0)
177 goto fail_fd;
178
Willy Tarreau4a226272012-11-11 20:49:49 +0100179 /* we can have up to two events per fd (*/
180 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200181 if (kev == NULL)
182 goto fail_kev;
183
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200184 return 1;
185
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200186 fail_kev:
187 close(kqueue_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200188 kqueue_fd = -1;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200189 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 Tarreauef1d1f82007-04-16 00:25:25 +0200198REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200199{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200200 free(kev);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200201
202 if (kqueue_fd >= 0) {
203 close(kqueue_fd);
204 kqueue_fd = -1;
205 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200206
207 p->private = NULL;
208 p->pref = 0;
209}
210
211/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200212 * Check that the poller works.
213 * Returns 1 if OK, otherwise 0.
214 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200215REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200216{
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 Tarreauef1d1f82007-04-16 00:25:25 +0200231REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200232{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200233 if (kqueue_fd >= 0)
234 close(kqueue_fd);
Willy Tarreau2ff76222007-04-09 19:29:56 +0200235 kqueue_fd = kqueue();
236 if (kqueue_fd < 0)
237 return 0;
238 return 1;
239}
240
241/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200242 * 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 Tarreau1e63130a2007-04-09 12:03:06 +0200245 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200246__attribute__((constructor))
247static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200248{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200249 struct poller *p;
250
251 if (nbpollers >= MAX_POLLERS)
252 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200253
254 kqueue_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200255 p = &pollers[nbpollers++];
256
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200257 p->name = "kqueue";
258 p->pref = 300;
259 p->private = NULL;
260
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100261 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200262 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 Tarreau1e63130a2007-04-09 12:03:06 +0200267}
268
269
270/*
271 * Local variables:
272 * c-indent-level: 8
273 * c-basic-offset: 8
274 * End:
275 */