Add ctf::src::normalizeClkOffset()
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 11 Dec 2023 17:24:39 +0000 (12:24 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
Also, use it in `visitor-generate-ir.cpp` instead of the local
function.

Change-Id: I5073a901f8c4c434ad35e102aaeb145a30055506
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12718
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/Makefile.am
src/plugins/ctf/common/src/metadata/normalize-clk-offset.cpp [new file with mode: 0644]
src/plugins/ctf/common/src/metadata/normalize-clk-offset.hpp [new file with mode: 0644]
src/plugins/ctf/common/src/metadata/tsdl/visitor-generate-ir.cpp

index 408a37627c4cc0a662ceb28dde727777ad43deb4..9deaa9ae6acfbb2a47456aa433eba4988b924636 100644 (file)
@@ -706,6 +706,8 @@ plugins_ctf_babeltrace_plugin_ctf_la_SOURCES = \
        plugins/ctf/common/src/metadata/json/strings.hpp \
        plugins/ctf/common/src/metadata/json/val-req.cpp \
        plugins/ctf/common/src/metadata/json/val-req.hpp \
+       plugins/ctf/common/src/metadata/normalize-clk-offset.cpp \
+       plugins/ctf/common/src/metadata/normalize-clk-offset.hpp \
        plugins/ctf/common/src/metadata/tsdl/metadata-stream-decoder.cpp \
        plugins/ctf/common/src/metadata/tsdl/metadata-stream-decoder.hpp \
        plugins/ctf/common/src/msg-iter.cpp \
diff --git a/src/plugins/ctf/common/src/metadata/normalize-clk-offset.cpp b/src/plugins/ctf/common/src/metadata/normalize-clk-offset.cpp
new file mode 100644 (file)
index 0000000..ea085c3
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright 2022 EfficiOS, Inc.
+ */
+
+#include "normalize-clk-offset.hpp"
+
+namespace ctf {
+namespace src {
+
+std::pair<long long, unsigned long long> normalizeClkOffset(long long offsetSeconds,
+                                                            unsigned long long offsetCycles,
+                                                            const unsigned long long freq) noexcept
+{
+    if (offsetCycles >= freq) {
+        const unsigned long long secInOffsetCycles = offsetCycles / freq;
+
+        offsetSeconds += (long long) secInOffsetCycles;
+        offsetCycles -= secInOffsetCycles * freq;
+    }
+
+    return std::make_pair(offsetSeconds, offsetCycles);
+}
+
+} /* namespace src */
+} /* namespace ctf */
diff --git a/src/plugins/ctf/common/src/metadata/normalize-clk-offset.hpp b/src/plugins/ctf/common/src/metadata/normalize-clk-offset.hpp
new file mode 100644 (file)
index 0000000..fd8c8a3
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright 2022 EfficiOS, Inc.
+ */
+
+#ifndef BABELTRACE_PLUGINS_CTF_COMMON_SRC_METADATA_NORMALIZE_CLK_OFFSET_HPP
+#define BABELTRACE_PLUGINS_CTF_COMMON_SRC_METADATA_NORMALIZE_CLK_OFFSET_HPP
+
+#include <utility>
+
+namespace ctf {
+namespace src {
+
+/*
+ * Normalizes `offsetSeconds` and `offsetCycles` so that the cycle part
+ * is less than the frequency `freq` and returns the new offset parts.
+ */
+std::pair<long long, unsigned long long> normalizeClkOffset(long long offsetSeconds,
+                                                            unsigned long long offsetCycles,
+                                                            unsigned long long freq) noexcept;
+
+} /* namespace src */
+} /* namespace ctf */
+
+#endif /* BABELTRACE_PLUGINS_CTF_COMMON_SRC_METADATA_NORMALIZE_CLK_OFFSET_HPP */
index 48034a3fcbdeba2866eaa6d5983c704402b7c510..e430c18b326c4f38b1de274ce972ec0006cdb30c 100644 (file)
@@ -10,6 +10,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#include <babeltrace2/babeltrace.h>
+
 #include "logging.hpp"
 
 #include "common/assert.h"
 #include "compat/endian.h" /* IWYU pragma: keep  */
 #include "cpp-common/bt2c/logging.hpp"
 
+#include "../normalize-clk-offset.hpp"
 #include "ast.hpp"
 #include "ctf-meta-visitors.hpp"
+#include "ctf-meta.hpp"
+#include "decoder.hpp"
 
 /* Bit value (left shift) */
 #define _BV(_val) (1 << (_val))
@@ -4178,17 +4183,6 @@ static inline uint64_t cycles_from_ns(uint64_t frequency, uint64_t ns)
     return cycles;
 }
 
-static void calibrate_clock_class_offsets(int64_t *offset_seconds, uint64_t *offset_cycles,
-                                          uint64_t freq)
-{
-    if (*offset_cycles >= freq) {
-        const uint64_t s_in_offset_cycles = *offset_cycles / freq;
-
-        *offset_seconds += (int64_t) s_in_offset_cycles;
-        *offset_cycles -= (s_in_offset_cycles * freq);
-    }
-}
-
 static void apply_clock_class_is_absolute(struct ctf_visitor_generate_ir *ctx,
                                           struct ctf_clock_class *clock)
 {
@@ -4207,6 +4201,8 @@ static void apply_clock_class_offset(struct ctf_visitor_generate_ir *ctx,
     uint64_t offset_ns_to_apply;
     int64_t cur_offset_s;
     uint64_t cur_offset_cycles;
+    long long offsetSecLL;
+    unsigned long long offsetCyclesULL;
 
     if (ctx->decoder_config.clkClsCfg.offsetSec == 0 &&
         ctx->decoder_config.clkClsCfg.offsetNanoSec == 0) {
@@ -4246,11 +4242,16 @@ static void apply_clock_class_offset(struct ctf_visitor_generate_ir *ctx,
      * Recalibrate offsets because the part in cycles can be greater
      * than the frequency at this point.
      */
-    calibrate_clock_class_offsets(&cur_offset_s, &cur_offset_cycles, freq);
+    {
+        offsetSecLL = cur_offset_s;
+        offsetCyclesULL = cur_offset_cycles;
 
-    /* Set final offsets */
-    clock->offset_seconds = cur_offset_s;
-    clock->offset_cycles = cur_offset_cycles;
+        const auto offsetParts = ctf::src::normalizeClkOffset(offsetSecLL, offsetCyclesULL, freq);
+
+        /* Set final offsets */
+        clock->offset_seconds = offsetParts.first;
+        clock->offset_cycles = offsetParts.second;
+    }
 
 end:
     return;
@@ -4266,7 +4267,8 @@ static int visit_clock_decl(struct ctf_visitor_generate_ir *ctx, struct ctf_node
     const char *clock_class_name;
     int64_t offset_seconds = 0;
     uint64_t offset_cycles = 0;
-    uint64_t freq;
+    long long offsetSecLL;
+    unsigned long long offsetCyclesULL;
 
     if (clock_node->visited) {
         return 0;
@@ -4313,11 +4315,18 @@ static int visit_clock_decl(struct ctf_visitor_generate_ir *ctx, struct ctf_node
      * Adjust offsets so that the part in cycles is less than the
      * frequency (move to the part in seconds).
      */
-    freq = clock->frequency;
-    calibrate_clock_class_offsets(&offset_seconds, &offset_cycles, freq);
-    BT_ASSERT(offset_cycles < clock->frequency);
-    clock->offset_seconds = offset_seconds;
-    clock->offset_cycles = offset_cycles;
+    {
+        offsetSecLL = offset_seconds;
+        offsetCyclesULL = offset_cycles;
+        const auto normalized =
+            ctf::src::normalizeClkOffset(offsetSecLL, offsetCyclesULL, clock->frequency);
+        offsetSecLL = normalized.first;
+        offsetCyclesULL = normalized.second;
+        BT_ASSERT(offsetCyclesULL < clock->frequency);
+        clock->offset_seconds = offsetSecLL;
+        clock->offset_cycles = offsetCyclesULL;
+    }
+
     apply_clock_class_offset(ctx, clock);
     apply_clock_class_is_absolute(ctx, clock);
     g_ptr_array_add(ctx->ctf_tc->clock_classes, clock);
This page took 0.029328 seconds and 4 git commands to generate.