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