- /*
- # chrt --fifo 80 /home/test001/sleep_on_nic_clock1
- ## test run using nic clock
- # start time = 1635853936378080682, end_time=1635853937378080682, curr_ns=1635853937378081818, cycles=140719, diff(curr-end)=1136
- # start time = 1635853937378127226, end_time=1635853938378127226, curr_ns=1635853938378129274, cycles=133578, diff(curr-end)=2048
- # start time = 1635853938378158554, end_time=1635853939378158554, curr_ns=1635853939378158938, cycles=133578, diff(curr-end)=384
- # start time = 1635853939378187354, end_time=1635853940378187354, curr_ns=1635853940378192298, cycles=133026, diff(curr-end)=4944
- # start time = 1635853940378221738, end_time=1635853941378221738, curr_ns=1635853941378228090, cycles=133649, diff(curr-end)=6352
- # start time = 1635853941378255994, end_time=1635853942378255994, curr_ns=1635853942378259562, cycles=133100, diff(curr-end)=3568
- # start time = 1635853942378288378, end_time=1635853943378288378, curr_ns=1635853943378292810, cycles=133638, diff(curr-end)=4432
- # start time = 1635853943378322826, end_time=1635853944378322826, curr_ns=1635853944378325562, cycles=133073, diff(curr-end)=2736
- # start time = 1635853944378354330, end_time=1635853945378354330, curr_ns=1635853945378355882, cycles=133634, diff(curr-end)=1552
- # start time = 1635853945378384490, end_time=1635853946378384490, curr_ns=1635853946378387034, cycles=133037, diff(curr-end)=2544
- ## test run using CLOCK_MONOTONIC
- # start time = 2761980619832150, end_time=2761981619832150, curr_ns=2761981619832180, cycles=11911788, diff(curr-end)=30
- # start time = 2761981619853612, end_time=2761982619853612, curr_ns=2761982619853682, cycles=11904294, diff(curr-end)=70
- # start time = 2761982619876436, end_time=2761983619876436, curr_ns=2761983619876438, cycles=11856345, diff(curr-end)=2
- # start time = 2761983619898473, end_time=2761984619898473, curr_ns=2761984619898521, cycles=11905893, diff(curr-end)=48
- # start time = 2761984619921518, end_time=2761985619921518, curr_ns=2761985619921576, cycles=11847840, diff(curr-end)=58
- # start time = 2761985619943039, end_time=2761986619943039, curr_ns=2761986619943109, cycles=11905781, diff(curr-end)=70
- # start time = 2761986619964716, end_time=2761987619964716, curr_ns=2761987619964732, cycles=11853388, diff(curr-end)=16
- # start time = 2761987619990173, end_time=2761988619990173, curr_ns=2761988619990239, cycles=11906530, diff(curr-end)=66
- # start time = 2761988620012393, end_time=2761989620012393, curr_ns=2761989620012399, cycles=11857035, diff(curr-end)=6
- # start time = 2761989620035020, end_time=2761990620035020, curr_ns=2761990620035080, cycles=11896733, diff(curr-end)=60
- ## test run using CLOCK_REALTIME
- # start time = 1635853977432626806, end_time=1635853978432626806, curr_ns=1635853978432626835, cycles=11909804, diff(curr-end)=29
- # start time = 1635853978432648669, end_time=1635853979432648669, curr_ns=1635853979432648722, cycles=11856100, diff(curr-end)=53
- # start time = 1635853979432672061, end_time=1635853980432672061, curr_ns=1635853980432672122, cycles=11899583, diff(curr-end)=61
- # start time = 1635853980432699242, end_time=1635853981432699242, curr_ns=1635853981432699306, cycles=11845573, diff(curr-end)=64
- # start time = 1635853981432724632, end_time=1635853982432724632, curr_ns=1635853982432724705, cycles=11910562, diff(curr-end)=73
- # start time = 1635853982432746795, end_time=1635853983432746795, curr_ns=1635853983432746813, cycles=11857330, diff(curr-end)=18
- # start time = 1635853983432768427, end_time=1635853984432768427, curr_ns=1635853984432768450, cycles=11905325, diff(curr-end)=23
- # start time = 1635853984432790964, end_time=1635853985432790964, curr_ns=1635853985432791036, cycles=11854323, diff(curr-end)=72
- # start time = 1635853985432816708, end_time=1635853986432816708, curr_ns=1635853986432816741, cycles=11897911, diff(curr-end)=33
- # start time = 1635853986432841581, end_time=1635853987432841581, curr_ns=1635853987432841585, cycles=11899427, diff(curr-end)=4
- */
- /*
- * 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_REALTIME, 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 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)
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.