[Git][NTPsec/ntpsec][master] Tweak attic/clocks, add attic/backwards
Hal Murray
gitlab at mg.gitlab.com
Sun Apr 14 07:53:18 UTC 2019
Hal Murray pushed to branch master at NTPsec / ntpsec
Commits:
796dc0b4 by Hal Murray at 2019-04-14T07:52:21Z
Tweak attic/clocks, add attic/backwards
- - - - -
3 changed files:
- + attic/backwards.c
- attic/clocks.c
- attic/wscript
Changes:
=====================================
attic/backwards.c
=====================================
@@ -0,0 +1,66 @@
+/* backwards.c - check for time going backwards
+ * Started from Artturi Alm on port-arm at netbsd.org
+ * Fri, 25 Jan 2019 09:54:42 +0200
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define UNUSED_ARG(arg) ((void)(arg))
+
+static void ts_print(struct timespec *ts0, struct timespec *ts1);
+
+
+void ts_print(struct timespec *ts0, struct timespec *ts1)
+{
+ printf("ts0=(%ld,%ld)\nts1=(%ld,%ld)\n",
+ (long)ts0->tv_sec, ts0->tv_nsec,
+ (long)ts1->tv_sec, ts1->tv_nsec);
+}
+
+int
+main(int argc, char *argv[])
+{
+ UNUSED_ARG(argc);
+ UNUSED_ARG(argv);
+
+ struct timespec ts0, ts1;
+
+
+ /* Some systems return equal.
+ * Print first 5 equal examples. */
+ for(int equal = 0; equal < 5; ) {
+ clock_gettime(CLOCK_MONOTONIC, &ts0); /* warm up cache */
+ clock_gettime(CLOCK_MONOTONIC, &ts1);
+ clock_gettime(CLOCK_MONOTONIC, &ts0);
+ clock_gettime(CLOCK_MONOTONIC, &ts1);
+
+ if ((ts0.tv_sec < ts1.tv_sec) ||
+ ((ts0.tv_sec == ts1.tv_sec) &&
+ (ts0.tv_nsec <= ts1.tv_nsec))) {
+ } else {
+ ts_print(&ts0, &ts1);
+ if (ts0.tv_sec == ts1.tv_sec
+ && ts0.tv_nsec == ts1.tv_nsec)
+ equal++;
+ }
+
+ }
+
+ for(;;) {
+ clock_gettime(CLOCK_MONOTONIC, &ts0); /* warm up cache */
+ clock_gettime(CLOCK_MONOTONIC, &ts1);
+ clock_gettime(CLOCK_MONOTONIC, &ts0);
+ clock_gettime(CLOCK_MONOTONIC, &ts1);
+
+ if ((ts0.tv_sec < ts1.tv_sec) ||
+ ((ts0.tv_sec == ts1.tv_sec) &&
+ (ts0.tv_nsec < ts1.tv_nsec))) {
+ } else {
+ ts_print(&ts0, &ts1);
+ }
+
+ }
+}
+
=====================================
attic/clocks.c
=====================================
@@ -12,7 +12,9 @@ struct table {
#define BATCHSIZE 1000000
#define BILLION 1000000000
-#define HISTSIZE 250
+#define HISTSIZE 2500
+#define NSPERBUCKET 1
+#define MAXHISTLINES 10
struct table clocks [] = {
{CLOCK_REALTIME, "CLOCK_REALTIME"},
@@ -38,7 +40,7 @@ int dups;
int do_res(int type, const char* name);
int do_average(int type, const char* name);
int do_fastest(int type, const char* name);
-void do_hist(int type, int fastest);
+int do_hist(int type, int fastest);
int do_res(int type, const char* name) {
int err;
@@ -101,23 +103,33 @@ int do_fastest(int type, const char* name) {
return fastest;
}
-void do_hist(int type, int fastest) {
- int nsPerBucket = 5;
+int do_hist(int type, int fastest) {
+ int nsPerBucket = NSPERBUCKET;
int i;
int delta, lines, toobig, hits, miss;
struct timespec start, stop;
- uint64_t sec, nanos;
+ int64_t sec, nanos;
unsigned int hist[HISTSIZE];
+ int faster = 0;
dups = 0;
toobig = 0;
for (i = 0; i < HISTSIZE; i++) hist[i] = 0;
for (i = 0; i < BATCHSIZE; i++) {
+ clock_gettime(type, &start); /* warm up cache */
+ clock_gettime(type, &stop);
clock_gettime(type, &start);
clock_gettime(type, &stop);
sec = (stop.tv_sec-start.tv_sec);
nanos = sec*BILLION + (stop.tv_nsec-start.tv_nsec);
+ if (0 > nanos) {
+ printf("## Time went backwards: %ld.%9ld-%ld.%9ld=>%ld\n",
+ (long)stop.tv_sec, stop.tv_nsec,
+ (long)start.tv_sec, start.tv_nsec,
+ (long)nanos);
+ continue;
+ }
if (0 == nanos) {
/* I've seen this on Pi 1. */
dups++;
@@ -125,8 +137,11 @@ void do_hist(int type, int fastest) {
}
delta = (nanos-fastest) / nsPerBucket;
if (0 > delta) {
- /* fastest wasn't fast enough. */
- printf("Too small: %ld, %d\n", (long)nanos, fastest);
+ /* fastest wasn't fast enough */
+ if (0 == faster)
+ faster = delta;
+ if (faster > delta)
+ faster = delta;
continue;
}
if (delta < HISTSIZE) {
@@ -135,6 +150,7 @@ void do_hist(int type, int fastest) {
toobig++;
}
}
+ if (faster) return faster;
printf("\n");
printf("Histogram: CLOCK_REALTIME, %d ns per bucket, %d samples.\n",
nsPerBucket, BATCHSIZE);
@@ -142,20 +158,23 @@ void do_hist(int type, int fastest) {
lines = 0;
hits = 0;
for (i=0; i<HISTSIZE; i++) {
- if ((5 <= lines) && (0.98*BATCHSIZE<hits)) break; /* minimal printout */
+ if ((MAXHISTLINES <= lines) && (0.98*BATCHSIZE < hits))
+ break; /* Avoid clutter of printing long tail */
if (hist[i]) {
printf("%10d %8u\n", i*nsPerBucket+fastest, hist[i]);
lines++;
hits += hist[i];
}
}
- miss = i;
+ miss = i-1;
for ( ; i<HISTSIZE; i++) {
toobig += hist[i];
}
+ if (dups) printf("%d samples were duplicated.\n", dups);
if (toobig) printf("%d samples were bigger than %d.\n",
toobig, miss*nsPerBucket+fastest);
+ return faster;
}
@@ -166,12 +185,12 @@ int main(int argc, char *argv[])
(void)argc; /* Squash unused warnings */
(void)argv;
-printf(" res avg min dups CLOCK\n");
+printf(" res avg min dups CLOCK\n");
for (int i=0; (NULL != clocks[i].name); i++) {
res = do_res(clocks[i].type, clocks[i].name);
average = do_average(clocks[i].type, clocks[i].name);
fastest = do_fastest(clocks[i].type, clocks[i].name);
- printf("%9d %5d %7d", res, average, fastest);
+ printf("%9d %5d %8d", res, average, fastest);
if (0.9*BATCHSIZE < dups)
/* Hack: negative to show good if close to all are bad */
printf(" %5d", dups-BATCHSIZE);
@@ -184,8 +203,14 @@ for (int i=0; (NULL != clocks[i].name); i++) {
}
if (1) {
+ int faster;
fastest = do_fastest(CLOCK_REALTIME, "CLOCK_REALTIME");
- do_hist(CLOCK_REALTIME, fastest);
+ while (1) {
+ faster = do_hist(CLOCK_REALTIME, fastest);
+ if (0 == faster) break;
+ printf("Found faster: %d => %d\n", fastest, faster);
+ fastest = faster;
+ }
}
return 0;
=====================================
attic/wscript
=====================================
@@ -1,5 +1,5 @@
def build(ctx):
- util = ['sht', 'digest-find', 'digest-timing', 'clocks']
+ util = ['sht', 'digest-find', 'digest-timing', 'clocks', 'backwards']
for name in util:
ctx(
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/796dc0b495f04fa8303d5d7d687f3872aa3efc84
--
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/796dc0b495f04fa8303d5d7d687f3872aa3efc84
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190414/065360bf/attachment-0001.html>
More information about the vc
mailing list