pastebin - collaborative debugging tool
rovema.kpaste.net RSS


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

  1. /*
  2. # chrt --fifo 80 /home/test001/sleep_on_nic_clock1
  3.  
  4. ## test run using nic clock
  5. # start time = 1635853936378080682, end_time=1635853937378080682, curr_ns=1635853937378081818, cycles=140719,   diff(curr-end)=1136
  6. # start time = 1635853937378127226, end_time=1635853938378127226, curr_ns=1635853938378129274, cycles=133578,   diff(curr-end)=2048
  7. # start time = 1635853938378158554, end_time=1635853939378158554, curr_ns=1635853939378158938, cycles=133578,   diff(curr-end)=384
  8. # start time = 1635853939378187354, end_time=1635853940378187354, curr_ns=1635853940378192298, cycles=133026,   diff(curr-end)=4944
  9. # start time = 1635853940378221738, end_time=1635853941378221738, curr_ns=1635853941378228090, cycles=133649,   diff(curr-end)=6352
  10. # start time = 1635853941378255994, end_time=1635853942378255994, curr_ns=1635853942378259562, cycles=133100,   diff(curr-end)=3568
  11. # start time = 1635853942378288378, end_time=1635853943378288378, curr_ns=1635853943378292810, cycles=133638,   diff(curr-end)=4432
  12. # start time = 1635853943378322826, end_time=1635853944378322826, curr_ns=1635853944378325562, cycles=133073,   diff(curr-end)=2736
  13. # start time = 1635853944378354330, end_time=1635853945378354330, curr_ns=1635853945378355882, cycles=133634,   diff(curr-end)=1552
  14. # start time = 1635853945378384490, end_time=1635853946378384490, curr_ns=1635853946378387034, cycles=133037,   diff(curr-end)=2544
  15.  
  16. ## test run using CLOCK_MONOTONIC
  17. # start time = 2761980619832150, end_time=2761981619832150, curr_ns=2761981619832180, cycles=11911788,  diff(curr-end)=30
  18. # start time = 2761981619853612, end_time=2761982619853612, curr_ns=2761982619853682, cycles=11904294,  diff(curr-end)=70
  19. # start time = 2761982619876436, end_time=2761983619876436, curr_ns=2761983619876438, cycles=11856345,  diff(curr-end)=2
  20. # start time = 2761983619898473, end_time=2761984619898473, curr_ns=2761984619898521, cycles=11905893,  diff(curr-end)=48
  21. # start time = 2761984619921518, end_time=2761985619921518, curr_ns=2761985619921576, cycles=11847840,  diff(curr-end)=58
  22. # start time = 2761985619943039, end_time=2761986619943039, curr_ns=2761986619943109, cycles=11905781,  diff(curr-end)=70
  23. # start time = 2761986619964716, end_time=2761987619964716, curr_ns=2761987619964732, cycles=11853388,  diff(curr-end)=16
  24. # start time = 2761987619990173, end_time=2761988619990173, curr_ns=2761988619990239, cycles=11906530,  diff(curr-end)=66
  25. # start time = 2761988620012393, end_time=2761989620012393, curr_ns=2761989620012399, cycles=11857035,  diff(curr-end)=6
  26. # start time = 2761989620035020, end_time=2761990620035020, curr_ns=2761990620035080, cycles=11896733,  diff(curr-end)=60
  27.  
  28. ## test run using CLOCK_REALTIME
  29. # start time = 1635853977432626806, end_time=1635853978432626806, curr_ns=1635853978432626835, cycles=11909804, diff(curr-end)=29
  30. # start time = 1635853978432648669, end_time=1635853979432648669, curr_ns=1635853979432648722, cycles=11856100, diff(curr-end)=53
  31. # start time = 1635853979432672061, end_time=1635853980432672061, curr_ns=1635853980432672122, cycles=11899583, diff(curr-end)=61
  32. # start time = 1635853980432699242, end_time=1635853981432699242, curr_ns=1635853981432699306, cycles=11845573, diff(curr-end)=64
  33. # start time = 1635853981432724632, end_time=1635853982432724632, curr_ns=1635853982432724705, cycles=11910562, diff(curr-end)=73
  34. # start time = 1635853982432746795, end_time=1635853983432746795, curr_ns=1635853983432746813, cycles=11857330, diff(curr-end)=18
  35. # start time = 1635853983432768427, end_time=1635853984432768427, curr_ns=1635853984432768450, cycles=11905325, diff(curr-end)=23
  36. # start time = 1635853984432790964, end_time=1635853985432790964, curr_ns=1635853985432791036, cycles=11854323, diff(curr-end)=72
  37. # start time = 1635853985432816708, end_time=1635853986432816708, curr_ns=1635853986432816741, cycles=11897911, diff(curr-end)=33
  38. # start time = 1635853986432841581, end_time=1635853987432841581, curr_ns=1635853987432841585, cycles=11899427, diff(curr-end)=4
  39. */
  40.  
  41.  
  42. /*
  43.  * sleep_on_nic_clock1 - sleep on network card clock
  44.  *
  45.  */
  46.  
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <fcntl.h>
  50. #include <unistd.h>
  51. #include <time.h>
  52. #include <sched.h>
  53. #include <stdlib.h>
  54. #include <stdint.h>
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <errno.h>
  58.  
  59.  
  60. #ifndef CLOCK_INVALID
  61. #define CLOCK_INVALID -1
  62. #endif
  63.  
  64. #define NSEC_PER_SEC (1000000000LL)
  65.  
  66.  
  67. static
  68. clockid_t get_clockid(int fd)
  69. {
  70.         const int CLOCKFD = 3;
  71.  
  72.         return (((unsigned int) ~fd) << 3) | CLOCKFD;
  73. }
  74.  
  75. signed long long timespec2ns(struct timespec *ts)
  76. {
  77.         signed long long ns;
  78.  
  79.         ns = ts->tv_nsec + (ts->tv_sec * NSEC_PER_SEC);
  80.        
  81.         return(ns);
  82. }
  83.  
  84. void ns2timespec(signed long long ns, struct timespec *ts)
  85. {
  86.         lldiv_t lld;
  87.  
  88.         lld = lldiv(ns, NSEC_PER_SEC);
  89.         ts->tv_sec  = lld.quot;
  90.         ts->tv_nsec = lld.rem;
  91. }
  92.  
  93. int clock_gettime_ns(clockid_t clockid, signed long long *ns)
  94. {
  95.         int res;
  96.         struct timespec ts;
  97.  
  98.         res = clock_gettime(clockid, &ts);
  99.         if (res == 0)
  100.                 *ns = timespec2ns(&ts);
  101.  
  102.         return(res);
  103. }
  104.  
  105.  
  106. void test_busysleep_on_clock(clockid_t clkid, double delay)
  107. {
  108.         signed long long ns, start_ns, end_ns;
  109.         long cycles;
  110.  
  111.         /* cycle scheduler so it generates less interference */
  112.         (void)sched_yield();
  113.  
  114.         (void)clock_gettime_ns(clkid, &start_ns);
  115.  
  116.         end_ns = start_ns + NSEC_PER_SEC * delay;
  117.         ns = start_ns;
  118.        
  119.         for(cycles = 0 ; ns < end_ns ; cycles++)
  120.         {
  121.                 (void)clock_gettime_ns(clkid, &ns);
  122.         }
  123.  
  124.         (void)printf("# start time = %lld, end_time=%lld, curr_ns=%lld, cycles=%ld,\tdiff(curr-end)=%lld\n",
  125.                 start_ns, end_ns, ns, cycles, (ns-end_ns));
  126. }
  127.  
  128.  
  129. int main(int ac, char *av[])
  130. {
  131.         int fd;
  132.         clockid_t clkid;
  133.         const char *devicename = "/dev/ptp0";
  134.  
  135.         /*
  136.          * todo: socket fd to /dev/ptpX filename should be implemented here
  137.          */
  138.  
  139.         fd = open(devicename, O_RDWR);
  140.         if (fd < 0) {
  141.                 (void)fprintf(stderr, "opening %s: %s\n", devicename,
  142.                         strerror(errno));
  143.                 return(EXIT_FAILURE);
  144.         }
  145.  
  146.         clkid = get_clockid(fd);
  147.         if (CLOCK_INVALID == clkid) {
  148.                 (void)fprintf(stderr, "failed to read clock id\n");
  149.                 return(EXIT_FAILURE);
  150.         }
  151.        
  152.         (void)puts("/*");
  153.  
  154. #if 1/* TEST_CLOCK_NANOSLEEP_ON_NIC */
  155.         int i;
  156.  
  157.         (void)printf("\n## test run using nic clock\n");
  158.         for(i=0 ; i < 10 ; i++)
  159.                 test_busysleep_on_clock(clkid, 1.0);
  160.  
  161.         (void)printf("\n## test run using CLOCK_MONOTONIC\n");
  162.         for(i=0 ; i < 10 ; i++)
  163.                 test_busysleep_on_clock(CLOCK_MONOTONIC, 1.0);
  164.  
  165.         (void)printf("\n## test run using CLOCK_REALTIME\n");
  166.         for(i=0 ; i < 10 ; i++)
  167.                 test_busysleep_on_clock(CLOCK_REALTIME, 1.0);
  168.  
  169. #endif /* TEST_CLOCK_NANOSLEEP_ON_NIC */
  170.  
  171.         (void)close(fd);
  172.         (void)puts("*/");
  173.  
  174.         return(EXIT_SUCCESS);
  175. }

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