2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * BabelTrace - LTTng-live client Component
11 #ifndef BABELTRACE_PLUGINS_CTF_LTTNG_LIVE_LTTNG_LIVE_HPP
12 #define BABELTRACE_PLUGINS_CTF_LTTNG_LIVE_LTTNG_LIVE_HPP
17 #include <babeltrace2/babeltrace.h>
19 #include "cpp-common/bt2/message.hpp"
20 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
22 #include "../common/src/metadata/metadata-stream-parser-utils.hpp"
23 #include "../common/src/msg-iter.hpp"
24 #include "viewer-connection.hpp"
27 * bt_common_lttng_live_url_parts is defined in common code, and is also used
28 * by C code, so it can't be C++-ified yet. Use this separate deleter object
31 struct bt_common_lttng_live_url_parts_deleter
33 explicit bt_common_lttng_live_url_parts_deleter(bt_common_lttng_live_url_parts& obj) noexcept :
38 bt_common_lttng_live_url_parts_deleter(const bt_common_lttng_live_url_parts_deleter&) = delete;
39 bt_common_lttng_live_url_parts&
40 operator=(const bt_common_lttng_live_url_parts_deleter&) = delete;
42 ~bt_common_lttng_live_url_parts_deleter()
44 bt_common_destroy_lttng_live_url_parts(_mObj);
48 bt_common_lttng_live_url_parts *_mObj;
51 enum lttng_live_stream_state
53 /* This stream won't have data until some known time in the future. */
54 LTTNG_LIVE_STREAM_QUIESCENT,
56 * This stream won't have data until some known time in the future and
57 * the message iterator inactivity message was already sent downstream.
59 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
60 /* This stream has data ready to be consumed. */
61 LTTNG_LIVE_STREAM_ACTIVE_DATA,
63 * This stream has no data left to consume. We should asked the relay
66 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
67 /* This stream won't have anymore data, ever. */
68 LTTNG_LIVE_STREAM_EOF,
71 inline const char *format_as(const lttng_live_stream_state state) noexcept
74 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA:
75 return "ACTIVE_NO_DATA";
77 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA:
78 return "QUIESCENT_NO_DATA";
80 case LTTNG_LIVE_STREAM_QUIESCENT:
83 case LTTNG_LIVE_STREAM_ACTIVE_DATA:
86 case LTTNG_LIVE_STREAM_EOF:
93 /* Iterator over a live stream. */
94 struct lttng_live_stream_iterator
96 using UP = std::unique_ptr<lttng_live_stream_iterator>;
98 explicit lttng_live_stream_iterator(const bt2c::Logger& parentLogger) :
99 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/STREAM-ITER"}
103 ~lttng_live_stream_iterator();
107 bt2::Stream::Shared stream;
109 /* Weak reference. */
110 struct lttng_live_trace *trace = nullptr;
113 * Since only a single iterator per viewer connection, we have
114 * only a single message iterator per stream.
116 bt2s::optional<ctf::src::MsgIter> msg_iter;
118 uint64_t viewer_stream_id = 0;
124 } ctf_stream_class_id;
127 * Clock Snapshot value of the last message iterator inactivity message
134 } last_inactivity_ts;
137 * Clock Snapshot value of the current message iterator inactivity
138 * message we might want to send downstream.
140 uint64_t current_inactivity_ts = 0;
142 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
144 /* The current message produced by this live stream iterator. */
145 bt2::ConstMessage::Shared current_msg;
147 /* Timestamp in nanoseconds of the current message (current_msg). */
148 int64_t current_msg_ts_ns = 0;
152 bool has_stream_hung_up = false;
156 bt2c::DataLen offsetInRelay;
160 bt2s::optional<CurPktInfo> curPktInfo;
163 struct lttng_live_metadata
165 using UP = std::unique_ptr<lttng_live_metadata>;
167 explicit lttng_live_metadata(const bt2::SelfComponent selfComp,
168 const bt2c::Logger& parentLogger) :
169 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/METADATA"},
170 _mSelfComp {selfComp}
175 const ctf::src::TraceCls *traceCls() const
177 return _mMetadataStreamParser->traceCls();
180 const bt2s::optional<bt2c::Uuid>& metadataStreamUuid() const noexcept
182 return _mMetadataStreamParser->metadataStreamUuid();
185 void parseSection(const bt2c::ConstBytes data)
187 if (!_mMetadataStreamParser) {
188 _mMetadataStreamParser =
189 ctf::src::createMetadataStreamParser(data, _mSelfComp, {}, logger);
192 _mMetadataStreamParser->parseSection(data);
195 bt2::SelfComponent selfComp() const noexcept
202 uint64_t stream_id = 0;
205 bt2::SelfComponent _mSelfComp;
206 ctf::src::MetadataStreamParser::UP _mMetadataStreamParser;
209 enum lttng_live_metadata_stream_state
212 * The metadata needs to be updated. This is either because we just
213 * created the trace and haven't asked yet, or the relay specifically
214 * told us that new metadata is available.
216 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
218 * The metadata was updated and the relay has not told us we need to
221 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
223 * The relay has closed this metadata stream. We set this in reaction
224 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
225 * command to the relay. If this field is set, we have received all the
226 * metadata that we are ever going to get for that metadata stream.
228 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
231 struct lttng_live_trace
233 using UP = std::unique_ptr<lttng_live_trace>;
235 explicit lttng_live_trace(const bt2c::Logger& parentLogger) :
236 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/TRACE"}
242 /* Back reference to session. */
243 struct lttng_live_session *session = nullptr;
245 /* ctf trace ID within the session. */
248 bt2::Trace::Shared trace;
250 lttng_live_metadata::UP metadata;
252 bt2::OptionalBorrowedObject<bt2::ConstClockClass> clock_class;
254 std::vector<lttng_live_stream_iterator::UP> stream_iterators;
256 enum lttng_live_metadata_stream_state metadata_stream_state =
257 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
260 struct lttng_live_session
262 using UP = std::unique_ptr<lttng_live_session>;
264 explicit lttng_live_session(const bt2c::Logger& parentLogger,
265 const bt2::SelfComponent selfCompParam) :
266 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/SESSION"},
267 selfComp {selfCompParam}
271 ~lttng_live_session();
275 bt2::SelfComponent selfComp;
277 /* Weak reference. */
278 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
280 std::string hostname;
282 std::string session_name;
286 std::vector<lttng_live_trace::UP> traces;
288 bool attached = false;
289 bool new_streams_needed = false;
290 bool lazy_stream_msg_init = false;
294 enum session_not_found_action
296 SESSION_NOT_FOUND_ACTION_CONTINUE,
297 SESSION_NOT_FOUND_ACTION_FAIL,
298 SESSION_NOT_FOUND_ACTION_END,
302 * A component instance is an iterator on a single session.
304 struct lttng_live_component
306 using UP = std::unique_ptr<lttng_live_component>;
308 explicit lttng_live_component(bt2c::Logger loggerParam,
309 const bt2::SelfComponent selfCompParam) noexcept :
310 logger {std::move(loggerParam)},
311 selfComp {selfCompParam}
317 bt2::SelfComponent selfComp;
322 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
325 size_t max_query_size = 0;
328 * Keeps track of whether the downstream component already has a
329 * message iterator on this component.
331 bool has_msg_iter = false;
334 struct lttng_live_msg_iter
336 using UP = std::unique_ptr<lttng_live_msg_iter>;
338 explicit lttng_live_msg_iter(const bt2c::Logger& parentLogger,
339 const bt2::SelfComponent selfCompParam,
340 const bt2::SelfMessageIterator selfMsgIter) :
341 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/MSG-ITER"},
342 selfComp {selfCompParam}, selfMsgIter {selfMsgIter}
346 ~lttng_live_msg_iter();
350 bt2::SelfComponent selfComp;
352 /* Weak reference. */
353 struct lttng_live_component *lttng_live_comp = nullptr;
355 bt2::SelfMessageIterator selfMsgIter;
357 live_viewer_connection::UP viewer_connection;
359 std::vector<lttng_live_session::UP> sessions;
361 /* Number of live stream iterator this message iterator has.*/
362 uint64_t active_stream_iter = 0;
364 /* Timestamp in nanosecond of the last message sent downstream. */
365 int64_t last_msg_ts_ns = 0;
367 /* True if the iterator was interrupted. */
368 bool was_interrupted = false;
371 enum lttng_live_iterator_status
373 /** Iterator state has progressed. Continue iteration immediately. */
374 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
375 /** No message available for now. Try again later. */
376 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
377 /** No more CTF_LTTNG_LIVEs to be delivered. */
378 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
379 /** No error, okay. */
380 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
381 /** Invalid arguments. */
382 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
383 /** General error. */
384 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
385 /** Out of memory. */
386 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
387 /** Unsupported iterator feature. */
388 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
391 inline const char *format_as(const lttng_live_iterator_status status) noexcept
394 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE:
395 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
397 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
398 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
400 case LTTNG_LIVE_ITERATOR_STATUS_END:
401 return "LTTNG_LIVE_ITERATOR_STATUS_END";
403 case LTTNG_LIVE_ITERATOR_STATUS_OK:
404 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
406 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
407 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
409 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
410 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
412 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
413 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
415 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
416 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
422 bt_component_class_initialize_method_status
423 lttng_live_component_init(bt_self_component_source *self_comp,
424 bt_self_component_source_configuration *config, const bt_value *params,
425 void *init_method_data);
427 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
428 bt_private_query_executor *priv_query_exec,
429 const char *object, const bt_value *params,
430 void *method_data, const bt_value **result);
432 void lttng_live_component_finalize(bt_self_component_source *component);
434 bt_message_iterator_class_next_method_status
435 lttng_live_msg_iter_next(bt_self_message_iterator *self_msg_it, bt_message_array_const msgs,
436 uint64_t capacity, uint64_t *count);
438 bt_message_iterator_class_initialize_method_status
439 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
440 bt_self_message_iterator_configuration *config,
441 bt_self_component_port_output *self_port);
443 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
445 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session);
447 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
449 enum lttng_live_iterator_status
450 lttng_live_session_get_new_streams(struct lttng_live_session *session);
452 struct lttng_live_trace *
453 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
456 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
457 const char *hostname, const char *session_name);
460 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
461 * If new metadata is received, the function writes it to the provided file
462 * handle and updates the reply_len output parameter. This function should be
463 * called in loop until _END status is received to ensure all metadata is
464 * written to the file.
466 enum lttng_live_get_one_metadata_status
467 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<uint8_t>& buf);
469 enum lttng_live_iterator_status
470 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
471 struct lttng_live_stream_iterator *stream, struct packet_index *index);
473 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
475 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
476 enum lttng_live_stream_state new_state);
478 void lttng_live_stream_iterator_set_stream_class(lttng_live_stream_iterator *streamIter,
479 uint64_t ctfStreamClsId);
481 #endif /* BABELTRACE_PLUGINS_CTF_LTTNG_LIVE_LTTNG_LIVE_HPP */