Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / lib / ctf-ir / clock-class.c
index 7c28fee843f6271daf4b5c77124d5c8ea9d8ab6f..40654e990144c9dace17d393a47828f1be9517a2 100644 (file)
@@ -39,6 +39,7 @@
 #include <babeltrace/compat/string-internal.h>
 #include <inttypes.h>
 #include <babeltrace/object-internal.h>
+#include <babeltrace/assert-internal.h>
 
 static
 void bt_clock_class_destroy(struct bt_object *obj);
@@ -68,7 +69,7 @@ int bt_clock_class_set_name(struct bt_clock_class *clock_class,
        }
 
        if (!bt_identifier_is_valid(name)) {
-               BT_LOGE("Clock class's name is not a valid CTF identifier: "
+               BT_LOGW("Clock class's name is not a valid CTF identifier: "
                        "addr=%p, name=\"%s\"",
                        clock_class, name);
                ret = -1;
@@ -138,9 +139,7 @@ struct bt_clock_class *bt_clock_class_create(const char *name,
        if (name) {
                ret = bt_clock_class_set_name(clock_class, name);
                if (ret) {
-                       BT_LOGE("Cannot set clock class's name: "
-                               "addr=%p, name=\"%s\"",
-                               clock_class, name);
+                       /* bt_clock_class_set_name() logs errors */
                        goto error;
                }
        }
@@ -530,66 +529,13 @@ static uint64_t ns_from_value(uint64_t frequency, uint64_t value)
 BT_HIDDEN
 void bt_clock_class_freeze(struct bt_clock_class *clock_class)
 {
-       if (!clock_class) {
-               BT_LOGW_STR("Invalid parameter: clock class is NULL.");
-               return;
-       }
-
-       if (!clock_class->frozen) {
-               BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
-                       clock_class, bt_clock_class_get_name(clock_class));
-               clock_class->frozen = 1;
-       }
-}
-
-BT_HIDDEN
-void bt_clock_class_serialize(struct bt_clock_class *clock_class,
-               struct metadata_context *context)
-{
-       unsigned char *uuid;
-
-       BT_LOGD("Serializing clock class's metadata: clock-class-addr=%p, "
-               "name=\"%s\", metadata-context-addr=%p", clock_class,
-               bt_clock_class_get_name(clock_class), context);
-
-       if (!clock_class || !context) {
-               BT_LOGW("Invalid parameter: clock class or metadata context is NULL: "
-                       "clock-class-addr=%p, name=\"%s\", metadata-context-addr=%p",
-                       clock_class, bt_clock_class_get_name(clock_class),
-                       context);
+       if (!clock_class || clock_class->frozen) {
                return;
        }
 
-       uuid = clock_class->uuid;
-       g_string_append(context->string, "clock {\n");
-       g_string_append_printf(context->string, "\tname = %s;\n",
-               clock_class->name->str);
-
-       if (clock_class->uuid_set) {
-               g_string_append_printf(context->string,
-                       "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
-                       uuid[0], uuid[1], uuid[2], uuid[3],
-                       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_class->description) {
-               g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
-                       clock_class->description->str);
-       }
-
-       g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
-               clock_class->frequency);
-       g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
-               clock_class->precision);
-       g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
-               clock_class->offset_s);
-       g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
-               clock_class->offset);
-       g_string_append_printf(context->string, "\tabsolute = %s;\n",
-               clock_class->absolute ? "true" : "false");
-       g_string_append(context->string, "};\n\n");
+       BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
+               clock_class, bt_clock_class_get_name(clock_class));
+       clock_class->frozen = 1;
 }
 
 static
@@ -668,7 +614,7 @@ void set_ns_from_epoch(struct bt_clock_value *clock_value)
        }
 
        s_ns = (int64_t) u_ns;
