4 * Babeltrace CTF file system Reader Component
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/graph/private-port.h>
32 #include <babeltrace/graph/private-component.h>
33 #include <babeltrace/graph/private-component-source.h>
34 #include <babeltrace/graph/private-notification-iterator.h>
35 #include <babeltrace/graph/component.h>
36 #include <babeltrace/graph/notification-iterator.h>
37 #include <babeltrace/graph/clock-class-priority-map.h>
38 #include <plugins-common.h>
45 #include "data-stream.h"
47 #include "../common/metadata/decoder.h"
49 #define PRINT_ERR_STREAM ctf_fs->error_fp
50 #define PRINT_PREFIX "ctf-fs"
51 #define PRINT_DBG_CHECK ctf_fs_debug
53 #define METADATA_TEXT_SIG "/* CTF 1.8"
58 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
59 struct bt_private_notification_iterator
*iterator
)
61 struct ctf_fs_stream
*fs_stream
=
62 bt_private_notification_iterator_get_user_data(iterator
);
64 return ctf_fs_stream_next(fs_stream
);
67 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
70 bt_private_notification_iterator_get_user_data(it
);
72 ctf_fs_stream_destroy(ctf_fs_stream
);
75 enum bt_notification_iterator_status
ctf_fs_iterator_init(
76 struct bt_private_notification_iterator
*it
,
77 struct bt_private_port
*port
)
79 struct ctf_fs_stream
*stream
= NULL
;
80 struct ctf_fs_component
*ctf_fs
;
81 struct ctf_fs_port_data
*port_data
;
82 struct bt_private_component
*priv_comp
=
83 bt_private_notification_iterator_get_private_component(it
);
84 enum bt_notification_iterator_status ret
=
85 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
89 ctf_fs
= bt_private_component_get_user_data(priv_comp
);
91 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
95 port_data
= bt_private_port_get_user_data(port
);
97 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
101 stream
= ctf_fs_stream_create(ctf_fs
, port_data
->path
->str
);
106 ret
= bt_private_notification_iterator_set_user_data(it
, stream
);
115 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
117 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
118 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
122 ctf_fs_stream_destroy(stream
);
128 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
134 if (ctf_fs
->trace_path
) {
135 g_string_free(ctf_fs
->trace_path
, TRUE
);
138 if (ctf_fs
->port_data
) {
139 g_ptr_array_free(ctf_fs
->port_data
, TRUE
);
142 if (ctf_fs
->metadata
) {
143 ctf_fs_metadata_fini(ctf_fs
->metadata
);
144 g_free(ctf_fs
->metadata
);
147 bt_put(ctf_fs
->cc_prio_map
);
151 void ctf_fs_finalize(struct bt_private_component
*component
)
153 void *data
= bt_private_component_get_user_data(component
);
155 ctf_fs_destroy_data(data
);
159 void port_data_destroy(void *data
) {
160 struct ctf_fs_port_data
*port_data
= data
;
166 if (port_data
->path
) {
167 g_string_free(port_data
->path
, TRUE
);
174 int create_one_port(struct ctf_fs_component
*ctf_fs
,
175 const char *stream_basename
, const char *stream_path
)
178 struct bt_private_port
*port
= NULL
;
179 struct ctf_fs_port_data
*port_data
= NULL
;
180 GString
*port_name
= NULL
;
182 port_name
= g_string_new(NULL
);
187 /* Assign the name for the new output port */
188 g_string_assign(port_name
, "");
189 g_string_printf(port_name
, "trace0-stream-%s", stream_basename
);
190 PDBG("Creating one port named `%s` associated with path `%s`\n",
191 port_name
->str
, stream_path
);
193 /* Create output port for this file */
194 port_data
= g_new0(struct ctf_fs_port_data
, 1);
199 port_data
->path
= g_string_new(stream_path
);
200 if (!port_data
->path
) {
204 port
= bt_private_component_source_add_output_private_port(
205 ctf_fs
->priv_comp
, port_name
->str
, port_data
);
210 g_ptr_array_add(ctf_fs
->port_data
, port_data
);
219 g_string_free(port_name
, TRUE
);
223 port_data_destroy(port_data
);
228 int create_ports(struct ctf_fs_component
*ctf_fs
)
231 const char *basename
;
232 GError
*error
= NULL
;
234 struct ctf_fs_file
*file
= NULL
;
236 /* Create one output port for each stream file */
237 dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
239 PERR("Cannot open directory `%s`: %s (code %d)\n",
240 ctf_fs
->trace_path
->str
, error
->message
,
245 while ((basename
= g_dir_read_name(dir
))) {
246 if (!strcmp(basename
, CTF_FS_METADATA_FILENAME
)) {
247 /* Ignore the metadata stream. */
248 PDBG("Ignoring metadata file `%s`\n", basename
);
252 if (basename
[0] == '.') {
253 PDBG("Ignoring hidden file `%s`\n", basename
);
257 /* Create the file. */
258 file
= ctf_fs_file_create(ctf_fs
);
260 PERR("Cannot create stream file object for file `%s`\n",
265 /* Create full path string. */
266 g_string_append_printf(file
->path
, "%s/%s",
267 ctf_fs
->trace_path
->str
, basename
);
268 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
269 PDBG("Ignoring non-regular file `%s`\n", basename
);
270 ctf_fs_file_destroy(file
);
275 ret
= ctf_fs_file_open(ctf_fs
, file
, "rb");
277 PERR("Cannot open stream file `%s`\n", basename
);
281 if (file
->size
== 0) {
282 /* Skip empty stream. */
283 PDBG("Ignoring empty file `%s`\n", basename
);
284 ctf_fs_file_destroy(file
);
289 ret
= create_one_port(ctf_fs
, basename
, file
->path
->str
);
291 PERR("Cannot create output port for file `%s`\n",
296 ctf_fs_file_destroy(file
);
315 ctf_fs_file_destroy(file
);
320 int create_cc_prio_map(struct ctf_fs_component
*ctf_fs
)
327 ctf_fs
->cc_prio_map
= bt_clock_class_priority_map_create();
328 if (!ctf_fs
->cc_prio_map
) {
333 count
= bt_ctf_trace_get_clock_class_count(ctf_fs
->metadata
->trace
);
336 for (i
= 0; i
< count
; i
++) {
337 struct bt_ctf_clock_class
*clock_class
=
338 bt_ctf_trace_get_clock_class_by_index(
339 ctf_fs
->metadata
->trace
, i
);
342 ret
= bt_clock_class_priority_map_add_clock_class(
343 ctf_fs
->cc_prio_map
, clock_class
, 0);
356 struct ctf_fs_component
*ctf_fs_create(struct bt_private_component
*priv_comp
,
357 struct bt_value
*params
)
359 struct ctf_fs_component
*ctf_fs
;
360 struct bt_value
*value
= NULL
;
364 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
370 * We don't need to get a new reference here because as long as
371 * our private ctf_fs_component object exists, the containing
372 * private component should also exist.
374 ctf_fs
->priv_comp
= priv_comp
;
376 /* FIXME: should probably look for a source URI */
377 value
= bt_value_map_get(params
, "path");
378 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
382 ret
= bt_value_string_get(value
, &path
);
387 ctf_fs
->port_data
= g_ptr_array_new_with_free_func(port_data_destroy
);
388 if (!ctf_fs
->port_data
) {
392 ctf_fs
->trace_path
= g_string_new(path
);
393 if (!ctf_fs
->trace_path
) {
399 value
= bt_value_map_get(params
, "offset-s");
403 if (!bt_value_is_integer(value
)) {
405 "offset-s should be an integer\n");
408 ret
= bt_value_integer_get(value
, &offset
);
409 if (ret
!= BT_VALUE_STATUS_OK
) {
411 "Failed to get offset-s value\n");
414 ctf_fs
->options
.clock_offset
= offset
;
418 value
= bt_value_map_get(params
, "offset-ns");
422 if (!bt_value_is_integer(value
)) {
424 "offset-ns should be an integer\n");
427 ret
= bt_value_integer_get(value
, &offset
);
428 if (ret
!= BT_VALUE_STATUS_OK
) {
430 "Failed to get offset-ns value\n");
433 ctf_fs
->options
.clock_offset_ns
= offset
;
437 ctf_fs
->error_fp
= stderr
;
438 ctf_fs
->page_size
= (size_t) getpagesize();
440 // FIXME: check error.
441 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
442 if (!ctf_fs
->metadata
) {
446 ret
= ctf_fs_metadata_set_trace(ctf_fs
);
451 ret
= create_cc_prio_map(ctf_fs
);
456 ret
= create_ports(ctf_fs
);
464 ctf_fs_destroy_data(ctf_fs
);
471 enum bt_component_status
ctf_fs_init(struct bt_private_component
*priv_comp
,
472 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
474 struct ctf_fs_component
*ctf_fs
;
475 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
478 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
479 ctf_fs
= ctf_fs_create(priv_comp
, params
);
481 ret
= BT_COMPONENT_STATUS_NOMEM
;
485 ret
= bt_private_component_set_user_data(priv_comp
, ctf_fs
);
486 if (ret
!= BT_COMPONENT_STATUS_OK
) {
492 (void) bt_private_component_set_user_data(priv_comp
, NULL
);
493 ctf_fs_destroy_data(ctf_fs
);
498 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
499 const char *object
, struct bt_value
*params
)
501 struct bt_value
*results
= NULL
;
502 struct bt_value
*path_value
= NULL
;
503 char *metadata_text
= NULL
;
504 FILE *metadata_fp
= NULL
;
505 GString
*g_metadata_text
= NULL
;
507 if (strcmp(object
, "metadata-info") == 0) {
513 results
= bt_value_map_create();
518 if (!bt_value_is_map(params
)) {
520 "Query parameters is not a map value object\n");
524 path_value
= bt_value_map_get(params
, "path");
525 ret
= bt_value_string_get(path_value
, &path
);
528 "Cannot get `path` string parameter\n");
533 metadata_fp
= ctf_fs_metadata_open_file(path
);
536 "Cannot open trace at path `%s`\n", path
);
540 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
544 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
545 metadata_fp
, &metadata_text
, bo
);
548 "Cannot decode packetized metadata file\n");
554 fseek(metadata_fp
, 0, SEEK_END
);
555 filesize
= ftell(metadata_fp
);
557 metadata_text
= malloc(filesize
+ 1);
558 if (!metadata_text
) {
560 "Cannot allocate buffer for metadata text\n");
564 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
567 "Cannot read metadata file\n");
571 metadata_text
[filesize
] = '\0';
574 g_metadata_text
= g_string_new(NULL
);
575 if (!g_metadata_text
) {
579 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
580 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
581 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
582 g_string_append(g_metadata_text
, " */\n\n");
585 g_string_append(g_metadata_text
, metadata_text
);
587 ret
= bt_value_map_insert_string(results
, "text",
588 g_metadata_text
->str
);
590 fprintf(stderr
, "Cannot insert metadata text into results\n");
594 ret
= bt_value_map_insert_bool(results
, "is-packetized",
597 fprintf(stderr
, "Cannot insert is packetized into results\n");
601 fprintf(stderr
, "Unknown query object `%s`\n", object
);
614 if (g_metadata_text
) {
615 g_string_free(g_metadata_text
, TRUE
);