1 #ifndef BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H
2 #define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H
5 * BabelTrace - LTTng-live client Component
7 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
8 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #include <babeltrace2/babeltrace.h>
39 #include "common/macros.h"
40 #include "../common/metadata/decoder.h"
41 #include "../common/msg-iter/msg-iter.h"
42 #include "viewer-connection.h"
44 struct lttng_live_component
;
45 struct lttng_live_session
;
46 struct lttng_live_msg_iter
;
48 enum lttng_live_stream_state
{
49 /* This stream won't have data until some known time in the future. */
50 LTTNG_LIVE_STREAM_QUIESCENT
,
52 * This stream won't have data until some known time in the future and
53 * the message iterator inactivity message was already sent downstream.
55 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
, /* */
56 /* This stream has data ready to be consumed. */
57 LTTNG_LIVE_STREAM_ACTIVE_DATA
,
59 * This stream has no data left to consume. We should asked the relay
62 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
,
63 /* This stream won't have anymore data, ever. */
64 LTTNG_LIVE_STREAM_EOF
,
67 /* Iterator over a live stream. */
68 struct lttng_live_stream_iterator
{
69 bt_logging_level log_level
;
70 bt_self_component
*self_comp
;
76 struct lttng_live_trace
*trace
;
79 * Since only a single iterator per viewer connection, we have
80 * only a single message iterator per stream.
82 struct bt_msg_iter
*msg_iter
;
84 uint64_t viewer_stream_id
;
86 uint64_t ctf_stream_class_id
;
88 /* base offset in current index. */
90 /* len to read in current index. */
92 /* offset in current index. */
96 * Clock Snapshot value of the last message iterator inactivity message
99 uint64_t last_inactivity_ts
;
102 * Clock Snapshot value of the current message iterator inactivity
103 * message we might want to send downstream.
105 uint64_t current_inactivity_ts
;
107 enum lttng_live_stream_state state
;
110 * The current message produced by this live stream iterator. Owned by
113 bt_message
*current_msg
;
115 /* Timestamp in nanoseconds of the current message (current_msg). */
116 int64_t current_msg_ts_ns
;
125 bool has_stream_hung_up
;
128 struct lttng_live_metadata
{
129 bt_logging_level log_level
;
130 bt_self_component
*self_comp
;
132 /* Weak reference. */
133 struct lttng_live_trace
*trace
;
136 /* Weak reference. */
137 struct ctf_metadata_decoder
*decoder
;
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 bool new_metadata_needed
;
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
;
243 enum lttng_live_iterator_status
{
244 /** Iterator state has progressed. Continue iteration immediately. */
245 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
= 3,
246 /** No message available for now. Try again later. */
247 LTTNG_LIVE_ITERATOR_STATUS_AGAIN
= 2,
248 /** No more CTF_LTTNG_LIVEs to be delivered. */
249 LTTNG_LIVE_ITERATOR_STATUS_END
= 1,
250 /** No error, okay. */
251 LTTNG_LIVE_ITERATOR_STATUS_OK
= 0,
252 /** Invalid arguments. */
253 LTTNG_LIVE_ITERATOR_STATUS_INVAL
= -1,
254 /** General error. */
255 LTTNG_LIVE_ITERATOR_STATUS_ERROR
= -2,
256 /** Out of memory. */
257 LTTNG_LIVE_ITERATOR_STATUS_NOMEM
= -3,
258 /** Unsupported iterator feature. */
259 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
= -4,
262 bt_component_class_initialize_method_status
lttng_live_component_init(
263 bt_self_component_source
*self_comp
,
264 bt_self_component_source_configuration
*config
,
265 const bt_value
*params
, void *init_method_data
);
267 bt_component_class_query_method_status
lttng_live_query(
268 bt_self_component_class_source
*comp_class
,
269 bt_private_query_executor
*priv_query_exec
,
270 const char *object
, const bt_value
*params
,
271 void *method_data
, const bt_value
**result
);
273 void lttng_live_component_finalize(bt_self_component_source
*component
);
275 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
276 bt_self_message_iterator
*iterator
,
277 bt_message_array_const msgs
, uint64_t capacity
,
280 bt_component_class_message_iterator_initialize_method_status
lttng_live_msg_iter_init(
281 bt_self_message_iterator
*self_msg_it
,
282 bt_self_message_iterator_configuration
*config
,
283 bt_self_component_source
*self_comp
,
284 bt_self_component_port_output
*self_port
);
286 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*it
);
288 int lttng_live_create_viewer_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
);
290 enum lttng_live_attach_session_status
{
291 LTTNG_LIVE_ATTACH_SESSION_STATUS_OK
= 0,
292 LTTNG_LIVE_ATTACH_SESSION_STATUS_ERROR
= -2,
295 enum lttng_live_attach_session_status
lttng_live_attach_session(
296 struct lttng_live_session
*session
);
298 int lttng_live_detach_session(struct lttng_live_session
*session
);
299 enum lttng_live_iterator_status
lttng_live_get_new_streams(
300 struct lttng_live_session
*session
);
302 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
304 const char *hostname
,
305 const char *session_name
);
307 enum lttng_live_get_one_metadata_status
{
308 /* The end of the metadata stream was reached. */
309 LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
= 1,
310 /* One metadata packet was received and written to file. */
311 LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
= 0,
312 /* The metadata stream was not found on the relay. */
313 LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
= -1,
315 * A critical error occurred when contacting the relay or while
316 * handling its response.
318 LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
= -2,
322 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
323 * If new metadata is received, the function writes it to the provided file
324 * handle and updates the reply_len output parameter. This function should be
325 * called in loop until _END status is received to ensure all metadata is
326 * written to the file.
328 enum lttng_live_get_one_metadata_status
lttng_live_get_one_metadata_packet(
329 struct lttng_live_trace
*trace
, FILE *fp
, size_t *reply_len
);
331 enum lttng_live_iterator_status
lttng_live_get_next_index(
332 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
333 struct lttng_live_stream_iterator
*stream
,
334 struct packet_index
*index
);
336 enum bt_msg_iter_medium_status
lttng_live_get_stream_bytes(
337 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
);
340 void lttng_live_add_stream_iterator(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
341 struct lttng_live_stream_iterator
*stream_iter
);
342 void lttng_live_remove_stream_iterator(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
343 struct lttng_live_stream_iterator
*stream_iter
);
345 struct lttng_live_trace
*lttng_live_borrow_trace(
346 struct lttng_live_session
*session
, uint64_t trace_id
);
347 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
);
349 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
);
351 #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */