File: hist.c

package info (click to toggle)
xntp3 5.93-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,644 kB
  • ctags: 7,419
  • sloc: ansic: 59,474; perl: 3,633; sh: 2,623; awk: 417; makefile: 311; asm: 37
file content (93 lines) | stat: -rw-r--r-- 1,833 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * This program can be used to calibrate the clock reading jitter of a
 * particular CPU and operating system. It first tickles every element
 * of an array, in order to force pages into memory, then repeatedly calls
 * gettimeofday() and, finally, writes out the time values for later
 * analysis. From this you can determine the jitter and if the clock ever
 * runs backwards.
 */
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>

#define NBUF 100001		/* size of basic histogram */
#define NSRT 20000		/* size of overflow histogram */
#define NCNT (600 * 1000000)	/* sample interval (us) */

int col(long *, long *);

void
main()
{
	struct timeval ts, tr, tp;
	struct timezone tzp;
	int i, j, n;
	long t, u, v, w, gtod[NBUF], ovfl[NSRT];

	/*
	 * Force pages into memory
	 */
	for (i = 0; i < NBUF; i++)
		gtod[i] = 0;
	for (i = 0; i < NSRT; i++)
		ovfl[i] = 0;

	/*
	 * Construct histogram
	 */
	n = 0;
	gettimeofday(&ts, &tzp);
	t = ts.tv_sec * 1000000 + ts.tv_usec;
	v = t;
	while (1) {
		gettimeofday(&tr, &tzp);
		u = tr.tv_sec * 1000000 + tr.tv_usec; 
		if (u - v > NCNT)
			break;
		w = u - t;
		if (w <= 0) {
			printf("error <= 0 %ld %d %d, %d %d\n", w, ts.tv_sec,
			    ts.tv_usec, tr.tv_sec, tr.tv_usec);
		} else if (w > NBUF - 1) {
			ovfl[n] = w;
			if (n < NSRT - 1)
				n++;
		} else {
			gtod[w]++;
		}
		ts = tr;
		t = u;
	}

	/*
	 * Write out histogram
	 */
        for (i = 0; i < NBUF - 1; i++) {
                if (gtod[i] > 0)
			printf("%ld %ld\n", i, gtod[i]);
	}
	if (n == 0)
		return;
	qsort((char *)ovfl, (int)n, sizeof(long), col);
	w = 0;
	j = 0;
	for (i = 0; i < n; i++) {
		if (ovfl[i] != w) {
			if (j > 0)
				printf("%ld %ld\n", w, j);
			w = ovfl[i];
			j = 1;
		} else
			j++;
	}
	if (j > 0)
		printf("%ld %ld\n", w, j);
 
}

int
col(x, y)
	long *x, *y;
{
	return (*x - *y);
}