From nobody Wed Jun 1 07:08:13 2022 X-Original-To: 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 3F71E1B46A1E for ; Wed, 1 Jun 2022 07:08:25 +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 4LCgGb5FLlz3HnH for ; Wed, 1 Jun 2022 07:08:23 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from sslproxy06.your-server.de ([78.46.172.3]) by dedi548.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nwISm-000Pud-8C for hackers@freebsd.org; Wed, 01 Jun 2022 09:08:16 +0200 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy06.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwISm-000R8P-5G for hackers@freebsd.org; Wed, 01 Jun 2022 09:08:16 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id CA97E4800EA for ; Wed, 1 Jun 2022 09:08:15 +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 5ggYJShMKI8c for ; Wed, 1 Jun 2022 09:08:15 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 69F30480149 for ; Wed, 1 Jun 2022 09:08:15 +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 D-eMrDxHZU1C for ; Wed, 1 Jun 2022 09:08:15 +0200 (CEST) Received: from [10.10.171.14] (unknown [10.10.171.14]) by mail.embedded-brains.de (Postfix) with ESMTPSA id CFDD04800EA for ; Wed, 1 Jun 2022 09:08:14 +0200 (CEST) Message-ID: <5b8310db-c94b-709f-8c57-bec2d413a80f@embedded-brains.de> Date: Wed, 1 Jun 2022 09:08:13 +0200 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 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 To: hackers@freebsd.org Content-Language: en-US From: Sebastian Huber Subject: pps_capture() and pps_fetch() Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.103.6/26558/Tue May 31 10:05:17 2022) X-Rspamd-Queue-Id: 4LCgGb5FLlz3HnH 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 [-3.29 / 15.00]; RCVD_TLS_LAST(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_SPF_ALLOW(-0.20)[+ip4:85.10.215.148]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[hackers@freebsd.org]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-1.00)[-1.000]; ARC_NA(0.00)[]; NEURAL_HAM_SHORT(-1.00)[-0.999]; DMARC_NA(0.00)[embedded-brains.de]; NEURAL_HAM_MEDIUM(-0.99)[-0.992]; MLMMJ_DEST(0.00)[hackers]; 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 Hello, I try to understand how the PPS synchronization works in FreeBSD. It=20 seems that pps_capture() starts a transaction and pps_event() completes=20 the transaction if nothing interfered in the meantime. void pps_capture(struct pps_state *pps) { struct timehands *th; KASSERT(pps !=3D NULL, ("NULL pps pointer in pps_capture")); th =3D timehands; pps->capgen =3D atomic_load_acq_int(&th->th_generation); pps->capth =3D th; #ifdef FFCLOCK pps->capffth =3D fftimehands; #endif pps->capcount =3D th->th_counter->tc_get_timecount(th->th_counter); atomic_thread_fence_acq(); if (pps->capgen !=3D th->th_generation) pps->capgen =3D 0; } This function uses the current timehand to capture the time measured by=20 the timecounter of the timehand. The timehand generation is used to=20 ensure a consistent transaction. We have void pps_event(struct pps_state *pps, int event) { [...] /* If the timecounter was wound up underneath us, bail out. */ if (pps->capgen =3D=3D 0 || pps->capgen !=3D atomic_load_acq_int(&pps->capth->th_generation)) return; [...] /* * If the timecounter changed, we cannot compare the count values, so * we have to drop the rest of the PPS-stuff until the next event. */ if (pps->ppstc !=3D pps->capth->th_counter) { pps->ppstc =3D pps->capth->th_counter; *pcount =3D pps->capcount; pps->ppscount[2] =3D pps->capcount; return; } /* Convert the count to a timespec. */ tcount =3D pps->capcount - pps->capth->th_offset_count; tcount &=3D pps->capth->th_counter->tc_counter_mask; bt =3D pps->capth->th_bintime; bintime_addx(&bt, pps->capth->th_scale * tcount); bintime2timespec(&bt, &ts); /* If the timecounter was wound up underneath us, bail out. */ atomic_thread_fence_acq(); if (pps->capgen !=3D pps->capth->th_generation) return; [...] } Why do we need the atomic_thread_fence_acq(); if (pps->capgen !=3D th->th_generation) pps->capgen =3D 0; in pps_capture()? Why do we need the early /* If the timecounter was wound up underneath us, bail out. */ if (pps->capgen =3D=3D 0 || pps->capgen !=3D atomic_load_acq_int(&pps->capth->th_generation)) return; in pps_event()? Shouldn't a single if (pps->capgen =3D=3D 0 || pps->capgen !=3D atomic_load_acq_int(&pps->capth->th_generation)) return; after the bintime_addx(&bt, pps->capth->th_scale * tcount); in=20 pps_event() be sufficient? The capture timehand may change anytime, so=20 why is the generation checked multiple times? --=20 embedded brains GmbH Herr Sebastian HUBER Dornierstr. 4 82178 Puchheim Germany email: sebastian.huber@embedded-brains.de phone: +49-89-18 94 741 - 16 fax: +49-89-18 94 741 - 08 Registergericht: Amtsgericht M=C3=BCnchen Registernummer: HRB 157899 Vertretungsberechtigte Gesch=C3=A4ftsf=C3=BChrer: Peter Rasmussen, Thomas= D=C3=B6rfler Unsere Datenschutzerkl=C3=A4rung finden Sie hier: https://embedded-brains.de/datenschutzerklaerung/