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