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_msg_iter
*lttng_live_msg_iter
,
724 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
726 const bt_clock_snapshot
*clock_snapshot
= NULL
;
728 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
729 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
732 BT_ASSERT_DBG(ts_ns
);
734 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
735 "last-msg-ts=%" PRId64
,
736 lttng_live_msg_iter
, msg
, last_msg_ts_ns
);
738 switch (bt_message_get_type(msg
)) {
739 case BT_MESSAGE_TYPE_EVENT
:
740 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
742 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
743 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
745 case BT_MESSAGE_TYPE_PACKET_END
:
746 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
748 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
750 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
752 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
754 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
756 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
757 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
760 /* All the other messages have a higher priority */
761 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
762 *ts_ns
= last_msg_ts_ns
;
766 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
768 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
769 "Cannot get nanoseconds from Epoch of clock snapshot: "
770 "clock-snapshot-addr=%p",
782 BT_COMP_LOGD("Found message's timestamp: "
783 "iter-data-addr=%p, msg-addr=%p, "
784 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
785 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
791 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
792 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
793 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
795 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
796 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
797 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
798 enum ctf_msg_iter_status status
;
799 uint64_t session_idx
, trace_idx
;
801 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
802 struct lttng_live_session
*session
=
803 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
805 if (session
->new_streams_needed
) {
806 BT_COMP_LOGD("Need an update for streams: "
807 "session-id=%" PRIu64
,
809 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
812 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
813 struct lttng_live_trace
*trace
=
814 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
815 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
816 BT_COMP_LOGD("Need an update for metadata stream: "
817 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
818 session
->id
, trace
->id
);
819 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
825 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
826 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
827 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
828 "Invalid state of live stream iterator"
829 "stream-iter-status=%s",
830 lttng_live_stream_state_string(lttng_live_stream
->state
));
834 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
, message
);
836 case CTF_MSG_ITER_STATUS_EOF
:
837 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
839 case CTF_MSG_ITER_STATUS_OK
:
840 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
842 case CTF_MSG_ITER_STATUS_AGAIN
:
844 * Continue immediately (end of packet). The next
845 * get_index may return AGAIN to delay the following
848 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
850 case CTF_MSG_ITER_STATUS_ERROR
:
852 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
853 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
854 "CTF message iterator failed to get next message: "
855 "msg-iter=%p, msg-iter-status=%s",
856 lttng_live_stream
->msg_iter
, ctf_msg_iter_status_string(status
));
864 static enum lttng_live_iterator_status
865 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
866 struct lttng_live_stream_iterator
*stream_iter
,
867 const bt_message
**curr_msg
)
869 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
870 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
871 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
873 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
874 "viewer-stream-id=%" PRIu64
,
875 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
878 * The viewer has hung up on us so we are closing the stream. The
879 * `ctf_msg_iter` should simply realize that it needs to close the
880 * stream properly by emitting the necessary stream end message.
882 enum ctf_msg_iter_status status
=
883 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
, curr_msg
);
885 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
886 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
887 "Error getting the next message from CTF message iterator");
888 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
890 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
891 BT_COMP_LOGI("Reached the end of the live stream iterator.");
892 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
896 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
904 * handle_no_data_streams()
906 * - for each ACTIVE_NO_DATA stream:
907 * - query relayd for stream data, or quiescence info.
908 * - if need metadata, get metadata, goto retry.
909 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
910 * - if quiescent, move to QUIESCENT streams
911 * - if fetched data, move to ACTIVE_DATA streams
912 * (at this point each stream either has data, or is quiescent)
916 * handle_new_streams_and_metadata()
917 * - query relayd for known streams, add them as ACTIVE_NO_DATA
918 * - query relayd for metadata
920 * call handle_active_no_data_streams()
922 * handle_quiescent_streams()
923 * - if at least one stream is ACTIVE_DATA:
924 * - peek stream event with lowest timestamp -> next_ts
925 * - for each quiescent stream
926 * - if next_ts >= quiescent end
927 * - set state to ACTIVE_NO_DATA
929 * - for each quiescent stream
930 * - set state to ACTIVE_NO_DATA
932 * call handle_active_no_data_streams()
934 * handle_active_data_streams()
935 * - if at least one stream is ACTIVE_DATA:
936 * - get stream event with lowest timestamp from heap
937 * - make that stream event the current message.
938 * - move this stream heap position to its next event
939 * - if we need to fetch data from relayd, move
940 * stream to ACTIVE_NO_DATA.
944 * end criterion: ctrl-c on client. If relayd exits or the session
945 * closes on the relay daemon side, we keep on waiting for streams.
946 * Eventually handle --end timestamp (also an end criterion).
948 * When disconnected from relayd: try to re-connect endlessly.
950 static enum lttng_live_iterator_status
951 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
952 struct lttng_live_stream_iterator
*stream_iter
,
953 const bt_message
**curr_msg
)
955 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
956 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
957 enum lttng_live_iterator_status live_status
;
959 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
960 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
961 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
963 if (stream_iter
->has_stream_hung_up
) {
965 * The stream has hung up and the stream was properly closed
966 * during the last call to the current function. Return _END
967 * status now so that this stream iterator is removed for the
968 * stream iterator list.
970 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
975 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
978 * Make sure we have the most recent metadata and possibly some new
981 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
982 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
987 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
988 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
989 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
991 * We overwrite `live_status` since `curr_msg` is
992 * likely set to a valid message in this function.
995 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
1000 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
1001 stream_iter
, curr_msg
);
1002 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1003 BT_ASSERT(!*curr_msg
);
1009 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
1010 stream_iter
, curr_msg
);
1011 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1012 BT_ASSERT(!*curr_msg
);
1016 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1017 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1021 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, "
1022 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1023 lttng_live_iterator_status_string(live_status
), stream_iter
->name
->str
,
1024 stream_iter
->viewer_stream_id
);
1029 static bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1031 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1033 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1034 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1037 static enum lttng_live_iterator_status
1038 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1039 const bt_message
*msg_in
, bt_message
**msg_out
,
1040 uint64_t new_begin_ts
)
1042 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1043 enum bt_property_availability availability
;
1044 const bt_clock_snapshot
*clock_snapshot
;
1048 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1049 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1051 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1052 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1054 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1055 iter
, stream
, new_begin_ts
, end_ts
);
1057 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1061 bt_message_discarded_packets_set_count(*msg_out
, count
);
1066 static enum lttng_live_iterator_status
1067 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1068 const bt_message
*msg_in
, bt_message
**msg_out
,
1069 uint64_t new_begin_ts
)
1071 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1072 enum bt_property_availability availability
;
1073 const bt_clock_snapshot
*clock_snapshot
;
1077 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1078 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1080 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1081 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1083 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1084 iter
, stream
, new_begin_ts
, end_ts
);
1086 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1090 bt_message_discarded_events_set_count(*msg_out
, count
);
1095 static enum lttng_live_iterator_status
1096 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1097 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
1098 const bt_message
*late_msg
)
1100 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1101 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1102 const bt_clock_class
*clock_class
;
1103 const bt_stream_class
*stream_class
;
1104 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1105 int64_t last_inactivity_ts_ns
;
1106 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1107 enum lttng_live_iterator_status adjust_status
;
1108 bt_message
*adjusted_message
;
1111 * The timestamp of the current message is before the last message sent
1112 * by this component. We CANNOT send it as is.
1114 * The only expected scenario in which that could happen is the
1115 * following, everything else is a bug in this component, relay deamon,
1118 * Expected scenario: The CTF message iterator emitted discarded
1119 * packets and discarded events with synthesized beginning and end
1120 * timestamps from the bounds of the last known packet and the newly
1121 * decoded packet header. The CTF message iterator is not aware of
1122 * stream inactivity beacons. Hence, we have to adjust the beginning
1123 * timestamp of those types of messages if a stream signalled its
1124 * inactivity up until _after_ the last known packet's beginning
1127 * Otherwise, the monotonicity guarantee of message timestamps would
1130 * In short, the only scenario in which it's okay and fixable to
1131 * received a late message is when:
1132 * 1. the late message is a discarded packets or discarded events
1134 * 2. this stream produced an inactivity message downstream, and
1135 * 3. the timestamp of the late message is within the inactivity
1136 * timespan we sent downstream through the inactivity message.
1139 BT_COMP_LOGD("Handling late message on live stream iterator: "
1140 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1141 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1143 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1144 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1145 "have a late message when no inactivity message "
1146 "was ever sent for that stream.");
1147 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1151 if (!is_discarded_packet_or_event_message(late_msg
)) {
1152 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1153 "Invalid live stream state: "
1154 "have a late message that is not a packet discarded or "
1155 "event discarded message: late-msg-type=%s",
1156 bt_common_message_type_string(bt_message_get_type(late_msg
)));
1157 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1161 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1162 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1164 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1165 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1166 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1167 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error converting last "
1168 "inactivity message timestamp to nanoseconds");
1169 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1173 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1174 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1175 "Invalid live stream state: "
1176 "have a late message that is none included in a stream "
1177 "inactivity timespan: last-inactivity-ts-ns=%" PRIu64
1178 "late-msg-ts-ns=%" PRIu64
,
1179 last_inactivity_ts_ns
, late_msg_ts_ns
);
1180 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1185 * We now know that it's okay for this message to be late, we can now
1186 * adjust its timestamp to ensure monotonicity.
1188 BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, "
1189 "msg-new-ts-ns=%" PRIu64
,
1190 bt_common_message_type_string(bt_message_get_type(late_msg
)),
1191 stream_iter
->last_inactivity_ts
.value
);
1192 switch (bt_message_get_type(late_msg
)) {
1193 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1194 adjust_status
= adjust_discarded_events_message(
1195 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1196 stream_iter
->last_inactivity_ts
.value
);
1198 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1199 adjust_status
= adjust_discarded_packets_message(
1200 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1201 stream_iter
->last_inactivity_ts
.value
);
1207 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1208 stream_iter_status
= adjust_status
;
1212 BT_ASSERT_DBG(adjusted_message
);
1213 stream_iter
->current_msg
= adjusted_message
;
1214 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1215 bt_message_put_ref(late_msg
);
1218 return stream_iter_status
;
1221 static enum lttng_live_iterator_status
1222 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1223 struct lttng_live_trace
*live_trace
,
1224 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1226 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1227 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1228 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1229 enum lttng_live_iterator_status stream_iter_status
;
1231 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1232 uint64_t stream_iter_idx
;
1234 BT_ASSERT_DBG(live_trace
);
1235 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1237 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1238 "trace-id=%" PRIu64
,
1241 * Update the current message of every stream iterators of this trace.
1242 * The current msg of every stream must have a timestamp equal or
1243 * larger than the last message returned by this iterator. We must
1244 * ensure monotonicity.
1246 stream_iter_idx
= 0;
1247 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1248 bool stream_iter_is_ended
= false;
1249 struct lttng_live_stream_iterator
*stream_iter
=
1250 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1254 * If there is no current message for this stream, go fetch
1257 while (!stream_iter
->current_msg
) {
1258 const bt_message
*msg
= NULL
;
1259 int64_t curr_msg_ts_ns
= INT64_MAX
;
1261 stream_iter_status
=
1262 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, &msg
);
1264 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1265 stream_iter_is_ended
= true;
1269 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1275 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, "
1276 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1277 bt_common_message_type_string(bt_message_get_type(msg
)),
1278 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1281 * Get the timestamp in nanoseconds from origin of this
1284 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1288 * Check if the message of the current live stream
1289 * iterator occurred at the exact same time or after the
1290 * last message returned by this component's message
1291 * iterator. If not, we need to handle it with care.
1293 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1294 stream_iter
->current_msg
= msg
;
1295 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1298 * We received a message from the past. This
1299 * may be fixable but it can also be an error.
1301 stream_iter_status
=
1302 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, msg
);
1303 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1304 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1305 "Late message could not be handled correctly: "
1306 "lttng-live-msg-iter-addr=%p, "
1307 "stream-name=\"%s\", "
1308 "curr-msg-ts=%" PRId64
", last-msg-ts=%" PRId64
,
1309 lttng_live_msg_iter
, stream_iter
->name
->str
,
1310 curr_msg_ts_ns
, lttng_live_msg_iter
->last_msg_ts_ns
);
1311 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1317 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1319 if (!stream_iter_is_ended
) {
1320 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1321 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1323 * Update the current best candidate message
1324 * for the stream iterator of this live trace
1325 * to be forwarded downstream.
1327 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1328 youngest_candidate_stream_iter
= stream_iter
;
1329 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1331 * Order the messages in an arbitrary but
1332 * deterministic way.
1334 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1335 int ret
= common_muxing_compare_messages(
1336 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1339 * The `youngest_candidate_stream_iter->current_msg`
1340 * should go first. Update the next
1341 * iterator and the current timestamp.
1343 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1344 youngest_candidate_stream_iter
= stream_iter
;
1345 } else if (ret
== 0) {
1347 * Unable to pick which one should go
1351 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1352 "stream-iter-addr=%p"
1353 "stream-iter-addr=%p",
1354 stream_iter
, youngest_candidate_stream_iter
);
1361 * The live stream iterator has ended. That
1362 * iterator is removed from the array, but
1363 * there is no need to increment
1364 * stream_iter_idx as
1365 * g_ptr_array_remove_index_fast replaces the
1366 * removed element with the array's last
1369 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
, stream_iter_idx
);
1373 if (youngest_candidate_stream_iter
) {
1374 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1375 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1378 * The only case where we don't have a candidate for this trace
1379 * is if we reached the end of all the iterators.
1381 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1382 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1386 return stream_iter_status
;
1389 static enum lttng_live_iterator_status
1390 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1391 struct lttng_live_session
*session
,
1392 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1394 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1395 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1396 enum lttng_live_iterator_status stream_iter_status
;
1397 uint64_t trace_idx
= 0;
1398 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1399 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1401 BT_COMP_LOGD("Finding the next stream iterator for session: "
1402 "session-id=%" PRIu64
,
1405 * Make sure we are attached to the session and look for new streams
1408 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1409 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1410 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1411 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1415 BT_ASSERT_DBG(session
->traces
);
1417 while (trace_idx
< session
->traces
->len
) {
1418 bool trace_is_ended
= false;
1419 struct lttng_live_stream_iterator
*stream_iter
;
1420 struct lttng_live_trace
*trace
=
1421 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1423 stream_iter_status
=
1424 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1425 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1427 * All the live stream iterators for this trace are
1428 * ENDed. Remove the trace from this session.
1430 trace_is_ended
= true;
1431 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1435 if (!trace_is_ended
) {
1436 BT_ASSERT_DBG(stream_iter
);
1438 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1439 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1440 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1441 youngest_candidate_stream_iter
= stream_iter
;
1442 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1444 * Order the messages in an arbitrary but
1445 * deterministic way.
1447 int ret
= common_muxing_compare_messages(
1448 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1451 * The `youngest_candidate_stream_iter->current_msg`
1452 * should go first. Update the next iterator
1453 * and the current timestamp.
1455 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1456 youngest_candidate_stream_iter
= stream_iter
;
1457 } else if (ret
== 0) {
1458 /* Unable to pick which one should go first. */
1460 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1461 "stream-iter-addr=%p"
1462 "stream-iter-addr=%p",
1463 stream_iter
, youngest_candidate_stream_iter
);
1469 * trace_idx is not incremented since
1470 * g_ptr_array_remove_index_fast replaces the
1471 * element at trace_idx with the array's last element.
1473 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1476 if (youngest_candidate_stream_iter
) {
1477 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1478 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1481 * The only cases where we don't have a candidate for this
1483 * 1. if we reached the end of all the iterators of all the
1484 * traces of this session,
1485 * 2. if we never had live stream iterator in the first place.
1487 * In either cases, we return END.
1489 BT_ASSERT(session
->traces
->len
== 0);
1490 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1493 return stream_iter_status
;
1496 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1500 for (i
= 0; i
< count
; i
++) {
1501 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1505 bt_message_iterator_class_next_method_status
1506 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1507 uint64_t capacity
, uint64_t *count
)
1509 bt_message_iterator_class_next_method_status status
;
1510 enum lttng_live_viewer_status viewer_status
;
1511 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1512 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1513 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1514 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1515 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1516 enum lttng_live_iterator_status stream_iter_status
;
1517 uint64_t session_idx
;
1521 BT_ASSERT_DBG(lttng_live_msg_iter
);
1523 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1525 * The iterator was interrupted in a previous call to the
1526 * `_next()` method. We currently do not support generating
1527 * messages after such event. The babeltrace2 CLI should never
1528 * be running the graph after being interrupted. So this check
1529 * is to prevent other graph users from using this live
1530 * iterator in an messed up internal state.
1532 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1533 BT_COMP_LOGE_APPEND_CAUSE(
1535 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1540 * Clear all the invalid message reference that might be left over in
1543 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1546 * If no session are exposed on the relay found at the url provided by
1547 * the user, session count will be 0. In this case, we return status
1548 * end to return gracefully.
1550 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1551 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1552 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1556 * The are no more active session for this session
1557 * name. Retry to create a viewer session for the
1558 * requested session name.
1560 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1561 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1562 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1563 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1564 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1565 "Error creating LTTng live viewer session");
1566 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1567 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1576 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1577 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1581 * Here the muxing of message is done.
1583 * We need to iterate over all the streams of all the traces of all the
1584 * viewer sessions in order to get the message with the smallest
1585 * timestamp. In this case, a session is a viewer session and there is
1586 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1587 * kernel). Each viewer session can have multiple traces, for example,
1588 * 64bit UST viewer sessions could have multiple per-pid traces.
1590 * We iterate over the streams of each traces to update and see what is
1591 * their next message's timestamp. From those timestamps, we select the
1592 * message with the smallest timestamp as the best candidate message
1593 * for that trace and do the same thing across all the sessions.
1595 * We then compare the timestamp of best candidate message of all the
1596 * sessions to pick the message with the smallest timestamp and we
1599 while (*count
< capacity
) {
1600 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1601 *candidate_stream_iter
= NULL
;
1602 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1604 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1606 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1607 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1608 lttng_live_msg_iter
->sessions
, session_idx
);
1610 /* Find the best candidate message to send downstream. */
1611 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1612 &candidate_stream_iter
);
1614 /* If we receive an END status, it means that either:
1615 * - Those traces never had active streams (UST with no
1616 * data produced yet),
1617 * - All live stream iterators have ENDed.*/
1618 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1619 if (session
->closed
&& session
->traces
->len
== 0) {
1621 * Remove the session from the list.
1622 * session_idx is not modified since
1623 * g_ptr_array_remove_index_fast
1624 * replaces the the removed element with
1625 * the array's last element.
1627 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1634 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1638 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1639 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1640 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1641 youngest_stream_iter
= candidate_stream_iter
;
1642 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1644 * The currently selected message to be sent
1645 * downstream next has the exact same timestamp
1646 * that of the current candidate message. We
1647 * must break the tie in a predictable manner.
1650 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1652 * Order the messages in an arbitrary but
1653 * deterministic way.
1655 int ret
= common_muxing_compare_messages(candidate_stream_iter
->current_msg
,
1656 youngest_stream_iter
->current_msg
);
1659 * The `candidate_stream_iter->current_msg`
1660 * should go first. Update the next
1661 * iterator and the current timestamp.
1663 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1664 youngest_stream_iter
= candidate_stream_iter
;
1665 } else if (ret
== 0) {
1666 /* Unable to pick which one should go first. */
1668 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1669 "next-stream-iter-addr=%p"
1670 "candidate-stream-iter-addr=%p",
1671 youngest_stream_iter
, candidate_stream_iter
);
1678 if (!youngest_stream_iter
) {
1679 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1683 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1684 /* Ensure monotonicity. */
1685 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1686 youngest_stream_iter
->current_msg_ts_ns
);
1689 * Insert the next message to the message batch. This will set
1690 * stream iterator current messsage to NULL so that next time
1691 * we fetch the next message of that stream iterator
1693 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1696 /* Update the last timestamp in nanoseconds sent downstream. */
1697 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1698 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1700 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1704 switch (stream_iter_status
) {
1705 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1706 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1708 * If we gathered messages, return _OK even if the graph was
1709 * interrupted. This allows for the components downstream to at
1710 * least get the thoses messages. If the graph was indeed
1711 * interrupted there should not be another _next() call as the
1712 * application will tear down the graph. This component class
1713 * doesn't support restarting after an interruption.
1716 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1718 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1721 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1722 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1724 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1725 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1726 "Memory error preparing the next batch of messages: "
1727 "live-iter-status=%s",
1728 lttng_live_iterator_status_string(stream_iter_status
));
1729 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1731 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1732 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1733 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1734 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1735 "Error preparing the next batch of messages: "
1736 "live-iter-status=%s",
1737 lttng_live_iterator_status_string(stream_iter_status
));
1739 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1740 /* Put all existing messages on error. */
1741 put_messages(msgs
, *count
);
1751 static struct lttng_live_msg_iter
*
1752 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1753 bt_self_message_iterator
*self_msg_it
)
1755 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1756 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1758 struct lttng_live_msg_iter
*lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1759 if (!lttng_live_msg_iter
) {
1760 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate lttng_live_msg_iter");
1764 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1765 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1766 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1767 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1769 lttng_live_msg_iter
->active_stream_iter
= 0;
1770 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1771 lttng_live_msg_iter
->was_interrupted
= false;
1773 lttng_live_msg_iter
->sessions
=
1774 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1775 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1778 return lttng_live_msg_iter
;
1781 bt_message_iterator_class_initialize_method_status
1782 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1783 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1785 bt_message_iterator_class_initialize_method_status status
;
1786 struct lttng_live_component
*lttng_live
;
1787 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1788 enum lttng_live_viewer_status viewer_status
;
1789 bt_logging_level log_level
;
1790 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1792 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1793 log_level
= lttng_live
->log_level
;
1794 self_comp
= lttng_live
->self_comp
;
1796 /* There can be only one downstream iterator at the same time. */
1797 BT_ASSERT(!lttng_live
->has_msg_iter
);
1798 lttng_live
->has_msg_iter
= true;
1800 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1801 if (!lttng_live_msg_iter
) {
1802 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create lttng_live_msg_iter");
1803 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1807 viewer_status
= live_viewer_connection_create(
1808 self_comp
, NULL
, log_level
, lttng_live
->params
.url
->str
, false, lttng_live_msg_iter
,
1809 <tng_live_msg_iter
->viewer_connection
);
1810 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1811 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1812 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer connection");
1813 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1815 * Interruption in the _iter_init() method is not
1816 * supported. Return an error.
1818 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted while creating viewer connection");
1821 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1825 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1826 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1827 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1828 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer session");
1829 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1831 * Interruption in the _iter_init() method is not
1832 * supported. Return an error.
1834 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted when creating viewer session");
1837 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1841 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1842 switch (lttng_live
->params
.sess_not_found_act
) {
1843 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1845 "Unable to connect to the requested live viewer session. Keep trying to connect because of "
1846 "%s=\"%s\" component parameter: url=\"%s\"",
1847 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1848 lttng_live
->params
.url
->str
);
1850 case SESSION_NOT_FOUND_ACTION_FAIL
:
1851 BT_COMP_LOGE_APPEND_CAUSE(
1853 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1854 "component parameter: url =\"%s\"",
1855 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1856 lttng_live
->params
.url
->str
);
1857 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1859 case SESSION_NOT_FOUND_ACTION_END
:
1861 "Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1862 "call because of %s=\"%s\" component parameter: "
1864 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1865 lttng_live
->params
.url
->str
);
1872 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1873 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1877 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1882 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1883 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1884 bt_param_validation_value_descr::makeString()},
1885 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1887 static bt_component_class_query_method_status
1888 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1889 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1891 bt_component_class_query_method_status status
;
1892 const bt_value
*url_value
= NULL
;
1894 struct live_viewer_connection
*viewer_connection
= NULL
;
1895 enum lttng_live_viewer_status viewer_status
;
1896 enum bt_param_validation_status validation_status
;
1897 gchar
*validate_error
= NULL
;
1899 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1900 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1901 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1903 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1904 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1905 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1909 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1910 url
= bt_value_string_get(url_value
);
1912 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
, log_level
, url
, true, NULL
,
1913 &viewer_connection
);
1914 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1915 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1916 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to create viewer connection");
1917 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1918 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1919 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1926 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1927 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1928 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to list viewer sessions");
1935 BT_VALUE_PUT_REF_AND_RESET(*result
);
1938 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1942 if (viewer_connection
) {
1943 live_viewer_connection_destroy(viewer_connection
);
1946 g_free(validate_error
);
1951 static bt_component_class_query_method_status
1952 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1953 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1955 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1956 const bt_value
*input_type_value
;
1957 const bt_value
*input_value
;
1959 struct bt_common_lttng_live_url_parts parts
= {};
1961 /* Used by the logging macros */
1962 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1965 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1966 if (!input_type_value
) {
1967 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `type` parameter.");
1971 if (!bt_value_is_string(input_type_value
)) {
1972 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "`type` parameter is not a string value.");
1976 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1977 /* We don't handle file system paths */
1981 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1983 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `input` parameter.");
1987 if (!bt_value_is_string(input_value
)) {
1988 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1989 "`input` parameter is not a string value.");
1993 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1994 if (parts
.session_name
) {
1996 * Looks pretty much like an LTTng live URL: we got the
1997 * session name part, which forms a complete URL.
2003 *result
= bt_value_real_create_init(weight
);
2005 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2013 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2016 BT_ASSERT(!*result
);
2019 bt_common_destroy_lttng_live_url_parts(&parts
);
2023 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
2024 bt_private_query_executor
*priv_query_exec
,
2025 const char *object
, const bt_value
*params
,
2026 __attribute__((unused
)) void *method_data
,
2027 const bt_value
**result
)
2029 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2030 bt_self_component
*self_comp
= NULL
;
2031 bt_self_component_class
*self_comp_class
=
2032 bt_self_component_class_source_as_self_component_class(comp_class
);
2033 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2034 bt_private_query_executor_as_query_executor_const(priv_query_exec
));
2036 if (strcmp(object
, "sessions") == 0) {
2037 status
= lttng_live_query_list_sessions(params
, result
, self_comp_class
, log_level
);
2038 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2039 status
= lttng_live_query_support_info(params
, result
, self_comp_class
, log_level
);
2041 BT_COMP_LOGI("Unknown query object `%s`", object
);
2042 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2050 static void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2055 if (lttng_live
->params
.url
) {
2056 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
;
2172 bt_component_class_initialize_method_status
2173 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2174 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
2176 struct lttng_live_component
*lttng_live
;
2177 bt_component_class_initialize_method_status ret
;
2178 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2179 bt_logging_level log_level
=
2180 bt_component_get_logging_level(bt_self_component_as_component(self_comp
));
2181 bt_self_component_add_port_status add_port_status
;
2183 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2184 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2188 add_port_status
= bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2189 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2190 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2194 bt_self_component_set_data(self_comp
, lttng_live
);
2198 lttng_live_component_destroy_data(lttng_live
);