]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Return actual frequency in drv_set_freq functions
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 20 Apr 2010 16:29:39 +0000 (18:29 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 27 Apr 2010 12:35:27 +0000 (14:35 +0200)
This is needed to keep sourcestats accurate when the actual frequency is
different from the requested frequency due to clamping (or possibly
rounding in future system drivers).

local.c
local.h
localp.h
sys_linux.c
sys_netbsd.c
sys_solaris.c
sys_sunos.c
tempcomp.c
wrap_adjtimex.c
wrap_adjtimex.h

diff --git a/local.c b/local.c
index b967bcd861d743eefd27e15c6a950ea1ae8d0c2d..c5b6383450b939f453a887424447c4ee62279736 100644 (file)
--- a/local.c
+++ b/local.c
@@ -394,7 +394,7 @@ LCL_SetAbsoluteFrequency(double afreq_ppm)
 
   /* Call the system-specific driver for setting the frequency */
   
-  (*drv_set_freq)(afreq_ppm);
+  afreq_ppm = (*drv_set_freq)(afreq_ppm);
 
   dfreq = (afreq_ppm - current_freq_ppm) / (1.0e6 + current_freq_ppm);
 
@@ -417,6 +417,9 @@ LCL_AccumulateDeltaFrequency(double dfreq)
 {
   ChangeListEntry *ptr;
   struct timeval raw, cooked;
+  double old_freq_ppm;
+
+  old_freq_ppm = current_freq_ppm;
 
   /* Work out new absolute frequency.  Note that absolute frequencies
    are handled in units of ppm, whereas the 'dfreq' argument is in
@@ -425,7 +428,8 @@ LCL_AccumulateDeltaFrequency(double dfreq)
   current_freq_ppm = (1.0 + dfreq) * current_freq_ppm + 1.0e6 * dfreq;
 
   /* Call the system-specific driver for setting the frequency */
-  (*drv_set_freq)(current_freq_ppm);
+  current_freq_ppm = (*drv_set_freq)(current_freq_ppm);
+  dfreq = (current_freq_ppm - old_freq_ppm) / (1.0e6 + old_freq_ppm);
 
   LCL_ReadRawTime(&raw);
   LCL_CookTime(&raw, &cooked, NULL);
@@ -510,7 +514,9 @@ LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset)
 #endif
 
   /* Call the system-specific driver for setting the frequency */
-  (*drv_set_freq)(current_freq_ppm);
+  current_freq_ppm = (*drv_set_freq)(current_freq_ppm);
+  dfreq = (current_freq_ppm - old_freq_ppm) / (1.0e6 + old_freq_ppm);
+
   (*drv_accrue_offset)(doffset);
 
   /* Dispatch to all handlers */
@@ -599,25 +605,30 @@ LCL_SetLeap(int leap)
 
 /* ================================================== */
 
-void
+double
 LCL_SetTempComp(double comp)
 {
+  double uncomp_freq_ppm;
+
   if (temp_comp_ppm == comp)
-    return;
+    return comp;
 
   /* Undo previous compensation */
   current_freq_ppm = (current_freq_ppm + temp_comp_ppm) /
     (1.0 - 1.0e-6 * temp_comp_ppm);
 
+  uncomp_freq_ppm = current_freq_ppm;
+
   /* Apply new compensation */
   current_freq_ppm = current_freq_ppm * (1.0 - 1.0e-6 * comp) - comp;
 
-  temp_comp_ppm = comp;
-
   /* Call the system-specific driver for setting the frequency */
-  (*drv_set_freq)(current_freq_ppm);
+  current_freq_ppm = (*drv_set_freq)(current_freq_ppm);
 
-  return;
+  temp_comp_ppm = (uncomp_freq_ppm - current_freq_ppm) /
+    (1.0e-6 * uncomp_freq_ppm + 1.0);
+
+  return temp_comp_ppm;
 }
 
 /* ================================================== */
diff --git a/local.h b/local.h
index 373d246d422eb53746f17c35f264a1258ab70059..ce29918241ce8990166fd16dacf37bbb010403bc 100644 (file)
--- a/local.h
+++ b/local.h
@@ -186,7 +186,9 @@ extern void LCL_SetLeap(int leap);
 
 /* Routine to set a frequency correction (in ppm) that should be applied
    to local clock to compensate for temperature changes.  A positive
-   argument means that the clock frequency should be increased. */
-extern void LCL_SetTempComp(double comp);
+   argument means that the clock frequency should be increased. Return the
+   actual compensation (may be different from the requested compensation
+   due to clamping or rounding). */
+extern double LCL_SetTempComp(double comp);
 
 #endif /* GOT_LOCAL_H */
index 6f78d1dfbd51d33e226204487c7aef6b24d76048..6d00e9c4804013b0d8866aba696afabb8188bee8 100644 (file)
--- a/localp.h
+++ b/localp.h
@@ -40,8 +40,9 @@ typedef double (*lcl_ReadFrequencyDriver)(void);
 
 /* System driver to set the current local frequency, in ppm relative
    to nominal.  A positive value indicates that the local clock runs
-   fast when uncompensated. */
-typedef void (*lcl_SetFrequencyDriver)(double freq_ppm);
+   fast when uncompensated.  Return actual frequency (may be different
+   from the requested frequency due to clamping or rounding). */
+typedef double (*lcl_SetFrequencyDriver)(double freq_ppm);
 
 /* System driver to accrue an offset. A positive argument means slew
    the clock forwards. */
index 406b9d668e049c0be495fc64064590dd39fcbb13..081a036723de71f4bc1e6097d85019918a18a624 100644 (file)
@@ -562,7 +562,7 @@ apply_step_offset(double offset)
    convention is that this is called with a positive argument if the local
    clock runs fast when uncompensated.  */
 
-static void
+static double
 set_frequency(double freq_ppm)
 {
   long required_tick;
@@ -603,7 +603,7 @@ set_frequency(double freq_ppm)
     required_tick = slewing_tick;
   }
 
-  if (TMX_SetFrequency(scaled_freq, required_tick) < 0) {
+  if (TMX_SetFrequency(&scaled_freq, required_tick) < 0) {
     LOG_FATAL(LOGF_SysLinux, "adjtimex failed for set_frequency, freq_ppm=%10.4e scaled_freq=%10.4e required_tick=%ld",
         freq_ppm, scaled_freq, required_tick);
   }
@@ -616,6 +616,8 @@ set_frequency(double freq_ppm)
       current_total_tick;
     adjust_fast_slew(old_total_tick, old_delta_tick);
   }
+
+  return dhz * (nominal_tick - current_tick) - scaled_freq / freq_scale;
 }
 
 /* ================================================== */
