static void handle_initial_trim(void) { double rate; long delta_time; double rtc_error_now, sys_error_now; /* The idea is to accumulate some number of samples at 1 second intervals, then do a robust regression fit to this. This should give a good fix on the intercept (=system clock error rel to RTC) at a particular time, removing risk of any particular sample being an outlier. We can then look at the elapsed interval since the epoch recorded in the RTC file, and correct the system time accordingly. */ run_regression(1, &coefs_valid, &coef_ref_time, &coef_seconds_fast, &coef_gain_rate); n_samples_since_regression = 0; /* Set sample number to -1 so the next sample is not used, as it will not yet be corrected for System Trim*/ n_samples = -1; read_coefs_from_file(); if (valid_coefs_from_file) { /* Can process data */ delta_time = coef_ref_time - file_ref_time; rate = 1.0e-6 * file_rate_ppm; rtc_error_now = file_ref_offset + rate * (double) delta_time; /* sys_error_now is positive if the system clock is fast */ sys_error_now = rtc_error_now - coef_seconds_fast; LOG(LOGS_INFO, LOGF_RtcLinux, "System trim from RTC = %f", sys_error_now); LCL_AccumulateOffset(sys_error_now, 0.0); } else { LOG(LOGS_WARN, LOGF_RtcLinux, "No valid file coefficients, cannot trim system time"); } coefs_valid = 0; (after_init_hook)(after_init_hook_arg); operating_mode = OM_NORMAL; return; }
static void process_reading(time_t rtc_time, struct timeval *system_time) { double rtc_fast; accumulate_sample(rtc_time, system_time); switch (operating_mode) { case OM_NORMAL: if (n_samples_since_regression >= N_SAMPLES_PER_REGRESSION) { run_regression(1, &coefs_valid, &coef_ref_time, &coef_seconds_fast, &coef_gain_rate); n_samples_since_regression = 0; maybe_autotrim(); } break; case OM_INITIAL: if (n_samples_since_regression >= 8) { handle_initial_trim(); } break; case OM_AFTERTRIM: if (n_samples_since_regression >= 8) { handle_relock_after_trim(); } break; default: assert(0); break; } if (logfileid != -1) { rtc_fast = (double)(rtc_time - system_time->tv_sec) - 1.0e-6 * (double) system_time->tv_usec; LOG_FileWrite(logfileid, "%s %14.6f %1d %14.6f %12.3f %2d %2d %4d", UTI_TimeToLogForm(system_time->tv_sec), rtc_fast, coefs_valid, coef_seconds_fast, coef_gain_rate * 1.0e6, n_samples, n_runs, measurement_period); } }
static void handle_relock_after_trim(void) { int valid; time_t ref; double fast, slope; run_regression(1, &valid, &ref, &fast, &slope); if (valid) { write_coefs_to_file(1,ref,fast,saved_coef_gain_rate); } else { LOG(LOGS_WARN, LOGF_RtcLinux, "Could not do regression after trim"); } coefs_valid = 0; n_samples = 0; n_samples_since_regression = 0; operating_mode = OM_NORMAL; measurement_period = LOWEST_MEASUREMENT_PERIOD; }