pastebin - collaborative debugging tool
rovema.kpaste.net RSS


How precise is busy sleeping on nic clock ?
Posted by Anonymous on Tue 2nd Nov 2021 13:02
raw | new post
modification of post by Anonymous (view diff)

  1. /*
  2. chrt --fifo 60 /home/test001/sleep_on_nic_clock1
  3.  
  4. ## test run using nic clock
  5. # start time = 1635858056527505321, end_time=1635858057527505321, curr_ns=1635858057527507625, cycles=140997,   diff(curr-end)=2304
  6. # start time = 1635858057527550905, end_time=1635858058527550905, curr_ns=1635858058527555897, cycles=133327,   diff(curr-end)=4992
  7. # start time = 1635858058527582153, end_time=1635858059527582153, curr_ns=1635858059527585977, cycles=133327,   diff(curr-end)=3824
  8. # start time = 1635858059527612265, end_time=1635858060527612265, curr_ns=1635858060527617497, cycles=133871,   diff(curr-end)=5232
  9. # start time = 1635858060527644537, end_time=1635858061527644537, curr_ns=1635858061527648953, cycles=133371,   diff(curr-end)=4416
  10. # start time = 1635858061527675257, end_time=1635858062527675257, curr_ns=1635858062527675881, cycles=133998,   diff(curr-end)=624
  11. # start time = 1635858062527703737, end_time=1635858063527703737, curr_ns=1635858063527703865, cycles=133360,   diff(curr-end)=128
  12. # start time = 1635858063527731033, end_time=1635858064527731033, curr_ns=1635858064527737737, cycles=134070,   diff(curr-end)=6704
  13. # start time = 1635858064527764473, end_time=1635858065527764473, curr_ns=1635858065527766473, cycles=133456,   diff(curr-end)=2000
  14. # start time = 1635858065527793817, end_time=1635858066527793817, curr_ns=1635858066527796889, cycles=133460,   diff(curr-end)=3072
  15.  
  16. ## test run using CLOCK_MONOTONIC
  17. # start time = 669517490571, end_time=670517490571, curr_ns=670517490645, cycles=11906831,      diff(curr-end)=74
  18. # start time = 670517512413, end_time=671517512413, curr_ns=671517512461, cycles=11843279,      diff(curr-end)=48
  19. # start time = 671517533399, end_time=672517533399, curr_ns=672517533442, cycles=11895829,      diff(curr-end)=43
  20. # start time = 672517556529, end_time=673517556529, curr_ns=673517556566, cycles=11846659,      diff(curr-end)=37
  21. # start time = 673517579419, end_time=674517579419, curr_ns=674517579464, cycles=11895289,      diff(curr-end)=45
  22. # start time = 674517601115, end_time=675517601115, curr_ns=675517601152, cycles=11844346,      diff(curr-end)=37
  23. # start time = 675517623244, end_time=676517623244, curr_ns=676517623264, cycles=11844675,      diff(curr-end)=20
  24. # start time = 676517644032, end_time=677517644032, curr_ns=677517644047, cycles=12009474,      diff(curr-end)=15
  25. # start time = 677517667879, end_time=678517667879, curr_ns=678517667954, cycles=11849767,      diff(curr-end)=75
  26. # start time = 678517690157, end_time=679517690157, curr_ns=679517690233, cycles=11860084,      diff(curr-end)=76
  27.  
  28. ## test run using CLOCK_TAI
  29. # start time = 1635858076325920219, end_time=1635858077325920219, curr_ns=1635858077325920269, cycles=11912838, diff(curr-end)=50
  30. # start time = 1635858077325941037, end_time=1635858078325941037, curr_ns=1635858078325941042, cycles=11870445, diff(curr-end)=5
  31. # start time = 1635858078325961111, end_time=1635858079325961111, curr_ns=1635858079325961120, cycles=11920007, diff(curr-end)=9
  32. # start time = 1635858079325983482, end_time=1635858080325983482, curr_ns=1635858080325983528, cycles=11925287, diff(curr-end)=46
  33. # start time = 1635858080326008216, end_time=1635858081326008216, curr_ns=1635858081326008237, cycles=11895293, diff(curr-end)=21
  34. # start time = 1635858081326029771, end_time=1635858082326029771, curr_ns=1635858082326029800, cycles=11845071, diff(curr-end)=29
  35. # start time = 1635858082326051402, end_time=1635858083326051402, curr_ns=1635858083326051427, cycles=11899193, diff(curr-end)=25
  36. # start time = 1635858083326072174, end_time=1635858084326072174, curr_ns=1635858084326072197, cycles=11847583, diff(curr-end)=23
  37. # start time = 1635858084326091805, end_time=1635858085326091805, curr_ns=1635858085326091821, cycles=11897899, diff(curr-end)=16
  38. # start time = 1635858085326112769, end_time=1635858086326112769, curr_ns=1635858086326112798, cycles=11849424, diff(curr-end)=29
  39.  
  40. ## test run using CLOCK_THREAD_CPUTIME_ID
  41. # start time = 28557001827, end_time=29557001827, curr_ns=29557003459, cycles=489614,   diff(curr-end)=1632
  42. # start time = 29557027314, end_time=30557027314, curr_ns=30557028230, cycles=489774,   diff(curr-end)=916
  43. # start time = 30557049724, end_time=31557049724, curr_ns=31557050201, cycles=489826,   diff(curr-end)=477
  44. # start time = 31557072880, end_time=32557072880, curr_ns=32557074424, cycles=491721,   diff(curr-end)=1544
  45. # start time = 32557095157, end_time=33557095157, curr_ns=33557096222, cycles=487932,   diff(curr-end)=1065
  46. # start time = 33557123241, end_time=34557123241, curr_ns=34557125122, cycles=488096,   diff(curr-end)=1881
  47. # start time = 34557147688, end_time=35557147688, curr_ns=35557148558, cycles=488033,   diff(curr-end)=870
  48. # start time = 35557171194, end_time=36557171194, curr_ns=36557172886, cycles=488542,   diff(curr-end)=1692
  49. # start time = 36557196089, end_time=37557196089, curr_ns=37557198108, cycles=487254,   diff(curr-end)=2019
  50. # start time = 37557220863, end_time=38557220863, curr_ns=38557221789, cycles=493097,   diff(curr-end)=926
  51. */
  52.  
  53.  
  54. /*
  55.  * sleep_on_nic_clock1 - sleep on network card clock
  56.  *
  57.  */
  58.  
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61. #include <fcntl.h>
  62. #include <unistd.h>
  63. #include <time.h>
  64. #include <sched.h>
  65. #include <stdlib.h>
  66. #include <stdint.h>
  67. #include <stdio.h>
  68. #include <string.h>
  69. #include <errno.h>
  70.  
  71.  
  72. #ifndef CLOCK_INVALID
  73. #define CLOCK_INVALID -1
  74. #endif
  75.  
  76. #define NSEC_PER_SEC (1000000000LL)
  77.  
  78.  
  79. static
  80. clockid_t get_clockid(int fd)
  81. {
  82.         const int CLOCKFD = 3;
  83.  
  84.         return (((unsigned int) ~fd) << 3) | CLOCKFD;
  85. }
  86.  
  87. signed long long timespec2ns(struct timespec *ts)
  88. {
  89.         signed long long ns;
  90.  
  91.         ns = ts->tv_nsec + (ts->tv_sec * NSEC_PER_SEC);
  92.        
  93.         return(ns);
  94. }
  95.  
  96. void ns2timespec(signed long long ns, struct timespec *ts)
  97. {
  98.         lldiv_t lld;
  99.  
  100.         lld = lldiv(ns, NSEC_PER_SEC);
  101.         ts->tv_sec  = lld.quot;
  102.         ts->tv_nsec = lld.rem;
  103. }
  104.  
  105. int clock_gettime_ns(clockid_t clockid, signed long long *ns)
  106. {
  107.         int res;
  108.         struct timespec ts;
  109.  
  110.         res = clock_gettime(clockid, &ts);
  111.         if (res == 0)
  112.                 *ns = timespec2ns(&ts);
  113.  
  114.         return(res);
  115. }
  116.  
  117.  
  118. void test_busysleep_on_clock(clockid_t clkid, double delay)
  119. {
  120.         signed long long ns, start_ns, end_ns;
  121.         long cycles;
  122.  
  123.         /* cycle scheduler so it generates less interference */
  124.         (void)sched_yield();
  125.  
  126.         (void)clock_gettime_ns(clkid, &start_ns);
  127.  
  128.         end_ns = start_ns + NSEC_PER_SEC * delay;
  129.         ns = start_ns;
  130.        
  131.         for(cycles = 0 ; ns < end_ns ; cycles++)
  132.         {
  133.                 (void)clock_gettime_ns(clkid, &ns);
  134.         }
  135.  
  136.         (void)printf("# start time = %lld, end_time=%lld, curr_ns=%lld, cycles=%ld,\tdiff(curr-end)=%lld\n",
  137.                 start_ns, end_ns, ns, cycles, (ns-end_ns));
  138. }
  139.  
  140.  
  141. int main(int ac, char *av[])
  142. {
  143.         int fd;
  144.         clockid_t clkid;
  145.         const char *devicename = "/dev/ptp0";
  146.  
  147.         /*
  148.          * todo: socket fd to /dev/ptpX filename should be implemented here
  149.          */
  150.  
  151.         fd = open(devicename, O_RDWR);
  152.         if (fd < 0) {
  153.                 (void)fprintf(stderr, "opening %s: %s\n", devicename,
  154.                         strerror(errno));
  155.                 return(EXIT_FAILURE);
  156.         }
  157.  
  158.         clkid = get_clockid(fd);
  159.         if (CLOCK_INVALID == clkid) {
  160.                 (void)fprintf(stderr, "failed to read clock id\n");
  161.                 return(EXIT_FAILURE);
  162.         }
  163.        
  164.         (void)puts("/*");
  165.  
  166. #if 1/* TEST_CLOCK_NANOSLEEP_ON_NIC */
  167.         int i;
  168.  
  169.         (void)printf("\n## test run using nic clock\n");
  170.         for(i=0 ; i < 10 ; i++)
  171.                 test_busysleep_on_clock(clkid, 1.0);
  172.  
  173.         (void)printf("\n## test run using CLOCK_MONOTONIC\n");
  174.         for(i=0 ; i < 10 ; i++)
  175.                 test_busysleep_on_clock(CLOCK_MONOTONIC, 1.0);
  176.  
  177.         (void)printf("\n## test run using CLOCK_TAI\n");
  178.         for(i=0 ; i < 10 ; i++)
  179.                 test_busysleep_on_clock(CLOCK_TAI, 1.0);
  180.  
  181.         (void)printf("\n## test run using CLOCK_THREAD_CPUTIME_ID\n");
  182.         for(i=0 ; i < 10 ; i++)
  183.                 test_busysleep_on_clock(CLOCK_THREAD_CPUTIME_ID, 1.0);
  184.  
  185. #endif /* TEST_CLOCK_NANOSLEEP_ON_NIC */
  186.  
  187.         (void)close(fd);
  188.         (void)puts("*/");
  189.  
  190.         return(EXIT_SUCCESS);
  191. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at