-       assert(s_ns >= 0);
+       BT_ASSERT(s_ns >= 0);
 
        if (clock_class->offset < 0) {
                if (clock_value->ns_from_epoch >= 0) {
@@ -731,7 +677,7 @@ offset_ok:
        }
 
        s_ns = (int64_t) u_ns;
-       assert(s_ns >= 0);
+       BT_ASSERT(s_ns >= 0);
 
        /* Clock value (cycles) is always positive */
        if (clock_value->ns_from_epoch <= 0) {
@@ -859,3 +805,151 @@ struct bt_clock_class *bt_clock_value_get_class(
 end:
        return clock_class;
 }
+
+BT_HIDDEN
+int bt_clock_class_compare(struct bt_clock_class *clock_class_a,
+               struct bt_clock_class *clock_class_b)
+{
+       int ret = 1;
+       BT_ASSERT(clock_class_a);
+       BT_ASSERT(clock_class_b);
+
+       /* Name */
+       if (strcmp(clock_class_a->name->str, clock_class_b->name->str) != 0) {
+               BT_LOGV("Clock classes differ: different names: "
+                       "cc-a-name=\"%s\", cc-b-name=\"%s\"",
+                       clock_class_a->name->str,
+                       clock_class_b->name->str);
+               goto end;
+       }
+
+       /* Description */
+       if (clock_class_a->description) {
+               if (!clock_class_b->description) {
+                       BT_LOGV_STR("Clock classes differ: clock class A has a "
+                               "description, but clock class B does not.");
+                       goto end;
+               }
+
+               if (strcmp(clock_class_a->name->str, clock_class_b->name->str)
+                               != 0) {
+                       BT_LOGV("Clock classes differ: different descriptions: "
+                               "cc-a-descr=\"%s\", cc-b-descr=\"%s\"",
+                               clock_class_a->description->str,
+                               clock_class_b->description->str);
+                       goto end;
+               }
+       } else {
+               if (clock_class_b->description) {
+                       BT_LOGV_STR("Clock classes differ: clock class A has "
+                               "no description, but clock class B has one.");
+                       goto end;
+               }
+       }
+
+       /* Frequency */
+       if (clock_class_a->frequency != clock_class_b->frequency) {
+               BT_LOGV("Clock classes differ: different frequencies: "
+                       "cc-a-freq=%" PRIu64 ", cc-b-freq=%" PRIu64,
+                       clock_class_a->frequency,
+                       clock_class_b->frequency);
+               goto end;
+       }
+
+       /* Precision */
+       if (clock_class_a->precision != clock_class_b->precision) {
+               BT_LOGV("Clock classes differ: different precisions: "
+                       "cc-a-freq=%" PRIu64 ", cc-b-freq=%" PRIu64,
+                       clock_class_a->precision,
+                       clock_class_b->precision);
+               goto end;
+       }
+
+       /* Offset (seconds) */
+       if (clock_class_a->offset_s != clock_class_b->offset_s) {
+               BT_LOGV("Clock classes differ: different offsets (seconds): "
+                       "cc-a-offset-s=%" PRId64 ", cc-b-offset-s=%" PRId64,
+                       clock_class_a->offset_s,
+                       clock_class_b->offset_s);
+               goto end;
+       }
+
+       /* Offset (cycles) */
+       if (clock_class_a->offset != clock_class_b->offset) {
+               BT_LOGV("Clock classes differ: different offsets (cycles): "
+                       "cc-a-offset-s=%" PRId64 ", cc-b-offset-s=%" PRId64,
+                       clock_class_a->offset,
+                       clock_class_b->offset);
+               goto end;
+       }
+
+       /* UUIDs */
+       if (clock_class_a->uuid_set) {
+               if (!clock_class_b->uuid_set) {
+                       BT_LOGV_STR("Clock classes differ: clock class A has a "
+                               "UUID, but clock class B does not.");
+                       goto end;
+               }
+
+               if (memcmp(clock_class_a->uuid, clock_class_b->uuid,
+                               BABELTRACE_UUID_LEN) != 0) {
+                       BT_LOGV("Clock classes differ: different UUIDs: "
+                               "cc-a-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
+                               "cc-b-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
+                               (unsigned int) clock_class_a->uuid[0],
+                               (unsigned int) clock_class_a->uuid[1],
+                               (unsigned int) clock_class_a->uuid[2],
+                               (unsigned int) clock_class_a->uuid[3],
+                               (unsigned int) clock_class_a->uuid[4],
+                               (unsigned int) clock_class_a->uuid[5],
+                               (unsigned int) clock_class_a->uuid[6],
+                               (unsigned int) clock_class_a->uuid[7],
+                               (unsigned int) clock_class_a->uuid[8],
+                               (unsigned int) clock_class_a->uuid[9],
+                               (unsigned int) clock_class_a->uuid[10],
+                               (unsigned int) clock_class_a->uuid[11],
+                               (unsigned int) clock_class_a->uuid[12],
+                               (unsigned int) clock_class_a->uuid[13],
+                               (unsigned int) clock_class_a->uuid[14],
+                               (unsigned int) clock_class_a->uuid[15],
+                               (unsigned int) clock_class_b->uuid[0],
+                               (unsigned int) clock_class_b->uuid[1],
+                               (unsigned int) clock_class_b->uuid[2],
+                               (unsigned int) clock_class_b->uuid[3],
+                               (unsigned int) clock_class_b->uuid[4],
+                               (unsigned int) clock_class_b->uuid[5],
+                               (unsigned int) clock_class_b->uuid[6],
+                               (unsigned int) clock_class_b->uuid[7],
+                               (unsigned int) clock_class_b->uuid[8],
+                               (unsigned int) clock_class_b->uuid[9],
+                               (unsigned int) clock_class_b->uuid[10],
+                               (unsigned int) clock_class_b->uuid[11],
+                               (unsigned int) clock_class_b->uuid[12],
+                               (unsigned int) clock_class_b->uuid[13],
+                               (unsigned int) clock_class_b->uuid[14],
+                               (unsigned int) clock_class_b->uuid[15]);
+                       goto end;
+               }
+       } else {
+               if (clock_class_b->uuid_set) {
+                       BT_LOGV_STR("Clock classes differ: clock class A has "
+                               "no UUID, but clock class B has one.");
+                       goto end;
+               }
+       }
+
+       /* Absolute */
+       if (!!clock_class_a->absolute != !!clock_class_b->absolute) {
+               BT_LOGV("Clock classes differ: one is absolute, the other "
+                       "is not: cc-a-is-absolute=%d, cc-b-is-absolute=%d",
+                       !!clock_class_a->absolute,
+                       !!clock_class_b->absolute);
+               goto end;
+       }
+
+       /* Equal */
+       ret = 0;
+
+end:
+       return ret;
+}
This page took 0.036645 seconds and 4 git commands to generate.