From: Willy Tarreau Date: Wed, 17 Oct 2018 12:31:19 +0000 (+0200) Subject: MINOR: poller: move time and date computation out of the pollers X-Git-Tag: v1.9-dev4~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7e9c4ae4de3e7dc36058763fd70aa0bb28541d0c;p=thirdparty%2Fhaproxy.git MINOR: poller: move time and date computation out of the pollers By placing this code into time.h (tv_entering_poll() and tv_leaving_poll()) we can remove the logic from the pollers and prepare for extending this to offer more accurate time measurements. --- diff --git a/include/common/time.h b/include/common/time.h index f3dd3ba1d8..aeb19860d2 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -543,6 +543,25 @@ static inline void measure_idle() idle_time = samp_time = 0; } +/* Collect date and time information before calling poll(). This will be used + * to count the run time of the past loop and the sleep time of the next poll. + */ +static inline void tv_entering_poll() +{ + gettimeofday(&before_poll, NULL); +} + +/* Collect date and time information after leaving poll(). must be + * set to the maximum sleep time passed to poll (in milliseconds), and + * must be zero if the poller reached the timeout or non-zero + * otherwise, which generally is provided by the poller's return value. + */ +static inline void tv_leaving_poll(int timeout, int interrupted) +{ + tv_update_date(timeout, interrupted); + measure_idle(); +} + #endif /* _COMMON_TIME_H */ /* diff --git a/src/ev_epoll.c b/src/ev_epoll.c index b9980dbed3..9d111ce653 100644 --- a/src/ev_epoll.c +++ b/src/ev_epoll.c @@ -146,10 +146,9 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) /* now let's wait for polled events */ wait_time = compute_poll_timeout(exp); - gettimeofday(&before_poll, NULL); + tv_entering_poll(); status = epoll_wait(epoll_fd[tid], epoll_events, global.tune.maxpollevents, wait_time); - tv_update_date(wait_time, status); - measure_idle(); + tv_leaving_poll(wait_time, status); thread_harmless_end(); diff --git a/src/ev_kqueue.c b/src/ev_kqueue.c index 3d21cb0349..c1c3e11645 100644 --- a/src/ev_kqueue.c +++ b/src/ev_kqueue.c @@ -134,15 +134,14 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) timeout.tv_sec = (delta_ms / 1000); timeout.tv_nsec = (delta_ms % 1000) * 1000000; fd = global.tune.maxpollevents; - gettimeofday(&before_poll, NULL); + tv_entering_poll(); status = kevent(kqueue_fd[tid], // int kq NULL, // const struct kevent *changelist 0, // int nchanges kev, // struct kevent *eventlist fd, // int nevents &timeout); // const struct timespec *timeout - tv_update_date(delta_ms, status); - measure_idle(); + tv_leaving_poll(delta_ms, status); thread_harmless_end(); diff --git a/src/ev_poll.c b/src/ev_poll.c index 0b51e8d01a..b08274ccce 100644 --- a/src/ev_poll.c +++ b/src/ev_poll.c @@ -194,10 +194,9 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) /* now let's wait for events */ wait_time = compute_poll_timeout(exp); - gettimeofday(&before_poll, NULL); + tv_entering_poll(); status = poll(poll_events, nbfd, wait_time); - tv_update_date(wait_time, status); - measure_idle(); + tv_leaving_poll(wait_time, status); thread_harmless_end(); diff --git a/src/ev_select.c b/src/ev_select.c index 0f9b87ee31..464635b0f4 100644 --- a/src/ev_select.c +++ b/src/ev_select.c @@ -164,15 +164,13 @@ REGPRM2 static void _do_poll(struct poller *p, int exp) delta_ms = compute_poll_timeout(exp); delta.tv_sec = (delta_ms / 1000); delta.tv_usec = (delta_ms % 1000) * 1000; - gettimeofday(&before_poll, NULL); + tv_entering_poll(); status = select(maxfd, readnotnull ? tmp_evts[DIR_RD] : NULL, writenotnull ? tmp_evts[DIR_WR] : NULL, NULL, &delta); - - tv_update_date(delta_ms, status); - measure_idle(); + tv_leaving_poll(delta_ms, status); thread_harmless_end();