blob: 2af94b647c10a44541c128ea6d42a88c77ac17a9 [file] [log] [blame]
Willy Tarreau1e63130a2007-04-09 12:03:06 +02001/*
2 * FD polling functions for FreeBSD kqueue()
3 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01004 * Copyright 2000-2014 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 *
Willy Tarreau1e63130a2007-04-09 12:03:06 +020011 */
12
Willy Tarreau1e63130a2007-04-09 12:03:06 +020013#include <unistd.h>
14#include <sys/time.h>
15#include <sys/types.h>
16
17#include <sys/event.h>
18#include <sys/time.h>
19
20#include <common/compat.h>
21#include <common/config.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020022#include <common/ticks.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020023#include <common/time.h>
Willy Tarreau1db37712007-06-03 17:16:49 +020024#include <common/tools.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020025
Willy Tarreau1e63130a2007-04-09 12:03:06 +020026#include <types/global.h>
27
28#include <proto/fd.h>
Willy Tarreau332740d2009-05-10 09:57:21 +020029#include <proto/signal.h>
Willy Tarreau1e63130a2007-04-09 12:03:06 +020030#include <proto/task.h>
31
32/* private data */
Willy Tarreau1e63130a2007-04-09 12:03:06 +020033static int kqueue_fd;
34static struct kevent *kev = NULL;
35
Willy Tarreau1e63130a2007-04-09 12:03:06 +020036/*
Willy Tarreau4a226272012-11-11 20:49:49 +010037 * kqueue() poller
Willy Tarreau1e63130a2007-04-09 12:03:06 +020038 */
Willy Tarreau4a226272012-11-11 20:49:49 +010039REGPRM2 static void _do_poll(struct poller *p, int exp)
Willy Tarreau1e63130a2007-04-09 12:03:06 +020040{
Willy Tarreau4a226272012-11-11 20:49:49 +010041 int status;
42 int count, fd, delta_ms;
43 struct timespec timeout;
44 int updt_idx, en, eo;
45 int changes = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020046
Willy Tarreau4a226272012-11-11 20:49:49 +010047 /* first, scan the update list to find changes */
48 for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) {
49 fd = fd_updt[updt_idx];
Willy Tarreauf817e9f2014-01-10 16:58:45 +010050 fdtab[fd].updated = 0;
51 fdtab[fd].new = 0;
52
53 if (!fdtab[fd].owner)
54 continue;
55
Willy Tarreau25002d22014-01-25 10:32:56 +010056 eo = fdtab[fd].state;
57 en = fd_compute_new_polled_status(eo);
Willy Tarreauf817e9f2014-01-10 16:58:45 +010058
59 if ((eo ^ en) & FD_EV_POLLED_RW) {
60 /* poll status changed */
61 fdtab[fd].state = en;
62
Willy Tarreau4a226272012-11-11 20:49:49 +010063 if ((eo ^ en) & FD_EV_POLLED_R) {
64 /* read poll status changed */
65 if (en & FD_EV_POLLED_R) {
66 EV_SET(&kev[changes], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
67 changes++;
68 }
69 else {
70 EV_SET(&kev[changes], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
71 changes++;
72 }
73 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020074
Willy Tarreau4a226272012-11-11 20:49:49 +010075 if ((eo ^ en) & FD_EV_POLLED_W) {
76 /* write poll status changed */
77 if (en & FD_EV_POLLED_W) {
78 EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
79 changes++;
80 }
81 else {
82 EV_SET(&kev[changes], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
83 changes++;
84 }
85 }
Willy Tarreau4a226272012-11-11 20:49:49 +010086 }
Willy Tarreau4a226272012-11-11 20:49:49 +010087 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +020088 if (changes)
89 kevent(kqueue_fd, kev, changes, NULL, 0, NULL);
Willy Tarreau4a226272012-11-11 20:49:49 +010090 fd_nbupdt = 0;
Willy Tarreau1e63130a2007-04-09 12:03:06 +020091
Willy Tarreau0c303ee2008-07-07 00:09:58 +020092 delta_ms = 0;
93 timeout.tv_sec = 0;
94 timeout.tv_nsec = 0;
Willy Tarreau79b8a622007-05-14 03:15:46 +020095
Willy Tarreau16f649c2014-01-25 19:10:48 +010096 if (!fd_cache_num && !run_queue && !signal_queue_len) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +020097 if (!exp) {
98 delta_ms = MAX_DELAY_MS;
99 timeout.tv_sec = (MAX_DELAY_MS / 1000);
100 timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200101 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200102 else if (!tick_is_expired(exp, now_ms)) {
103 delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1;
104 if (delta_ms > MAX_DELAY_MS)
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200105 delta_ms = MAX_DELAY_MS;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200106 timeout.tv_sec = (delta_ms / 1000);
107 timeout.tv_nsec = (delta_ms % 1000) * 1000000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200108 }
Willy Tarreaucd5ce2a2007-04-09 16:25:46 +0200109 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200110
Willy Tarreau1db37712007-06-03 17:16:49 +0200111 fd = MIN(maxfd, global.tune.maxpollevents);
Willy Tarreau45a12512011-09-10 16:56:42 +0200112 gettimeofday(&before_poll, NULL);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200113 status = kevent(kqueue_fd, // int kq
114 NULL, // const struct kevent *changelist
115 0, // int nchanges
116 kev, // struct kevent *eventlist
Willy Tarreau1db37712007-06-03 17:16:49 +0200117 fd, // int nevents
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200118 &timeout); // const struct timespec *timeout
119 tv_update_date(delta_ms, status);
Willy Tarreau45a12512011-09-10 16:56:42 +0200120 measure_idle();
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200121
122 for (count = 0; count < status; count++) {
123 fd = kev[count].ident;
Willy Tarreau9845e752012-07-06 11:44:28 +0200124
Willy Tarreau076be252012-07-06 16:02:29 +0200125 if (!fdtab[fd].owner)
126 continue;
127
Willy Tarreau9845e752012-07-06 11:44:28 +0200128 fdtab[fd].ev &= FD_POLL_STICKY;
Willy Tarreau4a226272012-11-11 20:49:49 +0100129
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200130 if (kev[count].filter == EVFILT_READ) {
Willy Tarreau69a41fa2014-01-20 11:02:59 +0100131 if ((fdtab[fd].state & FD_EV_STATUS_R))
Willy Tarreau491c4982012-07-06 11:16:01 +0200132 fdtab[fd].ev |= FD_POLL_IN;
Willy Tarreau4a226272012-11-11 20:49:49 +0100133 }
134 else if (kev[count].filter == EVFILT_WRITE) {
Willy Tarreau69a41fa2014-01-20 11:02:59 +0100135 if ((fdtab[fd].state & FD_EV_STATUS_W))
Willy Tarreau491c4982012-07-06 11:16:01 +0200136 fdtab[fd].ev |= FD_POLL_OUT;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200137 }
Willy Tarreau4a226272012-11-11 20:49:49 +0100138
Willy Tarreau5be2f352014-11-19 19:43:05 +0100139 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
140 fd_may_recv(fd);
141
142 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
143 fd_may_send(fd);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200144 }
145}
146
147/*
148 * Initialization of the kqueue() poller.
149 * Returns 0 in case of failure, non-zero in case of success. If it fails, it
150 * disables the poller by setting its pref to 0.
151 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200152REGPRM1 static int _do_init(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200153{
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200154 p->private = NULL;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200155
156 kqueue_fd = kqueue();
157 if (kqueue_fd < 0)
158 goto fail_fd;
159
Willy Tarreau4a226272012-11-11 20:49:49 +0100160 /* we can have up to two events per fd (*/
161 kev = (struct kevent*)calloc(1, sizeof(struct kevent) * 2 * global.maxsock);
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200162 if (kev == NULL)
163 goto fail_kev;
164
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200165 return 1;
166
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200167 fail_kev:
168 close(kqueue_fd);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200169 kqueue_fd = -1;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200170 fail_fd:
171 p->pref = 0;
172 return 0;
173}
174
175/*
176 * Termination of the kqueue() poller.
177 * Memory is released and the poller is marked as unselectable.
178 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200179REGPRM1 static void _do_term(struct poller *p)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200180{
Willy Tarreaua534fea2008-08-03 12:19:50 +0200181 free(kev);
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200182
183 if (kqueue_fd >= 0) {
184 close(kqueue_fd);
185 kqueue_fd = -1;
186 }
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200187
188 p->private = NULL;
189 p->pref = 0;
190}
191
192/*
Willy Tarreau2ff76222007-04-09 19:29:56 +0200193 * Check that the poller works.
194 * Returns 1 if OK, otherwise 0.
195 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200196REGPRM1 static int _do_test(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200197{
198 int fd;
199
200 fd = kqueue();
201 if (fd < 0)
202 return 0;
203 close(fd);
204 return 1;
205}
206
207/*
208 * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK,
209 * otherwise 0. Note that some pollers need to be reopened after a fork()
210 * (such as kqueue), and some others may fail to do so in a chroot.
211 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200212REGPRM1 static int _do_fork(struct poller *p)
Willy Tarreau2ff76222007-04-09 19:29:56 +0200213{
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200214 if (kqueue_fd >= 0)
215 close(kqueue_fd);
Willy Tarreau2ff76222007-04-09 19:29:56 +0200216 kqueue_fd = kqueue();
217 if (kqueue_fd < 0)
218 return 0;
219 return 1;
220}
221
222/*
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200223 * It is a constructor, which means that it will automatically be called before
224 * main(). This is GCC-specific but it works at least since 2.95.
225 * Special care must be taken so that it does not need any uninitialized data.
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200226 */
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200227__attribute__((constructor))
228static void _do_register(void)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200229{
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200230 struct poller *p;
231
232 if (nbpollers >= MAX_POLLERS)
233 return;
Willy Tarreaud79e79b2009-05-10 10:18:54 +0200234
235 kqueue_fd = -1;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200236 p = &pollers[nbpollers++];
237
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200238 p->name = "kqueue";
239 p->pref = 300;
240 p->private = NULL;
241
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100242 p->clo = NULL;
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200243 p->test = _do_test;
244 p->init = _do_init;
245 p->term = _do_term;
246 p->poll = _do_poll;
247 p->fork = _do_fork;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200248}
249
250
251/*
252 * Local variables:
253 * c-indent-level: 8
254 * c-basic-offset: 8
255 * End:
256 */