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 | /* | |
f8a11f24 SM |
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, | |
35e432d7 SM |
567 | struct lttng_live_stream_iterator *stream_iter, |
568 | bt2::ConstMessage::Shared& message, uint64_t timestamp) | |
7cdc2bab | 569 | { |
4164020e | 570 | BT_ASSERT(stream_iter->trace->clock_class); |
0f5c5d5c SM |
571 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
572 | "Emitting inactivity message for stream: ctf-stream-id={}, " | |
573 | "viewer-stream-id={}, timestamp={}", | |
574 | stream_iter->ctf_stream_class_id.value, stream_iter->viewer_stream_id, | |
575 | timestamp); | |
4164020e | 576 | |
35e432d7 SM |
577 | const auto msg = bt_message_message_iterator_inactivity_create( |
578 | lttng_live_msg_iter->self_msg_iter, stream_iter->trace->clock_class, timestamp); | |
579 | ||
4164020e | 580 | if (!msg) { |
0f5c5d5c SM |
581 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
582 | "Error emitting message iterator inactivity message"); | |
35e432d7 | 583 | return LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
4164020e SM |
584 | } |
585 | ||
35e432d7 SM |
586 | message = bt2::ConstMessage::Shared::createWithoutRef(msg); |
587 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
7cdc2bab MD |
588 | } |
589 | ||
4164020e SM |
590 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_stream( |
591 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
35e432d7 | 592 | struct lttng_live_stream_iterator *lttng_live_stream, bt2::ConstMessage::Shared& message) |
7cdc2bab | 593 | { |
4164020e SM |
594 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
595 | ||
596 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT) { | |
597 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
598 | } | |
599 | ||
600 | /* | |
601 | * Check if we already sent an inactivty message downstream for this | |
602 | * `current_inactivity_ts` value. | |
603 | */ | |
604 | if (lttng_live_stream->last_inactivity_ts.is_set && | |
605 | lttng_live_stream->current_inactivity_ts == lttng_live_stream->last_inactivity_ts.value) { | |
606 | lttng_live_stream_iterator_set_state(lttng_live_stream, | |
607 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA); | |
608 | ||
609 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
610 | goto end; | |
611 | } | |
612 | ||
613 | ret = emit_inactivity_message(lttng_live_msg_iter, lttng_live_stream, message, | |
614 | lttng_live_stream->current_inactivity_ts); | |
615 | ||
616 | lttng_live_stream->last_inactivity_ts.value = lttng_live_stream->current_inactivity_ts; | |
617 | lttng_live_stream->last_inactivity_ts.is_set = true; | |
14f28187 | 618 | end: |
4164020e | 619 | return ret; |
14f28187 FD |
620 | } |
621 | ||
7d91f1ac | 622 | static int live_get_msg_ts_ns(struct lttng_live_msg_iter *lttng_live_msg_iter, |
4164020e | 623 | const bt_message *msg, int64_t last_msg_ts_ns, int64_t *ts_ns) |
14f28187 | 624 | { |
4164020e SM |
625 | const bt_clock_snapshot *clock_snapshot = NULL; |
626 | int ret = 0; | |
4164020e SM |
627 | |
628 | BT_ASSERT_DBG(msg); | |
629 | BT_ASSERT_DBG(ts_ns); | |
630 | ||
0f5c5d5c SM |
631 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
632 | "Getting message's timestamp: iter-data-addr={}, msg-addr={}, " | |
633 | "last-msg-ts={}", | |
634 | fmt::ptr(lttng_live_msg_iter), fmt::ptr(msg), last_msg_ts_ns); | |
4164020e SM |
635 | |
636 | switch (bt_message_get_type(msg)) { | |
637 | case BT_MESSAGE_TYPE_EVENT: | |
4164020e SM |
638 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(msg); |
639 | break; | |
640 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
4164020e SM |
641 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg); |
642 | break; | |
643 | case BT_MESSAGE_TYPE_PACKET_END: | |
4164020e SM |
644 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(msg); |
645 | break; | |
646 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
4164020e SM |
647 | clock_snapshot = |
648 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg); | |
649 | break; | |
650 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
4164020e SM |
651 | clock_snapshot = |
652 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg); | |
653 | break; | |
654 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
655 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg); | |
656 | break; | |
657 | default: | |
658 | /* All the other messages have a higher priority */ | |
0f5c5d5c SM |
659 | BT_CPPLOGD_STR_SPEC(lttng_live_msg_iter->logger, |
660 | "Message has no timestamp: using the last message timestamp."); | |
4164020e SM |
661 | *ts_ns = last_msg_ts_ns; |
662 | goto end; | |
663 | } | |
664 | ||
4164020e SM |
665 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
666 | if (ret) { | |
0f5c5d5c SM |
667 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
668 | "Cannot get nanoseconds from Epoch of clock snapshot: " | |
669 | "clock-snapshot-addr={}", | |
670 | fmt::ptr(clock_snapshot)); | |
4164020e SM |
671 | goto error; |
672 | } | |
673 | ||
674 | goto end; | |
14f28187 | 675 | |
14f28187 | 676 | error: |
4164020e | 677 | ret = -1; |
7cdc2bab | 678 | |
7cdc2bab | 679 | end: |
4164020e | 680 | if (ret == 0) { |
0f5c5d5c SM |
681 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
682 | "Found message's timestamp: iter-data-addr={}, msg-addr={}, " | |
683 | "last-msg-ts={}, ts={}", | |
684 | fmt::ptr(lttng_live_msg_iter), fmt::ptr(msg), last_msg_ts_ns, *ts_ns); | |
4164020e SM |
685 | } |
686 | ||
687 | return ret; | |
7cdc2bab MD |
688 | } |
689 | ||
4164020e SM |
690 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_stream( |
691 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
35e432d7 | 692 | struct lttng_live_stream_iterator *lttng_live_stream, bt2::ConstMessage::Shared& message) |
7cdc2bab | 693 | { |
4164020e | 694 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
4164020e SM |
695 | enum ctf_msg_iter_status status; |
696 | uint64_t session_idx, trace_idx; | |
697 | ||
698 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
699 | struct lttng_live_session *session = | |
700 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
701 | ||
702 | if (session->new_streams_needed) { | |
0f5c5d5c SM |
703 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
704 | "Need an update for streams: " | |
705 | "session-id={}", | |
706 | session->id); | |
4164020e SM |
707 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
708 | goto end; | |
709 | } | |
710 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
711 | struct lttng_live_trace *trace = | |
712 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
713 | if (trace->metadata_stream_state == LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
0f5c5d5c SM |
714 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
715 | "Need an update for metadata stream: " | |
716 | "session-id={}, trace-id={}", | |
717 | session->id, trace->id); | |
4164020e SM |
718 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
719 | goto end; | |
720 | } | |
721 | } | |
722 | } | |
723 | ||
724 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_DATA) { | |
725 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
0f5c5d5c SM |
726 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
727 | "Invalid state of live stream iterator" | |
728 | "stream-iter-status={}", | |
729 | lttng_live_stream->state); | |
4164020e SM |
730 | goto end; |
731 | } | |
732 | ||
35e432d7 SM |
733 | const bt_message *msg; |
734 | status = ctf_msg_iter_get_next_message(lttng_live_stream->msg_iter.get(), &msg); | |
4164020e SM |
735 | switch (status) { |
736 | case CTF_MSG_ITER_STATUS_EOF: | |
737 | ret = LTTNG_LIVE_ITERATOR_STATUS_END; | |
738 | break; | |
739 | case CTF_MSG_ITER_STATUS_OK: | |
35e432d7 | 740 | message = bt2::ConstMessage::Shared::createWithoutRef(msg); |
4164020e SM |
741 | ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
742 | break; | |
743 | case CTF_MSG_ITER_STATUS_AGAIN: | |
744 | /* | |
745 | * Continue immediately (end of packet). The next | |
746 | * get_index may return AGAIN to delay the following | |
747 | * attempt. | |
748 | */ | |
749 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
750 | break; | |
751 | case CTF_MSG_ITER_STATUS_ERROR: | |
752 | default: | |
753 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
0f5c5d5c SM |
754 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
755 | "CTF message iterator failed to get next message: " | |
756 | "msg-iter={}, msg-iter-status={}", | |
757 | fmt::ptr(lttng_live_stream->msg_iter), status); | |
4164020e SM |
758 | break; |
759 | } | |
14f28187 FD |
760 | |
761 | end: | |
4164020e | 762 | return ret; |
7cdc2bab MD |
763 | } |
764 | ||
4164020e SM |
765 | static enum lttng_live_iterator_status |
766 | lttng_live_iterator_close_stream(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
767 | struct lttng_live_stream_iterator *stream_iter, | |
35e432d7 | 768 | bt2::ConstMessage::Shared& curr_msg) |
4a39caef | 769 | { |
4164020e | 770 | enum lttng_live_iterator_status live_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
4164020e | 771 | |
0f5c5d5c SM |
772 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
773 | "Closing live stream iterator: stream-name=\"{}\", " | |
774 | "viewer-stream-id={}", | |
e28ca558 | 775 | stream_iter->name, stream_iter->viewer_stream_id); |
4164020e SM |
776 | |
777 | /* | |
778 | * The viewer has hung up on us so we are closing the stream. The | |
779 | * `ctf_msg_iter` should simply realize that it needs to close the | |
780 | * stream properly by emitting the necessary stream end message. | |
781 | */ | |
35e432d7 | 782 | const bt_message *msg; |
4164020e | 783 | enum ctf_msg_iter_status status = |
35e432d7 | 784 | ctf_msg_iter_get_next_message(stream_iter->msg_iter.get(), &msg); |
4164020e SM |
785 | |
786 | if (status == CTF_MSG_ITER_STATUS_ERROR) { | |
0f5c5d5c SM |
787 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
788 | "Error getting the next message from CTF message iterator"); | |
4164020e SM |
789 | live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
790 | goto end; | |
791 | } else if (status == CTF_MSG_ITER_STATUS_EOF) { | |
0f5c5d5c SM |
792 | BT_CPPLOGI_SPEC(lttng_live_msg_iter->logger, |
793 | "Reached the end of the live stream iterator."); | |
4164020e SM |
794 | live_status = LTTNG_LIVE_ITERATOR_STATUS_END; |
795 | goto end; | |
796 | } | |
797 | ||
798 | BT_ASSERT(status == CTF_MSG_ITER_STATUS_OK); | |
4a39caef | 799 | |
35e432d7 SM |
800 | curr_msg = bt2::ConstMessage::Shared::createWithoutRef(msg); |
801 | ||
4a39caef | 802 | end: |
4164020e | 803 | return live_status; |
4a39caef FD |
804 | } |
805 | ||
7cdc2bab MD |
806 | /* |
807 | * helper function: | |
808 | * handle_no_data_streams() | |
809 | * retry: | |
810 | * - for each ACTIVE_NO_DATA stream: | |
811 | * - query relayd for stream data, or quiescence info. | |
812 | * - if need metadata, get metadata, goto retry. | |
813 | * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry | |
814 | * - if quiescent, move to QUIESCENT streams | |
815 | * - if fetched data, move to ACTIVE_DATA streams | |
816 | * (at this point each stream either has data, or is quiescent) | |
817 | * | |
818 | * | |
819 | * iterator_next: | |
820 | * handle_new_streams_and_metadata() | |
821 | * - query relayd for known streams, add them as ACTIVE_NO_DATA | |
822 | * - query relayd for metadata | |
823 | * | |
824 | * call handle_active_no_data_streams() | |
825 | * | |
826 | * handle_quiescent_streams() | |
827 | * - if at least one stream is ACTIVE_DATA: | |
828 | * - peek stream event with lowest timestamp -> next_ts | |
829 | * - for each quiescent stream | |
830 | * - if next_ts >= quiescent end | |
831 | * - set state to ACTIVE_NO_DATA | |
832 | * - else | |
833 | * - for each quiescent stream | |
834 | * - set state to ACTIVE_NO_DATA | |
835 | * | |
836 | * call handle_active_no_data_streams() | |
837 | * | |
838 | * handle_active_data_streams() | |
839 | * - if at least one stream is ACTIVE_DATA: | |
840 | * - get stream event with lowest timestamp from heap | |
d6e69534 | 841 | * - make that stream event the current message. |
7cdc2bab MD |
842 | * - move this stream heap position to its next event |
843 | * - if we need to fetch data from relayd, move | |
844 | * stream to ACTIVE_NO_DATA. | |
845 | * - return OK | |
846 | * - return AGAIN | |
847 | * | |
848 | * end criterion: ctrl-c on client. If relayd exits or the session | |
849 | * closes on the relay daemon side, we keep on waiting for streams. | |
850 | * Eventually handle --end timestamp (also an end criterion). | |
851 | * | |
852 | * When disconnected from relayd: try to re-connect endlessly. | |
853 | */ | |
4164020e SM |
854 | static enum lttng_live_iterator_status |
855 | lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
856 | struct lttng_live_stream_iterator *stream_iter, | |
35e432d7 | 857 | bt2::ConstMessage::Shared& curr_msg) |
7cdc2bab | 858 | { |
4164020e SM |
859 | enum lttng_live_iterator_status live_status; |
860 | ||
0f5c5d5c SM |
861 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
862 | "Advancing live stream iterator until next message if possible: " | |
863 | "stream-name=\"{}\", viewer-stream-id={}", | |
e28ca558 | 864 | stream_iter->name, stream_iter->viewer_stream_id); |
4164020e SM |
865 | |
866 | if (stream_iter->has_stream_hung_up) { | |
867 | /* | |
868 | * The stream has hung up and the stream was properly closed | |
869 | * during the last call to the current function. Return _END | |
870 | * status now so that this stream iterator is removed for the | |
871 | * stream iterator list. | |
872 | */ | |
873 | live_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
874 | goto end; | |
875 | } | |
4a39caef | 876 | |
7cdc2bab | 877 | retry: |
4164020e SM |
878 | LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter); |
879 | ||
880 | /* | |
881 | * Make sure we have the most recent metadata and possibly some new | |
882 | * streams. | |
883 | */ | |
884 | live_status = lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter); | |
885 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
886 | goto end; | |
887 | } | |
888 | ||
889 | live_status = | |
890 | lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter, stream_iter); | |
891 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
892 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
893 | /* | |
894 | * We overwrite `live_status` since `curr_msg` is | |
895 | * likely set to a valid message in this function. | |
896 | */ | |
897 | live_status = | |
898 | lttng_live_iterator_close_stream(lttng_live_msg_iter, stream_iter, curr_msg); | |
899 | } | |
900 | goto end; | |
901 | } | |
902 | ||
903 | live_status = lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter, | |
904 | stream_iter, curr_msg); | |
905 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
35e432d7 | 906 | BT_ASSERT(!curr_msg); |
4164020e SM |
907 | goto end; |
908 | } | |
35e432d7 | 909 | if (curr_msg) { |
4164020e SM |
910 | goto end; |
911 | } | |
912 | live_status = lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter, | |
913 | stream_iter, curr_msg); | |
914 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
35e432d7 | 915 | BT_ASSERT(!curr_msg); |
4164020e | 916 | } |
7cdc2bab MD |
917 | |
918 | end: | |
4164020e | 919 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_CONTINUE) { |
0f5c5d5c SM |
920 | BT_CPPLOGD_SPEC( |
921 | lttng_live_msg_iter->logger, | |
922 | "Ask the relay daemon for an updated view of the data and metadata streams"); | |
4164020e SM |
923 | goto retry; |
924 | } | |
14f28187 | 925 | |
0f5c5d5c SM |
926 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
927 | "Returning from advancing live stream iterator: status={}, " | |
928 | "stream-name=\"{}\", viewer-stream-id={}", | |
e28ca558 | 929 | live_status, stream_iter->name, stream_iter->viewer_stream_id); |
f93afbf9 | 930 | |
4164020e | 931 | return live_status; |
7cdc2bab MD |
932 | } |
933 | ||
35e432d7 | 934 | static bool is_discarded_packet_or_event_message(const bt2::ConstMessage msg) |
8ec4d5ff | 935 | { |
35e432d7 SM |
936 | return msg.type() == bt2::MessageType::DiscardedEvents || |
937 | msg.type() == bt2::MessageType::DiscardedPackets; | |
8ec4d5ff JG |
938 | } |
939 | ||
4164020e SM |
940 | static enum lttng_live_iterator_status |
941 | adjust_discarded_packets_message(bt_self_message_iterator *iter, const bt_stream *stream, | |
35e432d7 | 942 | const bt_message *msg_in, bt2::ConstMessage::Shared& msg_out, |
4164020e | 943 | uint64_t new_begin_ts) |
8ec4d5ff | 944 | { |
4164020e SM |
945 | enum bt_property_availability availability; |
946 | const bt_clock_snapshot *clock_snapshot; | |
947 | uint64_t end_ts; | |
948 | uint64_t count; | |
949 | ||
950 | clock_snapshot = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in); | |
951 | end_ts = bt_clock_snapshot_get_value(clock_snapshot); | |
952 | ||
953 | availability = bt_message_discarded_packets_get_count(msg_in, &count); | |
954 | BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); | |
955 | ||
35e432d7 | 956 | const auto msg = bt_message_discarded_packets_create_with_default_clock_snapshots( |
4164020e | 957 | iter, stream, new_begin_ts, end_ts); |
35e432d7 SM |
958 | |
959 | if (!msg) { | |
960 | return LTTNG_LIVE_ITERATOR_STATUS_NOMEM; | |
4164020e SM |
961 | } |
962 | ||
35e432d7 SM |
963 | bt_message_discarded_packets_set_count(msg, count); |
964 | msg_out = bt2::ConstMessage::Shared::createWithoutRef(msg); | |
965 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
8ec4d5ff JG |
966 | } |
967 | ||
4164020e SM |
968 | static enum lttng_live_iterator_status |
969 | adjust_discarded_events_message(bt_self_message_iterator *iter, const bt_stream *stream, | |
35e432d7 | 970 | const bt_message *msg_in, bt2::ConstMessage::Shared& msg_out, |
4164020e | 971 | uint64_t new_begin_ts) |
8ec4d5ff | 972 | { |
4164020e SM |
973 | enum bt_property_availability availability; |
974 | const bt_clock_snapshot *clock_snapshot; | |
975 | uint64_t end_ts; | |
976 | uint64_t count; | |
977 | ||
978 | clock_snapshot = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in); | |
979 | end_ts = bt_clock_snapshot_get_value(clock_snapshot); | |
980 | ||
981 | availability = bt_message_discarded_events_get_count(msg_in, &count); | |
982 | BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); | |
983 | ||
35e432d7 | 984 | const auto msg = bt_message_discarded_events_create_with_default_clock_snapshots( |
4164020e | 985 | iter, stream, new_begin_ts, end_ts); |
35e432d7 SM |
986 | |
987 | if (!msg) { | |
988 | return LTTNG_LIVE_ITERATOR_STATUS_NOMEM; | |
4164020e SM |
989 | } |
990 | ||
35e432d7 SM |
991 | bt_message_discarded_events_set_count(msg, count); |
992 | msg_out = bt2::ConstMessage::Shared::createWithoutRef(msg); | |
993 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
8ec4d5ff JG |
994 | } |
995 | ||
4164020e SM |
996 | static enum lttng_live_iterator_status |
997 | handle_late_message(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
998 | struct lttng_live_stream_iterator *stream_iter, int64_t late_msg_ts_ns, | |
35e432d7 | 999 | const bt2::ConstMessage& late_msg) |
285951be | 1000 | { |
4164020e SM |
1001 | const bt_clock_class *clock_class; |
1002 | const bt_stream_class *stream_class; | |
1003 | enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status; | |
1004 | int64_t last_inactivity_ts_ns; | |
1005 | enum lttng_live_iterator_status stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1006 | enum lttng_live_iterator_status adjust_status; | |
35e432d7 | 1007 | bt2::ConstMessage::Shared adjusted_message; |
4164020e SM |
1008 | |
1009 | /* | |
1010 | * The timestamp of the current message is before the last message sent | |
1011 | * by this component. We CANNOT send it as is. | |
1012 | * | |
1013 | * The only expected scenario in which that could happen is the | |
e7401568 | 1014 | * following, everything else is a bug in this component, relay daemon, |
4164020e SM |
1015 | * or CTF parser. |
1016 | * | |
1017 | * Expected scenario: The CTF message iterator emitted discarded | |
1018 | * packets and discarded events with synthesized beginning and end | |
1019 | * timestamps from the bounds of the last known packet and the newly | |
1020 | * decoded packet header. The CTF message iterator is not aware of | |
1021 | * stream inactivity beacons. Hence, we have to adjust the beginning | |
1022 | * timestamp of those types of messages if a stream signalled its | |
1023 | * inactivity up until _after_ the last known packet's beginning | |
1024 | * timestamp. | |
1025 | * | |
1026 | * Otherwise, the monotonicity guarantee of message timestamps would | |
1027 | * not be preserved. | |
1028 | * | |
1029 | * In short, the only scenario in which it's okay and fixable to | |
1030 | * received a late message is when: | |
1031 | * 1. the late message is a discarded packets or discarded events | |
f8a11f24 | 1032 | * message, |
4164020e SM |
1033 | * 2. this stream produced an inactivity message downstream, and |
1034 | * 3. the timestamp of the late message is within the inactivity | |
f8a11f24 | 1035 | * timespan we sent downstream through the inactivity message. |
4164020e SM |
1036 | */ |
1037 | ||
0f5c5d5c SM |
1038 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1039 | "Handling late message on live stream iterator: " | |
1040 | "stream-name=\"{}\", viewer-stream-id={}", | |
e28ca558 | 1041 | stream_iter->name, stream_iter->viewer_stream_id); |
4164020e SM |
1042 | |
1043 | if (!stream_iter->last_inactivity_ts.is_set) { | |
0f5c5d5c SM |
1044 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1045 | "Invalid live stream state: " | |
1046 | "have a late message when no inactivity message " | |
1047 | "was ever sent for that stream."); | |
4164020e SM |
1048 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1049 | goto end; | |
1050 | } | |
1051 | ||
1052 | if (!is_discarded_packet_or_event_message(late_msg)) { | |
0f5c5d5c SM |
1053 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1054 | "Invalid live stream state: " | |
1055 | "have a late message that is not a packet discarded or " | |
1056 | "event discarded message: late-msg-type={}", | |
35e432d7 | 1057 | late_msg.type()); |
4164020e SM |
1058 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1059 | goto end; | |
1060 | } | |
1061 | ||
0b68f2bc | 1062 | stream_class = bt_stream_borrow_class_const(stream_iter->stream->libObjPtr()); |
4164020e SM |
1063 | clock_class = bt_stream_class_borrow_default_clock_class_const(stream_class); |
1064 | ||
1065 | ts_ns_status = bt_clock_class_cycles_to_ns_from_origin( | |
1066 | clock_class, stream_iter->last_inactivity_ts.value, &last_inactivity_ts_ns); | |
1067 | if (ts_ns_status != BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK) { | |
0f5c5d5c SM |
1068 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1069 | "Error converting last " | |
1070 | "inactivity message timestamp to nanoseconds"); | |
4164020e SM |
1071 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1072 | goto end; | |
1073 | } | |
1074 | ||
1075 | if (last_inactivity_ts_ns <= late_msg_ts_ns) { | |
0f5c5d5c SM |
1076 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1077 | "Invalid live stream state: " | |
1078 | "have a late message that is none included in a stream " | |
1079 | "inactivity timespan: last-inactivity-ts-ns={}, " | |
1080 | "late-msg-ts-ns={}", | |
1081 | last_inactivity_ts_ns, late_msg_ts_ns); | |
4164020e SM |
1082 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1083 | goto end; | |
1084 | } | |
1085 | ||
1086 | /* | |
1087 | * We now know that it's okay for this message to be late, we can now | |
1088 | * adjust its timestamp to ensure monotonicity. | |
1089 | */ | |
0f5c5d5c SM |
1090 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1091 | "Adjusting the timestamp of late message: late-msg-type={}, " | |
1092 | "msg-new-ts-ns={}", | |
35e432d7 SM |
1093 | late_msg.type(), stream_iter->last_inactivity_ts.value); |
1094 | switch (late_msg.type()) { | |
1095 | case bt2::MessageType::DiscardedEvents: | |
4164020e | 1096 | adjust_status = adjust_discarded_events_message( |
35e432d7 SM |
1097 | lttng_live_msg_iter->self_msg_iter, stream_iter->stream->libObjPtr(), |
1098 | late_msg.libObjPtr(), adjusted_message, stream_iter->last_inactivity_ts.value); | |
4164020e | 1099 | break; |
35e432d7 | 1100 | case bt2::MessageType::DiscardedPackets: |
4164020e | 1101 | adjust_status = adjust_discarded_packets_message( |
35e432d7 SM |
1102 | lttng_live_msg_iter->self_msg_iter, stream_iter->stream->libObjPtr(), |
1103 | late_msg.libObjPtr(), adjusted_message, stream_iter->last_inactivity_ts.value); | |
4164020e SM |
1104 | break; |
1105 | default: | |
1106 | bt_common_abort(); | |
1107 | } | |
1108 | ||
1109 | if (adjust_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1110 | stream_iter_status = adjust_status; | |
1111 | goto end; | |
1112 | } | |
1113 | ||
1114 | BT_ASSERT_DBG(adjusted_message); | |
1115 | stream_iter->current_msg = adjusted_message; | |
1116 | stream_iter->current_msg_ts_ns = last_inactivity_ts_ns; | |
cefd84f8 | 1117 | |
285951be | 1118 | end: |
4164020e | 1119 | return stream_iter_status; |
285951be FD |
1120 | } |
1121 | ||
4164020e SM |
1122 | static enum lttng_live_iterator_status |
1123 | next_stream_iterator_for_trace(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1124 | struct lttng_live_trace *live_trace, | |
1125 | struct lttng_live_stream_iterator **youngest_trace_stream_iter) | |
7cdc2bab | 1126 | { |
4164020e | 1127 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; |
4164020e | 1128 | enum lttng_live_iterator_status stream_iter_status; |
4164020e SM |
1129 | int64_t youngest_candidate_msg_ts = INT64_MAX; |
1130 | uint64_t stream_iter_idx; | |
1131 | ||
1132 | BT_ASSERT_DBG(live_trace); | |
1133 | BT_ASSERT_DBG(live_trace->stream_iterators); | |
1134 | ||
0f5c5d5c SM |
1135 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1136 | "Finding the next stream iterator for trace: " | |
1137 | "trace-id={}", | |
1138 | live_trace->id); | |
4164020e SM |
1139 | /* |
1140 | * Update the current message of every stream iterators of this trace. | |
1141 | * The current msg of every stream must have a timestamp equal or | |
1142 | * larger than the last message returned by this iterator. We must | |
1143 | * ensure monotonicity. | |
1144 | */ | |
1145 | stream_iter_idx = 0; | |
1146 | while (stream_iter_idx < live_trace->stream_iterators->len) { | |
1147 | bool stream_iter_is_ended = false; | |
1148 | struct lttng_live_stream_iterator *stream_iter = | |
1149 | (lttng_live_stream_iterator *) g_ptr_array_index(live_trace->stream_iterators, | |
1150 | stream_iter_idx); | |
1151 | ||
1152 | /* | |
1153 | * If there is no current message for this stream, go fetch | |
1154 | * one. | |
1155 | */ | |
1156 | while (!stream_iter->current_msg) { | |
35e432d7 | 1157 | bt2::ConstMessage::Shared msg; |
4164020e SM |
1158 | int64_t curr_msg_ts_ns = INT64_MAX; |
1159 | ||
1160 | stream_iter_status = | |
35e432d7 | 1161 | lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter, stream_iter, msg); |
4164020e SM |
1162 | |
1163 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1164 | stream_iter_is_ended = true; | |
1165 | break; | |
1166 | } | |
1167 | ||
1168 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1169 | goto end; | |
1170 | } | |
1171 | ||
1172 | BT_ASSERT_DBG(msg); | |
1173 | ||
0f5c5d5c SM |
1174 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1175 | "Live stream iterator returned message: msg-type={}, " | |
1176 | "stream-name=\"{}\", viewer-stream-id={}", | |
35e432d7 | 1177 | msg->type(), stream_iter->name, stream_iter->viewer_stream_id); |
4164020e SM |
1178 | |
1179 | /* | |
1180 | * Get the timestamp in nanoseconds from origin of this | |
e7401568 | 1181 | * message. |
4164020e | 1182 | */ |
35e432d7 SM |
1183 | live_get_msg_ts_ns(lttng_live_msg_iter, msg->libObjPtr(), |
1184 | lttng_live_msg_iter->last_msg_ts_ns, &curr_msg_ts_ns); | |
4164020e SM |
1185 | |
1186 | /* | |
1187 | * Check if the message of the current live stream | |
1188 | * iterator occurred at the exact same time or after the | |
1189 | * last message returned by this component's message | |
1190 | * iterator. If not, we need to handle it with care. | |
1191 | */ | |
1192 | if (curr_msg_ts_ns >= lttng_live_msg_iter->last_msg_ts_ns) { | |
35e432d7 | 1193 | stream_iter->current_msg = std::move(msg); |
4164020e SM |
1194 | stream_iter->current_msg_ts_ns = curr_msg_ts_ns; |
1195 | } else { | |
1196 | /* | |
1197 | * We received a message from the past. This | |
1198 | * may be fixable but it can also be an error. | |
1199 | */ | |
1200 | stream_iter_status = | |
35e432d7 | 1201 | handle_late_message(lttng_live_msg_iter, stream_iter, curr_msg_ts_ns, *msg); |
4164020e | 1202 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
0f5c5d5c SM |
1203 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1204 | "Late message could not be handled correctly: " | |
1205 | "lttng-live-msg-iter-addr={}, " | |
1206 | "stream-name=\"{}\", " | |
1207 | "curr-msg-ts={}, last-msg-ts={}", | |
e28ca558 SM |
1208 | fmt::ptr(lttng_live_msg_iter), stream_iter->name, |
1209 | curr_msg_ts_ns, | |
0f5c5d5c | 1210 | lttng_live_msg_iter->last_msg_ts_ns); |
4164020e SM |
1211 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1212 | goto end; | |
1213 | } | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); | |
1218 | ||
1219 | if (!stream_iter_is_ended) { | |
1220 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || | |
1221 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1222 | /* | |
1223 | * Update the current best candidate message | |
1224 | * for the stream iterator of this live trace | |
1225 | * to be forwarded downstream. | |
1226 | */ | |
1227 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1228 | youngest_candidate_stream_iter = stream_iter; | |
1229 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
1230 | /* | |
1231 | * Order the messages in an arbitrary but | |
1232 | * deterministic way. | |
1233 | */ | |
1234 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); | |
1235 | int ret = common_muxing_compare_messages( | |
35e432d7 SM |
1236 | stream_iter->current_msg->libObjPtr(), |
1237 | youngest_candidate_stream_iter->current_msg->libObjPtr()); | |
4164020e SM |
1238 | if (ret < 0) { |
1239 | /* | |
1240 | * The `youngest_candidate_stream_iter->current_msg` | |
1241 | * should go first. Update the next | |
1242 | * iterator and the current timestamp. | |
1243 | */ | |
1244 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1245 | youngest_candidate_stream_iter = stream_iter; | |
1246 | } else if (ret == 0) { | |
1247 | /* | |
1248 | * Unable to pick which one should go | |
1249 | * first. | |
1250 | */ | |
0f5c5d5c SM |
1251 | BT_CPPLOGW_SPEC( |
1252 | lttng_live_msg_iter->logger, | |
4164020e | 1253 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " |
0f5c5d5c SM |
1254 | "stream-iter-addr={}" |
1255 | "stream-iter-addr={}", | |
1256 | fmt::ptr(stream_iter), fmt::ptr(youngest_candidate_stream_iter)); | |
4164020e SM |
1257 | } |
1258 | } | |
1259 | ||
1260 | stream_iter_idx++; | |
1261 | } else { | |
1262 | /* | |
1263 | * The live stream iterator has ended. That | |
1264 | * iterator is removed from the array, but | |
1265 | * there is no need to increment | |
1266 | * stream_iter_idx as | |
1267 | * g_ptr_array_remove_index_fast replaces the | |
1268 | * removed element with the array's last | |
1269 | * element. | |
1270 | */ | |
1271 | g_ptr_array_remove_index_fast(live_trace->stream_iterators, stream_iter_idx); | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | if (youngest_candidate_stream_iter) { | |
1276 | *youngest_trace_stream_iter = youngest_candidate_stream_iter; | |
1277 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1278 | } else { | |
1279 | /* | |
1280 | * The only case where we don't have a candidate for this trace | |
1281 | * is if we reached the end of all the iterators. | |
1282 | */ | |
1283 | BT_ASSERT(live_trace->stream_iterators->len == 0); | |
1284 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1285 | } | |
14f28187 FD |
1286 | |
1287 | end: | |
4164020e | 1288 | return stream_iter_status; |
14f28187 FD |
1289 | } |
1290 | ||
4164020e SM |
1291 | static enum lttng_live_iterator_status |
1292 | next_stream_iterator_for_session(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1293 | struct lttng_live_session *session, | |
1294 | struct lttng_live_stream_iterator **youngest_session_stream_iter) | |
14f28187 | 1295 | { |
4164020e SM |
1296 | enum lttng_live_iterator_status stream_iter_status; |
1297 | uint64_t trace_idx = 0; | |
1298 | int64_t youngest_candidate_msg_ts = INT64_MAX; | |
1299 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; | |
1300 | ||
0f5c5d5c SM |
1301 | BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, |
1302 | "Finding the next stream iterator for session: " | |
1303 | "session-id={}", | |
1304 | session->id); | |
4164020e SM |
1305 | /* |
1306 | * Make sure we are attached to the session and look for new streams | |
1307 | * and metadata. | |
1308 | */ | |
1309 | stream_iter_status = lttng_live_get_session(lttng_live_msg_iter, session); | |
1310 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK && | |
1311 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_CONTINUE && | |
1312 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1313 | goto end; | |
1314 | } | |
1315 | ||
1316 | BT_ASSERT_DBG(session->traces); | |
1317 | ||
1318 | while (trace_idx < session->traces->len) { | |
1319 | bool trace_is_ended = false; | |
1320 | struct lttng_live_stream_iterator *stream_iter; | |
1321 | struct lttng_live_trace *trace = | |
1322 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
1323 | ||
1324 | stream_iter_status = | |
1325 | next_stream_iterator_for_trace(lttng_live_msg_iter, trace, &stream_iter); | |
1326 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1327 | /* | |
1328 | * All the live stream iterators for this trace are | |
1329 | * ENDed. Remove the trace from this session. | |
1330 | */ | |
1331 | trace_is_ended = true; | |
1332 | } else if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1333 | goto end; | |
1334 | } | |
1335 | ||
1336 | if (!trace_is_ended) { | |
1337 | BT_ASSERT_DBG(stream_iter); | |
1338 | ||
1339 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || | |
1340 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1341 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1342 | youngest_candidate_stream_iter = stream_iter; | |
1343 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
1344 | /* | |
1345 | * Order the messages in an arbitrary but | |
1346 | * deterministic way. | |
1347 | */ | |
1348 | int ret = common_muxing_compare_messages( | |
35e432d7 SM |
1349 | stream_iter->current_msg->libObjPtr(), |
1350 | youngest_candidate_stream_iter->current_msg->libObjPtr()); | |
4164020e SM |
1351 | if (ret < 0) { |
1352 | /* | |
1353 | * The `youngest_candidate_stream_iter->current_msg` | |
1354 | * should go first. Update the next iterator | |
1355 | * and the current timestamp. | |
1356 | */ | |
1357 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1358 | youngest_candidate_stream_iter = stream_iter; | |
1359 | } else if (ret == 0) { | |
1360 | /* Unable to pick which one should go first. */ | |
0f5c5d5c SM |
1361 | BT_CPPLOGW_SPEC( |
1362 | lttng_live_msg_iter->logger, | |
4164020e | 1363 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " |
0f5c5d5c SM |
1364 | "stream-iter-addr={}" |
1365 | "youngest-candidate-stream-iter-addr={}", | |
1366 | fmt::ptr(stream_iter), fmt::ptr(youngest_candidate_stream_iter)); | |
4164020e SM |
1367 | } |
1368 | } | |
1369 | trace_idx++; | |
1370 | } else { | |
1371 | /* | |
1372 | * trace_idx is not incremented since | |
1373 | * g_ptr_array_remove_index_fast replaces the | |
1374 | * element at trace_idx with the array's last element. | |
1375 | */ | |
1376 | g_ptr_array_remove_index_fast(session->traces, trace_idx); | |
1377 | } | |
1378 | } | |
1379 | if (youngest_candidate_stream_iter) { | |
1380 | *youngest_session_stream_iter = youngest_candidate_stream_iter; | |
1381 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1382 | } else { | |
1383 | /* | |
1384 | * The only cases where we don't have a candidate for this | |
1385 | * trace is: | |
1386 | * 1. if we reached the end of all the iterators of all the | |
1387 | * traces of this session, | |
1388 | * 2. if we never had live stream iterator in the first place. | |
1389 | * | |
1390 | * In either cases, we return END. | |
1391 | */ | |
1392 | BT_ASSERT(session->traces->len == 0); | |
1393 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1394 | } | |
7cdc2bab | 1395 | end: |
4164020e | 1396 | return stream_iter_status; |
14f28187 FD |
1397 | } |
1398 | ||
4164020e | 1399 | static inline void put_messages(bt_message_array_const msgs, uint64_t count) |
14f28187 | 1400 | { |
4164020e | 1401 | uint64_t i; |
14f28187 | 1402 | |
4164020e SM |
1403 | for (i = 0; i < count; i++) { |
1404 | BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]); | |
1405 | } | |
7cdc2bab MD |
1406 | } |
1407 | ||
4164020e SM |
1408 | bt_message_iterator_class_next_method_status |
1409 | lttng_live_msg_iter_next(bt_self_message_iterator *self_msg_it, bt_message_array_const msgs, | |
1410 | uint64_t capacity, uint64_t *count) | |
d3eb6e8f | 1411 | { |
1e690349 SM |
1412 | try { |
1413 | bt_message_iterator_class_next_method_status status; | |
1414 | enum lttng_live_viewer_status viewer_status; | |
1415 | struct lttng_live_msg_iter *lttng_live_msg_iter = | |
1416 | (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_it); | |
1417 | struct lttng_live_component *lttng_live = lttng_live_msg_iter->lttng_live_comp; | |
1418 | enum lttng_live_iterator_status stream_iter_status; | |
1419 | uint64_t session_idx; | |
4164020e | 1420 | |
1e690349 | 1421 | *count = 0; |
4164020e | 1422 | |
1e690349 | 1423 | BT_ASSERT_DBG(lttng_live_msg_iter); |
4164020e | 1424 | |
1e690349 SM |
1425 | if (G_UNLIKELY(lttng_live_msg_iter->was_interrupted)) { |
1426 | /* | |
f8a11f24 SM |
1427 | * The iterator was interrupted in a previous call to the |
1428 | * `_next()` method. We currently do not support generating | |
1429 | * messages after such event. The babeltrace2 CLI should never | |
1430 | * be running the graph after being interrupted. So this check | |
1431 | * is to prevent other graph users from using this live | |
1432 | * iterator in an messed up internal state. | |
1433 | */ | |
1e690349 SM |
1434 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
1435 | BT_CPPLOGE_APPEND_CAUSE_SPEC( | |
1436 | lttng_live_msg_iter->logger, | |
1437 | "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event."); | |
1438 | goto end; | |
1439 | } | |
4164020e | 1440 | |
1e690349 | 1441 | /* |
f8a11f24 SM |
1442 | * Clear all the invalid message reference that might be left over in |
1443 | * the output array. | |
1444 | */ | |
1e690349 | 1445 | memset(msgs, 0, capacity * sizeof(*msgs)); |
4164020e | 1446 | |
1e690349 | 1447 | /* |
f8a11f24 SM |
1448 | * If no session are exposed on the relay found at the url provided by |
1449 | * the user, session count will be 0. In this case, we return status | |
1450 | * end to return gracefully. | |
1451 | */ | |
1e690349 SM |
1452 | if (lttng_live_msg_iter->sessions->len == 0) { |
1453 | if (lttng_live->params.sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
1454 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
1455 | goto end; | |
1456 | } else { | |
1457 | /* | |
f8a11f24 SM |
1458 | * The are no more active session for this session |
1459 | * name. Retry to create a viewer session for the | |
1460 | * requested session name. | |
1461 | */ | |
1e690349 SM |
1462 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); |
1463 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1464 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1465 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1466 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1467 | "Error creating LTTng live viewer session"); | |
1468 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1469 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; | |
1470 | } else { | |
1471 | bt_common_abort(); | |
1472 | } | |
1473 | goto end; | |
4164020e | 1474 | } |
4164020e SM |
1475 | } |
1476 | } | |
4164020e | 1477 | |
1e690349 SM |
1478 | if (lttng_live_msg_iter->active_stream_iter == 0) { |
1479 | lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter); | |
1480 | } | |
4164020e | 1481 | |
1e690349 | 1482 | /* |
f8a11f24 SM |
1483 | * Here the muxing of message is done. |
1484 | * | |
1485 | * We need to iterate over all the streams of all the traces of all the | |
1486 | * viewer sessions in order to get the message with the smallest | |
1487 | * timestamp. In this case, a session is a viewer session and there is | |
1488 | * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or | |
1489 | * kernel). Each viewer session can have multiple traces, for example, | |
1490 | * 64bit UST viewer sessions could have multiple per-pid traces. | |
1491 | * | |
1492 | * We iterate over the streams of each traces to update and see what is | |
1493 | * their next message's timestamp. From those timestamps, we select the | |
1494 | * message with the smallest timestamp as the best candidate message | |
1495 | * for that trace and do the same thing across all the sessions. | |
1496 | * | |
1497 | * We then compare the timestamp of best candidate message of all the | |
1498 | * sessions to pick the message with the smallest timestamp and we | |
1499 | * return it. | |
1500 | */ | |
1e690349 SM |
1501 | while (*count < capacity) { |
1502 | struct lttng_live_stream_iterator *youngest_stream_iter = NULL, | |
1503 | *candidate_stream_iter = NULL; | |
1504 | int64_t youngest_msg_ts_ns = INT64_MAX; | |
1505 | ||
1506 | BT_ASSERT_DBG(lttng_live_msg_iter->sessions); | |
1507 | session_idx = 0; | |
1508 | while (session_idx < lttng_live_msg_iter->sessions->len) { | |
1509 | struct lttng_live_session *session = (lttng_live_session *) g_ptr_array_index( | |
1510 | lttng_live_msg_iter->sessions, session_idx); | |
1511 | ||
1512 | /* Find the best candidate message to send downstream. */ | |
1513 | stream_iter_status = next_stream_iterator_for_session(lttng_live_msg_iter, session, | |
1514 | &candidate_stream_iter); | |
1515 | ||
1516 | /* If we receive an END status, it means that either: | |
f8a11f24 SM |
1517 | * - Those traces never had active streams (UST with no |
1518 | * data produced yet), | |
1519 | * - All live stream iterators have ENDed. | |
1520 | */ | |
1e690349 SM |
1521 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { |
1522 | if (session->closed && session->traces->len == 0) { | |
1523 | /* | |
f8a11f24 SM |
1524 | * Remove the session from the list. |
1525 | * session_idx is not modified since | |
1526 | * g_ptr_array_remove_index_fast | |
1527 | * replaces the the removed element with | |
1528 | * the array's last element. | |
1529 | */ | |
1e690349 SM |
1530 | g_ptr_array_remove_index_fast(lttng_live_msg_iter->sessions, session_idx); |
1531 | } else { | |
1532 | session_idx++; | |
1533 | } | |
1534 | continue; | |
4164020e | 1535 | } |
4164020e | 1536 | |
1e690349 SM |
1537 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
1538 | goto return_status; | |
1539 | } | |
4164020e | 1540 | |
1e690349 SM |
1541 | if (G_UNLIKELY(youngest_stream_iter == NULL) || |
1542 | candidate_stream_iter->current_msg_ts_ns < youngest_msg_ts_ns) { | |
1543 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; | |
1544 | youngest_stream_iter = candidate_stream_iter; | |
1545 | } else if (candidate_stream_iter->current_msg_ts_ns == youngest_msg_ts_ns) { | |
1546 | /* | |
f8a11f24 SM |
1547 | * The currently selected message to be sent |
1548 | * downstream next has the exact same timestamp | |
1549 | * that of the current candidate message. We | |
1550 | * must break the tie in a predictable manner. | |
1551 | */ | |
1e690349 SM |
1552 | BT_CPPLOGD_STR_SPEC( |
1553 | lttng_live_msg_iter->logger, | |
1554 | "Two of the next message candidates have the same timestamps, pick one deterministically."); | |
1555 | /* | |
f8a11f24 SM |
1556 | * Order the messages in an arbitrary but |
1557 | * deterministic way. | |
1558 | */ | |
35e432d7 SM |
1559 | int ret = common_muxing_compare_messages( |
1560 | candidate_stream_iter->current_msg->libObjPtr(), | |
1561 | youngest_stream_iter->current_msg->libObjPtr()); | |
1e690349 SM |
1562 | if (ret < 0) { |
1563 | /* | |
f8a11f24 SM |
1564 | * The `candidate_stream_iter->current_msg` |
1565 | * should go first. Update the next | |
1566 | * iterator and the current timestamp. | |
1567 | */ | |
1e690349 SM |
1568 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; |
1569 | youngest_stream_iter = candidate_stream_iter; | |
1570 | } else if (ret == 0) { | |
1571 | /* Unable to pick which one should go first. */ | |
1572 | BT_CPPLOGW_SPEC( | |
1573 | lttng_live_msg_iter->logger, | |
1574 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1575 | "next-stream-iter-addr={}" | |
1576 | "candidate-stream-iter-addr={}", | |
1577 | fmt::ptr(youngest_stream_iter), fmt::ptr(candidate_stream_iter)); | |
1578 | } | |
4164020e | 1579 | } |
4164020e | 1580 | |
1e690349 SM |
1581 | session_idx++; |
1582 | } | |
4164020e | 1583 | |
1e690349 SM |
1584 | if (!youngest_stream_iter) { |
1585 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
1586 | goto return_status; | |
1587 | } | |
4164020e | 1588 | |
1e690349 SM |
1589 | BT_ASSERT_DBG(youngest_stream_iter->current_msg); |
1590 | /* Ensure monotonicity. */ | |
1591 | BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <= | |
1592 | youngest_stream_iter->current_msg_ts_ns); | |
4164020e | 1593 | |
1e690349 | 1594 | /* |
f8a11f24 SM |
1595 | * Insert the next message to the message batch. This will set |
1596 | * stream iterator current message to NULL so that next time | |
1597 | * we fetch the next message of that stream iterator | |
1598 | */ | |
35e432d7 | 1599 | msgs[*count] = youngest_stream_iter->current_msg.release().libObjPtr(); |
1e690349 | 1600 | (*count)++; |
4164020e | 1601 | |
1e690349 SM |
1602 | /* Update the last timestamp in nanoseconds sent downstream. */ |
1603 | lttng_live_msg_iter->last_msg_ts_ns = youngest_msg_ts_ns; | |
1604 | youngest_stream_iter->current_msg_ts_ns = INT64_MAX; | |
4164020e | 1605 | |
1e690349 SM |
1606 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
1607 | } | |
f79c2d7a FD |
1608 | |
1609 | return_status: | |
1e690349 SM |
1610 | switch (stream_iter_status) { |
1611 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
1612 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
1613 | /* | |
f8a11f24 SM |
1614 | * If we gathered messages, return _OK even if the graph was |
1615 | * interrupted. This allows for the components downstream to at | |
1616 | * least get the those messages. If the graph was indeed | |
1617 | * interrupted there should not be another _next() call as the | |
1618 | * application will tear down the graph. This component class | |
1619 | * doesn't support restarting after an interruption. | |
1620 | */ | |
1e690349 SM |
1621 | if (*count > 0) { |
1622 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
1623 | } else { | |
1624 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; | |
1625 | } | |
1626 | break; | |
1627 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
1628 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
1629 | break; | |
1630 | case LTTNG_LIVE_ITERATOR_STATUS_NOMEM: | |
1631 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1632 | "Memory error preparing the next batch of messages: " | |
1633 | "live-iter-status={}", | |
1634 | stream_iter_status); | |
1635 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; | |
1636 | break; | |
1637 | case LTTNG_LIVE_ITERATOR_STATUS_ERROR: | |
1638 | case LTTNG_LIVE_ITERATOR_STATUS_INVAL: | |
1639 | case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED: | |
1640 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1641 | "Error preparing the next batch of messages: " | |
1642 | "live-iter-status={}", | |
1643 | stream_iter_status); | |
4164020e | 1644 | |
1e690349 SM |
1645 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
1646 | /* Put all existing messages on error. */ | |
1647 | put_messages(msgs, *count); | |
1648 | break; | |
1649 | default: | |
1650 | bt_common_abort(); | |
1651 | } | |
14f28187 | 1652 | |
f79c2d7a | 1653 | end: |
1e690349 SM |
1654 | return status; |
1655 | } catch (const std::bad_alloc&) { | |
1656 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; | |
1657 | } catch (const bt2::Error&) { | |
1658 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1659 | } | |
7cdc2bab | 1660 | } |
41a2b7ae | 1661 | |
4164020e SM |
1662 | static struct lttng_live_msg_iter * |
1663 | lttng_live_msg_iter_create(struct lttng_live_component *lttng_live_comp, | |
1664 | bt_self_message_iterator *self_msg_it) | |
4f74db52 | 1665 | { |
0f5c5d5c SM |
1666 | lttng_live_msg_iter *msg_iter = new lttng_live_msg_iter {lttng_live_comp->logger}; |
1667 | msg_iter->self_comp = lttng_live_comp->self_comp; | |
1668 | msg_iter->lttng_live_comp = lttng_live_comp; | |
1669 | msg_iter->self_msg_iter = self_msg_it; | |
4164020e | 1670 | |
0f5c5d5c SM |
1671 | msg_iter->active_stream_iter = 0; |
1672 | msg_iter->last_msg_ts_ns = INT64_MIN; | |
1673 | msg_iter->was_interrupted = false; | |
4f74db52 | 1674 | |
0f5c5d5c | 1675 | msg_iter->sessions = |
4164020e | 1676 | g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_destroy_session); |
0f5c5d5c | 1677 | BT_ASSERT(msg_iter->sessions); |
4164020e | 1678 | |
0f5c5d5c | 1679 | return msg_iter; |
4f74db52 FD |
1680 | } |
1681 | ||
4164020e SM |
1682 | bt_message_iterator_class_initialize_method_status |
1683 | lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it, | |
7d91f1ac | 1684 | bt_self_message_iterator_configuration *, bt_self_component_port_output *) |
7cdc2bab | 1685 | { |
1e690349 SM |
1686 | try { |
1687 | bt_message_iterator_class_initialize_method_status status; | |
1688 | struct lttng_live_component *lttng_live; | |
1689 | struct lttng_live_msg_iter *lttng_live_msg_iter; | |
1690 | enum lttng_live_viewer_status viewer_status; | |
1691 | bt_self_component *self_comp = bt_self_message_iterator_borrow_component(self_msg_it); | |
1692 | ||
1693 | lttng_live = (lttng_live_component *) bt_self_component_get_data(self_comp); | |
1694 | ||
1695 | /* There can be only one downstream iterator at the same time. */ | |
1696 | BT_ASSERT(!lttng_live->has_msg_iter); | |
1697 | lttng_live->has_msg_iter = true; | |
1698 | ||
1699 | lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, self_msg_it); | |
1700 | if (!lttng_live_msg_iter) { | |
1701 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live->logger, | |
1702 | "Failed to create lttng_live_msg_iter"); | |
1703 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
1704 | goto error; | |
1705 | } | |
4164020e | 1706 | |
1e690349 | 1707 | viewer_status = live_viewer_connection_create( |
2780ec75 | 1708 | lttng_live->params.url.c_str(), false, lttng_live_msg_iter, lttng_live_msg_iter->logger, |
1e690349 SM |
1709 | <tng_live_msg_iter->viewer_connection); |
1710 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1711 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1712 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1713 | "Failed to create viewer connection"); | |
1714 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1715 | /* | |
f8a11f24 SM |
1716 | * Interruption in the _iter_init() method is not |
1717 | * supported. Return an error. | |
1718 | */ | |
1e690349 SM |
1719 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1720 | "Interrupted while creating viewer connection"); | |
1721 | } | |
10265ebe | 1722 | |
1e690349 SM |
1723 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
1724 | goto error; | |
1725 | } | |
4164020e | 1726 | |
1e690349 SM |
1727 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); |
1728 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1729 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1730 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, | |
1731 | "Failed to create viewer session"); | |
1732 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1733 | /* | |
f8a11f24 SM |
1734 | * Interruption in the _iter_init() method is not |
1735 | * supported. Return an error. | |
1736 | */ | |
1e690349 SM |
1737 | BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter->logger, |
1738 | "Interrupted when creating viewer session"); | |
1739 | } | |
4164020e | 1740 | |
10265ebe | 1741 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
4164020e | 1742 | goto error; |
4164020e | 1743 | } |
4164020e | 1744 | |
1e690349 SM |
1745 | if (lttng_live_msg_iter->sessions->len == 0) { |
1746 | switch (lttng_live->params.sess_not_found_act) { | |
1747 | case SESSION_NOT_FOUND_ACTION_CONTINUE: | |
1748 | BT_CPPLOGI_SPEC( | |
1749 | lttng_live_msg_iter->logger, | |
1750 | "Unable to connect to the requested live viewer session. " | |
1751 | "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"", | |
1752 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR, | |
2780ec75 | 1753 | lttng_live->params.url); |
1e690349 SM |
1754 | break; |
1755 | case SESSION_NOT_FOUND_ACTION_FAIL: | |
1756 | BT_CPPLOGE_APPEND_CAUSE_SPEC( | |
1757 | lttng_live_msg_iter->logger, | |
1758 | "Unable to connect to the requested live viewer session. " | |
1759 | "Fail the message iterator initialization because of {}=\"{}\" " | |
1760 | "component parameter: url =\"{}\"", | |
1761 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_FAIL_STR, | |
2780ec75 | 1762 | lttng_live->params.url); |
1e690349 SM |
1763 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
1764 | goto error; | |
1765 | case SESSION_NOT_FOUND_ACTION_END: | |
1766 | BT_CPPLOGI_SPEC(lttng_live_msg_iter->logger, | |
1767 | "Unable to connect to the requested live viewer session. " | |
1768 | "End gracefully at the first _next() call because of {}=\"{}\"" | |
1769 | " component parameter: url=\"{}\"", | |
1770 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_END_STR, | |
2780ec75 | 1771 | lttng_live->params.url); |
1e690349 SM |
1772 | break; |
1773 | default: | |
1774 | bt_common_abort(); | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter); | |
1779 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
1780 | goto end; | |
f79c2d7a | 1781 | |
14f28187 | 1782 | error: |
1e690349 | 1783 | lttng_live_msg_iter_destroy(lttng_live_msg_iter); |
7cdc2bab | 1784 | end: |
1e690349 SM |
1785 | return status; |
1786 | } catch (const std::bad_alloc&) { | |
1787 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
1788 | } catch (const bt2::Error&) { | |
1789 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
1790 | } | |
7cdc2bab MD |
1791 | } |
1792 | ||
80aff5ef | 1793 | static struct bt_param_validation_map_value_entry_descr list_sessions_params[] = { |
88730e42 SM |
1794 | {URL_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, |
1795 | bt_param_validation_value_descr::makeString()}, | |
4164020e SM |
1796 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; |
1797 | ||
1798 | static bt_component_class_query_method_status | |
1799 | lttng_live_query_list_sessions(const bt_value *params, const bt_value **result, | |
0f5c5d5c | 1800 | const bt2c::Logger& logger) |
7cdc2bab | 1801 | { |
4164020e SM |
1802 | bt_component_class_query_method_status status; |
1803 | const bt_value *url_value = NULL; | |
1804 | const char *url; | |
1805 | struct live_viewer_connection *viewer_connection = NULL; | |
1806 | enum lttng_live_viewer_status viewer_status; | |
1807 | enum bt_param_validation_status validation_status; | |
1808 | gchar *validate_error = NULL; | |
1809 | ||
1810 | validation_status = bt_param_validation_validate(params, list_sessions_params, &validate_error); | |
1811 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
1812 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1813 | goto error; | |
1814 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
1815 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
0f5c5d5c | 1816 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "{}", validate_error); |
4164020e SM |
1817 | goto error; |
1818 | } | |
1819 | ||
1820 | url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM); | |
1821 | url = bt_value_string_get(url_value); | |
1822 | ||
0f5c5d5c | 1823 | viewer_status = live_viewer_connection_create(url, true, NULL, logger, &viewer_connection); |
4164020e SM |
1824 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
1825 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
0f5c5d5c | 1826 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create viewer connection"); |
4164020e SM |
1827 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
1828 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1829 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; | |
1830 | } else { | |
1831 | bt_common_abort(); | |
1832 | } | |
1833 | goto error; | |
1834 | } | |
1835 | ||
1836 | status = live_viewer_connection_list_sessions(viewer_connection, result); | |
1837 | if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) { | |
0f5c5d5c | 1838 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to list viewer sessions"); |
4164020e SM |
1839 | goto error; |
1840 | } | |
1841 | ||
1842 | goto end; | |
c7eee084 | 1843 | |
7cdc2bab | 1844 | error: |
4164020e | 1845 | BT_VALUE_PUT_REF_AND_RESET(*result); |
c7eee084 | 1846 | |
4164020e SM |
1847 | if (status >= 0) { |
1848 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1849 | } | |
c7eee084 | 1850 | |
7cdc2bab | 1851 | end: |
4164020e SM |
1852 | if (viewer_connection) { |
1853 | live_viewer_connection_destroy(viewer_connection); | |
1854 | } | |
80aff5ef | 1855 | |
4164020e | 1856 | g_free(validate_error); |
80aff5ef | 1857 | |
4164020e | 1858 | return status; |
7cdc2bab MD |
1859 | } |
1860 | ||
4164020e SM |
1861 | static bt_component_class_query_method_status |
1862 | lttng_live_query_support_info(const bt_value *params, const bt_value **result, | |
0f5c5d5c | 1863 | const bt2c::Logger& logger) |
312df793 | 1864 | { |
4164020e SM |
1865 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
1866 | const bt_value *input_type_value; | |
1867 | const bt_value *input_value; | |
1868 | double weight = 0; | |
dd420a9b | 1869 | struct bt_common_lttng_live_url_parts parts = {}; |
4164020e SM |
1870 | |
1871 | /* Used by the logging macros */ | |
1872 | __attribute__((unused)) bt_self_component *self_comp = NULL; | |
1873 | ||
1874 | *result = NULL; | |
1875 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); | |
1876 | if (!input_type_value) { | |
0f5c5d5c | 1877 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Missing expected `type` parameter."); |
4164020e SM |
1878 | goto error; |
1879 | } | |
1880 | ||
1881 | if (!bt_value_is_string(input_type_value)) { | |
0f5c5d5c | 1882 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`type` parameter is not a string value."); |
4164020e SM |
1883 | goto error; |
1884 | } | |
1885 | ||
1886 | if (strcmp(bt_value_string_get(input_type_value), "string") != 0) { | |
1887 | /* We don't handle file system paths */ | |
1888 | goto create_result; | |
1889 | } | |
1890 | ||
1891 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
1892 | if (!input_value) { | |
0f5c5d5c | 1893 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Missing expected `input` parameter."); |
4164020e SM |
1894 | goto error; |
1895 | } | |
1896 | ||
1897 | if (!bt_value_is_string(input_value)) { | |
0f5c5d5c | 1898 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`input` parameter is not a string value."); |
4164020e SM |
1899 | goto error; |
1900 | } | |
1901 | ||
1902 | parts = bt_common_parse_lttng_live_url(bt_value_string_get(input_value), NULL, 0); | |
1903 | if (parts.session_name) { | |
1904 | /* | |
1905 | * Looks pretty much like an LTTng live URL: we got the | |
1906 | * session name part, which forms a complete URL. | |
1907 | */ | |
1908 | weight = .75; | |
1909 | } | |
312df793 PP |
1910 | |
1911 | create_result: | |
4164020e SM |
1912 | *result = bt_value_real_create_init(weight); |
1913 | if (!*result) { | |
1914 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1915 | goto error; | |
1916 | } | |
312df793 | 1917 | |
4164020e | 1918 | goto end; |
312df793 PP |
1919 | |
1920 | error: | |
4164020e SM |
1921 | if (status >= 0) { |
1922 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1923 | } | |
312df793 | 1924 | |
4164020e | 1925 | BT_ASSERT(!*result); |
312df793 PP |
1926 | |
1927 | end: | |
4164020e SM |
1928 | bt_common_destroy_lttng_live_url_parts(&parts); |
1929 | return status; | |
312df793 PP |
1930 | } |
1931 | ||
4164020e SM |
1932 | bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class, |
1933 | bt_private_query_executor *priv_query_exec, | |
1934 | const char *object, const bt_value *params, | |
1935 | __attribute__((unused)) void *method_data, | |
1936 | const bt_value **result) | |
7cdc2bab | 1937 | { |
1e690349 SM |
1938 | try { |
1939 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
1940 | bt2c::Logger logger {bt2::SelfComponentClass {comp_class}, | |
1941 | bt2::PrivateQueryExecutor {priv_query_exec}, | |
1942 | "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"}; | |
1943 | ||
1944 | if (strcmp(object, "sessions") == 0) { | |
1945 | status = lttng_live_query_list_sessions(params, result, logger); | |
1946 | } else if (strcmp(object, "babeltrace.support-info") == 0) { | |
1947 | status = lttng_live_query_support_info(params, result, logger); | |
1948 | } else { | |
1949 | BT_CPPLOGI_SPEC(logger, "Unknown query object `{}`", object); | |
1950 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; | |
1951 | goto end; | |
1952 | } | |
14f28187 FD |
1953 | |
1954 | end: | |
1e690349 SM |
1955 | return status; |
1956 | } catch (const std::bad_alloc&) { | |
1957 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1958 | } catch (const bt2::Error&) { | |
1959 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1960 | } | |
7cdc2bab MD |
1961 | } |
1962 | ||
14f28187 | 1963 | void lttng_live_component_finalize(bt_self_component_source *component) |
7cdc2bab | 1964 | { |
ef9e5f5d SM |
1965 | lttng_live_component::UP {static_cast<lttng_live_component *>( |
1966 | bt_self_component_get_data(bt_self_component_source_as_self_component(component)))}; | |
7cdc2bab MD |
1967 | } |
1968 | ||
4164020e SM |
1969 | static enum session_not_found_action |
1970 | parse_session_not_found_action_param(const bt_value *no_session_param) | |
14f28187 | 1971 | { |
4164020e SM |
1972 | enum session_not_found_action action; |
1973 | const char *no_session_act_str = bt_value_string_get(no_session_param); | |
1974 | ||
1975 | if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_CONTINUE_STR) == 0) { | |
1976 | action = SESSION_NOT_FOUND_ACTION_CONTINUE; | |
1977 | } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_FAIL_STR) == 0) { | |
1978 | action = SESSION_NOT_FOUND_ACTION_FAIL; | |
1979 | } else { | |
1980 | BT_ASSERT(strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0); | |
1981 | action = SESSION_NOT_FOUND_ACTION_END; | |
1982 | } | |
1983 | ||
1984 | return action; | |
14f28187 FD |
1985 | } |
1986 | ||
88730e42 SM |
1987 | static bt_param_validation_value_descr inputs_elem_descr = |
1988 | bt_param_validation_value_descr::makeString(); | |
80aff5ef SM |
1989 | |
1990 | static const char *sess_not_found_action_choices[] = { | |
4164020e SM |
1991 | SESS_NOT_FOUND_ACTION_CONTINUE_STR, |
1992 | SESS_NOT_FOUND_ACTION_FAIL_STR, | |
1993 | SESS_NOT_FOUND_ACTION_END_STR, | |
80aff5ef SM |
1994 | }; |
1995 | ||
1996 | static struct bt_param_validation_map_value_entry_descr params_descr[] = { | |
88730e42 SM |
1997 | {INPUTS_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, |
1998 | bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr)}, | |
1999 | {SESS_NOT_FOUND_ACTION_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, | |
2000 | bt_param_validation_value_descr::makeString(sess_not_found_action_choices)}, | |
4164020e SM |
2001 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; |
2002 | ||
2003 | static bt_component_class_initialize_method_status | |
0f5c5d5c | 2004 | lttng_live_component_create(const bt_value *params, bt_self_component_source *self_comp, |
ef9e5f5d | 2005 | lttng_live_component::UP& component) |
7cdc2bab | 2006 | { |
4164020e SM |
2007 | const bt_value *inputs_value; |
2008 | const bt_value *url_value; | |
2009 | const bt_value *value; | |
4164020e SM |
2010 | enum bt_param_validation_status validation_status; |
2011 | gchar *validation_error = NULL; | |
0f5c5d5c | 2012 | bt2c::Logger logger {bt2::SelfSourceComponent {self_comp}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"}; |
4164020e SM |
2013 | |
2014 | validation_status = bt_param_validation_validate(params, params_descr, &validation_error); | |
2015 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
ef9e5f5d | 2016 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
4164020e | 2017 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
6838d8aa | 2018 | bt2c::GCharUP errorFreer {validation_error}; |
0f5c5d5c | 2019 | BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "{}", validation_error); |
ef9e5f5d | 2020 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
4164020e SM |
2021 | } |
2022 | ||
ef9e5f5d | 2023 | auto lttng_live = bt2s::make_unique<lttng_live_component>(std::move(logger)); |
0f5c5d5c | 2024 | lttng_live->self_comp = bt_self_component_source_as_self_component(self_comp); |
4164020e SM |
2025 | lttng_live->max_query_size = MAX_QUERY_SIZE; |
2026 | lttng_live->has_msg_iter = false; | |
2027 | ||
2028 | inputs_value = bt_value_map_borrow_entry_value_const(params, INPUTS_PARAM); | |
2029 | url_value = bt_value_array_borrow_element_by_index_const(inputs_value, 0); | |
2780ec75 | 2030 | lttng_live->params.url = bt_value_string_get(url_value); |
4164020e SM |
2031 | |
2032 | value = bt_value_map_borrow_entry_value_const(params, SESS_NOT_FOUND_ACTION_PARAM); | |
2033 | if (value) { | |
2034 | lttng_live->params.sess_not_found_act = parse_session_not_found_action_param(value); | |
2035 | } else { | |
0f5c5d5c SM |
2036 | BT_CPPLOGI_SPEC(lttng_live->logger, |
2037 | "Optional `{}` parameter is missing: defaulting to `{}`.", | |
2038 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR); | |
4164020e SM |
2039 | lttng_live->params.sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE; |
2040 | } | |
2041 | ||
ef9e5f5d SM |
2042 | component = std::move(lttng_live); |
2043 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
d3e4dcd8 PP |
2044 | } |
2045 | ||
4164020e SM |
2046 | bt_component_class_initialize_method_status |
2047 | lttng_live_component_init(bt_self_component_source *self_comp_src, | |
7d91f1ac | 2048 | bt_self_component_source_configuration *, const bt_value *params, void *) |
f3bc2010 | 2049 | { |
1e690349 | 2050 | try { |
ef9e5f5d | 2051 | lttng_live_component::UP lttng_live; |
1e690349 SM |
2052 | bt_component_class_initialize_method_status ret; |
2053 | bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src); | |
2054 | bt_self_component_add_port_status add_port_status; | |
2055 | ||
ef9e5f5d | 2056 | ret = lttng_live_component_create(params, self_comp_src, lttng_live); |
1e690349 | 2057 | if (ret != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { |
ef9e5f5d | 2058 | return ret; |
1e690349 | 2059 | } |
4164020e | 2060 | |
1e690349 SM |
2061 | add_port_status = |
2062 | bt_self_component_source_add_output_port(self_comp_src, "out", NULL, NULL); | |
2063 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { | |
2064 | ret = (bt_component_class_initialize_method_status) add_port_status; | |
ef9e5f5d | 2065 | return ret; |
1e690349 | 2066 | } |
4164020e | 2067 | |
ef9e5f5d | 2068 | bt_self_component_set_data(self_comp, lttng_live.release()); |
7cdc2bab | 2069 | |
ef9e5f5d | 2070 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
1e690349 SM |
2071 | } catch (const std::bad_alloc&) { |
2072 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
2073 | } catch (const bt2::Error&) { | |
2074 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
2075 | } | |
d85ef162 | 2076 | } |