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 "../common/src/metadata/tsdl/decoder.hpp"
23 #include "../common/src/msg-iter/msg-iter.hpp"
24 #include "viewer-connection.hpp"
26 struct lttng_live_component;
27 struct lttng_live_session;
28 struct lttng_live_msg_iter;
30 enum lttng_live_stream_state
32 /* This stream won't have data until some known time in the future. */
33 LTTNG_LIVE_STREAM_QUIESCENT,
35 * This stream won't have data until some known time in the future and
36 * the message iterator inactivity message was already sent downstream.
38 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
39 /* This stream has data ready to be consumed. */
40 LTTNG_LIVE_STREAM_ACTIVE_DATA,
42 * This stream has no data left to consume. We should asked the relay
45 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
46 /* This stream won't have anymore data, ever. */
47 LTTNG_LIVE_STREAM_EOF,
50 /* Iterator over a live stream. */
51 struct lttng_live_stream_iterator
53 explicit lttng_live_stream_iterator(const bt2_common::LogCfg& logCfgParam) noexcept :
58 const bt2_common::LogCfg logCfg;
61 bt_stream *stream = nullptr;
64 struct lttng_live_trace *trace = nullptr;
67 * Since only a single iterator per viewer connection, we have
68 * only a single message iterator per stream.
70 struct ctf_msg_iter *msg_iter = nullptr;
72 uint64_t viewer_stream_id = 0;
78 } ctf_stream_class_id;
80 /* base offset in current index. */
81 uint64_t base_offset = 0;
82 /* len to read in current index. */
84 /* offset in current index. */
88 * Clock Snapshot value of the last message iterator inactivity message
98 * Clock Snapshot value of the current message iterator inactivity
99 * message we might want to send downstream.
101 uint64_t current_inactivity_ts = 0;
103 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
106 * The current message produced by this live stream iterator. Owned by
109 const bt_message *current_msg = nullptr;
111 /* Timestamp in nanoseconds of the current message (current_msg). */
112 int64_t current_msg_ts_ns = 0;
115 uint8_t *buf = nullptr;
119 GString *name = nullptr;
121 bool has_stream_hung_up = false;
124 struct lttng_live_metadata
126 explicit lttng_live_metadata(const bt2_common::LogCfg& logCfgParam) noexcept :
131 const bt2_common::LogCfg logCfg;
133 uint64_t stream_id = 0;
135 /* Weak reference. */
136 ctf_metadata_decoder_up decoder;
139 enum lttng_live_metadata_stream_state
142 * The metadata needs to be updated. This is either because we just
143 * created the trace and haven't asked yet, or the relay specifically
144 * told us that new metadata is available.
146 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
148 * The metadata was updated and the relay has not told us we need to
151 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
153 * The relay has closed this metadata stream. We set this in reaction
154 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
155 * command to the relay. If this field is set, we have received all the
156 * metadata that we are ever going to get for that metadata stream.
158 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
161 struct lttng_live_trace
163 explicit lttng_live_trace(const bt2_common::LogCfg& logCfgParam) noexcept : logCfg {logCfgParam}
167 const bt2_common::LogCfg logCfg;
169 /* Back reference to session. */
170 struct lttng_live_session *session = nullptr;
172 /* ctf trace ID within the session. */
176 nonstd::optional<bt2::Trace::Shared> trace;
178 nonstd::optional<bt2::TraceClass::Shared> trace_class;
180 struct lttng_live_metadata *metadata = nullptr;
182 const bt_clock_class *clock_class = nullptr;
184 /* Array of pointers to struct lttng_live_stream_iterator. */
186 GPtrArray *stream_iterators = nullptr;
188 enum lttng_live_metadata_stream_state metadata_stream_state =
189 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
192 struct lttng_live_session
194 explicit lttng_live_session(const bt2_common::LogCfg& logCfgParam) noexcept :
199 const bt2_common::LogCfg logCfg;
201 bt_self_component *self_comp = nullptr;
203 /* Weak reference. */
204 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
207 GString *hostname = nullptr;
210 GString *session_name = nullptr;
214 /* Array of pointers to struct lttng_live_trace. */
215 GPtrArray *traces = nullptr;
217 bool attached = false;
218 bool new_streams_needed = false;
219 bool lazy_stream_msg_init = false;
223 enum session_not_found_action
225 SESSION_NOT_FOUND_ACTION_CONTINUE,
226 SESSION_NOT_FOUND_ACTION_FAIL,
227 SESSION_NOT_FOUND_ACTION_END,
231 * A component instance is an iterator on a single session.
233 struct lttng_live_component
235 explicit lttng_live_component(const bt2_common::LogCfg& logCfgParam) noexcept :
240 const bt2_common::LogCfg logCfg;
242 /* Weak reference. */
243 bt_self_component *self_comp = nullptr;
247 GString *url = nullptr;
248 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
251 size_t max_query_size = 0;
254 * Keeps track of whether the downstream component already has a
255 * message iterator on this component.
257 bool has_msg_iter = false;
260 struct lttng_live_msg_iter
262 explicit lttng_live_msg_iter(const bt2_common::LogCfg& logCfgParam) noexcept :
267 const bt2_common::LogCfg logCfg;
269 bt_self_component *self_comp = nullptr;
271 /* Weak reference. */
272 struct lttng_live_component *lttng_live_comp = nullptr;
274 /* Weak reference. */
275 bt_self_message_iterator *self_msg_iter = nullptr;
278 struct live_viewer_connection *viewer_connection = nullptr;
280 /* Array of pointers to struct lttng_live_session. */
281 GPtrArray *sessions = nullptr;
283 /* Number of live stream iterator this message iterator has.*/
284 uint64_t active_stream_iter = 0;
286 /* Timestamp in nanosecond of the last message sent downstream. */
287 int64_t last_msg_ts_ns = 0;
289 /* True if the iterator was interrupted. */
290 bool was_interrupted = false;
293 enum lttng_live_iterator_status
295 /** Iterator state has progressed. Continue iteration immediately. */
296 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
297 /** No message available for now. Try again later. */
298 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
299 /** No more CTF_LTTNG_LIVEs to be delivered. */
300 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
301 /** No error, okay. */
302 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
303 /** Invalid arguments. */
304 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
305 /** General error. */
306 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
307 /** Out of memory. */
308 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
309 /** Unsupported iterator feature. */
310 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
313 bt_component_class_initialize_method_status
314 lttng_live_component_init(bt_self_component_source *self_comp,
315 bt_self_component_source_configuration *config, const bt_value *params,
316 void *init_method_data);
318 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
319 bt_private_query_executor *priv_query_exec,
320 const char *object, const bt_value *params,
321 void *method_data, const bt_value **result);
323 void lttng_live_component_finalize(bt_self_component_source *component);
325 bt_message_iterator_class_next_method_status
326 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
327 uint64_t capacity, uint64_t *count);
329 bt_message_iterator_class_initialize_method_status
330 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
331 bt_self_message_iterator_configuration *config,
332 bt_self_component_port_output *self_port);
334 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
336 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
337 bt_self_message_iterator *self_msg_iter);
339 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
341 enum lttng_live_iterator_status
342 lttng_live_session_get_new_streams(struct lttng_live_session *session,
343 bt_self_message_iterator *self_msg_iter);
345 struct lttng_live_trace *
346 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
349 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
350 const char *hostname, const char *session_name);
353 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
354 * If new metadata is received, the function writes it to the provided file
355 * handle and updates the reply_len output parameter. This function should be
356 * called in loop until _END status is received to ensure all metadata is
357 * written to the file.
359 enum lttng_live_get_one_metadata_status
360 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
362 enum lttng_live_iterator_status
363 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
364 struct lttng_live_stream_iterator *stream, struct packet_index *index);
366 enum ctf_msg_iter_medium_status
367 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
368 struct lttng_live_stream_iterator *stream, uint8_t *buf,
369 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
371 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
374 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
375 enum lttng_live_stream_state new_state);
377 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */