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 data. It can be either:
361 * - quiescent: need to put it in the prio heap at quiescent end
363 * - have data: need to wire up first event into the prio heap,
364 * - have no data on this stream at this point: need to retry (AGAIN) or
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 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
383 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
384 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
387 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
388 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
391 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
393 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
396 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
397 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
398 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
399 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
401 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
402 last_inact_ts
== curr_inact_ts
) {
403 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
404 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
406 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
410 lttng_live_stream
->base_offset
= index
.offset
;
411 lttng_live_stream
->offset
= index
.offset
;
412 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
414 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
415 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
421 * Creation of the message requires the ctf trace class to be created
422 * beforehand, but the live protocol gives us all streams (including metadata)
423 * at once. So we split it in three steps: getting streams, getting metadata
424 * (which creates the ctf trace class), and then creating the per-stream
428 enum lttng_live_iterator_status
lttng_live_get_session(
429 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
430 struct lttng_live_session
*session
)
432 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
433 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
434 enum lttng_live_iterator_status status
;
437 BT_COMP_LOGD("Updating all streams for session: "
438 "session-id=%"PRIu64
", session-name=\"%s\"",
439 session
->id
, session
->session_name
->str
);
441 if (!session
->attached
) {
442 enum lttng_live_viewer_status attach_status
=
443 lttng_live_session_attach(session
,
444 lttng_live_msg_iter
->self_msg_iter
);
445 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
446 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
448 * Clear any causes appended in
449 * `lttng_live_attach_session()` as we want to
450 * return gracefully since the graph was
453 bt_current_thread_clear_error();
454 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
456 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
457 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
458 "Error attaching to LTTng live session");
464 status
= lttng_live_session_get_new_streams(session
,
465 lttng_live_msg_iter
->self_msg_iter
);
466 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
467 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
471 while (trace_idx
< session
->traces
->len
) {
472 struct lttng_live_trace
*trace
=
473 g_ptr_array_index(session
->traces
, trace_idx
);
475 status
= lttng_live_metadata_update(trace
);
477 case LTTNG_LIVE_ITERATOR_STATUS_END
:
478 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
481 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
482 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
485 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
486 "Error updating trace metadata: "
487 "stream-iter-status=%s, trace-id=%"PRIu64
,
488 lttng_live_iterator_status_string(status
),
493 status
= lttng_live_lazy_msg_init(session
,
494 lttng_live_msg_iter
->self_msg_iter
);
501 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
503 uint64_t session_idx
, trace_idx
;
505 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
507 struct lttng_live_session
*session
=
508 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
509 session
->new_streams_needed
= true;
510 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
512 struct lttng_live_trace
*trace
=
513 g_ptr_array_index(session
->traces
, trace_idx
);
515 BT_ASSERT(trace
->metadata_stream_state
!=
516 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
518 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
524 enum lttng_live_iterator_status
525 lttng_live_iterator_handle_new_streams_and_metadata(
526 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
528 enum lttng_live_iterator_status status
;
529 enum lttng_live_viewer_status viewer_status
;
530 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
531 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
532 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
533 struct lttng_live_session
*session
;
534 enum session_not_found_action sess_not_found_act
=
535 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
537 BT_COMP_LOGD("Update data and metadata of all sessions: "
538 "live-msg-iter-addr=%p", lttng_live_msg_iter
);
540 * In a remotely distant future, we could add a "new
541 * session" flag to the protocol, which would tell us that we
542 * need to query for new sessions even though we have sessions
545 if (lttng_live_msg_iter
->sessions
->len
== 0) {
546 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
547 BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter");
548 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
551 BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
553 * Retry to create a viewer session for the requested
556 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
557 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
558 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
559 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
560 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
561 "Error creating LTTng live viewer session");
562 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
563 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
572 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
574 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
576 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
578 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
580 case LTTNG_LIVE_ITERATOR_STATUS_END
:
581 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
586 if (!session
->closed
) {
587 nr_sessions_opened
++;
591 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
592 nr_sessions_opened
== 0) {
593 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
595 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
603 enum lttng_live_iterator_status
emit_inactivity_message(
604 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
605 struct lttng_live_stream_iterator
*stream_iter
,
606 const bt_message
**message
, uint64_t timestamp
)
608 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
609 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
610 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
611 bt_message
*msg
= NULL
;
613 BT_ASSERT(stream_iter
->trace
->clock_class
);
615 msg
= bt_message_message_iterator_inactivity_create(
616 lttng_live_msg_iter
->self_msg_iter
,
617 stream_iter
->trace
->clock_class
, timestamp
);
619 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
620 "Error emitting message iterator inactivity message");
629 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
630 bt_message_put_ref(msg
);
635 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
636 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
637 struct lttng_live_stream_iterator
*lttng_live_stream
,
638 const bt_message
**message
)
640 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
642 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
643 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
646 if (lttng_live_stream
->current_inactivity_ts
==
647 lttng_live_stream
->last_inactivity_ts
) {
648 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
649 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
653 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
654 message
, lttng_live_stream
->current_inactivity_ts
);
656 lttng_live_stream
->last_inactivity_ts
=
657 lttng_live_stream
->current_inactivity_ts
;
663 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
664 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
665 const bt_message
*msg
, int64_t last_msg_ts_ns
,
668 const bt_clock_class
*clock_class
= NULL
;
669 const bt_clock_snapshot
*clock_snapshot
= NULL
;
671 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
672 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
675 BT_ASSERT_DBG(ts_ns
);
677 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
678 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
681 switch (bt_message_get_type(msg
)) {
682 case BT_MESSAGE_TYPE_EVENT
:
683 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
685 BT_ASSERT_DBG(clock_class
);
687 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
690 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
691 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
693 BT_ASSERT(clock_class
);
695 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
698 case BT_MESSAGE_TYPE_PACKET_END
:
699 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
701 BT_ASSERT(clock_class
);
703 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
706 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
707 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
709 BT_ASSERT(clock_class
);
711 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
714 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
715 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
717 BT_ASSERT(clock_class
);
719 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
722 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
723 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
727 /* All the other messages have a higher priority */
728 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
729 *ts_ns
= last_msg_ts_ns
;
733 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
734 BT_ASSERT_DBG(clock_class
);
736 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
738 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
739 "Cannot get nanoseconds from Epoch of clock snapshot: "
740 "clock-snapshot-addr=%p", clock_snapshot
);
751 BT_COMP_LOGD("Found message's timestamp: "
752 "iter-data-addr=%p, msg-addr=%p, "
753 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
754 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
761 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
762 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
763 struct lttng_live_stream_iterator
*lttng_live_stream
,
764 const bt_message
**message
)
766 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
767 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
768 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
769 enum ctf_msg_iter_status status
;
770 uint64_t session_idx
, trace_idx
;
772 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
774 struct lttng_live_session
*session
=
775 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
777 if (session
->new_streams_needed
) {
778 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
781 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
783 struct lttng_live_trace
*trace
=
784 g_ptr_array_index(session
->traces
, trace_idx
);
785 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
786 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
792 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
793 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
794 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
795 "Invalid state of live stream iterator"
796 "stream-iter-status=%s",
797 lttng_live_stream_state_string(lttng_live_stream
->state
));
801 status
= ctf_msg_iter_get_next_message(
802 lttng_live_stream
->msg_iter
, message
);
804 case CTF_MSG_ITER_STATUS_EOF
:
805 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
807 case CTF_MSG_ITER_STATUS_OK
:
808 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
810 case CTF_MSG_ITER_STATUS_AGAIN
:
812 * Continue immediately (end of packet). The next
813 * get_index may return AGAIN to delay the following
816 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
818 case CTF_MSG_ITER_STATUS_ERROR
:
820 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
821 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
822 "CTF message iterator failed to get next message: "
823 "msg-iter=%p, msg-iter-status=%s",
824 lttng_live_stream
->msg_iter
,
825 ctf_msg_iter_status_string(status
));
834 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
835 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
836 struct lttng_live_stream_iterator
*stream_iter
,
837 const bt_message
**curr_msg
)
839 enum lttng_live_iterator_status live_status
=
840 LTTNG_LIVE_ITERATOR_STATUS_OK
;
841 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
842 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
844 * The viewer has hung up on us so we are closing the stream. The
845 * `ctf_msg_iter` should simply realize that it needs to close the
846 * stream properly by emitting the necessary stream end message.
848 enum ctf_msg_iter_status status
= ctf_msg_iter_get_next_message(
849 stream_iter
->msg_iter
, curr_msg
);
851 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
852 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
853 "Error getting the next message from CTF message iterator");
854 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
856 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
857 BT_COMP_LOGI("Reached the end of the live stream iterator.");
858 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
862 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
870 * handle_no_data_streams()
872 * - for each ACTIVE_NO_DATA stream:
873 * - query relayd for stream data, or quiescence info.
874 * - if need metadata, get metadata, goto retry.
875 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
876 * - if quiescent, move to QUIESCENT streams
877 * - if fetched data, move to ACTIVE_DATA streams
878 * (at this point each stream either has data, or is quiescent)
882 * handle_new_streams_and_metadata()
883 * - query relayd for known streams, add them as ACTIVE_NO_DATA
884 * - query relayd for metadata
886 * call handle_active_no_data_streams()
888 * handle_quiescent_streams()
889 * - if at least one stream is ACTIVE_DATA:
890 * - peek stream event with lowest timestamp -> next_ts
891 * - for each quiescent stream
892 * - if next_ts >= quiescent end
893 * - set state to ACTIVE_NO_DATA
895 * - for each quiescent stream
896 * - set state to ACTIVE_NO_DATA
898 * call handle_active_no_data_streams()
900 * handle_active_data_streams()
901 * - if at least one stream is ACTIVE_DATA:
902 * - get stream event with lowest timestamp from heap
903 * - make that stream event the current message.
904 * - move this stream heap position to its next event
905 * - if we need to fetch data from relayd, move
906 * stream to ACTIVE_NO_DATA.
910 * end criterion: ctrl-c on client. If relayd exits or the session
911 * closes on the relay daemon side, we keep on waiting for streams.
912 * Eventually handle --end timestamp (also an end criterion).
914 * When disconnected from relayd: try to re-connect endlessly.
917 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
918 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
919 struct lttng_live_stream_iterator
*stream_iter
,
920 const bt_message
**curr_msg
)
922 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
923 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
924 enum lttng_live_iterator_status live_status
;
926 BT_COMP_LOGD("Finding the next message for stream iterator: "
927 "stream-name=\"%s\"", stream_iter
->name
->str
);
929 if (stream_iter
->has_stream_hung_up
) {
931 * The stream has hung up and the stream was properly closed
932 * during the last call to the current function. Return _END
933 * status now so that this stream iterator is removed for the
934 * stream iterator list.
936 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
941 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
942 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
943 lttng_live_msg_iter
);
944 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
947 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
948 lttng_live_msg_iter
, stream_iter
);
950 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
951 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
953 * We overwrite `live_status` since `curr_msg` is
954 * likely set to a valid message in this function.
956 live_status
= lttng_live_iterator_close_stream(
957 lttng_live_msg_iter
, stream_iter
, curr_msg
);
961 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
962 lttng_live_msg_iter
, stream_iter
, curr_msg
);
963 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
964 BT_ASSERT(!*curr_msg
);
970 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
971 lttng_live_msg_iter
, stream_iter
, curr_msg
);
972 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
973 BT_ASSERT(!*curr_msg
);
977 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
985 bool is_discarded_packet_or_event_message(const bt_message
*msg
)
987 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
989 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
990 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
994 enum lttng_live_iterator_status
adjust_discarded_packets_message(
995 bt_self_message_iterator
*iter
,
996 const bt_stream
*stream
,
997 const bt_message
*msg_in
, bt_message
**msg_out
,
998 uint64_t new_begin_ts
)
1000 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1001 enum bt_property_availability availability
;
1002 const bt_clock_snapshot
*clock_snapshot
;
1006 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1007 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1009 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1010 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1012 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1013 iter
, stream
, new_begin_ts
, end_ts
);
1015 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1019 bt_message_discarded_packets_set_count(*msg_out
, count
);
1025 enum lttng_live_iterator_status
adjust_discarded_events_message(
1026 bt_self_message_iterator
*iter
,
1027 const bt_stream
*stream
,
1028 const bt_message
*msg_in
, bt_message
**msg_out
,
1029 uint64_t new_begin_ts
)
1031 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1032 enum bt_property_availability availability
;
1033 const bt_clock_snapshot
*clock_snapshot
;
1037 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1038 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1040 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1041 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1043 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1044 iter
, stream
, new_begin_ts
, end_ts
);
1046 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1050 bt_message_discarded_events_set_count(*msg_out
, count
);
1056 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
1057 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1058 struct lttng_live_trace
*live_trace
,
1059 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1061 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1062 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1063 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1064 enum lttng_live_iterator_status stream_iter_status
;;
1065 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1066 uint64_t stream_iter_idx
;
1068 BT_ASSERT_DBG(live_trace
);
1069 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1071 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1072 "trace-id=%"PRIu64
, live_trace
->id
);
1074 * Update the current message of every stream iterators of this trace.
1075 * The current msg of every stream must have a timestamp equal or
1076 * larger than the last message returned by this iterator. We must
1077 * ensure monotonicity.
1079 stream_iter_idx
= 0;
1080 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1081 bool stream_iter_is_ended
= false;
1082 struct lttng_live_stream_iterator
*stream_iter
=
1083 g_ptr_array_index(live_trace
->stream_iterators
,
1087 * If there is no current message for this stream, go fetch
1090 while (!stream_iter
->current_msg
) {
1091 const bt_message
*msg
= NULL
;
1092 int64_t curr_msg_ts_ns
= INT64_MAX
;
1093 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1094 lttng_live_msg_iter
, stream_iter
, &msg
);
1096 BT_COMP_LOGD("live stream iterator returned status :%s",
1097 lttng_live_iterator_status_string(stream_iter_status
));
1098 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1099 stream_iter_is_ended
= true;
1103 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1110 * Get the timestamp in nanoseconds from origin of this
1113 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1114 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1118 * Check if the message of the current live stream
1119 * iterator occurred at the exact same time or after the
1120 * last message returned by this component's message
1121 * iterator. If not, we return an error.
1123 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1124 stream_iter
->current_msg
= msg
;
1125 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1127 const bt_clock_class
*clock_class
;
1128 const bt_stream_class
*stream_class
;
1129 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1130 int64_t last_inactivity_ts_ns
;
1132 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1133 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1135 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1137 stream_iter
->last_inactivity_ts
,
1138 &last_inactivity_ts_ns
);
1139 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1140 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1144 if (last_inactivity_ts_ns
> curr_msg_ts_ns
&&
1145 is_discarded_packet_or_event_message(msg
)) {
1147 * The CTF message iterator emits Discarded
1148 * Packets and Events with synthesized begin and
1149 * end timestamps from the bounds of the last
1150 * known packet and the newly decoded packet
1153 * The CTF message iterator is not aware of
1154 * stream inactivity beacons. Hence, we have
1155 * to adjust the begin timestamp of those types
1156 * of messages if a stream signaled its
1157 * inactivity up until _after_ the last known
1158 * packet's begin timestamp.
1160 * Otherwise, the monotonicity guarantee would
1163 const enum bt_message_type msg_type
=
1164 bt_message_get_type(msg
);
1165 enum lttng_live_iterator_status adjust_status
=
1166 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1167 bt_message
*adjusted_message
;
1170 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1171 adjust_status
= adjust_discarded_events_message(
1172 lttng_live_msg_iter
->self_msg_iter
,
1173 stream_iter
->stream
,
1174 msg
, &adjusted_message
,
1175 stream_iter
->last_inactivity_ts
);
1177 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1178 adjust_status
= adjust_discarded_packets_message(
1179 lttng_live_msg_iter
->self_msg_iter
,
1180 stream_iter
->stream
,
1181 msg
, &adjusted_message
,
1182 stream_iter
->last_inactivity_ts
);
1188 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1189 stream_iter_status
= adjust_status
;
1193 BT_ASSERT_DBG(adjusted_message
);
1194 stream_iter
->current_msg
= adjusted_message
;
1195 stream_iter
->current_msg_ts_ns
=
1196 last_inactivity_ts_ns
;
1199 * We received a message in the past. To ensure
1200 * monotonicity, we can't send it forward.
1202 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1203 "Message's timestamp is less than "
1204 "lttng-live's message iterator's last "
1205 "returned timestamp: "
1206 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1207 "last-msg-ts=%" PRId64
,
1208 lttng_live_msg_iter
, curr_msg_ts_ns
,
1209 lttng_live_msg_iter
->last_msg_ts_ns
);
1210 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1216 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1218 if (!stream_iter_is_ended
) {
1219 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1220 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1222 * Update the current best candidate message
1223 * for the stream iterator of this live trace
1224 * to be forwarded downstream.
1226 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1227 youngest_candidate_stream_iter
= stream_iter
;
1228 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1230 * Order the messages in an arbitrary but
1231 * deterministic way.
1233 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1234 int ret
= common_muxing_compare_messages(
1235 stream_iter
->current_msg
,
1236 youngest_candidate_stream_iter
->current_msg
);
1239 * The `youngest_candidate_stream_iter->current_msg`
1240 * should go first. Update the next
1241 * iterator and the current timestamp.
1243 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1244 youngest_candidate_stream_iter
= stream_iter
;
1245 } else if (ret
== 0) {
1247 * Unable to pick which one should go
1250 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1251 "stream-iter-addr=%p"
1252 "stream-iter-addr=%p",
1254 youngest_candidate_stream_iter
);
1261 * The live stream iterator has ended. That
1262 * iterator is removed from the array, but
1263 * there is no need to increment
1264 * stream_iter_idx as
1265 * g_ptr_array_remove_index_fast replaces the
1266 * removed element with the array's last
1269 g_ptr_array_remove_index_fast(
1270 live_trace
->stream_iterators
,
1275 if (youngest_candidate_stream_iter
) {
1276 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1277 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1280 * The only case where we don't have a candidate for this trace
1281 * is if we reached the end of all the iterators.
1283 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1284 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1288 return stream_iter_status
;
1292 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1293 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1294 struct lttng_live_session
*session
,
1295 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1297 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1298 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1299 enum lttng_live_iterator_status stream_iter_status
;
1300 uint64_t trace_idx
= 0;
1301 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1302 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1304 BT_COMP_LOGD("Finding the next stream iterator for session: "
1305 "session-id=%"PRIu64
, session
->id
);
1307 * Make sure we are attached to the session and look for new streams
1310 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1311 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1312 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1313 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1317 BT_ASSERT_DBG(session
->traces
);
1319 while (trace_idx
< session
->traces
->len
) {
1320 bool trace_is_ended
= false;
1321 struct lttng_live_stream_iterator
*stream_iter
;
1322 struct lttng_live_trace
*trace
=
1323 g_ptr_array_index(session
->traces
, trace_idx
);
1325 stream_iter_status
= next_stream_iterator_for_trace(
1326 lttng_live_msg_iter
, trace
, &stream_iter
);
1327 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1329 * All the live stream iterators for this trace are
1330 * ENDed. Remove the trace from this session.
1332 trace_is_ended
= true;
1333 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1337 if (!trace_is_ended
) {
1338 BT_ASSERT_DBG(stream_iter
);
1340 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1341 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1342 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1343 youngest_candidate_stream_iter
= stream_iter
;
1344 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1346 * Order the messages in an arbitrary but
1347 * deterministic way.
1349 int ret
= common_muxing_compare_messages(
1350 stream_iter
->current_msg
,
1351 youngest_candidate_stream_iter
->current_msg
);
1354 * The `youngest_candidate_stream_iter->current_msg`
1355 * should go first. Update the next iterator
1356 * and the current timestamp.
1358 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1359 youngest_candidate_stream_iter
= stream_iter
;
1360 } else if (ret
== 0) {
1361 /* Unable to pick which one should go first. */
1362 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1363 "stream-iter-addr=%p" "stream-iter-addr=%p",
1364 stream_iter
, youngest_candidate_stream_iter
);
1370 * trace_idx is not incremented since
1371 * g_ptr_array_remove_index_fast replaces the
1372 * element at trace_idx with the array's last element.
1374 g_ptr_array_remove_index_fast(session
->traces
,
1378 if (youngest_candidate_stream_iter
) {
1379 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1380 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1383 * The only cases where we don't have a candidate for this
1385 * 1. if we reached the end of all the iterators of all the
1386 * traces of this session,
1387 * 2. if we never had live stream iterator in the first place.
1389 * In either cases, we return END.
1391 BT_ASSERT(session
->traces
->len
== 0);
1392 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1395 return stream_iter_status
;
1399 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1403 for (i
= 0; i
< count
; i
++) {
1404 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1409 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
1410 bt_self_message_iterator
*self_msg_it
,
1411 bt_message_array_const msgs
, uint64_t capacity
,
1414 bt_message_iterator_class_next_method_status status
;
1415 enum lttng_live_viewer_status viewer_status
;
1416 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1417 bt_self_message_iterator_get_data(self_msg_it
);
1418 struct lttng_live_component
*lttng_live
=
1419 lttng_live_msg_iter
->lttng_live_comp
;
1420 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1421 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1422 enum lttng_live_iterator_status stream_iter_status
;
1423 uint64_t session_idx
;
1427 BT_ASSERT_DBG(lttng_live_msg_iter
);
1429 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1431 * The iterator was interrupted in a previous call to the
1432 * `_next()` method. We currently do not support generating
1433 * messages after such event. The babeltrace2 CLI should never
1434 * be running the graph after being interrupted. So this check
1435 * is to prevent other graph users from using this live
1436 * iterator in an messed up internal state.
1438 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1439 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1440 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1445 * Clear all the invalid message reference that might be left over in
1448 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1451 * If no session are exposed on the relay found at the url provided by
1452 * the user, session count will be 0. In this case, we return status
1453 * end to return gracefully.
1455 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1456 if (lttng_live
->params
.sess_not_found_act
!=
1457 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1458 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1462 * The are no more active session for this session
1463 * name. Retry to create a viewer session for the
1464 * requested session name.
1466 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1467 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1468 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1469 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1470 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1471 "Error creating LTTng live viewer session");
1472 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1473 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1482 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1483 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1487 * Here the muxing of message is done.
1489 * We need to iterate over all the streams of all the traces of all the
1490 * viewer sessions in order to get the message with the smallest
1491 * timestamp. In this case, a session is a viewer session and there is
1492 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1493 * kernel). Each viewer session can have multiple traces, for example,
1494 * 64bit UST viewer sessions could have multiple per-pid traces.
1496 * We iterate over the streams of each traces to update and see what is
1497 * their next message's timestamp. From those timestamps, we select the
1498 * message with the smallest timestamp as the best candidate message
1499 * for that trace and do the same thing across all the sessions.
1501 * We then compare the timestamp of best candidate message of all the
1502 * sessions to pick the message with the smallest timestamp and we
1505 while (*count
< capacity
) {
1506 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1507 *candidate_stream_iter
= NULL
;
1508 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1510 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1512 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1513 struct lttng_live_session
*session
=
1514 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1517 /* Find the best candidate message to send downstream. */
1518 stream_iter_status
= next_stream_iterator_for_session(
1519 lttng_live_msg_iter
, session
,
1520 &candidate_stream_iter
);
1522 /* If we receive an END status, it means that either:
1523 * - Those traces never had active streams (UST with no
1524 * data produced yet),
1525 * - All live stream iterators have ENDed.*/
1526 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1527 if (session
->closed
&& session
->traces
->len
== 0) {
1529 * Remove the session from the list.
1530 * session_idx is not modified since
1531 * g_ptr_array_remove_index_fast
1532 * replaces the the removed element with
1533 * the array's last element.
1535 g_ptr_array_remove_index_fast(
1536 lttng_live_msg_iter
->sessions
,
1544 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1548 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1549 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1550 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1551 youngest_stream_iter
= candidate_stream_iter
;
1552 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1554 * The currently selected message to be sent
1555 * downstream next has the exact same timestamp
1556 * that of the current candidate message. We
1557 * must break the tie in a predictable manner.
1559 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1561 * Order the messages in an arbitrary but
1562 * deterministic way.
1564 int ret
= common_muxing_compare_messages(
1565 candidate_stream_iter
->current_msg
,
1566 youngest_stream_iter
->current_msg
);
1569 * The `candidate_stream_iter->current_msg`
1570 * should go first. Update the next
1571 * iterator and the current timestamp.
1573 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1574 youngest_stream_iter
= candidate_stream_iter
;
1575 } else if (ret
== 0) {
1576 /* Unable to pick which one should go first. */
1577 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1578 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1579 youngest_stream_iter
, candidate_stream_iter
);
1586 if (!youngest_stream_iter
) {
1587 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1591 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1592 /* Ensure monotonicity. */
1593 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1594 youngest_stream_iter
->current_msg_ts_ns
);
1597 * Insert the next message to the message batch. This will set
1598 * stream iterator current messsage to NULL so that next time
1599 * we fetch the next message of that stream iterator
1601 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1604 /* Update the last timestamp in nanoseconds sent downstream. */
1605 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1606 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1608 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1612 switch (stream_iter_status
) {
1613 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1614 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1616 * If we gathered messages, return _OK even if the graph was
1617 * interrupted. This allows for the components downstream to at
1618 * least get the thoses messages. If the graph was indeed
1619 * interrupted there should not be another _next() call as the
1620 * application will tear down the graph. This component class
1621 * doesn't support restarting after an interruption.
1624 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1626 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1629 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1630 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1632 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1633 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1634 "Memory error preparing the next batch of messages: "
1635 "live-iter-status=%s",
1636 lttng_live_iterator_status_string(stream_iter_status
));
1637 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1639 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1640 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1641 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1642 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1643 "Error preparing the next batch of messages: "
1644 "live-iter-status=%s",
1645 lttng_live_iterator_status_string(stream_iter_status
));
1647 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1648 /* Put all existing messages on error. */
1649 put_messages(msgs
, *count
);
1660 struct lttng_live_msg_iter
*lttng_live_msg_iter_create(
1661 struct lttng_live_component
*lttng_live_comp
,
1662 bt_self_message_iterator
*self_msg_it
)
1664 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1665 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1667 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1668 g_new0(struct lttng_live_msg_iter
, 1);
1669 if (!lttng_live_msg_iter
) {
1670 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1671 "Failed to allocate lttng_live_msg_iter");
1675 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1676 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1677 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1678 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1680 lttng_live_msg_iter
->active_stream_iter
= 0;
1681 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1682 lttng_live_msg_iter
->was_interrupted
= false;
1684 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1685 (GDestroyNotify
) lttng_live_destroy_session
);
1686 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1689 return lttng_live_msg_iter
;
1694 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
1695 bt_self_message_iterator
*self_msg_it
,
1696 bt_self_message_iterator_configuration
*config
,
1697 bt_self_component_port_output
*self_port
)
1699 bt_message_iterator_class_initialize_method_status status
;
1700 struct lttng_live_component
*lttng_live
;
1701 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1702 enum lttng_live_viewer_status viewer_status
;
1703 bt_logging_level log_level
;
1704 bt_self_component
*self_comp
=
1705 bt_self_message_iterator_borrow_component(self_msg_it
);
1707 lttng_live
= bt_self_component_get_data(self_comp
);
1708 log_level
= lttng_live
->log_level
;
1709 self_comp
= lttng_live
->self_comp
;
1712 /* There can be only one downstream iterator at the same time. */
1713 BT_ASSERT(!lttng_live
->has_msg_iter
);
1714 lttng_live
->has_msg_iter
= true;
1716 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
,
1718 if (!lttng_live_msg_iter
) {
1719 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1720 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1721 "Failed to create lttng_live_msg_iter");
1725 viewer_status
= live_viewer_connection_create(self_comp
, NULL
,
1726 log_level
, lttng_live
->params
.url
->str
, false,
1727 lttng_live_msg_iter
, <tng_live_msg_iter
->viewer_connection
);
1728 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1729 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1730 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1731 "Failed to create viewer connection");
1732 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1734 * Interruption in the _iter_init() method is not
1735 * supported. Return an error.
1737 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1738 "Interrupted while creating viewer connection");
1743 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1744 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1745 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1746 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1747 "Failed to create viewer session");
1748 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1750 * Interruption in the _iter_init() method is not
1751 * supported. Return an error.
1753 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1754 "Interrupted when creating viewer session");
1759 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1760 switch (lttng_live
->params
.sess_not_found_act
) {
1761 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1762 BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of "
1763 "%s=\"%s\" component parameter: url=\"%s\"",
1764 SESS_NOT_FOUND_ACTION_PARAM
,
1765 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1766 lttng_live
->params
.url
->str
);
1768 case SESSION_NOT_FOUND_ACTION_FAIL
:
1769 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1770 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1771 "component parameter: url =\"%s\"",
1772 SESS_NOT_FOUND_ACTION_PARAM
,
1773 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1774 lttng_live
->params
.url
->str
);
1776 case SESSION_NOT_FOUND_ACTION_END
:
1777 BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1778 "call because of %s=\"%s\" component parameter: "
1779 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1780 SESS_NOT_FOUND_ACTION_END_STR
,
1781 lttng_live
->params
.url
->str
);
1788 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1789 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1793 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1794 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1799 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1800 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1801 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1805 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1806 const bt_value
*params
, const bt_value
**result
,
1807 bt_self_component_class
*self_comp_class
,
1808 bt_logging_level log_level
)
1810 bt_component_class_query_method_status status
;
1811 const bt_value
*url_value
= NULL
;
1813 struct live_viewer_connection
*viewer_connection
= NULL
;
1814 enum lttng_live_viewer_status viewer_status
;
1815 enum bt_param_validation_status validation_status
;
1816 gchar
*validate_error
= NULL
;
1818 validation_status
= bt_param_validation_validate(params
,
1819 list_sessions_params
, &validate_error
);
1820 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1821 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1823 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1824 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1825 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1830 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1831 url
= bt_value_string_get(url_value
);
1833 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
,
1834 log_level
, url
, true, NULL
, &viewer_connection
);
1835 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1836 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1837 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1838 "Failed to create viewer connection");
1839 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1840 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1841 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1848 status
= live_viewer_connection_list_sessions(viewer_connection
,
1850 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1851 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1852 "Failed to list viewer sessions");
1859 BT_VALUE_PUT_REF_AND_RESET(*result
);
1862 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1866 if (viewer_connection
) {
1867 live_viewer_connection_destroy(viewer_connection
);
1870 g_free(validate_error
);
1876 bt_component_class_query_method_status
lttng_live_query_support_info(
1877 const bt_value
*params
, const bt_value
**result
,
1878 bt_self_component_class
*self_comp_class
,
1879 bt_logging_level log_level
)
1881 bt_component_class_query_method_status status
=
1882 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1883 const bt_value
*input_type_value
;
1884 const bt_value
*input_value
;
1886 struct bt_common_lttng_live_url_parts parts
= { 0 };
1888 /* Used by the logging macros */
1889 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1892 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1894 if (!input_type_value
) {
1895 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1896 "Missing expected `type` parameter.");
1900 if (!bt_value_is_string(input_type_value
)) {
1901 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1902 "`type` parameter is not a string value.");
1906 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1907 /* We don't handle file system paths */
1911 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1913 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1914 "Missing expected `input` parameter.");
1918 if (!bt_value_is_string(input_value
)) {
1919 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1920 "`input` parameter is not a string value.");
1924 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1926 if (parts
.session_name
) {
1928 * Looks pretty much like an LTTng live URL: we got the
1929 * session name part, which forms a complete URL.
1935 *result
= bt_value_real_create_init(weight
);
1937 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1945 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1948 BT_ASSERT(!*result
);
1951 bt_common_destroy_lttng_live_url_parts(&parts
);
1956 bt_component_class_query_method_status
lttng_live_query(
1957 bt_self_component_class_source
*comp_class
,
1958 bt_private_query_executor
*priv_query_exec
,
1959 const char *object
, const bt_value
*params
,
1960 __attribute__((unused
)) void *method_data
,
1961 const bt_value
**result
)
1963 bt_component_class_query_method_status status
=
1964 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1965 bt_self_component
*self_comp
= NULL
;
1966 bt_self_component_class
*self_comp_class
=
1967 bt_self_component_class_source_as_self_component_class(comp_class
);
1968 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1969 bt_private_query_executor_as_query_executor_const(
1972 if (strcmp(object
, "sessions") == 0) {
1973 status
= lttng_live_query_list_sessions(params
, result
,
1974 self_comp_class
, log_level
);
1975 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1976 status
= lttng_live_query_support_info(params
, result
,
1977 self_comp_class
, log_level
);
1979 BT_COMP_LOGI("Unknown query object `%s`", object
);
1980 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1989 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1994 if (lttng_live
->params
.url
) {
1995 g_string_free(lttng_live
->params
.url
, TRUE
);
2001 void lttng_live_component_finalize(bt_self_component_source
*component
)
2003 void *data
= bt_self_component_get_data(
2004 bt_self_component_source_as_self_component(component
));
2009 lttng_live_component_destroy_data(data
);
2013 enum session_not_found_action
parse_session_not_found_action_param(
2014 const bt_value
*no_session_param
)
2016 enum session_not_found_action action
;
2017 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2019 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2020 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2021 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2022 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2024 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2025 action
= SESSION_NOT_FOUND_ACTION_END
;
2031 static struct bt_param_validation_value_descr inputs_elem_descr
= {
2032 .type
= BT_VALUE_TYPE_STRING
,
2035 static const char *sess_not_found_action_choices
[] = {
2036 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2037 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2038 SESS_NOT_FOUND_ACTION_END_STR
,
2041 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2042 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
2045 .element_type
= &inputs_elem_descr
,
2047 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
2048 .choices
= sess_not_found_action_choices
,
2050 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2054 bt_component_class_initialize_method_status
lttng_live_component_create(
2055 const bt_value
*params
,
2056 bt_logging_level log_level
,
2057 bt_self_component
*self_comp
,
2058 struct lttng_live_component
**component
)
2060 struct lttng_live_component
*lttng_live
= NULL
;
2061 const bt_value
*inputs_value
;
2062 const bt_value
*url_value
;
2063 const bt_value
*value
;
2065 enum bt_param_validation_status validation_status
;
2066 gchar
*validation_error
= NULL
;
2067 bt_component_class_initialize_method_status status
;
2069 validation_status
= bt_param_validation_validate(params
, params_descr
,
2071 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2072 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2074 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2075 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2076 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2080 lttng_live
= g_new0(struct lttng_live_component
, 1);
2082 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2085 lttng_live
->log_level
= log_level
;
2086 lttng_live
->self_comp
= self_comp
;
2087 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2088 lttng_live
->has_msg_iter
= false;
2091 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2093 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2094 url
= bt_value_string_get(url_value
);
2096 lttng_live
->params
.url
= g_string_new(url
);
2097 if (!lttng_live
->params
.url
) {
2098 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2102 value
= bt_value_map_borrow_entry_value_const(params
,
2103 SESS_NOT_FOUND_ACTION_PARAM
);
2105 lttng_live
->params
.sess_not_found_act
=
2106 parse_session_not_found_action_param(value
);
2108 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2109 "defaulting to `%s`.",
2110 SESS_NOT_FOUND_ACTION_PARAM
,
2111 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2112 lttng_live
->params
.sess_not_found_act
=
2113 SESSION_NOT_FOUND_ACTION_CONTINUE
;
2116 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2120 lttng_live_component_destroy_data(lttng_live
);
2123 g_free(validation_error
);
2125 *component
= lttng_live
;
2130 bt_component_class_initialize_method_status
lttng_live_component_init(
2131 bt_self_component_source
*self_comp_src
,
2132 bt_self_component_source_configuration
*config
,
2133 const bt_value
*params
,
2134 __attribute__((unused
)) void *init_method_data
)
2136 struct lttng_live_component
*lttng_live
;
2137 bt_component_class_initialize_method_status ret
;
2138 bt_self_component
*self_comp
=
2139 bt_self_component_source_as_self_component(self_comp_src
);
2140 bt_logging_level log_level
= bt_component_get_logging_level(
2141 bt_self_component_as_component(self_comp
));
2142 bt_self_component_add_port_status add_port_status
;
2144 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2145 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2149 add_port_status
= bt_self_component_source_add_output_port(
2150 self_comp_src
, "out", NULL
, NULL
);
2151 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2152 ret
= (int) add_port_status
;
2156 bt_self_component_set_data(self_comp
, lttng_live
);
2160 lttng_live_component_destroy_data(lttng_live
);