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
->graph
,
63 notif_iter_data
->notif_iter
,
64 notif_iter_data
->ds_file_group
->stream
,
65 ds_file_info
->path
->str
);
66 if (!notif_iter_data
->ds_file
) {
74 void ctf_fs_notif_iter_data_destroy(
75 struct ctf_fs_notif_iter_data
*notif_iter_data
)
77 if (!notif_iter_data
) {
81 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
83 if (notif_iter_data
->notif_iter
) {
84 bt_notif_iter_destroy(notif_iter_data
->notif_iter
);
87 g_free(notif_iter_data
);
91 enum bt_notification_iterator_status
ctf_fs_iterator_next_one(
92 struct ctf_fs_notif_iter_data
*notif_iter_data
,
93 struct bt_notification
**notif
)
95 enum bt_notification_iterator_status status
;
98 BT_ASSERT(notif_iter_data
->ds_file
);
99 status
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
, notif
);
101 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
&&
102 bt_notification_get_type(*notif
) ==
103 BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
104 if (notif_iter_data
->skip_stream_begin_notifs
) {
106 * We already emitted a
107 * BT_NOTIFICATION_TYPE_STREAM_BEGIN
108 * notification: skip this one, get a new one.
111 status
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
,
113 BT_ASSERT(status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
117 * First BT_NOTIFICATION_TYPE_STREAM_BEGIN
118 * notification: skip all following.
120 notif_iter_data
->skip_stream_begin_notifs
= true;
125 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
&&
126 bt_notification_get_type(*notif
) ==
127 BT_NOTIFICATION_TYPE_STREAM_END
) {
128 notif_iter_data
->ds_file_info_index
++;
130 if (notif_iter_data
->ds_file_info_index
==
131 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
133 * No more stream files to read: we reached the
134 * real end. Emit this
135 * BT_NOTIFICATION_TYPE_STREAM_END notification.
136 * The next time ctf_fs_iterator_next() is
137 * called for this notification iterator,
138 * ctf_fs_ds_file_next() will return
139 * BT_NOTIFICATION_ITERATOR_STATUS_END().
145 bt_notif_iter_reset(notif_iter_data
->notif_iter
);
148 * Open and start reading the next stream file within
149 * our stream file group.
151 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
153 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
157 status
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
, notif
);
160 * If we get a notification, we expect to get a
161 * BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
162 * because the iterator's state machine emits one before
163 * even requesting the first block of data from the
164 * medium. Skip this notification because we're not
165 * really starting a new stream here, and try getting a
166 * new notification (which, if it works, is a
167 * BT_NOTIFICATION_TYPE_PACKET_BEGIN one). We're sure to
168 * get at least one pair of
169 * BT_NOTIFICATION_TYPE_PACKET_BEGIN and
170 * BT_NOTIFICATION_TYPE_PACKET_END notifications in the
171 * case of a single, empty packet. We know there's at
172 * least one packet because the stream file group does
173 * not contain empty stream files.
175 BT_ASSERT(notif_iter_data
->skip_stream_begin_notifs
);
177 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
178 BT_ASSERT(bt_notification_get_type(*notif
) ==
179 BT_NOTIFICATION_TYPE_STREAM_BEGIN
);
181 status
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
,
183 BT_ASSERT(status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
192 enum bt_notification_iterator_status
ctf_fs_iterator_next(
193 struct bt_private_connection_private_notification_iterator
*iterator
,
194 bt_notification_array notifs
, uint64_t capacity
,
197 enum bt_notification_iterator_status status
=
198 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
199 struct ctf_fs_notif_iter_data
*notif_iter_data
=
200 bt_private_connection_private_notification_iterator_get_user_data(iterator
);
203 while (i
< capacity
&& status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
204 status
= ctf_fs_iterator_next_one(notif_iter_data
, ¬ifs
[i
]);
205 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
212 * Even if ctf_fs_iterator_next_one() returned something
213 * else than BT_NOTIFICATION_ITERATOR_STATUS_OK, we
214 * accumulated notification objects in the output
215 * notification array, so we need to return
216 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
217 * transfered to downstream. This other status occurs
218 * again the next time muxer_notif_iter_do_next() is
219 * called, possibly without any accumulated
220 * notification, in which case we'll return it.
223 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
229 void ctf_fs_iterator_finalize(struct bt_private_connection_private_notification_iterator
*it
)
231 void *notif_iter_data
=
232 bt_private_connection_private_notification_iterator_get_user_data(it
);
234 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
237 enum bt_notification_iterator_status
ctf_fs_iterator_init(
238 struct bt_private_connection_private_notification_iterator
*it
,
239 struct bt_private_port
*port
)
241 struct ctf_fs_port_data
*port_data
;
242 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
243 enum bt_notification_iterator_status ret
=
244 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
247 port_data
= bt_private_port_get_user_data(port
);
249 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
253 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
254 if (!notif_iter_data
) {
255 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
259 notif_iter_data
->graph
= bt_component_borrow_graph(
260 bt_component_borrow_from_private(port_data
->ctf_fs
->priv_comp
));
261 notif_iter_data
->notif_iter
= bt_notif_iter_create(
262 port_data
->ds_file_group
->ctf_fs_trace
->metadata
->trace
,
263 bt_common_get_page_size() * 8,
264 ctf_fs_ds_file_medops
, NULL
);
265 if (!notif_iter_data
->notif_iter
) {
266 BT_LOGE_STR("Cannot create a CTF notification iterator.");
267 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
271 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
272 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
274 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
278 ret
= bt_private_connection_private_notification_iterator_set_user_data(it
, notif_iter_data
);
279 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
283 notif_iter_data
= NULL
;
287 (void) bt_private_connection_private_notification_iterator_set_user_data(it
, NULL
);
290 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
295 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
301 if (ctf_fs
->traces
) {
302 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
305 if (ctf_fs
->port_data
) {
306 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
313 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
319 if (ctf_fs_trace
->ds_file_groups
) {
320 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
323 if (ctf_fs_trace
->path
) {
324 g_string_free(ctf_fs_trace
->path
, TRUE
);
327 if (ctf_fs_trace
->name
) {
328 g_string_free(ctf_fs_trace
->name
, TRUE
);
331 if (ctf_fs_trace
->metadata
) {
332 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
333 g_free(ctf_fs_trace
->metadata
);
336 bt_put(ctf_fs_trace
->cc_prio_map
);
337 g_free(ctf_fs_trace
);
341 void ctf_fs_trace_destroy_notifier(void *data
)
343 struct ctf_fs_trace
*trace
= data
;
344 ctf_fs_trace_destroy(trace
);
347 void ctf_fs_finalize(struct bt_private_component
*component
)
349 void *data
= bt_private_component_get_user_data(component
);
351 ctf_fs_destroy(data
);
355 void port_data_destroy(void *data
) {
356 struct ctf_fs_port_data
*port_data
= data
;
366 GString
*get_stream_instance_unique_name(
367 struct ctf_fs_ds_file_group
*ds_file_group
)
370 struct ctf_fs_ds_file_info
*ds_file_info
;
372 name
= g_string_new(NULL
);
378 * If there's more than one stream file in the stream file
379 * group, the first (earliest) stream file's path is used as
380 * the stream's unique name.
382 BT_ASSERT(ds_file_group
->ds_file_infos
->len
> 0);
383 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
384 g_string_assign(name
, ds_file_info
->path
->str
);
391 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
392 struct ctf_fs_trace
*ctf_fs_trace
,
393 struct ctf_fs_ds_file_group
*ds_file_group
)
396 struct ctf_fs_port_data
*port_data
= NULL
;
397 GString
*port_name
= NULL
;
399 port_name
= get_stream_instance_unique_name(ds_file_group
);
404 BT_LOGD("Creating one port named `%s`", port_name
->str
);
406 /* Create output port for this file */
407 port_data
= g_new0(struct ctf_fs_port_data
, 1);
412 port_data
->ctf_fs
= ctf_fs
;
413 port_data
->ds_file_group
= ds_file_group
;
414 ret
= bt_private_component_source_add_output_private_port(
415 ctf_fs
->priv_comp
, port_name
->str
, port_data
, NULL
);
420 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
429 g_string_free(port_name
, TRUE
);
432 port_data_destroy(port_data
);
437 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
438 struct ctf_fs_trace
*ctf_fs_trace
)
443 /* Create one output port for each stream file group */
444 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
445 struct ctf_fs_ds_file_group
*ds_file_group
=
446 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
448 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
451 BT_LOGE("Cannot create output port.");
461 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
462 struct bt_field
*packet_header_field
)
464 struct bt_field
*stream_instance_id_field
= NULL
;
465 uint64_t stream_instance_id
= -1ULL;
468 if (!packet_header_field
) {
472 stream_instance_id_field
= bt_field_structure_borrow_field_by_name(
473 packet_header_field
, "stream_instance_id");
474 if (!stream_instance_id_field
) {
478 ret
= bt_field_integer_unsigned_get_value(stream_instance_id_field
,
479 &stream_instance_id
);
481 stream_instance_id
= -1ULL;
486 return stream_instance_id
;
489 uint64_t get_packet_context_timestamp_begin_ns(
490 struct ctf_fs_trace
*ctf_fs_trace
,
491 struct bt_field
*packet_context_field
)
494 struct bt_field
*timestamp_begin_field
= NULL
;
495 struct bt_field_type
*timestamp_begin_ft
= NULL
;
496 uint64_t timestamp_begin_raw_value
= -1ULL;
497 uint64_t timestamp_begin_ns
= -1ULL;
498 int64_t timestamp_begin_ns_signed
;
499 struct bt_clock_class
*timestamp_begin_clock_class
= NULL
;
501 if (!packet_context_field
) {
505 timestamp_begin_field
= bt_field_structure_borrow_field_by_name(
506 packet_context_field
, "timestamp_begin");
507 if (!timestamp_begin_field
) {
511 timestamp_begin_ft
= bt_field_borrow_type(timestamp_begin_field
);
512 BT_ASSERT(timestamp_begin_ft
);
513 timestamp_begin_clock_class
=
514 bt_field_type_integer_borrow_mapped_clock_class(timestamp_begin_ft
);
515 if (!timestamp_begin_clock_class
) {
519 ret
= bt_field_integer_unsigned_get_value(timestamp_begin_field
,
520 ×tamp_begin_raw_value
);
525 ret
= bt_clock_class_cycles_to_ns(timestamp_begin_clock_class
,
526 timestamp_begin_raw_value
, ×tamp_begin_ns_signed
);
531 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
534 return timestamp_begin_ns
;
538 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
544 if (ds_file_info
->path
) {
545 g_string_free(ds_file_info
->path
, TRUE
);
548 ctf_fs_ds_index_destroy(ds_file_info
->index
);
549 g_free(ds_file_info
);
553 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
554 uint64_t begin_ns
, struct ctf_fs_ds_index
*index
)
556 struct ctf_fs_ds_file_info
*ds_file_info
;
558 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
563 ds_file_info
->path
= g_string_new(path
);
564 if (!ds_file_info
->path
) {
565 ctf_fs_ds_file_info_destroy(ds_file_info
);
570 ds_file_info
->begin_ns
= begin_ns
;
571 ds_file_info
->index
= index
;
575 ctf_fs_ds_index_destroy(index
);
580 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
582 if (!ds_file_group
) {
586 if (ds_file_group
->ds_file_infos
) {
587 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
590 bt_put(ds_file_group
->stream
);
591 bt_put(ds_file_group
->stream_class
);
592 g_free(ds_file_group
);
596 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
597 struct ctf_fs_trace
*ctf_fs_trace
,
598 struct bt_stream_class
*stream_class
,
599 uint64_t stream_instance_id
)
601 struct ctf_fs_ds_file_group
*ds_file_group
;
603 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
604 if (!ds_file_group
) {
608 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
609 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
610 if (!ds_file_group
->ds_file_infos
) {
614 ds_file_group
->stream_id
= stream_instance_id
;
615 BT_ASSERT(stream_class
);
616 ds_file_group
->stream_class
= bt_get(stream_class
);
617 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
621 ctf_fs_ds_file_group_destroy(ds_file_group
);
622 ds_file_group
= NULL
;
625 return ds_file_group
;
628 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
630 void array_insert(GPtrArray
*array
, gpointer element
, size_t pos
)
632 size_t original_array_len
= array
->len
;
634 /* Allocate an unused element at the end of the array. */
635 g_ptr_array_add(array
, NULL
);
637 /* If we are not inserting at the end, move the elements by one. */
638 if (pos
< original_array_len
) {
639 memmove(&(array
->pdata
[pos
+ 1]),
640 &(array
->pdata
[pos
]),
641 (original_array_len
- pos
) * sizeof(gpointer
));
644 /* Insert the value and bump the array len */
645 array
->pdata
[pos
] = element
;
649 int ctf_fs_ds_file_group_add_ds_file_info(
650 struct ctf_fs_ds_file_group
*ds_file_group
,
651 const char *path
, uint64_t begin_ns
,
652 struct ctf_fs_ds_index
*index
)
654 struct ctf_fs_ds_file_info
*ds_file_info
;
658 /* Onwership of index is transferred. */
659 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
, index
);
665 /* Find a spot to insert this one */
666 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
667 struct ctf_fs_ds_file_info
*other_ds_file_info
=
668 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
670 if (begin_ns
< other_ds_file_info
->begin_ns
) {
675 array_insert(ds_file_group
->ds_file_infos
, ds_file_info
, i
);
680 ctf_fs_ds_file_info_destroy(ds_file_info
);
681 ctf_fs_ds_index_destroy(index
);
688 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
689 struct bt_graph
*graph
, const char *path
)
691 struct bt_field
*packet_header_field
= NULL
;
692 struct bt_field
*packet_context_field
= NULL
;
693 struct bt_stream_class
*stream_class
= NULL
;
694 uint64_t stream_instance_id
= -1ULL;
695 uint64_t begin_ns
= -1ULL;
696 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
697 bool add_group
= false;
700 struct ctf_fs_ds_file
*ds_file
= NULL
;
701 struct ctf_fs_ds_index
*index
= NULL
;
702 struct bt_notif_iter
*notif_iter
= NULL
;
704 notif_iter
= bt_notif_iter_create(ctf_fs_trace
->metadata
->trace
,
705 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops
, NULL
);
707 BT_LOGE_STR("Cannot create a CTF notification iterator.");
711 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, graph
, notif_iter
,
717 ret
= ctf_fs_ds_file_borrow_packet_header_context_fields(ds_file
,
718 &packet_header_field
, &packet_context_field
);
720 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
725 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
726 packet_header_field
);
727 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
728 packet_context_field
);
729 stream_class
= ctf_utils_borrow_stream_class_from_packet_header(
730 ctf_fs_trace
->metadata
->trace
, packet_header_field
);
735 index
= ctf_fs_ds_file_build_index(ds_file
);
737 BT_LOGW("Failed to index CTF stream file \'%s\'",
738 ds_file
->file
->path
->str
);
741 if (begin_ns
== -1ULL) {
743 * No beggining timestamp to sort the stream files
744 * within a stream file group, so consider that this
745 * file must be the only one within its group.
747 stream_instance_id
= -1ULL;
750 if (stream_instance_id
== -1ULL) {
752 * No stream instance ID or no beginning timestamp:
753 * create a unique stream file group for this stream
754 * file because, even if there's a stream instance ID,
755 * there's no timestamp to order the file within its
758 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
759 stream_class
, stream_instance_id
);
760 if (!ds_file_group
) {
764 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
765 path
, begin_ns
, index
);
766 /* Ownership of index is transferred. */
776 BT_ASSERT(stream_instance_id
!= -1ULL);
777 BT_ASSERT(begin_ns
!= -1ULL);
779 /* Find an existing stream file group with this ID */
780 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
781 ds_file_group
= g_ptr_array_index(
782 ctf_fs_trace
->ds_file_groups
, i
);
784 if (ds_file_group
->stream_class
== stream_class
&&
785 ds_file_group
->stream_id
==
786 stream_instance_id
) {
790 ds_file_group
= NULL
;
793 if (!ds_file_group
) {
794 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
795 stream_class
, stream_instance_id
);
796 if (!ds_file_group
) {
803 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
, path
,
813 ctf_fs_ds_file_group_destroy(ds_file_group
);
817 if (add_group
&& ds_file_group
) {
818 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
821 ctf_fs_ds_file_destroy(ds_file
);
824 bt_notif_iter_destroy(notif_iter
);
827 ctf_fs_ds_index_destroy(index
);
832 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
,
833 struct bt_graph
*graph
)
836 const char *basename
;
837 GError
*error
= NULL
;
841 /* Check each file in the path directory, except specific ones */
842 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
844 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
845 ctf_fs_trace
->path
->str
, error
->message
,
850 while ((basename
= g_dir_read_name(dir
))) {
851 struct ctf_fs_file
*file
;
853 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
854 /* Ignore the metadata stream. */
855 BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S
"%s`",
856 ctf_fs_trace
->path
->str
, basename
);
860 if (basename
[0] == '.') {
861 BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S
"%s`",
862 ctf_fs_trace
->path
->str
, basename
);
866 /* Create the file. */
867 file
= ctf_fs_file_create();
869 BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
"%s`",
870 ctf_fs_trace
->path
->str
, basename
);
874 /* Create full path string. */
875 g_string_append_printf(file
->path
, "%s" G_DIR_SEPARATOR_S
"%s",
876 ctf_fs_trace
->path
->str
, basename
);
877 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
878 BT_LOGD("Ignoring non-regular file `%s`",
880 ctf_fs_file_destroy(file
);
885 ret
= ctf_fs_file_open(file
, "rb");
887 BT_LOGE("Cannot open stream file `%s`", file
->path
->str
);
891 if (file
->size
== 0) {
892 /* Skip empty stream. */
893 BT_LOGD("Ignoring empty file `%s`", file
->path
->str
);
894 ctf_fs_file_destroy(file
);
898 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
, graph
,
901 BT_LOGE("Cannot add stream file `%s` to stream file group",
903 ctf_fs_file_destroy(file
);
907 ctf_fs_file_destroy(file
);
911 * At this point, DS file groupes are created, but their
912 * associated stream objects do not exist yet. This is because
913 * we need to name the created stream object with the data
914 * stream file's path. We have everything we need here to do
917 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
918 struct ctf_fs_ds_file_group
*ds_file_group
=
919 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
920 GString
*name
= get_stream_instance_unique_name(ds_file_group
);
926 if (ds_file_group
->stream_id
== -1ULL) {
927 /* No stream ID: use 0 */
928 ds_file_group
->stream
= bt_stream_create(
929 ds_file_group
->stream_class
, name
->str
,
930 ctf_fs_trace
->next_stream_id
);
931 ctf_fs_trace
->next_stream_id
++;
933 /* Specific stream ID */
934 ds_file_group
->stream
= bt_stream_create(
935 ds_file_group
->stream_class
, name
->str
,
936 ds_file_group
->stream_id
);
939 g_string_free(name
, TRUE
);
941 if (!ds_file_group
->stream
) {
942 BT_LOGE("Cannot create stream for DS file group: "
943 "addr=%p, stream-name=\"%s\"",
944 ds_file_group
, name
->str
);
968 struct ctf_fs_trace
*ctf_fs_trace_create(const char *path
, const char *name
,
969 struct ctf_fs_metadata_config
*metadata_config
,
970 struct bt_graph
*graph
)
972 struct ctf_fs_trace
*ctf_fs_trace
;
975 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
980 ctf_fs_trace
->path
= g_string_new(path
);
981 if (!ctf_fs_trace
->path
) {
985 ctf_fs_trace
->name
= g_string_new(name
);
986 if (!ctf_fs_trace
->name
) {
990 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
991 if (!ctf_fs_trace
->metadata
) {
995 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
996 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
997 if (!ctf_fs_trace
->ds_file_groups
) {
1001 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
, metadata_config
);
1006 ret
= create_ds_file_groups(ctf_fs_trace
, graph
);
1012 * create_ds_file_groups() created all the streams that this
1013 * trace needs. There won't be any more. Therefore it is safe to
1014 * make this trace static.
1016 (void) bt_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
1021 ctf_fs_trace_destroy(ctf_fs_trace
);
1022 ctf_fs_trace
= NULL
;
1024 return ctf_fs_trace
;
1028 int path_is_ctf_trace(const char *path
)
1030 GString
*metadata_path
= g_string_new(NULL
);
1033 if (!metadata_path
) {
1038 g_string_printf(metadata_path
, "%s" G_DIR_SEPARATOR_S
"%s", path
, CTF_FS_METADATA_FILENAME
);
1040 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
1046 g_string_free(metadata_path
, TRUE
);
1051 int add_trace_path(GList
**trace_paths
, const char *path
)
1053 GString
*norm_path
= NULL
;
1056 norm_path
= bt_common_normalize_path(path
, NULL
);
1058 BT_LOGE("Failed to normalize path `%s`.", path
);
1063 // FIXME: Remove or ifdef for __MINGW32__
1064 if (strcmp(norm_path
->str
, "/") == 0) {
1065 BT_LOGE("Opening a trace in `/` is not supported.");
1070 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1071 BT_ASSERT(*trace_paths
);
1076 g_string_free(norm_path
, TRUE
);
1083 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
)
1086 GError
*error
= NULL
;
1088 const char *basename
= NULL
;
1090 /* Check if the starting path is a CTF trace itself */
1091 ret
= path_is_ctf_trace(start_path
);
1098 * Stop recursion: a CTF trace cannot contain another
1101 ret
= add_trace_path(trace_paths
, start_path
);
1105 /* Look for subdirectories */
1106 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1107 /* Starting path is not a directory: end of recursion */
1111 dir
= g_dir_open(start_path
, 0, &error
);
1113 if (error
->code
== G_FILE_ERROR_ACCES
) {
1114 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1115 start_path
, error
->message
, error
->code
);
1119 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1120 start_path
, error
->message
, error
->code
);
1125 while ((basename
= g_dir_read_name(dir
))) {
1126 GString
*sub_path
= g_string_new(NULL
);
1133 g_string_printf(sub_path
, "%s" G_DIR_SEPARATOR_S
"%s", start_path
, basename
);
1134 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
);
1135 g_string_free(sub_path
, TRUE
);
1147 g_error_free(error
);
1154 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1155 GList
*trace_names
= NULL
;
1157 const char *last_sep
;
1161 * At this point we know that all the trace paths are
1162 * normalized, and so is the base path. This means that
1163 * they are absolute and they don't end with a separator.
1164 * We can simply find the location of the last separator
1165 * in the base path, which gives us the name of the actual
1166 * directory to look into, and use this location as the
1167 * start of each trace name within each trace path.
1171 * Base path: /home/user/my-traces/some-trace
1173 * - /home/user/my-traces/some-trace/host1/trace1
1174 * - /home/user/my-traces/some-trace/host1/trace2
1175 * - /home/user/my-traces/some-trace/host2/trace
1176 * - /home/user/my-traces/some-trace/other-trace
1178 * In this case the trace names are:
1180 * - some-trace/host1/trace1
1181 * - some-trace/host1/trace2
1182 * - some-trace/host2/trace
1183 * - some-trace/other-trace
1185 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1187 /* We know there's at least one separator */
1188 BT_ASSERT(last_sep
);
1190 /* Distance to base */
1191 base_dist
= last_sep
- base_path
+ 1;
1193 /* Create the trace names */
1194 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1195 GString
*trace_name
= g_string_new(NULL
);
1196 GString
*trace_path
= node
->data
;
1198 BT_ASSERT(trace_name
);
1199 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1200 trace_names
= g_list_append(trace_names
, trace_name
);
1207 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1208 const char *path_param
)
1210 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1212 GString
*norm_path
= NULL
;
1213 GList
*trace_paths
= NULL
;
1214 GList
*trace_names
= NULL
;
1218 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1220 BT_LOGE("Failed to normalize path: `%s`.",
1225 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
);
1231 BT_LOGE("No CTF traces recursively found in `%s`.",
1236 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1238 BT_LOGE("Cannot create trace names from trace paths.");
1242 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1243 tp_node
= g_list_next(tp_node
),
1244 tn_node
= g_list_next(tn_node
)) {
1245 GString
*trace_path
= tp_node
->data
;
1246 GString
*trace_name
= tn_node
->data
;
1247 struct bt_graph
*graph
= bt_component_borrow_graph(
1248 bt_component_borrow_from_private(ctf_fs
->priv_comp
));
1251 ctf_fs_trace
= ctf_fs_trace_create(trace_path
->str
,
1252 trace_name
->str
, &ctf_fs
->metadata_config
,
1254 if (!ctf_fs_trace
) {
1255 BT_LOGE("Cannot create trace for `%s`.",
1260 ret
= create_ports_for_trace(ctf_fs
, ctf_fs_trace
);
1265 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1266 ctf_fs_trace
= NULL
;
1273 ctf_fs_trace_destroy(ctf_fs_trace
);
1276 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1277 if (tp_node
->data
) {
1278 g_string_free(tp_node
->data
, TRUE
);
1282 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1283 if (tn_node
->data
) {
1284 g_string_free(tn_node
->data
, TRUE
);
1289 g_list_free(trace_paths
);
1293 g_list_free(trace_names
);
1297 g_string_free(norm_path
, TRUE
);
1304 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1305 struct bt_value
*params
)
1307 struct ctf_fs_component
*ctf_fs
;
1308 struct bt_value
*value
= NULL
;
1309 const char *path_param
;
1310 enum bt_component_status ret
;
1311 enum bt_value_status value_ret
;
1313 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1318 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1319 BT_ASSERT(ret
== BT_COMPONENT_STATUS_OK
);
1322 * We don't need to get a new reference here because as long as
1323 * our private ctf_fs_component object exists, the containing
1324 * private component should also exist.
1326 ctf_fs
->priv_comp
= priv_comp
;
1327 value
= bt_value_map_borrow(params
, "path");
1328 if (value
&& !bt_value_is_string(value
)) {
1332 value_ret
= bt_value_string_get(value
, &path_param
);
1333 BT_ASSERT(value_ret
== BT_VALUE_STATUS_OK
);
1334 value
= bt_value_map_borrow(params
, "clock-class-offset-s");
1336 if (!bt_value_is_integer(value
)) {
1337 BT_LOGE("clock-class-offset-s should be an integer");
1340 value_ret
= bt_value_integer_get(value
,
1341 &ctf_fs
->metadata_config
.clock_class_offset_s
);
1342 BT_ASSERT(value_ret
== BT_VALUE_STATUS_OK
);
1345 value
= bt_value_map_borrow(params
, "clock-class-offset-ns");
1347 if (!bt_value_is_integer(value
)) {
1348 BT_LOGE("clock-class-offset-ns should be an integer");
1351 value_ret
= bt_value_integer_get(value
,
1352 &ctf_fs
->metadata_config
.clock_class_offset_ns
);
1353 BT_ASSERT(value_ret
== BT_VALUE_STATUS_OK
);
1356 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1357 if (!ctf_fs
->port_data
) {
1361 ctf_fs
->traces
= g_ptr_array_new_with_free_func(
1362 ctf_fs_trace_destroy_notifier
);
1363 if (!ctf_fs
->traces
) {
1367 if (create_ctf_fs_traces(ctf_fs
, path_param
)) {
1374 ctf_fs_destroy(ctf_fs
);
1376 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1377 BT_ASSERT(ret
== BT_COMPONENT_STATUS_OK
);
1384 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1385 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1387 struct ctf_fs_component
*ctf_fs
;
1388 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1390 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1392 ret
= BT_COMPONENT_STATUS_ERROR
;
1399 struct bt_component_class_query_method_return
ctf_fs_query(
1400 struct bt_component_class
*comp_class
,
1401 struct bt_query_executor
*query_exec
,
1402 const char *object
, struct bt_value
*params
)
1404 struct bt_component_class_query_method_return ret
= {
1406 .status
= BT_QUERY_STATUS_OK
,
1409 if (!strcmp(object
, "metadata-info")) {
1410 ret
= metadata_info_query(comp_class
, params
);
1411 } else if (!strcmp(object
, "trace-info")) {
1412 ret
= trace_info_query(comp_class
, params
);
1414 BT_LOGE("Unknown query object `%s`", object
);
1415 ret
.status
= BT_QUERY_STATUS_INVALID_OBJECT
;