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
5 * reads the system clock and, finally, writes out the time values for
6 * later analysis. From this you can determine the jitter and if the
7 * clock ever runs backwards.
20 #define FRAC 4294967296. /* a bbbbillion */
21 #define JAN_1970 2208988800UL /* Unix base epoch */
22 #define CLOCK_GETTIME /* Solaris hires clock */
28 void sys_gettime(l_fp *);
38 double dtemp, gtod[NBUF];
41 * Force pages into memory
43 for (i = 0; i < NBUF; i ++)
47 * Construct gtod array
49 for (i = 0; i < NBUF; i ++) {
55 * Write out gtod array for later processing with Matlab
58 for (i = 0; i < NBUF - 2; i++) {
59 gtod[i] = gtod[i + 1] - gtod[i];
60 printf("%13.9f\n", gtod[i]);
65 * Sort the gtod array and display deciles
67 for (i = 0; i < NBUF - 2; i++) {
68 for (j = 0; j <= i; j++) {
69 if (gtod[j] > gtod[i]) {
76 average = average / (NBUF - 2);
77 fprintf(stderr, "Average %13.9f\n", average);
78 fprintf(stderr, "First rank\n");
79 for (i = 0; i < 10; i++)
80 fprintf(stderr, "%2d %13.9f\n", i, gtod[i]);
81 fprintf(stderr, "Last rank\n");
82 for (i = NBUF - 12; i < NBUF - 2; i++)
83 fprintf(stderr, "%2d %13.9f\n", i, gtod[i]);
89 * get_systime - return system time in NTP timestamp format.
93 l_fp *now /* system time */
98 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
99 struct timespec ts; /* seconds and nanoseconds */
102 * Convert Unix clock from seconds and nanoseconds to seconds.
104 # ifdef HAVE_CLOCK_GETTIME
105 clock_gettime(CLOCK_REALTIME, &ts);
107 getclock(TIMEOFDAY, &ts);
109 now->l_i = ts.tv_sec + JAN_1970;
110 dtemp = ts.tv_nsec / 1e9;
112 #else /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
113 struct timeval tv; /* seconds and microseconds */
116 * Convert Unix clock from seconds and microseconds to seconds.
118 gettimeofday(&tv, NULL);
119 now->l_i = tv.tv_sec + JAN_1970;
120 dtemp = tv.tv_usec / 1e6;
122 #endif /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
125 * Renormalize to seconds past 1900 and fraction.
127 dtemp += sys_residual;
131 } else if (dtemp < -1) {
136 now->l_uf = (u_int32)dtemp;