Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread Miroslav Lichvar
On Mon, Jan 15, 2024 at 08:56:41PM +0700, James Clark wrote:
> There are three successive cases around a positive leap second:
> 
> - the true time is before the start of the leap: in this case the
> difference in the POSIX system times will be 3
> - the true time is after the leap has finished but system time is before
> the leap: in this case the difference in the POSIX times will be 2 (because
> 1 second of the difference is a leap second and so ignored)
> - the system time is after the leap is finished: in this case the
> difference will again be 3
> 
> Are you saying that chrony expects the offset to jump around like this
> (even though there is no change in the amount by which the clock needs to
> be corrected)?

It will ignore the 2-second offset because it's in that 10-second
interval around the leap.

If the offset was larger than 10/11 seconds and the clock couldn't be
corrected by step, it would get the one-second error in measurements
accepted before getting close to the leap.

-- 
Miroslav Lichvar


-- 
To unsubscribe email chrony-users-requ...@chrony.tuxfamily.org 
with "unsubscribe" in the subject.
For help email chrony-users-requ...@chrony.tuxfamily.org 
with "help" in the subject.
Trouble?  Email listmas...@chrony.tuxfamily.org.



Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread James Clark
On Mon, Jan 15, 2024 at 8:15 PM Miroslav Lichvar 
wrote:

> On Mon, Jan 15, 2024 at 06:57:48PM +0700, James Clark wrote:
> > In computing the initial step, how does chrony handle leap seconds?
> >
> > There are two ways of computing offsets:
> > - subtracting the TAI times (so that it is really the number of seconds
> > that elapse between the system time and the true time)
> > - subtracting the POSIX times (so leap seconds are ignored)
>
> chronyd corrects the system clock, which ignores leap seconds, so it
> expects the second approach. It doesn't do any corrections of the
> offset.
>

This is going to be a bit strange in the vicinity of a positive leap
second. Suppose the true time is ahead of the system time by 3 seconds.
There are three successive cases around a positive leap second:

- the true time is before the start of the leap: in this case the
difference in the POSIX system times will be 3
- the true time is after the leap has finished but system time is before
the leap: in this case the difference in the POSIX times will be 2 (because
1 second of the difference is a leap second and so ignored)
- the system time is after the leap is finished: in this case the
difference will again be 3

Are you saying that chrony expects the offset to jump around like this
(even though there is no change in the amount by which the clock needs to
be corrected)?

I can change my code to implement this, if that's what's needed: I would
convert both times to POSIX before computing the offset, instead of
converting them both to TAI.

James


Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread Miroslav Lichvar
On Mon, Jan 15, 2024 at 06:57:48PM +0700, James Clark wrote:
> In computing the initial step, how does chrony handle leap seconds?
> 
> There are two ways of computing offsets:
> - subtracting the TAI times (so that it is really the number of seconds
> that elapse between the system time and the true time)
> - subtracting the POSIX times (so leap seconds are ignored)

chronyd corrects the system clock, which ignores leap seconds, so it
expects the second approach. It doesn't do any corrections of the
offset.

> > The leap should be set until the leap happens, starting up to 24 hours
> > before.
> >
> 
> What does "until the leap happens" mean exactly? Does that mean that adding
> the offset to the system time gives a time that is < 24:00:00 on the day of
> the leap second (i.e. the leap second is over)? Or is it until the leap
> second starts?

Until the leap is finished from the point of the reference clock, i.e.
the leap bit should be cleared after when the day changes in the
reference time (UTC).

In practice, it doesn't have to be so exact. chronyd ignores any
measurements made in or pointing to a 10-second interval around the
leap. At 23:59:55 it stops updating the clock and coasts through.

-- 
Miroslav Lichvar


-- 
To unsubscribe email chrony-users-requ...@chrony.tuxfamily.org 
with "unsubscribe" in the subject.
For help email chrony-users-requ...@chrony.tuxfamily.org 
with "help" in the subject.
Trouble?  Email listmas...@chrony.tuxfamily.org.



Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread James Clark
On Mon, Jan 15, 2024 at 6:32 PM Miroslav Lichvar 
wrote:

>
> > the actual system time).  But the offset will be wrong by a small number
> of
> > seconds if the system clock is so far in the past that leap seconds other
> > than the latest one intervened: I don't think that matters.
>
> That will cause an error in the initial step of the clock and it will
> take longer to stabilize.
>

In computing the initial step, how does chrony handle leap seconds?

There are two ways of computing offsets:
- subtracting the TAI times (so that it is really the number of seconds
that elapse between the system time and the true time)
- subtracting the POSIX times (so leap seconds are ignored)

When the offset is small, I am assuming the first way is right (otherwise
you would get discontinuities). But if the system offset is a long time in
the past (so that chrony is doing a step), does chrony expect the first or
the second? The first would require chrony to have knowledge of all past
leap seconds.

Or am I misunderstanding something here?

The sample time in the SOCK needs to be in the system time (UTC),
> otherwise it will be rejected as invalid.
>

Right: I'm doing that.

