+int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
+ struct ctf_fs_trace *ctf_fs_trace)
+{
+ int ret = 0;
+ size_t i;
+
+ /* Create one output port for each stream file group */
+ for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
+ struct ctf_fs_ds_file_group *ds_file_group =
+ g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
+
+ ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace,
+ ds_file_group);
+ if (ret) {
+ BT_LOGE("Cannot create output port.");
+ goto end;
+ }
+ }
+
+end:
+ return ret;
+}
+
+uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace *ctf_fs_trace,
+ struct bt_ctf_field *packet_header_field)
+{
+ struct bt_ctf_field *stream_instance_id_field = NULL;
+ uint64_t stream_instance_id = -1ULL;
+ int ret;
+
+ if (!packet_header_field) {
+ goto end;
+ }
+
+ stream_instance_id_field = bt_ctf_field_structure_get_field_by_name(
+ packet_header_field, "stream_instance_id");
+ if (!stream_instance_id_field) {
+ goto end;
+ }
+
+ ret = bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field,
+ &stream_instance_id);
+ if (ret) {
+ stream_instance_id = -1ULL;
+ goto end;
+ }
+
+end:
+ bt_put(stream_instance_id_field);
+ return stream_instance_id;
+}
+
+struct bt_ctf_stream_class *stream_class_from_packet_header(
+ struct ctf_fs_trace *ctf_fs_trace,
+ struct bt_ctf_field *packet_header_field)
+{
+ struct bt_ctf_field *stream_id_field = NULL;
+ struct bt_ctf_stream_class *stream_class = NULL;
+ uint64_t stream_id = -1ULL;
+ int ret;
+
+ if (!packet_header_field) {
+ goto single_stream_class;
+ }
+
+ stream_id_field = bt_ctf_field_structure_get_field_by_name(
+ packet_header_field, "stream_id");
+ if (!stream_id_field) {
+ goto single_stream_class;
+ }
+
+ ret = bt_ctf_field_unsigned_integer_get_value(stream_id_field,
+ &stream_id);
+ if (ret) {
+ stream_id = -1ULL;
+ }
+
+ if (stream_id == -1ULL) {
+single_stream_class:
+ /* Single stream class */
+ if (bt_ctf_trace_get_stream_class_count(
+ ctf_fs_trace->metadata->trace) == 0) {
+ goto end;
+ }
+
+ stream_class = bt_ctf_trace_get_stream_class_by_index(
+ ctf_fs_trace->metadata->trace, 0);
+ } else {
+ stream_class = bt_ctf_trace_get_stream_class_by_id(
+ ctf_fs_trace->metadata->trace, stream_id);
+ }
+
+end:
+ bt_put(stream_id_field);
+ return stream_class;
+}
+
+uint64_t get_packet_context_timestamp_begin_ns(
+ struct ctf_fs_trace *ctf_fs_trace,
+ struct bt_ctf_field *packet_context_field)
+{
+ int ret;
+ struct bt_ctf_field *timestamp_begin_field = NULL;
+ struct bt_ctf_field_type *timestamp_begin_ft = NULL;
+ uint64_t timestamp_begin_raw_value = -1ULL;
+ uint64_t timestamp_begin_ns = -1ULL;
+ int64_t timestamp_begin_ns_signed;
+ struct bt_ctf_clock_class *timestamp_begin_clock_class = NULL;
+ struct bt_ctf_clock_value *clock_value = NULL;
+
+ if (!packet_context_field) {
+ goto end;
+ }
+
+ timestamp_begin_field = bt_ctf_field_structure_get_field_by_name(
+ packet_context_field, "timestamp_begin");
+ if (!timestamp_begin_field) {
+ goto end;
+ }
+
+ timestamp_begin_ft = bt_ctf_field_get_type(timestamp_begin_field);
+ assert(timestamp_begin_ft);
+ timestamp_begin_clock_class =
+ bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft);
+ if (!timestamp_begin_clock_class) {
+ goto end;
+ }
+
+ ret = bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field,
+ ×tamp_begin_raw_value);
+ if (ret) {
+ goto end;
+ }
+
+ clock_value = bt_ctf_clock_value_create(timestamp_begin_clock_class,
+ timestamp_begin_raw_value);
+ if (!clock_value) {
+ goto end;
+ }
+
+ ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value,
+ ×tamp_begin_ns_signed);
+ if (ret) {
+ goto end;
+ }
+
+ timestamp_begin_ns = (uint64_t) timestamp_begin_ns_signed;
+
+end:
+ bt_put(timestamp_begin_field);
+ bt_put(timestamp_begin_ft);
+ bt_put(timestamp_begin_clock_class);
+ bt_put(clock_value);
+ return timestamp_begin_ns;
+}
+
+static
+void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info)
+{
+ if (!ds_file_info) {
+ return;
+ }
+
+ if (ds_file_info->path) {
+ g_string_free(ds_file_info->path, TRUE);
+ }
+
+ ctf_fs_ds_index_destroy(ds_file_info->index);
+ g_free(ds_file_info);
+}
+
+static
+struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path,
+ uint64_t begin_ns, struct ctf_fs_ds_index *index)
+{
+ struct ctf_fs_ds_file_info *ds_file_info;
+
+ ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1);
+ if (!ds_file_info) {
+ goto end;
+ }
+
+ ds_file_info->path = g_string_new(path);
+ if (!ds_file_info->path) {
+ ctf_fs_ds_file_info_destroy(ds_file_info);
+ ds_file_info = NULL;
+ goto end;
+ }
+
+ ds_file_info->begin_ns = begin_ns;
+ ds_file_info->index = index;
+ index = NULL;
+
+end:
+ ctf_fs_ds_index_destroy(index);
+ return ds_file_info;
+}
+
+static
+void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
+{
+ if (!ds_file_group) {
+ return;
+ }
+
+ if (ds_file_group->ds_file_infos) {
+ g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
+ }
+
+ bt_put(ds_file_group->stream);
+ g_free(ds_file_group);
+}
+
+static
+struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(
+ struct ctf_fs_trace *ctf_fs_trace,
+ struct bt_ctf_stream_class *stream_class,
+ uint64_t stream_instance_id)
+{
+ struct ctf_fs_ds_file_group *ds_file_group;
+
+ assert(stream_class);
+ ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1);
+ if (!ds_file_group) {
+ goto error;
+ }
+
+ ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func(
+ (GDestroyNotify) ctf_fs_ds_file_info_destroy);
+ if (!ds_file_group->ds_file_infos) {
+ goto error;
+ }
+
+ if (stream_instance_id == -1ULL) {
+ ds_file_group->stream = bt_ctf_stream_create(
+ stream_class, NULL);
+ } else {
+ ds_file_group->stream = bt_ctf_stream_create_with_id(
+ stream_class, NULL, stream_instance_id);
+ }
+
+ if (!ds_file_group->stream) {
+ goto error;
+ }
+
+ ds_file_group->ctf_fs_trace = ctf_fs_trace;
+
+ goto end;
+
+error:
+ ctf_fs_ds_file_group_destroy(ds_file_group);
+ ds_file_group = NULL;
+
+end:
+ return ds_file_group;
+}
+
+static
+int ctf_fs_ds_file_group_add_ds_file_info(
+ struct ctf_fs_ds_file_group *ds_file_group,
+ const char *path, uint64_t begin_ns,
+ struct ctf_fs_ds_index *index)
+{
+ struct ctf_fs_ds_file_info *ds_file_info;
+ gint i = 0;
+ int ret = 0;
+
+ /* Onwership of index is transferred. */
+ ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index);
+ index = NULL;
+ if (!ds_file_info) {
+ goto error;
+ }
+
+ /* Find a spot to insert this one */
+ for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
+ struct ctf_fs_ds_file_info *other_ds_file_info =
+ g_ptr_array_index(ds_file_group->ds_file_infos, i);
+
+ if (begin_ns < other_ds_file_info->begin_ns) {
+ break;
+ }
+ }
+
+ if (i == ds_file_group->ds_file_infos->len) {
+ /* Append instead */
+ i = -1;
+ }
+
+ g_ptr_array_insert(ds_file_group->ds_file_infos, i, ds_file_info);
+ ds_file_info = NULL;
+ goto end;
+
+error:
+ ctf_fs_ds_file_info_destroy(ds_file_info);
+ ctf_fs_ds_index_destroy(index);
+ ret = -1;
+end:
+ return ret;
+}
+
+static
+int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
+ const char *path)
+{
+ struct bt_ctf_field *packet_header_field = NULL;
+ struct bt_ctf_field *packet_context_field = NULL;
+ struct bt_ctf_stream_class *stream_class = NULL;
+ uint64_t stream_instance_id = -1ULL;
+ uint64_t begin_ns = -1ULL;
+ struct ctf_fs_ds_file_group *ds_file_group = NULL;
+ bool add_group = false;
+ int ret;
+ size_t i;
+ struct ctf_fs_ds_file *ds_file;
+ struct ctf_fs_ds_index *index = NULL;
+
+ ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path);
+ if (!ds_file) {
+ goto error;
+ }
+
+ ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file,
+ &packet_header_field, &packet_context_field);
+ if (ret) {
+ BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
+ path);
+ goto error;
+ }
+
+ stream_instance_id = get_packet_header_stream_instance_id(ctf_fs_trace,
+ packet_header_field);
+ begin_ns = get_packet_context_timestamp_begin_ns(ctf_fs_trace,
+ packet_context_field);
+ stream_class = stream_class_from_packet_header(ctf_fs_trace,
+ packet_header_field);
+ if (!stream_class) {
+ goto error;
+ }
+
+ index = ctf_fs_ds_file_build_index(ds_file);
+ if (!index) {
+ BT_LOGW("Failed to index CTF stream file \'%s\'",
+ ds_file->file->path->str);
+ }
+
+ if (begin_ns == -1ULL) {
+ /*
+ * No beggining timestamp to sort the stream files
+ * within a stream file group, so consider that this
+ * file must be the only one within its group.
+ */
+ stream_instance_id = -1ULL;
+ }
+
+ if (stream_instance_id == -1ULL) {
+ /*
+ * No stream instance ID or no beginning timestamp:
+ * create a unique stream file group for this stream
+ * file because, even if there's a stream instance ID,
+ * there's no timestamp to order the file within its
+ * group.
+ */
+
+ ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
+ stream_class, stream_instance_id);
+ if (!ds_file_group) {
+ goto error;
+ }
+
+ ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group,
+ path, begin_ns, index);
+ /* Ownership of index is transferred. */
+ index = NULL;
+ if (ret) {
+ goto error;
+ }
+
+ add_group = true;
+ goto end;
+ }
+
+ assert(stream_instance_id != -1ULL);
+ assert(begin_ns != -1ULL);
+
+ /* Find an existing stream file group with this ID */
+ for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
+ int64_t id;
+ struct bt_ctf_stream_class *cand_stream_class;
+
+ ds_file_group = g_ptr_array_index(
+ ctf_fs_trace->ds_file_groups, i);
+ id = bt_ctf_stream_get_id(ds_file_group->stream);
+ cand_stream_class = bt_ctf_stream_get_class(
+ ds_file_group->stream);
+
+ assert(cand_stream_class);
+ bt_put(cand_stream_class);
+
+ if (cand_stream_class == stream_class &&
+ (uint64_t) id == stream_instance_id) {
+ break;
+ }
+
+ ds_file_group = NULL;
+ }
+
+ if (!ds_file_group) {
+ ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
+ stream_class, stream_instance_id);
+ if (!ds_file_group) {
+ goto error;
+ }
+
+ add_group = true;
+ }
+
+ ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group,
+ path, begin_ns, index);
+ index = NULL;
+ if (ret) {
+ goto error;
+ }
+
+ goto end;
+
+error:
+ ctf_fs_ds_file_group_destroy(ds_file_group);
+ ret = -1;
+
+end:
+ if (add_group && ds_file_group) {
+ g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group);
+ }
+ ctf_fs_ds_file_destroy(ds_file);
+ ctf_fs_ds_index_destroy(index);
+ bt_put(packet_header_field);
+ bt_put(packet_context_field);
+ bt_put(stream_class);
+ return ret;
+}
+
+static
+int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)