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"
51 #define PRINT_ERR_STREAM ctf_fs->error_fp
52 #define PRINT_PREFIX "ctf-fs"
53 #define PRINT_DBG_CHECK ctf_fs_debug
55 #define METADATA_TEXT_SIG "/* CTF 1.8"
61 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data
*notif_iter_data
)
63 struct ctf_fs_ds_file_info
*ds_file_info
;
66 assert(notif_iter_data
->ds_file_info_index
<
67 notif_iter_data
->ds_file_group
->ds_file_infos
->len
);
68 ds_file_info
= g_ptr_array_index(
69 notif_iter_data
->ds_file_group
->ds_file_infos
,
70 notif_iter_data
->ds_file_info_index
);
72 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
73 notif_iter_data
->ds_file
= ctf_fs_ds_file_create(
74 notif_iter_data
->ds_file_group
->ctf_fs_trace
,
75 notif_iter_data
->ds_file_group
->stream
,
76 ds_file_info
->path
->str
, true);
77 if (!notif_iter_data
->ds_file
) {
85 void ctf_fs_notif_iter_data_destroy(
86 struct ctf_fs_notif_iter_data
*notif_iter_data
)
88 if (!notif_iter_data
) {
92 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
93 g_free(notif_iter_data
);
96 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
97 struct bt_private_notification_iterator
*iterator
)
99 struct bt_notification_iterator_next_return next_ret
;
100 struct ctf_fs_notif_iter_data
*notif_iter_data
=
101 bt_private_notification_iterator_get_user_data(iterator
);
104 assert(notif_iter_data
->ds_file
);
105 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
106 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
107 assert(!next_ret
.notification
);
108 notif_iter_data
->ds_file_info_index
++;
110 if (notif_iter_data
->ds_file_info_index
==
111 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
113 * No more stream files to read: we reached the
120 * Open and start reading the next stream file within
121 * our stream file group.
123 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
125 next_ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
129 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
132 * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END
133 * with a brand new stream file because empty stream
134 * files are not even part of stream file groups, which
135 * means we're sure to get at least one pair of "packet
136 * begin" and "packet end" notifications in the case of
137 * a single, empty packet.
139 assert(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
146 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
148 void *notif_iter_data
=
149 bt_private_notification_iterator_get_user_data(it
);
151 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
154 enum bt_notification_iterator_status
ctf_fs_iterator_init(
155 struct bt_private_notification_iterator
*it
,
156 struct bt_private_port
*port
)
158 struct ctf_fs_port_data
*port_data
;
159 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
160 enum bt_notification_iterator_status ret
=
161 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
164 port_data
= bt_private_port_get_user_data(port
);
166 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
170 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
171 if (!notif_iter_data
) {
172 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
176 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
177 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
179 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
183 ret
= bt_private_notification_iterator_set_user_data(it
, notif_iter_data
);
188 notif_iter_data
= NULL
;
192 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
194 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
195 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
199 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
204 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
210 if (ctf_fs
->traces
) {
211 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
214 if (ctf_fs
->port_data
) {
215 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
222 void ctf_fs_trace_destroy(void *data
)
224 struct ctf_fs_trace
*ctf_fs_trace
= data
;
230 if (ctf_fs_trace
->ds_file_groups
) {
231 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
234 if (ctf_fs_trace
->path
) {
235 g_string_free(ctf_fs_trace
->path
, TRUE
);
238 if (ctf_fs_trace
->name
) {
239 g_string_free(ctf_fs_trace
->name
, TRUE
);
242 if (ctf_fs_trace
->metadata
) {
243 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
244 g_free(ctf_fs_trace
->metadata
);
247 bt_put(ctf_fs_trace
->cc_prio_map
);
248 g_free(ctf_fs_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_trace
*ctf_fs_trace
,
271 struct ctf_fs_ds_file_group
*ds_file_group
)
274 struct ctf_fs_port_data
*port_data
= NULL
;
275 GString
*port_name
= NULL
;
276 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
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 PDBG("Creating one port named `%s`\n", 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_trace
*ctf_fs_trace
)
328 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
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_trace
, ds_file_group
);
338 PERR("Cannot create output port.\n");
347 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
348 struct bt_ctf_field
*packet_header_field
)
350 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
351 uint64_t stream_instance_id
= -1ULL;
354 if (!packet_header_field
) {
358 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
359 packet_header_field
, "stream_instance_id");
360 if (!stream_instance_id_field
) {
364 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
365 &stream_instance_id
);
367 stream_instance_id
= -1ULL;
372 bt_put(stream_instance_id_field
);
373 return stream_instance_id
;
376 struct bt_ctf_stream_class
*stream_class_from_packet_header(
377 struct ctf_fs_trace
*ctf_fs_trace
,
378 struct bt_ctf_field
*packet_header_field
)
380 struct bt_ctf_field
*stream_id_field
= NULL
;
381 struct bt_ctf_stream_class
*stream_class
= NULL
;
382 uint64_t stream_id
= -1ULL;
385 if (!packet_header_field
) {
386 goto single_stream_class
;
389 stream_id_field
= bt_ctf_field_structure_get_field_by_name(
390 packet_header_field
, "stream_id");
391 if (!stream_id_field
) {
395 ret
= bt_ctf_field_unsigned_integer_get_value(stream_id_field
,
401 if (stream_id
== -1ULL) {
403 /* Single stream class */
404 if (bt_ctf_trace_get_stream_class_count(
405 ctf_fs_trace
->metadata
->trace
) == 0) {
409 stream_class
= bt_ctf_trace_get_stream_class_by_index(
410 ctf_fs_trace
->metadata
->trace
, 0);
412 stream_class
= bt_ctf_trace_get_stream_class_by_id(
413 ctf_fs_trace
->metadata
->trace
, stream_id
);
417 bt_put(stream_id_field
);
421 uint64_t get_packet_context_timestamp_begin_ns(
422 struct ctf_fs_trace
*ctf_fs_trace
,
423 struct bt_ctf_field
*packet_context_field
)
426 struct bt_ctf_field
*timestamp_begin_field
= NULL
;
427 struct bt_ctf_field_type
*timestamp_begin_ft
= NULL
;
428 uint64_t timestamp_begin_raw_value
= -1ULL;
429 uint64_t timestamp_begin_ns
= -1ULL;
430 int64_t timestamp_begin_ns_signed
;
431 struct bt_ctf_clock_class
*timestamp_begin_clock_class
= NULL
;
432 struct bt_ctf_clock_value
*clock_value
= NULL
;
434 if (!packet_context_field
) {
438 timestamp_begin_field
= bt_ctf_field_structure_get_field_by_name(
439 packet_context_field
, "timestamp_begin");
440 if (!timestamp_begin_field
) {
444 timestamp_begin_ft
= bt_ctf_field_get_type(timestamp_begin_field
);
445 assert(timestamp_begin_ft
);
446 timestamp_begin_clock_class
=
447 bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft
);
448 if (!timestamp_begin_clock_class
) {
452 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field
,
453 ×tamp_begin_raw_value
);
458 clock_value
= bt_ctf_clock_value_create(timestamp_begin_clock_class
,
459 timestamp_begin_raw_value
);
464 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
,
465 ×tamp_begin_ns_signed
);
470 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
473 bt_put(timestamp_begin_field
);
474 bt_put(timestamp_begin_ft
);
475 bt_put(timestamp_begin_clock_class
);
477 return timestamp_begin_ns
;
481 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
487 if (ds_file_info
->path
) {
488 g_string_free(ds_file_info
->path
, TRUE
);
491 g_free(ds_file_info
);
495 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
498 struct ctf_fs_ds_file_info
*ds_file_info
;
500 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
505 ds_file_info
->path
= g_string_new(path
);
506 if (!ds_file_info
->path
) {
507 ctf_fs_ds_file_info_destroy(ds_file_info
);
512 ds_file_info
->begin_ns
= begin_ns
;
519 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
521 if (!ds_file_group
) {
525 if (ds_file_group
->ds_file_infos
) {
526 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
529 bt_put(ds_file_group
->stream
);
530 g_free(ds_file_group
);
534 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
535 struct ctf_fs_trace
*ctf_fs_trace
,
536 struct bt_ctf_stream_class
*stream_class
,
537 uint64_t stream_instance_id
)
539 struct ctf_fs_ds_file_group
*ds_file_group
;
541 assert(stream_class
);
542 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
543 if (!ds_file_group
) {
547 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
548 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
549 if (!ds_file_group
->ds_file_infos
) {
553 if (stream_instance_id
== -1ULL) {
554 ds_file_group
->stream
= bt_ctf_stream_create(
557 ds_file_group
->stream
= bt_ctf_stream_create_with_id(
558 stream_class
, NULL
, stream_instance_id
);
561 if (!ds_file_group
->stream
) {
565 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
570 ctf_fs_ds_file_group_destroy(ds_file_group
);
571 ds_file_group
= NULL
;
574 return ds_file_group
;
578 int ctf_fs_ds_file_group_add_ds_file_info(
579 struct ctf_fs_ds_file_group
*ds_file_group
,
580 const char *path
, uint64_t begin_ns
)
582 struct ctf_fs_ds_file_info
*ds_file_info
;
586 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
);
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 if (i
== ds_file_group
->ds_file_infos
->len
) {
606 g_ptr_array_insert(ds_file_group
->ds_file_infos
, i
, ds_file_info
);
611 ctf_fs_ds_file_info_destroy(ds_file_info
);
619 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
622 struct bt_ctf_field
*packet_header_field
= NULL
;
623 struct bt_ctf_field
*packet_context_field
= NULL
;
624 struct bt_ctf_stream_class
*stream_class
= NULL
;
625 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
626 uint64_t stream_instance_id
= -1ULL;
627 uint64_t begin_ns
= -1ULL;
628 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
629 bool add_group
= false;
633 ret
= ctf_fs_ds_file_get_packet_header_context_fields(
634 ctf_fs_trace
, path
, &packet_header_field
,
635 &packet_context_field
);
637 PERR("Cannot get stream file's first packet's header and context fields (`%s`).\n",
642 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
643 packet_header_field
);
644 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
645 packet_context_field
);
646 stream_class
= stream_class_from_packet_header(ctf_fs_trace
,
647 packet_header_field
);
652 if (begin_ns
== -1ULL) {
654 * No beggining timestamp to sort the stream files
655 * within a stream file group, so consider that this
656 * file must be the only one within its group.
658 stream_instance_id
= -1ULL;
661 if (stream_instance_id
== -1ULL) {
663 * No stream instance ID or no beginning timestamp:
664 * create a unique stream file group for this stream
665 * file because, even if there's a stream instance ID,
666 * there's no timestamp to order the file within its
670 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
671 stream_class
, stream_instance_id
);
672 if (!ds_file_group
) {
676 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
686 assert(stream_instance_id
!= -1ULL);
687 assert(begin_ns
!= -1ULL);
689 /* Find an existing stream file group with this ID */
690 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
692 struct bt_ctf_stream_class
*cand_stream_class
;
694 ds_file_group
= g_ptr_array_index(
695 ctf_fs_trace
->ds_file_groups
, i
);
696 id
= bt_ctf_stream_get_id(ds_file_group
->stream
);
697 cand_stream_class
= bt_ctf_stream_get_class(
698 ds_file_group
->stream
);
700 assert(cand_stream_class
);
701 bt_put(cand_stream_class
);
703 if (cand_stream_class
== stream_class
&&
704 (uint64_t) id
== stream_instance_id
) {
708 ds_file_group
= NULL
;
711 if (!ds_file_group
) {
712 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
713 stream_class
, stream_instance_id
);
714 if (!ds_file_group
) {
721 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
730 ctf_fs_ds_file_group_destroy(ds_file_group
);
734 if (add_group
&& ds_file_group
) {
735 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
738 bt_put(packet_header_field
);
739 bt_put(packet_context_field
);
740 bt_put(stream_class
);
745 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
748 const char *basename
;
749 GError
*error
= NULL
;
751 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
753 /* Check each file in the path directory, except specific ones */
754 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
756 PERR("Cannot open directory `%s`: %s (code %d)\n",
757 ctf_fs_trace
->path
->str
, error
->message
,
762 while ((basename
= g_dir_read_name(dir
))) {
763 struct ctf_fs_file
*file
;
765 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
766 /* Ignore the metadata stream. */
767 PDBG("Ignoring metadata file `%s/%s`\n",
768 ctf_fs_trace
->path
->str
, basename
);
772 if (basename
[0] == '.') {
773 PDBG("Ignoring hidden file `%s/%s`\n",
774 ctf_fs_trace
->path
->str
, basename
);
778 /* Create the file. */
779 file
= ctf_fs_file_create(ctf_fs
);
781 PERR("Cannot create stream file object for file `%s/%s`\n",
782 ctf_fs_trace
->path
->str
, basename
);
786 /* Create full path string. */
787 g_string_append_printf(file
->path
, "%s/%s",
788 ctf_fs_trace
->path
->str
, basename
);
789 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
790 PDBG("Ignoring non-regular file `%s`\n",
792 ctf_fs_file_destroy(file
);
797 ret
= ctf_fs_file_open(ctf_fs
, file
, "rb");
799 PERR("Cannot open stream file `%s`\n", file
->path
->str
);
803 if (file
->size
== 0) {
804 /* Skip empty stream. */
805 PDBG("Ignoring empty file `%s`\n", file
->path
->str
);
806 ctf_fs_file_destroy(file
);
810 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
813 PDBG("Cannot add stream file `%s` to stream file group\n",
815 ctf_fs_file_destroy(file
);
819 ctf_fs_file_destroy(file
);
841 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
847 assert(ctf_fs_trace
);
848 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
849 if (!ctf_fs_trace
->cc_prio_map
) {
854 count
= bt_ctf_trace_get_clock_class_count(
855 ctf_fs_trace
->metadata
->trace
);
858 for (i
= 0; i
< count
; i
++) {
859 struct bt_ctf_clock_class
*clock_class
=
860 bt_ctf_trace_get_clock_class_by_index(
861 ctf_fs_trace
->metadata
->trace
, i
);
864 ret
= bt_clock_class_priority_map_add_clock_class(
865 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
878 struct ctf_fs_trace
*ctf_fs_trace_create(struct ctf_fs_component
*ctf_fs
,
879 const char *path
, const char *name
)
881 struct ctf_fs_trace
*ctf_fs_trace
;
884 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
889 ctf_fs_trace
->ctf_fs
= ctf_fs
;
890 ctf_fs_trace
->path
= g_string_new(path
);
891 if (!ctf_fs_trace
->path
) {
895 ctf_fs_trace
->name
= g_string_new(name
);
896 if (!ctf_fs_trace
->name
) {
900 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
901 if (!ctf_fs_trace
->metadata
) {
905 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
906 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
907 if (!ctf_fs_trace
->ds_file_groups
) {
911 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
);
916 ret
= create_ds_file_groups(ctf_fs_trace
);
921 ret
= create_cc_prio_map(ctf_fs_trace
);
926 ret
= create_ports_for_trace(ctf_fs_trace
);
932 * create_ds_file_groups() created all the streams that this
933 * trace needs. There won't be any more. Therefore it is safe to
934 * make this trace static.
936 (void) bt_ctf_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
941 ctf_fs_trace_destroy(ctf_fs_trace
);
948 int path_is_ctf_trace(const char *path
)
950 GString
*metadata_path
= g_string_new(NULL
);
953 if (!metadata_path
) {
958 g_string_printf(metadata_path
, "%s/%s", path
, CTF_FS_METADATA_FILENAME
);
960 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
966 g_string_free(metadata_path
, TRUE
);
971 int add_trace_path(struct ctf_fs_component
*ctf_fs
, GList
**trace_paths
,
974 GString
*norm_path
= NULL
;
977 norm_path
= bt_common_normalize_path(path
, NULL
);
979 PERR("Failed to normalize path `%s`.\n", path
);
984 if (strcmp(norm_path
->str
, "/") == 0) {
985 PERR("Opening a trace in `/` is not supported.\n");
990 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
991 assert(*trace_paths
);
996 g_string_free(norm_path
, TRUE
);
1003 int find_ctf_traces(struct ctf_fs_component
*ctf_fs
,
1004 GList
**trace_paths
, const char *start_path
)
1007 GError
*error
= NULL
;
1009 const char *basename
= NULL
;
1011 /* Check if the starting path is a CTF trace itself */
1012 ret
= path_is_ctf_trace(start_path
);
1019 * Stop recursion: a CTF trace cannot contain another
1022 ret
= add_trace_path(ctf_fs
, trace_paths
, start_path
);
1026 /* Look for subdirectories */
1027 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1028 /* Starting path is not a directory: end of recursion */
1032 dir
= g_dir_open(start_path
, 0, &error
);
1034 if (error
->code
== G_FILE_ERROR_ACCES
) {
1035 PDBG("Cannot open directory `%s`: %s (code %d): continuing\n",
1036 start_path
, error
->message
, error
->code
);
1040 PERR("Cannot open directory `%s`: %s (code %d)\n",
1041 start_path
, error
->message
, error
->code
);
1046 while ((basename
= g_dir_read_name(dir
))) {
1047 GString
*sub_path
= g_string_new(NULL
);
1054 g_string_printf(sub_path
, "%s/%s", start_path
, basename
);
1055 ret
= find_ctf_traces(ctf_fs
, trace_paths
, sub_path
->str
);
1056 g_string_free(sub_path
, TRUE
);
1068 g_error_free(error
);
1075 GList
*create_trace_names(GList
*trace_paths
, const char *base_path
) {
1076 GList
*trace_names
= NULL
;
1078 const char *last_sep
;
1082 * At this point we know that all the trace paths are
1083 * normalized, and so is the base path. This means that
1084 * they are absolute and they don't end with a separator.
1085 * We can simply find the location of the last separator
1086 * in the base path, which gives us the name of the actual
1087 * directory to look into, and use this location as the
1088 * start of each trace name within each trace path.
1092 * Base path: /home/user/my-traces/some-trace
1094 * - /home/user/my-traces/some-trace/host1/trace1
1095 * - /home/user/my-traces/some-trace/host1/trace2
1096 * - /home/user/my-traces/some-trace/host2/trace
1097 * - /home/user/my-traces/some-trace/other-trace
1099 * In this case the trace names are:
1101 * - some-trace/host1/trace1
1102 * - some-trace/host1/trace2
1103 * - some-trace/host2/trace
1104 * - some-trace/other-trace
1106 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1108 /* We know there's at least one separator */
1111 /* Distance to base */
1112 base_dist
= last_sep
- base_path
+ 1;
1114 /* Create the trace names */
1115 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1116 GString
*trace_name
= g_string_new(NULL
);
1117 GString
*trace_path
= node
->data
;
1120 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1121 trace_names
= g_list_append(trace_names
, trace_name
);
1128 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1129 const char *path_param
)
1131 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1133 GString
*norm_path
= NULL
;
1134 GList
*trace_paths
= NULL
;
1135 GList
*trace_names
= NULL
;
1139 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1141 PERR("Failed to normalize path: `%s`.\n",
1146 ret
= find_ctf_traces(ctf_fs
, &trace_paths
, norm_path
->str
);
1152 PERR("No CTF traces recursively found in `%s`.\n",
1157 trace_names
= create_trace_names(trace_paths
, norm_path
->str
);
1159 PERR("Cannot create trace names from trace paths.\n");
1163 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1164 tp_node
= g_list_next(tp_node
),
1165 tn_node
= g_list_next(tn_node
)) {
1166 GString
*trace_path
= tp_node
->data
;
1167 GString
*trace_name
= tn_node
->data
;
1169 ctf_fs_trace
= ctf_fs_trace_create(ctf_fs
, trace_path
->str
,
1171 if (!ctf_fs_trace
) {
1172 PERR("Cannot create trace for `%s`.\n",
1177 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1178 ctf_fs_trace
= NULL
;
1185 ctf_fs_trace_destroy(ctf_fs_trace
);
1188 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1189 if (tp_node
->data
) {
1190 g_string_free(tp_node
->data
, TRUE
);
1194 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1195 if (tn_node
->data
) {
1196 g_string_free(tn_node
->data
, TRUE
);
1201 g_list_free(trace_paths
);
1205 g_list_free(trace_names
);
1209 g_string_free(norm_path
, TRUE
);
1216 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1217 struct bt_value
*params
)
1219 struct ctf_fs_component
*ctf_fs
;
1220 struct bt_value
*value
= NULL
;
1221 const char *path_param
;
1224 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1229 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1233 * We don't need to get a new reference here because as long as
1234 * our private ctf_fs_component object exists, the containing
1235 * private component should also exist.
1237 ctf_fs
->priv_comp
= priv_comp
;
1238 value
= bt_value_map_get(params
, "path");
1239 if (!bt_value_is_string(value
)) {
1243 ret
= bt_value_string_get(value
, &path_param
);
1246 value
= bt_value_map_get(params
, "offset-s");
1250 if (!bt_value_is_integer(value
)) {
1252 "offset-s should be an integer\n");
1255 ret
= bt_value_integer_get(value
, &offset
);
1257 ctf_fs
->options
.clock_offset
= offset
;
1261 value
= bt_value_map_get(params
, "offset-ns");
1265 if (!bt_value_is_integer(value
)) {
1267 "offset-ns should be an integer\n");
1270 ret
= bt_value_integer_get(value
, &offset
);
1272 ctf_fs
->options
.clock_offset_ns
= offset
;
1276 ctf_fs
->error_fp
= stderr
;
1277 ctf_fs
->page_size
= bt_common_get_page_size();
1278 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1279 if (!ctf_fs
->port_data
) {
1283 ctf_fs
->traces
= g_ptr_array_new_with_free_func(ctf_fs_trace_destroy
);
1284 if (!ctf_fs
->traces
) {
1288 ret
= create_ctf_fs_traces(ctf_fs
, path_param
);
1296 ctf_fs_destroy(ctf_fs
);
1298 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1307 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1308 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1310 struct ctf_fs_component
*ctf_fs
;
1311 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1313 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
1314 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1316 ret
= BT_COMPONENT_STATUS_ERROR
;
1323 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
1324 const char *object
, struct bt_value
*params
)
1326 struct bt_value
*results
= NULL
;
1327 struct bt_value
*path_value
= NULL
;
1328 char *metadata_text
= NULL
;
1329 FILE *metadata_fp
= NULL
;
1330 GString
*g_metadata_text
= NULL
;
1332 if (strcmp(object
, "metadata-info") == 0) {
1338 results
= bt_value_map_create();
1343 if (!bt_value_is_map(params
)) {
1345 "Query parameters is not a map value object\n");
1349 path_value
= bt_value_map_get(params
, "path");
1350 ret
= bt_value_string_get(path_value
, &path
);
1353 "Cannot get `path` string parameter\n");
1358 metadata_fp
= ctf_fs_metadata_open_file(path
);
1361 "Cannot open trace at path `%s`\n", path
);
1365 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
1368 if (is_packetized
) {
1369 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
1370 metadata_fp
, &metadata_text
, bo
);
1373 "Cannot decode packetized metadata file\n");
1379 fseek(metadata_fp
, 0, SEEK_END
);
1380 filesize
= ftell(metadata_fp
);
1381 rewind(metadata_fp
);
1382 metadata_text
= malloc(filesize
+ 1);
1383 if (!metadata_text
) {
1385 "Cannot allocate buffer for metadata text\n");
1389 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
1392 "Cannot read metadata file\n");
1396 metadata_text
[filesize
] = '\0';
1399 g_metadata_text
= g_string_new(NULL
);
1400 if (!g_metadata_text
) {
1404 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
1405 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
1406 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
1407 g_string_append(g_metadata_text
, " */\n\n");
1410 g_string_append(g_metadata_text
, metadata_text
);
1412 ret
= bt_value_map_insert_string(results
, "text",
1413 g_metadata_text
->str
);
1415 fprintf(stderr
, "Cannot insert metadata text into results\n");
1419 ret
= bt_value_map_insert_bool(results
, "is-packetized",
1422 fprintf(stderr
, "Cannot insert is packetized into results\n");
1426 fprintf(stderr
, "Unknown query object `%s`\n", object
);
1437 free(metadata_text
);
1439 if (g_metadata_text
) {
1440 g_string_free(g_metadata_text
, TRUE
);
1444 fclose(metadata_fp
);