[PATCH] WIP: add option to synchronize PHC instead of system clock |
[ Thread Index |
Date Index
| More chrony.tuxfamily.org/chrony-users Archives
]
- Subject: [PATCH] WIP: add option to synchronize PHC instead of system clock
- From: Miroslav Lichvar <mlichvar@xxxxxxxxxx>
- Date: Wed, 27 Feb 2019 13:17:43 +0100
---
local.c | 33 +++++++++++++++++++++++++++++++--
local.h | 4 +++-
main.c | 8 ++++++--
ntp_io.c | 8 ++++----
ntp_io_linux.c | 8 +++++---
refclock_phc.c | 4 +++-
sys_linux.c | 11 ++++++++++-
sys_timex.c | 14 ++++++++++++++
8 files changed, 76 insertions(+), 14 deletions(-)
diff --git a/local.c b/local.c
index b4baaac..f2d1d67 100644
--- a/local.c
+++ b/local.c
@@ -51,6 +51,9 @@ static double max_freq_ppm;
/* Temperature compensation, in ppm */
static double temp_comp_ppm;
+//static
+clockid_t clock_id;
+
/* ================================================== */
/* Store the system dependent drivers */
@@ -135,6 +138,8 @@ calculate_sys_precision(void)
assert(best > 0);
+ best = 10;
+
precision_quantum = 1.0e-9 * best;
/* Get rounded log2 value of the measured precision */
@@ -152,7 +157,7 @@ calculate_sys_precision(void)
/* ================================================== */
void
-LCL_Initialise(void)
+LCL_Initialise(const char *clock)
{
change_list.next = change_list.prev = &change_list;
@@ -170,6 +175,17 @@ LCL_Initialise(void)
current_freq_ppm = 0.0;
temp_comp_ppm = 0.0;
+ clock_id = CLOCK_REALTIME;
+ if (1) {
+#define FD_TO_CLOCKID(fd) ((clockid_t) ((((unsigned int) ~fd) << 3) | 3))
+ if (!clock)
+ LOG_FATAL("Unspecified -c");
+ int fd = open(clock, O_RDWR);
+ if (fd < 0)
+ LOG_FATAL("Couldn't open PTP clock");
+ clock_id = FD_TO_CLOCKID(fd);
+ }
+
calculate_sys_precision();
/* This is the maximum allowed frequency offset in ppm, the time must
@@ -357,7 +373,7 @@ void
LCL_ReadRawTime(struct timespec *ts)
{
#if HAVE_CLOCK_GETTIME
- if (clock_gettime(CLOCK_REALTIME, ts) < 0)
+ if (clock_gettime(clock_id, ts) < 0)
LOG_FATAL("clock_gettime() failed : %s", strerror(errno));
#else
struct timeval tv;
@@ -400,6 +416,19 @@ LCL_GetOffsetCorrection(struct timespec *raw, double *correction, double *err)
(*drv_offset_convert)(raw, correction, err);
}
+/* ================================================== */
+
+int
+LCL_CookSystemTime(struct timespec *raw, struct timespec *cooked, double *err)
+{
+ if (clock_id != CLOCK_REALTIME)
+ return 0;
+
+ LCL_CookTime(raw, cooked, err);
+
+ return 1;
+}
+
/* ================================================== */
/* Return current frequency */
diff --git a/local.h b/local.h
index 7ceb76a..6c98b86 100644
--- a/local.h
+++ b/local.h
@@ -53,6 +53,8 @@ extern void LCL_CookTime(struct timespec *raw, struct timespec *cooked, double *
extern void LCL_GetOffsetCorrection(struct timespec *raw, double *correction, double *err);
+extern int LCL_CookSystemTime(struct timespec *raw, struct timespec *cooked, double *err);
+
/* Type of routines that may be invoked as callbacks when there is a
change to the frequency or offset.
@@ -186,7 +188,7 @@ extern double LCL_GetMaxClockError(void);
/* Routine to initialise the module (to be called once at program
start-up) */
-extern void LCL_Initialise(void);
+extern void LCL_Initialise(const char *clock);
/* Routine to finalise the module (to be called once at end of
run). */
diff --git a/main.c b/main.c
index 6ccf32e..a8206b7 100644
--- a/main.c
+++ b/main.c
@@ -401,6 +401,7 @@ int main
{
const char *conf_file = DEFAULT_CONF_FILE;
const char *progname = argv[0];
+ const char *clock = NULL;
char *user = NULL, *log_file = NULL;
struct passwd *pw;
int opt, debug = 0, nofork = 0, address_family = IPADDR_UNSPEC;
@@ -427,12 +428,15 @@ int main
optind = 1;
/* Parse short command-line options */
- while ((opt = getopt(argc, argv, "46df:F:hl:mnP:qQrRst:u:vx")) != -1) {
+ while ((opt = getopt(argc, argv, "46df:F:hl:mnP:qQrRst:u:vxc:")) != -1) {
switch (opt) {
case '4':
case '6':
address_family = opt == '4' ? IPADDR_INET4 : IPADDR_INET6;
break;
+ case 'c':
+ clock = optarg;
+ break;
case 'd':
debug++;
nofork = 1;
@@ -544,7 +548,7 @@ int main
write_pidfile();
PRV_Initialise();
- LCL_Initialise();
+ LCL_Initialise(clock);
SCH_Initialise();
SYS_Initialise(clock_control);
RTC_Initialise(do_init_rtc);
diff --git a/ntp_io.c b/ntp_io.c
index ab08372..3a254fe 100644
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -677,8 +677,8 @@ process_message(struct msghdr *hdr, int length, int sock_fd)
memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
UTI_TimevalToTimespec(&tv, &ts);
- LCL_CookTime(&ts, &local_ts.ts, &local_ts.err);
- local_ts.source = NTP_TS_KERNEL;
+ if (LCL_CookSystemTime(&ts, &local_ts.ts, &local_ts.err))
+ local_ts.source = NTP_TS_KERNEL;
}
#endif
@@ -687,8 +687,8 @@ process_message(struct msghdr *hdr, int length, int sock_fd)
struct timespec ts;
memcpy(&ts, CMSG_DATA(cmsg), sizeof (ts));
- LCL_CookTime(&ts, &local_ts.ts, &local_ts.err);
- local_ts.source = NTP_TS_KERNEL;
+ if (LCL_CookSystemTime(&ts, &local_ts.ts, &local_ts.err))
+ local_ts.source = NTP_TS_KERNEL;
}
#endif
}
diff --git a/ntp_io_linux.c b/ntp_io_linux.c
index eb4a3a8..d13e060 100644
--- a/ntp_io_linux.c
+++ b/ntp_io_linux.c
@@ -594,7 +594,9 @@ process_hw_timestamp(struct Interface *iface, struct timespec *hw_ts,
UTI_AddDoubleToTimespec(hw_ts, rx_correction, hw_ts);
}
- if (!HCL_CookTime(iface->clock, hw_ts, &ts, &local_err))
+ if (1)
+ LCL_CookTime(hw_ts, &ts, &local_err);
+ else if (!HCL_CookTime(iface->clock, hw_ts, &ts, &local_err))
return;
if (!rx_ntp_length && iface->tx_comp)
@@ -757,8 +759,8 @@ NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *loc
if (local_ts->source == NTP_TS_DAEMON && !UTI_IsZeroTimespec(&ts3.ts[0]) &&
(!is_tx || UTI_IsZeroTimespec(&ts3.ts[2]))) {
- LCL_CookTime(&ts3.ts[0], &local_ts->ts, &local_ts->err);
- local_ts->source = NTP_TS_KERNEL;
+ if (LCL_CookSystemTime(&ts3.ts[0], &local_ts->ts, &local_ts->err))
+ local_ts->source = NTP_TS_KERNEL;
}
}
diff --git a/refclock_phc.c b/refclock_phc.c
index 03450db..47f2712 100644
--- a/refclock_phc.c
+++ b/refclock_phc.c
@@ -131,7 +131,9 @@ static void read_ext_pulse(int fd, int event, void *anything)
return;
}
- if (!HCL_CookTime(phc->clock, &phc_ts, &local_ts, &local_err))
+ if (1)
+ LCL_CookTime(&phc_ts, &local_ts, &local_err);
+ else if (!HCL_CookTime(phc->clock, &phc_ts, &local_ts, &local_err))
return;
RCL_AddCookedPulse(instance, &local_ts, 1.0e-9 * local_ts.tv_nsec, local_err,
diff --git a/sys_linux.c b/sys_linux.c
index 7688d51..e72b4c9 100644
--- a/sys_linux.c
+++ b/sys_linux.c
@@ -404,6 +404,14 @@ report_time_adjust_blockers(void)
void
SYS_Linux_Initialise(void)
{
+ if (1) {
+ SYS_Timex_InitialiseWithFunctions(500.0,
+ 1.0 / 1000.0,
+ NULL, NULL, apply_step_offset,
+ 0.0, 0.0, NULL, NULL);
+ return;
+ }
+
get_version_specific_details();
report_time_adjust_blockers();
@@ -417,7 +425,8 @@ SYS_Linux_Initialise(void)
SYS_Timex_InitialiseWithFunctions(1.0e6 * max_tick_bias / nominal_tick,
1.0 / tick_update_hz,
- read_frequency, set_frequency,
+ NULL, NULL,
+ //read_frequency, set_frequency,
have_setoffset ? apply_step_offset : NULL,
0.0, 0.0, NULL, NULL);
}
diff --git a/sys_timex.c b/sys_timex.c
index e54ad24..23fb448 100644
--- a/sys_timex.c
+++ b/sys_timex.c
@@ -66,6 +66,15 @@ static int sys_status;
/* Saved TAI-UTC offset */
static int sys_tai_offset;
+#undef MOD_STATUS
+#define MOD_STATUS 0
+#undef MOD_OFFSET
+#define MOD_OFFSET 0
+#undef MOD_MAXERROR
+#define MOD_MAXERROR 0
+#undef MOD_ESTERROR
+#define MOD_ESTERROR 0
+
/* ================================================== */
static double
@@ -242,6 +251,7 @@ SYS_Timex_Finalise(void)
/* ================================================== */
+extern clockid_t clock_id;
int
SYS_Timex_Adjust(struct timex *txc, int ignore_error)
{
@@ -253,7 +263,11 @@ SYS_Timex_Adjust(struct timex *txc, int ignore_error)
txc->constant = 10;
#endif
+#if 0
state = NTP_ADJTIME(txc);
+#else
+ state = clock_adjtime(clock_id, txc);
+#endif
if (state < 0) {
if (!ignore_error)
--
2.17.2
--bp/iNruPH9dso1Pn--
--
To unsubscribe email chrony-users-request@xxxxxxxxxxxxxxxxxxxx
with "unsubscribe" in the subject.
For help email chrony-users-request@xxxxxxxxxxxxxxxxxxxx
with "help" in the subject.
Trouble? Email listmaster@xxxxxxxxxxxxxxxxxxxx.