4 #include <sys/limits.h>
13 long long nanotime(void)
17 if(clock_gettime(CLOCK_MONOTONIC, &t)) {
18 fprintf(stderr,"clock failure\n");
22 return (((long long) t.tv_sec) * 1000000000LL) +
23 ((long long) t.tv_nsec);
26 static struct timespec ts_sub(struct timespec a, struct timespec b)
29 r.tv_sec = a.tv_sec - b.tv_sec;
30 r.tv_nsec = a.tv_nsec - b.tv_nsec;
33 r.tv_nsec += 1000 * 1000 * 1000;
35 if(r.tv_sec < 0 && r.tv_nsec > 0) {
37 r.tv_nsec -= 1000 * 1000 * 1000;
42 static struct timespec ts_min(struct timespec a, struct timespec b)
44 if(a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec))
50 static struct timespec ts_max(struct timespec a, struct timespec b)
52 if(a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec))
58 int main(int argc, char **argv)
60 long long tnow, tlast;
61 struct timespec t1, dtmin, dtminp, dtmax;
62 int print_interval = 50000;
63 int print_countdown = 1;
64 int clock_id = CLOCK_MONOTONIC;
65 dtmin.tv_sec = INT_MAX;
67 dtminp.tv_sec = INT_MAX;
74 clock_id = atoi(argv[1]);
75 printf("using clock %d\n", clock_id);
77 clock_gettime(clock_id, &t1);
80 struct timespec t, dt;
81 clock_gettime(clock_id, &t);
84 dtmin = ts_min(dtmin, dt);
85 if(dt.tv_sec > 0 || dt.tv_nsec > 0)
86 dtminp = ts_min(dtminp, dt);
87 if(print_countdown != print_interval)
88 dtmax = ts_max(dtmax, dt);
89 if(--print_countdown == 0) {
90 fprintf(stderr,"%09ld.%09ld, dt %ld.%09ld, min %ld.%09ld, minp %ld.%09ld, max %ld.%09ld\n",
91 t.tv_sec, t.tv_nsec, dt.tv_sec, dt.tv_nsec,
92 dtmin.tv_sec, dtmin.tv_nsec, dtminp.tv_sec, dtminp.tv_nsec,
93 dtmax.tv_sec, dtmax.tv_nsec);
94 print_countdown = print_interval;
101 fprintf(stderr,"time went backwards: %lld -> %lld\n",
105 fprintf(stderr,"%lld ROLLBACK\n", tnow);
107 fprintf(stderr,"%lld\n", tnow);