Skip to content
Commits on Source (2)
  • Eric S. Raymond's avatar
  • Eric S. Raymond's avatar
    Remove the undocumented util/hist.c code. · 647a1642
    Eric S. Raymond authored
    Daniel Franke explains:
    
        This whole module is a big mess of undefined behavior.
    
        If you're going to store time as a count of nanoseconds, then of
        course you're going to overflow a 32-bit integer quickly. 'long' is 32
        bits on most 32-bit ABIs. You want all your working variables to be
        int64_t. The correct way to express an int64_t literal is
    
        #include <stdint.h>
        INT64_C(9999999999999)
    
        But even a 64-bit integer risks overflow, because CLOCK_MONOTONIC has
        an unspecified starting point. (tv_sec * NANOSECONDS) may not fit in
        an int64_t. Since you only care about time differences and not
        absolute times, you can get around this by normalizing your starting
        point to tv_sec = 0. So declare
    
        time_t s;
    
        and then have your initialization be:
    
        clock_gettime(CLOCK_MONOTONIC, &ts);
        s = ts.tv_sec;
        t = ts.tv_nsec;
    
        and change the u assignment to read:
    
        u = (tr.tv_sec - s) * NANOSECONDS + ts.tv_nsec;
    
        In the printf statements, use the macro PRId64 to give you the correct
        printf conversion specifier for an int64_t.
    
        The col() function needs to be rewritten to make sure that casting its
        return type to int won't overflow.
    
        All the comments about forcing pages into memory are bizarre. Of
        course the code that follows has to be there because otherwise gtod
        will be used uninitialized. But no memory is being locked so there's
        no guarantee that those pages will still be there by the time they're
        used, especially given all the intervening context switches to call
        clock_gettime().
    
        clock_gettime()'s return value needs to be checked.
    
    Time to shoot this sick dog.  It wasn't doing us any good, anyway;
    totally undocumented.  If we need this kind of clock sanity checking, better
    to write a decent one from scratch.
    647a1642
......@@ -366,6 +366,8 @@ long intercept_ntp_random(const char *legend)
return roll;
}
extern bool drift_read(const char *drift_file, double *drift);
bool intercept_drift_read(const char *drift_file, double *drift)
{
if (mode == replay) {
......@@ -377,25 +379,8 @@ bool intercept_drift_read(const char *drift_file, double *drift)
replay_fail("garbled event format\n");
*drift = df;
} else {
FILE *fp;
if ((fp = fopen(drift_file, "r")) == NULL) {
if (mode == capture)
printf("drift-read false\n");
return false;
}
if (fscanf(fp, "%lf", drift) != 1) {
msyslog(LOG_ERR,
"format error frequency file %s",
drift_file);
fclose(fp);
if (mode == capture)
printf("drift-read false\n");
if (!drift_read(drift_file, drift))
return false;
}
fclose(fp);
if (mode == capture)
printf("drift-read %.3f\n", *drift);
......
......@@ -213,6 +213,23 @@ write_stats(void)
/*
* stats_config - configure the stats operation
*/
bool drift_read(const char *drift_file, double *drift)
{
FILE *fp;
if ((fp = fopen(drift_file, "r")) == NULL) {
return false;
}
if (fscanf(fp, "%lf", drift) != 1) {
msyslog(LOG_ERR,
"format error frequency file %s",
drift_file);
fclose(fp);
return false;
}
fclose(fp);
return true;
}
void
stats_config(
int item,
......
......@@ -7,18 +7,6 @@ the header comments.
calc_tickadj:: Calculates "optimal" value for tick given ntp.drift file
Tested: 20160226
hist.c:: 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
clock_gettime() and, finally, writes out the time
values for later analysis. From this you can determine
the jitter and if the clock ever runs backwards.
To compile in the util directory, run waf configure and:
gcc -I ../include/ -I ../build/ hist.c
There is no documentation on how to interpret results.
Tested: 20160226
kern.c:: Header comment from deep in the mists of past time says:
"This program simulates a first-order, type-II
phase-lock loop using actual code segments from
......
/*
* 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
* clock_gettime() 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 <config.h>
#include "ntp_types.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NANOSECONDS 1000000000L
#define NBUF 100001 /* size of basic histogram */
#define NSRT 20000 /* size of overflow histogram */
#define NCNT (600L * NANOSECONDS) /* sample interval (ns) */
int col (const void *, const void *);
int
main(
int argc,
char *argv[]
)
{
struct timespec ts, tr;
int i, j, n;
long t, u, v, w, gtod[NBUF], ovfl[NSRT];
UNUSED_ARG(argc);
UNUSED_ARG(argv);
/*
* 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;
clock_gettime(CLOCK_MONOTONIC, &ts);
t = ts.tv_sec * NANOSECONDS + ts.tv_nsec;
v = t;
while (1) {
clock_gettime(CLOCK_MONOTONIC, &tr);
u = tr.tv_sec * NANOSECONDS + tr.tv_nsec;
if (u - v > NCNT)
break;
w = u - t;
if (w <= 0) {
/*
printf("error <= 0 %ld %ld %ld, %ld %ld\n", w,
(long)ts.tv_sec, (long)ts.tv_nsec,
(long)tr.tv_sec, (long)tr.tv_nsec);
*/
} 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("%d %ld\n", i, gtod[i]);
}
if (n == 0)
exit(0);
qsort(&ovfl, (size_t)n, sizeof(ovfl[0]), col);
w = 0;
j = 0;
for (i = 0; i < n; i++) {
if (ovfl[i] != w) {
if (j > 0)
printf("%ld %d\n", w, j);
w = ovfl[i];
j = 1;
} else
j++;
}
if (j > 0)
printf("%ld %d\n", w, j);
exit(0);
}
int
col(
const void *vx,
const void *vy
)
{
const long *x = vx;
const long *y = vy;
return (*x - *y);
}