4 * Babeltrace CTF LTTng-live Client Component
6 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define BT_COMP_LOG_SELF_COMP self_comp
32 #define BT_LOG_OUTPUT_LEVEL log_level
33 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
34 #include "logging/comp-logging.h"
41 #include "common/assert.h"
42 #include <babeltrace2/babeltrace.h>
43 #include "compat/compiler.h"
44 #include <babeltrace2/types.h>
46 #include "plugins/common/muxing/muxing.h"
47 #include "plugins/common/param-validation/param-validation.h"
49 #include "data-stream.h"
51 #include "lttng-live.h"
53 #define MAX_QUERY_SIZE (256*1024)
54 #define URL_PARAM "url"
55 #define INPUTS_PARAM "inputs"
56 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
57 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
58 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
59 #define SESS_NOT_FOUND_ACTION_END_STR "end"
61 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
64 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
67 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
68 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
69 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
70 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
71 case LTTNG_LIVE_ITERATOR_STATUS_END
:
72 return "LTTNG_LIVE_ITERATOR_STATUS_END";
73 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
74 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
75 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
76 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
77 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
78 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
79 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
80 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
81 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
82 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
89 const char *print_state(struct lttng_live_stream_iterator
*s
)
92 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
93 return "ACTIVE_NO_DATA";
94 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
95 return "QUIESCENT_NO_DATA";
96 case LTTNG_LIVE_STREAM_QUIESCENT
:
98 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
100 case LTTNG_LIVE_STREAM_EOF
:
107 #define print_stream_state(live_stream_iter) \
109 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
110 ", curr_inact_ts %" PRId64, \
111 print_state(live_stream_iter), \
112 live_stream_iter->last_inactivity_ts, \
113 live_stream_iter->current_inactivity_ts); \
117 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
126 ret
= bt_self_message_iterator_is_interrupted(
127 msg_iter
->self_msg_iter
);
134 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
138 struct lttng_live_trace
*ret_trace
= NULL
;
140 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
141 struct lttng_live_trace
*trace
=
142 g_ptr_array_index(session
->traces
, trace_idx
);
143 if (trace
->id
== trace_id
) {
154 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
156 bt_logging_level log_level
= trace
->log_level
;
157 bt_self_component
*self_comp
= trace
->self_comp
;
159 BT_COMP_LOGD("Destroy lttng_live_trace");
161 BT_ASSERT(trace
->stream_iterators
);
162 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
164 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
165 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
167 lttng_live_metadata_fini(trace
);
172 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
175 struct lttng_live_trace
*trace
= NULL
;
176 bt_logging_level log_level
= session
->log_level
;
177 bt_self_component
*self_comp
= session
->self_comp
;
179 trace
= g_new0(struct lttng_live_trace
, 1);
181 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
182 "Failed to allocate live trace");
185 trace
->log_level
= session
->log_level
;
186 trace
->self_comp
= session
->self_comp
;
187 trace
->session
= session
;
188 trace
->id
= trace_id
;
189 trace
->trace_class
= NULL
;
191 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
192 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
193 BT_ASSERT(trace
->stream_iterators
);
194 trace
->new_metadata_needed
= true;
195 g_ptr_array_add(session
->traces
, trace
);
197 BT_COMP_LOGI("Create trace");
207 struct lttng_live_trace
*lttng_live_borrow_trace(
208 struct lttng_live_session
*session
, uint64_t trace_id
)
210 struct lttng_live_trace
*trace
;
212 trace
= lttng_live_find_trace(session
, trace_id
);
217 /* The session is the owner of the newly created trace. */
218 trace
= lttng_live_create_trace(session
, trace_id
);
225 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
226 uint64_t session_id
, const char *hostname
,
227 const char *session_name
)
230 struct lttng_live_session
*session
;
231 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
232 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
234 session
= g_new0(struct lttng_live_session
, 1);
236 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
237 "Failed to allocate live session");
241 session
->log_level
= lttng_live_msg_iter
->log_level
;
242 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
243 session
->id
= session_id
;
244 session
->traces
= g_ptr_array_new_with_free_func(
245 (GDestroyNotify
) lttng_live_destroy_trace
);
246 BT_ASSERT(session
->traces
);
247 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
248 session
->new_streams_needed
= true;
249 session
->hostname
= g_string_new(hostname
);
250 BT_ASSERT(session
->hostname
);
252 session
->session_name
= g_string_new(session_name
);
253 BT_ASSERT(session
->session_name
);
255 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
256 session
->id
, hostname
, session_name
);
257 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
260 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error adding session");
268 void lttng_live_destroy_session(struct lttng_live_session
*session
)
270 bt_logging_level log_level
;
271 bt_self_component
*self_comp
;
277 log_level
= session
->log_level
;
278 self_comp
= session
->self_comp
;
279 BT_COMP_LOGD("Destroy lttng live session");
280 if (session
->id
!= -1ULL) {
281 if (lttng_live_detach_session(session
)) {
282 if (!lttng_live_graph_is_canceled(
283 session
->lttng_live_msg_iter
)) {
284 /* Old relayd cannot detach sessions. */
285 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
292 if (session
->traces
) {
293 g_ptr_array_free(session
->traces
, TRUE
);
296 if (session
->hostname
) {
297 g_string_free(session
->hostname
, TRUE
);
299 if (session
->session_name
) {
300 g_string_free(session
->session_name
, TRUE
);
309 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
311 if (!lttng_live_msg_iter
) {
315 if (lttng_live_msg_iter
->sessions
) {
316 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
319 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
320 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
321 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
323 /* All stream iterators must be destroyed at this point. */
324 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
325 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
327 g_free(lttng_live_msg_iter
);
334 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
336 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
338 BT_ASSERT(self_msg_iter
);
340 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
341 BT_ASSERT(lttng_live_msg_iter
);
342 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
346 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
347 struct lttng_live_stream_iterator
*lttng_live_stream
)
349 bt_logging_level log_level
= lttng_live_stream
->log_level
;
350 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
352 switch (lttng_live_stream
->state
) {
353 case LTTNG_LIVE_STREAM_QUIESCENT
:
354 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
356 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
358 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
360 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
362 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
364 case LTTNG_LIVE_STREAM_EOF
:
367 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
371 * For active no data stream, fetch next data. It can be either:
372 * - quiescent: need to put it in the prio heap at quiescent end
374 * - have data: need to wire up first event into the prio heap,
375 * - have no data on this stream at this point: need to retry (AGAIN) or
379 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
380 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
381 struct lttng_live_stream_iterator
*lttng_live_stream
)
383 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
384 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
385 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
386 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
387 struct packet_index index
;
389 if (lttng_live_stream
->trace
->new_metadata_needed
) {
390 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
393 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
394 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
397 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
398 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
401 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
403 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
406 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
407 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
408 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
409 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
411 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
412 last_inact_ts
== curr_inact_ts
) {
413 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
414 print_stream_state(lttng_live_stream
);
416 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
420 lttng_live_stream
->base_offset
= index
.offset
;
421 lttng_live_stream
->offset
= index
.offset
;
422 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
424 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
425 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
431 * Creation of the message requires the ctf trace class to be created
432 * beforehand, but the live protocol gives us all streams (including metadata)
433 * at once. So we split it in three steps: getting streams, getting metadata
434 * (which creates the ctf trace class), and then creating the per-stream
438 enum lttng_live_iterator_status
lttng_live_get_session(
439 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
440 struct lttng_live_session
*session
)
442 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
443 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
444 enum lttng_live_iterator_status status
;
447 if (!session
->attached
) {
448 enum lttng_live_attach_session_status attach_status
=
449 lttng_live_attach_session(session
);
450 if (attach_status
!= LTTNG_LIVE_ATTACH_SESSION_STATUS_OK
) {
451 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
453 * Clear any causes appended in
454 * `lttng_live_attach_session()` as we want to
455 * return gracefully since the graph was
458 bt_current_thread_clear_error();
459 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
461 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
462 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
463 "Error attaching to LTTng live session");
469 status
= lttng_live_get_new_streams(session
);
470 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
471 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
475 while (trace_idx
< session
->traces
->len
) {
476 struct lttng_live_trace
*trace
=
477 g_ptr_array_index(session
->traces
, trace_idx
);
479 status
= lttng_live_metadata_update(trace
);
481 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
484 case LTTNG_LIVE_ITERATOR_STATUS_END
:
486 * The trace has ended. Remove it of the array an
487 * continue the iteration.
488 * We can remove the trace safely when using the
489 * g_ptr_array_remove_index_fast because it replaces
490 * the element at trace_idx with the array's last
491 * element. trace_idx is not incremented because of
494 (void) g_ptr_array_remove_index_fast(session
->traces
,
501 status
= lttng_live_lazy_msg_init(session
);
508 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
510 uint64_t session_idx
;
512 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
514 struct lttng_live_session
*session
=
515 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
516 session
->new_streams_needed
= true;
521 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
523 uint64_t session_idx
, trace_idx
;
525 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
527 struct lttng_live_session
*session
=
528 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
529 session
->new_streams_needed
= true;
530 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
532 struct lttng_live_trace
*trace
=
533 g_ptr_array_index(session
->traces
, trace_idx
);
534 trace
->new_metadata_needed
= true;
540 enum lttng_live_iterator_status
541 lttng_live_iterator_handle_new_streams_and_metadata(
542 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
544 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
545 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
546 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
547 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
548 struct lttng_live_session
*session
;
549 enum session_not_found_action sess_not_found_act
=
550 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
553 * In a remotely distant future, we could add a "new
554 * session" flag to the protocol, which would tell us that we
555 * need to query for new sessions even though we have sessions
558 if (lttng_live_msg_iter
->sessions
->len
== 0) {
559 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
560 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
564 * Retry to create a viewer session for the requested
567 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
568 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
569 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
570 "Error creating LTTng live viewer session");
576 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
578 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
580 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
582 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
584 case LTTNG_LIVE_ITERATOR_STATUS_END
:
585 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
590 if (!session
->closed
) {
591 nr_sessions_opened
++;
595 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
596 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
597 nr_sessions_opened
== 0) {
598 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
604 enum lttng_live_iterator_status
emit_inactivity_message(
605 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
606 struct lttng_live_stream_iterator
*stream_iter
,
607 bt_message
**message
, uint64_t timestamp
)
609 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
610 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
611 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
612 bt_message
*msg
= NULL
;
614 BT_ASSERT(stream_iter
->trace
->clock_class
);
616 msg
= bt_message_message_iterator_inactivity_create(
617 lttng_live_msg_iter
->self_msg_iter
,
618 stream_iter
->trace
->clock_class
, timestamp
);
620 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
621 "Error emitting message iterator inactivity message");
630 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
631 bt_message_put_ref(msg
);
636 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
637 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
638 struct lttng_live_stream_iterator
*lttng_live_stream
,
639 bt_message
**message
)
641 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
643 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
644 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
647 if (lttng_live_stream
->current_inactivity_ts
==
648 lttng_live_stream
->last_inactivity_ts
) {
649 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
650 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
654 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
655 message
, lttng_live_stream
->current_inactivity_ts
);
657 lttng_live_stream
->last_inactivity_ts
=
658 lttng_live_stream
->current_inactivity_ts
;
664 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
665 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
666 const bt_message
*msg
, int64_t last_msg_ts_ns
,
669 const bt_clock_class
*clock_class
= NULL
;
670 const bt_clock_snapshot
*clock_snapshot
= NULL
;
672 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
673 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
676 BT_ASSERT_DBG(ts_ns
);
678 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
679 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
682 switch (bt_message_get_type(msg
)) {
683 case BT_MESSAGE_TYPE_EVENT
:
684 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
686 BT_ASSERT_DBG(clock_class
);
688 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
691 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
692 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
694 BT_ASSERT(clock_class
);
696 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
699 case BT_MESSAGE_TYPE_PACKET_END
:
700 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
702 BT_ASSERT(clock_class
);
704 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
707 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
708 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
710 BT_ASSERT(clock_class
);
712 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
715 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
716 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
718 BT_ASSERT(clock_class
);
720 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
723 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
724 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
728 /* All the other messages have a higher priority */
729 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
730 *ts_ns
= last_msg_ts_ns
;
734 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
735 BT_ASSERT_DBG(clock_class
);
737 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
739 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
740 "Cannot get nanoseconds from Epoch of clock snapshot: "
741 "clock-snapshot-addr=%p", clock_snapshot
);
752 BT_COMP_LOGD("Found message's timestamp: "
753 "iter-data-addr=%p, msg-addr=%p, "
754 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
755 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
762 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
763 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
764 struct lttng_live_stream_iterator
*lttng_live_stream
,
765 bt_message
**message
)
767 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
768 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
769 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
770 enum bt_msg_iter_status status
;
771 uint64_t session_idx
, trace_idx
;
773 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
775 struct lttng_live_session
*session
=
776 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
778 if (session
->new_streams_needed
) {
779 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
782 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
784 struct lttng_live_trace
*trace
=
785 g_ptr_array_index(session
->traces
, trace_idx
);
786 if (trace
->new_metadata_needed
) {
787 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
793 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
794 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
798 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
799 lttng_live_msg_iter
->self_msg_iter
, message
);
801 case BT_MSG_ITER_STATUS_EOF
:
802 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
804 case BT_MSG_ITER_STATUS_OK
:
805 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
807 case BT_MSG_ITER_STATUS_AGAIN
:
809 * Continue immediately (end of packet). The next
810 * get_index may return AGAIN to delay the following
813 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
815 case BT_MSG_ITER_STATUS_INVAL
:
816 /* No argument provided by the user, so don't return INVAL. */
817 case BT_MSG_ITER_STATUS_ERROR
:
819 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
820 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
821 "CTF message iterator return an error or failed: "
822 "msg_iter=%p", lttng_live_stream
->msg_iter
);
831 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
832 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
833 struct lttng_live_stream_iterator
*stream_iter
,
834 bt_message
**curr_msg
)
836 enum lttng_live_iterator_status live_status
=
837 LTTNG_LIVE_ITERATOR_STATUS_OK
;
838 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
839 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
841 * The viewer has hung up on us so we are closing the stream. The
842 * `bt_msg_iter` should simply realize that it needs to close the
843 * stream properly by emitting the necessary stream end message.
845 enum bt_msg_iter_status status
= bt_msg_iter_get_next_message(
846 stream_iter
->msg_iter
, lttng_live_msg_iter
->self_msg_iter
,
849 if (status
== BT_MSG_ITER_STATUS_ERROR
) {
850 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
851 "Error getting the next message from CTF message iterator");
852 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
856 BT_ASSERT(status
== BT_MSG_ITER_STATUS_OK
);
864 * handle_no_data_streams()
866 * - for each ACTIVE_NO_DATA stream:
867 * - query relayd for stream data, or quiescence info.
868 * - if need metadata, get metadata, goto retry.
869 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
870 * - if quiescent, move to QUIESCENT streams
871 * - if fetched data, move to ACTIVE_DATA streams
872 * (at this point each stream either has data, or is quiescent)
876 * handle_new_streams_and_metadata()
877 * - query relayd for known streams, add them as ACTIVE_NO_DATA
878 * - query relayd for metadata
880 * call handle_active_no_data_streams()
882 * handle_quiescent_streams()
883 * - if at least one stream is ACTIVE_DATA:
884 * - peek stream event with lowest timestamp -> next_ts
885 * - for each quiescent stream
886 * - if next_ts >= quiescent end
887 * - set state to ACTIVE_NO_DATA
889 * - for each quiescent stream
890 * - set state to ACTIVE_NO_DATA
892 * call handle_active_no_data_streams()
894 * handle_active_data_streams()
895 * - if at least one stream is ACTIVE_DATA:
896 * - get stream event with lowest timestamp from heap
897 * - make that stream event the current message.
898 * - move this stream heap position to its next event
899 * - if we need to fetch data from relayd, move
900 * stream to ACTIVE_NO_DATA.
904 * end criterion: ctrl-c on client. If relayd exits or the session
905 * closes on the relay daemon side, we keep on waiting for streams.
906 * Eventually handle --end timestamp (also an end criterion).
908 * When disconnected from relayd: try to re-connect endlessly.
911 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
912 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
913 struct lttng_live_stream_iterator
*stream_iter
,
914 bt_message
**curr_msg
)
916 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
917 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
918 enum lttng_live_iterator_status live_status
;
920 if (stream_iter
->has_stream_hung_up
) {
922 * The stream has hung up and the stream was properly closed
923 * during the last call to the current function. Return _END
924 * status now so that this stream iterator is removed for the
925 * stream iterator list.
927 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
932 print_stream_state(stream_iter
);
933 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
934 lttng_live_msg_iter
);
935 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
938 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
939 lttng_live_msg_iter
, stream_iter
);
941 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
942 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
944 * We overwrite `live_status` since `curr_msg` is
945 * likely set to a valid message in this function.
947 live_status
= lttng_live_iterator_close_stream(
948 lttng_live_msg_iter
, stream_iter
, curr_msg
);
952 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
953 lttng_live_msg_iter
, stream_iter
, curr_msg
);
954 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
955 BT_ASSERT(!*curr_msg
);
961 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
962 lttng_live_msg_iter
, stream_iter
, curr_msg
);
963 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
964 BT_ASSERT(!*curr_msg
);
968 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
976 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
977 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
978 struct lttng_live_trace
*live_trace
,
979 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
981 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
982 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
983 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
984 enum lttng_live_iterator_status stream_iter_status
;;
985 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
986 uint64_t stream_iter_idx
;
988 BT_ASSERT_DBG(live_trace
);
989 BT_ASSERT_DBG(live_trace
->stream_iterators
);
991 * Update the current message of every stream iterators of this trace.
992 * The current msg of every stream must have a timestamp equal or
993 * larger than the last message returned by this iterator. We must
994 * ensure monotonicity.
997 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
998 bool stream_iter_is_ended
= false;
999 struct lttng_live_stream_iterator
*stream_iter
=
1000 g_ptr_array_index(live_trace
->stream_iterators
,
1004 * Find if there is are now current message for this stream
1007 while (!stream_iter
->current_msg
) {
1008 bt_message
*msg
= NULL
;
1009 int64_t curr_msg_ts_ns
= INT64_MAX
;
1010 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1011 lttng_live_msg_iter
, stream_iter
, &msg
);
1013 BT_COMP_LOGD("live stream iterator returned status :%s",
1014 print_live_iterator_status(stream_iter_status
));
1015 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1016 stream_iter_is_ended
= true;
1020 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1027 * Get the timestamp in nanoseconds from origin of this
1030 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1031 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1035 * Check if the message of the current live stream
1036 * iterator occured at the exact same time or after the
1037 * last message returned by this component's message
1038 * iterator. If not, we return an error.
1040 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1041 stream_iter
->current_msg
= msg
;
1042 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1045 * We received a message in the past. To ensure
1046 * monotonicity, we can't send it forward.
1048 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1049 "Message's timestamp is less than "
1050 "lttng-live's message iterator's last "
1051 "returned timestamp: "
1052 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1053 "last-msg-ts=%" PRId64
,
1054 lttng_live_msg_iter
, curr_msg_ts_ns
,
1055 lttng_live_msg_iter
->last_msg_ts_ns
);
1056 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1061 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1063 if (!stream_iter_is_ended
) {
1064 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1065 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1067 * Update the current best candidate message
1068 * for the stream iterator of this live trace
1069 * to be forwarded downstream.
1071 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1072 youngest_candidate_stream_iter
= stream_iter
;
1073 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1075 * Order the messages in an arbitrary but
1076 * deterministic way.
1078 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1079 int ret
= common_muxing_compare_messages(
1080 stream_iter
->current_msg
,
1081 youngest_candidate_stream_iter
->current_msg
);
1084 * The `youngest_candidate_stream_iter->current_msg`
1085 * should go first. Update the next
1086 * iterator and the current timestamp.
1088 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1089 youngest_candidate_stream_iter
= stream_iter
;
1090 } else if (ret
== 0) {
1092 * Unable to pick which one should go
1095 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1096 "stream-iter-addr=%p"
1097 "stream-iter-addr=%p",
1099 youngest_candidate_stream_iter
);
1106 * The live stream iterator has ended. That
1107 * iterator is removed from the array, but
1108 * there is no need to increment
1109 * stream_iter_idx as
1110 * g_ptr_array_remove_index_fast replaces the
1111 * removed element with the array's last
1114 g_ptr_array_remove_index_fast(
1115 live_trace
->stream_iterators
,
1120 if (youngest_candidate_stream_iter
) {
1121 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1122 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1125 * The only case where we don't have a candidate for this trace
1126 * is if we reached the end of all the iterators.
1128 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1129 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1133 return stream_iter_status
;
1137 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1138 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1139 struct lttng_live_session
*session
,
1140 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1142 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1143 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1144 enum lttng_live_iterator_status stream_iter_status
;
1145 uint64_t trace_idx
= 0;
1146 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1147 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1150 * Make sure we are attached to the session and look for new streams
1153 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1154 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1155 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1156 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1160 BT_ASSERT_DBG(session
->traces
);
1162 while (trace_idx
< session
->traces
->len
) {
1163 bool trace_is_ended
= false;
1164 struct lttng_live_stream_iterator
*stream_iter
;
1165 struct lttng_live_trace
*trace
=
1166 g_ptr_array_index(session
->traces
, trace_idx
);
1168 stream_iter_status
= next_stream_iterator_for_trace(
1169 lttng_live_msg_iter
, trace
, &stream_iter
);
1170 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1172 * All the live stream iterators for this trace are
1173 * ENDed. Remove the trace from this session.
1175 trace_is_ended
= true;
1176 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1180 if (!trace_is_ended
) {
1181 BT_ASSERT_DBG(stream_iter
);
1183 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1184 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1185 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1186 youngest_candidate_stream_iter
= stream_iter
;
1187 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1189 * Order the messages in an arbitrary but
1190 * deterministic way.
1192 int ret
= common_muxing_compare_messages(
1193 stream_iter
->current_msg
,
1194 youngest_candidate_stream_iter
->current_msg
);
1197 * The `youngest_candidate_stream_iter->current_msg`
1198 * should go first. Update the next iterator
1199 * and the current timestamp.
1201 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1202 youngest_candidate_stream_iter
= stream_iter
;
1203 } else if (ret
== 0) {
1204 /* Unable to pick which one should go first. */
1205 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1206 "stream-iter-addr=%p" "stream-iter-addr=%p",
1207 stream_iter
, youngest_candidate_stream_iter
);
1213 * trace_idx is not incremented since
1214 * g_ptr_array_remove_index_fast replaces the
1215 * element at trace_idx with the array's last element.
1217 g_ptr_array_remove_index_fast(session
->traces
,
1221 if (youngest_candidate_stream_iter
) {
1222 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1223 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1226 * The only cases where we don't have a candidate for this
1228 * 1. if we reached the end of all the iterators of all the
1229 * traces of this session,
1230 * 2. if we never had live stream iterator in the first place.
1232 * In either cases, we return END.
1234 BT_ASSERT(session
->traces
->len
== 0);
1235 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1238 return stream_iter_status
;
1242 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1246 for (i
= 0; i
< count
; i
++) {
1247 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1252 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1253 bt_self_message_iterator
*self_msg_it
,
1254 bt_message_array_const msgs
, uint64_t capacity
,
1257 bt_component_class_message_iterator_next_method_status status
;
1258 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1259 bt_self_message_iterator_get_data(self_msg_it
);
1260 struct lttng_live_component
*lttng_live
=
1261 lttng_live_msg_iter
->lttng_live_comp
;
1262 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1263 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1264 enum lttng_live_iterator_status stream_iter_status
;
1265 uint64_t session_idx
;
1269 BT_ASSERT_DBG(lttng_live_msg_iter
);
1272 * Clear all the invalid message reference that might be left over in
1275 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1278 * If no session are exposed on the relay found at the url provided by
1279 * the user, session count will be 0. In this case, we return status
1280 * end to return gracefully.
1282 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1283 if (lttng_live
->params
.sess_not_found_act
!=
1284 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1285 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1289 * The are no more active session for this session
1290 * name. Retry to create a viewer session for the
1291 * requested session name.
1293 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1294 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1300 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1301 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1305 * Here the muxing of message is done.
1307 * We need to iterate over all the streams of all the traces of all the
1308 * viewer sessions in order to get the message with the smallest
1309 * timestamp. In this case, a session is a viewer session and there is
1310 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1311 * kernel). Each viewer session can have multiple traces, for example,
1312 * 64bit UST viewer sessions could have multiple per-pid traces.
1314 * We iterate over the streams of each traces to update and see what is
1315 * their next message's timestamp. From those timestamps, we select the
1316 * message with the smallest timestamp as the best candidate message
1317 * for that trace and do the same thing across all the sessions.
1319 * We then compare the timestamp of best candidate message of all the
1320 * sessions to pick the message with the smallest timestamp and we
1323 while (*count
< capacity
) {
1324 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1325 *candidate_stream_iter
= NULL
;
1326 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1328 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1330 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1331 struct lttng_live_session
*session
=
1332 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1335 /* Find the best candidate message to send downstream. */
1336 stream_iter_status
= next_stream_iterator_for_session(
1337 lttng_live_msg_iter
, session
,
1338 &candidate_stream_iter
);
1340 /* If we receive an END status, it means that either:
1341 * - Those traces never had active streams (UST with no
1342 * data produced yet),
1343 * - All live stream iterators have ENDed.*/
1344 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1345 if (session
->closed
&& session
->traces
->len
== 0) {
1347 * Remove the session from the list.
1348 * session_idx is not modified since
1349 * g_ptr_array_remove_index_fast
1350 * replaces the the removed element with
1351 * the array's last element.
1353 g_ptr_array_remove_index_fast(
1354 lttng_live_msg_iter
->sessions
,
1362 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1366 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1367 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1368 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1369 youngest_stream_iter
= candidate_stream_iter
;
1370 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1372 * The currently selected message to be sent
1373 * downstream next has the exact same timestamp
1374 * that of the current candidate message. We
1375 * must break the tie in a predictable manner.
1377 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1379 * Order the messages in an arbitrary but
1380 * deterministic way.
1382 int ret
= common_muxing_compare_messages(
1383 candidate_stream_iter
->current_msg
,
1384 youngest_stream_iter
->current_msg
);
1387 * The `candidate_stream_iter->current_msg`
1388 * should go first. Update the next
1389 * iterator and the current timestamp.
1391 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1392 youngest_stream_iter
= candidate_stream_iter
;
1393 } else if (ret
== 0) {
1394 /* Unable to pick which one should go first. */
1395 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1396 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1397 youngest_stream_iter
, candidate_stream_iter
);
1404 if (!youngest_stream_iter
) {
1405 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1409 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1410 /* Ensure monotonicity. */
1411 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1412 youngest_stream_iter
->current_msg_ts_ns
);
1415 * Insert the next message to the message batch. This will set
1416 * stream iterator current messsage to NULL so that next time
1417 * we fetch the next message of that stream iterator
1419 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1422 /* Update the last timestamp in nanoseconds sent downstream. */
1423 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1424 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1426 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1429 switch (stream_iter_status
) {
1430 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1431 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1434 * We received a again status but we have some messages
1435 * to send downstream. We send them and return OK for
1436 * now. On the next call we return again if there are
1437 * still no new message to send.
1439 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1441 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1444 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1445 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1447 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1448 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1450 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1451 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1452 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1453 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1454 /* Put all existing messages on error. */
1455 put_messages(msgs
, *count
);
1466 bt_component_class_message_iterator_initialize_method_status
lttng_live_msg_iter_init(
1467 bt_self_message_iterator
*self_msg_it
,
1468 bt_self_message_iterator_configuration
*config
,
1469 bt_self_component_source
*self_comp_src
,
1470 bt_self_component_port_output
*self_port
)
1472 bt_component_class_message_iterator_initialize_method_status ret
=
1473 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
1474 bt_self_component
*self_comp
=
1475 bt_self_component_source_as_self_component(self_comp_src
);
1476 struct lttng_live_component
*lttng_live
;
1477 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1478 bt_logging_level log_level
;
1480 BT_ASSERT(self_msg_it
);
1482 lttng_live
= bt_self_component_get_data(self_comp
);
1483 log_level
= lttng_live
->log_level
;
1484 self_comp
= lttng_live
->self_comp
;
1486 /* There can be only one downstream iterator at the same time. */
1487 BT_ASSERT(!lttng_live
->has_msg_iter
);
1488 lttng_live
->has_msg_iter
= true;
1490 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1491 if (!lttng_live_msg_iter
) {
1492 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1496 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1497 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1498 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1499 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1501 lttng_live_msg_iter
->active_stream_iter
= 0;
1502 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1503 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1504 (GDestroyNotify
) lttng_live_destroy_session
);
1505 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1507 lttng_live_msg_iter
->viewer_connection
=
1508 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1509 lttng_live_msg_iter
, self_comp
, NULL
, log_level
);
1510 if (!lttng_live_msg_iter
->viewer_connection
) {
1514 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1517 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1518 switch (lttng_live
->params
.sess_not_found_act
) {
1519 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1520 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1521 "session. Keep trying to connect because of "
1522 "%s=\"%s\" component parameter: url=\"%s\"",
1523 SESS_NOT_FOUND_ACTION_PARAM
,
1524 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1525 lttng_live
->params
.url
->str
);
1527 case SESSION_NOT_FOUND_ACTION_FAIL
:
1528 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Unable to connect to the requested live viewer "
1529 "session. Fail the message iterator"
1530 "initialization because of %s=\"%s\" "
1531 "component parameter: url =\"%s\"",
1532 SESS_NOT_FOUND_ACTION_PARAM
,
1533 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1534 lttng_live
->params
.url
->str
);
1536 case SESSION_NOT_FOUND_ACTION_END
:
1537 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1538 "session. End gracefully at the first _next() "
1539 "call because of %s=\"%s\" component parameter: "
1540 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1541 SESS_NOT_FOUND_ACTION_END_STR
,
1542 lttng_live
->params
.url
->str
);
1549 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1553 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1554 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1559 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1560 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1561 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1565 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1566 const bt_value
*params
, const bt_value
**result
,
1567 bt_self_component_class
*self_comp_class
,
1568 bt_logging_level log_level
)
1570 bt_component_class_query_method_status status
;
1571 const bt_value
*url_value
= NULL
;
1573 struct live_viewer_connection
*viewer_connection
= NULL
;
1574 enum bt_param_validation_status validation_status
;
1575 gchar
*validate_error
= NULL
;
1577 validation_status
= bt_param_validation_validate(params
,
1578 list_sessions_params
, &validate_error
);
1579 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1580 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1582 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1583 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1584 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1589 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1590 url
= bt_value_string_get(url_value
);
1592 viewer_connection
= live_viewer_connection_create(url
, true, NULL
,
1593 NULL
, self_comp_class
, log_level
);
1594 if (!viewer_connection
) {
1595 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1596 "Failed to create viewer connection");
1597 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1601 status
= live_viewer_connection_list_sessions(viewer_connection
,
1603 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1604 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1605 "Failed to list viewer sessions");
1612 BT_VALUE_PUT_REF_AND_RESET(*result
);
1615 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1619 if (viewer_connection
) {
1620 live_viewer_connection_destroy(viewer_connection
);
1623 g_free(validate_error
);
1629 bt_component_class_query_method_status
lttng_live_query_support_info(
1630 const bt_value
*params
, const bt_value
**result
,
1631 bt_self_component_class
*self_comp_class
,
1632 bt_logging_level log_level
)
1634 bt_component_class_query_method_status status
=
1635 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1636 const bt_value
*input_type_value
;
1637 const bt_value
*input_value
;
1639 struct bt_common_lttng_live_url_parts parts
= { 0 };
1641 /* Used by the logging macros */
1642 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1645 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1647 if (!input_type_value
) {
1648 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1649 "Missing expected `type` parameter.");
1653 if (!bt_value_is_string(input_type_value
)) {
1654 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1655 "`type` parameter is not a string value.");
1659 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1660 /* We don't handle file system paths */
1664 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1666 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1667 "Missing expected `input` parameter.");
1671 if (!bt_value_is_string(input_value
)) {
1672 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1673 "`input` parameter is not a string value.");
1677 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1679 if (parts
.session_name
) {
1681 * Looks pretty much like an LTTng live URL: we got the
1682 * session name part, which forms a complete URL.
1688 *result
= bt_value_real_create_init(weight
);
1690 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1698 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1701 BT_ASSERT(!*result
);
1704 bt_common_destroy_lttng_live_url_parts(&parts
);
1709 bt_component_class_query_method_status
lttng_live_query(
1710 bt_self_component_class_source
*comp_class
,
1711 bt_private_query_executor
*priv_query_exec
,
1712 const char *object
, const bt_value
*params
,
1713 __attribute__((unused
)) void *method_data
,
1714 const bt_value
**result
)
1716 bt_component_class_query_method_status status
=
1717 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1718 bt_self_component
*self_comp
= NULL
;
1719 bt_self_component_class
*self_comp_class
=
1720 bt_self_component_class_source_as_self_component_class(comp_class
);
1721 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1722 bt_private_query_executor_as_query_executor_const(
1725 if (strcmp(object
, "sessions") == 0) {
1726 status
= lttng_live_query_list_sessions(params
, result
,
1727 self_comp_class
, log_level
);
1728 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1729 status
= lttng_live_query_support_info(params
, result
,
1730 self_comp_class
, log_level
);
1732 BT_COMP_LOGI("Unknown query object `%s`", object
);
1733 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1742 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1747 if (lttng_live
->params
.url
) {
1748 g_string_free(lttng_live
->params
.url
, TRUE
);
1754 void lttng_live_component_finalize(bt_self_component_source
*component
)
1756 void *data
= bt_self_component_get_data(
1757 bt_self_component_source_as_self_component(component
));
1762 lttng_live_component_destroy_data(data
);
1766 enum session_not_found_action
parse_session_not_found_action_param(
1767 const bt_value
*no_session_param
)
1769 enum session_not_found_action action
;
1770 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1772 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1773 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1774 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1775 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1777 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1778 action
= SESSION_NOT_FOUND_ACTION_END
;
1784 static struct bt_param_validation_value_descr inputs_elem_descr
= {
1785 .type
= BT_VALUE_TYPE_STRING
,
1788 static const char *sess_not_found_action_choices
[] = {
1789 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1790 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1791 SESS_NOT_FOUND_ACTION_END_STR
,
1794 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1795 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
1798 .element_type
= &inputs_elem_descr
,
1800 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
1801 .choices
= sess_not_found_action_choices
,
1803 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1807 bt_component_class_initialize_method_status
lttng_live_component_create(
1808 const bt_value
*params
,
1809 bt_logging_level log_level
,
1810 bt_self_component
*self_comp
,
1811 struct lttng_live_component
**component
)
1813 struct lttng_live_component
*lttng_live
= NULL
;
1814 const bt_value
*inputs_value
;
1815 const bt_value
*url_value
;
1816 const bt_value
*value
;
1818 enum bt_param_validation_status validation_status
;
1819 gchar
*validation_error
= NULL
;
1820 bt_component_class_initialize_method_status status
;
1822 validation_status
= bt_param_validation_validate(params
, params_descr
,
1824 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1825 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1827 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1828 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
1829 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1833 lttng_live
= g_new0(struct lttng_live_component
, 1);
1835 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1838 lttng_live
->log_level
= log_level
;
1839 lttng_live
->self_comp
= self_comp
;
1840 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1841 lttng_live
->has_msg_iter
= false;
1844 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
1846 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
1847 url
= bt_value_string_get(url_value
);
1849 lttng_live
->params
.url
= g_string_new(url
);
1850 if (!lttng_live
->params
.url
) {
1851 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1855 value
= bt_value_map_borrow_entry_value_const(params
,
1856 SESS_NOT_FOUND_ACTION_PARAM
);
1858 lttng_live
->params
.sess_not_found_act
=
1859 parse_session_not_found_action_param(value
);
1861 BT_COMP_LOGI("Optional `%s` parameter is missing: "
1862 "defaulting to `%s`.",
1863 SESS_NOT_FOUND_ACTION_PARAM
,
1864 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1865 lttng_live
->params
.sess_not_found_act
=
1866 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1869 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1873 lttng_live_component_destroy_data(lttng_live
);
1876 g_free(validation_error
);
1878 *component
= lttng_live
;
1883 bt_component_class_initialize_method_status
lttng_live_component_init(
1884 bt_self_component_source
*self_comp_src
,
1885 bt_self_component_source_configuration
*config
,
1886 const bt_value
*params
,
1887 __attribute__((unused
)) void *init_method_data
)
1889 struct lttng_live_component
*lttng_live
;
1890 bt_component_class_initialize_method_status ret
;
1891 bt_self_component
*self_comp
=
1892 bt_self_component_source_as_self_component(self_comp_src
);
1893 bt_logging_level log_level
= bt_component_get_logging_level(
1894 bt_self_component_as_component(self_comp
));
1895 bt_self_component_add_port_status add_port_status
;
1897 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
1898 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1902 add_port_status
= bt_self_component_source_add_output_port(
1903 self_comp_src
, "out", NULL
, NULL
);
1904 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1905 ret
= (int) add_port_status
;
1909 bt_self_component_set_data(self_comp
, lttng_live
);
1913 lttng_live_component_destroy_data(lttng_live
);