diff options
author | Dong Zhu <bluezhudong@gmail.com> | 2013-09-17 03:32:35 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2013-09-23 16:46:17 -0400 |
commit | 568ebc5985f5d849814117e40967b5d2e739405c (patch) | |
tree | 2b92c2c47e5198b11a5b5e7f9f59dc63a31a27f8 /Documentation/ptp | |
parent | 7b584460689d4326d2d1664271ab0c56b56d4328 (diff) |
ptp: add the PTP_SYS_OFFSET ioctl to the testptp program
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>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'Documentation/ptp')
-rw-r--r-- | Documentation/ptp/testptp.c | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/Documentation/ptp/testptp.c b/Documentation/ptp/testptp.c index f59ded066108..a74d0a84d329 100644 --- a/Documentation/ptp/testptp.c +++ b/Documentation/ptp/testptp.c | |||
@@ -100,6 +100,11 @@ static long ppb_to_scaled_ppm(int ppb) | |||
100 | return (long) (ppb * 65.536); | 100 | return (long) (ppb * 65.536); |
101 | } | 101 | } |
102 | 102 | ||
103 | static int64_t pctns(struct ptp_clock_time *t) | ||
104 | { | ||
105 | return t->sec * 1000000000LL + t->nsec; | ||
106 | } | ||
107 | |||
103 | static void usage(char *progname) | 108 | static void usage(char *progname) |
104 | { | 109 | { |
105 | fprintf(stderr, | 110 | fprintf(stderr, |
@@ -112,6 +117,8 @@ static void usage(char *progname) | |||
112 | " -f val adjust the ptp clock frequency by 'val' ppb\n" | 117 | " -f val adjust the ptp clock frequency by 'val' ppb\n" |
113 | " -g get the ptp clock time\n" | 118 | " -g get the ptp clock time\n" |
114 | " -h prints this message\n" | 119 | " -h prints this message\n" |
120 | " -k val measure the time offset between system and phc clock\n" | ||
121 | " for 'val' times (Maximum 25)\n" | ||
115 | " -p val enable output with a period of 'val' nanoseconds\n" | 122 | " -p val enable output with a period of 'val' nanoseconds\n" |
116 | " -P val enable or disable (val=1|0) the system clock PPS\n" | 123 | " -P val enable or disable (val=1|0) the system clock PPS\n" |
117 | " -s set the ptp clock time from the system time\n" | 124 | " -s set the ptp clock time from the system time\n" |
@@ -133,8 +140,12 @@ int main(int argc, char *argv[]) | |||
133 | struct itimerspec timeout; | 140 | struct itimerspec timeout; |
134 | struct sigevent sigevent; | 141 | struct sigevent sigevent; |
135 | 142 | ||
143 | struct ptp_clock_time *pct; | ||
144 | struct ptp_sys_offset *sysoff; | ||
145 | |||
146 | |||
136 | char *progname; | 147 | char *progname; |
137 | int c, cnt, fd; | 148 | int i, c, cnt, fd; |
138 | 149 | ||
139 | char *device = DEVICE; | 150 | char *device = DEVICE; |
140 | clockid_t clkid; | 151 | clockid_t clkid; |
@@ -144,14 +155,19 @@ int main(int argc, char *argv[]) | |||
144 | int extts = 0; | 155 | int extts = 0; |
145 | int gettime = 0; | 156 | int gettime = 0; |
146 | int oneshot = 0; | 157 | int oneshot = 0; |
158 | int pct_offset = 0; | ||
159 | int n_samples = 0; | ||
147 | int periodic = 0; | 160 | int periodic = 0; |
148 | int perout = -1; | 161 | int perout = -1; |
149 | int pps = -1; | 162 | int pps = -1; |
150 | int settime = 0; | 163 | int settime = 0; |
151 | 164 | ||
165 | int64_t t1, t2, tp; | ||
166 | int64_t interval, offset; | ||
167 | |||
152 | progname = strrchr(argv[0], '/'); | 168 | progname = strrchr(argv[0], '/'); |
153 | progname = progname ? 1+progname : argv[0]; | 169 | progname = progname ? 1+progname : argv[0]; |
154 | while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghp:P:sSt:v"))) { | 170 | while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) { |
155 | switch (c) { | 171 | switch (c) { |
156 | case 'a': | 172 | case 'a': |
157 | oneshot = atoi(optarg); | 173 | oneshot = atoi(optarg); |
@@ -174,6 +190,10 @@ int main(int argc, char *argv[]) | |||
174 | case 'g': | 190 | case 'g': |
175 | gettime = 1; | 191 | gettime = 1; |
176 | break; | 192 | break; |
193 | case 'k': | ||
194 | pct_offset = 1; | ||
195 | n_samples = atoi(optarg); | ||
196 | break; | ||
177 | case 'p': | 197 | case 'p': |
178 | perout = atoi(optarg); | 198 | perout = atoi(optarg); |
179 | break; | 199 | break; |
@@ -376,6 +396,47 @@ int main(int argc, char *argv[]) | |||
376 | } | 396 | } |
377 | } | 397 | } |
378 | 398 | ||
399 | if (pct_offset) { | ||
400 | if (n_samples <= 0 || n_samples > 25) { | ||
401 | puts("n_samples should be between 1 and 25"); | ||
402 | usage(progname); | ||
403 | return -1; | ||
404 | } | ||
405 | |||
406 | sysoff = calloc(1, sizeof(*sysoff)); | ||
407 | if (!sysoff) { | ||
408 | perror("calloc"); | ||
409 | return -1; | ||
410 | } | ||
411 | sysoff->n_samples = n_samples; | ||
412 | |||
413 | if (ioctl(fd, PTP_SYS_OFFSET, sysoff)) | ||
414 | perror("PTP_SYS_OFFSET"); | ||
415 | else | ||
416 | puts("system and phc clock time offset request okay"); | ||
417 | |||
418 | pct = &sysoff->ts[0]; | ||
419 | for (i = 0; i < sysoff->n_samples; i++) { | ||
420 | t1 = pctns(pct+2*i); | ||
421 | tp = pctns(pct+2*i+1); | ||
422 | t2 = pctns(pct+2*i+2); | ||
423 | interval = t2 - t1; | ||
424 | offset = (t2 + t1) / 2 - tp; | ||
425 | |||
426 | printf("system time: %ld.%ld\n", | ||
427 | (pct+2*i)->sec, (pct+2*i)->nsec); | ||
428 | printf("phc time: %ld.%ld\n", | ||
429 | (pct+2*i+1)->sec, (pct+2*i+1)->nsec); | ||
430 | printf("system time: %ld.%ld\n", | ||
431 | (pct+2*i+2)->sec, (pct+2*i+2)->nsec); | ||
432 | printf("system/phc clock time offset is %ld ns\n" | ||
433 | "system clock time delay is %ld ns\n", | ||
434 | offset, interval); | ||
435 | } | ||
436 | |||
437 | free(sysoff); | ||
438 | } | ||
439 | |||
379 | close(fd); | 440 | close(fd); |
380 | return 0; | 441 | return 0; |
381 | } | 442 | } |