2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Babeltrace CTF LTTng-live Client Component
14 #include "common/assert.h"
15 #include "cpp-common/bt2c/fmt.hpp"
16 #include "cpp-common/bt2c/glib-up.hpp"
17 #include "cpp-common/bt2c/vector.hpp"
18 #include "cpp-common/bt2s/make-unique.hpp"
19 #include "cpp-common/vendor/fmt/format.h"
21 #include "plugins/common/muxing/muxing.h"
22 #include "plugins/common/param-validation/param-validation.h"
24 #include "data-stream.hpp"
25 #include "lttng-live.hpp"
26 #include "metadata.hpp"
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"
36 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
37 enum lttng_live_stream_state new_state
)
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
);
44 stream_iter
->state
= new_state
;
47 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
49 BT_CPPLOGD_SPEC((live_stream_iter)->logger, \
50 "Live stream iterator state={}, " \
51 "last-inact-ts-is-set={}, last-inact-ts-value={}, " \
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); \
58 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
64 return bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
67 static struct lttng_live_trace
*
68 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
70 for (lttng_live_trace::UP
& trace
: session
->traces
) {
71 if (trace
->id
== trace_id
) {
79 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
82 BT_CPPLOGD_SPEC(session
->logger
, "Creating live trace: session-id={}, trace-id={}", session
->id
,
85 auto trace
= bt2s::make_unique
<lttng_live_trace
>(session
->logger
);
87 trace
->session
= session
;
89 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
91 const auto ret
= trace
.get();
92 session
->traces
.emplace_back(std::move(trace
));
96 struct lttng_live_trace
*
97 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
100 if (lttng_live_trace
*trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
)) {
104 /* The session is the owner of the newly created trace. */
105 return lttng_live_create_trace(session
, trace_id
);
108 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
109 const char *hostname
, const char *session_name
)
111 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
112 "Adding live session: "
113 "session-id={}, hostname=\"{}\", session-name=\"{}\"",
114 session_id
, hostname
, session_name
);
116 auto session
= bt2s::make_unique
<lttng_live_session
>(lttng_live_msg_iter
->logger
);
118 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
119 session
->id
= session_id
;
120 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
121 session
->new_streams_needed
= true;
122 session
->hostname
= hostname
;
123 session
->session_name
= session_name
;
125 lttng_live_msg_iter
->sessions
.emplace_back(std::move(session
));
130 lttng_live_session::~lttng_live_session()
132 BT_CPPLOGD_SPEC(this->logger
, "Destroying live session: session-id={}, session-name=\"{}\"",
133 this->id
, this->session_name
);
135 if (this->id
!= -1ULL) {
136 if (lttng_live_session_detach(this)) {
137 if (!lttng_live_graph_is_canceled(this->lttng_live_msg_iter
)) {
138 /* Old relayd cannot detach sessions. */
139 BT_CPPLOGD_SPEC(this->logger
, "Unable to detach lttng live session {}", this->id
);
147 lttng_live_msg_iter::~lttng_live_msg_iter()
149 BT_ASSERT(this->lttng_live_comp
);
150 BT_ASSERT(this->lttng_live_comp
->has_msg_iter
);
152 /* All stream iterators must be destroyed at this point. */
153 BT_ASSERT(this->active_stream_iter
== 0);
154 this->lttng_live_comp
->has_msg_iter
= false;
157 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
159 lttng_live_msg_iter::UP
{
160 static_cast<lttng_live_msg_iter
*>(bt_self_message_iterator_get_data(self_msg_iter
))};
163 static enum lttng_live_iterator_status
164 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
166 switch (lttng_live_stream
->state
) {
167 case LTTNG_LIVE_STREAM_QUIESCENT
:
168 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
170 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
172 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"ACTIVE_NO_DATA\"");
174 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
176 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"QUIESCENT_NO_DATA\"");
178 case LTTNG_LIVE_STREAM_EOF
:
181 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
185 * For active no data stream, fetch next index. As a result of that it can
187 * - quiescent: won't have events for a bit,
188 * - have data: need to get that data and produce the event,
189 * - have no data on this stream at this point: need to retry (AGAIN) or return
192 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
193 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
194 struct lttng_live_stream_iterator
*lttng_live_stream
)
196 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
197 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
198 struct packet_index index
;
200 if (lttng_live_stream
->trace
->metadata_stream_state
==
201 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
202 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
203 "Need to get an update for the metadata stream before proceeding "
204 "further with this stream: stream-name=\"{}\"",
205 lttng_live_stream
->name
);
206 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
210 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
211 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
212 "Need to get an update of all streams before proceeding further "
213 "with this stream: stream-name=\"{}\"",
214 lttng_live_stream
->name
);
215 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
219 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
220 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
223 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
224 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
228 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
230 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
231 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
232 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
234 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
236 * Because the stream is in the QUIESCENT_NO_DATA
237 * state, we can assert that the last_inactivity_ts was
238 * set and can be safely used in the `if` above.
240 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
242 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
243 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
245 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
250 lttng_live_stream
->base_offset
= index
.offset
;
251 lttng_live_stream
->offset
= index
.offset
;
252 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
254 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
255 "Setting live stream reading info: stream-name=\"{}\", "
256 "viewer-stream-id={}, stream-base-offset={}, stream-offset={}, stream-len={}",
257 lttng_live_stream
->name
, lttng_live_stream
->viewer_stream_id
,
258 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
,
259 lttng_live_stream
->len
);
262 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
263 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
269 * Creation of the message requires the ctf trace class to be created
270 * beforehand, but the live protocol gives us all streams (including metadata)
271 * at once. So we split it in three steps: getting streams, getting metadata
272 * (which creates the ctf trace class), and then creating the per-stream
275 static enum lttng_live_iterator_status
276 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
277 struct lttng_live_session
*session
)
279 enum lttng_live_iterator_status status
;
281 if (!session
->attached
) {
282 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
, "Attach to session: session-id={}",
284 enum lttng_live_viewer_status attach_status
=
285 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
286 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
287 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
289 * Clear any causes appended in
290 * `lttng_live_attach_session()` as we want to
291 * return gracefully since the graph was
294 bt_current_thread_clear_error();
295 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
297 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
298 "Error attaching to LTTng live session");
299 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
304 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
305 "Updating all data streams: session-id={}, session-name=\"{}\"", session
->id
,
306 session
->session_name
);
308 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
310 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
312 case LTTNG_LIVE_ITERATOR_STATUS_END
:
314 * We received a `_END` from the `_get_new_streams()` function,
315 * which means no more data will ever be received from the data
316 * streams of this session. But it's possible that the metadata
318 * The live protocol guarantees that we receive all the
319 * metadata needed before we receive data streams needing it.
320 * But it's possible to receive metadata NOT needed by
321 * data streams after the session was closed. For example, this
322 * could happen if a new event is registered and the session is
323 * stopped before any tracepoint for that event is actually
327 lttng_live_msg_iter
->logger
,
328 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
329 "session-id={}, session-name=\"{}\"",
330 session
->id
, session
->session_name
);
331 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
336 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
337 "Updating metadata stream for session: session-id={}, session-name=\"{}\"",
338 session
->id
, session
->session_name
);
340 for (lttng_live_trace::UP
& trace
: session
->traces
) {
341 status
= lttng_live_metadata_update(trace
.get());
343 case LTTNG_LIVE_ITERATOR_STATUS_END
:
344 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
346 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
347 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
350 BT_CPPLOGE_APPEND_CAUSE_SPEC(
351 lttng_live_msg_iter
->logger
,
352 "Error updating trace metadata: stream-iter-status={}, trace-id={}", status
,
359 * Now that we have the metadata we can initialize the downstream
362 return lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
366 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
368 for (const auto& session
: lttng_live_msg_iter
->sessions
) {
369 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
370 "Force marking session as needing new streams: "
373 session
->new_streams_needed
= true;
374 for (lttng_live_trace::UP
& trace
: session
->traces
) {
375 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
376 "Force marking trace metadata state as needing an update: "
377 "session-id={}, trace-id={}",
378 session
->id
, trace
->id
);
380 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
382 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
387 static enum lttng_live_iterator_status
388 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
390 enum lttng_live_viewer_status viewer_status
;
391 uint64_t nr_sessions_opened
= 0;
392 enum session_not_found_action sess_not_found_act
=
393 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
395 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
396 "Update data and metadata of all sessions: "
397 "live-msg-iter-addr={}",
398 fmt::ptr(lttng_live_msg_iter
));
400 * In a remotely distant future, we could add a "new
401 * session" flag to the protocol, which would tell us that we
402 * need to query for new sessions even though we have sessions
405 if (lttng_live_msg_iter
->sessions
.empty()) {
406 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
408 lttng_live_msg_iter
->logger
,
409 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
410 return LTTNG_LIVE_ITERATOR_STATUS_END
;
413 lttng_live_msg_iter
->logger
,
414 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
416 * Retry to create a viewer session for the requested
419 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
420 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
421 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
422 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
423 "Error creating LTTng live viewer session");
424 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
425 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
426 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
434 for (const auto& session
: lttng_live_msg_iter
->sessions
) {
435 const auto status
= lttng_live_get_session(lttng_live_msg_iter
, session
.get());
437 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
438 case LTTNG_LIVE_ITERATOR_STATUS_END
:
440 * A session returned `_END`. Other sessions may still
441 * be active so we override the status and continue
448 if (!session
->closed
) {
449 nr_sessions_opened
++;
453 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
454 return LTTNG_LIVE_ITERATOR_STATUS_END
;
456 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
460 static enum lttng_live_iterator_status
461 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
462 struct lttng_live_stream_iterator
*stream_iter
,
463 bt2::ConstMessage::Shared
& message
, uint64_t timestamp
)
465 BT_ASSERT(stream_iter
->trace
->clock_class
);
466 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
467 "Emitting inactivity message for stream: ctf-stream-id={}, "
468 "viewer-stream-id={}, timestamp={}",
469 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
,
472 const auto msg
= bt_message_message_iterator_inactivity_create(
473 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->trace
->clock_class
, timestamp
);
476 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
477 "Error emitting message iterator inactivity message");
478 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
481 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
482 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
485 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
486 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
487 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
489 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
490 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
494 * Check if we already sent an inactivty message downstream for this
495 * `current_inactivity_ts` value.
497 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
498 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
499 lttng_live_stream_iterator_set_state(lttng_live_stream
,
500 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
502 return LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
505 const auto status
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
506 lttng_live_stream
->current_inactivity_ts
);
508 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
512 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
513 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
515 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
518 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
519 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
521 const bt_clock_snapshot
*clock_snapshot
= NULL
;
525 BT_ASSERT_DBG(ts_ns
);
527 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
528 "Getting message's timestamp: iter-data-addr={}, msg-addr={}, "
530 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
);
532 switch (bt_message_get_type(msg
)) {
533 case BT_MESSAGE_TYPE_EVENT
:
534 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
536 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
537 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
539 case BT_MESSAGE_TYPE_PACKET_END
:
540 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
542 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
544 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
546 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
548 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
550 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
551 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
554 /* All the other messages have a higher priority */
555 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
556 "Message has no timestamp, using the last message timestamp: "
557 "iter-data-addr={}, msg-addr={}, last-msg-ts={}, ts={}",
558 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
, *ts_ns
);
559 *ts_ns
= last_msg_ts_ns
;
563 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
565 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
566 "Cannot get nanoseconds from Epoch of clock snapshot: "
567 "clock-snapshot-addr={}",
568 fmt::ptr(clock_snapshot
));
573 lttng_live_msg_iter
->logger
,
574 "Found message's timestamp: iter-data-addr={}, msg-addr={}, last-msg-ts={}, ts={}",
575 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
, *ts_ns
);
580 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
581 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
582 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
584 enum ctf_msg_iter_status status
;
586 for (const auto& session
: lttng_live_msg_iter
->sessions
) {
587 if (session
->new_streams_needed
) {
588 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
589 "Need an update for streams: session-id={}", session
->id
);
590 return LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
592 for (lttng_live_trace::UP
& trace
: session
->traces
) {
593 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
594 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
595 "Need an update for metadata stream: session-id={}, trace-id={}",
596 session
->id
, trace
->id
);
597 return LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
602 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
603 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
604 "Invalid state of live stream iterator: stream-iter-status={}",
605 lttng_live_stream
->state
);
606 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
609 const bt_message
*msg
;
610 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
.get(), &msg
);
612 case CTF_MSG_ITER_STATUS_EOF
:
613 return LTTNG_LIVE_ITERATOR_STATUS_END
;
614 case CTF_MSG_ITER_STATUS_OK
:
615 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
616 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
617 case CTF_MSG_ITER_STATUS_AGAIN
:
619 * Continue immediately (end of packet). The next
620 * get_index may return AGAIN to delay the following
623 return LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
624 case CTF_MSG_ITER_STATUS_ERROR
:
626 BT_CPPLOGE_APPEND_CAUSE_SPEC(
627 lttng_live_msg_iter
->logger
,
628 "CTF message iterator failed to get next message: msg-iter={}, msg-iter-status={}",
629 fmt::ptr(lttng_live_stream
->msg_iter
), status
);
630 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
634 static enum lttng_live_iterator_status
635 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
636 struct lttng_live_stream_iterator
*stream_iter
,
637 bt2::ConstMessage::Shared
& curr_msg
)
639 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
640 "Closing live stream iterator: stream-name=\"{}\", "
641 "viewer-stream-id={}",
642 stream_iter
->name
, stream_iter
->viewer_stream_id
);
645 * The viewer has hung up on us so we are closing the stream. The
646 * `ctf_msg_iter` should simply realize that it needs to close the
647 * stream properly by emitting the necessary stream end message.
649 const bt_message
*msg
;
650 enum ctf_msg_iter_status status
=
651 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
.get(), &msg
);
653 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
654 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
655 "Error getting the next message from CTF message iterator");
656 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
657 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
658 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
659 "Reached the end of the live stream iterator.");
660 return LTTNG_LIVE_ITERATOR_STATUS_END
;
663 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
665 curr_msg
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
667 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
672 * handle_no_data_streams()
674 * - for each ACTIVE_NO_DATA stream:
675 * - query relayd for stream data, or quiescence info.
676 * - if need metadata, get metadata, goto retry.
677 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
678 * - if quiescent, move to QUIESCENT streams
679 * - if fetched data, move to ACTIVE_DATA streams
680 * (at this point each stream either has data, or is quiescent)
684 * handle_new_streams_and_metadata()
685 * - query relayd for known streams, add them as ACTIVE_NO_DATA
686 * - query relayd for metadata
688 * call handle_active_no_data_streams()
690 * handle_quiescent_streams()
691 * - if at least one stream is ACTIVE_DATA:
692 * - peek stream event with lowest timestamp -> next_ts
693 * - for each quiescent stream
694 * - if next_ts >= quiescent end
695 * - set state to ACTIVE_NO_DATA
697 * - for each quiescent stream
698 * - set state to ACTIVE_NO_DATA
700 * call handle_active_no_data_streams()
702 * handle_active_data_streams()
703 * - if at least one stream is ACTIVE_DATA:
704 * - get stream event with lowest timestamp from heap
705 * - make that stream event the current message.
706 * - move this stream heap position to its next event
707 * - if we need to fetch data from relayd, move
708 * stream to ACTIVE_NO_DATA.
712 * end criterion: ctrl-c on client. If relayd exits or the session
713 * closes on the relay daemon side, we keep on waiting for streams.
714 * Eventually handle --end timestamp (also an end criterion).
716 * When disconnected from relayd: try to re-connect endlessly.
718 static enum lttng_live_iterator_status
719 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
720 struct lttng_live_stream_iterator
*stream_iter
,
721 bt2::ConstMessage::Shared
& curr_msg
)
723 enum lttng_live_iterator_status live_status
;
725 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
726 "Advancing live stream iterator until next message if possible: "
727 "stream-name=\"{}\", viewer-stream-id={}",
728 stream_iter
->name
, stream_iter
->viewer_stream_id
);
730 if (stream_iter
->has_stream_hung_up
) {
732 * The stream has hung up and the stream was properly closed
733 * during the last call to the current function. Return _END
734 * status now so that this stream iterator is removed for the
735 * stream iterator list.
737 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
742 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
745 * Make sure we have the most recent metadata and possibly some new
748 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
749 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
754 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
755 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
756 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
758 * We overwrite `live_status` since `curr_msg` is
759 * likely set to a valid message in this function.
762 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
767 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
768 stream_iter
, curr_msg
);
769 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
770 BT_ASSERT(!curr_msg
);
776 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
777 stream_iter
, curr_msg
);
778 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
779 BT_ASSERT(!curr_msg
);
783 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
785 lttng_live_msg_iter
->logger
,
786 "Ask the relay daemon for an updated view of the data and metadata streams");
790 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
791 "Returning from advancing live stream iterator: status={}, "
792 "stream-name=\"{}\", viewer-stream-id={}",
793 live_status
, stream_iter
->name
, stream_iter
->viewer_stream_id
);
798 static bool is_discarded_packet_or_event_message(const bt2::ConstMessage msg
)
800 return msg
.type() == bt2::MessageType::DiscardedEvents
||
801 msg
.type() == bt2::MessageType::DiscardedPackets
;
804 static enum lttng_live_iterator_status
805 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
806 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
807 uint64_t new_begin_ts
)
809 enum bt_property_availability availability
;
810 const bt_clock_snapshot
*clock_snapshot
;
814 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
815 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
817 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
818 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
820 const auto msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
821 iter
, stream
, new_begin_ts
, end_ts
);
824 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
827 bt_message_discarded_packets_set_count(msg
, count
);
828 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
829 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
832 static enum lttng_live_iterator_status
833 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
834 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
835 uint64_t new_begin_ts
)
837 enum bt_property_availability availability
;
838 const bt_clock_snapshot
*clock_snapshot
;
842 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
843 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
845 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
846 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
848 const auto msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
849 iter
, stream
, new_begin_ts
, end_ts
);
852 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
855 bt_message_discarded_events_set_count(msg
, count
);
856 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
857 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
860 static enum lttng_live_iterator_status
861 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
862 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
863 const bt2::ConstMessage
& late_msg
)
865 const bt_clock_class
*clock_class
;
866 const bt_stream_class
*stream_class
;
867 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
868 int64_t last_inactivity_ts_ns
;
869 enum lttng_live_iterator_status adjust_status
;
870 bt2::ConstMessage::Shared adjusted_message
;
873 * The timestamp of the current message is before the last message sent
874 * by this component. We CANNOT send it as is.
876 * The only expected scenario in which that could happen is the
877 * following, everything else is a bug in this component, relay daemon,
880 * Expected scenario: The CTF message iterator emitted discarded
881 * packets and discarded events with synthesized beginning and end
882 * timestamps from the bounds of the last known packet and the newly
883 * decoded packet header. The CTF message iterator is not aware of
884 * stream inactivity beacons. Hence, we have to adjust the beginning
885 * timestamp of those types of messages if a stream signalled its
886 * inactivity up until _after_ the last known packet's beginning
889 * Otherwise, the monotonicity guarantee of message timestamps would
892 * In short, the only scenario in which it's okay and fixable to
893 * received a late message is when:
894 * 1. the late message is a discarded packets or discarded events
896 * 2. this stream produced an inactivity message downstream, and
897 * 3. the timestamp of the late message is within the inactivity
898 * timespan we sent downstream through the inactivity message.
901 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
902 "Handling late message on live stream iterator: "
903 "stream-name=\"{}\", viewer-stream-id={}",
904 stream_iter
->name
, stream_iter
->viewer_stream_id
);
906 if (!stream_iter
->last_inactivity_ts
.is_set
) {
907 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
908 "Invalid live stream state: "
909 "have a late message when no inactivity message "
910 "was ever sent for that stream.");
911 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
914 if (!is_discarded_packet_or_event_message(late_msg
)) {
915 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
916 "Invalid live stream state: "
917 "have a late message that is not a packet discarded or "
918 "event discarded message: late-msg-type={}",
920 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
923 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
->libObjPtr());
924 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
926 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
927 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
928 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
929 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
930 "Error converting last "
931 "inactivity message timestamp to nanoseconds");
932 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
935 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
936 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
937 "Invalid live stream state: "
938 "have a late message that is none included in a stream "
939 "inactivity timespan: last-inactivity-ts-ns={}, "
941 last_inactivity_ts_ns
, late_msg_ts_ns
);
942 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
946 * We now know that it's okay for this message to be late, we can now
947 * adjust its timestamp to ensure monotonicity.
949 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
950 "Adjusting the timestamp of late message: late-msg-type={}, "
952 late_msg
.type(), stream_iter
->last_inactivity_ts
.value
);
953 switch (late_msg
.type()) {
954 case bt2::MessageType::DiscardedEvents
:
955 adjust_status
= adjust_discarded_events_message(
956 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
957 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
959 case bt2::MessageType::DiscardedPackets
:
960 adjust_status
= adjust_discarded_packets_message(
961 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
962 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
968 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
969 return adjust_status
;
972 BT_ASSERT_DBG(adjusted_message
);
973 stream_iter
->current_msg
= adjusted_message
;
974 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
976 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
979 static enum lttng_live_iterator_status
980 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
981 struct lttng_live_trace
*live_trace
,
982 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
984 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
985 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
986 uint64_t stream_iter_idx
;
988 BT_ASSERT_DBG(live_trace
);
990 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
991 "Finding the next stream iterator for trace: "
995 * Update the current message of every stream iterators of this trace.
996 * The current msg of every stream must have a timestamp equal or
997 * larger than the last message returned by this iterator. We must
998 * ensure monotonicity.
1000 stream_iter_idx
= 0;
1001 while (stream_iter_idx
< live_trace
->stream_iterators
.size()) {
1002 bool stream_iter_is_ended
= false;
1003 lttng_live_stream_iterator
*stream_iter
=
1004 live_trace
->stream_iterators
[stream_iter_idx
].get();
1007 * If there is no current message for this stream, go fetch
1010 while (!stream_iter
->current_msg
) {
1011 bt2::ConstMessage::Shared msg
;
1012 int64_t curr_msg_ts_ns
= INT64_MAX
;
1014 const auto stream_iter_status
=
1015 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, msg
);
1017 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1018 stream_iter_is_ended
= true;
1022 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1023 return stream_iter_status
;
1028 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1029 "Live stream iterator returned message: msg-type={}, "
1030 "stream-name=\"{}\", viewer-stream-id={}",
1031 msg
->type(), stream_iter
->name
, stream_iter
->viewer_stream_id
);
1034 * Get the timestamp in nanoseconds from origin of this
1037 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
->libObjPtr(),
1038 lttng_live_msg_iter
->last_msg_ts_ns
, &curr_msg_ts_ns
);
1041 * Check if the message of the current live stream
1042 * iterator occurred at the exact same time or after the
1043 * last message returned by this component's message
1044 * iterator. If not, we need to handle it with care.
1046 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1047 stream_iter
->current_msg
= std::move(msg
);
1048 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1051 * We received a message from the past. This
1052 * may be fixable but it can also be an error.
1054 if (handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, *msg
) !=
1055 LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1056 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1057 "Late message could not be handled correctly: "
1058 "lttng-live-msg-iter-addr={}, "
1059 "stream-name=\"{}\", "
1060 "curr-msg-ts={}, last-msg-ts={}",
1061 fmt::ptr(lttng_live_msg_iter
), stream_iter
->name
,
1063 lttng_live_msg_iter
->last_msg_ts_ns
);
1064 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1069 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1071 if (!stream_iter_is_ended
) {
1072 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1073 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1075 * Update the current best candidate message
1076 * for the stream iterator of this live trace
1077 * to be forwarded downstream.
1079 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1080 youngest_candidate_stream_iter
= stream_iter
;
1081 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1083 * Order the messages in an arbitrary but
1084 * deterministic way.
1086 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1087 int ret
= common_muxing_compare_messages(
1088 stream_iter
->current_msg
->libObjPtr(),
1089 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1092 * The `youngest_candidate_stream_iter->current_msg`
1093 * should go first. Update the next
1094 * iterator and the current timestamp.
1096 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1097 youngest_candidate_stream_iter
= stream_iter
;
1098 } else if (ret
== 0) {
1100 * Unable to pick which one should go
1104 lttng_live_msg_iter
->logger
,
1105 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1106 "stream-iter-addr={}"
1107 "stream-iter-addr={}",
1108 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1115 * The live stream iterator has ended. That
1116 * iterator is removed from the array, but
1117 * there is no need to increment
1118 * stream_iter_idx as
1119 * g_ptr_array_remove_index_fast replaces the
1120 * removed element with the array's last
1123 bt2c::vectorFastRemove(live_trace
->stream_iterators
, stream_iter_idx
);
1127 if (youngest_candidate_stream_iter
) {
1128 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1129 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
1132 * The only case where we don't have a candidate for this trace
1133 * is if we reached the end of all the iterators.
1135 BT_ASSERT(live_trace
->stream_iterators
.empty());
1136 return LTTNG_LIVE_ITERATOR_STATUS_END
;
1140 static enum lttng_live_iterator_status
1141 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1142 struct lttng_live_session
*session
,
1143 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1145 enum lttng_live_iterator_status stream_iter_status
;
1146 uint64_t trace_idx
= 0;
1147 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1148 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1150 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1151 "Finding the next stream iterator for session: "
1155 * Make sure we are attached to the session and look for new streams
1158 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1159 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1160 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1161 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1162 return stream_iter_status
;
1165 while (trace_idx
< session
->traces
.size()) {
1166 bool trace_is_ended
= false;
1167 struct lttng_live_stream_iterator
*stream_iter
;
1168 lttng_live_trace
*trace
= session
->traces
[trace_idx
].get();
1170 stream_iter_status
=
1171 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1172 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1174 * All the live stream iterators for this trace are
1175 * ENDed. Remove the trace from this session.
1177 trace_is_ended
= true;
1178 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1179 return stream_iter_status
;
1182 if (!trace_is_ended
) {
1183 BT_ASSERT_DBG(stream_iter
);
1185 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1186 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1187 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1188 youngest_candidate_stream_iter
= stream_iter
;
1189 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1191 * Order the messages in an arbitrary but
1192 * deterministic way.
1194 int ret
= common_muxing_compare_messages(
1195 stream_iter
->current_msg
->libObjPtr(),
1196 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1199 * The `youngest_candidate_stream_iter->current_msg`
1200 * should go first. Update the next iterator
1201 * and the current timestamp.
1203 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1204 youngest_candidate_stream_iter
= stream_iter
;
1205 } else if (ret
== 0) {
1206 /* Unable to pick which one should go first. */
1208 lttng_live_msg_iter
->logger
,
1209 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1210 "stream-iter-addr={}"
1211 "youngest-candidate-stream-iter-addr={}",
1212 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1218 * trace_idx is not incremented since
1219 * vectorFastRemove replaces the
1220 * element at trace_idx with the array's last element.
1222 bt2c::vectorFastRemove(session
->traces
, trace_idx
);
1225 if (youngest_candidate_stream_iter
) {
1226 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1227 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
1230 * The only cases where we don't have a candidate for this
1232 * 1. if we reached the end of all the iterators of all the
1233 * traces of this session,
1234 * 2. if we never had live stream iterator in the first place.
1236 * In either cases, we return END.
1238 BT_ASSERT(session
->traces
.empty());
1239 return LTTNG_LIVE_ITERATOR_STATUS_END
;
1243 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1247 for (i
= 0; i
< count
; i
++) {
1248 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1252 bt_message_iterator_class_next_method_status
1253 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1254 uint64_t capacity
, uint64_t *count
)
1257 bt_message_iterator_class_next_method_status status
;
1258 enum lttng_live_viewer_status viewer_status
;
1259 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1260 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1261 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1262 enum lttng_live_iterator_status stream_iter_status
;
1266 BT_ASSERT_DBG(lttng_live_msg_iter
);
1268 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1270 * The iterator was interrupted in a previous call to the
1271 * `_next()` method. We currently do not support generating
1272 * messages after such event. The babeltrace2 CLI should never
1273 * be running the graph after being interrupted. So this check
1274 * is to prevent other graph users from using this live
1275 * iterator in an messed up internal state.
1277 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1278 lttng_live_msg_iter
->logger
,
1279 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1280 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1284 * Clear all the invalid message reference that might be left over in
1287 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1290 * If no session are exposed on the relay found at the url provided by
1291 * the user, session count will be 0. In this case, we return status
1292 * end to return gracefully.
1294 if (lttng_live_msg_iter
->sessions
.empty()) {
1295 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1296 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1299 * The are no more active session for this session
1300 * name. Retry to create a viewer session for the
1301 * requested session name.
1303 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1304 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1305 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1306 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1307 "Error creating LTTng live viewer session");
1308 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1309 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1310 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1318 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1319 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1323 * Here the muxing of message is done.
1325 * We need to iterate over all the streams of all the traces of all the
1326 * viewer sessions in order to get the message with the smallest
1327 * timestamp. In this case, a session is a viewer session and there is
1328 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1329 * kernel). Each viewer session can have multiple traces, for example,
1330 * 64bit UST viewer sessions could have multiple per-pid traces.
1332 * We iterate over the streams of each traces to update and see what is
1333 * their next message's timestamp. From those timestamps, we select the
1334 * message with the smallest timestamp as the best candidate message
1335 * for that trace and do the same thing across all the sessions.
1337 * We then compare the timestamp of best candidate message of all the
1338 * sessions to pick the message with the smallest timestamp and we
1341 while (*count
< capacity
) {
1342 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1343 *candidate_stream_iter
= NULL
;
1344 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1346 uint64_t session_idx
= 0;
1347 while (session_idx
< lttng_live_msg_iter
->sessions
.size()) {
1348 lttng_live_session
*session
= lttng_live_msg_iter
->sessions
[session_idx
].get();
1350 /* Find the best candidate message to send downstream. */
1351 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1352 &candidate_stream_iter
);
1354 /* If we receive an END status, it means that either:
1355 * - Those traces never had active streams (UST with no
1356 * data produced yet),
1357 * - All live stream iterators have ENDed.
1359 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1360 if (session
->closed
&& session
->traces
.empty()) {
1362 * Remove the session from the list.
1363 * session_idx is not modified since
1364 * g_ptr_array_remove_index_fast
1365 * replaces the the removed element with
1366 * the array's last element.
1368 bt2c::vectorFastRemove(lttng_live_msg_iter
->sessions
, session_idx
);
1375 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1379 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1380 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1381 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1382 youngest_stream_iter
= candidate_stream_iter
;
1383 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1385 * The currently selected message to be sent
1386 * downstream next has the exact same timestamp
1387 * that of the current candidate message. We
1388 * must break the tie in a predictable manner.
1390 BT_CPPLOGD_STR_SPEC(
1391 lttng_live_msg_iter
->logger
,
1392 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1394 * Order the messages in an arbitrary but
1395 * deterministic way.
1397 int ret
= common_muxing_compare_messages(
1398 candidate_stream_iter
->current_msg
->libObjPtr(),
1399 youngest_stream_iter
->current_msg
->libObjPtr());
1402 * The `candidate_stream_iter->current_msg`
1403 * should go first. Update the next
1404 * iterator and the current timestamp.
1406 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1407 youngest_stream_iter
= candidate_stream_iter
;
1408 } else if (ret
== 0) {
1409 /* Unable to pick which one should go first. */
1411 lttng_live_msg_iter
->logger
,
1412 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1413 "next-stream-iter-addr={}"
1414 "candidate-stream-iter-addr={}",
1415 fmt::ptr(youngest_stream_iter
), fmt::ptr(candidate_stream_iter
));
1422 if (!youngest_stream_iter
) {
1423 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1427 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1428 /* Ensure monotonicity. */
1429 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1430 youngest_stream_iter
->current_msg_ts_ns
);
1433 * Insert the next message to the message batch. This will set
1434 * stream iterator current message to NULL so that next time
1435 * we fetch the next message of that stream iterator
1437 msgs
[*count
] = youngest_stream_iter
->current_msg
.release().libObjPtr();
1440 /* Update the last timestamp in nanoseconds sent downstream. */
1441 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1442 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1444 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1448 switch (stream_iter_status
) {
1449 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1450 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1452 * If we gathered messages, return _OK even if the graph was
1453 * interrupted. This allows for the components downstream to at
1454 * least get the those messages. If the graph was indeed
1455 * interrupted there should not be another _next() call as the
1456 * application will tear down the graph. This component class
1457 * doesn't support restarting after an interruption.
1460 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1462 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1465 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1466 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1468 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1469 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1470 "Memory error preparing the next batch of messages: "
1471 "live-iter-status={}",
1472 stream_iter_status
);
1473 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1475 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1476 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1477 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1478 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1479 "Error preparing the next batch of messages: "
1480 "live-iter-status={}",
1481 stream_iter_status
);
1483 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1484 /* Put all existing messages on error. */
1485 put_messages(msgs
, *count
);
1492 } catch (const std::bad_alloc
&) {
1493 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1494 } catch (const bt2::Error
&) {
1495 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1499 static lttng_live_msg_iter::UP
1500 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1501 bt_self_message_iterator
*self_msg_it
)
1503 auto msg_iter
= bt2s::make_unique
<struct lttng_live_msg_iter
>(lttng_live_comp
->logger
);
1505 msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1506 msg_iter
->lttng_live_comp
= lttng_live_comp
;
1507 msg_iter
->self_msg_iter
= self_msg_it
;
1508 msg_iter
->active_stream_iter
= 0;
1509 msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1510 msg_iter
->was_interrupted
= false;
1515 bt_message_iterator_class_initialize_method_status
1516 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1517 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1520 struct lttng_live_component
*lttng_live
;
1521 enum lttng_live_viewer_status viewer_status
;
1522 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1524 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1526 /* There can be only one downstream iterator at the same time. */
1527 BT_ASSERT(!lttng_live
->has_msg_iter
);
1528 lttng_live
->has_msg_iter
= true;
1530 auto lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1531 if (!lttng_live_msg_iter
) {
1532 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live
->logger
,
1533 "Failed to create lttng_live_msg_iter");
1534 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1537 viewer_status
= live_viewer_connection_create(
1538 lttng_live
->params
.url
.c_str(), false, lttng_live_msg_iter
.get(),
1539 lttng_live_msg_iter
->logger
, lttng_live_msg_iter
->viewer_connection
);
1540 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1541 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1542 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1543 "Failed to create viewer connection");
1544 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1546 * Interruption in the _iter_init() method is not
1547 * supported. Return an error.
1549 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1550 "Interrupted while creating viewer connection");
1553 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1556 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
.get());
1557 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1558 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1559 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1560 "Failed to create viewer session");
1561 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1563 * Interruption in the _iter_init() method is not
1564 * supported. Return an error.
1566 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1567 "Interrupted when creating viewer session");
1570 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1573 if (lttng_live_msg_iter
->sessions
.empty()) {
1574 switch (lttng_live
->params
.sess_not_found_act
) {
1575 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1577 lttng_live_msg_iter
->logger
,
1578 "Unable to connect to the requested live viewer session. "
1579 "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"",
1580 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1581 lttng_live
->params
.url
);
1583 case SESSION_NOT_FOUND_ACTION_FAIL
:
1584 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1585 lttng_live_msg_iter
->logger
,
1586 "Unable to connect to the requested live viewer session. "
1587 "Fail the message iterator initialization because of {}=\"{}\" "
1588 "component parameter: url =\"{}\"",
1589 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1590 lttng_live
->params
.url
);
1591 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1592 case SESSION_NOT_FOUND_ACTION_END
:
1593 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
1594 "Unable to connect to the requested live viewer session. "
1595 "End gracefully at the first _next() call because of {}=\"{}\""
1596 " component parameter: url=\"{}\"",
1597 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1598 lttng_live
->params
.url
);
1605 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
.release());
1606 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1607 } catch (const std::bad_alloc
&) {
1608 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1609 } catch (const bt2::Error
&) {
1610 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1614 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1615 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1616 bt_param_validation_value_descr::makeString()},
1617 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1619 static bt2::Value::Shared
lttng_live_query_list_sessions(const bt2::ConstMapValue params
,
1620 const bt2c::Logger
& logger
)
1623 live_viewer_connection::UP viewer_connection
;
1624 enum lttng_live_viewer_status viewer_status
;
1625 enum bt_param_validation_status validation_status
;
1626 gchar
*validate_error
= NULL
;
1629 bt_param_validation_validate(params
.libObjPtr(), list_sessions_params
, &validate_error
);
1630 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1631 throw bt2c::MemoryError
{};
1632 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1633 bt2c::GCharUP errorFreer
{validate_error
};
1635 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "{}", validate_error
);
1638 url
= params
[URL_PARAM
]->asString().value();
1640 viewer_status
= live_viewer_connection_create(url
, true, NULL
, logger
, viewer_connection
);
1641 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1642 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1643 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
1644 "Failed to create viewer connection");
1645 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1646 throw bt2c::TryAgain
{};
1652 return live_viewer_connection_list_sessions(viewer_connection
.get());
1655 static bt2::Value::Shared
lttng_live_query_support_info(const bt2::ConstMapValue params
,
1656 const bt2c::Logger
& logger
)
1658 struct bt_common_lttng_live_url_parts parts
= {};
1659 bt_common_lttng_live_url_parts_deleter partsDeleter
{parts
};
1661 /* Used by the logging macros */
1662 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1664 const auto typeValue
= params
["type"];
1666 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
1667 "Missing expected `type` parameter.");
1670 if (!typeValue
->isString()) {
1671 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
1672 "`type` parameter is not a string value.");
1675 if (strcmp(typeValue
->asString().value(), "string") != 0) {
1676 /* We don't handle file system paths */
1677 return bt2::RealValue::create();
1680 const auto inputValue
= params
["input"];
1682 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
1683 "Missing expected `input` parameter.");
1686 if (!inputValue
->isString()) {
1687 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
1688 "`input` parameter is not a string value.");
1691 parts
= bt_common_parse_lttng_live_url(inputValue
->asString().value(), NULL
, 0);
1692 if (parts
.session_name
) {
1694 * Looks pretty much like an LTTng live URL: we got the
1695 * session name part, which forms a complete URL.
1697 return bt2::RealValue::create(.75);
1700 return bt2::RealValue::create();
1703 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1704 bt_private_query_executor
*priv_query_exec
,
1705 const char *object
, const bt_value
*params
,
1706 __attribute__((unused
)) void *method_data
,
1707 const bt_value
**result
)
1710 bt2c::Logger logger
{bt2::SelfComponentClass
{comp_class
},
1711 bt2::PrivateQueryExecutor
{priv_query_exec
},
1712 "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"};
1713 const bt2::ConstMapValue
paramsObj(params
);
1714 bt2::Value::Shared resultObj
;
1716 if (strcmp(object
, "sessions") == 0) {
1717 resultObj
= lttng_live_query_list_sessions(paramsObj
, logger
);
1718 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1719 resultObj
= lttng_live_query_support_info(paramsObj
, logger
);
1721 BT_CPPLOGI_SPEC(logger
, "Unknown query object `{}`", object
);
1722 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1725 *result
= resultObj
.release().libObjPtr();
1727 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1728 } catch (const bt2c::TryAgain
&) {
1729 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1730 } catch (const std::bad_alloc
&) {
1731 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1732 } catch (const bt2::Error
&) {
1733 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1737 void lttng_live_component_finalize(bt_self_component_source
*component
)
1739 lttng_live_component::UP
{static_cast<lttng_live_component
*>(
1740 bt_self_component_get_data(bt_self_component_source_as_self_component(component
)))};
1743 static enum session_not_found_action
1744 parse_session_not_found_action_param(const bt_value
*no_session_param
)
1746 enum session_not_found_action action
;
1747 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1749 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1750 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1751 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1752 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1754 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1755 action
= SESSION_NOT_FOUND_ACTION_END
;
1761 static bt_param_validation_value_descr inputs_elem_descr
=
1762 bt_param_validation_value_descr::makeString();
1764 static const char *sess_not_found_action_choices
[] = {
1765 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1766 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1767 SESS_NOT_FOUND_ACTION_END_STR
,
1770 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1771 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1772 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
1773 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1774 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
1775 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1777 static bt_component_class_initialize_method_status
1778 lttng_live_component_create(const bt_value
*params
, bt_self_component_source
*self_comp
,
1779 lttng_live_component::UP
& component
)
1781 const bt_value
*inputs_value
;
1782 const bt_value
*url_value
;
1783 const bt_value
*value
;
1784 enum bt_param_validation_status validation_status
;
1785 gchar
*validation_error
= NULL
;
1786 bt2c::Logger logger
{bt2::SelfSourceComponent
{self_comp
}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"};
1788 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
1789 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1790 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1791 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1792 bt2c::GCharUP errorFreer
{validation_error
};
1793 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validation_error
);
1794 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1797 auto lttng_live
= bt2s::make_unique
<lttng_live_component
>(std::move(logger
));
1798 lttng_live
->self_comp
= bt_self_component_source_as_self_component(self_comp
);
1799 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1800 lttng_live
->has_msg_iter
= false;
1802 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
1803 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
1804 lttng_live
->params
.url
= bt_value_string_get(url_value
);
1806 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
1808 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
1810 BT_CPPLOGI_SPEC(lttng_live
->logger
,
1811 "Optional `{}` parameter is missing: defaulting to `{}`.",
1812 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1813 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1816 component
= std::move(lttng_live
);
1817 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1820 bt_component_class_initialize_method_status
1821 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
1822 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
1825 lttng_live_component::UP lttng_live
;
1826 bt_component_class_initialize_method_status ret
;
1827 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
1828 bt_self_component_add_port_status add_port_status
;
1830 ret
= lttng_live_component_create(params
, self_comp_src
, lttng_live
);
1831 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1836 bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
1837 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1838 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
1842 bt_self_component_set_data(self_comp
, lttng_live
.release());
1844 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1845 } catch (const std::bad_alloc
&) {
1846 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1847 } catch (const bt2::Error
&) {
1848 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;