4 * Babeltrace CTF file system Reader Component
6 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define BT_COMP_LOG_SELF_COMP self_comp
29 #define BT_LOG_OUTPUT_LEVEL log_level
30 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS"
31 #include "plugins/comp-logging.h"
33 #include "common/common.h"
34 #include <babeltrace2/babeltrace.h>
35 #include "compat/uuid.h"
37 #include "common/assert.h"
42 #include "data-stream-file.h"
44 #include "../common/metadata/decoder.h"
45 #include "../common/msg-iter/msg-iter.h"
49 int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data
*msg_iter_data
)
51 struct ctf_fs_ds_file_info
*ds_file_info
;
54 BT_ASSERT(msg_iter_data
->ds_file_info_index
<
55 msg_iter_data
->ds_file_group
->ds_file_infos
->len
);
56 ds_file_info
= g_ptr_array_index(
57 msg_iter_data
->ds_file_group
->ds_file_infos
,
58 msg_iter_data
->ds_file_info_index
);
60 ctf_fs_ds_file_destroy(msg_iter_data
->ds_file
);
61 msg_iter_data
->ds_file
= ctf_fs_ds_file_create(
62 msg_iter_data
->ds_file_group
->ctf_fs_trace
,
63 msg_iter_data
->pc_msg_iter
,
64 msg_iter_data
->msg_iter
,
65 msg_iter_data
->ds_file_group
->stream
,
66 ds_file_info
->path
->str
,
67 msg_iter_data
->log_level
);
68 if (!msg_iter_data
->ds_file
) {
76 void ctf_fs_msg_iter_data_destroy(
77 struct ctf_fs_msg_iter_data
*msg_iter_data
)
83 ctf_fs_ds_file_destroy(msg_iter_data
->ds_file
);
85 if (msg_iter_data
->msg_iter
) {
86 bt_msg_iter_destroy(msg_iter_data
->msg_iter
);
89 g_free(msg_iter_data
);
93 void set_msg_iter_emits_stream_beginning_end_messages(
94 struct ctf_fs_msg_iter_data
*msg_iter_data
)
96 bt_msg_iter_set_emit_stream_beginning_message(
97 msg_iter_data
->ds_file
->msg_iter
,
98 msg_iter_data
->ds_file_info_index
== 0);
99 bt_msg_iter_set_emit_stream_end_message(
100 msg_iter_data
->ds_file
->msg_iter
,
101 msg_iter_data
->ds_file_info_index
==
102 msg_iter_data
->ds_file_group
->ds_file_infos
->len
- 1);
106 bt_component_class_message_iterator_next_method_status
ctf_fs_iterator_next_one(
107 struct ctf_fs_msg_iter_data
*msg_iter_data
,
108 const bt_message
**out_msg
)
110 bt_component_class_message_iterator_next_method_status status
;
112 BT_ASSERT(msg_iter_data
->ds_file
);
117 status
= ctf_fs_ds_file_next(msg_iter_data
->ds_file
, &msg
);
119 case BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
:
123 case BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
:
127 if (msg_iter_data
->ds_file_info_index
==
128 msg_iter_data
->ds_file_group
->ds_file_infos
->len
- 1) {
129 /* End of all group's stream files */
133 msg_iter_data
->ds_file_info_index
++;
134 bt_msg_iter_reset_for_next_stream_file(
135 msg_iter_data
->msg_iter
);
136 set_msg_iter_emits_stream_beginning_end_messages(
140 * Open and start reading the next stream file
141 * within our stream file group.
143 ret
= msg_iter_data_set_current_ds_file(msg_iter_data
);
145 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
149 /* Continue the loop to get the next message */
162 bt_component_class_message_iterator_next_method_status
ctf_fs_iterator_next(
163 bt_self_message_iterator
*iterator
,
164 bt_message_array_const msgs
, uint64_t capacity
,
167 bt_component_class_message_iterator_next_method_status status
=
168 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
169 struct ctf_fs_msg_iter_data
*msg_iter_data
=
170 bt_self_message_iterator_get_data(iterator
);
173 while (i
< capacity
&&
174 status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
175 status
= ctf_fs_iterator_next_one(msg_iter_data
, &msgs
[i
]);
176 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
183 * Even if ctf_fs_iterator_next_one() returned something
184 * else than BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK, we
185 * accumulated message objects in the output
186 * message array, so we need to return
187 * BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK so that they are
188 * transfered to downstream. This other status occurs
189 * again the next time muxer_msg_iter_do_next() is
190 * called, possibly without any accumulated
191 * message, in which case we'll return it.
194 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
201 int ctf_fs_iterator_reset(struct ctf_fs_msg_iter_data
*msg_iter_data
)
205 msg_iter_data
->ds_file_info_index
= 0;
206 ret
= msg_iter_data_set_current_ds_file(msg_iter_data
);
211 bt_msg_iter_reset(msg_iter_data
->msg_iter
);
212 set_msg_iter_emits_stream_beginning_end_messages(msg_iter_data
);
219 bt_component_class_message_iterator_seek_beginning_method_status
220 ctf_fs_iterator_seek_beginning(bt_self_message_iterator
*it
)
222 struct ctf_fs_msg_iter_data
*msg_iter_data
=
223 bt_self_message_iterator_get_data(it
);
224 bt_component_class_message_iterator_seek_beginning_method_status status
=
225 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
227 BT_ASSERT(msg_iter_data
);
228 if (ctf_fs_iterator_reset(msg_iter_data
)) {
229 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_ERROR
;
236 void ctf_fs_iterator_finalize(bt_self_message_iterator
*it
)
238 ctf_fs_msg_iter_data_destroy(
239 bt_self_message_iterator_get_data(it
));
243 bt_component_class_message_iterator_init_method_status
ctf_fs_iterator_init(
244 bt_self_message_iterator
*self_msg_iter
,
245 bt_self_component_source
*self_comp_src
,
246 bt_self_component_port_output
*self_port
)
248 struct ctf_fs_port_data
*port_data
;
249 struct ctf_fs_msg_iter_data
*msg_iter_data
= NULL
;
250 bt_component_class_message_iterator_init_method_status ret
=
251 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
252 bt_logging_level log_level
;
253 bt_self_component
*self_comp
;
255 port_data
= bt_self_component_port_get_data(
256 bt_self_component_port_output_as_self_component_port(
258 BT_ASSERT(port_data
);
259 log_level
= port_data
->ctf_fs
->log_level
;
260 self_comp
= port_data
->ctf_fs
->self_comp
;
261 msg_iter_data
= g_new0(struct ctf_fs_msg_iter_data
, 1);
262 if (!msg_iter_data
) {
263 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
267 msg_iter_data
->log_level
= log_level
;
268 msg_iter_data
->self_comp
= self_comp
;
269 msg_iter_data
->pc_msg_iter
= self_msg_iter
;
270 msg_iter_data
->msg_iter
= bt_msg_iter_create(
271 port_data
->ds_file_group
->ctf_fs_trace
->metadata
->tc
,
272 bt_common_get_page_size(msg_iter_data
->log_level
) * 8,
273 ctf_fs_ds_file_medops
, NULL
, msg_iter_data
->log_level
,
275 if (!msg_iter_data
->msg_iter
) {
276 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
277 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
281 msg_iter_data
->ds_file_group
= port_data
->ds_file_group
;
282 if (ctf_fs_iterator_reset(msg_iter_data
)) {
283 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
287 bt_self_message_iterator_set_data(self_msg_iter
,
289 if (ret
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
) {
293 msg_iter_data
= NULL
;
297 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
300 ctf_fs_msg_iter_data_destroy(msg_iter_data
);
305 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
311 if (ctf_fs
->traces
) {
312 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
315 if (ctf_fs
->port_data
) {
316 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
323 void port_data_destroy(struct ctf_fs_port_data
*port_data
)
333 void port_data_destroy_notifier(void *data
) {
334 port_data_destroy(data
);
338 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
344 if (ctf_fs_trace
->ds_file_groups
) {
345 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
348 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace
->trace
);
350 if (ctf_fs_trace
->path
) {
351 g_string_free(ctf_fs_trace
->path
, TRUE
);
354 if (ctf_fs_trace
->name
) {
355 g_string_free(ctf_fs_trace
->name
, TRUE
);
358 if (ctf_fs_trace
->metadata
) {
359 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
360 g_free(ctf_fs_trace
->metadata
);
363 g_free(ctf_fs_trace
);
367 void ctf_fs_trace_destroy_notifier(void *data
)
369 struct ctf_fs_trace
*trace
= data
;
370 ctf_fs_trace_destroy(trace
);
373 struct ctf_fs_component
*ctf_fs_component_create(bt_logging_level log_level
,
374 bt_self_component
*self_comp
)
376 struct ctf_fs_component
*ctf_fs
;
378 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
383 ctf_fs
->log_level
= log_level
;
384 ctf_fs
->self_comp
= self_comp
;
386 g_ptr_array_new_with_free_func(port_data_destroy_notifier
);
387 if (!ctf_fs
->port_data
) {
392 g_ptr_array_new_with_free_func(ctf_fs_trace_destroy_notifier
);
393 if (!ctf_fs
->traces
) {
401 ctf_fs_destroy(ctf_fs
);
408 void ctf_fs_finalize(bt_self_component_source
*component
)
410 ctf_fs_destroy(bt_self_component_get_data(
411 bt_self_component_source_as_self_component(component
)));
414 gchar
*ctf_fs_make_port_name(struct ctf_fs_ds_file_group
*ds_file_group
)
416 GString
*name
= g_string_new(NULL
);
419 * The unique port name is generated by concatenating unique identifiers
427 /* For the trace, use the uuid if present, else the path. */
428 if (ds_file_group
->ctf_fs_trace
->metadata
->tc
->is_uuid_set
) {
429 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
431 bt_uuid_unparse(ds_file_group
->ctf_fs_trace
->metadata
->tc
->uuid
, uuid_str
);
432 g_string_assign(name
, uuid_str
);
434 g_string_assign(name
, ds_file_group
->ctf_fs_trace
->path
->str
);
438 * For the stream class, use the id if present. We can omit this field
439 * otherwise, as there will only be a single stream class.
441 if (ds_file_group
->sc
->id
!= UINT64_C(-1)) {
442 g_string_append_printf(name
, " | %" PRIu64
, ds_file_group
->sc
->id
);
445 /* For the stream, use the id if present, else, use the path. */
446 if (ds_file_group
->stream_id
!= UINT64_C(-1)) {
447 g_string_append_printf(name
, " | %" PRIu64
, ds_file_group
->stream_id
);
449 BT_ASSERT(ds_file_group
->ds_file_infos
->len
== 1);
450 struct ctf_fs_ds_file_info
*ds_file_info
=
451 g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
452 g_string_append_printf(name
, " | %s", ds_file_info
->path
->str
);
455 return g_string_free(name
, FALSE
);
459 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
460 struct ctf_fs_trace
*ctf_fs_trace
,
461 struct ctf_fs_ds_file_group
*ds_file_group
)
464 struct ctf_fs_port_data
*port_data
= NULL
;
466 bt_logging_level log_level
= ctf_fs
->log_level
;
467 bt_self_component
*self_comp
= ctf_fs
->self_comp
;
469 port_name
= ctf_fs_make_port_name(ds_file_group
);
474 BT_COMP_LOGI("Creating one port named `%s`", port_name
);
476 /* Create output port for this file */
477 port_data
= g_new0(struct ctf_fs_port_data
, 1);
482 port_data
->ctf_fs
= ctf_fs
;
483 port_data
->ds_file_group
= ds_file_group
;
484 ret
= bt_self_component_source_add_output_port(
485 ctf_fs
->self_comp_src
, port_name
, port_data
, NULL
);
490 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
502 port_data_destroy(port_data
);
507 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
508 struct ctf_fs_trace
*ctf_fs_trace
)
512 bt_logging_level log_level
= ctf_fs_trace
->log_level
;
513 bt_self_component
*self_comp
= ctf_fs_trace
->self_comp
;
515 /* Create one output port for each stream file group */
516 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
517 struct ctf_fs_ds_file_group
*ds_file_group
=
518 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
520 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
523 BT_COMP_LOGE("Cannot create output port.");
533 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
539 if (ds_file_info
->path
) {
540 g_string_free(ds_file_info
->path
, TRUE
);
543 g_free(ds_file_info
);
547 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
550 struct ctf_fs_ds_file_info
*ds_file_info
;
552 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
557 ds_file_info
->path
= g_string_new(path
);
558 if (!ds_file_info
->path
) {
559 ctf_fs_ds_file_info_destroy(ds_file_info
);
564 ds_file_info
->begin_ns
= begin_ns
;
571 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
573 if (!ds_file_group
) {
577 if (ds_file_group
->ds_file_infos
) {
578 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
581 if (ds_file_group
->index
) {
582 if (ds_file_group
->index
->entries
) {
583 g_ptr_array_free(ds_file_group
->index
->entries
, TRUE
);
585 g_free(ds_file_group
->index
);
588 bt_stream_put_ref(ds_file_group
->stream
);
589 g_free(ds_file_group
);
593 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
594 struct ctf_fs_trace
*ctf_fs_trace
,
595 struct ctf_stream_class
*sc
,
596 uint64_t stream_instance_id
,
597 struct ctf_fs_ds_index
*index
)
599 struct ctf_fs_ds_file_group
*ds_file_group
;
601 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
602 if (!ds_file_group
) {
606 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
607 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
608 if (!ds_file_group
->ds_file_infos
) {
612 ds_file_group
->index
= index
;
614 ds_file_group
->stream_id
= stream_instance_id
;
616 ds_file_group
->sc
= sc
;
617 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
621 ctf_fs_ds_file_group_destroy(ds_file_group
);
622 ctf_fs_ds_index_destroy(index
);
623 ds_file_group
= NULL
;
626 return ds_file_group
;
629 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
631 void array_insert(GPtrArray
*array
, gpointer element
, size_t pos
)
633 size_t original_array_len
= array
->len
;
635 /* Allocate an unused element at the end of the array. */
636 g_ptr_array_add(array
, NULL
);
638 /* If we are not inserting at the end, move the elements by one. */
639 if (pos
< original_array_len
) {
640 memmove(&(array
->pdata
[pos
+ 1]),
641 &(array
->pdata
[pos
]),
642 (original_array_len
- pos
) * sizeof(gpointer
));
645 /* Insert the value. */
646 array
->pdata
[pos
] = element
;
650 * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
651 * place to keep it sorted.
655 void ds_file_group_insert_ds_file_info_sorted(
656 struct ctf_fs_ds_file_group
*ds_file_group
,
657 struct ctf_fs_ds_file_info
*ds_file_info
)
661 /* Find the spot where to insert this ds_file_info. */
662 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
663 struct ctf_fs_ds_file_info
*other_ds_file_info
=
664 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
666 if (ds_file_info
->begin_ns
< other_ds_file_info
->begin_ns
) {
671 array_insert(ds_file_group
->ds_file_infos
, ds_file_info
, i
);
675 void ds_file_group_insert_ds_index_entry_sorted(
676 struct ctf_fs_ds_file_group
*ds_file_group
,
677 struct ctf_fs_ds_index_entry
*entry
)
681 /* Find the spot where to insert this index entry. */
682 for (i
= 0; i
< ds_file_group
->index
->entries
->len
; i
++) {
683 struct ctf_fs_ds_index_entry
*other_entry
= g_ptr_array_index(
684 ds_file_group
->index
->entries
, i
);
686 if (entry
->timestamp_begin_ns
< other_entry
->timestamp_begin_ns
) {
691 array_insert(ds_file_group
->index
->entries
, entry
, i
);
695 * Create a new ds_file_info using the provided path, begin_ns and index, then
696 * add it to ds_file_group's list of ds_file_infos.
700 int ctf_fs_ds_file_group_add_ds_file_info(
701 struct ctf_fs_ds_file_group
*ds_file_group
,
702 const char *path
, int64_t begin_ns
)
704 struct ctf_fs_ds_file_info
*ds_file_info
;
707 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
);
712 ds_file_group_insert_ds_file_info_sorted(ds_file_group
, ds_file_info
);
718 ctf_fs_ds_file_info_destroy(ds_file_info
);
725 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
728 int64_t stream_instance_id
= -1;
729 int64_t begin_ns
= -1;
730 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
731 bool add_group
= false;
734 struct ctf_fs_ds_file
*ds_file
= NULL
;
735 struct ctf_fs_ds_index
*index
= NULL
;
736 struct bt_msg_iter
*msg_iter
= NULL
;
737 struct ctf_stream_class
*sc
= NULL
;
738 struct bt_msg_iter_packet_properties props
;
739 bt_logging_level log_level
= ctf_fs_trace
->log_level
;
740 bt_self_component
*self_comp
= ctf_fs_trace
->self_comp
;
742 msg_iter
= bt_msg_iter_create(ctf_fs_trace
->metadata
->tc
,
743 bt_common_get_page_size(log_level
) * 8,
744 ctf_fs_ds_file_medops
, NULL
, log_level
, self_comp
);
746 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
750 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, NULL
, msg_iter
,
751 NULL
, path
, log_level
);
756 ret
= bt_msg_iter_get_packet_properties(ds_file
->msg_iter
, &props
);
758 BT_COMP_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
763 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
764 props
.stream_class_id
);
766 stream_instance_id
= props
.data_stream_id
;
768 if (props
.snapshots
.beginning_clock
!= UINT64_C(-1)) {
769 BT_ASSERT(sc
->default_clock_class
);
770 ret
= bt_util_clock_cycles_to_ns_from_origin(
771 props
.snapshots
.beginning_clock
,
772 sc
->default_clock_class
->frequency
,
773 sc
->default_clock_class
->offset_seconds
,
774 sc
->default_clock_class
->offset_cycles
, &begin_ns
);
776 BT_COMP_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).",
782 index
= ctf_fs_ds_file_build_index(ds_file
);
784 BT_COMP_LOGW("Failed to index CTF stream file \'%s\'",
785 ds_file
->file
->path
->str
);
788 if (begin_ns
== -1) {
790 * No beggining timestamp to sort the stream files
791 * within a stream file group, so consider that this
792 * file must be the only one within its group.
794 stream_instance_id
= -1;
797 if (stream_instance_id
== -1) {
799 * No stream instance ID or no beginning timestamp:
800 * create a unique stream file group for this stream
801 * file because, even if there's a stream instance ID,
802 * there's no timestamp to order the file within its
805 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
806 sc
, UINT64_C(-1), index
);
807 /* Ownership of index is transferred. */
810 if (!ds_file_group
) {
814 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
824 BT_ASSERT(stream_instance_id
!= -1);
825 BT_ASSERT(begin_ns
!= -1);
827 /* Find an existing stream file group with this ID */
828 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
829 ds_file_group
= g_ptr_array_index(
830 ctf_fs_trace
->ds_file_groups
, i
);
832 if (ds_file_group
->sc
== sc
&&
833 ds_file_group
->stream_id
==
834 stream_instance_id
) {
838 ds_file_group
= NULL
;
841 if (!ds_file_group
) {
842 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
843 sc
, stream_instance_id
, index
);
844 /* Ownership of index is transferred. */
846 if (!ds_file_group
) {
853 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
, path
,
862 ctf_fs_ds_file_group_destroy(ds_file_group
);
863 ds_file_group
= NULL
;
867 if (add_group
&& ds_file_group
) {
868 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
871 ctf_fs_ds_file_destroy(ds_file
);
874 bt_msg_iter_destroy(msg_iter
);
877 ctf_fs_ds_index_destroy(index
);
882 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
885 const char *basename
;
886 GError
*error
= NULL
;
888 bt_logging_level log_level
= ctf_fs_trace
->log_level
;
889 bt_self_component
*self_comp
= ctf_fs_trace
->self_comp
;
891 /* Check each file in the path directory, except specific ones */
892 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
894 BT_COMP_LOGE("Cannot open directory `%s`: %s (code %d)",
895 ctf_fs_trace
->path
->str
, error
->message
,
900 while ((basename
= g_dir_read_name(dir
))) {
901 struct ctf_fs_file
*file
;
903 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
904 /* Ignore the metadata stream. */
905 BT_COMP_LOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S
"%s`",
906 ctf_fs_trace
->path
->str
, basename
);
910 if (basename
[0] == '.') {
911 BT_COMP_LOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S
"%s`",
912 ctf_fs_trace
->path
->str
, basename
);
916 /* Create the file. */
917 file
= ctf_fs_file_create(log_level
, self_comp
);
919 BT_COMP_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
"%s`",
920 ctf_fs_trace
->path
->str
, basename
);
924 /* Create full path string. */
925 g_string_append_printf(file
->path
, "%s" G_DIR_SEPARATOR_S
"%s",
926 ctf_fs_trace
->path
->str
, basename
);
927 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
928 BT_COMP_LOGI("Ignoring non-regular file `%s`",
930 ctf_fs_file_destroy(file
);
935 ret
= ctf_fs_file_open(file
, "rb");
937 BT_COMP_LOGE("Cannot open stream file `%s`", file
->path
->str
);
941 if (file
->size
== 0) {
942 /* Skip empty stream. */
943 BT_COMP_LOGI("Ignoring empty file `%s`", file
->path
->str
);
944 ctf_fs_file_destroy(file
);
948 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
951 BT_COMP_LOGE("Cannot add stream file `%s` to stream file group",
953 ctf_fs_file_destroy(file
);
957 ctf_fs_file_destroy(file
);
979 int set_trace_name(bt_trace
*trace
, const char *name_suffix
,
980 bt_logging_level log_level
, bt_self_component
*self_comp
)
983 const bt_trace_class
*tc
= bt_trace_borrow_class_const(trace
);
987 name
= g_string_new(NULL
);
989 BT_COMP_LOGE_STR("Failed to allocate a GString.");
995 * Check if we have a trace environment string value named `hostname`.
996 * If so, use it as the trace name's prefix.
998 val
= bt_trace_class_borrow_environment_entry_value_by_name_const(
1000 if (val
&& bt_value_is_string(val
)) {
1001 g_string_append(name
, bt_value_string_get(val
));
1004 g_string_append_c(name
, G_DIR_SEPARATOR
);
1009 g_string_append(name
, name_suffix
);
1012 ret
= bt_trace_set_name(trace
, name
->str
);
1021 g_string_free(name
, TRUE
);
1028 struct ctf_fs_trace
*ctf_fs_trace_create(bt_self_component
*self_comp
,
1029 const char *path
, const char *name
,
1030 struct ctf_fs_metadata_config
*metadata_config
,
1031 bt_logging_level log_level
)
1033 struct ctf_fs_trace
*ctf_fs_trace
;
1036 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
1037 if (!ctf_fs_trace
) {
1041 ctf_fs_trace
->log_level
= log_level
;
1042 ctf_fs_trace
->self_comp
= self_comp
;
1043 ctf_fs_trace
->path
= g_string_new(path
);
1044 if (!ctf_fs_trace
->path
) {
1048 ctf_fs_trace
->name
= g_string_new(name
);
1049 if (!ctf_fs_trace
->name
) {
1053 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
1054 if (!ctf_fs_trace
->metadata
) {
1058 ctf_fs_metadata_init(ctf_fs_trace
->metadata
);
1059 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
1060 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
1061 if (!ctf_fs_trace
->ds_file_groups
) {
1065 ret
= ctf_fs_metadata_set_trace_class(self_comp
, ctf_fs_trace
,
1071 if (ctf_fs_trace
->metadata
->trace_class
) {
1072 ctf_fs_trace
->trace
=
1073 bt_trace_create(ctf_fs_trace
->metadata
->trace_class
);
1074 if (!ctf_fs_trace
->trace
) {
1079 if (ctf_fs_trace
->trace
) {
1080 ret
= set_trace_name(ctf_fs_trace
->trace
, name
, log_level
,
1087 ret
= create_ds_file_groups(ctf_fs_trace
);
1095 ctf_fs_trace_destroy(ctf_fs_trace
);
1096 ctf_fs_trace
= NULL
;
1099 return ctf_fs_trace
;
1103 int path_is_ctf_trace(const char *path
)
1105 GString
*metadata_path
= g_string_new(NULL
);
1108 if (!metadata_path
) {
1113 g_string_printf(metadata_path
, "%s" G_DIR_SEPARATOR_S
"%s", path
, CTF_FS_METADATA_FILENAME
);
1115 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
1121 g_string_free(metadata_path
, TRUE
);
1126 int add_trace_path(GList
**trace_paths
, const char *path
,
1127 bt_logging_level log_level
, bt_self_component
*self_comp
)
1129 GString
*norm_path
= NULL
;
1132 norm_path
= bt_common_normalize_path(path
, NULL
);
1134 BT_COMP_LOGE("Failed to normalize path `%s`.", path
);
1139 // FIXME: Remove or ifdef for __MINGW32__
1140 if (strcmp(norm_path
->str
, "/") == 0) {
1141 BT_COMP_LOGE("Opening a trace in `/` is not supported.");
1146 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1147 BT_ASSERT(*trace_paths
);
1152 g_string_free(norm_path
, TRUE
);
1159 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
,
1160 bt_logging_level log_level
, bt_self_component
*self_comp
)
1163 GError
*error
= NULL
;
1165 const char *basename
= NULL
;
1167 /* Check if the starting path is a CTF trace itself */
1168 ret
= path_is_ctf_trace(start_path
);
1175 * Stop recursion: a CTF trace cannot contain another
1178 ret
= add_trace_path(trace_paths
, start_path
, log_level
,
1183 /* Look for subdirectories */
1184 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1185 /* Starting path is not a directory: end of recursion */
1189 dir
= g_dir_open(start_path
, 0, &error
);
1191 if (error
->code
== G_FILE_ERROR_ACCES
) {
1192 BT_COMP_LOGI("Cannot open directory `%s`: %s (code %d): continuing",
1193 start_path
, error
->message
, error
->code
);
1197 BT_COMP_LOGE("Cannot open directory `%s`: %s (code %d)",
1198 start_path
, error
->message
, error
->code
);
1203 while ((basename
= g_dir_read_name(dir
))) {
1204 GString
*sub_path
= g_string_new(NULL
);
1211 g_string_printf(sub_path
, "%s" G_DIR_SEPARATOR_S
"%s", start_path
, basename
);
1212 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
,
1213 log_level
, self_comp
);
1214 g_string_free(sub_path
, TRUE
);
1226 g_error_free(error
);
1233 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1234 GList
*trace_names
= NULL
;
1236 const char *last_sep
;
1240 * At this point we know that all the trace paths are
1241 * normalized, and so is the base path. This means that
1242 * they are absolute and they don't end with a separator.
1243 * We can simply find the location of the last separator
1244 * in the base path, which gives us the name of the actual
1245 * directory to look into, and use this location as the
1246 * start of each trace name within each trace path.
1250 * Base path: /home/user/my-traces/some-trace
1252 * - /home/user/my-traces/some-trace/host1/trace1
1253 * - /home/user/my-traces/some-trace/host1/trace2
1254 * - /home/user/my-traces/some-trace/host2/trace
1255 * - /home/user/my-traces/some-trace/other-trace
1257 * In this case the trace names are:
1259 * - some-trace/host1/trace1
1260 * - some-trace/host1/trace2
1261 * - some-trace/host2/trace
1262 * - some-trace/other-trace
1264 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1266 /* We know there's at least one separator */
1267 BT_ASSERT(last_sep
);
1269 /* Distance to base */
1270 base_dist
= last_sep
- base_path
+ 1;
1272 /* Create the trace names */
1273 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1274 GString
*trace_name
= g_string_new(NULL
);
1275 GString
*trace_path
= node
->data
;
1277 BT_ASSERT(trace_name
);
1278 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1279 trace_names
= g_list_append(trace_names
, trace_name
);
1285 /* Helper for ctf_fs_component_create_ctf_fs_traces, to handle a single path/root. */
1288 int ctf_fs_component_create_ctf_fs_traces_one_root(
1289 struct ctf_fs_component
*ctf_fs
,
1290 const char *path_param
)
1292 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1294 GString
*norm_path
= NULL
;
1295 GList
*trace_paths
= NULL
;
1296 GList
*trace_names
= NULL
;
1299 bt_logging_level log_level
= ctf_fs
->log_level
;
1300 bt_self_component
*self_comp
= ctf_fs
->self_comp
;
1302 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1304 BT_COMP_LOGE("Failed to normalize path: `%s`.",
1309 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
, log_level
,
1316 BT_COMP_LOGE("No CTF traces recursively found in `%s`.",
1321 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1323 BT_COMP_LOGE("Cannot create trace names from trace paths.");
1327 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1328 tp_node
= g_list_next(tp_node
),
1329 tn_node
= g_list_next(tn_node
)) {
1330 GString
*trace_path
= tp_node
->data
;
1331 GString
*trace_name
= tn_node
->data
;
1333 ctf_fs_trace
= ctf_fs_trace_create(self_comp
,
1334 trace_path
->str
, trace_name
->str
,
1335 &ctf_fs
->metadata_config
,
1337 if (!ctf_fs_trace
) {
1338 BT_COMP_LOGE("Cannot create trace for `%s`.",
1343 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1344 ctf_fs_trace
= NULL
;
1351 ctf_fs_trace_destroy(ctf_fs_trace
);
1354 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1355 if (tp_node
->data
) {
1356 g_string_free(tp_node
->data
, TRUE
);
1360 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1361 if (tn_node
->data
) {
1362 g_string_free(tn_node
->data
, TRUE
);
1367 g_list_free(trace_paths
);
1371 g_list_free(trace_names
);
1375 g_string_free(norm_path
, TRUE
);
1381 /* GCompareFunc to sort traces by UUID. */
1384 gint
sort_traces_by_uuid(gconstpointer a
, gconstpointer b
)
1386 const struct ctf_fs_trace
*trace_a
= *((const struct ctf_fs_trace
**) a
);
1387 const struct ctf_fs_trace
*trace_b
= *((const struct ctf_fs_trace
**) b
);
1389 bool trace_a_has_uuid
= trace_a
->metadata
->tc
->is_uuid_set
;
1390 bool trace_b_has_uuid
= trace_b
->metadata
->tc
->is_uuid_set
;
1393 /* Order traces without uuid first. */
1394 if (!trace_a_has_uuid
&& trace_b_has_uuid
) {
1396 } else if (trace_a_has_uuid
&& !trace_b_has_uuid
) {
1398 } else if (!trace_a_has_uuid
&& !trace_b_has_uuid
) {
1401 ret
= bt_uuid_compare(trace_a
->metadata
->tc
->uuid
, trace_b
->metadata
->tc
->uuid
);
1408 * Count the number of stream and event classes defined by this trace's metadata.
1410 * This is used to determine which metadata is the "latest", out of multiple
1411 * traces sharing the same UUID. It is assumed that amongst all these metadatas,
1412 * a bigger metadata is a superset of a smaller metadata. Therefore, it is
1413 * enough to just count the classes.
1417 unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace
*trace
)
1419 unsigned int num
= trace
->metadata
->tc
->stream_classes
->len
;
1422 for (i
= 0; i
< trace
->metadata
->tc
->stream_classes
->len
; i
++) {
1423 struct ctf_stream_class
*sc
= trace
->metadata
->tc
->stream_classes
->pdata
[i
];
1424 num
+= sc
->event_classes
->len
;
1431 * Merge the src ds_file_group into dest. This consists of merging their
1432 * ds_file_infos, making sure to keep the result sorted.
1436 void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group
*dest
, struct ctf_fs_ds_file_group
*src
)
1440 for (i
= 0; i
< src
->ds_file_infos
->len
; i
++) {
1441 struct ctf_fs_ds_file_info
*ds_file_info
=
1442 g_ptr_array_index(src
->ds_file_infos
, i
);
1444 /* Ownership of the ds_file_info is transferred to dest. */
1445 g_ptr_array_index(src
->ds_file_infos
, i
) = NULL
;
1447 ds_file_group_insert_ds_file_info_sorted(dest
, ds_file_info
);
1450 /* Merge both indexes. */
1451 for (i
= 0; i
< src
->index
->entries
->len
; i
++) {
1452 struct ctf_fs_ds_index_entry
*entry
= g_ptr_array_index(
1453 src
->index
->entries
, i
);
1456 * Ownership of the ctf_fs_ds_index_entry is transferred to
1459 g_ptr_array_index(src
->index
->entries
, i
) = NULL
;
1461 ds_file_group_insert_ds_index_entry_sorted(dest
, entry
);
1464 /* Merge src_trace's data stream file groups into dest_trace's. */
1467 int merge_matching_ctf_fs_ds_file_groups(
1468 struct ctf_fs_trace
*dest_trace
,
1469 struct ctf_fs_trace
*src_trace
)
1472 GPtrArray
*dest
= dest_trace
->ds_file_groups
;
1473 GPtrArray
*src
= src_trace
->ds_file_groups
;
1478 * Save the initial length of dest: we only want to check against the
1479 * original elements in the inner loop.
1481 const guint dest_len
= dest
->len
;
1483 for (s_i
= 0; s_i
< src
->len
; s_i
++) {
1484 struct ctf_fs_ds_file_group
*src_group
= g_ptr_array_index(src
, s_i
);
1485 struct ctf_fs_ds_file_group
*dest_group
= NULL
;
1487 /* A stream instance without ID can't match a stream in the other trace. */
1488 if (src_group
->stream_id
!= -1) {
1491 /* Let's search for a matching ds_file_group in the destination. */
1492 for (d_i
= 0; d_i
< dest_len
; d_i
++) {
1493 struct ctf_fs_ds_file_group
*candidate_dest
= g_ptr_array_index(dest
, d_i
);
1495 /* Can't match a stream instance without ID. */
1496 if (candidate_dest
->stream_id
== -1) {
1501 * If the two groups have the same stream instance id
1502 * and belong to the same stream class (stream instance
1503 * ids are per-stream class), they represent the same
1506 if (candidate_dest
->stream_id
!= src_group
->stream_id
||
1507 candidate_dest
->sc
->id
!= src_group
->sc
->id
) {
1511 dest_group
= candidate_dest
;
1517 * Didn't find a friend in dest to merge our src_group into?
1518 * Create a new empty one. This can happen if a stream was
1519 * active in the source trace chunk but not in the destination
1523 struct ctf_stream_class
*sc
;
1524 struct ctf_fs_ds_index
*index
;
1526 sc
= ctf_trace_class_borrow_stream_class_by_id(
1527 dest_trace
->metadata
->tc
, src_group
->sc
->id
);
1530 index
= ctf_fs_ds_index_create(dest_trace
->log_level
,
1531 dest_trace
->self_comp
);
1537 dest_group
= ctf_fs_ds_file_group_create(dest_trace
, sc
,
1538 src_group
->stream_id
, index
);
1539 /* Ownership of index is transferred. */
1546 g_ptr_array_add(dest_trace
->ds_file_groups
, dest_group
);
1549 BT_ASSERT(dest_group
);
1550 merge_ctf_fs_ds_file_groups(dest_group
, src_group
);
1558 * Collapse the given traces, which must all share the same UUID, in a single
1561 * The trace with the most expansive metadata is chosen and all other traces
1562 * are merged into that one. The array slots of all the traces that get merged
1563 * in the chosen one are set to NULL, so only the slot of the chosen trace
1568 int merge_ctf_fs_traces(struct ctf_fs_trace
**traces
, unsigned int num_traces
)
1570 unsigned int winner_count
;
1571 struct ctf_fs_trace
*winner
;
1574 char uuid_str
[BABELTRACE_UUID_STR_LEN
];
1576 BT_ASSERT(num_traces
>= 2);
1578 winner_count
= metadata_count_stream_and_event_classes(traces
[0]);
1581 /* Find the trace with the largest metadata. */
1582 for (i
= 1; i
< num_traces
; i
++) {
1583 struct ctf_fs_trace
*candidate
;
1584 unsigned int candidate_count
;
1586 candidate
= traces
[i
];
1588 /* A bit of sanity check. */
1589 BT_ASSERT(bt_uuid_compare(winner
->metadata
->tc
->uuid
, candidate
->metadata
->tc
->uuid
) == 0);
1591 candidate_count
= metadata_count_stream_and_event_classes(candidate
);
1593 if (candidate_count
> winner_count
) {
1594 winner_count
= candidate_count
;
1599 /* Merge all the other traces in the winning trace. */
1600 for (i
= 0; i
< num_traces
; i
++) {
1601 struct ctf_fs_trace
*trace
= traces
[i
];
1603 /* Don't merge the winner into itself. */
1604 if (trace
== winner
) {
1608 /* Merge trace's data stream file groups into winner's. */
1609 ret
= merge_matching_ctf_fs_ds_file_groups(winner
, trace
);
1614 /* Free the trace that got merged into winner, clear the slot in the array. */
1615 ctf_fs_trace_destroy(trace
);
1619 /* Use the string representation of the UUID as the trace name. */
1620 bt_uuid_unparse(winner
->metadata
->tc
->uuid
, uuid_str
);
1621 g_string_printf(winner
->name
, "%s", uuid_str
);
1628 * Merge all traces of `ctf_fs` that share the same UUID in a single trace.
1629 * Traces with no UUID are not merged.
1633 int merge_traces_with_same_uuid(struct ctf_fs_component
*ctf_fs
)
1635 GPtrArray
*traces
= ctf_fs
->traces
;
1636 guint range_start_idx
= 0;
1637 unsigned int num_traces
= 0;
1641 /* Sort the traces by uuid, then collapse traces with the same uuid in a single one. */
1642 g_ptr_array_sort(traces
, sort_traces_by_uuid
);
1644 /* Find ranges of consecutive traces that share the same UUID. */
1645 while (range_start_idx
< traces
->len
) {
1647 struct ctf_fs_trace
*range_start_trace
= g_ptr_array_index(traces
, range_start_idx
);
1649 /* Exclusive end of range. */
1650 guint range_end_exc_idx
= range_start_idx
+ 1;
1652 while (range_end_exc_idx
< traces
->len
) {
1653 struct ctf_fs_trace
*this_trace
= g_ptr_array_index(traces
, range_end_exc_idx
);
1655 if (!range_start_trace
->metadata
->tc
->is_uuid_set
||
1656 (bt_uuid_compare(range_start_trace
->metadata
->tc
->uuid
, this_trace
->metadata
->tc
->uuid
) != 0)) {
1660 range_end_exc_idx
++;
1663 /* If we have two or more traces with matching UUIDs, merge them. */
1664 range_len
= range_end_exc_idx
- range_start_idx
;
1665 if (range_len
> 1) {
1666 struct ctf_fs_trace
**range_start
= (struct ctf_fs_trace
**) &traces
->pdata
[range_start_idx
];
1667 ret
= merge_ctf_fs_traces(range_start
, range_len
);
1674 range_start_idx
= range_end_exc_idx
;
1677 /* Clear any NULL slot (traces that got merged in another one) in the array. */
1678 for (i
= 0; i
< traces
->len
;) {
1679 if (g_ptr_array_index(traces
, i
) == NULL
) {
1680 g_ptr_array_remove_index_fast(traces
, i
);
1686 BT_ASSERT(num_traces
== traces
->len
);
1692 int ctf_fs_component_create_ctf_fs_traces(bt_self_component_source
*self_comp
,
1693 struct ctf_fs_component
*ctf_fs
,
1694 const bt_value
*paths_value
)
1699 for (i
= 0; i
< bt_value_array_get_size(paths_value
); i
++) {
1700 const bt_value
*path_value
= bt_value_array_borrow_element_by_index_const(paths_value
, i
);
1701 const char *path
= bt_value_string_get(path_value
);
1703 ret
= ctf_fs_component_create_ctf_fs_traces_one_root(ctf_fs
,
1710 ret
= merge_traces_with_same_uuid(ctf_fs
);
1717 GString
*get_stream_instance_unique_name(
1718 struct ctf_fs_ds_file_group
*ds_file_group
)
1721 struct ctf_fs_ds_file_info
*ds_file_info
;
1723 name
= g_string_new(NULL
);
1729 * If there's more than one stream file in the stream file
1730 * group, the first (earliest) stream file's path is used as
1731 * the stream's unique name.
1733 BT_ASSERT(ds_file_group
->ds_file_infos
->len
> 0);
1734 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
1735 g_string_assign(name
, ds_file_info
->path
->str
);
1741 /* Create the IR stream objects for ctf_fs_trace. */
1744 int create_streams_for_trace(struct ctf_fs_trace
*ctf_fs_trace
)
1747 GString
*name
= NULL
;
1749 bt_logging_level log_level
= ctf_fs_trace
->log_level
;
1750 bt_self_component
*self_comp
= ctf_fs_trace
->self_comp
;
1752 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
1753 struct ctf_fs_ds_file_group
*ds_file_group
=
1754 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
1755 name
= get_stream_instance_unique_name(ds_file_group
);
1761 if (ds_file_group
->sc
->ir_sc
) {
1762 BT_ASSERT(ctf_fs_trace
->trace
);
1764 if (ds_file_group
->stream_id
== UINT64_C(-1)) {
1765 /* No stream ID: use 0 */
1766 ds_file_group
->stream
= bt_stream_create_with_id(
1767 ds_file_group
->sc
->ir_sc
,
1768 ctf_fs_trace
->trace
,
1769 ctf_fs_trace
->next_stream_id
);
1770 ctf_fs_trace
->next_stream_id
++;
1772 /* Specific stream ID */
1773 ds_file_group
->stream
= bt_stream_create_with_id(
1774 ds_file_group
->sc
->ir_sc
,
1775 ctf_fs_trace
->trace
,
1776 (uint64_t) ds_file_group
->stream_id
);
1779 ds_file_group
->stream
= NULL
;
1782 if (!ds_file_group
->stream
) {
1783 BT_COMP_LOGE("Cannot create stream for DS file group: "
1784 "addr=%p, stream-name=\"%s\"",
1785 ds_file_group
, name
->str
);
1789 ret
= bt_stream_set_name(ds_file_group
->stream
,
1792 BT_COMP_LOGE("Cannot set stream's name: "
1793 "addr=%p, stream-name=\"%s\"",
1794 ds_file_group
->stream
, name
->str
);
1798 g_string_free(name
, TRUE
);
1811 g_string_free(name
, TRUE
);
1817 * Validate the "paths" parameter passed to this component. It must be
1818 * present, and it must be an array of strings.
1822 bool validate_paths_parameter(struct ctf_fs_component
*ctf_fs
,
1823 const bt_value
*paths
)
1828 bt_logging_level log_level
= ctf_fs
->log_level
;
1829 bt_self_component
*self_comp
= ctf_fs
->self_comp
;
1832 BT_COMP_LOGE("missing \"paths\" parameter");
1836 type
= bt_value_get_type(paths
);
1837 if (type
!= BT_VALUE_TYPE_ARRAY
) {
1838 BT_COMP_LOGE("`paths` parameter: expecting array value: type=%s",
1839 bt_common_value_type_string(type
));
1843 for (i
= 0; i
< bt_value_array_get_size(paths
); i
++) {
1844 const bt_value
*elem
;
1846 elem
= bt_value_array_borrow_element_by_index_const(paths
, i
);
1847 type
= bt_value_get_type(elem
);
1848 if (type
!= BT_VALUE_TYPE_STRING
) {
1849 BT_COMP_LOGE("`paths` parameter: expecting string value: index=%" PRIu64
", type=%s",
1850 i
, bt_common_value_type_string(type
));
1865 bool read_src_fs_parameters(const bt_value
*params
,
1866 const bt_value
**paths
, struct ctf_fs_component
*ctf_fs
) {
1868 const bt_value
*value
;
1869 bt_logging_level log_level
= ctf_fs
->log_level
;
1870 bt_self_component
*self_comp
= ctf_fs
->self_comp
;
1872 /* paths parameter */
1873 *paths
= bt_value_map_borrow_entry_value_const(params
, "paths");
1874 if (!validate_paths_parameter(ctf_fs
, *paths
)) {
1878 /* clock-class-offset-s parameter */
1879 value
= bt_value_map_borrow_entry_value_const(params
,
1880 "clock-class-offset-s");
1882 if (!bt_value_is_signed_integer(value
)) {
1883 BT_COMP_LOGE("clock-class-offset-s must be an integer");
1886 ctf_fs
->metadata_config
.clock_class_offset_s
=
1887 bt_value_signed_integer_get(value
);
1890 /* clock-class-offset-ns parameter */
1891 value
= bt_value_map_borrow_entry_value_const(params
,
1892 "clock-class-offset-ns");
1894 if (!bt_value_is_signed_integer(value
)) {
1895 BT_COMP_LOGE("clock-class-offset-ns must be an integer");
1898 ctf_fs
->metadata_config
.clock_class_offset_ns
=
1899 bt_value_signed_integer_get(value
);
1914 struct ctf_fs_component
*ctf_fs_create(
1915 bt_self_component_source
*self_comp_src
,
1916 const bt_value
*params
)
1918 struct ctf_fs_component
*ctf_fs
= NULL
;
1920 const bt_value
*paths_value
;
1921 bt_self_component
*self_comp
=
1922 bt_self_component_source_as_self_component(self_comp_src
);
1924 ctf_fs
= ctf_fs_component_create(bt_component_get_logging_level(
1925 bt_self_component_as_component(self_comp
)), self_comp
);
1930 if (!read_src_fs_parameters(params
, &paths_value
, ctf_fs
)) {
1934 bt_self_component_set_data(self_comp
, ctf_fs
);
1935 ctf_fs
->self_comp
= self_comp
;
1936 ctf_fs
->self_comp_src
= self_comp_src
;
1938 if (ctf_fs_component_create_ctf_fs_traces(self_comp_src
, ctf_fs
, paths_value
)) {
1942 for (i
= 0; i
< ctf_fs
->traces
->len
; i
++) {
1943 struct ctf_fs_trace
*trace
= g_ptr_array_index(ctf_fs
->traces
, i
);
1945 if (create_streams_for_trace(trace
)) {
1949 if (create_ports_for_trace(ctf_fs
, trace
)) {
1957 ctf_fs_destroy(ctf_fs
);
1959 bt_self_component_set_data(self_comp
, NULL
);
1966 bt_component_class_init_method_status
ctf_fs_init(
1967 bt_self_component_source
*self_comp
,
1968 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1970 struct ctf_fs_component
*ctf_fs
;
1971 bt_component_class_init_method_status ret
=
1972 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
1974 ctf_fs
= ctf_fs_create(self_comp
, params
);
1976 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1983 bt_component_class_query_method_status
ctf_fs_query(
1984 bt_self_component_class_source
*comp_class
,
1985 const bt_query_executor
*query_exec
,
1986 const char *object
, const bt_value
*params
,
1987 bt_logging_level log_level
,
1988 const bt_value
**result
)
1990 bt_component_class_query_method_status status
=
1991 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1993 if (!strcmp(object
, "metadata-info")) {
1994 status
= metadata_info_query(comp_class
, params
, log_level
,
1996 } else if (!strcmp(object
, "trace-info")) {
1997 status
= trace_info_query(comp_class
, params
, log_level
,
2000 BT_LOGE("Unknown query object `%s`", object
);
2001 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_OBJECT
;