NetBSD/dist/ntp/util/hist.c

109 lines
1.9 KiB
C
Raw Normal View History

/* $NetBSD: hist.c,v 1.2 2003/12/04 16:23:38 drochner Exp $ */
2000-03-29 16:38:44 +04:00
/*
* 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.
*/
2003-12-04 19:05:14 +03:00
#if 0
2000-03-29 16:38:44 +04:00
#ifdef HAVE_CONFIG_H
2003-12-04 19:05:14 +03:00
# include <config.h>
2000-03-29 16:38:44 +04:00
#endif
#include "ntp_types.h"
2003-12-04 19:05:14 +03:00
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
2000-03-29 16:38:44 +04:00
#define NBUF 100001 /* size of basic histogram */
#define NSRT 20000 /* size of overflow histogram */
#define NCNT (600 * 1000000) /* sample interval (us) */
2003-12-04 19:05:14 +03:00
extern int col(hrtime_t *, hrtime_t *);
extern hrtime_t gethrtime(void);
2000-03-29 16:38:44 +04:00
int
main(
int argc,
char *argv[]
)
{
int i, j, n;
2003-12-04 19:05:14 +03:00
hrtime_t t, u, v, w, gtod[NBUF], ovfl[NSRT];
2000-03-29 16:38:44 +04:00
/*
* 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;
2003-12-04 19:05:14 +03:00
t = gethrtime();
2000-03-29 16:38:44 +04:00
v = t;
while (1) {
2003-12-04 19:05:14 +03:00
u = gethirestime();
2000-03-29 16:38:44 +04:00
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]++;
}
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;
2003-12-04 19:05:14 +03:00
qsort((char *)ovfl, (size_t)n, sizeof(hrtime_t), col);
2000-03-29 16:38:44 +04:00
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);
exit(0);
}
int
col(
2003-12-04 19:05:14 +03:00
hrtime_t *x,
hrtime_t *y
2000-03-29 16:38:44 +04:00
)
{
return (*x - *y);
}