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
19 #include <babeltrace2/babeltrace.h>
21 #include "common/macros.h"
22 #include "../common/metadata/decoder.h"
23 #include "../common/msg-iter/msg-iter.h"
24 #include "viewer-connection.h"
26 struct lttng_live_component
;
27 struct lttng_live_session
;
28 struct lttng_live_msg_iter
;
30 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
{
51 bt_logging_level log_level
;
52 bt_self_component
*self_comp
;
58 struct lttng_live_trace
*trace
;
61 * Since only a single iterator per viewer connection, we have
62 * only a single message iterator per stream.
64 struct ctf_msg_iter
*msg_iter
;
66 uint64_t viewer_stream_id
;
68 uint64_t ctf_stream_class_id
;
70 /* base offset in current index. */
72 /* len to read in current index. */
74 /* offset in current index. */
78 * Clock Snapshot value of the last message iterator inactivity message
81 uint64_t last_inactivity_ts
;
84 * Clock Snapshot value of the current message iterator inactivity
85 * message we might want to send downstream.
87 uint64_t current_inactivity_ts
;
89 enum lttng_live_stream_state state
;
92 * The current message produced by this live stream iterator. Owned by
95 const bt_message
*current_msg
;
97 /* Timestamp in nanoseconds of the current message (current_msg). */
98 int64_t current_msg_ts_ns
;
107 bool has_stream_hung_up
;
110 struct lttng_live_metadata
{
111 bt_logging_level log_level
;
112 bt_self_component
*self_comp
;
115 /* Weak reference. */
116 struct ctf_metadata_decoder
*decoder
;
119 enum lttng_live_metadata_stream_state
{
121 * The metadata needs to be updated. This is either because we just
122 * created the trace and haven't asked yet, or the relay specifically
123 * told us that new metadata is available.
125 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
,
127 * The metadata was updated and the relay has not told us we need to
130 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
,
132 * The relay has closed this metadata stream. We set this in reaction
133 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
134 * command to the relay. If this field is set, we have received all the
135 * metadata that we are ever going to get for that metadata stream.
137 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
,
140 struct lttng_live_trace
{
141 bt_logging_level log_level
;
142 bt_self_component
*self_comp
;
144 /* Back reference to session. */
145 struct lttng_live_session
*session
;
147 /* ctf trace ID within the session. */
153 /* Weak reference. */
154 bt_trace_class
*trace_class
;
156 struct lttng_live_metadata
*metadata
;
158 const bt_clock_class
*clock_class
;
160 /* Array of pointers to struct lttng_live_stream_iterator. */
162 GPtrArray
*stream_iterators
;
164 enum lttng_live_metadata_stream_state metadata_stream_state
;
167 struct lttng_live_session
{
168 bt_logging_level log_level
;
169 bt_self_component
*self_comp
;
171 /* Weak reference. */
172 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
178 GString
*session_name
;
182 /* Array of pointers to struct lttng_live_trace. */
186 bool new_streams_needed
;
187 bool lazy_stream_msg_init
;
191 enum session_not_found_action
{
192 SESSION_NOT_FOUND_ACTION_CONTINUE
,
193 SESSION_NOT_FOUND_ACTION_FAIL
,
194 SESSION_NOT_FOUND_ACTION_END
,
198 * A component instance is an iterator on a single session.
200 struct lttng_live_component
{
201 bt_logging_level log_level
;
203 /* Weak reference. */
204 bt_self_component
*self_comp
;
208 enum session_not_found_action sess_not_found_act
;
211 size_t max_query_size
;
214 * Keeps track of whether the downstream component already has a
215 * message iterator on this component.
220 struct lttng_live_msg_iter
{
221 bt_logging_level log_level
;
222 bt_self_component
*self_comp
;
224 /* Weak reference. */
225 struct lttng_live_component
*lttng_live_comp
;
227 /* Weak reference. */
228 bt_self_message_iterator
*self_msg_iter
;
231 struct live_viewer_connection
*viewer_connection
;
233 /* Array of pointers to struct lttng_live_session. */
236 /* Number of live stream iterator this message iterator has.*/
237 uint64_t active_stream_iter
;
239 /* Timestamp in nanosecond of the last message sent downstream. */
240 int64_t last_msg_ts_ns
;
242 /* True if the iterator was interrupted. */
243 bool was_interrupted
;
246 enum lttng_live_iterator_status
{
247 /** Iterator state has progressed. Continue iteration immediately. */
248 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
= 3,
249 /** No message available for now. Try again later. */
250 LTTNG_LIVE_ITERATOR_STATUS_AGAIN
= 2,
251 /** No more CTF_LTTNG_LIVEs to be delivered. */
252 LTTNG_LIVE_ITERATOR_STATUS_END
= 1,
253 /** No error, okay. */
254 LTTNG_LIVE_ITERATOR_STATUS_OK
= 0,
255 /** Invalid arguments. */
256 LTTNG_LIVE_ITERATOR_STATUS_INVAL
= -1,
257 /** General error. */
258 LTTNG_LIVE_ITERATOR_STATUS_ERROR
= -2,
259 /** Out of memory. */
260 LTTNG_LIVE_ITERATOR_STATUS_NOMEM
= -3,
261 /** Unsupported iterator feature. */
262 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
= -4,
265 bt_component_class_initialize_method_status
lttng_live_component_init(
266 bt_self_component_source
*self_comp
,
267 bt_self_component_source_configuration
*config
,
268 const bt_value
*params
, void *init_method_data
);
270 bt_component_class_query_method_status
lttng_live_query(
271 bt_self_component_class_source
*comp_class
,
272 bt_private_query_executor
*priv_query_exec
,
273 const char *object
, const bt_value
*params
,
274 void *method_data
, const bt_value
**result
);
276 void lttng_live_component_finalize(bt_self_component_source
*component
);
278 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
279 bt_self_message_iterator
*iterator
,
280 bt_message_array_const msgs
, uint64_t capacity
,
283 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
284 bt_self_message_iterator
*self_msg_it
,
285 bt_self_message_iterator_configuration
*config
,
286 bt_self_component_port_output
*self_port
);
288 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*it
);
290 enum lttng_live_viewer_status
lttng_live_session_attach(
291 struct lttng_live_session
*session
,
292 bt_self_message_iterator
*self_msg_iter
);
294 enum lttng_live_viewer_status
lttng_live_session_detach(
295 struct lttng_live_session
*session
);
297 enum lttng_live_iterator_status
lttng_live_session_get_new_streams(
298 struct lttng_live_session
*session
,
299 bt_self_message_iterator
*self_msg_iter
);
301 struct lttng_live_trace
*lttng_live_session_borrow_or_create_trace_by_id(
302 struct lttng_live_session
*session
, uint64_t trace_id
);
304 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
306 const char *hostname
,
307 const char *session_name
);
310 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
311 * If new metadata is received, the function writes it to the provided file
312 * handle and updates the reply_len output parameter. This function should be
313 * called in loop until _END status is received to ensure all metadata is
314 * written to the file.
316 enum lttng_live_get_one_metadata_status
lttng_live_get_one_metadata_packet(
317 struct lttng_live_trace
*trace
, FILE *fp
, size_t *reply_len
);
319 enum lttng_live_iterator_status
lttng_live_get_next_index(
320 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
321 struct lttng_live_stream_iterator
*stream
,
322 struct packet_index
*index
);
324 enum ctf_msg_iter_medium_status
lttng_live_get_stream_bytes(
325 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
326 struct lttng_live_stream_iterator
*stream
, uint8_t *buf
,
327 uint64_t offset
, uint64_t req_len
, uint64_t *recv_len
);
329 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
);
331 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */