src.ctf.lttng-live: introduce and use lttng_live_stream_iterator::UP
[babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.hpp
CommitLineData
14f28187 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
14f28187
FD
3 *
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>
7 *
0235b0db 8 * BabelTrace - LTTng-live client Component
14f28187
FD
9 */
10
0235b0db
MJ
11#ifndef BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H
12#define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H
13
c802cacb 14#include <glib.h>
3c22a242
FD
15#include <stdint.h>
16
3fadfbc0 17#include <babeltrace2/babeltrace.h>
14f28187 18
35e432d7 19#include "cpp-common/bt2/message.hpp"
0f5c5d5c
SM
20#include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
21
5656cea5
PP
22#include "../common/src/metadata/tsdl/decoder.hpp"
23#include "../common/src/msg-iter/msg-iter.hpp"
087cd0f5 24#include "viewer-connection.hpp"
14f28187 25
4164020e
SM
26enum lttng_live_stream_state
27{
28 /* This stream won't have data until some known time in the future. */
29 LTTNG_LIVE_STREAM_QUIESCENT,
30 /*
31 * This stream won't have data until some known time in the future and
32 * the message iterator inactivity message was already sent downstream.
33 */
34 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */
35 /* This stream has data ready to be consumed. */
36 LTTNG_LIVE_STREAM_ACTIVE_DATA,
37 /*
38 * This stream has no data left to consume. We should asked the relay
39 * for more.
40 */
41 LTTNG_LIVE_STREAM_ACTIVE_NO_DATA,
42 /* This stream won't have anymore data, ever. */
43 LTTNG_LIVE_STREAM_EOF,
14f28187
FD
44};
45
0f5c5d5c
SM
46inline const char *format_as(const lttng_live_stream_state state) noexcept
47{
48 switch (state) {
49 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA:
50 return "ACTIVE_NO_DATA";
51
52 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA:
53 return "QUIESCENT_NO_DATA";
54
55 case LTTNG_LIVE_STREAM_QUIESCENT:
56 return "QUIESCENT";
57
58 case LTTNG_LIVE_STREAM_ACTIVE_DATA:
59 return "ACTIVE_DATA";
60
61 case LTTNG_LIVE_STREAM_EOF:
62 return "EOF";
63 }
64
65 bt_common_abort();
66}
67
14f28187 68/* Iterator over a live stream. */
4164020e
SM
69struct lttng_live_stream_iterator
70{
6eeff3ca
SM
71 using UP = std::unique_ptr<lttng_live_stream_iterator>;
72
0f5c5d5c
SM
73 explicit lttng_live_stream_iterator(const bt2c::Logger& parentLogger) :
74 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/STREAM-ITER"}
75 {
76 }
77
ce4ee876
SM
78 ~lttng_live_stream_iterator();
79
0f5c5d5c 80 bt2c::Logger logger;
4164020e 81
0b68f2bc 82 bt2::Stream::Shared stream;
4164020e
SM
83
84 /* Weak reference. */
afb0f12b 85 struct lttng_live_trace *trace = nullptr;
4164020e
SM
86
87 /*
88 * Since only a single iterator per viewer connection, we have
89 * only a single message iterator per stream.
90 */
2d5d8c7b 91 ctf_msg_iter_up msg_iter;
4164020e 92
afb0f12b 93 uint64_t viewer_stream_id = 0;
4164020e
SM
94
95 struct
96 {
afb0f12b
SM
97 bool is_set = false;
98 uint64_t value = 0;
4164020e
SM
99 } ctf_stream_class_id;
100
101 /* base offset in current index. */
afb0f12b 102 uint64_t base_offset = 0;
4164020e 103 /* len to read in current index. */
afb0f12b 104 uint64_t len = 0;
4164020e 105 /* offset in current index. */
afb0f12b 106 uint64_t offset = 0;
4164020e
SM
107
108 /*
109 * Clock Snapshot value of the last message iterator inactivity message
110 * sent downstream.
111 */
112 struct
113 {
afb0f12b
SM
114 bool is_set = false;
115 uint64_t value = 0;
4164020e
SM
116 } last_inactivity_ts;
117
118 /*
119 * Clock Snapshot value of the current message iterator inactivity
120 * message we might want to send downstream.
121 */
afb0f12b 122 uint64_t current_inactivity_ts = 0;
4164020e 123
afb0f12b 124 enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT;
4164020e 125
35e432d7
SM
126 /* The current message produced by this live stream iterator. */
127 bt2::ConstMessage::Shared current_msg;
4164020e
SM
128
129 /* Timestamp in nanoseconds of the current message (current_msg). */
afb0f12b 130 int64_t current_msg_ts_ns = 0;
4164020e 131
afbf59c4 132 std::vector<uint8_t> buf;
4164020e 133
e28ca558 134 std::string name;
4164020e 135
afb0f12b 136 bool has_stream_hung_up = false;
14f28187
FD
137};
138
4164020e
SM
139struct lttng_live_metadata
140{
e66be7c3
SM
141 using UP = std::unique_ptr<lttng_live_metadata>;
142
0f5c5d5c
SM
143 explicit lttng_live_metadata(const bt2c::Logger& parentLogger) :
144 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/METADATA"}
145 {
146 }
147
148 bt2c::Logger logger;
c01594de 149
afb0f12b 150 uint64_t stream_id = 0;
1fa280c9 151
4164020e 152 /* Weak reference. */
1fa280c9 153 ctf_metadata_decoder_up decoder;
14f28187
FD
154};
155
4164020e
SM
156enum lttng_live_metadata_stream_state
157{
158 /*
159 * The metadata needs to be updated. This is either because we just
160 * created the trace and haven't asked yet, or the relay specifically
161 * told us that new metadata is available.
162 */
163 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED,
164 /*
165 * The metadata was updated and the relay has not told us we need to
166 * update it yet.
167 */
168 LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED,
169 /*
170 * The relay has closed this metadata stream. We set this in reaction
171 * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA
172 * command to the relay. If this field is set, we have received all the
173 * metadata that we are ever going to get for that metadata stream.
174 */
175 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED,
76bbaebc
FD
176};
177
4164020e
SM
178struct lttng_live_trace
179{
0f5c5d5c
SM
180 explicit lttng_live_trace(const bt2c::Logger& parentLogger) :
181 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/TRACE"}
182 {
183 }
184
185 bt2c::Logger logger;
c01594de 186
4164020e 187 /* Back reference to session. */
afb0f12b 188 struct lttng_live_session *session = nullptr;
14f28187 189
4164020e 190 /* ctf trace ID within the session. */
afb0f12b 191 uint64_t id = 0;
14f28187 192
7916e7eb 193 bt2::Trace::Shared trace;
14f28187 194
a52f1f2e 195 bt2::TraceClass::Shared trace_class;
14f28187 196
e66be7c3 197 lttng_live_metadata::UP metadata;
14f28187 198
afb0f12b 199 const bt_clock_class *clock_class = nullptr;
14f28187 200
4164020e
SM
201 /* Array of pointers to struct lttng_live_stream_iterator. */
202 /* Owned by this. */
afb0f12b 203 GPtrArray *stream_iterators = nullptr;
14f28187 204
afb0f12b
SM
205 enum lttng_live_metadata_stream_state metadata_stream_state =
206 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
14f28187
FD
207};
208
4164020e
SM
209struct lttng_live_session
210{
0f5c5d5c
SM
211 explicit lttng_live_session(const bt2c::Logger& parentLogger) :
212 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/SESSION"}
213 {
214 }
215
216 bt2c::Logger logger;
217
afb0f12b 218 bt_self_component *self_comp = nullptr;
c01594de 219
4164020e 220 /* Weak reference. */
afb0f12b 221 struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr;
14f28187 222
4164020e 223 /* Owned by this. */
afb0f12b 224 GString *hostname = nullptr;
14f28187 225
4164020e 226 /* Owned by this. */
afb0f12b 227 GString *session_name = nullptr;
14f28187 228
afb0f12b 229 uint64_t id = 0;
14f28187 230
4164020e 231 /* Array of pointers to struct lttng_live_trace. */
afb0f12b 232 GPtrArray *traces = nullptr;
14f28187 233
afb0f12b
SM
234 bool attached = false;
235 bool new_streams_needed = false;
236 bool lazy_stream_msg_init = false;
237 bool closed = false;
14f28187
FD
238};
239
4164020e
SM
240enum session_not_found_action
241{
242 SESSION_NOT_FOUND_ACTION_CONTINUE,
243 SESSION_NOT_FOUND_ACTION_FAIL,
244 SESSION_NOT_FOUND_ACTION_END,
14f28187
FD
245};
246
247/*
248 * A component instance is an iterator on a single session.
249 */
4164020e
SM
250struct lttng_live_component
251{
ef9e5f5d
SM
252 using UP = std::unique_ptr<lttng_live_component>;
253
0f5c5d5c
SM
254 explicit lttng_live_component(bt2c::Logger loggerParam) noexcept :
255 logger {std::move(loggerParam)}
256 {
257 }
258
259 bt2c::Logger logger;
4164020e
SM
260
261 /* Weak reference. */
afb0f12b 262 bt_self_component *self_comp = nullptr;
4164020e
SM
263
264 struct
265 {
2780ec75 266 std::string url;
afb0f12b 267 enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE;
4164020e
SM
268 } params;
269
afb0f12b 270 size_t max_query_size = 0;
4164020e
SM
271
272 /*
273 * Keeps track of whether the downstream component already has a
274 * message iterator on this component.
275 */
afb0f12b 276 bool has_msg_iter = false;
14f28187
FD
277};
278
4164020e
SM
279struct lttng_live_msg_iter
280{
0f5c5d5c
SM
281 explicit lttng_live_msg_iter(const bt2c::Logger& parentLogger) :
282 logger {parentLogger, "PLUGIN/SRC.CTF.LTTNG-LIVE/MSG-ITER"}
283 {
284 }
285
286 bt2c::Logger logger;
287
afb0f12b 288 bt_self_component *self_comp = nullptr;
c01594de 289
4164020e 290 /* Weak reference. */
afb0f12b 291 struct lttng_live_component *lttng_live_comp = nullptr;
14f28187 292
4164020e 293 /* Weak reference. */
afb0f12b 294 bt_self_message_iterator *self_msg_iter = nullptr;
14f28187 295
4164020e 296 /* Owned by this. */
afb0f12b 297 struct live_viewer_connection *viewer_connection = nullptr;
14f28187 298
4164020e 299 /* Array of pointers to struct lttng_live_session. */
afb0f12b 300 GPtrArray *sessions = nullptr;
14f28187 301
4164020e 302 /* Number of live stream iterator this message iterator has.*/
afb0f12b 303 uint64_t active_stream_iter = 0;
14f28187 304
4164020e 305 /* Timestamp in nanosecond of the last message sent downstream. */
afb0f12b 306 int64_t last_msg_ts_ns = 0;
f79c2d7a 307
4164020e 308 /* True if the iterator was interrupted. */
afb0f12b 309 bool was_interrupted = false;
14f28187
FD
310};
311
4164020e
SM
312enum lttng_live_iterator_status
313{
314 /** Iterator state has progressed. Continue iteration immediately. */
315 LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3,
316 /** No message available for now. Try again later. */
317 LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2,
318 /** No more CTF_LTTNG_LIVEs to be delivered. */
319 LTTNG_LIVE_ITERATOR_STATUS_END = 1,
320 /** No error, okay. */
321 LTTNG_LIVE_ITERATOR_STATUS_OK = 0,
322 /** Invalid arguments. */
323 LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1,
324 /** General error. */
325 LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2,
326 /** Out of memory. */
327 LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3,
328 /** Unsupported iterator feature. */
329 LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4,
14f28187
FD
330};
331
0f5c5d5c
SM
332inline const char *format_as(const lttng_live_iterator_status status) noexcept
333{
334 switch (status) {
335 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE:
336 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
337
338 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
339 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
340
341 case LTTNG_LIVE_ITERATOR_STATUS_END:
342 return "LTTNG_LIVE_ITERATOR_STATUS_END";
343
344 case LTTNG_LIVE_ITERATOR_STATUS_OK:
345 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
346
347 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
348 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
349
350 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
351 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
352
353 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
354 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
355
356 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
357 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
358 }
359
360 bt_common_abort();
361}
362
4164020e
SM
363bt_component_class_initialize_method_status
364lttng_live_component_init(bt_self_component_source *self_comp,
365 bt_self_component_source_configuration *config, const bt_value *params,
366 void *init_method_data);
14f28187 367
4164020e
SM
368bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class,
369 bt_private_query_executor *priv_query_exec,
370 const char *object, const bt_value *params,
371 void *method_data, const bt_value **result);
14f28187
FD
372
373void lttng_live_component_finalize(bt_self_component_source *component);
374
4164020e
SM
375bt_message_iterator_class_next_method_status
376lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
377 uint64_t capacity, uint64_t *count);
14f28187 378
4164020e
SM
379bt_message_iterator_class_initialize_method_status
380lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it,
381 bt_self_message_iterator_configuration *config,
382 bt_self_component_port_output *self_port);
14f28187
FD
383
384void lttng_live_msg_iter_finalize(bt_self_message_iterator *it);
385
4164020e
SM
386enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session,
387 bt_self_message_iterator *self_msg_iter);
eee8e741 388
4164020e 389enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session);
f79c2d7a 390
4164020e
SM
391enum lttng_live_iterator_status
392lttng_live_session_get_new_streams(struct lttng_live_session *session,
393 bt_self_message_iterator *self_msg_iter);
14f28187 394
4164020e
SM
395struct lttng_live_trace *
396lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session,
397 uint64_t trace_id);
36e94ad6 398
4164020e
SM
399int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id,
400 const char *hostname, const char *session_name);
14f28187 401
c28512ab
FD
402/*
403 * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata.
404 * If new metadata is received, the function writes it to the provided file
405 * handle and updates the reply_len output parameter. This function should be
406 * called in loop until _END status is received to ensure all metadata is
407 * written to the file.
408 */
4164020e 409enum lttng_live_get_one_metadata_status
15fcc425 410lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<char>& buf);
c28512ab 411
4164020e
SM
412enum lttng_live_iterator_status
413lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
414 struct lttng_live_stream_iterator *stream, struct packet_index *index);
14f28187 415
4164020e
SM
416enum ctf_msg_iter_medium_status
417lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
418 struct lttng_live_stream_iterator *stream, uint8_t *buf,
419 uint64_t offset, uint64_t req_len, uint64_t *recv_len);
14f28187 420
9b4f9b42 421bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter);
14f28187 422
4164020e
SM
423void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter,
424 enum lttng_live_stream_state new_state);
34533ae0 425
14f28187 426#endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */
This page took 0.101713 seconds and 4 git commands to generate.