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/babeltrace.h>
30 #include <plugins-common.h>
32 #include <babeltrace/assert-internal.h>
37 #include "data-stream-file.h"
39 #include "../common/metadata/decoder.h"
40 #include "../common/notif-iter/notif-iter.h"
41 #include "../common/utils/utils.h"
44 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
48 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data
*notif_iter_data
)
50 struct ctf_fs_ds_file_info
*ds_file_info
;
53 BT_ASSERT(notif_iter_data
->ds_file_info_index
<
54 notif_iter_data
->ds_file_group
->ds_file_infos
->len
);
55 ds_file_info
= g_ptr_array_index(
56 notif_iter_data
->ds_file_group
->ds_file_infos
,
57 notif_iter_data
->ds_file_info_index
);
59 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
60 notif_iter_data
->ds_file
= ctf_fs_ds_file_create(
61 notif_iter_data
->ds_file_group
->ctf_fs_trace
,
62 notif_iter_data
->notif_iter
,
63 notif_iter_data
->ds_file_group
->stream
,
64 ds_file_info
->path
->str
);
65 if (!notif_iter_data
->ds_file
) {
73 void ctf_fs_notif_iter_data_destroy(
74 struct ctf_fs_notif_iter_data
*notif_iter_data
)
76 if (!notif_iter_data
) {
80 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
82 if (notif_iter_data
->notif_iter
) {
83 bt_notif_iter_destroy(notif_iter_data
->notif_iter
);
86 g_free(notif_iter_data
);
89 struct bt_notification_iterator_next_method_return
ctf_fs_iterator_next(
90 struct bt_private_connection_private_notification_iterator
*iterator
)
92 struct bt_notification_iterator_next_method_return next_ret
;
93 struct ctf_fs_notif_iter_data
*notif_iter_data
=
94 bt_private_connection_private_notification_iterator_get_user_data(iterator
);
97 BT_ASSERT(notif_iter_data
->ds_file
);
98 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
100 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
&&
101 bt_notification_get_type(next_ret
.notification
) ==
102 BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
103 if (notif_iter_data
->skip_stream_begin_notifs
) {
105 * We already emitted a
106 * BT_NOTIFICATION_TYPE_STREAM_BEGIN
107 * notification: skip this one, get a new one.
109 BT_PUT(next_ret
.notification
);
110 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
111 BT_ASSERT(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
115 * First BT_NOTIFICATION_TYPE_STREAM_BEGIN
116 * notification: skip all following.
118 notif_iter_data
->skip_stream_begin_notifs
= true;
123 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
&&
124 bt_notification_get_type(next_ret
.notification
) ==
125 BT_NOTIFICATION_TYPE_STREAM_END
) {
126 notif_iter_data
->ds_file_info_index
++;
128 if (notif_iter_data
->ds_file_info_index
==
129 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
131 * No more stream files to read: we reached the
132 * real end. Emit this
133 * BT_NOTIFICATION_TYPE_STREAM_END notification.
134 * The next time ctf_fs_iterator_next() is
135 * called for this notification iterator,
136 * ctf_fs_ds_file_next() will return
137 * BT_NOTIFICATION_ITERATOR_STATUS_END().
142 BT_PUT(next_ret
.notification
);
143 bt_notif_iter_reset(notif_iter_data
->notif_iter
);
146 * Open and start reading the next stream file within
147 * our stream file group.
149 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
151 next_ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
155 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
158 * If we get a notification, we expect to get a
159 * BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
160 * because the iterator's state machine emits one before
161 * even requesting the first block of data from the
162 * medium. Skip this notification because we're not
163 * really starting a new stream here, and try getting a
164 * new notification (which, if it works, is a
165 * BT_NOTIFICATION_TYPE_PACKET_BEGIN one). We're sure to
166 * get at least one pair of
167 * BT_NOTIFICATION_TYPE_PACKET_BEGIN and
168 * BT_NOTIFICATION_TYPE_PACKET_END notifications in the
169 * case of a single, empty packet. We know there's at
170 * least one packet because the stream file group does
171 * not contain empty stream files.
173 BT_ASSERT(notif_iter_data
->skip_stream_begin_notifs
);
175 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
176 BT_ASSERT(bt_notification_get_type(next_ret
.notification
) ==
177 BT_NOTIFICATION_TYPE_STREAM_BEGIN
);
178 BT_PUT(next_ret
.notification
);
179 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
180 BT_ASSERT(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
188 void ctf_fs_iterator_finalize(struct bt_private_connection_private_notification_iterator
*it
)
190 void *notif_iter_data
=
191 bt_private_connection_private_notification_iterator_get_user_data(it
);
193 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
196 enum bt_notification_iterator_status
ctf_fs_iterator_init(
197 struct bt_private_connection_private_notification_iterator
*it
,
198 struct bt_private_port
*port
)
200 struct ctf_fs_port_data
*port_data
;
201 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
202 enum bt_notification_iterator_status ret
=
203 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
206 port_data
= bt_private_port_get_user_data(port
);
208 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
212 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
213 if (!notif_iter_data
) {
214 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
218 notif_iter_data
->notif_iter
= bt_notif_iter_create(
219 port_data
->ds_file_group
->ctf_fs_trace
->metadata
->trace
,
220 bt_common_get_page_size() * 8,
221 ctf_fs_ds_file_medops
, NULL
);
222 if (!notif_iter_data
->notif_iter
) {
223 BT_LOGE_STR("Cannot create a CTF notification iterator.");
224 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
228 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
229 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
231 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
235 ret
= bt_private_connection_private_notification_iterator_set_user_data(it
, notif_iter_data
);
236 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
240 notif_iter_data
= NULL
;
244 (void) bt_private_connection_private_notification_iterator_set_user_data(it
, NULL
);
247 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
252 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
258 if (ctf_fs
->traces
) {
259 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
262 if (ctf_fs
->port_data
) {
263 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
270 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
276 if (ctf_fs_trace
->ds_file_groups
) {
277 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
280 if (ctf_fs_trace
->path
) {
281 g_string_free(ctf_fs_trace
->path
, TRUE
);
284 if (ctf_fs_trace
->name
) {
285 g_string_free(ctf_fs_trace
->name
, TRUE
);
288 if (ctf_fs_trace
->metadata
) {
289 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
290 g_free(ctf_fs_trace
->metadata
);
293 bt_put(ctf_fs_trace
->cc_prio_map
);
294 g_free(ctf_fs_trace
);
298 void ctf_fs_trace_destroy_notifier(void *data
)
300 struct ctf_fs_trace
*trace
= data
;
301 ctf_fs_trace_destroy(trace
);
304 void ctf_fs_finalize(struct bt_private_component
*component
)
306 void *data
= bt_private_component_get_user_data(component
);
308 ctf_fs_destroy(data
);
312 void port_data_destroy(void *data
) {
313 struct ctf_fs_port_data
*port_data
= data
;
323 GString
*get_stream_instance_unique_name(
324 struct ctf_fs_ds_file_group
*ds_file_group
)
327 struct ctf_fs_ds_file_info
*ds_file_info
;
329 name
= g_string_new(NULL
);
335 * If there's more than one stream file in the stream file
336 * group, the first (earliest) stream file's path is used as
337 * the stream's unique name.
339 BT_ASSERT(ds_file_group
->ds_file_infos
->len
> 0);
340 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
341 g_string_assign(name
, ds_file_info
->path
->str
);
348 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
349 struct ctf_fs_trace
*ctf_fs_trace
,
350 struct ctf_fs_ds_file_group
*ds_file_group
)
353 struct ctf_fs_port_data
*port_data
= NULL
;
354 GString
*port_name
= NULL
;
356 port_name
= get_stream_instance_unique_name(ds_file_group
);
361 BT_LOGD("Creating one port named `%s`", port_name
->str
);
363 /* Create output port for this file */
364 port_data
= g_new0(struct ctf_fs_port_data
, 1);
369 port_data
->ds_file_group
= ds_file_group
;
370 ret
= bt_private_component_source_add_output_private_port(
371 ctf_fs
->priv_comp
, port_name
->str
, port_data
, NULL
);
376 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
385 g_string_free(port_name
, TRUE
);
388 port_data_destroy(port_data
);
393 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
394 struct ctf_fs_trace
*ctf_fs_trace
)
399 /* Create one output port for each stream file group */
400 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
401 struct ctf_fs_ds_file_group
*ds_file_group
=
402 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
404 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
407 BT_LOGE("Cannot create output port.");
417 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
418 struct bt_field
*packet_header_field
)
420 struct bt_field
*stream_instance_id_field
= NULL
;
421 uint64_t stream_instance_id
= -1ULL;
424 if (!packet_header_field
) {
428 stream_instance_id_field
= bt_field_structure_borrow_field_by_name(
429 packet_header_field
, "stream_instance_id");
430 if (!stream_instance_id_field
) {
434 ret
= bt_field_integer_unsigned_get_value(stream_instance_id_field
,
435 &stream_instance_id
);
437 stream_instance_id
= -1ULL;
442 return stream_instance_id
;
445 uint64_t get_packet_context_timestamp_begin_ns(
446 struct ctf_fs_trace
*ctf_fs_trace
,
447 struct bt_field
*packet_context_field
)
450 struct bt_field
*timestamp_begin_field
= NULL
;
451 struct bt_field_type
*timestamp_begin_ft
= NULL
;
452 uint64_t timestamp_begin_raw_value
= -1ULL;
453 uint64_t timestamp_begin_ns
= -1ULL;
454 int64_t timestamp_begin_ns_signed
;
455 struct bt_clock_class
*timestamp_begin_clock_class
= NULL
;
457 if (!packet_context_field
) {
461 timestamp_begin_field
= bt_field_structure_borrow_field_by_name(
462 packet_context_field
, "timestamp_begin");
463 if (!timestamp_begin_field
) {
467 timestamp_begin_ft
= bt_field_borrow_type(timestamp_begin_field
);
468 BT_ASSERT(timestamp_begin_ft
);
469 timestamp_begin_clock_class
=
470 bt_field_type_integer_borrow_mapped_clock_class(timestamp_begin_ft
);
471 if (!timestamp_begin_clock_class
) {
475 ret
= bt_field_integer_unsigned_get_value(timestamp_begin_field
,
476 ×tamp_begin_raw_value
);
481 ret
= bt_clock_class_cycles_to_ns(timestamp_begin_clock_class
,
482 timestamp_begin_raw_value
, ×tamp_begin_ns_signed
);
487 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
490 return timestamp_begin_ns
;
494 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
500 if (ds_file_info
->path
) {
501 g_string_free(ds_file_info
->path
, TRUE
);
504 ctf_fs_ds_index_destroy(ds_file_info
->index
);
505 g_free(ds_file_info
);
509 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
510 uint64_t begin_ns
, struct ctf_fs_ds_index
*index
)
512 struct ctf_fs_ds_file_info
*ds_file_info
;
514 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
519 ds_file_info
->path
= g_string_new(path
);
520 if (!ds_file_info
->path
) {
521 ctf_fs_ds_file_info_destroy(ds_file_info
);
526 ds_file_info
->begin_ns
= begin_ns
;
527 ds_file_info
->index
= index
;
531 ctf_fs_ds_index_destroy(index
);
536 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
538 if (!ds_file_group
) {
542 if (ds_file_group
->ds_file_infos
) {
543 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
546 bt_put(ds_file_group
->stream
);
547 bt_put(ds_file_group
->stream_class
);
548 g_free(ds_file_group
);
552 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
553 struct ctf_fs_trace
*ctf_fs_trace
,
554 struct bt_stream_class
*stream_class
,
555 uint64_t stream_instance_id
)
557 struct ctf_fs_ds_file_group
*ds_file_group
;
559 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
560 if (!ds_file_group
) {
564 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
565 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
566 if (!ds_file_group
->ds_file_infos
) {
570 ds_file_group
->stream_id
= stream_instance_id
;
571 BT_ASSERT(stream_class
);
572 ds_file_group
->stream_class
= bt_get(stream_class
);
573 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
577 ctf_fs_ds_file_group_destroy(ds_file_group
);
578 ds_file_group
= NULL
;
581 return ds_file_group
;
584 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
586 void array_insert(GPtrArray
*array
, gpointer element
, size_t pos
)
588 size_t original_array_len
= array
->len
;
590 /* Allocate an unused element at the end of the array. */
591 g_ptr_array_add(array
, NULL
);
593 /* If we are not inserting at the end, move the elements by one. */
594 if (pos
< original_array_len
) {
595 memmove(&(array
->pdata
[pos
+ 1]),
596 &(array
->pdata
[pos
]),
597 (original_array_len
- pos
) * sizeof(gpointer
));
600 /* Insert the value and bump the array len */
601 array
->pdata
[pos
] = element
;
605 int ctf_fs_ds_file_group_add_ds_file_info(
606 struct ctf_fs_ds_file_group
*ds_file_group
,
607 const char *path
, uint64_t begin_ns
,
608 struct ctf_fs_ds_index
*index
)
610 struct ctf_fs_ds_file_info
*ds_file_info
;
614 /* Onwership of index is transferred. */
615 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
, index
);
621 /* Find a spot to insert this one */
622 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
623 struct ctf_fs_ds_file_info
*other_ds_file_info
=
624 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
626 if (begin_ns
< other_ds_file_info
->begin_ns
) {
631 array_insert(ds_file_group
->ds_file_infos
, ds_file_info
, i
);
636 ctf_fs_ds_file_info_destroy(ds_file_info
);
637 ctf_fs_ds_index_destroy(index
);
644 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
647 struct bt_field
*packet_header_field
= NULL
;
648 struct bt_field
*packet_context_field
= NULL
;
649 struct bt_stream_class
*stream_class
= NULL
;
650 uint64_t stream_instance_id
= -1ULL;
651 uint64_t begin_ns
= -1ULL;
652 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
653 bool add_group
= false;
656 struct ctf_fs_ds_file
*ds_file
= NULL
;
657 struct ctf_fs_ds_index
*index
= NULL
;
658 struct bt_notif_iter
*notif_iter
= NULL
;
660 notif_iter
= bt_notif_iter_create(ctf_fs_trace
->metadata
->trace
,
661 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops
, NULL
);
663 BT_LOGE_STR("Cannot create a CTF notification iterator.");
667 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, notif_iter
, NULL
, path
);
672 ret
= ctf_fs_ds_file_borrow_packet_header_context_fields(ds_file
,
673 &packet_header_field
, &packet_context_field
);
675 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
680 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
681 packet_header_field
);
682 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
683 packet_context_field
);
684 stream_class
= ctf_utils_borrow_stream_class_from_packet_header(
685 ctf_fs_trace
->metadata
->trace
, packet_header_field
);
690 index
= ctf_fs_ds_file_build_index(ds_file
);
692 BT_LOGW("Failed to index CTF stream file \'%s\'",
693 ds_file
->file
->path
->str
);
696 if (begin_ns
== -1ULL) {
698 * No beggining timestamp to sort the stream files
699 * within a stream file group, so consider that this
700 * file must be the only one within its group.
702 stream_instance_id
= -1ULL;
705 if (stream_instance_id
== -1ULL) {
707 * No stream instance ID or no beginning timestamp:
708 * create a unique stream file group for this stream
709 * file because, even if there's a stream instance ID,
710 * there's no timestamp to order the file within its
713 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
714 stream_class
, stream_instance_id
);
715 if (!ds_file_group
) {
719 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
720 path
, begin_ns
, index
);
721 /* Ownership of index is transferred. */
731 BT_ASSERT(stream_instance_id
!= -1ULL);
732 BT_ASSERT(begin_ns
!= -1ULL);
734 /* Find an existing stream file group with this ID */
735 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
736 ds_file_group
= g_ptr_array_index(
737 ctf_fs_trace
->ds_file_groups
, i
);
739 if (ds_file_group
->stream_class
== stream_class
&&
740 ds_file_group
->stream_id
==
741 stream_instance_id
) {
745 ds_file_group
= NULL
;
748 if (!ds_file_group
) {
749 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
750 stream_class
, stream_instance_id
);
751 if (!ds_file_group
) {
758 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
, path
,
768 ctf_fs_ds_file_group_destroy(ds_file_group
);
772 if (add_group
&& ds_file_group
) {
773 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
776 ctf_fs_ds_file_destroy(ds_file
);
779 bt_notif_iter_destroy(notif_iter
);
782 ctf_fs_ds_index_destroy(index
);
787 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
790 const char *basename
;
791 GError
*error
= NULL
;
795 /* Check each file in the path directory, except specific ones */
796 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
798 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
799 ctf_fs_trace
->path
->str
, error
->message
,
804 while ((basename
= g_dir_read_name(dir
))) {
805 struct ctf_fs_file
*file
;
807 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
808 /* Ignore the metadata stream. */
809 BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S
"%s`",
810 ctf_fs_trace
->path
->str
, basename
);
814 if (basename
[0] == '.') {
815 BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S
"%s`",
816 ctf_fs_trace
->path
->str
, basename
);
820 /* Create the file. */
821 file
= ctf_fs_file_create();
823 BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
"%s`",
824 ctf_fs_trace
->path
->str
, basename
);
828 /* Create full path string. */
829 g_string_append_printf(file
->path
, "%s" G_DIR_SEPARATOR_S
"%s",
830 ctf_fs_trace
->path
->str
, basename
);
831 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
832 BT_LOGD("Ignoring non-regular file `%s`",
834 ctf_fs_file_destroy(file
);
839 ret
= ctf_fs_file_open(file
, "rb");
841 BT_LOGE("Cannot open stream file `%s`", file
->path
->str
);
845 if (file
->size
== 0) {
846 /* Skip empty stream. */
847 BT_LOGD("Ignoring empty file `%s`", file
->path
->str
);
848 ctf_fs_file_destroy(file
);
852 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
855 BT_LOGE("Cannot add stream file `%s` to stream file group",
857 ctf_fs_file_destroy(file
);
861 ctf_fs_file_destroy(file
);
865 * At this point, DS file groupes are created, but their
866 * associated stream objects do not exist yet. This is because
867 * we need to name the created stream object with the data
868 * stream file's path. We have everything we need here to do
871 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
872 struct ctf_fs_ds_file_group
*ds_file_group
=
873 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
874 GString
*name
= get_stream_instance_unique_name(ds_file_group
);
880 if (ds_file_group
->stream_id
== -1ULL) {
881 /* No stream ID: use 0 */
882 ds_file_group
->stream
= bt_stream_create(
883 ds_file_group
->stream_class
, name
->str
,
884 ctf_fs_trace
->next_stream_id
);
885 ctf_fs_trace
->next_stream_id
++;
887 /* Specific stream ID */
888 ds_file_group
->stream
= bt_stream_create(
889 ds_file_group
->stream_class
, name
->str
,
890 ds_file_group
->stream_id
);
893 g_string_free(name
, TRUE
);
895 if (!ds_file_group
->stream
) {
896 BT_LOGE("Cannot create stream for DS file group: "
897 "addr=%p, stream-name=\"%s\"",
898 ds_file_group
, name
->str
);
922 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
928 BT_ASSERT(ctf_fs_trace
);
929 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
930 if (!ctf_fs_trace
->cc_prio_map
) {
935 count
= bt_trace_get_clock_class_count(
936 ctf_fs_trace
->metadata
->trace
);
937 BT_ASSERT(count
>= 0);
939 for (i
= 0; i
< count
; i
++) {
940 struct bt_clock_class
*clock_class
=
941 bt_trace_borrow_clock_class_by_index(
942 ctf_fs_trace
->metadata
->trace
, i
);
944 BT_ASSERT(clock_class
);
945 ret
= bt_clock_class_priority_map_add_clock_class(
946 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
958 struct ctf_fs_trace
*ctf_fs_trace_create(const char *path
, const char *name
,
959 struct ctf_fs_metadata_config
*metadata_config
)
961 struct ctf_fs_trace
*ctf_fs_trace
;
964 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
969 ctf_fs_trace
->path
= g_string_new(path
);
970 if (!ctf_fs_trace
->path
) {
974 ctf_fs_trace
->name
= g_string_new(name
);
975 if (!ctf_fs_trace
->name
) {
979 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
980 if (!ctf_fs_trace
->metadata
) {
984 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
985 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
986 if (!ctf_fs_trace
->ds_file_groups
) {
990 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
, metadata_config
);
995 ret
= create_ds_file_groups(ctf_fs_trace
);
1000 ret
= create_cc_prio_map(ctf_fs_trace
);
1006 * create_ds_file_groups() created all the streams that this
1007 * trace needs. There won't be any more. Therefore it is safe to
1008 * make this trace static.
1010 (void) bt_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
1015 ctf_fs_trace_destroy(ctf_fs_trace
);
1016 ctf_fs_trace
= NULL
;
1018 return ctf_fs_trace
;
1022 int path_is_ctf_trace(const char *path
)
1024 GString
*metadata_path
= g_string_new(NULL
);
1027 if (!metadata_path
) {
1032 g_string_printf(metadata_path
, "%s" G_DIR_SEPARATOR_S
"%s", path
, CTF_FS_METADATA_FILENAME
);
1034 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
1040 g_string_free(metadata_path
, TRUE
);
1045 int add_trace_path(GList
**trace_paths
, const char *path
)
1047 GString
*norm_path
= NULL
;
1050 norm_path
= bt_common_normalize_path(path
, NULL
);
1052 BT_LOGE("Failed to normalize path `%s`.", path
);
1057 // FIXME: Remove or ifdef for __MINGW32__
1058 if (strcmp(norm_path
->str
, "/") == 0) {
1059 BT_LOGE("Opening a trace in `/` is not supported.");
1064 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1065 BT_ASSERT(*trace_paths
);
1070 g_string_free(norm_path
, TRUE
);
1077 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
)
1080 GError
*error
= NULL
;
1082 const char *basename
= NULL
;
1084 /* Check if the starting path is a CTF trace itself */
1085 ret
= path_is_ctf_trace(start_path
);
1092 * Stop recursion: a CTF trace cannot contain another
1095 ret
= add_trace_path(trace_paths
, start_path
);
1099 /* Look for subdirectories */
1100 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1101 /* Starting path is not a directory: end of recursion */
1105 dir
= g_dir_open(start_path
, 0, &error
);
1107 if (error
->code
== G_FILE_ERROR_ACCES
) {
1108 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1109 start_path
, error
->message
, error
->code
);
1113 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1114 start_path
, error
->message
, error
->code
);
1119 while ((basename
= g_dir_read_name(dir
))) {
1120 GString
*sub_path
= g_string_new(NULL
);
1127 g_string_printf(sub_path
, "%s" G_DIR_SEPARATOR_S
"%s", start_path
, basename
);
1128 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
);
1129 g_string_free(sub_path
, TRUE
);
1141 g_error_free(error
);
1148 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1149 GList
*trace_names
= NULL
;
1151 const char *last_sep
;
1155 * At this point we know that all the trace paths are
1156 * normalized, and so is the base path. This means that
1157 * they are absolute and they don't end with a separator.
1158 * We can simply find the location of the last separator
1159 * in the base path, which gives us the name of the actual
1160 * directory to look into, and use this location as the
1161 * start of each trace name within each trace path.
1165 * Base path: /home/user/my-traces/some-trace
1167 * - /home/user/my-traces/some-trace/host1/trace1
1168 * - /home/user/my-traces/some-trace/host1/trace2
1169 * - /home/user/my-traces/some-trace/host2/trace
1170 * - /home/user/my-traces/some-trace/other-trace
1172 * In this case the trace names are:
1174 * - some-trace/host1/trace1
1175 * - some-trace/host1/trace2
1176 * - some-trace/host2/trace
1177 * - some-trace/other-trace
1179 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1181 /* We know there's at least one separator */
1182 BT_ASSERT(last_sep
);
1184 /* Distance to base */
1185 base_dist
= last_sep
- base_path
+ 1;
1187 /* Create the trace names */
1188 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1189 GString
*trace_name
= g_string_new(NULL
);
1190 GString
*trace_path
= node
->data
;
1192 BT_ASSERT(trace_name
);
1193 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1194 trace_names
= g_list_append(trace_names
, trace_name
);
1201 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1202 const char *path_param
)
1204 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1206 GString
*norm_path
= NULL
;
1207 GList
*trace_paths
= NULL
;
1208 GList
*trace_names
= NULL
;
1212 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1214 BT_LOGE("Failed to normalize path: `%s`.",
1219 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
);
1225 BT_LOGE("No CTF traces recursively found in `%s`.",
1230 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1232 BT_LOGE("Cannot create trace names from trace paths.");
1236 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1237 tp_node
= g_list_next(tp_node
),
1238 tn_node
= g_list_next(tn_node
)) {
1239 GString
*trace_path
= tp_node
->data
;
1240 GString
*trace_name
= tn_node
->data
;
1242 ctf_fs_trace
= ctf_fs_trace_create(trace_path
->str
,
1243 trace_name
->str
, &ctf_fs
->metadata_config
);
1244 if (!ctf_fs_trace
) {
1245 BT_LOGE("Cannot create trace for `%s`.",
1250 ret
= create_ports_for_trace(ctf_fs
, ctf_fs_trace
);
1255 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1256 ctf_fs_trace
= NULL
;
1263 ctf_fs_trace_destroy(ctf_fs_trace
);
1266 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1267 if (tp_node
->data
) {
1268 g_string_free(tp_node
->data
, TRUE
);
1272 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1273 if (tn_node
->data
) {
1274 g_string_free(tn_node
->data
, TRUE
);
1279 g_list_free(trace_paths
);
1283 g_list_free(trace_names
);
1287 g_string_free(norm_path
, TRUE
);
1294 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1295 struct bt_value
*params
)
1297 struct ctf_fs_component
*ctf_fs
;
1298 struct bt_value
*value
= NULL
;
1299 const char *path_param
;
1300 enum bt_component_status ret
;
1301 enum bt_value_status value_ret
;
1303 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1308 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1309 BT_ASSERT(ret
== BT_COMPONENT_STATUS_OK
);
1312 * We don't need to get a new reference here because as long as
1313 * our private ctf_fs_component object exists, the containing
1314 * private component should also exist.
1316 ctf_fs
->priv_comp
= priv_comp
;
1317 value
= bt_value_map_borrow(params
, "path");
1318 if (value
&& !bt_value_is_string(value
)) {
1322 value_ret
= bt_value_string_get(value
, &path_param
);
1323 BT_ASSERT(value_ret
== BT_VALUE_STATUS_OK
);
1324 value
= bt_value_map_borrow(params
, "clock-class-offset-s");
1326 if (!bt_value_is_integer(value
)) {
1327 BT_LOGE("clock-class-offset-s should be an integer");
1330 value_ret
= bt_value_integer_get(value
,
1331 &ctf_fs
->metadata_config
.clock_class_offset_s
);
1332 BT_ASSERT(value_ret
== BT_VALUE_STATUS_OK
);
1335 value
= bt_value_map_borrow(params
, "clock-class-offset-ns");
1337 if (!bt_value_is_integer(value
)) {
1338 BT_LOGE("clock-class-offset-ns should be an integer");
1341 value_ret
= bt_value_integer_get(value
,
1342 &ctf_fs
->metadata_config
.clock_class_offset_ns
);
1343 BT_ASSERT(value_ret
== BT_VALUE_STATUS_OK
);
1346 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1347 if (!ctf_fs
->port_data
) {
1351 ctf_fs
->traces
= g_ptr_array_new_with_free_func(
1352 ctf_fs_trace_destroy_notifier
);
1353 if (!ctf_fs
->traces
) {
1357 if (create_ctf_fs_traces(ctf_fs
, path_param
)) {
1364 ctf_fs_destroy(ctf_fs
);
1366 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1367 BT_ASSERT(ret
== BT_COMPONENT_STATUS_OK
);
1374 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1375 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1377 struct ctf_fs_component
*ctf_fs
;
1378 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1380 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1382 ret
= BT_COMPONENT_STATUS_ERROR
;
1389 struct bt_component_class_query_method_return
ctf_fs_query(
1390 struct bt_component_class
*comp_class
,
1391 struct bt_query_executor
*query_exec
,
1392 const char *object
, struct bt_value
*params
)
1394 struct bt_component_class_query_method_return ret
= {
1396 .status
= BT_QUERY_STATUS_OK
,
1399 if (!strcmp(object
, "metadata-info")) {
1400 ret
= metadata_info_query(comp_class
, params
);
1401 } else if (!strcmp(object
, "trace-info")) {
1402 ret
= trace_info_query(comp_class
, params
);
1404 BT_LOGE("Unknown query object `%s`", object
);
1405 ret
.status
= BT_QUERY_STATUS_INVALID_OBJECT
;