From 6d887f54d29c571b9c2f28ab7eac04662f46b551 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Vavru=C5=A1a?= Date: Tue, 14 Jul 2015 13:24:46 +0200 Subject: [PATCH] daemon/worker: satisfy subreq sockets from freelist since the structures are ~ same size as I/O requests (udp_send_t being 60B larger than others), they all share the same freelist --- daemon/io.c | 33 ++++++++++----------------------- daemon/io.h | 3 +-- daemon/worker.c | 35 ++++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/daemon/io.c b/daemon/io.c index f43baf7f0..4457f456e 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -23,11 +23,7 @@ static void *handle_alloc(uv_loop_t *loop, size_t size) { - uv_handle_t *handle = malloc(size); - if (handle) { - memset(handle, 0, size); - } - return handle; + return malloc(size); } static void handle_free(uv_handle_t *handle) @@ -127,8 +123,12 @@ static void tcp_accept(uv_stream_t *master, int status) return; } - uv_stream_t *client = (uv_stream_t *)io_create(master->loop, SOCK_STREAM); - if (!client || uv_accept(master, client) != 0) { + uv_stream_t *client = handle_alloc(master->loop, sizeof(*client)); + if (!client) { + return; + } + io_create(master->loop, (uv_handle_t *)client, SOCK_STREAM); + if (uv_accept(master, client) != 0) { handle_free((uv_handle_t *)client); return; } @@ -163,28 +163,15 @@ void tcp_unbind(struct endpoint *ep) uv_close((uv_handle_t *)&ep->tcp, NULL); } -uv_handle_t *io_create(uv_loop_t *loop, int type) +void io_create(uv_loop_t *loop, uv_handle_t *handle, int type) { if (type == SOCK_DGRAM) { - uv_udp_t *handle = handle_alloc(loop, sizeof(*handle)); - if (handle) { - uv_udp_init(loop, handle); - } - return (uv_handle_t *)handle; + uv_udp_init(loop, (uv_udp_t *)handle); } else { - uv_tcp_t *handle = handle_alloc(loop, sizeof(*handle)); - if (handle) { - uv_tcp_init(loop, handle); - } - return (uv_handle_t *)handle; + uv_tcp_init(loop, (uv_tcp_t *)handle); } } -void io_close(uv_handle_t *handle) -{ - uv_close(handle, (uv_close_cb) handle_free); -} - int io_start_read(uv_handle_t *handle) { if (handle->type == UV_UDP) { diff --git a/daemon/io.h b/daemon/io.h index 83e01a068..35e882dc1 100644 --- a/daemon/io.h +++ b/daemon/io.h @@ -24,7 +24,6 @@ int udp_bind(struct endpoint *ep, struct sockaddr *addr); void udp_unbind(struct endpoint *ep); int tcp_bind(struct endpoint *ep, struct sockaddr *addr); void tcp_unbind(struct endpoint *ep); -uv_handle_t *io_create(uv_loop_t *loop, int type); -void io_close(uv_handle_t *handle); +void io_create(uv_loop_t *loop, uv_handle_t *handle, int type); int io_start_read(uv_handle_t *handle); int io_stop_read(uv_handle_t *handle); \ No newline at end of file diff --git a/daemon/worker.c b/daemon/worker.c index 8b446875a..680849e14 100644 --- a/daemon/worker.c +++ b/daemon/worker.c @@ -29,16 +29,18 @@ /* @internal IO request entry. */ struct ioreq { - union { - uv_udp_send_t send; - uv_write_t write; - uv_connect_t connect; - } as; + union { + uv_udp_t udp; + uv_tcp_t tcp; + uv_udp_send_t send; + uv_write_t write; + uv_connect_t connect; + } as; }; static inline struct ioreq *ioreq_take(struct worker_ctx *worker) { - struct ioreq *req = NULL; + struct ioreq *req = NULL; if (worker->ioreqs.len > 0) { req = array_tail(worker->ioreqs); array_pop(worker->ioreqs); @@ -209,18 +211,24 @@ static int qr_task_on_send(struct qr_task *task, int status) return status; } +static void on_close(uv_handle_t *handle) +{ + struct qr_task *task = handle->data; + ioreq_release(task->worker, (struct ioreq *)handle); +} + static void on_send(uv_udp_send_t *req, int status) { struct qr_task *task = req->data; - qr_task_on_send(task, status); - ioreq_release(task->worker, (struct ioreq *)req); + qr_task_on_send(task, status); + ioreq_release(task->worker, (struct ioreq *)req); } static void on_write(uv_write_t *req, int status) { struct qr_task *task = req->data; - qr_task_on_send(task, status); - ioreq_release(task->worker, (struct ioreq *)req); + qr_task_on_send(task, status); + ioreq_release(task->worker, (struct ioreq *)req); } static int qr_task_send(struct qr_task *task, uv_handle_t *handle, struct sockaddr *addr, knot_pkt_t *pkt) @@ -291,7 +299,7 @@ static int qr_task_step(struct qr_task *task, knot_pkt_t *packet) if (task->next_handle) { if (!uv_is_closing(task->next_handle)) { io_stop_read(task->next_handle); - uv_close(task->next_handle, (uv_close_cb) free); + uv_close(task->next_handle, on_close); } uv_timer_stop(&task->timeout); task->next_handle = NULL; @@ -320,12 +328,13 @@ static int qr_task_step(struct qr_task *task, knot_pkt_t *packet) } /* Create connection for iterative query */ - task->next_handle = io_create(task->worker->loop, sock_type); - if (task->next_handle == NULL) { + task->next_handle = (uv_handle_t *)ioreq_take(task->worker); + if (!task->next_handle) { return qr_task_finalize(task, KNOT_STATE_FAIL); } /* Connect or issue query datagram */ + io_create(task->worker->loop, task->next_handle, sock_type); task->next_handle->data = task; if (sock_type == SOCK_STREAM) { struct ioreq *req = ioreq_take(task->worker); -- 2.47.2