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/vendor/fmt/format.h" /* IWYU pragma: keep */
21 #include "../common/src/metadata/tsdl/decoder.hpp"
22 #include "../common/src/msg-iter/msg-iter.hpp"
23 #include "viewer-connection.hpp"
25 enum lttng_live_stream_state
27 /* This stream won't have data until some known time in the future. */
28 LTTNG_LIVE_STREAM_QUIESCENT,
30 * This stream won't have data until some known time in the future and
31 * the message iterator inactivity message was already sent downstream.
33 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
34 /* This stream has data ready to be consumed. */
35 LTTNG_LIVE_STREAM_ACTIVE_DATA,
37 * This stream has no data left to consume. We should asked the relay
40 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
41 /* This stream won't have anymore data, ever. */
42 LTTNG_LIVE_STREAM_EOF,
45 inline const char *format_as(const lttng_live_stream_state state) noexcept
48 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA:
49 return "ACTIVE_NO_DATA";
51 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA:
52 return "QUIESCENT_NO_DATA";
54 case LTTNG_LIVE_STREAM_QUIESCENT:
57 case LTTNG_LIVE_STREAM_ACTIVE_DATA:
60 case LTTNG_LIVE_STREAM_EOF:
67 /* Iterator over a live stream. */
68 struct lttng_live_stream_iterator
70 explicit lttng_live_stream_iterator(const bt2c::Logger& parentLogger) :
71 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/STREAM-ITER"}
77 bt2::Stream::Shared stream;
80 struct lttng_live_trace *trace = nullptr;
83 * Since only a single iterator per viewer connection, we have
84 * only a single message iterator per stream.
86 ctf_msg_iter_up msg_iter;
88 uint64_t viewer_stream_id = 0;
94 } ctf_stream_class_id;
96 /* base offset in current index. */
97 uint64_t base_offset = 0;
98 /* len to read in current index. */
100 /* offset in current index. */
104 * Clock Snapshot value of the last message iterator inactivity message
111 } last_inactivity_ts;
114 * Clock Snapshot value of the current message iterator inactivity
115 * message we might want to send downstream.
117 uint64_t current_inactivity_ts = 0;
119 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
122 * The current message produced by this live stream iterator. Owned by
125 const bt_message *current_msg = nullptr;
127 /* Timestamp in nanoseconds of the current message (current_msg). */
128 int64_t current_msg_ts_ns = 0;
130 std::vector<uint8_t> buf;
133 GString *name = nullptr;
135 bool has_stream_hung_up = false;
138 struct lttng_live_metadata
140 using UP = std::unique_ptr<lttng_live_metadata>;
142 explicit lttng_live_metadata(const bt2c::Logger& parentLogger) :
143 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/METADATA"}
149 uint64_t stream_id = 0;
151 /* Weak reference. */
152 ctf_metadata_decoder_up decoder;
155 enum lttng_live_metadata_stream_state
158 * The metadata needs to be updated. This is either because we just
159 * created the trace and haven't asked yet, or the relay specifically
160 * told us that new metadata is available.
162 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
164 * The metadata was updated and the relay has not told us we need to
167 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
169 * The relay has closed this metadata stream. We set this in reaction
170 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
171 * command to the relay. If this field is set, we have received all the
172 * metadata that we are ever going to get for that metadata stream.
174 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
177 struct lttng_live_trace
179 explicit lttng_live_trace(const bt2c::Logger& parentLogger) :
180 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/TRACE"}
186 /* Back reference to session. */
187 struct lttng_live_session *session = nullptr;
189 /* ctf trace ID within the session. */
192 bt2::Trace::Shared trace;
194 bt2::TraceClass::Shared trace_class;
196 lttng_live_metadata::UP metadata;
198 const bt_clock_class *clock_class = nullptr;
200 /* Array of pointers to struct lttng_live_stream_iterator. */
202 GPtrArray *stream_iterators = nullptr;
204 enum lttng_live_metadata_stream_state metadata_stream_state =
205 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
208 struct lttng_live_session
210 explicit lttng_live_session(const bt2c::Logger& parentLogger) :
211 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/SESSION"}
217 bt_self_component *self_comp = nullptr;
219 /* Weak reference. */
220 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
223 GString *hostname = nullptr;
226 GString *session_name = nullptr;
230 /* Array of pointers to struct lttng_live_trace. */
231 GPtrArray *traces = nullptr;
233 bool attached = false;
234 bool new_streams_needed = false;
235 bool lazy_stream_msg_init = false;
239 enum session_not_found_action
241 SESSION_NOT_FOUND_ACTION_CONTINUE,
242 SESSION_NOT_FOUND_ACTION_FAIL,
243 SESSION_NOT_FOUND_ACTION_END,
247 * A component instance is an iterator on a single session.
249 struct lttng_live_component
251 using UP = std::unique_ptr<lttng_live_component>;
253 explicit lttng_live_component(bt2c::Logger loggerParam) noexcept :
254 logger {std::move(loggerParam)}
260 /* Weak reference. */
261 bt_self_component *self_comp = nullptr;
266 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
269 size_t max_query_size = 0;
272 * Keeps track of whether the downstream component already has a
273 * message iterator on this component.
275 bool has_msg_iter = false;
278 struct lttng_live_msg_iter
280 explicit lttng_live_msg_iter(const bt2c::Logger& parentLogger) :
281 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/MSG-ITER"}
287 bt_self_component *self_comp = nullptr;
289 /* Weak reference. */
290 struct lttng_live_component *lttng_live_comp = nullptr;
292 /* Weak reference. */
293 bt_self_message_iterator *self_msg_iter = nullptr;
296 struct live_viewer_connection *viewer_connection = nullptr;
298 /* Array of pointers to struct lttng_live_session. */
299 GPtrArray *sessions = nullptr;
301 /* Number of live stream iterator this message iterator has.*/
302 uint64_t active_stream_iter = 0;
304 /* Timestamp in nanosecond of the last message sent downstream. */
305 int64_t last_msg_ts_ns = 0;
307 /* True if the iterator was interrupted. */
308 bool was_interrupted = false;
311 enum lttng_live_iterator_status
313 /** Iterator state has progressed. Continue iteration immediately. */
314 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
315 /** No message available for now. Try again later. */
316 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
317 /** No more CTF_LTTNG_LIVEs to be delivered. */
318 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
319 /** No error, okay. */
320 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
321 /** Invalid arguments. */
322 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
323 /** General error. */
324 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
325 /** Out of memory. */
326 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
327 /** Unsupported iterator feature. */
328 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
331 inline const char *format_as(const lttng_live_iterator_status status) noexcept
334 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE:
335 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
337 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
338 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
340 case LTTNG_LIVE_ITERATOR_STATUS_END:
341 return "LTTNG_LIVE_ITERATOR_STATUS_END";
343 case LTTNG_LIVE_ITERATOR_STATUS_OK:
344 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
346 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
347 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
349 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
350 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
352 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
353 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
355 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
356 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
362 bt_component_class_initialize_method_status
363 lttng_live_component_init(bt_self_component_source *self_comp,
364 bt_self_component_source_configuration *config, const bt_value *params,
365 void *init_method_data);
367 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
368 bt_private_query_executor *priv_query_exec,
369 const char *object, const bt_value *params,
370 void *method_data, const bt_value **result);
372 void lttng_live_component_finalize(bt_self_component_source *component);
374 bt_message_iterator_class_next_method_status
375 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
376 uint64_t capacity, uint64_t *count);
378 bt_message_iterator_class_initialize_method_status
379 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
380 bt_self_message_iterator_configuration *config,
381 bt_self_component_port_output *self_port);
383 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
385 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
386 bt_self_message_iterator *self_msg_iter);
388 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
390 enum lttng_live_iterator_status
391 lttng_live_session_get_new_streams(struct lttng_live_session *session,
392 bt_self_message_iterator *self_msg_iter);
394 struct lttng_live_trace *
395 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
398 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
399 const char *hostname, const char *session_name);
402 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
403 * If new metadata is received, the function writes it to the provided file
404 * handle and updates the reply_len output parameter. This function should be
405 * called in loop until _END status is received to ensure all metadata is
406 * written to the file.
408 enum lttng_live_get_one_metadata_status
409 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
411 enum lttng_live_iterator_status
412 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
413 struct lttng_live_stream_iterator *stream, struct packet_index *index);
415 enum ctf_msg_iter_medium_status
416 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
417 struct lttng_live_stream_iterator *stream, uint8_t *buf,
418 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
420 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
422 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
423 enum lttng_live_stream_state new_state);
425 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */