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