[Git][NTPsec/ntpsec][master] Remove the undocumented util/hist.c code.

Eric S. Raymond gitlab at mg.gitlab.com
Sun Oct 2 13:48:37 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
4379e60e by Eric S. Raymond at 2016-10-02T09:45:46-04:00
Remove the undocumented util/hist.c code.

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.

- - - - -


3 changed files:

- util/README
- − util/hist.c
- util/wscript


Changes:

=====================================
util/README
=====================================
--- a/util/README
+++ b/util/README
@@ -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


=====================================
util/hist.c deleted
=====================================
--- a/util/hist.c
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * 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);
-}


=====================================
util/wscript
=====================================
--- a/util/wscript
+++ b/util/wscript
@@ -4,7 +4,7 @@ def build(ctx):
 	srcnode = ctx.srcnode.abspath()
 	bldnode = ctx.bldnode.abspath()
 
-	util = ['hist', 'sht']
+	util = ['sht']
 
 	for name in util:
 		ctx(



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/4379e60ec1c05458198eafdeabae66fca0739348
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161002/e9dd0661/attachment.html>


More information about the vc mailing list