All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ptp: measure the time offset between PHC and system clock
@ 2013-09-14  8:03 Dong Zhu
  2013-09-14 14:31 ` Richard Cochran
  2013-09-14 19:21 ` [PATCH] ptp: measure the time offset between PHC and system clock Sergei Shtylyov
  0 siblings, 2 replies; 8+ messages in thread
From: Dong Zhu @ 2013-09-14  8:03 UTC (permalink / raw)
  To: Richard Cochran, Rob Landley; +Cc: netdev, linux-doc, linux-kernel

This patch add a method into testptp.c to measure the time offset
between phc and system clock through the ioctl PTP_SYS_OFFSET.

Signed-off-by: Dong Zhu <bluezhudong@gmail.com>
---
 Documentation/ptp/testptp.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
index f59ded0..72bb030 100644
--- a/Documentation/ptp/testptp.c
+++ b/Documentation/ptp/testptp.c
@@ -112,6 +112,7 @@ static void usage(char *progname)
 		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
 		" -g         get the ptp clock time\n"
 		" -h         prints this message\n"
+		" -k val     measure the time offset between PHC and system clock\n"
 		" -p val     enable output with a period of 'val' nanoseconds\n"
 		" -P val     enable or disable (val=1|0) the system clock PPS\n"
 		" -s         set the ptp clock time from the system time\n"
@@ -133,8 +134,12 @@ int main(int argc, char *argv[])
 	struct itimerspec timeout;
 	struct sigevent sigevent;
 
+	struct ptp_clock_time *pct;
+	struct ptp_sys_offset *sysoff;
+
+
 	char *progname;
-	int c, cnt, fd;
+	int i, c, cnt, fd;
 
 	char *device = DEVICE;
 	clockid_t clkid;
@@ -144,6 +149,8 @@ int main(int argc, char *argv[])
 	int extts = 0;
 	int gettime = 0;
 	int oneshot = 0;
+	int offset = 0;
+	int n_samples = 0;
 	int periodic = 0;
 	int perout = -1;
 	int pps = -1;
@@ -151,7 +158,7 @@ int main(int argc, char *argv[])
 
 	progname = strrchr(argv[0], '/');
 	progname = progname ? 1+progname : argv[0];
