1 /* 2 * This program can be used to calibrate the clock reading jitter of a 3 * particular CPU and operating system. It first tickles every element 4 * of an array, in order to force pages into memory, then repeatedly calls 5 * gettimeofday() and, finally, writes out the time values for later 6 * analysis. From this you can determine the jitter and if the clock ever 7 * runs backwards. 8 */ 9 10 #if 0 11 #ifdef HAVE_CONFIG_H 12 # include <config.h> 13 #endif 14 15 #include "ntp_types.h" 16 #endif 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <time.h> 21 22 #define NBUF 100001 /* size of basic histogram */ 23 #define NSRT 20000 /* size of overflow histogram */ 24 #define NCNT (600 * 1000000) /* sample interval (us) */ 25 26 extern int col(hrtime_t *, hrtime_t *); 27 extern hrtime_t gethrtime(void); 28 29 int 30 main( 31 int argc, 32 char *argv[] 33 ) 34 { 35 int i, j, n; 36 hrtime_t t, u, v, w, gtod[NBUF], ovfl[NSRT]; 37 38 /* 39 * Force pages into memory 40 */ 41 for (i = 0; i < NBUF; i++) 42 gtod[i] = 0; 43 for (i = 0; i < NSRT; i++) 44 ovfl[i] = 0; 45 46 /* 47 * Construct histogram 48 */ 49 n = 0; 50 t = gethrtime(); 51 v = t; 52 while (1) { 53 u = gethirestime(); 54 if (u - v > NCNT) 55 break; 56 w = u - t; 57 if (w <= 0) { 58 /* 59 printf("error <= 0 %ld %d %d, %d %d\n", w, ts.tv_sec, 60 ts.tv_usec, tr.tv_sec, tr.tv_usec); 61 */ 62 } else if (w > NBUF - 1) { 63 ovfl[n] = w; 64 if (n < NSRT - 1) 65 n++; 66 } else { 67 gtod[w]++; 68 } 69 t = u; 70 } 71 72 /* 73 * Write out histogram 74 */ 75 for (i = 0; i < NBUF - 1; i++) { 76 if (gtod[i] > 0) 77 printf("%ld %ld\n", i, gtod[i]); 78 } 79 if (n == 0) 80 return; 81 qsort((char *)ovfl, (size_t)n, sizeof(hrtime_t), col); 82 w = 0; 83 j = 0; 84 for (i = 0; i < n; i++) { 85 if (ovfl[i] != w) { 86 if (j > 0) 87 printf("%ld %ld\n", w, j); 88 w = ovfl[i]; 89 j = 1; 90 } else 91 j++; 92 } 93 if (j > 0) 94 printf("%ld %ld\n", w, j); 95 96 exit(0); 97 } 98 99 int 100 col( 101 hrtime_t *x, 102 hrtime_t *y 103 ) 104 { 105 return (*x - *y); 106 } 107