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
)
77 struct lttng_live_trace
*ret_trace
= NULL
;
79 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
80 struct lttng_live_trace
*trace
=
81 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
82 if (trace
->id
== trace_id
) {
92 static void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
94 BT_CPPLOGD_SPEC(trace
->logger
, "Destroying live trace: trace-id={}", trace
->id
);
99 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
102 BT_CPPLOGD_SPEC(session
->logger
, "Creating live trace: session-id={}, trace-id={}", session
->id
,
105 auto trace
= bt2s::make_unique
<lttng_live_trace
>(session
->logger
);
107 trace
->session
= session
;
108 trace
->id
= trace_id
;
109 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
111 const auto ret
= trace
.get();
112 g_ptr_array_add(session
->traces
, trace
.release());
116 struct lttng_live_trace
*
117 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
120 struct lttng_live_trace
*trace
;
122 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
127 /* The session is the owner of the newly created trace. */
128 trace
= lttng_live_create_trace(session
, trace_id
);
134 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
135 const char *hostname
, const char *session_name
)
137 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
138 "Adding live session: "
139 "session-id={}, hostname=\"{}\", session-name=\"{}\"",
140 session_id
, hostname
, session_name
);
142 lttng_live_session
*session
= new lttng_live_session
{lttng_live_msg_iter
->logger
};
143 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
144 session
->id
= session_id
;
145 session
->traces
= g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_trace
);
146 BT_ASSERT(session
->traces
);
147 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
148 session
->new_streams_needed
= true;
149 session
->hostname
= g_string_new(hostname
);
150 BT_ASSERT(session
->hostname
);
152 session
->session_name
= g_string_new(session_name
);
153 BT_ASSERT(session
->session_name
);
155 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
160 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
166 BT_CPPLOGD_SPEC(session
->logger
,
167 "Destroying live session: "
168 "session-id={}, session-name=\"{}\"",
169 session
->id
, session
->session_name
->str
);
170 if (session
->id
!= -1ULL) {
171 if (lttng_live_session_detach(session
)) {
172 if (!lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
173 /* Old relayd cannot detach sessions. */
174 BT_CPPLOGD_SPEC(session
->logger
, "Unable to detach lttng live session {}",
181 if (session
->traces
) {
182 g_ptr_array_free(session
->traces
, TRUE
);
185 if (session
->hostname
) {
186 g_string_free(session
->hostname
, TRUE
);
189 if (session
->session_name
) {
190 g_string_free(session
->session_name
, TRUE
);
199 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
201 if (!lttng_live_msg_iter
) {
205 if (lttng_live_msg_iter
->sessions
) {
206 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
209 if (lttng_live_msg_iter
->viewer_connection
) {
210 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
212 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
213 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
215 /* All stream iterators must be destroyed at this point. */
216 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
217 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
219 delete lttng_live_msg_iter
;
225 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
227 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
229 BT_ASSERT(self_msg_iter
);
231 lttng_live_msg_iter
=
232 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
233 BT_ASSERT(lttng_live_msg_iter
);
234 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
237 static enum lttng_live_iterator_status
238 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
240 switch (lttng_live_stream
->state
) {
241 case LTTNG_LIVE_STREAM_QUIESCENT
:
242 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
244 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
246 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"ACTIVE_NO_DATA\"");
248 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
250 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"QUIESCENT_NO_DATA\"");
252 case LTTNG_LIVE_STREAM_EOF
:
255 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
259 * For active no data stream, fetch next index. As a result of that it can
261 * - quiescent: won't have events for a bit,
262 * - have data: need to get that data and produce the event,
263 * - have no data on this stream at this point: need to retry (AGAIN) or return
266 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
267 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
268 struct lttng_live_stream_iterator
*lttng_live_stream
)
270 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
271 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
272 struct packet_index index
;
274 if (lttng_live_stream
->trace
->metadata_stream_state
==
275 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
276 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
277 "Need to get an update for the metadata stream before proceeding "
278 "further with this stream: stream-name=\"{}\"",
279 lttng_live_stream
->name
);
280 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
284 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
285 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
286 "Need to get an update of all streams before proceeding further "
287 "with this stream: stream-name=\"{}\"",
288 lttng_live_stream
->name
);
289 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
293 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
294 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
297 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
298 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
302 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
304 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
305 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
306 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
308 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
310 * Because the stream is in the QUIESCENT_NO_DATA
311 * state, we can assert that the last_inactivity_ts was
312 * set and can be safely used in the `if` above.
314 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
316 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
317 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
319 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
324 lttng_live_stream
->base_offset
= index
.offset
;
325 lttng_live_stream
->offset
= index
.offset
;
326 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
328 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
329 "Setting live stream reading info: stream-name=\"{}\", "
330 "viewer-stream-id={}, stream-base-offset={}, stream-offset={}, stream-len={}",
331 lttng_live_stream
->name
, lttng_live_stream
->viewer_stream_id
,
332 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
,
333 lttng_live_stream
->len
);
336 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
337 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
343 * Creation of the message requires the ctf trace class to be created
344 * beforehand, but the live protocol gives us all streams (including metadata)
345 * at once. So we split it in three steps: getting streams, getting metadata
346 * (which creates the ctf trace class), and then creating the per-stream
349 static enum lttng_live_iterator_status
350 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
351 struct lttng_live_session
*session
)
353 enum lttng_live_iterator_status status
;
356 if (!session
->attached
) {
357 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
, "Attach to session: session-id={}",
359 enum lttng_live_viewer_status attach_status
=
360 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
361 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
362 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
364 * Clear any causes appended in
365 * `lttng_live_attach_session()` as we want to
366 * return gracefully since the graph was
369 bt_current_thread_clear_error();
370 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
372 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
373 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
374 "Error attaching to LTTng live session");
380 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
381 "Updating all data streams: "
382 "session-id={}, session-name=\"{}\"",
383 session
->id
, session
->session_name
->str
);
385 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
387 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
389 case LTTNG_LIVE_ITERATOR_STATUS_END
:
391 * We received a `_END` from the `_get_new_streams()` function,
392 * which means no more data will ever be received from the data
393 * streams of this session. But it's possible that the metadata
395 * The live protocol guarantees that we receive all the
396 * metadata needed before we receive data streams needing it.
397 * But it's possible to receive metadata NOT needed by
398 * data streams after the session was closed. For example, this
399 * could happen if a new event is registered and the session is
400 * stopped before any tracepoint for that event is actually
404 lttng_live_msg_iter
->logger
,
405 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
406 "session-id={}, session-name=\"{}\"",
407 session
->id
, session
->session_name
->str
);
408 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
414 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
415 "Updating metadata stream for session: "
416 "session-id={}, session-name=\"{}\"",
417 session
->id
, session
->session_name
->str
);
420 while (trace_idx
< session
->traces
->len
) {
421 struct lttng_live_trace
*trace
=
422 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
424 status
= lttng_live_metadata_update(trace
);
426 case LTTNG_LIVE_ITERATOR_STATUS_END
:
427 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
430 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
431 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
434 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
435 "Error updating trace metadata: "
436 "stream-iter-status={}, trace-id={}",
443 * Now that we have the metadata we can initialize the downstream
446 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
453 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
455 uint64_t session_idx
, trace_idx
;
457 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
458 struct lttng_live_session
*session
=
459 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
460 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
461 "Force marking session as needing new streams: "
464 session
->new_streams_needed
= true;
465 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
466 struct lttng_live_trace
*trace
=
467 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
468 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
469 "Force marking trace metadata state as needing an update: "
470 "session-id={}, trace-id={}",
471 session
->id
, trace
->id
);
473 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
475 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
480 static enum lttng_live_iterator_status
481 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
483 enum lttng_live_iterator_status status
;
484 enum lttng_live_viewer_status viewer_status
;
485 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
486 struct lttng_live_session
*session
;
487 enum session_not_found_action sess_not_found_act
=
488 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
490 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
491 "Update data and metadata of all sessions: "
492 "live-msg-iter-addr={}",
493 fmt::ptr(lttng_live_msg_iter
));
495 * In a remotely distant future, we could add a "new
496 * session" flag to the protocol, which would tell us that we
497 * need to query for new sessions even though we have sessions
500 if (lttng_live_msg_iter
->sessions
->len
== 0) {
501 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
503 lttng_live_msg_iter
->logger
,
504 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
505 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
509 lttng_live_msg_iter
->logger
,
510 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
512 * Retry to create a viewer session for the requested
515 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
516 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
517 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
518 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
519 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
520 "Error creating LTTng live viewer session");
521 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
522 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
531 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
533 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
534 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
536 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
537 case LTTNG_LIVE_ITERATOR_STATUS_END
:
539 * A session returned `_END`. Other sessions may still
540 * be active so we override the status and continue
547 if (!session
->closed
) {
548 nr_sessions_opened
++;
552 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
553 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
555 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
562 static enum lttng_live_iterator_status
563 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
564 struct lttng_live_stream_iterator
*stream_iter
,
565 bt2::ConstMessage::Shared
& message
, uint64_t timestamp
)
567 BT_ASSERT(stream_iter
->trace
->clock_class
);
568 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
569 "Emitting inactivity message for stream: ctf-stream-id={}, "
570 "viewer-stream-id={}, timestamp={}",
571 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
,
574 const auto msg
= bt_message_message_iterator_inactivity_create(
575 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->trace
->clock_class
, timestamp
);
578 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
579 "Error emitting message iterator inactivity message");
580 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
583 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
584 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
587 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
588 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
589 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
591 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
593 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
594 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
598 * Check if we already sent an inactivty message downstream for this
599 * `current_inactivity_ts` value.
601 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
602 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
603 lttng_live_stream_iterator_set_state(lttng_live_stream
,
604 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
606 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
610 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
611 lttng_live_stream
->current_inactivity_ts
);
613 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
614 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
619 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
620 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
622 const bt_clock_snapshot
*clock_snapshot
= NULL
;
626 BT_ASSERT_DBG(ts_ns
);
628 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
629 "Getting message's timestamp: iter-data-addr={}, msg-addr={}, "
631 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
);
633 switch (bt_message_get_type(msg
)) {
634 case BT_MESSAGE_TYPE_EVENT
:
635 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
637 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
638 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
640 case BT_MESSAGE_TYPE_PACKET_END
:
641 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
643 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
645 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
647 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
649 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
651 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
652 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
655 /* All the other messages have a higher priority */
656 BT_CPPLOGD_STR_SPEC(lttng_live_msg_iter
->logger
,
657 "Message has no timestamp: using the last message timestamp.");
658 *ts_ns
= last_msg_ts_ns
;
662 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
664 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
665 "Cannot get nanoseconds from Epoch of clock snapshot: "
666 "clock-snapshot-addr={}",
667 fmt::ptr(clock_snapshot
));
678 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
679 "Found message's timestamp: iter-data-addr={}, msg-addr={}, "
680 "last-msg-ts={}, ts={}",
681 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
, *ts_ns
);
687 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
688 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
689 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
691 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
692 enum ctf_msg_iter_status status
;
693 uint64_t session_idx
, trace_idx
;
695 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
696 struct lttng_live_session
*session
=
697 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
699 if (session
->new_streams_needed
) {
700 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
701 "Need an update for streams: "
704 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
707 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
708 struct lttng_live_trace
*trace
=
709 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
710 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
711 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
712 "Need an update for metadata stream: "
713 "session-id={}, trace-id={}",
714 session
->id
, trace
->id
);
715 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
721 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
722 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
723 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
724 "Invalid state of live stream iterator"
725 "stream-iter-status={}",
726 lttng_live_stream
->state
);
730 const bt_message
*msg
;
731 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
.get(), &msg
);
733 case CTF_MSG_ITER_STATUS_EOF
:
734 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
736 case CTF_MSG_ITER_STATUS_OK
:
737 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
738 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
740 case CTF_MSG_ITER_STATUS_AGAIN
:
742 * Continue immediately (end of packet). The next
743 * get_index may return AGAIN to delay the following
746 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
748 case CTF_MSG_ITER_STATUS_ERROR
:
750 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
751 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
752 "CTF message iterator failed to get next message: "
753 "msg-iter={}, msg-iter-status={}",
754 fmt::ptr(lttng_live_stream
->msg_iter
), status
);
762 static enum lttng_live_iterator_status
763 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
764 struct lttng_live_stream_iterator
*stream_iter
,
765 bt2::ConstMessage::Shared
& curr_msg
)
767 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
769 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
770 "Closing live stream iterator: stream-name=\"{}\", "
771 "viewer-stream-id={}",
772 stream_iter
->name
, stream_iter
->viewer_stream_id
);
775 * The viewer has hung up on us so we are closing the stream. The
776 * `ctf_msg_iter` should simply realize that it needs to close the
777 * stream properly by emitting the necessary stream end message.
779 const bt_message
*msg
;
780 enum ctf_msg_iter_status status
=
781 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
.get(), &msg
);
783 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
784 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
785 "Error getting the next message from CTF message iterator");
786 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
788 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
789 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
790 "Reached the end of the live stream iterator.");
791 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
795 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
797 curr_msg
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
805 * handle_no_data_streams()
807 * - for each ACTIVE_NO_DATA stream:
808 * - query relayd for stream data, or quiescence info.
809 * - if need metadata, get metadata, goto retry.
810 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
811 * - if quiescent, move to QUIESCENT streams
812 * - if fetched data, move to ACTIVE_DATA streams
813 * (at this point each stream either has data, or is quiescent)
817 * handle_new_streams_and_metadata()
818 * - query relayd for known streams, add them as ACTIVE_NO_DATA
819 * - query relayd for metadata
821 * call handle_active_no_data_streams()
823 * handle_quiescent_streams()
824 * - if at least one stream is ACTIVE_DATA:
825 * - peek stream event with lowest timestamp -> next_ts
826 * - for each quiescent stream
827 * - if next_ts >= quiescent end
828 * - set state to ACTIVE_NO_DATA
830 * - for each quiescent stream
831 * - set state to ACTIVE_NO_DATA
833 * call handle_active_no_data_streams()
835 * handle_active_data_streams()
836 * - if at least one stream is ACTIVE_DATA:
837 * - get stream event with lowest timestamp from heap
838 * - make that stream event the current message.
839 * - move this stream heap position to its next event
840 * - if we need to fetch data from relayd, move
841 * stream to ACTIVE_NO_DATA.
845 * end criterion: ctrl-c on client. If relayd exits or the session
846 * closes on the relay daemon side, we keep on waiting for streams.
847 * Eventually handle --end timestamp (also an end criterion).
849 * When disconnected from relayd: try to re-connect endlessly.
851 static enum lttng_live_iterator_status
852 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
853 struct lttng_live_stream_iterator
*stream_iter
,
854 bt2::ConstMessage::Shared
& curr_msg
)
856 enum lttng_live_iterator_status live_status
;
858 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
859 "Advancing live stream iterator until next message if possible: "
860 "stream-name=\"{}\", viewer-stream-id={}",
861 stream_iter
->name
, stream_iter
->viewer_stream_id
);
863 if (stream_iter
->has_stream_hung_up
) {
865 * The stream has hung up and the stream was properly closed
866 * during the last call to the current function. Return _END
867 * status now so that this stream iterator is removed for the
868 * stream iterator list.
870 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
875 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
878 * Make sure we have the most recent metadata and possibly some new
881 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
882 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
887 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
888 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
889 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
891 * We overwrite `live_status` since `curr_msg` is
892 * likely set to a valid message in this function.
895 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
900 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
901 stream_iter
, curr_msg
);
902 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
903 BT_ASSERT(!curr_msg
);
909 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
910 stream_iter
, curr_msg
);
911 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
912 BT_ASSERT(!curr_msg
);
916 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
918 lttng_live_msg_iter
->logger
,
919 "Ask the relay daemon for an updated view of the data and metadata streams");
923 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
924 "Returning from advancing live stream iterator: status={}, "
925 "stream-name=\"{}\", viewer-stream-id={}",
926 live_status
, stream_iter
->name
, stream_iter
->viewer_stream_id
);
931 static bool is_discarded_packet_or_event_message(const bt2::ConstMessage msg
)
933 return msg
.type() == bt2::MessageType::DiscardedEvents
||
934 msg
.type() == bt2::MessageType::DiscardedPackets
;
937 static enum lttng_live_iterator_status
938 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
939 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
940 uint64_t new_begin_ts
)
942 enum bt_property_availability availability
;
943 const bt_clock_snapshot
*clock_snapshot
;
947 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
948 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
950 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
951 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
953 const auto msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
954 iter
, stream
, new_begin_ts
, end_ts
);
957 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
960 bt_message_discarded_packets_set_count(msg
, count
);
961 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
962 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
965 static enum lttng_live_iterator_status
966 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
967 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
968 uint64_t new_begin_ts
)
970 enum bt_property_availability availability
;
971 const bt_clock_snapshot
*clock_snapshot
;
975 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
976 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
978 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
979 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
981 const auto msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
982 iter
, stream
, new_begin_ts
, end_ts
);
985 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
988 bt_message_discarded_events_set_count(msg
, count
);
989 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
990 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
993 static enum lttng_live_iterator_status
994 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
995 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
996 const bt2::ConstMessage
& late_msg
)
998 const bt_clock_class
*clock_class
;
999 const bt_stream_class
*stream_class
;
1000 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1001 int64_t last_inactivity_ts_ns
;
1002 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1003 enum lttng_live_iterator_status adjust_status
;
1004 bt2::ConstMessage::Shared adjusted_message
;
1007 * The timestamp of the current message is before the last message sent
1008 * by this component. We CANNOT send it as is.
1010 * The only expected scenario in which that could happen is the
1011 * following, everything else is a bug in this component, relay daemon,
1014 * Expected scenario: The CTF message iterator emitted discarded
1015 * packets and discarded events with synthesized beginning and end
1016 * timestamps from the bounds of the last known packet and the newly
1017 * decoded packet header. The CTF message iterator is not aware of
1018 * stream inactivity beacons. Hence, we have to adjust the beginning
1019 * timestamp of those types of messages if a stream signalled its
1020 * inactivity up until _after_ the last known packet's beginning
1023 * Otherwise, the monotonicity guarantee of message timestamps would
1026 * In short, the only scenario in which it's okay and fixable to
1027 * received a late message is when:
1028 * 1. the late message is a discarded packets or discarded events
1030 * 2. this stream produced an inactivity message downstream, and
1031 * 3. the timestamp of the late message is within the inactivity
1032 * timespan we sent downstream through the inactivity message.
1035 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1036 "Handling late message on live stream iterator: "
1037 "stream-name=\"{}\", viewer-stream-id={}",
1038 stream_iter
->name
, stream_iter
->viewer_stream_id
);
1040 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1041 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1042 "Invalid live stream state: "
1043 "have a late message when no inactivity message "
1044 "was ever sent for that stream.");
1045 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1049 if (!is_discarded_packet_or_event_message(late_msg
)) {
1050 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1051 "Invalid live stream state: "
1052 "have a late message that is not a packet discarded or "
1053 "event discarded message: late-msg-type={}",
1055 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1059 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
->libObjPtr());
1060 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1062 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1063 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1064 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1065 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1066 "Error converting last "
1067 "inactivity message timestamp to nanoseconds");
1068 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1072 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1073 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1074 "Invalid live stream state: "
1075 "have a late message that is none included in a stream "
1076 "inactivity timespan: last-inactivity-ts-ns={}, "
1077 "late-msg-ts-ns={}",
1078 last_inactivity_ts_ns
, late_msg_ts_ns
);
1079 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1084 * We now know that it's okay for this message to be late, we can now
1085 * adjust its timestamp to ensure monotonicity.
1087 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1088 "Adjusting the timestamp of late message: late-msg-type={}, "
1090 late_msg
.type(), stream_iter
->last_inactivity_ts
.value
);
1091 switch (late_msg
.type()) {
1092 case bt2::MessageType::DiscardedEvents
:
1093 adjust_status
= adjust_discarded_events_message(
1094 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
1095 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
1097 case bt2::MessageType::DiscardedPackets
:
1098 adjust_status
= adjust_discarded_packets_message(
1099 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
1100 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
1106 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1107 stream_iter_status
= adjust_status
;
1111 BT_ASSERT_DBG(adjusted_message
);
1112 stream_iter
->current_msg
= adjusted_message
;
1113 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1116 return stream_iter_status
;
1119 static enum lttng_live_iterator_status
1120 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1121 struct lttng_live_trace
*live_trace
,
1122 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1124 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1125 enum lttng_live_iterator_status stream_iter_status
;
1126 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1127 uint64_t stream_iter_idx
;
1129 BT_ASSERT_DBG(live_trace
);
1131 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1132 "Finding the next stream iterator for trace: "
1136 * Update the current message of every stream iterators of this trace.
1137 * The current msg of every stream must have a timestamp equal or
1138 * larger than the last message returned by this iterator. We must
1139 * ensure monotonicity.
1141 stream_iter_idx
= 0;
1142 while (stream_iter_idx
< live_trace
->stream_iterators
.size()) {
1143 bool stream_iter_is_ended
= false;
1144 lttng_live_stream_iterator
*stream_iter
=
1145 live_trace
->stream_iterators
[stream_iter_idx
].get();
1148 * If there is no current message for this stream, go fetch
1151 while (!stream_iter
->current_msg
) {
1152 bt2::ConstMessage::Shared msg
;
1153 int64_t curr_msg_ts_ns
= INT64_MAX
;
1155 stream_iter_status
=
1156 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, msg
);
1158 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1159 stream_iter_is_ended
= true;
1163 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1169 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1170 "Live stream iterator returned message: msg-type={}, "
1171 "stream-name=\"{}\", viewer-stream-id={}",
1172 msg
->type(), stream_iter
->name
, stream_iter
->viewer_stream_id
);
1175 * Get the timestamp in nanoseconds from origin of this
1178 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
->libObjPtr(),
1179 lttng_live_msg_iter
->last_msg_ts_ns
, &curr_msg_ts_ns
);
1182 * Check if the message of the current live stream
1183 * iterator occurred at the exact same time or after the
1184 * last message returned by this component's message
1185 * iterator. If not, we need to handle it with care.
1187 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1188 stream_iter
->current_msg
= std::move(msg
);
1189 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1192 * We received a message from the past. This
1193 * may be fixable but it can also be an error.
1195 stream_iter_status
=
1196 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, *msg
);
1197 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1198 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1199 "Late message could not be handled correctly: "
1200 "lttng-live-msg-iter-addr={}, "
1201 "stream-name=\"{}\", "
1202 "curr-msg-ts={}, last-msg-ts={}",
1203 fmt::ptr(lttng_live_msg_iter
), stream_iter
->name
,
1205 lttng_live_msg_iter
->last_msg_ts_ns
);
1206 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1212 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1214 if (!stream_iter_is_ended
) {
1215 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1216 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1218 * Update the current best candidate message
1219 * for the stream iterator of this live trace
1220 * to be forwarded downstream.
1222 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1223 youngest_candidate_stream_iter
= stream_iter
;
1224 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1226 * Order the messages in an arbitrary but
1227 * deterministic way.
1229 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1230 int ret
= common_muxing_compare_messages(
1231 stream_iter
->current_msg
->libObjPtr(),
1232 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1235 * The `youngest_candidate_stream_iter->current_msg`
1236 * should go first. Update the next
1237 * iterator and the current timestamp.
1239 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1240 youngest_candidate_stream_iter
= stream_iter
;
1241 } else if (ret
== 0) {
1243 * Unable to pick which one should go
1247 lttng_live_msg_iter
->logger
,
1248 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1249 "stream-iter-addr={}"
1250 "stream-iter-addr={}",
1251 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1258 * The live stream iterator has ended. That
1259 * iterator is removed from the array, but
1260 * there is no need to increment
1261 * stream_iter_idx as
1262 * g_ptr_array_remove_index_fast replaces the
1263 * removed element with the array's last
1266 bt2c::vectorFastRemove(live_trace
->stream_iterators
, stream_iter_idx
);
1270 if (youngest_candidate_stream_iter
) {
1271 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1272 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1275 * The only case where we don't have a candidate for this trace
1276 * is if we reached the end of all the iterators.
1278 BT_ASSERT(live_trace
->stream_iterators
.empty());
1279 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1283 return stream_iter_status
;
1286 static enum lttng_live_iterator_status
1287 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1288 struct lttng_live_session
*session
,
1289 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1291 enum lttng_live_iterator_status stream_iter_status
;
1292 uint64_t trace_idx
= 0;
1293 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1294 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1296 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1297 "Finding the next stream iterator for session: "
1301 * Make sure we are attached to the session and look for new streams
1304 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1305 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1306 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1307 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1311 BT_ASSERT_DBG(session
->traces
);
1313 while (trace_idx
< session
->traces
->len
) {
1314 bool trace_is_ended
= false;
1315 struct lttng_live_stream_iterator
*stream_iter
;
1316 struct lttng_live_trace
*trace
=
1317 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1319 stream_iter_status
=
1320 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1321 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1323 * All the live stream iterators for this trace are
1324 * ENDed. Remove the trace from this session.
1326 trace_is_ended
= true;
1327 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1331 if (!trace_is_ended
) {
1332 BT_ASSERT_DBG(stream_iter
);
1334 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1335 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1336 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1337 youngest_candidate_stream_iter
= stream_iter
;
1338 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1340 * Order the messages in an arbitrary but
1341 * deterministic way.
1343 int ret
= common_muxing_compare_messages(
1344 stream_iter
->current_msg
->libObjPtr(),
1345 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1348 * The `youngest_candidate_stream_iter->current_msg`
1349 * should go first. Update the next iterator
1350 * and the current timestamp.
1352 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1353 youngest_candidate_stream_iter
= stream_iter
;
1354 } else if (ret
== 0) {
1355 /* Unable to pick which one should go first. */
1357 lttng_live_msg_iter
->logger
,
1358 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1359 "stream-iter-addr={}"
1360 "youngest-candidate-stream-iter-addr={}",
1361 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1367 * trace_idx is not incremented since
1368 * g_ptr_array_remove_index_fast replaces the
1369 * element at trace_idx with the array's last element.
1371 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1374 if (youngest_candidate_stream_iter
) {
1375 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1376 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1379 * The only cases where we don't have a candidate for this
1381 * 1. if we reached the end of all the iterators of all the
1382 * traces of this session,
1383 * 2. if we never had live stream iterator in the first place.
1385 * In either cases, we return END.
1387 BT_ASSERT(session
->traces
->len
== 0);
1388 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1391 return stream_iter_status
;
1394 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1398 for (i
= 0; i
< count
; i
++) {
1399 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1403 bt_message_iterator_class_next_method_status
1404 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1405 uint64_t capacity
, uint64_t *count
)
1408 bt_message_iterator_class_next_method_status status
;
1409 enum lttng_live_viewer_status viewer_status
;
1410 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1411 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1412 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1413 enum lttng_live_iterator_status stream_iter_status
;
1414 uint64_t session_idx
;
1418 BT_ASSERT_DBG(lttng_live_msg_iter
);
1420 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1422 * The iterator was interrupted in a previous call to the
1423 * `_next()` method. We currently do not support generating
1424 * messages after such event. The babeltrace2 CLI should never
1425 * be running the graph after being interrupted. So this check
1426 * is to prevent other graph users from using this live
1427 * iterator in an messed up internal state.
1429 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1430 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1431 lttng_live_msg_iter
->logger
,
1432 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1437 * Clear all the invalid message reference that might be left over in
1440 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1443 * If no session are exposed on the relay found at the url provided by
1444 * the user, session count will be 0. In this case, we return status
1445 * end to return gracefully.
1447 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1448 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1449 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1453 * The are no more active session for this session
1454 * name. Retry to create a viewer session for the
1455 * requested session name.
1457 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1458 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1459 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1460 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1461 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1462 "Error creating LTTng live viewer session");
1463 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1464 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1473 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1474 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1478 * Here the muxing of message is done.
1480 * We need to iterate over all the streams of all the traces of all the
1481 * viewer sessions in order to get the message with the smallest
1482 * timestamp. In this case, a session is a viewer session and there is
1483 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1484 * kernel). Each viewer session can have multiple traces, for example,
1485 * 64bit UST viewer sessions could have multiple per-pid traces.
1487 * We iterate over the streams of each traces to update and see what is
1488 * their next message's timestamp. From those timestamps, we select the
1489 * message with the smallest timestamp as the best candidate message
1490 * for that trace and do the same thing across all the sessions.
1492 * We then compare the timestamp of best candidate message of all the
1493 * sessions to pick the message with the smallest timestamp and we
1496 while (*count
< capacity
) {
1497 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1498 *candidate_stream_iter
= NULL
;
1499 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1501 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1503 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1504 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1505 lttng_live_msg_iter
->sessions
, session_idx
);
1507 /* Find the best candidate message to send downstream. */
1508 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1509 &candidate_stream_iter
);
1511 /* If we receive an END status, it means that either:
1512 * - Those traces never had active streams (UST with no
1513 * data produced yet),
1514 * - All live stream iterators have ENDed.
1516 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1517 if (session
->closed
&& session
->traces
->len
== 0) {
1519 * Remove the session from the list.
1520 * session_idx is not modified since
1521 * g_ptr_array_remove_index_fast
1522 * replaces the the removed element with
1523 * the array's last element.
1525 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1532 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1536 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1537 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1538 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1539 youngest_stream_iter
= candidate_stream_iter
;
1540 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1542 * The currently selected message to be sent
1543 * downstream next has the exact same timestamp
1544 * that of the current candidate message. We
1545 * must break the tie in a predictable manner.
1547 BT_CPPLOGD_STR_SPEC(
1548 lttng_live_msg_iter
->logger
,
1549 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1551 * Order the messages in an arbitrary but
1552 * deterministic way.
1554 int ret
= common_muxing_compare_messages(
1555 candidate_stream_iter
->current_msg
->libObjPtr(),
1556 youngest_stream_iter
->current_msg
->libObjPtr());
1559 * The `candidate_stream_iter->current_msg`
1560 * should go first. Update the next
1561 * iterator and the current timestamp.
1563 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1564 youngest_stream_iter
= candidate_stream_iter
;
1565 } else if (ret
== 0) {
1566 /* Unable to pick which one should go first. */
1568 lttng_live_msg_iter
->logger
,
1569 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1570 "next-stream-iter-addr={}"
1571 "candidate-stream-iter-addr={}",
1572 fmt::ptr(youngest_stream_iter
), fmt::ptr(candidate_stream_iter
));
1579 if (!youngest_stream_iter
) {
1580 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1584 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1585 /* Ensure monotonicity. */
1586 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1587 youngest_stream_iter
->current_msg_ts_ns
);
1590 * Insert the next message to the message batch. This will set
1591 * stream iterator current message to NULL so that next time
1592 * we fetch the next message of that stream iterator
1594 msgs
[*count
] = youngest_stream_iter
->current_msg
.release().libObjPtr();
1597 /* Update the last timestamp in nanoseconds sent downstream. */
1598 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1599 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1601 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1605 switch (stream_iter_status
) {
1606 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1607 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1609 * If we gathered messages, return _OK even if the graph was
1610 * interrupted. This allows for the components downstream to at
1611 * least get the those messages. If the graph was indeed
1612 * interrupted there should not be another _next() call as the
1613 * application will tear down the graph. This component class
1614 * doesn't support restarting after an interruption.
1617 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1619 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1622 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1623 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1625 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1626 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1627 "Memory error preparing the next batch of messages: "
1628 "live-iter-status={}",
1629 stream_iter_status
);
1630 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1632 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1633 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1634 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1635 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1636 "Error preparing the next batch of messages: "
1637 "live-iter-status={}",
1638 stream_iter_status
);
1640 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1641 /* Put all existing messages on error. */
1642 put_messages(msgs
, *count
);
1650 } catch (const std::bad_alloc
&) {
1651 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1652 } catch (const bt2::Error
&) {
1653 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1657 static struct lttng_live_msg_iter
*
1658 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1659 bt_self_message_iterator
*self_msg_it
)
1661 lttng_live_msg_iter
*msg_iter
= new lttng_live_msg_iter
{lttng_live_comp
->logger
};
1662 msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1663 msg_iter
->lttng_live_comp
= lttng_live_comp
;
1664 msg_iter
->self_msg_iter
= self_msg_it
;
1666 msg_iter
->active_stream_iter
= 0;
1667 msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1668 msg_iter
->was_interrupted
= false;
1670 msg_iter
->sessions
=
1671 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1672 BT_ASSERT(msg_iter
->sessions
);
1677 bt_message_iterator_class_initialize_method_status
1678 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1679 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1682 bt_message_iterator_class_initialize_method_status status
;
1683 struct lttng_live_component
*lttng_live
;
1684 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1685 enum lttng_live_viewer_status viewer_status
;
1686 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1688 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1690 /* There can be only one downstream iterator at the same time. */
1691 BT_ASSERT(!lttng_live
->has_msg_iter
);
1692 lttng_live
->has_msg_iter
= true;
1694 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1695 if (!lttng_live_msg_iter
) {
1696 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live
->logger
,
1697 "Failed to create lttng_live_msg_iter");
1698 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1702 viewer_status
= live_viewer_connection_create(
1703 lttng_live
->params
.url
.c_str(), false, lttng_live_msg_iter
, lttng_live_msg_iter
->logger
,
1704 <tng_live_msg_iter
->viewer_connection
);
1705 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1706 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1707 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1708 "Failed to create viewer connection");
1709 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1711 * Interruption in the _iter_init() method is not
1712 * supported. Return an error.
1714 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1715 "Interrupted while creating viewer connection");
1718 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1722 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1723 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1724 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1725 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1726 "Failed to create viewer session");
1727 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1729 * Interruption in the _iter_init() method is not
1730 * supported. Return an error.
1732 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1733 "Interrupted when creating viewer session");
1736 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1740 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1741 switch (lttng_live
->params
.sess_not_found_act
) {
1742 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1744 lttng_live_msg_iter
->logger
,
1745 "Unable to connect to the requested live viewer session. "
1746 "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"",
1747 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1748 lttng_live
->params
.url
);
1750 case SESSION_NOT_FOUND_ACTION_FAIL
:
1751 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1752 lttng_live_msg_iter
->logger
,
1753 "Unable to connect to the requested live viewer session. "
1754 "Fail the message iterator initialization because of {}=\"{}\" "
1755 "component parameter: url =\"{}\"",
1756 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1757 lttng_live
->params
.url
);
1758 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1760 case SESSION_NOT_FOUND_ACTION_END
:
1761 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
1762 "Unable to connect to the requested live viewer session. "
1763 "End gracefully at the first _next() call because of {}=\"{}\""
1764 " component parameter: url=\"{}\"",
1765 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1766 lttng_live
->params
.url
);
1773 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1774 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1778 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1781 } catch (const std::bad_alloc
&) {
1782 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1783 } catch (const bt2::Error
&) {
1784 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1788 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1789 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1790 bt_param_validation_value_descr::makeString()},
1791 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1793 static bt_component_class_query_method_status
1794 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1795 const bt2c::Logger
& logger
)
1797 bt_component_class_query_method_status status
;
1798 const bt_value
*url_value
= NULL
;
1800 struct live_viewer_connection
*viewer_connection
= NULL
;
1801 enum lttng_live_viewer_status viewer_status
;
1802 enum bt_param_validation_status validation_status
;
1803 gchar
*validate_error
= NULL
;
1805 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1806 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1807 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1809 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1810 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1811 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validate_error
);
1815 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1816 url
= bt_value_string_get(url_value
);
1818 viewer_status
= live_viewer_connection_create(url
, true, NULL
, logger
, &viewer_connection
);
1819 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1820 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1821 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to create viewer connection");
1822 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1823 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1824 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1831 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1832 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1833 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to list viewer sessions");
1840 BT_VALUE_PUT_REF_AND_RESET(*result
);
1843 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1847 if (viewer_connection
) {
1848 live_viewer_connection_destroy(viewer_connection
);
1851 g_free(validate_error
);
1856 static bt_component_class_query_method_status
1857 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1858 const bt2c::Logger
& logger
)
1860 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1861 const bt_value
*input_type_value
;
1862 const bt_value
*input_value
;
1864 struct bt_common_lttng_live_url_parts parts
= {};
1866 /* Used by the logging macros */
1867 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1870 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1871 if (!input_type_value
) {
1872 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `type` parameter.");
1876 if (!bt_value_is_string(input_type_value
)) {
1877 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`type` parameter is not a string value.");
1881 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1882 /* We don't handle file system paths */
1886 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1888 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `input` parameter.");
1892 if (!bt_value_is_string(input_value
)) {
1893 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`input` parameter is not a string value.");
1897 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1898 if (parts
.session_name
) {
1900 * Looks pretty much like an LTTng live URL: we got the
1901 * session name part, which forms a complete URL.
1907 *result
= bt_value_real_create_init(weight
);
1909 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1917 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1920 BT_ASSERT(!*result
);
1923 bt_common_destroy_lttng_live_url_parts(&parts
);
1927 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1928 bt_private_query_executor
*priv_query_exec
,
1929 const char *object
, const bt_value
*params
,
1930 __attribute__((unused
)) void *method_data
,
1931 const bt_value
**result
)
1934 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1935 bt2c::Logger logger
{bt2::SelfComponentClass
{comp_class
},
1936 bt2::PrivateQueryExecutor
{priv_query_exec
},
1937 "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"};
1939 if (strcmp(object
, "sessions") == 0) {
1940 status
= lttng_live_query_list_sessions(params
, result
, logger
);
1941 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1942 status
= lttng_live_query_support_info(params
, result
, logger
);
1944 BT_CPPLOGI_SPEC(logger
, "Unknown query object `{}`", object
);
1945 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1951 } catch (const std::bad_alloc
&) {
1952 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1953 } catch (const bt2::Error
&) {
1954 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1958 void lttng_live_component_finalize(bt_self_component_source
*component
)
1960 lttng_live_component::UP
{static_cast<lttng_live_component
*>(
1961 bt_self_component_get_data(bt_self_component_source_as_self_component(component
)))};
1964 static enum session_not_found_action
1965 parse_session_not_found_action_param(const bt_value
*no_session_param
)
1967 enum session_not_found_action action
;
1968 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1970 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1971 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1972 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1973 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1975 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1976 action
= SESSION_NOT_FOUND_ACTION_END
;
1982 static bt_param_validation_value_descr inputs_elem_descr
=
1983 bt_param_validation_value_descr::makeString();
1985 static const char *sess_not_found_action_choices
[] = {
1986 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1987 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1988 SESS_NOT_FOUND_ACTION_END_STR
,
1991 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1992 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1993 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
1994 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1995 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
1996 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1998 static bt_component_class_initialize_method_status
1999 lttng_live_component_create(const bt_value
*params
, bt_self_component_source
*self_comp
,
2000 lttng_live_component::UP
& component
)
2002 const bt_value
*inputs_value
;
2003 const bt_value
*url_value
;
2004 const bt_value
*value
;
2005 enum bt_param_validation_status validation_status
;
2006 gchar
*validation_error
= NULL
;
2007 bt2c::Logger logger
{bt2::SelfSourceComponent
{self_comp
}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"};
2009 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
2010 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2011 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2012 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2013 bt2c::GCharUP errorFreer
{validation_error
};
2014 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validation_error
);
2015 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2018 auto lttng_live
= bt2s::make_unique
<lttng_live_component
>(std::move(logger
));
2019 lttng_live
->self_comp
= bt_self_component_source_as_self_component(self_comp
);
2020 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2021 lttng_live
->has_msg_iter
= false;
2023 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2024 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2025 lttng_live
->params
.url
= bt_value_string_get(url_value
);
2027 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
2029 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
2031 BT_CPPLOGI_SPEC(lttng_live
->logger
,
2032 "Optional `{}` parameter is missing: defaulting to `{}`.",
2033 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2034 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2037 component
= std::move(lttng_live
);
2038 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2041 bt_component_class_initialize_method_status
2042 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2043 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
2046 lttng_live_component::UP lttng_live
;
2047 bt_component_class_initialize_method_status ret
;
2048 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2049 bt_self_component_add_port_status add_port_status
;
2051 ret
= lttng_live_component_create(params
, self_comp_src
, lttng_live
);
2052 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2057 bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2058 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2059 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2063 bt_self_component_set_data(self_comp
, lttng_live
.release());
2065 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2066 } catch (const std::bad_alloc
&) {
2067 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2068 } catch (const bt2::Error
&) {
2069 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;