From nobody Thu Oct 28 08:22:58 2021 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id C90AD182B46D for ; Thu, 28 Oct 2021 08:23:10 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4Hfz8Z0KJBz4p8K for ; Thu, 28 Oct 2021 08:23:09 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from sslproxy03.your-server.de ([88.198.220.132]) by dedi548.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1mg0gg-0001Bv-Bt for freebsd-hackers@freebsd.org; Thu, 28 Oct 2021 10:23:02 +0200 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mg0gg-0009sM-98 for freebsd-hackers@freebsd.org; Thu, 28 Oct 2021 10:23:02 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id EC9E0480097 for ; Thu, 28 Oct 2021 10:23:01 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id n_s5JAjZQMeS for ; Thu, 28 Oct 2021 10:23:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 895C148009E for ; Thu, 28 Oct 2021 10:23:01 +0200 (CEST) X-Virus-Scanned: amavisd-new at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id tBIo7bsTMedr for ; Thu, 28 Oct 2021 10:23:01 +0200 (CEST) Received: from zimbra.eb.localhost (unknown [192.168.96.242]) by mail.embedded-brains.de (Postfix) with ESMTPSA id 6B06C480097 for ; Thu, 28 Oct 2021 10:23:01 +0200 (CEST) From: Sebastian Huber To: freebsd-hackers@freebsd.org Subject: [PATCH] kern_tc.c: Scaling/large delta recalculation Date: Thu, 28 Oct 2021 10:22:58 +0200 Message-Id: <20211028082258.82847-1-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 2.26.2 List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.103.3/26335/Wed Oct 27 10:28:55 2021) X-Rspamd-Queue-Id: 4Hfz8Z0KJBz4p8K X-Spamd-Bar: ++ Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of sebastian.huber@embedded-brains.de designates 85.10.215.148 as permitted sender) smtp.mailfrom=sebastian.huber@embedded-brains.de X-Spamd-Result: default: False [2.20 / 15.00]; RCVD_TLS_LAST(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; NEURAL_SPAM_SHORT(1.00)[1.000]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-1.00)[-1.000]; DMARC_NA(0.00)[embedded-brains.de]; R_SPF_ALLOW(-0.20)[+ip4:85.10.215.148]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:85.10.192.0/18, country:DE]; RCVD_COUNT_SEVEN(0.00)[8]; HAS_X_AS(0.00)[] X-ThisMailContainsUnwantedMimeParts: N This change is a slight performance optimization for systems with a slow = 64-bit division. The th->th_scale and th->th_large_delta values only depend on the timecou= nter frequency and the th->th_adjustment. The timecounter frequency of a time= hand only changes when a new timecounter is activated for the timehand. The th->th_adjustment is only changed by the NTP second update. The NTP seco= nd update is not done for every call of tc_windup(). Move the code block to recalculate the scaling factor and the large delta= of a timehand to the new helper function recalculate_scaling_factor_and_large_delta(). Call recalculate_scaling_factor_and_large_delta() when a new timecounter = is activated and a NTP second update occurred. --- sys/kern/kern_tc.c | 88 ++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index 467e5cf8ee3a..c7309f487dc3 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1274,6 +1274,40 @@ tc_setclock(struct timespec *ts) } } =20 +/* + * Recalculate the scaling factor. We want the number of 1/2^64 + * fractions of a second per period of the hardware counter, taking + * into account the th_adjustment factor which the NTP PLL/adjtime(2) + * processing provides us with. + * + * The th_adjustment is nanoseconds per second with 32 bit binary + * fraction and we want 64 bit binary fraction of second: + * + * x =3D a * 2^32 / 10^9 =3D a * 4.294967296 + * + * The range of th_adjustment is +/- 5000PPM so inside a 64bit int + * we can only multiply by about 850 without overflowing, that + * leaves no suitably precise fractions for multiply before divide. + * + * Divide before multiply with a fraction of 2199/512 results in a + * systematic undercompensation of 10PPM of th_adjustment. On a + * 5000PPM adjustment this is a 0.05PPM error. This is acceptable. + * + * We happily sacrifice the lowest of the 64 bits of our result + * to the goddess of code clarity. + */ +static void +recalculate_scaling_factor_and_large_delta(struct timehands *th) +{ + uint64_t scale; + + scale =3D (uint64_t)1 << 63; + scale +=3D (th->th_adjustment / 1024) * 2199; + scale /=3D th->th_counter->tc_frequency; + th->th_scale =3D scale * 2; + th->th_large_delta =3D MIN(((uint64_t)1 << 63) / scale, UINT_MAX); +} + /* * Initialize the next struct timehands in the ring and make * it the active timehands. Along the way we might switch to a differen= t @@ -1284,7 +1318,6 @@ tc_windup(struct bintime *new_boottimebin) { struct bintime bt; struct timehands *th, *tho; - uint64_t scale; u_int delta, ncount, ogen; int i; time_t t; @@ -1346,7 +1379,7 @@ tc_windup(struct bintime *new_boottimebin) tho->th_counter->tc_poll_pps(tho->th_counter); =20 /* - * Deal with NTP second processing. The for loop normally + * Deal with NTP second processing. The loop normally * iterates at most once, but in extreme situations it might * keep NTP sane if timeouts are not run for several seconds. * At boot, the time step can be large when the TOD hardware @@ -1357,14 +1390,21 @@ tc_windup(struct bintime *new_boottimebin) bt =3D th->th_offset; bintime_add(&bt, &th->th_boottime); i =3D bt.sec - tho->th_microtime.tv_sec; - if (i > LARGE_STEP) - i =3D 2; - for (; i > 0; i--) { - t =3D bt.sec; - ntp_update_second(&th->th_adjustment, &bt.sec); - if (bt.sec !=3D t) - th->th_boottime.sec +=3D bt.sec - t; + if (i > 0) { + if (i > LARGE_STEP) + i =3D 2; + + do { + t =3D bt.sec; + ntp_update_second(&th->th_adjustment, &bt.sec); + if (bt.sec !=3D t) + th->th_boottime.sec +=3D bt.sec - t; + --i; + } while (i > 0); + + recalculate_scaling_factor_and_large_delta(th); } + /* Update the UTC timestamps used by the get*() functions. */ th->th_bintime =3D bt; bintime2timeval(&bt, &th->th_microtime); @@ -1382,40 +1422,12 @@ tc_windup(struct bintime *new_boottimebin) th->th_offset_count =3D ncount; tc_min_ticktock_freq =3D max(1, timecounter->tc_frequency / (((uint64_t)timecounter->tc_counter_mask + 1) / 3)); + recalculate_scaling_factor_and_large_delta(th); #ifdef FFCLOCK ffclock_change_tc(th); #endif } =20 - /*- - * Recalculate the scaling factor. We want the number of 1/2^64 - * fractions of a second per period of the hardware counter, taking - * into account the th_adjustment factor which the NTP PLL/adjtime(2) - * processing provides us with. - * - * The th_adjustment is nanoseconds per second with 32 bit binary - * fraction and we want 64 bit binary fraction of second: - * - * x =3D a * 2^32 / 10^9 =3D a * 4.294967296 - * - * The range of th_adjustment is +/- 5000PPM so inside a 64bit int - * we can only multiply by about 850 without overflowing, that - * leaves no suitably precise fractions for multiply before divide. - * - * Divide before multiply with a fraction of 2199/512 results in a - * systematic undercompensation of 10PPM of th_adjustment. On a - * 5000PPM adjustment this is a 0.05PPM error. This is acceptable. - * - * We happily sacrifice the lowest of the 64 bits of our result - * to the goddess of code clarity. - * - */ - scale =3D (uint64_t)1 << 63; - scale +=3D (th->th_adjustment / 1024) * 2199; - scale /=3D th->th_counter->tc_frequency; - th->th_scale =3D scale * 2; - th->th_large_delta =3D MIN(((uint64_t)1 << 63) / scale, UINT_MAX); - /* * Now that the struct timehands is again consistent, set the new * generation number, making sure to not make it zero. --=20 2.26.2