From: Simon Marchi Date: Mon, 9 Sep 2024 17:27:42 +0000 (-0400) Subject: ctf: use self component / message iterator C++ wrappers throughout X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=4d6634b83c864b6f2d40619cfe6dedb1c55701c7;p=babeltrace.git ctf: use self component / message iterator C++ wrappers throughout Use `bt2::SelfComponent`, `bt2::SelfSourceComponent`, `bt2::SelfSinkComponent` and `bt2::SelfMessageIterator` for function parameters and fields throughout `src/plugins/ctf` instead of raw pointers. Don't try to adapt the code too much to use the wrappers yet. Change-Id: I2a3ae5849ef44fd639486185e5718919a152be93 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/12416 Reviewed-by: Philippe Proulx Tested-by: jenkins --- diff --git a/src/plugins/ctf/fs-sink/fs-sink.cpp b/src/plugins/ctf/fs-sink/fs-sink.cpp index 6b0c88da..c580b39f 100644 --- a/src/plugins/ctf/fs-sink/fs-sink.cpp +++ b/src/plugins/ctf/fs-sink/fs-sink.cpp @@ -10,6 +10,7 @@ #include #include "common/assert.h" +#include "cpp-common/bt2/wrap.hpp" #include "cpp-common/vendor/fmt/format.h" #include "ctfser/ctfser.h" @@ -136,9 +137,8 @@ bt_component_class_initialize_method_status ctf_fs_sink_init(bt_self_component_s bt_component_class_initialize_method_status status; bt_self_component_add_port_status add_port_status; struct fs_sink_comp *fs_sink = NULL; - bt_self_component *self_comp = bt_self_component_sink_as_self_component(self_comp_sink); - fs_sink = new fs_sink_comp {bt2::SelfSinkComponent {self_comp_sink}}; + fs_sink = new fs_sink_comp {bt2::wrap(self_comp_sink)}; fs_sink->output_dir_path = g_string_new(NULL); status = configure_component(fs_sink, params); if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { @@ -177,7 +177,8 @@ bt_component_class_initialize_method_status ctf_fs_sink_init(bt_self_component_s goto end; } - bt_self_component_set_data(self_comp, fs_sink); + bt_self_component_set_data(bt_self_component_sink_as_self_component(self_comp_sink), + fs_sink); end: if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { diff --git a/src/plugins/ctf/fs-src/fs.cpp b/src/plugins/ctf/fs-src/fs.cpp index 97e1f34e..0b157281 100644 --- a/src/plugins/ctf/fs-src/fs.cpp +++ b/src/plugins/ctf/fs-src/fs.cpp @@ -121,10 +121,10 @@ static void instantiateMsgIter(ctf_fs_msg_iter_data *msg_iter_data) ctf_fs_ds_file_group *ds_file_group = msg_iter_data->port_data->ds_file_group; Medium::UP medium = bt2s::make_unique(ds_file_group->index, msg_iter_data->logger); - msg_iter_data->msgIter.emplace( - bt2::wrap(msg_iter_data->self_msg_iter), *ds_file_group->ctf_fs_trace->cls(), - ds_file_group->ctf_fs_trace->metadataStreamUuid(), *ds_file_group->stream, - std::move(medium), msg_iter_data->port_data->ctf_fs->quirks, msg_iter_data->logger); + msg_iter_data->msgIter.emplace(msg_iter_data->selfMsgIter, *ds_file_group->ctf_fs_trace->cls(), + ds_file_group->ctf_fs_trace->metadataStreamUuid(), + *ds_file_group->stream, std::move(medium), + msg_iter_data->port_data->ctf_fs->quirks, msg_iter_data->logger); } bt_message_iterator_class_seek_beginning_method_status @@ -162,7 +162,7 @@ ctf_fs_iterator_init(bt_self_message_iterator *self_msg_iter, bt_self_component_port_output_as_self_component_port(self_port)); BT_ASSERT(port_data); - auto msg_iter_data = bt2s::make_unique(self_msg_iter); + auto msg_iter_data = bt2s::make_unique(bt2::wrap(self_msg_iter)); msg_iter_data->port_data = port_data; instantiateMsgIter(msg_iter_data.get()); @@ -235,7 +235,7 @@ std::string ctf_fs_make_port_name(ctf_fs_ds_file_group *ds_file_group) static int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, struct ctf_fs_ds_file_group *ds_file_group, - bt_self_component_source *self_comp_src) + const bt2::SelfSourceComponent selfSrcComp) { const auto port_name = ctf_fs_make_port_name(ds_file_group); auto port_data = bt2s::make_unique(); @@ -245,7 +245,7 @@ static int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, port_data->ctf_fs = ctf_fs; port_data->ds_file_group = ds_file_group; - int ret = bt_self_component_source_add_output_port(self_comp_src, port_name.c_str(), + int ret = bt_self_component_source_add_output_port(selfSrcComp.libObjPtr(), port_name.c_str(), port_data.get(), NULL); if (ret) { return ret; @@ -257,11 +257,11 @@ static int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, static int create_ports_for_trace(struct ctf_fs_component *ctf_fs, struct ctf_fs_trace *ctf_fs_trace, - bt_self_component_source *self_comp_src) + const bt2::SelfSourceComponent selfSrcComp) { /* Create one output port for each stream file group */ for (const auto& ds_file_group : ctf_fs_trace->ds_file_groups) { - int ret = create_one_port_for_trace(ctf_fs, ds_file_group.get(), self_comp_src); + int ret = create_one_port_for_trace(ctf_fs, ds_file_group.get(), selfSrcComp); if (ret) { BT_CPPLOGE_APPEND_CAUSE_SPEC(ctf_fs->logger, "Cannot create output port."); return ret; @@ -490,9 +490,10 @@ static void set_trace_name(const bt2::Trace trace, const char *name_suffix) trace.name(name); } -static ctf_fs_trace::UP ctf_fs_trace_create(const char *path, const char *name, - const ctf::src::ClkClsCfg& clkClsCfg, - bt_self_component *selfComp, const bt2c::Logger& logger) +static ctf_fs_trace::UP +ctf_fs_trace_create(const char *path, const char *name, const ctf::src::ClkClsCfg& clkClsCfg, + const bt2::OptionalBorrowedObject selfComp, + const bt2c::Logger& logger) { auto ctf_fs_trace = bt2s::make_unique(clkClsCfg, selfComp, logger); const auto metadataPath = fmt::format("{}" G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME, path); @@ -505,9 +506,9 @@ static ctf_fs_trace::UP ctf_fs_trace_create(const char *path, const char *name, if (ctf_fs_trace->cls()->libCls()) { bt2::TraceClass traceCls = *ctf_fs_trace->cls()->libCls(); ctf_fs_trace->trace = traceCls.instantiate(); - ctf_trace_class_configure_ir_trace(*ctf_fs_trace->cls(), *ctf_fs_trace->trace, - bt_self_component_get_graph_mip_version(selfComp), - logger); + ctf_trace_class_configure_ir_trace( + *ctf_fs_trace->cls(), *ctf_fs_trace->trace, + bt_self_component_get_graph_mip_version(selfComp->libObjPtr()), logger); set_trace_name(*ctf_fs_trace->trace, name); } @@ -527,11 +528,10 @@ static int path_is_ctf_trace(const char *path) /* Helper for ctf_fs_component_create_ctf_fs_trace, to handle a single path. */ -static int ctf_fs_component_create_ctf_fs_trace_one_path(struct ctf_fs_component *ctf_fs, - const char *path_param, - const char *trace_name, - std::vector& traces, - bt_self_component *selfComp) +static int ctf_fs_component_create_ctf_fs_trace_one_path( + struct ctf_fs_component *ctf_fs, const char *path_param, const char *trace_name, + std::vector& traces, + const bt2::OptionalBorrowedObject selfComp) { bt2c::GStringUP norm_path {bt_common_normalize_path(path_param, NULL)}; if (!norm_path) { @@ -1267,9 +1267,9 @@ static bool compare_ds_file_groups_by_first_path(const ctf_fs_ds_file_group::UP& return first_ds_file_info_a.path < first_ds_file_info_b.path; } -int ctf_fs_component_create_ctf_fs_trace(struct ctf_fs_component *ctf_fs, - const bt2::ConstArrayValue pathsValue, - const char *traceName, bt_self_component *selfComp) +int ctf_fs_component_create_ctf_fs_trace( + struct ctf_fs_component *ctf_fs, const bt2::ConstArrayValue pathsValue, const char *traceName, + const bt2::OptionalBorrowedObject selfComp) { std::vector paths; @@ -1467,22 +1467,22 @@ ctf::src::fs::Parameters read_src_fs_parameters(const bt2::ConstMapValue params, } static ctf_fs_component::UP ctf_fs_create(const bt2::ConstMapValue params, - bt_self_component_source *self_comp_src) + const bt2::SelfSourceComponent selfSrcComp) { - bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src); - const bt2c::Logger logger {bt2::SelfSourceComponent {self_comp_src}, "PLUGIN/SRC.CTF.FS/COMP"}; + const bt2c::Logger logger {selfSrcComp, "PLUGIN/SRC.CTF.FS/COMP"}; const auto parameters = read_src_fs_parameters(params, logger); auto ctf_fs = bt2s::make_unique(parameters.clkClsCfg, logger); - if (ctf_fs_component_create_ctf_fs_trace( - ctf_fs.get(), parameters.inputs, - parameters.traceName ? parameters.traceName->c_str() : nullptr, self_comp)) { + if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), parameters.inputs, + parameters.traceName ? parameters.traceName->c_str() : + nullptr, + static_cast(selfSrcComp))) { return nullptr; } create_streams_for_trace(ctf_fs->trace.get()); - if (create_ports_for_trace(ctf_fs.get(), ctf_fs->trace.get(), self_comp_src)) { + if (create_ports_for_trace(ctf_fs.get(), ctf_fs->trace.get(), selfSrcComp)) { return nullptr; } @@ -1494,7 +1494,8 @@ bt_component_class_initialize_method_status ctf_fs_init(bt_self_component_source const bt_value *params, void *) { try { - ctf_fs_component::UP ctf_fs = ctf_fs_create(bt2::ConstMapValue {params}, self_comp_src); + ctf_fs_component::UP ctf_fs = + ctf_fs_create(bt2::ConstMapValue {params}, bt2::wrap(self_comp_src)); if (!ctf_fs) { return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; } diff --git a/src/plugins/ctf/fs-src/fs.hpp b/src/plugins/ctf/fs-src/fs.hpp index 37da1835..35ffe3e5 100644 --- a/src/plugins/ctf/fs-src/fs.hpp +++ b/src/plugins/ctf/fs-src/fs.hpp @@ -107,14 +107,12 @@ struct ctf_fs_msg_iter_data { using UP = std::unique_ptr; - explicit ctf_fs_msg_iter_data(bt_self_message_iterator *selfMsgIter) : - self_msg_iter {selfMsgIter}, - logger {bt2::SelfMessageIterator {self_msg_iter}, "PLUGIN/SRC.CTF.FS/MSG-ITER"} + explicit ctf_fs_msg_iter_data(const bt2::SelfMessageIterator selfMsgIterParam) : + selfMsgIter {selfMsgIterParam}, logger {selfMsgIter, "PLUGIN/SRC.CTF.FS/MSG-ITER"} { } - /* Weak */ - bt_self_message_iterator *self_msg_iter = nullptr; + bt2::SelfMessageIterator selfMsgIter; bt2c::Logger logger; @@ -165,14 +163,11 @@ ctf_fs_iterator_seek_beginning(bt_self_message_iterator *message_iterator); * `paths_value` must be an array of strings, * * The created `struct ctf_fs_trace` is assigned to `ctf_fs->trace`. - * - * `self_comp` and `self_comp_class` are used for logging, only one of them - * should be set. */ int ctf_fs_component_create_ctf_fs_trace(struct ctf_fs_component *ctf_fs, bt2::ConstArrayValue pathsValue, const char *traceName, - bt_self_component *selfComp); + bt2::OptionalBorrowedObject selfComp); namespace ctf { namespace src { diff --git a/src/plugins/ctf/fs-src/metadata.hpp b/src/plugins/ctf/fs-src/metadata.hpp index bab69e26..c1523a96 100644 --- a/src/plugins/ctf/fs-src/metadata.hpp +++ b/src/plugins/ctf/fs-src/metadata.hpp @@ -11,6 +11,9 @@ #include +#include "cpp-common/bt2/optional-borrowed-object.hpp" +#include "cpp-common/bt2/self-component-port.hpp" + namespace bt2c { class Logger; @@ -21,7 +24,8 @@ class Logger; #define CTF_FS_METADATA_FILENAME "metadata" -int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_trace *ctf_fs_trace, +int ctf_fs_metadata_set_trace_class(bt2::OptionalBorrowedObject selfComp, + struct ctf_fs_trace *ctf_fs_trace, const ctf::src::ClkClsCfg& clkClsCfg); FILE *ctf_fs_metadata_open_file(const char *trace_path, const bt2c::Logger& logger); diff --git a/src/plugins/ctf/fs-src/query.cpp b/src/plugins/ctf/fs-src/query.cpp index 20645320..ee90eb20 100644 --- a/src/plugins/ctf/fs-src/query.cpp +++ b/src/plugins/ctf/fs-src/query.cpp @@ -145,7 +145,7 @@ bt2::Value::Shared trace_infos_query(const bt2::ConstMapValue params, const bt2c if (ctf_fs_component_create_ctf_fs_trace( &ctf_fs, parameters.inputs, - parameters.traceName ? parameters.traceName->c_str() : nullptr, nullptr)) { + parameters.traceName ? parameters.traceName->c_str() : nullptr, {})) { BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "Failed to create trace"); } diff --git a/src/plugins/ctf/lttng-live/data-stream.cpp b/src/plugins/ctf/lttng-live/data-stream.cpp index b385df90..812628b4 100644 --- a/src/plugins/ctf/lttng-live/data-stream.cpp +++ b/src/plugins/ctf/lttng-live/data-stream.cpp @@ -13,7 +13,6 @@ #include "common/assert.h" #include "compat/mman.h" /* IWYU pragma: keep */ -#include "cpp-common/bt2/wrap.hpp" #include "cpp-common/bt2s/make-unique.hpp" #include "cpp-common/vendor/fmt/format.h" @@ -139,7 +138,7 @@ lttng_live_stream_iterator_create_msg_iter(lttng_live_stream_iterator *liveStrea liveStreamIter->stream->name(liveStreamIter->name); auto medium = bt2s::make_unique(*liveStreamIter); - liveStreamIter->msg_iter.emplace(bt2::wrap(liveMsgIter->self_msg_iter), *ctfTc, + liveStreamIter->msg_iter.emplace(liveMsgIter->selfMsgIter, *ctfTc, liveStreamIter->trace->metadata->metadataStreamUuid(), *liveStreamIter->stream, std::move(medium), ctf::src::MsgIterQuirks {}, liveStreamIter->logger); @@ -147,7 +146,7 @@ lttng_live_stream_iterator_create_msg_iter(lttng_live_stream_iterator *liveStrea } enum lttng_live_iterator_status lttng_live_lazy_msg_init(struct lttng_live_session *session, - bt_self_message_iterator *self_msg_iter) + const bt2::SelfMessageIterator selfMsgIter) { if (!session->lazy_stream_msg_init) { return LTTNG_LIVE_ITERATOR_STATUS_OK; @@ -156,7 +155,7 @@ enum lttng_live_iterator_status lttng_live_lazy_msg_init(struct lttng_live_sessi BT_CPPLOGD_SPEC(session->logger, "Lazily initializing self message iterator for live session: " "session-id={}, self-msg-iter-addr={}", - session->id, fmt::ptr(self_msg_iter)); + session->id, fmt::ptr(selfMsgIter.libObjPtr())); for (lttng_live_trace::UP& trace : session->traces) { for (lttng_live_stream_iterator::UP& stream_iter : trace->stream_iterators) { @@ -169,7 +168,7 @@ enum lttng_live_iterator_status lttng_live_lazy_msg_init(struct lttng_live_sessi "Creating CTF message iterator: session-id={}, ctf-tc-addr={}, " "stream-iter-name={}, self-msg-iter-addr={}", session->id, fmt::ptr(ctfTraceCls), stream_iter->name.c_str(), - fmt::ptr(self_msg_iter)); + fmt::ptr(selfMsgIter.libObjPtr())); } } diff --git a/src/plugins/ctf/lttng-live/data-stream.hpp b/src/plugins/ctf/lttng-live/data-stream.hpp index 702454e7..5370b867 100644 --- a/src/plugins/ctf/lttng-live/data-stream.hpp +++ b/src/plugins/ctf/lttng-live/data-stream.hpp @@ -12,7 +12,7 @@ #include "lttng-live.hpp" enum lttng_live_iterator_status lttng_live_lazy_msg_init(struct lttng_live_session *session, - bt_self_message_iterator *self_msg_iter); + bt2::SelfMessageIterator selfMsgIter); struct lttng_live_stream_iterator * lttng_live_stream_iterator_create(struct lttng_live_session *session, uint64_t ctf_trace_id, diff --git a/src/plugins/ctf/lttng-live/lttng-live.cpp b/src/plugins/ctf/lttng-live/lttng-live.cpp index 0f2d6b5f..efe51e32 100644 --- a/src/plugins/ctf/lttng-live/lttng-live.cpp +++ b/src/plugins/ctf/lttng-live/lttng-live.cpp @@ -62,7 +62,7 @@ bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter) return false; } - return bt_self_message_iterator_is_interrupted(msg_iter->self_msg_iter); + return bt_self_message_iterator_is_interrupted(msg_iter->selfMsgIter.libObjPtr()); } static struct lttng_live_trace * @@ -361,7 +361,7 @@ lttng_live_get_session(struct lttng_live_msg_iter *lttng_live_msg_iter, * Now that we have the metadata we can initialize the downstream * iterator. */ - return lttng_live_lazy_msg_init(session, lttng_live_msg_iter->self_msg_iter); + return lttng_live_lazy_msg_init(session, lttng_live_msg_iter->selfMsgIter); } static void @@ -472,7 +472,7 @@ emit_inactivity_message(struct lttng_live_msg_iter *lttng_live_msg_iter, timestamp); const auto msg = bt_message_message_iterator_inactivity_create( - lttng_live_msg_iter->self_msg_iter, stream_iter->trace->clock_class->libObjPtr(), + lttng_live_msg_iter->selfMsgIter.libObjPtr(), stream_iter->trace->clock_class->libObjPtr(), timestamp); if (!msg) { @@ -919,13 +919,13 @@ handle_late_message(struct lttng_live_msg_iter *lttng_live_msg_iter, const auto adjust_status = bt2c::call([&] { switch (late_msg.type()) { case bt2::MessageType::DiscardedEvents: - return adjust_discarded_events_message(lttng_live_msg_iter->self_msg_iter, + return adjust_discarded_events_message(lttng_live_msg_iter->selfMsgIter.libObjPtr(), *stream_iter->stream, late_msg.asDiscardedEvents(), adjustedMessage, stream_iter->last_inactivity_ts.value); case bt2::MessageType::DiscardedPackets: - return adjust_discarded_packets_message(lttng_live_msg_iter->self_msg_iter, + return adjust_discarded_packets_message(lttng_live_msg_iter->selfMsgIter.libObjPtr(), *stream_iter->stream, late_msg.asDiscardedPackets(), adjustedMessage, stream_iter->last_inactivity_ts.value); @@ -1468,13 +1468,12 @@ return_status: static lttng_live_msg_iter::UP lttng_live_msg_iter_create(struct lttng_live_component *lttng_live_comp, - bt_self_message_iterator *self_msg_it) + const bt2::SelfMessageIterator selfMsgIter) { - auto msg_iter = bt2s::make_unique(lttng_live_comp->logger, - lttng_live_comp->selfComp); + auto msg_iter = bt2s::make_unique( + lttng_live_comp->logger, lttng_live_comp->selfComp, selfMsgIter); msg_iter->lttng_live_comp = lttng_live_comp; - msg_iter->self_msg_iter = self_msg_it; msg_iter->active_stream_iter = 0; msg_iter->last_msg_ts_ns = INT64_MIN; msg_iter->was_interrupted = false; @@ -1497,7 +1496,7 @@ lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it, BT_ASSERT(!lttng_live->has_msg_iter); lttng_live->has_msg_iter = true; - auto lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, self_msg_it); + auto lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, bt2::wrap(self_msg_it)); if (!lttng_live_msg_iter) { BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live->logger, "Failed to create lttng_live_msg_iter"); @@ -1628,9 +1627,6 @@ static bt2::Value::Shared lttng_live_query_support_info(const bt2::ConstMapValue struct bt_common_lttng_live_url_parts parts = {}; bt_common_lttng_live_url_parts_deleter partsDeleter {parts}; - /* Used by the logging macros */ - __attribute__((unused)) bt_self_component *self_comp = NULL; - const auto typeValue = params["type"]; if (!typeValue) { BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, @@ -1795,7 +1791,6 @@ lttng_live_component_init(bt_self_component_source *self_comp_src, try { lttng_live_component::UP lttng_live; bt_component_class_initialize_method_status ret; - bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src); bt_self_component_add_port_status add_port_status; ret = lttng_live_component_create(params, self_comp_src, lttng_live); @@ -1810,7 +1805,8 @@ lttng_live_component_init(bt_self_component_source *self_comp_src, return ret; } - bt_self_component_set_data(self_comp, lttng_live.release()); + bt_self_component_set_data(bt_self_component_source_as_self_component(self_comp_src), + lttng_live.release()); return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; } catch (const std::bad_alloc&) { diff --git a/src/plugins/ctf/lttng-live/lttng-live.hpp b/src/plugins/ctf/lttng-live/lttng-live.hpp index 19df6353..384cc424 100644 --- a/src/plugins/ctf/lttng-live/lttng-live.hpp +++ b/src/plugins/ctf/lttng-live/lttng-live.hpp @@ -336,9 +336,10 @@ struct lttng_live_msg_iter using UP = std::unique_ptr; explicit lttng_live_msg_iter(const bt2c::Logger& parentLogger, - const bt2::SelfComponent selfCompParam) : + const bt2::SelfComponent selfCompParam, + const bt2::SelfMessageIterator selfMsgIter) : logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/MSG-ITER"}, - selfComp {selfCompParam} + selfComp {selfCompParam}, selfMsgIter {selfMsgIter} { } @@ -351,8 +352,7 @@ struct lttng_live_msg_iter /* Weak reference. */ struct lttng_live_component *lttng_live_comp = nullptr; - /* Weak reference. */ - bt_self_message_iterator *self_msg_iter = nullptr; + bt2::SelfMessageIterator selfMsgIter; live_viewer_connection::UP viewer_connection; @@ -432,7 +432,7 @@ bt_component_class_query_method_status lttng_live_query(bt_self_component_class_ void lttng_live_component_finalize(bt_self_component_source *component); bt_message_iterator_class_next_method_status -lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs, +lttng_live_msg_iter_next(bt_self_message_iterator *self_msg_it, bt_message_array_const msgs, uint64_t capacity, uint64_t *count); bt_message_iterator_class_initialize_method_status