Commit | Line | Data |
---|---|---|
f3bc2010 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
f3bc2010 | 3 | * |
14f28187 | 4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
f3bc2010 | 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7cdc2bab | 6 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f3bc2010 | 7 | * |
0235b0db | 8 | * Babeltrace CTF LTTng-live Client Component |
f3bc2010 JG |
9 | */ |
10 | ||
c802cacb | 11 | #include <glib.h> |
14f28187 FD |
12 | #include <unistd.h> |
13 | ||
578e048b | 14 | #include "common/assert.h" |
0f5c5d5c | 15 | #include "cpp-common/bt2c/fmt.hpp" |
6838d8aa | 16 | #include "cpp-common/bt2c/glib-up.hpp" |
ef9e5f5d | 17 | #include "cpp-common/bt2s/make-unique.hpp" |
0f5c5d5c | 18 | #include "cpp-common/vendor/fmt/format.h" |
f3bc2010 | 19 | |
376fc2bd | 20 | #include "plugins/common/muxing/muxing.h" |
80aff5ef | 21 | #include "plugins/common/param-validation/param-validation.h" |
376fc2bd | 22 | |
087cd0f5 | 23 | #include "data-stream.hpp" |
087cd0f5 | 24 | #include "lttng-live.hpp" |
c802cacb | 25 | #include "metadata.hpp" |
7cdc2bab | 26 | |
4164020e SM |
27 | #define MAX_QUERY_SIZE (256 * 1024) |
28 | #define URL_PARAM "url" | |
29 | #define INPUTS_PARAM "inputs" | |
30 | #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action" | |
31 | #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue" | |
32 | #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail" | |
33 | #define SESS_NOT_FOUND_ACTION_END_STR "end" | |
7cdc2bab | 34 | |
34533ae0 | 35 | void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter, |
4164020e | 36 | enum lttng_live_stream_state new_state) |
34533ae0 | 37 | { |
0f5c5d5c SM |
38 | BT_CPPLOGD_SPEC(stream_iter->logger, |
39 | "Setting live stream iterator state: viewer-stream-id={}, " | |
40 | "old-state={}, new-state={}", | |
41 | stream_iter->viewer_stream_id, stream_iter->state, new_state); | |
34533ae0 | 42 | |
4164020e | 43 | stream_iter->state = new_state; |
34533ae0 FD |
44 | } |
45 | ||
4164020e SM |
46 | #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \ |
47 | do { \ | |
0f5c5d5c SM |
48 | BT_CPPLOGD_SPEC((live_stream_iter)->logger, \ |
49 | "Live stream iterator state={}, " \ | |
50 | "last-inact-ts-is-set={}, last-inact-ts-value={}, " \ | |
51 | "curr-inact-ts={}", \ | |
52 | (live_stream_iter)->state, (live_stream_iter)->last_inactivity_ts.is_set, \ | |
53 | (live_stream_iter)->last_inactivity_ts.value, \ | |
54 | (live_stream_iter)->current_inactivity_ts); \ | |
4164020e | 55 | } while (0); |
6f79a7cf | 56 | |
9b4f9b42 | 57 | bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter) |
6f79a7cf | 58 | { |
4164020e | 59 | bool ret; |
6f79a7cf | 60 | |
4164020e SM |
61 | if (!msg_iter) { |
62 | ret = false; | |
63 | goto end; | |
64 | } | |
7cdc2bab | 65 | |
4164020e | 66 | ret = bt_self_message_iterator_is_interrupted(msg_iter->self_msg_iter); |
4bf0e537 | 67 | |
14f28187 | 68 | end: |
4164020e | 69 | return ret; |
7cdc2bab MD |
70 | } |
71 | ||
4164020e SM |
72 | static struct lttng_live_trace * |
73 | lttng_live_session_borrow_trace_by_id(struct lttng_live_session *session, uint64_t trace_id) | |
d3e4dcd8 | 74 | { |
4164020e SM |
75 | uint64_t trace_idx; |
76 | struct lttng_live_trace *ret_trace = NULL; | |
77 | ||
78 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
79 | struct lttng_live_trace *trace = | |
80 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
81 | if (trace->id == trace_id) { | |
82 | ret_trace = trace; | |
83 | goto end; | |
84 | } | |
85 | } | |
14f28187 FD |
86 | |
87 | end: | |
4164020e | 88 | return ret_trace; |
d3eb6e8f PP |
89 | } |
90 | ||
4164020e | 91 | static void lttng_live_destroy_trace(struct lttng_live_trace *trace) |
7cdc2bab | 92 | { |
0f5c5d5c | 93 | BT_CPPLOGD_SPEC(trace->logger, "Destroying live trace: trace-id={}", trace->id); |
7cdc2bab | 94 | |
4164020e SM |
95 | BT_ASSERT(trace->stream_iterators); |
96 | g_ptr_array_free(trace->stream_iterators, TRUE); | |
5bd230f4 | 97 | |
4164020e | 98 | lttng_live_metadata_fini(trace); |
afb0f12b | 99 | delete trace; |
7cdc2bab MD |
100 | } |
101 | ||
4164020e SM |
102 | static struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_session *session, |
103 | uint64_t trace_id) | |
7cdc2bab | 104 | { |
0f5c5d5c SM |
105 | BT_CPPLOGD_SPEC(session->logger, "Creating live trace: session-id={}, trace-id={}", session->id, |
106 | trace_id); | |
4164020e | 107 | |
0f5c5d5c | 108 | lttng_live_trace *trace = new lttng_live_trace {session->logger}; |
4164020e SM |
109 | trace->session = session; |
110 | trace->id = trace_id; | |
4164020e SM |
111 | trace->stream_iterators = |
112 | g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_stream_iterator_destroy); | |
113 | BT_ASSERT(trace->stream_iterators); | |
114 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; | |
115 | g_ptr_array_add(session->traces, trace); | |
116 | ||
4164020e | 117 | return trace; |
7cdc2bab MD |
118 | } |
119 | ||
4164020e SM |
120 | struct lttng_live_trace * |
121 | lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session, | |
122 | uint64_t trace_id) | |
7cdc2bab | 123 | { |
4164020e | 124 | struct lttng_live_trace *trace; |
7cdc2bab | 125 | |
4164020e SM |
126 | trace = lttng_live_session_borrow_trace_by_id(session, trace_id); |
127 | if (trace) { | |
128 | goto end; | |
129 | } | |
7cdc2bab | 130 | |
4164020e SM |
131 | /* The session is the owner of the newly created trace. */ |
132 | trace = lttng_live_create_trace(session, trace_id); | |
7cdc2bab | 133 | |
14f28187 | 134 | end: |
4164020e | 135 | return trace; |
7cdc2bab MD |
136 | } |
137 | ||
4164020e SM |
138 | int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id, |
139 | const char *hostname, const char *session_name) | |
7cdc2bab | 140 | { |
0f5c5d5c SM |
141 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
142 | "Adding live session: " | |
143 | "session-id={}, hostname=\"{}\", session-name=\"{}\"", | |
144 | session_id, hostname, session_name); | |
4164020e | 145 | |
0f5c5d5c | 146 | lttng_live_session *session = new lttng_live_session {lttng_live_msg_iter->logger}; |
4164020e SM |
147 | session->self_comp = lttng_live_msg_iter->self_comp; |
148 | session->id = session_id; | |
149 | session->traces = g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_destroy_trace); | |
150 | BT_ASSERT(session->traces); | |
151 | session->lttng_live_msg_iter = lttng_live_msg_iter; | |
152 | session->new_streams_needed = true; | |
153 | session->hostname = g_string_new(hostname); | |
154 | BT_ASSERT(session->hostname); | |
155 | ||
156 | session->session_name = g_string_new(session_name); | |
157 | BT_ASSERT(session->session_name); | |
158 | ||
159 | g_ptr_array_add(lttng_live_msg_iter->sessions, session); | |
afb0f12b SM |
160 | |
161 | return 0; | |
7cdc2bab MD |
162 | } |
163 | ||
4164020e | 164 | static void lttng_live_destroy_session(struct lttng_live_session *session) |
7cdc2bab | 165 | { |
4164020e SM |
166 | if (!session) { |
167 | goto end; | |
168 | } | |
169 | ||
0f5c5d5c SM |
170 | BT_CPPLOGD_SPEC(session->logger, |
171 | "Destroying live session: " | |
172 | "session-id={}, session-name=\"{}\"", | |
173 | session->id, session->session_name->str); | |
4164020e SM |
174 | if (session->id != -1ULL) { |
175 | if (lttng_live_session_detach(session)) { | |
176 | if (!lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) { | |
177 | /* Old relayd cannot detach sessions. */ | |
0f5c5d5c SM |
178 | BT_CPPLOGD_SPEC(session->logger, "Unable to detach lttng live session {}", |
179 | session->id); | |
4164020e SM |
180 | } |
181 | } | |
182 | session->id = -1ULL; | |
183 | } | |
184 | ||
185 | if (session->traces) { | |
186 | g_ptr_array_free(session->traces, TRUE); | |
187 | } | |
188 | ||
189 | if (session->hostname) { | |
190 | g_string_free(session->hostname, TRUE); | |
191 | } | |
afb0f12b | 192 | |
4164020e SM |
193 | if (session->session_name) { |
194 | g_string_free(session->session_name, TRUE); | |
195 | } | |
afb0f12b SM |
196 | |
197 | delete session; | |
14f28187 FD |
198 | |
199 | end: | |
4164020e | 200 | return; |
7cdc2bab MD |
201 | } |
202 | ||
4164020e | 203 | static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter *lttng_live_msg_iter) |
7cdc2bab | 204 | { |
4164020e SM |
205 | if (!lttng_live_msg_iter) { |
206 | goto end; | |
207 | } | |
7cdc2bab | 208 | |
4164020e SM |
209 | if (lttng_live_msg_iter->sessions) { |
210 | g_ptr_array_free(lttng_live_msg_iter->sessions, TRUE); | |
211 | } | |
14f28187 | 212 | |
4164020e SM |
213 | if (lttng_live_msg_iter->viewer_connection) { |
214 | live_viewer_connection_destroy(lttng_live_msg_iter->viewer_connection); | |
215 | } | |
216 | BT_ASSERT(lttng_live_msg_iter->lttng_live_comp); | |
217 | BT_ASSERT(lttng_live_msg_iter->lttng_live_comp->has_msg_iter); | |
14f28187 | 218 | |
4164020e SM |
219 | /* All stream iterators must be destroyed at this point. */ |
220 | BT_ASSERT(lttng_live_msg_iter->active_stream_iter == 0); | |
221 | lttng_live_msg_iter->lttng_live_comp->has_msg_iter = false; | |
14f28187 | 222 | |
afb0f12b | 223 | delete lttng_live_msg_iter; |
14f28187 FD |
224 | |
225 | end: | |
4164020e | 226 | return; |
14f28187 FD |
227 | } |
228 | ||
14f28187 FD |
229 | void lttng_live_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) |
230 | { | |
4164020e | 231 | struct lttng_live_msg_iter *lttng_live_msg_iter; |
14f28187 | 232 | |
4164020e | 233 | BT_ASSERT(self_msg_iter); |
14f28187 | 234 | |
4164020e SM |
235 | lttng_live_msg_iter = |
236 | (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_iter); | |
237 | BT_ASSERT(lttng_live_msg_iter); | |
238 | lttng_live_msg_iter_destroy(lttng_live_msg_iter); | |
7cdc2bab MD |
239 | } |
240 | ||
4164020e SM |
241 | static enum lttng_live_iterator_status |
242 | lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator *lttng_live_stream) | |
7cdc2bab | 243 | { |
4164020e SM |
244 | switch (lttng_live_stream->state) { |
245 | case LTTNG_LIVE_STREAM_QUIESCENT: | |
246 | case LTTNG_LIVE_STREAM_ACTIVE_DATA: | |
247 | break; | |
248 | case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA: | |
249 | /* Invalid state. */ | |
0f5c5d5c | 250 | BT_CPPLOGF_SPEC(lttng_live_stream->logger, "Unexpected stream state \"ACTIVE_NO_DATA\""); |
4164020e SM |
251 | bt_common_abort(); |
252 | case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA: | |
253 | /* Invalid state. */ | |
0f5c5d5c | 254 | BT_CPPLOGF_SPEC(lttng_live_stream->logger, "Unexpected stream state \"QUIESCENT_NO_DATA\""); |
4164020e SM |
255 | bt_common_abort(); |
256 | case LTTNG_LIVE_STREAM_EOF: | |
257 | break; | |
258 | } | |
259 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
7cdc2bab MD |
260 | } |
261 | ||
262 | /* | |
f93afbf9 FD |
263 | * For active no data stream, fetch next index. As a result of that it can |
264 | * become either: | |
265 | * - quiescent: won't have events for a bit, | |
266 | * - have data: need to get that data and produce the event, | |
267 | * - have no data on this stream at this point: need to retry (AGAIN) or return | |
268 | * EOF. | |
7cdc2bab | 269 | */ |
4164020e SM |
270 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stream( |
271 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
272 | struct lttng_live_stream_iterator *lttng_live_stream) | |
7cdc2bab | 273 | { |
4164020e SM |
274 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
275 | enum lttng_live_stream_state orig_state = lttng_live_stream->state; | |
276 | struct packet_index index; | |
277 | ||
278 | if (lttng_live_stream->trace->metadata_stream_state == | |
279 | LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
0f5c5d5c SM |
280 | BT_CPPLOGD_SPEC( |
281 | lttng_live_msg_iter->logger, | |
4164020e | 282 | "Need to get an update for the metadata stream before proceeding further with this stream: " |
0f5c5d5c | 283 | "stream-name=\"{}\"", |
4164020e SM |
284 | lttng_live_stream->name->str); |
285 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
286 | goto end; | |
287 | } | |
288 | ||
289 | if (lttng_live_stream->trace->session->new_streams_needed) { | |
0f5c5d5c SM |
290 | BT_CPPLOGD_SPEC( |
291 | lttng_live_msg_iter->logger, | |
4164020e | 292 | "Need to get an update of all streams before proceeding further with this stream: " |
0f5c5d5c | 293 | "stream-name=\"{}\"", |
4164020e SM |
294 | lttng_live_stream->name->str); |
295 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
296 | goto end; | |
297 | } | |
298 | ||
299 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_NO_DATA && | |
300 | lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA) { | |
301 | goto end; | |
302 | } | |
303 | ret = lttng_live_get_next_index(lttng_live_msg_iter, lttng_live_stream, &index); | |
304 | if (ret != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
305 | goto end; | |
306 | } | |
307 | ||
308 | BT_ASSERT_DBG(lttng_live_stream->state != LTTNG_LIVE_STREAM_EOF); | |
309 | ||
310 | if (lttng_live_stream->state == LTTNG_LIVE_STREAM_QUIESCENT) { | |
311 | uint64_t last_inact_ts = lttng_live_stream->last_inactivity_ts.value, | |
312 | curr_inact_ts = lttng_live_stream->current_inactivity_ts; | |
313 | ||
314 | if (orig_state == LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA && last_inact_ts == curr_inact_ts) { | |
315 | /* | |
5cb27175 JG |
316 | * Because the stream is in the QUIESCENT_NO_DATA |
317 | * state, we can assert that the last_inactivity_ts was | |
318 | * set and can be safely used in the `if` above. | |
319 | */ | |
4164020e SM |
320 | BT_ASSERT(lttng_live_stream->last_inactivity_ts.is_set); |
321 | ||
322 | ret = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
323 | LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream); | |
324 | } else { | |
325 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
326 | } | |
327 | goto end; | |
328 | } | |
329 | ||
330 | lttng_live_stream->base_offset = index.offset; | |
331 | lttng_live_stream->offset = index.offset; | |
332 | lttng_live_stream->len = index.packet_size / CHAR_BIT; | |
333 | ||
0f5c5d5c SM |
334 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
335 | "Setting live stream reading info: stream-name=\"{}\", " | |
336 | "viewer-stream-id={}, stream-base-offset={}, stream-offset={}, stream-len={}", | |
337 | lttng_live_stream->name->str, lttng_live_stream->viewer_stream_id, | |
338 | lttng_live_stream->base_offset, lttng_live_stream->offset, | |
339 | lttng_live_stream->len); | |
f93afbf9 | 340 | |
7cdc2bab | 341 | end: |
4164020e SM |
342 | if (ret == LTTNG_LIVE_ITERATOR_STATUS_OK) { |
343 | ret = lttng_live_iterator_next_check_stream_state(lttng_live_stream); | |
344 | } | |
345 | return ret; | |
7cdc2bab MD |
346 | } |
347 | ||
348 | /* | |
14f28187 FD |
349 | * Creation of the message requires the ctf trace class to be created |
350 | * beforehand, but the live protocol gives us all streams (including metadata) | |
351 | * at once. So we split it in three steps: getting streams, getting metadata | |
352 | * (which creates the ctf trace class), and then creating the per-stream | |
353 | * messages. | |
7cdc2bab | 354 | */ |
4164020e SM |
355 | static enum lttng_live_iterator_status |
356 | lttng_live_get_session(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
357 | struct lttng_live_session *session) | |
7cdc2bab | 358 | { |
4164020e SM |
359 | enum lttng_live_iterator_status status; |
360 | uint64_t trace_idx; | |
361 | ||
362 | if (!session->attached) { | |
0f5c5d5c SM |
363 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, "Attach to session: session-id={}", |
364 | session->id); | |
4164020e SM |
365 | enum lttng_live_viewer_status attach_status = |
366 | lttng_live_session_attach(session, lttng_live_msg_iter->self_msg_iter); | |
367 | if (attach_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
368 | if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) { | |
369 | /* | |
370 | * Clear any causes appended in | |
371 | * `lttng_live_attach_session()` as we want to | |
372 | * return gracefully since the graph was | |
373 | * cancelled. | |
374 | */ | |
375 | bt_current_thread_clear_error(); | |
376 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
377 | } else { | |
378 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
0f5c5d5c SM |
379 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
380 | "Error attaching to LTTng live session"); | |
4164020e SM |
381 | } |
382 | goto end; | |
383 | } | |
384 | } | |
385 | ||
0f5c5d5c SM |
386 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
387 | "Updating all data streams: " | |
388 | "session-id={}, session-name=\"{}\"", | |
389 | session->id, session->session_name->str); | |
4164020e SM |
390 | |
391 | status = lttng_live_session_get_new_streams(session, lttng_live_msg_iter->self_msg_iter); | |
a4f118a3 FD |
392 | switch (status) { |
393 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
394 | break; | |
395 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
396 | /* | |
397 | * We received a `_END` from the `_get_new_streams()` function, | |
398 | * which means no more data will ever be received from the data | |
399 | * streams of this session. But it's possible that the metadata | |
400 | * is incomplete. | |
401 | * The live protocol guarantees that we receive all the | |
402 | * metadata needed before we receive data streams needing it. | |
403 | * But it's possible to receive metadata NOT needed by | |
404 | * data streams after the session was closed. For example, this | |
405 | * could happen if a new event is registered and the session is | |
406 | * stopped before any tracepoint for that event is actually | |
407 | * fired. | |
408 | */ | |
0f5c5d5c SM |
409 | BT_CPPLOGD_SPEC( |
410 | lttng_live_msg_iter->logger, | |
a4f118a3 | 411 | "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:" |
0f5c5d5c | 412 | "session-id={}, session-name=\"{}\"", |
a4f118a3 FD |
413 | session->id, session->session_name->str); |
414 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
415 | break; | |
416 | default: | |
4164020e SM |
417 | goto end; |
418 | } | |
419 | ||
0f5c5d5c SM |
420 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
421 | "Updating metadata stream for session: " | |
422 | "session-id={}, session-name=\"{}\"", | |
423 | session->id, session->session_name->str); | |
a4f118a3 | 424 | |
4164020e SM |
425 | trace_idx = 0; |
426 | while (trace_idx < session->traces->len) { | |
427 | struct lttng_live_trace *trace = | |
428 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
429 | ||
430 | status = lttng_live_metadata_update(trace); | |
431 | switch (status) { | |
432 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
433 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
434 | trace_idx++; | |
435 | break; | |
436 | case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE: | |
437 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
438 | goto end; | |
439 | default: | |
0f5c5d5c SM |
440 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
441 | "Error updating trace metadata: " | |
442 | "stream-iter-status={}, trace-id={}", | |
443 | status, trace->id); | |
4164020e SM |
444 | goto end; |
445 | } | |
446 | } | |
447 | ||
448 | /* | |
449 | * Now that we have the metadata we can initialize the downstream | |
450 | * iterator. | |
451 | */ | |
452 | status = lttng_live_lazy_msg_init(session, lttng_live_msg_iter->self_msg_iter); | |
14f28187 FD |
453 | |
454 | end: | |
4164020e | 455 | return status; |
7cdc2bab MD |
456 | } |
457 | ||
4164020e SM |
458 | static void |
459 | lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter) | |
7cdc2bab | 460 | { |
4164020e | 461 | uint64_t session_idx, trace_idx; |
4164020e SM |
462 | |
463 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
464 | struct lttng_live_session *session = | |
465 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
0f5c5d5c SM |
466 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
467 | "Force marking session as needing new streams: " | |
468 | "session-id={}", | |
469 | session->id); | |
4164020e SM |
470 | session->new_streams_needed = true; |
471 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
472 | struct lttng_live_trace *trace = | |
473 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
0f5c5d5c SM |
474 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
475 | "Force marking trace metadata state as needing an update: " | |
476 | "session-id={}, trace-id={}", | |
477 | session->id, trace->id); | |
4164020e SM |
478 | |
479 | BT_ASSERT(trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED); | |
480 | ||
481 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; | |
482 | } | |
483 | } | |
7cdc2bab MD |
484 | } |
485 | ||
4164020e SM |
486 | static enum lttng_live_iterator_status |
487 | lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter) | |
7cdc2bab | 488 | { |
4164020e SM |
489 | enum lttng_live_iterator_status status; |
490 | enum lttng_live_viewer_status viewer_status; | |
4164020e SM |
491 | uint64_t session_idx = 0, nr_sessions_opened = 0; |
492 | struct lttng_live_session *session; | |
493 | enum session_not_found_action sess_not_found_act = | |
494 | lttng_live_msg_iter->lttng_live_comp->params.sess_not_found_act; | |
495 | ||
0f5c5d5c SM |
496 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
497 | "Update data and metadata of all sessions: " | |
498 | "live-msg-iter-addr={}", | |
499 | fmt::ptr(lttng_live_msg_iter)); | |
4164020e SM |
500 | /* |
501 | * In a remotely distant future, we could add a "new | |
502 | * session" flag to the protocol, which would tell us that we | |
503 | * need to query for new sessions even though we have sessions | |
504 | * currently ongoing. | |
505 | */ | |
506 | if (lttng_live_msg_iter->sessions->len == 0) { | |
507 | if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
0f5c5d5c SM |
508 | BT_CPPLOGD_SPEC( |
509 | lttng_live_msg_iter->logger, | |
4164020e SM |
510 | "No session found. Exiting in accordance with the `session-not-found-action` parameter"); |
511 | status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
512 | goto end; | |
513 | } else { | |
0f5c5d5c SM |
514 | BT_CPPLOGD_SPEC( |
515 | lttng_live_msg_iter->logger, | |
4164020e SM |
516 | "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter"); |
517 | /* | |
518 | * Retry to create a viewer session for the requested | |
519 | * session name. | |
520 | */ | |
521 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); | |
522 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
523 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
524 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
0f5c5d5c SM |
525 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
526 | "Error creating LTTng live viewer session"); | |
4164020e SM |
527 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
528 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
529 | } else { | |
530 | bt_common_abort(); | |
531 | } | |
532 | goto end; | |
533 | } | |
534 | } | |
535 | } | |
536 | ||
537 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
538 | session = | |
539 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
540 | status = lttng_live_get_session(lttng_live_msg_iter, session); | |
541 | switch (status) { | |
542 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
4164020e | 543 | case LTTNG_LIVE_ITERATOR_STATUS_END: |
a4f118a3 FD |
544 | /* |
545 | * A session returned `_END`. Other sessions may still | |
546 | * be active so we override the status and continue | |
547 | * looping if needed. | |
548 | */ | |
4164020e SM |
549 | break; |
550 | default: | |
551 | goto end; | |
552 | } | |
553 | if (!session->closed) { | |
554 | nr_sessions_opened++; | |
555 | } | |
556 | } | |
557 | ||
558 | if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE && nr_sessions_opened == 0) { | |
559 | status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
560 | } else { | |
561 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
562 | } | |
f79c2d7a FD |
563 | |
564 | end: | |
4164020e | 565 | return status; |
7cdc2bab MD |
566 | } |
567 | ||
4164020e SM |
568 | static enum lttng_live_iterator_status |
569 | emit_inactivity_message(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
570 | struct lttng_live_stream_iterator *stream_iter, const bt_message **message, | |
571 | uint64_t timestamp) | |
7cdc2bab | 572 | { |
4164020e | 573 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
4164020e SM |
574 | bt_message *msg = NULL; |
575 | ||
576 | BT_ASSERT(stream_iter->trace->clock_class); | |
577 | ||
0f5c5d5c SM |
578 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
579 | "Emitting inactivity message for stream: ctf-stream-id={}, " | |
580 | "viewer-stream-id={}, timestamp={}", | |
581 | stream_iter->ctf_stream_class_id.value, stream_iter->viewer_stream_id, | |
582 | timestamp); | |
4164020e SM |
583 | |
584 | msg = bt_message_message_iterator_inactivity_create(lttng_live_msg_iter->self_msg_iter, | |
585 | stream_iter->trace->clock_class, timestamp); | |
586 | if (!msg) { | |
0f5c5d5c SM |
587 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
588 | "Error emitting message iterator inactivity message"); | |
4164020e SM |
589 | goto error; |
590 | } | |
591 | ||
592 | *message = msg; | |
7cdc2bab | 593 | end: |
4164020e | 594 | return ret; |
7cdc2bab MD |
595 | |
596 | error: | |
4164020e SM |
597 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
598 | bt_message_put_ref(msg); | |
599 | goto end; | |
7cdc2bab MD |
600 | } |
601 | ||
4164020e SM |
602 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_stream( |
603 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
604 | struct lttng_live_stream_iterator *lttng_live_stream, const bt_message **message) | |
7cdc2bab | 605 | { |
4164020e SM |
606 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
607 | ||
608 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT) { | |
609 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
610 | } | |
611 | ||
612 | /* | |
613 | * Check if we already sent an inactivty message downstream for this | |
614 | * `current_inactivity_ts` value. | |
615 | */ | |
616 | if (lttng_live_stream->last_inactivity_ts.is_set && | |
617 | lttng_live_stream->current_inactivity_ts == lttng_live_stream->last_inactivity_ts.value) { | |
618 | lttng_live_stream_iterator_set_state(lttng_live_stream, | |
619 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA); | |
620 | ||
621 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
622 | goto end; | |
623 | } | |
624 | ||
625 | ret = emit_inactivity_message(lttng_live_msg_iter, lttng_live_stream, message, | |
626 | lttng_live_stream->current_inactivity_ts); | |
627 | ||
628 | lttng_live_stream->last_inactivity_ts.value = lttng_live_stream->current_inactivity_ts; | |
629 | lttng_live_stream->last_inactivity_ts.is_set = true; | |
14f28187 | 630 | end: |
4164020e | 631 | return ret; |
14f28187 FD |
632 | } |
633 | ||
7d91f1ac | 634 | static int live_get_msg_ts_ns(struct lttng_live_msg_iter *lttng_live_msg_iter, |
4164020e | 635 | const bt_message *msg, int64_t last_msg_ts_ns, int64_t *ts_ns) |
14f28187 | 636 | { |
4164020e SM |
637 | const bt_clock_snapshot *clock_snapshot = NULL; |
638 | int ret = 0; | |
4164020e SM |
639 | |
640 | BT_ASSERT_DBG(msg); | |
641 | BT_ASSERT_DBG(ts_ns); | |
642 | ||
0f5c5d5c SM |
643 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
644 | "Getting message's timestamp: iter-data-addr={}, msg-addr={}, " | |
645 | "last-msg-ts={}", | |
646 | fmt::ptr(lttng_live_msg_iter), fmt::ptr(msg), last_msg_ts_ns); | |
4164020e SM |
647 | |
648 | switch (bt_message_get_type(msg)) { | |
649 | case BT_MESSAGE_TYPE_EVENT: | |
4164020e SM |
650 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(msg); |
651 | break; | |
652 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
4164020e SM |
653 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg); |
654 | break; | |
655 | case BT_MESSAGE_TYPE_PACKET_END: | |
4164020e SM |
656 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(msg); |
657 | break; | |
658 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
4164020e SM |
659 | clock_snapshot = |
660 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg); | |
661 | break; | |
662 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
4164020e SM |
663 | clock_snapshot = |
664 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg); | |
665 | break; | |
666 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
667 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg); | |
668 | break; | |
669 | default: | |
670 | /* All the other messages have a higher priority */ | |
0f5c5d5c SM |
671 | BT_CPPLOGD_STR_SPEC(lttng_live_msg_iter->logger, |
672 | "Message has no timestamp: using the last message timestamp."); | |
4164020e SM |
673 | *ts_ns = last_msg_ts_ns; |
674 | goto end; | |
675 | } | |
676 | ||
4164020e SM |
677 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
678 | if (ret) { | |
0f5c5d5c SM |
679 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
680 | "Cannot get nanoseconds from Epoch of clock snapshot: " | |
681 | "clock-snapshot-addr={}", | |
682 | fmt::ptr(clock_snapshot)); | |
4164020e SM |
683 | goto error; |
684 | } | |
685 | ||
686 | goto end; | |
14f28187 | 687 | |
14f28187 | 688 | error: |
4164020e | 689 | ret = -1; |
7cdc2bab | 690 | |
7cdc2bab | 691 | end: |
4164020e | 692 | if (ret == 0) { |
0f5c5d5c SM |
693 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
694 | "Found message's timestamp: iter-data-addr={}, msg-addr={}, " | |
695 | "last-msg-ts={}, ts={}", | |
696 | fmt::ptr(lttng_live_msg_iter), fmt::ptr(msg), last_msg_ts_ns, *ts_ns); | |
4164020e SM |
697 | } |
698 | ||
699 | return ret; | |
7cdc2bab MD |
700 | } |
701 | ||
4164020e SM |
702 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_stream( |
703 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
704 | struct lttng_live_stream_iterator *lttng_live_stream, const bt_message **message) | |
7cdc2bab | 705 | { |
4164020e | 706 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
4164020e SM |
707 | enum ctf_msg_iter_status status; |
708 | uint64_t session_idx, trace_idx; | |
709 | ||
710 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
711 | struct lttng_live_session *session = | |
712 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
713 | ||
714 | if (session->new_streams_needed) { | |
0f5c5d5c SM |
715 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
716 | "Need an update for streams: " | |
717 | "session-id={}", | |
718 | session->id); | |
4164020e SM |
719 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
720 | goto end; | |
721 | } | |
722 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
723 | struct lttng_live_trace *trace = | |
724 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
725 | if (trace->metadata_stream_state == LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
0f5c5d5c SM |
726 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
727 | "Need an update for metadata stream: " | |
728 | "session-id={}, trace-id={}", | |
729 | session->id, trace->id); | |
4164020e SM |
730 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
731 | goto end; | |
732 | } | |
733 | } | |
734 | } | |
735 | ||
736 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_DATA) { | |
737 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
0f5c5d5c SM |
738 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
739 | "Invalid state of live stream iterator" | |
740 | "stream-iter-status={}", | |
741 | lttng_live_stream->state); | |
4164020e SM |
742 | goto end; |
743 | } | |
744 | ||
745 | status = ctf_msg_iter_get_next_message(lttng_live_stream->msg_iter, message); | |
746 | switch (status) { | |
747 | case CTF_MSG_ITER_STATUS_EOF: | |
748 | ret = LTTNG_LIVE_ITERATOR_STATUS_END; | |
749 | break; | |
750 | case CTF_MSG_ITER_STATUS_OK: | |
751 | ret = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
752 | break; | |
753 | case CTF_MSG_ITER_STATUS_AGAIN: | |
754 | /* | |
755 | * Continue immediately (end of packet). The next | |
756 | * get_index may return AGAIN to delay the following | |
757 | * attempt. | |
758 | */ | |
759 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
760 | break; | |
761 | case CTF_MSG_ITER_STATUS_ERROR: | |
762 | default: | |
763 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
0f5c5d5c SM |
764 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
765 | "CTF message iterator failed to get next message: " | |
766 | "msg-iter={}, msg-iter-status={}", | |
767 | fmt::ptr(lttng_live_stream->msg_iter), status); | |
4164020e SM |
768 | break; |
769 | } | |
14f28187 FD |
770 | |
771 | end: | |
4164020e | 772 | return ret; |
7cdc2bab MD |
773 | } |
774 | ||
4164020e SM |
775 | static enum lttng_live_iterator_status |
776 | lttng_live_iterator_close_stream(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
777 | struct lttng_live_stream_iterator *stream_iter, | |
778 | const bt_message **curr_msg) | |
4a39caef | 779 | { |
4164020e | 780 | enum lttng_live_iterator_status live_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
4164020e | 781 | |
0f5c5d5c SM |
782 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
783 | "Closing live stream iterator: stream-name=\"{}\", " | |
784 | "viewer-stream-id={}", | |
785 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
4164020e SM |
786 | |
787 | /* | |
788 | * The viewer has hung up on us so we are closing the stream. The | |
789 | * `ctf_msg_iter` should simply realize that it needs to close the | |
790 | * stream properly by emitting the necessary stream end message. | |
791 | */ | |
792 | enum ctf_msg_iter_status status = | |
793 | ctf_msg_iter_get_next_message(stream_iter->msg_iter, curr_msg); | |
794 | ||
795 | if (status == CTF_MSG_ITER_STATUS_ERROR) { | |
0f5c5d5c SM |
796 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
797 | "Error getting the next message from CTF message iterator"); | |
4164020e SM |
798 | live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
799 | goto end; | |
800 | } else if (status == CTF_MSG_ITER_STATUS_EOF) { | |
0f5c5d5c SM |
801 | BT_CPPLOGI_SPEC(lttng_live_msg_iter->logger, |
802 | "Reached the end of the live stream iterator."); | |
4164020e SM |
803 | live_status = LTTNG_LIVE_ITERATOR_STATUS_END; |
804 | goto end; | |
805 | } | |
806 | ||
807 | BT_ASSERT(status == CTF_MSG_ITER_STATUS_OK); | |
4a39caef FD |
808 | |
809 | end: | |
4164020e | 810 | return live_status; |
4a39caef FD |
811 | } |
812 | ||
7cdc2bab MD |
813 | /* |
814 | * helper function: | |
815 | * handle_no_data_streams() | |
816 | * retry: | |
817 | * - for each ACTIVE_NO_DATA stream: | |
818 | * - query relayd for stream data, or quiescence info. | |
819 | * - if need metadata, get metadata, goto retry. | |
820 | * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry | |
821 | * - if quiescent, move to QUIESCENT streams | |
822 | * - if fetched data, move to ACTIVE_DATA streams | |
823 | * (at this point each stream either has data, or is quiescent) | |
824 | * | |
825 | * | |
826 | * iterator_next: | |
827 | * handle_new_streams_and_metadata() | |
828 | * - query relayd for known streams, add them as ACTIVE_NO_DATA | |
829 | * - query relayd for metadata | |
830 | * | |
831 | * call handle_active_no_data_streams() | |
832 | * | |
833 | * handle_quiescent_streams() | |
834 | * - if at least one stream is ACTIVE_DATA: | |
835 | * - peek stream event with lowest timestamp -> next_ts | |
836 | * - for each quiescent stream | |
837 | * - if next_ts >= quiescent end | |
838 | * - set state to ACTIVE_NO_DATA | |
839 | * - else | |
840 | * - for each quiescent stream | |
841 | * - set state to ACTIVE_NO_DATA | |
842 | * | |
843 | * call handle_active_no_data_streams() | |
844 | * | |
845 | * handle_active_data_streams() | |
846 | * - if at least one stream is ACTIVE_DATA: | |
847 | * - get stream event with lowest timestamp from heap | |
d6e69534 | 848 | * - make that stream event the current message. |
7cdc2bab MD |
849 | * - move this stream heap position to its next event |
850 | * - if we need to fetch data from relayd, move | |
851 | * stream to ACTIVE_NO_DATA. | |
852 | * - return OK | |
853 | * - return AGAIN | |
854 | * | |
855 | * end criterion: ctrl-c on client. If relayd exits or the session | |
856 | * closes on the relay daemon side, we keep on waiting for streams. | |
857 | * Eventually handle --end timestamp (also an end criterion). | |
858 | * | |
859 | * When disconnected from relayd: try to re-connect endlessly. | |
860 | */ | |
4164020e SM |
861 | static enum lttng_live_iterator_status |
862 | lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
863 | struct lttng_live_stream_iterator *stream_iter, | |
864 | const bt_message **curr_msg) | |
7cdc2bab | 865 | { |
4164020e SM |
866 | enum lttng_live_iterator_status live_status; |
867 | ||
0f5c5d5c SM |
868 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
869 | "Advancing live stream iterator until next message if possible: " | |
870 | "stream-name=\"{}\", viewer-stream-id={}", | |
871 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
4164020e SM |
872 | |
873 | if (stream_iter->has_stream_hung_up) { | |
874 | /* | |
875 | * The stream has hung up and the stream was properly closed | |
876 | * during the last call to the current function. Return _END | |
877 | * status now so that this stream iterator is removed for the | |
878 | * stream iterator list. | |
879 | */ | |
880 | live_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
881 | goto end; | |
882 | } | |
4a39caef | 883 | |
7cdc2bab | 884 | retry: |
4164020e SM |
885 | LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter); |
886 | ||
887 | /* | |
888 | * Make sure we have the most recent metadata and possibly some new | |
889 | * streams. | |
890 | */ | |
891 | live_status = lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter); | |
892 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
893 | goto end; | |
894 | } | |
895 | ||
896 | live_status = | |
897 | lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter, stream_iter); | |
898 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
899 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
900 | /* | |
901 | * We overwrite `live_status` since `curr_msg` is | |
902 | * likely set to a valid message in this function. | |
903 | */ | |
904 | live_status = | |
905 | lttng_live_iterator_close_stream(lttng_live_msg_iter, stream_iter, curr_msg); | |
906 | } | |
907 | goto end; | |
908 | } | |
909 | ||
910 | live_status = lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter, | |
911 | stream_iter, curr_msg); | |
912 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
913 | BT_ASSERT(!*curr_msg); | |
914 | goto end; | |
915 | } | |
916 | if (*curr_msg) { | |
917 | goto end; | |
918 | } | |
919 | live_status = lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter, | |
920 | stream_iter, curr_msg); | |
921 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
922 | BT_ASSERT(!*curr_msg); | |
923 | } | |
7cdc2bab MD |
924 | |
925 | end: | |
4164020e | 926 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_CONTINUE) { |
0f5c5d5c SM |
927 | BT_CPPLOGD_SPEC( |
928 | lttng_live_msg_iter->logger, | |
929 | "Ask the relay daemon for an updated view of the data and metadata streams"); | |
4164020e SM |
930 | goto retry; |
931 | } | |
14f28187 | 932 | |
0f5c5d5c SM |
933 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
934 | "Returning from advancing live stream iterator: status={}, " | |
935 | "stream-name=\"{}\", viewer-stream-id={}", | |
936 | live_status, stream_iter->name->str, stream_iter->viewer_stream_id); | |
f93afbf9 | 937 | |
4164020e | 938 | return live_status; |
7cdc2bab MD |
939 | } |
940 | ||
4164020e | 941 | static bool is_discarded_packet_or_event_message(const bt_message *msg) |
8ec4d5ff | 942 | { |
4164020e | 943 | const enum bt_message_type msg_type = bt_message_get_type(msg); |
8ec4d5ff | 944 | |
4164020e SM |
945 | return msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS || |
946 | msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS; | |
8ec4d5ff JG |
947 | } |
948 | ||
4164020e SM |
949 | static enum lttng_live_iterator_status |
950 | adjust_discarded_packets_message(bt_self_message_iterator *iter, const bt_stream *stream, | |
951 | const bt_message *msg_in, bt_message **msg_out, | |
952 | uint64_t new_begin_ts) | |
8ec4d5ff | 953 | { |
4164020e SM |
954 | enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
955 | enum bt_property_availability availability; | |
956 | const bt_clock_snapshot *clock_snapshot; | |
957 | uint64_t end_ts; | |
958 | uint64_t count; | |
959 | ||
960 | clock_snapshot = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in); | |
961 | end_ts = bt_clock_snapshot_get_value(clock_snapshot); | |
962 | ||
963 | availability = bt_message_discarded_packets_get_count(msg_in, &count); | |
964 | BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); | |
965 | ||
966 | *msg_out = bt_message_discarded_packets_create_with_default_clock_snapshots( | |
967 | iter, stream, new_begin_ts, end_ts); | |
968 | if (!*msg_out) { | |
969 | status = LTTNG_LIVE_ITERATOR_STATUS_NOMEM; | |
970 | goto end; | |
971 | } | |
972 | ||
973 | bt_message_discarded_packets_set_count(*msg_out, count); | |
8ec4d5ff | 974 | end: |
4164020e | 975 | return status; |
8ec4d5ff JG |
976 | } |
977 | ||
4164020e SM |
978 | static enum lttng_live_iterator_status |
979 | adjust_discarded_events_message(bt_self_message_iterator *iter, const bt_stream *stream, | |
980 | const bt_message *msg_in, bt_message **msg_out, | |
981 | uint64_t new_begin_ts) | |
8ec4d5ff | 982 | { |
4164020e SM |
983 | enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
984 | enum bt_property_availability availability; | |
985 | const bt_clock_snapshot *clock_snapshot; | |
986 | uint64_t end_ts; | |
987 | uint64_t count; | |
988 | ||
989 | clock_snapshot = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in); | |
990 | end_ts = bt_clock_snapshot_get_value(clock_snapshot); | |
991 | ||
992 | availability = bt_message_discarded_events_get_count(msg_in, &count); | |
993 | BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); | |
994 | ||
995 | *msg_out = bt_message_discarded_events_create_with_default_clock_snapshots( | |
996 | iter, stream, new_begin_ts, end_ts); | |
997 | if (!*msg_out) { | |
998 | status = LTTNG_LIVE_ITERATOR_STATUS_NOMEM; | |
999 | goto end; | |
1000 | } | |
1001 | ||
1002 | bt_message_discarded_events_set_count(*msg_out, count); | |
8ec4d5ff | 1003 | end: |
4164020e | 1004 | return status; |
8ec4d5ff JG |
1005 | } |
1006 | ||
4164020e SM |
1007 | static enum lttng_live_iterator_status |
1008 | handle_late_message(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1009 | struct lttng_live_stream_iterator *stream_iter, int64_t late_msg_ts_ns, | |
1010 | const bt_message *late_msg) | |
285951be | 1011 | { |
4164020e SM |
1012 | const bt_clock_class *clock_class; |
1013 | const bt_stream_class *stream_class; | |
1014 | enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status; | |
1015 | int64_t last_inactivity_ts_ns; | |
1016 | enum lttng_live_iterator_status stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1017 | enum lttng_live_iterator_status adjust_status; | |
1018 | bt_message *adjusted_message; | |
1019 | ||
1020 | /* | |
1021 | * The timestamp of the current message is before the last message sent | |
1022 | * by this component. We CANNOT send it as is. | |
1023 | * | |
1024 | * The only expected scenario in which that could happen is the | |
e7401568 | 1025 | * following, everything else is a bug in this component, relay daemon, |
4164020e SM |
1026 | * or CTF parser. |
1027 | * | |
1028 | * Expected scenario: The CTF message iterator emitted discarded | |
1029 | * packets and discarded events with synthesized beginning and end | |
1030 | * timestamps from the bounds of the last known packet and the newly | |
1031 | * decoded packet header. The CTF message iterator is not aware of | |
1032 | * stream inactivity beacons. Hence, we have to adjust the beginning | |
1033 | * timestamp of those types of messages if a stream signalled its | |
1034 | * inactivity up until _after_ the last known packet's beginning | |
1035 | * timestamp. | |
1036 | * | |
1037 | * Otherwise, the monotonicity guarantee of message timestamps would | |
1038 | * not be preserved. | |
1039 | * | |
1040 | * In short, the only scenario in which it's okay and fixable to | |
1041 | * received a late message is when: | |
1042 | * 1. the late message is a discarded packets or discarded events | |
1043 | * message, | |
1044 | * 2. this stream produced an inactivity message downstream, and | |
1045 | * 3. the timestamp of the late message is within the inactivity | |
1046 | * timespan we sent downstream through the inactivity message. | |
1047 | */ | |
1048 | ||
0f5c5d5c SM |
1049 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1050 | "Handling late message on live stream iterator: " | |
1051 | "stream-name=\"{}\", viewer-stream-id={}", | |
1052 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
4164020e SM |
1053 | |
1054 | if (!stream_iter->last_inactivity_ts.is_set) { | |
0f5c5d5c SM |
1055 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1056 | "Invalid live stream state: " | |
1057 | "have a late message when no inactivity message " | |
1058 | "was ever sent for that stream."); | |
4164020e SM |
1059 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1060 | goto end; | |
1061 | } | |
1062 | ||
1063 | if (!is_discarded_packet_or_event_message(late_msg)) { | |
0f5c5d5c SM |
1064 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1065 | "Invalid live stream state: " | |
1066 | "have a late message that is not a packet discarded or " | |
1067 | "event discarded message: late-msg-type={}", | |
1068 | static_cast<bt2::MessageType>(bt_message_get_type(late_msg))); | |
4164020e SM |
1069 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1070 | goto end; | |
1071 | } | |
1072 | ||
1073 | stream_class = bt_stream_borrow_class_const(stream_iter->stream); | |
1074 | clock_class = bt_stream_class_borrow_default_clock_class_const(stream_class); | |
1075 | ||
1076 | ts_ns_status = bt_clock_class_cycles_to_ns_from_origin( | |
1077 | clock_class, stream_iter->last_inactivity_ts.value, &last_inactivity_ts_ns); | |
1078 | if (ts_ns_status != BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK) { | |
0f5c5d5c SM |
1079 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1080 | "Error converting last " | |
1081 | "inactivity message timestamp to nanoseconds"); | |
4164020e SM |
1082 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1083 | goto end; | |
1084 | } | |
1085 | ||
1086 | if (last_inactivity_ts_ns <= late_msg_ts_ns) { | |
0f5c5d5c SM |
1087 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1088 | "Invalid live stream state: " | |
1089 | "have a late message that is none included in a stream " | |
1090 | "inactivity timespan: last-inactivity-ts-ns={}, " | |
1091 | "late-msg-ts-ns={}", | |
1092 | last_inactivity_ts_ns, late_msg_ts_ns); | |
4164020e SM |
1093 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1094 | goto end; | |
1095 | } | |
1096 | ||
1097 | /* | |
1098 | * We now know that it's okay for this message to be late, we can now | |
1099 | * adjust its timestamp to ensure monotonicity. | |
1100 | */ | |
0f5c5d5c SM |
1101 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1102 | "Adjusting the timestamp of late message: late-msg-type={}, " | |
1103 | "msg-new-ts-ns={}", | |
1104 | static_cast<bt2::MessageType>(bt_message_get_type(late_msg)), | |
1105 | stream_iter->last_inactivity_ts.value); | |
4164020e SM |
1106 | switch (bt_message_get_type(late_msg)) { |
1107 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1108 | adjust_status = adjust_discarded_events_message( | |
1109 | lttng_live_msg_iter->self_msg_iter, stream_iter->stream, late_msg, &adjusted_message, | |
1110 | stream_iter->last_inactivity_ts.value); | |
1111 | break; | |
1112 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1113 | adjust_status = adjust_discarded_packets_message( | |
1114 | lttng_live_msg_iter->self_msg_iter, stream_iter->stream, late_msg, &adjusted_message, | |
1115 | stream_iter->last_inactivity_ts.value); | |
1116 | break; | |
1117 | default: | |
1118 | bt_common_abort(); | |
1119 | } | |
1120 | ||
1121 | if (adjust_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1122 | stream_iter_status = adjust_status; | |
1123 | goto end; | |
1124 | } | |
1125 | ||
1126 | BT_ASSERT_DBG(adjusted_message); | |
1127 | stream_iter->current_msg = adjusted_message; | |
1128 | stream_iter->current_msg_ts_ns = last_inactivity_ts_ns; | |
1129 | bt_message_put_ref(late_msg); | |
cefd84f8 | 1130 | |
285951be | 1131 | end: |
4164020e | 1132 | return stream_iter_status; |
285951be FD |
1133 | } |
1134 | ||
4164020e SM |
1135 | static enum lttng_live_iterator_status |
1136 | next_stream_iterator_for_trace(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1137 | struct lttng_live_trace *live_trace, | |
1138 | struct lttng_live_stream_iterator **youngest_trace_stream_iter) | |
7cdc2bab | 1139 | { |
4164020e | 1140 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; |
4164020e | 1141 | enum lttng_live_iterator_status stream_iter_status; |
4164020e SM |
1142 | int64_t youngest_candidate_msg_ts = INT64_MAX; |
1143 | uint64_t stream_iter_idx; | |
1144 | ||
1145 | BT_ASSERT_DBG(live_trace); | |
1146 | BT_ASSERT_DBG(live_trace->stream_iterators); | |
1147 | ||
0f5c5d5c SM |
1148 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1149 | "Finding the next stream iterator for trace: " | |
1150 | "trace-id={}", | |
1151 | live_trace->id); | |
4164020e SM |
1152 | /* |
1153 | * Update the current message of every stream iterators of this trace. | |
1154 | * The current msg of every stream must have a timestamp equal or | |
1155 | * larger than the last message returned by this iterator. We must | |
1156 | * ensure monotonicity. | |
1157 | */ | |
1158 | stream_iter_idx = 0; | |
1159 | while (stream_iter_idx < live_trace->stream_iterators->len) { | |
1160 | bool stream_iter_is_ended = false; | |
1161 | struct lttng_live_stream_iterator *stream_iter = | |
1162 | (lttng_live_stream_iterator *) g_ptr_array_index(live_trace->stream_iterators, | |
1163 | stream_iter_idx); | |
1164 | ||
1165 | /* | |
1166 | * If there is no current message for this stream, go fetch | |
1167 | * one. | |
1168 | */ | |
1169 | while (!stream_iter->current_msg) { | |
1170 | const bt_message *msg = NULL; | |
1171 | int64_t curr_msg_ts_ns = INT64_MAX; | |
1172 | ||
1173 | stream_iter_status = | |
1174 | lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter, stream_iter, &msg); | |
1175 | ||
1176 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1177 | stream_iter_is_ended = true; | |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1182 | goto end; | |
1183 | } | |
1184 | ||
1185 | BT_ASSERT_DBG(msg); | |
1186 | ||
0f5c5d5c SM |
1187 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1188 | "Live stream iterator returned message: msg-type={}, " | |
1189 | "stream-name=\"{}\", viewer-stream-id={}", | |
1190 | static_cast<bt2::MessageType>(bt_message_get_type(msg)), | |
1191 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
4164020e SM |
1192 | |
1193 | /* | |
1194 | * Get the timestamp in nanoseconds from origin of this | |
e7401568 | 1195 | * message. |
4164020e | 1196 | */ |
7d91f1ac SM |
1197 | live_get_msg_ts_ns(lttng_live_msg_iter, msg, lttng_live_msg_iter->last_msg_ts_ns, |
1198 | &curr_msg_ts_ns); | |
4164020e SM |
1199 | |
1200 | /* | |
1201 | * Check if the message of the current live stream | |
1202 | * iterator occurred at the exact same time or after the | |
1203 | * last message returned by this component's message | |
1204 | * iterator. If not, we need to handle it with care. | |
1205 | */ | |
1206 | if (curr_msg_ts_ns >= lttng_live_msg_iter->last_msg_ts_ns) { | |
1207 | stream_iter->current_msg = msg; | |
1208 | stream_iter->current_msg_ts_ns = curr_msg_ts_ns; | |
1209 | } else { | |
1210 | /* | |
1211 | * We received a message from the past. This | |
1212 | * may be fixable but it can also be an error. | |
1213 | */ | |
1214 | stream_iter_status = | |
1215 | handle_late_message(lttng_live_msg_iter, stream_iter, curr_msg_ts_ns, msg); | |
1216 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
0f5c5d5c SM |
1217 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1218 | "Late message could not be handled correctly: " | |
1219 | "lttng-live-msg-iter-addr={}, " | |
1220 | "stream-name=\"{}\", " | |
1221 | "curr-msg-ts={}, last-msg-ts={}", | |
1222 | fmt::ptr(lttng_live_msg_iter), | |
1223 | stream_iter->name->str, curr_msg_ts_ns, | |
1224 | lttng_live_msg_iter->last_msg_ts_ns); | |
4164020e SM |
1225 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1226 | goto end; | |
1227 | } | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); | |
1232 | ||
1233 | if (!stream_iter_is_ended) { | |
1234 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || | |
1235 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1236 | /* | |
1237 | * Update the current best candidate message | |
1238 | * for the stream iterator of this live trace | |
1239 | * to be forwarded downstream. | |
1240 | */ | |
1241 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1242 | youngest_candidate_stream_iter = stream_iter; | |
1243 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
1244 | /* | |
1245 | * Order the messages in an arbitrary but | |
1246 | * deterministic way. | |
1247 | */ | |
1248 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); | |
1249 | int ret = common_muxing_compare_messages( | |
1250 | stream_iter->current_msg, youngest_candidate_stream_iter->current_msg); | |
1251 | if (ret < 0) { | |
1252 | /* | |
1253 | * The `youngest_candidate_stream_iter->current_msg` | |
1254 | * should go first. Update the next | |
1255 | * iterator and the current timestamp. | |
1256 | */ | |
1257 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1258 | youngest_candidate_stream_iter = stream_iter; | |
1259 | } else if (ret == 0) { | |
1260 | /* | |
1261 | * Unable to pick which one should go | |
1262 | * first. | |
1263 | */ | |
0f5c5d5c SM |
1264 | BT_CPPLOGW_SPEC( |
1265 | lttng_live_msg_iter->logger, | |
4164020e | 1266 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " |
0f5c5d5c SM |
1267 | "stream-iter-addr={}" |
1268 | "stream-iter-addr={}", | |
1269 | fmt::ptr(stream_iter), fmt::ptr(youngest_candidate_stream_iter)); | |
4164020e SM |
1270 | } |
1271 | } | |
1272 | ||
1273 | stream_iter_idx++; | |
1274 | } else { | |
1275 | /* | |
1276 | * The live stream iterator has ended. That | |
1277 | * iterator is removed from the array, but | |
1278 | * there is no need to increment | |
1279 | * stream_iter_idx as | |
1280 | * g_ptr_array_remove_index_fast replaces the | |
1281 | * removed element with the array's last | |
1282 | * element. | |
1283 | */ | |
1284 | g_ptr_array_remove_index_fast(live_trace->stream_iterators, stream_iter_idx); | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | if (youngest_candidate_stream_iter) { | |
1289 | *youngest_trace_stream_iter = youngest_candidate_stream_iter; | |
1290 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1291 | } else { | |
1292 | /* | |
1293 | * The only case where we don't have a candidate for this trace | |
1294 | * is if we reached the end of all the iterators. | |
1295 | */ | |
1296 | BT_ASSERT(live_trace->stream_iterators->len == 0); | |
1297 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1298 | } | |
14f28187 FD |
1299 | |
1300 | end: | |
4164020e | 1301 | return stream_iter_status; |
14f28187 FD |
1302 | } |
1303 | ||
4164020e SM |
1304 | static enum lttng_live_iterator_status |
1305 | next_stream_iterator_for_session(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1306 | struct lttng_live_session *session, | |
1307 | struct lttng_live_stream_iterator **youngest_session_stream_iter) | |
14f28187 | 1308 | { |
4164020e SM |
1309 | enum lttng_live_iterator_status stream_iter_status; |
1310 | uint64_t trace_idx = 0; | |
1311 | int64_t youngest_candidate_msg_ts = INT64_MAX; | |
1312 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; | |
1313 | ||
0f5c5d5c SM |
1314 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1315 | "Finding the next stream iterator for session: " | |
1316 | "session-id={}", | |
1317 | session->id); | |
4164020e SM |
1318 | /* |
1319 | * Make sure we are attached to the session and look for new streams | |
1320 | * and metadata. | |
1321 | */ | |
1322 | stream_iter_status = lttng_live_get_session(lttng_live_msg_iter, session); | |
1323 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK && | |
1324 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_CONTINUE && | |
1325 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1326 | goto end; | |
1327 | } | |
1328 | ||
1329 | BT_ASSERT_DBG(session->traces); | |
1330 | ||
1331 | while (trace_idx < session->traces->len) { | |
1332 | bool trace_is_ended = false; | |
1333 | struct lttng_live_stream_iterator *stream_iter; | |
1334 | struct lttng_live_trace *trace = | |
1335 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
1336 | ||
1337 | stream_iter_status = | |
1338 | next_stream_iterator_for_trace(lttng_live_msg_iter, trace, &stream_iter); | |
1339 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1340 | /* | |
1341 | * All the live stream iterators for this trace are | |
1342 | * ENDed. Remove the trace from this session. | |
1343 | */ | |
1344 | trace_is_ended = true; | |
1345 | } else if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1346 | goto end; | |
1347 | } | |
1348 | ||
1349 | if (!trace_is_ended) { | |
1350 | BT_ASSERT_DBG(stream_iter); | |
1351 | ||
1352 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || | |
1353 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1354 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1355 | youngest_candidate_stream_iter = stream_iter; | |
1356 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
1357 | /* | |
1358 | * Order the messages in an arbitrary but | |
1359 | * deterministic way. | |
1360 | */ | |
1361 | int ret = common_muxing_compare_messages( | |
1362 | stream_iter->current_msg, youngest_candidate_stream_iter->current_msg); | |
1363 | if (ret < 0) { | |
1364 | /* | |
1365 | * The `youngest_candidate_stream_iter->current_msg` | |
1366 | * should go first. Update the next iterator | |
1367 | * and the current timestamp. | |
1368 | */ | |
1369 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1370 | youngest_candidate_stream_iter = stream_iter; | |
1371 | } else if (ret == 0) { | |
1372 | /* Unable to pick which one should go first. */ | |
0f5c5d5c SM |
1373 | BT_CPPLOGW_SPEC( |
1374 | lttng_live_msg_iter->logger, | |
4164020e | 1375 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " |
0f5c5d5c SM |
1376 | "stream-iter-addr={}" |
1377 | "youngest-candidate-stream-iter-addr={}", | |
1378 | fmt::ptr(stream_iter), fmt::ptr(youngest_candidate_stream_iter)); | |
4164020e SM |
1379 | } |
1380 | } | |
1381 | trace_idx++; | |
1382 | } else { | |
1383 | /* | |
1384 | * trace_idx is not incremented since | |
1385 | * g_ptr_array_remove_index_fast replaces the | |
1386 | * element at trace_idx with the array's last element. | |
1387 | */ | |
1388 | g_ptr_array_remove_index_fast(session->traces, trace_idx); | |
1389 | } | |
1390 | } | |
1391 | if (youngest_candidate_stream_iter) { | |
1392 | *youngest_session_stream_iter = youngest_candidate_stream_iter; | |
1393 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1394 | } else { | |
1395 | /* | |
1396 | * The only cases where we don't have a candidate for this | |
1397 | * trace is: | |
1398 | * 1. if we reached the end of all the iterators of all the | |
1399 | * traces of this session, | |
1400 | * 2. if we never had live stream iterator in the first place. | |
1401 | * | |
1402 | * In either cases, we return END. | |
1403 | */ | |
1404 | BT_ASSERT(session->traces->len == 0); | |
1405 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1406 | } | |
7cdc2bab | 1407 | end: |
4164020e | 1408 | return stream_iter_status; |
14f28187 FD |
1409 | } |
1410 | ||
4164020e | 1411 | static inline void put_messages(bt_message_array_const msgs, uint64_t count) |
14f28187 | 1412 | { |
4164020e | 1413 | uint64_t i; |
14f28187 | 1414 | |
4164020e SM |
1415 | for (i = 0; i < count; i++) { |
1416 | BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]); | |
1417 | } | |
7cdc2bab MD |
1418 | } |
1419 | ||
4164020e SM |
1420 | bt_message_iterator_class_next_method_status |
1421 | lttng_live_msg_iter_next(bt_self_message_iterator *self_msg_it, bt_message_array_const msgs, | |
1422 | uint64_t capacity, uint64_t *count) | |
d3eb6e8f | 1423 | { |
1e690349 SM |
1424 | try { |
1425 | bt_message_iterator_class_next_method_status status; | |
1426 | enum lttng_live_viewer_status viewer_status; | |
1427 | struct lttng_live_msg_iter *lttng_live_msg_iter = | |
1428 | (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_it); | |
1429 | struct lttng_live_component *lttng_live = lttng_live_msg_iter->lttng_live_comp; | |
1430 | enum lttng_live_iterator_status stream_iter_status; | |
1431 | uint64_t session_idx; | |
4164020e | 1432 | |
1e690349 | 1433 | *count = 0; |
4164020e | 1434 | |
1e690349 | 1435 | BT_ASSERT_DBG(lttng_live_msg_iter); |
4164020e | 1436 | |
1e690349 SM |
1437 | if (G_UNLIKELY(lttng_live_msg_iter->was_interrupted)) { |
1438 | /* | |
4164020e SM |
1439 | * The iterator was interrupted in a previous call to the |
1440 | * `_next()` method. We currently do not support generating | |
1441 | * messages after such event. The babeltrace2 CLI should never | |
1442 | * be running the graph after being interrupted. So this check | |
1443 | * is to prevent other graph users from using this live | |
1444 | * iterator in an messed up internal state. | |
1445 | */ | |
1e690349 SM |
1446 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
1447 | BT_CPPLOGE_APPEND_CAUSE_SPEC( | |
1448 | lttng_live_msg_iter->logger, | |
1449 | "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event."); | |
1450 | goto end; | |
1451 | } | |
4164020e | 1452 | |
1e690349 | 1453 | /* |
4164020e SM |
1454 | * Clear all the invalid message reference that might be left over in |
1455 | * the output array. | |
1456 | */ | |
1e690349 | 1457 | memset(msgs, 0, capacity * sizeof(*msgs)); |
4164020e | 1458 | |
1e690349 | 1459 | /* |
4164020e SM |
1460 | * If no session are exposed on the relay found at the url provided by |
1461 | * the user, session count will be 0. In this case, we return status | |
1462 | * end to return gracefully. | |
1463 | */ | |
1e690349 SM |
1464 | if (lttng_live_msg_iter->sessions->len == 0) { |
1465 | if (lttng_live->params.sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
1466 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
1467 | goto end; | |
1468 | } else { | |
1469 | /* | |
4164020e SM |
1470 | * The are no more active session for this session |
1471 | * name. Retry to create a viewer session for the | |
1472 | * requested session name. | |
1473 | */ | |
1e690349 SM |
1474 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); |
1475 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1476 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1477 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1478 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1479 | "Error creating LTTng live viewer session"); | |
1480 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1481 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; | |
1482 | } else { | |
1483 | bt_common_abort(); | |
1484 | } | |
1485 | goto end; | |
4164020e | 1486 | } |
4164020e SM |
1487 | } |
1488 | } | |
4164020e | 1489 | |
1e690349 SM |
1490 | if (lttng_live_msg_iter->active_stream_iter == 0) { |
1491 | lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter); | |
1492 | } | |
4164020e | 1493 | |
1e690349 | 1494 | /* |
4164020e SM |
1495 | * Here the muxing of message is done. |
1496 | * | |
1497 | * We need to iterate over all the streams of all the traces of all the | |
1498 | * viewer sessions in order to get the message with the smallest | |
1499 | * timestamp. In this case, a session is a viewer session and there is | |
1500 | * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or | |
1501 | * kernel). Each viewer session can have multiple traces, for example, | |
1502 | * 64bit UST viewer sessions could have multiple per-pid traces. | |
1503 | * | |
1504 | * We iterate over the streams of each traces to update and see what is | |
1505 | * their next message's timestamp. From those timestamps, we select the | |
1506 | * message with the smallest timestamp as the best candidate message | |
1507 | * for that trace and do the same thing across all the sessions. | |
1508 | * | |
1509 | * We then compare the timestamp of best candidate message of all the | |
1510 | * sessions to pick the message with the smallest timestamp and we | |
1511 | * return it. | |
1512 | */ | |
1e690349 SM |
1513 | while (*count < capacity) { |
1514 | struct lttng_live_stream_iterator *youngest_stream_iter = NULL, | |
1515 | *candidate_stream_iter = NULL; | |
1516 | int64_t youngest_msg_ts_ns = INT64_MAX; | |
1517 | ||
1518 | BT_ASSERT_DBG(lttng_live_msg_iter->sessions); | |
1519 | session_idx = 0; | |
1520 | while (session_idx < lttng_live_msg_iter->sessions->len) { | |
1521 | struct lttng_live_session *session = (lttng_live_session *) g_ptr_array_index( | |
1522 | lttng_live_msg_iter->sessions, session_idx); | |
1523 | ||
1524 | /* Find the best candidate message to send downstream. */ | |
1525 | stream_iter_status = next_stream_iterator_for_session(lttng_live_msg_iter, session, | |
1526 | &candidate_stream_iter); | |
1527 | ||
1528 | /* If we receive an END status, it means that either: | |
4164020e SM |
1529 | * - Those traces never had active streams (UST with no |
1530 | * data produced yet), | |
1531 | * - All live stream iterators have ENDed.*/ | |
1e690349 SM |
1532 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { |
1533 | if (session->closed && session->traces->len == 0) { | |
1534 | /* | |
4164020e SM |
1535 | * Remove the session from the list. |
1536 | * session_idx is not modified since | |
1537 | * g_ptr_array_remove_index_fast | |
1538 | * replaces the the removed element with | |
1539 | * the array's last element. | |
1540 | */ | |
1e690349 SM |
1541 | g_ptr_array_remove_index_fast(lttng_live_msg_iter->sessions, session_idx); |
1542 | } else { | |
1543 | session_idx++; | |
1544 | } | |
1545 | continue; | |
4164020e | 1546 | } |
4164020e | 1547 | |
1e690349 SM |
1548 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
1549 | goto return_status; | |
1550 | } | |
4164020e | 1551 | |
1e690349 SM |
1552 | if (G_UNLIKELY(youngest_stream_iter == NULL) || |
1553 | candidate_stream_iter->current_msg_ts_ns < youngest_msg_ts_ns) { | |
1554 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; | |
1555 | youngest_stream_iter = candidate_stream_iter; | |
1556 | } else if (candidate_stream_iter->current_msg_ts_ns == youngest_msg_ts_ns) { | |
1557 | /* | |
4164020e SM |
1558 | * The currently selected message to be sent |
1559 | * downstream next has the exact same timestamp | |
1560 | * that of the current candidate message. We | |
1561 | * must break the tie in a predictable manner. | |
1562 | */ | |
1e690349 SM |
1563 | BT_CPPLOGD_STR_SPEC( |
1564 | lttng_live_msg_iter->logger, | |
1565 | "Two of the next message candidates have the same timestamps, pick one deterministically."); | |
1566 | /* | |
4164020e SM |
1567 | * Order the messages in an arbitrary but |
1568 | * deterministic way. | |
1569 | */ | |
1e690349 SM |
1570 | int ret = common_muxing_compare_messages(candidate_stream_iter->current_msg, |
1571 | youngest_stream_iter->current_msg); | |
1572 | if (ret < 0) { | |
1573 | /* | |
4164020e SM |
1574 | * The `candidate_stream_iter->current_msg` |
1575 | * should go first. Update the next | |
1576 | * iterator and the current timestamp. | |
1577 | */ | |
1e690349 SM |
1578 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; |
1579 | youngest_stream_iter = candidate_stream_iter; | |
1580 | } else if (ret == 0) { | |
1581 | /* Unable to pick which one should go first. */ | |
1582 | BT_CPPLOGW_SPEC( | |
1583 | lttng_live_msg_iter->logger, | |
1584 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1585 | "next-stream-iter-addr={}" | |
1586 | "candidate-stream-iter-addr={}", | |
1587 | fmt::ptr(youngest_stream_iter), fmt::ptr(candidate_stream_iter)); | |
1588 | } | |
4164020e | 1589 | } |
4164020e | 1590 | |
1e690349 SM |
1591 | session_idx++; |
1592 | } | |
4164020e | 1593 | |
1e690349 SM |
1594 | if (!youngest_stream_iter) { |
1595 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
1596 | goto return_status; | |
1597 | } | |
4164020e | 1598 | |
1e690349 SM |
1599 | BT_ASSERT_DBG(youngest_stream_iter->current_msg); |
1600 | /* Ensure monotonicity. */ | |
1601 | BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <= | |
1602 | youngest_stream_iter->current_msg_ts_ns); | |
4164020e | 1603 | |
1e690349 | 1604 | /* |
4164020e | 1605 | * Insert the next message to the message batch. This will set |
e7401568 | 1606 | * stream iterator current message to NULL so that next time |
4164020e SM |
1607 | * we fetch the next message of that stream iterator |
1608 | */ | |
1e690349 SM |
1609 | BT_MESSAGE_MOVE_REF(msgs[*count], youngest_stream_iter->current_msg); |
1610 | (*count)++; | |
4164020e | 1611 | |
1e690349 SM |
1612 | /* Update the last timestamp in nanoseconds sent downstream. */ |
1613 | lttng_live_msg_iter->last_msg_ts_ns = youngest_msg_ts_ns; | |
1614 | youngest_stream_iter->current_msg_ts_ns = INT64_MAX; | |
4164020e | 1615 | |
1e690349 SM |
1616 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
1617 | } | |
f79c2d7a FD |
1618 | |
1619 | return_status: | |
1e690349 SM |
1620 | switch (stream_iter_status) { |
1621 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
1622 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
1623 | /* | |
4164020e SM |
1624 | * If we gathered messages, return _OK even if the graph was |
1625 | * interrupted. This allows for the components downstream to at | |
e7401568 | 1626 | * least get the those messages. If the graph was indeed |
4164020e SM |
1627 | * interrupted there should not be another _next() call as the |
1628 | * application will tear down the graph. This component class | |
1629 | * doesn't support restarting after an interruption. | |
1630 | */ | |
1e690349 SM |
1631 | if (*count > 0) { |
1632 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
1633 | } else { | |
1634 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; | |
1635 | } | |
1636 | break; | |
1637 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
1638 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
1639 | break; | |
1640 | case LTTNG_LIVE_ITERATOR_STATUS_NOMEM: | |
1641 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1642 | "Memory error preparing the next batch of messages: " | |
1643 | "live-iter-status={}", | |
1644 | stream_iter_status); | |
1645 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; | |
1646 | break; | |
1647 | case LTTNG_LIVE_ITERATOR_STATUS_ERROR: | |
1648 | case LTTNG_LIVE_ITERATOR_STATUS_INVAL: | |
1649 | case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED: | |
1650 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1651 | "Error preparing the next batch of messages: " | |
1652 | "live-iter-status={}", | |
1653 | stream_iter_status); | |
4164020e | 1654 | |
1e690349 SM |
1655 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
1656 | /* Put all existing messages on error. */ | |
1657 | put_messages(msgs, *count); | |
1658 | break; | |
1659 | default: | |
1660 | bt_common_abort(); | |
1661 | } | |
14f28187 | 1662 | |
f79c2d7a | 1663 | end: |
1e690349 SM |
1664 | return status; |
1665 | } catch (const std::bad_alloc&) { | |
1666 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; | |
1667 | } catch (const bt2::Error&) { | |
1668 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1669 | } | |
7cdc2bab | 1670 | } |
41a2b7ae | 1671 | |
4164020e SM |
1672 | static struct lttng_live_msg_iter * |
1673 | lttng_live_msg_iter_create(struct lttng_live_component *lttng_live_comp, | |
1674 | bt_self_message_iterator *self_msg_it) | |
4f74db52 | 1675 | { |
0f5c5d5c SM |
1676 | lttng_live_msg_iter *msg_iter = new lttng_live_msg_iter {lttng_live_comp->logger}; |
1677 | msg_iter->self_comp = lttng_live_comp->self_comp; | |
1678 | msg_iter->lttng_live_comp = lttng_live_comp; | |
1679 | msg_iter->self_msg_iter = self_msg_it; | |
4164020e | 1680 | |
0f5c5d5c SM |
1681 | msg_iter->active_stream_iter = 0; |
1682 | msg_iter->last_msg_ts_ns = INT64_MIN; | |
1683 | msg_iter->was_interrupted = false; | |
4f74db52 | 1684 | |
0f5c5d5c | 1685 | msg_iter->sessions = |
4164020e | 1686 | g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_destroy_session); |
0f5c5d5c | 1687 | BT_ASSERT(msg_iter->sessions); |
4164020e | 1688 | |
0f5c5d5c | 1689 | return msg_iter; |
4f74db52 FD |
1690 | } |
1691 | ||
4164020e SM |
1692 | bt_message_iterator_class_initialize_method_status |
1693 | lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it, | |
7d91f1ac | 1694 | bt_self_message_iterator_configuration *, bt_self_component_port_output *) |
7cdc2bab | 1695 | { |
1e690349 SM |
1696 | try { |
1697 | bt_message_iterator_class_initialize_method_status status; | |
1698 | struct lttng_live_component *lttng_live; | |
1699 | struct lttng_live_msg_iter *lttng_live_msg_iter; | |
1700 | enum lttng_live_viewer_status viewer_status; | |
1701 | bt_self_component *self_comp = bt_self_message_iterator_borrow_component(self_msg_it); | |
1702 | ||
1703 | lttng_live = (lttng_live_component *) bt_self_component_get_data(self_comp); | |
1704 | ||
1705 | /* There can be only one downstream iterator at the same time. */ | |
1706 | BT_ASSERT(!lttng_live->has_msg_iter); | |
1707 | lttng_live->has_msg_iter = true; | |
1708 | ||
1709 | lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, self_msg_it); | |
1710 | if (!lttng_live_msg_iter) { | |
1711 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live->logger, | |
1712 | "Failed to create lttng_live_msg_iter"); | |
1713 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
1714 | goto error; | |
1715 | } | |
4164020e | 1716 | |
1e690349 | 1717 | viewer_status = live_viewer_connection_create( |
2780ec75 | 1718 | lttng_live->params.url.c_str(), false, lttng_live_msg_iter, lttng_live_msg_iter->logger, |
1e690349 SM |
1719 | <tng_live_msg_iter->viewer_connection); |
1720 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1721 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1722 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1723 | "Failed to create viewer connection"); | |
1724 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1725 | /* | |
4164020e SM |
1726 | * Interruption in the _iter_init() method is not |
1727 | * supported. Return an error. | |
1728 | */ | |
1e690349 SM |
1729 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1730 | "Interrupted while creating viewer connection"); | |
1731 | } | |
10265ebe | 1732 | |
1e690349 SM |
1733 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
1734 | goto error; | |
1735 | } | |
4164020e | 1736 | |
1e690349 SM |
1737 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); |
1738 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1739 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1740 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1741 | "Failed to create viewer session"); | |
1742 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1743 | /* | |
4164020e SM |
1744 | * Interruption in the _iter_init() method is not |
1745 | * supported. Return an error. | |
1746 | */ | |
1e690349 SM |
1747 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1748 | "Interrupted when creating viewer session"); | |
1749 | } | |
4164020e | 1750 | |
10265ebe | 1751 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
4164020e | 1752 | goto error; |
4164020e | 1753 | } |
4164020e | 1754 | |
1e690349 SM |
1755 | if (lttng_live_msg_iter->sessions->len == 0) { |
1756 | switch (lttng_live->params.sess_not_found_act) { | |
1757 | case SESSION_NOT_FOUND_ACTION_CONTINUE: | |
1758 | BT_CPPLOGI_SPEC( | |
1759 | lttng_live_msg_iter->logger, | |
1760 | "Unable to connect to the requested live viewer session. " | |
1761 | "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"", | |
1762 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR, | |
2780ec75 | 1763 | lttng_live->params.url); |
1e690349 SM |
1764 | break; |
1765 | case SESSION_NOT_FOUND_ACTION_FAIL: | |
1766 | BT_CPPLOGE_APPEND_CAUSE_SPEC( | |
1767 | lttng_live_msg_iter->logger, | |
1768 | "Unable to connect to the requested live viewer session. " | |
1769 | "Fail the message iterator initialization because of {}=\"{}\" " | |
1770 | "component parameter: url =\"{}\"", | |
1771 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_FAIL_STR, | |
2780ec75 | 1772 | lttng_live->params.url); |
1e690349 SM |
1773 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
1774 | goto error; | |
1775 | case SESSION_NOT_FOUND_ACTION_END: | |
1776 | BT_CPPLOGI_SPEC(lttng_live_msg_iter->logger, | |
1777 | "Unable to connect to the requested live viewer session. " | |
1778 | "End gracefully at the first _next() call because of {}=\"{}\"" | |
1779 | " component parameter: url=\"{}\"", | |
1780 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_END_STR, | |
2780ec75 | 1781 | lttng_live->params.url); |
1e690349 SM |
1782 | break; |
1783 | default: | |
1784 | bt_common_abort(); | |
1785 | } | |
1786 | } | |
1787 | ||
1788 | bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter); | |
1789 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
1790 | goto end; | |
f79c2d7a | 1791 | |
14f28187 | 1792 | error: |
1e690349 | 1793 | lttng_live_msg_iter_destroy(lttng_live_msg_iter); |
7cdc2bab | 1794 | end: |
1e690349 SM |
1795 | return status; |
1796 | } catch (const std::bad_alloc&) { | |
1797 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
1798 | } catch (const bt2::Error&) { | |
1799 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
1800 | } | |
7cdc2bab MD |
1801 | } |
1802 | ||
80aff5ef | 1803 | static struct bt_param_validation_map_value_entry_descr list_sessions_params[] = { |
88730e42 SM |
1804 | {URL_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, |
1805 | bt_param_validation_value_descr::makeString()}, | |
4164020e SM |
1806 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; |
1807 | ||
1808 | static bt_component_class_query_method_status | |
1809 | lttng_live_query_list_sessions(const bt_value *params, const bt_value **result, | |
0f5c5d5c | 1810 | const bt2c::Logger& logger) |
7cdc2bab | 1811 | { |
4164020e SM |
1812 | bt_component_class_query_method_status status; |
1813 | const bt_value *url_value = NULL; | |
1814 | const char *url; | |
1815 | struct live_viewer_connection *viewer_connection = NULL; | |
1816 | enum lttng_live_viewer_status viewer_status; | |
1817 | enum bt_param_validation_status validation_status; | |
1818 | gchar *validate_error = NULL; | |
1819 | ||
1820 | validation_status = bt_param_validation_validate(params, list_sessions_params, &validate_error); | |
1821 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
1822 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1823 | goto error; | |
1824 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
1825 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
0f5c5d5c | 1826 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "{}", validate_error); |
4164020e SM |
1827 | goto error; |
1828 | } | |
1829 | ||
1830 | url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM); | |
1831 | url = bt_value_string_get(url_value); | |
1832 | ||
0f5c5d5c | 1833 | viewer_status = live_viewer_connection_create(url, true, NULL, logger, &viewer_connection); |
4164020e SM |
1834 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
1835 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
0f5c5d5c | 1836 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create viewer connection"); |
4164020e SM |
1837 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
1838 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1839 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; | |
1840 | } else { | |
1841 | bt_common_abort(); | |
1842 | } | |
1843 | goto error; | |
1844 | } | |
1845 | ||
1846 | status = live_viewer_connection_list_sessions(viewer_connection, result); | |
1847 | if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) { | |
0f5c5d5c | 1848 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to list viewer sessions"); |
4164020e SM |
1849 | goto error; |
1850 | } | |
1851 | ||
1852 | goto end; | |
c7eee084 | 1853 | |
7cdc2bab | 1854 | error: |
4164020e | 1855 | BT_VALUE_PUT_REF_AND_RESET(*result); |
c7eee084 | 1856 | |
4164020e SM |
1857 | if (status >= 0) { |
1858 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1859 | } | |
c7eee084 | 1860 | |
7cdc2bab | 1861 | end: |
4164020e SM |
1862 | if (viewer_connection) { |
1863 | live_viewer_connection_destroy(viewer_connection); | |
1864 | } | |
80aff5ef | 1865 | |
4164020e | 1866 | g_free(validate_error); |
80aff5ef | 1867 | |
4164020e | 1868 | return status; |
7cdc2bab MD |
1869 | } |
1870 | ||
4164020e SM |
1871 | static bt_component_class_query_method_status |
1872 | lttng_live_query_support_info(const bt_value *params, const bt_value **result, | |
0f5c5d5c | 1873 | const bt2c::Logger& logger) |
312df793 | 1874 | { |
4164020e SM |
1875 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
1876 | const bt_value *input_type_value; | |
1877 | const bt_value *input_value; | |
1878 | double weight = 0; | |
dd420a9b | 1879 | struct bt_common_lttng_live_url_parts parts = {}; |
4164020e SM |
1880 | |
1881 | /* Used by the logging macros */ | |
1882 | __attribute__((unused)) bt_self_component *self_comp = NULL; | |
1883 | ||
1884 | *result = NULL; | |
1885 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); | |
1886 | if (!input_type_value) { | |
0f5c5d5c | 1887 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Missing expected `type` parameter."); |
4164020e SM |
1888 | goto error; |
1889 | } | |
1890 | ||
1891 | if (!bt_value_is_string(input_type_value)) { | |
0f5c5d5c | 1892 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`type` parameter is not a string value."); |
4164020e SM |
1893 | goto error; |
1894 | } | |
1895 | ||
1896 | if (strcmp(bt_value_string_get(input_type_value), "string") != 0) { | |
1897 | /* We don't handle file system paths */ | |
1898 | goto create_result; | |
1899 | } | |
1900 | ||
1901 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
1902 | if (!input_value) { | |
0f5c5d5c | 1903 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Missing expected `input` parameter."); |
4164020e SM |
1904 | goto error; |
1905 | } | |
1906 | ||
1907 | if (!bt_value_is_string(input_value)) { | |
0f5c5d5c | 1908 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`input` parameter is not a string value."); |
4164020e SM |
1909 | goto error; |
1910 | } | |
1911 | ||
1912 | parts = bt_common_parse_lttng_live_url(bt_value_string_get(input_value), NULL, 0); | |
1913 | if (parts.session_name) { | |
1914 | /* | |
1915 | * Looks pretty much like an LTTng live URL: we got the | |
1916 | * session name part, which forms a complete URL. | |
1917 | */ | |
1918 | weight = .75; | |
1919 | } | |
312df793 PP |
1920 | |
1921 | create_result: | |
4164020e SM |
1922 | *result = bt_value_real_create_init(weight); |
1923 | if (!*result) { | |
1924 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1925 | goto error; | |
1926 | } | |
312df793 | 1927 | |
4164020e | 1928 | goto end; |
312df793 PP |
1929 | |
1930 | error: | |
4164020e SM |
1931 | if (status >= 0) { |
1932 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1933 | } | |
312df793 | 1934 | |
4164020e | 1935 | BT_ASSERT(!*result); |
312df793 PP |
1936 | |
1937 | end: | |
4164020e SM |
1938 | bt_common_destroy_lttng_live_url_parts(&parts); |
1939 | return status; | |
312df793 PP |
1940 | } |
1941 | ||
4164020e SM |
1942 | bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class, |
1943 | bt_private_query_executor *priv_query_exec, | |
1944 | const char *object, const bt_value *params, | |
1945 | __attribute__((unused)) void *method_data, | |
1946 | const bt_value **result) | |
7cdc2bab | 1947 | { |
1e690349 SM |
1948 | try { |
1949 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
1950 | bt2c::Logger logger {bt2::SelfComponentClass {comp_class}, | |
1951 | bt2::PrivateQueryExecutor {priv_query_exec}, | |
1952 | "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"}; | |
1953 | ||
1954 | if (strcmp(object, "sessions") == 0) { | |
1955 | status = lttng_live_query_list_sessions(params, result, logger); | |
1956 | } else if (strcmp(object, "babeltrace.support-info") == 0) { | |
1957 | status = lttng_live_query_support_info(params, result, logger); | |
1958 | } else { | |
1959 | BT_CPPLOGI_SPEC(logger, "Unknown query object `{}`", object); | |
1960 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; | |
1961 | goto end; | |
1962 | } | |
14f28187 FD |
1963 | |
1964 | end: | |
1e690349 SM |
1965 | return status; |
1966 | } catch (const std::bad_alloc&) { | |
1967 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1968 | } catch (const bt2::Error&) { | |
1969 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1970 | } | |
7cdc2bab MD |
1971 | } |
1972 | ||
14f28187 | 1973 | void lttng_live_component_finalize(bt_self_component_source *component) |
7cdc2bab | 1974 | { |
ef9e5f5d SM |
1975 | lttng_live_component::UP {static_cast<lttng_live_component *>( |
1976 | bt_self_component_get_data(bt_self_component_source_as_self_component(component)))}; | |
7cdc2bab MD |
1977 | } |
1978 | ||
4164020e SM |
1979 | static enum session_not_found_action |
1980 | parse_session_not_found_action_param(const bt_value *no_session_param) | |
14f28187 | 1981 | { |
4164020e SM |
1982 | enum session_not_found_action action; |
1983 | const char *no_session_act_str = bt_value_string_get(no_session_param); | |
1984 | ||
1985 | if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_CONTINUE_STR) == 0) { | |
1986 | action = SESSION_NOT_FOUND_ACTION_CONTINUE; | |
1987 | } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_FAIL_STR) == 0) { | |
1988 | action = SESSION_NOT_FOUND_ACTION_FAIL; | |
1989 | } else { | |
1990 | BT_ASSERT(strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0); | |
1991 | action = SESSION_NOT_FOUND_ACTION_END; | |
1992 | } | |
1993 | ||
1994 | return action; | |
14f28187 FD |
1995 | } |
1996 | ||
88730e42 SM |
1997 | static bt_param_validation_value_descr inputs_elem_descr = |
1998 | bt_param_validation_value_descr::makeString(); | |
80aff5ef SM |
1999 | |
2000 | static const char *sess_not_found_action_choices[] = { | |
4164020e SM |
2001 | SESS_NOT_FOUND_ACTION_CONTINUE_STR, |
2002 | SESS_NOT_FOUND_ACTION_FAIL_STR, | |
2003 | SESS_NOT_FOUND_ACTION_END_STR, | |
80aff5ef SM |
2004 | }; |
2005 | ||
2006 | static struct bt_param_validation_map_value_entry_descr params_descr[] = { | |
88730e42 SM |
2007 | {INPUTS_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, |
2008 | bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr)}, | |
2009 | {SESS_NOT_FOUND_ACTION_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, | |
2010 | bt_param_validation_value_descr::makeString(sess_not_found_action_choices)}, | |
4164020e SM |
2011 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; |
2012 | ||
2013 | static bt_component_class_initialize_method_status | |
0f5c5d5c | 2014 | lttng_live_component_create(const bt_value *params, bt_self_component_source *self_comp, |
ef9e5f5d | 2015 | lttng_live_component::UP& component) |
7cdc2bab | 2016 | { |
4164020e SM |
2017 | const bt_value *inputs_value; |
2018 | const bt_value *url_value; | |
2019 | const bt_value *value; | |
4164020e SM |
2020 | enum bt_param_validation_status validation_status; |
2021 | gchar *validation_error = NULL; | |
0f5c5d5c | 2022 | bt2c::Logger logger {bt2::SelfSourceComponent {self_comp}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"}; |
4164020e SM |
2023 | |
2024 | validation_status = bt_param_validation_validate(params, params_descr, &validation_error); | |
2025 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
ef9e5f5d | 2026 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
4164020e | 2027 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
6838d8aa | 2028 | bt2c::GCharUP errorFreer {validation_error}; |
0f5c5d5c | 2029 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "{}", validation_error); |
ef9e5f5d | 2030 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
4164020e SM |
2031 | } |
2032 | ||
ef9e5f5d | 2033 | auto lttng_live = bt2s::make_unique<lttng_live_component>(std::move(logger)); |
0f5c5d5c | 2034 | lttng_live->self_comp = bt_self_component_source_as_self_component(self_comp); |
4164020e SM |
2035 | lttng_live->max_query_size = MAX_QUERY_SIZE; |
2036 | lttng_live->has_msg_iter = false; | |
2037 | ||
2038 | inputs_value = bt_value_map_borrow_entry_value_const(params, INPUTS_PARAM); | |
2039 | url_value = bt_value_array_borrow_element_by_index_const(inputs_value, 0); | |
2780ec75 | 2040 | lttng_live->params.url = bt_value_string_get(url_value); |
4164020e SM |
2041 | |
2042 | value = bt_value_map_borrow_entry_value_const(params, SESS_NOT_FOUND_ACTION_PARAM); | |
2043 | if (value) { | |
2044 | lttng_live->params.sess_not_found_act = parse_session_not_found_action_param(value); | |
2045 | } else { | |
0f5c5d5c SM |
2046 | BT_CPPLOGI_SPEC(lttng_live->logger, |
2047 | "Optional `{}` parameter is missing: defaulting to `{}`.", | |
2048 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR); | |
4164020e SM |
2049 | lttng_live->params.sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE; |
2050 | } | |
2051 | ||
ef9e5f5d SM |
2052 | component = std::move(lttng_live); |
2053 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
d3e4dcd8 PP |
2054 | } |
2055 | ||
4164020e SM |
2056 | bt_component_class_initialize_method_status |
2057 | lttng_live_component_init(bt_self_component_source *self_comp_src, | |
7d91f1ac | 2058 | bt_self_component_source_configuration *, const bt_value *params, void *) |
f3bc2010 | 2059 | { |
1e690349 | 2060 | try { |
ef9e5f5d | 2061 | lttng_live_component::UP lttng_live; |
1e690349 SM |
2062 | bt_component_class_initialize_method_status ret; |
2063 | bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src); | |
2064 | bt_self_component_add_port_status add_port_status; | |
2065 | ||
ef9e5f5d | 2066 | ret = lttng_live_component_create(params, self_comp_src, lttng_live); |
1e690349 | 2067 | if (ret != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { |
ef9e5f5d | 2068 | return ret; |
1e690349 | 2069 | } |
4164020e | 2070 | |
1e690349 SM |
2071 | add_port_status = |
2072 | bt_self_component_source_add_output_port(self_comp_src, "out", NULL, NULL); | |
2073 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { | |
2074 | ret = (bt_component_class_initialize_method_status) add_port_status; | |
ef9e5f5d | 2075 | return ret; |
1e690349 | 2076 | } |
4164020e | 2077 | |
ef9e5f5d | 2078 | bt_self_component_set_data(self_comp, lttng_live.release()); |
7cdc2bab | 2079 | |
ef9e5f5d | 2080 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
1e690349 SM |
2081 | } catch (const std::bad_alloc&) { |
2082 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
2083 | } catch (const bt2::Error&) { | |
2084 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
2085 | } | |
d85ef162 | 2086 | } |