ir: clock: use value in cycles instead of ns
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 16 Feb 2016 01:13:02 +0000 (20:13 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 19 Feb 2016 22:21:34 +0000 (17:21 -0500)
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 <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
formats/ctf/ir/clock.c
include/babeltrace/ctf-ir/clock-internal.h
include/babeltrace/ctf-ir/clock.h
tests/lib/test_ctf_writer.c

index bf2fbda5b2627b8cffb9584650ff1a81921b2afa..0bf7f3518cd5026450703001190699bd031be717 100644 (file)
@@ -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;
 }
index e13978110e3c799b566ec4c3cdb7b7ecdd6861fd..07e37f9895678f050e0743c711b398cabd2ae1b6 100644 (file)
@@ -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;
index e149d7fadb684f7ce0da904498ea62eb33cc9f0b..a9ba7134be0c45937d88c669bb5347546303031d 100644 (file)
@@ -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
index 576c4cf023320dafd08a1fcd383ffad6b3e8dbd3..8d8ffa915826313c5de2930009047e5770771254 100644 (file)
@@ -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,
This page took 0.029249 seconds and 4 git commands to generate.