From 61ec14e66eb9e2329776dfe56a928232bc68b94e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 17 Nov 2016 16:47:47 -0500 Subject: [PATCH] Implement bt_ctf_clock_value interface MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/clock.c | 145 ++++++++++----------- formats/ctf/ir/event.c | 5 +- include/babeltrace/ctf-ir/clock-internal.h | 6 + include/babeltrace/ctf-ir/clock.h | 11 +- include/babeltrace/ctf-writer/clock.h | 7 - tests/lib/test_ctf_ir_ref.c | 3 - tests/lib/test_ctf_writer.c | 46 +------ 7 files changed, 87 insertions(+), 136 deletions(-) diff --git a/formats/ctf/ir/clock.c b/formats/ctf/ir/clock.c index 738f2d80..dee93c7a 100644 --- a/formats/ctf/ir/clock.c +++ b/formats/ctf/ir/clock.c @@ -336,30 +336,6 @@ uint64_t ns_from_value(uint64_t frequency, uint64_t value) return ns; } -int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time) -{ - int ret = 0; - - if (!clock || !time) { - ret = -1; - goto end; - } - - - if (!clock->has_value) { - /* - * Clock belongs to a non-writer mode trace and thus - * this function is disabled. - */ - goto end; - } - - *time = (int64_t) ns_from_value(clock->frequency, clock->value); - -end: - return ret; -} - int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) { int ret = 0; @@ -389,52 +365,8 @@ int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) (double) clock->frequency) / 1e9); } - ret = bt_ctf_clock_set_value(clock, value); -end: - return ret; -} - -uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock *clock) -{ - uint64_t ret = -1ULL; - - if (!clock) { - goto end; - } - - if (!clock->has_value) { - /* - * Clock belongs to a non-writer mode trace and thus - * this function is disabled. - */ - 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; - - if (!clock) { - ret = -1; - goto end; - } - - if (!clock->has_value) { - /* - * Clock belongs to a non-writer mode trace and thus - * this function is disabled. - */ - ret = -1; - goto end; - } - - /* Timestamps are strictly monotonic */ - if (value < clock->value) { + if (clock->value > value) { + /* Timestamps must be strictly monotonic. */ ret = -1; goto end; } @@ -519,23 +451,78 @@ void bt_ctf_clock_destroy(struct bt_object *obj) g_free(clock); } -int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock *clock, uint64_t value) +static +void bt_ctf_clock_value_destroy(struct bt_object *obj) +{ + struct bt_ctf_clock_value *value; + + if (!obj) { + return; + } + + value = container_of(obj, struct bt_ctf_clock_value, base); + bt_put(value->clock_class); + g_free(value); +} + +struct bt_ctf_clock_value *bt_ctf_clock_value_create( + struct bt_ctf_clock *clock, uint64_t value) { - int64_t ns = -1ULL; + struct bt_ctf_clock_value *ret = NULL; if (!clock) { goto end; } - /* Initialize nanosecond timestamp to clock's offset in seconds */ - ns = clock->offset_s * 1000000000; + ret = g_new0(struct bt_ctf_clock_value, 1); + if (!ret) { + goto end; + } + + bt_object_init(ret, bt_ctf_clock_value_destroy); + ret->clock_class = bt_get(clock); + ret->value = value; +end: + return ret; +} - /* Add offset in cycles, converted to nanoseconds */ - ns += ns_from_value(clock->frequency, clock->offset); +int bt_ctf_clock_value_get_value( + struct bt_ctf_clock_value *clock_value, uint64_t *raw_value) +{ + int ret = 0; - /* Add given value, converter to nanoseconds */ - ns += ns_from_value(clock->frequency, value); + if (!clock_value || !raw_value) { + ret = -1; + goto end; + } + *raw_value = clock_value->value; end: - return ns; + return ret; +} + +int bt_ctf_clock_value_ns_from_epoch(struct bt_ctf_clock_value *value, + int64_t *ret_value_ns) +{ + int ret = 0; + int64_t ns; + + if (!value || !ret_value_ns) { + ret = -1; + goto end; + } + + /* Initialize nanosecond timestamp to clock's offset in seconds. */ + ns = value->clock_class->offset_s * 1000000000; + + /* Add offset in cycles, converted to nanoseconds. */ + ns += ns_from_value(value->clock_class->frequency, + value->clock_class->offset); + + /* Add given value, converter to nanoseconds. */ + ns += ns_from_value(value->clock_class->frequency, value->value); + + *ret_value_ns = ns; +end: + return ret; } diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c index 505aa104..beac9d48 100644 --- a/formats/ctf/ir/event.c +++ b/formats/ctf/ir/event.c @@ -762,13 +762,16 @@ int bt_ctf_event_populate_event_header(struct bt_ctf_event *event) timestamp_field_type); bt_put(timestamp_field_type); if (mapped_clock) { - int64_t timestamp; + int64_t timestamp = 0; + // FIXME - Clock refactoring + /* ret = bt_ctf_clock_get_time(mapped_clock, ×tamp); bt_put(mapped_clock); if (ret) { goto end; } + */ ret = set_integer_field_value(timestamp_field, timestamp); diff --git a/include/babeltrace/ctf-ir/clock-internal.h b/include/babeltrace/ctf-ir/clock-internal.h index ac13a8e3..0ed43ae9 100644 --- a/include/babeltrace/ctf-ir/clock-internal.h +++ b/include/babeltrace/ctf-ir/clock-internal.h @@ -63,6 +63,12 @@ struct bt_ctf_clock { int frozen; }; +struct bt_ctf_clock_value { + struct bt_object base; + struct bt_ctf_clock *clock_class; + uint64_t value; +}; + BT_HIDDEN void bt_ctf_clock_freeze(struct bt_ctf_clock *clock); diff --git a/include/babeltrace/ctf-ir/clock.h b/include/babeltrace/ctf-ir/clock.h index 03f335fb..2bd1924b 100644 --- a/include/babeltrace/ctf-ir/clock.h +++ b/include/babeltrace/ctf-ir/clock.h @@ -37,6 +37,7 @@ extern "C" { #endif struct bt_ctf_clock; +struct bt_ctf_clock_value; /* * bt_ctf_clock_create: create a clock. @@ -249,8 +250,14 @@ extern const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock); extern int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid); -extern int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock *clock, - uint64_t value); +extern struct bt_ctf_clock_value *bt_ctf_clock_value_create( + struct bt_ctf_clock *clock, uint64_t value); + +extern int bt_ctf_clock_value_get_value( + struct bt_ctf_clock_value *clock_value, uint64_t *raw_value); + +extern int bt_ctf_clock_value_get_value_ns_from_epoch( + struct bt_ctf_clock_value, int64_t *value_ns); #ifdef __cplusplus } diff --git a/include/babeltrace/ctf-writer/clock.h b/include/babeltrace/ctf-writer/clock.h index 1bacc34e..7ee14c65 100644 --- a/include/babeltrace/ctf-writer/clock.h +++ b/include/babeltrace/ctf-writer/clock.h @@ -36,8 +36,6 @@ extern "C" { #endif -extern int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time); - /* * bt_ctf_clock_set_time: set a clock's current time value. * @@ -49,11 +47,6 @@ extern int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time); 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_ir_ref.c b/tests/lib/test_ctf_ir_ref.c index 5d0816b1..809af51c 100644 --- a/tests/lib/test_ctf_ir_ref.c +++ b/tests/lib/test_ctf_ir_ref.c @@ -519,9 +519,6 @@ static void create_user_full(struct user *user) assert(clock); ret = bt_ctf_stream_class_set_clock(user->sc, clock); assert(!ret); - ret = bt_ctf_clock_set_value(clock, 23); - assert(!ret); - BT_PUT(clock); user->stream = bt_ctf_writer_create_stream(user->writer, user->sc); assert(user->stream); user->ec = bt_ctf_event_class_create("ec"); diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 38bd3b71..c72e1d3a 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -59,7 +59,7 @@ #define DEFAULT_CLOCK_TIME 0 #define DEFAULT_CLOCK_VALUE 0 -#define NR_TESTS 620 +#define NR_TESTS 604 static int64_t current_time = 42; @@ -2789,24 +2789,6 @@ void test_create_writer_vs_non_writer_mode(void) ok(!bt_ctf_stream_append_event(writer_stream, event), "bt_ctf_stream_append_event() succeeds with a writer stream"); - /* - * Verify that it's possible to get and set the value of a - * writer mode clock. - */ - ok (!bt_ctf_clock_set_value(writer_clock, 1000), - "bt_ctf_clock_set_value() succeeds with a writer mode clock"); - ok (bt_ctf_clock_get_value(writer_clock) == 1000, - "bt_ctf_clock_get_value() succeeds with a writer mode clock"); - - /* - * Verify that it's impossible to get and set the value of a - * non-writer mode clock. - */ - ok (bt_ctf_clock_set_value(non_writer_clock, 1000), - "bt_ctf_clock_set_value() fails with a non-writer mode clock"); - ok (bt_ctf_clock_get_value(non_writer_clock) == -1ULL, - "bt_ctf_clock_get_value() fails with a non-writer mode clock"); - /* * It should be possible to create a packet from a non-writer * stream, but not from a writer stream. @@ -2912,13 +2894,8 @@ void test_clock_utils(void) assert(!ret); ret = bt_ctf_clock_set_frequency(clock, 1000000000); assert(!ret); - ok(bt_ctf_clock_ns_from_value(clock, 4321) == 1234000005321ULL, - "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency"); ret = bt_ctf_clock_set_frequency(clock, 1534); assert(!ret); - ok(bt_ctf_clock_ns_from_value(clock, 4321) == - (uint64_t) 1237468709256.845, - "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency"); BT_PUT(clock); } @@ -2935,8 +2912,7 @@ int main(int argc, char **argv) const int64_t offset_s = 1351530929945824323; const int64_t offset = 1234567; int64_t get_offset_s, - get_offset, - get_time; + get_offset; const uint64_t precision = 10; const int is_absolute = 0xFF; char *metadata_string; @@ -3212,22 +3188,8 @@ int main(int argc, char **argv) ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute, "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set"); - ok(bt_ctf_clock_get_time(clock, &get_time) == 0, - "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 - 1 && get_time <= current_time + 1, - "bt_ctf_clock_get_time returns the correct time once it is set"); ok(!bt_ctf_clock_get_name(NULL), "bt_ctf_clock_get_name correctly handles NULL"); @@ -3247,10 +3209,6 @@ int main(int argc, char **argv) "bt_ctf_clock_get_offset correctly handles NULL output"); ok(bt_ctf_clock_get_is_absolute(NULL) < 0, "bt_ctf_clock_get_is_absolute correctly handles NULL"); - ok(bt_ctf_clock_get_time(NULL, &get_time) < 0, - "bt_ctf_clock_get_time correctly handles NULL clock"); - ok(bt_ctf_clock_get_time(clock, NULL) < 0, - "bt_ctf_clock_get_time correctly handles NULL output"); ok(bt_ctf_clock_set_description(NULL, NULL) < 0, "bt_ctf_clock_set_description correctly handles NULL clock"); -- 2.34.1