[chrony-dev] [PATCH] MacOS X - Dynamic drift removal interval

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


Use the kernel slew rate amd measure drift to estimate the time required to compensate
for the maximum error observed in the most recent sample. Use this time as the interval
between drift removal updates until the next sample is received.

Lan connected NTP server - root delay 0.5 ms - drift_removal_interval about 0.2 secs
Local NZ Server(s) - root delay 10ms - drift_removal_interval about 10secs
Overseas (US) Servers - root delay 200ms - drift_removal_interval about 60secs
---
 sys_macosx.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 51 insertions(+), 6 deletions(-)

diff --git a/sys_macosx.c b/sys_macosx.c
index 4db6a0c..645943a 100644
--- a/sys_macosx.c
+++ b/sys_macosx.c
@@ -70,6 +70,17 @@ static double current_freq;
 
 static double adjustment_requested;
 
+/* Interval in seconds between adjustments to cancel systematic drift */
+
+#define DRIFT_REMOVAL_INTERVAL (1.0)
+#define DRIFT_REMOVAL_INTERVAL_MIN (0.1)
+
+static double drift_removal_interval;
+
+/* rate at which kernel slews clock in adjtime() - usec / sec */
+
+static double kernel_slewrate;
+
 /* ================================================== */
 
 static void
@@ -80,6 +91,7 @@ clock_initialise(void)
   offset_register = 0.0;
   adjustment_requested = 0.0;
   current_freq = 0.0;
+  drift_removal_interval = DRIFT_REMOVAL_INTERVAL;
 
   if (gettimeofday(&T0, NULL) < 0) {
     LOG_FATAL(LOGF_SysMacOSX, "gettimeofday() failed");
@@ -186,6 +198,25 @@ accrue_offset(double offset, double corr_rate)
 
 /* ================================================== */
 
+/* use max_error to calculate the drift_removal_interval */
+
+static void
+set_sync_status(int synchronised, double est_error, double max_error)
+{
+  drift_removal_interval = DRIFT_REMOVAL_INTERVAL;
+
+  if (!synchronised) {
+    return;
+  }
+
+  if (max_error > 0.0 && fabs(current_freq + kernel_slewrate) >= 1.0e-9) {
+    drift_removal_interval = fabs(max_error / (current_freq + kernel_slewrate));
+    drift_removal_interval = MAX(drift_removal_interval, DRIFT_REMOVAL_INTERVAL_MIN);
+  }
+}
+
+/* ================================================== */
+
 /* Positive offset means system clock is fast of true time, therefore
    step backwards */
 
@@ -250,9 +281,6 @@ get_offset_correction(struct timeval *raw,
 
 /* ================================================== */
 
-/* Interval in seconds between adjustments to cancel systematic drift */
-#define DRIFT_REMOVAL_INTERVAL (1.0)
-
 static int drift_removal_running = 0;
 static SCH_TimeoutID drift_removal_id;
 
@@ -271,7 +299,7 @@ drift_removal_timeout(SCH_ArbitraryArgument not_used)
 {
   stop_adjust();
   start_adjust();
-  drift_removal_id = SCH_AddTimeoutByDelay(DRIFT_REMOVAL_INTERVAL, drift_removal_timeout, NULL);
+  drift_removal_id = SCH_AddTimeoutByDelay(drift_removal_interval, drift_removal_timeout, NULL);
 }
 
 /* ================================================== */
@@ -279,16 +307,33 @@ drift_removal_timeout(SCH_ArbitraryArgument not_used)
 void
 SYS_MacOSX_Initialise(void)
 {
+  int result;
+  size_t len;
+  struct clockinfo clockinfo;
+  int mib[2];
+
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_CLOCKRATE;
+
+  len = sizeof(clockinfo);
+  result = sysctl(mib, 2, &clockinfo, &len, NULL, 0);
+
+  if(result < 0) {
+    LOG_FATAL(LOGF_SysMacOSX, "Cannot read clockinfo");
+  }
+
+  kernel_slewrate = (double)clockinfo.tickadj / (double)clockinfo.tick;
+
   clock_initialise();
 
   lcl_RegisterSystemDrivers(read_frequency, set_frequency,
                             accrue_offset, apply_step_offset,
                             get_offset_correction,
                             NULL /* set_leap */,
-                            NULL /* set_sync_status */);
+                            set_sync_status);
 
 
-  drift_removal_id = SCH_AddTimeoutByDelay(DRIFT_REMOVAL_INTERVAL, drift_removal_timeout, NULL);
+  drift_removal_id = SCH_AddTimeoutByDelay(drift_removal_interval, drift_removal_timeout, NULL);
   drift_removal_running = 1;
 }
 
-- 
2.3.2 (Apple Git-55)


-- 
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/