/* * Program to measure high-res timer latency. * */ #include #include #include #include #include #include #include #include #ifndef N_SAMPLES #define N_SAMPLES 100 #endif int main(int argc, char *const* argv, char *const* envp) { clockid_t clk = CLOCK_MONOTONIC_RAW; if((argc > 1) && (argv[1] != NULL) && (0 == strcmp(argv[1],"-m"))) clk = CLOCK_MONOTONIC; struct timespec sample[N_SAMPLES+1]; unsigned int cnt=N_SAMPLES, s=0 ; do { if( 0 != clock_gettime(CLOCK_MONOTONIC_RAW, &sample[s++]) ) { fprintf(stderr,"oops, clock_gettime() failed: %d: '%s'.\n", errno, strerror(errno)); return 1; } }while( --cnt ); clock_gettime(CLOCK_MONOTONIC_RAW, &sample[s]); #define TS2NS(_TS_) ((((unsigned long long)(_TS_).tv_sec)*1000000000ULL) + (((unsigned long long)((_TS_).tv_nsec)))) unsigned long long deltas [ N_SAMPLES ] , t1, t2, sum=0 , t_start = TS2NS(sample[0]); for(s=1; s < (N_SAMPLES+1); s+=1) { t1 = TS2NS(sample[s-1]); t2 = TS2NS(sample[s]); deltas[s-1]=t2-t1; } for(s = 0; s < N_SAMPLES; s+=1) sum += deltas[s]; fprintf(stderr,"sum: %llu\n",sum); unsigned long long avg_ns = sum / N_SAMPLES; t1=(t2 - t_start); fprintf(stderr, "Total time: %1.1llu.%9.9lluS - Average Latency: %1.1llu.%9.9lluS\n", t1/1000000000, t1-((t1/1000000000)*1000000000), avg_ns/1000000000, avg_ns-((avg_ns/1000000000)*1000000000) ); return 0; }