/*
* clock.c
*
- * Babeltrace CTF IR - Clock
+ * Babeltrace CTF writer - Clock
*
* Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
* Copyright 2017 Philippe Proulx <pproulx@efficios.com>
* SOFTWARE.
*/
-#include <babeltrace/ctf-writer/clock-internal.h>
-#include <babeltrace/ctf-ir/clock-class.h>
-#include <babeltrace/ctf-ir/clock-class-internal.h>
-#include <babeltrace/ctf-ir/utils.h>
+#define BT_LOG_TAG "CTF-WRITER-CLOCK"
+#include <babeltrace/lib-logging-internal.h>
+
+#include <babeltrace/assert-internal.h>
#include <babeltrace/compat/uuid-internal.h>
-#include <babeltrace/ref.h>
-#include <babeltrace/object-internal.h>
#include <babeltrace/compiler-internal.h>
+#include <babeltrace/ctf-writer/clock-class-internal.h>
+#include <babeltrace/ctf-writer/clock-internal.h>
+#include <babeltrace/ctf-writer/utils.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/ctf-writer/object-internal.h>
+#include <babeltrace/ctf-writer/object.h>
#include <inttypes.h>
static
-void bt_ctf_clock_destroy(struct bt_object *obj);
+void bt_ctf_clock_destroy(struct bt_ctf_object *obj);
struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
{
struct bt_ctf_clock *clock = NULL;
unsigned char cc_uuid[BABELTRACE_UUID_LEN];
- if (!name) {
- goto error;
- }
-
+ BT_ASSERT_PRE_NON_NULL(name, "Name");
clock = g_new0(struct bt_ctf_clock, 1);
-
if (!clock) {
goto error;
}
- bt_object_init(clock, bt_ctf_clock_destroy);
+ bt_ctf_object_init_shared(&clock->base, bt_ctf_clock_destroy);
clock->value = 0;
- clock->clock_class = bt_ctf_clock_class_create(name);
+
+ /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */
+ clock->clock_class = (void *) bt_ctf_clock_class_create(name, 1000000000);
if (!clock->clock_class) {
goto error;
}
}
ret = bt_ctf_clock_class_set_uuid(clock->clock_class, cc_uuid);
- assert(ret == 0);
+ BT_ASSERT(ret == 0);
return clock;
error:
- BT_PUT(clock);
+ BT_CTF_OBJECT_PUT_REF_AND_RESET(clock);
return clock;
}
const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
{
- const char *name = NULL;
-
- if (clock) {
- name = bt_ctf_clock_class_get_name(clock->clock_class);
- }
-
- return name;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_name(clock->clock_class);
}
const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
{
- const char *description = NULL;
-
- if (clock) {
- description = bt_ctf_clock_class_get_description(
- clock->clock_class);
- }
-
- return description;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_description(clock->clock_class);
}
int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_description(clock->clock_class,
- desc);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_description(clock->clock_class,
+ desc);
}
uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
{
- uint64_t freq = -1ULL;
-
- if (clock) {
- freq = bt_ctf_clock_class_get_frequency(clock->clock_class);
- }
-
- return freq;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_frequency(clock->clock_class);
}
int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_frequency(clock->clock_class,
- freq);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_frequency(clock->clock_class,
+ freq);
}
uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
{
- uint64_t precision = -1ULL;
-
- if (clock) {
- precision = bt_ctf_clock_class_get_precision(
- clock->clock_class);
- }
-
- return precision;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_precision(clock->clock_class);
}
int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_precision(clock->clock_class,
- precision);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_precision(clock->clock_class,
+ precision);
}
int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_get_offset_s(clock->clock_class,
- offset_s);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_offset_s(clock->clock_class,
+ offset_s);
}
int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_offset_s(clock->clock_class,
- offset_s);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_offset_s(clock->clock_class,
+ offset_s);
}
int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_get_offset_cycles(clock->clock_class,
- offset);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_offset_cycles(clock->clock_class,
+ offset);
}
int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_offset_cycles(clock->clock_class,
- offset);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_offset_cycles(clock->clock_class,
+ offset);
}
int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
{
- int is_absolute = -1;
-
- if (clock) {
- is_absolute = bt_ctf_clock_class_is_absolute(
- clock->clock_class);
- }
-
- return is_absolute;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_is_absolute(clock->clock_class);
}
int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_is_absolute(clock->clock_class,
- is_absolute);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_is_absolute(clock->clock_class,
+ is_absolute);
}
const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
{
- const unsigned char *uuid = NULL;
-
- if (clock) {
- uuid = bt_ctf_clock_class_get_uuid(clock->clock_class);
- }
-
- return uuid;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_get_uuid(clock->clock_class);
}
int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
{
- int ret = -1;
-
- if (clock) {
- ret = bt_ctf_clock_class_set_uuid(clock->clock_class, uuid);
- }
-
- return ret;
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ return bt_ctf_clock_class_set_uuid(clock->clock_class, uuid);
}
int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
{
- int ret = 0;
int64_t value;
+ struct bt_ctf_clock_class *cc;
- if (!clock) {
- ret = -1;
- goto end;
- }
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ cc = clock->clock_class;
/* Common case where cycles are actually nanoseconds */
- if (clock->clock_class->frequency == 1000000000) {
+ if (cc->frequency == 1000000000) {
value = time;
} else {
value = (uint64_t) (((double) time *
- (double) clock->clock_class->frequency) / 1e9);
- }
-
- if (clock->value > value) {
- /* Timestamps must be strictly monotonic. */
- ret = -1;
- goto end;
+ (double) cc->frequency) / 1e9);
}
+ BT_ASSERT_PRE(clock->value <= value,
+ "CTF writer clock value must be updated monotonically: "
+ "prev-value=%" PRId64 ", new-value=%" PRId64,
+ clock->value, value);
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);
+ return 0;
}
BT_HIDDEN
int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
{
- int ret = 0;
-
- if (!clock || !value) {
- ret = -1;
- goto end;
- }
-
+ BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
+ BT_ASSERT_PRE_NON_NULL(value, "Value");
*value = clock->value;
-end:
- return ret;
+ return 0;
}
static
-void bt_ctf_clock_destroy(struct bt_object *obj)
+void bt_ctf_clock_destroy(struct bt_ctf_object *obj)
{
struct bt_ctf_clock *clock;
clock = container_of(obj, struct bt_ctf_clock, base);
- bt_put(clock->clock_class);
+ bt_ctf_object_put_ref(clock->clock_class);
g_free(clock);
}
+
+BT_HIDDEN
+void bt_ctf_clock_class_serialize(struct bt_ctf_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_ctf_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_ctf_clock_class_get_name(clock_class),
+ context);
+ 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");
+}