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>
48 #include "data-stream-file.h"
50 #include "../common/metadata/decoder.h"
52 #define PRINT_ERR_STREAM ctf_fs->error_fp
53 #define PRINT_PREFIX "ctf-fs"
54 #define PRINT_DBG_CHECK ctf_fs_debug
56 #define METADATA_TEXT_SIG "/* CTF 1.8"
62 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data
*notif_iter_data
)
64 struct ctf_fs_ds_file_info
*ds_file_info
;
67 assert(notif_iter_data
->ds_file_info_index
<
68 notif_iter_data
->ds_file_group
->ds_file_infos
->len
);
69 ds_file_info
= g_ptr_array_index(
70 notif_iter_data
->ds_file_group
->ds_file_infos
,
71 notif_iter_data
->ds_file_info_index
);
73 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
74 notif_iter_data
->ds_file
= ctf_fs_ds_file_create(
75 notif_iter_data
->ds_file_group
->ctf_fs_trace
,
76 notif_iter_data
->ds_file_group
->stream
,
77 ds_file_info
->path
->str
, true);
78 if (!notif_iter_data
->ds_file
) {
86 void ctf_fs_notif_iter_data_destroy(
87 struct ctf_fs_notif_iter_data
*notif_iter_data
)
89 if (!notif_iter_data
) {
93 ctf_fs_ds_file_destroy(notif_iter_data
->ds_file
);
94 g_free(notif_iter_data
);
97 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
98 struct bt_private_notification_iterator
*iterator
)
100 struct bt_notification_iterator_next_return next_ret
;
101 struct ctf_fs_notif_iter_data
*notif_iter_data
=
102 bt_private_notification_iterator_get_user_data(iterator
);
105 assert(notif_iter_data
->ds_file
);
106 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
107 if (next_ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
108 assert(!next_ret
.notification
);
109 notif_iter_data
->ds_file_info_index
++;
111 if (notif_iter_data
->ds_file_info_index
==
112 notif_iter_data
->ds_file_group
->ds_file_infos
->len
) {
114 * No more stream files to read: we reached the
121 * Open and start reading the next stream file within
122 * our stream file group.
124 ret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
126 next_ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
130 next_ret
= ctf_fs_ds_file_next(notif_iter_data
->ds_file
);
133 * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END
134 * with a brand new stream file because empty stream
135 * files are not even part of stream file groups, which
136 * means we're sure to get at least one pair of "packet
137 * begin" and "packet end" notifications in the case of
138 * a single, empty packet.
140 assert(next_ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
147 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
149 void *notif_iter_data
=
150 bt_private_notification_iterator_get_user_data(it
);
152 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
155 enum bt_notification_iterator_status
ctf_fs_iterator_init(
156 struct bt_private_notification_iterator
*it
,
157 struct bt_private_port
*port
)
159 struct ctf_fs_port_data
*port_data
;
160 struct ctf_fs_notif_iter_data
*notif_iter_data
= NULL
;
161 enum bt_notification_iterator_status ret
=
162 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
165 port_data
= bt_private_port_get_user_data(port
);
167 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
171 notif_iter_data
= g_new0(struct ctf_fs_notif_iter_data
, 1);
172 if (!notif_iter_data
) {
173 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
177 notif_iter_data
->ds_file_group
= port_data
->ds_file_group
;
178 iret
= notif_iter_data_set_current_ds_file(notif_iter_data
);
180 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
184 ret
= bt_private_notification_iterator_set_user_data(it
, notif_iter_data
);
189 notif_iter_data
= NULL
;
193 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
195 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
196 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
200 ctf_fs_notif_iter_data_destroy(notif_iter_data
);
205 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
211 if (ctf_fs
->traces
) {
212 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
215 if (ctf_fs
->port_data
) {
216 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
223 void ctf_fs_trace_destroy(void *data
)
225 struct ctf_fs_trace
*ctf_fs_trace
= data
;
231 if (ctf_fs_trace
->ds_file_groups
) {
232 g_ptr_array_free(ctf_fs_trace
->ds_file_groups
, TRUE
);
235 if (ctf_fs_trace
->path
) {
236 g_string_free(ctf_fs_trace
->path
, TRUE
);
239 if (ctf_fs_trace
->name
) {
240 g_string_free(ctf_fs_trace
->name
, TRUE
);
243 if (ctf_fs_trace
->metadata
) {
244 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
245 g_free(ctf_fs_trace
->metadata
);
248 bt_put(ctf_fs_trace
->cc_prio_map
);
249 g_free(ctf_fs_trace
);
252 void ctf_fs_finalize(struct bt_private_component
*component
)
254 void *data
= bt_private_component_get_user_data(component
);
256 ctf_fs_destroy(data
);
260 void port_data_destroy(void *data
) {
261 struct ctf_fs_port_data
*port_data
= data
;
271 int create_one_port_for_trace(struct ctf_fs_trace
*ctf_fs_trace
,
272 struct ctf_fs_ds_file_group
*ds_file_group
)
275 struct bt_private_port
*port
= NULL
;
276 struct ctf_fs_port_data
*port_data
= NULL
;
277 GString
*port_name
= NULL
;
278 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
279 struct ctf_fs_ds_file_info
*ds_file_info
=
280 g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
282 port_name
= g_string_new(NULL
);
288 * Assign the name for the new output port. If there's more than
289 * one stream file in the stream file group, the first
290 * (earliest) stream file's path is used.
292 assert(ds_file_group
->ds_file_infos
->len
> 0);
293 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
294 g_string_assign(port_name
, ds_file_info
->path
->str
);
295 PDBG("Creating one port named `%s`\n", port_name
->str
);
297 /* Create output port for this file */
298 port_data
= g_new0(struct ctf_fs_port_data
, 1);
303 port_data
->ds_file_group
= ds_file_group
;
304 port
= bt_private_component_source_add_output_private_port(
305 ctf_fs
->priv_comp
, port_name
->str
, port_data
);
310 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
319 g_string_free(port_name
, TRUE
);
323 port_data_destroy(port_data
);
328 int create_ports_for_trace(struct ctf_fs_trace
*ctf_fs_trace
)
331 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
334 /* Create one output port for each stream file group */
335 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
336 struct ctf_fs_ds_file_group
*ds_file_group
=
337 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
339 ret
= create_one_port_for_trace(ctf_fs_trace
, ds_file_group
);
341 PERR("Cannot create output port.\n");
350 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
351 struct bt_ctf_field
*packet_header_field
)
353 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
354 uint64_t stream_instance_id
= -1ULL;
357 if (!packet_header_field
) {
361 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
362 packet_header_field
, "stream_instance_id");
363 if (!stream_instance_id_field
) {
367 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
368 &stream_instance_id
);
370 stream_instance_id
= -1ULL;
375 bt_put(stream_instance_id_field
);
376 return stream_instance_id
;
379 struct bt_ctf_stream_class
*stream_class_from_packet_header(
380 struct ctf_fs_trace
*ctf_fs_trace
,
381 struct bt_ctf_field
*packet_header_field
)
383 struct bt_ctf_field
*stream_id_field
= NULL
;
384 struct bt_ctf_stream_class
*stream_class
= NULL
;
385 uint64_t stream_id
= -1ULL;
388 if (!packet_header_field
) {
389 goto single_stream_class
;
392 stream_id_field
= bt_ctf_field_structure_get_field_by_name(
393 packet_header_field
, "stream_id");
394 if (!stream_id_field
) {
398 ret
= bt_ctf_field_unsigned_integer_get_value(stream_id_field
,
404 if (stream_id
== -1ULL) {
406 /* Single stream class */
407 if (bt_ctf_trace_get_stream_class_count(
408 ctf_fs_trace
->metadata
->trace
) == 0) {
412 stream_class
= bt_ctf_trace_get_stream_class_by_index(
413 ctf_fs_trace
->metadata
->trace
, 0);
415 stream_class
= bt_ctf_trace_get_stream_class_by_id(
416 ctf_fs_trace
->metadata
->trace
, stream_id
);
420 bt_put(stream_id_field
);
424 uint64_t get_packet_context_timestamp_begin_ns(
425 struct ctf_fs_trace
*ctf_fs_trace
,
426 struct bt_ctf_field
*packet_context_field
)
429 struct bt_ctf_field
*timestamp_begin_field
= NULL
;
430 struct bt_ctf_field_type
*timestamp_begin_ft
= NULL
;
431 uint64_t timestamp_begin_raw_value
= -1ULL;
432 uint64_t timestamp_begin_ns
= -1ULL;
433 int64_t timestamp_begin_ns_signed
;
434 struct bt_ctf_clock_class
*timestamp_begin_clock_class
= NULL
;
435 struct bt_ctf_clock_value
*clock_value
= NULL
;
437 if (!packet_context_field
) {
441 timestamp_begin_field
= bt_ctf_field_structure_get_field_by_name(
442 packet_context_field
, "timestamp_begin");
443 if (!timestamp_begin_field
) {
447 timestamp_begin_ft
= bt_ctf_field_get_type(timestamp_begin_field
);
448 assert(timestamp_begin_ft
);
449 timestamp_begin_clock_class
=
450 bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft
);
451 if (!timestamp_begin_clock_class
) {
455 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field
,
456 ×tamp_begin_raw_value
);
461 clock_value
= bt_ctf_clock_value_create(timestamp_begin_clock_class
,
462 timestamp_begin_raw_value
);
467 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
,
468 ×tamp_begin_ns_signed
);
473 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
476 bt_put(timestamp_begin_field
);
477 bt_put(timestamp_begin_ft
);
478 bt_put(timestamp_begin_clock_class
);
480 return timestamp_begin_ns
;
484 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
490 if (ds_file_info
->path
) {
491 g_string_free(ds_file_info
->path
, TRUE
);
494 g_free(ds_file_info
);
498 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
501 struct ctf_fs_ds_file_info
*ds_file_info
;
503 ds_file_info
= g_new0(struct ctf_fs_ds_file_info
, 1);
508 ds_file_info
->path
= g_string_new(path
);
509 if (!ds_file_info
->path
) {
510 ctf_fs_ds_file_info_destroy(ds_file_info
);
515 ds_file_info
->begin_ns
= begin_ns
;
522 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
524 if (!ds_file_group
) {
528 if (ds_file_group
->ds_file_infos
) {
529 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
532 bt_put(ds_file_group
->stream
);
533 g_free(ds_file_group
);
537 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
538 struct ctf_fs_trace
*ctf_fs_trace
,
539 struct bt_ctf_stream_class
*stream_class
,
540 uint64_t stream_instance_id
)
542 struct ctf_fs_ds_file_group
*ds_file_group
;
544 assert(stream_class
);
545 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
546 if (!ds_file_group
) {
550 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
551 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
552 if (!ds_file_group
->ds_file_infos
) {
556 if (stream_instance_id
== -1ULL) {
557 ds_file_group
->stream
= bt_ctf_stream_create(
560 ds_file_group
->stream
= bt_ctf_stream_create_with_id(
561 stream_class
, NULL
, stream_instance_id
);
564 if (!ds_file_group
->stream
) {
568 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
573 ctf_fs_ds_file_group_destroy(ds_file_group
);
574 ds_file_group
= NULL
;
577 return ds_file_group
;
581 int ctf_fs_ds_file_group_add_ds_file_info(
582 struct ctf_fs_ds_file_group
*ds_file_group
,
583 const char *path
, uint64_t begin_ns
)
585 struct ctf_fs_ds_file_info
*ds_file_info
;
589 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
);
594 /* Find a spot to insert this one */
595 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
596 struct ctf_fs_ds_file_info
*other_ds_file_info
=
597 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
599 if (begin_ns
< other_ds_file_info
->begin_ns
) {
604 if (i
== ds_file_group
->ds_file_infos
->len
) {
609 g_ptr_array_insert(ds_file_group
->ds_file_infos
, i
, ds_file_info
);
614 ctf_fs_ds_file_info_destroy(ds_file_info
);
622 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
625 struct bt_ctf_field
*packet_header_field
= NULL
;
626 struct bt_ctf_field
*packet_context_field
= NULL
;
627 struct bt_ctf_stream_class
*stream_class
= NULL
;
628 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
629 uint64_t stream_instance_id
= -1ULL;
630 uint64_t begin_ns
= -1ULL;
631 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
632 bool add_group
= false;
636 ret
= ctf_fs_ds_file_get_packet_header_context_fields(
637 ctf_fs_trace
, path
, &packet_header_field
,
638 &packet_context_field
);
640 PERR("Cannot get stream file's first packet's header and context fields (`%s`).\n",
645 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
646 packet_header_field
);
647 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
648 packet_context_field
);
649 stream_class
= stream_class_from_packet_header(ctf_fs_trace
,
650 packet_header_field
);
655 if (begin_ns
== -1ULL) {
657 * No beggining timestamp to sort the stream files
658 * within a stream file group, so consider that this
659 * file must be the only one within its group.
661 stream_instance_id
= -1ULL;
664 if (stream_instance_id
== -1ULL) {
666 * No stream instance ID or no beginning timestamp:
667 * create a unique stream file group for this stream
668 * file because, even if there's a stream instance ID,
669 * there's no timestamp to order the file within its
673 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
674 stream_class
, stream_instance_id
);
675 if (!ds_file_group
) {
679 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
689 assert(stream_instance_id
!= -1ULL);
690 assert(begin_ns
!= -1ULL);
692 /* Find an existing stream file group with this ID */
693 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
695 struct bt_ctf_stream_class
*cand_stream_class
;
697 ds_file_group
= g_ptr_array_index(
698 ctf_fs_trace
->ds_file_groups
, i
);
699 id
= bt_ctf_stream_get_id(ds_file_group
->stream
);
700 cand_stream_class
= bt_ctf_stream_get_class(
701 ds_file_group
->stream
);
703 assert(cand_stream_class
);
704 bt_put(cand_stream_class
);
706 if (cand_stream_class
== stream_class
&&
707 (uint64_t) id
== stream_instance_id
) {
711 ds_file_group
= NULL
;
714 if (!ds_file_group
) {
715 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
716 stream_class
, stream_instance_id
);
717 if (!ds_file_group
) {
724 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
733 ctf_fs_ds_file_group_destroy(ds_file_group
);
737 if (add_group
&& ds_file_group
) {
738 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
741 bt_put(packet_header_field
);
742 bt_put(packet_context_field
);
743 bt_put(stream_class
);
748 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
751 const char *basename
;
752 GError
*error
= NULL
;
754 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
756 /* Check each file in the path directory, except specific ones */
757 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
759 PERR("Cannot open directory `%s`: %s (code %d)\n",
760 ctf_fs_trace
->path
->str
, error
->message
,
765 while ((basename
= g_dir_read_name(dir
))) {
766 struct ctf_fs_file
*file
;
768 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
769 /* Ignore the metadata stream. */
770 PDBG("Ignoring metadata file `%s/%s`\n",
771 ctf_fs_trace
->path
->str
, basename
);
775 if (basename
[0] == '.') {
776 PDBG("Ignoring hidden file `%s/%s`\n",
777 ctf_fs_trace
->path
->str
, basename
);
781 /* Create the file. */
782 file
= ctf_fs_file_create(ctf_fs
);
784 PERR("Cannot create stream file object for file `%s/%s`\n",
785 ctf_fs_trace
->path
->str
, basename
);
789 /* Create full path string. */
790 g_string_append_printf(file
->path
, "%s/%s",
791 ctf_fs_trace
->path
->str
, basename
);
792 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
793 PDBG("Ignoring non-regular file `%s`\n",
795 ctf_fs_file_destroy(file
);
800 ret
= ctf_fs_file_open(ctf_fs
, file
, "rb");
802 PERR("Cannot open stream file `%s`\n", file
->path
->str
);
806 if (file
->size
== 0) {
807 /* Skip empty stream. */
808 PDBG("Ignoring empty file `%s`\n", file
->path
->str
);
809 ctf_fs_file_destroy(file
);
813 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
816 PDBG("Cannot add stream file `%s` to stream file group\n",
818 ctf_fs_file_destroy(file
);
822 ctf_fs_file_destroy(file
);
844 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
850 assert(ctf_fs_trace
);
851 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
852 if (!ctf_fs_trace
->cc_prio_map
) {
857 count
= bt_ctf_trace_get_clock_class_count(
858 ctf_fs_trace
->metadata
->trace
);
861 for (i
= 0; i
< count
; i
++) {
862 struct bt_ctf_clock_class
*clock_class
=
863 bt_ctf_trace_get_clock_class_by_index(
864 ctf_fs_trace
->metadata
->trace
, i
);
867 ret
= bt_clock_class_priority_map_add_clock_class(
868 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
881 struct ctf_fs_trace
*ctf_fs_trace_create(struct ctf_fs_component
*ctf_fs
,
882 const char *path
, const char *name
)
884 struct ctf_fs_trace
*ctf_fs_trace
;
887 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
892 ctf_fs_trace
->ctf_fs
= ctf_fs
;
893 ctf_fs_trace
->path
= g_string_new(path
);
894 if (!ctf_fs_trace
->path
) {
898 ctf_fs_trace
->name
= g_string_new(name
);
899 if (!ctf_fs_trace
->name
) {
903 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
904 if (!ctf_fs_trace
->metadata
) {
908 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
909 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
910 if (!ctf_fs_trace
->ds_file_groups
) {
914 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
);
919 ret
= create_ds_file_groups(ctf_fs_trace
);
924 ret
= create_cc_prio_map(ctf_fs_trace
);
929 ret
= create_ports_for_trace(ctf_fs_trace
);
935 * create_ds_file_groups() created all the streams that this
936 * trace needs. There won't be any more. Therefore it is safe to
937 * make this trace static.
939 (void) bt_ctf_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
944 ctf_fs_trace_destroy(ctf_fs_trace
);
951 int path_is_ctf_trace(const char *path
)
953 GString
*metadata_path
= g_string_new(NULL
);
956 if (!metadata_path
) {
961 g_string_printf(metadata_path
, "%s/%s", path
, CTF_FS_METADATA_FILENAME
);
963 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
969 g_string_free(metadata_path
, TRUE
);
974 int add_trace_path(struct ctf_fs_component
*ctf_fs
, GList
**trace_paths
,
977 GString
*norm_path
= NULL
;
980 norm_path
= bt_common_normalize_path(path
, NULL
);
982 PERR("Failed to normalize path `%s`.\n", path
);
987 if (strcmp(norm_path
->str
, "/") == 0) {
988 PERR("Opening a trace in `/` is not supported.\n");
993 *trace_paths
= g_list_prepend(*trace_paths
, norm_path
);
994 assert(*trace_paths
);
999 g_string_free(norm_path
, TRUE
);
1006 int find_ctf_traces(struct ctf_fs_component
*ctf_fs
,
1007 GList
**trace_paths
, const char *start_path
)
1010 GError
*error
= NULL
;
1012 const char *basename
= NULL
;
1014 /* Check if the starting path is a CTF trace itself */
1015 ret
= path_is_ctf_trace(start_path
);
1022 * Stop recursion: a CTF trace cannot contain another
1025 ret
= add_trace_path(ctf_fs
, trace_paths
, start_path
);
1029 /* Look for subdirectories */
1030 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1031 /* Starting path is not a directory: end of recursion */
1035 dir
= g_dir_open(start_path
, 0, &error
);
1037 if (error
->code
== G_FILE_ERROR_ACCES
) {
1038 PDBG("Cannot open directory `%s`: %s (code %d): continuing\n",
1039 start_path
, error
->message
, error
->code
);
1043 PERR("Cannot open directory `%s`: %s (code %d)\n",
1044 start_path
, error
->message
, error
->code
);
1049 while ((basename
= g_dir_read_name(dir
))) {
1050 GString
*sub_path
= g_string_new(NULL
);
1057 g_string_printf(sub_path
, "%s/%s", start_path
, basename
);
1058 ret
= find_ctf_traces(ctf_fs
, trace_paths
, sub_path
->str
);
1059 g_string_free(sub_path
, TRUE
);
1071 g_error_free(error
);
1078 GList
*create_trace_names(GList
*trace_paths
, const char *base_path
) {
1079 GList
*trace_names
= NULL
;
1081 const char *last_sep
;
1085 * At this point we know that all the trace paths are
1086 * normalized, and so is the base path. This means that
1087 * they are absolute and they don't end with a separator.
1088 * We can simply find the location of the last separator
1089 * in the base path, which gives us the name of the actual
1090 * directory to look into, and use this location as the
1091 * start of each trace name within each trace path.
1095 * Base path: /home/user/my-traces/some-trace
1097 * - /home/user/my-traces/some-trace/host1/trace1
1098 * - /home/user/my-traces/some-trace/host1/trace2
1099 * - /home/user/my-traces/some-trace/host2/trace
1100 * - /home/user/my-traces/some-trace/other-trace
1102 * In this case the trace names are:
1104 * - some-trace/host1/trace1
1105 * - some-trace/host1/trace2
1106 * - some-trace/host2/trace
1107 * - some-trace/other-trace
1109 last_sep
= strrchr(base_path
, G_DIR_SEPARATOR
);
1111 /* We know there's at least one separator */
1114 /* Distance to base */
1115 base_dist
= last_sep
- base_path
+ 1;
1117 /* Create the trace names */
1118 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1119 GString
*trace_name
= g_string_new(NULL
);
1120 GString
*trace_path
= node
->data
;
1123 g_string_assign(trace_name
, &trace_path
->str
[base_dist
]);
1124 trace_names
= g_list_append(trace_names
, trace_name
);
1131 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1132 const char *path_param
)
1134 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1136 GString
*norm_path
= NULL
;
1137 GList
*trace_paths
= NULL
;
1138 GList
*trace_names
= NULL
;
1142 norm_path
= bt_common_normalize_path(path_param
, NULL
);
1144 PERR("Failed to normalize path: `%s`.\n",
1149 ret
= find_ctf_traces(ctf_fs
, &trace_paths
, norm_path
->str
);
1155 PERR("No CTF traces recursively found in `%s`.\n",
1160 trace_names
= create_trace_names(trace_paths
, norm_path
->str
);
1162 PERR("Cannot create trace names from trace paths.\n");
1166 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1167 tp_node
= g_list_next(tp_node
),
1168 tn_node
= g_list_next(tn_node
)) {
1169 GString
*trace_path
= tp_node
->data
;
1170 GString
*trace_name
= tn_node
->data
;
1172 ctf_fs_trace
= ctf_fs_trace_create(ctf_fs
, trace_path
->str
,
1174 if (!ctf_fs_trace
) {
1175 PERR("Cannot create trace for `%s`.\n",
1180 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1181 ctf_fs_trace
= NULL
;
1188 ctf_fs_trace_destroy(ctf_fs_trace
);
1191 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1192 if (tp_node
->data
) {
1193 g_string_free(tp_node
->data
, TRUE
);
1197 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1198 if (tn_node
->data
) {
1199 g_string_free(tn_node
->data
, TRUE
);
1204 g_list_free(trace_paths
);
1208 g_list_free(trace_names
);
1212 g_string_free(norm_path
, TRUE
);
1219 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1220 struct bt_value
*params
)
1222 struct ctf_fs_component
*ctf_fs
;
1223 struct bt_value
*value
= NULL
;
1224 const char *path_param
;
1227 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1232 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1236 * We don't need to get a new reference here because as long as
1237 * our private ctf_fs_component object exists, the containing
1238 * private component should also exist.
1240 ctf_fs
->priv_comp
= priv_comp
;
1241 value
= bt_value_map_get(params
, "path");
1242 if (!bt_value_is_string(value
)) {
1246 ret
= bt_value_string_get(value
, &path_param
);
1249 value
= bt_value_map_get(params
, "offset-s");
1253 if (!bt_value_is_integer(value
)) {
1255 "offset-s should be an integer\n");
1258 ret
= bt_value_integer_get(value
, &offset
);
1260 ctf_fs
->options
.clock_offset
= offset
;
1264 value
= bt_value_map_get(params
, "offset-ns");
1268 if (!bt_value_is_integer(value
)) {
1270 "offset-ns should be an integer\n");
1273 ret
= bt_value_integer_get(value
, &offset
);
1275 ctf_fs
->options
.clock_offset_ns
= offset
;
1279 ctf_fs
->error_fp
= stderr
;
1280 ctf_fs
->page_size
= (size_t) getpagesize();
1281 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1282 if (!ctf_fs
->port_data
) {
1286 ctf_fs
->traces
= g_ptr_array_new_with_free_func(ctf_fs_trace_destroy
);
1287 if (!ctf_fs
->traces
) {
1291 ret
= create_ctf_fs_traces(ctf_fs
, path_param
);
1299 ctf_fs_destroy(ctf_fs
);
1301 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1310 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1311 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1313 struct ctf_fs_component
*ctf_fs
;
1314 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1316 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
1317 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1319 ret
= BT_COMPONENT_STATUS_ERROR
;
1326 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
1327 const char *object
, struct bt_value
*params
)
1329 struct bt_value
*results
= NULL
;
1330 struct bt_value
*path_value
= NULL
;
1331 char *metadata_text
= NULL
;
1332 FILE *metadata_fp
= NULL
;
1333 GString
*g_metadata_text
= NULL
;
1335 if (strcmp(object
, "metadata-info") == 0) {
1341 results
= bt_value_map_create();
1346 if (!bt_value_is_map(params
)) {
1348 "Query parameters is not a map value object\n");
1352 path_value
= bt_value_map_get(params
, "path");
1353 ret
= bt_value_string_get(path_value
, &path
);
1356 "Cannot get `path` string parameter\n");
1361 metadata_fp
= ctf_fs_metadata_open_file(path
);
1364 "Cannot open trace at path `%s`\n", path
);
1368 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
1371 if (is_packetized
) {
1372 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
1373 metadata_fp
, &metadata_text
, bo
);
1376 "Cannot decode packetized metadata file\n");
1382 fseek(metadata_fp
, 0, SEEK_END
);
1383 filesize
= ftell(metadata_fp
);
1384 rewind(metadata_fp
);
1385 metadata_text
= malloc(filesize
+ 1);
1386 if (!metadata_text
) {
1388 "Cannot allocate buffer for metadata text\n");
1392 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
1395 "Cannot read metadata file\n");
1399 metadata_text
[filesize
] = '\0';
1402 g_metadata_text
= g_string_new(NULL
);
1403 if (!g_metadata_text
) {
1407 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
1408 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
1409 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
1410 g_string_append(g_metadata_text
, " */\n\n");
1413 g_string_append(g_metadata_text
, metadata_text
);
1415 ret
= bt_value_map_insert_string(results
, "text",
1416 g_metadata_text
->str
);
1418 fprintf(stderr
, "Cannot insert metadata text into results\n");
1422 ret
= bt_value_map_insert_bool(results
, "is-packetized",
1425 fprintf(stderr
, "Cannot insert is packetized into results\n");
1429 fprintf(stderr
, "Unknown query object `%s`\n", object
);
1440 free(metadata_text
);
1442 if (g_metadata_text
) {
1443 g_string_free(g_metadata_text
, TRUE
);
1447 fclose(metadata_fp
);