4 * Babeltrace CTF file system Reader Component
6 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <babeltrace/common-internal.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/stream.h>
32 #include <babeltrace/ctf-ir/fields.h>
33 #include <babeltrace/graph/private-port.h>
34 #include <babeltrace/graph/private-component.h>
35 #include <babeltrace/graph/private-component-source.h>
36 #include <babeltrace/graph/private-notification-iterator.h>
37 #include <babeltrace/graph/component.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/clock-class-priority-map.h>
40 #include <plugins-common.h>
47 #include "data-stream-file.h"
49 #include "../common/metadata/decoder.h"
52 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
56 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data
*notif_iter_data
)
58 struct ctf_fs_ds_file_info
*ds_file_info
;
61 assert(notif_iter_data
->ds_file_info_index
<
62 notif_iter_data
->ds_file_group
->ds_file_infos
->len
);
63 ds_file_info
= g_ptr_array_index(
64 notif_iter_data
->ds_file_group
->ds_file_infos
,
65 notif_iter_data
->ds_file_info_index
);
67 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
68 notif_iter_data
->ds_file
= ctf_fs_ds_file_create(
69 notif_iter_data
->ds_file_group
->ctf_fs_trace
,
70 notif_iter_data
->ds_file_group
->stream
,
71 ds_file_info
->path
->str
);
72 if (!notif_iter_data
->ds_file
) {
80 void ctf_fs_notif_iter_data_destroy(
81 struct ctf_fs_notif_iter_data
*notif_iter_data
)
83 if (!notif_iter_data
) {
87 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
88 g_free(notif_iter_data
);
91 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
92 struct bt_private_notification_iterator
*iterator
)
94 struct bt_notification_iterator_next_return next_ret
;
95 struct ctf_fs_notif_iter_data
*notif_iter_data
=
96 bt_private_notification_iterator_get_user_data(iterator
);
99 assert(notif_iter_data
->ds_file
);
100 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
101 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
102 assert(!next_ret
.notification
);
103 notif_iter_data
->ds_file_info_index
++;
105 if (notif_iter_data
->ds_file_info_index
==
106 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
108 * No more stream files to read: we reached the
115 * Open and start reading the next stream file within
116 * our stream file group.
118 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
120 next_ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
124 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
127 * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END
128 * with a brand new stream file because empty stream
129 * files are not even part of stream file groups, which
130 * means we're sure to get at least one pair of "packet
131 * begin" and "packet end" notifications in the case of
132 * a single, empty packet.
134 assert(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
141 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
143 void *notif_iter_data
=
144 bt_private_notification_iterator_get_user_data(it
);
146 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
149 enum bt_notification_iterator_status
ctf_fs_iterator_init(
150 struct bt_private_notification_iterator
*it
,
151 struct bt_private_port
*port
)
153 struct ctf_fs_port_data
*port_data
;
154 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
155 enum bt_notification_iterator_status ret
=
156 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
159 port_data
= bt_private_port_get_user_data(port
);
161 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
165 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
166 if (!notif_iter_data
) {
167 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
171 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
172 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
174 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
178 ret
= bt_private_notification_iterator_set_user_data(it
, notif_iter_data
);
183 notif_iter_data
= NULL
;
187 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
189 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
190 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
194 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
199 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
205 if (ctf_fs
->traces
) {
206 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
209 if (ctf_fs
->port_data
) {
210 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
217 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
223 if (ctf_fs_trace
->ds_file_groups
) {
224 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
227 if (ctf_fs_trace
->path
) {
228 g_string_free(ctf_fs_trace
->path
, TRUE
);
231 if (ctf_fs_trace
->name
) {
232 g_string_free(ctf_fs_trace
->name
, TRUE
);
235 if (ctf_fs_trace
->metadata
) {
236 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
237 g_free(ctf_fs_trace
->metadata
);
240 bt_put(ctf_fs_trace
->cc_prio_map
);
241 g_free(ctf_fs_trace
);
245 void ctf_fs_trace_destroy_notifier(void *data
)
247 struct ctf_fs_trace
*trace
= data
;
248 ctf_fs_trace_destroy(trace
);
251 void ctf_fs_finalize(struct bt_private_component
*component
)
253 void *data
= bt_private_component_get_user_data(component
);
255 ctf_fs_destroy(data
);
259 void port_data_destroy(void *data
) {
260 struct ctf_fs_port_data
*port_data
= data
;
270 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
271 struct ctf_fs_trace
*ctf_fs_trace
,
272 struct ctf_fs_ds_file_group
*ds_file_group
)
275 struct ctf_fs_port_data
*port_data
= NULL
;
276 GString
*port_name
= NULL
;
277 struct ctf_fs_ds_file_info
*ds_file_info
=
278 g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
280 port_name
= g_string_new(NULL
);
286 * Assign the name for the new output port. If there's more than
287 * one stream file in the stream file group, the first
288 * (earliest) stream file's path is used.
290 assert(ds_file_group
->ds_file_infos
->len
> 0);
291 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
292 g_string_assign(port_name
, ds_file_info
->path
->str
);
293 BT_LOGD("Creating one port named `%s`", port_name
->str
);
295 /* Create output port for this file */
296 port_data
= g_new0(struct ctf_fs_port_data
, 1);
301 port_data
->ds_file_group
= ds_file_group
;
302 ret
= bt_private_component_source_add_output_private_port(
303 ctf_fs
->priv_comp
, port_name
->str
, port_data
, NULL
);
308 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
317 g_string_free(port_name
, TRUE
);
320 port_data_destroy(port_data
);
325 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
326 struct ctf_fs_trace
*ctf_fs_trace
)
331 /* Create one output port for each stream file group */
332 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
333 struct ctf_fs_ds_file_group
*ds_file_group
=
334 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
336 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
339 BT_LOGE("Cannot create output port.");
348 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
349 struct bt_ctf_field
*packet_header_field
)
351 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
352 uint64_t stream_instance_id
= -1ULL;
355 if (!packet_header_field
) {
359 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
360 packet_header_field
, "stream_instance_id");
361 if (!stream_instance_id_field
) {
365 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
366 &stream_instance_id
);
368 stream_instance_id
= -1ULL;
373 bt_put(stream_instance_id_field
);
374 return stream_instance_id
;
377 struct bt_ctf_stream_class
*stream_class_from_packet_header(
378 struct ctf_fs_trace
*ctf_fs_trace
,
379 struct bt_ctf_field
*packet_header_field
)
381 struct bt_ctf_field
*stream_id_field
= NULL
;
382 struct bt_ctf_stream_class
*stream_class
= NULL
;
383 uint64_t stream_id
= -1ULL;
386 if (!packet_header_field
) {
387 goto single_stream_class
;
390 stream_id_field
= bt_ctf_field_structure_get_field_by_name(
391 packet_header_field
, "stream_id");
392 if (!stream_id_field
) {
393 goto single_stream_class
;
396 ret
= bt_ctf_field_unsigned_integer_get_value(stream_id_field
,
402 if (stream_id
== -1ULL) {
404 /* Single stream class */
405 if (bt_ctf_trace_get_stream_class_count(
406 ctf_fs_trace
->metadata
->trace
) == 0) {
410 stream_class
= bt_ctf_trace_get_stream_class_by_index(
411 ctf_fs_trace
->metadata
->trace
, 0);
413 stream_class
= bt_ctf_trace_get_stream_class_by_id(
414 ctf_fs_trace
->metadata
->trace
, stream_id
);
418 bt_put(stream_id_field
);
422 uint64_t get_packet_context_timestamp_begin_ns(
423 struct ctf_fs_trace
*ctf_fs_trace
,
424 struct bt_ctf_field
*packet_context_field
)
427 struct bt_ctf_field
*timestamp_begin_field
= NULL
;
428 struct bt_ctf_field_type
*timestamp_begin_ft
= NULL
;
429 uint64_t timestamp_begin_raw_value
= -1ULL;
430 uint64_t timestamp_begin_ns
= -1ULL;
431 int64_t timestamp_begin_ns_signed
;
432 struct bt_ctf_clock_class
*timestamp_begin_clock_class
= NULL
;
433 struct bt_ctf_clock_value
*clock_value
= NULL
;
435 if (!packet_context_field
) {
439 timestamp_begin_field
= bt_ctf_field_structure_get_field_by_name(
440 packet_context_field
, "timestamp_begin");
441 if (!timestamp_begin_field
) {
445 timestamp_begin_ft
= bt_ctf_field_get_type(timestamp_begin_field
);
446 assert(timestamp_begin_ft
);
447 timestamp_begin_clock_class
=
448 bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft
);
449 if (!timestamp_begin_clock_class
) {
453 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field
,
454 ×tamp_begin_raw_value
);
459 clock_value
= bt_ctf_clock_value_create(timestamp_begin_clock_class
,
460 timestamp_begin_raw_value
);
465 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
,
466 ×tamp_begin_ns_signed
);
471 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
474 bt_put(timestamp_begin_field
);
475 bt_put(timestamp_begin_ft
);
476 bt_put(timestamp_begin_clock_class
);
478 return timestamp_begin_ns
;
482 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
488 if (ds_file_info
->path
) {
489 g_string_free(ds_file_info
->path
, TRUE
);
492 ctf_fs_ds_index_destroy(ds_file_info
->index
);
493 g_free(ds_file_info
);
497 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
498 uint64_t begin_ns
, struct ctf_fs_ds_index
*index
)
500 struct ctf_fs_ds_file_info
*ds_file_info
;
502 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
507 ds_file_info
->path
= g_string_new(path
);
508 if (!ds_file_info
->path
) {
509 ctf_fs_ds_file_info_destroy(ds_file_info
);
514 ds_file_info
->begin_ns
= begin_ns
;
515 ds_file_info
->index
= index
;
519 ctf_fs_ds_index_destroy(index
);
524 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
526 if (!ds_file_group
) {
530 if (ds_file_group
->ds_file_infos
) {
531 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
534 bt_put(ds_file_group
->stream
);
535 g_free(ds_file_group
);
539 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
540 struct ctf_fs_trace
*ctf_fs_trace
,
541 struct bt_ctf_stream_class
*stream_class
,
542 uint64_t stream_instance_id
)
544 struct ctf_fs_ds_file_group
*ds_file_group
;
546 assert(stream_class
);
547 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
548 if (!ds_file_group
) {
552 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
553 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
554 if (!ds_file_group
->ds_file_infos
) {
558 if (stream_instance_id
== -1ULL) {
559 ds_file_group
->stream
= bt_ctf_stream_create(
562 ds_file_group
->stream
= bt_ctf_stream_create_with_id(
563 stream_class
, NULL
, stream_instance_id
);
566 if (!ds_file_group
->stream
) {
570 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
575 ctf_fs_ds_file_group_destroy(ds_file_group
);
576 ds_file_group
= NULL
;
579 return ds_file_group
;
583 int ctf_fs_ds_file_group_add_ds_file_info(
584 struct ctf_fs_ds_file_group
*ds_file_group
,
585 const char *path
, uint64_t begin_ns
,
586 struct ctf_fs_ds_index
*index
)
588 struct ctf_fs_ds_file_info
*ds_file_info
;
592 /* Onwership of index is transferred. */
593 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
, index
);
599 /* Find a spot to insert this one */
600 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
601 struct ctf_fs_ds_file_info
*other_ds_file_info
=
602 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
604 if (begin_ns
< other_ds_file_info
->begin_ns
) {
609 if (i
== ds_file_group
->ds_file_infos
->len
) {
614 g_ptr_array_insert(ds_file_group
->ds_file_infos
, i
, ds_file_info
);
619 ctf_fs_ds_file_info_destroy(ds_file_info
);
620 ctf_fs_ds_index_destroy(index
);
627 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
630 struct bt_ctf_field
*packet_header_field
= NULL
;
631 struct bt_ctf_field
*packet_context_field
= NULL
;
632 struct bt_ctf_stream_class
*stream_class
= NULL
;
633 uint64_t stream_instance_id
= -1ULL;
634 uint64_t begin_ns
= -1ULL;
635 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
636 bool add_group
= false;
639 struct ctf_fs_ds_file
*ds_file
;
640 struct ctf_fs_ds_index
*index
= NULL
;
642 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, NULL
, path
);
647 ret
= ctf_fs_ds_file_get_packet_header_context_fields(ds_file
,
648 &packet_header_field
, &packet_context_field
);
650 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
655 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
656 packet_header_field
);
657 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
658 packet_context_field
);
659 stream_class
= stream_class_from_packet_header(ctf_fs_trace
,
660 packet_header_field
);
665 index
= ctf_fs_ds_file_build_index(ds_file
);
667 BT_LOGW("Failed to index CTF stream file \'%s\'",
668 ds_file
->file
->path
->str
);
671 if (begin_ns
== -1ULL) {
673 * No beggining timestamp to sort the stream files
674 * within a stream file group, so consider that this
675 * file must be the only one within its group.
677 stream_instance_id
= -1ULL;
680 if (stream_instance_id
== -1ULL) {
682 * No stream instance ID or no beginning timestamp:
683 * create a unique stream file group for this stream
684 * file because, even if there's a stream instance ID,
685 * there's no timestamp to order the file within its
689 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
690 stream_class
, stream_instance_id
);
691 if (!ds_file_group
) {
695 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
696 path
, begin_ns
, index
);
697 /* Ownership of index is transferred. */
707 assert(stream_instance_id
!= -1ULL);
708 assert(begin_ns
!= -1ULL);
710 /* Find an existing stream file group with this ID */
711 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
713 struct bt_ctf_stream_class
*cand_stream_class
;
715 ds_file_group
= g_ptr_array_index(
716 ctf_fs_trace
->ds_file_groups
, i
);
717 id
= bt_ctf_stream_get_id(ds_file_group
->stream
);
718 cand_stream_class
= bt_ctf_stream_get_class(
719 ds_file_group
->stream
);
721 assert(cand_stream_class
);
722 bt_put(cand_stream_class
);
724 if (cand_stream_class
== stream_class
&&
725 (uint64_t) id
== stream_instance_id
) {
729 ds_file_group
= NULL
;
732 if (!ds_file_group
) {
733 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
734 stream_class
, stream_instance_id
);
735 if (!ds_file_group
) {
742 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
743 path
, begin_ns
, index
);
752 ctf_fs_ds_file_group_destroy(ds_file_group
);
756 if (add_group
&& ds_file_group
) {
757 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
759 ctf_fs_ds_file_destroy(ds_file
);
760 ctf_fs_ds_index_destroy(index
);
761 bt_put(packet_header_field
);
762 bt_put(packet_context_field
);
763 bt_put(stream_class
);
768 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
771 const char *basename
;
772 GError
*error
= NULL
;
775 /* Check each file in the path directory, except specific ones */
776 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
778 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
779 ctf_fs_trace
->path
->str
, error
->message
,
784 while ((basename
= g_dir_read_name(dir
))) {
785 struct ctf_fs_file
*file
;
787 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
788 /* Ignore the metadata stream. */
789 BT_LOGD("Ignoring metadata file `%s/%s`",
790 ctf_fs_trace
->path
->str
, basename
);
794 if (basename
[0] == '.') {
795 BT_LOGD("Ignoring hidden file `%s/%s`",
796 ctf_fs_trace
->path
->str
, basename
);
800 /* Create the file. */
801 file
= ctf_fs_file_create();
803 BT_LOGE("Cannot create stream file object for file `%s/%s`",
804 ctf_fs_trace
->path
->str
, basename
);
808 /* Create full path string. */
809 g_string_append_printf(file
->path
, "%s/%s",
810 ctf_fs_trace
->path
->str
, basename
);
811 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
812 BT_LOGD("Ignoring non-regular file `%s`",
814 ctf_fs_file_destroy(file
);
819 ret
= ctf_fs_file_open(file
, "rb");
821 BT_LOGE("Cannot open stream file `%s`", file
->path
->str
);
825 if (file
->size
== 0) {
826 /* Skip empty stream. */
827 BT_LOGD("Ignoring empty file `%s`", file
->path
->str
);
828 ctf_fs_file_destroy(file
);
832 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
835 BT_LOGE("Cannot add stream file `%s` to stream file group",
837 ctf_fs_file_destroy(file
);
841 ctf_fs_file_destroy(file
);
863 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
869 assert(ctf_fs_trace
);
870 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
871 if (!ctf_fs_trace
->cc_prio_map
) {
876 count
= bt_ctf_trace_get_clock_class_count(
877 ctf_fs_trace
->metadata
->trace
);
880 for (i
= 0; i
< count
; i
++) {
881 struct bt_ctf_clock_class
*clock_class
=
882 bt_ctf_trace_get_clock_class_by_index(
883 ctf_fs_trace
->metadata
->trace
, i
);
886 ret
= bt_clock_class_priority_map_add_clock_class(
887 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
900 struct ctf_fs_trace
*ctf_fs_trace_create(const char *path
, const char *name
,
901 struct ctf_fs_metadata_config
*metadata_config
)
903 struct ctf_fs_trace
*ctf_fs_trace
;
906 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
911 ctf_fs_trace
->path
= g_string_new(path
);
912 if (!ctf_fs_trace
->path
) {
916 ctf_fs_trace
->name
= g_string_new(name
);
917 if (!ctf_fs_trace
->name
) {
921 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
922 if (!ctf_fs_trace
->metadata
) {
926 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
927 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
928 if (!ctf_fs_trace
->ds_file_groups
) {
932 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
, metadata_config
);
937 ret
= create_ds_file_groups(ctf_fs_trace
);
942 ret
= create_cc_prio_map(ctf_fs_trace
);
948 * create_ds_file_groups() created all the streams that this
949 * trace needs. There won't be any more. Therefore it is safe to
950 * make this trace static.
952 (void) bt_ctf_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
957 ctf_fs_trace_destroy(ctf_fs_trace
);
964 int path_is_ctf_trace(const char *path
)
966 GString
*metadata_path
= g_string_new(NULL
);
969 if (!metadata_path
) {
974 g_string_printf(metadata_path
, "%s/%s", path
, CTF_FS_METADATA_FILENAME
);
976 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
982 g_string_free(metadata_path
, TRUE
);
987 int add_trace_path(GList
**trace_paths
, const char *path
)
989 GString
*norm_path
= NULL
;
992 norm_path
= bt_common_normalize_path(path
, NULL
);
994 BT_LOGE("Failed to normalize path `%s`.", path
);
999 if (strcmp(norm_path
->str
, "/") == 0) {
1000 BT_LOGE("Opening a trace in `/` is not supported.");
1005 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1006 assert(*trace_paths
);
1011 g_string_free(norm_path
, TRUE
);
1018 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
)
1021 GError
*error
= NULL
;
1023 const char *basename
= NULL
;
1025 /* Check if the starting path is a CTF trace itself */
1026 ret
= path_is_ctf_trace(start_path
);
1033 * Stop recursion: a CTF trace cannot contain another
1036 ret
= add_trace_path(trace_paths
, start_path
);
1040 /* Look for subdirectories */
1041 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1042 /* Starting path is not a directory: end of recursion */
1046 dir
= g_dir_open(start_path
, 0, &error
);
1048 if (error
->code
== G_FILE_ERROR_ACCES
) {
1049 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1050 start_path
, error
->message
, error
->code
);
1054 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1055 start_path
, error
->message
, error
->code
);
1060 while ((basename
= g_dir_read_name(dir
))) {
1061 GString
*sub_path
= g_string_new(NULL
);
1068 g_string_printf(sub_path
, "%s/%s", start_path
, basename
);
1069 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
);
1070 g_string_free(sub_path
, TRUE
);
1082 g_error_free(error
);
1089 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1090 GList
*trace_names
= NULL
;
1092 const char *last_sep
;
1096 * At this point we know that all the trace paths are
1097 * normalized, and so is the base path. This means that
1098 * they are absolute and they don't end with a separator.
1099 * We can simply find the location of the last separator
1100 * in the base path, which gives us the name of the actual
1101 * directory to look into, and use this location as the
1102 * start of each trace name within each trace path.
1106 * Base path: /home/user/my-traces/some-trace
1108 * - /home/user/my-traces/some-trace/host1/trace1
1109 * - /home/user/my-traces/some-trace/host1/trace2
1110 * - /home/user/my-traces/some-trace/host2/trace
1111 * - /home/user/my-traces/some-trace/other-trace
1113 * In this case the trace names are:
1115 * - some-trace/host1/trace1
1116 * - some-trace/host1/trace2
1117 * - some-trace/host2/trace
1118 * - some-trace/other-trace
1120 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1122 /* We know there's at least one separator */
1125 /* Distance to base */
1126 base_dist
= last_sep
- base_path
+ 1;
1128 /* Create the trace names */
1129 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1130 GString
*trace_name
= g_string_new(NULL
);
1131 GString
*trace_path
= node
->data
;
1134 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1135 trace_names
= g_list_append(trace_names
, trace_name
);
1142 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1143 const char *path_param
)
1145 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1147 GString
*norm_path
= NULL
;
1148 GList
*trace_paths
= NULL
;
1149 GList
*trace_names
= NULL
;
1153 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1155 BT_LOGE("Failed to normalize path: `%s`.",
1160 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
);
1166 BT_LOGE("No CTF traces recursively found in `%s`.",
1171 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1173 BT_LOGE("Cannot create trace names from trace paths.");
1177 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1178 tp_node
= g_list_next(tp_node
),
1179 tn_node
= g_list_next(tn_node
)) {
1180 GString
*trace_path
= tp_node
->data
;
1181 GString
*trace_name
= tn_node
->data
;
1183 ctf_fs_trace
= ctf_fs_trace_create(trace_path
->str
,
1184 trace_name
->str
, &ctf_fs
->metadata_config
);
1185 if (!ctf_fs_trace
) {
1186 BT_LOGE("Cannot create trace for `%s`.",
1191 ret
= create_ports_for_trace(ctf_fs
, ctf_fs_trace
);
1196 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1197 ctf_fs_trace
= NULL
;
1204 ctf_fs_trace_destroy(ctf_fs_trace
);
1207 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1208 if (tp_node
->data
) {
1209 g_string_free(tp_node
->data
, TRUE
);
1213 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1214 if (tn_node
->data
) {
1215 g_string_free(tn_node
->data
, TRUE
);
1220 g_list_free(trace_paths
);
1224 g_list_free(trace_names
);
1228 g_string_free(norm_path
, TRUE
);
1235 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1236 struct bt_value
*params
)
1238 struct ctf_fs_component
*ctf_fs
;
1239 struct bt_value
*value
= NULL
;
1240 const char *path_param
;
1243 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1248 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1252 * We don't need to get a new reference here because as long as
1253 * our private ctf_fs_component object exists, the containing
1254 * private component should also exist.
1256 ctf_fs
->priv_comp
= priv_comp
;
1257 value
= bt_value_map_get(params
, "path");
1258 if (!bt_value_is_string(value
)) {
1262 ret
= bt_value_string_get(value
, &path_param
);
1265 value
= bt_value_map_get(params
, "clock-class-offset-s");
1267 if (!bt_value_is_integer(value
)) {
1268 BT_LOGE("clock-class-offset-s should be an integer");
1271 ret
= bt_value_integer_get(value
,
1272 &ctf_fs
->metadata_config
.clock_class_offset_s
);
1277 value
= bt_value_map_get(params
, "clock-class-offset-ns");
1279 if (!bt_value_is_integer(value
)) {
1280 BT_LOGE("clock-class-offset-ns should be an integer");
1283 ret
= bt_value_integer_get(value
,
1284 &ctf_fs
->metadata_config
.clock_class_offset_ns
);
1289 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1290 if (!ctf_fs
->port_data
) {
1294 ctf_fs
->traces
= g_ptr_array_new_with_free_func(
1295 ctf_fs_trace_destroy_notifier
);
1296 if (!ctf_fs
->traces
) {
1300 ret
= create_ctf_fs_traces(ctf_fs
, path_param
);
1308 ctf_fs_destroy(ctf_fs
);
1310 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1319 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1320 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1322 struct ctf_fs_component
*ctf_fs
;
1323 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1325 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1327 ret
= BT_COMPONENT_STATUS_ERROR
;
1334 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
1335 const char *object
, struct bt_value
*params
)
1337 struct bt_value
*result
= NULL
;
1339 if (!strcmp(object
, "metadata-info")) {
1340 result
= metadata_info_query(comp_class
, params
);
1341 } else if (!strcmp(object
, "trace-info")) {
1342 result
= trace_info_query(comp_class
, params
);
1344 BT_LOGE("Unknown query object `%s`", object
);