-	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) {
+	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) {
 		switch (c) {
 		case 'a':
 			oneshot = atoi(optarg);
@@ -174,6 +181,10 @@ int main(int argc, char *argv[])
 		case 'g':
 			gettime = 1;
 			break;
+		case 'k':
+			offset = 1;
+			n_samples = atoi(optarg);
+			break;
 		case 'p':
 			perout = atoi(optarg);
 			break;
@@ -376,6 +387,31 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (offset) {
+		sysoff = calloc(1, sizeof(*sysoff));
+		if (!sysoff) {
+			perror("calloc");
+			return -1;
+		}
+		sysoff->n_samples = n_samples;
+
+		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
+			perror("PTP_SYS_OFFSET");
+		else
+			puts("time offset between PHC and
+					 system clock request okay");
+
+		pct = &sysoff->ts[0];
+		for (i = 0; i < sysoff->n_samples; i++, pct++) {
+			printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
+			pct++;
+			printf("phc    time: %ld.%ld\n\n", pct->sec, pct->nsec);
+		}
+		printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
+
+		free(sysoff);
+	}
+
 	close(fd);
 	return 0;
 }
-- 
1.7.11.7


-- 
Best Regards,
Dong Zhu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] ptp: measure the time offset between PHC and system clock
  2013-09-14  8:03 [PATCH] ptp: measure the time offset between PHC and system clock Dong Zhu
@ 2013-09-14 14:31 ` Richard Cochran
  2013-09-14 15:39   ` [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program clock Dong Zhu
  2013-09-14 19:21 ` [PATCH] ptp: measure the time offset between PHC and system clock Sergei Shtylyov
  1 sibling, 1 reply; 8+ messages in thread
From: Richard Cochran @ 2013-09-14 14:31 UTC (permalink / raw)
  To: Dong Zhu; +Cc: Rob Landley, netdev, linux-doc, linux-kernel

On Sat, Sep 14, 2013 at 04:03:06PM +0800, Dong Zhu wrote:
> This patch add a method into testptp.c to measure the time offset
> between phc and system clock through the ioctl PTP_SYS_OFFSET.
> 

This is a nice addition to the testptp program. I do have a few
comments, below.

First off, the subject line should mention testptp. How about this?

    [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program

> Signed-off-by: Dong Zhu <bluezhudong@gmail.com>
> ---
>  Documentation/ptp/testptp.c | 40 ++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 38 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
> index f59ded0..72bb030 100644
> --- a/Documentation/ptp/testptp.c
> +++ b/Documentation/ptp/testptp.c
> @@ -112,6 +112,7 @@ static void usage(char *progname)
>  		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
>  		" -g         get the ptp clock time\n"
>  		" -h         prints this message\n"
> +		" -k val     measure the time offset between PHC and system clock\n"

The help message should tell the user what 'val' is.

>  		" -p val     enable output with a period of 'val' nanoseconds\n"
>  		" -P val     enable or disable (val=1|0) the system clock PPS\n"
>  		" -s         set the ptp clock time from the system time\n"
> @@ -133,8 +134,12 @@ int main(int argc, char *argv[])
>  	struct itimerspec timeout;
>  	struct sigevent sigevent;
>  
> +	struct ptp_clock_time *pct;
> +	struct ptp_sys_offset *sysoff;
> +
> +
>  	char *progname;
> -	int c, cnt, fd;
> +	int i, c, cnt, fd;
>  
>  	char *device = DEVICE;
>  	clockid_t clkid;
> @@ -144,6 +149,8 @@ int main(int argc, char *argv[])
>  	int extts = 0;
>  	int gettime = 0;
>  	int oneshot = 0;
> +	int offset = 0;
> +	int n_samples = 0;
>  	int periodic = 0;
>  	int perout = -1;
>  	int pps = -1;
> @@ -151,7 +158,7 @@ int main(int argc, char *argv[])
>  
>  	progname = strrchr(argv[0], '/');
>  	progname = progname ? 1+progname : argv[0];
> -	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) {
> +	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) {
>  		switch (c) {
>  		case 'a':
>  			oneshot = atoi(optarg);
> @@ -174,6 +181,10 @@ int main(int argc, char *argv[])
>  		case 'g':
>  			gettime = 1;
>  			break;
> +		case 'k':
> +			offset = 1;
> +			n_samples = atoi(optarg);
> +			break;
>  		case 'p':
>  			perout = atoi(optarg);
>  			break;
> @@ -376,6 +387,31 @@ int main(int argc, char *argv[])
>  		}
>  	}
>  
> +	if (offset) {
> +		sysoff = calloc(1, sizeof(*sysoff));
> +		if (!sysoff) {
> +			perror("calloc");
> +			return -1;
> +		}
> +		sysoff->n_samples = n_samples;
> +
> +		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
> +			perror("PTP_SYS_OFFSET");
> +		else
> +			puts("time offset between PHC and
> +					 system clock request okay");
> +
> +		pct = &sysoff->ts[0];
> +		for (i = 0; i < sysoff->n_samples; i++, pct++) {
> +			printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
> +			pct++;
> +			printf("phc    time: %ld.%ld\n\n", pct->sec, pct->nsec);
                                                    ^^^^
I think the output would look nicer with only one newline. After all,
each measurement is a {sys,phc,sys} triplet and not a {sys,phc} pair.

> +		}
> +		printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
> +
> +		free(sysoff);
> +	}
> +

Thanks,
Richard

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program clock
  2013-09-14 14:31 ` Richard Cochran
@ 2013-09-14 15:39   ` Dong Zhu
  2013-09-15  8:48     ` Richard Cochran
  0 siblings, 1 reply; 8+ messages in thread
From: Dong Zhu @ 2013-09-14 15:39 UTC (permalink / raw)
  To: Richard Cochran; +Cc: Rob Landley, netdev, linux-doc, linux-kernel

On Sat, Sep 14, 2013 at 04:31:46PM +0200, Richard Cochran wrote:
> On Sat, Sep 14, 2013 at 04:03:06PM +0800, Dong Zhu wrote:
> > This patch add a method into testptp.c to measure the time offset
> > between phc and system clock through the ioctl PTP_SYS_OFFSET.
> > 
> 
> This is a nice addition to the testptp program. I do have a few
> comments, below.
> 

Thanks very much for your comments, I have modified the patch as below,
Cuold you have a look at it again ? Any comments would be appreciated.

>From 655b45785a85599d5fff5eb3b8d9b49b72f2991f Mon Sep 17 00:00:00 2001
From: Dong Zhu <bluezhudong@gmail.com> 
Date: Sat, 14 Sep 2013 23:32:14 +0800

This patch add a method into testptp.c to measure the time offset
between phc and system clock through the ioctl PTP_SYS_OFFSET.

Signed-off-by: Dong Zhu <bluezhudong@gmail.com>
---
 Documentation/ptp/testptp.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
index f59ded0..8acdc70 100644
--- a/Documentation/ptp/testptp.c
+++ b/Documentation/ptp/testptp.c
@@ -112,6 +112,8 @@ static void usage(char *progname)
 		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
 		" -g         get the ptp clock time\n"
 		" -h         prints this message\n"
+		" -k val     measure the time offset between phc and system clock "
+		"for 'val' times (Maximum 25)\n"
 		" -p val     enable output with a period of 'val' nanoseconds\n"
 		" -P val     enable or disable (val=1|0) the system clock PPS\n"
 		" -s         set the ptp clock time from the system time\n"
@@ -133,8 +135,12 @@ int main(int argc, char *argv[])
 	struct itimerspec timeout;
 	struct sigevent sigevent;
 
+	struct ptp_clock_time *pct;
+	struct ptp_sys_offset *sysoff;
+
+
 	char *progname;
-	int c, cnt, fd;
+	int i, c, cnt, fd;
 
 	char *device = DEVICE;
 	clockid_t clkid;
@@ -144,6 +150,8 @@ int main(int argc, char *argv[])
 	int extts = 0;
 	int gettime = 0;
 	int oneshot = 0;
+	int offset = 0;
+	int n_samples = 0;
 	int periodic = 0;
 	int perout = -1;
 	int pps = -1;
@@ -151,7 +159,7 @@ int main(int argc, char *argv[])
 
 	progname = strrchr(argv[0], '/');
 	progname = progname ? 1+progname : argv[0];
-	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) {
+	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) {
 		switch (c) {
 		case 'a':
 			oneshot = atoi(optarg);
@@ -174,6 +182,10 @@ int main(int argc, char *argv[])
 		case 'g':
 			gettime = 1;
 			break;
+		case 'k':
+			offset = 1;
+			n_samples = atoi(optarg);
+			break;
 		case 'p':
 			perout = atoi(optarg);
 			break;
@@ -376,6 +388,30 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (offset) {
+		sysoff = calloc(1, sizeof(*sysoff));
+		if (!sysoff) {
+			perror("calloc");
+			return -1;
+		}
+		sysoff->n_samples = n_samples;
+
+		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
+			perror("PTP_SYS_OFFSET");
+		else
+			puts("phc and system clock time offset request okay");
+
+		pct = &sysoff->ts[0];
+		for (i = 0; i < sysoff->n_samples; i++, pct++) {
+			printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
+			pct++;
+			printf("phc    time: %ld.%ld\n", pct->sec, pct->nsec);
+		}
+		printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
+
+		free(sysoff);
+	}
+
 	close(fd);
 	return 0;
 }
-- 
1.7.11.7

-- 
Best Regards,
Dong Zhu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] ptp: measure the time offset between PHC and system clock
  2013-09-14  8:03 [PATCH] ptp: measure the time offset between PHC and system clock Dong Zhu
  2013-09-14 14:31 ` Richard Cochran
@ 2013-09-14 19:21 ` Sergei Shtylyov
  1 sibling, 0 replies; 8+ messages in thread
From: Sergei Shtylyov @ 2013-09-14 19:21 UTC (permalink / raw)
  To: Dong Zhu; +Cc: Richard Cochran, Rob Landley, netdev, linux-doc, linux-kernel

Hello.

On 09/14/2013 12:03 PM, Dong Zhu wrote:

> This patch add a method into testptp.c to measure the time offset
> between phc and system clock through the ioctl PTP_SYS_OFFSET.

> Signed-off-by: Dong Zhu <bluezhudong@gmail.com>
> ---
>   Documentation/ptp/testptp.c | 40 ++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 38 insertions(+), 2 deletions(-)

> diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
> index f59ded0..72bb030 100644
> --- a/Documentation/ptp/testptp.c
> +++ b/Documentation/ptp/testptp.c
[...]
> @@ -376,6 +387,31 @@ int main(int argc, char *argv[])
>   		}
>   	}
>
> +	if (offset) {
> +		sysoff = calloc(1, sizeof(*sysoff));
> +		if (!sysoff) {
> +			perror("calloc");
> +			return -1;
> +		}
> +		sysoff->n_samples = n_samples;
> +
> +		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
> +			perror("PTP_SYS_OFFSET");
> +		else
> +			puts("time offset between PHC and
> +					 system clock request okay");

    Don't break the string constant that way, there'll be all spaces between 
"and" and "system" included in it. Do it like this:

			puts("time offset between PHC and "
			     "system clock request okay");

WBR, Sergei


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program clock
  2013-09-14 15:39   ` [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program clock Dong Zhu
@ 2013-09-15  8:48     ` Richard Cochran
  2013-09-15  9:25       ` [PATCH net-next v3] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program Dong Zhu
  2013-09-17  7:32       ` [PATCH net-next v4] " Dong Zhu
  0 siblings, 2 replies; 8+ messages in thread
From: Richard Cochran @ 2013-09-15  8:48 UTC (permalink / raw)
  To: Dong Zhu; +Cc: Rob Landley, netdev, linux-doc, linux-kernel

On Sat, Sep 14, 2013 at 11:39:52PM +0800, Dong Zhu wrote:
> On Sat, Sep 14, 2013 at 04:31:46PM +0200, Richard Cochran wrote:
> > On Sat, Sep 14, 2013 at 04:03:06PM +0800, Dong Zhu wrote:
> > > This patch add a method into testptp.c to measure the time offset
> > > between phc and system clock through the ioctl PTP_SYS_OFFSET.
> > > 
> > 
> > This is a nice addition to the testptp program. I do have a few
> > comments, below.
> > 
> 
> Thanks very much for your comments, I have modified the patch as below,
> Cuold you have a look at it again ? Any comments would be appreciated.

It looks better, but could you please tweak a few more things?

> Subject: Re: [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program clock

The subject line has the word "clock" at the end by mistake.
 
...

> diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
> index f59ded0..8acdc70 100644
> --- a/Documentation/ptp/testptp.c
> +++ b/Documentation/ptp/testptp.c
> @@ -112,6 +112,8 @@ static void usage(char *progname)
>  		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
>  		" -g         get the ptp clock time\n"
>  		" -h         prints this message\n"
> +		" -k val     measure the time offset between phc and system clock "
> +		"for 'val' times (Maximum 25)\n"

This line is getting a bit too long for the terminal. Please line up
the text, like this:

		" -k val     measure the time offset between phc and system clock\n"
		"            for 'val' times (Maximum 25)\n"

Also, when you resubmit the patch, add "net-next" and a patch version,
like this: [PATCH net-next v3].

Thanks,
Richard

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v3] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program
  2013-09-15  8:48     ` Richard Cochran
@ 2013-09-15  9:25       ` Dong Zhu
  2013-09-17  7:32       ` [PATCH net-next v4] " Dong Zhu
  1 sibling, 0 replies; 8+ messages in thread
From: Dong Zhu @ 2013-09-15  9:25 UTC (permalink / raw)
  To: Richard Cochran; +Cc: Rob Landley, netdev, linux-doc, linux-kernel

Hi Richard, 

Thanks for your comments, I modified the patch and resubmit it again:

>From 7636f69b74c34eca14c85fd2d518da6044b94f53 Mon Sep 17 00:00:00 2001
From: Dong Zhu <bluezhudong@gmail.com> 
Date: Sun, 15 Sep 2013 17:12:52 +0800

This patch add a method into testptp.c to measure the time offset
between phc and system clock through the ioctl PTP_SYS_OFFSET.

Signed-off-by: Dong Zhu <bluezhudong@gmail.com>
---
 Documentation/ptp/testptp.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
index f59ded0..b6f6f47 100644
--- a/Documentation/ptp/testptp.c
+++ b/Documentation/ptp/testptp.c
@@ -112,6 +112,8 @@ static void usage(char *progname)
 		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
 		" -g         get the ptp clock time\n"
 		" -h         prints this message\n"
+		" -k val     measure the time offset between phc and system clock\n"
+		"            for 'val' times (Maximum 25)\n"
 		" -p val     enable output with a period of 'val' nanoseconds\n"
 		" -P val     enable or disable (val=1|0) the system clock PPS\n"
 		" -s         set the ptp clock time from the system time\n"
@@ -133,8 +135,12 @@ int main(int argc, char *argv[])
 	struct itimerspec timeout;
 	struct sigevent sigevent;
 
+	struct ptp_clock_time *pct;
+	struct ptp_sys_offset *sysoff;
+
+
 	char *progname;
-	int c, cnt, fd;
+	int i, c, cnt, fd;
 
 	char *device = DEVICE;
 	clockid_t clkid;
@@ -144,6 +150,8 @@ int main(int argc, char *argv[])
 	int extts = 0;
 	int gettime = 0;
 	int oneshot = 0;
+	int offset = 0;
+	int n_samples = 0;
 	int periodic = 0;
 	int perout = -1;
 	int pps = -1;
@@ -151,7 +159,7 @@ int main(int argc, char *argv[])
 
 	progname = strrchr(argv[0], '/');
 	progname = progname ? 1+progname : argv[0];
-	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) {
+	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) {
 		switch (c) {
 		case 'a':
 			oneshot = atoi(optarg);
@@ -174,6 +182,10 @@ int main(int argc, char *argv[])
 		case 'g':
 			gettime = 1;
 			break;
+		case 'k':
+			offset = 1;
+			n_samples = atoi(optarg);
+			break;
 		case 'p':
 			perout = atoi(optarg);
 			break;
@@ -376,6 +388,30 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (offset) {
+		sysoff = calloc(1, sizeof(*sysoff));
+		if (!sysoff) {
+			perror("calloc");
+			return -1;
+		}
+		sysoff->n_samples = n_samples;
+
+		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
+			perror("PTP_SYS_OFFSET");
+		else
+			puts("phc and system clock time offset request okay");
+
+		pct = &sysoff->ts[0];
+		for (i = 0; i < sysoff->n_samples; i++, pct++) {
+			printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
+			pct++;
+			printf("phc    time: %ld.%ld\n", pct->sec, pct->nsec);
+		}
+		printf("system time: %ld.%ld\n", pct->sec, pct->nsec);
+
+		free(sysoff);
+	}
+
 	close(fd);
 	return 0;
 }
-- 
1.7.11.7


-- 
Best Regards,
Dong Zhu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v4] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program
  2013-09-15  8:48     ` Richard Cochran
  2013-09-15  9:25       ` [PATCH net-next v3] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program Dong Zhu
@ 2013-09-17  7:32       ` Dong Zhu
  2013-09-23 20:46         ` David Miller
  1 sibling, 1 reply; 8+ messages in thread
From: Dong Zhu @ 2013-09-17  7:32 UTC (permalink / raw)
  To: Richard Cochran; +Cc: Rob Landley, netdev, linux-doc, linux-kernel

Hi Richard,

I developed a new patch and added a method to estimate the time offset
between system and phc clock.

Could you help reviewing it again ? If it do make sense I hope it could
be accepted.

Thanks !

>From e524e3b68f3df3cd91acd814490d092bad05386b Mon Sep 17 00:00:00 2001

This patch add a method into testptp.c to measure the time offset
between phc and system clock through the ioctl PTP_SYS_OFFSET.

Signed-off-by: Dong Zhu <bluezhudong@gmail.com>
---
 Documentation/ptp/testptp.c | 65 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 2 deletions(-)

diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c
index f59ded0..a74d0a8 100644
--- a/Documentation/ptp/testptp.c
+++ b/Documentation/ptp/testptp.c
@@ -100,6 +100,11 @@ static long ppb_to_scaled_ppm(int ppb)
 	return (long) (ppb * 65.536);
 }
 
+static int64_t pctns(struct ptp_clock_time *t)
+{
+	return t->sec * 1000000000LL + t->nsec;
+}
+
 static void usage(char *progname)
 {
 	fprintf(stderr,
@@ -112,6 +117,8 @@ static void usage(char *progname)
 		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
 		" -g         get the ptp clock time\n"
 		" -h         prints this message\n"
+		" -k val     measure the time offset between system and phc clock\n"
+		"            for 'val' times (Maximum 25)\n"
 		" -p val     enable output with a period of 'val' nanoseconds\n"
 		" -P val     enable or disable (val=1|0) the system clock PPS\n"
 		" -s         set the ptp clock time from the system time\n"
@@ -133,8 +140,12 @@ int main(int argc, char *argv[])
 	struct itimerspec timeout;
 	struct sigevent sigevent;
 
+	struct ptp_clock_time *pct;
+	struct ptp_sys_offset *sysoff;
+
+
 	char *progname;
-	int c, cnt, fd;
+	int i, c, cnt, fd;
 
 	char *device = DEVICE;
 	clockid_t clkid;
@@ -144,14 +155,19 @@ int main(int argc, char *argv[])
 	int extts = 0;
 	int gettime = 0;
 	int oneshot = 0;
+	int pct_offset = 0;
+	int n_samples = 0;
 	int periodic = 0;
 	int perout = -1;
 	int pps = -1;
 	int settime = 0;
 
+	int64_t t1, t2, tp;
+	int64_t interval, offset;
+
 	progname = strrchr(argv[0], '/');
 	progname = progname ? 1+progname : argv[0];
-	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) {
+	while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) {
 		switch (c) {
 		case 'a':
 			oneshot = atoi(optarg);
@@ -174,6 +190,10 @@ int main(int argc, char *argv[])
 		case 'g':
 			gettime = 1;
 			break;
+		case 'k':
+			pct_offset = 1;
+			n_samples = atoi(optarg);
+			break;
 		case 'p':
 			perout = atoi(optarg);
 			break;
@@ -376,6 +396,47 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (pct_offset) {
+		if (n_samples <= 0 || n_samples > 25) {
+			puts("n_samples should be between 1 and 25");
+			usage(progname);
+			return -1;
+		}
+
+		sysoff = calloc(1, sizeof(*sysoff));
+		if (!sysoff) {
+			perror("calloc");
+			return -1;
+		}
+		sysoff->n_samples = n_samples;
+
+		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
+			perror("PTP_SYS_OFFSET");
+		else
+			puts("system and phc clock time offset request okay");
+
+		pct = &sysoff->ts[0];
+		for (i = 0; i < sysoff->n_samples; i++) {
+			t1 = pctns(pct+2*i);
+			tp = pctns(pct+2*i+1);
+			t2 = pctns(pct+2*i+2);
+			interval = t2 - t1;
+			offset = (t2 + t1) / 2 - tp;
+
+			printf("system time: %ld.%ld\n",
+				(pct+2*i)->sec, (pct+2*i)->nsec);
+			printf("phc    time: %ld.%ld\n",
+				(pct+2*i+1)->sec, (pct+2*i+1)->nsec);
+			printf("system time: %ld.%ld\n",
+				(pct+2*i+2)->sec, (pct+2*i+2)->nsec);
+			printf("system/phc clock time offset is %ld ns\n"
+				"system     clock time delay  is %ld ns\n",
+				offset, interval);
+		}
+
+		free(sysoff);
+	}
+
 	close(fd);
 	return 0;
 }
-- 
1.7.11.7


-- 
Best Regards,
Dong Zhu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH net-next v4] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program
  2013-09-17  7:32       ` [PATCH net-next v4] " Dong Zhu
@ 2013-09-23 20:46         ` David Miller
  0 siblings, 0 replies; 8+ messages in thread
From: David Miller @ 2013-09-23 20:46 UTC (permalink / raw)
  To: bluezhudong; +Cc: richardcochran, rob, netdev, linux-doc, linux-kernel

From: Dong Zhu <bluezhudong@gmail.com>
Date: Tue, 17 Sep 2013 15:32:35 +0800

> This patch add a method into testptp.c to measure the time offset
> between phc and system clock through the ioctl PTP_SYS_OFFSET.
> 
> Signed-off-by: Dong Zhu <bluezhudong@gmail.com>

Applied.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-09-23 20:46 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-14  8:03 [PATCH] ptp: measure the time offset between PHC and system clock Dong Zhu
2013-09-14 14:31 ` Richard Cochran
2013-09-14 15:39   ` [PATCH] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program clock Dong Zhu
2013-09-15  8:48     ` Richard Cochran
2013-09-15  9:25       ` [PATCH net-next v3] ptp: add the PTP_SYS_OFFSET ioctl to the testptp program Dong Zhu
2013-09-17  7:32       ` [PATCH net-next v4] " Dong Zhu
2013-09-23 20:46         ` David Miller
2013-09-14 19:21 ` [PATCH] ptp: measure the time offset between PHC and system clock Sergei Shtylyov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.