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__)
44 static const char *lttng_live_iterator_status_string(enum lttng_live_iterator_status status
)
47 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
48 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
49 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
50 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
51 case LTTNG_LIVE_ITERATOR_STATUS_END
:
52 return "LTTNG_LIVE_ITERATOR_STATUS_END";
53 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
54 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
55 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
56 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
57 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
58 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
59 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
60 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
61 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
62 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
68 static const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
71 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
72 return "ACTIVE_NO_DATA";
73 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
74 return "QUIESCENT_NO_DATA";
75 case LTTNG_LIVE_STREAM_QUIESCENT
:
77 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
79 case LTTNG_LIVE_STREAM_EOF
:
86 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
87 enum lttng_live_stream_state new_state
)
89 bt_self_component
*self_comp
= stream_iter
->self_comp
;
90 bt_logging_level log_level
= stream_iter
->log_level
;
92 BT_COMP_LOGD("Setting live stream iterator state: viewer-stream-id=%" PRIu64
93 ", old-state=%s, new-state=%s",
94 stream_iter
->viewer_stream_id
, lttng_live_stream_state_string(stream_iter
->state
),
95 lttng_live_stream_state_string(new_state
));
97 stream_iter
->state
= new_state
;
100 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
102 BT_COMP_LOGD("Live stream iterator state=%s, " \
103 "last-inact-ts-is-set=%d, last-inact-ts-value=%" PRId64 ", " \
104 "curr-inact-ts=%" PRId64, \
105 lttng_live_stream_state_string(live_stream_iter->state), \
106 live_stream_iter->last_inactivity_ts.is_set, \
107 live_stream_iter->last_inactivity_ts.value, \
108 live_stream_iter->current_inactivity_ts); \
112 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
121 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
127 static struct lttng_live_trace
*
128 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
131 struct lttng_live_trace
*ret_trace
= NULL
;
133 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
134 struct lttng_live_trace
*trace
=
135 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
136 if (trace
->id
== trace_id
) {
146 static void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
148 bt_logging_level log_level
= trace
->log_level
;
149 bt_self_component
*self_comp
= trace
->self_comp
;
151 BT_COMP_LOGD("Destroying live trace: trace-id=%" PRIu64
, trace
->id
);
153 BT_ASSERT(trace
->stream_iterators
);
154 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
156 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
157 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
159 lttng_live_metadata_fini(trace
);
163 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
166 struct lttng_live_trace
*trace
= NULL
;
167 bt_logging_level log_level
= session
->log_level
;
168 bt_self_component
*self_comp
= session
->self_comp
;
170 BT_COMP_LOGD("Creating live trace: "
171 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
172 session
->id
, trace_id
);
173 trace
= g_new0(struct lttng_live_trace
, 1);
175 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate live trace");
178 trace
->log_level
= session
->log_level
;
179 trace
->self_comp
= session
->self_comp
;
180 trace
->session
= session
;
181 trace
->id
= trace_id
;
182 trace
->trace_class
= NULL
;
184 trace
->stream_iterators
=
185 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_stream_iterator_destroy
);
186 BT_ASSERT(trace
->stream_iterators
);
187 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
188 g_ptr_array_add(session
->traces
, trace
);
199 struct lttng_live_trace
*
200 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
203 struct lttng_live_trace
*trace
;
205 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
210 /* The session is the owner of the newly created trace. */
211 trace
= lttng_live_create_trace(session
, trace_id
);
218 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
219 const char *hostname
, const char *session_name
)
222 struct lttng_live_session
*session
;
223 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
224 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
226 BT_COMP_LOGD("Adding live session: "
227 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
228 session_id
, hostname
, session_name
);
230 session
= g_new0(struct lttng_live_session
, 1);
232 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate live session");
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((GDestroyNotify
) lttng_live_destroy_trace
);
240 BT_ASSERT(session
->traces
);
241 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
242 session
->new_streams_needed
= true;
243 session
->hostname
= g_string_new(hostname
);
244 BT_ASSERT(session
->hostname
);
246 session
->session_name
= g_string_new(session_name
);
247 BT_ASSERT(session
->session_name
);
249 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
258 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
260 bt_logging_level log_level
;
261 bt_self_component
*self_comp
;
267 log_level
= session
->log_level
;
268 self_comp
= session
->self_comp
;
269 BT_COMP_LOGD("Destroying live session: "
270 "session-id=%" PRIu64
", session-name=\"%s\"",
271 session
->id
, session
->session_name
->str
);
272 if (session
->id
!= -1ULL) {
273 if (lttng_live_session_detach(session
)) {
274 if (!lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
275 /* Old relayd cannot detach sessions. */
276 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
, session
->id
);
282 if (session
->traces
) {
283 g_ptr_array_free(session
->traces
, TRUE
);
286 if (session
->hostname
) {
287 g_string_free(session
->hostname
, TRUE
);
289 if (session
->session_name
) {
290 g_string_free(session
->session_name
, TRUE
);
298 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
300 if (!lttng_live_msg_iter
) {
304 if (lttng_live_msg_iter
->sessions
) {
305 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
308 if (lttng_live_msg_iter
->viewer_connection
) {
309 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
311 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
312 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
314 /* All stream iterators must be destroyed at this point. */
315 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
316 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
318 g_free(lttng_live_msg_iter
);
325 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
327 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
329 BT_ASSERT(self_msg_iter
);
331 lttng_live_msg_iter
=
332 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
333 BT_ASSERT(lttng_live_msg_iter
);
334 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
337 static enum lttng_live_iterator_status
338 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
340 bt_logging_level log_level
= lttng_live_stream
->log_level
;
341 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
343 switch (lttng_live_stream
->state
) {
344 case LTTNG_LIVE_STREAM_QUIESCENT
:
345 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
347 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
349 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
351 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
353 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
355 case LTTNG_LIVE_STREAM_EOF
:
358 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
362 * For active no data stream, fetch next index. As a result of that it can
364 * - quiescent: won't have events for a bit,
365 * - have data: need to get that data and produce the event,
366 * - have no data on this stream at this point: need to retry (AGAIN) or return
369 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
370 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
371 struct lttng_live_stream_iterator
*lttng_live_stream
)
373 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
374 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
375 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
376 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
377 struct packet_index index
;
379 if (lttng_live_stream
->trace
->metadata_stream_state
==
380 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
382 "Need to get an update for the metadata stream before proceeding further with this stream: "
383 "stream-name=\"%s\"",
384 lttng_live_stream
->name
->str
);
385 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
389 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
391 "Need to get an update of all streams before proceeding further with this stream: "
392 "stream-name=\"%s\"",
393 lttng_live_stream
->name
->str
);
394 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
398 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
399 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
402 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
403 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
407 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
409 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
410 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
411 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
413 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
415 * Because the stream is in the QUIESCENT_NO_DATA
416 * state, we can assert that the last_inactivity_ts was
417 * set and can be safely used in the `if` above.
419 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
421 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
422 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
424 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
429 lttng_live_stream
->base_offset
= index
.offset
;
430 lttng_live_stream
->offset
= index
.offset
;
431 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
433 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
434 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
435 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
436 lttng_live_stream
->name
->str
, lttng_live_stream
->viewer_stream_id
,
437 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
, lttng_live_stream
->len
);
440 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
441 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
447 * Creation of the message requires the ctf trace class to be created
448 * beforehand, but the live protocol gives us all streams (including metadata)
449 * at once. So we split it in three steps: getting streams, getting metadata
450 * (which creates the ctf trace class), and then creating the per-stream
453 static enum lttng_live_iterator_status
454 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
455 struct lttng_live_session
*session
)
457 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
458 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
459 enum lttng_live_iterator_status status
;
462 if (!session
->attached
) {
463 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
, session
->id
);
464 enum lttng_live_viewer_status attach_status
=
465 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
466 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
467 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
469 * Clear any causes appended in
470 * `lttng_live_attach_session()` as we want to
471 * return gracefully since the graph was
474 bt_current_thread_clear_error();
475 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
477 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
478 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error attaching to LTTng live session");
484 BT_COMP_LOGD("Updating all streams and metadata for session: "
485 "session-id=%" PRIu64
", session-name=\"%s\"",
486 session
->id
, session
->session_name
->str
);
488 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
489 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&& status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
494 while (trace_idx
< session
->traces
->len
) {
495 struct lttng_live_trace
*trace
=
496 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
498 status
= lttng_live_metadata_update(trace
);
500 case LTTNG_LIVE_ITERATOR_STATUS_END
:
501 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
504 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
505 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
508 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
509 "Error updating trace metadata: "
510 "stream-iter-status=%s, trace-id=%" PRIu64
,
511 lttng_live_iterator_status_string(status
), trace
->id
);
517 * Now that we have the metadata we can initialize the downstream
520 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
527 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
529 uint64_t session_idx
, trace_idx
;
530 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
531 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
533 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
534 struct lttng_live_session
*session
=
535 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
536 BT_COMP_LOGD("Force marking session as needing new streams: "
537 "session-id=%" PRIu64
,
539 session
->new_streams_needed
= true;
540 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
541 struct lttng_live_trace
*trace
=
542 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
543 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
544 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
545 session
->id
, trace
->id
);
547 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
549 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
554 static enum lttng_live_iterator_status
555 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
557 enum lttng_live_iterator_status status
;
558 enum lttng_live_viewer_status viewer_status
;
559 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
560 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
561 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
562 struct lttng_live_session
*session
;
563 enum session_not_found_action sess_not_found_act
=
564 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
566 BT_COMP_LOGD("Update data and metadata of all sessions: "
567 "live-msg-iter-addr=%p",
568 lttng_live_msg_iter
);
570 * In a remotely distant future, we could add a "new
571 * session" flag to the protocol, which would tell us that we
572 * need to query for new sessions even though we have sessions
575 if (lttng_live_msg_iter
->sessions
->len
== 0) {
576 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
578 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
579 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
583 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
585 * Retry to create a viewer session for the requested
588 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
589 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
590 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
591 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
592 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
593 "Error creating LTTng live viewer session");
594 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
595 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
604 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
606 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
607 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
609 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
610 case LTTNG_LIVE_ITERATOR_STATUS_END
:
615 if (!session
->closed
) {
616 nr_sessions_opened
++;
620 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
621 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
623 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
630 static enum lttng_live_iterator_status
631 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
632 struct lttng_live_stream_iterator
*stream_iter
, const bt_message
**message
,
635 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
636 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
637 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
638 bt_message
*msg
= NULL
;
640 BT_ASSERT(stream_iter
->trace
->clock_class
);
642 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
643 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
644 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
, timestamp
);
646 msg
= bt_message_message_iterator_inactivity_create(lttng_live_msg_iter
->self_msg_iter
,
647 stream_iter
->trace
->clock_class
, timestamp
);
649 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error emitting message iterator inactivity message");
658 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
659 bt_message_put_ref(msg
);
663 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
664 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
665 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
667 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
669 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
670 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
674 * Check if we already sent an inactivty message downstream for this
675 * `current_inactivity_ts` value.
677 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
678 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
679 lttng_live_stream_iterator_set_state(lttng_live_stream
,
680 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
682 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
686 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
687 lttng_live_stream
->current_inactivity_ts
);
689 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
690 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
695 static int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
696 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
697 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
699 const bt_clock_class
*clock_class
= NULL
;
700 const bt_clock_snapshot
*clock_snapshot
= NULL
;
702 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
703 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
706 BT_ASSERT_DBG(ts_ns
);
708 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
709 "last-msg-ts=%" PRId64
,
710 lttng_live_msg_iter
, msg
, last_msg_ts_ns
);
712 switch (bt_message_get_type(msg
)) {
713 case BT_MESSAGE_TYPE_EVENT
:
714 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(msg
);
715 BT_ASSERT_DBG(clock_class
);
717 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
719 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
721 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(msg
);
722 BT_ASSERT(clock_class
);
724 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
726 case BT_MESSAGE_TYPE_PACKET_END
:
727 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(msg
);
728 BT_ASSERT(clock_class
);
730 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
732 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
734 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(msg
);
735 BT_ASSERT(clock_class
);
738 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
740 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
742 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(msg
);
743 BT_ASSERT(clock_class
);
746 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
748 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
749 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
752 /* All the other messages have a higher priority */
753 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
754 *ts_ns
= last_msg_ts_ns
;
758 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
759 BT_ASSERT_DBG(clock_class
);
761 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
763 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
764 "Cannot get nanoseconds from Epoch of clock snapshot: "
765 "clock-snapshot-addr=%p",
777 BT_COMP_LOGD("Found message's timestamp: "
778 "iter-data-addr=%p, msg-addr=%p, "
779 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
780 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
786 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
787 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
788 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
790 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
791 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
792 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
793 enum ctf_msg_iter_status status
;
794 uint64_t session_idx
, trace_idx
;
796 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
797 struct lttng_live_session
*session
=
798 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
800 if (session
->new_streams_needed
) {
801 BT_COMP_LOGD("Need an update for streams: "
802 "session-id=%" PRIu64
,
804 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
807 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
808 struct lttng_live_trace
*trace
=
809 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
810 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
811 BT_COMP_LOGD("Need an update for metadata stream: "
812 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
813 session
->id
, trace
->id
);
814 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
820 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
821 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
822 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
823 "Invalid state of live stream iterator"
824 "stream-iter-status=%s",
825 lttng_live_stream_state_string(lttng_live_stream
->state
));
829 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
, message
);
831 case CTF_MSG_ITER_STATUS_EOF
:
832 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
834 case CTF_MSG_ITER_STATUS_OK
:
835 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
837 case CTF_MSG_ITER_STATUS_AGAIN
:
839 * Continue immediately (end of packet). The next
840 * get_index may return AGAIN to delay the following
843 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
845 case CTF_MSG_ITER_STATUS_ERROR
:
847 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
848 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
849 "CTF message iterator failed to get next message: "
850 "msg-iter=%p, msg-iter-status=%s",
851 lttng_live_stream
->msg_iter
, ctf_msg_iter_status_string(status
));
859 static enum lttng_live_iterator_status
860 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
861 struct lttng_live_stream_iterator
*stream_iter
,
862 const bt_message
**curr_msg
)
864 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
865 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
866 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
868 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
869 "viewer-stream-id=%" PRIu64
,
870 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
873 * The viewer has hung up on us so we are closing the stream. The
874 * `ctf_msg_iter` should simply realize that it needs to close the
875 * stream properly by emitting the necessary stream end message.
877 enum ctf_msg_iter_status status
=
878 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
, curr_msg
);
880 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
881 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
882 "Error getting the next message from CTF message iterator");
883 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
885 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
886 BT_COMP_LOGI("Reached the end of the live stream iterator.");
887 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
891 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
899 * handle_no_data_streams()
901 * - for each ACTIVE_NO_DATA stream:
902 * - query relayd for stream data, or quiescence info.
903 * - if need metadata, get metadata, goto retry.
904 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
905 * - if quiescent, move to QUIESCENT streams
906 * - if fetched data, move to ACTIVE_DATA streams
907 * (at this point each stream either has data, or is quiescent)
911 * handle_new_streams_and_metadata()
912 * - query relayd for known streams, add them as ACTIVE_NO_DATA
913 * - query relayd for metadata
915 * call handle_active_no_data_streams()
917 * handle_quiescent_streams()
918 * - if at least one stream is ACTIVE_DATA:
919 * - peek stream event with lowest timestamp -> next_ts
920 * - for each quiescent stream
921 * - if next_ts >= quiescent end
922 * - set state to ACTIVE_NO_DATA
924 * - for each quiescent stream
925 * - set state to ACTIVE_NO_DATA
927 * call handle_active_no_data_streams()
929 * handle_active_data_streams()
930 * - if at least one stream is ACTIVE_DATA:
931 * - get stream event with lowest timestamp from heap
932 * - make that stream event the current message.
933 * - move this stream heap position to its next event
934 * - if we need to fetch data from relayd, move
935 * stream to ACTIVE_NO_DATA.
939 * end criterion: ctrl-c on client. If relayd exits or the session
940 * closes on the relay daemon side, we keep on waiting for streams.
941 * Eventually handle --end timestamp (also an end criterion).
943 * When disconnected from relayd: try to re-connect endlessly.
945 static enum lttng_live_iterator_status
946 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
947 struct lttng_live_stream_iterator
*stream_iter
,
948 const bt_message
**curr_msg
)
950 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
951 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
952 enum lttng_live_iterator_status live_status
;
954 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
955 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
956 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
958 if (stream_iter
->has_stream_hung_up
) {
960 * The stream has hung up and the stream was properly closed
961 * during the last call to the current function. Return _END
962 * status now so that this stream iterator is removed for the
963 * stream iterator list.
965 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
970 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
973 * Make sure we have the most recent metadata and possibly some new
976 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
977 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
982 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
983 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
984 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
986 * We overwrite `live_status` since `curr_msg` is
987 * likely set to a valid message in this function.
990 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
995 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
996 stream_iter
, curr_msg
);
997 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
998 BT_ASSERT(!*curr_msg
);
1004 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
1005 stream_iter
, curr_msg
);
1006 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1007 BT_ASSERT(!*curr_msg
);
1011 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1012 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1016 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, "
1017 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1018 lttng_live_iterator_status_string(live_status
), stream_iter
->name
->str
,
1019 stream_iter
->viewer_stream_id
);
1024 static bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1026 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1028 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1029 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1032 static enum lttng_live_iterator_status
1033 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1034 const bt_message
*msg_in
, bt_message
**msg_out
,
1035 uint64_t new_begin_ts
)
1037 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1038 enum bt_property_availability availability
;
1039 const bt_clock_snapshot
*clock_snapshot
;
1043 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1044 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1046 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1047 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1049 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1050 iter
, stream
, new_begin_ts
, end_ts
);
1052 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1056 bt_message_discarded_packets_set_count(*msg_out
, count
);
1061 static enum lttng_live_iterator_status
1062 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1063 const bt_message
*msg_in
, bt_message
**msg_out
,
1064 uint64_t new_begin_ts
)
1066 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1067 enum bt_property_availability availability
;
1068 const bt_clock_snapshot
*clock_snapshot
;
1072 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1073 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1075 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1076 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1078 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1079 iter
, stream
, new_begin_ts
, end_ts
);
1081 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1085 bt_message_discarded_events_set_count(*msg_out
, count
);
1090 static enum lttng_live_iterator_status
1091 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1092 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
1093 const bt_message
*late_msg
)
1095 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1096 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1097 const bt_clock_class
*clock_class
;
1098 const bt_stream_class
*stream_class
;
1099 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1100 int64_t last_inactivity_ts_ns
;
1101 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1102 enum lttng_live_iterator_status adjust_status
;
1103 bt_message
*adjusted_message
;
1106 * The timestamp of the current message is before the last message sent
1107 * by this component. We CANNOT send it as is.
1109 * The only expected scenario in which that could happen is the
1110 * following, everything else is a bug in this component, relay deamon,
1113 * Expected scenario: The CTF message iterator emitted discarded
1114 * packets and discarded events with synthesized beginning and end
1115 * timestamps from the bounds of the last known packet and the newly
1116 * decoded packet header. The CTF message iterator is not aware of
1117 * stream inactivity beacons. Hence, we have to adjust the beginning
1118 * timestamp of those types of messages if a stream signalled its
1119 * inactivity up until _after_ the last known packet's beginning
1122 * Otherwise, the monotonicity guarantee of message timestamps would
1125 * In short, the only scenario in which it's okay and fixable to
1126 * received a late message is when:
1127 * 1. the late message is a discarded packets or discarded events
1129 * 2. this stream produced an inactivity message downstream, and
1130 * 3. the timestamp of the late message is within the inactivity
1131 * timespan we sent downstream through the inactivity message.
1134 BT_COMP_LOGD("Handling late message on live stream iterator: "
1135 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1136 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1138 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1139 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1140 "have a late message when no inactivity message "
1141 "was ever sent for that stream.");
1142 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1146 if (!is_discarded_packet_or_event_message(late_msg
)) {
1147 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1148 "Invalid live stream state: "
1149 "have a late message that is not a packet discarded or "
1150 "event discarded message: late-msg-type=%s",
1151 bt_common_message_type_string(bt_message_get_type(late_msg
)));
1152 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1156 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1157 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1159 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1160 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1161 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1162 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error converting last "
1163 "inactivity message timestamp to nanoseconds");
1164 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1168 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1169 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1170 "Invalid live stream state: "
1171 "have a late message that is none included in a stream "
1172 "inactivity timespan: last-inactivity-ts-ns=%" PRIu64
1173 "late-msg-ts-ns=%" PRIu64
,
1174 last_inactivity_ts_ns
, late_msg_ts_ns
);
1175 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1180 * We now know that it's okay for this message to be late, we can now
1181 * adjust its timestamp to ensure monotonicity.
1183 BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, "
1184 "msg-new-ts-ns=%" PRIu64
,
1185 bt_common_message_type_string(bt_message_get_type(late_msg
)),
1186 stream_iter
->last_inactivity_ts
.value
);
1187 switch (bt_message_get_type(late_msg
)) {
1188 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1189 adjust_status
= adjust_discarded_events_message(
1190 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1191 stream_iter
->last_inactivity_ts
.value
);
1193 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1194 adjust_status
= adjust_discarded_packets_message(
1195 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1196 stream_iter
->last_inactivity_ts
.value
);
1202 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1203 stream_iter_status
= adjust_status
;
1207 BT_ASSERT_DBG(adjusted_message
);
1208 stream_iter
->current_msg
= adjusted_message
;
1209 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1210 bt_message_put_ref(late_msg
);
1213 return stream_iter_status
;
1216 static enum lttng_live_iterator_status
1217 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1218 struct lttng_live_trace
*live_trace
,
1219 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1221 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1222 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1223 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1224 enum lttng_live_iterator_status stream_iter_status
;
1226 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1227 uint64_t stream_iter_idx
;
1229 BT_ASSERT_DBG(live_trace
);
1230 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1232 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1233 "trace-id=%" PRIu64
,
1236 * Update the current message of every stream iterators of this trace.
1237 * The current msg of every stream must have a timestamp equal or
1238 * larger than the last message returned by this iterator. We must
1239 * ensure monotonicity.
1241 stream_iter_idx
= 0;
1242 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1243 bool stream_iter_is_ended
= false;
1244 struct lttng_live_stream_iterator
*stream_iter
=
1245 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1249 * If there is no current message for this stream, go fetch
1252 while (!stream_iter
->current_msg
) {
1253 const bt_message
*msg
= NULL
;
1254 int64_t curr_msg_ts_ns
= INT64_MAX
;
1256 stream_iter_status
=
1257 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, &msg
);
1259 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1260 stream_iter_is_ended
= true;
1264 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1270 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, "
1271 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1272 bt_common_message_type_string(bt_message_get_type(msg
)),
1273 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1276 * Get the timestamp in nanoseconds from origin of this
1279 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
, msg
,
1280 lttng_live_msg_iter
->last_msg_ts_ns
, &curr_msg_ts_ns
);
1283 * Check if the message of the current live stream
1284 * iterator occurred at the exact same time or after the
1285 * last message returned by this component's message
1286 * iterator. If not, we need to handle it with care.
1288 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1289 stream_iter
->current_msg
= msg
;
1290 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1293 * We received a message from the past. This
1294 * may be fixable but it can also be an error.
1296 stream_iter_status
=
1297 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, msg
);
1298 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1299 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1300 "Late message could not be handled correctly: "
1301 "lttng-live-msg-iter-addr=%p, "
1302 "stream-name=\"%s\", "
1303 "curr-msg-ts=%" PRId64
", last-msg-ts=%" PRId64
,
1304 lttng_live_msg_iter
, stream_iter
->name
->str
,
1305 curr_msg_ts_ns
, lttng_live_msg_iter
->last_msg_ts_ns
);
1306 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1312 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1314 if (!stream_iter_is_ended
) {
1315 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1316 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1318 * Update the current best candidate message
1319 * for the stream iterator of this live trace
1320 * to be forwarded downstream.
1322 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1323 youngest_candidate_stream_iter
= stream_iter
;
1324 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1326 * Order the messages in an arbitrary but
1327 * deterministic way.
1329 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1330 int ret
= common_muxing_compare_messages(
1331 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1334 * The `youngest_candidate_stream_iter->current_msg`
1335 * should go first. Update the next
1336 * iterator and the current timestamp.
1338 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1339 youngest_candidate_stream_iter
= stream_iter
;
1340 } else if (ret
== 0) {
1342 * Unable to pick which one should go
1346 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1347 "stream-iter-addr=%p"
1348 "stream-iter-addr=%p",
1349 stream_iter
, youngest_candidate_stream_iter
);
1356 * The live stream iterator has ended. That
1357 * iterator is removed from the array, but
1358 * there is no need to increment
1359 * stream_iter_idx as
1360 * g_ptr_array_remove_index_fast replaces the
1361 * removed element with the array's last
1364 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
, stream_iter_idx
);
1368 if (youngest_candidate_stream_iter
) {
1369 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1370 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1373 * The only case where we don't have a candidate for this trace
1374 * is if we reached the end of all the iterators.
1376 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1377 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1381 return stream_iter_status
;
1384 static enum lttng_live_iterator_status
1385 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1386 struct lttng_live_session
*session
,
1387 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1389 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1390 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1391 enum lttng_live_iterator_status stream_iter_status
;
1392 uint64_t trace_idx
= 0;
1393 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1394 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1396 BT_COMP_LOGD("Finding the next stream iterator for session: "
1397 "session-id=%" PRIu64
,
1400 * Make sure we are attached to the session and look for new streams
1403 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1404 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1405 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1406 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1410 BT_ASSERT_DBG(session
->traces
);
1412 while (trace_idx
< session
->traces
->len
) {
1413 bool trace_is_ended
= false;
1414 struct lttng_live_stream_iterator
*stream_iter
;
1415 struct lttng_live_trace
*trace
=
1416 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1418 stream_iter_status
=
1419 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1420 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1422 * All the live stream iterators for this trace are
1423 * ENDed. Remove the trace from this session.
1425 trace_is_ended
= true;
1426 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1430 if (!trace_is_ended
) {
1431 BT_ASSERT_DBG(stream_iter
);
1433 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1434 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1435 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1436 youngest_candidate_stream_iter
= stream_iter
;
1437 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1439 * Order the messages in an arbitrary but
1440 * deterministic way.
1442 int ret
= common_muxing_compare_messages(
1443 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1446 * The `youngest_candidate_stream_iter->current_msg`
1447 * should go first. Update the next iterator
1448 * and the current timestamp.
1450 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1451 youngest_candidate_stream_iter
= stream_iter
;
1452 } else if (ret
== 0) {
1453 /* Unable to pick which one should go first. */
1455 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1456 "stream-iter-addr=%p"
1457 "stream-iter-addr=%p",
1458 stream_iter
, youngest_candidate_stream_iter
);
1464 * trace_idx is not incremented since
1465 * g_ptr_array_remove_index_fast replaces the
1466 * element at trace_idx with the array's last element.
1468 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1471 if (youngest_candidate_stream_iter
) {
1472 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1473 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1476 * The only cases where we don't have a candidate for this
1478 * 1. if we reached the end of all the iterators of all the
1479 * traces of this session,
1480 * 2. if we never had live stream iterator in the first place.
1482 * In either cases, we return END.
1484 BT_ASSERT(session
->traces
->len
== 0);
1485 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1488 return stream_iter_status
;
1491 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1495 for (i
= 0; i
< count
; i
++) {
1496 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1501 bt_message_iterator_class_next_method_status
1502 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1503 uint64_t capacity
, uint64_t *count
)
1505 bt_message_iterator_class_next_method_status status
;
1506 enum lttng_live_viewer_status viewer_status
;
1507 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1508 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1509 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1510 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1511 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1512 enum lttng_live_iterator_status stream_iter_status
;
1513 uint64_t session_idx
;
1517 BT_ASSERT_DBG(lttng_live_msg_iter
);
1519 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1521 * The iterator was interrupted in a previous call to the
1522 * `_next()` method. We currently do not support generating
1523 * messages after such event. The babeltrace2 CLI should never
1524 * be running the graph after being interrupted. So this check
1525 * is to prevent other graph users from using this live
1526 * iterator in an messed up internal state.
1528 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1529 BT_COMP_LOGE_APPEND_CAUSE(
1531 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1536 * Clear all the invalid message reference that might be left over in
1539 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1542 * If no session are exposed on the relay found at the url provided by
1543 * the user, session count will be 0. In this case, we return status
1544 * end to return gracefully.
1546 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1547 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1548 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1552 * The are no more active session for this session
1553 * name. Retry to create a viewer session for the
1554 * requested session name.
1556 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1557 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1558 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1559 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1560 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1561 "Error creating LTTng live viewer session");
1562 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1563 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1572 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1573 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1577 * Here the muxing of message is done.
1579 * We need to iterate over all the streams of all the traces of all the
1580 * viewer sessions in order to get the message with the smallest
1581 * timestamp. In this case, a session is a viewer session and there is
1582 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1583 * kernel). Each viewer session can have multiple traces, for example,
1584 * 64bit UST viewer sessions could have multiple per-pid traces.
1586 * We iterate over the streams of each traces to update and see what is
1587 * their next message's timestamp. From those timestamps, we select the
1588 * message with the smallest timestamp as the best candidate message
1589 * for that trace and do the same thing across all the sessions.
1591 * We then compare the timestamp of best candidate message of all the
1592 * sessions to pick the message with the smallest timestamp and we
1595 while (*count
< capacity
) {
1596 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1597 *candidate_stream_iter
= NULL
;
1598 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1600 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1602 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1603 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1604 lttng_live_msg_iter
->sessions
, session_idx
);
1606 /* Find the best candidate message to send downstream. */
1607 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1608 &candidate_stream_iter
);
1610 /* If we receive an END status, it means that either:
1611 * - Those traces never had active streams (UST with no
1612 * data produced yet),
1613 * - All live stream iterators have ENDed.*/
1614 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1615 if (session
->closed
&& session
->traces
->len
== 0) {
1617 * Remove the session from the list.
1618 * session_idx is not modified since
1619 * g_ptr_array_remove_index_fast
1620 * replaces the the removed element with
1621 * the array's last element.
1623 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1630 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1634 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1635 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1636 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1637 youngest_stream_iter
= candidate_stream_iter
;
1638 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1640 * The currently selected message to be sent
1641 * downstream next has the exact same timestamp
1642 * that of the current candidate message. We
1643 * must break the tie in a predictable manner.
1646 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1648 * Order the messages in an arbitrary but
1649 * deterministic way.
1651 int ret
= common_muxing_compare_messages(candidate_stream_iter
->current_msg
,
1652 youngest_stream_iter
->current_msg
);
1655 * The `candidate_stream_iter->current_msg`
1656 * should go first. Update the next
1657 * iterator and the current timestamp.
1659 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1660 youngest_stream_iter
= candidate_stream_iter
;
1661 } else if (ret
== 0) {
1662 /* Unable to pick which one should go first. */
1664 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1665 "next-stream-iter-addr=%p"
1666 "candidate-stream-iter-addr=%p",
1667 youngest_stream_iter
, candidate_stream_iter
);
1674 if (!youngest_stream_iter
) {
1675 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1679 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1680 /* Ensure monotonicity. */
1681 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1682 youngest_stream_iter
->current_msg_ts_ns
);
1685 * Insert the next message to the message batch. This will set
1686 * stream iterator current messsage to NULL so that next time
1687 * we fetch the next message of that stream iterator
1689 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1692 /* Update the last timestamp in nanoseconds sent downstream. */
1693 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1694 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1696 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1700 switch (stream_iter_status
) {
1701 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1702 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1704 * If we gathered messages, return _OK even if the graph was
1705 * interrupted. This allows for the components downstream to at
1706 * least get the thoses messages. If the graph was indeed
1707 * interrupted there should not be another _next() call as the
1708 * application will tear down the graph. This component class
1709 * doesn't support restarting after an interruption.
1712 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1714 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1717 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1718 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1720 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1721 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1722 "Memory error preparing the next batch of messages: "
1723 "live-iter-status=%s",
1724 lttng_live_iterator_status_string(stream_iter_status
));
1725 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1727 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1728 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1729 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1730 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1731 "Error preparing the next batch of messages: "
1732 "live-iter-status=%s",
1733 lttng_live_iterator_status_string(stream_iter_status
));
1735 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1736 /* Put all existing messages on error. */
1737 put_messages(msgs
, *count
);
1747 static struct lttng_live_msg_iter
*
1748 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1749 bt_self_message_iterator
*self_msg_it
)
1751 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1752 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1754 struct lttng_live_msg_iter
*lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1755 if (!lttng_live_msg_iter
) {
1756 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate lttng_live_msg_iter");
1760 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1761 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1762 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1763 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1765 lttng_live_msg_iter
->active_stream_iter
= 0;
1766 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1767 lttng_live_msg_iter
->was_interrupted
= false;
1769 lttng_live_msg_iter
->sessions
=
1770 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1771 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1774 return lttng_live_msg_iter
;
1778 bt_message_iterator_class_initialize_method_status
1779 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1780 bt_self_message_iterator_configuration
*config
,
1781 bt_self_component_port_output
*self_port
)
1783 bt_message_iterator_class_initialize_method_status status
;
1784 struct lttng_live_component
*lttng_live
;
1785 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1786 enum lttng_live_viewer_status viewer_status
;
1787 bt_logging_level log_level
;
1788 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1790 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1791 log_level
= lttng_live
->log_level
;
1792 self_comp
= lttng_live
->self_comp
;
1794 /* There can be only one downstream iterator at the same time. */
1795 BT_ASSERT(!lttng_live
->has_msg_iter
);
1796 lttng_live
->has_msg_iter
= true;
1798 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1799 if (!lttng_live_msg_iter
) {
1800 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create lttng_live_msg_iter");
1801 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1805 viewer_status
= live_viewer_connection_create(
1806 self_comp
, NULL
, log_level
, lttng_live
->params
.url
->str
, false, lttng_live_msg_iter
,
1807 <tng_live_msg_iter
->viewer_connection
);
1808 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1809 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1810 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer connection");
1811 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1813 * Interruption in the _iter_init() method is not
1814 * supported. Return an error.
1816 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted while creating viewer connection");
1819 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1823 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1824 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1825 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1826 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer session");
1827 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1829 * Interruption in the _iter_init() method is not
1830 * supported. Return an error.
1832 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted when creating viewer session");
1835 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1839 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1840 switch (lttng_live
->params
.sess_not_found_act
) {
1841 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1843 "Unable to connect to the requested live viewer session. Keep trying to connect because of "
1844 "%s=\"%s\" component parameter: url=\"%s\"",
1845 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1846 lttng_live
->params
.url
->str
);
1848 case SESSION_NOT_FOUND_ACTION_FAIL
:
1849 BT_COMP_LOGE_APPEND_CAUSE(
1851 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1852 "component parameter: url =\"%s\"",
1853 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1854 lttng_live
->params
.url
->str
);
1855 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1857 case SESSION_NOT_FOUND_ACTION_END
:
1859 "Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1860 "call because of %s=\"%s\" component parameter: "
1862 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1863 lttng_live
->params
.url
->str
);
1870 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1871 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1875 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1880 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1881 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1882 bt_param_validation_value_descr::makeString()},
1883 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1885 static bt_component_class_query_method_status
1886 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1887 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1889 bt_component_class_query_method_status status
;
1890 const bt_value
*url_value
= NULL
;
1892 struct live_viewer_connection
*viewer_connection
= NULL
;
1893 enum lttng_live_viewer_status viewer_status
;
1894 enum bt_param_validation_status validation_status
;
1895 gchar
*validate_error
= NULL
;
1897 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1898 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1899 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1901 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1902 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1903 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1907 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1908 url
= bt_value_string_get(url_value
);
1910 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
, log_level
, url
, true, NULL
,
1911 &viewer_connection
);
1912 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1913 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1914 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to create viewer connection");
1915 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1916 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1917 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1924 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1925 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1926 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to list viewer sessions");
1933 BT_VALUE_PUT_REF_AND_RESET(*result
);
1936 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1940 if (viewer_connection
) {
1941 live_viewer_connection_destroy(viewer_connection
);
1944 g_free(validate_error
);
1949 static bt_component_class_query_method_status
1950 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1951 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1953 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1954 const bt_value
*input_type_value
;
1955 const bt_value
*input_value
;
1957 struct bt_common_lttng_live_url_parts parts
= {0};
1959 /* Used by the logging macros */
1960 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1963 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1964 if (!input_type_value
) {
1965 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `type` parameter.");
1969 if (!bt_value_is_string(input_type_value
)) {
1970 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "`type` parameter is not a string value.");
1974 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1975 /* We don't handle file system paths */
1979 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1981 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `input` parameter.");
1985 if (!bt_value_is_string(input_value
)) {
1986 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1987 "`input` parameter is not a string value.");
1991 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1992 if (parts
.session_name
) {
1994 * Looks pretty much like an LTTng live URL: we got the
1995 * session name part, which forms a complete URL.
2001 *result
= bt_value_real_create_init(weight
);
2003 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2011 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2014 BT_ASSERT(!*result
);
2017 bt_common_destroy_lttng_live_url_parts(&parts
);
2022 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
2023 bt_private_query_executor
*priv_query_exec
,
2024 const char *object
, const bt_value
*params
,
2025 __attribute__((unused
)) void *method_data
,
2026 const bt_value
**result
)
2028 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2029 bt_self_component
*self_comp
= NULL
;
2030 bt_self_component_class
*self_comp_class
=
2031 bt_self_component_class_source_as_self_component_class(comp_class
);
2032 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2033 bt_private_query_executor_as_query_executor_const(priv_query_exec
));
2035 if (strcmp(object
, "sessions") == 0) {
2036 status
= lttng_live_query_list_sessions(params
, result
, self_comp_class
, log_level
);
2037 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2038 status
= lttng_live_query_support_info(params
, result
, self_comp_class
, log_level
);
2040 BT_COMP_LOGI("Unknown query object `%s`", object
);
2041 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2049 static void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2054 if (lttng_live
->params
.url
) {
2055 g_string_free(lttng_live
->params
.url
, TRUE
);
2061 void lttng_live_component_finalize(bt_self_component_source
*component
)
2063 lttng_live_component
*data
= (lttng_live_component
*) bt_self_component_get_data(
2064 bt_self_component_source_as_self_component(component
));
2069 lttng_live_component_destroy_data(data
);
2072 static enum session_not_found_action
2073 parse_session_not_found_action_param(const bt_value
*no_session_param
)
2075 enum session_not_found_action action
;
2076 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2078 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2079 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2080 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2081 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2083 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2084 action
= SESSION_NOT_FOUND_ACTION_END
;
2090 static bt_param_validation_value_descr inputs_elem_descr
=
2091 bt_param_validation_value_descr::makeString();
2093 static const char *sess_not_found_action_choices
[] = {
2094 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2095 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2096 SESS_NOT_FOUND_ACTION_END_STR
,
2099 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2100 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
2101 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
2102 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
2103 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
2104 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
2106 static bt_component_class_initialize_method_status
2107 lttng_live_component_create(const bt_value
*params
, bt_logging_level log_level
,
2108 bt_self_component
*self_comp
, struct lttng_live_component
**component
)
2110 struct lttng_live_component
*lttng_live
= NULL
;
2111 const bt_value
*inputs_value
;
2112 const bt_value
*url_value
;
2113 const bt_value
*value
;
2115 enum bt_param_validation_status validation_status
;
2116 gchar
*validation_error
= NULL
;
2117 bt_component_class_initialize_method_status status
;
2119 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
2120 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2121 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2123 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2124 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2125 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2129 lttng_live
= g_new0(struct lttng_live_component
, 1);
2131 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2134 lttng_live
->log_level
= log_level
;
2135 lttng_live
->self_comp
= self_comp
;
2136 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2137 lttng_live
->has_msg_iter
= false;
2139 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2140 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2141 url
= bt_value_string_get(url_value
);
2143 lttng_live
->params
.url
= g_string_new(url
);
2144 if (!lttng_live
->params
.url
) {
2145 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2149 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
2151 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
2153 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2154 "defaulting to `%s`.",
2155 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2156 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2159 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2163 lttng_live_component_destroy_data(lttng_live
);
2166 g_free(validation_error
);
2168 *component
= lttng_live
;
2173 bt_component_class_initialize_method_status
2174 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2175 bt_self_component_source_configuration
*config
, const bt_value
*params
,
2176 __attribute__((unused
)) void *init_method_data
)
2178 struct lttng_live_component
*lttng_live
;
2179 bt_component_class_initialize_method_status ret
;
2180 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2181 bt_logging_level log_level
=
2182 bt_component_get_logging_level(bt_self_component_as_component(self_comp
));
2183 bt_self_component_add_port_status add_port_status
;
2185 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2186 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2190 add_port_status
= bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2191 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2192 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2196 bt_self_component_set_data(self_comp
, lttng_live
);
2200 lttng_live_component_destroy_data(lttng_live
);