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/msg-iter/msg-iter.h"
41 #include "../common/utils/utils.h"
44 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
48 int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data
*msg_iter_data
)
50 struct ctf_fs_ds_file_info
*ds_file_info
;
53 BT_ASSERT(msg_iter_data
->ds_file_info_index
<
54 msg_iter_data
->ds_file_group
->ds_file_infos
->len
);
55 ds_file_info
= g_ptr_array_index(
56 msg_iter_data
->ds_file_group
->ds_file_infos
,
57 msg_iter_data
->ds_file_info_index
);
59 ctf_fs_ds_file_destroy(msg_iter_data
->ds_file
);
60 msg_iter_data
->ds_file
= ctf_fs_ds_file_create(
61 msg_iter_data
->ds_file_group
->ctf_fs_trace
,
62 msg_iter_data
->pc_msg_iter
,
63 msg_iter_data
->msg_iter
,
64 msg_iter_data
->ds_file_group
->stream
,
65 ds_file_info
->path
->str
);
66 if (!msg_iter_data
->ds_file
) {
74 void ctf_fs_msg_iter_data_destroy(
75 struct ctf_fs_msg_iter_data
*msg_iter_data
)
81 ctf_fs_ds_file_destroy(msg_iter_data
->ds_file
);
83 if (msg_iter_data
->msg_iter
) {
84 bt_msg_iter_destroy(msg_iter_data
->msg_iter
);
87 g_free(msg_iter_data
);
91 bt_self_message_iterator_status
ctf_fs_iterator_next_one(
92 struct ctf_fs_msg_iter_data
*msg_iter_data
,
93 const bt_message
**msg
)
95 bt_self_message_iterator_status status
;
99 BT_ASSERT(msg_iter_data
->ds_file
);
100 status
= ctf_fs_ds_file_next(msg_iter_data
->ds_file
, &priv_msg
);
103 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
&&
104 bt_message_get_type(*msg
) ==
105 BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
106 if (msg_iter_data
->skip_stream_begin_msgs
) {
108 * We already emitted a
109 * BT_MESSAGE_TYPE_STREAM_BEGINNING
110 * message: skip this one, get a new one.
112 BT_MESSAGE_PUT_REF_AND_RESET(*msg
);
113 status
= ctf_fs_ds_file_next(msg_iter_data
->ds_file
,
116 BT_ASSERT(status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_END
);
120 * First BT_MESSAGE_TYPE_STREAM_BEGINNING
121 * message: skip all following.
123 msg_iter_data
->skip_stream_begin_msgs
= true;
128 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
&&
129 bt_message_get_type(*msg
) ==
130 BT_MESSAGE_TYPE_STREAM_END
) {
131 msg_iter_data
->ds_file_info_index
++;
133 if (msg_iter_data
->ds_file_info_index
==
134 msg_iter_data
->ds_file_group
->ds_file_infos
->len
) {
136 * No more stream files to read: we reached the
137 * real end. Emit this
138 * BT_MESSAGE_TYPE_STREAM_END message.
139 * The next time ctf_fs_iterator_next() is
140 * called for this message iterator,
141 * ctf_fs_ds_file_next() will return
142 * BT_SELF_MESSAGE_ITERATOR_STATUS_END().
147 BT_MESSAGE_PUT_REF_AND_RESET(*msg
);
148 bt_msg_iter_reset(msg_iter_data
->msg_iter
);
151 * Open and start reading the next stream file within
152 * our stream file group.
154 ret
= msg_iter_data_set_current_ds_file(msg_iter_data
);
156 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
160 status
= ctf_fs_ds_file_next(msg_iter_data
->ds_file
, &priv_msg
);
164 * If we get a message, we expect to get a
165 * BT_MESSAGE_TYPE_STREAM_BEGINNING message
166 * because the iterator's state machine emits one before
167 * even requesting the first block of data from the
168 * medium. Skip this message because we're not
169 * really starting a new stream here, and try getting a
170 * new message (which, if it works, is a
171 * BT_MESSAGE_TYPE_PACKET_BEGINNING one). We're sure to
172 * get at least one pair of
173 * BT_MESSAGE_TYPE_PACKET_BEGINNING and
174 * BT_MESSAGE_TYPE_PACKET_END messages in the
175 * case of a single, empty packet. We know there's at
176 * least one packet because the stream file group does
177 * not contain empty stream files.
179 BT_ASSERT(msg_iter_data
->skip_stream_begin_msgs
);
181 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
182 BT_ASSERT(bt_message_get_type(*msg
) ==
183 BT_MESSAGE_TYPE_STREAM_BEGINNING
);
184 BT_MESSAGE_PUT_REF_AND_RESET(*msg
);
185 status
= ctf_fs_ds_file_next(msg_iter_data
->ds_file
,
188 BT_ASSERT(status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_END
);
197 bt_self_message_iterator_status
ctf_fs_iterator_next(
198 bt_self_message_iterator
*iterator
,
199 bt_message_array_const msgs
, uint64_t capacity
,
202 bt_self_message_iterator_status status
=
203 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
204 struct ctf_fs_msg_iter_data
*msg_iter_data
=
205 bt_self_message_iterator_get_data(iterator
);
208 while (i
< capacity
&& status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
209 status
= ctf_fs_iterator_next_one(msg_iter_data
, &msgs
[i
]);
210 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
217 * Even if ctf_fs_iterator_next_one() returned something
218 * else than BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
219 * accumulated message objects in the output
220 * message array, so we need to return
221 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they are
222 * transfered to downstream. This other status occurs
223 * again the next time muxer_msg_iter_do_next() is
224 * called, possibly without any accumulated
225 * message, in which case we'll return it.
228 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
234 void ctf_fs_iterator_finalize(bt_self_message_iterator
*it
)
236 ctf_fs_msg_iter_data_destroy(
237 bt_self_message_iterator_get_data(it
));
240 bt_self_message_iterator_status
ctf_fs_iterator_init(
241 bt_self_message_iterator
*self_msg_iter
,
242 bt_self_component_source
*self_comp
,
243 bt_self_component_port_output
*self_port
)
245 struct ctf_fs_port_data
*port_data
;
246 struct ctf_fs_msg_iter_data
*msg_iter_data
= NULL
;
247 bt_self_message_iterator_status ret
=
248 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
251 port_data
= bt_self_component_port_get_data(
252 bt_self_component_port_output_as_self_component_port(
254 BT_ASSERT(port_data
);
255 msg_iter_data
= g_new0(struct ctf_fs_msg_iter_data
, 1);
256 if (!msg_iter_data
) {
257 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
261 msg_iter_data
->pc_msg_iter
= self_msg_iter
;
262 msg_iter_data
->msg_iter
= bt_msg_iter_create(
263 port_data
->ds_file_group
->ctf_fs_trace
->metadata
->tc
,
264 bt_common_get_page_size() * 8,
265 ctf_fs_ds_file_medops
, NULL
);
266 if (!msg_iter_data
->msg_iter
) {
267 BT_LOGE_STR("Cannot create a CTF message iterator.");
268 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
272 msg_iter_data
->ds_file_group
= port_data
->ds_file_group
;
273 iret
= msg_iter_data_set_current_ds_file(msg_iter_data
);
275 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
279 bt_self_message_iterator_set_data(self_msg_iter
,
281 if (ret
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
285 msg_iter_data
= NULL
;
289 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
292 ctf_fs_msg_iter_data_destroy(msg_iter_data
);
297 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
303 if (ctf_fs
->traces
) {
304 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
307 if (ctf_fs
->port_data
) {
308 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
315 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
321 if (ctf_fs_trace
->ds_file_groups
) {
322 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
325 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace
->trace
);
327 if (ctf_fs_trace
->path
) {
328 g_string_free(ctf_fs_trace
->path
, TRUE
);
331 if (ctf_fs_trace
->name
) {
332 g_string_free(ctf_fs_trace
->name
, TRUE
);
335 if (ctf_fs_trace
->metadata
) {
336 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
337 g_free(ctf_fs_trace
->metadata
);
340 g_free(ctf_fs_trace
);
344 void ctf_fs_trace_destroy_msgier(void *data
)
346 struct ctf_fs_trace
*trace
= data
;
347 ctf_fs_trace_destroy(trace
);
350 void ctf_fs_finalize(bt_self_component_source
*component
)
352 ctf_fs_destroy(bt_self_component_get_data(
353 bt_self_component_source_as_self_component(component
)));
357 void port_data_destroy(void *data
) {
358 struct ctf_fs_port_data
*port_data
= data
;
368 GString
*get_stream_instance_unique_name(
369 struct ctf_fs_ds_file_group
*ds_file_group
)
372 struct ctf_fs_ds_file_info
*ds_file_info
;
374 name
= g_string_new(NULL
);
380 * If there's more than one stream file in the stream file
381 * group, the first (earliest) stream file's path is used as
382 * the stream's unique name.
384 BT_ASSERT(ds_file_group
->ds_file_infos
->len
> 0);
385 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
386 g_string_assign(name
, ds_file_info
->path
->str
);
393 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
394 struct ctf_fs_trace
*ctf_fs_trace
,
395 struct ctf_fs_ds_file_group
*ds_file_group
)
398 struct ctf_fs_port_data
*port_data
= NULL
;
399 GString
*port_name
= NULL
;
401 port_name
= get_stream_instance_unique_name(ds_file_group
);
406 BT_LOGD("Creating one port named `%s`", port_name
->str
);
408 /* Create output port for this file */
409 port_data
= g_new0(struct ctf_fs_port_data
, 1);
414 port_data
->ctf_fs
= ctf_fs
;
415 port_data
->ds_file_group
= ds_file_group
;
416 ret
= bt_self_component_source_add_output_port(
417 ctf_fs
->self_comp
, port_name
->str
, port_data
, NULL
);
422 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
431 g_string_free(port_name
, TRUE
);
434 port_data_destroy(port_data
);
439 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
440 struct ctf_fs_trace
*ctf_fs_trace
)
445 /* Create one output port for each stream file group */
446 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
447 struct ctf_fs_ds_file_group
*ds_file_group
=
448 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
450 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
453 BT_LOGE("Cannot create output port.");
463 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
469 if (ds_file_info
->path
) {
470 g_string_free(ds_file_info
->path
, TRUE
);
473 ctf_fs_ds_index_destroy(ds_file_info
->index
);
474 g_free(ds_file_info
);
478 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
479 int64_t begin_ns
, struct ctf_fs_ds_index
*index
)
481 struct ctf_fs_ds_file_info
*ds_file_info
;
483 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
488 ds_file_info
->path
= g_string_new(path
);
489 if (!ds_file_info
->path
) {
490 ctf_fs_ds_file_info_destroy(ds_file_info
);
495 ds_file_info
->begin_ns
= begin_ns
;
496 ds_file_info
->index
= index
;
500 ctf_fs_ds_index_destroy(index
);
505 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
507 if (!ds_file_group
) {
511 if (ds_file_group
->ds_file_infos
) {
512 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
515 bt_stream_put_ref(ds_file_group
->stream
);
516 bt_stream_class_put_ref(ds_file_group
->stream_class
);
517 g_free(ds_file_group
);
521 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
522 struct ctf_fs_trace
*ctf_fs_trace
,
523 bt_stream_class
*stream_class
,
524 uint64_t stream_instance_id
)
526 struct ctf_fs_ds_file_group
*ds_file_group
;
528 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
529 if (!ds_file_group
) {
533 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
534 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
535 if (!ds_file_group
->ds_file_infos
) {
539 ds_file_group
->stream_id
= stream_instance_id
;
540 BT_ASSERT(stream_class
);
541 ds_file_group
->stream_class
= stream_class
;
542 bt_stream_class_get_ref(ds_file_group
->stream_class
);
543 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
547 ctf_fs_ds_file_group_destroy(ds_file_group
);
548 ds_file_group
= NULL
;
551 return ds_file_group
;
554 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
556 void array_insert(GPtrArray
*array
, gpointer element
, size_t pos
)
558 size_t original_array_len
= array
->len
;
560 /* Allocate an unused element at the end of the array. */
561 g_ptr_array_add(array
, NULL
);
563 /* If we are not inserting at the end, move the elements by one. */
564 if (pos
< original_array_len
) {
565 memmove(&(array
->pdata
[pos
+ 1]),
566 &(array
->pdata
[pos
]),
567 (original_array_len
- pos
) * sizeof(gpointer
));
570 /* Insert the value and bump the array len */
571 array
->pdata
[pos
] = element
;
575 int ctf_fs_ds_file_group_add_ds_file_info(
576 struct ctf_fs_ds_file_group
*ds_file_group
,
577 const char *path
, int64_t begin_ns
,
578 struct ctf_fs_ds_index
*index
)
580 struct ctf_fs_ds_file_info
*ds_file_info
;
584 /* Onwership of index is transferred. */
585 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
, index
);
591 /* Find a spot to insert this one */
592 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
593 struct ctf_fs_ds_file_info
*other_ds_file_info
=
594 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
596 if (begin_ns
< other_ds_file_info
->begin_ns
) {
601 array_insert(ds_file_group
->ds_file_infos
, ds_file_info
, i
);
606 ctf_fs_ds_file_info_destroy(ds_file_info
);
607 ctf_fs_ds_index_destroy(index
);
614 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
617 bt_stream_class
*stream_class
= NULL
;
618 int64_t stream_instance_id
= -1;
619 int64_t begin_ns
= -1;
620 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
621 bool add_group
= false;
624 struct ctf_fs_ds_file
*ds_file
= NULL
;
625 struct ctf_fs_ds_index
*index
= NULL
;
626 struct bt_msg_iter
*msg_iter
= NULL
;
627 struct ctf_stream_class
*sc
= NULL
;
628 struct bt_msg_iter_packet_properties props
;
630 msg_iter
= bt_msg_iter_create(ctf_fs_trace
->metadata
->tc
,
631 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops
, NULL
);
633 BT_LOGE_STR("Cannot create a CTF message iterator.");
637 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, NULL
, msg_iter
,
643 ret
= bt_msg_iter_get_packet_properties(ds_file
->msg_iter
, &props
);
645 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
650 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
651 props
.stream_class_id
);
653 stream_class
= sc
->ir_sc
;
654 BT_ASSERT(stream_class
);
655 stream_instance_id
= props
.data_stream_id
;
657 if (props
.snapshots
.beginning_clock
!= UINT64_C(-1)) {
658 BT_ASSERT(sc
->default_clock_class
);
659 ret
= bt_clock_class_cycles_to_ns_from_origin(
660 sc
->default_clock_class
,
661 props
.snapshots
.beginning_clock
, &begin_ns
);
663 BT_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).",
669 index
= ctf_fs_ds_file_build_index(ds_file
);
671 BT_LOGW("Failed to index CTF stream file \'%s\'",
672 ds_file
->file
->path
->str
);
675 if (begin_ns
== -1) {
677 * No beggining timestamp to sort the stream files
678 * within a stream file group, so consider that this
679 * file must be the only one within its group.
681 stream_instance_id
= -1;
684 if (stream_instance_id
== -1) {
686 * No stream instance ID or no beginning timestamp:
687 * create a unique stream file group for this stream
688 * file because, even if there's a stream instance ID,
689 * there's no timestamp to order the file within its
692 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
693 stream_class
, stream_instance_id
);
694 if (!ds_file_group
) {
698 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
699 path
, begin_ns
, index
);
700 /* Ownership of index is transferred. */
710 BT_ASSERT(stream_instance_id
!= -1);
711 BT_ASSERT(begin_ns
!= -1);
713 /* Find an existing stream file group with this ID */
714 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
715 ds_file_group
= g_ptr_array_index(
716 ctf_fs_trace
->ds_file_groups
, i
);
718 if (ds_file_group
->stream_class
== stream_class
&&
719 ds_file_group
->stream_id
==
720 stream_instance_id
) {
724 ds_file_group
= NULL
;
727 if (!ds_file_group
) {
728 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
729 stream_class
, stream_instance_id
);
730 if (!ds_file_group
) {
737 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
, path
,
747 ctf_fs_ds_file_group_destroy(ds_file_group
);
751 if (add_group
&& ds_file_group
) {
752 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
755 ctf_fs_ds_file_destroy(ds_file
);
758 bt_msg_iter_destroy(msg_iter
);
761 ctf_fs_ds_index_destroy(index
);
766 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
769 const char *basename
;
770 GError
*error
= NULL
;
774 /* Check each file in the path directory, except specific ones */
775 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
777 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
778 ctf_fs_trace
->path
->str
, error
->message
,
783 while ((basename
= g_dir_read_name(dir
))) {
784 struct ctf_fs_file
*file
;
786 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
787 /* Ignore the metadata stream. */
788 BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S
"%s`",
789 ctf_fs_trace
->path
->str
, basename
);
793 if (basename
[0] == '.') {
794 BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S
"%s`",
795 ctf_fs_trace
->path
->str
, basename
);
799 /* Create the file. */
800 file
= ctf_fs_file_create();
802 BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
"%s`",
803 ctf_fs_trace
->path
->str
, basename
);
807 /* Create full path string. */
808 g_string_append_printf(file
->path
, "%s" G_DIR_SEPARATOR_S
"%s",
809 ctf_fs_trace
->path
->str
, basename
);
810 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
811 BT_LOGD("Ignoring non-regular file `%s`",
813 ctf_fs_file_destroy(file
);
818 ret
= ctf_fs_file_open(file
, "rb");
820 BT_LOGE("Cannot open stream file `%s`", file
->path
->str
);
824 if (file
->size
== 0) {
825 /* Skip empty stream. */
826 BT_LOGD("Ignoring empty file `%s`", file
->path
->str
);
827 ctf_fs_file_destroy(file
);
831 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
834 BT_LOGE("Cannot add stream file `%s` to stream file group",
836 ctf_fs_file_destroy(file
);
840 ctf_fs_file_destroy(file
);
843 if (!ctf_fs_trace
->trace
) {
848 * At this point, DS file groupes are created, but their
849 * associated stream objects do not exist yet. This is because
850 * we need to name the created stream object with the data
851 * stream file's path. We have everything we need here to do
854 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
855 struct ctf_fs_ds_file_group
*ds_file_group
=
856 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
857 GString
*name
= get_stream_instance_unique_name(ds_file_group
);
863 if (ds_file_group
->stream_id
== UINT64_C(-1)) {
864 /* No stream ID: use 0 */
865 ds_file_group
->stream
= bt_stream_create_with_id(
866 ds_file_group
->stream_class
,
868 ctf_fs_trace
->next_stream_id
);
869 ctf_fs_trace
->next_stream_id
++;
871 /* Specific stream ID */
872 ds_file_group
->stream
= bt_stream_create_with_id(
873 ds_file_group
->stream_class
,
875 (uint64_t) ds_file_group
->stream_id
);
878 if (!ds_file_group
->stream
) {
879 BT_LOGE("Cannot create stream for DS file group: "
880 "addr=%p, stream-name=\"%s\"",
881 ds_file_group
, name
->str
);
882 g_string_free(name
, TRUE
);
886 ret
= bt_stream_set_name(ds_file_group
->stream
,
889 BT_LOGE("Cannot set stream's name: "
890 "addr=%p, stream-name=\"%s\"",
891 ds_file_group
->stream
, name
->str
);
892 g_string_free(name
, TRUE
);
896 g_string_free(name
, TRUE
);
918 int set_trace_name(bt_trace
*trace
, const char *name_suffix
)
921 const bt_trace_class
*tc
= bt_trace_borrow_class_const(trace
);
925 name
= g_string_new(NULL
);
927 BT_LOGE_STR("Failed to allocate a GString.");
933 * Check if we have a trace environment string value named `hostname`.
934 * If so, use it as the trace name's prefix.
936 val
= bt_trace_class_borrow_environment_entry_value_by_name_const(
938 if (val
&& bt_value_is_string(val
)) {
939 g_string_append(name
, bt_value_string_get(val
));
942 g_string_append_c(name
, G_DIR_SEPARATOR
);
947 g_string_append(name
, name_suffix
);
950 ret
= bt_trace_set_name(trace
, name
->str
);
959 g_string_free(name
, TRUE
);
966 struct ctf_fs_trace
*ctf_fs_trace_create(bt_self_component_source
*self_comp
,
967 const char *path
, const char *name
,
968 struct ctf_fs_metadata_config
*metadata_config
)
970 struct ctf_fs_trace
*ctf_fs_trace
;
973 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
978 ctf_fs_trace
->path
= g_string_new(path
);
979 if (!ctf_fs_trace
->path
) {
983 ctf_fs_trace
->name
= g_string_new(name
);
984 if (!ctf_fs_trace
->name
) {
988 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
989 if (!ctf_fs_trace
->metadata
) {
993 ctf_fs_metadata_init(ctf_fs_trace
->metadata
);
994 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
995 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
996 if (!ctf_fs_trace
->ds_file_groups
) {
1000 ret
= ctf_fs_metadata_set_trace_class(self_comp
,
1001 ctf_fs_trace
, metadata_config
);
1006 if (ctf_fs_trace
->metadata
->trace_class
) {
1007 ctf_fs_trace
->trace
=
1008 bt_trace_create(ctf_fs_trace
->metadata
->trace_class
);
1009 if (!ctf_fs_trace
->trace
) {
1014 if (ctf_fs_trace
->trace
) {
1015 ret
= set_trace_name(ctf_fs_trace
->trace
, name
);
1021 ret
= create_ds_file_groups(ctf_fs_trace
);
1027 * create_ds_file_groups() created all the streams that this
1028 * trace needs. There won't be any more. Therefore it is safe to
1029 * make this trace static.
1031 if (ctf_fs_trace
->trace
) {
1032 (void) bt_trace_make_static(ctf_fs_trace
->trace
);
1038 ctf_fs_trace_destroy(ctf_fs_trace
);
1039 ctf_fs_trace
= NULL
;
1042 return ctf_fs_trace
;
1046 int path_is_ctf_trace(const char *path
)
1048 GString
*metadata_path
= g_string_new(NULL
);
1051 if (!metadata_path
) {
1056 g_string_printf(metadata_path
, "%s" G_DIR_SEPARATOR_S
"%s", path
, CTF_FS_METADATA_FILENAME
);
1058 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
1064 g_string_free(metadata_path
, TRUE
);
1069 int add_trace_path(GList
**trace_paths
, const char *path
)
1071 GString
*norm_path
= NULL
;
1074 norm_path
= bt_common_normalize_path(path
, NULL
);
1076 BT_LOGE("Failed to normalize path `%s`.", path
);
1081 // FIXME: Remove or ifdef for __MINGW32__
1082 if (strcmp(norm_path
->str
, "/") == 0) {
1083 BT_LOGE("Opening a trace in `/` is not supported.");
1088 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1089 BT_ASSERT(*trace_paths
);
1094 g_string_free(norm_path
, TRUE
);
1101 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
)
1104 GError
*error
= NULL
;
1106 const char *basename
= NULL
;
1108 /* Check if the starting path is a CTF trace itself */
1109 ret
= path_is_ctf_trace(start_path
);
1116 * Stop recursion: a CTF trace cannot contain another
1119 ret
= add_trace_path(trace_paths
, start_path
);
1123 /* Look for subdirectories */
1124 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1125 /* Starting path is not a directory: end of recursion */
1129 dir
= g_dir_open(start_path
, 0, &error
);
1131 if (error
->code
== G_FILE_ERROR_ACCES
) {
1132 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1133 start_path
, error
->message
, error
->code
);
1137 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1138 start_path
, error
->message
, error
->code
);
1143 while ((basename
= g_dir_read_name(dir
))) {
1144 GString
*sub_path
= g_string_new(NULL
);
1151 g_string_printf(sub_path
, "%s" G_DIR_SEPARATOR_S
"%s", start_path
, basename
);
1152 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
);
1153 g_string_free(sub_path
, TRUE
);
1165 g_error_free(error
);
1172 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1173 GList
*trace_names
= NULL
;
1175 const char *last_sep
;
1179 * At this point we know that all the trace paths are
1180 * normalized, and so is the base path. This means that
1181 * they are absolute and they don't end with a separator.
1182 * We can simply find the location of the last separator
1183 * in the base path, which gives us the name of the actual
1184 * directory to look into, and use this location as the
1185 * start of each trace name within each trace path.
1189 * Base path: /home/user/my-traces/some-trace
1191 * - /home/user/my-traces/some-trace/host1/trace1
1192 * - /home/user/my-traces/some-trace/host1/trace2
1193 * - /home/user/my-traces/some-trace/host2/trace
1194 * - /home/user/my-traces/some-trace/other-trace
1196 * In this case the trace names are:
1198 * - some-trace/host1/trace1
1199 * - some-trace/host1/trace2
1200 * - some-trace/host2/trace
1201 * - some-trace/other-trace
1203 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1205 /* We know there's at least one separator */
1206 BT_ASSERT(last_sep
);
1208 /* Distance to base */
1209 base_dist
= last_sep
- base_path
+ 1;
1211 /* Create the trace names */
1212 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1213 GString
*trace_name
= g_string_new(NULL
);
1214 GString
*trace_path
= node
->data
;
1216 BT_ASSERT(trace_name
);
1217 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1218 trace_names
= g_list_append(trace_names
, trace_name
);
1225 int create_ctf_fs_traces(bt_self_component_source
*self_comp
,
1226 struct ctf_fs_component
*ctf_fs
,
1227 const char *path_param
)
1229 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1231 GString
*norm_path
= NULL
;
1232 GList
*trace_paths
= NULL
;
1233 GList
*trace_names
= NULL
;
1237 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1239 BT_LOGE("Failed to normalize path: `%s`.",
1244 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
);
1250 BT_LOGE("No CTF traces recursively found in `%s`.",
1255 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1257 BT_LOGE("Cannot create trace names from trace paths.");
1261 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1262 tp_node
= g_list_next(tp_node
),
1263 tn_node
= g_list_next(tn_node
)) {
1264 GString
*trace_path
= tp_node
->data
;
1265 GString
*trace_name
= tn_node
->data
;
1267 ctf_fs_trace
= ctf_fs_trace_create(self_comp
,
1268 trace_path
->str
, trace_name
->str
,
1269 &ctf_fs
->metadata_config
);
1270 if (!ctf_fs_trace
) {
1271 BT_LOGE("Cannot create trace for `%s`.",
1276 ret
= create_ports_for_trace(ctf_fs
, ctf_fs_trace
);
1281 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1282 ctf_fs_trace
= NULL
;
1289 ctf_fs_trace_destroy(ctf_fs_trace
);
1292 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1293 if (tp_node
->data
) {
1294 g_string_free(tp_node
->data
, TRUE
);
1298 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1299 if (tn_node
->data
) {
1300 g_string_free(tn_node
->data
, TRUE
);
1305 g_list_free(trace_paths
);
1309 g_list_free(trace_names
);
1313 g_string_free(norm_path
, TRUE
);
1320 struct ctf_fs_component
*ctf_fs_create(
1321 bt_self_component_source
*self_comp
,
1322 const bt_value
*params
)
1324 struct ctf_fs_component
*ctf_fs
;
1325 const bt_value
*value
= NULL
;
1326 const char *path_param
;
1328 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1333 bt_self_component_set_data(
1334 bt_self_component_source_as_self_component(self_comp
),
1338 * We don't need to get a new reference here because as long as
1339 * our private ctf_fs_component object exists, the containing
1340 * private component should also exist.
1342 ctf_fs
->self_comp
= self_comp
;
1343 value
= bt_value_map_borrow_entry_value_const(params
, "path");
1344 if (value
&& !bt_value_is_string(value
)) {
1348 path_param
= bt_value_string_get(value
);
1349 value
= bt_value_map_borrow_entry_value_const(params
,
1350 "clock-class-offset-s");
1352 if (!bt_value_is_integer(value
)) {
1353 BT_LOGE("clock-class-offset-s should be an integer");
1356 ctf_fs
->metadata_config
.clock_class_offset_s
= bt_value_integer_get(value
);
1359 value
= bt_value_map_borrow_entry_value_const(params
,
1360 "clock-class-offset-ns");
1362 if (!bt_value_is_integer(value
)) {
1363 BT_LOGE("clock-class-offset-ns should be an integer");
1366 ctf_fs
->metadata_config
.clock_class_offset_ns
= bt_value_integer_get(value
);
1369 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1370 if (!ctf_fs
->port_data
) {
1374 ctf_fs
->traces
= g_ptr_array_new_with_free_func(
1375 ctf_fs_trace_destroy_msgier
);
1376 if (!ctf_fs
->traces
) {
1380 if (create_ctf_fs_traces(self_comp
, ctf_fs
, path_param
)) {
1387 ctf_fs_destroy(ctf_fs
);
1389 bt_self_component_set_data(
1390 bt_self_component_source_as_self_component(self_comp
),
1398 bt_self_component_status
ctf_fs_init(
1399 bt_self_component_source
*self_comp
,
1400 const bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1402 struct ctf_fs_component
*ctf_fs
;
1403 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
1405 ctf_fs
= ctf_fs_create(self_comp
, params
);
1407 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
1414 bt_query_status
ctf_fs_query(
1415 bt_self_component_class_source
*comp_class
,
1416 const bt_query_executor
*query_exec
,
1417 const char *object
, const bt_value
*params
,
1418 const bt_value
**result
)
1420 bt_query_status status
= BT_QUERY_STATUS_OK
;
1422 if (!strcmp(object
, "metadata-info")) {
1423 status
= metadata_info_query(comp_class
, params
, result
);
1424 } else if (!strcmp(object
, "trace-info")) {
1425 status
= trace_info_query(comp_class
, params
, result
);
1427 BT_LOGE("Unknown query object `%s`", object
);
1428 status
= BT_QUERY_STATUS_INVALID_OBJECT
;