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