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/ctf-ir/packet.h>
29 #include <babeltrace/ctf-ir/clock-class.h>
30 #include <babeltrace/ctf-ir/stream.h>
31 #include <babeltrace/ctf-ir/fields.h>
32 #include <babeltrace/graph/private-port.h>
33 #include <babeltrace/graph/private-component.h>
34 #include <babeltrace/graph/private-component-source.h>
35 #include <babeltrace/graph/private-notification-iterator.h>
36 #include <babeltrace/graph/component.h>
37 #include <babeltrace/graph/notification-iterator.h>
38 #include <babeltrace/graph/clock-class-priority-map.h>
39 #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 bt_private_port
*port
= NULL
;
275 struct ctf_fs_port_data
*port_data
= NULL
;
276 GString
*port_name
= NULL
;
277 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
278 struct ctf_fs_ds_file_info
*ds_file_info
=
279 g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
281 port_name
= g_string_new(NULL
);
287 * Assign the name for the new output port. If there's more than
288 * one stream file in the stream file group, the first
289 * (earliest) stream file's path is used.
291 assert(ds_file_group
->ds_file_infos
->len
> 0);
292 ds_file_info
= g_ptr_array_index(ds_file_group
->ds_file_infos
, 0);
293 g_string_assign(port_name
, ds_file_info
->path
->str
);
294 PDBG("Creating one port named `%s`\n", port_name
->str
);
296 /* Create output port for this file */
297 port_data
= g_new0(struct ctf_fs_port_data
, 1);
302 port_data
->ds_file_group
= ds_file_group
;
303 port
= bt_private_component_source_add_output_private_port(
304 ctf_fs
->priv_comp
, port_name
->str
, port_data
);
309 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
318 g_string_free(port_name
, TRUE
);
322 port_data_destroy(port_data
);
327 int create_ports_for_trace(struct ctf_fs_trace
*ctf_fs_trace
)
330 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
333 /* Create one output port for each stream file group */
334 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
335 struct ctf_fs_ds_file_group
*ds_file_group
=
336 g_ptr_array_index(ctf_fs_trace
->ds_file_groups
, i
);
338 ret
= create_one_port_for_trace(ctf_fs_trace
, ds_file_group
);
340 PERR("Cannot create output port.\n");
349 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace
*ctf_fs_trace
,
350 struct bt_ctf_field
*packet_header_field
)
352 struct bt_ctf_field
*stream_instance_id_field
= NULL
;
353 uint64_t stream_instance_id
= -1ULL;
356 if (!packet_header_field
) {
360 stream_instance_id_field
= bt_ctf_field_structure_get_field_by_name(
361 packet_header_field
, "stream_instance_id");
362 if (!stream_instance_id_field
) {
366 ret
= bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field
,
367 &stream_instance_id
);
369 stream_instance_id
= -1ULL;
374 bt_put(stream_instance_id_field
);
375 return stream_instance_id
;
378 struct bt_ctf_stream_class
*stream_class_from_packet_header(
379 struct ctf_fs_trace
*ctf_fs_trace
,
380 struct bt_ctf_field
*packet_header_field
)
382 struct bt_ctf_field
*stream_id_field
= NULL
;
383 struct bt_ctf_stream_class
*stream_class
= NULL
;
384 uint64_t stream_id
= -1ULL;
387 if (!packet_header_field
) {
388 goto single_stream_class
;
391 stream_id_field
= bt_ctf_field_structure_get_field_by_name(
392 packet_header_field
, "stream_id");
393 if (!stream_id_field
) {
397 ret
= bt_ctf_field_unsigned_integer_get_value(stream_id_field
,
403 if (stream_id
== -1ULL) {
405 /* Single stream class */
406 if (bt_ctf_trace_get_stream_class_count(
407 ctf_fs_trace
->metadata
->trace
) == 0) {
411 stream_class
= bt_ctf_trace_get_stream_class_by_index(
412 ctf_fs_trace
->metadata
->trace
, 0);
414 stream_class
= bt_ctf_trace_get_stream_class_by_id(
415 ctf_fs_trace
->metadata
->trace
, stream_id
);
419 bt_put(stream_id_field
);
423 uint64_t get_packet_context_timestamp_begin_ns(
424 struct ctf_fs_trace
*ctf_fs_trace
,
425 struct bt_ctf_field
*packet_context_field
)
428 struct bt_ctf_field
*timestamp_begin_field
= NULL
;
429 struct bt_ctf_field_type
*timestamp_begin_ft
= NULL
;
430 uint64_t timestamp_begin_raw_value
= -1ULL;
431 uint64_t timestamp_begin_ns
= -1ULL;
432 int64_t timestamp_begin_ns_signed
;
433 struct bt_ctf_clock_class
*timestamp_begin_clock_class
= NULL
;
434 struct bt_ctf_clock_value
*clock_value
= NULL
;
436 if (!packet_context_field
) {
440 timestamp_begin_field
= bt_ctf_field_structure_get_field_by_name(
441 packet_context_field
, "timestamp_begin");
442 if (!timestamp_begin_field
) {
446 timestamp_begin_ft
= bt_ctf_field_get_type(timestamp_begin_field
);
447 assert(timestamp_begin_ft
);
448 timestamp_begin_clock_class
=
449 bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft
);
450 if (!timestamp_begin_clock_class
) {
454 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field
,
455 ×tamp_begin_raw_value
);
460 clock_value
= bt_ctf_clock_value_create(timestamp_begin_clock_class
,
461 timestamp_begin_raw_value
);
466 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
,
467 ×tamp_begin_ns_signed
);
472 timestamp_begin_ns
= (uint64_t) timestamp_begin_ns_signed
;
475 bt_put(timestamp_begin_field
);
476 bt_put(timestamp_begin_ft
);
477 bt_put(timestamp_begin_clock_class
);
479 return timestamp_begin_ns
;
483 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info
*ds_file_info
)
489 if (ds_file_info
->path
) {
490 g_string_free(ds_file_info
->path
, TRUE
);
493 g_free(ds_file_info
);
497 struct ctf_fs_ds_file_info
*ctf_fs_ds_file_info_create(const char *path
,
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
;
521 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group
*ds_file_group
)
523 if (!ds_file_group
) {
527 if (ds_file_group
->ds_file_infos
) {
528 g_ptr_array_free(ds_file_group
->ds_file_infos
, TRUE
);
531 bt_put(ds_file_group
->stream
);
532 g_free(ds_file_group
);
536 struct ctf_fs_ds_file_group
*ctf_fs_ds_file_group_create(
537 struct ctf_fs_trace
*ctf_fs_trace
,
538 struct bt_ctf_stream_class
*stream_class
,
539 uint64_t stream_instance_id
)
541 struct ctf_fs_ds_file_group
*ds_file_group
;
543 assert(stream_class
);
544 ds_file_group
= g_new0(struct ctf_fs_ds_file_group
, 1);
545 if (!ds_file_group
) {
549 ds_file_group
->ds_file_infos
= g_ptr_array_new_with_free_func(
550 (GDestroyNotify
) ctf_fs_ds_file_info_destroy
);
551 if (!ds_file_group
->ds_file_infos
) {
555 if (stream_instance_id
== -1ULL) {
556 ds_file_group
->stream
= bt_ctf_stream_create(
559 ds_file_group
->stream
= bt_ctf_stream_create_with_id(
560 stream_class
, NULL
, stream_instance_id
);
563 if (!ds_file_group
->stream
) {
567 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
572 ctf_fs_ds_file_group_destroy(ds_file_group
);
573 ds_file_group
= NULL
;
576 return ds_file_group
;
580 int ctf_fs_ds_file_group_add_ds_file_info(
581 struct ctf_fs_ds_file_group
*ds_file_group
,
582 const char *path
, uint64_t begin_ns
)
584 struct ctf_fs_ds_file_info
*ds_file_info
;
588 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
);
593 /* Find a spot to insert this one */
594 for (i
= 0; i
< ds_file_group
->ds_file_infos
->len
; i
++) {
595 struct ctf_fs_ds_file_info
*other_ds_file_info
=
596 g_ptr_array_index(ds_file_group
->ds_file_infos
, i
);
598 if (begin_ns
< other_ds_file_info
->begin_ns
) {
603 if (i
== ds_file_group
->ds_file_infos
->len
) {
608 g_ptr_array_insert(ds_file_group
->ds_file_infos
, i
, ds_file_info
);
613 ctf_fs_ds_file_info_destroy(ds_file_info
);
621 int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
,
624 struct bt_ctf_field
*packet_header_field
= NULL
;
625 struct bt_ctf_field
*packet_context_field
= NULL
;
626 struct bt_ctf_stream_class
*stream_class
= NULL
;
627 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
628 uint64_t stream_instance_id
= -1ULL;
629 uint64_t begin_ns
= -1ULL;
630 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
631 bool add_group
= false;
635 ret
= ctf_fs_ds_file_get_packet_header_context_fields(
636 ctf_fs_trace
, path
, &packet_header_field
,
637 &packet_context_field
);
639 PERR("Cannot get stream file's first packet's header and context fields (`%s`).\n",
644 stream_instance_id
= get_packet_header_stream_instance_id(ctf_fs_trace
,
645 packet_header_field
);
646 begin_ns
= get_packet_context_timestamp_begin_ns(ctf_fs_trace
,
647 packet_context_field
);
648 stream_class
= stream_class_from_packet_header(ctf_fs_trace
,
649 packet_header_field
);
654 if (begin_ns
== -1ULL) {
656 * No beggining timestamp to sort the stream files
657 * within a stream file group, so consider that this
658 * file must be the only one within its group.
660 stream_instance_id
= -1ULL;
663 if (stream_instance_id
== -1ULL) {
665 * No stream instance ID or no beginning timestamp:
666 * create a unique stream file group for this stream
667 * file because, even if there's a stream instance ID,
668 * there's no timestamp to order the file within its
672 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
673 stream_class
, stream_instance_id
);
674 if (!ds_file_group
) {
678 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
688 assert(stream_instance_id
!= -1ULL);
689 assert(begin_ns
!= -1ULL);
691 /* Find an existing stream file group with this ID */
692 for (i
= 0; i
< ctf_fs_trace
->ds_file_groups
->len
; i
++) {
694 struct bt_ctf_stream_class
*cand_stream_class
;
696 ds_file_group
= g_ptr_array_index(
697 ctf_fs_trace
->ds_file_groups
, i
);
698 id
= bt_ctf_stream_get_id(ds_file_group
->stream
);
699 cand_stream_class
= bt_ctf_stream_get_class(
700 ds_file_group
->stream
);
702 assert(cand_stream_class
);
703 bt_put(cand_stream_class
);
705 if (cand_stream_class
== stream_class
&&
706 (uint64_t) id
== stream_instance_id
) {
710 ds_file_group
= NULL
;
713 if (!ds_file_group
) {
714 ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
,
715 stream_class
, stream_instance_id
);
716 if (!ds_file_group
) {
723 ret
= ctf_fs_ds_file_group_add_ds_file_info(ds_file_group
,
732 ctf_fs_ds_file_group_destroy(ds_file_group
);
736 if (add_group
&& ds_file_group
) {
737 g_ptr_array_add(ctf_fs_trace
->ds_file_groups
, ds_file_group
);
740 bt_put(packet_header_field
);
741 bt_put(packet_context_field
);
742 bt_put(stream_class
);
747 int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
750 const char *basename
;
751 GError
*error
= NULL
;
753 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
755 /* Check each file in the path directory, except specific ones */
756 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
758 PERR("Cannot open directory `%s`: %s (code %d)\n",
759 ctf_fs_trace
->path
->str
, error
->message
,
764 while ((basename
= g_dir_read_name(dir
))) {
765 struct ctf_fs_file
*file
;
767 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
768 /* Ignore the metadata stream. */
769 PDBG("Ignoring metadata file `%s/%s`\n",
770 ctf_fs_trace
->path
->str
, basename
);
774 if (basename
[0] == '.') {
775 PDBG("Ignoring hidden file `%s/%s`\n",
776 ctf_fs_trace
->path
->str
, basename
);
780 /* Create the file. */
781 file
= ctf_fs_file_create(ctf_fs
);
783 PERR("Cannot create stream file object for file `%s/%s`\n",
784 ctf_fs_trace
->path
->str
, basename
);
788 /* Create full path string. */
789 g_string_append_printf(file
->path
, "%s/%s",
790 ctf_fs_trace
->path
->str
, basename
);
791 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
792 PDBG("Ignoring non-regular file `%s`\n",
794 ctf_fs_file_destroy(file
);
799 ret
= ctf_fs_file_open(ctf_fs
, file
, "rb");
801 PERR("Cannot open stream file `%s`\n", file
->path
->str
);
805 if (file
->size
== 0) {
806 /* Skip empty stream. */
807 PDBG("Ignoring empty file `%s`\n", file
->path
->str
);
808 ctf_fs_file_destroy(file
);
812 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
,
815 PDBG("Cannot add stream file `%s` to stream file group\n",
817 ctf_fs_file_destroy(file
);
821 ctf_fs_file_destroy(file
);
843 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
849 assert(ctf_fs_trace
);
850 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
851 if (!ctf_fs_trace
->cc_prio_map
) {
856 count
= bt_ctf_trace_get_clock_class_count(
857 ctf_fs_trace
->metadata
->trace
);
860 for (i
= 0; i
< count
; i
++) {
861 struct bt_ctf_clock_class
*clock_class
=
862 bt_ctf_trace_get_clock_class_by_index(
863 ctf_fs_trace
->metadata
->trace
, i
);
866 ret
= bt_clock_class_priority_map_add_clock_class(
867 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
880 struct ctf_fs_trace
*ctf_fs_trace_create(struct ctf_fs_component
*ctf_fs
,
881 const char *path
, const char *name
)
883 struct ctf_fs_trace
*ctf_fs_trace
;
886 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
891 ctf_fs_trace
->ctf_fs
= ctf_fs
;
892 ctf_fs_trace
->path
= g_string_new(path
);
893 if (!ctf_fs_trace
->path
) {
897 ctf_fs_trace
->name
= g_string_new(name
);
898 if (!ctf_fs_trace
->name
) {
902 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
903 if (!ctf_fs_trace
->metadata
) {
907 ctf_fs_trace
->ds_file_groups
= g_ptr_array_new_with_free_func(
908 (GDestroyNotify
) ctf_fs_ds_file_group_destroy
);
909 if (!ctf_fs_trace
->ds_file_groups
) {
913 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
);
918 ret
= create_ds_file_groups(ctf_fs_trace
);
923 ret
= create_cc_prio_map(ctf_fs_trace
);
928 ret
= create_ports_for_trace(ctf_fs_trace
);
934 * create_ds_file_groups() created all the streams that this
935 * trace needs. There won't be any more. Therefore it is safe to
936 * make this trace static.
938 (void) bt_ctf_trace_set_is_static(ctf_fs_trace
->metadata
->trace
);
943 ctf_fs_trace_destroy(ctf_fs_trace
);
950 int path_is_ctf_trace(const char *path
)
952 GString
*metadata_path
= g_string_new(NULL
);
955 if (!metadata_path
) {
960 g_string_printf(metadata_path
, "%s/%s", path
, CTF_FS_METADATA_FILENAME
);
962 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
968 g_string_free(metadata_path
, TRUE
);
973 int add_trace_path(struct ctf_fs_component
*ctf_fs
, GList
**trace_paths
,
976 GString
*path_str
= g_string_new(NULL
);
986 * Find the real path so that we don't have relative components
987 * in the trace name. This also squashes consecutive slashes and
988 * removes any slash at the end.
990 rp
= realpath(path
, NULL
);
992 PERR("realpath() failed: %s (%d)\n", strerror(errno
), errno
);
997 if (strcmp(rp
, "/") == 0) {
998 PERR("Opening a trace in `/` is not supported.\n");
1003 g_string_assign(path_str
, rp
);
1004 *trace_paths
= g_list_prepend(*trace_paths
, path_str
);
1005 assert(*trace_paths
);
1013 int find_ctf_traces(struct ctf_fs_component
*ctf_fs
,
1014 GList
**trace_paths
, const char *start_path
)
1017 GError
*error
= NULL
;
1019 const char *basename
= NULL
;
1021 /* Check if the starting path is a CTF trace itself */
1022 ret
= path_is_ctf_trace(start_path
);
1029 * Do not even recurse: a CTF trace cannot contain
1030 * another CTF trace.
1032 ret
= add_trace_path(ctf_fs
, trace_paths
, start_path
);
1036 /* Look for subdirectories */
1037 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
1038 /* Starting path is not a directory: end of recursion */
1042 dir
= g_dir_open(start_path
, 0, &error
);
1044 if (error
->code
== G_FILE_ERROR_ACCES
) {
1045 PDBG("Cannot open directory `%s`: %s (code %d): continuing\n",
1046 start_path
, error
->message
, error
->code
);
1050 PERR("Cannot open directory `%s`: %s (code %d)\n",
1051 start_path
, error
->message
, error
->code
);
1056 while ((basename
= g_dir_read_name(dir
))) {
1057 GString
*sub_path
= g_string_new(NULL
);
1064 g_string_printf(sub_path
, "%s/%s", start_path
, basename
);
1065 ret
= find_ctf_traces(ctf_fs
, trace_paths
, sub_path
->str
);
1066 g_string_free(sub_path
, TRUE
);
1078 g_error_free(error
);
1085 GList
*create_trace_names(GList
*trace_paths
) {
1086 GList
*trace_names
= NULL
;
1087 size_t chars_to_strip
= 0;
1093 * Find the number of characters to strip from the beginning,
1094 * that is, the longest prefix until a common slash (also
1098 gchar common_ch
= '\0';
1100 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1101 GString
*gstr
= node
->data
;
1102 gchar this_ch
= gstr
->str
[at
];
1104 if (this_ch
== '\0') {
1109 if (common_ch
== '\0') {
1111 * Establish the expected common
1112 * character at this position.
1114 common_ch
= this_ch
;
1118 if (this_ch
!= common_ch
) {
1128 if (common_ch
== '/') {
1130 * Common character is a slash: safe to include
1131 * this slash in the number of characters to
1132 * strip because the paths are guaranteed not to
1135 chars_to_strip
= at
+ 1;
1141 /* Create the trace names */
1142 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
1143 GString
*trace_name
= g_string_new(NULL
);
1144 GString
*trace_path
= node
->data
;
1146 g_string_assign(trace_name
, &trace_path
->str
[chars_to_strip
]);
1147 trace_names
= g_list_append(trace_names
, trace_name
);
1154 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
1155 const char *path_param
)
1157 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
1159 GList
*trace_paths
= NULL
;
1160 GList
*trace_names
= NULL
;
1164 ret
= find_ctf_traces(ctf_fs
, &trace_paths
, path_param
);
1170 PERR("No CTF traces recursively found in `%s`.\n",
1175 trace_names
= create_trace_names(trace_paths
);
1177 PERR("Cannot create trace names from trace paths.\n");
1181 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
1182 tp_node
= g_list_next(tp_node
),
1183 tn_node
= g_list_next(tn_node
)) {
1184 GString
*trace_path
= tp_node
->data
;
1185 GString
*trace_name
= tn_node
->data
;
1187 ctf_fs_trace
= ctf_fs_trace_create(ctf_fs
, trace_path
->str
,
1189 if (!ctf_fs_trace
) {
1190 PERR("Cannot create trace for `%s`.\n",
1195 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
1196 ctf_fs_trace
= NULL
;
1203 ctf_fs_trace_destroy(ctf_fs_trace
);
1206 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
1207 if (tp_node
->data
) {
1208 g_string_free(tp_node
->data
, TRUE
);
1212 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
1213 if (tn_node
->data
) {
1214 g_string_free(tn_node
->data
, TRUE
);
1219 g_list_free(trace_paths
);
1223 g_list_free(trace_names
);
1230 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
1231 struct bt_value
*params
)
1233 struct ctf_fs_component
*ctf_fs
;
1234 struct bt_value
*value
= NULL
;
1235 const char *path_param
;
1238 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
1243 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
1247 * We don't need to get a new reference here because as long as
1248 * our private ctf_fs_component object exists, the containing
1249 * private component should also exist.
1251 ctf_fs
->priv_comp
= priv_comp
;
1252 value
= bt_value_map_get(params
, "path");
1253 if (!bt_value_is_string(value
)) {
1257 ret
= bt_value_string_get(value
, &path_param
);
1260 value
= bt_value_map_get(params
, "offset-s");
1264 if (!bt_value_is_integer(value
)) {
1266 "offset-s should be an integer\n");
1269 ret
= bt_value_integer_get(value
, &offset
);
1271 ctf_fs
->options
.clock_offset
= offset
;
1275 value
= bt_value_map_get(params
, "offset-ns");
1279 if (!bt_value_is_integer(value
)) {
1281 "offset-ns should be an integer\n");
1284 ret
= bt_value_integer_get(value
, &offset
);
1286 ctf_fs
->options
.clock_offset_ns
= offset
;
1290 ctf_fs
->error_fp
= stderr
;
1291 ctf_fs
->page_size
= (size_t) getpagesize();
1292 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
1293 if (!ctf_fs
->port_data
) {
1297 ctf_fs
->traces
= g_ptr_array_new_with_free_func(ctf_fs_trace_destroy
);
1298 if (!ctf_fs
->traces
) {
1302 ret
= create_ctf_fs_traces(ctf_fs
, path_param
);
1310 ctf_fs_destroy(ctf_fs
);
1312 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
1321 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
1322 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1324 struct ctf_fs_component
*ctf_fs
;
1325 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1327 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
1328 ctf_fs
= ctf_fs_create(priv_comp
, params
);
1330 ret
= BT_COMPONENT_STATUS_ERROR
;
1337 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
1338 const char *object
, struct bt_value
*params
)
1340 struct bt_value
*results
= NULL
;
1341 struct bt_value
*path_value
= NULL
;
1342 char *metadata_text
= NULL
;
1343 FILE *metadata_fp
= NULL
;
1344 GString
*g_metadata_text
= NULL
;
1346 if (strcmp(object
, "metadata-info") == 0) {
1352 results
= bt_value_map_create();
1357 if (!bt_value_is_map(params
)) {
1359 "Query parameters is not a map value object\n");
1363 path_value
= bt_value_map_get(params
, "path");
1364 ret
= bt_value_string_get(path_value
, &path
);
1367 "Cannot get `path` string parameter\n");
1372 metadata_fp
= ctf_fs_metadata_open_file(path
);
1375 "Cannot open trace at path `%s`\n", path
);
1379 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
1382 if (is_packetized
) {
1383 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
1384 metadata_fp
, &metadata_text
, bo
);
1387 "Cannot decode packetized metadata file\n");
1393 fseek(metadata_fp
, 0, SEEK_END
);
1394 filesize
= ftell(metadata_fp
);
1395 rewind(metadata_fp
);
1396 metadata_text
= malloc(filesize
+ 1);
1397 if (!metadata_text
) {
1399 "Cannot allocate buffer for metadata text\n");
1403 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
1406 "Cannot read metadata file\n");
1410 metadata_text
[filesize
] = '\0';
1413 g_metadata_text
= g_string_new(NULL
);
1414 if (!g_metadata_text
) {
1418 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
1419 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
1420 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
1421 g_string_append(g_metadata_text
, " */\n\n");
1424 g_string_append(g_metadata_text
, metadata_text
);
1426 ret
= bt_value_map_insert_string(results
, "text",
1427 g_metadata_text
->str
);
1429 fprintf(stderr
, "Cannot insert metadata text into results\n");
1433 ret
= bt_value_map_insert_bool(results
, "is-packetized",
1436 fprintf(stderr
, "Cannot insert is packetized into results\n");
1440 fprintf(stderr
, "Unknown query object `%s`\n", object
);
1451 free(metadata_text
);
1453 if (g_metadata_text
) {
1454 g_string_free(g_metadata_text
, TRUE
);
1458 fclose(metadata_fp
);