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
15 #include <babeltrace2/babeltrace.h>
17 #define BT_COMP_LOG_SELF_COMP self_comp
18 #define BT_LOG_OUTPUT_LEVEL log_level
19 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
20 #include "logging/comp-logging.h"
22 #include "common/assert.h"
24 #include "plugins/common/muxing/muxing.h"
25 #include "plugins/common/param-validation/param-validation.h"
27 #include "data-stream.hpp"
28 #include "lttng-live.hpp"
29 #include "metadata.hpp"
31 #define MAX_QUERY_SIZE (256 * 1024)
32 #define URL_PARAM "url"
33 #define INPUTS_PARAM "inputs"
34 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
35 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
36 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
37 #define SESS_NOT_FOUND_ACTION_END_STR "end"
39 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ##__VA_ARGS__)
41 static const char *lttng_live_iterator_status_string(enum lttng_live_iterator_status status
)
44 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
45 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
46 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
47 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
48 case LTTNG_LIVE_ITERATOR_STATUS_END
:
49 return "LTTNG_LIVE_ITERATOR_STATUS_END";
50 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
51 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
52 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
53 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
54 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
55 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
56 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
57 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
58 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
59 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
65 static const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
68 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
69 return "ACTIVE_NO_DATA";
70 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
71 return "QUIESCENT_NO_DATA";
72 case LTTNG_LIVE_STREAM_QUIESCENT
:
74 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
76 case LTTNG_LIVE_STREAM_EOF
:
83 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
84 enum lttng_live_stream_state new_state
)
86 bt_self_component
*self_comp
= stream_iter
->self_comp
;
87 bt_logging_level log_level
= stream_iter
->log_level
;
89 BT_COMP_LOGD("Setting live stream iterator state: viewer-stream-id=%" PRIu64
90 ", old-state=%s, new-state=%s",
91 stream_iter
->viewer_stream_id
, lttng_live_stream_state_string(stream_iter
->state
),
92 lttng_live_stream_state_string(new_state
));
94 stream_iter
->state
= new_state
;
97 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
99 BT_COMP_LOGD("Live stream iterator state=%s, " \
100 "last-inact-ts-is-set=%d, last-inact-ts-value=%" PRId64 ", " \
101 "curr-inact-ts=%" PRId64, \
102 lttng_live_stream_state_string(live_stream_iter->state), \
103 live_stream_iter->last_inactivity_ts.is_set, \
104 live_stream_iter->last_inactivity_ts.value, \
105 live_stream_iter->current_inactivity_ts); \
108 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
117 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
123 static struct lttng_live_trace
*
124 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
127 struct lttng_live_trace
*ret_trace
= NULL
;
129 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
130 struct lttng_live_trace
*trace
=
131 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
132 if (trace
->id
== trace_id
) {
142 static void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
144 bt_logging_level log_level
= trace
->log_level
;
145 bt_self_component
*self_comp
= trace
->self_comp
;
147 BT_COMP_LOGD("Destroying live trace: trace-id=%" PRIu64
, trace
->id
);
149 BT_ASSERT(trace
->stream_iterators
);
150 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
152 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
153 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
155 lttng_live_metadata_fini(trace
);
159 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
162 struct lttng_live_trace
*trace
= NULL
;
163 bt_logging_level log_level
= session
->log_level
;
164 bt_self_component
*self_comp
= session
->self_comp
;
166 BT_COMP_LOGD("Creating live trace: "
167 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
168 session
->id
, trace_id
);
169 trace
= g_new0(struct lttng_live_trace
, 1);
171 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate live trace");
174 trace
->log_level
= session
->log_level
;
175 trace
->self_comp
= session
->self_comp
;
176 trace
->session
= session
;
177 trace
->id
= trace_id
;
178 trace
->trace_class
= NULL
;
180 trace
->stream_iterators
=
181 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_stream_iterator_destroy
);
182 BT_ASSERT(trace
->stream_iterators
);
183 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
184 g_ptr_array_add(session
->traces
, trace
);
194 struct lttng_live_trace
*
195 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
198 struct lttng_live_trace
*trace
;
200 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
205 /* The session is the owner of the newly created trace. */
206 trace
= lttng_live_create_trace(session
, trace_id
);
212 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
213 const char *hostname
, const char *session_name
)
216 struct lttng_live_session
*session
;
217 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
218 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
220 BT_COMP_LOGD("Adding live session: "
221 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
222 session_id
, hostname
, session_name
);
224 session
= g_new0(struct lttng_live_session
, 1);
226 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate live session");
230 session
->log_level
= lttng_live_msg_iter
->log_level
;
231 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
232 session
->id
= session_id
;
233 session
->traces
= g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_trace
);
234 BT_ASSERT(session
->traces
);
235 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
236 session
->new_streams_needed
= true;
237 session
->hostname
= g_string_new(hostname
);
238 BT_ASSERT(session
->hostname
);
240 session
->session_name
= g_string_new(session_name
);
241 BT_ASSERT(session
->session_name
);
243 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
252 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
254 bt_logging_level log_level
;
255 bt_self_component
*self_comp
;
261 log_level
= session
->log_level
;
262 self_comp
= session
->self_comp
;
263 BT_COMP_LOGD("Destroying live session: "
264 "session-id=%" PRIu64
", session-name=\"%s\"",
265 session
->id
, session
->session_name
->str
);
266 if (session
->id
!= -1ULL) {
267 if (lttng_live_session_detach(session
)) {
268 if (!lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
269 /* Old relayd cannot detach sessions. */
270 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
, session
->id
);
276 if (session
->traces
) {
277 g_ptr_array_free(session
->traces
, TRUE
);
280 if (session
->hostname
) {
281 g_string_free(session
->hostname
, TRUE
);
283 if (session
->session_name
) {
284 g_string_free(session
->session_name
, TRUE
);
292 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
294 if (!lttng_live_msg_iter
) {
298 if (lttng_live_msg_iter
->sessions
) {
299 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
302 if (lttng_live_msg_iter
->viewer_connection
) {
303 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
305 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
306 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
308 /* All stream iterators must be destroyed at this point. */
309 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
310 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
312 g_free(lttng_live_msg_iter
);
318 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
320 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
322 BT_ASSERT(self_msg_iter
);
324 lttng_live_msg_iter
=
325 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
326 BT_ASSERT(lttng_live_msg_iter
);
327 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
330 static enum lttng_live_iterator_status
331 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
333 bt_logging_level log_level
= lttng_live_stream
->log_level
;
334 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
336 switch (lttng_live_stream
->state
) {
337 case LTTNG_LIVE_STREAM_QUIESCENT
:
338 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
340 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
342 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
344 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
346 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
348 case LTTNG_LIVE_STREAM_EOF
:
351 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
355 * For active no data stream, fetch next index. As a result of that it can
357 * - quiescent: won't have events for a bit,
358 * - have data: need to get that data and produce the event,
359 * - have no data on this stream at this point: need to retry (AGAIN) or return
362 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
363 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
364 struct lttng_live_stream_iterator
*lttng_live_stream
)
366 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
367 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
368 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
369 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
370 struct packet_index index
;
372 if (lttng_live_stream
->trace
->metadata_stream_state
==
373 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
375 "Need to get an update for the metadata stream before proceeding further with this stream: "
376 "stream-name=\"%s\"",
377 lttng_live_stream
->name
->str
);
378 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
382 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
384 "Need to get an update of all streams before proceeding further with this stream: "
385 "stream-name=\"%s\"",
386 lttng_live_stream
->name
->str
);
387 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
391 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
392 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
395 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
396 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
400 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
402 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
403 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
404 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
406 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
408 * Because the stream is in the QUIESCENT_NO_DATA
409 * state, we can assert that the last_inactivity_ts was
410 * set and can be safely used in the `if` above.
412 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
414 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
415 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
417 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
422 lttng_live_stream
->base_offset
= index
.offset
;
423 lttng_live_stream
->offset
= index
.offset
;
424 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
426 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
427 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
428 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
429 lttng_live_stream
->name
->str
, lttng_live_stream
->viewer_stream_id
,
430 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
, lttng_live_stream
->len
);
433 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
434 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
440 * Creation of the message requires the ctf trace class to be created
441 * beforehand, but the live protocol gives us all streams (including metadata)
442 * at once. So we split it in three steps: getting streams, getting metadata
443 * (which creates the ctf trace class), and then creating the per-stream
446 static enum lttng_live_iterator_status
447 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
448 struct lttng_live_session
*session
)
450 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
451 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
452 enum lttng_live_iterator_status status
;
455 if (!session
->attached
) {
456 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
, session
->id
);
457 enum lttng_live_viewer_status attach_status
=
458 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
459 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
460 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
462 * Clear any causes appended in
463 * `lttng_live_attach_session()` as we want to
464 * return gracefully since the graph was
467 bt_current_thread_clear_error();
468 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
470 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
471 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error attaching to LTTng live session");
477 BT_COMP_LOGD("Updating all data streams: "
478 "session-id=%" PRIu64
", session-name=\"%s\"",
479 session
->id
, session
->session_name
->str
);
481 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
483 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
485 case LTTNG_LIVE_ITERATOR_STATUS_END
:
487 * We received a `_END` from the `_get_new_streams()` function,
488 * which means no more data will ever be received from the data
489 * streams of this session. But it's possible that the metadata
491 * The live protocol guarantees that we receive all the
492 * metadata needed before we receive data streams needing it.
493 * But it's possible to receive metadata NOT needed by
494 * data streams after the session was closed. For example, this
495 * could happen if a new event is registered and the session is
496 * stopped before any tracepoint for that event is actually
500 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
501 "session-id=%" PRIu64
", session-name=\"%s\"",
502 session
->id
, session
->session_name
->str
);
503 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
509 BT_COMP_LOGD("Updating metadata stream for session: "
510 "session-id=%" PRIu64
", session-name=\"%s\"",
511 session
->id
, session
->session_name
->str
);
514 while (trace_idx
< session
->traces
->len
) {
515 struct lttng_live_trace
*trace
=
516 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
518 status
= lttng_live_metadata_update(trace
);
520 case LTTNG_LIVE_ITERATOR_STATUS_END
:
521 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
524 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
525 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
528 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
529 "Error updating trace metadata: "
530 "stream-iter-status=%s, trace-id=%" PRIu64
,
531 lttng_live_iterator_status_string(status
), trace
->id
);
537 * Now that we have the metadata we can initialize the downstream
540 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
547 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
549 uint64_t session_idx
, trace_idx
;
550 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
551 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
553 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
554 struct lttng_live_session
*session
=
555 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
556 BT_COMP_LOGD("Force marking session as needing new streams: "
557 "session-id=%" PRIu64
,
559 session
->new_streams_needed
= true;
560 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
561 struct lttng_live_trace
*trace
=
562 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
563 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
564 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
565 session
->id
, trace
->id
);
567 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
569 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
574 static enum lttng_live_iterator_status
575 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
577 enum lttng_live_iterator_status status
;
578 enum lttng_live_viewer_status viewer_status
;
579 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
580 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
581 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
582 struct lttng_live_session
*session
;
583 enum session_not_found_action sess_not_found_act
=
584 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
586 BT_COMP_LOGD("Update data and metadata of all sessions: "
587 "live-msg-iter-addr=%p",
588 lttng_live_msg_iter
);
590 * In a remotely distant future, we could add a "new
591 * session" flag to the protocol, which would tell us that we
592 * need to query for new sessions even though we have sessions
595 if (lttng_live_msg_iter
->sessions
->len
== 0) {
596 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
598 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
599 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
603 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
605 * Retry to create a viewer session for the requested
608 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
609 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
610 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
611 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
612 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
613 "Error creating LTTng live viewer session");
614 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
615 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
624 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
626 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
627 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
629 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
630 case LTTNG_LIVE_ITERATOR_STATUS_END
:
632 * A session returned `_END`. Other sessions may still
633 * be active so we override the status and continue
640 if (!session
->closed
) {
641 nr_sessions_opened
++;
645 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
646 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
648 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
655 static enum lttng_live_iterator_status
656 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
657 struct lttng_live_stream_iterator
*stream_iter
, const bt_message
**message
,
660 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
661 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
662 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
663 bt_message
*msg
= NULL
;
665 BT_ASSERT(stream_iter
->trace
->clock_class
);
667 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
668 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
669 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
, timestamp
);
671 msg
= bt_message_message_iterator_inactivity_create(lttng_live_msg_iter
->self_msg_iter
,
672 stream_iter
->trace
->clock_class
, timestamp
);
674 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error emitting message iterator inactivity message");
683 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
684 bt_message_put_ref(msg
);
688 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
689 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
690 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
692 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
694 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
695 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
699 * Check if we already sent an inactivty message downstream for this
700 * `current_inactivity_ts` value.
702 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
703 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
704 lttng_live_stream_iterator_set_state(lttng_live_stream
,
705 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
707 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
711 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
712 lttng_live_stream
->current_inactivity_ts
);
714 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
715 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
720 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
721 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
723 const bt_clock_snapshot
*clock_snapshot
= NULL
;
725 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
726 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
729 BT_ASSERT_DBG(ts_ns
);
731 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
732 "last-msg-ts=%" PRId64
,
733 lttng_live_msg_iter
, msg
, last_msg_ts_ns
);
735 switch (bt_message_get_type(msg
)) {
736 case BT_MESSAGE_TYPE_EVENT
:
737 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
739 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
740 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
742 case BT_MESSAGE_TYPE_PACKET_END
:
743 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
745 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
747 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
749 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
751 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
753 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
754 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
757 /* All the other messages have a higher priority */
758 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
759 *ts_ns
= last_msg_ts_ns
;
763 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
765 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
766 "Cannot get nanoseconds from Epoch of clock snapshot: "
767 "clock-snapshot-addr=%p",
779 BT_COMP_LOGD("Found message's timestamp: "
780 "iter-data-addr=%p, msg-addr=%p, "
781 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
782 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
788 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
789 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
790 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
792 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
793 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
794 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
795 enum ctf_msg_iter_status status
;
796 uint64_t session_idx
, trace_idx
;
798 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
799 struct lttng_live_session
*session
=
800 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
802 if (session
->new_streams_needed
) {
803 BT_COMP_LOGD("Need an update for streams: "
804 "session-id=%" PRIu64
,
806 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
809 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
810 struct lttng_live_trace
*trace
=
811 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
812 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
813 BT_COMP_LOGD("Need an update for metadata stream: "
814 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
815 session
->id
, trace
->id
);
816 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
822 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
823 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
824 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
825 "Invalid state of live stream iterator"
826 "stream-iter-status=%s",
827 lttng_live_stream_state_string(lttng_live_stream
->state
));
831 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
, message
);
833 case CTF_MSG_ITER_STATUS_EOF
:
834 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
836 case CTF_MSG_ITER_STATUS_OK
:
837 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
839 case CTF_MSG_ITER_STATUS_AGAIN
:
841 * Continue immediately (end of packet). The next
842 * get_index may return AGAIN to delay the following
845 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
847 case CTF_MSG_ITER_STATUS_ERROR
:
849 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
850 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
851 "CTF message iterator failed to get next message: "
852 "msg-iter=%p, msg-iter-status=%s",
853 lttng_live_stream
->msg_iter
, ctf_msg_iter_status_string(status
));
861 static enum lttng_live_iterator_status
862 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
863 struct lttng_live_stream_iterator
*stream_iter
,
864 const bt_message
**curr_msg
)
866 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
867 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
868 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
870 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
871 "viewer-stream-id=%" PRIu64
,
872 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
875 * The viewer has hung up on us so we are closing the stream. The
876 * `ctf_msg_iter` should simply realize that it needs to close the
877 * stream properly by emitting the necessary stream end message.
879 enum ctf_msg_iter_status status
=
880 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
, curr_msg
);
882 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
883 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
884 "Error getting the next message from CTF message iterator");
885 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
887 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
888 BT_COMP_LOGI("Reached the end of the live stream iterator.");
889 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
893 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
901 * handle_no_data_streams()
903 * - for each ACTIVE_NO_DATA stream:
904 * - query relayd for stream data, or quiescence info.
905 * - if need metadata, get metadata, goto retry.
906 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
907 * - if quiescent, move to QUIESCENT streams
908 * - if fetched data, move to ACTIVE_DATA streams
909 * (at this point each stream either has data, or is quiescent)
913 * handle_new_streams_and_metadata()
914 * - query relayd for known streams, add them as ACTIVE_NO_DATA
915 * - query relayd for metadata
917 * call handle_active_no_data_streams()
919 * handle_quiescent_streams()
920 * - if at least one stream is ACTIVE_DATA:
921 * - peek stream event with lowest timestamp -> next_ts
922 * - for each quiescent stream
923 * - if next_ts >= quiescent end
924 * - set state to ACTIVE_NO_DATA
926 * - for each quiescent stream
927 * - set state to ACTIVE_NO_DATA
929 * call handle_active_no_data_streams()
931 * handle_active_data_streams()
932 * - if at least one stream is ACTIVE_DATA:
933 * - get stream event with lowest timestamp from heap
934 * - make that stream event the current message.
935 * - move this stream heap position to its next event
936 * - if we need to fetch data from relayd, move
937 * stream to ACTIVE_NO_DATA.
941 * end criterion: ctrl-c on client. If relayd exits or the session
942 * closes on the relay daemon side, we keep on waiting for streams.
943 * Eventually handle --end timestamp (also an end criterion).
945 * When disconnected from relayd: try to re-connect endlessly.
947 static enum lttng_live_iterator_status
948 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
949 struct lttng_live_stream_iterator
*stream_iter
,
950 const bt_message
**curr_msg
)
952 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
953 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
954 enum lttng_live_iterator_status live_status
;
956 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
957 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
958 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
960 if (stream_iter
->has_stream_hung_up
) {
962 * The stream has hung up and the stream was properly closed
963 * during the last call to the current function. Return _END
964 * status now so that this stream iterator is removed for the
965 * stream iterator list.
967 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
972 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
975 * Make sure we have the most recent metadata and possibly some new
978 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
979 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
984 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
985 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
986 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
988 * We overwrite `live_status` since `curr_msg` is
989 * likely set to a valid message in this function.
992 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
997 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
998 stream_iter
, curr_msg
);
999 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1000 BT_ASSERT(!*curr_msg
);
1006 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
1007 stream_iter
, curr_msg
);
1008 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1009 BT_ASSERT(!*curr_msg
);
1013 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1014 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1018 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, "
1019 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1020 lttng_live_iterator_status_string(live_status
), stream_iter
->name
->str
,
1021 stream_iter
->viewer_stream_id
);
1026 static bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1028 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1030 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1031 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1034 static enum lttng_live_iterator_status
1035 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1036 const bt_message
*msg_in
, bt_message
**msg_out
,
1037 uint64_t new_begin_ts
)
1039 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1040 enum bt_property_availability availability
;
1041 const bt_clock_snapshot
*clock_snapshot
;
1045 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1046 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1048 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1049 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1051 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1052 iter
, stream
, new_begin_ts
, end_ts
);
1054 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1058 bt_message_discarded_packets_set_count(*msg_out
, count
);
1063 static enum lttng_live_iterator_status
1064 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1065 const bt_message
*msg_in
, bt_message
**msg_out
,
1066 uint64_t new_begin_ts
)
1068 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1069 enum bt_property_availability availability
;
1070 const bt_clock_snapshot
*clock_snapshot
;
1074 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1075 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1077 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1078 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1080 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1081 iter
, stream
, new_begin_ts
, end_ts
);
1083 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1087 bt_message_discarded_events_set_count(*msg_out
, count
);
1092 static enum lttng_live_iterator_status
1093 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1094 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
1095 const bt_message
*late_msg
)
1097 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1098 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1099 const bt_clock_class
*clock_class
;
1100 const bt_stream_class
*stream_class
;
1101 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1102 int64_t last_inactivity_ts_ns
;
1103 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1104 enum lttng_live_iterator_status adjust_status
;
1105 bt_message
*adjusted_message
;
1108 * The timestamp of the current message is before the last message sent
1109 * by this component. We CANNOT send it as is.
1111 * The only expected scenario in which that could happen is the
1112 * following, everything else is a bug in this component, relay daemon,
1115 * Expected scenario: The CTF message iterator emitted discarded
1116 * packets and discarded events with synthesized beginning and end
1117 * timestamps from the bounds of the last known packet and the newly
1118 * decoded packet header. The CTF message iterator is not aware of
1119 * stream inactivity beacons. Hence, we have to adjust the beginning
1120 * timestamp of those types of messages if a stream signalled its
1121 * inactivity up until _after_ the last known packet's beginning
1124 * Otherwise, the monotonicity guarantee of message timestamps would
1127 * In short, the only scenario in which it's okay and fixable to
1128 * received a late message is when:
1129 * 1. the late message is a discarded packets or discarded events
1131 * 2. this stream produced an inactivity message downstream, and
1132 * 3. the timestamp of the late message is within the inactivity
1133 * timespan we sent downstream through the inactivity message.
1136 BT_COMP_LOGD("Handling late message on live stream iterator: "
1137 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1138 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1140 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1141 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1142 "have a late message when no inactivity message "
1143 "was ever sent for that stream.");
1144 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1148 if (!is_discarded_packet_or_event_message(late_msg
)) {
1149 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1150 "Invalid live stream state: "
1151 "have a late message that is not a packet discarded or "
1152 "event discarded message: late-msg-type=%s",
1153 bt_common_message_type_string(bt_message_get_type(late_msg
)));
1154 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1158 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1159 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1161 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1162 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1163 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1164 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error converting last "
1165 "inactivity message timestamp to nanoseconds");
1166 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1170 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1171 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1172 "Invalid live stream state: "
1173 "have a late message that is none included in a stream "
1174 "inactivity timespan: last-inactivity-ts-ns=%" PRIu64
1175 "late-msg-ts-ns=%" PRIu64
,
1176 last_inactivity_ts_ns
, late_msg_ts_ns
);
1177 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1182 * We now know that it's okay for this message to be late, we can now
1183 * adjust its timestamp to ensure monotonicity.
1185 BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, "
1186 "msg-new-ts-ns=%" PRIu64
,
1187 bt_common_message_type_string(bt_message_get_type(late_msg
)),
1188 stream_iter
->last_inactivity_ts
.value
);
1189 switch (bt_message_get_type(late_msg
)) {
1190 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1191 adjust_status
= adjust_discarded_events_message(
1192 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1193 stream_iter
->last_inactivity_ts
.value
);
1195 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1196 adjust_status
= adjust_discarded_packets_message(
1197 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1198 stream_iter
->last_inactivity_ts
.value
);
1204 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1205 stream_iter_status
= adjust_status
;
1209 BT_ASSERT_DBG(adjusted_message
);
1210 stream_iter
->current_msg
= adjusted_message
;
1211 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1212 bt_message_put_ref(late_msg
);
1215 return stream_iter_status
;
1218 static enum lttng_live_iterator_status
1219 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1220 struct lttng_live_trace
*live_trace
,
1221 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1223 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1224 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1225 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1226 enum lttng_live_iterator_status stream_iter_status
;
1228 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1229 uint64_t stream_iter_idx
;
1231 BT_ASSERT_DBG(live_trace
);
1232 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1234 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1235 "trace-id=%" PRIu64
,
1238 * Update the current message of every stream iterators of this trace.
1239 * The current msg of every stream must have a timestamp equal or
1240 * larger than the last message returned by this iterator. We must
1241 * ensure monotonicity.
1243 stream_iter_idx
= 0;
1244 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1245 bool stream_iter_is_ended
= false;
1246 struct lttng_live_stream_iterator
*stream_iter
=
1247 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1251 * If there is no current message for this stream, go fetch
1254 while (!stream_iter
->current_msg
) {
1255 const bt_message
*msg
= NULL
;
1256 int64_t curr_msg_ts_ns
= INT64_MAX
;
1258 stream_iter_status
=
1259 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, &msg
);
1261 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1262 stream_iter_is_ended
= true;
1266 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1272 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, "
1273 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1274 bt_common_message_type_string(bt_message_get_type(msg
)),
1275 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1278 * Get the timestamp in nanoseconds from origin of this
1281 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1285 * Check if the message of the current live stream
1286 * iterator occurred at the exact same time or after the
1287 * last message returned by this component's message
1288 * iterator. If not, we need to handle it with care.
1290 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1291 stream_iter
->current_msg
= msg
;
1292 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1295 * We received a message from the past. This
1296 * may be fixable but it can also be an error.
1298 stream_iter_status
=
1299 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, msg
);
1300 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1301 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1302 "Late message could not be handled correctly: "
1303 "lttng-live-msg-iter-addr=%p, "
1304 "stream-name=\"%s\", "
1305 "curr-msg-ts=%" PRId64
", last-msg-ts=%" PRId64
,
1306 lttng_live_msg_iter
, stream_iter
->name
->str
,
1307 curr_msg_ts_ns
, lttng_live_msg_iter
->last_msg_ts_ns
);
1308 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1314 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1316 if (!stream_iter_is_ended
) {
1317 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1318 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1320 * Update the current best candidate message
1321 * for the stream iterator of this live trace
1322 * to be forwarded downstream.
1324 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1325 youngest_candidate_stream_iter
= stream_iter
;
1326 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1328 * Order the messages in an arbitrary but
1329 * deterministic way.
1331 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1332 int ret
= common_muxing_compare_messages(
1333 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1336 * The `youngest_candidate_stream_iter->current_msg`
1337 * should go first. Update the next
1338 * iterator and the current timestamp.
1340 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1341 youngest_candidate_stream_iter
= stream_iter
;
1342 } else if (ret
== 0) {
1344 * Unable to pick which one should go
1348 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1349 "stream-iter-addr=%p"
1350 "stream-iter-addr=%p",
1351 stream_iter
, youngest_candidate_stream_iter
);
1358 * The live stream iterator has ended. That
1359 * iterator is removed from the array, but
1360 * there is no need to increment
1361 * stream_iter_idx as
1362 * g_ptr_array_remove_index_fast replaces the
1363 * removed element with the array's last
1366 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
, stream_iter_idx
);
1370 if (youngest_candidate_stream_iter
) {
1371 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1372 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1375 * The only case where we don't have a candidate for this trace
1376 * is if we reached the end of all the iterators.
1378 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1379 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1383 return stream_iter_status
;
1386 static enum lttng_live_iterator_status
1387 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1388 struct lttng_live_session
*session
,
1389 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1391 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1392 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1393 enum lttng_live_iterator_status stream_iter_status
;
1394 uint64_t trace_idx
= 0;
1395 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1396 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1398 BT_COMP_LOGD("Finding the next stream iterator for session: "
1399 "session-id=%" PRIu64
,
1402 * Make sure we are attached to the session and look for new streams
1405 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1406 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1407 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1408 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1412 BT_ASSERT_DBG(session
->traces
);
1414 while (trace_idx
< session
->traces
->len
) {
1415 bool trace_is_ended
= false;
1416 struct lttng_live_stream_iterator
*stream_iter
;
1417 struct lttng_live_trace
*trace
=
1418 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1420 stream_iter_status
=
1421 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1422 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1424 * All the live stream iterators for this trace are
1425 * ENDed. Remove the trace from this session.
1427 trace_is_ended
= true;
1428 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1432 if (!trace_is_ended
) {
1433 BT_ASSERT_DBG(stream_iter
);
1435 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1436 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1437 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1438 youngest_candidate_stream_iter
= stream_iter
;
1439 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1441 * Order the messages in an arbitrary but
1442 * deterministic way.
1444 int ret
= common_muxing_compare_messages(
1445 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1448 * The `youngest_candidate_stream_iter->current_msg`
1449 * should go first. Update the next iterator
1450 * and the current timestamp.
1452 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1453 youngest_candidate_stream_iter
= stream_iter
;
1454 } else if (ret
== 0) {
1455 /* Unable to pick which one should go first. */
1457 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1458 "stream-iter-addr=%p"
1459 "stream-iter-addr=%p",
1460 stream_iter
, youngest_candidate_stream_iter
);
1466 * trace_idx is not incremented since
1467 * g_ptr_array_remove_index_fast replaces the
1468 * element at trace_idx with the array's last element.
1470 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1473 if (youngest_candidate_stream_iter
) {
1474 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1475 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1478 * The only cases where we don't have a candidate for this
1480 * 1. if we reached the end of all the iterators of all the
1481 * traces of this session,
1482 * 2. if we never had live stream iterator in the first place.
1484 * In either cases, we return END.
1486 BT_ASSERT(session
->traces
->len
== 0);
1487 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1490 return stream_iter_status
;
1493 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1497 for (i
= 0; i
< count
; i
++) {
1498 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1502 bt_message_iterator_class_next_method_status
1503 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1504 uint64_t capacity
, uint64_t *count
)
1506 bt_message_iterator_class_next_method_status status
;
1507 enum lttng_live_viewer_status viewer_status
;
1508 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1509 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1510 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1511 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1512 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1513 enum lttng_live_iterator_status stream_iter_status
;
1514 uint64_t session_idx
;
1518 BT_ASSERT_DBG(lttng_live_msg_iter
);
1520 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1522 * The iterator was interrupted in a previous call to the
1523 * `_next()` method. We currently do not support generating
1524 * messages after such event. The babeltrace2 CLI should never
1525 * be running the graph after being interrupted. So this check
1526 * is to prevent other graph users from using this live
1527 * iterator in an messed up internal state.
1529 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1530 BT_COMP_LOGE_APPEND_CAUSE(
1532 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1537 * Clear all the invalid message reference that might be left over in
1540 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1543 * If no session are exposed on the relay found at the url provided by
1544 * the user, session count will be 0. In this case, we return status
1545 * end to return gracefully.
1547 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1548 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1549 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1553 * The are no more active session for this session
1554 * name. Retry to create a viewer session for the
1555 * requested session name.
1557 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1558 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1559 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1560 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1561 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1562 "Error creating LTTng live viewer session");
1563 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1564 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1573 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1574 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1578 * Here the muxing of message is done.
1580 * We need to iterate over all the streams of all the traces of all the
1581 * viewer sessions in order to get the message with the smallest
1582 * timestamp. In this case, a session is a viewer session and there is
1583 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1584 * kernel). Each viewer session can have multiple traces, for example,
1585 * 64bit UST viewer sessions could have multiple per-pid traces.
1587 * We iterate over the streams of each traces to update and see what is
1588 * their next message's timestamp. From those timestamps, we select the
1589 * message with the smallest timestamp as the best candidate message
1590 * for that trace and do the same thing across all the sessions.
1592 * We then compare the timestamp of best candidate message of all the
1593 * sessions to pick the message with the smallest timestamp and we
1596 while (*count
< capacity
) {
1597 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1598 *candidate_stream_iter
= NULL
;
1599 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1601 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1603 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1604 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1605 lttng_live_msg_iter
->sessions
, session_idx
);
1607 /* Find the best candidate message to send downstream. */
1608 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1609 &candidate_stream_iter
);
1611 /* If we receive an END status, it means that either:
1612 * - Those traces never had active streams (UST with no
1613 * data produced yet),
1614 * - All live stream iterators have ENDed.*/
1615 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1616 if (session
->closed
&& session
->traces
->len
== 0) {
1618 * Remove the session from the list.
1619 * session_idx is not modified since
1620 * g_ptr_array_remove_index_fast
1621 * replaces the the removed element with
1622 * the array's last element.
1624 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1631 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1635 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1636 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1637 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1638 youngest_stream_iter
= candidate_stream_iter
;
1639 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1641 * The currently selected message to be sent
1642 * downstream next has the exact same timestamp
1643 * that of the current candidate message. We
1644 * must break the tie in a predictable manner.
1647 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1649 * Order the messages in an arbitrary but
1650 * deterministic way.
1652 int ret
= common_muxing_compare_messages(candidate_stream_iter
->current_msg
,
1653 youngest_stream_iter
->current_msg
);
1656 * The `candidate_stream_iter->current_msg`
1657 * should go first. Update the next
1658 * iterator and the current timestamp.
1660 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1661 youngest_stream_iter
= candidate_stream_iter
;
1662 } else if (ret
== 0) {
1663 /* Unable to pick which one should go first. */
1665 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1666 "next-stream-iter-addr=%p"
1667 "candidate-stream-iter-addr=%p",
1668 youngest_stream_iter
, candidate_stream_iter
);
1675 if (!youngest_stream_iter
) {
1676 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1680 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1681 /* Ensure monotonicity. */
1682 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1683 youngest_stream_iter
->current_msg_ts_ns
);
1686 * Insert the next message to the message batch. This will set
1687 * stream iterator current message to NULL so that next time
1688 * we fetch the next message of that stream iterator
1690 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1693 /* Update the last timestamp in nanoseconds sent downstream. */
1694 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1695 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1697 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1701 switch (stream_iter_status
) {
1702 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1703 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1705 * If we gathered messages, return _OK even if the graph was
1706 * interrupted. This allows for the components downstream to at
1707 * least get the those messages. If the graph was indeed
1708 * interrupted there should not be another _next() call as the
1709 * application will tear down the graph. This component class
1710 * doesn't support restarting after an interruption.
1713 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1715 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1718 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1719 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1721 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1722 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1723 "Memory error preparing the next batch of messages: "
1724 "live-iter-status=%s",
1725 lttng_live_iterator_status_string(stream_iter_status
));
1726 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1728 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1729 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1730 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1731 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1732 "Error preparing the next batch of messages: "
1733 "live-iter-status=%s",
1734 lttng_live_iterator_status_string(stream_iter_status
));
1736 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1737 /* Put all existing messages on error. */
1738 put_messages(msgs
, *count
);
1748 static struct lttng_live_msg_iter
*
1749 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1750 bt_self_message_iterator
*self_msg_it
)
1752 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1753 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1755 struct lttng_live_msg_iter
*lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1756 if (!lttng_live_msg_iter
) {
1757 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate lttng_live_msg_iter");
1761 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1762 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1763 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1764 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1766 lttng_live_msg_iter
->active_stream_iter
= 0;
1767 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1768 lttng_live_msg_iter
->was_interrupted
= false;
1770 lttng_live_msg_iter
->sessions
=
1771 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1772 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1775 return lttng_live_msg_iter
;
1778 bt_message_iterator_class_initialize_method_status
1779 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1780 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1782 bt_message_iterator_class_initialize_method_status status
;
1783 struct lttng_live_component
*lttng_live
;
1784 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1785 enum lttng_live_viewer_status viewer_status
;
1786 bt_logging_level log_level
;
1787 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1789 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1790 log_level
= lttng_live
->log_level
;
1791 self_comp
= lttng_live
->self_comp
;
1793 /* There can be only one downstream iterator at the same time. */
1794 BT_ASSERT(!lttng_live
->has_msg_iter
);
1795 lttng_live
->has_msg_iter
= true;
1797 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1798 if (!lttng_live_msg_iter
) {
1799 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create lttng_live_msg_iter");
1800 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1804 viewer_status
= live_viewer_connection_create(
1805 self_comp
, NULL
, log_level
, lttng_live
->params
.url
->str
, false, lttng_live_msg_iter
,
1806 <tng_live_msg_iter
->viewer_connection
);
1807 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1808 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1809 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer connection");
1810 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1812 * Interruption in the _iter_init() method is not
1813 * supported. Return an error.
1815 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted while creating viewer connection");
1818 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1822 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1823 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1824 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1825 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer session");
1826 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1828 * Interruption in the _iter_init() method is not
1829 * supported. Return an error.
1831 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted when creating viewer session");
1834 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1838 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1839 switch (lttng_live
->params
.sess_not_found_act
) {
1840 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1842 "Unable to connect to the requested live viewer session. Keep trying to connect because of "
1843 "%s=\"%s\" component parameter: url=\"%s\"",
1844 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1845 lttng_live
->params
.url
->str
);
1847 case SESSION_NOT_FOUND_ACTION_FAIL
:
1848 BT_COMP_LOGE_APPEND_CAUSE(
1850 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1851 "component parameter: url =\"%s\"",
1852 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1853 lttng_live
->params
.url
->str
);
1854 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1856 case SESSION_NOT_FOUND_ACTION_END
:
1858 "Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1859 "call because of %s=\"%s\" component parameter: "
1861 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1862 lttng_live
->params
.url
->str
);
1869 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1870 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1874 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1879 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1880 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1881 bt_param_validation_value_descr::makeString()},
1882 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1884 static bt_component_class_query_method_status
1885 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1886 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1888 bt_component_class_query_method_status status
;
1889 const bt_value
*url_value
= NULL
;
1891 struct live_viewer_connection
*viewer_connection
= NULL
;
1892 enum lttng_live_viewer_status viewer_status
;
1893 enum bt_param_validation_status validation_status
;
1894 gchar
*validate_error
= NULL
;
1896 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1897 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1898 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1900 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1901 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1902 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1906 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1907 url
= bt_value_string_get(url_value
);
1909 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
, log_level
, url
, true, NULL
,
1910 &viewer_connection
);
1911 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1912 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1913 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to create viewer connection");
1914 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1915 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1916 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1923 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1924 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1925 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to list viewer sessions");
1932 BT_VALUE_PUT_REF_AND_RESET(*result
);
1935 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1939 if (viewer_connection
) {
1940 live_viewer_connection_destroy(viewer_connection
);
1943 g_free(validate_error
);
1948 static bt_component_class_query_method_status
1949 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1950 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1952 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1953 const bt_value
*input_type_value
;
1954 const bt_value
*input_value
;
1956 struct bt_common_lttng_live_url_parts parts
= {};
1958 /* Used by the logging macros */
1959 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1962 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1963 if (!input_type_value
) {
1964 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `type` parameter.");
1968 if (!bt_value_is_string(input_type_value
)) {
1969 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "`type` parameter is not a string value.");
1973 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1974 /* We don't handle file system paths */
1978 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1980 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `input` parameter.");
1984 if (!bt_value_is_string(input_value
)) {
1985 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1986 "`input` parameter is not a string value.");
1990 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1991 if (parts
.session_name
) {
1993 * Looks pretty much like an LTTng live URL: we got the
1994 * session name part, which forms a complete URL.
2000 *result
= bt_value_real_create_init(weight
);
2002 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2010 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2013 BT_ASSERT(!*result
);
2016 bt_common_destroy_lttng_live_url_parts(&parts
);
2020 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
2021 bt_private_query_executor
*priv_query_exec
,
2022 const char *object
, const bt_value
*params
,
2023 __attribute__((unused
)) void *method_data
,
2024 const bt_value
**result
)
2026 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2027 bt_self_component
*self_comp
= NULL
;
2028 bt_self_component_class
*self_comp_class
=
2029 bt_self_component_class_source_as_self_component_class(comp_class
);
2030 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2031 bt_private_query_executor_as_query_executor_const(priv_query_exec
));
2033 if (strcmp(object
, "sessions") == 0) {
2034 status
= lttng_live_query_list_sessions(params
, result
, self_comp_class
, log_level
);
2035 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2036 status
= lttng_live_query_support_info(params
, result
, self_comp_class
, log_level
);
2038 BT_COMP_LOGI("Unknown query object `%s`", object
);
2039 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2047 static void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2052 if (lttng_live
->params
.url
) {
2053 g_string_free(lttng_live
->params
.url
, TRUE
);
2058 void lttng_live_component_finalize(bt_self_component_source
*component
)
2060 lttng_live_component
*data
= (lttng_live_component
*) bt_self_component_get_data(
2061 bt_self_component_source_as_self_component(component
));
2066 lttng_live_component_destroy_data(data
);
2069 static enum session_not_found_action
2070 parse_session_not_found_action_param(const bt_value
*no_session_param
)
2072 enum session_not_found_action action
;
2073 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2075 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2076 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2077 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2078 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2080 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2081 action
= SESSION_NOT_FOUND_ACTION_END
;
2087 static bt_param_validation_value_descr inputs_elem_descr
=
2088 bt_param_validation_value_descr::makeString();
2090 static const char *sess_not_found_action_choices
[] = {
2091 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2092 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2093 SESS_NOT_FOUND_ACTION_END_STR
,
2096 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2097 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
2098 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
2099 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
2100 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
2101 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
2103 static bt_component_class_initialize_method_status
2104 lttng_live_component_create(const bt_value
*params
, bt_logging_level log_level
,
2105 bt_self_component
*self_comp
, struct lttng_live_component
**component
)
2107 struct lttng_live_component
*lttng_live
= NULL
;
2108 const bt_value
*inputs_value
;
2109 const bt_value
*url_value
;
2110 const bt_value
*value
;
2112 enum bt_param_validation_status validation_status
;
2113 gchar
*validation_error
= NULL
;
2114 bt_component_class_initialize_method_status status
;
2116 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
2117 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2118 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2120 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2121 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2122 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2126 lttng_live
= g_new0(struct lttng_live_component
, 1);
2128 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2131 lttng_live
->log_level
= log_level
;
2132 lttng_live
->self_comp
= self_comp
;
2133 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2134 lttng_live
->has_msg_iter
= false;
2136 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2137 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2138 url
= bt_value_string_get(url_value
);
2140 lttng_live
->params
.url
= g_string_new(url
);
2141 if (!lttng_live
->params
.url
) {
2142 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2146 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
2148 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
2150 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2151 "defaulting to `%s`.",
2152 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2153 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2156 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2160 lttng_live_component_destroy_data(lttng_live
);
2163 g_free(validation_error
);
2165 *component
= lttng_live
;
2169 bt_component_class_initialize_method_status
2170 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2171 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
2173 struct lttng_live_component
*lttng_live
;
2174 bt_component_class_initialize_method_status ret
;
2175 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2176 bt_logging_level log_level
=
2177 bt_component_get_logging_level(bt_self_component_as_component(self_comp
));
2178 bt_self_component_add_port_status add_port_status
;
2180 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2181 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2185 add_port_status
= bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2186 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2187 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2191 bt_self_component_set_data(self_comp
, lttng_live
);
2195 lttng_live_component_destroy_data(lttng_live
);