Miroslav Lichvar
2012-09-05 15:00:31 UTC
On start, wait until the PHC and system clock are closer than the
specified limit. This can be used to avoid setting the system clock to
PHC until it's synchronized, assuming the system clock is already
close to the right time.
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
phc2sys.c | 27 ++++++++++++++++++++-------
1 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/phc2sys.c b/phc2sys.c
index b07701e..a035be8 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -206,6 +206,7 @@ static void usage(char *progname)
" -h prints this message and exits\n"
" -r [val] reading the PHC device takes 'val' nanoseconds\n"
" -s [device] set the time from this PHC device\n"
+ " -m [val] wait until offset is below 'val' seconds\n"
" -P [val] set proportional constant to 'val'\n"
" -I [val] set integration constant to 'val'\n"
" -R [val] set PHC update rate to 'val' Hz\n"
@@ -220,13 +221,13 @@ int main(int argc, char *argv[])
char *device = NULL, *progname;
clockid_t src = CLOCK_INVALID, dst = CLOCK_REALTIME;
uint64_t pps_ts, phc_ts;
- int64_t pps_offset, phc_offset;
+ int64_t pps_offset, phc_offset, max_offset = 0;
int c, fd = 0, rdelay = 0, phc_readings = 5, phc_rate = 1;
/* Process the command line arguments. */
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
- while (EOF != (c = getopt(argc, argv, "c:d:hr:s:P:I:R:N:"))) {
+ while (EOF != (c = getopt(argc, argv, "c:d:hr:s:m:P:I:R:N:"))) {
switch (c) {
case 'c':
dst = clock_open(optarg);
@@ -240,6 +241,9 @@ int main(int argc, char *argv[])
case 's':
src = clock_open(optarg);
break;
+ case 'm':
+ max_offset = atof(optarg) * 1e9;
+ break;
case 'P':
kp = atof(optarg);
break;
@@ -273,11 +277,20 @@ int main(int argc, char *argv[])
}
}
if (src != CLOCK_INVALID) {
- struct timespec now;
- if (clock_gettime(src, &now))
- perror("clock_gettime");
- if (clock_settime(dst, &now))
- perror("clock_settime");
+ while (1) {
+ if (!read_phc(src, dst, rdelay, phc_readings,
+ &phc_offset, &phc_ts))
+ return 1;
+
+ if (max_offset <= 0 || (phc_offset < max_offset &&
+ phc_offset > -max_offset))
+ break;
+
+ /* Try again later. */
+ usleep(1000000);
+ continue;
+ }
+ clock_step(dst, -phc_offset);
}
while (1) {
if (fd > 0) {
specified limit. This can be used to avoid setting the system clock to
PHC until it's synchronized, assuming the system clock is already
close to the right time.
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
phc2sys.c | 27 ++++++++++++++++++++-------
1 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/phc2sys.c b/phc2sys.c
index b07701e..a035be8 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -206,6 +206,7 @@ static void usage(char *progname)
" -h prints this message and exits\n"
" -r [val] reading the PHC device takes 'val' nanoseconds\n"
" -s [device] set the time from this PHC device\n"
+ " -m [val] wait until offset is below 'val' seconds\n"
" -P [val] set proportional constant to 'val'\n"
" -I [val] set integration constant to 'val'\n"
" -R [val] set PHC update rate to 'val' Hz\n"
@@ -220,13 +221,13 @@ int main(int argc, char *argv[])
char *device = NULL, *progname;
clockid_t src = CLOCK_INVALID, dst = CLOCK_REALTIME;
uint64_t pps_ts, phc_ts;
- int64_t pps_offset, phc_offset;
+ int64_t pps_offset, phc_offset, max_offset = 0;
int c, fd = 0, rdelay = 0, phc_readings = 5, phc_rate = 1;
/* Process the command line arguments. */
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
- while (EOF != (c = getopt(argc, argv, "c:d:hr:s:P:I:R:N:"))) {
+ while (EOF != (c = getopt(argc, argv, "c:d:hr:s:m:P:I:R:N:"))) {
switch (c) {
case 'c':
dst = clock_open(optarg);
@@ -240,6 +241,9 @@ int main(int argc, char *argv[])
case 's':
src = clock_open(optarg);
break;
+ case 'm':
+ max_offset = atof(optarg) * 1e9;
+ break;
case 'P':
kp = atof(optarg);
break;
@@ -273,11 +277,20 @@ int main(int argc, char *argv[])
}
}
if (src != CLOCK_INVALID) {
- struct timespec now;
- if (clock_gettime(src, &now))
- perror("clock_gettime");
- if (clock_settime(dst, &now))
- perror("clock_settime");
+ while (1) {
+ if (!read_phc(src, dst, rdelay, phc_readings,
+ &phc_offset, &phc_ts))
+ return 1;
+
+ if (max_offset <= 0 || (phc_offset < max_offset &&
+ phc_offset > -max_offset))
+ break;
+
+ /* Try again later. */
+ usleep(1000000);
+ continue;
+ }
+ clock_step(dst, -phc_offset);
}
while (1) {
if (fd > 0) {
--
1.7.7.6
1.7.7.6