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