X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fctf-writer%2Fwriter.c;h=e0215d337bcf3494458575b5f00abca4212ac7bb;hb=3fadfbc0c91f82c46bd36e6e0657ea93570c9db1;hp=c005993be3ffea9937f05f32d3880ea1fe6fc111;hpb=c55a9f585da53ea54ac458155fac3cdec1779d47;p=babeltrace.git diff --git a/lib/ctf-writer/writer.c b/lib/ctf-writer/writer.c index c005993b..e0215d33 100644 --- a/lib/ctf-writer/writer.c +++ b/lib/ctf-writer/writer.c @@ -26,34 +26,37 @@ * SOFTWARE. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#define BT_LOG_TAG "CTF-WRITER" +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include -#include #include -#include -#include static -void bt_ctf_writer_destroy(struct bt_object *obj); +void bt_ctf_writer_destroy(struct bt_ctf_object *obj); static int init_trace_packet_header(struct bt_ctf_trace *trace) { int ret = 0; - struct bt_ctf_field *magic = NULL, *uuid_array = NULL; struct bt_ctf_field_type *_uint32_t = get_field_type(FIELD_TYPE_ALIAS_UINT32_T); struct bt_ctf_field_type *_uint8_t = @@ -86,18 +89,16 @@ int init_trace_packet_header(struct bt_ctf_trace *trace) goto end; } - ret = bt_ctf_trace_set_packet_header_type(trace, + ret = bt_ctf_trace_set_packet_header_field_type(trace, trace_packet_header_type); if (ret) { goto end; } end: - bt_put(uuid_array_type); - bt_put(_uint32_t); - bt_put(_uint8_t); - bt_put(magic); - bt_put(uuid_array); - bt_put(trace_packet_header_type); + bt_ctf_object_put_ref(uuid_array_type); + bt_ctf_object_put_ref(_uint32_t); + bt_ctf_object_put_ref(_uint8_t); + bt_ctf_object_put_ref(trace_packet_header_type); return ret; } @@ -106,6 +107,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) int ret; struct bt_ctf_writer *writer = NULL; unsigned char uuid[16]; + char *metadata_path = NULL; if (!path) { goto error; @@ -116,7 +118,9 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) goto error; } - bt_object_init(writer, bt_ctf_writer_destroy); + metadata_path = g_build_filename(path, "metadata", NULL); + + bt_ctf_object_init_shared(&writer->base, bt_ctf_writer_destroy); writer->path = g_string_new(path); if (!writer->path) { goto error_destroy; @@ -133,19 +137,23 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) } /* Generate a UUID for this writer's trace */ - uuid_generate(uuid); + ret = bt_uuid_generate(uuid); + if (ret) { + BT_LOGE_STR("Cannot generate UUID for CTF writer's trace."); + goto error_destroy; + } + ret = bt_ctf_trace_set_uuid(writer->trace, uuid); if (ret) { goto error_destroy; } - writer->trace->is_created_by_writer = 1; - bt_object_set_parent(writer->trace, writer); - bt_put(writer->trace); + bt_ctf_object_set_parent(&writer->trace->common.base, &writer->base); + bt_ctf_object_put_ref(writer->trace); /* Default to little-endian */ ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE); - assert(ret == 0); + BT_ASSERT(ret == 0); /* Create trace directory if necessary and open a metadata file */ if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { @@ -153,26 +161,25 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) goto error_destroy; } - writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); - if (writer->trace_dir_fd < 0) { + writer->metadata_fd = open(metadata_path, + O_WRONLY | O_CREAT | O_TRUNC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (writer->metadata_fd < 0) { perror("open"); goto error_destroy; } - writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", - O_WRONLY | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); - + g_free(metadata_path); return writer; error_destroy: - unlinkat(writer->trace_dir_fd, "metadata", 0); - BT_PUT(writer); + BT_CTF_OBJECT_PUT_REF_AND_RESET(writer); error: + g_free(metadata_path); return writer; } -void bt_ctf_writer_destroy(struct bt_object *obj) +void bt_ctf_writer_destroy(struct bt_ctf_object *obj) { struct bt_ctf_writer *writer; @@ -182,19 +189,13 @@ void bt_ctf_writer_destroy(struct bt_object *obj) g_string_free(writer->path, TRUE); } - if (writer->trace_dir_fd > 0) { - if (close(writer->trace_dir_fd)) { - perror("close"); - } - } - if (writer->metadata_fd > 0) { if (close(writer->metadata_fd)) { perror("close"); } } - bt_object_release(writer->trace); + bt_ctf_object_try_spec_release(&writer->trace->common.base); g_free(writer); } @@ -207,7 +208,7 @@ struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) } trace = writer->trace; - bt_get(trace); + bt_ctf_object_get_ref(trace); end: return trace; } @@ -239,7 +240,7 @@ struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, stream_class_found = BT_TRUE; } - BT_PUT(existing_stream_class); + BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class); if (stream_class_found) { break; @@ -255,7 +256,7 @@ struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, } } - stream = bt_ctf_stream_create(stream_class, NULL); + stream = bt_ctf_stream_create_with_id(stream_class, NULL, -1ULL); if (!stream) { goto error; } @@ -263,7 +264,7 @@ struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, return stream; error: - BT_PUT(stream); + BT_CTF_OBJECT_PUT_REF_AND_RESET(stream); return stream; } @@ -372,7 +373,11 @@ int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, } if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { - byte_order = BT_CTF_MY_BYTE_ORDER; + if (BYTE_ORDER == LITTLE_ENDIAN) { + byte_order = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; + } else { + byte_order = BT_CTF_BYTE_ORDER_BIG_ENDIAN; + } } ret = bt_ctf_trace_set_native_byte_order(writer->trace, @@ -381,18 +386,70 @@ end: return ret; } -void bt_ctf_writer_get(struct bt_ctf_writer *writer) +BT_HIDDEN +void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) { - bt_get(writer); + writer->frozen = 1; } -void bt_ctf_writer_put(struct bt_ctf_writer *writer) +static +const unsigned int field_type_aliases_alignments[] = { + [FIELD_TYPE_ALIAS_UINT5_T] = 1, + [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, + [FIELD_TYPE_ALIAS_UINT27_T] = 1, + [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, +}; + +static +const unsigned int field_type_aliases_sizes[] = { + [FIELD_TYPE_ALIAS_UINT5_T] = 5, + [FIELD_TYPE_ALIAS_UINT8_T] = 8, + [FIELD_TYPE_ALIAS_UINT16_T] = 16, + [FIELD_TYPE_ALIAS_UINT27_T] = 27, + [FIELD_TYPE_ALIAS_UINT32_T] = 32, + [FIELD_TYPE_ALIAS_UINT64_T] = 64, +}; + +BT_HIDDEN +struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) { - bt_put(writer); + int ret; + unsigned int alignment, size; + struct bt_ctf_field_type *field_type = NULL; + + if (alias >= NR_FIELD_TYPE_ALIAS) { + goto end; + } + + alignment = field_type_aliases_alignments[alias]; + size = field_type_aliases_sizes[alias]; + field_type = bt_ctf_field_type_integer_create(size); + ret = bt_ctf_field_type_set_alignment(field_type, alignment); + if (ret) { + BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type); + } +end: + return field_type; } BT_HIDDEN -void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) +const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order) { - writer->frozen = 1; + const char *string; + + switch (byte_order) { + case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: + string = "le"; + break; + case BT_CTF_BYTE_ORDER_BIG_ENDIAN: + string = "be"; + break; + case BT_CTF_BYTE_ORDER_NATIVE: + string = "native"; + break; + default: + abort(); + } + + return string; }