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;
31 enum lttng_live_stream_state
33 /* This stream won't have data until some known time in the future. */
34 LTTNG_LIVE_STREAM_QUIESCENT,
36 * This stream won't have data until some known time in the future and
37 * the message iterator inactivity message was already sent downstream.
39 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
40 /* This stream has data ready to be consumed. */
41 LTTNG_LIVE_STREAM_ACTIVE_DATA,
43 * This stream has no data left to consume. We should asked the relay
46 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
47 /* This stream won't have anymore data, ever. */
48 LTTNG_LIVE_STREAM_EOF,
51 /* Iterator over a live stream. */
52 struct lttng_live_stream_iterator
54 using UP = std::unique_ptr<lttng_live_stream_iterator>;
56 explicit lttng_live_stream_iterator(const bt2_common::LogCfg& logCfgParam) noexcept :
61 ~lttng_live_stream_iterator();
63 const bt2_common::LogCfg logCfg;
66 nonstd::optional<bt2::Stream::Shared> stream;
69 struct lttng_live_trace *trace = nullptr;
72 * Since only a single iterator per viewer connection, we have
73 * only a single message iterator per stream.
75 ctf_msg_iter_up msg_iter;
77 uint64_t viewer_stream_id = 0;
83 } ctf_stream_class_id;
85 /* base offset in current index. */
86 uint64_t base_offset = 0;
87 /* len to read in current index. */
89 /* offset in current index. */
93 * Clock Snapshot value of the last message iterator inactivity message
100 } last_inactivity_ts;
103 * Clock Snapshot value of the current message iterator inactivity
104 * message we might want to send downstream.
106 uint64_t current_inactivity_ts = 0;
108 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
111 * The current message produced by this live stream iterator. Owned by
114 nonstd::optional<bt2::ConstMessage::Shared> current_msg;
116 /* Timestamp in nanoseconds of the current message (current_msg). */
117 int64_t current_msg_ts_ns = 0;
119 std::vector<uint8_t> buf;
123 bool has_stream_hung_up = false;
126 struct lttng_live_metadata
128 using UP = std::unique_ptr<lttng_live_metadata>;
130 explicit lttng_live_metadata(const bt2_common::LogCfg& logCfgParam) noexcept :
135 const bt2_common::LogCfg logCfg;
137 uint64_t stream_id = 0;
139 /* Weak reference. */
140 ctf_metadata_decoder_up decoder;
143 enum lttng_live_metadata_stream_state
146 * The metadata needs to be updated. This is either because we just
147 * created the trace and haven't asked yet, or the relay specifically
148 * told us that new metadata is available.
150 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
152 * The metadata was updated and the relay has not told us we need to
155 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
157 * The relay has closed this metadata stream. We set this in reaction
158 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
159 * command to the relay. If this field is set, we have received all the
160 * metadata that we are ever going to get for that metadata stream.
162 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
165 struct lttng_live_trace
167 using UP = std::unique_ptr<lttng_live_trace>;
169 explicit lttng_live_trace(const bt2_common::LogCfg& logCfgParam) noexcept : logCfg {logCfgParam}
173 const bt2_common::LogCfg logCfg;
175 /* Back reference to session. */
176 struct lttng_live_session *session = nullptr;
178 /* ctf trace ID within the session. */
182 nonstd::optional<bt2::Trace::Shared> trace;
184 nonstd::optional<bt2::TraceClass::Shared> trace_class;
186 lttng_live_metadata::UP metadata;
188 const bt_clock_class *clock_class = nullptr;
190 std::vector<lttng_live_stream_iterator::UP> stream_iterators;
192 enum lttng_live_metadata_stream_state metadata_stream_state =
193 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
196 struct lttng_live_session
198 using UP = std::unique_ptr<lttng_live_session>;
200 explicit lttng_live_session(const bt2_common::LogCfg& logCfgParam) noexcept :
205 ~lttng_live_session();
207 const bt2_common::LogCfg logCfg;
209 bt_self_component *self_comp = nullptr;
211 /* Weak reference. */
212 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
214 std::string hostname;
216 std::string session_name;
220 std::vector<lttng_live_trace::UP> traces;
222 bool attached = false;
223 bool new_streams_needed = false;
224 bool lazy_stream_msg_init = false;
228 enum session_not_found_action
230 SESSION_NOT_FOUND_ACTION_CONTINUE,
231 SESSION_NOT_FOUND_ACTION_FAIL,
232 SESSION_NOT_FOUND_ACTION_END,
236 * A component instance is an iterator on a single session.
238 struct lttng_live_component
240 using UP = std::unique_ptr<lttng_live_component>;
242 explicit lttng_live_component(const bt2_common::LogCfg& logCfgParam) noexcept :
247 const bt2_common::LogCfg logCfg;
249 /* Weak reference. */
250 bt_self_component *self_comp = nullptr;
255 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
258 size_t max_query_size = 0;
261 * Keeps track of whether the downstream component already has a
262 * message iterator on this component.
264 bool has_msg_iter = false;
267 struct lttng_live_msg_iter
269 explicit lttng_live_msg_iter(const bt2_common::LogCfg& logCfgParam) noexcept :
274 const bt2_common::LogCfg logCfg;
276 bt_self_component *self_comp = nullptr;
278 /* Weak reference. */
279 struct lttng_live_component *lttng_live_comp = nullptr;
281 /* Weak reference. */
282 bt_self_message_iterator *self_msg_iter = nullptr;
285 struct live_viewer_connection *viewer_connection = nullptr;
287 std::vector<lttng_live_session::UP> sessions;
289 /* Number of live stream iterator this message iterator has.*/
290 uint64_t active_stream_iter = 0;
292 /* Timestamp in nanosecond of the last message sent downstream. */
293 int64_t last_msg_ts_ns = 0;
295 /* True if the iterator was interrupted. */
296 bool was_interrupted = false;
299 enum lttng_live_iterator_status
301 /** Iterator state has progressed. Continue iteration immediately. */
302 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
303 /** No message available for now. Try again later. */
304 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
305 /** No more CTF_LTTNG_LIVEs to be delivered. */
306 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
307 /** No error, okay. */
308 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
309 /** Invalid arguments. */
310 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
311 /** General error. */
312 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
313 /** Out of memory. */
314 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
315 /** Unsupported iterator feature. */
316 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
319 bt_component_class_initialize_method_status
320 lttng_live_component_init(bt_self_component_source *self_comp,
321 bt_self_component_source_configuration *config, const bt_value *params,
322 void *init_method_data);
324 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
325 bt_private_query_executor *priv_query_exec,
326 const char *object, const bt_value *params,
327 void *method_data, const bt_value **result);
329 void lttng_live_component_finalize(bt_self_component_source *component);
331 bt_message_iterator_class_next_method_status
332 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
333 uint64_t capacity, uint64_t *count);
335 bt_message_iterator_class_initialize_method_status
336 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
337 bt_self_message_iterator_configuration *config,
338 bt_self_component_port_output *self_port);
340 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
342 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
343 bt_self_message_iterator *self_msg_iter);
345 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
347 enum lttng_live_iterator_status
348 lttng_live_session_get_new_streams(struct lttng_live_session *session,
349 bt_self_message_iterator *self_msg_iter);
351 struct lttng_live_trace *
352 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
355 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
356 const char *hostname, const char *session_name);
359 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
360 * If new metadata is received, the function writes it to the provided file
361 * handle and updates the reply_len output parameter. This function should be
362 * called in loop until _END status is received to ensure all metadata is
363 * written to the file.
365 enum lttng_live_get_one_metadata_status
366 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
368 enum lttng_live_iterator_status
369 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
370 struct lttng_live_stream_iterator *stream, struct packet_index *index);
372 enum ctf_msg_iter_medium_status
373 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
374 struct lttng_live_stream_iterator *stream, uint8_t *buf,
375 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
377 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
380 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
381 enum lttng_live_stream_state new_state);
383 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */