Reinitializing software from hardware clock?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Reinitializing software from hardware clock?

Maximilian Pichler
My vague understanding of how OpenBSD keeps track of time is:

* At boot the software clock (the value returned by gettimeofday) is
initialized from the hardware clock (the one with the coin-shaped
battery).

* The software clock is then incremented hz(9) times a second by a CPU
interrupt.

My question is: Can OpenBSD be told initialize the software from the
hardware clock again after the system is booted?

(Also, is the hardware clock ever modified aside from explicit
invocations of date(1) or similar?)

Thanks!

Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Janne Johansson-3
2018-01-10 12:30 GMT+01:00 Maximilian Pichler <[hidden email]>:

> My vague understanding of how OpenBSD keeps track of time is:
>
> * At boot the software clock (the value returned by gettimeofday) is
> initialized from the hardware clock (the one with the coin-shaped
> battery).
>
>
Not available on all platforms, in which case the kernel sometimes just
grabs the latest timestamp of the filesystem and takes it from there.


> * The software clock is then incremented hz(9) times a second by a CPU
> interrupt.
> My question is: Can OpenBSD be told initialize the software from the
> hardware clock again after the system is booted?
>
>
How would one try to know which is more exact?

For some/most platforms, the (multiple) internal timers are crazy exact
down to
counting cycles in the CPU, and others are not so exact and skew based on
CPU
load and other factors. (I think the obsd-on-mac68k used to suffer from
cpu-time-skew)
For emulated and virtual machines, its even worse, since the guest usually
can't tell
when the host decided to switch cpu frequency based on load or other
factors, throwing
off all kinds of "1 us is X loops in this microsleep routine" calibrations
one might have made
early on.


> (Also, is the hardware clock ever modified aside from explicit
> invocations of date(1) or similar?)
>

--
May the most significant bit of your life be positive.
Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Christian Weisgerber
In reply to this post by Maximilian Pichler
On 2018-01-10, Maximilian Pichler <[hidden email]> wrote:

> * At boot the software clock (the value returned by gettimeofday) is
> initialized from the hardware clock (the one with the coin-shaped
> battery).

... from the RTC ("real-time clock"), yes.

> My question is: Can OpenBSD be told initialize the software from the
> hardware clock again after the system is booted?

No.

> (Also, is the hardware clock ever modified aside from explicit
> invocations of date(1) or similar?)

Yes,
* when the system is properly shut down, and
* periodically every 30 minutes, to prevent the RTC from drifting
  off during long uptimes.

--
Christian "naddy" Weisgerber                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Mike Larkin
On Wed, Jan 10, 2018 at 01:23:19PM -0000, Christian Weisgerber wrote:

> On 2018-01-10, Maximilian Pichler <[hidden email]> wrote:
>
> > * At boot the software clock (the value returned by gettimeofday) is
> > initialized from the hardware clock (the one with the coin-shaped
> > battery).
>
> ... from the RTC ("real-time clock"), yes.
>
> > My question is: Can OpenBSD be told initialize the software from the
> > hardware clock again after the system is booted?
>
> No.
>

