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"
47 #include "data-stream.h"
49 #include "lttng-live.h"
51 #define MAX_QUERY_SIZE (256*1024)
52 #define URL_PARAM "url"
53 #define INPUTS_PARAM "inputs"
54 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
55 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
56 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
57 #define SESS_NOT_FOUND_ACTION_END_STR "end"
59 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
62 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
65 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
66 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
67 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
68 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
69 case LTTNG_LIVE_ITERATOR_STATUS_END
:
70 return "LTTNG_LIVE_ITERATOR_STATUS_END";
71 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
72 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
73 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
74 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
75 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
76 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
77 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
78 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
79 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
80 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
87 const char *print_state(struct lttng_live_stream_iterator
*s
)
90 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
91 return "ACTIVE_NO_DATA";
92 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
93 return "QUIESCENT_NO_DATA";
94 case LTTNG_LIVE_STREAM_QUIESCENT
:
96 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
98 case LTTNG_LIVE_STREAM_EOF
:
105 #define print_stream_state(live_stream_iter) \
107 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
108 ", curr_inact_ts %" PRId64, \
109 print_state(live_stream_iter), \
110 live_stream_iter->last_inactivity_ts, \
111 live_stream_iter->current_inactivity_ts); \
115 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
124 ret
= bt_self_message_iterator_is_interrupted(
125 msg_iter
->self_msg_iter
);
132 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
136 struct lttng_live_trace
*ret_trace
= NULL
;
138 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
139 struct lttng_live_trace
*trace
=
140 g_ptr_array_index(session
->traces
, trace_idx
);
141 if (trace
->id
== trace_id
) {
152 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
154 bt_logging_level log_level
= trace
->log_level
;
155 bt_self_component
*self_comp
= trace
->self_comp
;
157 BT_COMP_LOGD("Destroy lttng_live_trace");
159 BT_ASSERT(trace
->stream_iterators
);
160 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
162 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
163 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
165 lttng_live_metadata_fini(trace
);
170 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
173 struct lttng_live_trace
*trace
= NULL
;
174 bt_logging_level log_level
= session
->log_level
;
175 bt_self_component
*self_comp
= session
->self_comp
;
177 trace
= g_new0(struct lttng_live_trace
, 1);
181 trace
->log_level
= session
->log_level
;
182 trace
->self_comp
= session
->self_comp
;
183 trace
->session
= session
;
184 trace
->id
= trace_id
;
185 trace
->trace_class
= NULL
;
187 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
188 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
189 BT_ASSERT(trace
->stream_iterators
);
190 trace
->new_metadata_needed
= true;
191 g_ptr_array_add(session
->traces
, trace
);
193 BT_COMP_LOGI("Create trace");
203 struct lttng_live_trace
*lttng_live_borrow_trace(
204 struct lttng_live_session
*session
, uint64_t trace_id
)
206 struct lttng_live_trace
*trace
;
208 trace
= lttng_live_find_trace(session
, trace_id
);
213 /* The session is the owner of the newly created trace. */
214 trace
= lttng_live_create_trace(session
, trace_id
);
221 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
222 uint64_t session_id
, const char *hostname
,
223 const char *session_name
)
226 struct lttng_live_session
*session
;
227 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
228 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
230 session
= g_new0(struct lttng_live_session
, 1);
235 session
->log_level
= lttng_live_msg_iter
->log_level
;
236 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
237 session
->id
= session_id
;
238 session
->traces
= g_ptr_array_new_with_free_func(
239 (GDestroyNotify
) lttng_live_destroy_trace
);
240 BT_ASSERT(session
->traces
);
241 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
242 session
->new_streams_needed
= true;
243 session
->hostname
= g_string_new(hostname
);
244 BT_ASSERT(session
->hostname
);
246 session
->session_name
= g_string_new(session_name
);
247 BT_ASSERT(session
->session_name
);
249 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
250 session
->id
, hostname
, session_name
);
251 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
254 BT_COMP_LOGE("Error adding session");
262 void lttng_live_destroy_session(struct lttng_live_session
*session
)
264 bt_logging_level log_level
;
265 bt_self_component
*self_comp
;
271 log_level
= session
->log_level
;
272 self_comp
= session
->self_comp
;
273 BT_COMP_LOGD("Destroy lttng live session");
274 if (session
->id
!= -1ULL) {
275 if (lttng_live_detach_session(session
)) {
276 if (session
->lttng_live_msg_iter
&&
277 !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
=
381 LTTNG_LIVE_ITERATOR_STATUS_OK
;
382 struct packet_index index
;
383 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
385 if (lttng_live_stream
->trace
->new_metadata_needed
) {
386 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
389 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
390 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
393 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
394 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
397 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
399 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
402 BT_ASSERT(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
403 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
404 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
405 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
407 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
408 last_inact_ts
== curr_inact_ts
) {
409 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
410 print_stream_state(lttng_live_stream
);
412 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
416 lttng_live_stream
->base_offset
= index
.offset
;
417 lttng_live_stream
->offset
= index
.offset
;
418 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
420 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
421 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
427 * Creation of the message requires the ctf trace class to be created
428 * beforehand, but the live protocol gives us all streams (including metadata)
429 * at once. So we split it in three steps: getting streams, getting metadata
430 * (which creates the ctf trace class), and then creating the per-stream
434 enum lttng_live_iterator_status
lttng_live_get_session(
435 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
436 struct lttng_live_session
*session
)
438 enum lttng_live_iterator_status status
;
442 if (!session
->attached
) {
443 ret
= lttng_live_attach_session(session
);
445 if (lttng_live_msg_iter
&& lttng_live_graph_is_canceled(
446 lttng_live_msg_iter
)) {
447 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
449 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
455 status
= lttng_live_get_new_streams(session
);
456 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
457 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
460 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
461 struct lttng_live_trace
*trace
=
462 g_ptr_array_index(session
->traces
, trace_idx
);
464 status
= lttng_live_metadata_update(trace
);
465 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
466 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
470 status
= lttng_live_lazy_msg_init(session
);
477 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
479 uint64_t session_idx
;
481 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
483 struct lttng_live_session
*session
=
484 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
485 session
->new_streams_needed
= true;
490 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
492 uint64_t session_idx
, trace_idx
;
494 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
496 struct lttng_live_session
*session
=
497 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
498 session
->new_streams_needed
= true;
499 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
501 struct lttng_live_trace
*trace
=
502 g_ptr_array_index(session
->traces
, trace_idx
);
503 trace
->new_metadata_needed
= true;
509 enum lttng_live_iterator_status
510 lttng_live_iterator_handle_new_streams_and_metadata(
511 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
513 enum lttng_live_iterator_status ret
=
514 LTTNG_LIVE_ITERATOR_STATUS_OK
;
515 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
516 struct lttng_live_session
*session
;
517 enum session_not_found_action sess_not_found_act
=
518 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
521 * In a remotely distant future, we could add a "new
522 * session" flag to the protocol, which would tell us that we
523 * need to query for new sessions even though we have sessions
526 if (lttng_live_msg_iter
->sessions
->len
== 0) {
527 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
528 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
532 * Retry to create a viewer session for the requested
535 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
536 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
542 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
544 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
546 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
548 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
550 case LTTNG_LIVE_ITERATOR_STATUS_END
:
551 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
556 if (!session
->closed
) {
557 nr_sessions_opened
++;
561 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
562 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
563 nr_sessions_opened
== 0) {
564 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
570 enum lttng_live_iterator_status
emit_inactivity_message(
571 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
572 struct lttng_live_stream_iterator
*stream_iter
,
573 bt_message
**message
, uint64_t timestamp
)
575 enum lttng_live_iterator_status ret
=
576 LTTNG_LIVE_ITERATOR_STATUS_OK
;
577 bt_message
*msg
= NULL
;
579 BT_ASSERT(stream_iter
->trace
->clock_class
);
581 msg
= bt_message_message_iterator_inactivity_create(
582 lttng_live_msg_iter
->self_msg_iter
,
583 stream_iter
->trace
->clock_class
,
594 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
595 bt_message_put_ref(msg
);
600 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
601 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
602 struct lttng_live_stream_iterator
*lttng_live_stream
,
603 bt_message
**message
)
605 enum lttng_live_iterator_status ret
=
606 LTTNG_LIVE_ITERATOR_STATUS_OK
;
608 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
609 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
612 if (lttng_live_stream
->current_inactivity_ts
==
613 lttng_live_stream
->last_inactivity_ts
) {
614 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
615 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
619 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
620 message
, lttng_live_stream
->current_inactivity_ts
);
622 lttng_live_stream
->last_inactivity_ts
=
623 lttng_live_stream
->current_inactivity_ts
;
629 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
630 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
631 const bt_message
*msg
, int64_t last_msg_ts_ns
,
634 const bt_clock_class
*clock_class
= NULL
;
635 const bt_clock_snapshot
*clock_snapshot
= NULL
;
637 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
638 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
643 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
644 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
647 switch (bt_message_get_type(msg
)) {
648 case BT_MESSAGE_TYPE_EVENT
:
650 bt_message_event_borrow_stream_class_default_clock_class_const(
652 BT_ASSERT(clock_class
);
654 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
657 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
659 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
661 BT_ASSERT(clock_class
);
663 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
666 case BT_MESSAGE_TYPE_PACKET_END
:
668 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
670 BT_ASSERT(clock_class
);
672 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
675 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
677 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
679 BT_ASSERT(clock_class
);
681 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
684 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
686 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
688 BT_ASSERT(clock_class
);
690 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
693 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
695 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
699 /* All the other messages have a higher priority */
700 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
701 *ts_ns
= last_msg_ts_ns
;
705 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
706 BT_ASSERT(clock_class
);
708 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
710 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
711 "clock-snapshot-addr=%p", clock_snapshot
);
722 BT_COMP_LOGD("Found message's timestamp: "
723 "iter-data-addr=%p, msg-addr=%p, "
724 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
725 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
732 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
733 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
734 struct lttng_live_stream_iterator
*lttng_live_stream
,
735 bt_message
**message
)
737 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
738 enum bt_msg_iter_status status
;
739 uint64_t session_idx
, trace_idx
;
740 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
741 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
743 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
745 struct lttng_live_session
*session
=
746 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
748 if (session
->new_streams_needed
) {
749 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
752 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
754 struct lttng_live_trace
*trace
=
755 g_ptr_array_index(session
->traces
, trace_idx
);
756 if (trace
->new_metadata_needed
) {
757 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
763 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
764 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
768 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
769 lttng_live_msg_iter
->self_msg_iter
, message
);
771 case BT_MSG_ITER_STATUS_EOF
:
772 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
774 case BT_MSG_ITER_STATUS_OK
:
775 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
777 case BT_MSG_ITER_STATUS_AGAIN
:
779 * Continue immediately (end of packet). The next
780 * get_index may return AGAIN to delay the following
783 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
785 case BT_MSG_ITER_STATUS_INVAL
:
786 /* No argument provided by the user, so don't return INVAL. */
787 case BT_MSG_ITER_STATUS_ERROR
:
789 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
790 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
791 lttng_live_stream
->msg_iter
);
800 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
801 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
802 struct lttng_live_stream_iterator
*stream_iter
,
803 bt_message
**curr_msg
)
805 enum lttng_live_iterator_status live_status
=
806 LTTNG_LIVE_ITERATOR_STATUS_OK
;
808 * The viewer has hung up on us so we are closing the stream. The
809 * `bt_msg_iter` should simply realize that it needs to close the
810 * stream properly by emitting the necessary stream end message.
812 enum bt_msg_iter_status status
=
813 bt_msg_iter_get_next_message(stream_iter
->msg_iter
,
814 lttng_live_msg_iter
->self_msg_iter
, curr_msg
);
816 if (status
== BT_MSG_ITER_STATUS_ERROR
) {
817 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
821 BT_ASSERT(status
== BT_MSG_ITER_STATUS_OK
);
829 * handle_no_data_streams()
831 * - for each ACTIVE_NO_DATA stream:
832 * - query relayd for stream data, or quiescence info.
833 * - if need metadata, get metadata, goto retry.
834 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
835 * - if quiescent, move to QUIESCENT streams
836 * - if fetched data, move to ACTIVE_DATA streams
837 * (at this point each stream either has data, or is quiescent)
841 * handle_new_streams_and_metadata()
842 * - query relayd for known streams, add them as ACTIVE_NO_DATA
843 * - query relayd for metadata
845 * call handle_active_no_data_streams()
847 * handle_quiescent_streams()
848 * - if at least one stream is ACTIVE_DATA:
849 * - peek stream event with lowest timestamp -> next_ts
850 * - for each quiescent stream
851 * - if next_ts >= quiescent end
852 * - set state to ACTIVE_NO_DATA
854 * - for each quiescent stream
855 * - set state to ACTIVE_NO_DATA
857 * call handle_active_no_data_streams()
859 * handle_active_data_streams()
860 * - if at least one stream is ACTIVE_DATA:
861 * - get stream event with lowest timestamp from heap
862 * - make that stream event the current message.
863 * - move this stream heap position to its next event
864 * - if we need to fetch data from relayd, move
865 * stream to ACTIVE_NO_DATA.
869 * end criterion: ctrl-c on client. If relayd exits or the session
870 * closes on the relay daemon side, we keep on waiting for streams.
871 * Eventually handle --end timestamp (also an end criterion).
873 * When disconnected from relayd: try to re-connect endlessly.
876 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
877 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
878 struct lttng_live_stream_iterator
*stream_iter
,
879 bt_message
**curr_msg
)
881 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
882 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
883 enum lttng_live_iterator_status live_status
;
885 if (stream_iter
->has_stream_hung_up
) {
887 * The stream has hung up and the stream was properly closed
888 * during the last call to the current function. Return _END
889 * status now so that this stream iterator is removed for the
890 * stream iterator list.
892 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
897 print_stream_state(stream_iter
);
898 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
899 lttng_live_msg_iter
);
900 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
903 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
904 lttng_live_msg_iter
, stream_iter
);
906 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
907 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
909 * We overwrite `live_status` since `curr_msg` is
910 * likely set to a valid message in this function.
912 live_status
= lttng_live_iterator_close_stream(
913 lttng_live_msg_iter
, stream_iter
, curr_msg
);
917 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
918 lttng_live_msg_iter
, stream_iter
, curr_msg
);
919 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
920 BT_ASSERT(!*curr_msg
);
926 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
927 lttng_live_msg_iter
, stream_iter
, curr_msg
);
928 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
929 BT_ASSERT(!*curr_msg
);
933 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
941 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
942 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
943 struct lttng_live_trace
*live_trace
,
944 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
946 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
947 enum lttng_live_iterator_status stream_iter_status
;;
948 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
949 uint64_t stream_iter_idx
;
950 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
951 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
953 BT_ASSERT(live_trace
);
954 BT_ASSERT(live_trace
->stream_iterators
);
956 * Update the current message of every stream iterators of this trace.
957 * The current msg of every stream must have a timestamp equal or
958 * larger than the last message returned by this iterator. We must
959 * ensure monotonicity.
962 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
963 bool stream_iter_is_ended
= false;
964 struct lttng_live_stream_iterator
*stream_iter
=
965 g_ptr_array_index(live_trace
->stream_iterators
,
969 * Since we may remove elements from the GPtrArray as we
970 * iterate over it, it's possible to see the same element more
973 if (stream_iter
== youngest_candidate_stream_iter
) {
979 * Find if there is are now current message for this stream
982 while (!stream_iter
->current_msg
) {
983 bt_message
*msg
= NULL
;
984 int64_t curr_msg_ts_ns
= INT64_MAX
;
985 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
986 lttng_live_msg_iter
, stream_iter
, &msg
);
988 BT_COMP_LOGD("live stream iterator returned status :%s",
989 print_live_iterator_status(stream_iter_status
));
990 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
991 stream_iter_is_ended
= true;
995 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1002 * Get the timestamp in nanoseconds from origin of this
1005 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1006 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1010 * Check if the message of the current live stream
1011 * iterator occured at the exact same time or after the
1012 * last message returned by this component's message
1013 * iterator. If not, we return an error.
1015 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1016 stream_iter
->current_msg
= msg
;
1017 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1020 * We received a message in the past. To ensure
1021 * monotonicity, we can't send it forward.
1023 BT_COMP_LOGE("Message's timestamp is less than "
1024 "lttng-live's message iterator's last "
1025 "returned timestamp: "
1026 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1027 "last-msg-ts=%" PRId64
,
1028 lttng_live_msg_iter
, curr_msg_ts_ns
,
1029 lttng_live_msg_iter
->last_msg_ts_ns
);
1030 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1035 BT_ASSERT(stream_iter
!= youngest_candidate_stream_iter
);
1037 if (!stream_iter_is_ended
) {
1038 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1039 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1041 * Update the current best candidate message
1042 * for the stream iterator of this live trace
1043 * to be forwarded downstream.
1045 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1046 youngest_candidate_stream_iter
= stream_iter
;
1047 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1049 * Order the messages in an arbitrary but
1050 * deterministic way.
1052 BT_ASSERT(stream_iter
!= youngest_candidate_stream_iter
);
1053 int ret
= common_muxing_compare_messages(
1054 stream_iter
->current_msg
,
1055 youngest_candidate_stream_iter
->current_msg
);
1058 * The `youngest_candidate_stream_iter->current_msg`
1059 * should go first. Update the next
1060 * iterator and the current timestamp.
1062 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1063 youngest_candidate_stream_iter
= stream_iter
;
1064 } else if (ret
== 0) {
1066 * Unable to pick which one should go
1069 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1070 "stream-iter-addr=%p"
1071 "stream-iter-addr=%p",
1073 youngest_candidate_stream_iter
);
1080 * The live stream iterator is ENDed. We remove that
1081 * iterator from the list and we restart the iteration
1082 * at the beginning of the live stream iterator array
1083 * to because the removal will shuffle the array.
1085 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1087 stream_iter_idx
= 0;
1091 if (youngest_candidate_stream_iter
) {
1092 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1093 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1096 * The only case where we don't have a candidate for this trace
1097 * is if we reached the end of all the iterators.
1099 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1100 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1104 return stream_iter_status
;
1108 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1109 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1110 struct lttng_live_session
*session
,
1111 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1113 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1114 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1115 enum lttng_live_iterator_status stream_iter_status
;
1116 uint64_t trace_idx
= 0;
1117 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1118 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1121 * Make sure we are attached to the session and look for new streams
1124 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1125 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1126 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1127 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1131 BT_ASSERT(session
->traces
);
1134 * Use while loops here rather then for loops so we can restart the
1135 * iteration if an element is removed from the array during the
1138 while (trace_idx
< session
->traces
->len
) {
1139 bool trace_is_ended
= false;
1140 struct lttng_live_stream_iterator
*stream_iter
;
1141 struct lttng_live_trace
*trace
=
1142 g_ptr_array_index(session
->traces
, trace_idx
);
1144 stream_iter_status
= next_stream_iterator_for_trace(
1145 lttng_live_msg_iter
, trace
, &stream_iter
);
1146 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1148 * All the live stream iterators for this trace are
1149 * ENDed. Remove the trace from this session.
1151 trace_is_ended
= true;
1152 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1156 if (!trace_is_ended
) {
1157 BT_ASSERT(stream_iter
);
1159 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1160 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1161 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1162 youngest_candidate_stream_iter
= stream_iter
;
1163 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1165 * Order the messages in an arbitrary but
1166 * deterministic way.
1168 int ret
= common_muxing_compare_messages(
1169 stream_iter
->current_msg
,
1170 youngest_candidate_stream_iter
->current_msg
);
1173 * The `youngest_candidate_stream_iter->current_msg`
1174 * should go first. Update the next iterator
1175 * and the current timestamp.
1177 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1178 youngest_candidate_stream_iter
= stream_iter
;
1179 } else if (ret
== 0) {
1180 /* Unable to pick which one should go first. */
1181 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1182 "stream-iter-addr=%p" "stream-iter-addr=%p",
1183 stream_iter
, youngest_candidate_stream_iter
);
1188 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1192 if (youngest_candidate_stream_iter
) {
1193 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1194 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1197 * The only cases where we don't have a candidate for this
1199 * 1. if we reached the end of all the iterators of all the
1200 * traces of this session,
1201 * 2. if we never had live stream iterator in the first place.
1203 * In either cases, we return END.
1205 BT_ASSERT(session
->traces
->len
== 0);
1206 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1209 return stream_iter_status
;
1213 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1217 for (i
= 0; i
< count
; i
++) {
1218 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1223 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1224 bt_self_message_iterator
*self_msg_it
,
1225 bt_message_array_const msgs
, uint64_t capacity
,
1228 bt_component_class_message_iterator_next_method_status status
;
1229 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1230 bt_self_message_iterator_get_data(self_msg_it
);
1231 struct lttng_live_component
*lttng_live
=
1232 lttng_live_msg_iter
->lttng_live_comp
;
1233 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1234 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1235 enum lttng_live_iterator_status stream_iter_status
;
1236 uint64_t session_idx
;
1240 BT_ASSERT(lttng_live_msg_iter
);
1243 * Clear all the invalid message reference that might be left over in
1246 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1249 * If no session are exposed on the relay found at the url provided by
1250 * the user, session count will be 0. In this case, we return status
1251 * end to return gracefully.
1253 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1254 if (lttng_live
->params
.sess_not_found_act
!=
1255 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1256 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1260 * The are no more active session for this session
1261 * name. Retry to create a viewer session for the
1262 * requested session name.
1264 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1265 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1271 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1272 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1276 * Here the muxing of message is done.
1278 * We need to iterate over all the streams of all the traces of all the
1279 * viewer sessions in order to get the message with the smallest
1280 * timestamp. In this case, a session is a viewer session and there is
1281 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1282 * kernel). Each viewer session can have multiple traces, for example,
1283 * 64bit UST viewer sessions could have multiple per-pid traces.
1285 * We iterate over the streams of each traces to update and see what is
1286 * their next message's timestamp. From those timestamps, we select the
1287 * message with the smallest timestamp as the best candidate message
1288 * for that trace and do the same thing across all the sessions.
1290 * We then compare the timestamp of best candidate message of all the
1291 * sessions to pick the message with the smallest timestamp and we
1294 while (*count
< capacity
) {
1295 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1296 *candidate_stream_iter
= NULL
;
1297 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1299 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1302 * Use a while loop instead of a for loop so we can restart the
1303 * iteration if we remove an element. We can safely call
1304 * next_stream_iterator_for_session() multiple times on the
1305 * same session as we only fetch a new message if there is no
1306 * current next message for each live stream iterator.
1307 * If all live stream iterator of that session already have a
1308 * current next message, the function will simply exit return
1309 * the same candidate live stream iterator every time.
1311 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1312 struct lttng_live_session
*session
=
1313 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1316 /* Find the best candidate message to send downstream. */
1317 stream_iter_status
= next_stream_iterator_for_session(
1318 lttng_live_msg_iter
, session
,
1319 &candidate_stream_iter
);
1321 /* If we receive an END status, it means that either:
1322 * - Those traces never had active streams (UST with no
1323 * data produced yet),
1324 * - All live stream iterators have ENDed.*/
1325 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1326 if (session
->closed
&& session
->traces
->len
== 0) {
1328 * Remove the session from the list and restart the
1329 * iteration at the beginning of the array since the
1330 * removal shuffle the elements of the array.
1332 g_ptr_array_remove_index_fast(
1333 lttng_live_msg_iter
->sessions
,
1342 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1346 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1347 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1348 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1349 youngest_stream_iter
= candidate_stream_iter
;
1350 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1352 * The currently selected message to be sent
1353 * downstream next has the exact same timestamp
1354 * that of the current candidate message. We
1355 * must break the tie in a predictable manner.
1357 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1359 * Order the messages in an arbitrary but
1360 * deterministic way.
1362 int ret
= common_muxing_compare_messages(
1363 candidate_stream_iter
->current_msg
,
1364 youngest_stream_iter
->current_msg
);
1367 * The `candidate_stream_iter->current_msg`
1368 * should go first. Update the next
1369 * iterator and the current timestamp.
1371 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1372 youngest_stream_iter
= candidate_stream_iter
;
1373 } else if (ret
== 0) {
1374 /* Unable to pick which one should go first. */
1375 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1376 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1377 youngest_stream_iter
, candidate_stream_iter
);
1384 if (!youngest_stream_iter
) {
1385 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1389 BT_ASSERT(youngest_stream_iter
->current_msg
);
1390 /* Ensure monotonicity. */
1391 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1392 youngest_stream_iter
->current_msg_ts_ns
);
1395 * Insert the next message to the message batch. This will set
1396 * stream iterator current messsage to NULL so that next time
1397 * we fetch the next message of that stream iterator
1399 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1402 /* Update the last timestamp in nanoseconds sent downstream. */
1403 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1404 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1406 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1409 switch (stream_iter_status
) {
1410 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1411 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1414 * We received a again status but we have some messages
1415 * to send downstream. We send them and return OK for
1416 * now. On the next call we return again if there are
1417 * still no new message to send.
1419 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1421 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1424 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1425 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1427 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1428 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1430 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1431 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1432 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1433 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1434 /* Put all existing messages on error. */
1435 put_messages(msgs
, *count
);
1446 bt_component_class_message_iterator_init_method_status
lttng_live_msg_iter_init(
1447 bt_self_message_iterator
*self_msg_it
,
1448 bt_self_message_iterator_configuration
*config
,
1449 bt_self_component_source
*self_comp_src
,
1450 bt_self_component_port_output
*self_port
)
1452 bt_component_class_message_iterator_init_method_status ret
=
1453 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1454 bt_self_component
*self_comp
=
1455 bt_self_component_source_as_self_component(self_comp_src
);
1456 struct lttng_live_component
*lttng_live
;
1457 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1458 bt_logging_level log_level
;
1460 BT_ASSERT(self_msg_it
);
1462 lttng_live
= bt_self_component_get_data(self_comp
);
1463 log_level
= lttng_live
->log_level
;
1464 self_comp
= lttng_live
->self_comp
;
1466 /* There can be only one downstream iterator at the same time. */
1467 BT_ASSERT(!lttng_live
->has_msg_iter
);
1468 lttng_live
->has_msg_iter
= true;
1470 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1471 if (!lttng_live_msg_iter
) {
1472 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1476 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1477 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1478 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1479 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1481 lttng_live_msg_iter
->active_stream_iter
= 0;
1482 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1483 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1484 (GDestroyNotify
) lttng_live_destroy_session
);
1485 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1487 lttng_live_msg_iter
->viewer_connection
=
1488 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1489 lttng_live_msg_iter
, log_level
);
1490 if (!lttng_live_msg_iter
->viewer_connection
) {
1494 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1497 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1498 switch (lttng_live
->params
.sess_not_found_act
) {
1499 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1500 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1501 "session. Keep trying to connect because of "
1502 "%s=\"%s\" component parameter: url=\"%s\"",
1503 SESS_NOT_FOUND_ACTION_PARAM
,
1504 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1505 lttng_live
->params
.url
->str
);
1507 case SESSION_NOT_FOUND_ACTION_FAIL
:
1508 BT_COMP_LOGE("Unable to connect to the requested live viewer "
1509 "session. Fail the message iterator"
1510 "initialization because of %s=\"%s\" "
1511 "component parameter: url =\"%s\"",
1512 SESS_NOT_FOUND_ACTION_PARAM
,
1513 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1514 lttng_live
->params
.url
->str
);
1516 case SESSION_NOT_FOUND_ACTION_END
:
1517 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1518 "session. End gracefully at the first _next() "
1519 "call because of %s=\"%s\" component parameter: "
1520 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1521 SESS_NOT_FOUND_ACTION_END_STR
,
1522 lttng_live
->params
.url
->str
);
1524 case SESSION_NOT_FOUND_ACTION_UNKNOWN
:
1527 BT_COMP_LOGE("Unknown action for session not found"
1528 "error. Fail the message iterator"
1529 "initialization because of %s=\"%s\" "
1530 "component parameter: url =\"%s\"",
1531 SESS_NOT_FOUND_ACTION_PARAM
,
1532 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1533 lttng_live
->params
.url
->str
);
1538 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1542 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1543 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1549 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1550 const bt_value
*params
, const bt_value
**result
,
1551 bt_logging_level log_level
)
1553 bt_component_class_query_method_status status
=
1554 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1555 const bt_value
*url_value
= NULL
;
1557 struct live_viewer_connection
*viewer_connection
= NULL
;
1558 bt_self_component
*self_comp
= NULL
;
1560 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1562 BT_COMP_LOGE("Mandatory `%s` parameter missing", URL_PARAM
);
1563 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1567 if (!bt_value_is_string(url_value
)) {
1568 BT_COMP_LOGE("`%s` parameter is required to be a string value",
1570 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1574 url
= bt_value_string_get(url_value
);
1576 viewer_connection
= live_viewer_connection_create(url
, true, NULL
,
1578 if (!viewer_connection
) {
1582 status
= live_viewer_connection_list_sessions(viewer_connection
,
1584 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1591 BT_VALUE_PUT_REF_AND_RESET(*result
);
1594 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1598 if (viewer_connection
) {
1599 live_viewer_connection_destroy(viewer_connection
);
1605 bt_component_class_query_method_status
lttng_live_query_support_info(
1606 const bt_value
*params
, const bt_value
**result
,
1607 bt_logging_level log_level
)
1609 bt_component_class_query_method_status status
=
1610 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1611 const bt_value
*input_type_value
;
1612 const bt_value
*input_value
;
1614 struct bt_common_lttng_live_url_parts parts
= { 0 };
1616 /* Used by the logging macros */
1617 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1620 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1622 if (!input_type_value
) {
1623 BT_COMP_LOGE("Missing expected `type` parameter.");
1627 if (!bt_value_is_string(input_type_value
)) {
1628 BT_COMP_LOGE("`type` parameter is not a string value.");
1632 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1633 /* We don't handle file system paths */
1637 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1639 BT_COMP_LOGE("Missing expected `input` parameter.");
1643 if (!bt_value_is_string(input_value
)) {
1644 BT_COMP_LOGE("`input` parameter is not a string value.");
1648 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1650 if (parts
.session_name
) {
1652 * Looks pretty much like an LTTng live URL: we got the
1653 * session name part, which forms a complete URL.
1659 *result
= bt_value_real_create_init(weight
);
1661 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1669 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1672 BT_ASSERT(!*result
);
1675 bt_common_destroy_lttng_live_url_parts(&parts
);
1680 bt_component_class_query_method_status
lttng_live_query(
1681 bt_self_component_class_source
*comp_class
,
1682 bt_private_query_executor
*priv_query_exec
,
1683 const char *object
, const bt_value
*params
,
1684 __attribute__((unused
)) void *method_data
,
1685 const bt_value
**result
)
1687 bt_component_class_query_method_status status
=
1688 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1689 bt_self_component
*self_comp
= NULL
;
1690 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1691 bt_private_query_executor_as_query_executor_const(
1694 if (strcmp(object
, "sessions") == 0) {
1695 status
= lttng_live_query_list_sessions(params
, result
,
1697 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1698 status
= lttng_live_query_support_info(params
, result
,
1701 BT_COMP_LOGI("Unknown query object `%s`", object
);
1702 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1711 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1716 if (lttng_live
->params
.url
) {
1717 g_string_free(lttng_live
->params
.url
, TRUE
);
1723 void lttng_live_component_finalize(bt_self_component_source
*component
)
1725 void *data
= bt_self_component_get_data(
1726 bt_self_component_source_as_self_component(component
));
1731 lttng_live_component_destroy_data(data
);
1735 enum session_not_found_action
parse_session_not_found_action_param(
1736 const bt_value
*no_session_param
)
1738 enum session_not_found_action action
;
1739 const char *no_session_act_str
;
1740 no_session_act_str
= bt_value_string_get(no_session_param
);
1741 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1742 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1743 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1744 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1745 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0) {
1746 action
= SESSION_NOT_FOUND_ACTION_END
;
1748 action
= SESSION_NOT_FOUND_ACTION_UNKNOWN
;
1754 struct lttng_live_component
*lttng_live_component_create(const bt_value
*params
,
1755 bt_logging_level log_level
, bt_self_component
*self_comp
)
1757 struct lttng_live_component
*lttng_live
;
1758 const bt_value
*inputs_value
;
1759 const bt_value
*url_value
;
1760 const bt_value
*value
;
1763 lttng_live
= g_new0(struct lttng_live_component
, 1);
1767 lttng_live
->log_level
= log_level
;
1768 lttng_live
->self_comp
= self_comp
;
1769 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1770 lttng_live
->has_msg_iter
= false;
1772 inputs_value
= bt_value_map_borrow_entry_value_const(params
,
1774 if (!inputs_value
) {
1775 BT_COMP_LOGE("Mandatory `%s` parameter missing", INPUTS_PARAM
);
1779 if (!bt_value_is_array(inputs_value
)) {
1780 BT_COMP_LOGE("`%s` parameter is required to be an array value",
1785 if (bt_value_array_get_length(inputs_value
) != 1) {
1786 BT_COMP_LOGE("`%s` parameter's length is required to be 1",
1791 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
,
1793 BT_ASSERT(url_value
);
1795 if (!bt_value_is_string(url_value
)) {
1796 BT_COMP_LOGE("First element of `%s` parameter is required to be a string value (URL)",
1801 url
= bt_value_string_get(url_value
);
1802 lttng_live
->params
.url
= g_string_new(url
);
1803 if (!lttng_live
->params
.url
) {
1807 value
= bt_value_map_borrow_entry_value_const(params
,
1808 SESS_NOT_FOUND_ACTION_PARAM
);
1810 if (!bt_value_is_string(value
)) {
1811 BT_COMP_LOGE("`%s` parameter is required to be a string value",
1812 SESS_NOT_FOUND_ACTION_PARAM
);
1816 lttng_live
->params
.sess_not_found_act
=
1817 parse_session_not_found_action_param(value
);
1818 if (lttng_live
->params
.sess_not_found_act
== SESSION_NOT_FOUND_ACTION_UNKNOWN
) {
1819 BT_COMP_LOGE("Unexpected value for `%s` parameter: "
1820 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1821 bt_value_string_get(value
));
1825 BT_COMP_LOGI("Optional `%s` parameter is missing: "
1826 "defaulting to `%s`.",
1827 SESS_NOT_FOUND_ACTION_PARAM
,
1828 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1829 lttng_live
->params
.sess_not_found_act
=
1830 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1836 lttng_live_component_destroy_data(lttng_live
);
1843 bt_component_class_init_method_status
lttng_live_component_init(
1844 bt_self_component_source
*self_comp_src
,
1845 bt_self_component_source_configuration
*config
,
1846 const bt_value
*params
,
1847 __attribute__((unused
)) void *init_method_data
)
1849 struct lttng_live_component
*lttng_live
;
1850 bt_component_class_init_method_status ret
=
1851 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
1852 bt_self_component
*self_comp
=
1853 bt_self_component_source_as_self_component(self_comp_src
);
1854 bt_logging_level log_level
= bt_component_get_logging_level(
1855 bt_self_component_as_component(self_comp
));
1856 bt_self_component_add_port_status add_port_status
;
1858 lttng_live
= lttng_live_component_create(params
, log_level
, self_comp
);
1860 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1864 add_port_status
= bt_self_component_source_add_output_port(
1865 self_comp_src
, "out", NULL
, NULL
);
1866 switch (add_port_status
) {
1867 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
1868 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1870 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
1871 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1877 bt_self_component_set_data(self_comp
, lttng_live
);
1881 lttng_live_component_destroy_data(lttng_live
);