xref: /netbsd-src/external/bsd/ntp/dist/util/jitter.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: jitter.c,v 1.6 2020/05/25 20:47:37 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel  * This program can be used to calibrate the clock reading jitter of a
5abb0f93cSkardel  * particular CPU and operating system. It first tickles every element
6abb0f93cSkardel  * of an array, in order to force pages into memory, then repeatedly
7abb0f93cSkardel  * reads the system clock and, finally, writes out the time values for
8abb0f93cSkardel  * later analysis. From this you can determine the jitter and if the
9abb0f93cSkardel  * clock ever runs backwards.
10abb0f93cSkardel  */
11abb0f93cSkardel 
12abb0f93cSkardel #ifdef HAVE_CONFIG_H
13abb0f93cSkardel # include <config.h>
14abb0f93cSkardel #endif
15abb0f93cSkardel 
16abb0f93cSkardel #include <stdio.h>
17abb0f93cSkardel #include <sys/time.h>
18abb0f93cSkardel #include <stdlib.h>
197476e6e4Schristos #include "ntp_fp.h"
20abb0f93cSkardel 
21abb0f93cSkardel #define NBUF	800002
22abb0f93cSkardel #define JAN_1970 2208988800UL		/* Unix base epoch */
23abb0f93cSkardel #define CLOCK_GETTIME			/* Solaris hires clock */
24abb0f93cSkardel 
25abb0f93cSkardel char progname[10];
26abb0f93cSkardel double sys_residual;
27abb0f93cSkardel double average;
28abb0f93cSkardel void sys_gettime(l_fp *);
29abb0f93cSkardel 
30abb0f93cSkardel int
main(int argc,char * argv[])31abb0f93cSkardel main(
32abb0f93cSkardel 	int argc,
33abb0f93cSkardel 	char *argv[]
34abb0f93cSkardel 	)
35abb0f93cSkardel {
36abb0f93cSkardel 	l_fp tr;
37abb0f93cSkardel 	int i, j;
38abb0f93cSkardel 	double dtemp, gtod[NBUF];
39abb0f93cSkardel 
40abb0f93cSkardel 	/*
41abb0f93cSkardel 	 * Force pages into memory
42abb0f93cSkardel 	 */
43abb0f93cSkardel 	for (i = 0; i < NBUF; i ++)
44abb0f93cSkardel 	    gtod[i] = 0;
45abb0f93cSkardel 
46abb0f93cSkardel 	/*
47abb0f93cSkardel 	 * Construct gtod array
48abb0f93cSkardel 	 */
49abb0f93cSkardel 	for (i = 0; i < NBUF; i ++) {
50abb0f93cSkardel 		get_systime(&tr);
51abb0f93cSkardel 		LFPTOD(&tr, gtod[i]);
52abb0f93cSkardel 	}
53abb0f93cSkardel 
54abb0f93cSkardel 	/*
55abb0f93cSkardel 	 * Write out gtod array for later processing with Matlab
56abb0f93cSkardel 	 */
57abb0f93cSkardel 	average = 0;
58abb0f93cSkardel 	for (i = 0; i < NBUF - 2; i++) {
59abb0f93cSkardel 		gtod[i] = gtod[i + 1] - gtod[i];
60abb0f93cSkardel 		printf("%13.9f\n", gtod[i]);
61abb0f93cSkardel 		average += gtod[i];
62abb0f93cSkardel 	}
63abb0f93cSkardel 
64abb0f93cSkardel 	/*
65abb0f93cSkardel 	 * Sort the gtod array and display deciles
66abb0f93cSkardel 	 */
67abb0f93cSkardel 	for (i = 0; i < NBUF - 2; i++) {
68abb0f93cSkardel 		for (j = 0; j <= i; j++) {
69abb0f93cSkardel 			if (gtod[j] > gtod[i]) {
70abb0f93cSkardel 				dtemp = gtod[j];
71abb0f93cSkardel 				gtod[j] = gtod[i];
72abb0f93cSkardel 				gtod[i] = dtemp;
73abb0f93cSkardel 			}
74abb0f93cSkardel 		}
75abb0f93cSkardel 	}
76abb0f93cSkardel 	average = average / (NBUF - 2);
77abb0f93cSkardel 	fprintf(stderr, "Average %13.9f\n", average);
78abb0f93cSkardel 	fprintf(stderr, "First rank\n");
79abb0f93cSkardel 	for (i = 0; i < 10; i++)
80abb0f93cSkardel 		fprintf(stderr, "%2d %13.9f\n", i, gtod[i]);
81abb0f93cSkardel 	fprintf(stderr, "Last rank\n");
82abb0f93cSkardel 	for (i = NBUF - 12; i < NBUF - 2; i++)
83abb0f93cSkardel 		fprintf(stderr, "%2d %13.9f\n", i, gtod[i]);
84abb0f93cSkardel 	exit(0);
85abb0f93cSkardel }
86abb0f93cSkardel 
87abb0f93cSkardel 
88abb0f93cSkardel /*
89abb0f93cSkardel  * get_systime - return system time in NTP timestamp format.
90abb0f93cSkardel  */
91abb0f93cSkardel void
get_systime(l_fp * now)92abb0f93cSkardel get_systime(
93abb0f93cSkardel 	l_fp *now		/* system time */
94abb0f93cSkardel 	)
95abb0f93cSkardel {
96abb0f93cSkardel 	double dtemp;
97abb0f93cSkardel 
98abb0f93cSkardel #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
99abb0f93cSkardel 	struct timespec ts;	/* seconds and nanoseconds */
100abb0f93cSkardel 
101abb0f93cSkardel 	/*
102abb0f93cSkardel 	 * Convert Unix clock from seconds and nanoseconds to seconds.
103abb0f93cSkardel 	 */
104abb0f93cSkardel # ifdef HAVE_CLOCK_GETTIME
105abb0f93cSkardel 	clock_gettime(CLOCK_REALTIME, &ts);
106abb0f93cSkardel # else
107abb0f93cSkardel 	getclock(TIMEOFDAY, &ts);
108abb0f93cSkardel # endif
109abb0f93cSkardel 	now->l_i = ts.tv_sec + JAN_1970;
110abb0f93cSkardel 	dtemp = ts.tv_nsec / 1e9;
111abb0f93cSkardel 
112abb0f93cSkardel #else /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
113abb0f93cSkardel 	struct timeval tv;	/* seconds and microseconds */
114abb0f93cSkardel 
115abb0f93cSkardel 	/*
116abb0f93cSkardel 	 * Convert Unix clock from seconds and microseconds to seconds.
117abb0f93cSkardel 	 */
118abb0f93cSkardel 	gettimeofday(&tv, NULL);
119abb0f93cSkardel 	now->l_i = tv.tv_sec + JAN_1970;
120abb0f93cSkardel 	dtemp = tv.tv_usec / 1e6;
121abb0f93cSkardel 
122abb0f93cSkardel #endif /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
123abb0f93cSkardel 
124abb0f93cSkardel 	/*
125abb0f93cSkardel 	 * Renormalize to seconds past 1900 and fraction.
126abb0f93cSkardel 	 */
127abb0f93cSkardel 	dtemp += sys_residual;
128abb0f93cSkardel 	if (dtemp >= 1) {
129abb0f93cSkardel 		dtemp -= 1;
130abb0f93cSkardel 		now->l_i++;
131abb0f93cSkardel 	} else if (dtemp < -1) {
132abb0f93cSkardel 		dtemp += 1;
133abb0f93cSkardel 		now->l_i--;
134abb0f93cSkardel 	}
135abb0f93cSkardel 	dtemp *= FRAC;
136abb0f93cSkardel 	now->l_uf = (u_int32)dtemp;
137abb0f93cSkardel }
138