freebsd-skq/contrib/ntp/util/hist.c

111 lines
1.9 KiB
C
Raw Normal View History

1999-12-09 13:01:21 +00: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.
*/
#ifdef HAVE_CONFIG_H
2001-08-29 14:35:15 +00:00
# include <config.h>
1999-12-09 13:01:21 +00:00
#endif
#include "ntp_types.h"
2001-08-29 14:35:15 +00:00
#include <stdio.h>
#include <stdlib.h>
1999-12-09 13:01:21 +00:00
#define NBUF 100001 /* size of basic histogram */
#define NSRT 20000 /* size of overflow histogram */
#define NCNT (600 * 1000000) /* sample interval (us) */
int col (const void *, const void *);
1999-12-09 13:01:21 +00:00
int
main(
int argc,
char *argv[]
)
{
struct timeval ts, tr, tp;
struct timezone tzp;
1999-12-09 13:01:21 +00:00
int i, j, n;
long t, u, v, w, gtod[NBUF], ovfl[NSRT];
1999-12-09 13:01:21 +00:00
/*
* Force pages into memory
*/
for (i = 0; i < NBUF; i++)
gtod[i] = 0;
1999-12-09 13:01:21 +00:00
for (i = 0; i < NSRT; i++)
ovfl[i] = 0;
1999-12-09 13:01:21 +00:00
/*
* Construct histogram
*/
n = 0;
gettimeofday(&ts, &tzp);
t = ts.tv_sec * 1000000 + ts.tv_usec;
1999-12-09 13:01:21 +00:00
v = t;
while (1) {
gettimeofday(&tr, &tzp);
u = tr.tv_sec * 1000000 + tr.tv_usec;
1999-12-09 13:01:21 +00:00
if (u - v > NCNT)
break;
1999-12-09 13:01:21 +00:00
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++;
1999-12-09 13:01:21 +00:00
} else {
gtod[w]++;
}
ts = tr;
1999-12-09 13:01:21 +00:00
t = u;
}
/*
* Write out histogram
*/
for (i = 0; i < NBUF - 1; i++) {
if (gtod[i] > 0)
printf("%ld %ld\n", i, gtod[i]);
1999-12-09 13:01:21 +00:00
}
if (n == 0)
return;
qsort(&ovfl, (size_t)n, sizeof(ovfl[0]), col);
1999-12-09 13:01:21 +00:00
w = 0;
j = 0;
for (i = 0; i < n; i++) {
if (ovfl[i] != w) {
if (j > 0)
printf("%ld %ld\n", w, j);
1999-12-09 13:01:21 +00:00
w = ovfl[i];
j = 1;
} else
j++;
1999-12-09 13:01:21 +00:00
}
if (j > 0)
printf("%ld %ld\n", w, j);
1999-12-09 13:01:21 +00:00
exit(0);
}
int
col(
const void *vx,
const void *vy
1999-12-09 13:01:21 +00:00
)
{
const long *x = vx;
const long *y = vy;
1999-12-09 13:01:21 +00:00
return (*x - *y);
}