> I compute the leap flags based on the true time: if the true time occurs
> on
> > a day that ends with a leap second, then I set the leap flags. Or should
> I
> > be setting them if the true time is some number of hours before the end
> of
> > the day that ends with a leap second?
>
> The leap should be set until the leap happens, starting up to 24 hours
> before.
>

What does "until the leap happens" mean exactly? Does that mean that adding
the offset to the system time gives a time that is < 24:00:00 on the day of
the leap second (i.e. the leap second is over)? Or is it until the leap
second starts?

James


Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread Miroslav Lichvar
On Mon, Jan 15, 2024 at 06:09:53PM +0700, James Clark wrote:
> Oh, I see. So chrony is using the offset directly? I had misunderstood: I
> assumed it was computing the true time by adding the offset to the system
> time, and then using the true time. It all makes sense now.

Yes, chrony for the most part doesn't care what time it is, but rather
how much it is ahead or behind.

> the actual system time).  But the offset will be wrong by a small number of
> seconds if the system clock is so far in the past that leap seconds other
> than the latest one intervened: I don't think that matters.

That will cause an error in the initial step of the clock and it will
take longer to stabilize.

The sample time in the SOCK needs to be in the system time (UTC),
otherwise it will be rejected as invalid.

> I compute the leap flags based on the true time: if the true time occurs on
> a day that ends with a leap second, then I set the leap flags. Or should I
> be setting them if the true time is some number of hours before the end of
> the day that ends with a leap second?

The leap should be set until the leap happens, starting up to 24 hours
before.

-- 
Miroslav Lichvar


-- 
To unsubscribe email chrony-users-requ...@chrony.tuxfamily.org 
with "unsubscribe" in the subject.
For help email chrony-users-requ...@chrony.tuxfamily.org 
with "help" in the subject.
Trouble?  Email listmas...@chrony.tuxfamily.org.



Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread James Clark
On Mon, Jan 15, 2024 at 4:27 PM Miroslav Lichvar 
wrote:

> On Mon, Jan 15, 2024 at 01:05:32PM +0700, James Clark wrote:
> > I have implemented refclock SOCK for a project I'm working on. In my
> case,
> > I am getting samples from a PHC, so I have a system time with nanosecond
> > precision (from PTP_SYS_OFFSET*). But refclock SOCK uses struct timeval,
> > which is in microseconds. I looked at the refclock_sock servo in linuxptp
> > 4.x, and that just discards away the fraction of the microsecond.  But
> > that's not very satisfying and makes chronyc give misleading information
> > about the accuracy of the offset.
>
> The resolution of the timestamp is not very important. Milliseconds
> would be still fine. It is not used for calculating the offset. The
> offset is provided directly as a floating-point value in the SOCK
> message.
>

Oh, I see. So chrony is using the offset directly? I had misunderstood: I
assumed it was computing the true time by adding the offset to the system
time, and then using the true time. It all makes sense now.

I think my code is doing the right thing already. But let me just describe
it, in case I have misunderstood anything else.

The information from which I am computing the sample is:
- the true time in TAI with nanosecond precision
- the date and type of the next announced leap second (from the GNSS
receiver)
- the Linux system time with nanosecond precision

To compute the offset, I convert the system time to a TAI time. But if the
system time is in the range [23:59:59, 00:00:00) on the day of a positive
leap second, then I don't generate a sample (because it's ambiguous what
TAI time that system time corresponds to). I then use as the offset the
difference of true TAI time and the TAI time from converting the system
time.  So the offset is the amount by which the system clock needs to be
corrected (rather than the difference between the correct system time and
the actual system time).  But the offset will be wrong by a small number of
seconds if the system clock is so far in the past that leap seconds other
than the latest one intervened: I don't think that matters.

I compute the leap flags based on the true time: if the true time occurs on
a day that ends with a leap second, then I set the leap flags. Or should I
be setting them if the true time is some number of hours before the end of
the day that ends with a leap second?

James


Re: [chrony-users] Nanosecond precision with refclock SOCK

2024-01-15 Thread Miroslav Lichvar
On Mon, Jan 15, 2024 at 01:05:32PM +0700, James Clark wrote:
> I have implemented refclock SOCK for a project I'm working on. In my case,
> I am getting samples from a PHC, so I have a system time with nanosecond
> precision (from PTP_SYS_OFFSET*). But refclock SOCK uses struct timeval,
> which is in microseconds. I looked at the refclock_sock servo in linuxptp
> 4.x, and that just discards away the fraction of the microsecond.  But
> that's not very satisfying and makes chronyc give misleading information
> about the accuracy of the offset.

The resolution of the timestamp is not very important. Milliseconds
would be still fine. It is not used for calculating the offset. The
offset is provided directly as a floating-point value in the SOCK
message.

This is different from the SHM refclock, where the offset needs to be
calculated by the receiver as a difference between two timestamps and
where an extension was required to improve the resolution to
nanoseconds.

-- 
Miroslav Lichvar


-- 
To unsubscribe email chrony-users-requ...@chrony.tuxfamily.org 
with "unsubscribe" in the subject.
For help email chrony-users-requ...@chrony.tuxfamily.org 
with "help" in the subject.
Trouble?  Email listmas...@chrony.tuxfamily.org.