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_PLUGIN_CTF_LTTNG_LIVE_H
12 #define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H
19 #include <babeltrace2/babeltrace.h>
21 #include "common/macros.h"
22 #include "cpp-common/bt2/message.hpp"
23 #include "../common/src/metadata/tsdl/decoder.hpp"
24 #include "../common/src/msg-iter/msg-iter.hpp"
25 #include "viewer-connection.hpp"
27 struct lttng_live_component;
28 struct lttng_live_session;
29 struct lttng_live_msg_iter;
32 * bt_common_lttng_live_url_parts is defined in common code, and is also used
33 * by C code, so it can't be C++-ified yet. Use this separate deleter object
36 struct bt_common_lttng_live_url_parts_deleter
38 bt_common_lttng_live_url_parts_deleter(bt_common_lttng_live_url_parts& obj) : _mObj {obj}
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 /* Iterator over a live stream. */
72 struct lttng_live_stream_iterator
74 using UP = std::unique_ptr<lttng_live_stream_iterator>;
76 explicit lttng_live_stream_iterator(const bt2_common::LogCfg& logCfgParam) noexcept :
81 ~lttng_live_stream_iterator();
83 const bt2_common::LogCfg logCfg;
86 nonstd::optional<bt2::Stream::Shared> stream;
89 struct lttng_live_trace *trace = nullptr;
92 * Since only a single iterator per viewer connection, we have
93 * only a single message iterator per stream.
95 ctf_msg_iter_up msg_iter;
97 uint64_t viewer_stream_id = 0;
103 } ctf_stream_class_id;
105 /* base offset in current index. */
106 uint64_t base_offset = 0;
107 /* len to read in current index. */
109 /* offset in current index. */
113 * Clock Snapshot value of the last message iterator inactivity message
120 } last_inactivity_ts;
123 * Clock Snapshot value of the current message iterator inactivity
124 * message we might want to send downstream.
126 uint64_t current_inactivity_ts = 0;
128 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
131 * The current message produced by this live stream iterator. Owned by
134 nonstd::optional<bt2::ConstMessage::Shared> current_msg;
136 /* Timestamp in nanoseconds of the current message (current_msg). */
137 int64_t current_msg_ts_ns = 0;
139 std::vector<uint8_t> buf;
143 bool has_stream_hung_up = false;
146 struct lttng_live_metadata
148 using UP = std::unique_ptr<lttng_live_metadata>;
150 explicit lttng_live_metadata(const bt2_common::LogCfg& logCfgParam) noexcept :
155 const bt2_common::LogCfg logCfg;
157 uint64_t stream_id = 0;
159 /* Weak reference. */
160 ctf_metadata_decoder_up decoder;
163 enum lttng_live_metadata_stream_state
166 * The metadata needs to be updated. This is either because we just
167 * created the trace and haven't asked yet, or the relay specifically
168 * told us that new metadata is available.
170 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
172 * The metadata was updated and the relay has not told us we need to
175 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
177 * The relay has closed this metadata stream. We set this in reaction
178 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
179 * command to the relay. If this field is set, we have received all the
180 * metadata that we are ever going to get for that metadata stream.
182 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
185 struct lttng_live_trace
187 using UP = std::unique_ptr<lttng_live_trace>;
189 explicit lttng_live_trace(const bt2_common::LogCfg& logCfgParam) noexcept : logCfg {logCfgParam}
193 const bt2_common::LogCfg logCfg;
195 /* Back reference to session. */
196 struct lttng_live_session *session = nullptr;
198 /* ctf trace ID within the session. */
202 nonstd::optional<bt2::Trace::Shared> trace;
204 nonstd::optional<bt2::TraceClass::Shared> trace_class;
206 lttng_live_metadata::UP metadata;
208 const bt_clock_class *clock_class = nullptr;
210 std::vector<lttng_live_stream_iterator::UP> stream_iterators;
212 enum lttng_live_metadata_stream_state metadata_stream_state =
213 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
216 struct lttng_live_session
218 using UP = std::unique_ptr<lttng_live_session>;
220 explicit lttng_live_session(const bt2_common::LogCfg& logCfgParam) noexcept :
225 ~lttng_live_session();
227 const bt2_common::LogCfg logCfg;
229 bt_self_component *self_comp = nullptr;
231 /* Weak reference. */
232 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
234 std::string hostname;
236 std::string session_name;
240 std::vector<lttng_live_trace::UP> traces;
242 bool attached = false;
243 bool new_streams_needed = false;
244 bool lazy_stream_msg_init = false;
248 enum session_not_found_action
250 SESSION_NOT_FOUND_ACTION_CONTINUE,
251 SESSION_NOT_FOUND_ACTION_FAIL,
252 SESSION_NOT_FOUND_ACTION_END,
256 * A component instance is an iterator on a single session.
258 struct lttng_live_component
260 using UP = std::unique_ptr<lttng_live_component>;
262 explicit lttng_live_component(const bt2_common::LogCfg& logCfgParam) noexcept :
267 const bt2_common::LogCfg logCfg;
269 /* Weak reference. */
270 bt_self_component *self_comp = nullptr;
275 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
278 size_t max_query_size = 0;
281 * Keeps track of whether the downstream component already has a
282 * message iterator on this component.
284 bool has_msg_iter = false;
287 struct lttng_live_msg_iter
289 using UP = std::unique_ptr<lttng_live_msg_iter>;
291 explicit lttng_live_msg_iter(const bt2_common::LogCfg& logCfgParam) noexcept :
296 ~lttng_live_msg_iter();
298 const bt2_common::LogCfg logCfg;
300 bt_self_component *self_comp = nullptr;
302 /* Weak reference. */
303 struct lttng_live_component *lttng_live_comp = nullptr;
305 /* Weak reference. */
306 bt_self_message_iterator *self_msg_iter = nullptr;
308 live_viewer_connection::UP viewer_connection;
310 std::vector<lttng_live_session::UP> sessions;
312 /* Number of live stream iterator this message iterator has.*/
313 uint64_t active_stream_iter = 0;
315 /* Timestamp in nanosecond of the last message sent downstream. */
316 int64_t last_msg_ts_ns = 0;
318 /* True if the iterator was interrupted. */
319 bool was_interrupted = false;
322 enum lttng_live_iterator_status
324 /** Iterator state has progressed. Continue iteration immediately. */
325 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
326 /** No message available for now. Try again later. */
327 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
328 /** No more CTF_LTTNG_LIVEs to be delivered. */
329 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
330 /** No error, okay. */
331 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
332 /** Invalid arguments. */
333 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
334 /** General error. */
335 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
336 /** Out of memory. */
337 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
338 /** Unsupported iterator feature. */
339 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
342 bt_component_class_initialize_method_status
343 lttng_live_component_init(bt_self_component_source *self_comp,
344 bt_self_component_source_configuration *config, const bt_value *params,
345 void *init_method_data);
347 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
348 bt_private_query_executor *priv_query_exec,
349 const char *object, const bt_value *params,
350 void *method_data, const bt_value **result);
352 void lttng_live_component_finalize(bt_self_component_source *component);
354 bt_message_iterator_class_next_method_status
355 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
356 uint64_t capacity, uint64_t *count);
358 bt_message_iterator_class_initialize_method_status
359 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
360 bt_self_message_iterator_configuration *config,
361 bt_self_component_port_output *self_port);
363 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
365 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
366 bt_self_message_iterator *self_msg_iter);
368 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
370 enum lttng_live_iterator_status
371 lttng_live_session_get_new_streams(struct lttng_live_session *session,
372 bt_self_message_iterator *self_msg_iter);
374 struct lttng_live_trace *
375 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
378 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
379 const char *hostname, const char *session_name);
382 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
383 * If new metadata is received, the function writes it to the provided file
384 * handle and updates the reply_len output parameter. This function should be
385 * called in loop until _END status is received to ensure all metadata is
386 * written to the file.
388 enum lttng_live_get_one_metadata_status
389 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
391 enum lttng_live_iterator_status
392 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
393 struct lttng_live_stream_iterator *stream, struct packet_index *index);
395 enum ctf_msg_iter_medium_status
396 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
397 struct lttng_live_stream_iterator *stream, uint8_t *buf,
398 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
400 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
403 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
404 enum lttng_live_stream_state new_state);
406 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */