/* * count_timer_syscalls.c - runs timer 1000 times, strace can count syscalls * */ #include #include #include #include #include #include #include #include #include #include #include #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; } static void test_loop_on_clock(clockid_t clkid, unsigned long num_cycles) { struct timespec ts; unsigned long cycles; for(cycles = 0UL ; cycles < num_cycles ; cycles++) { (void)clock_gettime(clkid, &ts); (void)ts; } } int main(int ac, char *av[]) { const char *clockname = av[1]; (void)puts("# start."); if ((ac != 2) || (!clockname)) { (void)fprintf(stderr, "%s: Missing clockname argument\n", av[0]); return EXIT_FAILURE; } if (!strcmp(clockname, "nic")) { int fd; clockid_t clkid; const char *devicename = "/dev/ptp0"; /* * count_timer_syscalls.c - runs timer 1000 times, strace can count syscalls * */ #include #include #include #include #include #include #include #include #include #include #include #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; } static void test_loop_on_clock(clockid_t clkid, unsigned long num_cycles) { struct timespec ts; unsigned long cycles; for(cycles = 0UL ; cycles < num_cycles ; cycles++) { (void)clock_gettime(clkid, &ts); (void)ts; } } int main(int ac, char *av[]) { const char *clockname = av[1]; (void)puts("# start."); if ((ac != 2) || (!clockname)) { (void)fprintf(stderr, "%s: Missing clockname argument\n", av[0]); return EXIT_FAILURE; } if (!strcmp(clockname, "nic")) { int fd; clockid_t clkid; const char *devicename = "/dev/ptp0"; fd = open(devicename, O_RDWR); if (fd < 0) { (void)fprintf(stderr, "opening %s: %s\n", devicename, strerror(errno)); return(EXIT_FAILURE); } clkid = get_clockid(fd); if (CLOCK_INVALID == clkid) { (void)fprintf(stderr, "failed to read clock id\n"); return(EXIT_FAILURE); } (void)printf("## test run using nic clock\n"); test_loop_on_clock(clkid, 8000UL); (void)close(fd); } else if (!strcmp(clockname, "CLOCK_MONOTONIC")) { (void)printf("## test run using CLOCK_MONOTONIC\n"); test_loop_on_clock(CLOCK_MONOTONIC, 8000UL); } else if (!strcmp(clockname, "CLOCK_TAI")) { (void)printf("## test run using CLOCK_TAI\n"); test_loop_on_clock(CLOCK_TAI, 8000UL); } else if (!strcmp(clockname, "CLOCK_BOOTTIME")) { (void)printf("## test run using CLOCK_BOOTTIME\n"); test_loop_on_clock(CLOCK_BOOTTIME, 8000UL); } else if (!strcmp(clockname, "CLOCK_REALTIME")) { (void)printf("## test run using CLOCK_REALTIME\n"); test_loop_on_clock(CLOCK_REALTIME, 8000UL); } else if (!strcmp(clockname, "CLOCK_REALTIME_COARSE")) { (void)printf("## test run using CLOCK_REALTIME_COARSE\n"); test_loop_on_clock(CLOCK_REALTIME_COARSE, 8000UL); } else if (!strcmp(clockname, "CLOCK_THREAD_CPUTIME_ID")) { (void)printf("## test run using CLOCK_THREAD_CPUTIME_ID\n"); test_loop_on_clock(CLOCK_THREAD_CPUTIME_ID, 8000UL); } else { (void)fprintf(stderr, "%s: Unknown clockname argument %s\n", av[0], clockname); return EXIT_FAILURE; } (void)puts("# end."); return(EXIT_SUCCESS); } fd = open(devicename, O_RDWR); if (fd < 0) { (void)fprintf(stderr, "opening %s: %s\n", devicename, strerror(errno)); return(EXIT_FAILURE); } clkid = get_clockid(fd); if (CLOCK_INVALID == clkid) { (void)fprintf(stderr, "failed to read clock id\n"); return(EXIT_FAILURE); } (void)printf("## test run using nic clock\n"); test_loop_on_clock(clkid, 8000UL); (void)close(fd); } else if (!strcmp(clockname, "CLOCK_MONOTONIC")) { (void)printf("## test run using CLOCK_MONOTONIC\n"); test_loop_on_clock(CLOCK_MONOTONIC, 8000UL); } else if (!strcmp(clockname, "CLOCK_TAI")) { (void)printf("## test run using CLOCK_TAI\n"); test_loop_on_clock(CLOCK_TAI, 8000UL); } else if (!strcmp(clockname, "CLOCK_BOOTTIME")) { (void)printf("## test run using CLOCK_BOOTTIME\n"); test_loop_on_clock(CLOCK_BOOTTIME, 8000UL); } else if (!strcmp(clockname, "CLOCK_REALTIME")) { (void)printf("## test run using CLOCK_REALTIME\n"); test_loop_on_clock(CLOCK_REALTIME, 8000UL); } else if (!strcmp(clockname, "CLOCK_REALTIME_COARSE")) { (void)printf("## test run using CLOCK_REALTIME_COARSE\n"); test_loop_on_clock(CLOCK_REALTIME_COARSE, 8000UL); } else if (!strcmp(clockname, "CLOCK_THREAD_CPUTIME_ID")) { (void)printf("## test run using CLOCK_THREAD_CPUTIME_ID\n"); test_loop_on_clock(CLOCK_THREAD_CPUTIME_ID, 8000UL); } else { (void)fprintf(stderr, "%s: Unknown clockname argument %s\n", av[0], clockname); return EXIT_FAILURE; } (void)puts("# end."); return(EXIT_SUCCESS); }