]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
reference: rework activation of local reference
authorMiroslav Lichvar <mlichvar@redhat.com>
Fri, 8 Apr 2016 13:50:27 +0000 (15:50 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Fri, 8 Apr 2016 14:47:41 +0000 (16:47 +0200)
Instead of using a timer for switching the reference to the
unsynchronised state (which activates the local reference), check
if it should be active when returning the reference parameters.

reference.c

index ff73dbbea8141b117ed7fdea28313d6e0b3e6533..19857fb0f6fe2f23cc555422ae4a393c6f1898ee 100644 (file)
@@ -114,11 +114,6 @@ static char *leap_tzname;
 static time_t last_tz_leap_check;
 static NTP_Leap tz_leap;
 
-#define MAX_LOCAL_TIMEOUT (30 * 24 * 3600.0)
-
-/* Timer for local reference */
-static SCH_TimeoutID local_timeout_id;
-
 /* ================================================== */
 
 static LOG_FileID logfileid;
@@ -238,7 +233,6 @@ REF_Initialise(void)
   correction_time_ratio = CNF_GetCorrectionTimeRatio();
 
   enable_local_stratum = CNF_AllowLocalReference(&local_stratum, &local_orphan, &local_distance);
-  local_timeout_id = 0;
 
   leap_timeout_id = 0;
   leap_in_progress = 0;
@@ -809,38 +803,6 @@ update_leap_status(NTP_Leap leap, time_t now, int reset)
 
 /* ================================================== */
 
-static void
-local_timeout(void *arg)
-{
-  local_timeout_id = 0;
-  REF_SetUnsynchronised();
-}
-
-/* ================================================== */
-
-static void
-update_local_timeout(void)
-{
-  double delay;
-
-  SCH_RemoveTimeout(local_timeout_id);
-  local_timeout_id = 0;
-
-  if (!enable_local_stratum || !are_we_synchronised)
-    return;
-
-  /* Add a timer that will activate the local reference approximately at the
-     point when our root distance reaches the configured root distance */
-  delay = (local_distance - (our_root_delay / 2.0 + our_root_dispersion)) /
-          (our_skew + fabs(our_residual_freq) + LCL_GetMaxClockError());
-  delay = CLAMP(0.0, delay, MAX_LOCAL_TIMEOUT);
-  local_timeout_id = SCH_AddTimeoutByDelay(delay, local_timeout, NULL);
-
-  DEBUG_LOG(LOGF_Reference, "Local reference timeout %f", delay);
-}
-
-/* ================================================== */
-
 static void
 write_log(struct timeval *ref_time, char *ref, int stratum, NTP_Leap leap,
     double freq, double skew, double offset, int combined_sources,
@@ -1103,9 +1065,6 @@ REF_SetReference(int stratum,
     }
   }
 
-  /* Update timer that activates the local reference */
-  update_local_timeout();
-
   /* Update fallback drifts */
   if (fb_drifts) {
     update_fb_drifts(abs_freq_ppm, update_interval);
@@ -1170,7 +1129,6 @@ REF_SetUnsynchronised(void)
 
   update_leap_status(LEAP_Unsynchronised, 0, 0);
   are_we_synchronised = 0;
-  update_local_timeout();
 
   LCL_SetSyncStatus(0, 0.0, 0.0);
 
@@ -1201,25 +1159,33 @@ REF_GetReferenceParams
  double *root_dispersion
 )
 {
-  double elapsed;
-  double extra_dispersion;
+  double elapsed, dispersion;
 
   assert(initialised);
 
   if (are_we_synchronised) {
+    UTI_DiffTimevalsToDouble(&elapsed, local_time, &our_ref_time);
+    dispersion = our_root_dispersion +
+      (our_skew + fabs(our_residual_freq) + LCL_GetMaxClockError()) * elapsed;
+  } else {
+    dispersion = 0.0;
+  }
+
+  /* Local reference is active when enabled and the clock is not synchronised
+     or the root distance exceeds the threshold */
+
+  if (are_we_synchronised &&
+      !(enable_local_stratum && our_root_delay / 2 + dispersion > local_distance)) {
 
     *is_synchronised = 1;
 
     *stratum = our_stratum;
 
-    UTI_DiffTimevalsToDouble(&elapsed, local_time, &our_ref_time);
-    extra_dispersion = (our_skew + fabs(our_residual_freq) + LCL_GetMaxClockError()) * elapsed;
-
     *leap_status = !leap_in_progress ? our_leap_status : LEAP_Unsynchronised;
     *ref_id = our_ref_id;
     *ref_time = our_ref_time;
     *root_delay = our_root_delay;
-    *root_dispersion = our_root_dispersion + extra_dispersion;
+    *root_dispersion = dispersion;
 
   } else if (enable_local_stratum) {
 
@@ -1264,13 +1230,17 @@ REF_GetReferenceParams
 int
 REF_GetOurStratum(void)
 {
-  if (are_we_synchronised) {
-    return our_stratum;
-  } else if (enable_local_stratum) {
-    return local_stratum;
-  } else {
-    return NTP_MAX_STRATUM;
-  }
+  struct timeval now_cooked, ref_time;
+  int synchronised, stratum;
+  NTP_Leap leap_status;
+  uint32_t ref_id;
+  double root_delay, root_dispersion;
+
+  SCH_GetLastEventTime(&now_cooked, NULL, NULL);
+  REF_GetReferenceParams(&now_cooked, &synchronised, &leap_status, &stratum,
+                         &ref_id, &ref_time, &root_delay, &root_dispersion);
+
+  return stratum;
 }
 
 /* ================================================== */
@@ -1317,8 +1287,6 @@ REF_EnableLocal(int stratum, double distance, int orphan)
   local_stratum = CLAMP(1, stratum, NTP_MAX_STRATUM - 1);
   local_distance = distance;
   local_orphan = !!orphan;
-
-  update_local_timeout();
 }
 
 /* ================================================== */
@@ -1384,7 +1352,7 @@ REF_GetTrackingReport(RPT_TrackingReport *rep)
   rep->last_offset = last_offset;
   rep->rms_offset = sqrt(avg2_offset);
 
-  if (are_we_synchronised) {
+  if (synchronised) {
     rep->ip_addr = our_ref_ip;
     rep->resid_freq_ppm = 1.0e6 * our_residual_freq;
     rep->skew_ppm = 1.0e6 * our_skew;