4 * Babeltrace CTF file system Reader Component
6 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <babeltrace/common-internal.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/stream.h>
32 #include <babeltrace/ctf-ir/fields.h>
33 #include <babeltrace/graph/private-port.h>
34 #include <babeltrace/graph/private-component.h>
35 #include <babeltrace/graph/private-component-source.h>
36 #include <babeltrace/graph/private-notification-iterator.h>
37 #include <babeltrace/graph/component.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/clock-class-priority-map.h>
40 #include <plugins-common.h>
47 #include "data-stream-file.h"
49 #include "../common/metadata/decoder.h"
50 #include "../common/notif-iter/notif-iter.h"
53 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
57 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data
*notif_iter_data
)
59 struct ctf_fs_ds_file_info
*ds_file_info
;
62 assert(notif_iter_data
->ds_file_info_index
<
63 notif_iter_data
->ds_file_group
->ds_file_infos
->len
);
64 ds_file_info
= g_ptr_array_index(
65 notif_iter_data
->ds_file_group
->ds_file_infos
,
66 notif_iter_data
->ds_file_info_index
);
68 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
69 notif_iter_data
->ds_file
= ctf_fs_ds_file_create(
70 notif_iter_data
->ds_file_group
->ctf_fs_trace
,
71 notif_iter_data
->notif_iter
,
72 notif_iter_data
->ds_file_group
->stream
,
73 ds_file_info
->path
->str
);
74 if (!notif_iter_data
->ds_file
) {
82 void ctf_fs_notif_iter_data_destroy(
83 struct ctf_fs_notif_iter_data
*notif_iter_data
)
85 if (!notif_iter_data
) {
89 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
91 if (notif_iter_data
->notif_iter
) {
92 bt_ctf_notif_iter_destroy(notif_iter_data
->notif_iter
);
95 g_free(notif_iter_data
);
98 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
99 struct bt_private_notification_iterator
*iterator
)
101 struct bt_notification_iterator_next_return next_ret
;
102 struct ctf_fs_notif_iter_data
*notif_iter_data
=
103 bt_private_notification_iterator_get_user_data(iterator
);
106 assert(notif_iter_data
->ds_file
);
107 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
108 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
109 assert(!next_ret
.notification
);
110 notif_iter_data
->ds_file_info_index
++;
112 if (notif_iter_data
->ds_file_info_index
==
113 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
115 * No more stream files to read: we reached the
122 * Open and start reading the next stream file within
123 * our stream file group.
125 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
127 next_ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
131 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
134 * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END
135 * with a brand new stream file because empty stream
136 * files are not even part of stream file groups, which
137 * means we're sure to get at least one pair of "packet
138 * begin" and "packet end" notifications in the case of
139 * a single, empty packet.
141 assert(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
148 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
150 void *notif_iter_data
=
151 bt_private_notification_iterator_get_user_data(it
);
153 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
156 enum bt_notification_iterator_status
ctf_fs_iterator_init(
157 struct bt_private_notification_iterator
*it
,
158 struct bt_private_port
*port
)
160 struct ctf_fs_port_data
*port_data
;
161 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
162 enum bt_notification_iterator_status ret
=
163 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
166 port_data
= bt_private_port_get_user_data(port
);
168 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
172 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
173 if (!notif_iter_data
) {
174 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
178 notif_iter_data
->notif_iter
= bt_ctf_notif_iter_create(
179 port_data
->ds_file_group
->ctf_fs_trace
->metadata
->trace
,
180 bt_common_get_page_size() * 8,
181 ctf_fs_ds_file_medops
, NULL
);
182 if (!notif_iter_data
->notif_iter
) {
183 BT_LOGE_STR("Cannot create a CTF notification iterator.");
184 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
188 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
189 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
191 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
195 ret
= bt_private_notification_iterator_set_user_data(it
, notif_iter_data
);
200 notif_iter_data
= NULL
;
204 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
206 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
207 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
211 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
216 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
222 if (ctf_fs
->traces
) {
223 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
226 if (ctf_fs
->port_data
) {
227 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
234 void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
240 if (ctf_fs_trace
->ds_file_groups
) {
241 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
244 if (ctf_fs_trace
->path
) {
245 g_string_free(ctf_fs_trace
->path
, TRUE
);
248 if (ctf_fs_trace
->name
) {
249 g_string_free(ctf_fs_trace
->name
, TRUE
);
252 if (ctf_fs_trace
->metadata
) {
253 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
254 g_free(ctf_fs_trace
->metadata
);
257 bt_put(ctf_fs_trace
->cc_prio_map
);
258 g_free(ctf_fs_trace
);
262 void ctf_fs_trace_destroy_notifier(void *data
)
264 struct ctf_fs_trace
*trace
= data
;
265 ctf_fs_trace_destroy(trace
);
268 void ctf_fs_finalize(struct bt_private_component
*component
)
270 void *data
= bt_private_component_get_user_data(component
);
272 ctf_fs_destroy(data
);
276 void port_data_destroy(void *data
) {
277 struct ctf_fs_port_data
*port_data
= data
;
287 GString
*get_stream_instance_unique_name(
288 struct ctf_fs_ds_file_group
*ds_file_group
)
291 struct ctf_fs_ds_file_info
*ds_file_info
;
293 name
= g_string_new(NULL
);
299 * If there's more than one stream file in the stream file
300 * group, the first (earliest) stream file's path is used as
301 * the stream's unique name.
303 assert(ds_file_group
->ds_file_infos
->len
> 0);
304 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
305 g_string_assign(name
, ds_file_info
->path
->str
);
312 int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
313 struct ctf_fs_trace
*ctf_fs_trace
,
314 struct ctf_fs_ds_file_group
*ds_file_group
)
317 struct ctf_fs_port_data
*port_data
= NULL
;
318 GString
*port_name
= NULL
;
320 port_name
= get_stream_instance_unique_name(ds_file_group
);
325 BT_LOGD("Creating one port named `%s`", port_name
->str
);
327 /* Create output port for this file */
328 port_data
= g_new0(struct ctf_fs_port_data
, 1);
333 port_data
->ds_file_group
= ds_file_group
;
334 ret
= bt_private_component_source_add_output_private_port(
335 ctf_fs
->priv_comp
, port_name
->str
, port_data
, NULL
);
340 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
349 g_string_free(port_name
, TRUE
);
352 port_data_destroy(port_data
);
357 int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
358 struct ctf_fs_trace
*ctf_fs_trace
)
363 /* Create one output port for each stream file group */
364 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
365 struct ctf_fs_ds_file_group
*ds_file_group
=
366 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
368 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
,
371 BT_LOGE("Cannot create output port.");
381 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
382 struct bt_ctf_field
*packet_header_field
)
384 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
385 uint64_t stream_instance_id
= -1ULL;
388 if (!packet_header_field
) {
392 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
393 packet_header_field
, "stream_instance_id");
394 if (!stream_instance_id_field
) {
398 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
399 &stream_instance_id
);
401 stream_instance_id
= -1ULL;
406 bt_put(stream_instance_id_field
);
407 return stream_instance_id
;
410 struct bt_ctf_stream_class
*stream_class_from_packet_header(
411 struct ctf_fs_trace
*ctf_fs_trace
,
412 struct bt_ctf_field
*packet_header_field
)
414 struct bt_ctf_field
*stream_id_field
= NULL
;
415 struct bt_ctf_stream_class
*stream_class
= NULL
;
416 uint64_t stream_id
= -1ULL;
419 if (!packet_header_field
) {
420 goto single_stream_class
;
423 stream_id_field
= bt_ctf_field_structure_get_field_by_name(
424 packet_header_field
, "stream_id");
425 if (!stream_id_field
) {
426 goto single_stream_class
;
429 ret
= bt_ctf_field_unsigned_integer_get_value(stream_id_field
,
435 if (stream_id
== -1ULL) {
437 /* Single stream class */
438 if (bt_ctf_trace_get_stream_class_count(
439 ctf_fs_trace
->metadata
->trace
) == 0) {
443 stream_class
= bt_ctf_trace_get_stream_class_by_index(
444 ctf_fs_trace
->metadata
->trace
, 0);
446 stream_class
= bt_ctf_trace_get_stream_class_by_id(
447 ctf_fs_trace
->metadata
->trace
, stream_id
);
451 bt_put(stream_id_field
);
455 uint64_t get_packet_context_timestamp_begin_ns(
456 struct ctf_fs_trace
*ctf_fs_trace
,
457 struct bt_ctf_field
*packet_context_field
)
460 struct bt_ctf_field
*timestamp_begin_field
= NULL
;
461 struct bt_ctf_field_type
*timestamp_begin_ft
= NULL
;
462 uint64_t timestamp_begin_raw_value
= -1ULL;
463 uint64_t timestamp_begin_ns
= -1ULL;
464 int64_t timestamp_begin_ns_signed
;
465 struct bt_ctf_clock_class
*timestamp_begin_clock_class
= NULL
;
466 struct bt_ctf_clock_value
*clock_value
= NULL
;
468 if (!packet_context_field
) {
472 timestamp_begin_field
= bt_ctf_field_structure_get_field_by_name(
473 packet_context_field
, "timestamp_begin");
474 if (!timestamp_begin_field
) {
478 timestamp_begin_ft
= bt_ctf_field_get_type(timestamp_begin_field
);
479 assert(timestamp_begin_ft
);
480 timestamp_begin_clock_class
=
481 bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft
);
482 if (!timestamp_begin_clock_class
) {
486 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field
,
487 ×tamp_begin_raw_value
);
492 clock_value
= bt_ctf_clock_value_create(timestamp_begin_clock_class
,
493 timestamp_begin_raw_value
);
498 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
,
499 ×tamp_begin_ns_signed
);
504 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
507 bt_put(timestamp_begin_field
);
508 bt_put(timestamp_begin_ft
);
509 bt_put(timestamp_begin_clock_class
);
511 return timestamp_begin_ns
;
515 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
521 if (ds_file_info
->path
) {
522 g_string_free(ds_file_info
->path
, TRUE
);
525 ctf_fs_ds_index_destroy(ds_file_info
->index
);
526 g_free(ds_file_info
);
530 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
531 uint64_t begin_ns
, struct ctf_fs_ds_index
*index
)
533 struct ctf_fs_ds_file_info
*ds_file_info
;
535 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
540 ds_file_info
->path
= g_string_new(path
);
541 if (!ds_file_info
->path
) {
542 ctf_fs_ds_file_info_destroy(ds_file_info
);
547 ds_file_info
->begin_ns
= begin_ns
;
548 ds_file_info
->index
= index
;
552 ctf_fs_ds_index_destroy(index
);
557 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
559 if (!ds_file_group
) {
563 if (ds_file_group
->ds_file_infos
) {
564 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
567 bt_put(ds_file_group
->stream
);
568 bt_put(ds_file_group
->stream_class
);
569 g_free(ds_file_group
);
573 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
574 struct ctf_fs_trace
*ctf_fs_trace
,
575 struct bt_ctf_stream_class
*stream_class
,
576 uint64_t stream_instance_id
)
578 struct ctf_fs_ds_file_group
*ds_file_group
;
580 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
581 if (!ds_file_group
) {
585 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
586 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
587 if (!ds_file_group
->ds_file_infos
) {
591 ds_file_group
->stream_id
= stream_instance_id
;
592 assert(stream_class
);
593 ds_file_group
->stream_class
= bt_get(stream_class
);
594 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
598 ctf_fs_ds_file_group_destroy(ds_file_group
);
599 ds_file_group
= NULL
;
602 return ds_file_group
;
606 int ctf_fs_ds_file_group_add_ds_file_info(
607 struct ctf_fs_ds_file_group
*ds_file_group
,
608 const char *path
, uint64_t begin_ns
,
609 struct ctf_fs_ds_index
*index
)
611 struct ctf_fs_ds_file_info
*ds_file_info
;
615 /* Onwership of index is transferred. */
616 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
, index
);
622 /* Find a spot to insert this one */
623 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
624 struct ctf_fs_ds_file_info
*other_ds_file_info
=
625 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
627 if (begin_ns
< other_ds_file_info
->begin_ns
) {
632 if (i
== ds_file_group
->ds_file_infos
->len
) {
637 g_ptr_array_insert(ds_file_group
->ds_file_infos
, i
, ds_file_info
);
642 ctf_fs_ds_file_info_destroy(ds_file_info
);
643 ctf_fs_ds_index_destroy(index
);
650 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
653 struct bt_ctf_field
*packet_header_field
= NULL
;
654 struct bt_ctf_field
*packet_context_field
= NULL
;
655 struct bt_ctf_stream_class
*stream_class
= NULL
;
656 uint64_t stream_instance_id
= -1ULL;
657 uint64_t begin_ns
= -1ULL;
658 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
659 bool add_group
= false;
662 struct ctf_fs_ds_file
*ds_file
= NULL
;
663 struct ctf_fs_ds_index
*index
= NULL
;
664 struct bt_ctf_notif_iter
*notif_iter
= NULL
;
666 notif_iter
= bt_ctf_notif_iter_create(ctf_fs_trace
->metadata
->trace
,
667 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops
, NULL
);
669 BT_LOGE_STR("Cannot create a CTF notification iterator.");
673 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, notif_iter
, NULL
, path
);
678 ret
= ctf_fs_ds_file_get_packet_header_context_fields(ds_file
,
679 &packet_header_field
, &packet_context_field
);
681 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
686 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
687 packet_header_field
);
688 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
689 packet_context_field
);
690 stream_class
= stream_class_from_packet_header(ctf_fs_trace
,
691 packet_header_field
);
696 index
= ctf_fs_ds_file_build_index(ds_file
);
698 BT_LOGW("Failed to index CTF stream file \'%s\'",
699 ds_file
->file
->path
->str
);
702 if (begin_ns
== -1ULL) {
704 * No beggining timestamp to sort the stream files
705 * within a stream file group, so consider that this
706 * file must be the only one within its group.
708 stream_instance_id
= -1ULL;
711 if (stream_instance_id
== -1ULL) {
713 * No stream instance ID or no beginning timestamp:
714 * create a unique stream file group for this stream
715 * file because, even if there's a stream instance ID,
716 * there's no timestamp to order the file within its
719 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
720 stream_class
, stream_instance_id
);
721 if (!ds_file_group
) {
725 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
726 path
, begin_ns
, index
);
727 /* Ownership of index is transferred. */
737 assert(stream_instance_id
!= -1ULL);
738 assert(begin_ns
!= -1ULL);
740 /* Find an existing stream file group with this ID */
741 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
742 ds_file_group
= g_ptr_array_index(
743 ctf_fs_trace
->ds_file_groups
, i
);
745 if (ds_file_group
->stream_class
== stream_class
&&
746 ds_file_group
->stream_id
==
747 stream_instance_id
) {
751 ds_file_group
= NULL
;
754 if (!ds_file_group
) {
755 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
756 stream_class
, stream_instance_id
);
757 if (!ds_file_group
) {
764 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
, path
,
774 ctf_fs_ds_file_group_destroy(ds_file_group
);
778 if (add_group
&& ds_file_group
) {
779 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
782 ctf_fs_ds_file_destroy(ds_file
);
785 bt_ctf_notif_iter_destroy(notif_iter
);
788 ctf_fs_ds_index_destroy(index
);
789 bt_put(packet_header_field
);
790 bt_put(packet_context_field
);
791 bt_put(stream_class
);
796 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
799 const char *basename
;
800 GError
*error
= NULL
;
804 /* Check each file in the path directory, except specific ones */
805 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
807 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
808 ctf_fs_trace
->path
->str
, error
->message
,
813 while ((basename
= g_dir_read_name(dir
))) {
814 struct ctf_fs_file
*file
;
816 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
817 /* Ignore the metadata stream. */
818 BT_LOGD("Ignoring metadata file `%s/%s`",
819 ctf_fs_trace
->path
->str
, basename
);
823 if (basename
[0] == '.') {
824 BT_LOGD("Ignoring hidden file `%s/%s`",
825 ctf_fs_trace
->path
->str
, basename
);
829 /* Create the file. */
830 file
= ctf_fs_file_create();
832 BT_LOGE("Cannot create stream file object for file `%s/%s`",
833 ctf_fs_trace
->path
->str
, basename
);
837 /* Create full path string. */
838 g_string_append_printf(file
->path
, "%s/%s",
839 ctf_fs_trace
->path
->str
, basename
);
840 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
841 BT_LOGD("Ignoring non-regular file `%s`",
843 ctf_fs_file_destroy(file
);
848 ret
= ctf_fs_file_open(file
, "rb");
850 BT_LOGE("Cannot open stream file `%s`", file
->path
->str
);
854 if (file
->size
== 0) {
855 /* Skip empty stream. */
856 BT_LOGD("Ignoring empty file `%s`", file
->path
->str
);
857 ctf_fs_file_destroy(file
);
861 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
864 BT_LOGE("Cannot add stream file `%s` to stream file group",
866 ctf_fs_file_destroy(file
);
870 ctf_fs_file_destroy(file
);
874 * At this point, DS file groupes are created, but their
875 * associated stream objects do not exist yet. This is because
876 * we need to name the created stream object with the data
877 * stream file's path. We have everything we need here to do
880 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
881 struct ctf_fs_ds_file_group
*ds_file_group
=
882 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
883 GString
*name
= get_stream_instance_unique_name(ds_file_group
);
889 if (ds_file_group
->stream_id
== -1ULL) {
891 ds_file_group
->stream
= bt_ctf_stream_create(
892 ds_file_group
->stream_class
, name
->str
);
894 /* Specific stream ID */
895 ds_file_group
->stream
= bt_ctf_stream_create_with_id(
896 ds_file_group
->stream_class
, name
->str
,
897 ds_file_group
->stream_id
);
900 g_string_free(name
, TRUE
);
902 if (!ds_file_group
->stream
) {
903 BT_LOGE("Cannot create stream for DS file group: "
904 "addr=%p, stream-name=\"%s\"",
905 ds_file_group
, name
->str
);
929 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
935 assert(ctf_fs_trace
);
936 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
937 if (!ctf_fs_trace
->cc_prio_map
) {
942 count
= bt_ctf_trace_get_clock_class_count(
943 ctf_fs_trace
->metadata
->trace
);
946 for (i
= 0; i
< count
; i
++) {
947 struct bt_ctf_clock_class
*clock_class
=
948 bt_ctf_trace_get_clock_class_by_index(
949 ctf_fs_trace
->metadata
->trace
, i
);
952 ret
= bt_clock_class_priority_map_add_clock_class(
953 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
966 struct ctf_fs_trace
*ctf_fs_trace_create(const char *path
, const char *name
,
967 struct ctf_fs_metadata_config
*metadata_config
)
969 struct ctf_fs_trace
*ctf_fs_trace
;
972 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
977 ctf_fs_trace
->path
= g_string_new(path
);
978 if (!ctf_fs_trace
->path
) {
982 ctf_fs_trace
->name
= g_string_new(name
);
983 if (!ctf_fs_trace
->name
) {
987 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
988 if (!ctf_fs_trace
->metadata
) {
992 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
993 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
994 if (!ctf_fs_trace
->ds_file_groups
) {
998 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
, metadata_config
);
1003 ret
= create_ds_file_groups(ctf_fs_trace
);
1008 ret
= create_cc_prio_map(ctf_fs_trace
);
1014 * create_ds_file_groups() created all the streams that this
1015 * trace needs. There won't be any more. Therefore it is safe to
1016 * make this trace static.
1018 (void) bt_ctf_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
1023 ctf_fs_trace_destroy(ctf_fs_trace
);
1024 ctf_fs_trace
= NULL
;
1026 return ctf_fs_trace
;
1030 int path_is_ctf_trace(const char *path
)
1032 GString
*metadata_path
= g_string_new(NULL
);
1035 if (!metadata_path
) {
1040 g_string_printf(metadata_path
, "%s/%s", path
, CTF_FS_METADATA_FILENAME
);
1042 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
1048 g_string_free(metadata_path
, TRUE
);
1053 int add_trace_path(GList
**trace_paths
, const char *path
)
1055 GString
*norm_path
= NULL
;
1058 norm_path
= bt_common_normalize_path(path
, NULL
);
1060 BT_LOGE("Failed to normalize path `%s`.", path
);
1065 if (strcmp(norm_path
->str
, "/") == 0) {
1066 BT_LOGE("Opening a trace in `/` is not supported.");
1071 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
1072 assert(*trace_paths
);
1077 g_string_free(norm_path
, TRUE
);
1084 int ctf_fs_find_traces(GList
**trace_paths
, const char *start_path
)
1087 GError
*error
= NULL
;
1089 const char *basename
= NULL
;
1091 /* Check if the starting path is a CTF trace itself */
1092 ret
= path_is_ctf_trace(start_path
);
1099 * Stop recursion: a CTF trace cannot contain another
1102 ret
= add_trace_path(trace_paths
, start_path
);
1106 /* Look for subdirectories */
1107 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1108 /* Starting path is not a directory: end of recursion */
1112 dir
= g_dir_open(start_path
, 0, &error
);
1114 if (error
->code
== G_FILE_ERROR_ACCES
) {
1115 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1116 start_path
, error
->message
, error
->code
);
1120 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1121 start_path
, error
->message
, error
->code
);
1126 while ((basename
= g_dir_read_name(dir
))) {
1127 GString
*sub_path
= g_string_new(NULL
);
1134 g_string_printf(sub_path
, "%s/%s", start_path
, basename
);
1135 ret
= ctf_fs_find_traces(trace_paths
, sub_path
->str
);
1136 g_string_free(sub_path
, TRUE
);
1148 g_error_free(error
);
1155 GList
*ctf_fs_create_trace_names(GList
*trace_paths
, const char *base_path
) {
1156 GList
*trace_names
= NULL
;
1158 const char *last_sep
;
1162 * At this point we know that all the trace paths are
1163 * normalized, and so is the base path. This means that
1164 * they are absolute and they don't end with a separator.
1165 * We can simply find the location of the last separator
1166 * in the base path, which gives us the name of the actual
1167 * directory to look into, and use this location as the
1168 * start of each trace name within each trace path.
1172 * Base path: /home/user/my-traces/some-trace
1174 * - /home/user/my-traces/some-trace/host1/trace1
1175 * - /home/user/my-traces/some-trace/host1/trace2
1176 * - /home/user/my-traces/some-trace/host2/trace
1177 * - /home/user/my-traces/some-trace/other-trace
1179 * In this case the trace names are:
1181 * - some-trace/host1/trace1
1182 * - some-trace/host1/trace2
1183 * - some-trace/host2/trace
1184 * - some-trace/other-trace
1186 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1188 /* We know there's at least one separator */
1191 /* Distance to base */
1192 base_dist
= last_sep
- base_path
+ 1;
1194 /* Create the trace names */
1195 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1196 GString
*trace_name
= g_string_new(NULL
);
1197 GString
*trace_path
= node
->data
;
1200 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1201 trace_names
= g_list_append(trace_names
, trace_name
);
1208 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1209 const char *path_param
)
1211 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1213 GString
*norm_path
= NULL
;
1214 GList
*trace_paths
= NULL
;
1215 GList
*trace_names
= NULL
;
1219 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1221 BT_LOGE("Failed to normalize path: `%s`.",
1226 ret
= ctf_fs_find_traces(&trace_paths
, norm_path
->str
);
1232 BT_LOGE("No CTF traces recursively found in `%s`.",
1237 trace_names
= ctf_fs_create_trace_names(trace_paths
, norm_path
->str
);
1239 BT_LOGE("Cannot create trace names from trace paths.");
1243 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1244 tp_node
= g_list_next(tp_node
),
1245 tn_node
= g_list_next(tn_node
)) {
1246 GString
*trace_path
= tp_node
->data
;
1247 GString
*trace_name
= tn_node
->data
;
1249 ctf_fs_trace
= ctf_fs_trace_create(trace_path
->str
,
1250 trace_name
->str
, &ctf_fs
->metadata_config
);
1251 if (!ctf_fs_trace
) {
1252 BT_LOGE("Cannot create trace for `%s`.",
1257 ret
= create_ports_for_trace(ctf_fs
, ctf_fs_trace
);
1262 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1263 ctf_fs_trace
= NULL
;
1270 ctf_fs_trace_destroy(ctf_fs_trace
);
1273 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1274 if (tp_node
->data
) {
1275 g_string_free(tp_node
->data
, TRUE
);
1279 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1280 if (tn_node
->data
) {
1281 g_string_free(tn_node
->data
, TRUE
);
1286 g_list_free(trace_paths
);
1290 g_list_free(trace_names
);
1294 g_string_free(norm_path
, TRUE
);
1301 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1302 struct bt_value
*params
)
1304 struct ctf_fs_component
*ctf_fs
;
1305 struct bt_value
*value
= NULL
;
1306 const char *path_param
;
1309 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1314 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1318 * We don't need to get a new reference here because as long as
1319 * our private ctf_fs_component object exists, the containing
1320 * private component should also exist.
1322 ctf_fs
->priv_comp
= priv_comp
;
1323 value
= bt_value_map_get(params
, "path");
1324 if (!bt_value_is_string(value
)) {
1328 ret
= bt_value_string_get(value
, &path_param
);
1331 value
= bt_value_map_get(params
, "clock-class-offset-s");
1333 if (!bt_value_is_integer(value
)) {
1334 BT_LOGE("clock-class-offset-s should be an integer");
1337 ret
= bt_value_integer_get(value
,
1338 &ctf_fs
->metadata_config
.clock_class_offset_s
);
1343 value
= bt_value_map_get(params
, "clock-class-offset-ns");
1345 if (!bt_value_is_integer(value
)) {
1346 BT_LOGE("clock-class-offset-ns should be an integer");
1349 ret
= bt_value_integer_get(value
,
1350 &ctf_fs
->metadata_config
.clock_class_offset_ns
);
1355 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1356 if (!ctf_fs
->port_data
) {
1360 ctf_fs
->traces
= g_ptr_array_new_with_free_func(
1361 ctf_fs_trace_destroy_notifier
);
1362 if (!ctf_fs
->traces
) {
1366 ret
= 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
);
1385 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1386 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1388 struct ctf_fs_component
*ctf_fs
;
1389 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1391 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1393 ret
= BT_COMPONENT_STATUS_ERROR
;
1400 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
1401 const char *object
, struct bt_value
*params
)
1403 struct bt_value
*result
= NULL
;
1405 if (!strcmp(object
, "metadata-info")) {
1406 result
= metadata_info_query(comp_class
, params
);
1407 } else if (!strcmp(object
, "trace-info")) {
1408 result
= trace_info_query(comp_class
, params
);
1410 BT_LOGE("Unknown query object `%s`", object
);