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