2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Babeltrace CTF LTTng-live Client Component
14 #include "common/assert.h"
15 #include "cpp-common/bt2c/fmt.hpp"
16 #include "cpp-common/bt2c/glib-up.hpp"
17 #include "cpp-common/bt2c/vector.hpp"
18 #include "cpp-common/bt2s/make-unique.hpp"
19 #include "cpp-common/vendor/fmt/format.h"
21 #include "plugins/common/muxing/muxing.h"
22 #include "plugins/common/param-validation/param-validation.h"
24 #include "data-stream.hpp"
25 #include "lttng-live.hpp"
26 #include "metadata.hpp"
28 #define MAX_QUERY_SIZE (256 * 1024)
29 #define URL_PARAM "url"
30 #define INPUTS_PARAM "inputs"
31 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
32 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
33 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
34 #define SESS_NOT_FOUND_ACTION_END_STR "end"
36 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
37 enum lttng_live_stream_state new_state
)
39 BT_CPPLOGD_SPEC(stream_iter
->logger
,
40 "Setting live stream iterator state: viewer-stream-id={}, "
41 "old-state={}, new-state={}",
42 stream_iter
->viewer_stream_id
, stream_iter
->state
, new_state
);
44 stream_iter
->state
= new_state
;
47 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
49 BT_CPPLOGD_SPEC((live_stream_iter)->logger, \
50 "Live stream iterator state={}, " \
51 "last-inact-ts-is-set={}, last-inact-ts-value={}, " \
53 (live_stream_iter)->state, (live_stream_iter)->last_inactivity_ts.is_set, \
54 (live_stream_iter)->last_inactivity_ts.value, \
55 (live_stream_iter)->current_inactivity_ts); \
58 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
67 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
73 static struct lttng_live_trace
*
74 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
76 for (lttng_live_trace::UP
& trace
: session
->traces
) {
77 if (trace
->id
== trace_id
) {
85 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
88 BT_CPPLOGD_SPEC(session
->logger
, "Creating live trace: session-id={}, trace-id={}", session
->id
,
91 auto trace
= bt2s::make_unique
<lttng_live_trace
>(session
->logger
);
93 trace
->session
= session
;
95 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
97 const auto ret
= trace
.get();
98 session
->traces
.emplace_back(std::move(trace
));
102 struct lttng_live_trace
*
103 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
106 struct lttng_live_trace
*trace
;
108 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
113 /* The session is the owner of the newly created trace. */
114 trace
= lttng_live_create_trace(session
, trace_id
);
120 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
121 const char *hostname
, const char *session_name
)
123 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
124 "Adding live session: "
125 "session-id={}, hostname=\"{}\", session-name=\"{}\"",
126 session_id
, hostname
, session_name
);
128 auto session
= bt2s::make_unique
<lttng_live_session
>(lttng_live_msg_iter
->logger
);
130 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
131 session
->id
= session_id
;
132 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
133 session
->new_streams_needed
= true;
134 session
->hostname
= hostname
;
135 session
->session_name
= session_name
;
137 lttng_live_msg_iter
->sessions
.emplace_back(std::move(session
));
142 lttng_live_session::~lttng_live_session()
144 BT_CPPLOGD_SPEC(this->logger
, "Destroying live session: session-id={}, session-name=\"{}\"",
145 this->id
, this->session_name
);
147 if (this->id
!= -1ULL) {
148 if (lttng_live_session_detach(this)) {
149 if (!lttng_live_graph_is_canceled(this->lttng_live_msg_iter
)) {
150 /* Old relayd cannot detach sessions. */
151 BT_CPPLOGD_SPEC(this->logger
, "Unable to detach lttng live session {}", this->id
);
159 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
161 if (!lttng_live_msg_iter
) {
165 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
166 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
168 /* All stream iterators must be destroyed at this point. */
169 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
170 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
172 delete lttng_live_msg_iter
;
178 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
180 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
182 BT_ASSERT(self_msg_iter
);
184 lttng_live_msg_iter
=
185 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
186 BT_ASSERT(lttng_live_msg_iter
);
187 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
190 static enum lttng_live_iterator_status
191 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
193 switch (lttng_live_stream
->state
) {
194 case LTTNG_LIVE_STREAM_QUIESCENT
:
195 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
197 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
199 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"ACTIVE_NO_DATA\"");
201 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
203 BT_CPPLOGF_SPEC(lttng_live_stream
->logger
, "Unexpected stream state \"QUIESCENT_NO_DATA\"");
205 case LTTNG_LIVE_STREAM_EOF
:
208 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
212 * For active no data stream, fetch next index. As a result of that it can
214 * - quiescent: won't have events for a bit,
215 * - have data: need to get that data and produce the event,
216 * - have no data on this stream at this point: need to retry (AGAIN) or return
219 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
220 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
221 struct lttng_live_stream_iterator
*lttng_live_stream
)
223 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
224 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
225 struct packet_index index
;
227 if (lttng_live_stream
->trace
->metadata_stream_state
==
228 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
229 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
230 "Need to get an update for the metadata stream before proceeding "
231 "further with this stream: stream-name=\"{}\"",
232 lttng_live_stream
->name
);
233 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
237 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
238 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
239 "Need to get an update of all streams before proceeding further "
240 "with this stream: stream-name=\"{}\"",
241 lttng_live_stream
->name
);
242 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
246 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
247 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
250 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
251 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
255 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
257 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
258 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
259 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
261 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
263 * Because the stream is in the QUIESCENT_NO_DATA
264 * state, we can assert that the last_inactivity_ts was
265 * set and can be safely used in the `if` above.
267 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
269 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
270 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
272 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
277 lttng_live_stream
->base_offset
= index
.offset
;
278 lttng_live_stream
->offset
= index
.offset
;
279 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
281 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
282 "Setting live stream reading info: stream-name=\"{}\", "
283 "viewer-stream-id={}, stream-base-offset={}, stream-offset={}, stream-len={}",
284 lttng_live_stream
->name
, lttng_live_stream
->viewer_stream_id
,
285 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
,
286 lttng_live_stream
->len
);
289 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
290 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
296 * Creation of the message requires the ctf trace class to be created
297 * beforehand, but the live protocol gives us all streams (including metadata)
298 * at once. So we split it in three steps: getting streams, getting metadata
299 * (which creates the ctf trace class), and then creating the per-stream
302 static enum lttng_live_iterator_status
303 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
304 struct lttng_live_session
*session
)
306 enum lttng_live_iterator_status status
;
308 if (!session
->attached
) {
309 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
, "Attach to session: session-id={}",
311 enum lttng_live_viewer_status attach_status
=
312 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
313 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
314 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
316 * Clear any causes appended in
317 * `lttng_live_attach_session()` as we want to
318 * return gracefully since the graph was
321 bt_current_thread_clear_error();
322 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
324 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
325 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
326 "Error attaching to LTTng live session");
332 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
333 "Updating all data streams: session-id={}, session-name=\"{}\"", session
->id
,
334 session
->session_name
);
336 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
338 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
340 case LTTNG_LIVE_ITERATOR_STATUS_END
:
342 * We received a `_END` from the `_get_new_streams()` function,
343 * which means no more data will ever be received from the data
344 * streams of this session. But it's possible that the metadata
346 * The live protocol guarantees that we receive all the
347 * metadata needed before we receive data streams needing it.
348 * But it's possible to receive metadata NOT needed by
349 * data streams after the session was closed. For example, this
350 * could happen if a new event is registered and the session is
351 * stopped before any tracepoint for that event is actually
355 lttng_live_msg_iter
->logger
,
356 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
357 "session-id={}, session-name=\"{}\"",
358 session
->id
, session
->session_name
);
359 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
365 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
366 "Updating metadata stream for session: session-id={}, session-name=\"{}\"",
367 session
->id
, session
->session_name
);
369 for (lttng_live_trace::UP
& trace
: session
->traces
) {
370 status
= lttng_live_metadata_update(trace
.get());
372 case LTTNG_LIVE_ITERATOR_STATUS_END
:
373 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
375 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
376 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
379 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
380 "Error updating trace metadata: "
381 "stream-iter-status={}, trace-id={}",
388 * Now that we have the metadata we can initialize the downstream
391 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
398 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
400 for (const auto& session
: lttng_live_msg_iter
->sessions
) {
401 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
402 "Force marking session as needing new streams: "
405 session
->new_streams_needed
= true;
406 for (lttng_live_trace::UP
& trace
: session
->traces
) {
407 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
408 "Force marking trace metadata state as needing an update: "
409 "session-id={}, trace-id={}",
410 session
->id
, trace
->id
);
412 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
414 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
419 static enum lttng_live_iterator_status
420 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
422 enum lttng_live_iterator_status status
;
423 enum lttng_live_viewer_status viewer_status
;
424 uint64_t nr_sessions_opened
= 0;
425 enum session_not_found_action sess_not_found_act
=
426 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
428 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
429 "Update data and metadata of all sessions: "
430 "live-msg-iter-addr={}",
431 fmt::ptr(lttng_live_msg_iter
));
433 * In a remotely distant future, we could add a "new
434 * session" flag to the protocol, which would tell us that we
435 * need to query for new sessions even though we have sessions
438 if (lttng_live_msg_iter
->sessions
.empty()) {
439 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
441 lttng_live_msg_iter
->logger
,
442 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
443 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
447 lttng_live_msg_iter
->logger
,
448 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
450 * Retry to create a viewer session for the requested
453 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
454 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
455 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
456 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
457 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
458 "Error creating LTTng live viewer session");
459 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
460 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
469 for (const auto& session
: lttng_live_msg_iter
->sessions
) {
470 status
= lttng_live_get_session(lttng_live_msg_iter
, session
.get());
472 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
473 case LTTNG_LIVE_ITERATOR_STATUS_END
:
475 * A session returned `_END`. Other sessions may still
476 * be active so we override the status and continue
483 if (!session
->closed
) {
484 nr_sessions_opened
++;
488 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
489 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
491 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
498 static enum lttng_live_iterator_status
499 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
500 struct lttng_live_stream_iterator
*stream_iter
,
501 bt2::ConstMessage::Shared
& message
, uint64_t timestamp
)
503 BT_ASSERT(stream_iter
->trace
->clock_class
);
504 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
505 "Emitting inactivity message for stream: ctf-stream-id={}, "
506 "viewer-stream-id={}, timestamp={}",
507 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
,
510 const auto msg
= bt_message_message_iterator_inactivity_create(
511 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->trace
->clock_class
, timestamp
);
514 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
515 "Error emitting message iterator inactivity message");
516 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
519 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
520 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
523 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
524 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
525 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
527 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
529 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
530 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
534 * Check if we already sent an inactivty message downstream for this
535 * `current_inactivity_ts` value.
537 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
538 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
539 lttng_live_stream_iterator_set_state(lttng_live_stream
,
540 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
542 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
546 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
547 lttng_live_stream
->current_inactivity_ts
);
549 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
550 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
555 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
556 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
558 const bt_clock_snapshot
*clock_snapshot
= NULL
;
562 BT_ASSERT_DBG(ts_ns
);
564 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
565 "Getting message's timestamp: iter-data-addr={}, msg-addr={}, "
567 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
);
569 switch (bt_message_get_type(msg
)) {
570 case BT_MESSAGE_TYPE_EVENT
:
571 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
573 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
574 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
576 case BT_MESSAGE_TYPE_PACKET_END
:
577 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
579 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
581 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
583 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
585 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
587 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
588 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
591 /* All the other messages have a higher priority */
592 BT_CPPLOGD_STR_SPEC(lttng_live_msg_iter
->logger
,
593 "Message has no timestamp: using the last message timestamp.");
594 *ts_ns
= last_msg_ts_ns
;
598 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
600 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
601 "Cannot get nanoseconds from Epoch of clock snapshot: "
602 "clock-snapshot-addr={}",
603 fmt::ptr(clock_snapshot
));
614 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
615 "Found message's timestamp: iter-data-addr={}, msg-addr={}, "
616 "last-msg-ts={}, ts={}",
617 fmt::ptr(lttng_live_msg_iter
), fmt::ptr(msg
), last_msg_ts_ns
, *ts_ns
);
623 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
624 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
625 struct lttng_live_stream_iterator
*lttng_live_stream
, bt2::ConstMessage::Shared
& message
)
627 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
628 enum ctf_msg_iter_status status
;
630 for (const auto& session
: lttng_live_msg_iter
->sessions
) {
631 if (session
->new_streams_needed
) {
632 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
633 "Need an update for streams: "
636 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
639 for (lttng_live_trace::UP
& trace
: session
->traces
) {
640 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
641 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
642 "Need an update for metadata stream: "
643 "session-id={}, trace-id={}",
644 session
->id
, trace
->id
);
645 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
651 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
652 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
653 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
654 "Invalid state of live stream iterator"
655 "stream-iter-status={}",
656 lttng_live_stream
->state
);
660 const bt_message
*msg
;
661 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
.get(), &msg
);
663 case CTF_MSG_ITER_STATUS_EOF
:
664 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
666 case CTF_MSG_ITER_STATUS_OK
:
667 message
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
668 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
670 case CTF_MSG_ITER_STATUS_AGAIN
:
672 * Continue immediately (end of packet). The next
673 * get_index may return AGAIN to delay the following
676 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
678 case CTF_MSG_ITER_STATUS_ERROR
:
680 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
681 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
682 "CTF message iterator failed to get next message: "
683 "msg-iter={}, msg-iter-status={}",
684 fmt::ptr(lttng_live_stream
->msg_iter
), status
);
692 static enum lttng_live_iterator_status
693 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
694 struct lttng_live_stream_iterator
*stream_iter
,
695 bt2::ConstMessage::Shared
& curr_msg
)
697 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
699 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
700 "Closing live stream iterator: stream-name=\"{}\", "
701 "viewer-stream-id={}",
702 stream_iter
->name
, stream_iter
->viewer_stream_id
);
705 * The viewer has hung up on us so we are closing the stream. The
706 * `ctf_msg_iter` should simply realize that it needs to close the
707 * stream properly by emitting the necessary stream end message.
709 const bt_message
*msg
;
710 enum ctf_msg_iter_status status
=
711 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
.get(), &msg
);
713 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
714 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
715 "Error getting the next message from CTF message iterator");
716 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
718 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
719 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
720 "Reached the end of the live stream iterator.");
721 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
725 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
727 curr_msg
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
735 * handle_no_data_streams()
737 * - for each ACTIVE_NO_DATA stream:
738 * - query relayd for stream data, or quiescence info.
739 * - if need metadata, get metadata, goto retry.
740 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
741 * - if quiescent, move to QUIESCENT streams
742 * - if fetched data, move to ACTIVE_DATA streams
743 * (at this point each stream either has data, or is quiescent)
747 * handle_new_streams_and_metadata()
748 * - query relayd for known streams, add them as ACTIVE_NO_DATA
749 * - query relayd for metadata
751 * call handle_active_no_data_streams()
753 * handle_quiescent_streams()
754 * - if at least one stream is ACTIVE_DATA:
755 * - peek stream event with lowest timestamp -> next_ts
756 * - for each quiescent stream
757 * - if next_ts >= quiescent end
758 * - set state to ACTIVE_NO_DATA
760 * - for each quiescent stream
761 * - set state to ACTIVE_NO_DATA
763 * call handle_active_no_data_streams()
765 * handle_active_data_streams()
766 * - if at least one stream is ACTIVE_DATA:
767 * - get stream event with lowest timestamp from heap
768 * - make that stream event the current message.
769 * - move this stream heap position to its next event
770 * - if we need to fetch data from relayd, move
771 * stream to ACTIVE_NO_DATA.
775 * end criterion: ctrl-c on client. If relayd exits or the session
776 * closes on the relay daemon side, we keep on waiting for streams.
777 * Eventually handle --end timestamp (also an end criterion).
779 * When disconnected from relayd: try to re-connect endlessly.
781 static enum lttng_live_iterator_status
782 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
783 struct lttng_live_stream_iterator
*stream_iter
,
784 bt2::ConstMessage::Shared
& curr_msg
)
786 enum lttng_live_iterator_status live_status
;
788 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
789 "Advancing live stream iterator until next message if possible: "
790 "stream-name=\"{}\", viewer-stream-id={}",
791 stream_iter
->name
, stream_iter
->viewer_stream_id
);
793 if (stream_iter
->has_stream_hung_up
) {
795 * The stream has hung up and the stream was properly closed
796 * during the last call to the current function. Return _END
797 * status now so that this stream iterator is removed for the
798 * stream iterator list.
800 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
805 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
808 * Make sure we have the most recent metadata and possibly some new
811 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
812 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
817 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
818 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
819 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
821 * We overwrite `live_status` since `curr_msg` is
822 * likely set to a valid message in this function.
825 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
830 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
831 stream_iter
, curr_msg
);
832 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
833 BT_ASSERT(!curr_msg
);
839 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
840 stream_iter
, curr_msg
);
841 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
842 BT_ASSERT(!curr_msg
);
846 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
848 lttng_live_msg_iter
->logger
,
849 "Ask the relay daemon for an updated view of the data and metadata streams");
853 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
854 "Returning from advancing live stream iterator: status={}, "
855 "stream-name=\"{}\", viewer-stream-id={}",
856 live_status
, stream_iter
->name
, stream_iter
->viewer_stream_id
);
861 static bool is_discarded_packet_or_event_message(const bt2::ConstMessage msg
)
863 return msg
.type() == bt2::MessageType::DiscardedEvents
||
864 msg
.type() == bt2::MessageType::DiscardedPackets
;
867 static enum lttng_live_iterator_status
868 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
869 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
870 uint64_t new_begin_ts
)
872 enum bt_property_availability availability
;
873 const bt_clock_snapshot
*clock_snapshot
;
877 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
878 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
880 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
881 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
883 const auto msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
884 iter
, stream
, new_begin_ts
, end_ts
);
887 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
890 bt_message_discarded_packets_set_count(msg
, count
);
891 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
892 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
895 static enum lttng_live_iterator_status
896 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
897 const bt_message
*msg_in
, bt2::ConstMessage::Shared
& msg_out
,
898 uint64_t new_begin_ts
)
900 enum bt_property_availability availability
;
901 const bt_clock_snapshot
*clock_snapshot
;
905 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
906 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
908 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
909 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
911 const auto msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
912 iter
, stream
, new_begin_ts
, end_ts
);
915 return LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
918 bt_message_discarded_events_set_count(msg
, count
);
919 msg_out
= bt2::ConstMessage::Shared::createWithoutRef(msg
);
920 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
923 static enum lttng_live_iterator_status
924 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
925 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
926 const bt2::ConstMessage
& late_msg
)
928 const bt_clock_class
*clock_class
;
929 const bt_stream_class
*stream_class
;
930 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
931 int64_t last_inactivity_ts_ns
;
932 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
933 enum lttng_live_iterator_status adjust_status
;
934 bt2::ConstMessage::Shared adjusted_message
;
937 * The timestamp of the current message is before the last message sent
938 * by this component. We CANNOT send it as is.
940 * The only expected scenario in which that could happen is the
941 * following, everything else is a bug in this component, relay daemon,
944 * Expected scenario: The CTF message iterator emitted discarded
945 * packets and discarded events with synthesized beginning and end
946 * timestamps from the bounds of the last known packet and the newly
947 * decoded packet header. The CTF message iterator is not aware of
948 * stream inactivity beacons. Hence, we have to adjust the beginning
949 * timestamp of those types of messages if a stream signalled its
950 * inactivity up until _after_ the last known packet's beginning
953 * Otherwise, the monotonicity guarantee of message timestamps would
956 * In short, the only scenario in which it's okay and fixable to
957 * received a late message is when:
958 * 1. the late message is a discarded packets or discarded events
960 * 2. this stream produced an inactivity message downstream, and
961 * 3. the timestamp of the late message is within the inactivity
962 * timespan we sent downstream through the inactivity message.
965 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
966 "Handling late message on live stream iterator: "
967 "stream-name=\"{}\", viewer-stream-id={}",
968 stream_iter
->name
, stream_iter
->viewer_stream_id
);
970 if (!stream_iter
->last_inactivity_ts
.is_set
) {
971 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
972 "Invalid live stream state: "
973 "have a late message when no inactivity message "
974 "was ever sent for that stream.");
975 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
979 if (!is_discarded_packet_or_event_message(late_msg
)) {
980 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
981 "Invalid live stream state: "
982 "have a late message that is not a packet discarded or "
983 "event discarded message: late-msg-type={}",
985 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
989 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
->libObjPtr());
990 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
992 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
993 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
994 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
995 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
996 "Error converting last "
997 "inactivity message timestamp to nanoseconds");
998 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1002 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1003 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1004 "Invalid live stream state: "
1005 "have a late message that is none included in a stream "
1006 "inactivity timespan: last-inactivity-ts-ns={}, "
1007 "late-msg-ts-ns={}",
1008 last_inactivity_ts_ns
, late_msg_ts_ns
);
1009 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1014 * We now know that it's okay for this message to be late, we can now
1015 * adjust its timestamp to ensure monotonicity.
1017 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1018 "Adjusting the timestamp of late message: late-msg-type={}, "
1020 late_msg
.type(), stream_iter
->last_inactivity_ts
.value
);
1021 switch (late_msg
.type()) {
1022 case bt2::MessageType::DiscardedEvents
:
1023 adjust_status
= adjust_discarded_events_message(
1024 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
1025 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
1027 case bt2::MessageType::DiscardedPackets
:
1028 adjust_status
= adjust_discarded_packets_message(
1029 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
->libObjPtr(),
1030 late_msg
.libObjPtr(), adjusted_message
, stream_iter
->last_inactivity_ts
.value
);
1036 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1037 stream_iter_status
= adjust_status
;
1041 BT_ASSERT_DBG(adjusted_message
);
1042 stream_iter
->current_msg
= adjusted_message
;
1043 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1046 return stream_iter_status
;
1049 static enum lttng_live_iterator_status
1050 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1051 struct lttng_live_trace
*live_trace
,
1052 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1054 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1055 enum lttng_live_iterator_status stream_iter_status
;
1056 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1057 uint64_t stream_iter_idx
;
1059 BT_ASSERT_DBG(live_trace
);
1061 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1062 "Finding the next stream iterator for trace: "
1066 * Update the current message of every stream iterators of this trace.
1067 * The current msg of every stream must have a timestamp equal or
1068 * larger than the last message returned by this iterator. We must
1069 * ensure monotonicity.
1071 stream_iter_idx
= 0;
1072 while (stream_iter_idx
< live_trace
->stream_iterators
.size()) {
1073 bool stream_iter_is_ended
= false;
1074 lttng_live_stream_iterator
*stream_iter
=
1075 live_trace
->stream_iterators
[stream_iter_idx
].get();
1078 * If there is no current message for this stream, go fetch
1081 while (!stream_iter
->current_msg
) {
1082 bt2::ConstMessage::Shared msg
;
1083 int64_t curr_msg_ts_ns
= INT64_MAX
;
1085 stream_iter_status
=
1086 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, msg
);
1088 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1089 stream_iter_is_ended
= true;
1093 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1099 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1100 "Live stream iterator returned message: msg-type={}, "
1101 "stream-name=\"{}\", viewer-stream-id={}",
1102 msg
->type(), stream_iter
->name
, stream_iter
->viewer_stream_id
);
1105 * Get the timestamp in nanoseconds from origin of this
1108 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
->libObjPtr(),
1109 lttng_live_msg_iter
->last_msg_ts_ns
, &curr_msg_ts_ns
);
1112 * Check if the message of the current live stream
1113 * iterator occurred at the exact same time or after the
1114 * last message returned by this component's message
1115 * iterator. If not, we need to handle it with care.
1117 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1118 stream_iter
->current_msg
= std::move(msg
);
1119 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1122 * We received a message from the past. This
1123 * may be fixable but it can also be an error.
1125 stream_iter_status
=
1126 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, *msg
);
1127 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1128 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1129 "Late message could not be handled correctly: "
1130 "lttng-live-msg-iter-addr={}, "
1131 "stream-name=\"{}\", "
1132 "curr-msg-ts={}, last-msg-ts={}",
1133 fmt::ptr(lttng_live_msg_iter
), stream_iter
->name
,
1135 lttng_live_msg_iter
->last_msg_ts_ns
);
1136 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1142 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1144 if (!stream_iter_is_ended
) {
1145 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1146 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1148 * Update the current best candidate message
1149 * for the stream iterator of this live trace
1150 * to be forwarded downstream.
1152 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1153 youngest_candidate_stream_iter
= stream_iter
;
1154 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1156 * Order the messages in an arbitrary but
1157 * deterministic way.
1159 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1160 int ret
= common_muxing_compare_messages(
1161 stream_iter
->current_msg
->libObjPtr(),
1162 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1165 * The `youngest_candidate_stream_iter->current_msg`
1166 * should go first. Update the next
1167 * iterator and the current timestamp.
1169 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1170 youngest_candidate_stream_iter
= stream_iter
;
1171 } else if (ret
== 0) {
1173 * Unable to pick which one should go
1177 lttng_live_msg_iter
->logger
,
1178 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1179 "stream-iter-addr={}"
1180 "stream-iter-addr={}",
1181 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1188 * The live stream iterator has ended. That
1189 * iterator is removed from the array, but
1190 * there is no need to increment
1191 * stream_iter_idx as
1192 * g_ptr_array_remove_index_fast replaces the
1193 * removed element with the array's last
1196 bt2c::vectorFastRemove(live_trace
->stream_iterators
, stream_iter_idx
);
1200 if (youngest_candidate_stream_iter
) {
1201 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1202 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1205 * The only case where we don't have a candidate for this trace
1206 * is if we reached the end of all the iterators.
1208 BT_ASSERT(live_trace
->stream_iterators
.empty());
1209 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1213 return stream_iter_status
;
1216 static enum lttng_live_iterator_status
1217 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1218 struct lttng_live_session
*session
,
1219 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1221 enum lttng_live_iterator_status stream_iter_status
;
1222 uint64_t trace_idx
= 0;
1223 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1224 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1226 BT_CPPLOGD_SPEC(lttng_live_msg_iter
->logger
,
1227 "Finding the next stream iterator for session: "
1231 * Make sure we are attached to the session and look for new streams
1234 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1235 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1236 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1237 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1241 while (trace_idx
< session
->traces
.size()) {
1242 bool trace_is_ended
= false;
1243 struct lttng_live_stream_iterator
*stream_iter
;
1244 lttng_live_trace
*trace
= session
->traces
[trace_idx
].get();
1246 stream_iter_status
=
1247 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1248 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1250 * All the live stream iterators for this trace are
1251 * ENDed. Remove the trace from this session.
1253 trace_is_ended
= true;
1254 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1258 if (!trace_is_ended
) {
1259 BT_ASSERT_DBG(stream_iter
);
1261 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1262 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1263 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1264 youngest_candidate_stream_iter
= stream_iter
;
1265 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1267 * Order the messages in an arbitrary but
1268 * deterministic way.
1270 int ret
= common_muxing_compare_messages(
1271 stream_iter
->current_msg
->libObjPtr(),
1272 youngest_candidate_stream_iter
->current_msg
->libObjPtr());
1275 * The `youngest_candidate_stream_iter->current_msg`
1276 * should go first. Update the next iterator
1277 * and the current timestamp.
1279 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1280 youngest_candidate_stream_iter
= stream_iter
;
1281 } else if (ret
== 0) {
1282 /* Unable to pick which one should go first. */
1284 lttng_live_msg_iter
->logger
,
1285 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1286 "stream-iter-addr={}"
1287 "youngest-candidate-stream-iter-addr={}",
1288 fmt::ptr(stream_iter
), fmt::ptr(youngest_candidate_stream_iter
));
1294 * trace_idx is not incremented since
1295 * vectorFastRemove replaces the
1296 * element at trace_idx with the array's last element.
1298 bt2c::vectorFastRemove(session
->traces
, trace_idx
);
1301 if (youngest_candidate_stream_iter
) {
1302 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1303 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1306 * The only cases where we don't have a candidate for this
1308 * 1. if we reached the end of all the iterators of all the
1309 * traces of this session,
1310 * 2. if we never had live stream iterator in the first place.
1312 * In either cases, we return END.
1314 BT_ASSERT(session
->traces
.empty());
1315 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1318 return stream_iter_status
;
1321 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1325 for (i
= 0; i
< count
; i
++) {
1326 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1330 bt_message_iterator_class_next_method_status
1331 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1332 uint64_t capacity
, uint64_t *count
)
1335 bt_message_iterator_class_next_method_status status
;
1336 enum lttng_live_viewer_status viewer_status
;
1337 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1338 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1339 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1340 enum lttng_live_iterator_status stream_iter_status
;
1344 BT_ASSERT_DBG(lttng_live_msg_iter
);
1346 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1348 * The iterator was interrupted in a previous call to the
1349 * `_next()` method. We currently do not support generating
1350 * messages after such event. The babeltrace2 CLI should never
1351 * be running the graph after being interrupted. So this check
1352 * is to prevent other graph users from using this live
1353 * iterator in an messed up internal state.
1355 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1356 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1357 lttng_live_msg_iter
->logger
,
1358 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1363 * Clear all the invalid message reference that might be left over in
1366 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1369 * If no session are exposed on the relay found at the url provided by
1370 * the user, session count will be 0. In this case, we return status
1371 * end to return gracefully.
1373 if (lttng_live_msg_iter
->sessions
.empty()) {
1374 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1375 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1379 * The are no more active session for this session
1380 * name. Retry to create a viewer session for the
1381 * requested session name.
1383 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1384 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1385 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1386 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1387 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1388 "Error creating LTTng live viewer session");
1389 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1390 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1399 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1400 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1404 * Here the muxing of message is done.
1406 * We need to iterate over all the streams of all the traces of all the
1407 * viewer sessions in order to get the message with the smallest
1408 * timestamp. In this case, a session is a viewer session and there is
1409 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1410 * kernel). Each viewer session can have multiple traces, for example,
1411 * 64bit UST viewer sessions could have multiple per-pid traces.
1413 * We iterate over the streams of each traces to update and see what is
1414 * their next message's timestamp. From those timestamps, we select the
1415 * message with the smallest timestamp as the best candidate message
1416 * for that trace and do the same thing across all the sessions.
1418 * We then compare the timestamp of best candidate message of all the
1419 * sessions to pick the message with the smallest timestamp and we
1422 while (*count
< capacity
) {
1423 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1424 *candidate_stream_iter
= NULL
;
1425 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1427 uint64_t session_idx
= 0;
1428 while (session_idx
< lttng_live_msg_iter
->sessions
.size()) {
1429 lttng_live_session
*session
= lttng_live_msg_iter
->sessions
[session_idx
].get();
1431 /* Find the best candidate message to send downstream. */
1432 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1433 &candidate_stream_iter
);
1435 /* If we receive an END status, it means that either:
1436 * - Those traces never had active streams (UST with no
1437 * data produced yet),
1438 * - All live stream iterators have ENDed.
1440 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1441 if (session
->closed
&& session
->traces
.empty()) {
1443 * Remove the session from the list.
1444 * session_idx is not modified since
1445 * g_ptr_array_remove_index_fast
1446 * replaces the the removed element with
1447 * the array's last element.
1449 bt2c::vectorFastRemove(lttng_live_msg_iter
->sessions
, session_idx
);
1456 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1460 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1461 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1462 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1463 youngest_stream_iter
= candidate_stream_iter
;
1464 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1466 * The currently selected message to be sent
1467 * downstream next has the exact same timestamp
1468 * that of the current candidate message. We
1469 * must break the tie in a predictable manner.
1471 BT_CPPLOGD_STR_SPEC(
1472 lttng_live_msg_iter
->logger
,
1473 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1475 * Order the messages in an arbitrary but
1476 * deterministic way.
1478 int ret
= common_muxing_compare_messages(
1479 candidate_stream_iter
->current_msg
->libObjPtr(),
1480 youngest_stream_iter
->current_msg
->libObjPtr());
1483 * The `candidate_stream_iter->current_msg`
1484 * should go first. Update the next
1485 * iterator and the current timestamp.
1487 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1488 youngest_stream_iter
= candidate_stream_iter
;
1489 } else if (ret
== 0) {
1490 /* Unable to pick which one should go first. */
1492 lttng_live_msg_iter
->logger
,
1493 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1494 "next-stream-iter-addr={}"
1495 "candidate-stream-iter-addr={}",
1496 fmt::ptr(youngest_stream_iter
), fmt::ptr(candidate_stream_iter
));
1503 if (!youngest_stream_iter
) {
1504 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1508 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1509 /* Ensure monotonicity. */
1510 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1511 youngest_stream_iter
->current_msg_ts_ns
);
1514 * Insert the next message to the message batch. This will set
1515 * stream iterator current message to NULL so that next time
1516 * we fetch the next message of that stream iterator
1518 msgs
[*count
] = youngest_stream_iter
->current_msg
.release().libObjPtr();
1521 /* Update the last timestamp in nanoseconds sent downstream. */
1522 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1523 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1525 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1529 switch (stream_iter_status
) {
1530 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1531 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1533 * If we gathered messages, return _OK even if the graph was
1534 * interrupted. This allows for the components downstream to at
1535 * least get the those messages. If the graph was indeed
1536 * interrupted there should not be another _next() call as the
1537 * application will tear down the graph. This component class
1538 * doesn't support restarting after an interruption.
1541 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1543 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1546 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1547 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1549 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1550 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1551 "Memory error preparing the next batch of messages: "
1552 "live-iter-status={}",
1553 stream_iter_status
);
1554 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1556 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1557 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1558 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1559 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1560 "Error preparing the next batch of messages: "
1561 "live-iter-status={}",
1562 stream_iter_status
);
1564 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1565 /* Put all existing messages on error. */
1566 put_messages(msgs
, *count
);
1574 } catch (const std::bad_alloc
&) {
1575 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1576 } catch (const bt2::Error
&) {
1577 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1581 static struct lttng_live_msg_iter
*
1582 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1583 bt_self_message_iterator
*self_msg_it
)
1585 lttng_live_msg_iter
*msg_iter
= new lttng_live_msg_iter
{lttng_live_comp
->logger
};
1586 msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1587 msg_iter
->lttng_live_comp
= lttng_live_comp
;
1588 msg_iter
->self_msg_iter
= self_msg_it
;
1590 msg_iter
->active_stream_iter
= 0;
1591 msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1592 msg_iter
->was_interrupted
= false;
1597 bt_message_iterator_class_initialize_method_status
1598 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1599 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1602 bt_message_iterator_class_initialize_method_status status
;
1603 struct lttng_live_component
*lttng_live
;
1604 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1605 enum lttng_live_viewer_status viewer_status
;
1606 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1608 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1610 /* There can be only one downstream iterator at the same time. */
1611 BT_ASSERT(!lttng_live
->has_msg_iter
);
1612 lttng_live
->has_msg_iter
= true;
1614 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1615 if (!lttng_live_msg_iter
) {
1616 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live
->logger
,
1617 "Failed to create lttng_live_msg_iter");
1618 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1622 viewer_status
= live_viewer_connection_create(
1623 lttng_live
->params
.url
.c_str(), false, lttng_live_msg_iter
, lttng_live_msg_iter
->logger
,
1624 lttng_live_msg_iter
->viewer_connection
);
1625 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1626 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1627 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1628 "Failed to create viewer connection");
1629 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1631 * Interruption in the _iter_init() method is not
1632 * supported. Return an error.
1634 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1635 "Interrupted while creating viewer connection");
1638 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1642 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1643 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1644 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1645 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1646 "Failed to create viewer session");
1647 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1649 * Interruption in the _iter_init() method is not
1650 * supported. Return an error.
1652 BT_CPPLOGE_APPEND_CAUSE_SPEC(lttng_live_msg_iter
->logger
,
1653 "Interrupted when creating viewer session");
1656 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1660 if (lttng_live_msg_iter
->sessions
.empty()) {
1661 switch (lttng_live
->params
.sess_not_found_act
) {
1662 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1664 lttng_live_msg_iter
->logger
,
1665 "Unable to connect to the requested live viewer session. "
1666 "Keep trying to connect because of {}=\"{}\" component parameter: url=\"{}\"",
1667 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1668 lttng_live
->params
.url
);
1670 case SESSION_NOT_FOUND_ACTION_FAIL
:
1671 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1672 lttng_live_msg_iter
->logger
,
1673 "Unable to connect to the requested live viewer session. "
1674 "Fail the message iterator initialization because of {}=\"{}\" "
1675 "component parameter: url =\"{}\"",
1676 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1677 lttng_live
->params
.url
);
1678 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1680 case SESSION_NOT_FOUND_ACTION_END
:
1681 BT_CPPLOGI_SPEC(lttng_live_msg_iter
->logger
,
1682 "Unable to connect to the requested live viewer session. "
1683 "End gracefully at the first _next() call because of {}=\"{}\""
1684 " component parameter: url=\"{}\"",
1685 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1686 lttng_live
->params
.url
);
1693 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1694 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1698 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1701 } catch (const std::bad_alloc
&) {
1702 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1703 } catch (const bt2::Error
&) {
1704 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1708 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1709 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1710 bt_param_validation_value_descr::makeString()},
1711 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1713 static bt_component_class_query_method_status
1714 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1715 const bt2c::Logger
& logger
)
1717 bt_component_class_query_method_status status
;
1718 const bt_value
*url_value
= NULL
;
1720 live_viewer_connection::UP viewer_connection
;
1721 enum lttng_live_viewer_status viewer_status
;
1722 enum bt_param_validation_status validation_status
;
1723 gchar
*validate_error
= NULL
;
1725 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1726 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1727 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1729 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1730 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1731 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validate_error
);
1735 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1736 url
= bt_value_string_get(url_value
);
1738 viewer_status
= live_viewer_connection_create(url
, true, NULL
, logger
, viewer_connection
);
1739 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1740 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1741 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to create viewer connection");
1742 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1743 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1744 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1751 status
= live_viewer_connection_list_sessions(viewer_connection
.get(), result
);
1752 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1753 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to list viewer sessions");
1760 BT_VALUE_PUT_REF_AND_RESET(*result
);
1763 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1767 g_free(validate_error
);
1772 static bt_component_class_query_method_status
1773 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1774 const bt2c::Logger
& logger
)
1776 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1777 const bt_value
*input_type_value
;
1778 const bt_value
*input_value
;
1780 struct bt_common_lttng_live_url_parts parts
= {};
1782 /* Used by the logging macros */
1783 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1786 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1787 if (!input_type_value
) {
1788 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `type` parameter.");
1792 if (!bt_value_is_string(input_type_value
)) {
1793 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`type` parameter is not a string value.");
1797 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1798 /* We don't handle file system paths */
1802 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1804 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Missing expected `input` parameter.");
1808 if (!bt_value_is_string(input_value
)) {
1809 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`input` parameter is not a string value.");
1813 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1814 if (parts
.session_name
) {
1816 * Looks pretty much like an LTTng live URL: we got the
1817 * session name part, which forms a complete URL.
1823 *result
= bt_value_real_create_init(weight
);
1825 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1833 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1836 BT_ASSERT(!*result
);
1839 bt_common_destroy_lttng_live_url_parts(&parts
);
1843 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1844 bt_private_query_executor
*priv_query_exec
,
1845 const char *object
, const bt_value
*params
,
1846 __attribute__((unused
)) void *method_data
,
1847 const bt_value
**result
)
1850 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1851 bt2c::Logger logger
{bt2::SelfComponentClass
{comp_class
},
1852 bt2::PrivateQueryExecutor
{priv_query_exec
},
1853 "PLUGIN/SRC.CTF.LTTNG-LIVE/QUERY"};
1855 if (strcmp(object
, "sessions") == 0) {
1856 status
= lttng_live_query_list_sessions(params
, result
, logger
);
1857 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1858 status
= lttng_live_query_support_info(params
, result
, logger
);
1860 BT_CPPLOGI_SPEC(logger
, "Unknown query object `{}`", object
);
1861 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1867 } catch (const std::bad_alloc
&) {
1868 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1869 } catch (const bt2::Error
&) {
1870 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1874 void lttng_live_component_finalize(bt_self_component_source
*component
)
1876 lttng_live_component::UP
{static_cast<lttng_live_component
*>(
1877 bt_self_component_get_data(bt_self_component_source_as_self_component(component
)))};
1880 static enum session_not_found_action
1881 parse_session_not_found_action_param(const bt_value
*no_session_param
)
1883 enum session_not_found_action action
;
1884 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1886 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1887 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1888 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1889 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1891 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1892 action
= SESSION_NOT_FOUND_ACTION_END
;
1898 static bt_param_validation_value_descr inputs_elem_descr
=
1899 bt_param_validation_value_descr::makeString();
1901 static const char *sess_not_found_action_choices
[] = {
1902 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1903 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1904 SESS_NOT_FOUND_ACTION_END_STR
,
1907 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1908 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1909 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
1910 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
1911 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
1912 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1914 static bt_component_class_initialize_method_status
1915 lttng_live_component_create(const bt_value
*params
, bt_self_component_source
*self_comp
,
1916 lttng_live_component::UP
& component
)
1918 const bt_value
*inputs_value
;
1919 const bt_value
*url_value
;
1920 const bt_value
*value
;
1921 enum bt_param_validation_status validation_status
;
1922 gchar
*validation_error
= NULL
;
1923 bt2c::Logger logger
{bt2::SelfSourceComponent
{self_comp
}, "PLUGIN/SRC.CTF.LTTNG-LIVE/COMP"};
1925 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
1926 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1927 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1928 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1929 bt2c::GCharUP errorFreer
{validation_error
};
1930 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "{}", validation_error
);
1931 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1934 auto lttng_live
= bt2s::make_unique
<lttng_live_component
>(std::move(logger
));
1935 lttng_live
->self_comp
= bt_self_component_source_as_self_component(self_comp
);
1936 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1937 lttng_live
->has_msg_iter
= false;
1939 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
1940 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
1941 lttng_live
->params
.url
= bt_value_string_get(url_value
);
1943 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
1945 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
1947 BT_CPPLOGI_SPEC(lttng_live
->logger
,
1948 "Optional `{}` parameter is missing: defaulting to `{}`.",
1949 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1950 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1953 component
= std::move(lttng_live
);
1954 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1957 bt_component_class_initialize_method_status
1958 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
1959 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
1962 lttng_live_component::UP lttng_live
;
1963 bt_component_class_initialize_method_status ret
;
1964 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
1965 bt_self_component_add_port_status add_port_status
;
1967 ret
= lttng_live_component_create(params
, self_comp_src
, lttng_live
);
1968 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1973 bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
1974 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1975 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
1979 bt_self_component_set_data(self_comp
, lttng_live
.release());
1981 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1982 } catch (const std::bad_alloc
&) {
1983 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1984 } catch (const bt2::Error
&) {
1985 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;