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
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/tsdl/decoder.hpp"
23 #include "../common/src/msg-iter/msg-iter.hpp"
24 #include "viewer-connection.hpp"
26 enum lttng_live_stream_state
28 /* This stream won't have data until some known time in the future. */
29 LTTNG_LIVE_STREAM_QUIESCENT,
31 * This stream won't have data until some known time in the future and
32 * the message iterator inactivity message was already sent downstream.
34 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
35 /* This stream has data ready to be consumed. */
36 LTTNG_LIVE_STREAM_ACTIVE_DATA,
38 * This stream has no data left to consume. We should asked the relay
41 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
42 /* This stream won't have anymore data, ever. */
43 LTTNG_LIVE_STREAM_EOF,
46 inline const char *format_as(const lttng_live_stream_state state) noexcept
49 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA:
50 return "ACTIVE_NO_DATA";
52 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA:
53 return "QUIESCENT_NO_DATA";
55 case LTTNG_LIVE_STREAM_QUIESCENT:
58 case LTTNG_LIVE_STREAM_ACTIVE_DATA:
61 case LTTNG_LIVE_STREAM_EOF:
68 /* Iterator over a live stream. */
69 struct lttng_live_stream_iterator
71 using UP = std::unique_ptr<lttng_live_stream_iterator>;
73 explicit lttng_live_stream_iterator(const bt2c::Logger& parentLogger) :
74 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/STREAM-ITER"}
78 ~lttng_live_stream_iterator();
82 bt2::Stream::Shared stream;
85 struct lttng_live_trace *trace = nullptr;
88 * Since only a single iterator per viewer connection, we have
89 * only a single message iterator per stream.
91 ctf_msg_iter_up msg_iter;
93 uint64_t viewer_stream_id = 0;
99 } ctf_stream_class_id;
101 /* base offset in current index. */
102 uint64_t base_offset = 0;
103 /* len to read in current index. */
105 /* offset in current index. */
109 * Clock Snapshot value of the last message iterator inactivity message
116 } last_inactivity_ts;
119 * Clock Snapshot value of the current message iterator inactivity
120 * message we might want to send downstream.
122 uint64_t current_inactivity_ts = 0;
124 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
126 /* The current message produced by this live stream iterator. */
127 bt2::ConstMessage::Shared current_msg;
129 /* Timestamp in nanoseconds of the current message (current_msg). */
130 int64_t current_msg_ts_ns = 0;
132 std::vector<uint8_t> buf;
136 bool has_stream_hung_up = false;
139 struct lttng_live_metadata
141 using UP = std::unique_ptr<lttng_live_metadata>;
143 explicit lttng_live_metadata(const bt2c::Logger& parentLogger) :
144 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/METADATA"}
150 uint64_t stream_id = 0;
152 /* Weak reference. */
153 ctf_metadata_decoder_up decoder;
156 enum lttng_live_metadata_stream_state
159 * The metadata needs to be updated. This is either because we just
160 * created the trace and haven't asked yet, or the relay specifically
161 * told us that new metadata is available.
163 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
165 * The metadata was updated and the relay has not told us we need to
168 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
170 * The relay has closed this metadata stream. We set this in reaction
171 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
172 * command to the relay. If this field is set, we have received all the
173 * metadata that we are ever going to get for that metadata stream.
175 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
178 struct lttng_live_trace
180 using UP = std::unique_ptr<lttng_live_trace>;
182 explicit lttng_live_trace(const bt2c::Logger& parentLogger) :
183 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/TRACE"}
189 /* Back reference to session. */
190 struct lttng_live_session *session = nullptr;
192 /* ctf trace ID within the session. */
195 bt2::Trace::Shared trace;
197 bt2::TraceClass::Shared trace_class;
199 lttng_live_metadata::UP metadata;
201 const bt_clock_class *clock_class = nullptr;
203 std::vector<lttng_live_stream_iterator::UP> stream_iterators;
205 enum lttng_live_metadata_stream_state metadata_stream_state =
206 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
209 struct lttng_live_session
211 explicit lttng_live_session(const bt2c::Logger& parentLogger) :
212 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/SESSION"}
218 bt_self_component *self_comp = nullptr;
220 /* Weak reference. */
221 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
223 std::string hostname;
225 std::string session_name;
229 std::vector<lttng_live_trace::UP> traces;
231 bool attached = false;
232 bool new_streams_needed = false;
233 bool lazy_stream_msg_init = false;
237 enum session_not_found_action
239 SESSION_NOT_FOUND_ACTION_CONTINUE,
240 SESSION_NOT_FOUND_ACTION_FAIL,
241 SESSION_NOT_FOUND_ACTION_END,
245 * A component instance is an iterator on a single session.
247 struct lttng_live_component
249 using UP = std::unique_ptr<lttng_live_component>;
251 explicit lttng_live_component(bt2c::Logger loggerParam) noexcept :
252 logger {std::move(loggerParam)}
258 /* Weak reference. */
259 bt_self_component *self_comp = nullptr;
264 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
267 size_t max_query_size = 0;
270 * Keeps track of whether the downstream component already has a
271 * message iterator on this component.
273 bool has_msg_iter = false;
276 struct lttng_live_msg_iter
278 explicit lttng_live_msg_iter(const bt2c::Logger& parentLogger) :
279 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/MSG-ITER"}
285 bt_self_component *self_comp = nullptr;
287 /* Weak reference. */
288 struct lttng_live_component *lttng_live_comp = nullptr;
290 /* Weak reference. */
291 bt_self_message_iterator *self_msg_iter = nullptr;
294 struct live_viewer_connection *viewer_connection = nullptr;
296 /* Array of pointers to struct lttng_live_session. */
297 GPtrArray *sessions = nullptr;
299 /* Number of live stream iterator this message iterator has.*/
300 uint64_t active_stream_iter = 0;
302 /* Timestamp in nanosecond of the last message sent downstream. */
303 int64_t last_msg_ts_ns = 0;
305 /* True if the iterator was interrupted. */
306 bool was_interrupted = false;
309 enum lttng_live_iterator_status
311 /** Iterator state has progressed. Continue iteration immediately. */
312 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
313 /** No message available for now. Try again later. */
314 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
315 /** No more CTF_LTTNG_LIVEs to be delivered. */
316 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
317 /** No error, okay. */
318 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
319 /** Invalid arguments. */
320 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
321 /** General error. */
322 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
323 /** Out of memory. */
324 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
325 /** Unsupported iterator feature. */
326 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
329 inline const char *format_as(const lttng_live_iterator_status status) noexcept
332 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE:
333 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
335 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
336 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
338 case LTTNG_LIVE_ITERATOR_STATUS_END:
339 return "LTTNG_LIVE_ITERATOR_STATUS_END";
341 case LTTNG_LIVE_ITERATOR_STATUS_OK:
342 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
344 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
345 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
347 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
348 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
350 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
351 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
353 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
354 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
360 bt_component_class_initialize_method_status
361 lttng_live_component_init(bt_self_component_source *self_comp,
362 bt_self_component_source_configuration *config, const bt_value *params,
363 void *init_method_data);
365 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
366 bt_private_query_executor *priv_query_exec,
367 const char *object, const bt_value *params,
368 void *method_data, const bt_value **result);
370 void lttng_live_component_finalize(bt_self_component_source *component);
372 bt_message_iterator_class_next_method_status
373 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
374 uint64_t capacity, uint64_t *count);
376 bt_message_iterator_class_initialize_method_status
377 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
378 bt_self_message_iterator_configuration *config,
379 bt_self_component_port_output *self_port);
381 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
383 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
384 bt_self_message_iterator *self_msg_iter);
386 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
388 enum lttng_live_iterator_status
389 lttng_live_session_get_new_streams(struct lttng_live_session *session,
390 bt_self_message_iterator *self_msg_iter);
392 struct lttng_live_trace *
393 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
396 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
397 const char *hostname, const char *session_name);
400 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
401 * If new metadata is received, the function writes it to the provided file
402 * handle and updates the reply_len output parameter. This function should be
403 * called in loop until _END status is received to ensure all metadata is
404 * written to the file.
406 enum lttng_live_get_one_metadata_status
407 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
409 enum lttng_live_iterator_status
410 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
411 struct lttng_live_stream_iterator *stream, struct packet_index *index);
413 enum ctf_msg_iter_medium_status
414 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
415 struct lttng_live_stream_iterator *stream, uint8_t *buf,
416 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
418 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
420 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
421 enum lttng_live_stream_state new_state);
423 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */