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"
40 #include "common/assert.h"
41 #include <babeltrace2/babeltrace.h>
42 #include "compat/compiler.h"
43 #include <babeltrace2/types.h>
45 #include "plugins/common/muxing/muxing.h"
46 #include "plugins/common/param-validation/param-validation.h"
48 #include "data-stream.h"
50 #include "lttng-live.h"
52 #define MAX_QUERY_SIZE (256*1024)
53 #define URL_PARAM "url"
54 #define INPUTS_PARAM "inputs"
55 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
56 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
57 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
58 #define SESS_NOT_FOUND_ACTION_END_STR "end"
60 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
63 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
66 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
67 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
68 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
69 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
70 case LTTNG_LIVE_ITERATOR_STATUS_END
:
71 return "LTTNG_LIVE_ITERATOR_STATUS_END";
72 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
73 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
74 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
75 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
76 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
77 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
78 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
79 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
80 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
81 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
88 const char *print_state(struct lttng_live_stream_iterator
*s
)
91 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
92 return "ACTIVE_NO_DATA";
93 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
94 return "QUIESCENT_NO_DATA";
95 case LTTNG_LIVE_STREAM_QUIESCENT
:
97 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
99 case LTTNG_LIVE_STREAM_EOF
:
106 #define print_stream_state(live_stream_iter) \
108 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
109 ", curr_inact_ts %" PRId64, \
110 print_state(live_stream_iter), \
111 live_stream_iter->last_inactivity_ts, \
112 live_stream_iter->current_inactivity_ts); \
116 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
125 ret
= bt_self_message_iterator_is_interrupted(
126 msg_iter
->self_msg_iter
);
133 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
137 struct lttng_live_trace
*ret_trace
= NULL
;
139 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
140 struct lttng_live_trace
*trace
=
141 g_ptr_array_index(session
->traces
, trace_idx
);
142 if (trace
->id
== trace_id
) {
153 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
155 bt_logging_level log_level
= trace
->log_level
;
156 bt_self_component
*self_comp
= trace
->self_comp
;
158 BT_COMP_LOGD("Destroy lttng_live_trace");
160 BT_ASSERT(trace
->stream_iterators
);
161 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
163 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
164 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
166 lttng_live_metadata_fini(trace
);
171 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
174 struct lttng_live_trace
*trace
= NULL
;
175 bt_logging_level log_level
= session
->log_level
;
176 bt_self_component
*self_comp
= session
->self_comp
;
178 trace
= g_new0(struct lttng_live_trace
, 1);
182 trace
->log_level
= session
->log_level
;
183 trace
->self_comp
= session
->self_comp
;
184 trace
->session
= session
;
185 trace
->id
= trace_id
;
186 trace
->trace_class
= NULL
;
188 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
189 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
190 BT_ASSERT(trace
->stream_iterators
);
191 trace
->new_metadata_needed
= true;
192 g_ptr_array_add(session
->traces
, trace
);
194 BT_COMP_LOGI("Create trace");
204 struct lttng_live_trace
*lttng_live_borrow_trace(
205 struct lttng_live_session
*session
, uint64_t trace_id
)
207 struct lttng_live_trace
*trace
;
209 trace
= lttng_live_find_trace(session
, trace_id
);
214 /* The session is the owner of the newly created trace. */
215 trace
= lttng_live_create_trace(session
, trace_id
);
222 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
223 uint64_t session_id
, const char *hostname
,
224 const char *session_name
)
227 struct lttng_live_session
*session
;
228 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
229 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
231 session
= g_new0(struct lttng_live_session
, 1);
236 session
->log_level
= lttng_live_msg_iter
->log_level
;
237 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
238 session
->id
= session_id
;
239 session
->traces
= g_ptr_array_new_with_free_func(
240 (GDestroyNotify
) lttng_live_destroy_trace
);
241 BT_ASSERT(session
->traces
);
242 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
243 session
->new_streams_needed
= true;
244 session
->hostname
= g_string_new(hostname
);
245 BT_ASSERT(session
->hostname
);
247 session
->session_name
= g_string_new(session_name
);
248 BT_ASSERT(session
->session_name
);
250 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
251 session
->id
, hostname
, session_name
);
252 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
255 BT_COMP_LOGE("Error adding session");
263 void lttng_live_destroy_session(struct lttng_live_session
*session
)
265 bt_logging_level log_level
;
266 bt_self_component
*self_comp
;
272 log_level
= session
->log_level
;
273 self_comp
= session
->self_comp
;
274 BT_COMP_LOGD("Destroy lttng live session");
275 if (session
->id
!= -1ULL) {
276 if (lttng_live_detach_session(session
)) {
277 if (!lttng_live_graph_is_canceled(
278 session
->lttng_live_msg_iter
)) {
279 /* Old relayd cannot detach sessions. */
280 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
287 if (session
->traces
) {
288 g_ptr_array_free(session
->traces
, TRUE
);
291 if (session
->hostname
) {
292 g_string_free(session
->hostname
, TRUE
);
294 if (session
->session_name
) {
295 g_string_free(session
->session_name
, TRUE
);
304 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
306 if (!lttng_live_msg_iter
) {
310 if (lttng_live_msg_iter
->sessions
) {
311 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
314 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
315 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
316 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
318 /* All stream iterators must be destroyed at this point. */
319 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
320 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
322 g_free(lttng_live_msg_iter
);
329 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
331 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
333 BT_ASSERT(self_msg_iter
);
335 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
336 BT_ASSERT(lttng_live_msg_iter
);
337 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
341 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
342 struct lttng_live_stream_iterator
*lttng_live_stream
)
344 bt_logging_level log_level
= lttng_live_stream
->log_level
;
345 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
347 switch (lttng_live_stream
->state
) {
348 case LTTNG_LIVE_STREAM_QUIESCENT
:
349 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
351 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
353 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
355 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
357 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
359 case LTTNG_LIVE_STREAM_EOF
:
362 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
366 * For active no data stream, fetch next data. It can be either:
367 * - quiescent: need to put it in the prio heap at quiescent end
369 * - have data: need to wire up first event into the prio heap,
370 * - have no data on this stream at this point: need to retry (AGAIN) or
374 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
375 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
376 struct lttng_live_stream_iterator
*lttng_live_stream
)
378 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
379 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
380 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
381 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
382 struct packet_index index
;
384 if (lttng_live_stream
->trace
->new_metadata_needed
) {
385 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
388 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
389 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
392 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
393 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
396 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
398 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
401 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
402 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
403 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
404 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
406 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
407 last_inact_ts
== curr_inact_ts
) {
408 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
409 print_stream_state(lttng_live_stream
);
411 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
415 lttng_live_stream
->base_offset
= index
.offset
;
416 lttng_live_stream
->offset
= index
.offset
;
417 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
419 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
420 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
426 * Creation of the message requires the ctf trace class to be created
427 * beforehand, but the live protocol gives us all streams (including metadata)
428 * at once. So we split it in three steps: getting streams, getting metadata
429 * (which creates the ctf trace class), and then creating the per-stream
433 enum lttng_live_iterator_status
lttng_live_get_session(
434 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
435 struct lttng_live_session
*session
)
437 enum lttng_live_iterator_status status
;
441 if (!session
->attached
) {
442 ret
= lttng_live_attach_session(session
);
444 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
445 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
447 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
453 status
= lttng_live_get_new_streams(session
);
454 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
455 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
458 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
459 struct lttng_live_trace
*trace
=
460 g_ptr_array_index(session
->traces
, trace_idx
);
462 status
= lttng_live_metadata_update(trace
);
463 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
464 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
468 status
= lttng_live_lazy_msg_init(session
);
475 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
477 uint64_t session_idx
;
479 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
481 struct lttng_live_session
*session
=
482 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
483 session
->new_streams_needed
= true;
488 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
490 uint64_t session_idx
, trace_idx
;
492 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
494 struct lttng_live_session
*session
=
495 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
496 session
->new_streams_needed
= true;
497 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
499 struct lttng_live_trace
*trace
=
500 g_ptr_array_index(session
->traces
, trace_idx
);
501 trace
->new_metadata_needed
= true;
507 enum lttng_live_iterator_status
508 lttng_live_iterator_handle_new_streams_and_metadata(
509 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
511 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
512 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
513 struct lttng_live_session
*session
;
514 enum session_not_found_action sess_not_found_act
=
515 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
518 * In a remotely distant future, we could add a "new
519 * session" flag to the protocol, which would tell us that we
520 * need to query for new sessions even though we have sessions
523 if (lttng_live_msg_iter
->sessions
->len
== 0) {
524 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
525 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
529 * Retry to create a viewer session for the requested
532 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
533 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
539 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
541 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
543 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
545 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
547 case LTTNG_LIVE_ITERATOR_STATUS_END
:
548 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
553 if (!session
->closed
) {
554 nr_sessions_opened
++;
558 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
559 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
560 nr_sessions_opened
== 0) {
561 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
567 enum lttng_live_iterator_status
emit_inactivity_message(
568 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
569 struct lttng_live_stream_iterator
*stream_iter
,
570 bt_message
**message
, uint64_t timestamp
)
572 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
573 bt_message
*msg
= NULL
;
575 BT_ASSERT(stream_iter
->trace
->clock_class
);
577 msg
= bt_message_message_iterator_inactivity_create(
578 lttng_live_msg_iter
->self_msg_iter
,
579 stream_iter
->trace
->clock_class
, timestamp
);
589 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
590 bt_message_put_ref(msg
);
595 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
596 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
597 struct lttng_live_stream_iterator
*lttng_live_stream
,
598 bt_message
**message
)
600 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
602 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
603 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
606 if (lttng_live_stream
->current_inactivity_ts
==
607 lttng_live_stream
->last_inactivity_ts
) {
608 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
609 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
613 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
614 message
, lttng_live_stream
->current_inactivity_ts
);
616 lttng_live_stream
->last_inactivity_ts
=
617 lttng_live_stream
->current_inactivity_ts
;
623 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
624 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
625 const bt_message
*msg
, int64_t last_msg_ts_ns
,
628 const bt_clock_class
*clock_class
= NULL
;
629 const bt_clock_snapshot
*clock_snapshot
= NULL
;
631 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
632 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
635 BT_ASSERT_DBG(ts_ns
);
637 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
638 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
641 switch (bt_message_get_type(msg
)) {
642 case BT_MESSAGE_TYPE_EVENT
:
643 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
645 BT_ASSERT_DBG(clock_class
);
647 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
650 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
651 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
653 BT_ASSERT(clock_class
);
655 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
658 case BT_MESSAGE_TYPE_PACKET_END
:
659 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
661 BT_ASSERT(clock_class
);
663 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
666 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
667 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
669 BT_ASSERT(clock_class
);
671 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
674 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
675 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
677 BT_ASSERT(clock_class
);
679 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
682 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
683 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
687 /* All the other messages have a higher priority */
688 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
689 *ts_ns
= last_msg_ts_ns
;
693 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
694 BT_ASSERT_DBG(clock_class
);
696 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
698 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
699 "clock-snapshot-addr=%p", clock_snapshot
);
710 BT_COMP_LOGD("Found message's timestamp: "
711 "iter-data-addr=%p, msg-addr=%p, "
712 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
713 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
720 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
721 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
722 struct lttng_live_stream_iterator
*lttng_live_stream
,
723 bt_message
**message
)
725 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
726 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
727 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
728 enum bt_msg_iter_status status
;
729 uint64_t session_idx
, trace_idx
;
731 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
733 struct lttng_live_session
*session
=
734 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
736 if (session
->new_streams_needed
) {
737 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
740 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
742 struct lttng_live_trace
*trace
=
743 g_ptr_array_index(session
->traces
, trace_idx
);
744 if (trace
->new_metadata_needed
) {
745 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
751 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
752 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
756 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
757 lttng_live_msg_iter
->self_msg_iter
, message
);
759 case BT_MSG_ITER_STATUS_EOF
:
760 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
762 case BT_MSG_ITER_STATUS_OK
:
763 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
765 case BT_MSG_ITER_STATUS_AGAIN
:
767 * Continue immediately (end of packet). The next
768 * get_index may return AGAIN to delay the following
771 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
773 case BT_MSG_ITER_STATUS_INVAL
:
774 /* No argument provided by the user, so don't return INVAL. */
775 case BT_MSG_ITER_STATUS_ERROR
:
777 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
778 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
779 lttng_live_stream
->msg_iter
);
788 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
789 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
790 struct lttng_live_stream_iterator
*stream_iter
,
791 bt_message
**curr_msg
)
793 enum lttng_live_iterator_status live_status
=
794 LTTNG_LIVE_ITERATOR_STATUS_OK
;
796 * The viewer has hung up on us so we are closing the stream. The
797 * `bt_msg_iter` should simply realize that it needs to close the
798 * stream properly by emitting the necessary stream end message.
800 enum bt_msg_iter_status status
= bt_msg_iter_get_next_message(
801 stream_iter
->msg_iter
, lttng_live_msg_iter
->self_msg_iter
,
804 if (status
== BT_MSG_ITER_STATUS_ERROR
) {
805 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
809 BT_ASSERT(status
== BT_MSG_ITER_STATUS_OK
);
817 * handle_no_data_streams()
819 * - for each ACTIVE_NO_DATA stream:
820 * - query relayd for stream data, or quiescence info.
821 * - if need metadata, get metadata, goto retry.
822 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
823 * - if quiescent, move to QUIESCENT streams
824 * - if fetched data, move to ACTIVE_DATA streams
825 * (at this point each stream either has data, or is quiescent)
829 * handle_new_streams_and_metadata()
830 * - query relayd for known streams, add them as ACTIVE_NO_DATA
831 * - query relayd for metadata
833 * call handle_active_no_data_streams()
835 * handle_quiescent_streams()
836 * - if at least one stream is ACTIVE_DATA:
837 * - peek stream event with lowest timestamp -> next_ts
838 * - for each quiescent stream
839 * - if next_ts >= quiescent end
840 * - set state to ACTIVE_NO_DATA
842 * - for each quiescent stream
843 * - set state to ACTIVE_NO_DATA
845 * call handle_active_no_data_streams()
847 * handle_active_data_streams()
848 * - if at least one stream is ACTIVE_DATA:
849 * - get stream event with lowest timestamp from heap
850 * - make that stream event the current message.
851 * - move this stream heap position to its next event
852 * - if we need to fetch data from relayd, move
853 * stream to ACTIVE_NO_DATA.
857 * end criterion: ctrl-c on client. If relayd exits or the session
858 * closes on the relay daemon side, we keep on waiting for streams.
859 * Eventually handle --end timestamp (also an end criterion).
861 * When disconnected from relayd: try to re-connect endlessly.
864 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
865 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
866 struct lttng_live_stream_iterator
*stream_iter
,
867 bt_message
**curr_msg
)
869 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
870 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
871 enum lttng_live_iterator_status live_status
;
873 if (stream_iter
->has_stream_hung_up
) {
875 * The stream has hung up and the stream was properly closed
876 * during the last call to the current function. Return _END
877 * status now so that this stream iterator is removed for the
878 * stream iterator list.
880 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
885 print_stream_state(stream_iter
);
886 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
887 lttng_live_msg_iter
);
888 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
891 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
892 lttng_live_msg_iter
, stream_iter
);
894 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
895 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
897 * We overwrite `live_status` since `curr_msg` is
898 * likely set to a valid message in this function.
900 live_status
= lttng_live_iterator_close_stream(
901 lttng_live_msg_iter
, stream_iter
, curr_msg
);
905 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
906 lttng_live_msg_iter
, stream_iter
, curr_msg
);
907 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
908 BT_ASSERT(!*curr_msg
);
914 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
915 lttng_live_msg_iter
, stream_iter
, curr_msg
);
916 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
917 BT_ASSERT(!*curr_msg
);
921 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
929 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
930 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
931 struct lttng_live_trace
*live_trace
,
932 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
934 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
935 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
936 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
937 enum lttng_live_iterator_status stream_iter_status
;;
938 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
939 uint64_t stream_iter_idx
;
941 BT_ASSERT_DBG(live_trace
);
942 BT_ASSERT_DBG(live_trace
->stream_iterators
);
944 * Update the current message of every stream iterators of this trace.
945 * The current msg of every stream must have a timestamp equal or
946 * larger than the last message returned by this iterator. We must
947 * ensure monotonicity.
950 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
951 bool stream_iter_is_ended
= false;
952 struct lttng_live_stream_iterator
*stream_iter
=
953 g_ptr_array_index(live_trace
->stream_iterators
,
957 * Find if there is are now current message for this stream
960 while (!stream_iter
->current_msg
) {
961 bt_message
*msg
= NULL
;
962 int64_t curr_msg_ts_ns
= INT64_MAX
;
963 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
964 lttng_live_msg_iter
, stream_iter
, &msg
);
966 BT_COMP_LOGD("live stream iterator returned status :%s",
967 print_live_iterator_status(stream_iter_status
));
968 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
969 stream_iter_is_ended
= true;
973 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
980 * Get the timestamp in nanoseconds from origin of this
983 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
984 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
988 * Check if the message of the current live stream
989 * iterator occured at the exact same time or after the
990 * last message returned by this component's message
991 * iterator. If not, we return an error.
993 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
994 stream_iter
->current_msg
= msg
;
995 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
998 * We received a message in the past. To ensure
999 * monotonicity, we can't send it forward.
1001 BT_COMP_LOGE("Message's timestamp is less than "
1002 "lttng-live's message iterator's last "
1003 "returned timestamp: "
1004 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1005 "last-msg-ts=%" PRId64
,
1006 lttng_live_msg_iter
, curr_msg_ts_ns
,
1007 lttng_live_msg_iter
->last_msg_ts_ns
);
1008 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1013 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1015 if (!stream_iter_is_ended
) {
1016 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1017 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1019 * Update the current best candidate message
1020 * for the stream iterator of this live trace
1021 * to be forwarded downstream.
1023 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1024 youngest_candidate_stream_iter
= stream_iter
;
1025 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1027 * Order the messages in an arbitrary but
1028 * deterministic way.
1030 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1031 int ret
= common_muxing_compare_messages(
1032 stream_iter
->current_msg
,
1033 youngest_candidate_stream_iter
->current_msg
);
1036 * The `youngest_candidate_stream_iter->current_msg`
1037 * should go first. Update the next
1038 * iterator and the current timestamp.
1040 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1041 youngest_candidate_stream_iter
= stream_iter
;
1042 } else if (ret
== 0) {
1044 * Unable to pick which one should go
1047 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1048 "stream-iter-addr=%p"
1049 "stream-iter-addr=%p",
1051 youngest_candidate_stream_iter
);
1058 * The live stream iterator has ended. That
1059 * iterator is removed from the array, but
1060 * there is no need to increment
1061 * stream_iter_idx as
1062 * g_ptr_array_remove_index_fast replaces the
1063 * removed element with the array's last
1066 g_ptr_array_remove_index_fast(
1067 live_trace
->stream_iterators
,
1072 if (youngest_candidate_stream_iter
) {
1073 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1074 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1077 * The only case where we don't have a candidate for this trace
1078 * is if we reached the end of all the iterators.
1080 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1081 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1085 return stream_iter_status
;
1089 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1090 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1091 struct lttng_live_session
*session
,
1092 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1094 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1095 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1096 enum lttng_live_iterator_status stream_iter_status
;
1097 uint64_t trace_idx
= 0;
1098 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1099 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1102 * Make sure we are attached to the session and look for new streams
1105 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1106 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1107 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1108 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1112 BT_ASSERT_DBG(session
->traces
);
1114 while (trace_idx
< session
->traces
->len
) {
1115 bool trace_is_ended
= false;
1116 struct lttng_live_stream_iterator
*stream_iter
;
1117 struct lttng_live_trace
*trace
=
1118 g_ptr_array_index(session
->traces
, trace_idx
);
1120 stream_iter_status
= next_stream_iterator_for_trace(
1121 lttng_live_msg_iter
, trace
, &stream_iter
);
1122 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1124 * All the live stream iterators for this trace are
1125 * ENDed. Remove the trace from this session.
1127 trace_is_ended
= true;
1128 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1132 if (!trace_is_ended
) {
1133 BT_ASSERT_DBG(stream_iter
);
1135 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1136 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1137 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1138 youngest_candidate_stream_iter
= stream_iter
;
1139 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1141 * Order the messages in an arbitrary but
1142 * deterministic way.
1144 int ret
= common_muxing_compare_messages(
1145 stream_iter
->current_msg
,
1146 youngest_candidate_stream_iter
->current_msg
);
1149 * The `youngest_candidate_stream_iter->current_msg`
1150 * should go first. Update the next iterator
1151 * and the current timestamp.
1153 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1154 youngest_candidate_stream_iter
= stream_iter
;
1155 } else if (ret
== 0) {
1156 /* Unable to pick which one should go first. */
1157 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1158 "stream-iter-addr=%p" "stream-iter-addr=%p",
1159 stream_iter
, youngest_candidate_stream_iter
);
1165 * trace_idx is not incremented since
1166 * g_ptr_array_remove_index_fast replaces the
1167 * element at trace_idx with the array's last element.
1169 g_ptr_array_remove_index_fast(session
->traces
,
1173 if (youngest_candidate_stream_iter
) {
1174 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1175 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1178 * The only cases where we don't have a candidate for this
1180 * 1. if we reached the end of all the iterators of all the
1181 * traces of this session,
1182 * 2. if we never had live stream iterator in the first place.
1184 * In either cases, we return END.
1186 BT_ASSERT(session
->traces
->len
== 0);
1187 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1190 return stream_iter_status
;
1194 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1198 for (i
= 0; i
< count
; i
++) {
1199 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1204 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1205 bt_self_message_iterator
*self_msg_it
,
1206 bt_message_array_const msgs
, uint64_t capacity
,
1209 bt_component_class_message_iterator_next_method_status status
;
1210 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1211 bt_self_message_iterator_get_data(self_msg_it
);
1212 struct lttng_live_component
*lttng_live
=
1213 lttng_live_msg_iter
->lttng_live_comp
;
1214 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1215 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1216 enum lttng_live_iterator_status stream_iter_status
;
1217 uint64_t session_idx
;
1221 BT_ASSERT_DBG(lttng_live_msg_iter
);
1224 * Clear all the invalid message reference that might be left over in
1227 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1230 * If no session are exposed on the relay found at the url provided by
1231 * the user, session count will be 0. In this case, we return status
1232 * end to return gracefully.
1234 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1235 if (lttng_live
->params
.sess_not_found_act
!=
1236 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1237 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1241 * The are no more active session for this session
1242 * name. Retry to create a viewer session for the
1243 * requested session name.
1245 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1246 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1252 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1253 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1257 * Here the muxing of message is done.
1259 * We need to iterate over all the streams of all the traces of all the
1260 * viewer sessions in order to get the message with the smallest
1261 * timestamp. In this case, a session is a viewer session and there is
1262 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1263 * kernel). Each viewer session can have multiple traces, for example,
1264 * 64bit UST viewer sessions could have multiple per-pid traces.
1266 * We iterate over the streams of each traces to update and see what is
1267 * their next message's timestamp. From those timestamps, we select the
1268 * message with the smallest timestamp as the best candidate message
1269 * for that trace and do the same thing across all the sessions.
1271 * We then compare the timestamp of best candidate message of all the
1272 * sessions to pick the message with the smallest timestamp and we
1275 while (*count
< capacity
) {
1276 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1277 *candidate_stream_iter
= NULL
;
1278 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1280 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1282 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1283 struct lttng_live_session
*session
=
1284 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1287 /* Find the best candidate message to send downstream. */
1288 stream_iter_status
= next_stream_iterator_for_session(
1289 lttng_live_msg_iter
, session
,
1290 &candidate_stream_iter
);
1292 /* If we receive an END status, it means that either:
1293 * - Those traces never had active streams (UST with no
1294 * data produced yet),
1295 * - All live stream iterators have ENDed.*/
1296 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1297 if (session
->closed
&& session
->traces
->len
== 0) {
1299 * Remove the session from the list.
1300 * session_idx is not modified since
1301 * g_ptr_array_remove_index_fast
1302 * replaces the the removed element with
1303 * the array's last element.
1305 g_ptr_array_remove_index_fast(
1306 lttng_live_msg_iter
->sessions
,
1314 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1318 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1319 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1320 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1321 youngest_stream_iter
= candidate_stream_iter
;
1322 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1324 * The currently selected message to be sent
1325 * downstream next has the exact same timestamp
1326 * that of the current candidate message. We
1327 * must break the tie in a predictable manner.
1329 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1331 * Order the messages in an arbitrary but
1332 * deterministic way.
1334 int ret
= common_muxing_compare_messages(
1335 candidate_stream_iter
->current_msg
,
1336 youngest_stream_iter
->current_msg
);
1339 * The `candidate_stream_iter->current_msg`
1340 * should go first. Update the next
1341 * iterator and the current timestamp.
1343 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1344 youngest_stream_iter
= candidate_stream_iter
;
1345 } else if (ret
== 0) {
1346 /* Unable to pick which one should go first. */
1347 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1348 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1349 youngest_stream_iter
, candidate_stream_iter
);
1356 if (!youngest_stream_iter
) {
1357 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1361 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1362 /* Ensure monotonicity. */
1363 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1364 youngest_stream_iter
->current_msg_ts_ns
);
1367 * Insert the next message to the message batch. This will set
1368 * stream iterator current messsage to NULL so that next time
1369 * we fetch the next message of that stream iterator
1371 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1374 /* Update the last timestamp in nanoseconds sent downstream. */
1375 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1376 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1378 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1381 switch (stream_iter_status
) {
1382 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1383 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1386 * We received a again status but we have some messages
1387 * to send downstream. We send them and return OK for
1388 * now. On the next call we return again if there are
1389 * still no new message to send.
1391 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1393 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1396 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1397 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1399 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1400 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1402 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1403 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1404 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1405 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1406 /* Put all existing messages on error. */
1407 put_messages(msgs
, *count
);
1418 bt_component_class_message_iterator_initialize_method_status
lttng_live_msg_iter_init(
1419 bt_self_message_iterator
*self_msg_it
,
1420 bt_self_message_iterator_configuration
*config
,
1421 bt_self_component_source
*self_comp_src
,
1422 bt_self_component_port_output
*self_port
)
1424 bt_component_class_message_iterator_initialize_method_status ret
=
1425 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
1426 bt_self_component
*self_comp
=
1427 bt_self_component_source_as_self_component(self_comp_src
);
1428 struct lttng_live_component
*lttng_live
;
1429 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1430 bt_logging_level log_level
;
1432 BT_ASSERT(self_msg_it
);
1434 lttng_live
= bt_self_component_get_data(self_comp
);
1435 log_level
= lttng_live
->log_level
;
1436 self_comp
= lttng_live
->self_comp
;
1438 /* There can be only one downstream iterator at the same time. */
1439 BT_ASSERT(!lttng_live
->has_msg_iter
);
1440 lttng_live
->has_msg_iter
= true;
1442 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1443 if (!lttng_live_msg_iter
) {
1444 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1448 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1449 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1450 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1451 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1453 lttng_live_msg_iter
->active_stream_iter
= 0;
1454 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1455 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1456 (GDestroyNotify
) lttng_live_destroy_session
);
1457 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1459 lttng_live_msg_iter
->viewer_connection
=
1460 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1461 lttng_live_msg_iter
, log_level
);
1462 if (!lttng_live_msg_iter
->viewer_connection
) {
1466 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1469 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1470 switch (lttng_live
->params
.sess_not_found_act
) {
1471 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1472 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1473 "session. Keep trying to connect because of "
1474 "%s=\"%s\" component parameter: url=\"%s\"",
1475 SESS_NOT_FOUND_ACTION_PARAM
,
1476 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1477 lttng_live
->params
.url
->str
);
1479 case SESSION_NOT_FOUND_ACTION_FAIL
:
1480 BT_COMP_LOGE("Unable to connect to the requested live viewer "
1481 "session. Fail the message iterator"
1482 "initialization because of %s=\"%s\" "
1483 "component parameter: url =\"%s\"",
1484 SESS_NOT_FOUND_ACTION_PARAM
,
1485 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1486 lttng_live
->params
.url
->str
);
1488 case SESSION_NOT_FOUND_ACTION_END
:
1489 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1490 "session. End gracefully at the first _next() "
1491 "call because of %s=\"%s\" component parameter: "
1492 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1493 SESS_NOT_FOUND_ACTION_END_STR
,
1494 lttng_live
->params
.url
->str
);
1501 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1505 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1506 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1511 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1512 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1513 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1517 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1518 const bt_value
*params
, const bt_value
**result
,
1519 bt_self_component_class
*self_comp_class
,
1520 bt_logging_level log_level
)
1522 bt_component_class_query_method_status status
;
1523 const bt_value
*url_value
= NULL
;
1525 struct live_viewer_connection
*viewer_connection
= NULL
;
1526 enum bt_param_validation_status validation_status
;
1527 gchar
*validate_error
= NULL
;
1529 validation_status
= bt_param_validation_validate(params
,
1530 list_sessions_params
, &validate_error
);
1531 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1532 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1534 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1535 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1536 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1540 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1541 url
= bt_value_string_get(url_value
);
1543 viewer_connection
= live_viewer_connection_create(url
, true, NULL
,
1545 if (!viewer_connection
) {
1546 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1550 status
= live_viewer_connection_list_sessions(viewer_connection
,
1552 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1559 BT_VALUE_PUT_REF_AND_RESET(*result
);
1562 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1566 if (viewer_connection
) {
1567 live_viewer_connection_destroy(viewer_connection
);
1570 g_free(validate_error
);
1576 bt_component_class_query_method_status
lttng_live_query_support_info(
1577 const bt_value
*params
, const bt_value
**result
,
1578 bt_logging_level log_level
)
1580 bt_component_class_query_method_status status
=
1581 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1582 const bt_value
*input_type_value
;
1583 const bt_value
*input_value
;
1585 struct bt_common_lttng_live_url_parts parts
= { 0 };
1587 /* Used by the logging macros */
1588 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1591 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1593 if (!input_type_value
) {
1594 BT_COMP_LOGE("Missing expected `type` parameter.");
1598 if (!bt_value_is_string(input_type_value
)) {
1599 BT_COMP_LOGE("`type` parameter is not a string value.");
1603 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1604 /* We don't handle file system paths */
1608 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1610 BT_COMP_LOGE("Missing expected `input` parameter.");
1614 if (!bt_value_is_string(input_value
)) {
1615 BT_COMP_LOGE("`input` parameter is not a string value.");
1619 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1621 if (parts
.session_name
) {
1623 * Looks pretty much like an LTTng live URL: we got the
1624 * session name part, which forms a complete URL.
1630 *result
= bt_value_real_create_init(weight
);
1632 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1640 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1643 BT_ASSERT(!*result
);
1646 bt_common_destroy_lttng_live_url_parts(&parts
);
1651 bt_component_class_query_method_status
lttng_live_query(
1652 bt_self_component_class_source
*comp_class
,
1653 bt_private_query_executor
*priv_query_exec
,
1654 const char *object
, const bt_value
*params
,
1655 __attribute__((unused
)) void *method_data
,
1656 const bt_value
**result
)
1658 bt_component_class_query_method_status status
=
1659 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1660 bt_self_component
*self_comp
= NULL
;
1661 bt_self_component_class
*self_comp_class
=
1662 bt_self_component_class_source_as_self_component_class(comp_class
);
1663 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1664 bt_private_query_executor_as_query_executor_const(
1667 if (strcmp(object
, "sessions") == 0) {
1668 status
= lttng_live_query_list_sessions(params
, result
,
1669 self_comp_class
, log_level
);
1670 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1671 status
= lttng_live_query_support_info(params
, result
,
1674 BT_COMP_LOGI("Unknown query object `%s`", object
);
1675 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1684 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1689 if (lttng_live
->params
.url
) {
1690 g_string_free(lttng_live
->params
.url
, TRUE
);
1696 void lttng_live_component_finalize(bt_self_component_source
*component
)
1698 void *data
= bt_self_component_get_data(
1699 bt_self_component_source_as_self_component(component
));
1704 lttng_live_component_destroy_data(data
);
1708 enum session_not_found_action
parse_session_not_found_action_param(
1709 const bt_value
*no_session_param
)
1711 enum session_not_found_action action
;
1712 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1714 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1715 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1716 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1717 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1719 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1720 action
= SESSION_NOT_FOUND_ACTION_END
;
1726 static struct bt_param_validation_value_descr inputs_elem_descr
= {
1727 .type
= BT_VALUE_TYPE_STRING
,
1730 static const char *sess_not_found_action_choices
[] = {
1731 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1732 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1733 SESS_NOT_FOUND_ACTION_END_STR
,
1736 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1737 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
1740 .element_type
= &inputs_elem_descr
,
1742 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
1743 .choices
= sess_not_found_action_choices
,
1745 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1749 bt_component_class_initialize_method_status
lttng_live_component_create(
1750 const bt_value
*params
,
1751 bt_logging_level log_level
,
1752 bt_self_component
*self_comp
,
1753 struct lttng_live_component
**component
)
1755 struct lttng_live_component
*lttng_live
= NULL
;
1756 const bt_value
*inputs_value
;
1757 const bt_value
*url_value
;
1758 const bt_value
*value
;
1760 enum bt_param_validation_status validation_status
;
1761 gchar
*validation_error
= NULL
;
1762 bt_component_class_initialize_method_status status
;
1764 validation_status
= bt_param_validation_validate(params
, params_descr
,
1766 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1767 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1769 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1770 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
1771 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1775 lttng_live
= g_new0(struct lttng_live_component
, 1);
1777 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1780 lttng_live
->log_level
= log_level
;
1781 lttng_live
->self_comp
= self_comp
;
1782 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1783 lttng_live
->has_msg_iter
= false;
1786 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
1788 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
1789 url
= bt_value_string_get(url_value
);
1791 lttng_live
->params
.url
= g_string_new(url
);
1792 if (!lttng_live
->params
.url
) {
1793 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1797 value
= bt_value_map_borrow_entry_value_const(params
,
1798 SESS_NOT_FOUND_ACTION_PARAM
);
1800 lttng_live
->params
.sess_not_found_act
=
1801 parse_session_not_found_action_param(value
);
1803 BT_COMP_LOGI("Optional `%s` parameter is missing: "
1804 "defaulting to `%s`.",
1805 SESS_NOT_FOUND_ACTION_PARAM
,
1806 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1807 lttng_live
->params
.sess_not_found_act
=
1808 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1811 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1815 lttng_live_component_destroy_data(lttng_live
);
1818 g_free(validation_error
);
1820 *component
= lttng_live
;
1825 bt_component_class_initialize_method_status
lttng_live_component_init(
1826 bt_self_component_source
*self_comp_src
,
1827 bt_self_component_source_configuration
*config
,
1828 const bt_value
*params
,
1829 __attribute__((unused
)) void *init_method_data
)
1831 struct lttng_live_component
*lttng_live
;
1832 bt_component_class_initialize_method_status ret
;
1833 bt_self_component
*self_comp
=
1834 bt_self_component_source_as_self_component(self_comp_src
);
1835 bt_logging_level log_level
= bt_component_get_logging_level(
1836 bt_self_component_as_component(self_comp
));
1837 bt_self_component_add_port_status add_port_status
;
1839 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
1840 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1844 add_port_status
= bt_self_component_source_add_output_port(
1845 self_comp_src
, "out", NULL
, NULL
);
1846 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1847 ret
= (int) add_port_status
;
1851 bt_self_component_set_data(self_comp
, lttng_live
);
1855 lttng_live_component_destroy_data(lttng_live
);