periodically save current time to time-of-day hardware

Peter Jeremy peterjeremy at acm.org
Fri Mar 26 21:30:36 UTC 2010


On 2010-Mar-26 17:30:14 +0100, Dag-Erling Smørgrav <des at des.no> wrote:
>Andriy Gapon <avg at icyb.net.ua> writes:
>> Dag-Erling Smørgrav <des at des.no> writes:
>> > Andriy Gapon <avg at icyb.net.ua> writes:
>> > > Also, I am aware that the period should be configurable (sysctl).
>> > Why?
>> Because there would always be someone who would want a different value :)
>> > Although I can see an argument for a sysctl to turn it on or off.
>> Good idea.
>
>You can combine the two - P == 0 means "don't save", P > 0 means "save
>every P minutes".
>
>> > IIRC, Linux saves the clock at shutdown, and every 11 minutes if and
>> > only if the system clock is synchronized to an external reference.

At least some versions of Linux also save a RTC drift approximation
and "last set" timestamp whenever the RTC is updated.  This allows the
kernel to better set the system clock from the RTC at boot (ie, our
inittodr()).  The downside is that this needs to store 8-16 bytes of
state somewhere non-volatile.  Linux does this using an external
program and a file - but finding a location for a regularly updated
file that is read very early in the rc.d sequence might be problematic.

>> I know how to add a shutdown hook (event handler), but I don't know how to check
>> if time synchronization is taking place.
>
>adjtime() / adjtimex() sets a flag.  I'm not sure if (or how) the flag
>is cleared when synchronization stops (i.e. /etc/rc.d/ntpd stop);
>perhaps the simplest solution is to set a T = monotime() every time
>adjtime() is called, and check that monotime() - (T * 60) < (P * 60).

It's not especially important how regularly the RTC is updated, just
that it _is_ updated.  This suggests that an alternative approach
would be for adjtime() / ntp_adjtime() to directly call resettodr() if
it's more than P minutes since resettodr() was last called.

As a general comment, whilst resettodr() needs to be serialised, there
is no need for it to block.  If thread B wants to call resettodr()
whilst thread A is doing so, thread B can just skip the call because
calling resettodr() twice in quick succession has no benefit.  This
means the serialisation can be a simple atomic_readandclear_int().

-- 
Peter Jeremy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20100326/d14839cf/attachment.pgp


More information about the freebsd-hackers mailing list