Skip to contentSkip to conversation

Fix: measure UST clock offset with best sample

PATCH Mathieu Desnoyers 1 participant 1 message Jan 27, 2014 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

----- Original Message -----

Quoted 20 lines

From: "Mathieu Desnoyers" <[email protected]>
To: [email protected]
Cc: [email protected], "Mathieu Desnoyers" <[email protected]>
Sent: Monday, January 27, 2014 2:28:36 PM
Subject: [PATCH lttng-tools] Fix: measure UST clock offset with best sample

Fixes #729

---
 src/bin/lttng-sessiond/ust-metadata.c |   57
 ++++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 11 deletions(-)
src/bin/lttng-sessiond/ust-metadata.c +0 -0
index b0f83d2..55d00da 100644

src/bin/lttng-sessiond/ust-metadata.c line 27

Quoted 4 lines
src/bin/lttng-sessiond/ust-metadata.c +1 -0
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>

Hrm, I forgot to remove this include from the previous patch.

Will wait for comments before re-send.

Thanks,

Mathieu

Quoted 92 lines
src/bin/lttng-sessiond/ust-metadata.c +45 -11
 #include <limits.h>
 #include <unistd.h>
 #include <inttypes.h>
@@ -37,6 +38,13 @@
 #define max_t(type, a, b)	((type) ((a) > (b) ? (a) : (b)))
 #endif
 
+#define NR_CLOCK_OFFSET_SAMPLES		10
+
+struct offset_sample {
+	uint64_t offset;		/* correlation offset */
+	uint64_t measure_delta;		/* lower is better */
+};
+
 static inline
 int fls(unsigned int x)
 {
@@ -490,31 +498,58 @@ int _lttng_event_header_declare(struct
 ust_registry_session *session)
 	);
 }
 
-/*
- * Approximation of NTP time of day to clock monotonic correlation,
- * taken at start of trace.
- * Yes, this is only an approximation. Yes, we can (and will) do better
- * in future versions.
- */
 static
-uint64_t measure_clock_offset(void)
+int measure_single_clock_offset(struct offset_sample *sample)
 {
-	uint64_t offset, monotonic[2], realtime;
+	uint64_t offset, monotonic[2], measure_delta, realtime;
 	struct timespec rts = { 0, 0 };
 	int ret;
 
 	monotonic[0] = trace_clock_read64();
 	ret = clock_gettime(CLOCK_REALTIME, &rts);
-	if (ret < 0)
-		return 0;
+	if (ret < 0) {
+		return ret;
+	}
 	monotonic[1] = trace_clock_read64();
+	measure_delta = monotonic[1] - monotonic[0];
+	if (measure_delta > sample->measure_delta) {
+		/*
+		 * Discard value if it took longer to read than the best
+		 * sample so far.
+		 */
+		return 0;
+	}
 	offset = (monotonic[0] + monotonic[1]) >> 1;
 	realtime = (uint64_t) rts.tv_sec * 1000000000ULL;
 	realtime += rts.tv_nsec;
 	offset = realtime - offset;
-	return offset;
+	sample->offset = offset;
+	sample->measure_delta = measure_delta;
+	return 0;
 }
 
+/*
+ * Approximation of NTP time of day to clock monotonic correlation,
+ * taken at start of trace.
+ * Take the median of many measurements to remove uncertainty caused by
+ * preemption.
+ */
+static
+uint64_t measure_clock_offset(void)
+{
+	int i;
+	struct offset_sample offset_best_sample = {
+		.offset = 0,
+		.measure_delta = UINT64_MAX,
+	};
+
+	for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
+		if (measure_single_clock_offset(&offset_best_sample)) {
+			return 0;
+		}
+	}
+	return offset_best_sample.offset;
+}
 
 /*

* Should be called with session registry mutex held.
--
1.7.10.4

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com