From 29fdcf0c714b52f22ce3f84d5b3b7da90e06f36f Mon Sep 17 00:00:00 2001 From: "W.C.A. Wijngaards" Date: Thu, 23 Jan 2020 11:44:32 +0100 Subject: [PATCH] loop in output callback for performance. --- dnstap/dtstream.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/dnstap/dtstream.c b/dnstap/dtstream.c index 963d4769f..164b56927 100644 --- a/dnstap/dtstream.c +++ b/dnstap/dtstream.c @@ -50,6 +50,9 @@ #include #endif +/** number of messages to process in one output callback */ +#define DTIO_MESSAGES_PER_CALLBACK 100 + void* fstrm_create_control_frame_start(char* contenttype, size_t* len) { uint32_t* control; @@ -591,30 +594,40 @@ static int dtio_check_close(struct dt_io_thread* dtio) static void dtio_output_cb(int ATTR_UNUSED(fd), short bits, void* arg) { struct dt_io_thread* dtio = (struct dt_io_thread*)arg; + int i; if((bits&UB_EV_READ)) { if(!dtio_check_close(dtio)) return; } - /* see if there are messages that need writing */ - if(!dtio->cur_msg) { - if(!dtio_find_msg(dtio)) - return; /* nothing to do */ - } + /* loop to process a number of messages. This improves throughput, + * because selecting on write-event if not needed for busy messages + * (dnstap log) generation and if they need to all be written back. + * The write event is usually not blocked up. But not forever, + * because the event loop needs to stay responsive for other events. + * If there are no (more) messages, or if the output buffers get + * full, it returns out of the loop. */ + for(i=0; icur_msg) { + if(!dtio_find_msg(dtio)) + return; /* nothing to do */ + } - /* write it */ - if(dtio->cur_msg_done < dtio->cur_msg_len) { - if(!dtio_write_more(dtio)) - return; - } + /* write it */ + if(dtio->cur_msg_done < dtio->cur_msg_len) { + if(!dtio_write_more(dtio)) + return; + } - /* done with the current message */ - free(dtio->cur_msg); - dtio->cur_msg = NULL; - dtio->cur_msg_len = 0; - dtio->cur_msg_done = 0; - dtio->cur_msg_len_done = 0; + /* done with the current message */ + free(dtio->cur_msg); + dtio->cur_msg = NULL; + dtio->cur_msg_len = 0; + dtio->cur_msg_done = 0; + dtio->cur_msg_len_done = 0; + } } /** callback for the dnstap commandpipe, to stop the dnstap IO */