nginx/src/event/modules/ngx_kqueue_module.c

481 lines
12 KiB
C
Raw Normal View History

2003-05-20 00:39:14 +08:00
/*
2003-03-04 14:33:48 +08:00
* Copyright (C) 2002-2003 Igor Sysoev, http://sysoev.ru
*/
#include <ngx_config.h>
2002-08-30 00:59:54 +08:00
#include <ngx_core.h>
#include <ngx_event.h>
#include <ngx_kqueue_module.h>
2003-06-06 22:59:20 +08:00
typedef struct {
2003-07-04 00:30:22 +08:00
u_int changes;
u_int events;
2003-06-06 22:59:20 +08:00
} ngx_kqueue_conf_t;
2003-07-04 23:10:33 +08:00
static int ngx_kqueue_init(ngx_cycle_t *cycle);
static void ngx_kqueue_done(ngx_cycle_t *cycle);
2003-05-20 00:39:14 +08:00
static int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_kqueue_del_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags);
static int ngx_kqueue_process_events(ngx_log_t *log);
2002-12-25 01:30:59 +08:00
2003-07-07 14:11:50 +08:00
static void *ngx_kqueue_create_conf(ngx_cycle_t *cycle);
static char *ngx_kqueue_init_conf(ngx_cycle_t *cycle, void *conf);
2003-05-16 23:27:48 +08:00
2003-06-16 02:32:13 +08:00
int ngx_kqueue = -1;
2003-05-20 00:39:14 +08:00
static struct kevent *change_list, *event_list;
2003-06-16 02:32:13 +08:00
static u_int max_changes, nchanges, nevents;
2002-12-15 14:25:09 +08:00
2003-05-20 00:39:14 +08:00
static ngx_str_t kqueue_name = ngx_string("kqueue");
2003-05-16 23:27:48 +08:00
static ngx_command_t ngx_kqueue_commands[] = {
{ngx_string("kqueue_changes"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
2003-05-20 00:39:14 +08:00
offsetof(ngx_kqueue_conf_t, changes),
2003-05-16 23:27:48 +08:00
NULL},
{ngx_string("kqueue_events"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
2003-05-20 00:39:14 +08:00
offsetof(ngx_kqueue_conf_t, events),
2003-05-16 23:27:48 +08:00
NULL},
2003-05-27 20:18:54 +08:00
ngx_null_command
2003-05-16 23:27:48 +08:00
};
2003-05-20 00:39:14 +08:00
ngx_event_module_t ngx_kqueue_module_ctx = {
&kqueue_name,
ngx_kqueue_create_conf, /* create configuration */
ngx_kqueue_init_conf, /* init configuration */
2003-05-23 19:53:01 +08:00
{
2003-05-20 00:39:14 +08:00
ngx_kqueue_add_event, /* add an event */
ngx_kqueue_del_event, /* delete an event */
ngx_kqueue_add_event, /* enable an event */
ngx_kqueue_del_event, /* disable an event */
NULL, /* add an connection */
NULL, /* delete an connection */
ngx_kqueue_process_events, /* process the events */
ngx_kqueue_init, /* init the events */
2003-05-21 21:28:21 +08:00
ngx_kqueue_done /* done the events */
2003-05-20 00:39:14 +08:00
}
};
2003-05-16 23:27:48 +08:00
ngx_module_t ngx_kqueue_module = {
2003-05-27 20:18:54 +08:00
NGX_MODULE,
2003-05-20 00:39:14 +08:00
&ngx_kqueue_module_ctx, /* module context */
2003-05-16 23:27:48 +08:00
ngx_kqueue_commands, /* module directives */
2003-05-27 20:18:54 +08:00
NGX_EVENT_MODULE, /* module type */
2003-07-04 14:03:52 +08:00
NULL, /* init module */
NULL /* init child */
2003-05-16 23:27:48 +08:00
};
2003-07-04 00:30:22 +08:00
2003-07-04 23:10:33 +08:00
static int ngx_kqueue_init(ngx_cycle_t *cycle)
2003-07-04 00:30:22 +08:00
{
struct timespec ts;
ngx_kqueue_conf_t *kcf;
kcf = ngx_event_get_conf(cycle->conf_ctx, ngx_kqueue_module);
2003-07-04 23:10:33 +08:00
ngx_log_debug(cycle->log, "CH: %d" _ kcf->changes);
ngx_log_debug(cycle->log, "EV: %d" _ kcf->events);
2003-05-20 00:39:14 +08:00
2003-06-16 02:32:13 +08:00
if (ngx_kqueue == -1) {
ngx_kqueue = kqueue();
if (ngx_kqueue == -1) {
2003-07-04 23:10:33 +08:00
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"kqueue() failed");
2003-06-16 02:32:13 +08:00
return NGX_ERROR;
}
}
if (max_changes < kcf->changes) {
if (nchanges) {
ts.tv_sec = 0;
ts.tv_nsec = 0;
if (kevent(ngx_kqueue, change_list, nchanges, NULL, 0, &ts) == -1) {
2003-07-04 23:10:33 +08:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"kevent() failed");
2003-06-16 02:32:13 +08:00
return NGX_ERROR;
}
2003-07-04 23:10:33 +08:00
nchanges = 0;
2003-06-16 02:32:13 +08:00
}
if (change_list) {
ngx_free(change_list);
}
ngx_test_null(change_list,
2003-07-04 23:10:33 +08:00
ngx_alloc(kcf->changes * sizeof(struct kevent),
cycle->log),
2003-06-16 02:32:13 +08:00
NGX_ERROR);
}
2003-05-20 00:39:14 +08:00
max_changes = kcf->changes;
2003-06-16 02:32:13 +08:00
if (nevents < kcf->events) {
if (event_list) {
ngx_free(event_list);
}
2002-09-13 22:47:42 +08:00
2003-06-16 02:32:13 +08:00
ngx_test_null(event_list,
2003-07-04 23:10:33 +08:00
ngx_alloc(kcf->events * sizeof(struct kevent),
cycle->log),
2003-06-16 02:32:13 +08:00
NGX_ERROR);
2002-08-26 23:18:19 +08:00
}
2003-06-16 02:32:13 +08:00
nevents = kcf->events;
2003-07-04 23:10:33 +08:00
if (ngx_event_timer_init(cycle) == NGX_ERROR) {
2003-01-24 02:47:54 +08:00
return NGX_ERROR;
}
2003-07-04 23:10:33 +08:00
ngx_io = ngx_os_io;
2003-06-16 02:32:13 +08:00
2003-05-20 00:39:14 +08:00
ngx_event_actions = ngx_kqueue_module_ctx.actions;
2003-02-11 15:14:40 +08:00
2003-10-13 00:49:16 +08:00
ngx_event_flags = NGX_USE_ONESHOT_EVENT
2003-02-11 15:14:40 +08:00
#if (HAVE_CLEAR_EVENT)
2003-10-13 00:49:16 +08:00
|NGX_USE_CLEAR_EVENT
2003-03-12 04:38:13 +08:00
#else
|NGX_USE_LEVEL_EVENT
2003-03-04 14:33:48 +08:00
#endif
#if (HAVE_LOWAT_EVENT)
|NGX_HAVE_LOWAT_EVENT
2003-02-07 01:21:13 +08:00
#endif
2003-02-11 15:14:40 +08:00
|NGX_HAVE_KQUEUE_EVENT;
2002-09-27 23:05:29 +08:00
return NGX_OK;
}
2002-12-15 14:25:09 +08:00
2003-07-04 23:10:33 +08:00
static void ngx_kqueue_done(ngx_cycle_t *cycle)
2003-03-04 14:33:48 +08:00
{
2003-05-20 00:39:14 +08:00
if (close(ngx_kqueue) == -1) {
2003-07-04 23:10:33 +08:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"kqueue close() failed");
2003-03-04 14:33:48 +08:00
}
2003-05-20 00:39:14 +08:00
2003-06-16 02:32:13 +08:00
ngx_kqueue = -1;
2003-07-04 23:10:33 +08:00
ngx_event_timer_done(cycle);
2003-05-20 00:39:14 +08:00
ngx_free(change_list);
ngx_free(event_list);
2003-06-16 02:32:13 +08:00
change_list = NULL;
event_list = NULL;
max_changes = 0;
nchanges = 0;
nevents = 0;
2003-03-04 14:33:48 +08:00
}
2003-05-20 00:39:14 +08:00
static int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags)
{
2003-05-20 00:39:14 +08:00
ngx_connection_t *c;
2002-12-25 01:30:59 +08:00
ev->active = 1;
2003-05-20 23:37:55 +08:00
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
2002-09-12 22:42:29 +08:00
2002-12-23 14:29:22 +08:00
if (nchanges > 0
&& ev->index < nchanges
2003-05-20 00:39:14 +08:00
&& (void *) ((uintptr_t) change_list[ev->index].udata & ~1) == ev)
2002-12-23 14:29:22 +08:00
{
2003-05-20 00:39:14 +08:00
c = ev->data;
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
"previous event were not passed in kernel", c->fd);
2003-03-21 00:09:44 +08:00
2003-05-20 00:39:14 +08:00
return NGX_ERROR;
2002-12-23 14:29:22 +08:00
}
return ngx_kqueue_set_event(ev, event, EV_ADD | flags);
}
2002-12-15 14:25:09 +08:00
2003-05-20 00:39:14 +08:00
static int ngx_kqueue_del_event(ngx_event_t *ev, int event, u_int flags)
{
2002-12-25 01:30:59 +08:00
ngx_event_t *e;
ev->active = 0;
2002-10-05 01:58:04 +08:00
2002-12-23 14:29:22 +08:00
if (nchanges > 0
&& ev->index < nchanges
2003-05-12 23:52:24 +08:00
&& (void *) ((uintptr_t) change_list[ev->index].udata & ~1) == ev)
2002-12-19 15:08:55 +08:00
{
2002-12-25 01:30:59 +08:00
#if (NGX_DEBUG_EVENT)
2002-12-23 14:29:22 +08:00
ngx_connection_t *c = (ngx_connection_t *) ev->data;
2003-05-20 00:39:14 +08:00
ngx_log_debug(ev->log, "kqueue event deleted: %d: ft:%d" _
c->fd _ event);
2002-12-25 01:30:59 +08:00
#endif
2003-03-21 00:09:44 +08:00
/* if the event is still not passed to a kernel we will not pass it */
2002-12-15 14:25:09 +08:00
if (ev->index < --nchanges) {
e = (ngx_event_t *) change_list[nchanges].udata;
change_list[ev->index] = change_list[nchanges];
e->index = ev->index;
}
2002-10-05 01:58:04 +08:00
return NGX_OK;
}
2003-10-13 00:49:16 +08:00
/*
* when the file descriptor is closed a kqueue automatically deletes
* its filters so we do not need to delete explicity the event
* before the closing the file descriptor.
*/
2003-03-21 00:09:44 +08:00
2002-12-25 01:30:59 +08:00
if (flags & NGX_CLOSE_EVENT) {
2002-12-15 14:25:09 +08:00
return NGX_OK;
2002-12-25 01:30:59 +08:00
}
2002-12-15 14:25:09 +08:00
return ngx_kqueue_set_event(ev, event, EV_DELETE);
}
2002-12-15 14:25:09 +08:00
2003-05-20 00:39:14 +08:00
static int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags)
{
2003-06-16 02:32:13 +08:00
struct timespec ts;
ngx_connection_t *c;
2002-12-19 15:08:55 +08:00
2003-05-20 00:39:14 +08:00
c = ev->data;
2002-12-25 01:30:59 +08:00
#if (NGX_DEBUG_EVENT)
2003-06-06 22:59:20 +08:00
ngx_log_debug(ev->log, "kqueue set event: %d: ft:%d fl:%08x" _
2002-12-23 14:29:22 +08:00
c->fd _ filter _ flags);
2002-12-25 01:30:59 +08:00
#endif
2003-05-20 00:39:14 +08:00
if (nchanges >= max_changes) {
ngx_log_error(NGX_LOG_WARN, ev->log, 0,
2002-09-27 23:05:29 +08:00
"kqueue change list is filled up");
2002-12-19 15:08:55 +08:00
ts.tv_sec = 0;
ts.tv_nsec = 0;
2003-05-20 00:39:14 +08:00
if (kevent(ngx_kqueue, change_list, nchanges, NULL, 0, &ts) == -1) {
2003-06-16 02:32:13 +08:00
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, "kevent() failed");
2002-09-13 22:47:42 +08:00
return NGX_ERROR;
}
2002-12-25 01:30:59 +08:00
nchanges = 0;
}
2002-12-23 14:29:22 +08:00
change_list[nchanges].ident = c->fd;
change_list[nchanges].filter = filter;
change_list[nchanges].flags = flags;
2003-05-12 23:52:24 +08:00
change_list[nchanges].udata = (void *) ((uintptr_t) ev | ev->instance);
2003-03-04 14:33:48 +08:00
#if (HAVE_LOWAT_EVENT)
if ((flags & EV_ADD) && ev->lowat > 0) {
change_list[nchanges].fflags = NOTE_LOWAT;
change_list[nchanges].data = ev->lowat;
} else {
change_list[nchanges].fflags = 0;
change_list[nchanges].data = 0;
}
#else
change_list[nchanges].fflags = 0;
change_list[nchanges].data = 0;
2003-03-04 14:33:48 +08:00
#endif
2002-10-05 01:58:04 +08:00
2002-12-24 15:09:57 +08:00
ev->index = nchanges;
2002-10-05 01:58:04 +08:00
nchanges++;
2002-09-13 22:47:42 +08:00
return NGX_OK;
}
2002-12-15 14:25:09 +08:00
2003-05-20 00:39:14 +08:00
static int ngx_kqueue_process_events(ngx_log_t *log)
{
2003-05-12 23:52:24 +08:00
int events, instance, i;
2003-07-07 14:11:50 +08:00
ngx_err_t err;
2003-01-27 05:08:14 +08:00
ngx_msec_t timer, delta;
2002-09-18 01:49:32 +08:00
ngx_event_t *ev;
struct timeval tv;
2002-12-15 14:25:09 +08:00
struct timespec ts, *tp;
2003-01-24 14:20:47 +08:00
timer = ngx_event_find_timer();
if (timer) {
ts.tv_sec = timer / 1000;
ts.tv_nsec = (timer % 1000) * 1000000;
tp = &ts;
gettimeofday(&tv, NULL);
delta = tv.tv_sec * 1000 + tv.tv_usec / 1000;
} else {
timer = 0;
delta = 0;
tp = NULL;
}
2002-12-25 01:30:59 +08:00
#if (NGX_DEBUG_EVENT)
2002-09-13 22:47:42 +08:00
ngx_log_debug(log, "kevent timer: %d" _ timer);
2002-12-25 01:30:59 +08:00
#endif
2003-05-20 00:39:14 +08:00
events = kevent(ngx_kqueue, change_list, nchanges, event_list, nevents, tp);
2002-12-19 15:08:55 +08:00
2002-09-13 22:47:42 +08:00
if (events == -1) {
2003-07-07 14:11:50 +08:00
err = ngx_errno;
} else {
err = 0;
}
nchanges = 0;
if (timer) {
gettimeofday(&tv, NULL);
delta = tv.tv_sec * 1000 + tv.tv_usec / 1000 - delta;
2003-07-07 14:11:50 +08:00
#if (NGX_DEBUG_EVENT)
ngx_log_debug(log, "kevent timer: %d, delta: %d" _ timer _ delta);
#endif
2003-05-20 00:39:14 +08:00
/* The expired timers must be handled before a processing of the events
because the new timers can be added during a processing */
2003-03-21 00:09:44 +08:00
ngx_event_expire_timers(delta);
} else {
2002-12-25 01:30:59 +08:00
if (events == 0) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
2003-06-16 02:32:13 +08:00
"kevent() returned no events without timeout");
2002-12-25 01:30:59 +08:00
return NGX_ERROR;
}
2002-12-25 01:30:59 +08:00
#if (NGX_DEBUG_EVENT)
2003-07-07 14:11:50 +08:00
ngx_log_debug(log, "kevent timer: %d, delta: %d" _ timer _ delta);
2002-12-25 01:30:59 +08:00
#endif
2003-07-07 14:11:50 +08:00
}
if (err) {
ngx_log_error(NGX_LOG_ALERT, log, err, "kevent() failed");
return NGX_ERROR;
}
for (i = 0; i < events; i++) {
2002-12-25 01:30:59 +08:00
#if (NGX_DEBUG_EVENT)
2003-02-07 01:21:13 +08:00
if (event_list[i].ident > 0x8000000) {
ngx_log_debug(log,
2003-06-06 22:59:20 +08:00
"kevent: %08x: ft:%d fl:%08x ff:%08x d:%d ud:%08x" _
2003-02-07 01:21:13 +08:00
event_list[i].ident _ event_list[i].filter _
event_list[i].flags _ event_list[i].fflags _
event_list[i].data _ event_list[i].udata);
} else {
ngx_log_debug(log,
2003-06-06 22:59:20 +08:00
"kevent: %d: ft:%d fl:%08x ff:%08x d:%d ud:%08x" _
2003-02-07 01:21:13 +08:00
event_list[i].ident _ event_list[i].filter _
event_list[i].flags _ event_list[i].fflags _
event_list[i].data _ event_list[i].udata);
}
2002-12-25 01:30:59 +08:00
#endif
if (event_list[i].flags & EV_ERROR) {
ngx_log_error(NGX_LOG_ALERT, log, event_list[i].data,
2003-06-16 02:32:13 +08:00
"kevent() error on %d", event_list[i].ident);
continue;
}
ev = (ngx_event_t *) event_list[i].udata;
2002-12-25 01:30:59 +08:00
switch (event_list[i].filter) {
case EVFILT_READ:
case EVFILT_WRITE:
2003-03-21 00:09:44 +08:00
2003-05-21 21:28:21 +08:00
instance = (uintptr_t) ev & 1;
ev = (void *) ((uintptr_t) ev & ~1);
/* It's a stale event from a file descriptor
that was just closed in this iteration */
if (ev->active == 0 || ev->instance != instance) {
ngx_log_debug(log, "stale kevent");
continue;
}
ev->available = event_list[i].data;
if (event_list[i].flags & EV_EOF) {
ev->eof = 1;
ev->error = event_list[i].fflags;
}
2003-05-20 00:39:14 +08:00
if (ev->oneshot && ev->timer_set) {
2002-09-12 22:42:29 +08:00
ngx_del_timer(ev);
2002-12-25 01:30:59 +08:00
}
2002-09-12 22:42:29 +08:00
2003-10-13 00:49:16 +08:00
ev->ready = 1;
ev->event_handler(ev);
break;
2003-02-07 01:21:13 +08:00
case EVFILT_AIO:
ev->ready = 1;
2003-10-13 00:49:16 +08:00
ev->active = 0;
2003-02-07 01:21:13 +08:00
2003-05-14 00:02:32 +08:00
ev->event_handler(ev);
break;
2003-02-07 01:21:13 +08:00
default:
2002-12-25 01:30:59 +08:00
ngx_log_error(NGX_LOG_ALERT, log, 0,
"unknown kevent filter %d" _ event_list[i].filter);
}
}
2002-09-13 22:47:42 +08:00
return NGX_OK;
}
2003-05-20 00:39:14 +08:00
2003-07-07 14:11:50 +08:00
static void *ngx_kqueue_create_conf(ngx_cycle_t *cycle)
2003-05-20 00:39:14 +08:00
{
ngx_kqueue_conf_t *kcf;
2003-07-07 14:11:50 +08:00
ngx_test_null(kcf, ngx_palloc(cycle->pool, sizeof(ngx_kqueue_conf_t)),
2003-05-20 00:39:14 +08:00
NGX_CONF_ERROR);
kcf->changes = NGX_CONF_UNSET;
kcf->events = NGX_CONF_UNSET;
return kcf;
}
2003-07-07 14:11:50 +08:00
static char *ngx_kqueue_init_conf(ngx_cycle_t *cycle, void *conf)
2003-05-20 00:39:14 +08:00
{
ngx_kqueue_conf_t *kcf = conf;
2003-06-16 02:32:13 +08:00
ngx_conf_init_unsigned_value(kcf->changes, 512);
ngx_conf_init_unsigned_value(kcf->events, 512);
2003-05-20 00:39:14 +08:00
return NGX_CONF_OK;
}