4 * Babeltrace CTF LTTng-live Client Component
6 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define BT_COMP_LOG_SELF_COMP self_comp
32 #define BT_LOG_OUTPUT_LEVEL log_level
33 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
34 #include "logging/comp-logging.h"
40 #include "common/assert.h"
41 #include <babeltrace2/babeltrace.h>
42 #include "compat/compiler.h"
43 #include <babeltrace2/types.h>
45 #include "plugins/common/muxing/muxing.h"
46 #include "plugins/common/param-validation/param-validation.h"
48 #include "data-stream.h"
50 #include "lttng-live.h"
52 #define MAX_QUERY_SIZE (256*1024)
53 #define URL_PARAM "url"
54 #define INPUTS_PARAM "inputs"
55 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
56 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
57 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
58 #define SESS_NOT_FOUND_ACTION_END_STR "end"
60 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
63 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
66 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
67 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
68 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
69 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
70 case LTTNG_LIVE_ITERATOR_STATUS_END
:
71 return "LTTNG_LIVE_ITERATOR_STATUS_END";
72 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
73 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
74 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
75 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
76 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
77 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
78 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
79 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
80 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
81 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
88 const char *print_state(struct lttng_live_stream_iterator
*s
)
91 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
92 return "ACTIVE_NO_DATA";
93 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
94 return "QUIESCENT_NO_DATA";
95 case LTTNG_LIVE_STREAM_QUIESCENT
:
97 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
99 case LTTNG_LIVE_STREAM_EOF
:
106 #define print_stream_state(live_stream_iter) \
108 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
109 ", curr_inact_ts %" PRId64, \
110 print_state(live_stream_iter), \
111 live_stream_iter->last_inactivity_ts, \
112 live_stream_iter->current_inactivity_ts); \
116 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
125 ret
= bt_self_message_iterator_is_interrupted(
126 msg_iter
->self_msg_iter
);
133 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
137 struct lttng_live_trace
*ret_trace
= NULL
;
139 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
140 struct lttng_live_trace
*trace
=
141 g_ptr_array_index(session
->traces
, trace_idx
);
142 if (trace
->id
== trace_id
) {
153 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
155 bt_logging_level log_level
= trace
->log_level
;
156 bt_self_component
*self_comp
= trace
->self_comp
;
158 BT_COMP_LOGD("Destroy lttng_live_trace");
160 BT_ASSERT(trace
->stream_iterators
);
161 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
163 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
164 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
166 lttng_live_metadata_fini(trace
);
171 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
174 struct lttng_live_trace
*trace
= NULL
;
175 bt_logging_level log_level
= session
->log_level
;
176 bt_self_component
*self_comp
= session
->self_comp
;
178 trace
= g_new0(struct lttng_live_trace
, 1);
182 trace
->log_level
= session
->log_level
;
183 trace
->self_comp
= session
->self_comp
;
184 trace
->session
= session
;
185 trace
->id
= trace_id
;
186 trace
->trace_class
= NULL
;
188 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
189 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
190 BT_ASSERT(trace
->stream_iterators
);
191 trace
->new_metadata_needed
= true;
192 g_ptr_array_add(session
->traces
, trace
);
194 BT_COMP_LOGI("Create trace");
204 struct lttng_live_trace
*lttng_live_borrow_trace(
205 struct lttng_live_session
*session
, uint64_t trace_id
)
207 struct lttng_live_trace
*trace
;
209 trace
= lttng_live_find_trace(session
, trace_id
);
214 /* The session is the owner of the newly created trace. */
215 trace
= lttng_live_create_trace(session
, trace_id
);
222 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
223 uint64_t session_id
, const char *hostname
,
224 const char *session_name
)
227 struct lttng_live_session
*session
;
228 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
229 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
231 session
= g_new0(struct lttng_live_session
, 1);
236 session
->log_level
= lttng_live_msg_iter
->log_level
;
237 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
238 session
->id
= session_id
;
239 session
->traces
= g_ptr_array_new_with_free_func(
240 (GDestroyNotify
) lttng_live_destroy_trace
);
241 BT_ASSERT(session
->traces
);
242 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
243 session
->new_streams_needed
= true;
244 session
->hostname
= g_string_new(hostname
);
245 BT_ASSERT(session
->hostname
);
247 session
->session_name
= g_string_new(session_name
);
248 BT_ASSERT(session
->session_name
);
250 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
251 session
->id
, hostname
, session_name
);
252 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
255 BT_COMP_LOGE("Error adding session");
263 void lttng_live_destroy_session(struct lttng_live_session
*session
)
265 bt_logging_level log_level
;
266 bt_self_component
*self_comp
;
272 log_level
= session
->log_level
;
273 self_comp
= session
->self_comp
;
274 BT_COMP_LOGD("Destroy lttng live session");
275 if (session
->id
!= -1ULL) {
276 if (lttng_live_detach_session(session
)) {
277 if (session
->lttng_live_msg_iter
&&
278 !lttng_live_graph_is_canceled(
279 session
->lttng_live_msg_iter
)) {
280 /* Old relayd cannot detach sessions. */
281 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
288 if (session
->traces
) {
289 g_ptr_array_free(session
->traces
, TRUE
);
292 if (session
->hostname
) {
293 g_string_free(session
->hostname
, TRUE
);
295 if (session
->session_name
) {
296 g_string_free(session
->session_name
, TRUE
);
305 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
307 if (!lttng_live_msg_iter
) {
311 if (lttng_live_msg_iter
->sessions
) {
312 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
315 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
316 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
317 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
319 /* All stream iterators must be destroyed at this point. */
320 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
321 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
323 g_free(lttng_live_msg_iter
);
330 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
332 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
334 BT_ASSERT(self_msg_iter
);
336 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
337 BT_ASSERT(lttng_live_msg_iter
);
338 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
342 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
343 struct lttng_live_stream_iterator
*lttng_live_stream
)
345 bt_logging_level log_level
= lttng_live_stream
->log_level
;
346 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
348 switch (lttng_live_stream
->state
) {
349 case LTTNG_LIVE_STREAM_QUIESCENT
:
350 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
352 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
354 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
356 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
358 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
360 case LTTNG_LIVE_STREAM_EOF
:
363 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
367 * For active no data stream, fetch next data. It can be either:
368 * - quiescent: need to put it in the prio heap at quiescent end
370 * - have data: need to wire up first event into the prio heap,
371 * - have no data on this stream at this point: need to retry (AGAIN) or
375 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
376 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
377 struct lttng_live_stream_iterator
*lttng_live_stream
)
379 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
380 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
381 enum lttng_live_iterator_status ret
=
382 LTTNG_LIVE_ITERATOR_STATUS_OK
;
383 struct packet_index index
;
384 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
386 if (lttng_live_stream
->trace
->new_metadata_needed
) {
387 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
390 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
391 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
394 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
395 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
398 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
400 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
403 BT_ASSERT(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
404 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
405 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
406 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
408 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
409 last_inact_ts
== curr_inact_ts
) {
410 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
411 print_stream_state(lttng_live_stream
);
413 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
417 lttng_live_stream
->base_offset
= index
.offset
;
418 lttng_live_stream
->offset
= index
.offset
;
419 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
421 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
422 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
428 * Creation of the message requires the ctf trace class to be created
429 * beforehand, but the live protocol gives us all streams (including metadata)
430 * at once. So we split it in three steps: getting streams, getting metadata
431 * (which creates the ctf trace class), and then creating the per-stream
435 enum lttng_live_iterator_status
lttng_live_get_session(
436 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
437 struct lttng_live_session
*session
)
439 enum lttng_live_iterator_status status
;
443 if (!session
->attached
) {
444 ret
= lttng_live_attach_session(session
);
446 if (lttng_live_msg_iter
&& lttng_live_graph_is_canceled(
447 lttng_live_msg_iter
)) {
448 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
450 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
456 status
= lttng_live_get_new_streams(session
);
457 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
458 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
461 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
462 struct lttng_live_trace
*trace
=
463 g_ptr_array_index(session
->traces
, trace_idx
);
465 status
= lttng_live_metadata_update(trace
);
466 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
467 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
471 status
= lttng_live_lazy_msg_init(session
);
478 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
480 uint64_t session_idx
;
482 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
484 struct lttng_live_session
*session
=
485 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
486 session
->new_streams_needed
= true;
491 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
493 uint64_t session_idx
, trace_idx
;
495 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
497 struct lttng_live_session
*session
=
498 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
499 session
->new_streams_needed
= true;
500 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
502 struct lttng_live_trace
*trace
=
503 g_ptr_array_index(session
->traces
, trace_idx
);
504 trace
->new_metadata_needed
= true;
510 enum lttng_live_iterator_status
511 lttng_live_iterator_handle_new_streams_and_metadata(
512 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
514 enum lttng_live_iterator_status ret
=
515 LTTNG_LIVE_ITERATOR_STATUS_OK
;
516 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
517 struct lttng_live_session
*session
;
518 enum session_not_found_action sess_not_found_act
=
519 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
522 * In a remotely distant future, we could add a "new
523 * session" flag to the protocol, which would tell us that we
524 * need to query for new sessions even though we have sessions
527 if (lttng_live_msg_iter
->sessions
->len
== 0) {
528 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
529 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
533 * Retry to create a viewer session for the requested
536 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
537 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
543 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
545 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
547 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
549 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
551 case LTTNG_LIVE_ITERATOR_STATUS_END
:
552 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
557 if (!session
->closed
) {
558 nr_sessions_opened
++;
562 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
563 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
564 nr_sessions_opened
== 0) {
565 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
571 enum lttng_live_iterator_status
emit_inactivity_message(
572 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
573 struct lttng_live_stream_iterator
*stream_iter
,
574 bt_message
**message
, uint64_t timestamp
)
576 enum lttng_live_iterator_status ret
=
577 LTTNG_LIVE_ITERATOR_STATUS_OK
;
578 bt_message
*msg
= NULL
;
580 BT_ASSERT(stream_iter
->trace
->clock_class
);
582 msg
= bt_message_message_iterator_inactivity_create(
583 lttng_live_msg_iter
->self_msg_iter
,
584 stream_iter
->trace
->clock_class
,
595 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
596 bt_message_put_ref(msg
);
601 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
602 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
603 struct lttng_live_stream_iterator
*lttng_live_stream
,
604 bt_message
**message
)
606 enum lttng_live_iterator_status ret
=
607 LTTNG_LIVE_ITERATOR_STATUS_OK
;
609 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
610 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
613 if (lttng_live_stream
->current_inactivity_ts
==
614 lttng_live_stream
->last_inactivity_ts
) {
615 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
616 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
620 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
621 message
, lttng_live_stream
->current_inactivity_ts
);
623 lttng_live_stream
->last_inactivity_ts
=
624 lttng_live_stream
->current_inactivity_ts
;
630 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
631 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
632 const bt_message
*msg
, int64_t last_msg_ts_ns
,
635 const bt_clock_class
*clock_class
= NULL
;
636 const bt_clock_snapshot
*clock_snapshot
= NULL
;
638 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
639 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
644 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
645 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
648 switch (bt_message_get_type(msg
)) {
649 case BT_MESSAGE_TYPE_EVENT
:
651 bt_message_event_borrow_stream_class_default_clock_class_const(
653 BT_ASSERT(clock_class
);
655 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
658 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
660 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
662 BT_ASSERT(clock_class
);
664 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
667 case BT_MESSAGE_TYPE_PACKET_END
:
669 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
671 BT_ASSERT(clock_class
);
673 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
676 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
678 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
680 BT_ASSERT(clock_class
);
682 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
685 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
687 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
689 BT_ASSERT(clock_class
);
691 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
694 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
696 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
700 /* All the other messages have a higher priority */
701 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
702 *ts_ns
= last_msg_ts_ns
;
706 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
707 BT_ASSERT(clock_class
);
709 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
711 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
712 "clock-snapshot-addr=%p", clock_snapshot
);
723 BT_COMP_LOGD("Found message's timestamp: "
724 "iter-data-addr=%p, msg-addr=%p, "
725 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
726 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
733 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
734 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
735 struct lttng_live_stream_iterator
*lttng_live_stream
,
736 bt_message
**message
)
738 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
739 enum bt_msg_iter_status status
;
740 uint64_t session_idx
, trace_idx
;
741 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
742 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
744 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
746 struct lttng_live_session
*session
=
747 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
749 if (session
->new_streams_needed
) {
750 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
753 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
755 struct lttng_live_trace
*trace
=
756 g_ptr_array_index(session
->traces
, trace_idx
);
757 if (trace
->new_metadata_needed
) {
758 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
764 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
765 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
769 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
770 lttng_live_msg_iter
->self_msg_iter
, message
);
772 case BT_MSG_ITER_STATUS_EOF
:
773 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
775 case BT_MSG_ITER_STATUS_OK
:
776 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
778 case BT_MSG_ITER_STATUS_AGAIN
:
780 * Continue immediately (end of packet). The next
781 * get_index may return AGAIN to delay the following
784 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
786 case BT_MSG_ITER_STATUS_INVAL
:
787 /* No argument provided by the user, so don't return INVAL. */
788 case BT_MSG_ITER_STATUS_ERROR
:
790 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
791 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
792 lttng_live_stream
->msg_iter
);
801 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
802 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
803 struct lttng_live_stream_iterator
*stream_iter
,
804 bt_message
**curr_msg
)
806 enum lttng_live_iterator_status live_status
=
807 LTTNG_LIVE_ITERATOR_STATUS_OK
;
809 * The viewer has hung up on us so we are closing the stream. The
810 * `bt_msg_iter` should simply realize that it needs to close the
811 * stream properly by emitting the necessary stream end message.
813 enum bt_msg_iter_status status
=
814 bt_msg_iter_get_next_message(stream_iter
->msg_iter
,
815 lttng_live_msg_iter
->self_msg_iter
, curr_msg
);
817 if (status
== BT_MSG_ITER_STATUS_ERROR
) {
818 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
822 BT_ASSERT(status
== BT_MSG_ITER_STATUS_OK
);
830 * handle_no_data_streams()
832 * - for each ACTIVE_NO_DATA stream:
833 * - query relayd for stream data, or quiescence info.
834 * - if need metadata, get metadata, goto retry.
835 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
836 * - if quiescent, move to QUIESCENT streams
837 * - if fetched data, move to ACTIVE_DATA streams
838 * (at this point each stream either has data, or is quiescent)
842 * handle_new_streams_and_metadata()
843 * - query relayd for known streams, add them as ACTIVE_NO_DATA
844 * - query relayd for metadata
846 * call handle_active_no_data_streams()
848 * handle_quiescent_streams()
849 * - if at least one stream is ACTIVE_DATA:
850 * - peek stream event with lowest timestamp -> next_ts
851 * - for each quiescent stream
852 * - if next_ts >= quiescent end
853 * - set state to ACTIVE_NO_DATA
855 * - for each quiescent stream
856 * - set state to ACTIVE_NO_DATA
858 * call handle_active_no_data_streams()
860 * handle_active_data_streams()
861 * - if at least one stream is ACTIVE_DATA:
862 * - get stream event with lowest timestamp from heap
863 * - make that stream event the current message.
864 * - move this stream heap position to its next event
865 * - if we need to fetch data from relayd, move
866 * stream to ACTIVE_NO_DATA.
870 * end criterion: ctrl-c on client. If relayd exits or the session
871 * closes on the relay daemon side, we keep on waiting for streams.
872 * Eventually handle --end timestamp (also an end criterion).
874 * When disconnected from relayd: try to re-connect endlessly.
877 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
878 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
879 struct lttng_live_stream_iterator
*stream_iter
,
880 bt_message
**curr_msg
)
882 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
883 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
884 enum lttng_live_iterator_status live_status
;
886 if (stream_iter
->has_stream_hung_up
) {
888 * The stream has hung up and the stream was properly closed
889 * during the last call to the current function. Return _END
890 * status now so that this stream iterator is removed for the
891 * stream iterator list.
893 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
898 print_stream_state(stream_iter
);
899 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
900 lttng_live_msg_iter
);
901 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
904 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
905 lttng_live_msg_iter
, stream_iter
);
907 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
908 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
910 * We overwrite `live_status` since `curr_msg` is
911 * likely set to a valid message in this function.
913 live_status
= lttng_live_iterator_close_stream(
914 lttng_live_msg_iter
, stream_iter
, curr_msg
);
918 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
919 lttng_live_msg_iter
, stream_iter
, curr_msg
);
920 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
921 BT_ASSERT(!*curr_msg
);
927 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
928 lttng_live_msg_iter
, stream_iter
, curr_msg
);
929 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
930 BT_ASSERT(!*curr_msg
);
934 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
942 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
943 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
944 struct lttng_live_trace
*live_trace
,
945 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
947 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
948 enum lttng_live_iterator_status stream_iter_status
;;
949 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
950 uint64_t stream_iter_idx
;
951 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
952 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
954 BT_ASSERT(live_trace
);
955 BT_ASSERT(live_trace
->stream_iterators
);
957 * Update the current message of every stream iterators of this trace.
958 * The current msg of every stream must have a timestamp equal or
959 * larger than the last message returned by this iterator. We must
960 * ensure monotonicity.
963 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
964 bool stream_iter_is_ended
= false;
965 struct lttng_live_stream_iterator
*stream_iter
=
966 g_ptr_array_index(live_trace
->stream_iterators
,
970 * Since we may remove elements from the GPtrArray as we
971 * iterate over it, it's possible to see the same element more
974 if (stream_iter
== youngest_candidate_stream_iter
) {
980 * Find if there is are now current message for this stream
983 while (!stream_iter
->current_msg
) {
984 bt_message
*msg
= NULL
;
985 int64_t curr_msg_ts_ns
= INT64_MAX
;
986 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
987 lttng_live_msg_iter
, stream_iter
, &msg
);
989 BT_COMP_LOGD("live stream iterator returned status :%s",
990 print_live_iterator_status(stream_iter_status
));
991 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
992 stream_iter_is_ended
= true;
996 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1003 * Get the timestamp in nanoseconds from origin of this
1006 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1007 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1011 * Check if the message of the current live stream
1012 * iterator occured at the exact same time or after the
1013 * last message returned by this component's message
1014 * iterator. If not, we return an error.
1016 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1017 stream_iter
->current_msg
= msg
;
1018 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1021 * We received a message in the past. To ensure
1022 * monotonicity, we can't send it forward.
1024 BT_COMP_LOGE("Message's timestamp is less than "
1025 "lttng-live's message iterator's last "
1026 "returned timestamp: "
1027 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1028 "last-msg-ts=%" PRId64
,
1029 lttng_live_msg_iter
, curr_msg_ts_ns
,
1030 lttng_live_msg_iter
->last_msg_ts_ns
);
1031 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1036 BT_ASSERT(stream_iter
!= youngest_candidate_stream_iter
);
1038 if (!stream_iter_is_ended
) {
1039 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1040 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1042 * Update the current best candidate message
1043 * for the stream iterator of this live trace
1044 * to be forwarded downstream.
1046 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1047 youngest_candidate_stream_iter
= stream_iter
;
1048 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1050 * Order the messages in an arbitrary but
1051 * deterministic way.
1053 BT_ASSERT(stream_iter
!= youngest_candidate_stream_iter
);
1054 int ret
= common_muxing_compare_messages(
1055 stream_iter
->current_msg
,
1056 youngest_candidate_stream_iter
->current_msg
);
1059 * The `youngest_candidate_stream_iter->current_msg`
1060 * should go first. Update the next
1061 * iterator and the current timestamp.
1063 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1064 youngest_candidate_stream_iter
= stream_iter
;
1065 } else if (ret
== 0) {
1067 * Unable to pick which one should go
1070 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1071 "stream-iter-addr=%p"
1072 "stream-iter-addr=%p",
1074 youngest_candidate_stream_iter
);
1081 * The live stream iterator is ENDed. We remove that
1082 * iterator from the list and we restart the iteration
1083 * at the beginning of the live stream iterator array
1084 * to because the removal will shuffle the array.
1086 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1088 stream_iter_idx
= 0;
1092 if (youngest_candidate_stream_iter
) {
1093 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1094 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1097 * The only case where we don't have a candidate for this trace
1098 * is if we reached the end of all the iterators.
1100 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1101 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1105 return stream_iter_status
;
1109 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1110 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1111 struct lttng_live_session
*session
,
1112 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1114 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1115 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1116 enum lttng_live_iterator_status stream_iter_status
;
1117 uint64_t trace_idx
= 0;
1118 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1119 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1122 * Make sure we are attached to the session and look for new streams
1125 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1126 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1127 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1128 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1132 BT_ASSERT(session
->traces
);
1135 * Use while loops here rather then for loops so we can restart the
1136 * iteration if an element is removed from the array during the
1139 while (trace_idx
< session
->traces
->len
) {
1140 bool trace_is_ended
= false;
1141 struct lttng_live_stream_iterator
*stream_iter
;
1142 struct lttng_live_trace
*trace
=
1143 g_ptr_array_index(session
->traces
, trace_idx
);
1145 stream_iter_status
= next_stream_iterator_for_trace(
1146 lttng_live_msg_iter
, trace
, &stream_iter
);
1147 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1149 * All the live stream iterators for this trace are
1150 * ENDed. Remove the trace from this session.
1152 trace_is_ended
= true;
1153 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1157 if (!trace_is_ended
) {
1158 BT_ASSERT(stream_iter
);
1160 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1161 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1162 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1163 youngest_candidate_stream_iter
= stream_iter
;
1164 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1166 * Order the messages in an arbitrary but
1167 * deterministic way.
1169 int ret
= common_muxing_compare_messages(
1170 stream_iter
->current_msg
,
1171 youngest_candidate_stream_iter
->current_msg
);
1174 * The `youngest_candidate_stream_iter->current_msg`
1175 * should go first. Update the next iterator
1176 * and the current timestamp.
1178 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1179 youngest_candidate_stream_iter
= stream_iter
;
1180 } else if (ret
== 0) {
1181 /* Unable to pick which one should go first. */
1182 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1183 "stream-iter-addr=%p" "stream-iter-addr=%p",
1184 stream_iter
, youngest_candidate_stream_iter
);
1189 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1193 if (youngest_candidate_stream_iter
) {
1194 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1195 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1198 * The only cases where we don't have a candidate for this
1200 * 1. if we reached the end of all the iterators of all the
1201 * traces of this session,
1202 * 2. if we never had live stream iterator in the first place.
1204 * In either cases, we return END.
1206 BT_ASSERT(session
->traces
->len
== 0);
1207 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1210 return stream_iter_status
;
1214 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1218 for (i
= 0; i
< count
; i
++) {
1219 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1224 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1225 bt_self_message_iterator
*self_msg_it
,
1226 bt_message_array_const msgs
, uint64_t capacity
,
1229 bt_component_class_message_iterator_next_method_status status
;
1230 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1231 bt_self_message_iterator_get_data(self_msg_it
);
1232 struct lttng_live_component
*lttng_live
=
1233 lttng_live_msg_iter
->lttng_live_comp
;
1234 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1235 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1236 enum lttng_live_iterator_status stream_iter_status
;
1237 uint64_t session_idx
;
1241 BT_ASSERT(lttng_live_msg_iter
);
1244 * Clear all the invalid message reference that might be left over in
1247 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1250 * If no session are exposed on the relay found at the url provided by
1251 * the user, session count will be 0. In this case, we return status
1252 * end to return gracefully.
1254 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1255 if (lttng_live
->params
.sess_not_found_act
!=
1256 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1257 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1261 * The are no more active session for this session
1262 * name. Retry to create a viewer session for the
1263 * requested session name.
1265 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1266 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1272 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1273 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1277 * Here the muxing of message is done.
1279 * We need to iterate over all the streams of all the traces of all the
1280 * viewer sessions in order to get the message with the smallest
1281 * timestamp. In this case, a session is a viewer session and there is
1282 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1283 * kernel). Each viewer session can have multiple traces, for example,
1284 * 64bit UST viewer sessions could have multiple per-pid traces.
1286 * We iterate over the streams of each traces to update and see what is
1287 * their next message's timestamp. From those timestamps, we select the
1288 * message with the smallest timestamp as the best candidate message
1289 * for that trace and do the same thing across all the sessions.
1291 * We then compare the timestamp of best candidate message of all the
1292 * sessions to pick the message with the smallest timestamp and we
1295 while (*count
< capacity
) {
1296 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1297 *candidate_stream_iter
= NULL
;
1298 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1300 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1303 * Use a while loop instead of a for loop so we can restart the
1304 * iteration if we remove an element. We can safely call
1305 * next_stream_iterator_for_session() multiple times on the
1306 * same session as we only fetch a new message if there is no
1307 * current next message for each live stream iterator.
1308 * If all live stream iterator of that session already have a
1309 * current next message, the function will simply exit return
1310 * the same candidate live stream iterator every time.
1312 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1313 struct lttng_live_session
*session
=
1314 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1317 /* Find the best candidate message to send downstream. */
1318 stream_iter_status
= next_stream_iterator_for_session(
1319 lttng_live_msg_iter
, session
,
1320 &candidate_stream_iter
);
1322 /* If we receive an END status, it means that either:
1323 * - Those traces never had active streams (UST with no
1324 * data produced yet),
1325 * - All live stream iterators have ENDed.*/
1326 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1327 if (session
->closed
&& session
->traces
->len
== 0) {
1329 * Remove the session from the list and restart the
1330 * iteration at the beginning of the array since the
1331 * removal shuffle the elements of the array.
1333 g_ptr_array_remove_index_fast(
1334 lttng_live_msg_iter
->sessions
,
1343 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1347 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1348 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1349 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1350 youngest_stream_iter
= candidate_stream_iter
;
1351 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1353 * The currently selected message to be sent
1354 * downstream next has the exact same timestamp
1355 * that of the current candidate message. We
1356 * must break the tie in a predictable manner.
1358 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1360 * Order the messages in an arbitrary but
1361 * deterministic way.
1363 int ret
= common_muxing_compare_messages(
1364 candidate_stream_iter
->current_msg
,
1365 youngest_stream_iter
->current_msg
);
1368 * The `candidate_stream_iter->current_msg`
1369 * should go first. Update the next
1370 * iterator and the current timestamp.
1372 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1373 youngest_stream_iter
= candidate_stream_iter
;
1374 } else if (ret
== 0) {
1375 /* Unable to pick which one should go first. */
1376 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1377 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1378 youngest_stream_iter
, candidate_stream_iter
);
1385 if (!youngest_stream_iter
) {
1386 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1390 BT_ASSERT(youngest_stream_iter
->current_msg
);
1391 /* Ensure monotonicity. */
1392 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1393 youngest_stream_iter
->current_msg_ts_ns
);
1396 * Insert the next message to the message batch. This will set
1397 * stream iterator current messsage to NULL so that next time
1398 * we fetch the next message of that stream iterator
1400 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1403 /* Update the last timestamp in nanoseconds sent downstream. */
1404 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1405 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1407 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1410 switch (stream_iter_status
) {
1411 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1412 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1415 * We received a again status but we have some messages
1416 * to send downstream. We send them and return OK for
1417 * now. On the next call we return again if there are
1418 * still no new message to send.
1420 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1422 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1425 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1426 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1428 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1429 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1431 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1432 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1433 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1434 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1435 /* Put all existing messages on error. */
1436 put_messages(msgs
, *count
);
1447 bt_component_class_message_iterator_initialize_method_status
lttng_live_msg_iter_init(
1448 bt_self_message_iterator
*self_msg_it
,
1449 bt_self_message_iterator_configuration
*config
,
1450 bt_self_component_source
*self_comp_src
,
1451 bt_self_component_port_output
*self_port
)
1453 bt_component_class_message_iterator_initialize_method_status ret
=
1454 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
1455 bt_self_component
*self_comp
=
1456 bt_self_component_source_as_self_component(self_comp_src
);
1457 struct lttng_live_component
*lttng_live
;
1458 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1459 bt_logging_level log_level
;
1461 BT_ASSERT(self_msg_it
);
1463 lttng_live
= bt_self_component_get_data(self_comp
);
1464 log_level
= lttng_live
->log_level
;
1465 self_comp
= lttng_live
->self_comp
;
1467 /* There can be only one downstream iterator at the same time. */
1468 BT_ASSERT(!lttng_live
->has_msg_iter
);
1469 lttng_live
->has_msg_iter
= true;
1471 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1472 if (!lttng_live_msg_iter
) {
1473 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1477 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1478 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1479 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1480 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1482 lttng_live_msg_iter
->active_stream_iter
= 0;
1483 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1484 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1485 (GDestroyNotify
) lttng_live_destroy_session
);
1486 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1488 lttng_live_msg_iter
->viewer_connection
=
1489 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1490 lttng_live_msg_iter
, log_level
);
1491 if (!lttng_live_msg_iter
->viewer_connection
) {
1495 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1498 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1499 switch (lttng_live
->params
.sess_not_found_act
) {
1500 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1501 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1502 "session. Keep trying to connect because of "
1503 "%s=\"%s\" component parameter: url=\"%s\"",
1504 SESS_NOT_FOUND_ACTION_PARAM
,
1505 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1506 lttng_live
->params
.url
->str
);
1508 case SESSION_NOT_FOUND_ACTION_FAIL
:
1509 BT_COMP_LOGE("Unable to connect to the requested live viewer "
1510 "session. Fail the message iterator"
1511 "initialization because of %s=\"%s\" "
1512 "component parameter: url =\"%s\"",
1513 SESS_NOT_FOUND_ACTION_PARAM
,
1514 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1515 lttng_live
->params
.url
->str
);
1517 case SESSION_NOT_FOUND_ACTION_END
:
1518 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1519 "session. End gracefully at the first _next() "
1520 "call because of %s=\"%s\" component parameter: "
1521 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1522 SESS_NOT_FOUND_ACTION_END_STR
,
1523 lttng_live
->params
.url
->str
);
1530 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1534 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1535 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1540 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1541 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1542 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1546 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1547 const bt_value
*params
, const bt_value
**result
,
1548 bt_self_component_class
*self_comp_class
,
1549 bt_logging_level log_level
)
1551 bt_component_class_query_method_status status
;
1552 const bt_value
*url_value
= NULL
;
1554 struct live_viewer_connection
*viewer_connection
= NULL
;
1555 enum bt_param_validation_status validation_status
;
1556 gchar
*validate_error
= NULL
;
1558 validation_status
= bt_param_validation_validate(params
,
1559 list_sessions_params
, &validate_error
);
1560 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1561 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1563 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1564 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1565 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1569 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1570 url
= bt_value_string_get(url_value
);
1572 viewer_connection
= live_viewer_connection_create(url
, true, NULL
,
1574 if (!viewer_connection
) {
1575 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1579 status
= live_viewer_connection_list_sessions(viewer_connection
,
1581 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1588 BT_VALUE_PUT_REF_AND_RESET(*result
);
1591 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1595 if (viewer_connection
) {
1596 live_viewer_connection_destroy(viewer_connection
);
1599 g_free(validate_error
);
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_self_component_class
*self_comp_class
=
1691 bt_self_component_class_source_as_self_component_class(comp_class
);
1692 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1693 bt_private_query_executor_as_query_executor_const(
1696 if (strcmp(object
, "sessions") == 0) {
1697 status
= lttng_live_query_list_sessions(params
, result
,
1698 self_comp_class
, log_level
);
1699 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1700 status
= lttng_live_query_support_info(params
, result
,
1703 BT_COMP_LOGI("Unknown query object `%s`", object
);
1704 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1713 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1718 if (lttng_live
->params
.url
) {
1719 g_string_free(lttng_live
->params
.url
, TRUE
);
1725 void lttng_live_component_finalize(bt_self_component_source
*component
)
1727 void *data
= bt_self_component_get_data(
1728 bt_self_component_source_as_self_component(component
));
1733 lttng_live_component_destroy_data(data
);
1737 enum session_not_found_action
parse_session_not_found_action_param(
1738 const bt_value
*no_session_param
)
1740 enum session_not_found_action action
;
1741 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
1743 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1744 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1745 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1746 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1748 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
1749 action
= SESSION_NOT_FOUND_ACTION_END
;
1755 static struct bt_param_validation_value_descr inputs_elem_descr
= {
1756 .type
= BT_VALUE_TYPE_STRING
,
1759 static const char *sess_not_found_action_choices
[] = {
1760 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1761 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1762 SESS_NOT_FOUND_ACTION_END_STR
,
1765 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
1766 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
1769 .element_type
= &inputs_elem_descr
,
1771 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
1772 .choices
= sess_not_found_action_choices
,
1774 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1778 bt_component_class_initialize_method_status
lttng_live_component_create(
1779 const bt_value
*params
,
1780 bt_logging_level log_level
,
1781 bt_self_component
*self_comp
,
1782 struct lttng_live_component
**component
)
1784 struct lttng_live_component
*lttng_live
= NULL
;
1785 const bt_value
*inputs_value
;
1786 const bt_value
*url_value
;
1787 const bt_value
*value
;
1789 enum bt_param_validation_status validation_status
;
1790 gchar
*validation_error
= NULL
;
1791 bt_component_class_initialize_method_status status
;
1793 validation_status
= bt_param_validation_validate(params
, params_descr
,
1795 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1796 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1798 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1799 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
1800 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1804 lttng_live
= g_new0(struct lttng_live_component
, 1);
1806 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1809 lttng_live
->log_level
= log_level
;
1810 lttng_live
->self_comp
= self_comp
;
1811 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1812 lttng_live
->has_msg_iter
= false;
1815 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
1817 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
1818 url
= bt_value_string_get(url_value
);
1820 lttng_live
->params
.url
= g_string_new(url
);
1821 if (!lttng_live
->params
.url
) {
1822 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1826 value
= bt_value_map_borrow_entry_value_const(params
,
1827 SESS_NOT_FOUND_ACTION_PARAM
);
1829 lttng_live
->params
.sess_not_found_act
=
1830 parse_session_not_found_action_param(value
);
1832 BT_COMP_LOGI("Optional `%s` parameter is missing: "
1833 "defaulting to `%s`.",
1834 SESS_NOT_FOUND_ACTION_PARAM
,
1835 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1836 lttng_live
->params
.sess_not_found_act
=
1837 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1840 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1844 lttng_live_component_destroy_data(lttng_live
);
1847 g_free(validation_error
);
1849 *component
= lttng_live
;
1854 bt_component_class_initialize_method_status
lttng_live_component_init(
1855 bt_self_component_source
*self_comp_src
,
1856 bt_self_component_source_configuration
*config
,
1857 const bt_value
*params
,
1858 __attribute__((unused
)) void *init_method_data
)
1860 struct lttng_live_component
*lttng_live
;
1861 bt_component_class_initialize_method_status ret
;
1862 bt_self_component
*self_comp
=
1863 bt_self_component_source_as_self_component(self_comp_src
);
1864 bt_logging_level log_level
= bt_component_get_logging_level(
1865 bt_self_component_as_component(self_comp
));
1866 bt_self_component_add_port_status add_port_status
;
1868 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
1869 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
1873 add_port_status
= bt_self_component_source_add_output_port(
1874 self_comp_src
, "out", NULL
, NULL
);
1875 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
1876 ret
= (int) add_port_status
;
1880 bt_self_component_set_data(self_comp
, lttng_live
);
1884 lttng_live_component_destroy_data(lttng_live
);