ir: add bt_ctf_clock_ns_from_value() utility
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 19 Feb 2016 02:34:12 +0000 (21:34 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 22 Feb 2016 19:57:45 +0000 (14:57 -0500)
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.h

index 2e765430f66d8ad590e8dfbff01777fc62a3963f..253b033da398f50ebada3c851c08ef5a2e369959 100644 (file)
@@ -325,6 +325,19 @@ end:
        return ret;
 }
 
+uint64_t ns_from_value(uint64_t frequency, uint64_t value)
+{
+       uint64_t ns;
+
+       if (frequency == 1000000000) {
+               ns = value;
+       } else {
+               ns = (uint64_t) ((1e9 * (double) value) / (double) frequency);
+       }
+
+       return ns;
+}
+
 int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
 {
        int ret = 0;
@@ -343,13 +356,7 @@ int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
                goto end;
        }
 
-       /* 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);
-       }
+       *time = (int64_t) ns_from_value(clock->frequency, clock->value);
 
 end:
        return ret;
@@ -512,3 +519,24 @@ 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)
+{
+       int64_t ns = -1ULL;
+
+       if (!clock) {
+               goto end;
+       }
+
+       /* Initialize nanosecond timestamp to clock's offset in seconds */
+       ns = clock->offset_s * 1000000000;
+
+       /* Add offset in cycles, converted to nanoseconds */
+       ns += ns_from_value(clock->frequency, clock->offset);
+
+       /* Add given value, converter to nanoseconds */
+       ns += ns_from_value(clock->frequency, value);
+
+end:
+       return ns;
+}
index ec08810b6851eee8ed88f16f2da53fbada020a41..e35882e05eb7155e879b1b4cabd95023adbaf55a 100644 (file)
@@ -237,6 +237,9 @@ 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);
+
 #ifdef __cplusplus
 }
 #endif
This page took 0.026707 seconds and 4 git commands to generate.