ir: stream: add bt_ctf_stream_is_writer()
[babeltrace.git] / formats / ctf / ir / clock.c
index 2860e01039aba31b44c0b801cb92994e6e9ac018..e24b864b04e200d02f02f4cb96d4aaad5d7d3333 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * clock.c
  *
- * Babeltrace CTF Writer
+ * Babeltrace CTF IR - Clock
  *
- * Copyright 2013 EfficiOS Inc.
+ * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/clock.h>
 #include <babeltrace/ctf-ir/clock-internal.h>
+#include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ref.h>
 #include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/object-internal.h>
 #include <babeltrace/compiler.h>
 #include <inttypes.h>
 
 static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_clock_destroy(struct bt_object *obj);
 
-struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
+BT_HIDDEN
+bool bt_ctf_clock_is_valid(struct bt_ctf_clock *clock)
 {
-       struct bt_ctf_clock *clock = NULL;
+       return clock && clock->name;
+}
 
-       if (validate_identifier(name)) {
-               goto error;
+int bt_ctf_clock_set_name(struct bt_ctf_clock *clock,
+               const char *name)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
        }
 
-       clock = g_new0(struct bt_ctf_clock, 1);
-       if (!clock) {
-               goto error;
+       if (bt_ctf_validate_identifier(name)) {
+               ret = -1;
+               goto end;
        }
 
-       clock->name = g_string_new(name);
-       if (!clock->name) {
-               goto error_destroy;
+       if (clock->name) {
+               g_string_assign(clock->name, name);
+       } else {
+               clock->name = g_string_new(name);
+               if (!clock->name) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+end:
+       return ret;
+}
+
+struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
+{
+       int ret;
+       struct bt_ctf_clock *clock = g_new0(struct bt_ctf_clock, 1);
+
+       if (!clock) {
+               goto error;
        }
 
        clock->precision = 1;
        clock->frequency = 1000000000;
-       uuid_generate(clock->uuid);
-       bt_ctf_ref_init(&clock->ref_count);
+       bt_object_init(clock, bt_ctf_clock_destroy);
+
+       if (name) {
+               ret = bt_ctf_clock_set_name(clock, name);
+               if (ret) {
+                       goto error;
+               }
+       }
+
+       ret = bt_uuid_generate(clock->uuid);
+       if (ret) {
+               goto error;
+       }
+
+       /*
+        * For backward compatibility reasons, a fresh clock can have
+        * a value because it could be added to a trace created by a
+        * CTF writer. As soon as this clock is added to a non-writer
+        * trace, then its value/time functions will be disabled.
+        */
+       clock->has_value = 1;
+       clock->uuid_set = 1;
        return clock;
-error_destroy:
-       bt_ctf_clock_destroy(&clock->ref_count);
 error:
-       return NULL;
+       BT_PUT(clock);
+       return clock;
 }
 
 const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
@@ -164,20 +211,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !offset_s) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->offset_s;
+       *offset_s = clock->offset_s;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
+int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
 {
        int ret = 0;
 
@@ -191,20 +239,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !offset) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->offset;
+       *offset = clock->offset;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
+int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
 {
        int ret = 0;
 
@@ -245,50 +294,96 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
+const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
 {
-       uint64_t ret = -1ULL;
+       const unsigned char *ret;
 
-       if (!clock) {
+       if (!clock || !clock->uuid_set) {
+               ret = NULL;
                goto end;
        }
 
-       ret = clock->time;
+       ret = clock->uuid;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
+int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
 {
        int ret = 0;
 
-       /* Timestamps are strictly monotonic */
-       if (!clock || time < clock->time) {
+       if (!clock || !uuid || clock->frozen) {
                ret = -1;
                goto end;
        }
 
-       clock->time = time;
+       memcpy(clock->uuid, uuid, sizeof(uuid_t));
+       clock->uuid_set = 1;
 end:
        return ret;
 }
 
-void bt_ctf_clock_get(struct bt_ctf_clock *clock)
+uint64_t ns_from_value(uint64_t frequency, uint64_t value)
 {
-       if (!clock) {
-               return;
+       uint64_t ns;
+
+       if (frequency == 1000000000) {
+               ns = value;
+       } else {
+               ns = (uint64_t) ((1e9 * (double) value) / (double) frequency);
        }
 
-       bt_ctf_ref_get(&clock->ref_count);
+       return ns;
 }
 
-void bt_ctf_clock_put(struct bt_ctf_clock *clock)
+int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
 {
+       int ret = 0;
+       int64_t value;
+
+       /* Timestamps are strictly monotonic */
        if (!clock) {
-               return;
+               ret = -1;
+               goto end;
        }
 
-       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
+
+       if (!clock->has_value) {
+               /*
+                * Clock belongs to a non-writer mode trace and thus
+                * this function is disabled.
+                */
+               ret = -1;
+               goto end;
+       }
+
+       /* Common case where cycles are actually nanoseconds */
+       if (clock->frequency == 1000000000) {
+               value = time;
+       } else {
+               value = (uint64_t) (((double) time *
+                       (double) clock->frequency) / 1e9);
+       }
+
+       if (clock->value > value) {
+               /* Timestamps must be strictly monotonic. */
+               ret = -1;
+               goto end;
+       }
+
+       clock->value = value;
+end:
+       return ret;
+}
+
+void bt_ctf_clock_get(struct bt_ctf_clock *clock)
+{
+       bt_get(clock);
+}
+
+void bt_ctf_clock_put(struct bt_ctf_clock *clock)
+{
+       bt_put(clock);
 }
 
 BT_HIDDEN
@@ -321,7 +416,7 @@ void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
                uuid[4], uuid[5], uuid[6], uuid[7],
                uuid[8], uuid[9], uuid[10], uuid[11],
                uuid[12], uuid[13], uuid[14], uuid[15]);
-       if (clock->description->len) {
+       if (clock->description) {
                g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
                        clock->description->str);
        }
@@ -339,16 +434,27 @@ void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
        g_string_append(context->string, "};\n\n");
 }
 
-static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
+BT_HIDDEN
+int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
 {
-       struct bt_ctf_clock *clock;
+       int ret = 0;
 
-       if (!ref) {
-               return;
+       if (!clock || !value) {
+               ret = -1;
+               goto end;
        }
 
-       clock = container_of(ref, struct bt_ctf_clock, ref_count);
+       *value = clock->value;
+end:
+       return ret;
+}
+
+static
+void bt_ctf_clock_destroy(struct bt_object *obj)
+{
+       struct bt_ctf_clock *clock;
+
+       clock = container_of(obj, struct bt_ctf_clock, base);
        if (clock->name) {
                g_string_free(clock->name, TRUE);
        }
@@ -359,3 +465,79 @@ void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
 
        g_free(clock);
 }
+
+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)
+{
+       struct bt_ctf_clock_value *ret = NULL;
+
+       if (!clock) {
+               goto end;
+       }
+
+       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;
+}
+
+int bt_ctf_clock_value_get_value(
+               struct bt_ctf_clock_value *clock_value, uint64_t *raw_value)
+{
+       int ret = 0;
+
+       if (!clock_value || !raw_value) {
+               ret = -1;
+               goto end;
+       }
+
+       *raw_value = clock_value->value;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_value_get_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;
+}
This page took 0.029069 seconds and 4 git commands to generate.