Updating and displaying CMOS clock

Ian Smith smithi at nimnet.asn.au
Mon Jul 7 13:32:56 UTC 2014

In freebsd-questions Digest, Vol 527, Issue 1, Message: 2
On Sun, 6 Jul 2014 22:32:07 +0700 Victor Sudakov <vas at mpeks.tomsk.su> wrote:
 > Colleagues,
 > What's the command to update the CMOS clock to the time of the kernel
 > clock? 
 > ntpd does a good job of keeping the accurate time in the kernel clock,
 > but from my experience, this time is not propagated to the CMOS clock
 > which lives its own life. As a result, there is a large time skew
 > everytime the box is rebooted, which prevents from Kerberos login
 > until the kernel clock is synchronized.

That shouldn't be happening.  There may be some hardware problem with 
your RTC if you haven't messed with the sysctl settings (see below).  
Could be a borderline battery, poor quality 32768Hz timebase or ..

 > In other words, what's the FreeBSD equivalent of the Linux 
 > "hwclock --systohc" command? 

People have pointed out incantations with date that will do the job, but 
it really shouldn't be necessary.

 > And no, contrary to popular belief, the correction of the CMOS clock
 > does not happen automatically in FreeBSD even if ntpd is running.

Yes, it does.  Has since 2.2.6 anyway, whether or not ntpd is running.

In 8.x /var/log/messages gets spammed with RTC update messages every 
half hour, if you had booted with verbose messages:

Jul  7 19:34:41 t23 ntpd[65982]: kernel time sync status change 6001
Jul  7 19:45:29 t23 ntpd[65982]: kernel time sync status change 2001
Jul  7 19:46:22 t23 kernel: ts_to_ct(1404762382.687179418) = [2014-07-07 19:46:22]
Jul  7 20:16:22 t23 kernel: ts_to_ct(1404764182.481274525) = [2014-07-07 20:16:22]
Jul  7 20:46:22 t23 kernel: ts_to_ct(1404765982.270411900) = [2014-07-07 20:46:22]
Jul  7 21:16:22 t23 kernel: ts_to_ct(1404767782.058302318) = [2014-07-07 21:16:22]
Jul  7 21:46:21 t23 kernel: ts_to_ct(1404769581.848953320) = [2014-07-07 21:46:21]
Jul  7 22:16:21 t23 kernel: ts_to_ct(1404771381.645923462) = [2014-07-07 22:16:21]

In 9.x there's a new sysctl debug.clocktime to enable such verbosity; I 
turned it on about 3 hours ago after seeing this thread, suspended and 
resumed to confirm it working, then restarted ntpd for good measure:

Jul  7 19:25:23 x200 ntpd[15232]: ntpd exiting on signal 15
Jul  7 19:25:23 x200 ntpd[15436]: ntpd 4.2.4p5-a (1)
Jul  7 19:25:31 x200 kernel: ts_to_ct(1404761131.551088025) = [2014-07-07 19:25:31]
Jul  7 19:25:31 x200 kernel:
Jul  7 19:25:31 x200 ntpd[15437]: time reset -2.866706 s
Jul  7 19:45:58 x200 kernel: ts_to_ct(1404762359.155395216) = [2014-07-07 19:45:59]
Jul  7 20:15:58 x200 kernel: ts_to_ct(1404764159.155396751) = [2014-07-07 20:15:59]
Jul  7 20:45:58 x200 kernel: ts_to_ct(1404765959.155395746) = [2014-07-07 20:45:59]
Jul  7 21:15:58 x200 kernel: ts_to_ct(1404767759.155397106) = [2014-07-07 21:15:59]
Jul  7 21:45:58 x200 kernel: ts_to_ct(1404769559.155398316) = [2014-07-07 21:45:59]
Jul  7 22:15:58 x200 kernel: ts_to_ct(1404771359.155397764) = [2014-07-07 22:15:59]

 > If there is a way to display the time of the CMOS (RTC) clock, I
 > would be interested to know too.

True, the above shows when it's updated, but not the amount corrected 
(which would require a superfluous read before write, and RTC is slow).  
Time can be a bit rough because you can only read or set the RTC to the 
nearest second, always rounded down going on the 8.x example above.

>From your later message:

 > Ralf Mardorf wrote:
 > > 
 > > "In the last episode (Mar 10), Kelly Jones said:
 > > > What are the FreeBSD equivalents of hwclock (view/set the BIOS
 > > > hardware clock) and adjtimex (adjust clock speed)? I couldn't find
 > > > these two well-known Linux commands in ports?
 > > 
 > > FreeBSD sets the hardware clock whenever settimeofday() is called
 > > (unless the sysctl machdep.disable_rtc_set is set). 
 > I have been told about the machdep.rtc_save_period sysctl, but it seems
 > undocumented.

!sysctl -d machdep | grep rtc
machdep.rtc_save_period: Save system time to RTC with this period (in seconds)
machdep.disable_rtc_set: Disallow adjusting time-of-day clock

!sysctl machdep | grep rtc
machdep.rtc_save_period: 1800
machdep.disable_rtc_set: 0

Do these match yours?  And set sysctl debug.clocktime=1 and report back.  
I'm not sure whether you need to have also booted with verbose messages 
to get the ts_to_ct (and ct_to_ts on booting or resume) messages; I've 
been booting verbose mostly lately, maybe you should too if need be; if 
there's any problem like failing to update your RTC, it should show up.

A few code references (of many) if you want to dig a bit deeper:

cheers, Ian

More information about the freebsd-questions mailing list