phc2sys: Use nanosleep instead of usleep.

[RC: use CLOCK_MONOTONIC as suggested on the list.]

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2013-05-16 14:49:20 +02:00
parent 7ce1486d22
commit 3ae0ff1f44
1 changed files with 13 additions and 8 deletions

View File

@ -286,7 +286,7 @@ static int do_pps_loop(struct clock *clock, int fd,
} }
static int do_sysoff_loop(struct clock *clock, clockid_t src, static int do_sysoff_loop(struct clock *clock, clockid_t src,
double rate, int n_readings) struct timespec *interval, int n_readings)
{ {
uint64_t ts; uint64_t ts;
int64_t offset, delay; int64_t offset, delay;
@ -295,7 +295,7 @@ static int do_sysoff_loop(struct clock *clock, clockid_t src,
clock->source_label = "sys"; clock->source_label = "sys";
while (1) { while (1) {
usleep(1000000 / rate); clock_nanosleep(CLOCK_MONOTONIC, 0, interval, NULL);
if (sysoff_measure(fd, n_readings, &offset, &ts, &delay)) { if (sysoff_measure(fd, n_readings, &offset, &ts, &delay)) {
err = -1; err = -1;
break; break;
@ -306,7 +306,7 @@ static int do_sysoff_loop(struct clock *clock, clockid_t src,
} }
static int do_phc_loop(struct clock *clock, clockid_t src, static int do_phc_loop(struct clock *clock, clockid_t src,
double rate, int n_readings) struct timespec *interval, int n_readings)
{ {
uint64_t ts; uint64_t ts;
int64_t offset, delay; int64_t offset, delay;
@ -314,7 +314,7 @@ static int do_phc_loop(struct clock *clock, clockid_t src,
clock->source_label = "phc"; clock->source_label = "phc";
while (1) { while (1) {
usleep(1000000 / rate); clock_nanosleep(CLOCK_MONOTONIC, 0, interval, NULL);
if (!read_phc(src, clock->clkid, n_readings, if (!read_phc(src, clock->clkid, n_readings,
&offset, &ts, &delay)) { &offset, &ts, &delay)) {
continue; continue;
@ -562,7 +562,8 @@ int main(int argc, char *argv[])
int c, domain_number = 0, phc_readings = 5, pps_fd = -1; int c, domain_number = 0, phc_readings = 5, pps_fd = -1;
int max_ppb, r, wait_sync = 0, forced_sync_offset = 0; int max_ppb, r, wait_sync = 0, forced_sync_offset = 0;
int print_level = LOG_INFO, use_syslog = 1, verbose = 0; int print_level = LOG_INFO, use_syslog = 1, verbose = 0;
double ppb, phc_rate = 1.0; double ppb, phc_interval = 1.0;
struct timespec phc_interval_tp;
struct clock dst_clock = { struct clock dst_clock = {
.clkid = CLOCK_REALTIME, .clkid = CLOCK_REALTIME,
.servo_state = SERVO_UNLOCKED, .servo_state = SERVO_UNLOCKED,
@ -602,7 +603,7 @@ int main(int argc, char *argv[])
configured_pi_offset = atof(optarg); configured_pi_offset = atof(optarg);
break; break;
case 'R': case 'R':
phc_rate = atof(optarg); phc_interval = 1.0 / atof(optarg);
break; break;
case 'N': case 'N':
phc_readings = atoi(optarg); phc_readings = atoi(optarg);
@ -736,9 +737,13 @@ int main(int argc, char *argv[])
if (pps_fd >= 0) if (pps_fd >= 0)
return do_pps_loop(&dst_clock, pps_fd, src, phc_readings); return do_pps_loop(&dst_clock, pps_fd, src, phc_readings);
phc_interval_tp.tv_sec = phc_interval;
phc_interval_tp.tv_nsec = (phc_interval - phc_interval_tp.tv_sec) * 1e9;
if (dst_clock.clkid == CLOCK_REALTIME && src != CLOCK_REALTIME && if (dst_clock.clkid == CLOCK_REALTIME && src != CLOCK_REALTIME &&
SYSOFF_SUPPORTED == sysoff_probe(CLOCKID_TO_FD(src), phc_readings)) SYSOFF_SUPPORTED == sysoff_probe(CLOCKID_TO_FD(src), phc_readings))
return do_sysoff_loop(&dst_clock, src, phc_rate, phc_readings); return do_sysoff_loop(&dst_clock, src, &phc_interval_tp,
phc_readings);
return do_phc_loop(&dst_clock, src, phc_rate, phc_readings); return do_phc_loop(&dst_clock, src, &phc_interval_tp, phc_readings);
} }