blob: 2142132e21bea4050480a84422b2312a07bb0626 [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 Tarreau39ebef82012-12-14 00:17:03 +0100161 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 Tarreau9845e752012-07-06 11:44:28 +0200168 fdtab[fd].iocb(fd);
Willy Tarreau4a226272012-11-11 20:49:49 +0100169 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200170 }
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 Tarreauef1d1f82007-04-16 00:25:25 +0200178REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200179{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200180 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200181
182 kqueue_fd = kqueue();
183 if (kqueue_fd < 0)
184 goto fail_fd;
185
Willy Tarreau4a226272012-11-11 20:49:49 +0100186 /* we can have up to two events per fd (*/
187 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200188 if (kev == NULL)
189 goto fail_kev;
190
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200191 return 1;
192
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200193 fail_kev:
194 close(kqueue_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200195 kqueue_fd = -1;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200196 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 Tarreauef1d1f82007-04-16 00:25:25 +0200205REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200206{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200207 free(kev);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200208
209 if (kqueue_fd >= 0) {
210 close(kqueue_fd);
211 kqueue_fd = -1;
212 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200213
214 p->private = NULL;
215 p->pref = 0;
216}
217
218/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200219 * Check that the poller works.
220 * Returns 1 if OK, otherwise 0.
221 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200222REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200223{
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 Tarreauef1d1f82007-04-16 00:25:25 +0200238REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200239{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200240 if (kqueue_fd >= 0)
241 close(kqueue_fd);
Willy Tarreau2ff76222007-04-09 19:29:56 +0200242 kqueue_fd = kqueue();
243 if (kqueue_fd < 0)
244 return 0;
245 return 1;
246}
247
248/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200249 * 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 Tarreau1e63130a2007-04-09 12:03:06 +0200252 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200253__attribute__((constructor))
254static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200255{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200256 struct poller *p;
257
258 if (nbpollers >= MAX_POLLERS)
259 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200260
261 kqueue_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200262 p = &pollers[nbpollers++];
263
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200264 p->name = "kqueue";
265 p->pref = 300;
266 p->private = NULL;
267
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100268 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200269 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 Tarreau1e63130a2007-04-09 12:03:06 +0200274}
275
276
277/*
278 * Local variables:
279 * c-indent-level: 8
280 * c-basic-offset: 8
281 * End:
282 */