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"
42 #include "common/assert.h"
43 #include <babeltrace2/babeltrace.h>
44 #include "compat/compiler.h"
45 #include <babeltrace2/types.h>
47 #include "plugins/common/muxing/muxing.h"
48 #include "plugins/common/param-validation/param-validation.h"
50 #include "data-stream.h"
52 #include "lttng-live.h"
54 #define MAX_QUERY_SIZE (256*1024)
55 #define URL_PARAM "url"
56 #define INPUTS_PARAM "inputs"
57 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
58 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
59 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
60 #define SESS_NOT_FOUND_ACTION_END_STR "end"
62 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
65 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
68 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
69 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
70 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
71 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
72 case LTTNG_LIVE_ITERATOR_STATUS_END
:
73 return "LTTNG_LIVE_ITERATOR_STATUS_END";
74 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
75 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
76 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
77 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
78 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
79 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
80 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
81 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
82 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
83 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
90 const char *print_state(struct lttng_live_stream_iterator
*s
)
93 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
94 return "ACTIVE_NO_DATA";
95 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
96 return "QUIESCENT_NO_DATA";
97 case LTTNG_LIVE_STREAM_QUIESCENT
:
99 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
100 return "ACTIVE_DATA";
101 case LTTNG_LIVE_STREAM_EOF
:
108 #define print_stream_state(live_stream_iter) \
110 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
111 ", curr_inact_ts %" PRId64, \
112 print_state(live_stream_iter), \
113 live_stream_iter->last_inactivity_ts, \
114 live_stream_iter->current_inactivity_ts); \
118 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
127 ret
= bt_self_message_iterator_is_interrupted(
128 msg_iter
->self_msg_iter
);
135 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
139 struct lttng_live_trace
*ret_trace
= NULL
;
141 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
142 struct lttng_live_trace
*trace
=
143 g_ptr_array_index(session
->traces
, trace_idx
);
144 if (trace
->id
== trace_id
) {
155 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
157 bt_logging_level log_level
= trace
->log_level
;
158 bt_self_component
*self_comp
= trace
->self_comp
;
160 BT_COMP_LOGD("Destroy lttng_live_trace");
162 BT_ASSERT(trace
->stream_iterators
);
163 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
165 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
166 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
168 lttng_live_metadata_fini(trace
);
173 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
176 struct lttng_live_trace
*trace
= NULL
;
177 bt_logging_level log_level
= session
->log_level
;
178 bt_self_component
*self_comp
= session
->self_comp
;
180 trace
= g_new0(struct lttng_live_trace
, 1);
182 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
183 "Failed to allocate live trace");
186 trace
->log_level
= session
->log_level
;
187 trace
->self_comp
= session
->self_comp
;
188 trace
->session
= session
;
189 trace
->id
= trace_id
;
190 trace
->trace_class
= NULL
;
192 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
193 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
194 BT_ASSERT(trace
->stream_iterators
);
195 trace
->new_metadata_needed
= true;
196 g_ptr_array_add(session
->traces
, trace
);
198 BT_COMP_LOGI("Create trace");
208 struct lttng_live_trace
*lttng_live_borrow_trace(
209 struct lttng_live_session
*session
, uint64_t trace_id
)
211 struct lttng_live_trace
*trace
;
213 trace
= lttng_live_find_trace(session
, trace_id
);
218 /* The session is the owner of the newly created trace. */
219 trace
= lttng_live_create_trace(session
, trace_id
);
226 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
227 uint64_t session_id
, const char *hostname
,
228 const char *session_name
)
231 struct lttng_live_session
*session
;
232 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
233 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
235 session
= g_new0(struct lttng_live_session
, 1);
237 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
238 "Failed to allocate live session");
242 session
->log_level
= lttng_live_msg_iter
->log_level
;
243 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
244 session
->id
= session_id
;
245 session
->traces
= g_ptr_array_new_with_free_func(
246 (GDestroyNotify
) lttng_live_destroy_trace
);
247 BT_ASSERT(session
->traces
);
248 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
249 session
->new_streams_needed
= true;
250 session
->hostname
= g_string_new(hostname
);
251 BT_ASSERT(session
->hostname
);
253 session
->session_name
= g_string_new(session_name
);
254 BT_ASSERT(session
->session_name
);
256 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
257 session
->id
, hostname
, session_name
);
258 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
261 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error adding session");
269 void lttng_live_destroy_session(struct lttng_live_session
*session
)
271 bt_logging_level log_level
;
272 bt_self_component
*self_comp
;
278 log_level
= session
->log_level
;
279 self_comp
= session
->self_comp
;
280 BT_COMP_LOGD("Destroy lttng live session");
281 if (session
->id
!= -1ULL) {
282 if (lttng_live_detach_session(session
)) {
283 if (!lttng_live_graph_is_canceled(
284 session
->lttng_live_msg_iter
)) {
285 /* Old relayd cannot detach sessions. */
286 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
293 if (session
->traces
) {
294 g_ptr_array_free(session
->traces
, TRUE
);
297 if (session
->hostname
) {
298 g_string_free(session
->hostname
, TRUE
);
300 if (session
->session_name
) {
301 g_string_free(session
->session_name
, TRUE
);
310 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
312 if (!lttng_live_msg_iter
) {
316 if (lttng_live_msg_iter
->sessions
) {
317 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
320 if (lttng_live_msg_iter
->viewer_connection
) {
321 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
323 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
324 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
326 /* All stream iterators must be destroyed at this point. */
327 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
328 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
330 g_free(lttng_live_msg_iter
);
337 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
339 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
341 BT_ASSERT(self_msg_iter
);
343 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
344 BT_ASSERT(lttng_live_msg_iter
);
345 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
349 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
350 struct lttng_live_stream_iterator
*lttng_live_stream
)
352 bt_logging_level log_level
= lttng_live_stream
->log_level
;
353 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
355 switch (lttng_live_stream
->state
) {
356 case LTTNG_LIVE_STREAM_QUIESCENT
:
357 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
359 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
361 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
363 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
365 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
367 case LTTNG_LIVE_STREAM_EOF
:
370 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
374 * For active no data stream, fetch next data. It can be either:
375 * - quiescent: need to put it in the prio heap at quiescent end
377 * - have data: need to wire up first event into the prio heap,
378 * - have no data on this stream at this point: need to retry (AGAIN) or
382 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
383 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
384 struct lttng_live_stream_iterator
*lttng_live_stream
)
386 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
387 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
388 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
389 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
390 struct packet_index index
;
392 if (lttng_live_stream
->trace
->new_metadata_needed
) {
393 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
396 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
397 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
400 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
401 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
404 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
406 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
409 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
410 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
411 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
412 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
414 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
415 last_inact_ts
== curr_inact_ts
) {
416 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
417 print_stream_state(lttng_live_stream
);
419 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
423 lttng_live_stream
->base_offset
= index
.offset
;
424 lttng_live_stream
->offset
= index
.offset
;
425 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
427 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
428 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
434 * Creation of the message requires the ctf trace class to be created
435 * beforehand, but the live protocol gives us all streams (including metadata)
436 * at once. So we split it in three steps: getting streams, getting metadata
437 * (which creates the ctf trace class), and then creating the per-stream
441 enum lttng_live_iterator_status
lttng_live_get_session(
442 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
443 struct lttng_live_session
*session
)
445 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
446 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
447 enum lttng_live_iterator_status status
;
450 if (!session
->attached
) {
451 enum lttng_live_attach_session_status attach_status
=
452 lttng_live_attach_session(session
,
453 lttng_live_msg_iter
->self_msg_iter
);
454 if (attach_status
!= LTTNG_LIVE_ATTACH_SESSION_STATUS_OK
) {
455 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
457 * Clear any causes appended in
458 * `lttng_live_attach_session()` as we want to
459 * return gracefully since the graph was
462 bt_current_thread_clear_error();
463 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
465 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
466 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
467 "Error attaching to LTTng live session");
473 status
= lttng_live_get_new_streams(session
,
474 lttng_live_msg_iter
->self_msg_iter
);
475 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
476 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
480 while (trace_idx
< session
->traces
->len
) {
481 struct lttng_live_trace
*trace
=
482 g_ptr_array_index(session
->traces
, trace_idx
);
484 status
= lttng_live_metadata_update(trace
);
486 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
489 case LTTNG_LIVE_ITERATOR_STATUS_END
:
491 * The trace has ended. Remove it of the array an
492 * continue the iteration.
493 * We can remove the trace safely when using the
494 * g_ptr_array_remove_index_fast because it replaces
495 * the element at trace_idx with the array's last
496 * element. trace_idx is not incremented because of
499 (void) g_ptr_array_remove_index_fast(session
->traces
,
506 status
= lttng_live_lazy_msg_init(session
,
507 lttng_live_msg_iter
->self_msg_iter
);
514 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
516 uint64_t session_idx
;
518 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
520 struct lttng_live_session
*session
=
521 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
522 session
->new_streams_needed
= true;
527 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
529 uint64_t session_idx
, trace_idx
;
531 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
533 struct lttng_live_session
*session
=
534 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
535 session
->new_streams_needed
= true;
536 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
538 struct lttng_live_trace
*trace
=
539 g_ptr_array_index(session
->traces
, trace_idx
);
540 trace
->new_metadata_needed
= true;
546 enum lttng_live_iterator_status
547 lttng_live_iterator_handle_new_streams_and_metadata(
548 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
550 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
551 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
552 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
553 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
554 struct lttng_live_session
*session
;
555 enum session_not_found_action sess_not_found_act
=
556 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
559 * In a remotely distant future, we could add a "new
560 * session" flag to the protocol, which would tell us that we
561 * need to query for new sessions even though we have sessions
564 if (lttng_live_msg_iter
->sessions
->len
== 0) {
565 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
566 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
570 * Retry to create a viewer session for the requested
573 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
574 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
575 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
576 "Error creating LTTng live viewer session");
582 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
584 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
586 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
588 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
590 case LTTNG_LIVE_ITERATOR_STATUS_END
:
591 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
596 if (!session
->closed
) {
597 nr_sessions_opened
++;
601 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
602 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
603 nr_sessions_opened
== 0) {
604 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
610 enum lttng_live_iterator_status
emit_inactivity_message(
611 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
612 struct lttng_live_stream_iterator
*stream_iter
,
613 bt_message
**message
, uint64_t timestamp
)
615 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
616 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
617 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
618 bt_message
*msg
= NULL
;
620 BT_ASSERT(stream_iter
->trace
->clock_class
);
622 msg
= bt_message_message_iterator_inactivity_create(
623 lttng_live_msg_iter
->self_msg_iter
,
624 stream_iter
->trace
->clock_class
, timestamp
);
626 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
627 "Error emitting message iterator inactivity message");
636 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
637 bt_message_put_ref(msg
);
642 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
643 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
644 struct lttng_live_stream_iterator
*lttng_live_stream
,
645 bt_message
**message
)
647 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
649 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
650 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
653 if (lttng_live_stream
->current_inactivity_ts
==
654 lttng_live_stream
->last_inactivity_ts
) {
655 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
656 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
660 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
661 message
, lttng_live_stream
->current_inactivity_ts
);
663 lttng_live_stream
->last_inactivity_ts
=
664 lttng_live_stream
->current_inactivity_ts
;
670 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
671 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
672 const bt_message
*msg
, int64_t last_msg_ts_ns
,
675 const bt_clock_class
*clock_class
= NULL
;
676 const bt_clock_snapshot
*clock_snapshot
= NULL
;
678 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
679 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
682 BT_ASSERT_DBG(ts_ns
);
684 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
685 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
688 switch (bt_message_get_type(msg
)) {
689 case BT_MESSAGE_TYPE_EVENT
:
690 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
692 BT_ASSERT_DBG(clock_class
);
694 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
697 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
698 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
700 BT_ASSERT(clock_class
);
702 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
705 case BT_MESSAGE_TYPE_PACKET_END
:
706 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
708 BT_ASSERT(clock_class
);
710 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
713 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
714 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
716 BT_ASSERT(clock_class
);
718 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
721 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
722 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
724 BT_ASSERT(clock_class
);
726 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
729 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
730 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
734 /* All the other messages have a higher priority */
735 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
736 *ts_ns
= last_msg_ts_ns
;
740 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
741 BT_ASSERT_DBG(clock_class
);
743 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
745 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
746 "Cannot get nanoseconds from Epoch of clock snapshot: "
747 "clock-snapshot-addr=%p", clock_snapshot
);
758 BT_COMP_LOGD("Found message's timestamp: "
759 "iter-data-addr=%p, msg-addr=%p, "
760 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
761 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
768 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
769 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
770 struct lttng_live_stream_iterator
*lttng_live_stream
,
771 bt_message
**message
)
773 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
774 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
775 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
776 enum ctf_msg_iter_status status
;
777 uint64_t session_idx
, trace_idx
;
779 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
781 struct lttng_live_session
*session
=
782 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
784 if (session
->new_streams_needed
) {
785 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
788 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
790 struct lttng_live_trace
*trace
=
791 g_ptr_array_index(session
->traces
, trace_idx
);
792 if (trace
->new_metadata_needed
) {
793 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
799 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
800 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
804 status
= ctf_msg_iter_get_next_message(
805 lttng_live_stream
->msg_iter
, message
);
807 case CTF_MSG_ITER_STATUS_EOF
:
808 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
810 case CTF_MSG_ITER_STATUS_OK
:
811 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
813 case CTF_MSG_ITER_STATUS_AGAIN
:
815 * Continue immediately (end of packet). The next
816 * get_index may return AGAIN to delay the following
819 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
821 case CTF_MSG_ITER_STATUS_INVAL
:
822 /* No argument provided by the user, so don't return INVAL. */
823 case CTF_MSG_ITER_STATUS_ERROR
:
825 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
826 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
827 "CTF message iterator return an error or failed: "
828 "msg_iter=%p", lttng_live_stream
->msg_iter
);
837 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
838 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
839 struct lttng_live_stream_iterator
*stream_iter
,
840 bt_message
**curr_msg
)
842 enum lttng_live_iterator_status live_status
=
843 LTTNG_LIVE_ITERATOR_STATUS_OK
;
844 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
845 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
847 * The viewer has hung up on us so we are closing the stream. The
848 * `ctf_msg_iter` should simply realize that it needs to close the
849 * stream properly by emitting the necessary stream end message.
851 enum ctf_msg_iter_status status
= ctf_msg_iter_get_next_message(
852 stream_iter
->msg_iter
, curr_msg
);
854 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
855 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
856 "Error getting the next message from CTF message iterator");
857 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
861 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
869 * handle_no_data_streams()
871 * - for each ACTIVE_NO_DATA stream:
872 * - query relayd for stream data, or quiescence info.
873 * - if need metadata, get metadata, goto retry.
874 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
875 * - if quiescent, move to QUIESCENT streams
876 * - if fetched data, move to ACTIVE_DATA streams
877 * (at this point each stream either has data, or is quiescent)
881 * handle_new_streams_and_metadata()
882 * - query relayd for known streams, add them as ACTIVE_NO_DATA
883 * - query relayd for metadata
885 * call handle_active_no_data_streams()
887 * handle_quiescent_streams()
888 * - if at least one stream is ACTIVE_DATA:
889 * - peek stream event with lowest timestamp -> next_ts
890 * - for each quiescent stream
891 * - if next_ts >= quiescent end
892 * - set state to ACTIVE_NO_DATA
894 * - for each quiescent stream
895 * - set state to ACTIVE_NO_DATA
897 * call handle_active_no_data_streams()
899 * handle_active_data_streams()
900 * - if at least one stream is ACTIVE_DATA:
901 * - get stream event with lowest timestamp from heap
902 * - make that stream event the current message.
903 * - move this stream heap position to its next event
904 * - if we need to fetch data from relayd, move
905 * stream to ACTIVE_NO_DATA.
909 * end criterion: ctrl-c on client. If relayd exits or the session
910 * closes on the relay daemon side, we keep on waiting for streams.
911 * Eventually handle --end timestamp (also an end criterion).
913 * When disconnected from relayd: try to re-connect endlessly.
916 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
917 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
918 struct lttng_live_stream_iterator
*stream_iter
,
919 bt_message
**curr_msg
)
921 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
922 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
923 enum lttng_live_iterator_status live_status
;
925 if (stream_iter
->has_stream_hung_up
) {
927 * The stream has hung up and the stream was properly closed
928 * during the last call to the current function. Return _END
929 * status now so that this stream iterator is removed for the
930 * stream iterator list.
932 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
937 print_stream_state(stream_iter
);
938 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
939 lttng_live_msg_iter
);
940 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
943 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
944 lttng_live_msg_iter
, stream_iter
);
946 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
947 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
949 * We overwrite `live_status` since `curr_msg` is
950 * likely set to a valid message in this function.
952 live_status
= lttng_live_iterator_close_stream(
953 lttng_live_msg_iter
, stream_iter
, curr_msg
);
957 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
958 lttng_live_msg_iter
, stream_iter
, curr_msg
);
959 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
960 BT_ASSERT(!*curr_msg
);
966 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
967 lttng_live_msg_iter
, stream_iter
, curr_msg
);
968 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
969 BT_ASSERT(!*curr_msg
);
973 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
981 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
982 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
983 struct lttng_live_trace
*live_trace
,
984 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
986 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
987 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
988 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
989 enum lttng_live_iterator_status stream_iter_status
;;
990 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
991 uint64_t stream_iter_idx
;
993 BT_ASSERT_DBG(live_trace
);
994 BT_ASSERT_DBG(live_trace
->stream_iterators
);
996 * Update the current message of every stream iterators of this trace.
997 * The current msg of every stream must have a timestamp equal or
998 * larger than the last message returned by this iterator. We must
999 * ensure monotonicity.
1001 stream_iter_idx
= 0;
1002 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1003 bool stream_iter_is_ended
= false;
1004 struct lttng_live_stream_iterator
*stream_iter
=
1005 g_ptr_array_index(live_trace
->stream_iterators
,
1009 * Find if there is are now current message for this stream
1012 while (!stream_iter
->current_msg
) {
1013 bt_message
*msg
= NULL
;
1014 int64_t curr_msg_ts_ns
= INT64_MAX
;
1015 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1016 lttng_live_msg_iter
, stream_iter
, &msg
);
1018 BT_COMP_LOGD("live stream iterator returned status :%s",
1019 print_live_iterator_status(stream_iter_status
));
1020 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1021 stream_iter_is_ended
= true;
1025 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1032 * Get the timestamp in nanoseconds from origin of this
1035 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1036 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1040 * Check if the message of the current live stream
1041 * iterator occured at the exact same time or after the
1042 * last message returned by this component's message
1043 * iterator. If not, we return an error.
1045 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1046 stream_iter
->current_msg
= msg
;
1047 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1050 * We received a message in the past. To ensure
1051 * monotonicity, we can't send it forward.
1053 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1054 "Message's timestamp is less than "
1055 "lttng-live's message iterator's last "
1056 "returned timestamp: "
1057 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1058 "last-msg-ts=%" PRId64
,
1059 lttng_live_msg_iter
, curr_msg_ts_ns
,
1060 lttng_live_msg_iter
->last_msg_ts_ns
);
1061 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1066 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1068 if (!stream_iter_is_ended
) {
1069 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1070 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1072 * Update the current best candidate message
1073 * for the stream iterator of this live trace
1074 * to be forwarded downstream.
1076 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1077 youngest_candidate_stream_iter
= stream_iter
;
1078 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1080 * Order the messages in an arbitrary but
1081 * deterministic way.
1083 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1084 int ret
= common_muxing_compare_messages(
1085 stream_iter
->current_msg
,
1086 youngest_candidate_stream_iter
->current_msg
);
1089 * The `youngest_candidate_stream_iter->current_msg`
1090 * should go first. Update the next
1091 * iterator and the current timestamp.
1093 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1094 youngest_candidate_stream_iter
= stream_iter
;
1095 } else if (ret
== 0) {
1097 * Unable to pick which one should go
1100 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1101 "stream-iter-addr=%p"
1102 "stream-iter-addr=%p",
1104 youngest_candidate_stream_iter
);
1111 * The live stream iterator has ended. That
1112 * iterator is removed from the array, but
1113 * there is no need to increment
1114 * stream_iter_idx as
1115 * g_ptr_array_remove_index_fast replaces the
1116 * removed element with the array's last
1119 g_ptr_array_remove_index_fast(
1120 live_trace
->stream_iterators
,
1125 if (youngest_candidate_stream_iter
) {
1126 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1127 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1130 * The only case where we don't have a candidate for this trace
1131 * is if we reached the end of all the iterators.
1133 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1134 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1138 return stream_iter_status
;
1142 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1143 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1144 struct lttng_live_session
*session
,
1145 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1147 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1148 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1149 enum lttng_live_iterator_status stream_iter_status
;
1150 uint64_t trace_idx
= 0;
1151 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1152 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1155 * Make sure we are attached to the session and look for new streams
1158 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1159 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1160 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1161 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1165 BT_ASSERT_DBG(session
->traces
);
1167 while (trace_idx
< session
->traces
->len
) {
1168 bool trace_is_ended
= false;
1169 struct lttng_live_stream_iterator
*stream_iter
;
1170 struct lttng_live_trace
*trace
=
1171 g_ptr_array_index(session
->traces
, trace_idx
);
1173 stream_iter_status
= next_stream_iterator_for_trace(
1174 lttng_live_msg_iter
, trace
, &stream_iter
);
1175 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1177 * All the live stream iterators for this trace are
1178 * ENDed. Remove the trace from this session.
1180 trace_is_ended
= true;
1181 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1185 if (!trace_is_ended
) {
1186 BT_ASSERT_DBG(stream_iter
);
1188 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1189 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1190 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1191 youngest_candidate_stream_iter
= stream_iter
;
1192 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1194 * Order the messages in an arbitrary but
1195 * deterministic way.
1197 int ret
= common_muxing_compare_messages(
1198 stream_iter
->current_msg
,
1199 youngest_candidate_stream_iter
->current_msg
);
1202 * The `youngest_candidate_stream_iter->current_msg`
1203 * should go first. Update the next iterator
1204 * and the current timestamp.
1206 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1207 youngest_candidate_stream_iter
= stream_iter
;
1208 } else if (ret
== 0) {
1209 /* Unable to pick which one should go first. */
1210 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1211 "stream-iter-addr=%p" "stream-iter-addr=%p",
1212 stream_iter
, youngest_candidate_stream_iter
);
1218 * trace_idx is not incremented since
1219 * g_ptr_array_remove_index_fast replaces the
1220 * element at trace_idx with the array's last element.
1222 g_ptr_array_remove_index_fast(session
->traces
,
1226 if (youngest_candidate_stream_iter
) {
1227 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1228 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1231 * The only cases where we don't have a candidate for this
1233 * 1. if we reached the end of all the iterators of all the
1234 * traces of this session,
1235 * 2. if we never had live stream iterator in the first place.
1237 * In either cases, we return END.
1239 BT_ASSERT(session
->traces
->len
== 0);
1240 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1243 return stream_iter_status
;
1247 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1251 for (i
= 0; i
< count
; i
++) {
1252 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1257 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1258 bt_self_message_iterator
*self_msg_it
,
1259 bt_message_array_const msgs
, uint64_t capacity
,
1262 bt_component_class_message_iterator_next_method_status status
;
1263 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1264 bt_self_message_iterator_get_data(self_msg_it
);
1265 struct lttng_live_component
*lttng_live
=
1266 lttng_live_msg_iter
->lttng_live_comp
;
1267 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1268 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1269 enum lttng_live_iterator_status stream_iter_status
;
1270 uint64_t session_idx
;
1274 BT_ASSERT_DBG(lttng_live_msg_iter
);
1277 * Clear all the invalid message reference that might be left over in
1280 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1283 * If no session are exposed on the relay found at the url provided by
1284 * the user, session count will be 0. In this case, we return status
1285 * end to return gracefully.
1287 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1288 if (lttng_live
->params
.sess_not_found_act
!=
1289 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1290 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1294 * The are no more active session for this session
1295 * name. Retry to create a viewer session for the
1296 * requested session name.
1298 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1299 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1305 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1306 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1310 * Here the muxing of message is done.
1312 * We need to iterate over all the streams of all the traces of all the
1313 * viewer sessions in order to get the message with the smallest
1314 * timestamp. In this case, a session is a viewer session and there is
1315 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1316 * kernel). Each viewer session can have multiple traces, for example,
1317 * 64bit UST viewer sessions could have multiple per-pid traces.
1319 * We iterate over the streams of each traces to update and see what is
1320 * their next message's timestamp. From those timestamps, we select the
1321 * message with the smallest timestamp as the best candidate message
1322 * for that trace and do the same thing across all the sessions.
1324 * We then compare the timestamp of best candidate message of all the
1325 * sessions to pick the message with the smallest timestamp and we
1328 while (*count
< capacity
) {
1329 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1330 *candidate_stream_iter
= NULL
;
1331 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1333 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1335 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1336 struct lttng_live_session
*session
=
1337 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1340 /* Find the best candidate message to send downstream. */
1341 stream_iter_status
= next_stream_iterator_for_session(
1342 lttng_live_msg_iter
, session
,
1343 &candidate_stream_iter
);
1345 /* If we receive an END status, it means that either:
1346 * - Those traces never had active streams (UST with no
1347 * data produced yet),
1348 * - All live stream iterators have ENDed.*/
1349 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1350 if (session
->closed
&& session
->traces
->len
== 0) {
1352 * Remove the session from the list.
1353 * session_idx is not modified since
1354 * g_ptr_array_remove_index_fast
1355 * replaces the the removed element with
1356 * the array's last element.
1358 g_ptr_array_remove_index_fast(
1359 lttng_live_msg_iter
->sessions
,
1367 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1371 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1372 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1373 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1374 youngest_stream_iter
= candidate_stream_iter
;
1375 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1377 * The currently selected message to be sent
1378 * downstream next has the exact same timestamp
1379 * that of the current candidate message. We
1380 * must break the tie in a predictable manner.
1382 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1384 * Order the messages in an arbitrary but
1385 * deterministic way.
1387 int ret
= common_muxing_compare_messages(
1388 candidate_stream_iter
->current_msg
,
1389 youngest_stream_iter
->current_msg
);
1392 * The `candidate_stream_iter->current_msg`
1393 * should go first. Update the next
1394 * iterator and the current timestamp.
1396 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1397 youngest_stream_iter
= candidate_stream_iter
;
1398 } else if (ret
== 0) {
1399 /* Unable to pick which one should go first. */
1400 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1401 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1402 youngest_stream_iter
, candidate_stream_iter
);
1409 if (!youngest_stream_iter
) {
1410 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1414 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1415 /* Ensure monotonicity. */
1416 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1417 youngest_stream_iter
->current_msg_ts_ns
);
1420 * Insert the next message to the message batch. This will set
1421 * stream iterator current messsage to NULL so that next time
1422 * we fetch the next message of that stream iterator
1424 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1427 /* Update the last timestamp in nanoseconds sent downstream. */
1428 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1429 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1431 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1434 switch (stream_iter_status
) {
1435 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1436 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1439 * We received a again status but we have some messages
1440 * to send downstream. We send them and return OK for
1441 * now. On the next call we return again if there are
1442 * still no new message to send.
1444 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1446 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1449 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1450 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1452 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1453 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1455 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1456 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1457 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1458 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1459 /* Put all existing messages on error. */
1460 put_messages(msgs
, *count
);
1471 bt_component_class_message_iterator_initialize_method_status
lttng_live_msg_iter_init(
1472 bt_self_message_iterator
*self_msg_it
,
1473 bt_self_message_iterator_configuration
*config
,
1474 bt_self_component_source
*self_comp_src
,
1475 bt_self_component_port_output
*self_port
)
1477 bt_component_class_message_iterator_initialize_method_status ret
=
1478 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
1479 bt_self_component
*self_comp
=
1480 bt_self_component_source_as_self_component(self_comp_src
);
1481 struct lttng_live_component
*lttng_live
;
1482 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1483 bt_logging_level log_level
;
1485 BT_ASSERT(self_msg_it
);
1487 lttng_live
= bt_self_component_get_data(self_comp
);
1488 log_level
= lttng_live
->log_level
;
1489 self_comp
= lttng_live
->self_comp
;
1491 /* There can be only one downstream iterator at the same time. */
1492 BT_ASSERT(!lttng_live
->has_msg_iter
);
1493 lttng_live
->has_msg_iter
= true;
1495 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1496 if (!lttng_live_msg_iter
) {
1497 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1501 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1502 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1503 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1504 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1506 lttng_live_msg_iter
->active_stream_iter
= 0;
1507 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1508 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1509 (GDestroyNotify
) lttng_live_destroy_session
);
1510 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1512 lttng_live_msg_iter
->viewer_connection
=
1513 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1514 lttng_live_msg_iter
, self_comp
, NULL
, log_level
);
1515 if (!lttng_live_msg_iter
->viewer_connection
) {
1519 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1522 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1523 switch (lttng_live
->params
.sess_not_found_act
) {
1524 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1525 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1526 "session. Keep trying to connect because of "
1527 "%s=\"%s\" component parameter: url=\"%s\"",
1528 SESS_NOT_FOUND_ACTION_PARAM
,
1529 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1530 lttng_live
->params
.url
->str
);
1532 case SESSION_NOT_FOUND_ACTION_FAIL
:
1533 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Unable to connect to the requested live viewer "
1534 "session. Fail the message iterator"
1535 "initialization because of %s=\"%s\" "
1536 "component parameter: url =\"%s\"",
1537 SESS_NOT_FOUND_ACTION_PARAM
,
1538 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1539 lttng_live
->params
.url
->str
);
1541 case SESSION_NOT_FOUND_ACTION_END
:
1542 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1543 "session. End gracefully at the first _next() "
1544 "call because of %s=\"%s\" component parameter: "
1545 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1546 SESS_NOT_FOUND_ACTION_END_STR
,
1547 lttng_live
->params
.url
->str
);
1554 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1558 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1559 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1564 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1565 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1566 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1570 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1571 const bt_value
*params
, const bt_value
**result
,
1572 bt_self_component_class
*self_comp_class
,
1573 bt_logging_level log_level
)
1575 bt_component_class_query_method_status status
;
1576 const bt_value
*url_value
= NULL
;
1578 struct live_viewer_connection
*viewer_connection
= NULL
;
1579 enum bt_param_validation_status validation_status
;
1580 gchar
*validate_error
= NULL
;
1582 validation_status
= bt_param_validation_validate(params
,
1583 list_sessions_params
, &validate_error
);
1584 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1585 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1587 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1588 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1589 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1594 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1595 url
= bt_value_string_get(url_value
);
1597 viewer_connection
= live_viewer_connection_create(url
, true, NULL
,
1598 NULL
, self_comp_class
, log_level
);
1599 if (!viewer_connection
) {
1600 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1601 "Failed to create viewer connection");
1602 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1606 status
= live_viewer_connection_list_sessions(viewer_connection
,
1608 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1609 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1610 "Failed to list viewer sessions");
1617 BT_VALUE_PUT_REF_AND_RESET(*result
);
1620 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1624 if (viewer_connection
) {
1625 live_viewer_connection_destroy(viewer_connection
);
1628 g_free(validate_error
);
1634 bt_component_class_query_method_status
lttng_live_query_support_info(
1635 const bt_value
*params
, const bt_value
**result
,
1636 bt_self_component_class
*self_comp_class
,
1637 bt_logging_level log_level
)
1639 bt_component_class_query_method_status status
=
1640 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1641 const bt_value
*input_type_value
;
1642 const bt_value
*input_value
;
1644 struct bt_common_lttng_live_url_parts parts
= { 0 };
1646 /* Used by the logging macros */
1647 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1650 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1652 if (!input_type_value
) {
1653 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1654 "Missing expected `type` parameter.");
1658 if (!bt_value_is_string(input_type_value
)) {
1659 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1660 "`type` parameter is not a string value.");
1664 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1665 /* We don't handle file system paths */
1669 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1671 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1672 "Missing expected `input` parameter.");
1676 if (!bt_value_is_string(input_value
)) {
1677 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1678 "`input` parameter is not a string value.");
1682 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1684 if (parts
.session_name
) {
1686 * Looks pretty much like an LTTng live URL: we got the
1687 * session name part, which forms a complete URL.
1693 *result
= bt_value_real_create_init(weight
);
1695 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1703 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1706 BT_ASSERT(!*result
);
1709 bt_common_destroy_lttng_live_url_parts(&parts
);
1714 bt_component_class_query_method_status
lttng_live_query(
1715 bt_self_component_class_source
*comp_class
,
1716 bt_private_query_executor
*priv_query_exec
,
1717 const char *object
, const bt_value
*params
,
1718 __attribute__((unused
)) void *method_data
,
1719 const bt_value
**result
)
1721 bt_component_class_query_method_status status
=
1722 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1723 bt_self_component
*self_comp
= NULL
;
1724 bt_self_component_class
*self_comp_class
=
1725 bt_self_component_class_source_as_self_component_class(comp_class
);
1726 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1727 bt_private_query_executor_as_query_executor_const(
1730 if (strcmp(object
, "sessions") == 0) {
1731 status
= lttng_live_query_list_sessions(params
, result
,
1732 self_comp_class
, log_level
);
1733 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1734 status
= lttng_live_query_support_info(params
, result
,
1735 self_comp_class
, log_level
);
1737 BT_COMP_LOGI("Unknown query object `%s`", object
);
1738 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1747 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1752 if (lttng_live
->params
.url
) {
1753 g_string_free(lttng_live
->params
.url
, TRUE
);
1759 void lttng_live_component_finalize(bt_self_component_source
*component
)
1761 void *data
= bt_self_component_get_data(
1762 bt_self_component_source_as_self_component(component
));
1767 lttng_live_component_destroy_data(data
);
1771 enum session_not_found_action
parse_session_not_found_action_param(
1772 const bt_value
*no_session_param
)
1774 enum session_not_found_action action
;
1775 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1777 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1778 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1779 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1780 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1782 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1783 action
= SESSION_NOT_FOUND_ACTION_END
;
1789 static struct bt_param_validation_value_descr inputs_elem_descr
= {
1790 .type
= BT_VALUE_TYPE_STRING
,
1793 static const char *sess_not_found_action_choices
[] = {
1794 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1795 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1796 SESS_NOT_FOUND_ACTION_END_STR
,
1799 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1800 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
1803 .element_type
= &inputs_elem_descr
,
1805 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
1806 .choices
= sess_not_found_action_choices
,
1808 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1812 bt_component_class_initialize_method_status
lttng_live_component_create(
1813 const bt_value
*params
,
1814 bt_logging_level log_level
,
1815 bt_self_component
*self_comp
,
1816 struct lttng_live_component
**component
)
1818 struct lttng_live_component
*lttng_live
= NULL
;
1819 const bt_value
*inputs_value
;
1820 const bt_value
*url_value
;
1821 const bt_value
*value
;
1823 enum bt_param_validation_status validation_status
;
1824 gchar
*validation_error
= NULL
;
1825 bt_component_class_initialize_method_status status
;
1827 validation_status
= bt_param_validation_validate(params
, params_descr
,
1829 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1830 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1832 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1833 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
1834 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1838 lttng_live
= g_new0(struct lttng_live_component
, 1);
1840 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1843 lttng_live
->log_level
= log_level
;
1844 lttng_live
->self_comp
= self_comp
;
1845 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1846 lttng_live
->has_msg_iter
= false;
1849 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
1851 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
1852 url
= bt_value_string_get(url_value
);
1854 lttng_live
->params
.url
= g_string_new(url
);
1855 if (!lttng_live
->params
.url
) {
1856 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1860 value
= bt_value_map_borrow_entry_value_const(params
,
1861 SESS_NOT_FOUND_ACTION_PARAM
);
1863 lttng_live
->params
.sess_not_found_act
=
1864 parse_session_not_found_action_param(value
);
1866 BT_COMP_LOGI("Optional `%s` parameter is missing: "
1867 "defaulting to `%s`.",
1868 SESS_NOT_FOUND_ACTION_PARAM
,
1869 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1870 lttng_live
->params
.sess_not_found_act
=
1871 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1874 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1878 lttng_live_component_destroy_data(lttng_live
);
1881 g_free(validation_error
);
1883 *component
= lttng_live
;
1888 bt_component_class_initialize_method_status
lttng_live_component_init(
1889 bt_self_component_source
*self_comp_src
,
1890 bt_self_component_source_configuration
*config
,
1891 const bt_value
*params
,
1892 __attribute__((unused
)) void *init_method_data
)
1894 struct lttng_live_component
*lttng_live
;
1895 bt_component_class_initialize_method_status ret
;
1896 bt_self_component
*self_comp
=
1897 bt_self_component_source_as_self_component(self_comp_src
);
1898 bt_logging_level log_level
= bt_component_get_logging_level(
1899 bt_self_component_as_component(self_comp
));
1900 bt_self_component_add_port_status add_port_status
;
1902 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
1903 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1907 add_port_status
= bt_self_component_source_add_output_port(
1908 self_comp_src
, "out", NULL
, NULL
);
1909 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1910 ret
= (int) add_port_status
;
1914 bt_self_component_set_data(self_comp
, lttng_live
);
1918 lttng_live_component_destroy_data(lttng_live
);