Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest/ptp: update ptp selftest to exercise the gettimex options

With the inclusion of commit c259acab839e ("ptp/ioctl: support
MONOTONIC{,_RAW} timestamps for PTP_SYS_OFFSET_EXTENDED") clock_gettime()
now allows retrieval of pre/post timestamps for CLOCK_MONOTONIC and
CLOCK_MONOTONIC_RAW timebases along with the previously supported
CLOCK_REALTIME.

This patch adds a command line option 'y' to the testptp program to
choose one of the allowed timebases [realtime aka system, monotonic,
and monotonic-raw).

Signed-off-by: Mahesh Bandewar <maheshb@google.com>
Cc: Shuah Khan <shuah@kernel.org>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Link: https://patch.msgid.link/20241003101506.769418-1-maheshb@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Mahesh Bandewar and committed by
Jakub Kicinski
3d07b691 2f651683

+57 -5
+57 -5
tools/testing/selftests/ptp/testptp.c
··· 146 146 " -T val set the ptp clock time to 'val' seconds\n" 147 147 " -x val get an extended ptp clock time with the desired number of samples (up to %d)\n" 148 148 " -X get a ptp clock cross timestamp\n" 149 + " -y val pre/post tstamp timebase to use {realtime|monotonic|monotonic-raw}\n" 149 150 " -z test combinations of rising/falling external time stamp flags\n", 150 151 progname, PTP_MAX_SAMPLES); 151 152 } ··· 190 189 int seconds = 0; 191 190 int settime = 0; 192 191 int channel = -1; 192 + clockid_t ext_clockid = CLOCK_REALTIME; 193 193 194 194 int64_t t1, t2, tp; 195 195 int64_t interval, offset; ··· 200 198 201 199 progname = strrchr(argv[0], '/'); 202 200 progname = progname ? 1+progname : argv[0]; 203 - while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xz"))) { 201 + while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xy:z"))) { 204 202 switch (c) { 205 203 case 'c': 206 204 capabilities = 1; ··· 280 278 case 'X': 281 279 getcross = 1; 282 280 break; 281 + case 'y': 282 + if (!strcasecmp(optarg, "realtime")) 283 + ext_clockid = CLOCK_REALTIME; 284 + else if (!strcasecmp(optarg, "monotonic")) 285 + ext_clockid = CLOCK_MONOTONIC; 286 + else if (!strcasecmp(optarg, "monotonic-raw")) 287 + ext_clockid = CLOCK_MONOTONIC_RAW; 288 + else { 289 + fprintf(stderr, 290 + "type needs to be realtime, monotonic or monotonic-raw; was given %s\n", 291 + optarg); 292 + return -1; 293 + } 294 + break; 295 + 283 296 case 'z': 284 297 flagtest = 1; 285 298 break; ··· 583 566 } 584 567 585 568 soe->n_samples = getextended; 569 + soe->clockid = ext_clockid; 586 570 587 571 if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, soe)) { 588 572 perror("PTP_SYS_OFFSET_EXTENDED"); ··· 592 574 getextended); 593 575 594 576 for (i = 0; i < getextended; i++) { 595 - printf("sample #%2d: system time before: %lld.%09u\n", 596 - i, soe->ts[i][0].sec, soe->ts[i][0].nsec); 577 + switch (ext_clockid) { 578 + case CLOCK_REALTIME: 579 + printf("sample #%2d: real time before: %lld.%09u\n", 580 + i, soe->ts[i][0].sec, 581 + soe->ts[i][0].nsec); 582 + break; 583 + case CLOCK_MONOTONIC: 584 + printf("sample #%2d: monotonic time before: %lld.%09u\n", 585 + i, soe->ts[i][0].sec, 586 + soe->ts[i][0].nsec); 587 + break; 588 + case CLOCK_MONOTONIC_RAW: 589 + printf("sample #%2d: monotonic-raw time before: %lld.%09u\n", 590 + i, soe->ts[i][0].sec, 591 + soe->ts[i][0].nsec); 592 + break; 593 + default: 594 + break; 595 + } 597 596 printf(" phc time: %lld.%09u\n", 598 597 soe->ts[i][1].sec, soe->ts[i][1].nsec); 599 - printf(" system time after: %lld.%09u\n", 600 - soe->ts[i][2].sec, soe->ts[i][2].nsec); 598 + switch (ext_clockid) { 599 + case CLOCK_REALTIME: 600 + printf(" real time after: %lld.%09u\n", 601 + soe->ts[i][2].sec, 602 + soe->ts[i][2].nsec); 603 + break; 604 + case CLOCK_MONOTONIC: 605 + printf(" monotonic time after: %lld.%09u\n", 606 + soe->ts[i][2].sec, 607 + soe->ts[i][2].nsec); 608 + break; 609 + case CLOCK_MONOTONIC_RAW: 610 + printf(" monotonic-raw time after: %lld.%09u\n", 611 + soe->ts[i][2].sec, 612 + soe->ts[i][2].nsec); 613 + break; 614 + default: 615 + break; 616 + } 601 617 } 602 618 } 603 619