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
)
67 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
73 static struct lttng_live_trace
*
74 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
76 for (lttng_live_trace::UP
& trace
: session
->traces
) {
77 if (trace
->id
== trace_id
) {
85 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
88 BT_CPPLOGD_SPEC(session
->logger
, "Creating live trace: session-id={}, trace-id={}", session
->id
,
91 auto trace
= bt2s::make_unique
<lttng_live_trace
>(session
->logger
);
93 trace
->session
= session
;
95 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
97 const auto ret
= trace
.get();
98 session
->traces
.emplace_back(std::move(trace
));
102 struct lttng_live_trace
*
103 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
106 struct lttng_live_trace
*trace
;
108 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
113 /* The session is the owner of the newly created trace. */
114 trace
= lttng_live_create_trace(session
, trace_id
);
120 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
121 const char *hostname
, const char *session_name
)
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
);
128 lttng_live_session
*session
= new lttng_live_session
{lttng_live_msg_iter
->logger
};
129 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
130 session
->id
= session_id
;
131 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
132 session
->new_streams_needed
= true;
133 session
->hostname
= hostname
;
134 session
->session_name
= session_name
;
136 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
141 lttng_live_session::~lttng_live_session()
143 BT_CPPLOGD_SPEC(this->logger
, "Destroying live session: session-id={}, session-name=\"{}\"",
144 this->id
, this->session_name
);
146 if (this->id
!= -1ULL) {
147 if (lttng_live_session_detach(this)) {
148 if (!lttng_live_graph_is_canceled(this->lttng_live_msg_iter
)) {
149 /* Old relayd cannot detach sessions. */
150 BT_CPPLOGD_SPEC(this->logger
, "Unable to detach lttng live session {}", this->id
);
158 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
163 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
165 if (!lttng_live_msg_iter
) {
169 if (lttng_live_msg_iter
->sessions
) {
170 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
173 if (lttng_live_msg_iter
->viewer_connection
) {
174 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
176 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
177 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
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;
183 delete lttng_live_msg_iter
;
189 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
191 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
193 BT_ASSERT(self_msg_iter
);
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
);
201 static enum lttng_live_iterator_status
202 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
204 switch (lttng_live_stream
->state
) {
205 case LTTNG_LIVE_STREAM_QUIESCENT
:
206 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
208 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
210 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"ACTIVE_NO_DATA\"");
212 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
214 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"QUIESCENT_NO_DATA\"");
216 case LTTNG_LIVE_STREAM_EOF
:
219 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
223 * For active no data stream, fetch next index. As a result of that it can
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
230 static 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
)
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
;
238 if (lttng_live_stream
->trace
->metadata_stream_state
==
239 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
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
);
244 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
248 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
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
);
253 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
257 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
258 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
261 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
262 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
266 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
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
;
272 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
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.
278 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
280 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
281 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
283 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
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
;
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={}",
295 lttng_live_stream
->name
, lttng_live_stream
->viewer_stream_id
,
296 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
,
297 lttng_live_stream
->len
);
300 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
301 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
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
313 static enum lttng_live_iterator_status
314 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
315 struct lttng_live_session
*session
)
317 enum lttng_live_iterator_status status
;
319 if (!session
->attached
) {
320 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
, "Attach to session: session-id={}",
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
)) {
327 * Clear any causes appended in
328 * `lttng_live_attach_session()` as we want to
329 * return gracefully since the graph was
332 bt_current_thread_clear_error();
333 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
335 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
336 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
337 "Error attaching to LTTng live session");
343 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
344 "Updating all data streams: session-id={}, session-name=\"{}\"", session
->id
,
345 session
->session_name
);
347 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
349 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
351 case LTTNG_LIVE_ITERATOR_STATUS_END
:
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
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
366 lttng_live_msg_iter
->logger
,
367 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
368 "session-id={}, session-name=\"{}\"",
369 session
->id
, session
->session_name
);
370 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
376 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
377 "Updating metadata stream for session: session-id={}, session-name=\"{}\"",
378 session
->id
, session
->session_name
);
380 for (lttng_live_trace::UP
& trace
: session
->traces
) {
381 status
= lttng_live_metadata_update(trace
.get());
383 case LTTNG_LIVE_ITERATOR_STATUS_END
:
384 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
386 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
387 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
390 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
391 "Error updating trace metadata: "
392 "stream-iter-status={}, trace-id={}",
399 * Now that we have the metadata we can initialize the downstream
402 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
409 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
411 uint64_t session_idx
;
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
);
416 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
417 "Force marking session as needing new streams: "
420 session
->new_streams_needed
= true;
421 for (lttng_live_trace::UP
& trace
: session
->traces
) {
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
);
427 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
429 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
434 static enum lttng_live_iterator_status
435 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
437 enum lttng_live_iterator_status status
;
438 enum lttng_live_viewer_status viewer_status
;
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
;
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
));
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
454 if (lttng_live_msg_iter
->sessions
->len
== 0) {
455 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
457 lttng_live_msg_iter
->logger
,
458 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
459 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
463 lttng_live_msg_iter
->logger
,
464 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
466 * Retry to create a viewer session for the requested
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
;
473 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
474 "Error creating LTTng live viewer session");
475 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
476 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
485 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
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
);
490 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
491 case LTTNG_LIVE_ITERATOR_STATUS_END
:
493 * A session returned `_END`. Other sessions may still
494 * be active so we override the status and continue
501 if (!session
->closed
) {
502 nr_sessions_opened
++;
506 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
507 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
509 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
516 static enum lttng_live_iterator_status
517 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
518 struct lttng_live_stream_iterator
*stream_iter
,
519 bt2::ConstMessage::Shared
& message
, uint64_t timestamp
)
521 BT_ASSERT(stream_iter
->trace
->clock_class
);
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
,
528 const auto msg
= bt_message_message_iterator_inactivity_create(
529 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->trace
->clock_class
, timestamp
);
532 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
533 "Error emitting message iterator inactivity message");
534 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
537 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
538 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
541 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
542 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
543 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
545 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
547 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
548 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
552 * Check if we already sent an inactivty message downstream for this
553 * `current_inactivity_ts` value.
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
);
560 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
564 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
565 lttng_live_stream
->current_inactivity_ts
);
567 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
568 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
573 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
574 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
576 const bt_clock_snapshot
*clock_snapshot
= NULL
;
580 BT_ASSERT_DBG(ts_ns
);
582 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
583 "Getting message's timestamp: iter-data-addr={}, msg-addr={}, "
585 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
);
587 switch (bt_message_get_type(msg
)) {
588 case BT_MESSAGE_TYPE_EVENT
:
589 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
591 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
592 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
594 case BT_MESSAGE_TYPE_PACKET_END
:
595 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
597 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
599 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
601 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
603 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
605 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
606 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
609 /* All the other messages have a higher priority */
610 BT_CPPLOGD_STR_SPEC(lttng_live_msg_iter
->logger
,
611 "Message has no timestamp: using the last message timestamp.");
612 *ts_ns
= last_msg_ts_ns
;
616 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
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
));
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
);
641 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
642 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
643 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
645 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
646 enum ctf_msg_iter_status status
;
647 uint64_t session_idx
;
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
);
653 if (session
->new_streams_needed
) {
654 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
655 "Need an update for streams: "
658 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
661 for (lttng_live_trace::UP
& trace
: session
->traces
) {
662 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
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
);
667 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
673 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
674 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
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
);
682 const bt_message
*msg
;
683 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
.get(), &msg
);
685 case CTF_MSG_ITER_STATUS_EOF
:
686 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
688 case CTF_MSG_ITER_STATUS_OK
:
689 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
690 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
692 case CTF_MSG_ITER_STATUS_AGAIN
:
694 * Continue immediately (end of packet). The next
695 * get_index may return AGAIN to delay the following
698 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
700 case CTF_MSG_ITER_STATUS_ERROR
:
702 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
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
);
714 static enum lttng_live_iterator_status
715 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
716 struct lttng_live_stream_iterator
*stream_iter
,
717 bt2::ConstMessage::Shared
& curr_msg
)
719 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
721 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
722 "Closing live stream iterator: stream-name=\"{}\", "
723 "viewer-stream-id={}",
724 stream_iter
->name
, stream_iter
->viewer_stream_id
);
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.
731 const bt_message
*msg
;
732 enum ctf_msg_iter_status status
=
733 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
.get(), &msg
);
735 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
736 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
737 "Error getting the next message from CTF message iterator");
738 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
740 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
741 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
742 "Reached the end of the live stream iterator.");
743 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
747 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
749 curr_msg
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
757 * handle_no_data_streams()
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)
769 * handle_new_streams_and_metadata()
770 * - query relayd for known streams, add them as ACTIVE_NO_DATA
771 * - query relayd for metadata
773 * call handle_active_no_data_streams()
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
782 * - for each quiescent stream
783 * - set state to ACTIVE_NO_DATA
785 * call handle_active_no_data_streams()
787 * handle_active_data_streams()
788 * - if at least one stream is ACTIVE_DATA:
789 * - get stream event with lowest timestamp from heap
790 * - make that stream event the current message.
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.
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).
801 * When disconnected from relayd: try to re-connect endlessly.
803 static enum lttng_live_iterator_status
804 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
805 struct lttng_live_stream_iterator
*stream_iter
,
806 bt2::ConstMessage::Shared
& curr_msg
)
808 enum lttng_live_iterator_status live_status
;
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={}",
813 stream_iter
->name
, stream_iter
->viewer_stream_id
);
815 if (stream_iter
->has_stream_hung_up
) {
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.
822 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
827 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
830 * Make sure we have the most recent metadata and possibly some new
833 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
834 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
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
) {
843 * We overwrite `live_status` since `curr_msg` is
844 * likely set to a valid message in this function.
847 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
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
) {
855 BT_ASSERT(!curr_msg
);
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
) {
864 BT_ASSERT(!curr_msg
);
868 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
870 lttng_live_msg_iter
->logger
,
871 "Ask the relay daemon for an updated view of the data and metadata streams");
875 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
876 "Returning from advancing live stream iterator: status={}, "
877 "stream-name=\"{}\", viewer-stream-id={}",
878 live_status
, stream_iter
->name
, stream_iter
->viewer_stream_id
);
883 static bool is_discarded_packet_or_event_message(const bt2::ConstMessage msg
)
885 return msg
.type() == bt2::MessageType::DiscardedEvents
||
886 msg
.type() == bt2::MessageType::DiscardedPackets
;
889 static enum lttng_live_iterator_status
890 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
891 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
892 uint64_t new_begin_ts
)
894 enum bt_property_availability availability
;
895 const bt_clock_snapshot
*clock_snapshot
;
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
);
902 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
903 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
905 const auto msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
906 iter
, stream
, new_begin_ts
, end_ts
);
909 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
912 bt_message_discarded_packets_set_count(msg
, count
);
913 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
914 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
917 static enum lttng_live_iterator_status
918 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
919 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
920 uint64_t new_begin_ts
)
922 enum bt_property_availability availability
;
923 const bt_clock_snapshot
*clock_snapshot
;
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
);
930 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
931 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
933 const auto msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
934 iter
, stream
, new_begin_ts
, end_ts
);
937 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
940 bt_message_discarded_events_set_count(msg
, count
);
941 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
942 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
945 static enum lttng_live_iterator_status
946 handle_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
,
948 const bt2::ConstMessage
& late_msg
)
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
;
956 bt2::ConstMessage::Shared adjusted_message
;
959 * The timestamp of the current message is before the last message sent
960 * by this component. We CANNOT send it as is.
962 * The only expected scenario in which that could happen is the
963 * following, everything else is a bug in this component, relay daemon,
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
975 * Otherwise, the monotonicity guarantee of message timestamps would
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
982 * 2. this stream produced an inactivity message downstream, and
983 * 3. the timestamp of the late message is within the inactivity
984 * timespan we sent downstream through the inactivity message.
987 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
988 "Handling late message on live stream iterator: "
989 "stream-name=\"{}\", viewer-stream-id={}",
990 stream_iter
->name
, stream_iter
->viewer_stream_id
);
992 if (!stream_iter
->last_inactivity_ts
.is_set
) {
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.");
997 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1001 if (!is_discarded_packet_or_event_message(late_msg
)) {
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={}",
1007 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1011 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
->libObjPtr());
1012 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
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
) {
1017 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1018 "Error converting last "
1019 "inactivity message timestamp to nanoseconds");
1020 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1024 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
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
);
1031 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1036 * We now know that it's okay for this message to be late, we can now
1037 * adjust its timestamp to ensure monotonicity.
1039 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1040 "Adjusting the timestamp of late message: late-msg-type={}, "
1042 late_msg
.type(), stream_iter
->last_inactivity_ts
.value
);
1043 switch (late_msg
.type()) {
1044 case bt2::MessageType::DiscardedEvents
:
1045 adjust_status
= adjust_discarded_events_message(
1046 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
1047 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
1049 case bt2::MessageType::DiscardedPackets
:
1050 adjust_status
= adjust_discarded_packets_message(
1051 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
1052 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
1058 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1059 stream_iter_status
= adjust_status
;
1063 BT_ASSERT_DBG(adjusted_message
);
1064 stream_iter
->current_msg
= adjusted_message
;
1065 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1068 return stream_iter_status
;
1071 static enum lttng_live_iterator_status
1072 next_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
)
1076 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1077 enum lttng_live_iterator_status stream_iter_status
;
1078 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1079 uint64_t stream_iter_idx
;
1081 BT_ASSERT_DBG(live_trace
);
1083 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1084 "Finding the next stream iterator for trace: "
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.
1093 stream_iter_idx
= 0;
1094 while (stream_iter_idx
< live_trace
->stream_iterators
.size()) {
1095 bool stream_iter_is_ended
= false;
1096 lttng_live_stream_iterator
*stream_iter
=
1097 live_trace
->stream_iterators
[stream_iter_idx
].get();
1100 * If there is no current message for this stream, go fetch
1103 while (!stream_iter
->current_msg
) {
1104 bt2::ConstMessage::Shared msg
;
1105 int64_t curr_msg_ts_ns
= INT64_MAX
;
1107 stream_iter_status
=
1108 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, msg
);
1110 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1111 stream_iter_is_ended
= true;
1115 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1121 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1122 "Live stream iterator returned message: msg-type={}, "
1123 "stream-name=\"{}\", viewer-stream-id={}",
1124 msg
->type(), stream_iter
->name
, stream_iter
->viewer_stream_id
);
1127 * Get the timestamp in nanoseconds from origin of this
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
);
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.
1139 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1140 stream_iter
->current_msg
= std::move(msg
);
1141 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1144 * We received a message from the past. This
1145 * may be fixable but it can also be an error.
1147 stream_iter_status
=
1148 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, *msg
);
1149 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
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={}",
1155 fmt::ptr(lttng_live_msg_iter
), stream_iter
->name
,
1157 lttng_live_msg_iter
->last_msg_ts_ns
);
1158 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1164 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
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
) {
1170 * Update the current best candidate message
1171 * for the stream iterator of this live trace
1172 * to be forwarded downstream.
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
) {
1178 * Order the messages in an arbitrary but
1179 * deterministic way.
1181 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1182 int ret
= common_muxing_compare_messages(
1183 stream_iter
->current_msg
->libObjPtr(),
1184 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1187 * The `youngest_candidate_stream_iter->current_msg`
1188 * should go first. Update the next
1189 * iterator and the current timestamp.
1191 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1192 youngest_candidate_stream_iter
= stream_iter
;
1193 } else if (ret
== 0) {
1195 * Unable to pick which one should go
1199 lttng_live_msg_iter
->logger
,
1200 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1201 "stream-iter-addr={}"
1202 "stream-iter-addr={}",
1203 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
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
1218 bt2c::vectorFastRemove(live_trace
->stream_iterators
, stream_iter_idx
);
1222 if (youngest_candidate_stream_iter
) {
1223 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1224 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
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.
1230 BT_ASSERT(live_trace
->stream_iterators
.empty());
1231 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1235 return stream_iter_status
;
1238 static enum lttng_live_iterator_status
1239 next_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
)
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
;
1248 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1249 "Finding the next stream iterator for session: "
1253 * Make sure we are attached to the session and look for new streams
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
) {
1263 while (trace_idx
< session
->traces
.size()) {
1264 bool trace_is_ended
= false;
1265 struct lttng_live_stream_iterator
*stream_iter
;
1266 lttng_live_trace
*trace
= session
->traces
[trace_idx
].get();
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
) {
1272 * All the live stream iterators for this trace are
1273 * ENDed. Remove the trace from this session.
1275 trace_is_ended
= true;
1276 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1280 if (!trace_is_ended
) {
1281 BT_ASSERT_DBG(stream_iter
);
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
) {
1289 * Order the messages in an arbitrary but
1290 * deterministic way.
1292 int ret
= common_muxing_compare_messages(
1293 stream_iter
->current_msg
->libObjPtr(),
1294 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1297 * The `youngest_candidate_stream_iter->current_msg`
1298 * should go first. Update the next iterator
1299 * and the current timestamp.
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. */
1306 lttng_live_msg_iter
->logger
,
1307 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1308 "stream-iter-addr={}"
1309 "youngest-candidate-stream-iter-addr={}",
1310 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1316 * trace_idx is not incremented since
1317 * vectorFastRemove replaces the
1318 * element at trace_idx with the array's last element.
1320 bt2c::vectorFastRemove(session
->traces
, trace_idx
);
1323 if (youngest_candidate_stream_iter
) {
1324 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1325 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1328 * The only cases where we don't have a candidate for this
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.
1334 * In either cases, we return END.
1336 BT_ASSERT(session
->traces
.empty());
1337 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1340 return stream_iter_status
;
1343 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1347 for (i
= 0; i
< count
; i
++) {
1348 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1352 bt_message_iterator_class_next_method_status
1353 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1354 uint64_t capacity
, uint64_t *count
)
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
;
1367 BT_ASSERT_DBG(lttng_live_msg_iter
);
1369 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
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.
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.");
1386 * Clear all the invalid message reference that might be left over in
1389 memset(msgs
, 0, capacity
* sizeof(*msgs
));
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.
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
;
1402 * The are no more active session for this session
1403 * name. Retry to create a viewer session for the
1404 * requested session name.
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
;
1422 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1423 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1427 * Here the muxing of message is done.
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.
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.
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
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
;
1450 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
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
);
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
);
1460 /* If we receive an END status, it means that either:
1461 * - Those traces never had active streams (UST with no
1462 * data produced yet),
1463 * - All live stream iterators have ENDed.
1465 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1466 if (session
->closed
&& session
->traces
.empty()) {
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.
1474 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1481 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
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
) {
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.
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.");
1500 * Order the messages in an arbitrary but
1501 * deterministic way.
1503 int ret
= common_muxing_compare_messages(
1504 candidate_stream_iter
->current_msg
->libObjPtr(),
1505 youngest_stream_iter
->current_msg
->libObjPtr());
1508 * The `candidate_stream_iter->current_msg`
1509 * should go first. Update the next
1510 * iterator and the current timestamp.
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. */
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
));
1528 if (!youngest_stream_iter
) {
1529 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
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
);
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
1543 msgs
[*count
] = youngest_stream_iter
->current_msg
.release().libObjPtr();
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
;
1550 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1554 switch (stream_iter_status
) {
1555 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1556 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
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.
1566 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1568 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1571 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1572 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
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
;
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
);
1589 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1590 /* Put all existing messages on error. */
1591 put_messages(msgs
, *count
);
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
;
1606 static struct lttng_live_msg_iter
*
1607 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1608 bt_self_message_iterator
*self_msg_it
)
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
;
1615 msg_iter
->active_stream_iter
= 0;
1616 msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1617 msg_iter
->was_interrupted
= false;
1619 msg_iter
->sessions
=
1620 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1621 BT_ASSERT(msg_iter
->sessions
);
1626 bt_message_iterator_class_initialize_method_status
1627 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1628 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
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
);
1637 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
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;
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
;
1651 viewer_status
= live_viewer_connection_create(
1652 lttng_live
->params
.url
.c_str(), false, lttng_live_msg_iter
, lttng_live_msg_iter
->logger
,
1653 <tng_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
) {
1660 * Interruption in the _iter_init() method is not
1661 * supported. Return an error.
1663 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1664 "Interrupted while creating viewer connection");
1667 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
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
) {
1678 * Interruption in the _iter_init() method is not
1679 * supported. Return an error.
1681 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1682 "Interrupted when creating viewer session");
1685 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
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
:
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
,
1697 lttng_live
->params
.url
);
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
,
1706 lttng_live
->params
.url
);
1707 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_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
,
1715 lttng_live
->params
.url
);
1722 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1723 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1727 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
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
;
1737 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1738 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1739 bt_param_validation_value_descr::makeString()},
1740 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1742 static bt_component_class_query_method_status
1743 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1744 const bt2c::Logger
& logger
)
1746 bt_component_class_query_method_status status
;
1747 const bt_value
*url_value
= NULL
;
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
;
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
;
1758 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1759 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1760 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validate_error
);
1764 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1765 url
= bt_value_string_get(url_value
);
1767 viewer_status
= live_viewer_connection_create(url
, true, NULL
, logger
, &viewer_connection
);
1768 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1769 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1770 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to create viewer connection");
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
;
1780 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1781 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1782 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to list viewer sessions");
1789 BT_VALUE_PUT_REF_AND_RESET(*result
);
1792 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1796 if (viewer_connection
) {
1797 live_viewer_connection_destroy(viewer_connection
);
1800 g_free(validate_error
);
1805 static bt_component_class_query_method_status
1806 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1807 const bt2c::Logger
& logger
)
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
;
1813 struct bt_common_lttng_live_url_parts parts
= {};
1815 /* Used by the logging macros */
1816 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1819 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1820 if (!input_type_value
) {
1821 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `type` parameter.");
1825 if (!bt_value_is_string(input_type_value
)) {
1826 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`type` parameter is not a string value.");
1830 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1831 /* We don't handle file system paths */
1835 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1837 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `input` parameter.");
1841 if (!bt_value_is_string(input_value
)) {
1842 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`input` parameter is not a string value.");
1846 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1847 if (parts
.session_name
) {
1849 * Looks pretty much like an LTTng live URL: we got the
1850 * session name part, which forms a complete URL.
1856 *result
= bt_value_real_create_init(weight
);
1858 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1866 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1869 BT_ASSERT(!*result
);
1872 bt_common_destroy_lttng_live_url_parts(&parts
);
1876 bt_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
)
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"};
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
);
1893 BT_CPPLOGI_SPEC(logger
, "Unknown query object `{}`", object
);
1894 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
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
;
1907 void lttng_live_component_finalize(bt_self_component_source
*component
)
1909 lttng_live_component::UP
{static_cast<lttng_live_component
*>(
1910 bt_self_component_get_data(bt_self_component_source_as_self_component(component
)))};
1913 static enum session_not_found_action
1914 parse_session_not_found_action_param(const bt_value
*no_session_param
)
1916 enum session_not_found_action action
;
1917 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
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
;
1924 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1925 action
= SESSION_NOT_FOUND_ACTION_END
;
1931 static bt_param_validation_value_descr inputs_elem_descr
=
1932 bt_param_validation_value_descr::makeString();
1934 static const char *sess_not_found_action_choices
[] = {
1935 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1936 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1937 SESS_NOT_FOUND_ACTION_END_STR
,
1940 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
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
)},
1945 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1947 static bt_component_class_initialize_method_status
1948 lttng_live_component_create(const bt_value
*params
, bt_self_component_source
*self_comp
,
1949 lttng_live_component::UP
& component
)
1951 const bt_value
*inputs_value
;
1952 const bt_value
*url_value
;
1953 const bt_value
*value
;
1954 enum bt_param_validation_status validation_status
;
1955 gchar
*validation_error
= NULL
;
1956 bt2c::Logger logger
{bt2::SelfSourceComponent
{self_comp
}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"};
1958 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
1959 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1960 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1961 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1962 bt2c::GCharUP errorFreer
{validation_error
};
1963 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validation_error
);
1964 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1967 auto lttng_live
= bt2s::make_unique
<lttng_live_component
>(std::move(logger
));
1968 lttng_live
->self_comp
= bt_self_component_source_as_self_component(self_comp
);
1969 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1970 lttng_live
->has_msg_iter
= false;
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);
1974 lttng_live
->params
.url
= bt_value_string_get(url_value
);
1976 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
1978 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
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
);
1983 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1986 component
= std::move(lttng_live
);
1987 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1990 bt_component_class_initialize_method_status
1991 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
1992 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
1995 lttng_live_component::UP lttng_live
;
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
;
2000 ret
= lttng_live_component_create(params
, self_comp_src
, lttng_live
);
2001 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
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
;
2012 bt_self_component_set_data(self_comp
, lttng_live
.release());
2014 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
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
;