From nobody Mon Feb 27 22:12:22 2023 X-Original-To: dev-commits-src-all@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 4PQZVb39v1z3tDYr; Mon, 27 Feb 2023 22:12:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PQZVb0J3gz3Qps; Mon, 27 Feb 2023 22:12:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677535943; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fDWwRlMuf+UkRR2mk0J0xl7Ed9LFp5sr8YsgHAHgLrU=; b=gTSLpGq/HBLOQgYXdZq5+yLikzgbmkUsV/UGmllqqz8ky6UsO9tuHpywWvSXB2cw+hYFzF QRfxptS3Jjau5tDF3pXYNpukfgUnDUx7MWe6FicSbWnigMW1k1GEgINsVAjjkWS0L5AmKn PDwJ3Tbvk20B4FaFy6orn77SalkTTG2EufTrNkwKEQ3ueJSGRUeqTXVkLz1Gnfp9y4dwPi Vt+9uOpCSAx3ihyeTw02AGdrvu4F5BivZQcO/2TaqT9FcAXOunmMfh7QvyftKfukqRn1K9 9lMGbOPVVfdMcPYaw5/e3FyKbInT+PuNFRPSnkqcgkXlK0SE49M3CMHsfr+pTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677535943; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fDWwRlMuf+UkRR2mk0J0xl7Ed9LFp5sr8YsgHAHgLrU=; b=qxa2l+ttJrP+PYRXhAqEi38ssD9VCR7lhVe8kBCUI66Ygx3I7EMdJk7hiphsc4eb5bkcGl Nu5c3eKCReO9A67PPyps1sezcALErK7FDDelYzplseBJ4KqThHnfyAmnTj0MpsX1iNU+9S nrttCeLgwdEkyibl9SMhPsM06NTQqlQ16K7cixfU86HsuGjYd5H3kvDagtkaFrGSrT/Mb1 aWKLNnc0+B5YvbjCBgFX41aq8+GJl/4B5qC2kMt6r5fvzYi+WOWYkUeYIVAhmnISyybxMi QSLo0wDdUVTaHFsSvcrPocclbuGRVnAF7RDntng4SYYzTBRwmZHbBOCx6+hPug== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1677535943; a=rsa-sha256; cv=none; b=N0lw31q2aa3zNcpRahmTYZvYn1Ccn2+I7WsGA20r6eZtcWJOFdcS4L+LTAKjue8Yq9NGTy oe17VRYhTS4cUoq9CEAjzKPrYkbe+krK505HWIqSTZLkErK+PDmibN/emuWRzZWqCwMZzl SZtyeE0VeT5CuqfxqjNd4BtSVUmmFpLBc8vyQwx7QOaGleVyIwrBXa/GJN3xZwU48E6IgU SbnDRe0ConLe8DijAzip+6KJmUPcuVyQqYgnGsYF1F3DpOdqZEMEuTgHZbgpuu9pZBaU9P mQ/TCYwRvgsDUV3rPgI8V0ZhGATHKe1pUK4gmUpwFCeXXo52ZzpETto7A6zCYw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PQZVZ6J2tzjS5; Mon, 27 Feb 2023 22:12:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 31RMCMKq028857; Mon, 27 Feb 2023 22:12:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31RMCMBg028856; Mon, 27 Feb 2023 22:12:22 GMT (envelope-from git) Date: Mon, 27 Feb 2023 22:12:22 GMT Message-Id: <202302272212.31RMCMBg028856@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 1e48d9d336c0 - main - pps: Simplify the nsec calculation in pps_event() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1e48d9d336c042a44edc0f66d35b72655f68f258 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=1e48d9d336c042a44edc0f66d35b72655f68f258 commit 1e48d9d336c042a44edc0f66d35b72655f68f258 Author: Sebastian Huber AuthorDate: 2023-02-27 21:49:09 +0000 Commit: Warner Losh CommitDate: 2023-02-27 22:10:55 +0000 pps: Simplify the nsec calculation in pps_event() Let A be the current calculation of the frequency accumulator (pps_fcount) update in pps_event() scale = (uint64_t)1 << 63; scale /= captc->tc_frequency; scale *= 2; bt.sec = 0; bt.frac = 0; bintime_addx(&bt, scale * tcount); bintime2timespec(&bt, &ts); hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec); and hardpps(..., delta_nsec): u_nsec = delta_nsec; if (u_nsec > (NANOSECOND >> 1)) u_nsec -= NANOSECOND; else if (u_nsec < -(NANOSECOND >> 1)) u_nsec += NANOSECOND; pps_fcount += u_nsec; This change introduces a new calculation which is slightly simpler and more straight forward. Name it B. Consider the following sample values with a tcount of 2000000100 and a tc_frequency of 2000000000 (2GHz). For A, the scale is 9223372036. Then scale * tcount is 18446744994337203600 which is larger than UINT64_MAX (= 18446744073709551615). The result is 920627651984 == 18446744994337203600 % UINT64_MAX. Since all operands are unsigned the result is well defined through modulo arithmetic. The result of bintime2timespec(&bt, &ts) is 49. This is equal to the correct result 1000000049 % NANOSECOND. In hardpps(), both conditional statements are not executed and pps_fcount is incremented by 49. For the new calculation B, we have 1000000000 * tcount is 2000000100000000000 which is less than UINT64_MAX. This yields after the division with tc_frequency the correct result of 1000000050 for delta_nsec. In hardpps(), the first conditional statement is executed and pps_fcount is incremented by 50. This shows that both methods yield roughly the same results. However, method B is easier to understand and requires fewer conditional statements. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/604 --- sys/kern/kern_ntptime.c | 12 +++--------- sys/kern/kern_tc.c | 16 ++++++---------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/sys/kern/kern_ntptime.c b/sys/kern/kern_ntptime.c index b632cecdfadb..815a26182096 100644 --- a/sys/kern/kern_ntptime.c +++ b/sys/kern/kern_ntptime.c @@ -771,16 +771,10 @@ hardpps(struct timespec *tsp, long delta_nsec) pps_tf[0].tv_nsec = u_nsec; /* - * Compute the difference between the current and previous - * counter values. If the difference exceeds 0.5 s, assume it - * has wrapped around, so correct 1.0 s. + * Update the frequency accumulator using the difference between the + * current and previous PPS event measured directly by the timecounter. */ - u_nsec = delta_nsec; - if (u_nsec > (NANOSECOND >> 1)) - u_nsec -= NANOSECOND; - else if (u_nsec < -(NANOSECOND >> 1)) - u_nsec += NANOSECOND; - pps_fcount += u_nsec; + pps_fcount += delta_nsec - NANOSECOND; if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) goto out; time_status &= ~STA_PPSJITTER; diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index be564e4347f8..0dc233896baa 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1781,7 +1781,7 @@ pps_event(struct pps_state *pps, int event) struct timecounter *captc; uint64_t capth_scale; struct bintime bt; - struct timespec ts, *tsp, *osp; + struct timespec *tsp, *osp; u_int tcount, *pcount; int foff; pps_seq_t *pseq; @@ -1881,7 +1881,7 @@ pps_event(struct pps_state *pps, int event) #ifdef PPS_SYNC if (fhard) { - uint64_t scale; + uint64_t delta_nsec; /* * Feed the NTP PLL/FLL. @@ -1891,14 +1891,10 @@ pps_event(struct pps_state *pps, int event) tcount = pps->capcount - pps->ppscount[2]; pps->ppscount[2] = pps->capcount; tcount &= captc->tc_counter_mask; - scale = (uint64_t)1 << 63; - scale /= captc->tc_frequency; - scale *= 2; - bt.sec = 0; - bt.frac = 0; - bintime_addx(&bt, scale * tcount); - bintime2timespec(&bt, &ts); - hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec); + delta_nsec = 1000000000; + delta_nsec *= tcount; + delta_nsec /= captc->tc_frequency; + hardpps(tsp, (long)delta_nsec); } #endif