From f793845f4af346159878b4c74e40537a5fedac55 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 8 Sep 2024 18:10:25 +0000 Subject: [PATCH] MEDIUM: clock: collect the monotonic time in clock_local_update_date() Now we collect this clock in clock_local_update_date(), the closest from the poller, which is also used when busy-polling, and the values is set into the thread's curr_mono_time which did not exist before. Later, clock_leaving_poll() just sets the prev_mono_time value from the curr_ one instead of retrieving the time at this specific point. It also means that the monotonic time will now also cover the time needed to update the global time, which should be negligible. Note that we don't collect the CPU time in the clock_local_update_date() function even though it's tempting, because when doing busy-polling, it would be collected on each round while being useless. Doing so will make sure that the local time always knows the monotonic time when it is available. --- include/haproxy/tinfo-t.h | 1 + src/clock.c | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/haproxy/tinfo-t.h b/include/haproxy/tinfo-t.h index 636d5b2e5..9aae92a40 100644 --- a/include/haproxy/tinfo-t.h +++ b/include/haproxy/tinfo-t.h @@ -169,6 +169,7 @@ struct thread_ctx { uint64_t prev_cpu_time; /* previous per thread CPU time */ uint64_t prev_mono_time; /* previous system wide monotonic time */ + uint64_t curr_mono_time; /* latest system wide monotonic time */ struct eb_root rqueue_shared; /* run queue fed by other threads */ __decl_thread(HA_SPINLOCK_T rqsh_lock); /* lock protecting the shared runqueue */ diff --git a/src/clock.c b/src/clock.c index 9665dfc0a..75a129bd8 100644 --- a/src/clock.c +++ b/src/clock.c @@ -205,6 +205,8 @@ void clock_update_local_date(int max_wait, int interrupted) gettimeofday(&date, NULL); date_ns = tv_to_ns(&date); + th_ctx->curr_mono_time = now_mono_time(); + /* compute the minimum and maximum local date we may have reached based * on our past date and the associated timeout. There are three possible * extremities: @@ -296,6 +298,7 @@ void clock_update_global_date() void clock_init_process_date(void) { now_offset = 0; + th_ctx->prev_mono_time = th_ctx->curr_mono_time = now_mono_time(); // 0 if not supported gettimeofday(&date, NULL); after_poll = before_poll = date; now_ns = global_now_ns = tv_to_ns(&date); @@ -332,6 +335,8 @@ void clock_init_thread_date(void) now_ns = _HA_ATOMIC_LOAD(&global_now_ns); th_ctx->idle_pct = 100; th_ctx->prev_cpu_time = now_cpu_time(); + th_ctx->prev_mono_time = now_mono_time(); + th_ctx->curr_mono_time = th_ctx->prev_mono_time; clock_update_date(0, 1); } @@ -391,7 +396,7 @@ void clock_leaving_poll(int timeout, int interrupted) { clock_measure_idle(); th_ctx->prev_cpu_time = now_cpu_time(); - th_ctx->prev_mono_time = now_mono_time(); + th_ctx->prev_mono_time = th_ctx->curr_mono_time; } /* Collect date and time information before calling poll(). This will be used