X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;ds=sidebyside;f=formats%2Fctf%2Fwriter%2Fwriter.c;h=854de432f36d1e830aa68bae4c819c28ae4c5e7d;hb=f3985ab106d89d8e764c1a8dd0c8bda09b755d10;hp=9adfcd381f22f09e5fdea1fbe41316ab6b74958b;hpb=de3dd40e6fcad56e227f5fc8a8290fbaa88b4e07;p=babeltrace.git diff --git a/formats/ctf/writer/writer.c b/formats/ctf/writer/writer.c index 9adfcd38..854de432 100644 --- a/formats/ctf/writer/writer.c +++ b/formats/ctf/writer/writer.c @@ -26,15 +26,15 @@ * SOFTWARE. */ -#include +#include #include -#include -#include +#include +#include #include #include #include -#include -#include +#include +#include #include #include #include @@ -45,10 +45,7 @@ #include static -void bt_ctf_writer_destroy(struct bt_ref *ref); -static -int create_stream_file(struct bt_ctf_writer *writer, - struct bt_ctf_stream *stream); +void bt_ctf_writer_destroy(struct bt_object *obj); struct bt_ctf_writer *bt_ctf_writer_create(const char *path) { @@ -63,7 +60,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) goto error; } - bt_ctf_base_init(writer, bt_ctf_writer_destroy); + bt_object_init(writer, bt_ctf_writer_destroy); writer->path = g_string_new(path); if (!writer->path) { goto error_destroy; @@ -74,6 +71,9 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) goto error_destroy; } + writer->trace->is_created_by_writer = 1; + bt_object_set_parent(writer->trace, writer); + bt_put(writer->trace); /* Create trace directory if necessary and open a metadata file */ if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { perror("g_mkdir_with_parents"); @@ -94,23 +94,16 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) error_destroy: unlinkat(writer->trace_dir_fd, "metadata", 0); - bt_ctf_writer_destroy(&writer->base.ref_count); - writer = NULL; + BT_PUT(writer); error: return writer; } -void bt_ctf_writer_destroy(struct bt_ref *ref) +void bt_ctf_writer_destroy(struct bt_object *obj) { struct bt_ctf_writer *writer; - struct bt_ctf_base *base; - - if (!ref) { - return; - } - base = container_of(ref, struct bt_ctf_base, ref_count); - writer = container_of(base, struct bt_ctf_writer, base); + writer = container_of(obj, struct bt_ctf_writer, base); bt_ctf_writer_flush_metadata(writer); if (writer->path) { g_string_free(writer->path, TRUE); @@ -128,7 +121,7 @@ void bt_ctf_writer_destroy(struct bt_ref *ref) } } - bt_ctf_trace_put(writer->trace); + bt_object_release(writer->trace); g_free(writer); } @@ -141,7 +134,7 @@ struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) } trace = writer->trace; - bt_ctf_trace_get(trace); + bt_get(trace); end: return trace; } @@ -149,29 +142,55 @@ end: struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class) { - int stream_fd; struct bt_ctf_stream *stream = NULL; + int stream_class_count; + bool stream_class_found = false; + int i; if (!writer || !stream_class) { goto error; } - stream = bt_ctf_trace_create_stream(writer->trace, stream_class); - if (!stream) { + /* Make sure the stream class is part of the writer's trace */ + stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace); + if (stream_class_count < 0) { goto error; } - stream_fd = create_stream_file(writer, stream); - if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) { + for (i = 0; i < stream_class_count; i++) { + struct bt_ctf_stream_class *existing_stream_class = + bt_ctf_trace_get_stream_class(writer->trace, i); + + if (existing_stream_class == stream_class) { + stream_class_found = true; + } + + BT_PUT(existing_stream_class); + + if (stream_class_found) { + break; + } + } + + if (!stream_class_found) { + int ret = bt_ctf_trace_add_stream_class(writer->trace, + stream_class); + + if (ret) { + goto error; + } + } + + stream = bt_ctf_stream_create(stream_class, NULL); + if (!stream) { goto error; } - writer->frozen = 1; return stream; error: - bt_ctf_stream_put(stream); - return NULL; + BT_PUT(stream); + return stream; } int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, @@ -190,6 +209,22 @@ end: return ret; } +int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, + const char *name, + int64_t value) +{ + int ret = -1; + + if (!writer || !name) { + goto end; + } + + ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name, + value); +end: + return ret; +} + int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock) { @@ -199,7 +234,7 @@ int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, goto end; } - ret = bt_ctf_trace_add_clock(writer->trace, clock); + ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class); end: return ret; } @@ -271,38 +306,16 @@ end: void bt_ctf_writer_get(struct bt_ctf_writer *writer) { - bt_ctf_get(writer); + bt_get(writer); } void bt_ctf_writer_put(struct bt_ctf_writer *writer) { - bt_ctf_put(writer); + bt_put(writer); } -static -int create_stream_file(struct bt_ctf_writer *writer, - struct bt_ctf_stream *stream) +BT_HIDDEN +void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) { - int fd; - GString *filename = g_string_new(stream->stream_class->name->str); - - if (stream->stream_class->name->len == 0) { - int64_t ret; - - ret = bt_ctf_stream_class_get_id(stream->stream_class); - if (ret < 0) { - fd = -1; - goto error; - } - - g_string_printf(filename, "stream_%" PRId64, ret); - } - - g_string_append_printf(filename, "_%" PRIu32, stream->id); - fd = openat(writer->trace_dir_fd, filename->str, - O_RDWR | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); -error: - g_string_free(filename, TRUE); - return fd; + writer->frozen = 1; }