2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Babeltrace CTF LTTng-live Client Component
11 #define BT_COMP_LOG_SELF_COMP self_comp
12 #define BT_LOG_OUTPUT_LEVEL log_level
13 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
14 #include "logging/comp-logging.h"
22 #include "common/assert.h"
23 #include <babeltrace2/babeltrace.h>
24 #include "compat/compiler.h"
25 #include <babeltrace2/types.h>
27 #include "plugins/common/muxing/muxing.h"
28 #include "plugins/common/param-validation/param-validation.h"
30 #include "data-stream.hpp"
31 #include "metadata.hpp"
32 #include "lttng-live.hpp"
34 #define MAX_QUERY_SIZE (256*1024)
35 #define URL_PARAM "url"
36 #define INPUTS_PARAM "inputs"
37 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
38 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
39 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
40 #define SESS_NOT_FOUND_ACTION_END_STR "end"
42 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
45 const char *lttng_live_iterator_status_string(
46 enum lttng_live_iterator_status status
)
49 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
50 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
51 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
52 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
53 case LTTNG_LIVE_ITERATOR_STATUS_END
:
54 return "LTTNG_LIVE_ITERATOR_STATUS_END";
55 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
56 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
57 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
58 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
59 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
60 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
61 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
62 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
63 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
64 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
71 const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
74 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
75 return "ACTIVE_NO_DATA";
76 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
77 return "QUIESCENT_NO_DATA";
78 case LTTNG_LIVE_STREAM_QUIESCENT
:
80 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
82 case LTTNG_LIVE_STREAM_EOF
:
89 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
90 enum lttng_live_stream_state new_state
)
92 bt_self_component
*self_comp
= stream_iter
->self_comp
;
93 bt_logging_level log_level
= stream_iter
->log_level
;
95 BT_COMP_LOGD("Setting live stream iterator state: viewer-stream-id=%" PRIu64
96 ", old-state=%s, new-state=%s",
97 stream_iter
->viewer_stream_id
,
98 lttng_live_stream_state_string(stream_iter
->state
),
99 lttng_live_stream_state_string(new_state
));
101 stream_iter
->state
= new_state
;
104 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
106 BT_COMP_LOGD("Live stream iterator state=%s, " \
107 "last-inact-ts-is-set=%d, last-inact-ts-value=%" PRId64 ", " \
108 "curr-inact-ts=%" PRId64, \
109 lttng_live_stream_state_string(live_stream_iter->state), \
110 live_stream_iter->last_inactivity_ts.is_set, \
111 live_stream_iter->last_inactivity_ts.value, \
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_session_borrow_trace_by_id(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 (lttng_live_trace
*) 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("Destroying live trace: trace-id=%" PRIu64
, trace
->id
);
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 BT_COMP_LOGD("Creating live trace: "
179 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
180 session
->id
, trace_id
);
181 trace
= g_new0(struct lttng_live_trace
, 1);
183 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
184 "Failed to allocate live trace");
187 trace
->log_level
= session
->log_level
;
188 trace
->self_comp
= session
->self_comp
;
189 trace
->session
= session
;
190 trace
->id
= trace_id
;
191 trace
->trace_class
= NULL
;
193 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
194 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
195 BT_ASSERT(trace
->stream_iterators
);
196 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
197 g_ptr_array_add(session
->traces
, trace
);
208 struct lttng_live_trace
*lttng_live_session_borrow_or_create_trace_by_id(
209 struct lttng_live_session
*session
, uint64_t trace_id
)
211 struct lttng_live_trace
*trace
;
213 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
218 /* The session is the owner of the newly created trace. */
219 trace
= lttng_live_create_trace(session
, trace_id
);
226 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
227 uint64_t session_id
, const char *hostname
,
228 const char *session_name
)
231 struct lttng_live_session
*session
;
232 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
233 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
235 BT_COMP_LOGD("Adding live session: "
236 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
237 session_id
, hostname
, session_name
);
239 session
= g_new0(struct lttng_live_session
, 1);
241 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
242 "Failed to allocate live session");
246 session
->log_level
= lttng_live_msg_iter
->log_level
;
247 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
248 session
->id
= session_id
;
249 session
->traces
= g_ptr_array_new_with_free_func(
250 (GDestroyNotify
) lttng_live_destroy_trace
);
251 BT_ASSERT(session
->traces
);
252 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
253 session
->new_streams_needed
= true;
254 session
->hostname
= g_string_new(hostname
);
255 BT_ASSERT(session
->hostname
);
257 session
->session_name
= g_string_new(session_name
);
258 BT_ASSERT(session
->session_name
);
260 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
270 void lttng_live_destroy_session(struct lttng_live_session
*session
)
272 bt_logging_level log_level
;
273 bt_self_component
*self_comp
;
279 log_level
= session
->log_level
;
280 self_comp
= session
->self_comp
;
281 BT_COMP_LOGD("Destroying live session: "
282 "session-id=%" PRIu64
", session-name=\"%s\"",
283 session
->id
, session
->session_name
->str
);
284 if (session
->id
!= -1ULL) {
285 if (lttng_live_session_detach(session
)) {
286 if (!lttng_live_graph_is_canceled(
287 session
->lttng_live_msg_iter
)) {
288 /* Old relayd cannot detach sessions. */
289 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
296 if (session
->traces
) {
297 g_ptr_array_free(session
->traces
, TRUE
);
300 if (session
->hostname
) {
301 g_string_free(session
->hostname
, TRUE
);
303 if (session
->session_name
) {
304 g_string_free(session
->session_name
, TRUE
);
313 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
315 if (!lttng_live_msg_iter
) {
319 if (lttng_live_msg_iter
->sessions
) {
320 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
323 if (lttng_live_msg_iter
->viewer_connection
) {
324 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
326 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
327 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
329 /* All stream iterators must be destroyed at this point. */
330 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
331 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
333 g_free(lttng_live_msg_iter
);
340 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
342 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
344 BT_ASSERT(self_msg_iter
);
346 lttng_live_msg_iter
= (struct lttng_live_msg_iter
*)
347 bt_self_message_iterator_get_data(self_msg_iter
);
348 BT_ASSERT(lttng_live_msg_iter
);
349 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
353 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
354 struct lttng_live_stream_iterator
*lttng_live_stream
)
356 bt_logging_level log_level
= lttng_live_stream
->log_level
;
357 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
359 switch (lttng_live_stream
->state
) {
360 case LTTNG_LIVE_STREAM_QUIESCENT
:
361 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
363 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
365 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
367 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
369 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
371 case LTTNG_LIVE_STREAM_EOF
:
374 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
378 * For active no data stream, fetch next index. As a result of that it can
380 * - quiescent: won't have events for a bit,
381 * - have data: need to get that data and produce the event,
382 * - have no data on this stream at this point: need to retry (AGAIN) or return
386 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
387 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
388 struct lttng_live_stream_iterator
*lttng_live_stream
)
390 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
391 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
392 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
393 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
394 struct packet_index index
;
396 if (lttng_live_stream
->trace
->metadata_stream_state
==
397 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
398 BT_COMP_LOGD("Need to get an update for the metadata stream before proceeding further with this stream: "
399 "stream-name=\"%s\"", lttng_live_stream
->name
->str
);
400 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
404 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
405 BT_COMP_LOGD("Need to get an update of all streams before proceeding further with this stream: "
406 "stream-name=\"%s\"", lttng_live_stream
->name
->str
);
407 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
411 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
412 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
415 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
417 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
421 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
423 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
424 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
425 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
427 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
428 last_inact_ts
== curr_inact_ts
) {
430 * Because the stream is in the QUIESCENT_NO_DATA
431 * state, we can assert that the last_inactivity_ts was
432 * set and can be safely used in the `if` above.
434 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
436 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
437 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
439 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
444 lttng_live_stream
->base_offset
= index
.offset
;
445 lttng_live_stream
->offset
= index
.offset
;
446 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
448 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
449 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
450 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
451 lttng_live_stream
->name
->str
,
452 lttng_live_stream
->viewer_stream_id
,
453 lttng_live_stream
->base_offset
,
454 lttng_live_stream
->offset
, lttng_live_stream
->len
);
457 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
458 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
464 * Creation of the message requires the ctf trace class to be created
465 * beforehand, but the live protocol gives us all streams (including metadata)
466 * at once. So we split it in three steps: getting streams, getting metadata
467 * (which creates the ctf trace class), and then creating the per-stream
471 enum lttng_live_iterator_status
lttng_live_get_session(
472 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
473 struct lttng_live_session
*session
)
475 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
476 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
477 enum lttng_live_iterator_status status
;
480 if (!session
->attached
) {
481 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
,
483 enum lttng_live_viewer_status attach_status
=
484 lttng_live_session_attach(session
,
485 lttng_live_msg_iter
->self_msg_iter
);
486 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
487 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
489 * Clear any causes appended in
490 * `lttng_live_attach_session()` as we want to
491 * return gracefully since the graph was
494 bt_current_thread_clear_error();
495 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
497 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
498 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
499 "Error attaching to LTTng live session");
505 BT_COMP_LOGD("Updating all streams and metadata for session: "
506 "session-id=%" PRIu64
", session-name=\"%s\"",
507 session
->id
, session
->session_name
->str
);
509 status
= lttng_live_session_get_new_streams(session
,
510 lttng_live_msg_iter
->self_msg_iter
);
511 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
512 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
517 while (trace_idx
< session
->traces
->len
) {
518 struct lttng_live_trace
*trace
=
519 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
521 status
= lttng_live_metadata_update(trace
);
523 case LTTNG_LIVE_ITERATOR_STATUS_END
:
524 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
527 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
528 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
531 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
532 "Error updating trace metadata: "
533 "stream-iter-status=%s, trace-id=%" PRIu64
,
534 lttng_live_iterator_status_string(status
),
541 * Now that we have the metadata we can initialize the downstream
544 status
= lttng_live_lazy_msg_init(session
,
545 lttng_live_msg_iter
->self_msg_iter
);
552 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
554 uint64_t session_idx
, trace_idx
;
555 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
556 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
558 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
560 struct lttng_live_session
*session
=
561 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
562 BT_COMP_LOGD("Force marking session as needing new streams: "
563 "session-id=%" PRIu64
, session
->id
);
564 session
->new_streams_needed
= true;
565 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
567 struct lttng_live_trace
*trace
=
568 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
569 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
570 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
571 session
->id
, trace
->id
);
573 BT_ASSERT(trace
->metadata_stream_state
!=
574 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
576 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
582 enum lttng_live_iterator_status
583 lttng_live_iterator_handle_new_streams_and_metadata(
584 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
586 enum lttng_live_iterator_status status
;
587 enum lttng_live_viewer_status viewer_status
;
588 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
589 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
590 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
591 struct lttng_live_session
*session
;
592 enum session_not_found_action sess_not_found_act
=
593 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
595 BT_COMP_LOGD("Update data and metadata of all sessions: "
596 "live-msg-iter-addr=%p", lttng_live_msg_iter
);
598 * In a remotely distant future, we could add a "new
599 * session" flag to the protocol, which would tell us that we
600 * need to query for new sessions even though we have sessions
603 if (lttng_live_msg_iter
->sessions
->len
== 0) {
604 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
605 BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter");
606 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
609 BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
611 * Retry to create a viewer session for the requested
614 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
615 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
616 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
617 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
618 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
619 "Error creating LTTng live viewer session");
620 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
621 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
630 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
632 session
= (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
,
634 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
636 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
638 case LTTNG_LIVE_ITERATOR_STATUS_END
:
639 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
644 if (!session
->closed
) {
645 nr_sessions_opened
++;
649 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
650 nr_sessions_opened
== 0) {
651 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
653 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
661 enum lttng_live_iterator_status
emit_inactivity_message(
662 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
663 struct lttng_live_stream_iterator
*stream_iter
,
664 const bt_message
**message
, uint64_t timestamp
)
666 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
667 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
668 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
669 bt_message
*msg
= NULL
;
671 BT_ASSERT(stream_iter
->trace
->clock_class
);
673 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
674 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
675 stream_iter
->ctf_stream_class_id
.value
,
676 stream_iter
->viewer_stream_id
, timestamp
);
678 msg
= bt_message_message_iterator_inactivity_create(
679 lttng_live_msg_iter
->self_msg_iter
,
680 stream_iter
->trace
->clock_class
, timestamp
);
682 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
683 "Error emitting message iterator inactivity message");
692 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
693 bt_message_put_ref(msg
);
698 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
699 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
700 struct lttng_live_stream_iterator
*lttng_live_stream
,
701 const bt_message
**message
)
703 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
705 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
706 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
710 * Check if we already sent an inactivty message downstream for this
711 * `current_inactivity_ts` value.
713 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
714 lttng_live_stream
->current_inactivity_ts
==
715 lttng_live_stream
->last_inactivity_ts
.value
) {
716 lttng_live_stream_iterator_set_state(lttng_live_stream
,
717 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
719 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
723 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
724 message
, lttng_live_stream
->current_inactivity_ts
);
726 lttng_live_stream
->last_inactivity_ts
.value
=
727 lttng_live_stream
->current_inactivity_ts
;
728 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
734 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
735 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
736 const bt_message
*msg
, int64_t last_msg_ts_ns
,
739 const bt_clock_class
*clock_class
= NULL
;
740 const bt_clock_snapshot
*clock_snapshot
= NULL
;
742 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
743 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
746 BT_ASSERT_DBG(ts_ns
);
748 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
749 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
752 switch (bt_message_get_type(msg
)) {
753 case BT_MESSAGE_TYPE_EVENT
:
754 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
756 BT_ASSERT_DBG(clock_class
);
758 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
761 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
762 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
764 BT_ASSERT(clock_class
);
766 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
769 case BT_MESSAGE_TYPE_PACKET_END
:
770 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
772 BT_ASSERT(clock_class
);
774 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
777 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
778 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
780 BT_ASSERT(clock_class
);
782 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
785 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
786 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
788 BT_ASSERT(clock_class
);
790 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
793 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
794 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
798 /* All the other messages have a higher priority */
799 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
800 *ts_ns
= last_msg_ts_ns
;
804 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
805 BT_ASSERT_DBG(clock_class
);
807 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
809 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
810 "Cannot get nanoseconds from Epoch of clock snapshot: "
811 "clock-snapshot-addr=%p", clock_snapshot
);
822 BT_COMP_LOGD("Found message's timestamp: "
823 "iter-data-addr=%p, msg-addr=%p, "
824 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
825 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
832 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
833 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
834 struct lttng_live_stream_iterator
*lttng_live_stream
,
835 const bt_message
**message
)
837 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
838 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
839 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
840 enum ctf_msg_iter_status status
;
841 uint64_t session_idx
, trace_idx
;
843 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
845 struct lttng_live_session
*session
=
846 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
848 if (session
->new_streams_needed
) {
849 BT_COMP_LOGD("Need an update for streams: "
850 "session-id=%" PRIu64
, session
->id
);
851 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
854 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
856 struct lttng_live_trace
*trace
=
857 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
858 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
859 BT_COMP_LOGD("Need an update for metadata stream: "
860 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
861 session
->id
, trace
->id
);
862 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
868 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
869 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
870 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
871 "Invalid state of live stream iterator"
872 "stream-iter-status=%s",
873 lttng_live_stream_state_string(lttng_live_stream
->state
));
877 status
= ctf_msg_iter_get_next_message(
878 lttng_live_stream
->msg_iter
, message
);
880 case CTF_MSG_ITER_STATUS_EOF
:
881 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
883 case CTF_MSG_ITER_STATUS_OK
:
884 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
886 case CTF_MSG_ITER_STATUS_AGAIN
:
888 * Continue immediately (end of packet). The next
889 * get_index may return AGAIN to delay the following
892 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
894 case CTF_MSG_ITER_STATUS_ERROR
:
896 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
897 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
898 "CTF message iterator failed to get next message: "
899 "msg-iter=%p, msg-iter-status=%s",
900 lttng_live_stream
->msg_iter
,
901 ctf_msg_iter_status_string(status
));
910 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
911 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
912 struct lttng_live_stream_iterator
*stream_iter
,
913 const bt_message
**curr_msg
)
915 enum lttng_live_iterator_status live_status
=
916 LTTNG_LIVE_ITERATOR_STATUS_OK
;
917 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
918 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
920 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
921 "viewer-stream-id=%" PRIu64
, stream_iter
->name
->str
,
922 stream_iter
->viewer_stream_id
);
925 * The viewer has hung up on us so we are closing the stream. The
926 * `ctf_msg_iter` should simply realize that it needs to close the
927 * stream properly by emitting the necessary stream end message.
929 enum ctf_msg_iter_status status
= ctf_msg_iter_get_next_message(
930 stream_iter
->msg_iter
, curr_msg
);
932 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
933 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
934 "Error getting the next message from CTF message iterator");
935 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
937 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
938 BT_COMP_LOGI("Reached the end of the live stream iterator.");
939 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
943 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
951 * handle_no_data_streams()
953 * - for each ACTIVE_NO_DATA stream:
954 * - query relayd for stream data, or quiescence info.
955 * - if need metadata, get metadata, goto retry.
956 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
957 * - if quiescent, move to QUIESCENT streams
958 * - if fetched data, move to ACTIVE_DATA streams
959 * (at this point each stream either has data, or is quiescent)
963 * handle_new_streams_and_metadata()
964 * - query relayd for known streams, add them as ACTIVE_NO_DATA
965 * - query relayd for metadata
967 * call handle_active_no_data_streams()
969 * handle_quiescent_streams()
970 * - if at least one stream is ACTIVE_DATA:
971 * - peek stream event with lowest timestamp -> next_ts
972 * - for each quiescent stream
973 * - if next_ts >= quiescent end
974 * - set state to ACTIVE_NO_DATA
976 * - for each quiescent stream
977 * - set state to ACTIVE_NO_DATA
979 * call handle_active_no_data_streams()
981 * handle_active_data_streams()
982 * - if at least one stream is ACTIVE_DATA:
983 * - get stream event with lowest timestamp from heap
984 * - make that stream event the current message.
985 * - move this stream heap position to its next event
986 * - if we need to fetch data from relayd, move
987 * stream to ACTIVE_NO_DATA.
991 * end criterion: ctrl-c on client. If relayd exits or the session
992 * closes on the relay daemon side, we keep on waiting for streams.
993 * Eventually handle --end timestamp (also an end criterion).
995 * When disconnected from relayd: try to re-connect endlessly.
998 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
999 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1000 struct lttng_live_stream_iterator
*stream_iter
,
1001 const bt_message
**curr_msg
)
1003 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1004 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1005 enum lttng_live_iterator_status live_status
;
1007 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
1008 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1009 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1011 if (stream_iter
->has_stream_hung_up
) {
1013 * The stream has hung up and the stream was properly closed
1014 * during the last call to the current function. Return _END
1015 * status now so that this stream iterator is removed for the
1016 * stream iterator list.
1018 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1023 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
1026 * Make sure we have the most recent metadata and possibly some new
1029 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
1030 lttng_live_msg_iter
);
1031 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1035 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
1036 lttng_live_msg_iter
, stream_iter
);
1037 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1038 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1040 * We overwrite `live_status` since `curr_msg` is
1041 * likely set to a valid message in this function.
1043 live_status
= lttng_live_iterator_close_stream(
1044 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1049 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
1050 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1051 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1052 BT_ASSERT(!*curr_msg
);
1058 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
1059 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1060 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1061 BT_ASSERT(!*curr_msg
);
1065 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1066 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1070 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, "
1071 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1072 lttng_live_iterator_status_string(live_status
),
1073 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1079 bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1081 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1083 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1084 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1088 enum lttng_live_iterator_status
adjust_discarded_packets_message(
1089 bt_self_message_iterator
*iter
,
1090 const bt_stream
*stream
,
1091 const bt_message
*msg_in
, bt_message
**msg_out
,
1092 uint64_t new_begin_ts
)
1094 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1095 enum bt_property_availability availability
;
1096 const bt_clock_snapshot
*clock_snapshot
;
1100 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1101 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1103 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1104 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1106 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1107 iter
, stream
, new_begin_ts
, end_ts
);
1109 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1113 bt_message_discarded_packets_set_count(*msg_out
, count
);
1119 enum lttng_live_iterator_status
adjust_discarded_events_message(
1120 bt_self_message_iterator
*iter
,
1121 const bt_stream
*stream
,
1122 const bt_message
*msg_in
, bt_message
**msg_out
,
1123 uint64_t new_begin_ts
)
1125 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1126 enum bt_property_availability availability
;
1127 const bt_clock_snapshot
*clock_snapshot
;
1131 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1132 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1134 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1135 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1137 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1138 iter
, stream
, new_begin_ts
, end_ts
);
1140 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1144 bt_message_discarded_events_set_count(*msg_out
, count
);
1150 enum lttng_live_iterator_status
handle_late_message(
1151 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1152 struct lttng_live_stream_iterator
*stream_iter
,
1153 int64_t late_msg_ts_ns
, const bt_message
*late_msg
)
1155 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1156 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1157 const bt_clock_class
*clock_class
;
1158 const bt_stream_class
*stream_class
;
1159 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1160 int64_t last_inactivity_ts_ns
;
1161 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1162 enum lttng_live_iterator_status adjust_status
;
1163 bt_message
*adjusted_message
;
1166 * The timestamp of the current message is before the last message sent
1167 * by this component. We CANNOT send it as is.
1169 * The only expected scenario in which that could happen is the
1170 * following, everything else is a bug in this component, relay deamon,
1173 * Expected scenario: The CTF message iterator emitted discarded
1174 * packets and discarded events with synthesized beginning and end
1175 * timestamps from the bounds of the last known packet and the newly
1176 * decoded packet header. The CTF message iterator is not aware of
1177 * stream inactivity beacons. Hence, we have to adjust the beginning
1178 * timestamp of those types of messages if a stream signalled its
1179 * inactivity up until _after_ the last known packet's beginning
1182 * Otherwise, the monotonicity guarantee of message timestamps would
1185 * In short, the only scenario in which it's okay and fixable to
1186 * received a late message is when:
1187 * 1. the late message is a discarded packets or discarded events
1189 * 2. this stream produced an inactivity message downstream, and
1190 * 3. the timestamp of the late message is within the inactivity
1191 * timespan we sent downstream through the inactivity message.
1194 BT_COMP_LOGD("Handling late message on live stream iterator: "
1195 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1196 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1198 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1199 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1200 "have a late message when no inactivity message "
1201 "was ever sent for that stream.");
1202 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1206 if (!is_discarded_packet_or_event_message(late_msg
)) {
1207 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1208 "have a late message that is not a packet discarded or "
1209 "event discarded message: late-msg-type=%s",
1210 bt_common_message_type_string(bt_message_get_type(late_msg
)));
1211 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1215 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1216 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1218 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(clock_class
,
1219 stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1220 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1221 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,"Error converting last "
1222 "inactivity message timestamp to nanoseconds");
1223 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1227 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1228 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1229 "have a late message that is none included in a stream "
1230 "inactivity timespan: last-inactivity-ts-ns=%" PRIu64
1231 "late-msg-ts-ns=%" PRIu64
, last_inactivity_ts_ns
, late_msg_ts_ns
);
1232 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1237 * We now know that it's okay for this message to be late, we can now
1238 * adjust its timestamp to ensure monotonicity.
1240 BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, "
1241 "msg-new-ts-ns=%" PRIu64
,
1242 bt_common_message_type_string(bt_message_get_type(late_msg
)),
1243 stream_iter
->last_inactivity_ts
.value
);
1244 switch (bt_message_get_type(late_msg
)) {
1245 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1246 adjust_status
= adjust_discarded_events_message(
1247 lttng_live_msg_iter
->self_msg_iter
,
1248 stream_iter
->stream
,
1249 late_msg
, &adjusted_message
,
1250 stream_iter
->last_inactivity_ts
.value
);
1252 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1253 adjust_status
= adjust_discarded_packets_message(
1254 lttng_live_msg_iter
->self_msg_iter
,
1255 stream_iter
->stream
,
1256 late_msg
, &adjusted_message
,
1257 stream_iter
->last_inactivity_ts
.value
);
1263 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1264 stream_iter_status
= adjust_status
;
1268 BT_ASSERT_DBG(adjusted_message
);
1269 stream_iter
->current_msg
= adjusted_message
;
1270 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1271 bt_message_put_ref(late_msg
);
1274 return stream_iter_status
;
1278 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
1279 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1280 struct lttng_live_trace
*live_trace
,
1281 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1283 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1284 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1285 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1286 enum lttng_live_iterator_status stream_iter_status
;;
1287 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1288 uint64_t stream_iter_idx
;
1290 BT_ASSERT_DBG(live_trace
);
1291 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1293 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1294 "trace-id=%" PRIu64
, live_trace
->id
);
1296 * Update the current message of every stream iterators of this trace.
1297 * The current msg of every stream must have a timestamp equal or
1298 * larger than the last message returned by this iterator. We must
1299 * ensure monotonicity.
1301 stream_iter_idx
= 0;
1302 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1303 bool stream_iter_is_ended
= false;
1304 struct lttng_live_stream_iterator
*stream_iter
=
1305 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1309 * If there is no current message for this stream, go fetch
1312 while (!stream_iter
->current_msg
) {
1313 const bt_message
*msg
= NULL
;
1314 int64_t curr_msg_ts_ns
= INT64_MAX
;
1316 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1317 lttng_live_msg_iter
, stream_iter
, &msg
);
1319 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1320 stream_iter_is_ended
= true;
1324 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1330 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, "
1331 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1332 bt_common_message_type_string(bt_message_get_type(msg
)),
1333 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1336 * Get the timestamp in nanoseconds from origin of this
1339 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1340 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1344 * Check if the message of the current live stream
1345 * iterator occurred at the exact same time or after the
1346 * last message returned by this component's message
1347 * iterator. If not, we need to handle it with care.
1349 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1350 stream_iter
->current_msg
= msg
;
1351 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1354 * We received a message from the past. This
1355 * may be fixable but it can also be an error.
1357 stream_iter_status
= handle_late_message(
1358 lttng_live_msg_iter
, stream_iter
,
1359 curr_msg_ts_ns
, msg
);
1360 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1361 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1362 "Late message could not be handled correctly: "
1363 "lttng-live-msg-iter-addr=%p, "
1364 "stream-name=\"%s\", "
1365 "curr-msg-ts=%" PRId64
1366 ", last-msg-ts=%" PRId64
,
1367 lttng_live_msg_iter
,
1368 stream_iter
->name
->str
,
1370 lttng_live_msg_iter
->last_msg_ts_ns
);
1371 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1377 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1379 if (!stream_iter_is_ended
) {
1380 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1381 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1383 * Update the current best candidate message
1384 * for the stream iterator of this live trace
1385 * to be forwarded downstream.
1387 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1388 youngest_candidate_stream_iter
= stream_iter
;
1389 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1391 * Order the messages in an arbitrary but
1392 * deterministic way.
1394 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1395 int ret
= common_muxing_compare_messages(
1396 stream_iter
->current_msg
,
1397 youngest_candidate_stream_iter
->current_msg
);
1400 * The `youngest_candidate_stream_iter->current_msg`
1401 * should go first. Update the next
1402 * iterator and the current timestamp.
1404 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1405 youngest_candidate_stream_iter
= stream_iter
;
1406 } else if (ret
== 0) {
1408 * Unable to pick which one should go
1411 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1412 "stream-iter-addr=%p"
1413 "stream-iter-addr=%p",
1415 youngest_candidate_stream_iter
);
1422 * The live stream iterator has ended. That
1423 * iterator is removed from the array, but
1424 * there is no need to increment
1425 * stream_iter_idx as
1426 * g_ptr_array_remove_index_fast replaces the
1427 * removed element with the array's last
1430 g_ptr_array_remove_index_fast(
1431 live_trace
->stream_iterators
,
1436 if (youngest_candidate_stream_iter
) {
1437 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1438 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1441 * The only case where we don't have a candidate for this trace
1442 * is if we reached the end of all the iterators.
1444 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1445 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1449 return stream_iter_status
;
1453 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1454 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1455 struct lttng_live_session
*session
,
1456 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1458 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1459 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1460 enum lttng_live_iterator_status stream_iter_status
;
1461 uint64_t trace_idx
= 0;
1462 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1463 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1465 BT_COMP_LOGD("Finding the next stream iterator for session: "
1466 "session-id=%" PRIu64
, session
->id
);
1468 * Make sure we are attached to the session and look for new streams
1471 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1472 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1473 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1474 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1478 BT_ASSERT_DBG(session
->traces
);
1480 while (trace_idx
< session
->traces
->len
) {
1481 bool trace_is_ended
= false;
1482 struct lttng_live_stream_iterator
*stream_iter
;
1483 struct lttng_live_trace
*trace
=
1484 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1486 stream_iter_status
= next_stream_iterator_for_trace(
1487 lttng_live_msg_iter
, trace
, &stream_iter
);
1488 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1490 * All the live stream iterators for this trace are
1491 * ENDed. Remove the trace from this session.
1493 trace_is_ended
= true;
1494 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1498 if (!trace_is_ended
) {
1499 BT_ASSERT_DBG(stream_iter
);
1501 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1502 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1503 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1504 youngest_candidate_stream_iter
= stream_iter
;
1505 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1507 * Order the messages in an arbitrary but
1508 * deterministic way.
1510 int ret
= common_muxing_compare_messages(
1511 stream_iter
->current_msg
,
1512 youngest_candidate_stream_iter
->current_msg
);
1515 * The `youngest_candidate_stream_iter->current_msg`
1516 * should go first. Update the next iterator
1517 * and the current timestamp.
1519 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1520 youngest_candidate_stream_iter
= stream_iter
;
1521 } else if (ret
== 0) {
1522 /* Unable to pick which one should go first. */
1523 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1524 "stream-iter-addr=%p" "stream-iter-addr=%p",
1525 stream_iter
, youngest_candidate_stream_iter
);
1531 * trace_idx is not incremented since
1532 * g_ptr_array_remove_index_fast replaces the
1533 * element at trace_idx with the array's last element.
1535 g_ptr_array_remove_index_fast(session
->traces
,
1539 if (youngest_candidate_stream_iter
) {
1540 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1541 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1544 * The only cases where we don't have a candidate for this
1546 * 1. if we reached the end of all the iterators of all the
1547 * traces of this session,
1548 * 2. if we never had live stream iterator in the first place.
1550 * In either cases, we return END.
1552 BT_ASSERT(session
->traces
->len
== 0);
1553 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1556 return stream_iter_status
;
1560 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1564 for (i
= 0; i
< count
; i
++) {
1565 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1570 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
1571 bt_self_message_iterator
*self_msg_it
,
1572 bt_message_array_const msgs
, uint64_t capacity
,
1575 bt_message_iterator_class_next_method_status status
;
1576 enum lttng_live_viewer_status viewer_status
;
1577 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1578 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1579 struct lttng_live_component
*lttng_live
=
1580 lttng_live_msg_iter
->lttng_live_comp
;
1581 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1582 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1583 enum lttng_live_iterator_status stream_iter_status
;
1584 uint64_t session_idx
;
1588 BT_ASSERT_DBG(lttng_live_msg_iter
);
1590 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1592 * The iterator was interrupted in a previous call to the
1593 * `_next()` method. We currently do not support generating
1594 * messages after such event. The babeltrace2 CLI should never
1595 * be running the graph after being interrupted. So this check
1596 * is to prevent other graph users from using this live
1597 * iterator in an messed up internal state.
1599 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1600 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1601 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1606 * Clear all the invalid message reference that might be left over in
1609 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1612 * If no session are exposed on the relay found at the url provided by
1613 * the user, session count will be 0. In this case, we return status
1614 * end to return gracefully.
1616 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1617 if (lttng_live
->params
.sess_not_found_act
!=
1618 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1619 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1623 * The are no more active session for this session
1624 * name. Retry to create a viewer session for the
1625 * requested session name.
1627 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1628 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1629 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1630 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1631 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1632 "Error creating LTTng live viewer session");
1633 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1634 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1643 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1644 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1648 * Here the muxing of message is done.
1650 * We need to iterate over all the streams of all the traces of all the
1651 * viewer sessions in order to get the message with the smallest
1652 * timestamp. In this case, a session is a viewer session and there is
1653 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1654 * kernel). Each viewer session can have multiple traces, for example,
1655 * 64bit UST viewer sessions could have multiple per-pid traces.
1657 * We iterate over the streams of each traces to update and see what is
1658 * their next message's timestamp. From those timestamps, we select the
1659 * message with the smallest timestamp as the best candidate message
1660 * for that trace and do the same thing across all the sessions.
1662 * We then compare the timestamp of best candidate message of all the
1663 * sessions to pick the message with the smallest timestamp and we
1666 while (*count
< capacity
) {
1667 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1668 *candidate_stream_iter
= NULL
;
1669 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1671 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1673 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1674 struct lttng_live_session
*session
=
1675 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1678 /* Find the best candidate message to send downstream. */
1679 stream_iter_status
= next_stream_iterator_for_session(
1680 lttng_live_msg_iter
, session
,
1681 &candidate_stream_iter
);
1683 /* If we receive an END status, it means that either:
1684 * - Those traces never had active streams (UST with no
1685 * data produced yet),
1686 * - All live stream iterators have ENDed.*/
1687 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1688 if (session
->closed
&& session
->traces
->len
== 0) {
1690 * Remove the session from the list.
1691 * session_idx is not modified since
1692 * g_ptr_array_remove_index_fast
1693 * replaces the the removed element with
1694 * the array's last element.
1696 g_ptr_array_remove_index_fast(
1697 lttng_live_msg_iter
->sessions
,
1705 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1709 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1710 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1711 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1712 youngest_stream_iter
= candidate_stream_iter
;
1713 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1715 * The currently selected message to be sent
1716 * downstream next has the exact same timestamp
1717 * that of the current candidate message. We
1718 * must break the tie in a predictable manner.
1720 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1722 * Order the messages in an arbitrary but
1723 * deterministic way.
1725 int ret
= common_muxing_compare_messages(
1726 candidate_stream_iter
->current_msg
,
1727 youngest_stream_iter
->current_msg
);
1730 * The `candidate_stream_iter->current_msg`
1731 * should go first. Update the next
1732 * iterator and the current timestamp.
1734 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1735 youngest_stream_iter
= candidate_stream_iter
;
1736 } else if (ret
== 0) {
1737 /* Unable to pick which one should go first. */
1738 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1739 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1740 youngest_stream_iter
, candidate_stream_iter
);
1747 if (!youngest_stream_iter
) {
1748 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1752 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1753 /* Ensure monotonicity. */
1754 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1755 youngest_stream_iter
->current_msg_ts_ns
);
1758 * Insert the next message to the message batch. This will set
1759 * stream iterator current messsage to NULL so that next time
1760 * we fetch the next message of that stream iterator
1762 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1765 /* Update the last timestamp in nanoseconds sent downstream. */
1766 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1767 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1769 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1773 switch (stream_iter_status
) {
1774 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1775 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1777 * If we gathered messages, return _OK even if the graph was
1778 * interrupted. This allows for the components downstream to at
1779 * least get the thoses messages. If the graph was indeed
1780 * interrupted there should not be another _next() call as the
1781 * application will tear down the graph. This component class
1782 * doesn't support restarting after an interruption.
1785 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1787 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1790 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1791 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1793 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1794 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1795 "Memory error preparing the next batch of messages: "
1796 "live-iter-status=%s",
1797 lttng_live_iterator_status_string(stream_iter_status
));
1798 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1800 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1801 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1802 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1803 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1804 "Error preparing the next batch of messages: "
1805 "live-iter-status=%s",
1806 lttng_live_iterator_status_string(stream_iter_status
));
1808 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1809 /* Put all existing messages on error. */
1810 put_messages(msgs
, *count
);
1821 struct lttng_live_msg_iter
*lttng_live_msg_iter_create(
1822 struct lttng_live_component
*lttng_live_comp
,
1823 bt_self_message_iterator
*self_msg_it
)
1825 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1826 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1828 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1829 g_new0(struct lttng_live_msg_iter
, 1);
1830 if (!lttng_live_msg_iter
) {
1831 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1832 "Failed to allocate lttng_live_msg_iter");
1836 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1837 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1838 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1839 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1841 lttng_live_msg_iter
->active_stream_iter
= 0;
1842 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1843 lttng_live_msg_iter
->was_interrupted
= false;
1845 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1846 (GDestroyNotify
) lttng_live_destroy_session
);
1847 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1850 return lttng_live_msg_iter
;
1855 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
1856 bt_self_message_iterator
*self_msg_it
,
1857 bt_self_message_iterator_configuration
*config
,
1858 bt_self_component_port_output
*self_port
)
1860 bt_message_iterator_class_initialize_method_status status
;
1861 struct lttng_live_component
*lttng_live
;
1862 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1863 enum lttng_live_viewer_status viewer_status
;
1864 bt_logging_level log_level
;
1865 bt_self_component
*self_comp
=
1866 bt_self_message_iterator_borrow_component(self_msg_it
);
1868 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1869 log_level
= lttng_live
->log_level
;
1870 self_comp
= lttng_live
->self_comp
;
1873 /* There can be only one downstream iterator at the same time. */
1874 BT_ASSERT(!lttng_live
->has_msg_iter
);
1875 lttng_live
->has_msg_iter
= true;
1877 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
,
1879 if (!lttng_live_msg_iter
) {
1880 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1881 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1882 "Failed to create lttng_live_msg_iter");
1886 viewer_status
= live_viewer_connection_create(self_comp
, NULL
,
1887 log_level
, lttng_live
->params
.url
->str
, false,
1888 lttng_live_msg_iter
, <tng_live_msg_iter
->viewer_connection
);
1889 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1890 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1891 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1892 "Failed to create viewer connection");
1893 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1895 * Interruption in the _iter_init() method is not
1896 * supported. Return an error.
1898 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1899 "Interrupted while creating viewer connection");
1904 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1905 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1906 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1907 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1908 "Failed to create viewer session");
1909 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1911 * Interruption in the _iter_init() method is not
1912 * supported. Return an error.
1914 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1915 "Interrupted when creating viewer session");
1920 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1921 switch (lttng_live
->params
.sess_not_found_act
) {
1922 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1923 BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of "
1924 "%s=\"%s\" component parameter: url=\"%s\"",
1925 SESS_NOT_FOUND_ACTION_PARAM
,
1926 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1927 lttng_live
->params
.url
->str
);
1929 case SESSION_NOT_FOUND_ACTION_FAIL
:
1930 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1931 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1932 "component parameter: url =\"%s\"",
1933 SESS_NOT_FOUND_ACTION_PARAM
,
1934 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1935 lttng_live
->params
.url
->str
);
1937 case SESSION_NOT_FOUND_ACTION_END
:
1938 BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1939 "call because of %s=\"%s\" component parameter: "
1940 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1941 SESS_NOT_FOUND_ACTION_END_STR
,
1942 lttng_live
->params
.url
->str
);
1949 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1950 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1954 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1955 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1960 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1961 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1962 { bt_param_validation_value_descr::string_t
} },
1963 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1967 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1968 const bt_value
*params
, const bt_value
**result
,
1969 bt_self_component_class
*self_comp_class
,
1970 bt_logging_level log_level
)
1972 bt_component_class_query_method_status status
;
1973 const bt_value
*url_value
= NULL
;
1975 struct live_viewer_connection
*viewer_connection
= NULL
;
1976 enum lttng_live_viewer_status viewer_status
;
1977 enum bt_param_validation_status validation_status
;
1978 gchar
*validate_error
= NULL
;
1980 validation_status
= bt_param_validation_validate(params
,
1981 list_sessions_params
, &validate_error
);
1982 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1983 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1985 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1986 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1987 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1992 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1993 url
= bt_value_string_get(url_value
);
1995 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
,
1996 log_level
, url
, true, NULL
, &viewer_connection
);
1997 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1998 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1999 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2000 "Failed to create viewer connection");
2001 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2002 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
2003 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
2010 status
= live_viewer_connection_list_sessions(viewer_connection
,
2012 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
2013 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2014 "Failed to list viewer sessions");
2021 BT_VALUE_PUT_REF_AND_RESET(*result
);
2024 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2028 if (viewer_connection
) {
2029 live_viewer_connection_destroy(viewer_connection
);
2032 g_free(validate_error
);
2038 bt_component_class_query_method_status
lttng_live_query_support_info(
2039 const bt_value
*params
, const bt_value
**result
,
2040 bt_self_component_class
*self_comp_class
,
2041 bt_logging_level log_level
)
2043 bt_component_class_query_method_status status
=
2044 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2045 const bt_value
*input_type_value
;
2046 const bt_value
*input_value
;
2048 struct bt_common_lttng_live_url_parts parts
= { 0 };
2050 /* Used by the logging macros */
2051 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
2054 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
2056 if (!input_type_value
) {
2057 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2058 "Missing expected `type` parameter.");
2062 if (!bt_value_is_string(input_type_value
)) {
2063 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2064 "`type` parameter is not a string value.");
2068 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
2069 /* We don't handle file system paths */
2073 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
2075 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2076 "Missing expected `input` parameter.");
2080 if (!bt_value_is_string(input_value
)) {
2081 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2082 "`input` parameter is not a string value.");
2086 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
2088 if (parts
.session_name
) {
2090 * Looks pretty much like an LTTng live URL: we got the
2091 * session name part, which forms a complete URL.
2097 *result
= bt_value_real_create_init(weight
);
2099 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2107 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2110 BT_ASSERT(!*result
);
2113 bt_common_destroy_lttng_live_url_parts(&parts
);
2118 bt_component_class_query_method_status
lttng_live_query(
2119 bt_self_component_class_source
*comp_class
,
2120 bt_private_query_executor
*priv_query_exec
,
2121 const char *object
, const bt_value
*params
,
2122 __attribute__((unused
)) void *method_data
,
2123 const bt_value
**result
)
2125 bt_component_class_query_method_status status
=
2126 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2127 bt_self_component
*self_comp
= NULL
;
2128 bt_self_component_class
*self_comp_class
=
2129 bt_self_component_class_source_as_self_component_class(comp_class
);
2130 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2131 bt_private_query_executor_as_query_executor_const(
2134 if (strcmp(object
, "sessions") == 0) {
2135 status
= lttng_live_query_list_sessions(params
, result
,
2136 self_comp_class
, log_level
);
2137 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2138 status
= lttng_live_query_support_info(params
, result
,
2139 self_comp_class
, log_level
);
2141 BT_COMP_LOGI("Unknown query object `%s`", object
);
2142 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2151 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2156 if (lttng_live
->params
.url
) {
2157 g_string_free(lttng_live
->params
.url
, TRUE
);
2163 void lttng_live_component_finalize(bt_self_component_source
*component
)
2165 lttng_live_component
*data
=
2166 (lttng_live_component
*) bt_self_component_get_data(
2167 bt_self_component_source_as_self_component(component
));
2172 lttng_live_component_destroy_data(data
);
2176 enum session_not_found_action
parse_session_not_found_action_param(
2177 const bt_value
*no_session_param
)
2179 enum session_not_found_action action
;
2180 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2182 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2183 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2184 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2185 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2187 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2188 action
= SESSION_NOT_FOUND_ACTION_END
;
2194 static bt_param_validation_value_descr inputs_elem_descr
{
2195 bt_param_validation_value_descr::string_t
,
2198 static const char *sess_not_found_action_choices
[] = {
2199 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2200 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2201 SESS_NOT_FOUND_ACTION_END_STR
,
2204 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2205 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
2206 { bt_param_validation_value_descr::array_t
,
2207 1, 1, inputs_elem_descr
} },
2208 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
2209 { bt_param_validation_value_descr::string_t
,
2210 sess_not_found_action_choices
} },
2211 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2215 bt_component_class_initialize_method_status
lttng_live_component_create(
2216 const bt_value
*params
,
2217 bt_logging_level log_level
,
2218 bt_self_component
*self_comp
,
2219 struct lttng_live_component
**component
)
2221 struct lttng_live_component
*lttng_live
= NULL
;
2222 const bt_value
*inputs_value
;
2223 const bt_value
*url_value
;
2224 const bt_value
*value
;
2226 enum bt_param_validation_status validation_status
;
2227 gchar
*validation_error
= NULL
;
2228 bt_component_class_initialize_method_status status
;
2230 validation_status
= bt_param_validation_validate(params
, params_descr
,
2232 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2233 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2235 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2236 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2237 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2241 lttng_live
= g_new0(struct lttng_live_component
, 1);
2243 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2246 lttng_live
->log_level
= log_level
;
2247 lttng_live
->self_comp
= self_comp
;
2248 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2249 lttng_live
->has_msg_iter
= false;
2252 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2254 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2255 url
= bt_value_string_get(url_value
);
2257 lttng_live
->params
.url
= g_string_new(url
);
2258 if (!lttng_live
->params
.url
) {
2259 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2263 value
= bt_value_map_borrow_entry_value_const(params
,
2264 SESS_NOT_FOUND_ACTION_PARAM
);
2266 lttng_live
->params
.sess_not_found_act
=
2267 parse_session_not_found_action_param(value
);
2269 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2270 "defaulting to `%s`.",
2271 SESS_NOT_FOUND_ACTION_PARAM
,
2272 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2273 lttng_live
->params
.sess_not_found_act
=
2274 SESSION_NOT_FOUND_ACTION_CONTINUE
;
2277 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2281 lttng_live_component_destroy_data(lttng_live
);
2284 g_free(validation_error
);
2286 *component
= lttng_live
;
2291 bt_component_class_initialize_method_status
lttng_live_component_init(
2292 bt_self_component_source
*self_comp_src
,
2293 bt_self_component_source_configuration
*config
,
2294 const bt_value
*params
,
2295 __attribute__((unused
)) void *init_method_data
)
2297 struct lttng_live_component
*lttng_live
;
2298 bt_component_class_initialize_method_status ret
;
2299 bt_self_component
*self_comp
=
2300 bt_self_component_source_as_self_component(self_comp_src
);
2301 bt_logging_level log_level
= bt_component_get_logging_level(
2302 bt_self_component_as_component(self_comp
));
2303 bt_self_component_add_port_status add_port_status
;
2305 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2306 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2310 add_port_status
= bt_self_component_source_add_output_port(
2311 self_comp_src
, "out", NULL
, NULL
);
2312 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2313 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2317 bt_self_component_set_data(self_comp
, lttng_live
);
2321 lttng_live_component_destroy_data(lttng_live
);