From e1e30a8c4db03e39b614e97479a3f0aeb1ceadc7 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 15 Feb 2016 20:13:02 -0500 Subject: [PATCH] ir: clock: use value in cycles instead of ns MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The value of a clock in CTF is in cycles, not in nanoseconds. Here, bt_ctf_clock_get_time() and bt_ctf_clock_set_time() are left as utilities to converter from/to the clock value in cycles. Note that this conversion, unless the frequency is exactly 1 GHz, implies casting 64-bit integers to doubles so it is possible to lose accuracy. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/clock.c | 49 ++++++++++++++++++++-- include/babeltrace/ctf-ir/clock-internal.h | 2 +- include/babeltrace/ctf-ir/clock.h | 5 +++ tests/lib/test_ctf_writer.c | 11 ++++- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/formats/ctf/ir/clock.c b/formats/ctf/ir/clock.c index bf2fbda5b..0bf7f3518 100644 --- a/formats/ctf/ir/clock.c +++ b/formats/ctf/ir/clock.c @@ -327,7 +327,14 @@ int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time) goto end; } - *time = clock->time; + /* Common case where cycles are actually nanoseconds */ + if (clock->frequency == 1000000000) { + *time = (int64_t) clock->value; + } else { + *time = (int64_t) ((1e9 * (double) clock->value) / + (double) clock->frequency); + } + end: return ret; } @@ -337,12 +344,48 @@ int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) int ret = 0; /* Timestamps are strictly monotonic */ - if (!clock || time < clock->time) { + if (!clock) { + ret = -1; + goto end; + } + + /* Common case where cycles are actually nanoseconds */ + if (clock->frequency == 1000000000) { + clock->value = time; + goto end; + } + + ret = bt_ctf_clock_set_value(clock, + (uint64_t) (((double) time * (double) clock->frequency) / 1e9)); + +end: + return ret; +} + +uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock *clock) +{ + uint64_t ret = -1ULL; + + if (!clock) { + goto end; + } + + ret = clock->value; +end: + return ret; +} + +int bt_ctf_clock_set_value(struct bt_ctf_clock *clock, uint64_t value) +{ + int ret = 0; + + /* Timestamps are strictly monotonic */ + if (!clock || value < clock->value) { ret = -1; goto end; } - clock->time = time; + clock->value = value; end: return ret; } diff --git a/include/babeltrace/ctf-ir/clock-internal.h b/include/babeltrace/ctf-ir/clock-internal.h index e13978110..07e37f989 100644 --- a/include/babeltrace/ctf-ir/clock-internal.h +++ b/include/babeltrace/ctf-ir/clock-internal.h @@ -42,7 +42,7 @@ struct bt_ctf_clock { uint64_t precision; int64_t offset_s; /* Offset in seconds */ int64_t offset; /* Offset in ticks */ - int64_t time; /* Current clock value */ + uint64_t value; /* Current clock value */ uuid_t uuid; int uuid_set; int absolute; diff --git a/include/babeltrace/ctf-ir/clock.h b/include/babeltrace/ctf-ir/clock.h index e149d7fad..a9ba7134b 100644 --- a/include/babeltrace/ctf-ir/clock.h +++ b/include/babeltrace/ctf-ir/clock.h @@ -262,6 +262,11 @@ extern int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, extern int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time); +extern uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock *clock); + +extern int bt_ctf_clock_set_value(struct bt_ctf_clock *clock, + uint64_t value); + /* * bt_ctf_clock_get and bt_ctf_clock_put: increment and decrement the * refcount of the clock diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 576c4cf02..8d8ffa915 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -56,8 +56,9 @@ #define DEFAULT_CLOCK_OFFSET_S 0 #define DEFAULT_CLOCK_IS_ABSOLUTE 0 #define DEFAULT_CLOCK_TIME 0 +#define DEFAULT_CLOCK_VALUE 0 -#define NR_TESTS 594 +#define NR_TESTS 597 static int64_t current_time = 42; @@ -2990,11 +2991,17 @@ int main(int argc, char **argv) "bt_ctf_clock_get_time succeeds"); ok(get_time == DEFAULT_CLOCK_TIME, "bt_ctf_clock_get_time returns the correct default time"); + ok(bt_ctf_clock_get_value(clock) == DEFAULT_CLOCK_VALUE, + "bt_ctf_clock_get_value returns the correct default value"); + ok(bt_ctf_clock_set_value(clock, current_time) == 0, + "Set clock value"); + ok(bt_ctf_clock_get_value(clock) == current_time, + "bt_ctf_clock_get_value returns the correct value once it is set"); ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time"); ok(bt_ctf_clock_get_time(clock, &get_time) == 0, "bt_ctf_clock_get_time succeeds"); - ok(get_time == current_time, + ok(get_time >= current_time - 1 && get_time <= current_time + 1, "bt_ctf_clock_get_time returns the correct time once it is set"); ok(bt_ctf_writer_add_clock(writer, clock) == 0, -- 2.34.1