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