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
11 #define BT_COMP_LOG_SELF_COMP self_comp
12 #define BT_LOG_OUTPUT_LEVEL log_level
13 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
14 #include "logging/comp-logging.h"
22 #include "common/assert.h"
23 #include <babeltrace2/babeltrace.h>
24 #include "compat/compiler.h"
25 #include <babeltrace2/types.h>
27 #include "plugins/common/muxing/muxing.h"
28 #include "plugins/common/param-validation/param-validation.h"
30 #include "data-stream.h"
32 #include "lttng-live.h"
34 #define MAX_QUERY_SIZE (256*1024)
35 #define URL_PARAM "url"
36 #define INPUTS_PARAM "inputs"
37 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
38 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
39 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
40 #define SESS_NOT_FOUND_ACTION_END_STR "end"
42 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
45 const char *lttng_live_iterator_status_string(
46 enum lttng_live_iterator_status status
)
49 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
50 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
51 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
52 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
53 case LTTNG_LIVE_ITERATOR_STATUS_END
:
54 return "LTTNG_LIVE_ITERATOR_STATUS_END";
55 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
56 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
57 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
58 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
59 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
60 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
61 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
62 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
63 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
64 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
71 const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
74 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
75 return "ACTIVE_NO_DATA";
76 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
77 return "QUIESCENT_NO_DATA";
78 case LTTNG_LIVE_STREAM_QUIESCENT
:
80 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
82 case LTTNG_LIVE_STREAM_EOF
:
89 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
91 BT_COMP_LOGD("Live stream iterator state=%s, last-inact-ts=%" PRId64 \
92 ", curr-inact-ts %" PRId64, \
93 lttng_live_stream_state_string(live_stream_iter->state), \
94 live_stream_iter->last_inactivity_ts, \
95 live_stream_iter->current_inactivity_ts); \
99 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
108 ret
= bt_self_message_iterator_is_interrupted(
109 msg_iter
->self_msg_iter
);
116 struct lttng_live_trace
*lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
,
120 struct lttng_live_trace
*ret_trace
= NULL
;
122 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
123 struct lttng_live_trace
*trace
=
124 g_ptr_array_index(session
->traces
, trace_idx
);
125 if (trace
->id
== trace_id
) {
136 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
138 bt_logging_level log_level
= trace
->log_level
;
139 bt_self_component
*self_comp
= trace
->self_comp
;
141 BT_COMP_LOGD("Destroying live trace: trace-id=%"PRIu64
, trace
->id
);
143 BT_ASSERT(trace
->stream_iterators
);
144 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
146 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
147 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
149 lttng_live_metadata_fini(trace
);
154 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
157 struct lttng_live_trace
*trace
= NULL
;
158 bt_logging_level log_level
= session
->log_level
;
159 bt_self_component
*self_comp
= session
->self_comp
;
161 BT_COMP_LOGD("Creating live trace: "
162 "session-id=%"PRIu64
", trace-id=%"PRIu64
,
163 session
->id
, trace_id
);
164 trace
= g_new0(struct lttng_live_trace
, 1);
166 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
167 "Failed to allocate live trace");
170 trace
->log_level
= session
->log_level
;
171 trace
->self_comp
= session
->self_comp
;
172 trace
->session
= session
;
173 trace
->id
= trace_id
;
174 trace
->trace_class
= NULL
;
176 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
177 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
178 BT_ASSERT(trace
->stream_iterators
);
179 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
180 g_ptr_array_add(session
->traces
, trace
);
191 struct lttng_live_trace
*lttng_live_session_borrow_or_create_trace_by_id(
192 struct lttng_live_session
*session
, uint64_t trace_id
)
194 struct lttng_live_trace
*trace
;
196 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
201 /* The session is the owner of the newly created trace. */
202 trace
= lttng_live_create_trace(session
, trace_id
);
209 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
210 uint64_t session_id
, const char *hostname
,
211 const char *session_name
)
214 struct lttng_live_session
*session
;
215 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
216 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
218 BT_COMP_LOGD("Adding live session: "
219 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
220 session_id
, hostname
, session_name
);
222 session
= g_new0(struct lttng_live_session
, 1);
224 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
225 "Failed to allocate live session");
229 session
->log_level
= lttng_live_msg_iter
->log_level
;
230 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
231 session
->id
= session_id
;
232 session
->traces
= g_ptr_array_new_with_free_func(
233 (GDestroyNotify
) lttng_live_destroy_trace
);
234 BT_ASSERT(session
->traces
);
235 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
236 session
->new_streams_needed
= true;
237 session
->hostname
= g_string_new(hostname
);
238 BT_ASSERT(session
->hostname
);
240 session
->session_name
= g_string_new(session_name
);
241 BT_ASSERT(session
->session_name
);
243 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
253 void lttng_live_destroy_session(struct lttng_live_session
*session
)
255 bt_logging_level log_level
;
256 bt_self_component
*self_comp
;
262 log_level
= session
->log_level
;
263 self_comp
= session
->self_comp
;
264 BT_COMP_LOGD("Destroying live session: "
265 "session-id=%"PRIu64
", session-name=\"%s\"",
266 session
->id
, session
->session_name
->str
);
267 if (session
->id
!= -1ULL) {
268 if (lttng_live_session_detach(session
)) {
269 if (!lttng_live_graph_is_canceled(
270 session
->lttng_live_msg_iter
)) {
271 /* Old relayd cannot detach sessions. */
272 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
279 if (session
->traces
) {
280 g_ptr_array_free(session
->traces
, TRUE
);
283 if (session
->hostname
) {
284 g_string_free(session
->hostname
, TRUE
);
286 if (session
->session_name
) {
287 g_string_free(session
->session_name
, TRUE
);
296 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
298 if (!lttng_live_msg_iter
) {
302 if (lttng_live_msg_iter
->sessions
) {
303 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
306 if (lttng_live_msg_iter
->viewer_connection
) {
307 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
309 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
310 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
312 /* All stream iterators must be destroyed at this point. */
313 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
314 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
316 g_free(lttng_live_msg_iter
);
323 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
325 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
327 BT_ASSERT(self_msg_iter
);
329 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
330 BT_ASSERT(lttng_live_msg_iter
);
331 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
335 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
336 struct lttng_live_stream_iterator
*lttng_live_stream
)
338 bt_logging_level log_level
= lttng_live_stream
->log_level
;
339 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
341 switch (lttng_live_stream
->state
) {
342 case LTTNG_LIVE_STREAM_QUIESCENT
:
343 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
345 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
347 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
349 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
351 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
353 case LTTNG_LIVE_STREAM_EOF
:
356 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
360 * For active no data stream, fetch next index. As a result of that it can
362 * - quiescent: won't have events for a bit,
363 * - have data: need to get that data and produce the event,
364 * - have no data on this stream at this point: need to retry (AGAIN) or return
368 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
369 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
370 struct lttng_live_stream_iterator
*lttng_live_stream
)
372 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
373 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
374 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
375 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
376 struct packet_index index
;
378 if (lttng_live_stream
->trace
->metadata_stream_state
==
379 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
380 BT_COMP_LOGD("Need to get an update for the metadata stream before proceeding further with this stream: "
381 "stream-name=\"%s\"", lttng_live_stream
->name
->str
);
382 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
386 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
387 BT_COMP_LOGD("Need to get an update of all streams before proceeding further with this stream: "
388 "stream-name=\"%s\"", lttng_live_stream
->name
->str
);
389 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
393 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
394 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
397 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
399 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
403 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
405 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
406 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
407 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
409 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
410 last_inact_ts
== curr_inact_ts
) {
411 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
412 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
414 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
419 lttng_live_stream
->base_offset
= index
.offset
;
420 lttng_live_stream
->offset
= index
.offset
;
421 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
423 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
424 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
425 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
426 lttng_live_stream
->name
->str
,
427 lttng_live_stream
->viewer_stream_id
,
428 lttng_live_stream
->base_offset
,
429 lttng_live_stream
->offset
, lttng_live_stream
->len
);
432 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
433 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
439 * Creation of the message requires the ctf trace class to be created
440 * beforehand, but the live protocol gives us all streams (including metadata)
441 * at once. So we split it in three steps: getting streams, getting metadata
442 * (which creates the ctf trace class), and then creating the per-stream
446 enum lttng_live_iterator_status
lttng_live_get_session(
447 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
448 struct lttng_live_session
*session
)
450 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
451 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
452 enum lttng_live_iterator_status status
;
455 if (!session
->attached
) {
456 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
,
458 enum lttng_live_viewer_status attach_status
=
459 lttng_live_session_attach(session
,
460 lttng_live_msg_iter
->self_msg_iter
);
461 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
462 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
464 * Clear any causes appended in
465 * `lttng_live_attach_session()` as we want to
466 * return gracefully since the graph was
469 bt_current_thread_clear_error();
470 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
472 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
473 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
474 "Error attaching to LTTng live session");
480 BT_COMP_LOGD("Updating all streams and metadata for session: "
481 "session-id=%" PRIu64
", session-name=\"%s\"",
482 session
->id
, session
->session_name
->str
);
484 status
= lttng_live_session_get_new_streams(session
,
485 lttng_live_msg_iter
->self_msg_iter
);
486 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
487 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
492 while (trace_idx
< session
->traces
->len
) {
493 struct lttng_live_trace
*trace
=
494 g_ptr_array_index(session
->traces
, trace_idx
);
496 status
= lttng_live_metadata_update(trace
);
498 case LTTNG_LIVE_ITERATOR_STATUS_END
:
499 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
502 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
503 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
506 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
507 "Error updating trace metadata: "
508 "stream-iter-status=%s, trace-id=%"PRIu64
,
509 lttng_live_iterator_status_string(status
),
516 * Now that we have the metadata we can initialize the downstream
519 status
= lttng_live_lazy_msg_init(session
,
520 lttng_live_msg_iter
->self_msg_iter
);
527 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
529 uint64_t session_idx
, trace_idx
;
530 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
531 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
533 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
535 struct lttng_live_session
*session
=
536 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
537 BT_COMP_LOGD("Force marking session as needing new streams: "
538 "session-id=%" PRIu64
, session
->id
);
539 session
->new_streams_needed
= true;
540 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
542 struct lttng_live_trace
*trace
=
543 g_ptr_array_index(session
->traces
, trace_idx
);
544 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
545 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
546 session
->id
, trace
->id
);
548 BT_ASSERT(trace
->metadata_stream_state
!=
549 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
551 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
557 enum lttng_live_iterator_status
558 lttng_live_iterator_handle_new_streams_and_metadata(
559 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
561 enum lttng_live_iterator_status status
;
562 enum lttng_live_viewer_status viewer_status
;
563 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
564 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
565 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
566 struct lttng_live_session
*session
;
567 enum session_not_found_action sess_not_found_act
=
568 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
570 BT_COMP_LOGD("Update data and metadata of all sessions: "
571 "live-msg-iter-addr=%p", lttng_live_msg_iter
);
573 * In a remotely distant future, we could add a "new
574 * session" flag to the protocol, which would tell us that we
575 * need to query for new sessions even though we have sessions
578 if (lttng_live_msg_iter
->sessions
->len
== 0) {
579 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
580 BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter");
581 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
584 BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
586 * Retry to create a viewer session for the requested
589 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
590 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
591 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
592 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
593 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
594 "Error creating LTTng live viewer session");
595 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
596 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
605 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
607 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
609 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
611 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
613 case LTTNG_LIVE_ITERATOR_STATUS_END
:
614 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
619 if (!session
->closed
) {
620 nr_sessions_opened
++;
624 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
625 nr_sessions_opened
== 0) {
626 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
628 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
636 enum lttng_live_iterator_status
emit_inactivity_message(
637 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
638 struct lttng_live_stream_iterator
*stream_iter
,
639 const bt_message
**message
, uint64_t timestamp
)
641 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
642 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
643 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
644 bt_message
*msg
= NULL
;
646 BT_ASSERT(stream_iter
->trace
->clock_class
);
648 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
649 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
650 stream_iter
->ctf_stream_class_id
,
651 stream_iter
->viewer_stream_id
, timestamp
);
653 msg
= bt_message_message_iterator_inactivity_create(
654 lttng_live_msg_iter
->self_msg_iter
,
655 stream_iter
->trace
->clock_class
, timestamp
);
657 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
658 "Error emitting message iterator inactivity message");
667 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
668 bt_message_put_ref(msg
);
673 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
674 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
675 struct lttng_live_stream_iterator
*lttng_live_stream
,
676 const bt_message
**message
)
678 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
680 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
681 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
685 * Check if we already sent an inactivty message downstream for this
686 * `current_inactivity_ts` value.
688 if (lttng_live_stream
->current_inactivity_ts
==
689 lttng_live_stream
->last_inactivity_ts
) {
690 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
691 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
695 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
696 message
, lttng_live_stream
->current_inactivity_ts
);
698 lttng_live_stream
->last_inactivity_ts
=
699 lttng_live_stream
->current_inactivity_ts
;
705 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
706 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
707 const bt_message
*msg
, int64_t last_msg_ts_ns
,
710 const bt_clock_class
*clock_class
= NULL
;
711 const bt_clock_snapshot
*clock_snapshot
= NULL
;
713 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
714 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
717 BT_ASSERT_DBG(ts_ns
);
719 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
720 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
723 switch (bt_message_get_type(msg
)) {
724 case BT_MESSAGE_TYPE_EVENT
:
725 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
727 BT_ASSERT_DBG(clock_class
);
729 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
732 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
733 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
735 BT_ASSERT(clock_class
);
737 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
740 case BT_MESSAGE_TYPE_PACKET_END
:
741 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
743 BT_ASSERT(clock_class
);
745 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
748 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
749 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
751 BT_ASSERT(clock_class
);
753 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
756 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
757 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
759 BT_ASSERT(clock_class
);
761 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
764 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
765 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
769 /* All the other messages have a higher priority */
770 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
771 *ts_ns
= last_msg_ts_ns
;
775 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
776 BT_ASSERT_DBG(clock_class
);
778 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
780 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
781 "Cannot get nanoseconds from Epoch of clock snapshot: "
782 "clock-snapshot-addr=%p", clock_snapshot
);
793 BT_COMP_LOGD("Found message's timestamp: "
794 "iter-data-addr=%p, msg-addr=%p, "
795 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
796 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
803 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
804 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
805 struct lttng_live_stream_iterator
*lttng_live_stream
,
806 const bt_message
**message
)
808 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
809 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
810 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
811 enum ctf_msg_iter_status status
;
812 uint64_t session_idx
, trace_idx
;
814 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
816 struct lttng_live_session
*session
=
817 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
819 if (session
->new_streams_needed
) {
820 BT_COMP_LOGD("Need an update for streams: "
821 "session-id=%" PRIu64
, session
->id
);
822 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
825 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
827 struct lttng_live_trace
*trace
=
828 g_ptr_array_index(session
->traces
, trace_idx
);
829 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
830 BT_COMP_LOGD("Need an update for metadata stream: "
831 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
832 session
->id
, trace
->id
);
833 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
839 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
840 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
841 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
842 "Invalid state of live stream iterator"
843 "stream-iter-status=%s",
844 lttng_live_stream_state_string(lttng_live_stream
->state
));
848 status
= ctf_msg_iter_get_next_message(
849 lttng_live_stream
->msg_iter
, message
);
851 case CTF_MSG_ITER_STATUS_EOF
:
852 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
854 case CTF_MSG_ITER_STATUS_OK
:
855 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
857 case CTF_MSG_ITER_STATUS_AGAIN
:
859 * Continue immediately (end of packet). The next
860 * get_index may return AGAIN to delay the following
863 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
865 case CTF_MSG_ITER_STATUS_ERROR
:
867 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
868 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
869 "CTF message iterator failed to get next message: "
870 "msg-iter=%p, msg-iter-status=%s",
871 lttng_live_stream
->msg_iter
,
872 ctf_msg_iter_status_string(status
));
881 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
882 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
883 struct lttng_live_stream_iterator
*stream_iter
,
884 const bt_message
**curr_msg
)
886 enum lttng_live_iterator_status live_status
=
887 LTTNG_LIVE_ITERATOR_STATUS_OK
;
888 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
889 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
891 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
892 "viewer-stream-id=%" PRIu64
, stream_iter
->name
->str
,
893 stream_iter
->viewer_stream_id
);
896 * The viewer has hung up on us so we are closing the stream. The
897 * `ctf_msg_iter` should simply realize that it needs to close the
898 * stream properly by emitting the necessary stream end message.
900 enum ctf_msg_iter_status status
= ctf_msg_iter_get_next_message(
901 stream_iter
->msg_iter
, curr_msg
);
903 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
904 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
905 "Error getting the next message from CTF message iterator");
906 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
908 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
909 BT_COMP_LOGI("Reached the end of the live stream iterator.");
910 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
914 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
922 * handle_no_data_streams()
924 * - for each ACTIVE_NO_DATA stream:
925 * - query relayd for stream data, or quiescence info.
926 * - if need metadata, get metadata, goto retry.
927 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
928 * - if quiescent, move to QUIESCENT streams
929 * - if fetched data, move to ACTIVE_DATA streams
930 * (at this point each stream either has data, or is quiescent)
934 * handle_new_streams_and_metadata()
935 * - query relayd for known streams, add them as ACTIVE_NO_DATA
936 * - query relayd for metadata
938 * call handle_active_no_data_streams()
940 * handle_quiescent_streams()
941 * - if at least one stream is ACTIVE_DATA:
942 * - peek stream event with lowest timestamp -> next_ts
943 * - for each quiescent stream
944 * - if next_ts >= quiescent end
945 * - set state to ACTIVE_NO_DATA
947 * - for each quiescent stream
948 * - set state to ACTIVE_NO_DATA
950 * call handle_active_no_data_streams()
952 * handle_active_data_streams()
953 * - if at least one stream is ACTIVE_DATA:
954 * - get stream event with lowest timestamp from heap
955 * - make that stream event the current message.
956 * - move this stream heap position to its next event
957 * - if we need to fetch data from relayd, move
958 * stream to ACTIVE_NO_DATA.
962 * end criterion: ctrl-c on client. If relayd exits or the session
963 * closes on the relay daemon side, we keep on waiting for streams.
964 * Eventually handle --end timestamp (also an end criterion).
966 * When disconnected from relayd: try to re-connect endlessly.
969 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
970 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
971 struct lttng_live_stream_iterator
*stream_iter
,
972 const bt_message
**curr_msg
)
974 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
975 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
976 enum lttng_live_iterator_status live_status
;
978 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
979 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
980 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
982 if (stream_iter
->has_stream_hung_up
) {
984 * The stream has hung up and the stream was properly closed
985 * during the last call to the current function. Return _END
986 * status now so that this stream iterator is removed for the
987 * stream iterator list.
989 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
994 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
997 * Make sure we have the most recent metadata and possibly some new
1000 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
1001 lttng_live_msg_iter
);
1002 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1006 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
1007 lttng_live_msg_iter
, stream_iter
);
1008 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1009 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1011 * We overwrite `live_status` since `curr_msg` is
1012 * likely set to a valid message in this function.
1014 live_status
= lttng_live_iterator_close_stream(
1015 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1020 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
1021 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1022 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1023 BT_ASSERT(!*curr_msg
);
1029 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
1030 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1031 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1032 BT_ASSERT(!*curr_msg
);
1036 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1037 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1041 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s"
1042 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1043 lttng_live_iterator_status_string(live_status
),
1044 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1050 bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1052 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1054 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1055 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1059 enum lttng_live_iterator_status
adjust_discarded_packets_message(
1060 bt_self_message_iterator
*iter
,
1061 const bt_stream
*stream
,
1062 const bt_message
*msg_in
, bt_message
**msg_out
,
1063 uint64_t new_begin_ts
)
1065 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1066 enum bt_property_availability availability
;
1067 const bt_clock_snapshot
*clock_snapshot
;
1071 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1072 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1074 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1075 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1077 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1078 iter
, stream
, new_begin_ts
, end_ts
);
1080 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1084 bt_message_discarded_packets_set_count(*msg_out
, count
);
1090 enum lttng_live_iterator_status
adjust_discarded_events_message(
1091 bt_self_message_iterator
*iter
,
1092 const bt_stream
*stream
,
1093 const bt_message
*msg_in
, bt_message
**msg_out
,
1094 uint64_t new_begin_ts
)
1096 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1097 enum bt_property_availability availability
;
1098 const bt_clock_snapshot
*clock_snapshot
;
1102 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1103 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1105 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1106 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1108 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1109 iter
, stream
, new_begin_ts
, end_ts
);
1111 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1115 bt_message_discarded_events_set_count(*msg_out
, count
);
1121 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
1122 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1123 struct lttng_live_trace
*live_trace
,
1124 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1126 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1127 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1128 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1129 enum lttng_live_iterator_status stream_iter_status
;;
1130 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1131 uint64_t stream_iter_idx
;
1133 BT_ASSERT_DBG(live_trace
);
1134 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1136 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1137 "trace-id=%"PRIu64
, live_trace
->id
);
1139 * Update the current message of every stream iterators of this trace.
1140 * The current msg of every stream must have a timestamp equal or
1141 * larger than the last message returned by this iterator. We must
1142 * ensure monotonicity.
1144 stream_iter_idx
= 0;
1145 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1146 bool stream_iter_is_ended
= false;
1147 struct lttng_live_stream_iterator
*stream_iter
=
1148 g_ptr_array_index(live_trace
->stream_iterators
,
1152 * If there is no current message for this stream, go fetch
1155 while (!stream_iter
->current_msg
) {
1156 const bt_message
*msg
= NULL
;
1157 int64_t curr_msg_ts_ns
= INT64_MAX
;
1159 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1160 lttng_live_msg_iter
, stream_iter
, &msg
);
1162 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1163 stream_iter_is_ended
= true;
1167 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1173 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s"
1174 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1175 bt_common_message_type_string(bt_message_get_type(msg
)),
1176 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1179 * Get the timestamp in nanoseconds from origin of this
1182 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1183 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1187 * Check if the message of the current live stream
1188 * iterator occurred at the exact same time or after the
1189 * last message returned by this component's message
1190 * iterator. If not, we return an error.
1192 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1193 stream_iter
->current_msg
= msg
;
1194 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1196 const bt_clock_class
*clock_class
;
1197 const bt_stream_class
*stream_class
;
1198 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1199 int64_t last_inactivity_ts_ns
;
1201 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1202 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1204 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1206 stream_iter
->last_inactivity_ts
,
1207 &last_inactivity_ts_ns
);
1208 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1209 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1213 if (last_inactivity_ts_ns
> curr_msg_ts_ns
&&
1214 is_discarded_packet_or_event_message(msg
)) {
1216 * The CTF message iterator emits Discarded
1217 * Packets and Events with synthesized begin and
1218 * end timestamps from the bounds of the last
1219 * known packet and the newly decoded packet
1222 * The CTF message iterator is not aware of
1223 * stream inactivity beacons. Hence, we have
1224 * to adjust the begin timestamp of those types
1225 * of messages if a stream signaled its
1226 * inactivity up until _after_ the last known
1227 * packet's begin timestamp.
1229 * Otherwise, the monotonicity guarantee would
1232 const enum bt_message_type msg_type
=
1233 bt_message_get_type(msg
);
1234 enum lttng_live_iterator_status adjust_status
=
1235 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1236 bt_message
*adjusted_message
;
1239 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1240 adjust_status
= adjust_discarded_events_message(
1241 lttng_live_msg_iter
->self_msg_iter
,
1242 stream_iter
->stream
,
1243 msg
, &adjusted_message
,
1244 stream_iter
->last_inactivity_ts
);
1246 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1247 adjust_status
= adjust_discarded_packets_message(
1248 lttng_live_msg_iter
->self_msg_iter
,
1249 stream_iter
->stream
,
1250 msg
, &adjusted_message
,
1251 stream_iter
->last_inactivity_ts
);
1257 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1258 stream_iter_status
= adjust_status
;
1262 BT_ASSERT_DBG(adjusted_message
);
1263 stream_iter
->current_msg
= adjusted_message
;
1264 stream_iter
->current_msg_ts_ns
=
1265 last_inactivity_ts_ns
;
1268 * We received a message in the past. To ensure
1269 * monotonicity, we can't send it forward.
1271 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1272 "Message's timestamp is less than "
1273 "lttng-live's message iterator's last "
1274 "returned timestamp: "
1275 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1276 "last-msg-ts=%" PRId64
,
1277 lttng_live_msg_iter
, curr_msg_ts_ns
,
1278 lttng_live_msg_iter
->last_msg_ts_ns
);
1279 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1285 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1287 if (!stream_iter_is_ended
) {
1288 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1289 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1291 * Update the current best candidate message
1292 * for the stream iterator of this live trace
1293 * to be forwarded downstream.
1295 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1296 youngest_candidate_stream_iter
= stream_iter
;
1297 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1299 * Order the messages in an arbitrary but
1300 * deterministic way.
1302 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1303 int ret
= common_muxing_compare_messages(
1304 stream_iter
->current_msg
,
1305 youngest_candidate_stream_iter
->current_msg
);
1308 * The `youngest_candidate_stream_iter->current_msg`
1309 * should go first. Update the next
1310 * iterator and the current timestamp.
1312 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1313 youngest_candidate_stream_iter
= stream_iter
;
1314 } else if (ret
== 0) {
1316 * Unable to pick which one should go
1319 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1320 "stream-iter-addr=%p"
1321 "stream-iter-addr=%p",
1323 youngest_candidate_stream_iter
);
1330 * The live stream iterator has ended. That
1331 * iterator is removed from the array, but
1332 * there is no need to increment
1333 * stream_iter_idx as
1334 * g_ptr_array_remove_index_fast replaces the
1335 * removed element with the array's last
1338 g_ptr_array_remove_index_fast(
1339 live_trace
->stream_iterators
,
1344 if (youngest_candidate_stream_iter
) {
1345 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1346 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1349 * The only case where we don't have a candidate for this trace
1350 * is if we reached the end of all the iterators.
1352 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1353 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1357 return stream_iter_status
;
1361 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1362 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1363 struct lttng_live_session
*session
,
1364 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1366 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1367 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1368 enum lttng_live_iterator_status stream_iter_status
;
1369 uint64_t trace_idx
= 0;
1370 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1371 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1373 BT_COMP_LOGD("Finding the next stream iterator for session: "
1374 "session-id=%"PRIu64
, session
->id
);
1376 * Make sure we are attached to the session and look for new streams
1379 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1380 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1381 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1382 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1386 BT_ASSERT_DBG(session
->traces
);
1388 while (trace_idx
< session
->traces
->len
) {
1389 bool trace_is_ended
= false;
1390 struct lttng_live_stream_iterator
*stream_iter
;
1391 struct lttng_live_trace
*trace
=
1392 g_ptr_array_index(session
->traces
, trace_idx
);
1394 stream_iter_status
= next_stream_iterator_for_trace(
1395 lttng_live_msg_iter
, trace
, &stream_iter
);
1396 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1398 * All the live stream iterators for this trace are
1399 * ENDed. Remove the trace from this session.
1401 trace_is_ended
= true;
1402 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1406 if (!trace_is_ended
) {
1407 BT_ASSERT_DBG(stream_iter
);
1409 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1410 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1411 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1412 youngest_candidate_stream_iter
= stream_iter
;
1413 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1415 * Order the messages in an arbitrary but
1416 * deterministic way.
1418 int ret
= common_muxing_compare_messages(
1419 stream_iter
->current_msg
,
1420 youngest_candidate_stream_iter
->current_msg
);
1423 * The `youngest_candidate_stream_iter->current_msg`
1424 * should go first. Update the next iterator
1425 * and the current timestamp.
1427 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1428 youngest_candidate_stream_iter
= stream_iter
;
1429 } else if (ret
== 0) {
1430 /* Unable to pick which one should go first. */
1431 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1432 "stream-iter-addr=%p" "stream-iter-addr=%p",
1433 stream_iter
, youngest_candidate_stream_iter
);
1439 * trace_idx is not incremented since
1440 * g_ptr_array_remove_index_fast replaces the
1441 * element at trace_idx with the array's last element.
1443 g_ptr_array_remove_index_fast(session
->traces
,
1447 if (youngest_candidate_stream_iter
) {
1448 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1449 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1452 * The only cases where we don't have a candidate for this
1454 * 1. if we reached the end of all the iterators of all the
1455 * traces of this session,
1456 * 2. if we never had live stream iterator in the first place.
1458 * In either cases, we return END.
1460 BT_ASSERT(session
->traces
->len
== 0);
1461 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1464 return stream_iter_status
;
1468 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1472 for (i
= 0; i
< count
; i
++) {
1473 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1478 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
1479 bt_self_message_iterator
*self_msg_it
,
1480 bt_message_array_const msgs
, uint64_t capacity
,
1483 bt_message_iterator_class_next_method_status status
;
1484 enum lttng_live_viewer_status viewer_status
;
1485 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1486 bt_self_message_iterator_get_data(self_msg_it
);
1487 struct lttng_live_component
*lttng_live
=
1488 lttng_live_msg_iter
->lttng_live_comp
;
1489 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1490 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1491 enum lttng_live_iterator_status stream_iter_status
;
1492 uint64_t session_idx
;
1496 BT_ASSERT_DBG(lttng_live_msg_iter
);
1498 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1500 * The iterator was interrupted in a previous call to the
1501 * `_next()` method. We currently do not support generating
1502 * messages after such event. The babeltrace2 CLI should never
1503 * be running the graph after being interrupted. So this check
1504 * is to prevent other graph users from using this live
1505 * iterator in an messed up internal state.
1507 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1508 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1509 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1514 * Clear all the invalid message reference that might be left over in
1517 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1520 * If no session are exposed on the relay found at the url provided by
1521 * the user, session count will be 0. In this case, we return status
1522 * end to return gracefully.
1524 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1525 if (lttng_live
->params
.sess_not_found_act
!=
1526 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1527 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1531 * The are no more active session for this session
1532 * name. Retry to create a viewer session for the
1533 * requested session name.
1535 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1536 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1537 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1538 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1539 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1540 "Error creating LTTng live viewer session");
1541 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1542 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1551 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1552 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1556 * Here the muxing of message is done.
1558 * We need to iterate over all the streams of all the traces of all the
1559 * viewer sessions in order to get the message with the smallest
1560 * timestamp. In this case, a session is a viewer session and there is
1561 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1562 * kernel). Each viewer session can have multiple traces, for example,
1563 * 64bit UST viewer sessions could have multiple per-pid traces.
1565 * We iterate over the streams of each traces to update and see what is
1566 * their next message's timestamp. From those timestamps, we select the
1567 * message with the smallest timestamp as the best candidate message
1568 * for that trace and do the same thing across all the sessions.
1570 * We then compare the timestamp of best candidate message of all the
1571 * sessions to pick the message with the smallest timestamp and we
1574 while (*count
< capacity
) {
1575 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1576 *candidate_stream_iter
= NULL
;
1577 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1579 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1581 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1582 struct lttng_live_session
*session
=
1583 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1586 /* Find the best candidate message to send downstream. */
1587 stream_iter_status
= next_stream_iterator_for_session(
1588 lttng_live_msg_iter
, session
,
1589 &candidate_stream_iter
);
1591 /* If we receive an END status, it means that either:
1592 * - Those traces never had active streams (UST with no
1593 * data produced yet),
1594 * - All live stream iterators have ENDed.*/
1595 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1596 if (session
->closed
&& session
->traces
->len
== 0) {
1598 * Remove the session from the list.
1599 * session_idx is not modified since
1600 * g_ptr_array_remove_index_fast
1601 * replaces the the removed element with
1602 * the array's last element.
1604 g_ptr_array_remove_index_fast(
1605 lttng_live_msg_iter
->sessions
,
1613 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1617 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1618 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1619 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1620 youngest_stream_iter
= candidate_stream_iter
;
1621 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1623 * The currently selected message to be sent
1624 * downstream next has the exact same timestamp
1625 * that of the current candidate message. We
1626 * must break the tie in a predictable manner.
1628 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1630 * Order the messages in an arbitrary but
1631 * deterministic way.
1633 int ret
= common_muxing_compare_messages(
1634 candidate_stream_iter
->current_msg
,
1635 youngest_stream_iter
->current_msg
);
1638 * The `candidate_stream_iter->current_msg`
1639 * should go first. Update the next
1640 * iterator and the current timestamp.
1642 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1643 youngest_stream_iter
= candidate_stream_iter
;
1644 } else if (ret
== 0) {
1645 /* Unable to pick which one should go first. */
1646 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1647 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1648 youngest_stream_iter
, candidate_stream_iter
);
1655 if (!youngest_stream_iter
) {
1656 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1660 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1661 /* Ensure monotonicity. */
1662 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1663 youngest_stream_iter
->current_msg_ts_ns
);
1666 * Insert the next message to the message batch. This will set
1667 * stream iterator current messsage to NULL so that next time
1668 * we fetch the next message of that stream iterator
1670 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1673 /* Update the last timestamp in nanoseconds sent downstream. */
1674 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1675 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1677 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1681 switch (stream_iter_status
) {
1682 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1683 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1685 * If we gathered messages, return _OK even if the graph was
1686 * interrupted. This allows for the components downstream to at
1687 * least get the thoses messages. If the graph was indeed
1688 * interrupted there should not be another _next() call as the
1689 * application will tear down the graph. This component class
1690 * doesn't support restarting after an interruption.
1693 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1695 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1698 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1699 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1701 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1702 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1703 "Memory error preparing the next batch of messages: "
1704 "live-iter-status=%s",
1705 lttng_live_iterator_status_string(stream_iter_status
));
1706 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1708 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1709 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1710 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1711 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1712 "Error preparing the next batch of messages: "
1713 "live-iter-status=%s",
1714 lttng_live_iterator_status_string(stream_iter_status
));
1716 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1717 /* Put all existing messages on error. */
1718 put_messages(msgs
, *count
);
1729 struct lttng_live_msg_iter
*lttng_live_msg_iter_create(
1730 struct lttng_live_component
*lttng_live_comp
,
1731 bt_self_message_iterator
*self_msg_it
)
1733 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1734 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1736 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1737 g_new0(struct lttng_live_msg_iter
, 1);
1738 if (!lttng_live_msg_iter
) {
1739 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1740 "Failed to allocate lttng_live_msg_iter");
1744 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1745 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1746 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1747 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1749 lttng_live_msg_iter
->active_stream_iter
= 0;
1750 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1751 lttng_live_msg_iter
->was_interrupted
= false;
1753 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1754 (GDestroyNotify
) lttng_live_destroy_session
);
1755 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1758 return lttng_live_msg_iter
;
1763 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
1764 bt_self_message_iterator
*self_msg_it
,
1765 bt_self_message_iterator_configuration
*config
,
1766 bt_self_component_port_output
*self_port
)
1768 bt_message_iterator_class_initialize_method_status status
;
1769 struct lttng_live_component
*lttng_live
;
1770 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1771 enum lttng_live_viewer_status viewer_status
;
1772 bt_logging_level log_level
;
1773 bt_self_component
*self_comp
=
1774 bt_self_message_iterator_borrow_component(self_msg_it
);
1776 lttng_live
= bt_self_component_get_data(self_comp
);
1777 log_level
= lttng_live
->log_level
;
1778 self_comp
= lttng_live
->self_comp
;
1781 /* There can be only one downstream iterator at the same time. */
1782 BT_ASSERT(!lttng_live
->has_msg_iter
);
1783 lttng_live
->has_msg_iter
= true;
1785 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
,
1787 if (!lttng_live_msg_iter
) {
1788 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1789 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1790 "Failed to create lttng_live_msg_iter");
1794 viewer_status
= live_viewer_connection_create(self_comp
, NULL
,
1795 log_level
, lttng_live
->params
.url
->str
, false,
1796 lttng_live_msg_iter
, <tng_live_msg_iter
->viewer_connection
);
1797 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1798 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1799 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1800 "Failed to create viewer connection");
1801 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1803 * Interruption in the _iter_init() method is not
1804 * supported. Return an error.
1806 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1807 "Interrupted while creating viewer connection");
1812 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1813 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1814 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1815 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1816 "Failed to create viewer session");
1817 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1819 * Interruption in the _iter_init() method is not
1820 * supported. Return an error.
1822 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1823 "Interrupted when creating viewer session");
1828 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1829 switch (lttng_live
->params
.sess_not_found_act
) {
1830 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1831 BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of "
1832 "%s=\"%s\" component parameter: url=\"%s\"",
1833 SESS_NOT_FOUND_ACTION_PARAM
,
1834 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1835 lttng_live
->params
.url
->str
);
1837 case SESSION_NOT_FOUND_ACTION_FAIL
:
1838 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1839 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1840 "component parameter: url =\"%s\"",
1841 SESS_NOT_FOUND_ACTION_PARAM
,
1842 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1843 lttng_live
->params
.url
->str
);
1845 case SESSION_NOT_FOUND_ACTION_END
:
1846 BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1847 "call because of %s=\"%s\" component parameter: "
1848 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1849 SESS_NOT_FOUND_ACTION_END_STR
,
1850 lttng_live
->params
.url
->str
);
1857 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1858 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1862 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1863 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1868 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1869 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1870 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1874 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1875 const bt_value
*params
, const bt_value
**result
,
1876 bt_self_component_class
*self_comp_class
,
1877 bt_logging_level log_level
)
1879 bt_component_class_query_method_status status
;
1880 const bt_value
*url_value
= NULL
;
1882 struct live_viewer_connection
*viewer_connection
= NULL
;
1883 enum lttng_live_viewer_status viewer_status
;
1884 enum bt_param_validation_status validation_status
;
1885 gchar
*validate_error
= NULL
;
1887 validation_status
= bt_param_validation_validate(params
,
1888 list_sessions_params
, &validate_error
);
1889 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1890 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1892 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1893 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1894 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1899 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1900 url
= bt_value_string_get(url_value
);
1902 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
,
1903 log_level
, url
, true, NULL
, &viewer_connection
);
1904 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1905 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1906 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1907 "Failed to create viewer connection");
1908 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1909 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1910 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1917 status
= live_viewer_connection_list_sessions(viewer_connection
,
1919 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1920 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1921 "Failed to list viewer sessions");
1928 BT_VALUE_PUT_REF_AND_RESET(*result
);
1931 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1935 if (viewer_connection
) {
1936 live_viewer_connection_destroy(viewer_connection
);
1939 g_free(validate_error
);
1945 bt_component_class_query_method_status
lttng_live_query_support_info(
1946 const bt_value
*params
, const bt_value
**result
,
1947 bt_self_component_class
*self_comp_class
,
1948 bt_logging_level log_level
)
1950 bt_component_class_query_method_status status
=
1951 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1952 const bt_value
*input_type_value
;
1953 const bt_value
*input_value
;
1955 struct bt_common_lttng_live_url_parts parts
= { 0 };
1957 /* Used by the logging macros */
1958 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1961 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1963 if (!input_type_value
) {
1964 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1965 "Missing expected `type` parameter.");
1969 if (!bt_value_is_string(input_type_value
)) {
1970 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1971 "`type` parameter is not a string value.");
1975 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1976 /* We don't handle file system paths */
1980 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1982 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1983 "Missing expected `input` parameter.");
1987 if (!bt_value_is_string(input_value
)) {
1988 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1989 "`input` parameter is not a string value.");
1993 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1995 if (parts
.session_name
) {
1997 * Looks pretty much like an LTTng live URL: we got the
1998 * session name part, which forms a complete URL.
2004 *result
= bt_value_real_create_init(weight
);
2006 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2014 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2017 BT_ASSERT(!*result
);
2020 bt_common_destroy_lttng_live_url_parts(&parts
);
2025 bt_component_class_query_method_status
lttng_live_query(
2026 bt_self_component_class_source
*comp_class
,
2027 bt_private_query_executor
*priv_query_exec
,
2028 const char *object
, const bt_value
*params
,
2029 __attribute__((unused
)) void *method_data
,
2030 const bt_value
**result
)
2032 bt_component_class_query_method_status status
=
2033 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2034 bt_self_component
*self_comp
= NULL
;
2035 bt_self_component_class
*self_comp_class
=
2036 bt_self_component_class_source_as_self_component_class(comp_class
);
2037 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2038 bt_private_query_executor_as_query_executor_const(
2041 if (strcmp(object
, "sessions") == 0) {
2042 status
= lttng_live_query_list_sessions(params
, result
,
2043 self_comp_class
, log_level
);
2044 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2045 status
= lttng_live_query_support_info(params
, result
,
2046 self_comp_class
, log_level
);
2048 BT_COMP_LOGI("Unknown query object `%s`", object
);
2049 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2058 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2063 if (lttng_live
->params
.url
) {
2064 g_string_free(lttng_live
->params
.url
, TRUE
);
2070 void lttng_live_component_finalize(bt_self_component_source
*component
)
2072 void *data
= bt_self_component_get_data(
2073 bt_self_component_source_as_self_component(component
));
2078 lttng_live_component_destroy_data(data
);
2082 enum session_not_found_action
parse_session_not_found_action_param(
2083 const bt_value
*no_session_param
)
2085 enum session_not_found_action action
;
2086 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2088 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2089 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2090 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2091 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2093 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2094 action
= SESSION_NOT_FOUND_ACTION_END
;
2100 static struct bt_param_validation_value_descr inputs_elem_descr
= {
2101 .type
= BT_VALUE_TYPE_STRING
,
2104 static const char *sess_not_found_action_choices
[] = {
2105 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2106 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2107 SESS_NOT_FOUND_ACTION_END_STR
,
2110 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2111 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
2114 .element_type
= &inputs_elem_descr
,
2116 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
2117 .choices
= sess_not_found_action_choices
,
2119 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2123 bt_component_class_initialize_method_status
lttng_live_component_create(
2124 const bt_value
*params
,
2125 bt_logging_level log_level
,
2126 bt_self_component
*self_comp
,
2127 struct lttng_live_component
**component
)
2129 struct lttng_live_component
*lttng_live
= NULL
;
2130 const bt_value
*inputs_value
;
2131 const bt_value
*url_value
;
2132 const bt_value
*value
;
2134 enum bt_param_validation_status validation_status
;
2135 gchar
*validation_error
= NULL
;
2136 bt_component_class_initialize_method_status status
;
2138 validation_status
= bt_param_validation_validate(params
, params_descr
,
2140 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2141 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2143 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2144 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2145 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2149 lttng_live
= g_new0(struct lttng_live_component
, 1);
2151 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2154 lttng_live
->log_level
= log_level
;
2155 lttng_live
->self_comp
= self_comp
;
2156 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2157 lttng_live
->has_msg_iter
= false;
2160 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2162 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2163 url
= bt_value_string_get(url_value
);
2165 lttng_live
->params
.url
= g_string_new(url
);
2166 if (!lttng_live
->params
.url
) {
2167 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2171 value
= bt_value_map_borrow_entry_value_const(params
,
2172 SESS_NOT_FOUND_ACTION_PARAM
);
2174 lttng_live
->params
.sess_not_found_act
=
2175 parse_session_not_found_action_param(value
);
2177 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2178 "defaulting to `%s`.",
2179 SESS_NOT_FOUND_ACTION_PARAM
,
2180 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2181 lttng_live
->params
.sess_not_found_act
=
2182 SESSION_NOT_FOUND_ACTION_CONTINUE
;
2185 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2189 lttng_live_component_destroy_data(lttng_live
);
2192 g_free(validation_error
);
2194 *component
= lttng_live
;
2199 bt_component_class_initialize_method_status
lttng_live_component_init(
2200 bt_self_component_source
*self_comp_src
,
2201 bt_self_component_source_configuration
*config
,
2202 const bt_value
*params
,
2203 __attribute__((unused
)) void *init_method_data
)
2205 struct lttng_live_component
*lttng_live
;
2206 bt_component_class_initialize_method_status ret
;
2207 bt_self_component
*self_comp
=
2208 bt_self_component_source_as_self_component(self_comp_src
);
2209 bt_logging_level log_level
= bt_component_get_logging_level(
2210 bt_self_component_as_component(self_comp
));
2211 bt_self_component_add_port_status add_port_status
;
2213 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2214 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2218 add_port_status
= bt_self_component_source_add_output_port(
2219 self_comp_src
, "out", NULL
, NULL
);
2220 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2221 ret
= (int) add_port_status
;
2225 bt_self_component_set_data(self_comp
, lttng_live
);
2229 lttng_live_component_destroy_data(lttng_live
);