]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: clock: don't compute before_poll when using monotonic clock
authorWilly Tarreau <w@1wt.eu>
Sun, 8 Sep 2024 19:03:30 +0000 (19:03 +0000)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Sep 2024 07:08:10 +0000 (09:08 +0200)
There's no point keeping both clocks up to date; if the monotonic clock
is ticking, let's just refrain from updating the wall clock one before
polling since we won't use it. We still do it after polling however as
we need a wall clock time to communicate with outside.

This saves one gettimeofday() call per loop and two timeval comparisons.

src/clock.c

index ab266632d9bde248722c1e1e171317b392e2a1c5..f230a56f1b7e90ad3f4fc4eee7a41485a7e343a8 100644 (file)
@@ -442,8 +442,6 @@ void clock_entering_poll(void)
        uint32_t run_time;
        int64_t stolen;
 
-       gettimeofday(&before_poll, NULL);
-
        new_cpu_time   = now_cpu_time();
        new_mono_time  = now_mono_time();
 
@@ -461,15 +459,18 @@ void clock_entering_poll(void)
         * too much wrong during such jumps.
         */
 
-       if (unlikely(__tv_islt(&before_poll, &after_poll)))
-               before_poll = after_poll;
-       else if (unlikely(__tv_ms_elapsed(&after_poll, &before_poll) >= 2000))
-               tv_ms_add(&before_poll, &after_poll, 2000);
-
        if (before_poll_mono_ns)
                run_time = (before_poll_mono_ns - th_ctx->curr_mono_time) / 1000ull;
-       else
+       else {
+               gettimeofday(&before_poll, NULL);
+
+               if (unlikely(__tv_islt(&before_poll, &after_poll)))
+                       before_poll = after_poll;
+               else if (unlikely(__tv_ms_elapsed(&after_poll, &before_poll) >= 2000))
+                       tv_ms_add(&before_poll, &after_poll, 2000);
+
                run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec);
+       }
 
        if (th_ctx->prev_cpu_time && th_ctx->prev_mono_time) {
                new_cpu_time  -= th_ctx->prev_cpu_time;