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 "common/macros.h"
21 #include "../common/metadata/decoder.hpp"
22 #include "../common/msg-iter/msg-iter.hpp"
23 #include "viewer-connection.hpp"
25 struct lttng_live_component;
26 struct lttng_live_session;
27 struct lttng_live_msg_iter;
29 enum lttng_live_stream_state
31 /* This stream won't have data until some known time in the future. */
32 LTTNG_LIVE_STREAM_QUIESCENT,
34 * This stream won't have data until some known time in the future and
35 * the message iterator inactivity message was already sent downstream.
37 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
38 /* This stream has data ready to be consumed. */
39 LTTNG_LIVE_STREAM_ACTIVE_DATA,
41 * This stream has no data left to consume. We should asked the relay
44 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
45 /* This stream won't have anymore data, ever. */
46 LTTNG_LIVE_STREAM_EOF,
49 /* Iterator over a live stream. */
50 struct lttng_live_stream_iterator
52 bt_logging_level log_level;
53 bt_self_component *self_comp;
59 struct lttng_live_trace *trace;
62 * Since only a single iterator per viewer connection, we have
63 * only a single message iterator per stream.
65 struct ctf_msg_iter *msg_iter;
67 uint64_t viewer_stream_id;
73 } ctf_stream_class_id;
75 /* base offset in current index. */
77 /* len to read in current index. */
79 /* offset in current index. */
83 * Clock Snapshot value of the last message iterator inactivity message
93 * Clock Snapshot value of the current message iterator inactivity
94 * message we might want to send downstream.
96 uint64_t current_inactivity_ts;
98 enum lttng_live_stream_state state;
101 * The current message produced by this live stream iterator. Owned by
104 const bt_message *current_msg;
106 /* Timestamp in nanoseconds of the current message (current_msg). */
107 int64_t current_msg_ts_ns;
116 bool has_stream_hung_up;
119 struct lttng_live_metadata
121 bt_logging_level log_level;
122 bt_self_component *self_comp;
125 /* Weak reference. */
126 struct ctf_metadata_decoder *decoder;
129 enum lttng_live_metadata_stream_state
132 * The metadata needs to be updated. This is either because we just
133 * created the trace and haven't asked yet, or the relay specifically
134 * told us that new metadata is available.
136 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
138 * The metadata was updated and the relay has not told us we need to
141 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
143 * The relay has closed this metadata stream. We set this in reaction
144 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
145 * command to the relay. If this field is set, we have received all the
146 * metadata that we are ever going to get for that metadata stream.
148 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
151 struct lttng_live_trace
153 bt_logging_level log_level;
154 bt_self_component *self_comp;
156 /* Back reference to session. */
157 struct lttng_live_session *session;
159 /* ctf trace ID within the session. */
165 /* Weak reference. */
166 bt_trace_class *trace_class;
168 struct lttng_live_metadata *metadata;
170 const bt_clock_class *clock_class;
172 /* Array of pointers to struct lttng_live_stream_iterator. */
174 GPtrArray *stream_iterators;
176 enum lttng_live_metadata_stream_state metadata_stream_state;
179 struct lttng_live_session
181 bt_logging_level log_level;
182 bt_self_component *self_comp;
184 /* Weak reference. */
185 struct lttng_live_msg_iter *lttng_live_msg_iter;
191 GString *session_name;
195 /* Array of pointers to struct lttng_live_trace. */
199 bool new_streams_needed;
200 bool lazy_stream_msg_init;
204 enum session_not_found_action
206 SESSION_NOT_FOUND_ACTION_CONTINUE,
207 SESSION_NOT_FOUND_ACTION_FAIL,
208 SESSION_NOT_FOUND_ACTION_END,
212 * A component instance is an iterator on a single session.
214 struct lttng_live_component
216 bt_logging_level log_level;
218 /* Weak reference. */
219 bt_self_component *self_comp;
224 enum session_not_found_action sess_not_found_act;
227 size_t max_query_size;
230 * Keeps track of whether the downstream component already has a
231 * message iterator on this component.
236 struct lttng_live_msg_iter
238 bt_logging_level log_level;
239 bt_self_component *self_comp;
241 /* Weak reference. */
242 struct lttng_live_component *lttng_live_comp;
244 /* Weak reference. */
245 bt_self_message_iterator *self_msg_iter;
248 struct live_viewer_connection *viewer_connection;
250 /* Array of pointers to struct lttng_live_session. */
253 /* Number of live stream iterator this message iterator has.*/
254 uint64_t active_stream_iter;
256 /* Timestamp in nanosecond of the last message sent downstream. */
257 int64_t last_msg_ts_ns;
259 /* True if the iterator was interrupted. */
260 bool was_interrupted;
263 enum lttng_live_iterator_status
265 /** Iterator state has progressed. Continue iteration immediately. */
266 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
267 /** No message available for now. Try again later. */
268 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
269 /** No more CTF_LTTNG_LIVEs to be delivered. */
270 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
271 /** No error, okay. */
272 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
273 /** Invalid arguments. */
274 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
275 /** General error. */
276 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
277 /** Out of memory. */
278 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
279 /** Unsupported iterator feature. */
280 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
283 bt_component_class_initialize_method_status
284 lttng_live_component_init(bt_self_component_source *self_comp,
285 bt_self_component_source_configuration *config, const bt_value *params,
286 void *init_method_data);
288 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
289 bt_private_query_executor *priv_query_exec,
290 const char *object, const bt_value *params,
291 void *method_data, const bt_value **result);
293 void lttng_live_component_finalize(bt_self_component_source *component);
295 bt_message_iterator_class_next_method_status
296 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
297 uint64_t capacity, uint64_t *count);
299 bt_message_iterator_class_initialize_method_status
300 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
301 bt_self_message_iterator_configuration *config,
302 bt_self_component_port_output *self_port);
304 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
306 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
307 bt_self_message_iterator *self_msg_iter);
309 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
311 enum lttng_live_iterator_status
312 lttng_live_session_get_new_streams(struct lttng_live_session *session,
313 bt_self_message_iterator *self_msg_iter);
315 struct lttng_live_trace *
316 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
319 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
320 const char *hostname, const char *session_name);
323 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
324 * If new metadata is received, the function writes it to the provided file
325 * handle and updates the reply_len output parameter. This function should be
326 * called in loop until _END status is received to ensure all metadata is
327 * written to the file.
329 enum lttng_live_get_one_metadata_status
330 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, size_t *reply_len);
332 enum lttng_live_iterator_status
333 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
334 struct lttng_live_stream_iterator *stream, struct packet_index *index);
336 enum ctf_msg_iter_medium_status
337 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
338 struct lttng_live_stream_iterator *stream, uint8_t *buf,
339 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
341 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
343 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
344 enum lttng_live_stream_state new_state);
346 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */