Created
August 21, 2014 07:19
-
-
Save sw17ch/bca716279e55ac58658e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define _POSIX_C_SOURCE 199309L | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <stdbool.h> | |
| #include <stdint.h> | |
| #include <time.h> | |
| #include <signal.h> | |
| #define GTCHECK(EXP) \ | |
| do { \ | |
| int e = EXP; \ | |
| if (0 != e) { \ | |
| fprintf(stderr, "ERROR: clock_gettime failed (%d). %s:%d\n", e, __FILE__, __LINE__); \ | |
| exit(e); \ | |
| } \ | |
| } while(0) | |
| #define GT(PTR) clock_gettime(CLOCK_BOOTTIME, (PTR)) | |
| void done(int param); | |
| static struct timespec t; | |
| static uint64_t ts[128] = { 0 }; | |
| static uint64_t t_to_u64(struct timespec * t) { | |
| return (t->tv_sec * 1000000000) + t->tv_nsec; | |
| } | |
| int main(int argc, char * argv[]) { | |
| (void)argc; | |
| (void)argv; | |
| signal(SIGINT, done); | |
| /* Setup the first sample. */ | |
| GTCHECK(GT(&t)); | |
| uint64_t first = t_to_u64(&t); | |
| GTCHECK(GT(&t)); | |
| uint64_t second = t_to_u64(&t); | |
| ts[0] = second - first; | |
| uint64_t last = second; | |
| /* Begin sampling the clock as fast as possible. */ | |
| for (int i = 1; i < sizeof(ts)/sizeof(ts[0]); i++) { | |
| while(true) { | |
| GTCHECK(GT(&t)); | |
| uint64_t v = t_to_u64(&t); | |
| uint64_t diff = v - last; | |
| last = v; | |
| if (diff > ts[i - 1]) { | |
| ts[i] = diff; | |
| break; | |
| } | |
| } | |
| } | |
| done(0); | |
| return 0; | |
| } | |
| void done(int param) { | |
| (void)param; | |
| printf("Done!\n"); | |
| for (int i = 0; i < sizeof(ts)/sizeof(ts[0]) && ts[i]; i++) { | |
| fprintf(stdout, "diff=%lu\n", ts[i]); | |
| } | |
| exit(0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment