From cabad8a10a452bf2667bb516a523851d5a82cd88 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Thu, 2 Nov 2023 16:46:27 -0400 Subject: [PATCH] lib: Modify event system to treat fd access more fairly Keep track of the last starting spot of where fd's were being handled for read operations. Modify the io read handler to cycle through the list of fd's that need to be handled such that fd's at the front do not take precedence for being handled all the time. Signed-off-by: Donald Sharp --- lib/event.c | 15 ++++++++++++--- lib/frrevent.h | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/event.c b/lib/event.c index a3f278604461..c6a469835b20 100644 --- a/lib/event.c +++ b/lib/event.c @@ -1654,6 +1654,10 @@ static inline void thread_process_io_inner_loop(struct event_loop *m, * Walks through file descriptor array looking for those pollfds whose .revents * field has something interesting. Deletes any invalid file descriptors. * + * Try to impart some impartiality to handling of io. The event + * system will cycle through the fd's available for io + * giving each one a chance to go first. + * * @param m the thread master * @param num the number of active file descriptors (return value of poll()) */ @@ -1661,10 +1665,15 @@ static void thread_process_io(struct event_loop *m, unsigned int num) { unsigned int ready = 0; struct pollfd *pfds = m->handler.copy; + nfds_t last_read = m->last_read % m->handler.copycount; - for (nfds_t i = 0; i < m->handler.copycount && ready < num; ++i) { - thread_process_io_inner_loop(m, num, pfds, &i, &ready); - } + for (nfds_t i = last_read; i < m->handler.copycount && ready < num; ++i) + thread_process_io_inner_loop(m, num, pfds, &i, &ready); + + for (nfds_t i = 0; i < last_read && ready < num; ++i) + thread_process_io_inner_loop(m, num, pfds, &i, &ready); + + m->last_read++; } /* Add all timers that have popped to the ready list. */ diff --git a/lib/frrevent.h b/lib/frrevent.h index 3f74df359bc2..616fe131af5c 100644 --- a/lib/frrevent.h +++ b/lib/frrevent.h @@ -91,6 +91,8 @@ struct event_loop { pthread_mutex_t mtx; pthread_t owner; + nfds_t last_read; + bool ready_run_loop; RUSAGE_T last_getrusage; };