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); \
111 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
120 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
126 static struct lttng_live_trace
*
127 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
130 struct lttng_live_trace
*ret_trace
= NULL
;
132 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
133 struct lttng_live_trace
*trace
=
134 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
135 if (trace
->id
== trace_id
) {
145 static void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
147 bt_logging_level log_level
= trace
->log_level
;
148 bt_self_component
*self_comp
= trace
->self_comp
;
150 BT_COMP_LOGD("Destroying live trace: trace-id=%" PRIu64
, trace
->id
);
152 BT_ASSERT(trace
->stream_iterators
);
153 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
155 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
156 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
158 lttng_live_metadata_fini(trace
);
162 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
165 struct lttng_live_trace
*trace
= NULL
;
166 bt_logging_level log_level
= session
->log_level
;
167 bt_self_component
*self_comp
= session
->self_comp
;
169 BT_COMP_LOGD("Creating live trace: "
170 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
171 session
->id
, trace_id
);
172 trace
= g_new0(struct lttng_live_trace
, 1);
174 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate live trace");
177 trace
->log_level
= session
->log_level
;
178 trace
->self_comp
= session
->self_comp
;
179 trace
->session
= session
;
180 trace
->id
= trace_id
;
181 trace
->trace_class
= NULL
;
183 trace
->stream_iterators
=
184 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_stream_iterator_destroy
);
185 BT_ASSERT(trace
->stream_iterators
);
186 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
187 g_ptr_array_add(session
->traces
, trace
);
197 struct lttng_live_trace
*
198 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
201 struct lttng_live_trace
*trace
;
203 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
208 /* The session is the owner of the newly created trace. */
209 trace
= lttng_live_create_trace(session
, trace_id
);
215 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
216 const char *hostname
, const char *session_name
)
219 struct lttng_live_session
*session
;
220 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
221 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
223 BT_COMP_LOGD("Adding live session: "
224 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
225 session_id
, hostname
, session_name
);
227 session
= g_new0(struct lttng_live_session
, 1);
229 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate live session");
233 session
->log_level
= lttng_live_msg_iter
->log_level
;
234 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
235 session
->id
= session_id
;
236 session
->traces
= g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_trace
);
237 BT_ASSERT(session
->traces
);
238 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
239 session
->new_streams_needed
= true;
240 session
->hostname
= g_string_new(hostname
);
241 BT_ASSERT(session
->hostname
);
243 session
->session_name
= g_string_new(session_name
);
244 BT_ASSERT(session
->session_name
);
246 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
255 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
257 bt_logging_level log_level
;
258 bt_self_component
*self_comp
;
264 log_level
= session
->log_level
;
265 self_comp
= session
->self_comp
;
266 BT_COMP_LOGD("Destroying live session: "
267 "session-id=%" PRIu64
", session-name=\"%s\"",
268 session
->id
, session
->session_name
->str
);
269 if (session
->id
!= -1ULL) {
270 if (lttng_live_session_detach(session
)) {
271 if (!lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
272 /* Old relayd cannot detach sessions. */
273 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
, session
->id
);
279 if (session
->traces
) {
280 g_ptr_array_free(session
->traces
, TRUE
);
283 if (session
->hostname
) {
284 g_string_free(session
->hostname
, TRUE
);
286 if (session
->session_name
) {
287 g_string_free(session
->session_name
, TRUE
);
295 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
297 if (!lttng_live_msg_iter
) {
301 if (lttng_live_msg_iter
->sessions
) {
302 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
305 if (lttng_live_msg_iter
->viewer_connection
) {
306 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
308 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
309 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
311 /* All stream iterators must be destroyed at this point. */
312 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
313 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
315 g_free(lttng_live_msg_iter
);
321 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
323 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
325 BT_ASSERT(self_msg_iter
);
327 lttng_live_msg_iter
=
328 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
329 BT_ASSERT(lttng_live_msg_iter
);
330 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
333 static enum lttng_live_iterator_status
334 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
336 bt_logging_level log_level
= lttng_live_stream
->log_level
;
337 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
339 switch (lttng_live_stream
->state
) {
340 case LTTNG_LIVE_STREAM_QUIESCENT
:
341 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
343 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
345 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
347 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
349 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
351 case LTTNG_LIVE_STREAM_EOF
:
354 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
358 * For active no data stream, fetch next index. As a result of that it can
360 * - quiescent: won't have events for a bit,
361 * - have data: need to get that data and produce the event,
362 * - have no data on this stream at this point: need to retry (AGAIN) or return
365 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
366 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
367 struct lttng_live_stream_iterator
*lttng_live_stream
)
369 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
370 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
371 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
372 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
373 struct packet_index index
;
375 if (lttng_live_stream
->trace
->metadata_stream_state
==
376 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
378 "Need to get an update for the metadata stream before proceeding further with this stream: "
379 "stream-name=\"%s\"",
380 lttng_live_stream
->name
->str
);
381 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
385 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
387 "Need to get an update of all streams before proceeding further with this stream: "
388 "stream-name=\"%s\"",
389 lttng_live_stream
->name
->str
);
390 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
394 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
395 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
398 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
399 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
403 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
405 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
406 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
407 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
409 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
411 * Because the stream is in the QUIESCENT_NO_DATA
412 * state, we can assert that the last_inactivity_ts was
413 * set and can be safely used in the `if` above.
415 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
417 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
418 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
420 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
425 lttng_live_stream
->base_offset
= index
.offset
;
426 lttng_live_stream
->offset
= index
.offset
;
427 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
429 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
430 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
431 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
432 lttng_live_stream
->name
->str
, lttng_live_stream
->viewer_stream_id
,
433 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
, lttng_live_stream
->len
);
436 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
437 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
443 * Creation of the message requires the ctf trace class to be created
444 * beforehand, but the live protocol gives us all streams (including metadata)
445 * at once. So we split it in three steps: getting streams, getting metadata
446 * (which creates the ctf trace class), and then creating the per-stream
449 static enum lttng_live_iterator_status
450 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
451 struct lttng_live_session
*session
)
453 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
454 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
455 enum lttng_live_iterator_status status
;
458 if (!session
->attached
) {
459 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
, session
->id
);
460 enum lttng_live_viewer_status attach_status
=
461 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
462 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
463 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
465 * Clear any causes appended in
466 * `lttng_live_attach_session()` as we want to
467 * return gracefully since the graph was
470 bt_current_thread_clear_error();
471 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
473 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
474 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error attaching to LTTng live session");
480 BT_COMP_LOGD("Updating all data streams: "
481 "session-id=%" PRIu64
", session-name=\"%s\"",
482 session
->id
, session
->session_name
->str
);
484 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
486 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
488 case LTTNG_LIVE_ITERATOR_STATUS_END
:
490 * We received a `_END` from the `_get_new_streams()` function,
491 * which means no more data will ever be received from the data
492 * streams of this session. But it's possible that the metadata
494 * The live protocol guarantees that we receive all the
495 * metadata needed before we receive data streams needing it.
496 * But it's possible to receive metadata NOT needed by
497 * data streams after the session was closed. For example, this
498 * could happen if a new event is registered and the session is
499 * stopped before any tracepoint for that event is actually
503 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
504 "session-id=%" PRIu64
", session-name=\"%s\"",
505 session
->id
, session
->session_name
->str
);
506 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
512 BT_COMP_LOGD("Updating metadata stream for session: "
513 "session-id=%" PRIu64
", session-name=\"%s\"",
514 session
->id
, session
->session_name
->str
);
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
), trace
->id
);
540 * Now that we have the metadata we can initialize the downstream
543 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
550 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
552 uint64_t session_idx
, trace_idx
;
553 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
554 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
556 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
557 struct lttng_live_session
*session
=
558 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
559 BT_COMP_LOGD("Force marking session as needing new streams: "
560 "session-id=%" PRIu64
,
562 session
->new_streams_needed
= true;
563 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
564 struct lttng_live_trace
*trace
=
565 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
566 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
567 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
568 session
->id
, trace
->id
);
570 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
572 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
577 static enum lttng_live_iterator_status
578 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
580 enum lttng_live_iterator_status status
;
581 enum lttng_live_viewer_status viewer_status
;
582 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
583 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
584 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
585 struct lttng_live_session
*session
;
586 enum session_not_found_action sess_not_found_act
=
587 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
589 BT_COMP_LOGD("Update data and metadata of all sessions: "
590 "live-msg-iter-addr=%p",
591 lttng_live_msg_iter
);
593 * In a remotely distant future, we could add a "new
594 * session" flag to the protocol, which would tell us that we
595 * need to query for new sessions even though we have sessions
598 if (lttng_live_msg_iter
->sessions
->len
== 0) {
599 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
601 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
602 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
606 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
608 * Retry to create a viewer session for the requested
611 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
612 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
613 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
614 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
615 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
616 "Error creating LTTng live viewer session");
617 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
618 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
627 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
629 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
630 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
632 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
633 case LTTNG_LIVE_ITERATOR_STATUS_END
:
635 * A session returned `_END`. Other sessions may still
636 * be active so we override the status and continue
643 if (!session
->closed
) {
644 nr_sessions_opened
++;
648 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
649 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
651 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
658 static enum lttng_live_iterator_status
659 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
660 struct lttng_live_stream_iterator
*stream_iter
, const bt_message
**message
,
663 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
664 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
665 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
666 bt_message
*msg
= NULL
;
668 BT_ASSERT(stream_iter
->trace
->clock_class
);
670 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
671 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
672 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
, timestamp
);
674 msg
= bt_message_message_iterator_inactivity_create(lttng_live_msg_iter
->self_msg_iter
,
675 stream_iter
->trace
->clock_class
, timestamp
);
677 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error emitting message iterator inactivity message");
686 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
687 bt_message_put_ref(msg
);
691 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
692 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
693 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
695 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
697 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
698 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
702 * Check if we already sent an inactivty message downstream for this
703 * `current_inactivity_ts` value.
705 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
706 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
707 lttng_live_stream_iterator_set_state(lttng_live_stream
,
708 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
710 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
714 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
715 lttng_live_stream
->current_inactivity_ts
);
717 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
718 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
723 static int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
724 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
725 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
727 const bt_clock_snapshot
*clock_snapshot
= NULL
;
729 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
730 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
733 BT_ASSERT_DBG(ts_ns
);
735 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
736 "last-msg-ts=%" PRId64
,
737 lttng_live_msg_iter
, msg
, last_msg_ts_ns
);
739 switch (bt_message_get_type(msg
)) {
740 case BT_MESSAGE_TYPE_EVENT
:
741 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
743 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
744 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
746 case BT_MESSAGE_TYPE_PACKET_END
:
747 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
749 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
751 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
753 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
755 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
757 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
758 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
761 /* All the other messages have a higher priority */
762 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
763 *ts_ns
= last_msg_ts_ns
;
767 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
769 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
770 "Cannot get nanoseconds from Epoch of clock snapshot: "
771 "clock-snapshot-addr=%p",
783 BT_COMP_LOGD("Found message's timestamp: "
784 "iter-data-addr=%p, msg-addr=%p, "
785 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
786 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
792 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
793 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
794 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
796 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
797 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
798 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
799 enum ctf_msg_iter_status status
;
800 uint64_t session_idx
, trace_idx
;
802 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
803 struct lttng_live_session
*session
=
804 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
806 if (session
->new_streams_needed
) {
807 BT_COMP_LOGD("Need an update for streams: "
808 "session-id=%" PRIu64
,
810 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
813 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
814 struct lttng_live_trace
*trace
=
815 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
816 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
817 BT_COMP_LOGD("Need an update for metadata stream: "
818 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
819 session
->id
, trace
->id
);
820 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
826 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
827 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
828 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
829 "Invalid state of live stream iterator"
830 "stream-iter-status=%s",
831 lttng_live_stream_state_string(lttng_live_stream
->state
));
835 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
, message
);
837 case CTF_MSG_ITER_STATUS_EOF
:
838 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
840 case CTF_MSG_ITER_STATUS_OK
:
841 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
843 case CTF_MSG_ITER_STATUS_AGAIN
:
845 * Continue immediately (end of packet). The next
846 * get_index may return AGAIN to delay the following
849 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
851 case CTF_MSG_ITER_STATUS_ERROR
:
853 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
854 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
855 "CTF message iterator failed to get next message: "
856 "msg-iter=%p, msg-iter-status=%s",
857 lttng_live_stream
->msg_iter
, ctf_msg_iter_status_string(status
));
865 static enum lttng_live_iterator_status
866 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
867 struct lttng_live_stream_iterator
*stream_iter
,
868 const bt_message
**curr_msg
)
870 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
871 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
872 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
874 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
875 "viewer-stream-id=%" PRIu64
,
876 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
879 * The viewer has hung up on us so we are closing the stream. The
880 * `ctf_msg_iter` should simply realize that it needs to close the
881 * stream properly by emitting the necessary stream end message.
883 enum ctf_msg_iter_status status
=
884 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
, curr_msg
);
886 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
887 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
888 "Error getting the next message from CTF message iterator");
889 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
891 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
892 BT_COMP_LOGI("Reached the end of the live stream iterator.");
893 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
897 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
905 * handle_no_data_streams()
907 * - for each ACTIVE_NO_DATA stream:
908 * - query relayd for stream data, or quiescence info.
909 * - if need metadata, get metadata, goto retry.
910 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
911 * - if quiescent, move to QUIESCENT streams
912 * - if fetched data, move to ACTIVE_DATA streams
913 * (at this point each stream either has data, or is quiescent)
917 * handle_new_streams_and_metadata()
918 * - query relayd for known streams, add them as ACTIVE_NO_DATA
919 * - query relayd for metadata
921 * call handle_active_no_data_streams()
923 * handle_quiescent_streams()
924 * - if at least one stream is ACTIVE_DATA:
925 * - peek stream event with lowest timestamp -> next_ts
926 * - for each quiescent stream
927 * - if next_ts >= quiescent end
928 * - set state to ACTIVE_NO_DATA
930 * - for each quiescent stream
931 * - set state to ACTIVE_NO_DATA
933 * call handle_active_no_data_streams()
935 * handle_active_data_streams()
936 * - if at least one stream is ACTIVE_DATA:
937 * - get stream event with lowest timestamp from heap
938 * - make that stream event the current message.
939 * - move this stream heap position to its next event
940 * - if we need to fetch data from relayd, move
941 * stream to ACTIVE_NO_DATA.
945 * end criterion: ctrl-c on client. If relayd exits or the session
946 * closes on the relay daemon side, we keep on waiting for streams.
947 * Eventually handle --end timestamp (also an end criterion).
949 * When disconnected from relayd: try to re-connect endlessly.
951 static enum lttng_live_iterator_status
952 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
953 struct lttng_live_stream_iterator
*stream_iter
,
954 const bt_message
**curr_msg
)
956 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
957 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
958 enum lttng_live_iterator_status live_status
;
960 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
961 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
962 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
964 if (stream_iter
->has_stream_hung_up
) {
966 * The stream has hung up and the stream was properly closed
967 * during the last call to the current function. Return _END
968 * status now so that this stream iterator is removed for the
969 * stream iterator list.
971 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
976 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
979 * Make sure we have the most recent metadata and possibly some new
982 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
983 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
988 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
989 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
990 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
992 * We overwrite `live_status` since `curr_msg` is
993 * likely set to a valid message in this function.
996 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
1001 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
1002 stream_iter
, curr_msg
);
1003 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1004 BT_ASSERT(!*curr_msg
);
1010 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
1011 stream_iter
, curr_msg
);
1012 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1013 BT_ASSERT(!*curr_msg
);
1017 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1018 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1022 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, "
1023 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1024 lttng_live_iterator_status_string(live_status
), stream_iter
->name
->str
,
1025 stream_iter
->viewer_stream_id
);
1030 static bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1032 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1034 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1035 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1038 static enum lttng_live_iterator_status
1039 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1040 const bt_message
*msg_in
, bt_message
**msg_out
,
1041 uint64_t new_begin_ts
)
1043 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1044 enum bt_property_availability availability
;
1045 const bt_clock_snapshot
*clock_snapshot
;
1049 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1050 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1052 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1053 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1055 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1056 iter
, stream
, new_begin_ts
, end_ts
);
1058 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1062 bt_message_discarded_packets_set_count(*msg_out
, count
);
1067 static enum lttng_live_iterator_status
1068 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1069 const bt_message
*msg_in
, bt_message
**msg_out
,
1070 uint64_t new_begin_ts
)
1072 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1073 enum bt_property_availability availability
;
1074 const bt_clock_snapshot
*clock_snapshot
;
1078 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1079 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1081 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1082 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1084 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1085 iter
, stream
, new_begin_ts
, end_ts
);
1087 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1091 bt_message_discarded_events_set_count(*msg_out
, count
);
1096 static enum lttng_live_iterator_status
1097 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1098 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
1099 const bt_message
*late_msg
)
1101 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1102 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1103 const bt_clock_class
*clock_class
;
1104 const bt_stream_class
*stream_class
;
1105 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1106 int64_t last_inactivity_ts_ns
;
1107 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1108 enum lttng_live_iterator_status adjust_status
;
1109 bt_message
*adjusted_message
;
1112 * The timestamp of the current message is before the last message sent
1113 * by this component. We CANNOT send it as is.
1115 * The only expected scenario in which that could happen is the
1116 * following, everything else is a bug in this component, relay deamon,
1119 * Expected scenario: The CTF message iterator emitted discarded
1120 * packets and discarded events with synthesized beginning and end
1121 * timestamps from the bounds of the last known packet and the newly
1122 * decoded packet header. The CTF message iterator is not aware of
1123 * stream inactivity beacons. Hence, we have to adjust the beginning
1124 * timestamp of those types of messages if a stream signalled its
1125 * inactivity up until _after_ the last known packet's beginning
1128 * Otherwise, the monotonicity guarantee of message timestamps would
1131 * In short, the only scenario in which it's okay and fixable to
1132 * received a late message is when:
1133 * 1. the late message is a discarded packets or discarded events
1135 * 2. this stream produced an inactivity message downstream, and
1136 * 3. the timestamp of the late message is within the inactivity
1137 * timespan we sent downstream through the inactivity message.
1140 BT_COMP_LOGD("Handling late message on live stream iterator: "
1141 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1142 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1144 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1145 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1146 "have a late message when no inactivity message "
1147 "was ever sent for that stream.");
1148 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1152 if (!is_discarded_packet_or_event_message(late_msg
)) {
1153 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1154 "Invalid live stream state: "
1155 "have a late message that is not a packet discarded or "
1156 "event discarded message: late-msg-type=%s",
1157 bt_common_message_type_string(bt_message_get_type(late_msg
)));
1158 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1162 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1163 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1165 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1166 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1167 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1168 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error converting last "
1169 "inactivity message timestamp to nanoseconds");
1170 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1174 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1175 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1176 "Invalid live stream state: "
1177 "have a late message that is none included in a stream "
1178 "inactivity timespan: last-inactivity-ts-ns=%" PRIu64
1179 "late-msg-ts-ns=%" PRIu64
,
1180 last_inactivity_ts_ns
, late_msg_ts_ns
);
1181 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1186 * We now know that it's okay for this message to be late, we can now
1187 * adjust its timestamp to ensure monotonicity.
1189 BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, "
1190 "msg-new-ts-ns=%" PRIu64
,
1191 bt_common_message_type_string(bt_message_get_type(late_msg
)),
1192 stream_iter
->last_inactivity_ts
.value
);
1193 switch (bt_message_get_type(late_msg
)) {
1194 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1195 adjust_status
= adjust_discarded_events_message(
1196 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1197 stream_iter
->last_inactivity_ts
.value
);
1199 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1200 adjust_status
= adjust_discarded_packets_message(
1201 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1202 stream_iter
->last_inactivity_ts
.value
);
1208 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1209 stream_iter_status
= adjust_status
;
1213 BT_ASSERT_DBG(adjusted_message
);
1214 stream_iter
->current_msg
= adjusted_message
;
1215 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1216 bt_message_put_ref(late_msg
);
1219 return stream_iter_status
;
1222 static enum lttng_live_iterator_status
1223 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1224 struct lttng_live_trace
*live_trace
,
1225 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1227 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1228 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1229 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1230 enum lttng_live_iterator_status stream_iter_status
;
1232 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1233 uint64_t stream_iter_idx
;
1235 BT_ASSERT_DBG(live_trace
);
1236 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1238 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1239 "trace-id=%" PRIu64
,
1242 * Update the current message of every stream iterators of this trace.
1243 * The current msg of every stream must have a timestamp equal or
1244 * larger than the last message returned by this iterator. We must
1245 * ensure monotonicity.
1247 stream_iter_idx
= 0;
1248 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1249 bool stream_iter_is_ended
= false;
1250 struct lttng_live_stream_iterator
*stream_iter
=
1251 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1255 * If there is no current message for this stream, go fetch
1258 while (!stream_iter
->current_msg
) {
1259 const bt_message
*msg
= NULL
;
1260 int64_t curr_msg_ts_ns
= INT64_MAX
;
1262 stream_iter_status
=
1263 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, &msg
);
1265 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1266 stream_iter_is_ended
= true;
1270 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1276 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, "
1277 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1278 bt_common_message_type_string(bt_message_get_type(msg
)),
1279 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1282 * Get the timestamp in nanoseconds from origin of this
1285 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
, msg
,
1286 lttng_live_msg_iter
->last_msg_ts_ns
, &curr_msg_ts_ns
);
1289 * Check if the message of the current live stream
1290 * iterator occurred at the exact same time or after the
1291 * last message returned by this component's message
1292 * iterator. If not, we need to handle it with care.
1294 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1295 stream_iter
->current_msg
= msg
;
1296 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1299 * We received a message from the past. This
1300 * may be fixable but it can also be an error.
1302 stream_iter_status
=
1303 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, msg
);
1304 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1305 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1306 "Late message could not be handled correctly: "
1307 "lttng-live-msg-iter-addr=%p, "
1308 "stream-name=\"%s\", "
1309 "curr-msg-ts=%" PRId64
", last-msg-ts=%" PRId64
,
1310 lttng_live_msg_iter
, stream_iter
->name
->str
,
1311 curr_msg_ts_ns
, lttng_live_msg_iter
->last_msg_ts_ns
);
1312 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1318 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1320 if (!stream_iter_is_ended
) {
1321 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1322 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1324 * Update the current best candidate message
1325 * for the stream iterator of this live trace
1326 * to be forwarded downstream.
1328 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1329 youngest_candidate_stream_iter
= stream_iter
;
1330 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1332 * Order the messages in an arbitrary but
1333 * deterministic way.
1335 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1336 int ret
= common_muxing_compare_messages(
1337 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1340 * The `youngest_candidate_stream_iter->current_msg`
1341 * should go first. Update the next
1342 * iterator and the current timestamp.
1344 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1345 youngest_candidate_stream_iter
= stream_iter
;
1346 } else if (ret
== 0) {
1348 * Unable to pick which one should go
1352 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1353 "stream-iter-addr=%p"
1354 "stream-iter-addr=%p",
1355 stream_iter
, youngest_candidate_stream_iter
);
1362 * The live stream iterator has ended. That
1363 * iterator is removed from the array, but
1364 * there is no need to increment
1365 * stream_iter_idx as
1366 * g_ptr_array_remove_index_fast replaces the
1367 * removed element with the array's last
1370 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
, stream_iter_idx
);
1374 if (youngest_candidate_stream_iter
) {
1375 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1376 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1379 * The only case where we don't have a candidate for this trace
1380 * is if we reached the end of all the iterators.
1382 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1383 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1387 return stream_iter_status
;
1390 static enum lttng_live_iterator_status
1391 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1392 struct lttng_live_session
*session
,
1393 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1395 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1396 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1397 enum lttng_live_iterator_status stream_iter_status
;
1398 uint64_t trace_idx
= 0;
1399 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1400 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1402 BT_COMP_LOGD("Finding the next stream iterator for session: "
1403 "session-id=%" PRIu64
,
1406 * Make sure we are attached to the session and look for new streams
1409 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1410 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1411 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1412 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1416 BT_ASSERT_DBG(session
->traces
);
1418 while (trace_idx
< session
->traces
->len
) {
1419 bool trace_is_ended
= false;
1420 struct lttng_live_stream_iterator
*stream_iter
;
1421 struct lttng_live_trace
*trace
=
1422 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1424 stream_iter_status
=
1425 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1426 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1428 * All the live stream iterators for this trace are
1429 * ENDed. Remove the trace from this session.
1431 trace_is_ended
= true;
1432 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1436 if (!trace_is_ended
) {
1437 BT_ASSERT_DBG(stream_iter
);
1439 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1440 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1441 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1442 youngest_candidate_stream_iter
= stream_iter
;
1443 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1445 * Order the messages in an arbitrary but
1446 * deterministic way.
1448 int ret
= common_muxing_compare_messages(
1449 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1452 * The `youngest_candidate_stream_iter->current_msg`
1453 * should go first. Update the next iterator
1454 * and the current timestamp.
1456 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1457 youngest_candidate_stream_iter
= stream_iter
;
1458 } else if (ret
== 0) {
1459 /* Unable to pick which one should go first. */
1461 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1462 "stream-iter-addr=%p"
1463 "stream-iter-addr=%p",
1464 stream_iter
, youngest_candidate_stream_iter
);
1470 * trace_idx is not incremented since
1471 * g_ptr_array_remove_index_fast replaces the
1472 * element at trace_idx with the array's last element.
1474 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1477 if (youngest_candidate_stream_iter
) {
1478 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1479 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1482 * The only cases where we don't have a candidate for this
1484 * 1. if we reached the end of all the iterators of all the
1485 * traces of this session,
1486 * 2. if we never had live stream iterator in the first place.
1488 * In either cases, we return END.
1490 BT_ASSERT(session
->traces
->len
== 0);
1491 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1494 return stream_iter_status
;
1497 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1501 for (i
= 0; i
< count
; i
++) {
1502 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1506 bt_message_iterator_class_next_method_status
1507 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1508 uint64_t capacity
, uint64_t *count
)
1510 bt_message_iterator_class_next_method_status status
;
1511 enum lttng_live_viewer_status viewer_status
;
1512 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1513 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1514 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1515 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1516 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1517 enum lttng_live_iterator_status stream_iter_status
;
1518 uint64_t session_idx
;
1522 BT_ASSERT_DBG(lttng_live_msg_iter
);
1524 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1526 * The iterator was interrupted in a previous call to the
1527 * `_next()` method. We currently do not support generating
1528 * messages after such event. The babeltrace2 CLI should never
1529 * be running the graph after being interrupted. So this check
1530 * is to prevent other graph users from using this live
1531 * iterator in an messed up internal state.
1533 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1534 BT_COMP_LOGE_APPEND_CAUSE(
1536 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1541 * Clear all the invalid message reference that might be left over in
1544 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1547 * If no session are exposed on the relay found at the url provided by
1548 * the user, session count will be 0. In this case, we return status
1549 * end to return gracefully.
1551 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1552 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1553 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1557 * The are no more active session for this session
1558 * name. Retry to create a viewer session for the
1559 * requested session name.
1561 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1562 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1563 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1564 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1565 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1566 "Error creating LTTng live viewer session");
1567 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1568 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1577 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1578 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1582 * Here the muxing of message is done.
1584 * We need to iterate over all the streams of all the traces of all the
1585 * viewer sessions in order to get the message with the smallest
1586 * timestamp. In this case, a session is a viewer session and there is
1587 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1588 * kernel). Each viewer session can have multiple traces, for example,
1589 * 64bit UST viewer sessions could have multiple per-pid traces.
1591 * We iterate over the streams of each traces to update and see what is
1592 * their next message's timestamp. From those timestamps, we select the
1593 * message with the smallest timestamp as the best candidate message
1594 * for that trace and do the same thing across all the sessions.
1596 * We then compare the timestamp of best candidate message of all the
1597 * sessions to pick the message with the smallest timestamp and we
1600 while (*count
< capacity
) {
1601 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1602 *candidate_stream_iter
= NULL
;
1603 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1605 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1607 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1608 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1609 lttng_live_msg_iter
->sessions
, session_idx
);
1611 /* Find the best candidate message to send downstream. */
1612 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1613 &candidate_stream_iter
);
1615 /* If we receive an END status, it means that either:
1616 * - Those traces never had active streams (UST with no
1617 * data produced yet),
1618 * - All live stream iterators have ENDed.*/
1619 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1620 if (session
->closed
&& session
->traces
->len
== 0) {
1622 * Remove the session from the list.
1623 * session_idx is not modified since
1624 * g_ptr_array_remove_index_fast
1625 * replaces the the removed element with
1626 * the array's last element.
1628 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1635 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1639 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1640 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1641 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1642 youngest_stream_iter
= candidate_stream_iter
;
1643 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1645 * The currently selected message to be sent
1646 * downstream next has the exact same timestamp
1647 * that of the current candidate message. We
1648 * must break the tie in a predictable manner.
1651 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1653 * Order the messages in an arbitrary but
1654 * deterministic way.
1656 int ret
= common_muxing_compare_messages(candidate_stream_iter
->current_msg
,
1657 youngest_stream_iter
->current_msg
);
1660 * The `candidate_stream_iter->current_msg`
1661 * should go first. Update the next
1662 * iterator and the current timestamp.
1664 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1665 youngest_stream_iter
= candidate_stream_iter
;
1666 } else if (ret
== 0) {
1667 /* Unable to pick which one should go first. */
1669 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1670 "next-stream-iter-addr=%p"
1671 "candidate-stream-iter-addr=%p",
1672 youngest_stream_iter
, candidate_stream_iter
);
1679 if (!youngest_stream_iter
) {
1680 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1684 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1685 /* Ensure monotonicity. */
1686 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1687 youngest_stream_iter
->current_msg_ts_ns
);
1690 * Insert the next message to the message batch. This will set
1691 * stream iterator current messsage to NULL so that next time
1692 * we fetch the next message of that stream iterator
1694 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1697 /* Update the last timestamp in nanoseconds sent downstream. */
1698 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1699 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1701 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1705 switch (stream_iter_status
) {
1706 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1707 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1709 * If we gathered messages, return _OK even if the graph was
1710 * interrupted. This allows for the components downstream to at
1711 * least get the thoses messages. If the graph was indeed
1712 * interrupted there should not be another _next() call as the
1713 * application will tear down the graph. This component class
1714 * doesn't support restarting after an interruption.
1717 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1719 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1722 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1723 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1725 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1726 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1727 "Memory error preparing the next batch of messages: "
1728 "live-iter-status=%s",
1729 lttng_live_iterator_status_string(stream_iter_status
));
1730 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1732 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1733 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1734 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1735 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1736 "Error preparing the next batch of messages: "
1737 "live-iter-status=%s",
1738 lttng_live_iterator_status_string(stream_iter_status
));
1740 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1741 /* Put all existing messages on error. */
1742 put_messages(msgs
, *count
);
1752 static struct lttng_live_msg_iter
*
1753 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1754 bt_self_message_iterator
*self_msg_it
)
1756 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1757 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1759 struct lttng_live_msg_iter
*lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1760 if (!lttng_live_msg_iter
) {
1761 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate lttng_live_msg_iter");
1765 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1766 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1767 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1768 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1770 lttng_live_msg_iter
->active_stream_iter
= 0;
1771 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1772 lttng_live_msg_iter
->was_interrupted
= false;
1774 lttng_live_msg_iter
->sessions
=
1775 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1776 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1779 return lttng_live_msg_iter
;
1782 bt_message_iterator_class_initialize_method_status
1783 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1784 bt_self_message_iterator_configuration
*config
,
1785 bt_self_component_port_output
*self_port
)
1787 bt_message_iterator_class_initialize_method_status status
;
1788 struct lttng_live_component
*lttng_live
;
1789 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1790 enum lttng_live_viewer_status viewer_status
;
1791 bt_logging_level log_level
;
1792 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1794 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1795 log_level
= lttng_live
->log_level
;
1796 self_comp
= lttng_live
->self_comp
;
1798 /* There can be only one downstream iterator at the same time. */
1799 BT_ASSERT(!lttng_live
->has_msg_iter
);
1800 lttng_live
->has_msg_iter
= true;
1802 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1803 if (!lttng_live_msg_iter
) {
1804 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create lttng_live_msg_iter");
1805 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1809 viewer_status
= live_viewer_connection_create(
1810 self_comp
, NULL
, log_level
, lttng_live
->params
.url
->str
, false, lttng_live_msg_iter
,
1811 <tng_live_msg_iter
->viewer_connection
);
1812 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1813 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1814 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer connection");
1815 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1817 * Interruption in the _iter_init() method is not
1818 * supported. Return an error.
1820 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted while creating viewer connection");
1823 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1827 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1828 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1829 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1830 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer session");
1831 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1833 * Interruption in the _iter_init() method is not
1834 * supported. Return an error.
1836 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted when creating viewer session");
1839 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1843 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1844 switch (lttng_live
->params
.sess_not_found_act
) {
1845 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1847 "Unable to connect to the requested live viewer session. Keep trying to connect because of "
1848 "%s=\"%s\" component parameter: url=\"%s\"",
1849 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1850 lttng_live
->params
.url
->str
);
1852 case SESSION_NOT_FOUND_ACTION_FAIL
:
1853 BT_COMP_LOGE_APPEND_CAUSE(
1855 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1856 "component parameter: url =\"%s\"",
1857 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1858 lttng_live
->params
.url
->str
);
1859 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1861 case SESSION_NOT_FOUND_ACTION_END
:
1863 "Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1864 "call because of %s=\"%s\" component parameter: "
1866 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1867 lttng_live
->params
.url
->str
);
1874 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1875 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1879 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1884 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1885 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1886 bt_param_validation_value_descr::makeString()},
1887 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1889 static bt_component_class_query_method_status
1890 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1891 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1893 bt_component_class_query_method_status status
;
1894 const bt_value
*url_value
= NULL
;
1896 struct live_viewer_connection
*viewer_connection
= NULL
;
1897 enum lttng_live_viewer_status viewer_status
;
1898 enum bt_param_validation_status validation_status
;
1899 gchar
*validate_error
= NULL
;
1901 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1902 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1903 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1905 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1906 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1907 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1911 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1912 url
= bt_value_string_get(url_value
);
1914 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
, log_level
, url
, true, NULL
,
1915 &viewer_connection
);
1916 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1917 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1918 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to create viewer connection");
1919 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1920 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1921 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1928 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1929 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1930 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to list viewer sessions");
1937 BT_VALUE_PUT_REF_AND_RESET(*result
);
1940 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1944 if (viewer_connection
) {
1945 live_viewer_connection_destroy(viewer_connection
);
1948 g_free(validate_error
);
1953 static bt_component_class_query_method_status
1954 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1955 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1957 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1958 const bt_value
*input_type_value
;
1959 const bt_value
*input_value
;
1961 struct bt_common_lttng_live_url_parts parts
= {0};
1963 /* Used by the logging macros */
1964 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1967 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1968 if (!input_type_value
) {
1969 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `type` parameter.");
1973 if (!bt_value_is_string(input_type_value
)) {
1974 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "`type` parameter is not a string value.");
1978 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1979 /* We don't handle file system paths */
1983 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1985 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `input` parameter.");
1989 if (!bt_value_is_string(input_value
)) {
1990 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1991 "`input` parameter is not a string value.");
1995 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1996 if (parts
.session_name
) {
1998 * Looks pretty much like an LTTng live URL: we got the
1999 * session name part, which forms a complete URL.
2005 *result
= bt_value_real_create_init(weight
);
2007 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2015 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2018 BT_ASSERT(!*result
);
2021 bt_common_destroy_lttng_live_url_parts(&parts
);
2025 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
2026 bt_private_query_executor
*priv_query_exec
,
2027 const char *object
, const bt_value
*params
,
2028 __attribute__((unused
)) void *method_data
,
2029 const bt_value
**result
)
2031 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2032 bt_self_component
*self_comp
= NULL
;
2033 bt_self_component_class
*self_comp_class
=
2034 bt_self_component_class_source_as_self_component_class(comp_class
);
2035 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2036 bt_private_query_executor_as_query_executor_const(priv_query_exec
));
2038 if (strcmp(object
, "sessions") == 0) {
2039 status
= lttng_live_query_list_sessions(params
, result
, self_comp_class
, log_level
);
2040 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2041 status
= lttng_live_query_support_info(params
, result
, self_comp_class
, log_level
);
2043 BT_COMP_LOGI("Unknown query object `%s`", object
);
2044 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2052 static void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2057 if (lttng_live
->params
.url
) {
2058 g_string_free(lttng_live
->params
.url
, TRUE
);
2063 void lttng_live_component_finalize(bt_self_component_source
*component
)
2065 lttng_live_component
*data
= (lttng_live_component
*) bt_self_component_get_data(
2066 bt_self_component_source_as_self_component(component
));
2071 lttng_live_component_destroy_data(data
);
2074 static enum session_not_found_action
2075 parse_session_not_found_action_param(const bt_value
*no_session_param
)
2077 enum session_not_found_action action
;
2078 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2080 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2081 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2082 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2083 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2085 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2086 action
= SESSION_NOT_FOUND_ACTION_END
;
2092 static bt_param_validation_value_descr inputs_elem_descr
=
2093 bt_param_validation_value_descr::makeString();
2095 static const char *sess_not_found_action_choices
[] = {
2096 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2097 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2098 SESS_NOT_FOUND_ACTION_END_STR
,
2101 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2102 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
2103 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
2104 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
2105 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
2106 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
2108 static bt_component_class_initialize_method_status
2109 lttng_live_component_create(const bt_value
*params
, bt_logging_level log_level
,
2110 bt_self_component
*self_comp
, struct lttng_live_component
**component
)
2112 struct lttng_live_component
*lttng_live
= NULL
;
2113 const bt_value
*inputs_value
;
2114 const bt_value
*url_value
;
2115 const bt_value
*value
;
2117 enum bt_param_validation_status validation_status
;
2118 gchar
*validation_error
= NULL
;
2119 bt_component_class_initialize_method_status status
;
2121 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
2122 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2123 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2125 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2126 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2127 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2131 lttng_live
= g_new0(struct lttng_live_component
, 1);
2133 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2136 lttng_live
->log_level
= log_level
;
2137 lttng_live
->self_comp
= self_comp
;
2138 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2139 lttng_live
->has_msg_iter
= false;
2141 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2142 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2143 url
= bt_value_string_get(url_value
);
2145 lttng_live
->params
.url
= g_string_new(url
);
2146 if (!lttng_live
->params
.url
) {
2147 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2151 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
2153 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
2155 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2156 "defaulting to `%s`.",
2157 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2158 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2161 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2165 lttng_live_component_destroy_data(lttng_live
);
2168 g_free(validation_error
);
2170 *component
= lttng_live
;
2174 bt_component_class_initialize_method_status
2175 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2176 bt_self_component_source_configuration
*config
, const bt_value
*params
,
2177 __attribute__((unused
)) void *init_method_data
)
2179 struct lttng_live_component
*lttng_live
;
2180 bt_component_class_initialize_method_status ret
;
2181 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2182 bt_logging_level log_level
=
2183 bt_component_get_logging_level(bt_self_component_as_component(self_comp
));
2184 bt_self_component_add_port_status add_port_status
;
2186 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2187 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2191 add_port_status
= bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2192 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2193 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2197 bt_self_component_set_data(self_comp
, lttng_live
);
2201 lttng_live_component_destroy_data(lttng_live
);