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"
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 ctf_msg_iter_up msg_iter;
118 uint64_t viewer_stream_id = 0;
124 } ctf_stream_class_id;
126 /* base offset in current index. */
127 uint64_t base_offset = 0;
128 /* len to read in current index. */
130 /* offset in current index. */
134 * Clock Snapshot value of the last message iterator inactivity message
141 } last_inactivity_ts;
144 * Clock Snapshot value of the current message iterator inactivity
145 * message we might want to send downstream.
147 uint64_t current_inactivity_ts = 0;
149 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
151 /* The current message produced by this live stream iterator. */
152 bt2::ConstMessage::Shared current_msg;
154 /* Timestamp in nanoseconds of the current message (current_msg). */
155 int64_t current_msg_ts_ns = 0;
157 std::vector<uint8_t> buf;
161 bool has_stream_hung_up = false;
164 struct lttng_live_metadata
166 using UP = std::unique_ptr<lttng_live_metadata>;
168 explicit lttng_live_metadata(const bt2c::Logger& parentLogger) :
169 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/METADATA"}
175 uint64_t stream_id = 0;
177 /* Weak reference. */
178 ctf_metadata_decoder_up decoder;
181 enum lttng_live_metadata_stream_state
184 * The metadata needs to be updated. This is either because we just
185 * created the trace and haven't asked yet, or the relay specifically
186 * told us that new metadata is available.
188 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
190 * The metadata was updated and the relay has not told us we need to
193 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
195 * The relay has closed this metadata stream. We set this in reaction
196 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
197 * command to the relay. If this field is set, we have received all the
198 * metadata that we are ever going to get for that metadata stream.
200 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
203 struct lttng_live_trace
205 using UP = std::unique_ptr<lttng_live_trace>;
207 explicit lttng_live_trace(const bt2c::Logger& parentLogger) :
208 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/TRACE"}
214 /* Back reference to session. */
215 struct lttng_live_session *session = nullptr;
217 /* ctf trace ID within the session. */
220 bt2::Trace::Shared trace;
222 bt2::TraceClass::Shared trace_class;
224 lttng_live_metadata::UP metadata;
226 const bt_clock_class *clock_class = nullptr;
228 std::vector<lttng_live_stream_iterator::UP> stream_iterators;
230 enum lttng_live_metadata_stream_state metadata_stream_state =
231 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
234 struct lttng_live_session
236 using UP = std::unique_ptr<lttng_live_session>;
238 explicit lttng_live_session(const bt2c::Logger& parentLogger) :
239 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/SESSION"}
243 ~lttng_live_session();
247 bt_self_component *self_comp = nullptr;
249 /* Weak reference. */
250 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
252 std::string hostname;
254 std::string session_name;
258 std::vector<lttng_live_trace::UP> traces;
260 bool attached = false;
261 bool new_streams_needed = false;
262 bool lazy_stream_msg_init = false;
266 enum session_not_found_action
268 SESSION_NOT_FOUND_ACTION_CONTINUE,
269 SESSION_NOT_FOUND_ACTION_FAIL,
270 SESSION_NOT_FOUND_ACTION_END,
274 * A component instance is an iterator on a single session.
276 struct lttng_live_component
278 using UP = std::unique_ptr<lttng_live_component>;
280 explicit lttng_live_component(bt2c::Logger loggerParam) noexcept :
281 logger {std::move(loggerParam)}
287 /* Weak reference. */
288 bt_self_component *self_comp = nullptr;
293 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
296 size_t max_query_size = 0;
299 * Keeps track of whether the downstream component already has a
300 * message iterator on this component.
302 bool has_msg_iter = false;
305 struct lttng_live_msg_iter
307 using UP = std::unique_ptr<lttng_live_msg_iter>;
309 explicit lttng_live_msg_iter(const bt2c::Logger& parentLogger) :
310 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/MSG-ITER"}
314 ~lttng_live_msg_iter();
318 bt_self_component *self_comp = nullptr;
320 /* Weak reference. */
321 struct lttng_live_component *lttng_live_comp = nullptr;
323 /* Weak reference. */
324 bt_self_message_iterator *self_msg_iter = nullptr;
326 live_viewer_connection::UP viewer_connection;
328 std::vector<lttng_live_session::UP> sessions;
330 /* Number of live stream iterator this message iterator has.*/
331 uint64_t active_stream_iter = 0;
333 /* Timestamp in nanosecond of the last message sent downstream. */
334 int64_t last_msg_ts_ns = 0;
336 /* True if the iterator was interrupted. */
337 bool was_interrupted = false;
340 enum lttng_live_iterator_status
342 /** Iterator state has progressed. Continue iteration immediately. */
343 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
344 /** No message available for now. Try again later. */
345 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
346 /** No more CTF_LTTNG_LIVEs to be delivered. */
347 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
348 /** No error, okay. */
349 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
350 /** Invalid arguments. */
351 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
352 /** General error. */
353 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
354 /** Out of memory. */
355 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
356 /** Unsupported iterator feature. */
357 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
360 inline const char *format_as(const lttng_live_iterator_status status) noexcept
363 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE:
364 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
366 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
367 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
369 case LTTNG_LIVE_ITERATOR_STATUS_END:
370 return "LTTNG_LIVE_ITERATOR_STATUS_END";
372 case LTTNG_LIVE_ITERATOR_STATUS_OK:
373 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
375 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
376 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
378 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
379 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
381 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
382 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
384 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
385 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
391 bt_component_class_initialize_method_status
392 lttng_live_component_init(bt_self_component_source *self_comp,
393 bt_self_component_source_configuration *config, const bt_value *params,
394 void *init_method_data);
396 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
397 bt_private_query_executor *priv_query_exec,
398 const char *object, const bt_value *params,
399 void *method_data, const bt_value **result);
401 void lttng_live_component_finalize(bt_self_component_source *component);
403 bt_message_iterator_class_next_method_status
404 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
405 uint64_t capacity, uint64_t *count);
407 bt_message_iterator_class_initialize_method_status
408 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
409 bt_self_message_iterator_configuration *config,
410 bt_self_component_port_output *self_port);
412 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
414 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
415 bt_self_message_iterator *self_msg_iter);
417 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
419 enum lttng_live_iterator_status
420 lttng_live_session_get_new_streams(struct lttng_live_session *session,
421 bt_self_message_iterator *self_msg_iter);
423 struct lttng_live_trace *
424 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
427 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
428 const char *hostname, const char *session_name);
431 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
432 * If new metadata is received, the function writes it to the provided file
433 * handle and updates the reply_len output parameter. This function should be
434 * called in loop until _END status is received to ensure all metadata is
435 * written to the file.
437 enum lttng_live_get_one_metadata_status
438 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
440 enum lttng_live_iterator_status
441 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
442 struct lttng_live_stream_iterator *stream, struct packet_index *index);
444 enum ctf_msg_iter_medium_status
445 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
446 struct lttng_live_stream_iterator *stream, uint8_t *buf,
447 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
449 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
451 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
452 enum lttng_live_stream_state new_state);
454 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */