--- /dev/null
+/*
+ * 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 */
--- /dev/null
+/*
+ * 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 */
#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))
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)
{
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) {
* 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;
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;
* 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);