index 4b921e7a941939983fd985230dbf884333e7cca4..04b3f926d1e57f835d88b7a1d25f5fccb49b8d0a 100644 (file)
@@ -244,12 +244,14 @@ apply_step_offset(double offset)
 
 /* ================================================== */
 
-static void
+static double
 set_frequency(double new_freq_ppm)
 {
   stop_adjust();
   current_freq = new_freq_ppm * 1.0e-6;
   start_adjust();
+
+  return current_freq * 1.0e6;
 }
 
 /* ================================================== */
index 40a863df08bee6835d24350af370c0b4008c1068..4024048d07666f21bad4a9b5e152156510211c85 100644 (file)
@@ -273,12 +273,14 @@ apply_step_offset(double offset)
 
 /* ================================================== */
 
-static void
+static double
 set_frequency(double new_freq_ppm)
 {
   stop_adjust();
   current_freq = new_freq_ppm * 1.0e-6;
   start_adjust();
+
+  return current_freq * 1.0e6;
 }
 
 /* ================================================== */
index 6a98f512dcef17ecfc5c85625e6ec8ce526a9532..4878715726820b99527ef48663d3280713a8bb58 100644 (file)
@@ -260,12 +260,14 @@ apply_step_offset(double offset)
 
 /* ================================================== */
 
-static void
+static double
 set_frequency(double new_freq_ppm)
 {
   stop_adjust();
   current_freq = new_freq_ppm * 1.0e-6;
   start_adjust();
+
+  return current_freq * 1.0e6;
 }
 
 /* ================================================== */
index d7f02ad289d6cf31347fff0188159e2f60f85e44..a2b45616a5e4710e67240e716877653e93859936 100644 (file)
@@ -54,7 +54,7 @@ read_timeout(void *arg)
 
     /* Don't allow corrections above 10 ppm */
     if (fabs(comp) < 10.0) {
-      LCL_SetTempComp(comp);
+      comp = LCL_SetTempComp(comp);
 
       if (logfileid != -1) {
         struct timeval now;
index f76f71e8e0df96d6a7159f7a24ac3bcf0007b2eb..13a44407332b8eff8a66e3b0cb9f5eaf4f1e37dc 100644 (file)
@@ -66,13 +66,14 @@ TMX_ApplyOffset(long *offset)
 }
 
 int
-TMX_SetFrequency(double freq, long tick)
+TMX_SetFrequency(double *freq, long tick)
 {
   struct timex txc;
   
   txc.modes = ADJ_TICK | ADJ_FREQUENCY | ADJ_STATUS;
 
-  txc.freq = (long)(freq * (double)(1 << SHIFT_USEC));
+  txc.freq = (long)(*freq * (double)(1 << SHIFT_USEC));
+  *freq = txc.freq / (double)(1 << SHIFT_USEC);
   txc.tick = tick;
   txc.status = STA_UNSYNC; /* Prevent any of the FLL/PLL stuff coming
                               up */
index 543f8320c2209c9095ef9779f224126bc87f42ec..f88188afedf4e57d6ad98cca80470ba9c2a63a77 100644 (file)
@@ -70,7 +70,7 @@ struct tmx_params {
 
 int TMX_SetTick(long tick);
 int TMX_ApplyOffset(long *offset);
-int TMX_SetFrequency(double freq, long tick);
+int TMX_SetFrequency(double *freq, long tick);
 int TMX_GetFrequency(double *freq);
 int TMX_GetOffsetLeftOld(long *offset);
 int TMX_GetOffsetLeft(long *offset);