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