xref: /freebsd/contrib/ntp/util/jitter.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 #include <sys/time.h>
10 #include <stdio.h>
11 
12 #define NBUF 20002
13 
14 int
15 main(
16 	int argc,
17 	char *argv[]
18 	)
19 {
20 	struct timeval ts, tr;
21 	struct timezone tzp;
22 	long temp, j, i, gtod[NBUF];
23 
24 	gettimeofday(&ts, &tzp);
25 
26 	/*
27 	 * Force pages into memory
28 	 */
29 	for (i = 0; i < NBUF; i ++)
30 	    gtod[i] = 0;
31 
32 	/*
33 	 * Construct gtod array
34 	 */
35 	for (i = 0; i < NBUF; i ++) {
36 		gettimeofday(&tr, &tzp);
37 		gtod[i] = (tr.tv_sec - ts.tv_sec) * 1000000 + tr.tv_usec;
38 	}
39 
40 	/*
41 	 * Write out gtod array for later processing with S
42 	 */
43 	for (i = 0; i < NBUF - 2; i++) {
44 		/*
45 		  printf("%lu\n", gtod[i]);
46 		*/
47 		gtod[i] = gtod[i + 1] - gtod[i];
48 		printf("%lu\n", gtod[i]);
49 	}
50 
51 	/*
52 	 * Sort the gtod array and display deciles
53 	 */
54 	for (i = 0; i < NBUF - 2; i++) {
55 		for (j = 0; j <= i; j++) {
56 			if (gtod[j] > gtod[i]) {
57 				temp = gtod[j];
58 				gtod[j] = gtod[i];
59 				gtod[i] = temp;
60 			}
61 		}
62 	}
63 	fprintf(stderr, "First rank\n");
64 	for (i = 0; i < 10; i++)
65 	    fprintf(stderr, "%10ld%10ld\n", i, gtod[i]);
66 	fprintf(stderr, "Last rank\n");
67 	for (i = NBUF - 12; i < NBUF - 2; i++)
68 	    fprintf(stderr, "%10ld%10ld\n", i, gtod[i]);
69 	exit(0);
70 }
71