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