This does occur on occasion; for example, when running an OpenBSD vmm(4)
guest, vmd(8) will notify the guest to resynch the clock from the RTC
after the host resumes from suspend/hibernate (actually, "any time
the host clock varies from more than 5s since it was last read by vmd(8"),
but that practically means "only during suspend/hibernate resumes").

If the OP was looking for code to do this for whatever reason, it's done
via vmmci(4).

-ml

> > (Also, is the hardware clock ever modified aside from explicit
> > invocations of date(1) or similar?)
>
> Yes,
> * when the system is properly shut down, and
> * periodically every 30 minutes, to prevent the RTC from drifting
>   off during long uptimes.
>
> --
> Christian "naddy" Weisgerber                          [hidden email]
>

Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Maximilian Pichler
On Wed, Jan 10, 2018 at 6:50 PM, Mike Larkin <[hidden email]> wrote:
>> > My question is: Can OpenBSD be told initialize the software from the
>> > hardware clock again after the system is booted?

> This does occur on occasion; for example, when running an OpenBSD vmm(4)
> guest, vmd(8) will notify the guest to resynch the clock from the RTC
> after the host resumes from suspend/hibernate (actually, "any time
> the host clock varies from more than 5s since it was last read by vmd(8"),
> but that practically means "only during suspend/hibernate resumes").
>
> If the OP was looking for code to do this for whatever reason, it's done
> via vmmci(4).

In fact this is precisely the use case I had in mind, except for that
instead of inside vmm OpenBSD is running in Virtualbox on Mac OS. The
clock seems to drift randomly and is off significantly after
suspend/hibernate.

Following your hint at vmmci and looking at
https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/dev/pv/vmmci.c.diff?r1=1.2&r2=1.3&f=h
it looks like inittodr(9) would reinitialize the software clock.
However it seems to be available inside the kernel only. Is there no
way to do this from userland?

Thanks!

Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Mike Larkin
On Thu, Jan 11, 2018 at 02:22:00AM +0000, Maximilian Pichler wrote:

> On Wed, Jan 10, 2018 at 6:50 PM, Mike Larkin <[hidden email]> wrote:
> >> > My question is: Can OpenBSD be told initialize the software from the
> >> > hardware clock again after the system is booted?
>
> > This does occur on occasion; for example, when running an OpenBSD vmm(4)
> > guest, vmd(8) will notify the guest to resynch the clock from the RTC
> > after the host resumes from suspend/hibernate (actually, "any time
> > the host clock varies from more than 5s since it was last read by vmd(8"),
> > but that practically means "only during suspend/hibernate resumes").
> >
> > If the OP was looking for code to do this for whatever reason, it's done
> > via vmmci(4).
>
> In fact this is precisely the use case I had in mind, except for that
> instead of inside vmm OpenBSD is running in Virtualbox on Mac OS. The
> clock seems to drift randomly and is off significantly after
> suspend/hibernate.
>
> Following your hint at vmmci and looking at
> https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/dev/pv/vmmci.c.diff?r1=1.2&r2=1.3&f=h
> it looks like inittodr(9) would reinitialize the software clock.
> However it seems to be available inside the kernel only. Is there no
> way to do this from userland?
>
> Thanks!
>

not that I know of.

Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Maximilian Pichler
On Thu, Jan 11, 2018 at 2:23 AM, Mike Larkin <[hidden email]> wrote:
>> Following your hint at vmmci and looking at
>> https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/dev/pv/vmmci.c.diff?r1=1.2&r2=1.3&f=h
>> it looks like inittodr(9) would reinitialize the software clock.
>> However it seems to be available inside the kernel only. Is there no
>> way to do this from userland?
>
> not that I know of.

Not quite what I'd hoped for... now resorting to periodically updating
the clock:

while true; do date +%Y%m%d%H%M.%S | ssh virtual_machine 'date; xargs
doas date'; sleep 60; done

Reply | Threaded
Open this post in threaded view
|

Re: Reinitializing software from hardware clock?

Mike Larkin
On Thu, Jan 11, 2018 at 04:51:47AM +0000, Maximilian Pichler wrote:

> On Thu, Jan 11, 2018 at 2:23 AM, Mike Larkin <[hidden email]> wrote:
> >> Following your hint at vmmci and looking at
> >> https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/dev/pv/vmmci.c.diff?r1=1.2&r2=1.3&f=h
> >> it looks like inittodr(9) would reinitialize the software clock.
> >> However it seems to be available inside the kernel only. Is there no
> >> way to do this from userland?
> >
> > not that I know of.
>
> Not quite what I'd hoped for... now resorting to periodically updating
> the clock:
>
> while true; do date +%Y%m%d%H%M.%S | ssh virtual_machine 'date; xargs
> doas date'; sleep 60; done

the code is there. you can make a sysctl or other one-off to do what you
want.