Move `measure_clock_offset()` to its own file
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 11 Jan 2022 22:43:36 +0000 (17:43 -0500)
committerFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 15 Jul 2022 18:08:13 +0000 (14:08 -0400)
Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I008fdd9d09fdca6728357774aa9adf4dece2a59c

src/Kbuild
src/clock-utils.c [new file with mode: 0644]
src/clock-utils.h [new file with mode: 0644]
src/ctf1-8.c

index a4f6dddd620c23c04f45d30870a49b445c5b5235..ed7638504ccc93ce7ff7fe052760d0f742719fcb 100644 (file)
@@ -49,6 +49,7 @@ lttng-tracer-objs := lib/msgpack/msgpack.o \
                      lttng-events.o lttng-abi.o lttng-string-utils.o \
                      lttng-probes.o lttng-context.o \
                      metadata-printer.o \
+                     clock-utils.o \
                      ctf1-8.o \
                      lttng-context-pid.o lttng-context-procname.o \
                      lttng-context-prio.o lttng-context-nice.o \
diff --git a/src/clock-utils.c b/src/clock-utils.c
new file mode 100644 (file)
index 0000000..1abc94d
--- /dev/null
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
+ *
+ * src/clock-utils.h
+ *
+ * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ */
+
+/*
+ * 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.
+ * This function may return a negative offset. It may happen if the
+ * system sets the REALTIME clock to 0 after boot.
+ *
+ * Use 64bit timespec on kernels that have it, this makes 32bit arch
+ * y2038 compliant.
+ */
+
+#include <linux/types.h>
+
+#include <wrapper/time.h>
+#include <wrapper/trace-clock.h>
+
+int64_t trace_clock_measure_offset(void)
+{
+       uint64_t monotonic_avg, monotonic[2], realtime;
+       uint64_t tcf = trace_clock_freq();
+       int64_t offset;
+       unsigned long flags;
+#ifdef LTTNG_KERNEL_HAS_TIMESPEC64
+       struct timespec64 rts = { 0, 0 };
+#else
+       struct timespec rts = { 0, 0 };
+#endif
+
+       /* Disable interrupts to increase correlation precision. */
+       local_irq_save(flags);
+       monotonic[0] = trace_clock_read64();
+#ifdef LTTNG_KERNEL_HAS_TIMESPEC64
+       ktime_get_real_ts64(&rts);
+#else
+       getnstimeofday(&rts);
+#endif
+       monotonic[1] = trace_clock_read64();
+       local_irq_restore(flags);
+
+       monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
+       realtime = (uint64_t)rts.tv_sec * tcf;
+       if (tcf == NSEC_PER_SEC) {
+               realtime += rts.tv_nsec;
+       } else {
+               uint64_t n = rts.tv_nsec * tcf;
+
+               do_div(n, NSEC_PER_SEC);
+               realtime += n;
+       }
+       offset = (int64_t)realtime - monotonic_avg;
+       return offset;
+}
diff --git a/src/clock-utils.h b/src/clock-utils.h
new file mode 100644 (file)
index 0000000..8e78d66
--- /dev/null
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
+ *
+ * src/clock-utils.h
+ *
+ * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ */
+
+#ifndef LTTNG_CLOCK_UTILS_H
+#define LTTNG_CLOCK_UTILS_H
+
+#include <linux/types.h>
+
+int64_t trace_clock_measure_offset(void);
+
+#endif /* LTTNG_CLOCK_UTILS_H */
index 23eee4da986d82ff05fad3c1dab4109135f98fb9..a4e626173ee80aad64b681969abdb7e7593da8a6 100644 (file)
@@ -14,8 +14,8 @@
 #include <lttng/events.h>
 #include <lttng/events-internal.h>
 
-#include <wrapper/time.h>
 #include "metadata-printer.h"
+#include "clock-utils.h"
 
 static
 int _lttng_field_statedump(struct lttng_kernel_session *session,
@@ -822,54 +822,6 @@ int _lttng_event_header_declare(struct lttng_kernel_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.
- * This function may return a negative offset. It may happen if the
- * system sets the REALTIME clock to 0 after boot.
- *
- * Use 64bit timespec on kernels that have it, this makes 32bit arch
- * y2038 compliant.
- */
-int64_t measure_clock_offset(void)
-{
-       uint64_t monotonic_avg, monotonic[2], realtime;
-       uint64_t tcf = trace_clock_freq();
-       int64_t offset;
-       unsigned long flags;
-#ifdef LTTNG_KERNEL_HAS_TIMESPEC64
-       struct timespec64 rts = { 0, 0 };
-#else
-       struct timespec rts = { 0, 0 };
-#endif
-
-       /* Disable interrupts to increase correlation precision. */
-       local_irq_save(flags);
-       monotonic[0] = trace_clock_read64();
-#ifdef LTTNG_KERNEL_HAS_TIMESPEC64
-       ktime_get_real_ts64(&rts);
-#else
-       getnstimeofday(&rts);
-#endif
-       monotonic[1] = trace_clock_read64();
-       local_irq_restore(flags);
-
-       monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
-       realtime = (uint64_t) rts.tv_sec * tcf;
-       if (tcf == NSEC_PER_SEC) {
-               realtime += rts.tv_nsec;
-       } else {
-               uint64_t n = rts.tv_nsec * tcf;
-
-               do_div(n, NSEC_PER_SEC);
-               realtime += n;
-       }
-       offset = (int64_t) realtime - monotonic_avg;
-       return offset;
-}
-
 static
 int print_escaped_ctf_string(struct lttng_kernel_session *session, const char *string)
 {
@@ -1064,7 +1016,7 @@ int _lttng_session_metadata_statedump(struct lttng_kernel_session *session)
                "};\n\n",
                trace_clock_description(),
                (unsigned long long) trace_clock_freq(),
-               (long long) measure_clock_offset()
+               (long long) trace_clock_measure_offset()
                );
        if (ret)
                goto end;
This page took 0.032412 seconds and 5 git commands to generate.