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/src/metadata/tsdl/decoder.hpp"
20 #include "../common/src/msg-iter/msg-iter.hpp"
21 #include "viewer-connection.hpp"
23 enum lttng_live_stream_state
25 /* This stream won't have data until some known time in the future. */
26 LTTNG_LIVE_STREAM_QUIESCENT,
28 * This stream won't have data until some known time in the future and
29 * the message iterator inactivity message was already sent downstream.
31 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
32 /* This stream has data ready to be consumed. */
33 LTTNG_LIVE_STREAM_ACTIVE_DATA,
35 * This stream has no data left to consume. We should asked the relay
38 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
39 /* This stream won't have anymore data, ever. */
40 LTTNG_LIVE_STREAM_EOF,
43 /* Iterator over a live stream. */
44 struct lttng_live_stream_iterator
46 bt_logging_level log_level = (bt_logging_level) 0;
47 bt_self_component *self_comp = nullptr;
50 bt_stream *stream = nullptr;
53 struct lttng_live_trace *trace = nullptr;
56 * Since only a single iterator per viewer connection, we have
57 * only a single message iterator per stream.
59 struct ctf_msg_iter *msg_iter = nullptr;
61 uint64_t viewer_stream_id = 0;
67 } ctf_stream_class_id;
69 /* base offset in current index. */
70 uint64_t base_offset = 0;
71 /* len to read in current index. */
73 /* offset in current index. */
77 * Clock Snapshot value of the last message iterator inactivity message
87 * Clock Snapshot value of the current message iterator inactivity
88 * message we might want to send downstream.
90 uint64_t current_inactivity_ts = 0;
92 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
95 * The current message produced by this live stream iterator. Owned by
98 const bt_message *current_msg = nullptr;
100 /* Timestamp in nanoseconds of the current message (current_msg). */
101 int64_t current_msg_ts_ns = 0;
104 uint8_t *buf = nullptr;
108 GString *name = nullptr;
110 bool has_stream_hung_up = false;
113 struct lttng_live_metadata
115 bt_logging_level log_level = (bt_logging_level) 0;
116 bt_self_component *self_comp = nullptr;
118 uint64_t stream_id = 0;
119 /* Weak reference. */
120 struct ctf_metadata_decoder *decoder = nullptr;
123 enum lttng_live_metadata_stream_state
126 * The metadata needs to be updated. This is either because we just
127 * created the trace and haven't asked yet, or the relay specifically
128 * told us that new metadata is available.
130 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
132 * The metadata was updated and the relay has not told us we need to
135 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
137 * The relay has closed this metadata stream. We set this in reaction
138 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
139 * command to the relay. If this field is set, we have received all the
140 * metadata that we are ever going to get for that metadata stream.
142 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
145 struct lttng_live_trace
147 bt_logging_level log_level = (bt_logging_level) 0;
148 bt_self_component *self_comp = nullptr;
150 /* Back reference to session. */
151 struct lttng_live_session *session = nullptr;
153 /* ctf trace ID within the session. */
157 bt_trace *trace = nullptr;
159 /* Weak reference. */
160 bt_trace_class *trace_class = nullptr;
162 struct lttng_live_metadata *metadata = nullptr;
164 const bt_clock_class *clock_class = nullptr;
166 /* Array of pointers to struct lttng_live_stream_iterator. */
168 GPtrArray *stream_iterators = nullptr;
170 enum lttng_live_metadata_stream_state metadata_stream_state =
171 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
174 struct lttng_live_session
176 bt_logging_level log_level = (bt_logging_level) 0;
177 bt_self_component *self_comp = nullptr;
179 /* Weak reference. */
180 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
183 GString *hostname = nullptr;
186 GString *session_name = nullptr;
190 /* Array of pointers to struct lttng_live_trace. */
191 GPtrArray *traces = nullptr;
193 bool attached = false;
194 bool new_streams_needed = false;
195 bool lazy_stream_msg_init = false;
199 enum session_not_found_action
201 SESSION_NOT_FOUND_ACTION_CONTINUE,
202 SESSION_NOT_FOUND_ACTION_FAIL,
203 SESSION_NOT_FOUND_ACTION_END,
207 * A component instance is an iterator on a single session.
209 struct lttng_live_component
211 bt_logging_level log_level = (bt_logging_level) 0;
213 /* Weak reference. */
214 bt_self_component *self_comp = nullptr;
218 GString *url = nullptr;
219 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
222 size_t max_query_size = 0;
225 * Keeps track of whether the downstream component already has a
226 * message iterator on this component.
228 bool has_msg_iter = false;
231 struct lttng_live_msg_iter
233 bt_logging_level log_level = (bt_logging_level) 0;
234 bt_self_component *self_comp = nullptr;
236 /* Weak reference. */
237 struct lttng_live_component *lttng_live_comp = nullptr;
239 /* Weak reference. */
240 bt_self_message_iterator *self_msg_iter = nullptr;
243 struct live_viewer_connection *viewer_connection = nullptr;
245 /* Array of pointers to struct lttng_live_session. */
246 GPtrArray *sessions = nullptr;
248 /* Number of live stream iterator this message iterator has.*/
249 uint64_t active_stream_iter = 0;
251 /* Timestamp in nanosecond of the last message sent downstream. */
252 int64_t last_msg_ts_ns = 0;
254 /* True if the iterator was interrupted. */
255 bool was_interrupted = false;
258 enum lttng_live_iterator_status
260 /** Iterator state has progressed. Continue iteration immediately. */
261 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
262 /** No message available for now. Try again later. */
263 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
264 /** No more CTF_LTTNG_LIVEs to be delivered. */
265 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
266 /** No error, okay. */
267 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
268 /** Invalid arguments. */
269 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
270 /** General error. */
271 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
272 /** Out of memory. */
273 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
274 /** Unsupported iterator feature. */
275 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
278 bt_component_class_initialize_method_status
279 lttng_live_component_init(bt_self_component_source *self_comp,
280 bt_self_component_source_configuration *config, const bt_value *params,
281 void *init_method_data);
283 bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
284 bt_private_query_executor *priv_query_exec,
285 const char *object, const bt_value *params,
286 void *method_data, const bt_value **result);
288 void lttng_live_component_finalize(bt_self_component_source *component);
290 bt_message_iterator_class_next_method_status
291 lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
292 uint64_t capacity, uint64_t *count);
294 bt_message_iterator_class_initialize_method_status
295 lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
296 bt_self_message_iterator_configuration *config,
297 bt_self_component_port_output *self_port);
299 void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
301 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
302 bt_self_message_iterator *self_msg_iter);
304 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
306 enum lttng_live_iterator_status
307 lttng_live_session_get_new_streams(struct lttng_live_session *session,
308 bt_self_message_iterator *self_msg_iter);
310 struct lttng_live_trace *
311 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
314 int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
315 const char *hostname, const char *session_name);
318 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
319 * If new metadata is received, the function writes it to the provided file
320 * handle and updates the reply_len output parameter. This function should be
321 * called in loop until _END status is received to ensure all metadata is
322 * written to the file.
324 enum lttng_live_get_one_metadata_status
325 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, size_t *reply_len);
327 enum lttng_live_iterator_status
328 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
329 struct lttng_live_stream_iterator *stream, struct packet_index *index);
331 enum ctf_msg_iter_medium_status
332 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
333 struct lttng_live_stream_iterator *stream, uint8_t *buf,
334 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
336 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
338 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
339 enum lttng_live_stream_state new_state);
341 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */