Discussion:
[Linuxptp-devel] [PATCH] phc2sys: Use nanosleep instead of usleep.
Miroslav Lichvar
2013-05-10 15:49:21 UTC
Permalink
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
phc2sys.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/phc2sys.c b/phc2sys.c
index dbe2d9a..6264d56 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -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,
- double rate, int n_readings)
+ struct timespec *interval, int n_readings)
{
uint64_t ts;
int64_t offset, delay;
@@ -295,7 +295,7 @@ static int do_sysoff_loop(struct clock *clock, clockid_t src,
clock->source_label = "sys";

while (1) {
- usleep(1000000 / rate);
+ nanosleep(interval, NULL);
if (sysoff_measure(fd, n_readings, &offset, &ts, &delay)) {
err = -1;
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,
- double rate, int n_readings)
+ struct timespec *interval, int n_readings)
{
uint64_t ts;
int64_t offset, delay;
@@ -314,7 +314,7 @@ static int do_phc_loop(struct clock *clock, clockid_t src,
clock->source_label = "phc";

while (1) {
- usleep(1000000 / rate);
+ nanosleep(interval, NULL);
if (!read_phc(src, clock->clkid, n_readings,
&offset, &ts, &delay)) {
continue;
@@ -562,7 +562,8 @@ int main(int argc, char *argv[])
int c, domain_number = 0, phc_readings = 5, pps_fd = -1;
int max_ppb, r, wait_sync = 0, forced_sync_offset = 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 = {
.clkid = CLOCK_REALTIME,
.servo_state = SERVO_UNLOCKED,
@@ -602,7 +603,7 @@ int main(int argc, char *argv[])
configured_pi_offset = atof(optarg);
break;
case 'R':
- phc_rate = atof(optarg);
+ phc_interval = 1.0 / atof(optarg);
break;
case 'N':
phc_readings = atoi(optarg);
@@ -736,9 +737,13 @@ int main(int argc, char *argv[])
if (pps_fd >= 0)
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 &&
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);
}
--
1.8.1.4
Stephan Gatzka
2013-05-10 16:35:11 UTC
Permalink
Post by Miroslav Lichvar
while (1) {
- usleep(1000000 / rate);
+ nanosleep(interval, NULL);
I'm wondering if clock_nanosleep with CLOCK_MONOTONIC or even
CLOCK_MONOTONIC_RAW wouldn't be better. They are not subject to time jumps.

Regards,

Stephan
Richard Cochran
2013-05-16 12:51:47 UTC
Permalink
Post by Miroslav Lichvar
---
phc2sys.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
Applied, but using CLOCK_MONOTONIC as suggested by Stephan.

Thanks,
Richard
Miroslav Lichvar
2013-05-16 13:06:02 UTC
Permalink
Post by Richard Cochran
Applied, but using CLOCK_MONOTONIC as suggested by Stephan.
Thanks. I'm not sure if there will be any difference with
clock_nanosleep() though.
Richard Cochran
2013-05-16 17:54:07 UTC
Permalink
Post by Miroslav Lichvar
Post by Richard Cochran
Applied, but using CLOCK_MONOTONIC as suggested by Stephan.
Thanks. I'm not sure if there will be any difference with
clock_nanosleep() though.
Miroslav Lichvar
2013-05-17 07:02:54 UTC
Permalink
Darn, the commit lost the author. I messed it up, sorry.
I'll be more careful next time.
No problem, it was your idea anyway :).
--
Miroslav Lichvar
Stephan Gatzka
2013-05-17 05:28:50 UTC
Permalink
Hi!
Post by Miroslav Lichvar
Thanks. I'm not sure if there will be any difference with
clock_nanosleep() though.
Loading...