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 #include <babeltrace/common-internal.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/stream.h>
32 #include <babeltrace/ctf-ir/fields.h>
33 #include <babeltrace/graph/private-port.h>
34 #include <babeltrace/graph/private-component.h>
35 #include <babeltrace/graph/private-component-source.h>
36 #include <babeltrace/graph/private-notification-iterator.h>
37 #include <babeltrace/graph/component.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/clock-class-priority-map.h>
40 #include <plugins-common.h>
47 #include "data-stream-file.h"
49 #include "../common/metadata/decoder.h"
50 #include "../common/notif-iter/notif-iter.h"
51 #include "../common/utils/utils.h"
54 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
58 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data
*notif_iter_data
)
60 struct ctf_fs_ds_file_info
*ds_file_info
;
63 assert(notif_iter_data
->ds_file_info_index
<
64 notif_iter_data
->ds_file_group
->ds_file_infos
->len
);
65 ds_file_info
= g_ptr_array_index(
66 notif_iter_data
->ds_file_group
->ds_file_infos
,
67 notif_iter_data
->ds_file_info_index
);
69 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
70 notif_iter_data
->ds_file
= ctf_fs_ds_file_create(
71 notif_iter_data
->ds_file_group
->ctf_fs_trace
,
72 notif_iter_data
->notif_iter
,
73 notif_iter_data
->ds_file_group
->stream
,
74 ds_file_info
->path
->str
);
75 if (!notif_iter_data
->ds_file
) {
83 void ctf_fs_notif_iter_data_destroy(
84 struct ctf_fs_notif_iter_data
*notif_iter_data
)
86 if (!notif_iter_data
) {
90 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
92 if (notif_iter_data
->notif_iter
) {
93 bt_ctf_notif_iter_destroy(notif_iter_data
->notif_iter
);
96 g_free(notif_iter_data
);
99 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
100 struct bt_private_notification_iterator
*iterator
)
102 struct bt_notification_iterator_next_return next_ret
;
103 struct ctf_fs_notif_iter_data
*notif_iter_data
=
104 bt_private_notification_iterator_get_user_data(iterator
);
107 assert(notif_iter_data
->ds_file
);
108 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
109 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
110 assert(!next_ret
.notification
);
111 notif_iter_data
->ds_file_info_index
++;
113 if (notif_iter_data
->ds_file_info_index
==
114 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
116 * No more stream files to read: we reached the
123 * Open and start reading the next stream file within
124 * our stream file group.
126 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
128 next_ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
132 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
135 * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END
136 * with a brand new stream file because empty stream
137 * files are not even part of stream file groups, which
138 * means we're sure to get at least one pair of "packet
139 * begin" and "packet end" notifications in the case of
140 * a single, empty packet.
142 assert(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
149 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
151 void *notif_iter_data
=
152 bt_private_notification_iterator_get_user_data(it
);
154 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
157 enum bt_notification_iterator_status
ctf_fs_iterator_init(
158 struct bt_private_notification_iterator
*it
,
159 struct bt_private_port
*port
)
161 struct ctf_fs_port_data
*port_data
;
162 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
163 enum bt_notification_iterator_status ret
=
164 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
167 port_data
= bt_private_port_get_user_data(port
);
169 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
173 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
174 if (!notif_iter_data
) {
175 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
179 notif_iter_data
->notif_iter
= bt_ctf_notif_iter_create(
180 port_data
->ds_file_group
->ctf_fs_trace
->metadata
->trace
,
181 bt_common_get_page_size() * 8,
182 ctf_fs_ds_file_medops
, NULL
);
183 if (!notif_iter_data
->notif_iter
) {
184 BT_LOGE_STR("Cannot create a CTF notification iterator.");
185 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
189 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
190 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
192 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
196 ret
= bt_private_notification_iterator_set_user_data(it
, notif_iter_data
);
197 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
201 notif_iter_data
= NULL
;
205 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
208 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
213 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
219 if (ctf_fs
->traces
) {
220 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
223 if (ctf_fs
->port_data
) {
224 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
231 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
237 if (ctf_fs_trace
->ds_file_groups
) {
238 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
241 if (ctf_fs_trace
->path
) {
242 g_string_free(ctf_fs_trace
->path
, TRUE
);
245 if (ctf_fs_trace
->name
) {
246 g_string_free(ctf_fs_trace
->name
, TRUE
);
249 if (ctf_fs_trace
->metadata
) {
250 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
251 g_free(ctf_fs_trace
->metadata
);
254 bt_put(ctf_fs_trace
->cc_prio_map
);
255 g_free(ctf_fs_trace
);
259 void ctf_fs_trace_destroy_notifier(void *data
)
261 struct ctf_fs_trace
*trace
= data
;
262 ctf_fs_trace_destroy(trace
);
265 void ctf_fs_finalize(struct bt_private_component
*component
)
267 void *data
= bt_private_component_get_user_data(component
);
269 ctf_fs_destroy(data
);
273 void port_data_destroy(void *data
) {
274 struct ctf_fs_port_data
*port_data
= data
;
284 GString
*get_stream_instance_unique_name(
285 struct ctf_fs_ds_file_group
*ds_file_group
)
288 struct ctf_fs_ds_file_info
*ds_file_info
;
290 name
= g_string_new(NULL
);
296 * If there's more than one stream file in the stream file
297 * group, the first (earliest) stream file's path is used as
298 * the stream's unique name.
300 assert(ds_file_group
->ds_file_infos
->len
> 0);
301 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
302 g_string_assign(name
, ds_file_info
->path
->str
);
309 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
310 struct ctf_fs_trace
*ctf_fs_trace
,
311 struct ctf_fs_ds_file_group
*ds_file_group
)
314 struct ctf_fs_port_data
*port_data
= NULL
;
315 GString
*port_name
= NULL
;
317 port_name
= get_stream_instance_unique_name(ds_file_group
);
322 BT_LOGD("Creating one port named `%s`", port_name
->str
);
324 /* Create output port for this file */
325 port_data
= g_new0(struct ctf_fs_port_data
, 1);
330 port_data
->ds_file_group
= ds_file_group
;
331 ret
= bt_private_component_source_add_output_private_port(
332 ctf_fs
->priv_comp
, port_name
->str
, port_data
, NULL
);
337 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
346 g_string_free(port_name
, TRUE
);
349 port_data_destroy(port_data
);
354 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
355 struct ctf_fs_trace
*ctf_fs_trace
)
360 /* Create one output port for each stream file group */
361 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
362 struct ctf_fs_ds_file_group
*ds_file_group
=
363 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
365 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
368 BT_LOGE("Cannot create output port.");
378 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
379 struct bt_ctf_field
*packet_header_field
)
381 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
382 uint64_t stream_instance_id
= -1ULL;
385 if (!packet_header_field
) {
389 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
390 packet_header_field
, "stream_instance_id");
391 if (!stream_instance_id_field
) {
395 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
396 &stream_instance_id
);
398 stream_instance_id
= -1ULL;
403 bt_put(stream_instance_id_field
);
404 return stream_instance_id
;
407 uint64_t get_packet_context_timestamp_begin_ns(
408 struct ctf_fs_trace
*ctf_fs_trace
,
409 struct bt_ctf_field
*packet_context_field
)
412 struct bt_ctf_field
*timestamp_begin_field
= NULL
;
413 struct bt_ctf_field_type
*timestamp_begin_ft
= NULL
;
414 uint64_t timestamp_begin_raw_value
= -1ULL;
415 uint64_t timestamp_begin_ns
= -1ULL;
416 int64_t timestamp_begin_ns_signed
;
417 struct bt_ctf_clock_class
*timestamp_begin_clock_class
= NULL
;
418 struct bt_ctf_clock_value
*clock_value
= NULL
;
420 if (!packet_context_field
) {
424 timestamp_begin_field
= bt_ctf_field_structure_get_field_by_name(
425 packet_context_field
, "timestamp_begin");
426 if (!timestamp_begin_field
) {
430 timestamp_begin_ft
= bt_ctf_field_get_type(timestamp_begin_field
);
431 assert(timestamp_begin_ft
);
432 timestamp_begin_clock_class
=
433 bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft
);
434 if (!timestamp_begin_clock_class
) {
438 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field
,
439 ×tamp_begin_raw_value
);
444 clock_value
= bt_ctf_clock_value_create(timestamp_begin_clock_class
,
445 timestamp_begin_raw_value
);
450 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
,
451 ×tamp_begin_ns_signed
);
456 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
459 bt_put(timestamp_begin_field
);
460 bt_put(timestamp_begin_ft
);
461 bt_put(timestamp_begin_clock_class
);
463 return timestamp_begin_ns
;
467 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
473 if (ds_file_info
->path
) {
474 g_string_free(ds_file_info
->path
, TRUE
);
477 ctf_fs_ds_index_destroy(ds_file_info
->index
);
478 g_free(ds_file_info
);
482 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
483 uint64_t begin_ns
, struct ctf_fs_ds_index
*index
)
485 struct ctf_fs_ds_file_info
*ds_file_info
;
487 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
492 ds_file_info
->path
= g_string_new(path
);
493 if (!ds_file_info
->path
) {
494 ctf_fs_ds_file_info_destroy(ds_file_info
);
499 ds_file_info
->begin_ns
= begin_ns
;
500 ds_file_info
->index
= index
;
504 ctf_fs_ds_index_destroy(index
);
509 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
511 if (!ds_file_group
) {
515 if (ds_file_group
->ds_file_infos
) {
516 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
519 bt_put(ds_file_group
->stream
);
520 bt_put(ds_file_group
->stream_class
);
521 g_free(ds_file_group
);
525 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
526 struct ctf_fs_trace
*ctf_fs_trace
,
527 struct bt_ctf_stream_class
*stream_class
,
528 uint64_t stream_instance_id
)
530 struct ctf_fs_ds_file_group
*ds_file_group
;
532 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
533 if (!ds_file_group
) {
537 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
538 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
539 if (!ds_file_group
->ds_file_infos
) {
543 ds_file_group
->stream_id
= stream_instance_id
;
544 assert(stream_class
);
545 ds_file_group
->stream_class
= bt_get(stream_class
);
546 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
550 ctf_fs_ds_file_group_destroy(ds_file_group
);
551 ds_file_group
= NULL
;
554 return ds_file_group
;
557 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
559 void array_insert(GPtrArray
*array
, gpointer element
, size_t pos
)
561 size_t original_array_len
= array
->len
;
563 /* Allocate an unused element at the end of the array. */
564 g_ptr_array_add(array
, NULL
);
566 /* If we are not inserting at the end, move the elements by one. */
567 if (pos
< original_array_len
) {
568 memmove(&(array
->pdata
[pos
+ 1]),
569 &(array
->pdata
[pos
]),
570 (original_array_len
- pos
) * sizeof(gpointer
));
573 /* Insert the value and bump the array len */
574 array
->pdata
[pos
] = element
;
578 int ctf_fs_ds_file_group_add_ds_file_info(
579 struct ctf_fs_ds_file_group
*ds_file_group
,
580 const char *path
, uint64_t begin_ns
,
581 struct ctf_fs_ds_index
*index
)
583 struct ctf_fs_ds_file_info
*ds_file_info
;
587 /* Onwership of index is transferred. */
588 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
, index
);
594 /* Find a spot to insert this one */
595 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
596 struct ctf_fs_ds_file_info
*other_ds_file_info
=
597 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
599 if (begin_ns
< other_ds_file_info
->begin_ns
) {
604 array_insert(ds_file_group
->ds_file_infos
, ds_file_info
, i
);
609 ctf_fs_ds_file_info_destroy(ds_file_info
);
610 ctf_fs_ds_index_destroy(index
);
617 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
620 struct bt_ctf_field
*packet_header_field
= NULL
;
621 struct bt_ctf_field
*packet_context_field
= NULL
;
622 struct bt_ctf_stream_class
*stream_class
= NULL
;
623 uint64_t stream_instance_id
= -1ULL;
624 uint64_t begin_ns
= -1ULL;
625 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
626 bool add_group
= false;
629 struct ctf_fs_ds_file
*ds_file
= NULL
;
630 struct ctf_fs_ds_index
*index
= NULL
;
631 struct bt_ctf_notif_iter
*notif_iter
= NULL
;
633 notif_iter
= bt_ctf_notif_iter_create(ctf_fs_trace
->metadata
->trace
,
634 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops
, NULL
);
636 BT_LOGE_STR("Cannot create a CTF notification iterator.");
640 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, notif_iter
, NULL
, path
);
645 ret
= ctf_fs_ds_file_get_packet_header_context_fields(ds_file
,
646 &packet_header_field
, &packet_context_field
);
648 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
653 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
654 packet_header_field
);
655 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
656 packet_context_field
);
657 stream_class
= ctf_utils_stream_class_from_packet_header(
658 ctf_fs_trace
->metadata
->trace
, packet_header_field
);
663 index
= ctf_fs_ds_file_build_index(ds_file
);
665 BT_LOGW("Failed to index CTF stream file \'%s\'",
666 ds_file
->file
->path
->str
);
669 if (begin_ns
== -1ULL) {
671 * No beggining timestamp to sort the stream files
672 * within a stream file group, so consider that this
673 * file must be the only one within its group.
675 stream_instance_id
= -1ULL;
678 if (stream_instance_id
== -1ULL) {
680 * No stream instance ID or no beginning timestamp:
681 * create a unique stream file group for this stream
682 * file because, even if there's a stream instance ID,
683 * there's no timestamp to order the file within its
686 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
687 stream_class
, stream_instance_id
);
688 if (!ds_file_group
) {
692 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
693 path
, begin_ns
, index
);
694 /* Ownership of index is transferred. */
704 assert(stream_instance_id
!= -1ULL);
705 assert(begin_ns
!= -1ULL);
707 /* Find an existing stream file group with this ID */
708 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
709 ds_file_group
= g_ptr_array_index(
710 ctf_fs_trace
->ds_file_groups
, i
);
712 if (ds_file_group
->stream_class
== stream_class
&&
713 ds_file_group
->stream_id
==
714 stream_instance_id
) {
718 ds_file_group
= NULL
;
721 if (!ds_file_group
) {
722 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
723 stream_class
, stream_instance_id
);
724 if (!ds_file_group
) {
731 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
, path
,
741 ctf_fs_ds_file_group_destroy(ds_file_group
);
745 if (add_group
&& ds_file_group
) {
746 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
749 ctf_fs_ds_file_destroy(ds_file
);
752 bt_ctf_notif_iter_destroy(notif_iter
);
755 ctf_fs_ds_index_destroy(index
);
756 bt_put(packet_header_field
);
757 bt_put(packet_context_field
);
758 bt_put(stream_class
);
763 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
766 const char *basename
;
767 GError
*error
= NULL
;
771 /* Check each file in the path directory, except specific ones */
772 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
774 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
775 ctf_fs_trace
->path
->str
, error
->message
,
780 while ((basename
= g_dir_read_name(dir
))) {
781 struct ctf_fs_file
*file
;
783 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
784 /* Ignore the metadata stream. */
785 BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S
"%s`",
786 ctf_fs_trace
->path
->str
, basename
);
790 if (basename
[0] == '.') {
791 BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S
"%s`",
792 ctf_fs_trace
->path
->str
, basename
);
796 /* Create the file. */
797 file
= ctf_fs_file_create();
799 BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
"%s`",
800 ctf_fs_trace
->path
->str
, basename
);
804 /* Create full path string. */
805 g_string_append_printf(file
->path
, "%s" G_DIR_SEPARATOR_S
"%s",
806 ctf_fs_trace
->path
->str
, basename
);
807 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
808 BT_LOGD("Ignoring non-regular file `%s`",
810 ctf_fs_file_destroy(file
);
815 ret
= ctf_fs_file_open(file
, "rb");
817 BT_LOGE("Cannot open stream file `%s`", file
->path
->str
);
821 if (file
->size
== 0) {
822 /* Skip empty stream. */
823 BT_LOGD("Ignoring empty file `%s`", file
->path
->str
);
824 ctf_fs_file_destroy(file
);
828 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
831 BT_LOGE("Cannot add stream file `%s` to stream file group",
833 ctf_fs_file_destroy(file
);
837 ctf_fs_file_destroy(file
);
841 * At this point, DS file groupes are created, but their
842 * associated stream objects do not exist yet. This is because
843 * we need to name the created stream object with the data
844 * stream file's path. We have everything we need here to do
847 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
848 struct ctf_fs_ds_file_group
*ds_file_group
=
849 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
850 GString
*name
= get_stream_instance_unique_name(ds_file_group
);
856 if (ds_file_group
->stream_id
== -1ULL) {
858 ds_file_group
->stream
= bt_ctf_stream_create(
859 ds_file_group
->stream_class
, name
->str
);
861 /* Specific stream ID */
862 ds_file_group
->stream
= bt_ctf_stream_create_with_id(
863 ds_file_group
->stream_class
, name
->str
,
864 ds_file_group
->stream_id
);
867 g_string_free(name
, TRUE
);
869 if (!ds_file_group
->stream
) {
870 BT_LOGE("Cannot create stream for DS file group: "
871 "addr=%p, stream-name=\"%s\"",
872 ds_file_group
, name
->str
);
896 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
902 assert(ctf_fs_trace
);
903 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
904 if (!ctf_fs_trace
->cc_prio_map
) {
909 count
= bt_ctf_trace_get_clock_class_count(
910 ctf_fs_trace
->metadata
->trace
);
913 for (i
= 0; i
< count
; i
++) {
914 struct bt_ctf_clock_class
*clock_class
=
915 bt_ctf_trace_get_clock_class_by_index(
916 ctf_fs_trace
->metadata
->trace
, i
);
919 ret
= bt_clock_class_priority_map_add_clock_class(
920 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
933 struct ctf_fs_trace
*ctf_fs_trace_create(const char *path
, const char *name
,
934 struct ctf_fs_metadata_config
*metadata_config
)
936 struct ctf_fs_trace
*ctf_fs_trace
;
939 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
944 ctf_fs_trace
->path
= g_string_new(path
);
945 if (!ctf_fs_trace
->path
) {
949 ctf_fs_trace
->name
= g_string_new(name
);
950 if (!ctf_fs_trace
->name
) {
954 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
955 if (!ctf_fs_trace
->metadata
) {
959 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
960 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
961 if (!ctf_fs_trace
->ds_file_groups
) {
965 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
, metadata_config
);
970 ret
= create_ds_file_groups(ctf_fs_trace
);
975 ret
= create_cc_prio_map(ctf_fs_trace
);
981 * create_ds_file_groups() created all the streams that this
982 * trace needs. There won't be any more. Therefore it is safe to
983 * make this trace static.
985 (void) bt_ctf_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
990 ctf_fs_trace_destroy(ctf_fs_trace
);
997 int path_is_ctf_trace(const char *path
)
999 GString
*metadata_path
= g_string_new(NULL
);
1002 if (!metadata_path
) {
1007 g_string_printf(metadata_path
, "%s" G_DIR_SEPARATOR_S
"%s", path
, CTF_FS_METADATA_FILENAME
);
1009 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
1015 g_string_free(metadata_path
, TRUE
);
1020 int add_trace_path(GList
**trace_paths
, const char *path
)
1022 GString
*norm_path
= NULL
;
1025 norm_path
= bt_common_normalize_path(path
, NULL
);
1027 BT_LOGE("Failed to normalize path `%s`.", path
);
1032 // FIXME: Remove or ifdef for __MINGW32__
1033 if (strcmp(norm_path
->str
, "/") == 0) {
1034 BT_LOGE("Opening a trace in `/` is not supported.");
1039 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1040 assert(*trace_paths
);
1045 g_string_free(norm_path
, TRUE
);
1052 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
)
1055 GError
*error
= NULL
;
1057 const char *basename
= NULL
;
1059 /* Check if the starting path is a CTF trace itself */
1060 ret
= path_is_ctf_trace(start_path
);
1067 * Stop recursion: a CTF trace cannot contain another
1070 ret
= add_trace_path(trace_paths
, start_path
);
1074 /* Look for subdirectories */
1075 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1076 /* Starting path is not a directory: end of recursion */
1080 dir
= g_dir_open(start_path
, 0, &error
);
1082 if (error
->code
== G_FILE_ERROR_ACCES
) {
1083 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1084 start_path
, error
->message
, error
->code
);
1088 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1089 start_path
, error
->message
, error
->code
);
1094 while ((basename
= g_dir_read_name(dir
))) {
1095 GString
*sub_path
= g_string_new(NULL
);
1102 g_string_printf(sub_path
, "%s" G_DIR_SEPARATOR_S
"%s", start_path
, basename
);
1103 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
);
1104 g_string_free(sub_path
, TRUE
);
1116 g_error_free(error
);
1123 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1124 GList
*trace_names
= NULL
;
1126 const char *last_sep
;
1130 * At this point we know that all the trace paths are
1131 * normalized, and so is the base path. This means that
1132 * they are absolute and they don't end with a separator.
1133 * We can simply find the location of the last separator
1134 * in the base path, which gives us the name of the actual
1135 * directory to look into, and use this location as the
1136 * start of each trace name within each trace path.
1140 * Base path: /home/user/my-traces/some-trace
1142 * - /home/user/my-traces/some-trace/host1/trace1
1143 * - /home/user/my-traces/some-trace/host1/trace2
1144 * - /home/user/my-traces/some-trace/host2/trace
1145 * - /home/user/my-traces/some-trace/other-trace
1147 * In this case the trace names are:
1149 * - some-trace/host1/trace1
1150 * - some-trace/host1/trace2
1151 * - some-trace/host2/trace
1152 * - some-trace/other-trace
1154 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1156 /* We know there's at least one separator */
1159 /* Distance to base */
1160 base_dist
= last_sep
- base_path
+ 1;
1162 /* Create the trace names */
1163 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1164 GString
*trace_name
= g_string_new(NULL
);
1165 GString
*trace_path
= node
->data
;
1168 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1169 trace_names
= g_list_append(trace_names
, trace_name
);
1176 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1177 const char *path_param
)
1179 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1181 GString
*norm_path
= NULL
;
1182 GList
*trace_paths
= NULL
;
1183 GList
*trace_names
= NULL
;
1187 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1189 BT_LOGE("Failed to normalize path: `%s`.",
1194 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
);
1200 BT_LOGE("No CTF traces recursively found in `%s`.",
1205 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1207 BT_LOGE("Cannot create trace names from trace paths.");
1211 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1212 tp_node
= g_list_next(tp_node
),
1213 tn_node
= g_list_next(tn_node
)) {
1214 GString
*trace_path
= tp_node
->data
;
1215 GString
*trace_name
= tn_node
->data
;
1217 ctf_fs_trace
= ctf_fs_trace_create(trace_path
->str
,
1218 trace_name
->str
, &ctf_fs
->metadata_config
);
1219 if (!ctf_fs_trace
) {
1220 BT_LOGE("Cannot create trace for `%s`.",
1225 ret
= create_ports_for_trace(ctf_fs
, ctf_fs_trace
);
1230 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1231 ctf_fs_trace
= NULL
;
1238 ctf_fs_trace_destroy(ctf_fs_trace
);
1241 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1242 if (tp_node
->data
) {
1243 g_string_free(tp_node
->data
, TRUE
);
1247 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1248 if (tn_node
->data
) {
1249 g_string_free(tn_node
->data
, TRUE
);
1254 g_list_free(trace_paths
);
1258 g_list_free(trace_names
);
1262 g_string_free(norm_path
, TRUE
);
1269 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1270 struct bt_value
*params
)
1272 struct ctf_fs_component
*ctf_fs
;
1273 struct bt_value
*value
= NULL
;
1274 const char *path_param
;
1275 enum bt_component_status ret
;
1276 enum bt_value_status value_ret
;
1278 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1283 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1284 assert(ret
== BT_COMPONENT_STATUS_OK
);
1287 * We don't need to get a new reference here because as long as
1288 * our private ctf_fs_component object exists, the containing
1289 * private component should also exist.
1291 ctf_fs
->priv_comp
= priv_comp
;
1292 value
= bt_value_map_get(params
, "path");
1293 if (!bt_value_is_string(value
)) {
1297 value_ret
= bt_value_string_get(value
, &path_param
);
1298 assert(value_ret
== BT_VALUE_STATUS_OK
);
1300 value
= bt_value_map_get(params
, "clock-class-offset-s");
1302 if (!bt_value_is_integer(value
)) {
1303 BT_LOGE("clock-class-offset-s should be an integer");
1306 value_ret
= bt_value_integer_get(value
,
1307 &ctf_fs
->metadata_config
.clock_class_offset_s
);
1308 assert(value_ret
== BT_VALUE_STATUS_OK
);
1312 value
= bt_value_map_get(params
, "clock-class-offset-ns");
1314 if (!bt_value_is_integer(value
)) {
1315 BT_LOGE("clock-class-offset-ns should be an integer");
1318 value_ret
= bt_value_integer_get(value
,
1319 &ctf_fs
->metadata_config
.clock_class_offset_ns
);
1320 assert(value_ret
== BT_VALUE_STATUS_OK
);
1324 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1325 if (!ctf_fs
->port_data
) {
1329 ctf_fs
->traces
= g_ptr_array_new_with_free_func(
1330 ctf_fs_trace_destroy_notifier
);
1331 if (!ctf_fs
->traces
) {
1335 if (create_ctf_fs_traces(ctf_fs
, path_param
)) {
1342 ctf_fs_destroy(ctf_fs
);
1344 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1345 assert(ret
== BT_COMPONENT_STATUS_OK
);
1353 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1354 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1356 struct ctf_fs_component
*ctf_fs
;
1357 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1359 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1361 ret
= BT_COMPONENT_STATUS_ERROR
;
1368 struct bt_component_class_query_return
ctf_fs_query(
1369 struct bt_component_class
*comp_class
,
1370 struct bt_query_executor
*query_exec
,
1371 const char *object
, struct bt_value
*params
)
1373 struct bt_component_class_query_return ret
= {
1375 .status
= BT_QUERY_STATUS_OK
,
1378 if (!strcmp(object
, "metadata-info")) {
1379 ret
= metadata_info_query(comp_class
, params
);
1380 } else if (!strcmp(object
, "trace-info")) {
1381 ret
= trace_info_query(comp_class
, params
);
1383 BT_LOGE("Unknown query object `%s`", object
);
1384 ret
.status
= BT_QUERY_STATUS_INVALID_OBJECT
;