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/graph/private-port.h>
31 #include <babeltrace/graph/private-component.h>
32 #include <babeltrace/graph/private-component-source.h>
33 #include <babeltrace/graph/private-notification-iterator.h>
34 #include <babeltrace/graph/component.h>
35 #include <babeltrace/graph/notification-iterator.h>
36 #include <babeltrace/graph/clock-class-priority-map.h>
37 #include <plugins-common.h>
44 #include "data-stream.h"
46 #include "../common/metadata/decoder.h"
48 #define PRINT_ERR_STREAM ctf_fs->error_fp
49 #define PRINT_PREFIX "ctf-fs"
50 #define PRINT_DBG_CHECK ctf_fs_debug
52 #define METADATA_TEXT_SIG "/* CTF 1.8"
57 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
58 struct bt_private_notification_iterator
*iterator
)
60 struct ctf_fs_stream
*fs_stream
=
61 bt_private_notification_iterator_get_user_data(iterator
);
63 return ctf_fs_stream_next(fs_stream
);
66 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
69 bt_private_notification_iterator_get_user_data(it
);
71 ctf_fs_stream_destroy(ctf_fs_stream
);
74 enum bt_notification_iterator_status
ctf_fs_iterator_init(
75 struct bt_private_notification_iterator
*it
,
76 struct bt_private_port
*port
)
78 struct ctf_fs_stream
*ctf_fs_stream
= NULL
;
79 struct ctf_fs_port_data
*port_data
;
80 enum bt_notification_iterator_status ret
=
81 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
83 port_data
= bt_private_port_get_user_data(port
);
85 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
89 ctf_fs_stream
= ctf_fs_stream_create(port_data
->ctf_fs_trace
,
90 port_data
->path
->str
);
95 ret
= bt_private_notification_iterator_set_user_data(it
, ctf_fs_stream
);
100 ctf_fs_stream
= NULL
;
104 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
106 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
107 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
111 ctf_fs_stream_destroy(ctf_fs_stream
);
116 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
122 if (ctf_fs
->traces
) {
123 g_ptr_array_free(ctf_fs
->traces
, TRUE
);
126 if (ctf_fs
->port_data
) {
127 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
134 void ctf_fs_trace_destroy(void *data
)
136 struct ctf_fs_trace
*ctf_fs_trace
= data
;
142 if (ctf_fs_trace
->path
) {
143 g_string_free(ctf_fs_trace
->path
, TRUE
);
146 if (ctf_fs_trace
->name
) {
147 g_string_free(ctf_fs_trace
->name
, TRUE
);
150 if (ctf_fs_trace
->metadata
) {
151 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
152 g_free(ctf_fs_trace
->metadata
);
155 bt_put(ctf_fs_trace
->cc_prio_map
);
156 g_free(ctf_fs_trace
);
159 void ctf_fs_finalize(struct bt_private_component
*component
)
161 void *data
= bt_private_component_get_user_data(component
);
163 ctf_fs_destroy(data
);
167 void port_data_destroy(void *data
) {
168 struct ctf_fs_port_data
*port_data
= data
;
174 if (port_data
->path
) {
175 g_string_free(port_data
->path
, TRUE
);
182 int create_one_port_for_trace(struct ctf_fs_trace
*ctf_fs_trace
,
183 const char *stream_path
)
186 struct bt_private_port
*port
= NULL
;
187 struct ctf_fs_port_data
*port_data
= NULL
;
188 GString
*port_name
= NULL
;
189 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
191 port_name
= g_string_new(NULL
);
196 /* Assign the name for the new output port */
197 g_string_printf(port_name
, "%s", stream_path
);
198 PDBG("Creating one port named `%s`\n", port_name
->str
);
200 /* Create output port for this file */
201 port_data
= g_new0(struct ctf_fs_port_data
, 1);
206 port_data
->ctf_fs_trace
= ctf_fs_trace
;
207 port_data
->path
= g_string_new(stream_path
);
208 if (!port_data
->path
) {
212 port
= bt_private_component_source_add_output_private_port(
213 ctf_fs
->priv_comp
, port_name
->str
, port_data
);
218 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
227 g_string_free(port_name
, TRUE
);
231 port_data_destroy(port_data
);
236 int create_ports_for_trace(struct ctf_fs_trace
*ctf_fs_trace
)
239 const char *basename
;
240 GError
*error
= NULL
;
242 struct ctf_fs_file
*file
= NULL
;
243 struct ctf_fs_component
*ctf_fs
= ctf_fs_trace
->ctf_fs
;
245 /* Create one output port for each stream file */
246 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
248 PERR("Cannot open directory `%s`: %s (code %d)\n",
249 ctf_fs_trace
->path
->str
, error
->message
,
254 while ((basename
= g_dir_read_name(dir
))) {
255 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
256 /* Ignore the metadata stream. */
257 PDBG("Ignoring metadata file `%s/%s`\n",
258 ctf_fs_trace
->path
->str
, basename
);
262 if (basename
[0] == '.') {
263 PDBG("Ignoring hidden file `%s/%s`\n",
264 ctf_fs_trace
->path
->str
, basename
);
268 /* Create the file. */
269 file
= ctf_fs_file_create(ctf_fs
);
271 PERR("Cannot create stream file object for file `%s/%s`\n",
272 ctf_fs_trace
->path
->str
, basename
);
276 /* Create full path string. */
277 g_string_append_printf(file
->path
, "%s/%s",
278 ctf_fs_trace
->path
->str
, basename
);
279 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
280 PDBG("Ignoring non-regular file `%s`\n",
282 ctf_fs_file_destroy(file
);
287 ret
= ctf_fs_file_open(ctf_fs
, file
, "rb");
289 PERR("Cannot open stream file `%s`\n", file
->path
->str
);
293 if (file
->size
== 0) {
294 /* Skip empty stream. */
295 PDBG("Ignoring empty file `%s`\n", file
->path
->str
);
296 ctf_fs_file_destroy(file
);
301 ret
= create_one_port_for_trace(ctf_fs_trace
, file
->path
->str
);
303 PERR("Cannot create output port for file `%s`\n",
308 ctf_fs_file_destroy(file
);
327 ctf_fs_file_destroy(file
);
332 int create_cc_prio_map(struct ctf_fs_trace
*ctf_fs_trace
)
338 assert(ctf_fs_trace
);
339 ctf_fs_trace
->cc_prio_map
= bt_clock_class_priority_map_create();
340 if (!ctf_fs_trace
->cc_prio_map
) {
345 count
= bt_ctf_trace_get_clock_class_count(
346 ctf_fs_trace
->metadata
->trace
);
349 for (i
= 0; i
< count
; i
++) {
350 struct bt_ctf_clock_class
*clock_class
=
351 bt_ctf_trace_get_clock_class_by_index(
352 ctf_fs_trace
->metadata
->trace
, i
);
355 ret
= bt_clock_class_priority_map_add_clock_class(
356 ctf_fs_trace
->cc_prio_map
, clock_class
, 0);
369 struct ctf_fs_trace
*ctf_fs_trace_create(struct ctf_fs_component
*ctf_fs
,
370 const char *path
, const char *name
)
372 struct ctf_fs_trace
*ctf_fs_trace
;
375 ctf_fs_trace
= g_new0(struct ctf_fs_trace
, 1);
380 ctf_fs_trace
->ctf_fs
= ctf_fs
;
381 ctf_fs_trace
->path
= g_string_new(path
);
382 if (!ctf_fs_trace
->path
) {
386 ctf_fs_trace
->name
= g_string_new(name
);
387 if (!ctf_fs_trace
->name
) {
391 ctf_fs_trace
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
392 if (!ctf_fs_trace
->metadata
) {
396 ret
= ctf_fs_metadata_set_trace(ctf_fs_trace
);
401 ret
= create_cc_prio_map(ctf_fs_trace
);
406 ret
= create_ports_for_trace(ctf_fs_trace
);
414 ctf_fs_trace_destroy(ctf_fs_trace
);
421 int path_is_ctf_trace(const char *path
)
423 GString
*metadata_path
= g_string_new(NULL
);
426 if (!metadata_path
) {
431 g_string_printf(metadata_path
, "%s/%s", path
, CTF_FS_METADATA_FILENAME
);
433 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
439 g_string_free(metadata_path
, TRUE
);
444 int add_trace_path(struct ctf_fs_component
*ctf_fs
, GList
**trace_paths
,
447 GString
*path_str
= g_string_new(NULL
);
457 * Find the real path so that we don't have relative components
458 * in the trace name. This also squashes consecutive slashes and
459 * removes any slash at the end.
461 rp
= realpath(path
, NULL
);
463 PERR("realpath() failed: %s (%d)\n", strerror(errno
), errno
);
468 if (strcmp(rp
, "/") == 0) {
469 PERR("Opening a trace in `/` is not supported.\n");
474 g_string_assign(path_str
, rp
);
475 *trace_paths
= g_list_prepend(*trace_paths
, path_str
);
476 assert(*trace_paths
);
484 int find_ctf_traces(struct ctf_fs_component
*ctf_fs
,
485 GList
**trace_paths
, const char *start_path
)
488 GError
*error
= NULL
;
490 const char *basename
= NULL
;
492 /* Check if the starting path is a CTF trace itself */
493 ret
= path_is_ctf_trace(start_path
);
500 * Do not even recurse: a CTF trace cannot contain
503 ret
= add_trace_path(ctf_fs
, trace_paths
, start_path
);
507 /* Look for subdirectories */
508 if (!g_file_test(start_path
, G_FILE_TEST_IS_DIR
)) {
509 /* Starting path is not a directory: end of recursion */
513 dir
= g_dir_open(start_path
, 0, &error
);
515 if (error
->code
== G_FILE_ERROR_ACCES
) {
516 PDBG("Cannot open directory `%s`: %s (code %d): continuing\n",
517 start_path
, error
->message
, error
->code
);
521 PERR("Cannot open directory `%s`: %s (code %d)\n",
522 start_path
, error
->message
, error
->code
);
527 while ((basename
= g_dir_read_name(dir
))) {
528 GString
*sub_path
= g_string_new(NULL
);
535 g_string_printf(sub_path
, "%s/%s", start_path
, basename
);
536 ret
= find_ctf_traces(ctf_fs
, trace_paths
, sub_path
->str
);
537 g_string_free(sub_path
, TRUE
);
556 GList
*create_trace_names(GList
*trace_paths
) {
557 GList
*trace_names
= NULL
;
558 size_t chars_to_strip
= 0;
564 * Find the number of characters to strip from the beginning,
565 * that is, the longest prefix until a common slash (also
569 gchar common_ch
= '\0';
571 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
572 GString
*gstr
= node
->data
;
573 gchar this_ch
= gstr
->str
[at
];
575 if (this_ch
== '\0') {
580 if (common_ch
== '\0') {
582 * Establish the expected common
583 * character at this position.
589 if (this_ch
!= common_ch
) {
599 if (common_ch
== '/') {
601 * Common character is a slash: safe to include
602 * this slash in the number of characters to
603 * strip because the paths are guaranteed not to
606 chars_to_strip
= at
+ 1;
612 /* Create the trace names */
613 for (node
= trace_paths
; node
; node
= g_list_next(node
)) {
614 GString
*trace_name
= g_string_new(NULL
);
615 GString
*trace_path
= node
->data
;
617 g_string_assign(trace_name
, &trace_path
->str
[chars_to_strip
]);
618 trace_names
= g_list_append(trace_names
, trace_name
);
625 int create_ctf_fs_traces(struct ctf_fs_component
*ctf_fs
,
626 const char *path_param
)
628 struct ctf_fs_trace
*ctf_fs_trace
= NULL
;
630 GList
*trace_paths
= NULL
;
631 GList
*trace_names
= NULL
;
635 ret
= find_ctf_traces(ctf_fs
, &trace_paths
, path_param
);
641 PERR("No CTF traces recursively found in `%s`.\n",
646 trace_names
= create_trace_names(trace_paths
);
648 PERR("Cannot create trace names from trace paths.\n");
652 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
653 tp_node
= g_list_next(tp_node
),
654 tn_node
= g_list_next(tn_node
)) {
655 GString
*trace_path
= tp_node
->data
;
656 GString
*trace_name
= tn_node
->data
;
658 ctf_fs_trace
= ctf_fs_trace_create(ctf_fs
, trace_path
->str
,
661 PERR("Cannot create trace for `%s`.\n",
666 g_ptr_array_add(ctf_fs
->traces
, ctf_fs_trace
);
674 ctf_fs_trace_destroy(ctf_fs_trace
);
677 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
679 g_string_free(tp_node
->data
, TRUE
);
683 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
685 g_string_free(tn_node
->data
, TRUE
);
690 g_list_free(trace_paths
);
694 g_list_free(trace_names
);
701 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
702 struct bt_value
*params
)
704 struct ctf_fs_component
*ctf_fs
;
705 struct bt_value
*value
= NULL
;
706 const char *path_param
;
709 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
714 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
718 * We don't need to get a new reference here because as long as
719 * our private ctf_fs_component object exists, the containing
720 * private component should also exist.
722 ctf_fs
->priv_comp
= priv_comp
;
723 value
= bt_value_map_get(params
, "path");
724 if (!bt_value_is_string(value
)) {
728 ret
= bt_value_string_get(value
, &path_param
);
731 value
= bt_value_map_get(params
, "offset-s");
735 if (!bt_value_is_integer(value
)) {
737 "offset-s should be an integer\n");
740 ret
= bt_value_integer_get(value
, &offset
);
742 ctf_fs
->options
.clock_offset
= offset
;
746 value
= bt_value_map_get(params
, "offset-ns");
750 if (!bt_value_is_integer(value
)) {
752 "offset-ns should be an integer\n");
755 ret
= bt_value_integer_get(value
, &offset
);
757 ctf_fs
->options
.clock_offset_ns
= offset
;
761 ctf_fs
->error_fp
= stderr
;
762 ctf_fs
->page_size
= (size_t) getpagesize();
763 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
764 if (!ctf_fs
->port_data
) {
768 ctf_fs
->traces
= g_ptr_array_new_with_free_func(ctf_fs_trace_destroy
);
769 if (!ctf_fs
->traces
) {
773 ret
= create_ctf_fs_traces(ctf_fs
, path_param
);
781 ctf_fs_destroy(ctf_fs
);
783 ret
= bt_private_component_set_user_data(priv_comp
, NULL
);
792 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
793 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
795 struct ctf_fs_component
*ctf_fs
;
796 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
798 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
799 ctf_fs
= ctf_fs_create(priv_comp
, params
);
801 ret
= BT_COMPONENT_STATUS_ERROR
;
808 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
809 const char *object
, struct bt_value
*params
)
811 struct bt_value
*results
= NULL
;
812 struct bt_value
*path_value
= NULL
;
813 char *metadata_text
= NULL
;
814 FILE *metadata_fp
= NULL
;
815 GString
*g_metadata_text
= NULL
;
817 if (strcmp(object
, "metadata-info") == 0) {
823 results
= bt_value_map_create();
828 if (!bt_value_is_map(params
)) {
830 "Query parameters is not a map value object\n");
834 path_value
= bt_value_map_get(params
, "path");
835 ret
= bt_value_string_get(path_value
, &path
);
838 "Cannot get `path` string parameter\n");
843 metadata_fp
= ctf_fs_metadata_open_file(path
);
846 "Cannot open trace at path `%s`\n", path
);
850 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
854 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
855 metadata_fp
, &metadata_text
, bo
);
858 "Cannot decode packetized metadata file\n");
864 fseek(metadata_fp
, 0, SEEK_END
);
865 filesize
= ftell(metadata_fp
);
867 metadata_text
= malloc(filesize
+ 1);
868 if (!metadata_text
) {
870 "Cannot allocate buffer for metadata text\n");
874 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
877 "Cannot read metadata file\n");
881 metadata_text
[filesize
] = '\0';
884 g_metadata_text
= g_string_new(NULL
);
885 if (!g_metadata_text
) {
889 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
890 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
891 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
892 g_string_append(g_metadata_text
, " */\n\n");
895 g_string_append(g_metadata_text
, metadata_text
);
897 ret
= bt_value_map_insert_string(results
, "text",
898 g_metadata_text
->str
);
900 fprintf(stderr
, "Cannot insert metadata text into results\n");
904 ret
= bt_value_map_insert_bool(results
, "is-packetized",
907 fprintf(stderr
, "Cannot insert is packetized into results\n");
911 fprintf(stderr
, "Unknown query object `%s`\n", object
);
924 if (g_metadata_text
) {
925 g_string_free(g_metadata_text
, TRUE
);