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