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