From 0def80f1a5c8f7a02b92c823e5c71f9f746c3e6b Mon Sep 17 00:00:00 2001 From: Hans Petter Selasky Date: Mon, 3 Oct 2022 10:54:40 +0200 Subject: [PATCH] time(3): Align fast clock times to avoid firing multiple timers. In non-periodic mode absolute timers fire at exactly the time given. When specifying a fast clock, align the firing time so that less timer interrupt events are needed. Reviewed by: rrs @ Differential Revision: https://reviews.freebsd.org/D36858 MFC after: 1 week Sponsored by: NVIDIA Networking --- sys/kern/kern_umtx.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c index 6b4e3ca38d03..75ce1dc43f81 100644 --- a/sys/kern/kern_umtx.c +++ b/sys/kern/kern_umtx.c @@ -706,6 +706,7 @@ umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, { struct bintime bt, bbt; struct timespec tts; + sbintime_t rem; switch (timo->clockid) { @@ -738,14 +739,24 @@ umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, return (0); } *sbt = bttosbt(bt); + + /* + * Check if the absolute time should be aligned to + * avoid firing multiple timer events in non-periodic + * timer mode. + */ switch (timo->clockid) { case CLOCK_REALTIME_FAST: case CLOCK_MONOTONIC_FAST: case CLOCK_UPTIME_FAST: - *sbt += tc_tick_sbt; + rem = *sbt % tc_tick_sbt; + if (__predict_true(rem != 0)) + *sbt += tc_tick_sbt - rem; break; case CLOCK_SECOND: - *sbt += SBT_1S; + rem = *sbt % SBT_1S; + if (__predict_true(rem != 0)) + *sbt += SBT_1S - rem; break; } *flags = C_ABSOLUTE;