[chrony-dev] [PATCH] Mac OS X - Synchronise RTC from system time

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


When the rtcsync directive is specified in the chronyd config file,
chronyd will update the RTC via settimeofday() every 60 minutes if
the time is syncronised to NTP.
---
 chrony.texi.in | 15 ++++++++++-----
 sys_macosx.c   | 29 ++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/chrony.texi.in b/chrony.texi.in
index 4bb212a..5155bb9 100644
--- a/chrony.texi.in
+++ b/chrony.texi.in
@@ -2844,12 +2844,17 @@ Note that this setting is overriden when the @code{hwclockfile} directive
 @node rtcsync directive
 @subsection rtcsync
 
-The @code{rtcsync} directive will enable a kernel mode where the
-system time is copied to the real time clock (RTC) every 11 minutes.
+The @code{rtcsync} directive enables a mode where the system time is
+periodically copied to the real time clock (RTC).
 
-This directive is supported only on Linux and cannot be used when the
-normal RTC tracking is enabled, i.e. when the @code{rtcfile} directive
-is used.  On other systems this directive does nothing.
+On Linux the RTC copy is performed by the kernel every 11 minutes. This
+directive cannot be used when the normal RTC tracking is enabled,
+i.e. when the @code{rtcfile} directiveis used.
+
+On Mac OS X, chronyd will perform the RTC copy every 60 minutes when the
+system clock is in a synchronised state.
+
+On other systems this directive does nothing.
 @c }}}
 @c {{{ sched_priority
 @node sched_priority directive
diff --git a/sys_macosx.c b/sys_macosx.c
index 36981ba..696ace5 100644
--- a/sys_macosx.c
+++ b/sys_macosx.c
@@ -38,6 +38,7 @@
 #include <pthread.h>
 
 #include "sys_macosx.h"
+#include "conf.h"
 #include "localp.h"
 #include "logging.h"
 #include "sched.h"
@@ -88,6 +89,11 @@ static struct timeval Tdrift;
 
 #define NANOS_PER_MSEC (1000000ULL)
 
+/* RTC synchronisation - once an hour */
+
+static struct timeval last_rtc_sync;
+#define RTC_SYNC_INTERVAL (60 * 60.0)
+
 /* ================================================== */
 
 static void
@@ -101,6 +107,10 @@ clock_initialise(void)
   drift_removal_interval = DRIFT_REMOVAL_INTERVAL;
   current_drift_removal_interval = DRIFT_REMOVAL_INTERVAL;
 
+  if (gettimeofday(&last_rtc_sync, NULL) < 0) {
+    LOG_FATAL(LOGF_SysMacOSX, "gettimeofday() failed");
+  }
+
   if (gettimeofday(&T0, NULL) < 0) {
     LOG_FATAL(LOGF_SysMacOSX, "gettimeofday() failed");
   }
@@ -317,7 +327,9 @@ drift_removal_timeout(SCH_ArbitraryArgument not_used)
 
 /* ================================================== */
 
-/* use est_error to calculate the drift_removal_interval */
+/* use est_error to calculate the drift_removal_interval and
+   update the RTC 
+*/
 
 static void
 set_sync_status(int synchronised, double est_error, double max_error)
@@ -327,6 +339,21 @@ set_sync_status(int synchronised, double est_error, double max_error)
   if (!synchronised) {
     drift_removal_interval = MAX(drift_removal_interval, DRIFT_REMOVAL_INTERVAL);
   } else {
+    if (CNF_GetRtcSync()) {
+      struct timeval now;
+      double rtc_sync_elapsed;
+
+      if (gettimeofday(&now, NULL))
+        LOG_FATAL(LOGF_SysMacOSX, "gettimeofday() failed");
+      UTI_DiffTimevalsToDouble(&rtc_sync_elapsed, &now, &last_rtc_sync);
+      if (rtc_sync_elapsed >= RTC_SYNC_INTERVAL) {
+        /* update the RTC by applying a step of 0.0 secs */
+        apply_step_offset(0.0);
+        last_rtc_sync = now;
+        DEBUG_LOG(LOGF_SysMacOSX, "rtc synchronised");
+      }
+    }
+
     interval = ERROR_WEIGHT * est_error / (fabs(current_freq) + FREQUENCY_RES);
     drift_removal_interval = MAX(interval, DRIFT_REMOVAL_INTERVAL_MIN);
 
-- 
2.4.9 (Apple Git-60)


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/