git: 0def80f1a5c8 - main - time(3): Align fast clock times to avoid firing multiple timers.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Mon, 03 Oct 2022 15:53:55 UTC
The branch main has been updated by hselasky:

URL: https://cgit.FreeBSD.org/src/commit/?id=0def80f1a5c8f7a02b92c823e5c71f9f746c3e6b

commit 0def80f1a5c8f7a02b92c823e5c71f9f746c3e6b
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-10-03 08:54:40 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-10-03 15:53:17 +0000

    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;