- /*
- chrt --fifo 60 /home/test001/sleep_on_nic_clock1
- ## test run using nic clock
- # start time = 1635858056527505321, end_time=1635858057527505321, curr_ns=1635858057527507625, cycles=140997, diff(curr-end)=2304
- # start time = 1635858057527550905, end_time=1635858058527550905, curr_ns=1635858058527555897, cycles=133327, diff(curr-end)=4992
- # start time = 1635858058527582153, end_time=1635858059527582153, curr_ns=1635858059527585977, cycles=133327, diff(curr-end)=3824
- # start time = 1635858059527612265, end_time=1635858060527612265, curr_ns=1635858060527617497, cycles=133871, diff(curr-end)=5232
- # start time = 1635858060527644537, end_time=1635858061527644537, curr_ns=1635858061527648953, cycles=133371, diff(curr-end)=4416
- # start time = 1635858061527675257, end_time=1635858062527675257, curr_ns=1635858062527675881, cycles=133998, diff(curr-end)=624
- # start time = 1635858062527703737, end_time=1635858063527703737, curr_ns=1635858063527703865, cycles=133360, diff(curr-end)=128
- # start time = 1635858063527731033, end_time=1635858064527731033, curr_ns=1635858064527737737, cycles=134070, diff(curr-end)=6704
- # start time = 1635858064527764473, end_time=1635858065527764473, curr_ns=1635858065527766473, cycles=133456, diff(curr-end)=2000
- # start time = 1635858065527793817, end_time=1635858066527793817, curr_ns=1635858066527796889, cycles=133460, diff(curr-end)=3072
- ## test run using CLOCK_MONOTONIC
- # start time = 669517490571, end_time=670517490571, curr_ns=670517490645, cycles=11906831, diff(curr-end)=74
- # start time = 670517512413, end_time=671517512413, curr_ns=671517512461, cycles=11843279, diff(curr-end)=48
- # start time = 671517533399, end_time=672517533399, curr_ns=672517533442, cycles=11895829, diff(curr-end)=43
- # start time = 672517556529, end_time=673517556529, curr_ns=673517556566, cycles=11846659, diff(curr-end)=37
- # start time = 673517579419, end_time=674517579419, curr_ns=674517579464, cycles=11895289, diff(curr-end)=45
- # start time = 674517601115, end_time=675517601115, curr_ns=675517601152, cycles=11844346, diff(curr-end)=37
- # start time = 675517623244, end_time=676517623244, curr_ns=676517623264, cycles=11844675, diff(curr-end)=20
- # start time = 676517644032, end_time=677517644032, curr_ns=677517644047, cycles=12009474, diff(curr-end)=15
- # start time = 677517667879, end_time=678517667879, curr_ns=678517667954, cycles=11849767, diff(curr-end)=75
- # start time = 678517690157, end_time=679517690157, curr_ns=679517690233, cycles=11860084, diff(curr-end)=76
- ## test run using CLOCK_TAI
- # start time = 1635858076325920219, end_time=1635858077325920219, curr_ns=1635858077325920269, cycles=11912838, diff(curr-end)=50
- # start time = 1635858077325941037, end_time=1635858078325941037, curr_ns=1635858078325941042, cycles=11870445, diff(curr-end)=5
- # start time = 1635858078325961111, end_time=1635858079325961111, curr_ns=1635858079325961120, cycles=11920007, diff(curr-end)=9
- # start time = 1635858079325983482, end_time=1635858080325983482, curr_ns=1635858080325983528, cycles=11925287, diff(curr-end)=46
- # start time = 1635858080326008216, end_time=1635858081326008216, curr_ns=1635858081326008237, cycles=11895293, diff(curr-end)=21
- # start time = 1635858081326029771, end_time=1635858082326029771, curr_ns=1635858082326029800, cycles=11845071, diff(curr-end)=29
- # start time = 1635858082326051402, end_time=1635858083326051402, curr_ns=1635858083326051427, cycles=11899193, diff(curr-end)=25
- # start time = 1635858083326072174, end_time=1635858084326072174, curr_ns=1635858084326072197, cycles=11847583, diff(curr-end)=23
- # start time = 1635858084326091805, end_time=1635858085326091805, curr_ns=1635858085326091821, cycles=11897899, diff(curr-end)=16
- # start time = 1635858085326112769, end_time=1635858086326112769, curr_ns=1635858086326112798, cycles=11849424, diff(curr-end)=29
- ## test run using CLOCK_THREAD_CPUTIME_ID
- # start time = 28557001827, end_time=29557001827, curr_ns=29557003459, cycles=489614, diff(curr-end)=1632
- # start time = 29557027314, end_time=30557027314, curr_ns=30557028230, cycles=489774, diff(curr-end)=916
- # start time = 30557049724, end_time=31557049724, curr_ns=31557050201, cycles=489826, diff(curr-end)=477
- # start time = 31557072880, end_time=32557072880, curr_ns=32557074424, cycles=491721, diff(curr-end)=1544
- # start time = 32557095157, end_time=33557095157, curr_ns=33557096222, cycles=487932, diff(curr-end)=1065
- # start time = 33557123241, end_time=34557123241, curr_ns=34557125122, cycles=488096, diff(curr-end)=1881
- # start time = 34557147688, end_time=35557147688, curr_ns=35557148558, cycles=488033, diff(curr-end)=870
- # start time = 35557171194, end_time=36557171194, curr_ns=36557172886, cycles=488542, diff(curr-end)=1692
- # start time = 36557196089, end_time=37557196089, curr_ns=37557198108, cycles=487254, diff(curr-end)=2019
- # start time = 37557220863, end_time=38557220863, curr_ns=38557221789, cycles=493097, diff(curr-end)=926
- */
- /*
- * sleep_on_nic_clock1 - sleep on network card clock
- *
- */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <time.h>
- #include <sched.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #ifndef CLOCK_INVALID
- #define CLOCK_INVALID -1
- #endif
- #define NSEC_PER_SEC (1000000000LL)
- static
- clockid_t get_clockid(int fd)
- {
- const int CLOCKFD = 3;
- return (((unsigned int) ~fd) << 3) | CLOCKFD;
- }
- signed long long timespec2ns(struct timespec *ts)
- {
- signed long long ns;
- ns = ts->tv_nsec + (ts->tv_sec * NSEC_PER_SEC);
- return(ns);
- }
- void ns2timespec(signed long long ns, struct timespec *ts)
- {
- lldiv_t lld;
- lld = lldiv(ns, NSEC_PER_SEC);
- ts->tv_sec = lld.quot;
- ts->tv_nsec = lld.rem;
- }
- int clock_gettime_ns(clockid_t clockid, signed long long *ns)
- {
- int res;
- struct timespec ts;
- res = clock_gettime(clockid, &ts);
- if (res == 0)
- *ns = timespec2ns(&ts);
- return(res);
- }
- void test_busysleep_on_clock(clockid_t clkid, double delay)
- {
- signed long long ns, start_ns, end_ns;
- long cycles;
- /* cycle scheduler so it generates less interference */
- (void)sched_yield();
- (void)clock_gettime_ns(clkid, &start_ns);
- end_ns = start_ns + NSEC_PER_SEC * delay;
- ns = start_ns;
- for(cycles = 0 ; ns < end_ns ; cycles++)
- {
- (void)clock_gettime_ns(clkid, &ns);
- }
- (void)printf("# start time = %lld, end_time=%lld, curr_ns=%lld, cycles=%ld,\tdiff(curr-end)=%lld\n",
- start_ns, end_ns, ns, cycles, (ns-end_ns));
- }
- int main(int ac, char *av[])
- {
- int fd;
- clockid_t clkid;
- const char *devicename = "/dev/ptp0";
- /*
- * todo: socket fd to /dev/ptpX filename should be implemented here
- */
- fd = open(devicename, O_RDWR);
- if (fd < 0) {
- return(EXIT_FAILURE);
- }
- clkid = get_clockid(fd);
- if (CLOCK_INVALID == clkid) {
- return(EXIT_FAILURE);
- }
- #if 1/* TEST_CLOCK_NANOSLEEP_ON_NIC */
- int i;
- for(i=0 ; i < 10 ; i++)
- test_busysleep_on_clock(clkid, 1.0);
- for(i=0 ; i < 10 ; i++)
- test_busysleep_on_clock(CLOCK_MONOTONIC, 1.0);
- for(i=0 ; i < 10 ; i++)
- test_busysleep_on_clock(CLOCK_TAI, 1.0);
- for(i=0 ; i < 10 ; i++)
- test_busysleep_on_clock(CLOCK_THREAD_CPUTIME_ID, 1.0);
- #endif /* TEST_CLOCK_NANOSLEEP_ON_NIC */
- (void)close(fd);
- return(EXIT_SUCCESS);
- }
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)
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.