2 * SPDX-License-Identifier: MIT
4 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Babeltrace CTF file system Reader Component
10 #define BT_CLOG_CFG logCfg
11 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS"
13 #include "common/common.h"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/uuid.h"
17 #include "common/assert.h"
21 #include "metadata.hpp"
22 #include "data-stream-file.hpp"
24 #include "../common/src/metadata/tsdl/decoder.hpp"
25 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
26 #include "../common/src/msg-iter/msg-iter.hpp"
28 #include "plugins/common/param-validation/param-validation.h"
29 #include "cpp-common/cfg-logging.hpp"
30 #include "cpp-common/cfg-logging-error-reporting.hpp"
31 #include "cpp-common/exc.hpp"
32 #include "cpp-common/make-unique.hpp"
43 static void ctf_fs_msg_iter_data_destroy(struct ctf_fs_msg_iter_data
*msg_iter_data
)
49 if (msg_iter_data
->msg_iter
) {
50 ctf_msg_iter_destroy(msg_iter_data
->msg_iter
);
53 if (msg_iter_data
->msg_iter_medops_data
) {
54 ctf_fs_ds_group_medops_data_destroy(msg_iter_data
->msg_iter_medops_data
);
60 static bt_message_iterator_class_next_method_status
61 ctf_fs_iterator_next_one(struct ctf_fs_msg_iter_data
*msg_iter_data
, const bt_message
**out_msg
)
63 bt_message_iterator_class_next_method_status status
;
64 enum ctf_msg_iter_status msg_iter_status
;
65 const bt2_common::LogCfg
& logCfg
= msg_iter_data
->logCfg
;
67 msg_iter_status
= ctf_msg_iter_get_next_message(msg_iter_data
->msg_iter
, out_msg
);
69 switch (msg_iter_status
) {
70 case CTF_MSG_ITER_STATUS_OK
:
71 /* Cool, message has been written to *out_msg. */
72 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
75 case CTF_MSG_ITER_STATUS_EOF
:
76 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
79 case CTF_MSG_ITER_STATUS_AGAIN
:
81 * Should not make it this far as this is
82 * medium-specific; there is nothing for the user to do
83 * and it should have been handled upstream.
87 case CTF_MSG_ITER_STATUS_ERROR
:
88 BT_CLOGE_APPEND_CAUSE("Failed to get next message from CTF message iterator.");
89 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
92 case CTF_MSG_ITER_STATUS_MEMORY_ERROR
:
93 BT_CLOGE_APPEND_CAUSE("Failed to get next message from CTF message iterator.");
94 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
105 bt_message_iterator_class_next_method_status
106 ctf_fs_iterator_next(bt_self_message_iterator
*iterator
, bt_message_array_const msgs
,
107 uint64_t capacity
, uint64_t *count
)
109 struct ctf_fs_msg_iter_data
*msg_iter_data
=
110 (struct ctf_fs_msg_iter_data
*) bt_self_message_iterator_get_data(iterator
);
111 const bt2_common::LogCfg
& logCfg
= msg_iter_data
->logCfg
;
114 bt_message_iterator_class_next_method_status status
;
117 if (G_UNLIKELY(msg_iter_data
->next_saved_error
)) {
119 * Last time we were called, we hit an error but had some
120 * messages to deliver, so we stashed the error here. Return
123 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(msg_iter_data
->next_saved_error
);
124 status
= msg_iter_data
->next_saved_status
;
129 status
= ctf_fs_iterator_next_one(msg_iter_data
, &msgs
[i
]);
130 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
133 } while (i
< capacity
&& status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
);
137 * Even if ctf_fs_iterator_next_one() returned something
138 * else than BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK, we
139 * accumulated message objects in the output
140 * message array, so we need to return
141 * BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK so that they are
142 * transfered to downstream. This other status occurs
143 * again the next time muxer_msg_iter_do_next() is
144 * called, possibly without any accumulated
145 * message, in which case we'll return it.
149 * Save this error for the next _next call. Assume that
150 * this component always appends error causes when
151 * returning an error status code, which will cause the
152 * current thread error to be non-NULL.
154 msg_iter_data
->next_saved_error
= bt_current_thread_take_error();
155 BT_ASSERT(msg_iter_data
->next_saved_error
);
156 msg_iter_data
->next_saved_status
= status
;
160 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
165 } catch (const std::bad_alloc
&) {
166 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
167 } catch (const bt2_common::Error
&) {
168 BT_CLOGE_APPEND_CAUSE("Failed to fetch next messages");
169 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
174 bt_message_iterator_class_seek_beginning_method_status
175 ctf_fs_iterator_seek_beginning(bt_self_message_iterator
*it
)
177 struct ctf_fs_msg_iter_data
*msg_iter_data
=
178 (struct ctf_fs_msg_iter_data
*) bt_self_message_iterator_get_data(it
);
180 BT_ASSERT(msg_iter_data
);
182 const bt2_common::LogCfg
& logCfg
= msg_iter_data
->logCfg
;
185 ctf_msg_iter_reset(msg_iter_data
->msg_iter
);
186 ctf_fs_ds_group_medops_data_reset(msg_iter_data
->msg_iter_medops_data
);
188 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK
;
189 } catch (const std::bad_alloc
&) {
190 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR
;
191 } catch (const bt2_common::Error
&) {
192 BT_CLOGE_APPEND_CAUSE("Failed to seek beginning");
193 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR
;
198 void ctf_fs_iterator_finalize(bt_self_message_iterator
*it
)
200 ctf_fs_msg_iter_data_destroy(
201 (struct ctf_fs_msg_iter_data
*) bt_self_message_iterator_get_data(it
));
204 static bt_message_iterator_class_initialize_method_status
205 ctf_msg_iter_medium_status_to_msg_iter_initialize_status(enum ctf_msg_iter_medium_status status
)
208 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
209 case CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
:
210 case CTF_MSG_ITER_MEDIUM_STATUS_ERROR
:
211 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
212 case CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR
:
213 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
214 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
215 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
222 bt_message_iterator_class_initialize_method_status
223 ctf_fs_iterator_init(bt_self_message_iterator
*self_msg_iter
,
224 bt_self_message_iterator_configuration
*config
,
225 bt_self_component_port_output
*self_port
)
227 bt_self_component_port
*self_comp_port
=
228 bt_self_component_port_output_as_self_component_port(self_port
);
229 ctf_fs_port_data
*port_data
=
230 (ctf_fs_port_data
*) bt_self_component_port_get_data(self_comp_port
);
231 BT_ASSERT(port_data
);
233 bt2_common::LogCfg logCfg
{port_data
->ctf_fs
->logCfg
.logLevel(), *self_msg_iter
};
236 bt_message_iterator_class_initialize_method_status status
;
237 enum ctf_msg_iter_medium_status medium_status
;
239 ctf_fs_msg_iter_data
*msg_iter_data
= new ctf_fs_msg_iter_data
{logCfg
};
241 msg_iter_data
->self_msg_iter
= self_msg_iter
;
242 msg_iter_data
->ds_file_group
= port_data
->ds_file_group
;
245 ctf_fs_ds_group_medops_data_create(msg_iter_data
->ds_file_group
, self_msg_iter
, logCfg
,
246 &msg_iter_data
->msg_iter_medops_data
);
247 BT_ASSERT(medium_status
== CTF_MSG_ITER_MEDIUM_STATUS_OK
||
248 medium_status
== CTF_MSG_ITER_MEDIUM_STATUS_ERROR
||
249 medium_status
== CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR
);
250 if (medium_status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
251 BT_CLOGE_APPEND_CAUSE("Failed to create ctf_fs_ds_group_medops");
252 status
= ctf_msg_iter_medium_status_to_msg_iter_initialize_status(medium_status
);
256 msg_iter_data
->msg_iter
= ctf_msg_iter_create(
257 msg_iter_data
->ds_file_group
->ctf_fs_trace
->metadata
->tc
,
258 bt_common_get_page_size(logCfg
.logLevel()) * 8, ctf_fs_ds_group_medops
,
259 msg_iter_data
->msg_iter_medops_data
, self_msg_iter
, logCfg
);
260 if (!msg_iter_data
->msg_iter
) {
261 BT_CLOGE_APPEND_CAUSE("Cannot create a CTF message iterator.");
262 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
267 * This iterator can seek forward if its stream class has a default
270 if (msg_iter_data
->ds_file_group
->sc
->default_clock_class
) {
271 bt_self_message_iterator_configuration_set_can_seek_forward(config
, true);
274 bt_self_message_iterator_set_data(self_msg_iter
, msg_iter_data
);
275 msg_iter_data
= NULL
;
277 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
281 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
284 ctf_fs_msg_iter_data_destroy(msg_iter_data
);
286 } catch (const std::bad_alloc
&) {
287 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
288 } catch (const bt2_common::Error
&) {
289 BT_CLOGE_APPEND_CAUSE("Failed to initialize iterator");
290 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
294 static void ctf_fs_trace_destroy(struct ctf_fs_trace
*ctf_fs_trace
)
300 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace
->trace
);
302 if (ctf_fs_trace
->path
) {
303 g_string_free(ctf_fs_trace
->path
, TRUE
);
306 if (ctf_fs_trace
->metadata
) {
307 ctf_fs_metadata_fini(ctf_fs_trace
->metadata
);
308 delete ctf_fs_trace
->metadata
;
314 void ctf_fs_trace_deleter::operator()(ctf_fs_trace
*trace
)
316 ctf_fs_trace_destroy(trace
);
320 void ctf_fs_destroy(struct ctf_fs_component
*ctf_fs
)
329 void ctf_fs_component_deleter::operator()(ctf_fs_component
*comp
)
331 ctf_fs_destroy(comp
);
334 ctf_fs_component::UP
ctf_fs_component_create(const bt2_common::LogCfg
& logCfg
)
336 return ctf_fs_component::UP
{new ctf_fs_component
{logCfg
}};
339 void ctf_fs_finalize(bt_self_component_source
*component
)
341 ctf_fs_destroy((struct ctf_fs_component
*) bt_self_component_get_data(
342 bt_self_component_source_as_self_component(component
)));
345 bt2_common::GCharUP
ctf_fs_make_port_name(struct ctf_fs_ds_file_group
*ds_file_group
)
347 GString
*name
= g_string_new(NULL
);
350 * The unique port name is generated by concatenating unique identifiers
358 /* For the trace, use the uuid if present, else the path. */
359 if (ds_file_group
->ctf_fs_trace
->metadata
->tc
->is_uuid_set
) {
360 char uuid_str
[BT_UUID_STR_LEN
+ 1];
362 bt_uuid_to_str(ds_file_group
->ctf_fs_trace
->metadata
->tc
->uuid
, uuid_str
);
363 g_string_assign(name
, uuid_str
);
365 g_string_assign(name
, ds_file_group
->ctf_fs_trace
->path
->str
);
369 * For the stream class, use the id if present. We can omit this field
370 * otherwise, as there will only be a single stream class.
372 if (ds_file_group
->sc
->id
!= UINT64_C(-1)) {
373 g_string_append_printf(name
, " | %" PRIu64
, ds_file_group
->sc
->id
);
376 /* For the stream, use the id if present, else, use the path. */
377 if (ds_file_group
->stream_id
!= UINT64_C(-1)) {
378 g_string_append_printf(name
, " | %" PRIu64
, ds_file_group
->stream_id
);
380 BT_ASSERT(ds_file_group
->ds_file_infos
.size() == 1);
381 const ctf_fs_ds_file_info
& ds_file_info
= *ds_file_group
->ds_file_infos
[0];
382 g_string_append_printf(name
, " | %s", ds_file_info
.path
.c_str());
385 return bt2_common::GCharUP
{g_string_free(name
, FALSE
)};
388 static int create_one_port_for_trace(struct ctf_fs_component
*ctf_fs
,
389 struct ctf_fs_trace
*ctf_fs_trace
,
390 struct ctf_fs_ds_file_group
*ds_file_group
,
391 bt_self_component_source
*self_comp_src
)
394 ctf_fs_port_data::UP port_data
;
395 const bt2_common::LogCfg
& logCfg
= ctf_fs
->logCfg
;
397 bt2_common::GCharUP port_name
= ctf_fs_make_port_name(ds_file_group
);
402 BT_CLOGI("Creating one port named `%s`", port_name
.get());
404 /* Create output port for this file */
405 port_data
= bt2_common::makeUnique
<ctf_fs_port_data
>();
406 port_data
->ctf_fs
= ctf_fs
;
407 port_data
->ds_file_group
= ds_file_group
;
408 ret
= bt_self_component_source_add_output_port(self_comp_src
, port_name
.get(), port_data
.get(),
414 ctf_fs
->port_data
.emplace_back(std::move(port_data
));
424 static int create_ports_for_trace(struct ctf_fs_component
*ctf_fs
,
425 struct ctf_fs_trace
*ctf_fs_trace
,
426 bt_self_component_source
*self_comp_src
)
429 const bt2_common::LogCfg
& logCfg
= ctf_fs
->logCfg
;
431 /* Create one output port for each stream file group */
432 for (ctf_fs_ds_file_group::UP
& ds_file_group
: ctf_fs_trace
->ds_file_groups
) {
433 ret
= create_one_port_for_trace(ctf_fs
, ctf_fs_trace
, ds_file_group
.get(), self_comp_src
);
435 BT_CLOGE_APPEND_CAUSE("Cannot create output port.");
444 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
445 static void array_insert(GPtrArray
*array
, gpointer element
, size_t pos
)
447 size_t original_array_len
= array
->len
;
449 /* Allocate an unused element at the end of the array. */
450 g_ptr_array_add(array
, NULL
);
452 /* If we are not inserting at the end, move the elements by one. */
453 if (pos
< original_array_len
) {
454 memmove(&(array
->pdata
[pos
+ 1]), &(array
->pdata
[pos
]),
455 (original_array_len
- pos
) * sizeof(gpointer
));
458 /* Insert the value. */
459 array
->pdata
[pos
] = element
;
463 * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
464 * place to keep it sorted.
467 static void ds_file_group_insert_ds_file_info_sorted(struct ctf_fs_ds_file_group
*ds_file_group
,
468 ctf_fs_ds_file_info::UP ds_file_info
)
470 /* Find the spot where to insert this ds_file_info. */
471 auto it
= ds_file_group
->ds_file_infos
.begin();
473 for (; it
!= ds_file_group
->ds_file_infos
.end(); ++it
) {
474 const ctf_fs_ds_file_info
& other_ds_file_info
= **it
;
476 if (ds_file_info
->begin_ns
< other_ds_file_info
.begin_ns
) {
481 ds_file_group
->ds_file_infos
.insert(it
, std::move(ds_file_info
));
484 static bool ds_index_entries_equal(const struct ctf_fs_ds_index_entry
*left
,
485 const struct ctf_fs_ds_index_entry
*right
)
487 if (left
->packetSize
!= right
->packetSize
) {
491 if (left
->timestamp_begin
!= right
->timestamp_begin
) {
495 if (left
->timestamp_end
!= right
->timestamp_end
) {
499 if (left
->packet_seq_num
!= right
->packet_seq_num
) {
507 * Insert `entry` into `index`, without duplication.
509 * The entry is inserted only if there isn't an identical entry already.
511 * In any case, the ownership of `entry` is transferred to this function. So if
512 * the entry is not inserted, it is freed.
515 static void ds_index_insert_ds_index_entry_sorted(struct ctf_fs_ds_index
*index
,
516 struct ctf_fs_ds_index_entry
*entry
)
519 struct ctf_fs_ds_index_entry
*other_entry
= NULL
;
521 /* Find the spot where to insert this index entry. */
522 for (i
= 0; i
< index
->entries
->len
; i
++) {
523 other_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, i
);
525 if (entry
->timestamp_begin_ns
<= other_entry
->timestamp_begin_ns
) {
531 * Insert the entry only if a duplicate doesn't already exist.
533 * There can be duplicate packets if reading multiple overlapping
534 * snapshots of the same trace. We then want the index to contain
535 * a reference to only one copy of that packet.
537 if (i
== index
->entries
->len
|| !ds_index_entries_equal(entry
, other_entry
)) {
538 array_insert(index
->entries
, entry
, i
);
544 static void merge_ctf_fs_ds_indexes(struct ctf_fs_ds_index
*dest
, struct ctf_fs_ds_index
*src
)
548 for (i
= 0; i
< src
->entries
->len
; i
++) {
549 struct ctf_fs_ds_index_entry
*entry
=
550 (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(src
->entries
, i
);
553 * Ownership of the ctf_fs_ds_index_entry is transferred to
554 * ds_index_insert_ds_index_entry_sorted.
556 g_ptr_array_index(src
->entries
, i
) = NULL
;
557 ds_index_insert_ds_index_entry_sorted(dest
, entry
);
561 static int add_ds_file_to_ds_file_group(struct ctf_fs_trace
*ctf_fs_trace
, const char *path
)
563 int64_t stream_instance_id
= -1;
564 int64_t begin_ns
= -1;
565 struct ctf_fs_ds_file_group
*ds_file_group
= NULL
;
566 ctf_fs_ds_file_group::UP new_ds_file_group
;
568 struct ctf_fs_ds_file
*ds_file
= NULL
;
569 ctf_fs_ds_file_info::UP ds_file_info
;
570 struct ctf_fs_ds_index
*index
= NULL
;
571 struct ctf_msg_iter
*msg_iter
= NULL
;
572 struct ctf_stream_class
*sc
= NULL
;
573 struct ctf_msg_iter_packet_properties props
;
574 const bt2_common::LogCfg
& logCfg
= ctf_fs_trace
->logCfg
;
577 * Create a temporary ds_file to read some properties about the data
580 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, NULL
, path
, logCfg
);
585 /* Create a temporary iterator to read the ds_file. */
586 msg_iter
= ctf_msg_iter_create(ctf_fs_trace
->metadata
->tc
,
587 bt_common_get_page_size(logCfg
.logLevel()) * 8,
588 ctf_fs_ds_file_medops
, ds_file
, nullptr, logCfg
);
590 BT_CLOGE_STR("Cannot create a CTF message iterator.");
594 ctf_msg_iter_set_dry_run(msg_iter
, true);
596 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
598 BT_CLOGE_APPEND_CAUSE(
599 "Cannot get stream file's first packet's header and context fields (`%s`).", path
);
603 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
605 stream_instance_id
= props
.data_stream_id
;
607 if (props
.snapshots
.beginning_clock
!= UINT64_C(-1)) {
608 BT_ASSERT(sc
->default_clock_class
);
609 ret
= bt_util_clock_cycles_to_ns_from_origin(
610 props
.snapshots
.beginning_clock
, sc
->default_clock_class
->frequency
,
611 sc
->default_clock_class
->offset_seconds
, sc
->default_clock_class
->offset_cycles
,
614 BT_CLOGE_APPEND_CAUSE("Cannot convert clock cycles to nanoseconds from origin (`%s`).",
620 ds_file_info
= ctf_fs_ds_file_info_create(path
, begin_ns
);
625 index
= ctf_fs_ds_file_build_index(ds_file
, ds_file_info
.get(), msg_iter
).release();
627 BT_CLOGE_APPEND_CAUSE("Failed to index CTF stream file \'%s\'", ds_file
->file
->path
->str
);
631 if (begin_ns
== -1) {
633 * No beginning timestamp to sort the stream files
634 * within a stream file group, so consider that this
635 * file must be the only one within its group.
637 stream_instance_id
= -1;
640 if (stream_instance_id
== -1) {
642 * No stream instance ID or no beginning timestamp:
643 * create a unique stream file group for this stream
644 * file because, even if there's a stream instance ID,
645 * there's no timestamp to order the file within its
648 new_ds_file_group
= ctf_fs_ds_file_group_create(ctf_fs_trace
, sc
, UINT64_C(-1), index
);
649 /* Ownership of index is transferred. */
652 if (!new_ds_file_group
) {
656 ds_file_group_insert_ds_file_info_sorted(new_ds_file_group
.get(), std::move(ds_file_info
));
657 ctf_fs_trace
->ds_file_groups
.emplace_back(std::move(new_ds_file_group
));
661 BT_ASSERT(stream_instance_id
!= -1);
662 BT_ASSERT(begin_ns
!= -1);
664 /* Find an existing stream file group with this ID */
665 for (ctf_fs_ds_file_group::UP
& candidate
: ctf_fs_trace
->ds_file_groups
) {
666 if (candidate
->sc
== sc
&& candidate
->stream_id
== stream_instance_id
) {
667 ds_file_group
= candidate
.get();
672 if (!ds_file_group
) {
674 ctf_fs_ds_file_group_create(ctf_fs_trace
, sc
, stream_instance_id
, index
);
675 /* Ownership of index is transferred. */
677 if (!new_ds_file_group
) {
681 ds_file_group
= new_ds_file_group
.get();
682 ctf_fs_trace
->ds_file_groups
.emplace_back(std::move(new_ds_file_group
));
684 merge_ctf_fs_ds_indexes(ds_file_group
->index
, index
);
687 ds_file_group_insert_ds_file_info_sorted(ds_file_group
, std::move(ds_file_info
));
695 ctf_fs_ds_file_destroy(ds_file
);
698 ctf_msg_iter_destroy(msg_iter
);
701 ctf_fs_ds_index_destroy(index
);
705 static int create_ds_file_groups(struct ctf_fs_trace
*ctf_fs_trace
)
708 const char *basename
;
709 GError
*error
= NULL
;
711 const bt2_common::LogCfg
& logCfg
= ctf_fs_trace
->logCfg
;
713 /* Check each file in the path directory, except specific ones */
714 dir
= g_dir_open(ctf_fs_trace
->path
->str
, 0, &error
);
716 BT_CLOGE_APPEND_CAUSE("Cannot open directory `%s`: %s (code %d)", ctf_fs_trace
->path
->str
,
717 error
->message
, error
->code
);
721 while ((basename
= g_dir_read_name(dir
))) {
722 struct ctf_fs_file
*file
;
724 if (strcmp(basename
, CTF_FS_METADATA_FILENAME
) == 0) {
725 /* Ignore the metadata stream. */
726 BT_CLOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S
"%s`", ctf_fs_trace
->path
->str
,
731 if (basename
[0] == '.') {
732 BT_CLOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S
"%s`", ctf_fs_trace
->path
->str
,
737 /* Create the file. */
738 file
= ctf_fs_file_create(logCfg
);
740 BT_CLOGE_APPEND_CAUSE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
742 ctf_fs_trace
->path
->str
, basename
);
746 /* Create full path string. */
747 g_string_append_printf(file
->path
, "%s" G_DIR_SEPARATOR_S
"%s", ctf_fs_trace
->path
->str
,
749 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
750 BT_CLOGI("Ignoring non-regular file `%s`", file
->path
->str
);
751 ctf_fs_file_destroy(file
);
756 ret
= ctf_fs_file_open(file
, "rb");
758 BT_CLOGE_APPEND_CAUSE("Cannot open stream file `%s`", file
->path
->str
);
762 if (file
->size
== 0) {
763 /* Skip empty stream. */
764 BT_CLOGI("Ignoring empty file `%s`", file
->path
->str
);
765 ctf_fs_file_destroy(file
);
769 ret
= add_ds_file_to_ds_file_group(ctf_fs_trace
, file
->path
->str
);
771 BT_CLOGE_APPEND_CAUSE("Cannot add stream file `%s` to stream file group",
773 ctf_fs_file_destroy(file
);
777 ctf_fs_file_destroy(file
);
798 static int set_trace_name(bt_trace
*trace
, const char *name_suffix
,
799 const bt2_common::LogCfg
& logCfg
)
805 name
= g_string_new(NULL
);
807 BT_CLOGE_STR("Failed to allocate a GString.");
813 * Check if we have a trace environment string value named `hostname`.
814 * If so, use it as the trace name's prefix.
816 val
= bt_trace_borrow_environment_entry_value_by_name_const(trace
, "hostname");
817 if (val
&& bt_value_is_string(val
)) {
818 g_string_append(name
, bt_value_string_get(val
));
821 g_string_append_c(name
, G_DIR_SEPARATOR
);
826 g_string_append(name
, name_suffix
);
829 ret
= bt_trace_set_name(trace
, name
->str
);
838 g_string_free(name
, TRUE
);
844 static ctf_fs_trace::UP
ctf_fs_trace_create(const char *path
, const char *name
,
845 ctf::src::ClkClsCfg clkClsCfg
,
846 bt_self_component
*selfComp
,
847 const bt2_common::LogCfg
& logCfg
)
851 ctf_fs_trace::UP ctf_fs_trace
{new struct ctf_fs_trace(logCfg
)};
852 ctf_fs_trace
->path
= g_string_new(path
);
853 if (!ctf_fs_trace
->path
) {
857 ctf_fs_trace
->metadata
= new ctf_fs_metadata
;
858 ctf_fs_metadata_init(ctf_fs_trace
->metadata
);
860 ret
= ctf_fs_metadata_set_trace_class(ctf_fs_trace
.get(), clkClsCfg
, selfComp
, logCfg
);
865 if (ctf_fs_trace
->metadata
->trace_class
) {
866 ctf_fs_trace
->trace
= bt_trace_create(ctf_fs_trace
->metadata
->trace_class
);
867 if (!ctf_fs_trace
->trace
) {
872 if (ctf_fs_trace
->trace
) {
873 ret
= ctf_trace_class_configure_ir_trace(ctf_fs_trace
->metadata
->tc
, ctf_fs_trace
->trace
);
878 ret
= set_trace_name(ctf_fs_trace
->trace
, name
, logCfg
);
884 ret
= create_ds_file_groups(ctf_fs_trace
.get());
892 ctf_fs_trace
.reset();
898 static int path_is_ctf_trace(const char *path
)
900 GString
*metadata_path
= g_string_new(NULL
);
903 if (!metadata_path
) {
908 g_string_printf(metadata_path
, "%s" G_DIR_SEPARATOR_S
"%s", path
, CTF_FS_METADATA_FILENAME
);
910 if (g_file_test(metadata_path
->str
, G_FILE_TEST_IS_REGULAR
)) {
916 g_string_free(metadata_path
, TRUE
);
920 /* Helper for ctf_fs_component_create_ctf_fs_trace, to handle a single path. */
922 static int ctf_fs_component_create_ctf_fs_trace_one_path(struct ctf_fs_component
*ctf_fs
,
923 const char *path_param
,
924 const char *trace_name
,
925 std::vector
<ctf_fs_trace::UP
>& traces
,
926 bt_self_component
*selfComp
)
928 ctf_fs_trace::UP ctf_fs_trace
;
931 const bt2_common::LogCfg
& logCfg
= ctf_fs
->logCfg
;
933 norm_path
= bt_common_normalize_path(path_param
, NULL
);
935 BT_CLOGE_APPEND_CAUSE("Failed to normalize path: `%s`.", path_param
);
939 ret
= path_is_ctf_trace(norm_path
->str
);
941 BT_CLOGE_APPEND_CAUSE("Failed to check if path is a CTF trace: path=%s", norm_path
->str
);
943 } else if (ret
== 0) {
944 BT_CLOGE_APPEND_CAUSE("Path is not a CTF trace (does not contain a metadata file): `%s`.",
949 // FIXME: Remove or ifdef for __MINGW32__
950 if (strcmp(norm_path
->str
, "/") == 0) {
951 BT_CLOGE_APPEND_CAUSE("Opening a trace in `/` is not supported.");
957 ctf_fs_trace_create(norm_path
->str
, trace_name
, ctf_fs
->clkClsCfg
, selfComp
, logCfg
);
959 BT_CLOGE_APPEND_CAUSE("Cannot create trace for `%s`.", norm_path
->str
);
963 traces
.emplace_back(std::move(ctf_fs_trace
));
973 g_string_free(norm_path
, TRUE
);
980 * Count the number of stream and event classes defined by this trace's metadata.
982 * This is used to determine which metadata is the "latest", out of multiple
983 * traces sharing the same UUID. It is assumed that amongst all these metadatas,
984 * a bigger metadata is a superset of a smaller metadata. Therefore, it is
985 * enough to just count the classes.
988 static unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace
*trace
)
990 unsigned int num
= trace
->metadata
->tc
->stream_classes
->len
;
993 for (i
= 0; i
< trace
->metadata
->tc
->stream_classes
->len
; i
++) {
994 struct ctf_stream_class
*sc
=
995 (struct ctf_stream_class
*) trace
->metadata
->tc
->stream_classes
->pdata
[i
];
996 num
+= sc
->event_classes
->len
;
1003 * Merge the src ds_file_group into dest. This consists of merging their
1004 * ds_file_infos, making sure to keep the result sorted.
1007 static void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group
*dest
,
1008 ctf_fs_ds_file_group::UP src
)
1010 for (ctf_fs_ds_file_info::UP
& ds_file_info
: src
->ds_file_infos
) {
1011 ds_file_group_insert_ds_file_info_sorted(dest
, std::move(ds_file_info
));
1014 /* Merge both indexes. */
1015 merge_ctf_fs_ds_indexes(dest
->index
, src
->index
);
1018 /* Merge src_trace's data stream file groups into dest_trace's. */
1020 static int merge_matching_ctf_fs_ds_file_groups(struct ctf_fs_trace
*dest_trace
,
1021 ctf_fs_trace::UP src_trace
)
1023 std::vector
<ctf_fs_ds_file_group::UP
>& dest
= dest_trace
->ds_file_groups
;
1024 std::vector
<ctf_fs_ds_file_group::UP
>& src
= src_trace
->ds_file_groups
;
1028 * Save the initial length of dest: we only want to check against the
1029 * original elements in the inner loop.
1031 size_t dest_len
= dest
.size();
1033 for (ctf_fs_ds_file_group::UP
& src_group
: src
) {
1034 struct ctf_fs_ds_file_group
*dest_group
= NULL
;
1036 /* A stream instance without ID can't match a stream in the other trace. */
1037 if (src_group
->stream_id
!= -1) {
1038 /* Let's search for a matching ds_file_group in the destination. */
1039 for (size_t d_i
= 0; d_i
< dest_len
; ++d_i
) {
1040 ctf_fs_ds_file_group
*candidate_dest
= dest
[d_i
].get();
1042 /* Can't match a stream instance without ID. */
1043 if (candidate_dest
->stream_id
== -1) {
1048 * If the two groups have the same stream instance id
1049 * and belong to the same stream class (stream instance
1050 * ids are per-stream class), they represent the same
1053 if (candidate_dest
->stream_id
!= src_group
->stream_id
||
1054 candidate_dest
->sc
->id
!= src_group
->sc
->id
) {
1058 dest_group
= candidate_dest
;
1064 * Didn't find a friend in dest to merge our src_group into?
1065 * Create a new empty one. This can happen if a stream was
1066 * active in the source trace chunk but not in the destination
1070 struct ctf_stream_class
*sc
;
1071 struct ctf_fs_ds_index
*index
;
1073 sc
= ctf_trace_class_borrow_stream_class_by_id(dest_trace
->metadata
->tc
,
1077 index
= ctf_fs_ds_index_create(dest_trace
->logCfg
).release();
1083 ctf_fs_ds_file_group::UP new_dest_group
=
1084 ctf_fs_ds_file_group_create(dest_trace
, sc
, src_group
->stream_id
, index
);
1085 /* Ownership of index is transferred. */
1087 if (!new_dest_group
) {
1092 dest_group
= new_dest_group
.get();
1093 dest_trace
->ds_file_groups
.emplace_back(std::move(new_dest_group
));
1096 BT_ASSERT(dest_group
);
1097 merge_ctf_fs_ds_file_groups(dest_group
, std::move(src_group
));
1105 * Collapse the given traces, which must all share the same UUID, in a single
1108 * The trace with the most expansive metadata is chosen and all other traces
1109 * are merged into that one. On return, the elements of `traces` are nullptr
1110 * and the merged trace is placed in `out_trace`.
1113 static int merge_ctf_fs_traces(std::vector
<ctf_fs_trace::UP
> traces
, ctf_fs_trace::UP
& out_trace
)
1115 unsigned int winner_count
;
1116 struct ctf_fs_trace
*winner
;
1120 BT_ASSERT(traces
.size() >= 2);
1122 winner_count
= metadata_count_stream_and_event_classes(traces
[0].get());
1123 winner
= traces
[0].get();
1126 /* Find the trace with the largest metadata. */
1127 for (i
= 1; i
< traces
.size(); i
++) {
1128 ctf_fs_trace
*candidate
= traces
[i
].get();
1129 unsigned int candidate_count
;
1131 /* A bit of sanity check. */
1132 BT_ASSERT(bt_uuid_compare(winner
->metadata
->tc
->uuid
, candidate
->metadata
->tc
->uuid
) == 0);
1134 candidate_count
= metadata_count_stream_and_event_classes(candidate
);
1136 if (candidate_count
> winner_count
) {
1137 winner_count
= candidate_count
;
1143 /* Merge all the other traces in the winning trace. */
1144 for (ctf_fs_trace::UP
& trace
: traces
) {
1145 /* Don't merge the winner into itself. */
1146 if (trace
.get() == winner
) {
1150 /* Merge trace's data stream file groups into winner's. */
1151 ret
= merge_matching_ctf_fs_ds_file_groups(winner
, std::move(trace
));
1158 * Move the winner out of the array, into `*out_trace`.
1160 out_trace
= std::move(traces
[winner_i
]);
1172 static int decode_clock_snapshot_after_event(struct ctf_fs_trace
*ctf_fs_trace
,
1173 struct ctf_clock_class
*default_cc
,
1174 struct ctf_fs_ds_index_entry
*index_entry
,
1175 enum target_event target_event
, uint64_t *cs
,
1178 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
1179 struct ctf_fs_ds_file
*ds_file
= NULL
;
1180 struct ctf_msg_iter
*msg_iter
= NULL
;
1181 const bt2_common::LogCfg
& logCfg
= ctf_fs_trace
->logCfg
;
1184 BT_ASSERT(ctf_fs_trace
);
1185 BT_ASSERT(index_entry
);
1186 BT_ASSERT(index_entry
->path
);
1188 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, NULL
, index_entry
->path
, logCfg
);
1190 BT_CLOGE_APPEND_CAUSE("Failed to create a ctf_fs_ds_file");
1195 BT_ASSERT(ctf_fs_trace
->metadata
);
1196 BT_ASSERT(ctf_fs_trace
->metadata
->tc
);
1198 msg_iter
= ctf_msg_iter_create(ctf_fs_trace
->metadata
->tc
,
1199 bt_common_get_page_size(logCfg
.logLevel()) * 8,
1200 ctf_fs_ds_file_medops
, ds_file
, NULL
, logCfg
);
1202 /* ctf_msg_iter_create() logs errors. */
1208 * Turn on dry run mode to prevent the creation and usage of Babeltrace
1209 * library objects (bt_field, bt_message_*, etc.).
1211 ctf_msg_iter_set_dry_run(msg_iter
, true);
1213 /* Seek to the beginning of the target packet. */
1214 iter_status
= ctf_msg_iter_seek(msg_iter
, index_entry
->offset
.bytes());
1216 /* ctf_msg_iter_seek() logs errors. */
1221 switch (target_event
) {
1224 * Start to decode the packet until we reach the end of
1225 * the first event. To extract the first event's clock
1228 iter_status
= ctf_msg_iter_curr_packet_first_event_clock_snapshot(msg_iter
, cs
);
1231 /* Decode the packet to extract the last event's clock snapshot. */
1232 iter_status
= ctf_msg_iter_curr_packet_last_event_clock_snapshot(msg_iter
, cs
);
1242 /* Convert clock snapshot to timestamp. */
1243 ret
= bt_util_clock_cycles_to_ns_from_origin(
1244 *cs
, default_cc
->frequency
, default_cc
->offset_seconds
, default_cc
->offset_cycles
, ts_ns
);
1246 BT_CLOGE_APPEND_CAUSE("Failed to convert clock snapshot to timestamp");
1252 ctf_fs_ds_file_destroy(ds_file
);
1255 ctf_msg_iter_destroy(msg_iter
);
1261 static int decode_packet_first_event_timestamp(struct ctf_fs_trace
*ctf_fs_trace
,
1262 struct ctf_clock_class
*default_cc
,
1263 struct ctf_fs_ds_index_entry
*index_entry
,
1264 uint64_t *cs
, int64_t *ts_ns
)
1266 return decode_clock_snapshot_after_event(ctf_fs_trace
, default_cc
, index_entry
, FIRST_EVENT
, cs
,
1270 static int decode_packet_last_event_timestamp(struct ctf_fs_trace
*ctf_fs_trace
,
1271 struct ctf_clock_class
*default_cc
,
1272 struct ctf_fs_ds_index_entry
*index_entry
,
1273 uint64_t *cs
, int64_t *ts_ns
)
1275 return decode_clock_snapshot_after_event(ctf_fs_trace
, default_cc
, index_entry
, LAST_EVENT
, cs
,
1280 * Fix up packet index entries for lttng's "event-after-packet" bug.
1281 * Some buggy lttng tracer versions may emit events with a timestamp that is
1282 * larger (after) than the timestamp_end of the their packets.
1284 * To fix up this erroneous data we do the following:
1285 * 1. If it's not the stream file's last packet: set the packet index entry's
1286 * end time to the next packet's beginning time.
1287 * 2. If it's the stream file's last packet, set the packet index entry's end
1288 * time to the packet's last event's time, if any, or to the packet's
1289 * beginning time otherwise.
1291 * Known buggy tracer versions:
1292 * - before lttng-ust 2.11.0
1293 * - before lttng-module 2.11.0
1294 * - before lttng-module 2.10.10
1295 * - before lttng-module 2.9.13
1297 static int fix_index_lttng_event_after_packet_bug(struct ctf_fs_trace
*trace
)
1300 const bt2_common::LogCfg
& logCfg
= trace
->logCfg
;
1302 for (ctf_fs_ds_file_group::UP
& ds_file_group
: trace
->ds_file_groups
) {
1304 struct ctf_clock_class
*default_cc
;
1305 struct ctf_fs_ds_index_entry
*last_entry
;
1306 struct ctf_fs_ds_index
*index
;
1308 BT_ASSERT(ds_file_group
);
1309 index
= ds_file_group
->index
;
1312 BT_ASSERT(index
->entries
);
1313 BT_ASSERT(index
->entries
->len
> 0);
1316 * Iterate over all entries but the last one. The last one is
1317 * fixed differently after.
1319 for (entry_i
= 0; entry_i
< index
->entries
->len
- 1; entry_i
++) {
1320 struct ctf_fs_ds_index_entry
*curr_entry
, *next_entry
;
1322 curr_entry
= (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, entry_i
);
1323 next_entry
= (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, entry_i
+ 1);
1326 * 1. Set the current index entry `end` timestamp to
1327 * the next index entry `begin` timestamp.
1329 curr_entry
->timestamp_end
= next_entry
->timestamp_begin
;
1330 curr_entry
->timestamp_end_ns
= next_entry
->timestamp_begin_ns
;
1334 * 2. Fix the last entry by decoding the last event of the last
1338 (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, index
->entries
->len
- 1);
1339 BT_ASSERT(last_entry
);
1341 BT_ASSERT(ds_file_group
->sc
->default_clock_class
);
1342 default_cc
= ds_file_group
->sc
->default_clock_class
;
1345 * Decode packet to read the timestamp of the last event of the
1348 ret
= decode_packet_last_event_timestamp(trace
, default_cc
, last_entry
,
1349 &last_entry
->timestamp_end
,
1350 &last_entry
->timestamp_end_ns
);
1352 BT_CLOGE_APPEND_CAUSE(
1353 "Failed to decode stream's last packet to get its last event's clock snapshot.");
1363 * Fix up packet index entries for barectf's "event-before-packet" bug.
1364 * Some buggy barectf tracer versions may emit events with a timestamp that is
1365 * less than the timestamp_begin of the their packets.
1367 * To fix up this erroneous data we do the following:
1368 * 1. Starting at the second index entry, set the timestamp_begin of the
1369 * current entry to the timestamp of the first event of the packet.
1370 * 2. Set the previous entry's timestamp_end to the timestamp_begin of the
1373 * Known buggy tracer versions:
1374 * - before barectf 2.3.1
1376 static int fix_index_barectf_event_before_packet_bug(struct ctf_fs_trace
*trace
)
1379 const bt2_common::LogCfg
& logCfg
= trace
->logCfg
;
1381 for (ctf_fs_ds_file_group::UP
& ds_file_group
: trace
->ds_file_groups
) {
1383 struct ctf_clock_class
*default_cc
;
1384 struct ctf_fs_ds_index
*index
= ds_file_group
->index
;
1387 BT_ASSERT(index
->entries
);
1388 BT_ASSERT(index
->entries
->len
> 0);
1390 BT_ASSERT(ds_file_group
->sc
->default_clock_class
);
1391 default_cc
= ds_file_group
->sc
->default_clock_class
;
1394 * 1. Iterate over the index, starting from the second entry
1397 for (entry_i
= 1; entry_i
< index
->entries
->len
; entry_i
++) {
1398 ctf_fs_ds_index_entry
*prev_entry
=
1399 (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, entry_i
- 1);
1400 ctf_fs_ds_index_entry
*curr_entry
=
1401 (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, entry_i
);
1403 * 2. Set the current entry `begin` timestamp to the
1404 * timestamp of the first event of the current packet.
1406 ret
= decode_packet_first_event_timestamp(trace
, default_cc
, curr_entry
,
1407 &curr_entry
->timestamp_begin
,
1408 &curr_entry
->timestamp_begin_ns
);
1410 BT_CLOGE_APPEND_CAUSE("Failed to decode first event's clock snapshot");
1415 * 3. Set the previous entry `end` timestamp to the
1416 * timestamp of the first event of the current packet.
1418 prev_entry
->timestamp_end
= curr_entry
->timestamp_begin
;
1419 prev_entry
->timestamp_end_ns
= curr_entry
->timestamp_begin_ns
;
1427 * When using the lttng-crash feature it's likely that the last packets of each
1428 * stream have their timestamp_end set to zero. This is caused by the fact that
1429 * the tracer crashed and was not able to properly close the packets.
1431 * To fix up this erroneous data we do the following:
1432 * For each index entry, if the entry's timestamp_end is 0 and the
1433 * timestamp_begin is not 0:
1434 * - If it's the stream file's last packet: set the packet index entry's end
1435 * time to the packet's last event's time, if any, or to the packet's
1436 * beginning time otherwise.
1437 * - If it's not the stream file's last packet: set the packet index
1438 * entry's end time to the next packet's beginning time.
1440 * Affected versions:
1441 * - All current and future lttng-ust and lttng-modules versions.
1443 static int fix_index_lttng_crash_quirk(struct ctf_fs_trace
*trace
)
1446 const bt2_common::LogCfg
& logCfg
= trace
->logCfg
;
1448 for (ctf_fs_ds_file_group::UP
& ds_file_group
: trace
->ds_file_groups
) {
1450 struct ctf_clock_class
*default_cc
;
1451 struct ctf_fs_ds_index
*index
;
1453 BT_ASSERT(ds_file_group
);
1454 index
= ds_file_group
->index
;
1456 BT_ASSERT(ds_file_group
->sc
->default_clock_class
);
1457 default_cc
= ds_file_group
->sc
->default_clock_class
;
1460 BT_ASSERT(index
->entries
);
1461 BT_ASSERT(index
->entries
->len
> 0);
1463 ctf_fs_ds_index_entry
*last_entry
=
1464 (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, index
->entries
->len
- 1);
1465 BT_ASSERT(last_entry
);
1467 /* 1. Fix the last entry first. */
1468 if (last_entry
->timestamp_end
== 0 && last_entry
->timestamp_begin
!= 0) {
1470 * Decode packet to read the timestamp of the
1471 * last event of the stream file.
1473 ret
= decode_packet_last_event_timestamp(trace
, default_cc
, last_entry
,
1474 &last_entry
->timestamp_end
,
1475 &last_entry
->timestamp_end_ns
);
1477 BT_CLOGE_APPEND_CAUSE("Failed to decode last event's clock snapshot");
1482 /* Iterate over all entries but the last one. */
1483 for (entry_idx
= 0; entry_idx
< index
->entries
->len
- 1; entry_idx
++) {
1484 ctf_fs_ds_index_entry
*curr_entry
=
1485 (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, entry_idx
);
1486 ctf_fs_ds_index_entry
*next_entry
=
1487 (ctf_fs_ds_index_entry
*) g_ptr_array_index(index
->entries
, entry_idx
+ 1);
1489 if (curr_entry
->timestamp_end
== 0 && curr_entry
->timestamp_begin
!= 0) {
1491 * 2. Set the current index entry `end` timestamp to
1492 * the next index entry `begin` timestamp.
1494 curr_entry
->timestamp_end
= next_entry
->timestamp_begin
;
1495 curr_entry
->timestamp_end_ns
= next_entry
->timestamp_begin_ns
;
1505 * Extract the tracer information necessary to compare versions.
1506 * Returns 0 on success, and -1 if the extraction is not successful because the
1507 * necessary fields are absents in the trace metadata.
1509 static int extract_tracer_info(struct ctf_fs_trace
*trace
, struct tracer_info
*current_tracer_info
)
1512 struct ctf_trace_class_env_entry
*entry
;
1514 /* Clear the current_tracer_info struct */
1515 memset(current_tracer_info
, 0, sizeof(*current_tracer_info
));
1518 * To compare 2 tracer versions, at least the tracer name and it's
1519 * major version are needed. If one of these is missing, consider it an
1520 * extraction failure.
1522 entry
= ctf_trace_class_borrow_env_entry_by_name(trace
->metadata
->tc
, "tracer_name");
1523 if (!entry
|| entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
) {
1524 goto missing_bare_minimum
;
1527 /* Set tracer name. */
1528 current_tracer_info
->name
= entry
->value
.str
->str
;
1530 entry
= ctf_trace_class_borrow_env_entry_by_name(trace
->metadata
->tc
, "tracer_major");
1531 if (!entry
|| entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1532 goto missing_bare_minimum
;
1535 /* Set major version number. */
1536 current_tracer_info
->major
= entry
->value
.i
;
1538 entry
= ctf_trace_class_borrow_env_entry_by_name(trace
->metadata
->tc
, "tracer_minor");
1539 if (!entry
|| entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1543 /* Set minor version number. */
1544 current_tracer_info
->minor
= entry
->value
.i
;
1546 entry
= ctf_trace_class_borrow_env_entry_by_name(trace
->metadata
->tc
, "tracer_patch");
1549 * If `tracer_patch` doesn't exist `tracer_patchlevel` might.
1550 * For example, `lttng-modules` uses entry name
1551 * `tracer_patchlevel`.
1553 entry
= ctf_trace_class_borrow_env_entry_by_name(trace
->metadata
->tc
, "tracer_patchlevel");
1556 if (!entry
|| entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1560 /* Set patch version number. */
1561 current_tracer_info
->patch
= entry
->value
.i
;
1565 missing_bare_minimum
:
1571 static bool is_tracer_affected_by_lttng_event_after_packet_bug(struct tracer_info
*curr_tracer_info
)
1573 bool is_affected
= false;
1575 if (strcmp(curr_tracer_info
->name
, "lttng-ust") == 0) {
1576 if (curr_tracer_info
->major
< 2) {
1578 } else if (curr_tracer_info
->major
== 2) {
1579 /* fixed in lttng-ust 2.11.0 */
1580 if (curr_tracer_info
->minor
< 11) {
1584 } else if (strcmp(curr_tracer_info
->name
, "lttng-modules") == 0) {
1585 if (curr_tracer_info
->major
< 2) {
1587 } else if (curr_tracer_info
->major
== 2) {
1588 /* fixed in lttng-modules 2.11.0 */
1589 if (curr_tracer_info
->minor
== 10) {
1590 /* fixed in lttng-modules 2.10.10 */
1591 if (curr_tracer_info
->patch
< 10) {
1594 } else if (curr_tracer_info
->minor
== 9) {
1595 /* fixed in lttng-modules 2.9.13 */
1596 if (curr_tracer_info
->patch
< 13) {
1599 } else if (curr_tracer_info
->minor
< 9) {
1609 is_tracer_affected_by_barectf_event_before_packet_bug(struct tracer_info
*curr_tracer_info
)
1611 bool is_affected
= false;
1613 if (strcmp(curr_tracer_info
->name
, "barectf") == 0) {
1614 if (curr_tracer_info
->major
< 2) {
1616 } else if (curr_tracer_info
->major
== 2) {
1617 if (curr_tracer_info
->minor
< 3) {
1619 } else if (curr_tracer_info
->minor
== 3) {
1620 /* fixed in barectf 2.3.1 */
1621 if (curr_tracer_info
->patch
< 1) {
1631 static bool is_tracer_affected_by_lttng_crash_quirk(struct tracer_info
*curr_tracer_info
)
1633 bool is_affected
= false;
1635 /* All LTTng tracer may be affected by this lttng crash quirk. */
1636 if (strcmp(curr_tracer_info
->name
, "lttng-ust") == 0) {
1638 } else if (strcmp(curr_tracer_info
->name
, "lttng-modules") == 0) {
1646 * Looks for trace produced by known buggy tracers and fix up the index
1649 static int fix_packet_index_tracer_bugs(struct ctf_fs_component
*ctf_fs
)
1652 struct tracer_info current_tracer_info
;
1653 const bt2_common::LogCfg
& logCfg
= ctf_fs
->logCfg
;
1655 ret
= extract_tracer_info(ctf_fs
->trace
.get(), ¤t_tracer_info
);
1658 * A trace may not have all the necessary environment
1659 * entries to do the tracer version comparison.
1660 * At least, the tracer name and major version number
1661 * are needed. Failing to extract these entries is not
1665 BT_LOGI_STR("Cannot extract tracer information necessary to compare with buggy versions.");
1670 /* Check if the trace may be affected by old tracer bugs. */
1671 if (is_tracer_affected_by_lttng_event_after_packet_bug(¤t_tracer_info
)) {
1672 BT_LOGI_STR("Trace may be affected by LTTng tracer packet timestamp bug. Fixing up.");
1673 ret
= fix_index_lttng_event_after_packet_bug(ctf_fs
->trace
.get());
1675 BT_CLOGE_APPEND_CAUSE("Failed to fix LTTng event-after-packet bug.");
1678 ctf_fs
->trace
->metadata
->tc
->quirks
.lttng_event_after_packet
= true;
1681 if (is_tracer_affected_by_barectf_event_before_packet_bug(¤t_tracer_info
)) {
1682 BT_LOGI_STR("Trace may be affected by barectf tracer packet timestamp bug. Fixing up.");
1683 ret
= fix_index_barectf_event_before_packet_bug(ctf_fs
->trace
.get());
1685 BT_CLOGE_APPEND_CAUSE("Failed to fix barectf event-before-packet bug.");
1688 ctf_fs
->trace
->metadata
->tc
->quirks
.barectf_event_before_packet
= true;
1691 if (is_tracer_affected_by_lttng_crash_quirk(¤t_tracer_info
)) {
1692 ret
= fix_index_lttng_crash_quirk(ctf_fs
->trace
.get());
1694 BT_CLOGE_APPEND_CAUSE("Failed to fix lttng-crash timestamp quirks.");
1697 ctf_fs
->trace
->metadata
->tc
->quirks
.lttng_crash
= true;
1704 static bool compare_ds_file_groups_by_first_path(const ctf_fs_ds_file_group::UP
& ds_file_group_a
,
1705 const ctf_fs_ds_file_group::UP
& ds_file_group_b
)
1707 BT_ASSERT(!ds_file_group_a
->ds_file_infos
.empty());
1708 BT_ASSERT(!ds_file_group_b
->ds_file_infos
.empty());
1710 const ctf_fs_ds_file_info
& first_ds_file_info_a
= *ds_file_group_a
->ds_file_infos
[0];
1711 const ctf_fs_ds_file_info
& first_ds_file_info_b
= *ds_file_group_b
->ds_file_infos
[0];
1713 return first_ds_file_info_a
.path
< first_ds_file_info_b
.path
;
1716 static gint
compare_strings(gconstpointer p_a
, gconstpointer p_b
)
1718 const char *a
= *((const char **) p_a
);
1719 const char *b
= *((const char **) p_b
);
1721 return strcmp(a
, b
);
1724 int ctf_fs_component_create_ctf_fs_trace(struct ctf_fs_component
*ctf_fs
,
1725 const bt_value
*paths_value
,
1726 const bt_value
*trace_name_value
,
1727 bt_self_component
*selfComp
)
1731 const bt2_common::LogCfg
& logCfg
= ctf_fs
->logCfg
;
1732 GPtrArray
*paths
= NULL
;
1733 std::vector
<ctf_fs_trace::UP
> traces
;
1734 const char *trace_name
;
1736 BT_ASSERT(bt_value_get_type(paths_value
) == BT_VALUE_TYPE_ARRAY
);
1737 BT_ASSERT(!bt_value_array_is_empty(paths_value
));
1739 paths
= g_ptr_array_new_with_free_func(g_free
);
1741 BT_CLOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
1745 trace_name
= trace_name_value
? bt_value_string_get(trace_name_value
) : NULL
;
1748 * Create a sorted array of the paths, to make the execution of this
1749 * component deterministic.
1751 for (i
= 0; i
< bt_value_array_get_length(paths_value
); i
++) {
1752 const bt_value
*path_value
= bt_value_array_borrow_element_by_index_const(paths_value
, i
);
1753 const char *input
= bt_value_string_get(path_value
);
1756 input_copy
= g_strdup(input
);
1758 BT_CLOGE_APPEND_CAUSE("Failed to copy a string.");
1762 g_ptr_array_add(paths
, input_copy
);
1765 g_ptr_array_sort(paths
, compare_strings
);
1767 /* Create a separate ctf_fs_trace object for each path. */
1768 for (i
= 0; i
< paths
->len
; i
++) {
1769 const char *path
= (const char *) g_ptr_array_index(paths
, i
);
1771 ret
= ctf_fs_component_create_ctf_fs_trace_one_path(ctf_fs
, path
, trace_name
, traces
,
1778 if (traces
.size() > 1) {
1779 ctf_fs_trace
*first_trace
= traces
[0].get();
1780 const uint8_t *first_trace_uuid
= first_trace
->metadata
->tc
->uuid
;
1783 * We have more than one trace, they must all share the same
1784 * UUID, verify that.
1786 for (i
= 0; i
< traces
.size(); i
++) {
1787 ctf_fs_trace
*this_trace
= traces
[i
].get();
1788 const uint8_t *this_trace_uuid
= this_trace
->metadata
->tc
->uuid
;
1790 if (!this_trace
->metadata
->tc
->is_uuid_set
) {
1791 BT_CLOGE_APPEND_CAUSE(
1792 "Multiple traces given, but a trace does not have a UUID: path=%s",
1793 this_trace
->path
->str
);
1797 if (bt_uuid_compare(first_trace_uuid
, this_trace_uuid
) != 0) {
1798 char first_trace_uuid_str
[BT_UUID_STR_LEN
+ 1];
1799 char this_trace_uuid_str
[BT_UUID_STR_LEN
+ 1];
1801 bt_uuid_to_str(first_trace_uuid
, first_trace_uuid_str
);
1802 bt_uuid_to_str(this_trace_uuid
, this_trace_uuid_str
);
1804 BT_CLOGE_APPEND_CAUSE("Multiple traces given, but UUIDs don't match: "
1805 "first-trace-uuid=%s, first-trace-path=%s, "
1806 "trace-uuid=%s, trace-path=%s",
1807 first_trace_uuid_str
, first_trace
->path
->str
,
1808 this_trace_uuid_str
, this_trace
->path
->str
);
1813 ret
= merge_ctf_fs_traces(std::move(traces
), ctf_fs
->trace
);
1815 BT_CLOGE_APPEND_CAUSE("Failed to merge traces with the same UUID.");
1819 /* Just one trace, it may or may not have a UUID, both are fine. */
1820 ctf_fs
->trace
= std::move(traces
[0]);
1823 ret
= fix_packet_index_tracer_bugs(ctf_fs
);
1825 BT_CLOGE_APPEND_CAUSE("Failed to fix packet index tracer bugs.");
1829 * Sort data stream file groups by first data stream file info
1830 * path to get a deterministic order. This order influences the
1831 * order of the output ports. It also influences the order of
1832 * the automatic stream IDs if the trace's packet headers do not
1833 * contain a `stream_instance_id` field, in which case the data
1834 * stream file to stream ID association is always the same,
1835 * whatever the build and the system.
1837 * Having a deterministic order here can help debugging and
1840 std::sort(ctf_fs
->trace
->ds_file_groups
.begin(), ctf_fs
->trace
->ds_file_groups
.end(),
1841 compare_ds_file_groups_by_first_path
);
1848 g_ptr_array_free(paths
, TRUE
);
1854 static GString
*get_stream_instance_unique_name(struct ctf_fs_ds_file_group
*ds_file_group
)
1857 struct ctf_fs_ds_file_info
*ds_file_info
;
1859 name
= g_string_new(NULL
);
1865 * If there's more than one stream file in the stream file
1866 * group, the first (earliest) stream file's path is used as
1867 * the stream's unique name.
1869 BT_ASSERT(!ds_file_group
->ds_file_infos
.empty());
1870 ds_file_info
= ds_file_group
->ds_file_infos
[0].get();
1871 g_string_assign(name
, ds_file_info
->path
.c_str());
1877 /* Create the IR stream objects for ctf_fs_trace. */
1879 static int create_streams_for_trace(struct ctf_fs_trace
*ctf_fs_trace
)
1882 GString
*name
= NULL
;
1883 const bt2_common::LogCfg
& logCfg
= ctf_fs_trace
->logCfg
;
1885 for (ctf_fs_ds_file_group::UP
& ds_file_group
: ctf_fs_trace
->ds_file_groups
) {
1886 name
= get_stream_instance_unique_name(ds_file_group
.get());
1892 BT_ASSERT(ds_file_group
->sc
->ir_sc
);
1893 BT_ASSERT(ctf_fs_trace
->trace
);
1895 if (ds_file_group
->stream_id
== UINT64_C(-1)) {
1896 /* No stream ID: use 0 */
1897 ds_file_group
->stream
= bt_stream_create_with_id(
1898 ds_file_group
->sc
->ir_sc
, ctf_fs_trace
->trace
, ctf_fs_trace
->next_stream_id
);
1899 ctf_fs_trace
->next_stream_id
++;
1901 /* Specific stream ID */
1902 ds_file_group
->stream
= bt_stream_create_with_id(
1903 ds_file_group
->sc
->ir_sc
, ctf_fs_trace
->trace
, (uint64_t) ds_file_group
->stream_id
);
1906 if (!ds_file_group
->stream
) {
1907 BT_CLOGE_APPEND_CAUSE("Cannot create stream for DS file group: "
1908 "addr=%p, stream-name=\"%s\"",
1909 ds_file_group
.get(), name
->str
);
1913 ret
= bt_stream_set_name(ds_file_group
->stream
, name
->str
);
1915 BT_CLOGE_APPEND_CAUSE("Cannot set stream's name: "
1916 "addr=%p, stream-name=\"%s\"",
1917 ds_file_group
->stream
, name
->str
);
1921 g_string_free(name
, TRUE
);
1934 g_string_free(name
, TRUE
);
1939 static const bt_param_validation_value_descr inputs_elem_descr
=
1940 bt_param_validation_value_descr::makeString();
1942 static bt_param_validation_map_value_entry_descr fs_params_entries_descr
[] = {
1943 {"inputs", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1944 bt_param_validation_value_descr::makeArray(1, BT_PARAM_VALIDATION_INFINITE
,
1945 inputs_elem_descr
)},
1946 {"trace-name", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1947 bt_param_validation_value_descr::makeString()},
1948 {"clock-class-offset-s", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1949 bt_param_validation_value_descr::makeSignedInteger()},
1950 {"clock-class-offset-ns", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1951 bt_param_validation_value_descr::makeSignedInteger()},
1952 {"force-clock-class-origin-unix-epoch", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1953 bt_param_validation_value_descr::makeBool()},
1954 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1956 bool read_src_fs_parameters(const bt_value
*params
, const bt_value
**inputs
,
1957 const bt_value
**trace_name
, struct ctf_fs_component
*ctf_fs
)
1960 const bt_value
*value
;
1961 enum bt_param_validation_status validate_value_status
;
1962 gchar
*error
= NULL
;
1963 const bt2_common::LogCfg
& logCfg
= ctf_fs
->logCfg
;
1965 validate_value_status
= bt_param_validation_validate(params
, fs_params_entries_descr
, &error
);
1966 if (validate_value_status
!= BT_PARAM_VALIDATION_STATUS_OK
) {
1967 BT_CLOGE_APPEND_CAUSE("%s", error
);
1972 /* inputs parameter */
1973 *inputs
= bt_value_map_borrow_entry_value_const(params
, "inputs");
1975 /* clock-class-offset-s parameter */
1976 value
= bt_value_map_borrow_entry_value_const(params
, "clock-class-offset-s");
1978 ctf_fs
->clkClsCfg
.offsetSec
= bt_value_integer_signed_get(value
);
1981 /* clock-class-offset-ns parameter */
1982 value
= bt_value_map_borrow_entry_value_const(params
, "clock-class-offset-ns");
1984 ctf_fs
->clkClsCfg
.offsetNanoSec
= bt_value_integer_signed_get(value
);
1987 /* force-clock-class-origin-unix-epoch parameter */
1988 value
= bt_value_map_borrow_entry_value_const(params
, "force-clock-class-origin-unix-epoch");
1990 ctf_fs
->clkClsCfg
.forceOriginIsUnixEpoch
= bt_value_bool_get(value
);
1993 /* trace-name parameter */
1994 *trace_name
= bt_value_map_borrow_entry_value_const(params
, "trace-name");
2003 static ctf_fs_component::UP
ctf_fs_create(const bt_value
*params
,
2004 bt_self_component_source
*self_comp_src
,
2005 const bt2_common::LogCfg
& logCfg
)
2007 const bt_value
*inputs_value
;
2008 const bt_value
*trace_name_value
;
2009 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2011 ctf_fs_component::UP ctf_fs
= ctf_fs_component_create(logCfg
);
2016 if (!read_src_fs_parameters(params
, &inputs_value
, &trace_name_value
, ctf_fs
.get())) {
2020 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs
.get(), inputs_value
, trace_name_value
,
2025 if (create_streams_for_trace(ctf_fs
->trace
.get())) {
2029 if (create_ports_for_trace(ctf_fs
.get(), ctf_fs
->trace
.get(), self_comp_src
)) {
2037 bt_component_class_initialize_method_status
2038 ctf_fs_init(bt_self_component_source
*self_comp_src
, bt_self_component_source_configuration
*config
,
2039 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
2041 bt_self_component
*selfComp
= bt_self_component_source_as_self_component(self_comp_src
);
2042 const bt_component
*comp
= bt_self_component_as_component(selfComp
);
2043 bt_logging_level logLevel
= bt_component_get_logging_level(comp
);
2044 bt2_common::LogCfg
logCfg(logLevel
, *selfComp
);
2047 ctf_fs_component::UP ctf_fs
= ctf_fs_create(params
, self_comp_src
, logCfg
);
2049 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2052 bt_self_component_set_data(selfComp
, ctf_fs
.release());
2054 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2055 } catch (const std::bad_alloc
&) {
2056 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2057 } catch (const bt2_common::Error
&) {
2058 BT_CLOGE_APPEND_CAUSE("Failed to initialize component");
2059 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2064 bt_component_class_query_method_status
ctf_fs_query(bt_self_component_class_source
*comp_class_src
,
2065 bt_private_query_executor
*priv_query_exec
,
2066 const char *object
, const bt_value
*params
,
2067 __attribute__((unused
)) void *method_data
,
2068 const bt_value
**result
)
2070 const bt_query_executor
*query_exec
=
2071 bt_private_query_executor_as_query_executor_const(priv_query_exec
);
2072 bt_logging_level log_level
= bt_query_executor_get_logging_level(query_exec
);
2073 bt_self_component_class
*comp_class
=
2074 bt_self_component_class_source_as_self_component_class(comp_class_src
);
2075 bt2_common::LogCfg
logCfg(log_level
, *comp_class
);
2078 bt2::ConstMapValue
paramsObj(params
);
2079 nonstd::optional
<bt2::Value::Shared
> resultObj
;
2081 if (strcmp(object
, "metadata-info") == 0) {
2082 resultObj
= metadata_info_query(paramsObj
, logCfg
);
2083 } else if (strcmp(object
, "babeltrace.trace-infos") == 0) {
2084 resultObj
= trace_infos_query(paramsObj
, logCfg
);
2085 } else if (!strcmp(object
, "babeltrace.support-info")) {
2086 resultObj
= support_info_query(paramsObj
, logCfg
);
2088 BT_LOGE("Unknown query object `%s`", object
);
2089 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2092 *result
= resultObj
->release().libObjPtr();
2094 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2095 } catch (const std::bad_alloc
&) {
2096 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2097 } catch (const bt2_common::Error
&) {
2098 BT_CLOGE_APPEND_CAUSE("Failed to execute query: object=%s", object
);
2099 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;