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