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/bt2s/make-unique.hpp"
18 #include "cpp-common/vendor/fmt/format.h"
20 #include "plugins/common/muxing/muxing.h"
21 #include "plugins/common/param-validation/param-validation.h"
23 #include "data-stream.hpp"
24 #include "lttng-live.hpp"
25 #include "metadata.hpp"
27 #define MAX_QUERY_SIZE (256 * 1024)
28 #define URL_PARAM "url"
29 #define INPUTS_PARAM "inputs"
30 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
31 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
32 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
33 #define SESS_NOT_FOUND_ACTION_END_STR "end"
35 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
36 enum lttng_live_stream_state new_state
)
38 BT_CPPLOGD_SPEC(stream_iter
->logger
,
39 "Setting live stream iterator state: viewer-stream-id={}, "
40 "old-state={}, new-state={}",
41 stream_iter
->viewer_stream_id
, stream_iter
->state
, new_state
);
43 stream_iter
->state
= new_state
;
46 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
48 BT_CPPLOGD_SPEC((live_stream_iter)->logger, \
49 "Live stream iterator state={}, " \
50 "last-inact-ts-is-set={}, last-inact-ts-value={}, " \
52 (live_stream_iter)->state, (live_stream_iter)->last_inactivity_ts.is_set, \
53 (live_stream_iter)->last_inactivity_ts.value, \
54 (live_stream_iter)->current_inactivity_ts); \
57 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
66 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
72 static struct lttng_live_trace
*
73 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
76 struct lttng_live_trace
*ret_trace
= NULL
;
78 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
79 struct lttng_live_trace
*trace
=
80 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
81 if (trace
->id
== trace_id
) {
91 static void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
93 BT_CPPLOGD_SPEC(trace
->logger
, "Destroying live trace: trace-id={}", trace
->id
);
95 BT_ASSERT(trace
->stream_iterators
);
96 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
98 lttng_live_metadata_fini(trace
);
102 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
105 BT_CPPLOGD_SPEC(session
->logger
, "Creating live trace: session-id={}, trace-id={}", session
->id
,
108 lttng_live_trace
*trace
= new lttng_live_trace
{session
->logger
};
109 trace
->session
= session
;
110 trace
->id
= trace_id
;
111 trace
->stream_iterators
=
112 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_stream_iterator_destroy
);
113 BT_ASSERT(trace
->stream_iterators
);
114 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
115 g_ptr_array_add(session
->traces
, trace
);
120 struct lttng_live_trace
*
121 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
124 struct lttng_live_trace
*trace
;
126 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
131 /* The session is the owner of the newly created trace. */
132 trace
= lttng_live_create_trace(session
, trace_id
);
138 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
139 const char *hostname
, const char *session_name
)
141 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
142 "Adding live session: "
143 "session-id={}, hostname=\"{}\", session-name=\"{}\"",
144 session_id
, hostname
, session_name
);
146 lttng_live_session
*session
= new lttng_live_session
{lttng_live_msg_iter
->logger
};
147 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
148 session
->id
= session_id
;
149 session
->traces
= g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_trace
);
150 BT_ASSERT(session
->traces
);
151 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
152 session
->new_streams_needed
= true;
153 session
->hostname
= g_string_new(hostname
);
154 BT_ASSERT(session
->hostname
);
156 session
->session_name
= g_string_new(session_name
);
157 BT_ASSERT(session
->session_name
);
159 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
164 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
170 BT_CPPLOGD_SPEC(session
->logger
,
171 "Destroying live session: "
172 "session-id={}, session-name=\"{}\"",
173 session
->id
, session
->session_name
->str
);
174 if (session
->id
!= -1ULL) {
175 if (lttng_live_session_detach(session
)) {
176 if (!lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
177 /* Old relayd cannot detach sessions. */
178 BT_CPPLOGD_SPEC(session
->logger
, "Unable to detach lttng live session {}",
185 if (session
->traces
) {
186 g_ptr_array_free(session
->traces
, TRUE
);
189 if (session
->hostname
) {
190 g_string_free(session
->hostname
, TRUE
);
193 if (session
->session_name
) {
194 g_string_free(session
->session_name
, TRUE
);
203 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
205 if (!lttng_live_msg_iter
) {
209 if (lttng_live_msg_iter
->sessions
) {
210 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
213 if (lttng_live_msg_iter
->viewer_connection
) {
214 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
216 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
217 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
219 /* All stream iterators must be destroyed at this point. */
220 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
221 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
223 delete lttng_live_msg_iter
;
229 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
231 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
233 BT_ASSERT(self_msg_iter
);
235 lttng_live_msg_iter
=
236 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
237 BT_ASSERT(lttng_live_msg_iter
);
238 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
241 static enum lttng_live_iterator_status
242 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
244 switch (lttng_live_stream
->state
) {
245 case LTTNG_LIVE_STREAM_QUIESCENT
:
246 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
248 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
250 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"ACTIVE_NO_DATA\"");
252 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
254 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"QUIESCENT_NO_DATA\"");
256 case LTTNG_LIVE_STREAM_EOF
:
259 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
263 * For active no data stream, fetch next index. As a result of that it can
265 * - quiescent: won't have events for a bit,
266 * - have data: need to get that data and produce the event,
267 * - have no data on this stream at this point: need to retry (AGAIN) or return
270 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
271 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
272 struct lttng_live_stream_iterator
*lttng_live_stream
)
274 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
275 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
276 struct packet_index index
;
278 if (lttng_live_stream
->trace
->metadata_stream_state
==
279 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
281 lttng_live_msg_iter
->logger
,
282 "Need to get an update for the metadata stream before proceeding further with this stream: "
283 "stream-name=\"{}\"",
284 lttng_live_stream
->name
->str
);
285 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
289 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
291 lttng_live_msg_iter
->logger
,
292 "Need to get an update of all streams before proceeding further with this stream: "
293 "stream-name=\"{}\"",
294 lttng_live_stream
->name
->str
);
295 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
299 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
300 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
303 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
304 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
308 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
310 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
311 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
312 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
314 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
316 * Because the stream is in the QUIESCENT_NO_DATA
317 * state, we can assert that the last_inactivity_ts was
318 * set and can be safely used in the `if` above.
320 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
322 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
323 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
325 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
330 lttng_live_stream
->base_offset
= index
.offset
;
331 lttng_live_stream
->offset
= index
.offset
;
332 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
334 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
335 "Setting live stream reading info: stream-name=\"{}\", "
336 "viewer-stream-id={}, stream-base-offset={}, stream-offset={}, stream-len={}",
337 lttng_live_stream
->name
->str
, lttng_live_stream
->viewer_stream_id
,
338 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
,
339 lttng_live_stream
->len
);
342 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
343 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
349 * Creation of the message requires the ctf trace class to be created
350 * beforehand, but the live protocol gives us all streams (including metadata)
351 * at once. So we split it in three steps: getting streams, getting metadata
352 * (which creates the ctf trace class), and then creating the per-stream
355 static enum lttng_live_iterator_status
356 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
357 struct lttng_live_session
*session
)
359 enum lttng_live_iterator_status status
;
362 if (!session
->attached
) {
363 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
, "Attach to session: session-id={}",
365 enum lttng_live_viewer_status attach_status
=
366 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
367 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
368 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
370 * Clear any causes appended in
371 * `lttng_live_attach_session()` as we want to
372 * return gracefully since the graph was
375 bt_current_thread_clear_error();
376 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
378 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
379 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
380 "Error attaching to LTTng live session");
386 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
387 "Updating all data streams: "
388 "session-id={}, session-name=\"{}\"",
389 session
->id
, session
->session_name
->str
);
391 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
393 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
395 case LTTNG_LIVE_ITERATOR_STATUS_END
:
397 * We received a `_END` from the `_get_new_streams()` function,
398 * which means no more data will ever be received from the data
399 * streams of this session. But it's possible that the metadata
401 * The live protocol guarantees that we receive all the
402 * metadata needed before we receive data streams needing it.
403 * But it's possible to receive metadata NOT needed by
404 * data streams after the session was closed. For example, this
405 * could happen if a new event is registered and the session is
406 * stopped before any tracepoint for that event is actually
410 lttng_live_msg_iter
->logger
,
411 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
412 "session-id={}, session-name=\"{}\"",
413 session
->id
, session
->session_name
->str
);
414 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
420 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
421 "Updating metadata stream for session: "
422 "session-id={}, session-name=\"{}\"",
423 session
->id
, session
->session_name
->str
);
426 while (trace_idx
< session
->traces
->len
) {
427 struct lttng_live_trace
*trace
=
428 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
430 status
= lttng_live_metadata_update(trace
);
432 case LTTNG_LIVE_ITERATOR_STATUS_END
:
433 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
436 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
437 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
440 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
441 "Error updating trace metadata: "
442 "stream-iter-status={}, trace-id={}",
449 * Now that we have the metadata we can initialize the downstream
452 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
459 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
461 uint64_t session_idx
, trace_idx
;
463 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
464 struct lttng_live_session
*session
=
465 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
466 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
467 "Force marking session as needing new streams: "
470 session
->new_streams_needed
= true;
471 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
472 struct lttng_live_trace
*trace
=
473 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
474 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
475 "Force marking trace metadata state as needing an update: "
476 "session-id={}, trace-id={}",
477 session
->id
, trace
->id
);
479 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
481 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
486 static enum lttng_live_iterator_status
487 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
489 enum lttng_live_iterator_status status
;
490 enum lttng_live_viewer_status viewer_status
;
491 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
492 struct lttng_live_session
*session
;
493 enum session_not_found_action sess_not_found_act
=
494 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
496 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
497 "Update data and metadata of all sessions: "
498 "live-msg-iter-addr={}",
499 fmt::ptr(lttng_live_msg_iter
));
501 * In a remotely distant future, we could add a "new
502 * session" flag to the protocol, which would tell us that we
503 * need to query for new sessions even though we have sessions
506 if (lttng_live_msg_iter
->sessions
->len
== 0) {
507 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
509 lttng_live_msg_iter
->logger
,
510 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
511 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
515 lttng_live_msg_iter
->logger
,
516 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
518 * Retry to create a viewer session for the requested
521 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
522 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
523 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
524 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
525 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
526 "Error creating LTTng live viewer session");
527 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
528 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
537 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
539 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
540 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
542 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
543 case LTTNG_LIVE_ITERATOR_STATUS_END
:
545 * A session returned `_END`. Other sessions may still
546 * be active so we override the status and continue
553 if (!session
->closed
) {
554 nr_sessions_opened
++;
558 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
559 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
561 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
568 static enum lttng_live_iterator_status
569 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
570 struct lttng_live_stream_iterator
*stream_iter
, const bt_message
**message
,
573 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
574 bt_message
*msg
= NULL
;
576 BT_ASSERT(stream_iter
->trace
->clock_class
);
578 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
579 "Emitting inactivity message for stream: ctf-stream-id={}, "
580 "viewer-stream-id={}, timestamp={}",
581 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
,
584 msg
= bt_message_message_iterator_inactivity_create(lttng_live_msg_iter
->self_msg_iter
,
585 stream_iter
->trace
->clock_class
, timestamp
);
587 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
588 "Error emitting message iterator inactivity message");
597 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
598 bt_message_put_ref(msg
);
602 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
603 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
604 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
606 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
608 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
609 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
613 * Check if we already sent an inactivty message downstream for this
614 * `current_inactivity_ts` value.
616 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
617 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
618 lttng_live_stream_iterator_set_state(lttng_live_stream
,
619 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
621 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
625 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
626 lttng_live_stream
->current_inactivity_ts
);
628 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
629 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
634 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
635 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
637 const bt_clock_snapshot
*clock_snapshot
= NULL
;
641 BT_ASSERT_DBG(ts_ns
);
643 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
644 "Getting message's timestamp: iter-data-addr={}, msg-addr={}, "
646 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
);
648 switch (bt_message_get_type(msg
)) {
649 case BT_MESSAGE_TYPE_EVENT
:
650 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
652 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
653 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
655 case BT_MESSAGE_TYPE_PACKET_END
:
656 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
658 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
660 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
662 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
664 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
666 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
667 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
670 /* All the other messages have a higher priority */
671 BT_CPPLOGD_STR_SPEC(lttng_live_msg_iter
->logger
,
672 "Message has no timestamp: using the last message timestamp.");
673 *ts_ns
= last_msg_ts_ns
;
677 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
679 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
680 "Cannot get nanoseconds from Epoch of clock snapshot: "
681 "clock-snapshot-addr={}",
682 fmt::ptr(clock_snapshot
));
693 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
694 "Found message's timestamp: iter-data-addr={}, msg-addr={}, "
695 "last-msg-ts={}, ts={}",
696 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
, *ts_ns
);
702 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
703 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
704 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
706 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
707 enum ctf_msg_iter_status status
;
708 uint64_t session_idx
, trace_idx
;
710 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
711 struct lttng_live_session
*session
=
712 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
714 if (session
->new_streams_needed
) {
715 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
716 "Need an update for streams: "
719 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
722 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
723 struct lttng_live_trace
*trace
=
724 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
725 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
726 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
727 "Need an update for metadata stream: "
728 "session-id={}, trace-id={}",
729 session
->id
, trace
->id
);
730 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
736 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
737 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
738 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
739 "Invalid state of live stream iterator"
740 "stream-iter-status={}",
741 lttng_live_stream
->state
);
745 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
, message
);
747 case CTF_MSG_ITER_STATUS_EOF
:
748 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
750 case CTF_MSG_ITER_STATUS_OK
:
751 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
753 case CTF_MSG_ITER_STATUS_AGAIN
:
755 * Continue immediately (end of packet). The next
756 * get_index may return AGAIN to delay the following
759 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
761 case CTF_MSG_ITER_STATUS_ERROR
:
763 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
764 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
765 "CTF message iterator failed to get next message: "
766 "msg-iter={}, msg-iter-status={}",
767 fmt::ptr(lttng_live_stream
->msg_iter
), status
);
775 static enum lttng_live_iterator_status
776 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
777 struct lttng_live_stream_iterator
*stream_iter
,
778 const bt_message
**curr_msg
)
780 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
782 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
783 "Closing live stream iterator: stream-name=\"{}\", "
784 "viewer-stream-id={}",
785 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
788 * The viewer has hung up on us so we are closing the stream. The
789 * `ctf_msg_iter` should simply realize that it needs to close the
790 * stream properly by emitting the necessary stream end message.
792 enum ctf_msg_iter_status status
=
793 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
, curr_msg
);
795 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
796 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
797 "Error getting the next message from CTF message iterator");
798 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
800 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
801 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
802 "Reached the end of the live stream iterator.");
803 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
807 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
815 * handle_no_data_streams()
817 * - for each ACTIVE_NO_DATA stream:
818 * - query relayd for stream data, or quiescence info.
819 * - if need metadata, get metadata, goto retry.
820 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
821 * - if quiescent, move to QUIESCENT streams
822 * - if fetched data, move to ACTIVE_DATA streams
823 * (at this point each stream either has data, or is quiescent)
827 * handle_new_streams_and_metadata()
828 * - query relayd for known streams, add them as ACTIVE_NO_DATA
829 * - query relayd for metadata
831 * call handle_active_no_data_streams()
833 * handle_quiescent_streams()
834 * - if at least one stream is ACTIVE_DATA:
835 * - peek stream event with lowest timestamp -> next_ts
836 * - for each quiescent stream
837 * - if next_ts >= quiescent end
838 * - set state to ACTIVE_NO_DATA
840 * - for each quiescent stream
841 * - set state to ACTIVE_NO_DATA
843 * call handle_active_no_data_streams()
845 * handle_active_data_streams()
846 * - if at least one stream is ACTIVE_DATA:
847 * - get stream event with lowest timestamp from heap
848 * - make that stream event the current message.
849 * - move this stream heap position to its next event
850 * - if we need to fetch data from relayd, move
851 * stream to ACTIVE_NO_DATA.
855 * end criterion: ctrl-c on client. If relayd exits or the session
856 * closes on the relay daemon side, we keep on waiting for streams.
857 * Eventually handle --end timestamp (also an end criterion).
859 * When disconnected from relayd: try to re-connect endlessly.
861 static enum lttng_live_iterator_status
862 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
863 struct lttng_live_stream_iterator
*stream_iter
,
864 const bt_message
**curr_msg
)
866 enum lttng_live_iterator_status live_status
;
868 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
869 "Advancing live stream iterator until next message if possible: "
870 "stream-name=\"{}\", viewer-stream-id={}",
871 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
873 if (stream_iter
->has_stream_hung_up
) {
875 * The stream has hung up and the stream was properly closed
876 * during the last call to the current function. Return _END
877 * status now so that this stream iterator is removed for the
878 * stream iterator list.
880 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
885 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
888 * Make sure we have the most recent metadata and possibly some new
891 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
892 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
897 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
898 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
899 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
901 * We overwrite `live_status` since `curr_msg` is
902 * likely set to a valid message in this function.
905 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
910 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
911 stream_iter
, curr_msg
);
912 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
913 BT_ASSERT(!*curr_msg
);
919 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
920 stream_iter
, curr_msg
);
921 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
922 BT_ASSERT(!*curr_msg
);
926 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
928 lttng_live_msg_iter
->logger
,
929 "Ask the relay daemon for an updated view of the data and metadata streams");
933 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
934 "Returning from advancing live stream iterator: status={}, "
935 "stream-name=\"{}\", viewer-stream-id={}",
936 live_status
, stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
941 static bool is_discarded_packet_or_event_message(const bt_message
*msg
)
943 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
945 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
946 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
949 static enum lttng_live_iterator_status
950 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
951 const bt_message
*msg_in
, bt_message
**msg_out
,
952 uint64_t new_begin_ts
)
954 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
955 enum bt_property_availability availability
;
956 const bt_clock_snapshot
*clock_snapshot
;
960 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
961 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
963 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
964 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
966 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
967 iter
, stream
, new_begin_ts
, end_ts
);
969 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
973 bt_message_discarded_packets_set_count(*msg_out
, count
);
978 static enum lttng_live_iterator_status
979 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
980 const bt_message
*msg_in
, bt_message
**msg_out
,
981 uint64_t new_begin_ts
)
983 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
984 enum bt_property_availability availability
;
985 const bt_clock_snapshot
*clock_snapshot
;
989 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
990 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
992 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
993 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
995 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
996 iter
, stream
, new_begin_ts
, end_ts
);
998 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1002 bt_message_discarded_events_set_count(*msg_out
, count
);
1007 static enum lttng_live_iterator_status
1008 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1009 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
1010 const bt_message
*late_msg
)
1012 const bt_clock_class
*clock_class
;
1013 const bt_stream_class
*stream_class
;
1014 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1015 int64_t last_inactivity_ts_ns
;
1016 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1017 enum lttng_live_iterator_status adjust_status
;
1018 bt_message
*adjusted_message
;
1021 * The timestamp of the current message is before the last message sent
1022 * by this component. We CANNOT send it as is.
1024 * The only expected scenario in which that could happen is the
1025 * following, everything else is a bug in this component, relay daemon,
1028 * Expected scenario: The CTF message iterator emitted discarded
1029 * packets and discarded events with synthesized beginning and end
1030 * timestamps from the bounds of the last known packet and the newly
1031 * decoded packet header. The CTF message iterator is not aware of
1032 * stream inactivity beacons. Hence, we have to adjust the beginning
1033 * timestamp of those types of messages if a stream signalled its
1034 * inactivity up until _after_ the last known packet's beginning
1037 * Otherwise, the monotonicity guarantee of message timestamps would
1040 * In short, the only scenario in which it's okay and fixable to
1041 * received a late message is when:
1042 * 1. the late message is a discarded packets or discarded events
1044 * 2. this stream produced an inactivity message downstream, and
1045 * 3. the timestamp of the late message is within the inactivity
1046 * timespan we sent downstream through the inactivity message.
1049 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1050 "Handling late message on live stream iterator: "
1051 "stream-name=\"{}\", viewer-stream-id={}",
1052 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1054 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1055 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1056 "Invalid live stream state: "
1057 "have a late message when no inactivity message "
1058 "was ever sent for that stream.");
1059 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1063 if (!is_discarded_packet_or_event_message(late_msg
)) {
1064 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1065 "Invalid live stream state: "
1066 "have a late message that is not a packet discarded or "
1067 "event discarded message: late-msg-type={}",
1068 static_cast<bt2::MessageType
>(bt_message_get_type(late_msg
)));
1069 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1073 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1074 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1076 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1077 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1078 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1079 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1080 "Error converting last "
1081 "inactivity message timestamp to nanoseconds");
1082 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1086 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1087 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1088 "Invalid live stream state: "
1089 "have a late message that is none included in a stream "
1090 "inactivity timespan: last-inactivity-ts-ns={}, "
1091 "late-msg-ts-ns={}",
1092 last_inactivity_ts_ns
, late_msg_ts_ns
);
1093 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1098 * We now know that it's okay for this message to be late, we can now
1099 * adjust its timestamp to ensure monotonicity.
1101 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1102 "Adjusting the timestamp of late message: late-msg-type={}, "
1104 static_cast<bt2::MessageType
>(bt_message_get_type(late_msg
)),
1105 stream_iter
->last_inactivity_ts
.value
);
1106 switch (bt_message_get_type(late_msg
)) {
1107 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1108 adjust_status
= adjust_discarded_events_message(
1109 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1110 stream_iter
->last_inactivity_ts
.value
);
1112 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1113 adjust_status
= adjust_discarded_packets_message(
1114 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1115 stream_iter
->last_inactivity_ts
.value
);
1121 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1122 stream_iter_status
= adjust_status
;
1126 BT_ASSERT_DBG(adjusted_message
);
1127 stream_iter
->current_msg
= adjusted_message
;
1128 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1129 bt_message_put_ref(late_msg
);
1132 return stream_iter_status
;
1135 static enum lttng_live_iterator_status
1136 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1137 struct lttng_live_trace
*live_trace
,
1138 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1140 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1141 enum lttng_live_iterator_status stream_iter_status
;
1142 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1143 uint64_t stream_iter_idx
;
1145 BT_ASSERT_DBG(live_trace
);
1146 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1148 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1149 "Finding the next stream iterator for trace: "
1153 * Update the current message of every stream iterators of this trace.
1154 * The current msg of every stream must have a timestamp equal or
1155 * larger than the last message returned by this iterator. We must
1156 * ensure monotonicity.
1158 stream_iter_idx
= 0;
1159 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1160 bool stream_iter_is_ended
= false;
1161 struct lttng_live_stream_iterator
*stream_iter
=
1162 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1166 * If there is no current message for this stream, go fetch
1169 while (!stream_iter
->current_msg
) {
1170 const bt_message
*msg
= NULL
;
1171 int64_t curr_msg_ts_ns
= INT64_MAX
;
1173 stream_iter_status
=
1174 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, &msg
);
1176 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1177 stream_iter_is_ended
= true;
1181 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1187 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1188 "Live stream iterator returned message: msg-type={}, "
1189 "stream-name=\"{}\", viewer-stream-id={}",
1190 static_cast<bt2::MessageType
>(bt_message_get_type(msg
)),
1191 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1194 * Get the timestamp in nanoseconds from origin of this
1197 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1201 * Check if the message of the current live stream
1202 * iterator occurred at the exact same time or after the
1203 * last message returned by this component's message
1204 * iterator. If not, we need to handle it with care.
1206 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1207 stream_iter
->current_msg
= msg
;
1208 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1211 * We received a message from the past. This
1212 * may be fixable but it can also be an error.
1214 stream_iter_status
=
1215 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, msg
);
1216 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1217 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1218 "Late message could not be handled correctly: "
1219 "lttng-live-msg-iter-addr={}, "
1220 "stream-name=\"{}\", "
1221 "curr-msg-ts={}, last-msg-ts={}",
1222 fmt::ptr(lttng_live_msg_iter
),
1223 stream_iter
->name
->str
, curr_msg_ts_ns
,
1224 lttng_live_msg_iter
->last_msg_ts_ns
);
1225 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1231 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1233 if (!stream_iter_is_ended
) {
1234 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1235 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1237 * Update the current best candidate message
1238 * for the stream iterator of this live trace
1239 * to be forwarded downstream.
1241 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1242 youngest_candidate_stream_iter
= stream_iter
;
1243 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1245 * Order the messages in an arbitrary but
1246 * deterministic way.
1248 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1249 int ret
= common_muxing_compare_messages(
1250 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1253 * The `youngest_candidate_stream_iter->current_msg`
1254 * should go first. Update the next
1255 * iterator and the current timestamp.
1257 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1258 youngest_candidate_stream_iter
= stream_iter
;
1259 } else if (ret
== 0) {
1261 * Unable to pick which one should go
1265 lttng_live_msg_iter
->logger
,
1266 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1267 "stream-iter-addr={}"
1268 "stream-iter-addr={}",
1269 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1276 * The live stream iterator has ended. That
1277 * iterator is removed from the array, but
1278 * there is no need to increment
1279 * stream_iter_idx as
1280 * g_ptr_array_remove_index_fast replaces the
1281 * removed element with the array's last
1284 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
, stream_iter_idx
);
1288 if (youngest_candidate_stream_iter
) {
1289 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1290 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1293 * The only case where we don't have a candidate for this trace
1294 * is if we reached the end of all the iterators.
1296 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1297 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1301 return stream_iter_status
;
1304 static enum lttng_live_iterator_status
1305 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1306 struct lttng_live_session
*session
,
1307 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1309 enum lttng_live_iterator_status stream_iter_status
;
1310 uint64_t trace_idx
= 0;
1311 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1312 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1314 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1315 "Finding the next stream iterator for session: "
1319 * Make sure we are attached to the session and look for new streams
1322 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1323 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1324 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1325 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1329 BT_ASSERT_DBG(session
->traces
);
1331 while (trace_idx
< session
->traces
->len
) {
1332 bool trace_is_ended
= false;
1333 struct lttng_live_stream_iterator
*stream_iter
;
1334 struct lttng_live_trace
*trace
=
1335 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1337 stream_iter_status
=
1338 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1339 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1341 * All the live stream iterators for this trace are
1342 * ENDed. Remove the trace from this session.
1344 trace_is_ended
= true;
1345 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1349 if (!trace_is_ended
) {
1350 BT_ASSERT_DBG(stream_iter
);
1352 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1353 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1354 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1355 youngest_candidate_stream_iter
= stream_iter
;
1356 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1358 * Order the messages in an arbitrary but
1359 * deterministic way.
1361 int ret
= common_muxing_compare_messages(
1362 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1365 * The `youngest_candidate_stream_iter->current_msg`
1366 * should go first. Update the next iterator
1367 * and the current timestamp.
1369 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1370 youngest_candidate_stream_iter
= stream_iter
;
1371 } else if (ret
== 0) {
1372 /* Unable to pick which one should go first. */
1374 lttng_live_msg_iter
->logger
,
1375 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1376 "stream-iter-addr={}"
1377 "youngest-candidate-stream-iter-addr={}",
1378 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1384 * trace_idx is not incremented since
1385 * g_ptr_array_remove_index_fast replaces the
1386 * element at trace_idx with the array's last element.
1388 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1391 if (youngest_candidate_stream_iter
) {
1392 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1393 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1396 * The only cases where we don't have a candidate for this
1398 * 1. if we reached the end of all the iterators of all the
1399 * traces of this session,
1400 * 2. if we never had live stream iterator in the first place.
1402 * In either cases, we return END.
1404 BT_ASSERT(session
->traces
->len
== 0);
1405 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1408 return stream_iter_status
;
1411 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1415 for (i
= 0; i
< count
; i
++) {
1416 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1420 bt_message_iterator_class_next_method_status
1421 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1422 uint64_t capacity
, uint64_t *count
)
1425 bt_message_iterator_class_next_method_status status
;
1426 enum lttng_live_viewer_status viewer_status
;
1427 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1428 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1429 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1430 enum lttng_live_iterator_status stream_iter_status
;
1431 uint64_t session_idx
;
1435 BT_ASSERT_DBG(lttng_live_msg_iter
);
1437 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1439 * The iterator was interrupted in a previous call to the
1440 * `_next()` method. We currently do not support generating
1441 * messages after such event. The babeltrace2 CLI should never
1442 * be running the graph after being interrupted. So this check
1443 * is to prevent other graph users from using this live
1444 * iterator in an messed up internal state.
1446 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1447 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1448 lttng_live_msg_iter
->logger
,
1449 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1454 * Clear all the invalid message reference that might be left over in
1457 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1460 * If no session are exposed on the relay found at the url provided by
1461 * the user, session count will be 0. In this case, we return status
1462 * end to return gracefully.
1464 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1465 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1466 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1470 * The are no more active session for this session
1471 * name. Retry to create a viewer session for the
1472 * requested session name.
1474 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1475 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1476 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1477 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1478 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1479 "Error creating LTTng live viewer session");
1480 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1481 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1490 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1491 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1495 * Here the muxing of message is done.
1497 * We need to iterate over all the streams of all the traces of all the
1498 * viewer sessions in order to get the message with the smallest
1499 * timestamp. In this case, a session is a viewer session and there is
1500 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1501 * kernel). Each viewer session can have multiple traces, for example,
1502 * 64bit UST viewer sessions could have multiple per-pid traces.
1504 * We iterate over the streams of each traces to update and see what is
1505 * their next message's timestamp. From those timestamps, we select the
1506 * message with the smallest timestamp as the best candidate message
1507 * for that trace and do the same thing across all the sessions.
1509 * We then compare the timestamp of best candidate message of all the
1510 * sessions to pick the message with the smallest timestamp and we
1513 while (*count
< capacity
) {
1514 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1515 *candidate_stream_iter
= NULL
;
1516 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1518 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1520 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1521 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1522 lttng_live_msg_iter
->sessions
, session_idx
);
1524 /* Find the best candidate message to send downstream. */
1525 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1526 &candidate_stream_iter
);
1528 /* If we receive an END status, it means that either:
1529 * - Those traces never had active streams (UST with no
1530 * data produced yet),
1531 * - All live stream iterators have ENDed.*/
1532 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1533 if (session
->closed
&& session
->traces
->len
== 0) {
1535 * Remove the session from the list.
1536 * session_idx is not modified since
1537 * g_ptr_array_remove_index_fast
1538 * replaces the the removed element with
1539 * the array's last element.
1541 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1548 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1552 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1553 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1554 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1555 youngest_stream_iter
= candidate_stream_iter
;
1556 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1558 * The currently selected message to be sent
1559 * downstream next has the exact same timestamp
1560 * that of the current candidate message. We
1561 * must break the tie in a predictable manner.
1563 BT_CPPLOGD_STR_SPEC(
1564 lttng_live_msg_iter
->logger
,
1565 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1567 * Order the messages in an arbitrary but
1568 * deterministic way.
1570 int ret
= common_muxing_compare_messages(candidate_stream_iter
->current_msg
,
1571 youngest_stream_iter
->current_msg
);
1574 * The `candidate_stream_iter->current_msg`
1575 * should go first. Update the next
1576 * iterator and the current timestamp.
1578 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1579 youngest_stream_iter
= candidate_stream_iter
;
1580 } else if (ret
== 0) {
1581 /* Unable to pick which one should go first. */
1583 lttng_live_msg_iter
->logger
,
1584 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1585 "next-stream-iter-addr={}"
1586 "candidate-stream-iter-addr={}",
1587 fmt::ptr(youngest_stream_iter
), fmt::ptr(candidate_stream_iter
));
1594 if (!youngest_stream_iter
) {
1595 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1599 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1600 /* Ensure monotonicity. */
1601 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1602 youngest_stream_iter
->current_msg_ts_ns
);
1605 * Insert the next message to the message batch. This will set
1606 * stream iterator current message to NULL so that next time
1607 * we fetch the next message of that stream iterator
1609 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1612 /* Update the last timestamp in nanoseconds sent downstream. */
1613 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1614 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1616 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1620 switch (stream_iter_status
) {
1621 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1622 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1624 * If we gathered messages, return _OK even if the graph was
1625 * interrupted. This allows for the components downstream to at
1626 * least get the those messages. If the graph was indeed
1627 * interrupted there should not be another _next() call as the
1628 * application will tear down the graph. This component class
1629 * doesn't support restarting after an interruption.
1632 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1634 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1637 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1638 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1640 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1641 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1642 "Memory error preparing the next batch of messages: "
1643 "live-iter-status={}",
1644 stream_iter_status
);
1645 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1647 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1648 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1649 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1650 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1651 "Error preparing the next batch of messages: "
1652 "live-iter-status={}",
1653 stream_iter_status
);
1655 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1656 /* Put all existing messages on error. */
1657 put_messages(msgs
, *count
);
1665 } catch (const std::bad_alloc
&) {
1666 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1667 } catch (const bt2::Error
&) {
1668 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1672 static struct lttng_live_msg_iter
*
1673 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1674 bt_self_message_iterator
*self_msg_it
)
1676 lttng_live_msg_iter
*msg_iter
= new lttng_live_msg_iter
{lttng_live_comp
->logger
};
1677 msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1678 msg_iter
->lttng_live_comp
= lttng_live_comp
;
1679 msg_iter
->self_msg_iter
= self_msg_it
;
1681 msg_iter
->active_stream_iter
= 0;
1682 msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1683 msg_iter
->was_interrupted
= false;
1685 msg_iter
->sessions
=
1686 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1687 BT_ASSERT(msg_iter
->sessions
);
1692 bt_message_iterator_class_initialize_method_status
1693 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1694 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1697 bt_message_iterator_class_initialize_method_status status
;
1698 struct lttng_live_component
*lttng_live
;
1699 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1700 enum lttng_live_viewer_status viewer_status
;
1701 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1703 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1705 /* There can be only one downstream iterator at the same time. */
1706 BT_ASSERT(!lttng_live
->has_msg_iter
);
1707 lttng_live
->has_msg_iter
= true;
1709 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1710 if (!lttng_live_msg_iter
) {
1711 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live
->logger
,
1712 "Failed to create lttng_live_msg_iter");
1713 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1717 viewer_status
= live_viewer_connection_create(
1718 lttng_live
->params
.url
.c_str(), false, lttng_live_msg_iter
, lttng_live_msg_iter
->logger
,
1719 <tng_live_msg_iter
->viewer_connection
);
1720 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1721 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1722 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1723 "Failed to create viewer connection");
1724 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1726 * Interruption in the _iter_init() method is not
1727 * supported. Return an error.
1729 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1730 "Interrupted while creating viewer connection");
1733 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1737 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1738 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1739 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1740 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1741 "Failed to create viewer session");
1742 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1744 * Interruption in the _iter_init() method is not
1745 * supported. Return an error.
1747 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1748 "Interrupted when creating viewer session");
1751 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1755 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1756 switch (lttng_live
->params
.sess_not_found_act
) {
1757 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1759 lttng_live_msg_iter
->logger
,
1760 "Unable to connect to the requested live viewer session. "
1761 "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"",
1762 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1763 lttng_live
->params
.url
);
1765 case SESSION_NOT_FOUND_ACTION_FAIL
:
1766 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1767 lttng_live_msg_iter
->logger
,
1768 "Unable to connect to the requested live viewer session. "
1769 "Fail the message iterator initialization because of {}=\"{}\" "
1770 "component parameter: url =\"{}\"",
1771 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1772 lttng_live
->params
.url
);
1773 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1775 case SESSION_NOT_FOUND_ACTION_END
:
1776 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
1777 "Unable to connect to the requested live viewer session. "
1778 "End gracefully at the first _next() call because of {}=\"{}\""
1779 " component parameter: url=\"{}\"",
1780 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1781 lttng_live
->params
.url
);
1788 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1789 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1793 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1796 } catch (const std::bad_alloc
&) {
1797 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1798 } catch (const bt2::Error
&) {
1799 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1803 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1804 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1805 bt_param_validation_value_descr::makeString()},
1806 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1808 static bt_component_class_query_method_status
1809 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1810 const bt2c::Logger
& logger
)
1812 bt_component_class_query_method_status status
;
1813 const bt_value
*url_value
= NULL
;
1815 struct live_viewer_connection
*viewer_connection
= NULL
;
1816 enum lttng_live_viewer_status viewer_status
;
1817 enum bt_param_validation_status validation_status
;
1818 gchar
*validate_error
= NULL
;
1820 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1821 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1822 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1824 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1825 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1826 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validate_error
);
1830 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1831 url
= bt_value_string_get(url_value
);
1833 viewer_status
= live_viewer_connection_create(url
, true, NULL
, logger
, &viewer_connection
);
1834 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1835 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1836 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to create viewer connection");
1837 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1838 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1839 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1846 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1847 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1848 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to list viewer sessions");
1855 BT_VALUE_PUT_REF_AND_RESET(*result
);
1858 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1862 if (viewer_connection
) {
1863 live_viewer_connection_destroy(viewer_connection
);
1866 g_free(validate_error
);
1871 static bt_component_class_query_method_status
1872 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1873 const bt2c::Logger
& logger
)
1875 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1876 const bt_value
*input_type_value
;
1877 const bt_value
*input_value
;
1879 struct bt_common_lttng_live_url_parts parts
= {};
1881 /* Used by the logging macros */
1882 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1885 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1886 if (!input_type_value
) {
1887 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `type` parameter.");
1891 if (!bt_value_is_string(input_type_value
)) {
1892 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`type` parameter is not a string value.");
1896 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1897 /* We don't handle file system paths */
1901 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1903 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `input` parameter.");
1907 if (!bt_value_is_string(input_value
)) {
1908 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`input` parameter is not a string value.");
1912 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1913 if (parts
.session_name
) {
1915 * Looks pretty much like an LTTng live URL: we got the
1916 * session name part, which forms a complete URL.
1922 *result
= bt_value_real_create_init(weight
);
1924 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1932 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1935 BT_ASSERT(!*result
);
1938 bt_common_destroy_lttng_live_url_parts(&parts
);
1942 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1943 bt_private_query_executor
*priv_query_exec
,
1944 const char *object
, const bt_value
*params
,
1945 __attribute__((unused
)) void *method_data
,
1946 const bt_value
**result
)
1949 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1950 bt2c::Logger logger
{bt2::SelfComponentClass
{comp_class
},
1951 bt2::PrivateQueryExecutor
{priv_query_exec
},
1952 "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"};
1954 if (strcmp(object
, "sessions") == 0) {
1955 status
= lttng_live_query_list_sessions(params
, result
, logger
);
1956 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1957 status
= lttng_live_query_support_info(params
, result
, logger
);
1959 BT_CPPLOGI_SPEC(logger
, "Unknown query object `{}`", object
);
1960 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1966 } catch (const std::bad_alloc
&) {
1967 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1968 } catch (const bt2::Error
&) {
1969 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1973 void lttng_live_component_finalize(bt_self_component_source
*component
)
1975 lttng_live_component::UP
{static_cast<lttng_live_component
*>(
1976 bt_self_component_get_data(bt_self_component_source_as_self_component(component
)))};
1979 static enum session_not_found_action
1980 parse_session_not_found_action_param(const bt_value
*no_session_param
)
1982 enum session_not_found_action action
;
1983 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1985 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1986 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1987 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1988 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1990 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1991 action
= SESSION_NOT_FOUND_ACTION_END
;
1997 static bt_param_validation_value_descr inputs_elem_descr
=
1998 bt_param_validation_value_descr::makeString();
2000 static const char *sess_not_found_action_choices
[] = {
2001 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2002 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2003 SESS_NOT_FOUND_ACTION_END_STR
,
2006 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2007 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
2008 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
2009 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
2010 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
2011 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
2013 static bt_component_class_initialize_method_status
2014 lttng_live_component_create(const bt_value
*params
, bt_self_component_source
*self_comp
,
2015 lttng_live_component::UP
& component
)
2017 const bt_value
*inputs_value
;
2018 const bt_value
*url_value
;
2019 const bt_value
*value
;
2020 enum bt_param_validation_status validation_status
;
2021 gchar
*validation_error
= NULL
;
2022 bt2c::Logger logger
{bt2::SelfSourceComponent
{self_comp
}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"};
2024 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
2025 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2026 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2027 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2028 bt2c::GCharUP errorFreer
{validation_error
};
2029 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validation_error
);
2030 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2033 auto lttng_live
= bt2s::make_unique
<lttng_live_component
>(std::move(logger
));
2034 lttng_live
->self_comp
= bt_self_component_source_as_self_component(self_comp
);
2035 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2036 lttng_live
->has_msg_iter
= false;
2038 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2039 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2040 lttng_live
->params
.url
= bt_value_string_get(url_value
);
2042 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
2044 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
2046 BT_CPPLOGI_SPEC(lttng_live
->logger
,
2047 "Optional `{}` parameter is missing: defaulting to `{}`.",
2048 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2049 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2052 component
= std::move(lttng_live
);
2053 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2056 bt_component_class_initialize_method_status
2057 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2058 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
2061 lttng_live_component::UP lttng_live
;
2062 bt_component_class_initialize_method_status ret
;
2063 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2064 bt_self_component_add_port_status add_port_status
;
2066 ret
= lttng_live_component_create(params
, self_comp_src
, lttng_live
);
2067 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2072 bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2073 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2074 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2078 bt_self_component_set_data(self_comp
, lttng_live
.release());
2080 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2081 } catch (const std::bad_alloc
&) {
2082 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2083 } catch (const bt2::Error
&) {
2084 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;