X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fctf-ir%2Fclock-class.c;h=e4d5341925c0fa8e9b0f7dc822f4049affdfe053;hb=7b33a0e0d8f23d90285ea7c7820a725bcbd96c6b;hp=37635ca9763f309ee9d4fca342f73ad01fd41cfe;hpb=3d9990ac8bcbb870300869ed217b80151b52bf4e;p=babeltrace.git diff --git a/lib/ctf-ir/clock-class.c b/lib/ctf-ir/clock-class.c index 37635ca9..e4d53419 100644 --- a/lib/ctf-ir/clock-class.c +++ b/lib/ctf-ir/clock-class.c @@ -26,472 +26,304 @@ * SOFTWARE. */ +#define BT_LOG_TAG "CLOCK-CLASS" +#include + +#include +#include #include -#include +#include +#include #include -#include #include +#include +#include #include +#include +#include -static -void bt_ctf_clock_class_destroy(struct bt_object *obj); +#define BT_ASSERT_PRE_CLOCK_CLASS_HOT(_cc) \ + BT_ASSERT_PRE_HOT((_cc), "Clock class", ": %!+K", (_cc)) -BT_HIDDEN -bool bt_ctf_clock_class_is_valid(struct bt_ctf_clock_class *clock_class) -{ - return clock_class && clock_class->name; -} - -int bt_ctf_clock_class_set_name(struct bt_ctf_clock_class *clock_class, - const char *name) +static +void destroy_clock_class(struct bt_object *obj) { - int ret = 0; + struct bt_clock_class *clock_class = (void *) obj; - if (!clock_class || clock_class->frozen) { - ret = -1; - goto end; - } + BT_LIB_LOGD("Destroying clock class: %!+K", clock_class); - if (bt_ctf_validate_identifier(name)) { - ret = -1; - goto end; + if (clock_class->name.str) { + g_string_free(clock_class->name.str, TRUE); } - if (clock_class->name) { - g_string_assign(clock_class->name, name); - } else { - clock_class->name = g_string_new(name); - if (!clock_class->name) { - ret = -1; - goto end; - } + if (clock_class->description.str) { + g_string_free(clock_class->description.str, TRUE); } -end: - return ret; + bt_object_pool_finalize(&clock_class->cv_pool); + g_free(clock_class); } -struct bt_ctf_clock_class *bt_ctf_clock_class_create(const char *name) +static +void free_clock_value(struct bt_clock_value *clock_value, + struct bt_clock_class *clock_class) { - int ret; - struct bt_ctf_clock_class *clock_class = - g_new0(struct bt_ctf_clock_class, 1); - - if (!clock_class) { - goto error; - } - - clock_class->precision = 1; - clock_class->frequency = 1000000000; - bt_object_init(clock_class, bt_ctf_clock_class_destroy); - - if (name) { - ret = bt_ctf_clock_class_set_name(clock_class, name); - if (ret) { - goto error; - } - } - - ret = bt_uuid_generate(clock_class->uuid); - if (ret) { - goto error; - } - - clock_class->uuid_set = 1; - return clock_class; -error: - BT_PUT(clock_class); - return clock_class; + bt_clock_value_destroy(clock_value); } -const char *bt_ctf_clock_class_get_name(struct bt_ctf_clock_class *clock_class) +static inline +void set_base_offset(struct bt_clock_class *clock_class) { - const char *ret = NULL; - - if (!clock_class) { + uint64_t offset_cycles_ns; + + /* Initialize nanosecond timestamp to clock's offset in seconds */ + if (clock_class->offset_seconds <= (INT64_MIN / INT64_C(1000000000) - 1) || + clock_class->offset_seconds >= (INT64_MAX / INT64_C(1000000000)) - 1) { + /* + * Overflow: offset in seconds converted to nanoseconds + * is outside the int64_t range. We also subtract 1 here + * to leave "space" for the offset in cycles converted + * to nanoseconds (which is always less than 1 second by + * contract). + */ + clock_class->base_offset.overflows = true; goto end; } - if (clock_class->name) { - ret = clock_class->name->str; - } + /* Offset (seconds) to nanoseconds */ + clock_class->base_offset.value_ns = clock_class->offset_seconds * + INT64_C(1000000000); -end: - return ret; -} - -const char *bt_ctf_clock_class_get_description( - struct bt_ctf_clock_class *clock_class) -{ - const char *ret = NULL; - - if (!clock_class) { - goto end; - } + /* Add offset in cycles */ + BT_ASSERT(clock_class->offset_cycles < clock_class->frequency); + offset_cycles_ns = bt_util_ns_from_value(clock_class->frequency, + clock_class->offset_cycles); + BT_ASSERT(offset_cycles_ns < 1000000000); + clock_class->base_offset.value_ns += (int64_t) offset_cycles_ns; + clock_class->base_offset.overflows = false; - if (clock_class->description) { - ret = clock_class->description->str; - } end: - return ret; + return; } -int bt_ctf_clock_class_set_description(struct bt_ctf_clock_class *clock_class, - const char *desc) +struct bt_clock_class *bt_clock_class_create(void) { - int ret = 0; - - if (!clock_class || !desc || clock_class->frozen) { - ret = -1; - goto end; - } - - clock_class->description = g_string_new(desc); - ret = clock_class->description ? 0 : -1; -end: - return ret; -} + int ret; + struct bt_clock_class *clock_class = NULL; -uint64_t bt_ctf_clock_class_get_frequency( - struct bt_ctf_clock_class *clock_class) -{ - uint64_t ret = -1ULL; + BT_LOGD_STR("Creating default clock class object"); + clock_class = g_new0(struct bt_clock_class, 1); if (!clock_class) { - goto end; + BT_LOGE_STR("Failed to allocate one clock class."); + goto error; } - ret = clock_class->frequency; -end: - return ret; -} - -int bt_ctf_clock_class_set_frequency(struct bt_ctf_clock_class *clock_class, - uint64_t freq) -{ - int ret = 0; - - if (!clock_class || clock_class->frozen) { - ret = -1; - goto end; + bt_object_init_shared(&clock_class->base, destroy_clock_class); + clock_class->name.str = g_string_new(NULL); + if (!clock_class->name.str) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; } - clock_class->frequency = freq; -end: - return ret; -} - -uint64_t bt_ctf_clock_class_get_precision(struct bt_ctf_clock_class *clock_class) -{ - uint64_t ret = -1ULL; - - if (!clock_class) { - goto end; + clock_class->description.str = g_string_new(NULL); + if (!clock_class->description.str) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; } - ret = clock_class->precision; -end: - return ret; -} - -int bt_ctf_clock_class_set_precision(struct bt_ctf_clock_class *clock_class, - uint64_t precision) -{ - int ret = 0; - - if (!clock_class || clock_class->frozen) { - ret = -1; - goto end; + clock_class->frequency = UINT64_C(1000000000); + clock_class->is_absolute = BT_TRUE; + set_base_offset(clock_class); + ret = bt_object_pool_initialize(&clock_class->cv_pool, + (bt_object_pool_new_object_func) bt_clock_value_new, + (bt_object_pool_destroy_object_func) + free_clock_value, + clock_class); + if (ret) { + BT_LOGE("Failed to initialize clock value pool: ret=%d", + ret); + goto error; } - clock_class->precision = precision; -end: - return ret; -} + BT_LIB_LOGD("Created clock class object: %!+K", clock_class); + goto end; -int bt_ctf_clock_class_get_offset_s(struct bt_ctf_clock_class *clock_class, - int64_t *offset_s) -{ - int ret = 0; - - if (!clock_class || !offset_s) { - ret = -1; - goto end; - } +error: + BT_PUT(clock_class); - *offset_s = clock_class->offset_s; end: - return ret; + return clock_class; } -int bt_ctf_clock_class_set_offset_s(struct bt_ctf_clock_class *clock_class, - int64_t offset_s) +const char *bt_clock_class_get_name( + struct bt_clock_class *clock_class) { - int ret = 0; - - if (!clock_class || clock_class->frozen) { - ret = -1; - goto end; - } - - clock_class->offset_s = offset_s; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + return clock_class->name.value; } -int bt_ctf_clock_class_get_offset_cycles(struct bt_ctf_clock_class *clock_class, - int64_t *offset) +int bt_clock_class_set_name(struct bt_clock_class *clock_class, + const char *name) { - int ret = 0; - - if (!clock_class || !offset) { - ret = -1; - goto end; - } - - *offset = clock_class->offset; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_NON_NULL(name, "Name"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + g_string_assign(clock_class->name.str, name); + clock_class->name.value = clock_class->name.str->str; + BT_LIB_LOGV("Set clock class's name: %!+K", clock_class); + return 0; } -int bt_ctf_clock_class_set_offset_cycles(struct bt_ctf_clock_class *clock_class, - int64_t offset) +const char *bt_clock_class_get_description(struct bt_clock_class *clock_class) { - int ret = 0; - - if (!clock_class || clock_class->frozen) { - ret = -1; - goto end; - } - - clock_class->offset = offset; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + return clock_class->description.value; } -int bt_ctf_clock_class_get_is_absolute(struct bt_ctf_clock_class *clock_class) +int bt_clock_class_set_description(struct bt_clock_class *clock_class, + const char *descr) { - int ret = -1; - - if (!clock_class) { - goto end; - } - - ret = clock_class->absolute; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_NON_NULL(descr, "Description"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + g_string_assign(clock_class->description.str, descr); + clock_class->description.value = clock_class->description.str->str; + BT_LIB_LOGV("Set clock class's description: %!+K", + clock_class); + return 0; } -int bt_ctf_clock_class_set_is_absolute(struct bt_ctf_clock_class *clock_class, - int is_absolute) +uint64_t bt_clock_class_get_frequency(struct bt_clock_class *clock_class) { - int ret = 0; - - if (!clock_class || clock_class->frozen) { - ret = -1; - goto end; - } - - clock_class->absolute = !!is_absolute; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + return clock_class->frequency; } -const unsigned char *bt_ctf_clock_class_get_uuid( - struct bt_ctf_clock_class *clock_class) +int bt_clock_class_set_frequency(struct bt_clock_class *clock_class, + uint64_t frequency) { - const unsigned char *ret; - - if (!clock_class || !clock_class->uuid_set) { - ret = NULL; - goto end; - } - - ret = clock_class->uuid; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, + "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64, + clock_class, frequency); + BT_ASSERT_PRE(clock_class->offset_cycles < frequency, + "Offset (cycles) is greater than clock class's frequency: " + "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency); + clock_class->frequency = frequency; + set_base_offset(clock_class); + BT_LIB_LOGV("Set clock class's frequency: %!+K", clock_class); + return 0; } -int bt_ctf_clock_class_set_uuid(struct bt_ctf_clock_class *clock_class, - const unsigned char *uuid) +uint64_t bt_clock_class_get_precision(struct bt_clock_class *clock_class) { - int ret = 0; - - if (!clock_class || !uuid || clock_class->frozen) { - ret = -1; - goto end; - } - - memcpy(clock_class->uuid, uuid, sizeof(uuid_t)); - clock_class->uuid_set = 1; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + return clock_class->precision; } -static uint64_t ns_from_value(uint64_t frequency, uint64_t value) +int bt_clock_class_set_precision(struct bt_clock_class *clock_class, + uint64_t precision) { - uint64_t ns; - - if (frequency == 1000000000) { - ns = value; - } else { - ns = (uint64_t) ((1e9 * (double) value) / (double) frequency); - } - - return ns; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + BT_ASSERT_PRE(precision != UINT64_C(-1), + "Invalid precision: %![cc-]+K, new-precision=%" PRIu64, + clock_class, precision); + clock_class->precision = precision; + BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class); + return 0; } -BT_HIDDEN -void bt_ctf_clock_class_freeze(struct bt_ctf_clock_class *clock_class) +void bt_clock_class_get_offset(struct bt_clock_class *clock_class, + int64_t *seconds, uint64_t *cycles) { - if (!clock_class) { - return; - } - - clock_class->frozen = 1; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)"); + BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)"); + *seconds = clock_class->offset_seconds; + *cycles = clock_class->offset_cycles; } -BT_HIDDEN -void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class *clock_class, - struct metadata_context *context) +int bt_clock_class_set_offset(struct bt_clock_class *clock_class, + int64_t seconds, uint64_t cycles) { - unsigned char *uuid; - - if (!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); - 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_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + BT_ASSERT_PRE(cycles < clock_class->frequency, + "Offset (cycles) is greater than clock class's frequency: " + "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles); + clock_class->offset_seconds = seconds; + clock_class->offset_cycles = cycles; + set_base_offset(clock_class); + BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class); + return 0; } -static -void bt_ctf_clock_class_destroy(struct bt_object *obj) +bt_bool bt_clock_class_is_absolute(struct bt_clock_class *clock_class) { - struct bt_ctf_clock_class *clock_class; - - clock_class = container_of(obj, struct bt_ctf_clock_class, base); - if (clock_class->name) { - g_string_free(clock_class->name, TRUE); - } - if (clock_class->description) { - g_string_free(clock_class->description, TRUE); - } - - g_free(clock_class); + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + return (bool) clock_class->is_absolute; } -static -void bt_ctf_clock_value_destroy(struct bt_object *obj) +int bt_clock_class_set_is_absolute(struct bt_clock_class *clock_class, + bt_bool is_absolute) { - 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); + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + clock_class->is_absolute = (bool) is_absolute; + BT_LIB_LOGV("Set clock class's absolute property: %!+K", + clock_class); + return 0; } -struct bt_ctf_clock_value *bt_ctf_clock_value_create( - struct bt_ctf_clock_class *clock_class, uint64_t value) +bt_uuid bt_clock_class_get_uuid(struct bt_clock_class *clock_class) { - struct bt_ctf_clock_value *ret = NULL; - - if (!clock_class) { - 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_class); - ret->value = value; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + return clock_class->uuid.value; } -int bt_ctf_clock_value_get_value( - struct bt_ctf_clock_value *clock_value, uint64_t *raw_value) +int bt_clock_class_set_uuid(struct bt_clock_class *clock_class, + bt_uuid uuid) { - int ret = 0; - - if (!clock_value || !raw_value) { - ret = -1; - goto end; - } - - *raw_value = clock_value->value; -end: - return ret; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); + BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); + memcpy(clock_class->uuid.uuid, uuid, BABELTRACE_UUID_LEN); + clock_class->uuid.value = clock_class->uuid.uuid; + BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class); + return 0; } -int bt_ctf_clock_value_get_value_ns_from_epoch(struct bt_ctf_clock_value *value, - int64_t *ret_value_ns) +BT_HIDDEN +void _bt_clock_class_freeze(struct bt_clock_class *clock_class) { - int ret = 0; - int64_t ns; + BT_ASSERT(clock_class); - if (!value || !ret_value_ns) { - ret = -1; - goto end; + if (clock_class->frozen) { + return; } - /* 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; + BT_LIB_LOGD("Freezing clock class: %!+K", clock_class); + clock_class->frozen = 1; } -struct bt_ctf_clock_class *bt_ctf_clock_value_get_class( - struct bt_ctf_clock_value *clock_value) +int bt_clock_class_cycles_to_ns_from_origin(struct bt_clock_class *clock_class, + uint64_t cycles, int64_t *ns) { - struct bt_ctf_clock_class *clock_class = NULL; + int ret; - if (!clock_value) { - goto end; + BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); + BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); + ret = bt_util_ns_from_origin(clock_class, cycles, ns); + if (ret) { + BT_LIB_LOGW("Cannot convert cycles to nanoseconds " + "from origin for given clock class: " + "value overflows the signed 64-bit integer range: " + "%![cc-]+K, cycles=%" PRIu64, + clock_class, cycles); } - clock_class = bt_get(clock_value->clock_class); - -end: - return clock_class; + return ret; }