From: Guus Sliepen Date: Fri, 27 Mar 2020 21:52:46 +0000 (+0100) Subject: Reduce how often we have to poll the packet queue. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=57f6341e91373198ad2a4099015f2988795d8a3f;p=meshlink Reduce how often we have to poll the packet queue. Packets are moved to the MeshLink thread via the packet queue. However, each packet required a trigger byte to be sent to the event loop, requiring more calls to select() than necessary. Now we make event loop signals level triggered, and dequeue all enqueued packets at once. This also adds debug log statements for the packet queue. --- diff --git a/src/event.c b/src/event.c index af1b0d60..11f1ed81 100644 --- a/src/event.c +++ b/src/event.c @@ -171,6 +171,7 @@ static void signalio_handler(event_loop_t *loop, void *data, int flags) { }); if(sig) { + sig->set = false; sig->cb(loop, sig->data); } } @@ -195,7 +196,12 @@ static void pipe_exit(event_loop_t *loop) { } void signal_trigger(event_loop_t *loop, signal_t *sig) { + if(sig->set) { + return; + } + uint8_t signum = sig->signum; + sig->set = true; write(loop->pipefd[1], &signum, 1); return; } @@ -206,6 +212,7 @@ void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, u sig->cb = cb; sig->data = data; sig->signum = signum; + sig->set = false; sig->node.data = sig; if(loop->pipefd[0] == -1) { diff --git a/src/event.h b/src/event.h index 8a61e81b..f2ebf498 100644 --- a/src/event.h +++ b/src/event.h @@ -52,6 +52,7 @@ typedef struct timeout_t { typedef struct signal_t { struct splay_node_t node; int signum; + bool set; signal_cb_t cb; void *data; } signal_t; diff --git a/src/meshlink.c b/src/meshlink.c index 8513c8f9..e1cf7979 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2008,6 +2008,8 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const return false; } + logger(mesh, MESHLINK_DEBUG, "Adding packet of %zu bytes to packet queue", len); + // Notify event loop signal_trigger(&mesh->loop, &mesh->datafromapp); @@ -2017,17 +2019,16 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void meshlink_send_from_queue(event_loop_t *loop, void *data) { (void)loop; meshlink_handle_t *mesh = data; - vpn_packet_t *packet = meshlink_queue_pop(&mesh->outpacketqueue); - - if(!packet) { - return; - } - mesh->self->in_packets++; - mesh->self->in_bytes += packet->len; - route(mesh, mesh->self, packet); + logger(mesh, MESHLINK_DEBUG, "Flushing the packet queue"); - free(packet); + for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) { + logger(mesh, MESHLINK_DEBUG, "Removing packet of %d bytes from packet queue", packet->len); + mesh->self->in_packets++; + mesh->self->in_bytes += packet->len; + route(mesh, mesh->self, packet); + free(packet); + } } ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) {