From ede32597f5de3c51321ae86b96e6789c6eca5d8f Mon Sep 17 00:00:00 2001 From: J William Piggott Date: Wed, 15 Oct 2014 14:32:31 -0400 Subject: hwclock: hctosys drift compensation II COMMENTS Update source comments and --help output for the hwclock: hctosys drift compensation II patch. Signed-off-by: J William Piggott --- sys-utils/hwclock.c | 59 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 26 deletions(-) (limited to 'sys-utils/hwclock.c') diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c index 30db8b628..4befbab32 100644 --- a/sys-utils/hwclock.c +++ b/sys-utils/hwclock.c @@ -684,11 +684,9 @@ set_hardware_clock_exact(const time_t sethwtime, } /* - * Put the time "systime" on standard output in display format. Except if + * Put the time "hwctime" on standard output in display format. Except if * hclock_valid == false, just tell standard output that we don't know what * time it is. - * - * Include in the output the adjustment "sync_duration". */ static void display_time(const bool hclock_valid, struct timeval hwctime) @@ -1022,7 +1020,14 @@ adjust_drift_factor(struct adjtime *adjtime_p, struct timeval last_calib; last_calib = t2tv(adjtime_p->last_calib_time); - + /* + * Correction to apply to the current drift factor. + * + * Simplified: uncorrected_drift / days_since_calibration. + * + * hclocktime is fully corrected with the current drift factor. + * Its difference from nowtime is the missed drift correction. + */ factor_adjust = time_diff(nowtime, hclocktime) / (time_diff(nowtime, last_calib) / sec_per_day); @@ -1057,16 +1062,12 @@ adjust_drift_factor(struct adjtime *adjtime_p, } /* - * Do the drift adjustment calculation. - * - * The way we have to set the clock, we need the adjustment in two parts: + * Calculate the drift correction currently needed for the + * Hardware Clock based on the last time it was adjusted, + * and the current drift factor, as stored in the adjtime file. * - * 1) an integer number of seconds (return as *adjustment_p) - * 2) a positive fraction of a second (less than 1) (return as *retro_p) + * The total drift adjustment needed is stored at tdrift_p. * - * The sum of these two values is the adjustment needed. Positive means to - * advance the clock or insert seconds. Negative means to retard the clock - * or remove seconds. */ static void calculate_adjustment(const double factor, @@ -1161,24 +1162,21 @@ static void save_adjtime(const struct adjtime adjtime, const bool testing) * Do not update anything if the Hardware Clock does not currently present a * valid time. * - * Arguments and are current values from the adjtime - * file. + * means the Hardware Clock contains a valid time. * - * means the Hardware Clock contains a valid time, and that - * time is . + * is the drift corrected time read from the Hardware Clock. * - * is the current system time (to be precise, it is the system - * time at the time was read, which due to computational delay - * could be a short time ago). + * was the system time when the was read, which due + * to computational delay could be a short time ago. It is used to define a + * trigger point for setting the Hardware Clock. The fractional part of the + * Hardware clock set time is subtracted from read_time to 'refer back', or + * delay, the trigger point. Fractional parts must be accounted for in this + * way, because the Hardware Clock can only be set to a whole second. * * : the Hardware Clock is kept in UTC. * * : We are running in test mode (no updating of clock). * - * We do not bother to update the clock if the adjustment would be less than - * one second. This is to avoid cumulative error and needless CPU hogging - * (remember we use an infinite loop for some timing) if the user runs us - * frequently. */ static void do_adjustment(struct adjtime *adjtime_p, @@ -1263,11 +1261,11 @@ manipulate_clock(const bool show, const bool adjust, const bool noadjfile, */ bool hclock_valid = FALSE; /* - * The time the hardware clock had just after we - * synchronized to its next clock tick when we - * started up. Defined only if hclock_valid is true. + * Tick synchronized time read from the Hardware Clock and + * then drift correct for all operations except --show. */ struct timeval hclocktime = { 0, 0 }; + /* Total Hardware Clock drift correction needed. */ struct timeval tdrift; /* local return code */ int rc = 0; @@ -1323,6 +1321,14 @@ manipulate_clock(const bool show, const bool adjust, const bool noadjfile, return EX_IOERR; } } + /* + * Calculate Hardware Clock drift for --predict with the user + * supplied --date option time, and with the time read from the + * Hardware Clock for all other operations. Apply drift correction + * to the Hardware Clock time for everything except --show and + * --predict. For --predict negate the drift correction, because we + * want to 'predict' a future Hardware Clock time that includes drift. + */ hclocktime = predict ? t2tv(set_time) : hclocktime; calculate_adjustment(adjtime.drift_factor, adjtime.last_adj_time, @@ -1542,6 +1548,7 @@ static void usage(const char *fmt, ...) fputs(_("\nFunctions:\n"), usageto); fputs(_(" -h, --help show this help text and exit\n" " -r, --show read hardware clock and print result\n" + " --get read hardware clock and print drift corrected result\n" " --set set the RTC to the time given with --date\n"), usageto); fputs(_(" -s, --hctosys set the system time from the hardware clock\n" " -w, --systohc set the hardware clock from the current system time\n" -- cgit v1.2.3-55-g7522