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.h"
32 #include "lttng-live.h"
34 #define MAX_QUERY_SIZE (256*1024)
35 #define URL_PARAM "url"
36 #define INPUTS_PARAM "inputs"
37 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
38 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
39 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
40 #define SESS_NOT_FOUND_ACTION_END_STR "end"
42 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
45 const char *lttng_live_iterator_status_string(
46 enum lttng_live_iterator_status status
)
49 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
50 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
51 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
52 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
53 case LTTNG_LIVE_ITERATOR_STATUS_END
:
54 return "LTTNG_LIVE_ITERATOR_STATUS_END";
55 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
56 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
57 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
58 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
59 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
60 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
61 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
62 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
63 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
64 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
71 const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
74 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
75 return "ACTIVE_NO_DATA";
76 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
77 return "QUIESCENT_NO_DATA";
78 case LTTNG_LIVE_STREAM_QUIESCENT
:
80 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
82 case LTTNG_LIVE_STREAM_EOF
:
89 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
90 enum lttng_live_stream_state new_state
)
92 bt_self_component
*self_comp
= stream_iter
->self_comp
;
93 bt_logging_level log_level
= stream_iter
->log_level
;
95 BT_COMP_LOGD("Setting live stream iterator state: viewer-stream-id=%" PRIu64
96 ", old-state=%s, new-state=%s",
97 stream_iter
->viewer_stream_id
,
98 lttng_live_stream_state_string(stream_iter
->state
),
99 lttng_live_stream_state_string(new_state
));
101 stream_iter
->state
= new_state
;
104 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
106 BT_COMP_LOGD("Live stream iterator state=%s, last-inact-ts=%" PRId64 \
107 ", curr-inact-ts %" PRId64, \
108 lttng_live_stream_state_string(live_stream_iter->state), \
109 live_stream_iter->last_inactivity_ts, \
110 live_stream_iter->current_inactivity_ts); \
114 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
123 ret
= bt_self_message_iterator_is_interrupted(
124 msg_iter
->self_msg_iter
);
131 struct lttng_live_trace
*lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
,
135 struct lttng_live_trace
*ret_trace
= NULL
;
137 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
138 struct lttng_live_trace
*trace
=
139 g_ptr_array_index(session
->traces
, trace_idx
);
140 if (trace
->id
== trace_id
) {
151 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
153 bt_logging_level log_level
= trace
->log_level
;
154 bt_self_component
*self_comp
= trace
->self_comp
;
156 BT_COMP_LOGD("Destroying live trace: trace-id=%"PRIu64
, trace
->id
);
158 BT_ASSERT(trace
->stream_iterators
);
159 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
161 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
162 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
164 lttng_live_metadata_fini(trace
);
169 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
172 struct lttng_live_trace
*trace
= NULL
;
173 bt_logging_level log_level
= session
->log_level
;
174 bt_self_component
*self_comp
= session
->self_comp
;
176 BT_COMP_LOGD("Creating live trace: "
177 "session-id=%"PRIu64
", trace-id=%"PRIu64
,
178 session
->id
, trace_id
);
179 trace
= g_new0(struct lttng_live_trace
, 1);
181 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
182 "Failed to allocate live trace");
185 trace
->log_level
= session
->log_level
;
186 trace
->self_comp
= session
->self_comp
;
187 trace
->session
= session
;
188 trace
->id
= trace_id
;
189 trace
->trace_class
= NULL
;
191 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
192 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
193 BT_ASSERT(trace
->stream_iterators
);
194 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
195 g_ptr_array_add(session
->traces
, trace
);
206 struct lttng_live_trace
*lttng_live_session_borrow_or_create_trace_by_id(
207 struct lttng_live_session
*session
, uint64_t trace_id
)
209 struct lttng_live_trace
*trace
;
211 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
216 /* The session is the owner of the newly created trace. */
217 trace
= lttng_live_create_trace(session
, trace_id
);
224 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
225 uint64_t session_id
, const char *hostname
,
226 const char *session_name
)
229 struct lttng_live_session
*session
;
230 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
231 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
233 BT_COMP_LOGD("Adding live session: "
234 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
235 session_id
, hostname
, session_name
);
237 session
= g_new0(struct lttng_live_session
, 1);
239 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
240 "Failed to allocate live session");
244 session
->log_level
= lttng_live_msg_iter
->log_level
;
245 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
246 session
->id
= session_id
;
247 session
->traces
= g_ptr_array_new_with_free_func(
248 (GDestroyNotify
) lttng_live_destroy_trace
);
249 BT_ASSERT(session
->traces
);
250 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
251 session
->new_streams_needed
= true;
252 session
->hostname
= g_string_new(hostname
);
253 BT_ASSERT(session
->hostname
);
255 session
->session_name
= g_string_new(session_name
);
256 BT_ASSERT(session
->session_name
);
258 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
268 void lttng_live_destroy_session(struct lttng_live_session
*session
)
270 bt_logging_level log_level
;
271 bt_self_component
*self_comp
;
277 log_level
= session
->log_level
;
278 self_comp
= session
->self_comp
;
279 BT_COMP_LOGD("Destroying live session: "
280 "session-id=%"PRIu64
", session-name=\"%s\"",
281 session
->id
, session
->session_name
->str
);
282 if (session
->id
!= -1ULL) {
283 if (lttng_live_session_detach(session
)) {
284 if (!lttng_live_graph_is_canceled(
285 session
->lttng_live_msg_iter
)) {
286 /* Old relayd cannot detach sessions. */
287 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
294 if (session
->traces
) {
295 g_ptr_array_free(session
->traces
, TRUE
);
298 if (session
->hostname
) {
299 g_string_free(session
->hostname
, TRUE
);
301 if (session
->session_name
) {
302 g_string_free(session
->session_name
, TRUE
);
311 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
313 if (!lttng_live_msg_iter
) {
317 if (lttng_live_msg_iter
->sessions
) {
318 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
321 if (lttng_live_msg_iter
->viewer_connection
) {
322 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
324 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
325 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
327 /* All stream iterators must be destroyed at this point. */
328 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
329 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
331 g_free(lttng_live_msg_iter
);
338 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
340 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
342 BT_ASSERT(self_msg_iter
);
344 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
345 BT_ASSERT(lttng_live_msg_iter
);
346 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
350 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
351 struct lttng_live_stream_iterator
*lttng_live_stream
)
353 bt_logging_level log_level
= lttng_live_stream
->log_level
;
354 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
356 switch (lttng_live_stream
->state
) {
357 case LTTNG_LIVE_STREAM_QUIESCENT
:
358 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
360 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
362 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
364 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
366 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
368 case LTTNG_LIVE_STREAM_EOF
:
371 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
375 * For active no data stream, fetch next index. As a result of that it can
377 * - quiescent: won't have events for a bit,
378 * - have data: need to get that data and produce the event,
379 * - have no data on this stream at this point: need to retry (AGAIN) or return
383 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
384 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
385 struct lttng_live_stream_iterator
*lttng_live_stream
)
387 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
388 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
389 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
390 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
391 struct packet_index index
;
393 if (lttng_live_stream
->trace
->metadata_stream_state
==
394 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
395 BT_COMP_LOGD("Need to get an update for the metadata stream before proceeding further with this stream: "
396 "stream-name=\"%s\"", lttng_live_stream
->name
->str
);
397 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
401 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
402 BT_COMP_LOGD("Need to get an update of all streams before proceeding further with this stream: "
403 "stream-name=\"%s\"", lttng_live_stream
->name
->str
);
404 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
408 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
409 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
412 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
414 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
418 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
420 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
421 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
422 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
424 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
425 last_inact_ts
== curr_inact_ts
) {
426 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
427 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
429 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
434 lttng_live_stream
->base_offset
= index
.offset
;
435 lttng_live_stream
->offset
= index
.offset
;
436 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
438 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
439 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
440 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
441 lttng_live_stream
->name
->str
,
442 lttng_live_stream
->viewer_stream_id
,
443 lttng_live_stream
->base_offset
,
444 lttng_live_stream
->offset
, lttng_live_stream
->len
);
447 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
448 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
454 * Creation of the message requires the ctf trace class to be created
455 * beforehand, but the live protocol gives us all streams (including metadata)
456 * at once. So we split it in three steps: getting streams, getting metadata
457 * (which creates the ctf trace class), and then creating the per-stream
461 enum lttng_live_iterator_status
lttng_live_get_session(
462 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
463 struct lttng_live_session
*session
)
465 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
466 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
467 enum lttng_live_iterator_status status
;
470 if (!session
->attached
) {
471 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
,
473 enum lttng_live_viewer_status attach_status
=
474 lttng_live_session_attach(session
,
475 lttng_live_msg_iter
->self_msg_iter
);
476 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
477 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
479 * Clear any causes appended in
480 * `lttng_live_attach_session()` as we want to
481 * return gracefully since the graph was
484 bt_current_thread_clear_error();
485 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
487 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
488 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
489 "Error attaching to LTTng live session");
495 BT_COMP_LOGD("Updating all streams and metadata for session: "
496 "session-id=%" PRIu64
", session-name=\"%s\"",
497 session
->id
, session
->session_name
->str
);
499 status
= lttng_live_session_get_new_streams(session
,
500 lttng_live_msg_iter
->self_msg_iter
);
501 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
502 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
507 while (trace_idx
< session
->traces
->len
) {
508 struct lttng_live_trace
*trace
=
509 g_ptr_array_index(session
->traces
, trace_idx
);
511 status
= lttng_live_metadata_update(trace
);
513 case LTTNG_LIVE_ITERATOR_STATUS_END
:
514 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
517 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
518 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
521 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
522 "Error updating trace metadata: "
523 "stream-iter-status=%s, trace-id=%"PRIu64
,
524 lttng_live_iterator_status_string(status
),
531 * Now that we have the metadata we can initialize the downstream
534 status
= lttng_live_lazy_msg_init(session
,
535 lttng_live_msg_iter
->self_msg_iter
);
542 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
544 uint64_t session_idx
, trace_idx
;
545 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
546 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
548 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
550 struct lttng_live_session
*session
=
551 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
552 BT_COMP_LOGD("Force marking session as needing new streams: "
553 "session-id=%" PRIu64
, session
->id
);
554 session
->new_streams_needed
= true;
555 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
557 struct lttng_live_trace
*trace
=
558 g_ptr_array_index(session
->traces
, trace_idx
);
559 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
560 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
561 session
->id
, trace
->id
);
563 BT_ASSERT(trace
->metadata_stream_state
!=
564 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
566 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
572 enum lttng_live_iterator_status
573 lttng_live_iterator_handle_new_streams_and_metadata(
574 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
576 enum lttng_live_iterator_status status
;
577 enum lttng_live_viewer_status viewer_status
;
578 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
579 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
580 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
581 struct lttng_live_session
*session
;
582 enum session_not_found_action sess_not_found_act
=
583 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
585 BT_COMP_LOGD("Update data and metadata of all sessions: "
586 "live-msg-iter-addr=%p", lttng_live_msg_iter
);
588 * In a remotely distant future, we could add a "new
589 * session" flag to the protocol, which would tell us that we
590 * need to query for new sessions even though we have sessions
593 if (lttng_live_msg_iter
->sessions
->len
== 0) {
594 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
595 BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter");
596 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
599 BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
601 * Retry to create a viewer session for the requested
604 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
605 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
606 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
607 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
608 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
609 "Error creating LTTng live viewer session");
610 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
611 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
620 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
622 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
624 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
626 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
628 case LTTNG_LIVE_ITERATOR_STATUS_END
:
629 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
634 if (!session
->closed
) {
635 nr_sessions_opened
++;
639 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
640 nr_sessions_opened
== 0) {
641 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
643 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
651 enum lttng_live_iterator_status
emit_inactivity_message(
652 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
653 struct lttng_live_stream_iterator
*stream_iter
,
654 const bt_message
**message
, uint64_t timestamp
)
656 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
657 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
658 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
659 bt_message
*msg
= NULL
;
661 BT_ASSERT(stream_iter
->trace
->clock_class
);
663 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
664 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
665 stream_iter
->ctf_stream_class_id
,
666 stream_iter
->viewer_stream_id
, timestamp
);
668 msg
= bt_message_message_iterator_inactivity_create(
669 lttng_live_msg_iter
->self_msg_iter
,
670 stream_iter
->trace
->clock_class
, timestamp
);
672 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
673 "Error emitting message iterator inactivity message");
682 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
683 bt_message_put_ref(msg
);
688 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
,
691 const bt_message
**message
)
693 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
695 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
696 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
700 * Check if we already sent an inactivty message downstream for this
701 * `current_inactivity_ts` value.
703 if (lttng_live_stream
->current_inactivity_ts
==
704 lttng_live_stream
->last_inactivity_ts
) {
705 lttng_live_stream_iterator_set_state(lttng_live_stream
,
706 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
708 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
712 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
713 message
, lttng_live_stream
->current_inactivity_ts
);
715 lttng_live_stream
->last_inactivity_ts
=
716 lttng_live_stream
->current_inactivity_ts
;
722 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
723 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
724 const bt_message
*msg
, int64_t last_msg_ts_ns
,
727 const bt_clock_class
*clock_class
= NULL
;
728 const bt_clock_snapshot
*clock_snapshot
= NULL
;
730 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
731 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
734 BT_ASSERT_DBG(ts_ns
);
736 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
737 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
740 switch (bt_message_get_type(msg
)) {
741 case BT_MESSAGE_TYPE_EVENT
:
742 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
744 BT_ASSERT_DBG(clock_class
);
746 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
749 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
750 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
752 BT_ASSERT(clock_class
);
754 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
757 case BT_MESSAGE_TYPE_PACKET_END
:
758 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
760 BT_ASSERT(clock_class
);
762 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
765 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
766 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
768 BT_ASSERT(clock_class
);
770 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
773 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
774 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
776 BT_ASSERT(clock_class
);
778 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
781 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
782 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
786 /* All the other messages have a higher priority */
787 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
788 *ts_ns
= last_msg_ts_ns
;
792 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
793 BT_ASSERT_DBG(clock_class
);
795 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
797 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
798 "Cannot get nanoseconds from Epoch of clock snapshot: "
799 "clock-snapshot-addr=%p", clock_snapshot
);
810 BT_COMP_LOGD("Found message's timestamp: "
811 "iter-data-addr=%p, msg-addr=%p, "
812 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
813 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
820 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
821 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
822 struct lttng_live_stream_iterator
*lttng_live_stream
,
823 const bt_message
**message
)
825 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
826 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
827 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
828 enum ctf_msg_iter_status status
;
829 uint64_t session_idx
, trace_idx
;
831 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
833 struct lttng_live_session
*session
=
834 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
836 if (session
->new_streams_needed
) {
837 BT_COMP_LOGD("Need an update for streams: "
838 "session-id=%" PRIu64
, session
->id
);
839 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
842 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
844 struct lttng_live_trace
*trace
=
845 g_ptr_array_index(session
->traces
, trace_idx
);
846 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
847 BT_COMP_LOGD("Need an update for metadata stream: "
848 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
849 session
->id
, trace
->id
);
850 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
856 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
857 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
858 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
859 "Invalid state of live stream iterator"
860 "stream-iter-status=%s",
861 lttng_live_stream_state_string(lttng_live_stream
->state
));
865 status
= ctf_msg_iter_get_next_message(
866 lttng_live_stream
->msg_iter
, message
);
868 case CTF_MSG_ITER_STATUS_EOF
:
869 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
871 case CTF_MSG_ITER_STATUS_OK
:
872 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
874 case CTF_MSG_ITER_STATUS_AGAIN
:
876 * Continue immediately (end of packet). The next
877 * get_index may return AGAIN to delay the following
880 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
882 case CTF_MSG_ITER_STATUS_ERROR
:
884 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
885 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
886 "CTF message iterator failed to get next message: "
887 "msg-iter=%p, msg-iter-status=%s",
888 lttng_live_stream
->msg_iter
,
889 ctf_msg_iter_status_string(status
));
898 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
899 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
900 struct lttng_live_stream_iterator
*stream_iter
,
901 const bt_message
**curr_msg
)
903 enum lttng_live_iterator_status live_status
=
904 LTTNG_LIVE_ITERATOR_STATUS_OK
;
905 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
906 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
908 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
909 "viewer-stream-id=%" PRIu64
, stream_iter
->name
->str
,
910 stream_iter
->viewer_stream_id
);
913 * The viewer has hung up on us so we are closing the stream. The
914 * `ctf_msg_iter` should simply realize that it needs to close the
915 * stream properly by emitting the necessary stream end message.
917 enum ctf_msg_iter_status status
= ctf_msg_iter_get_next_message(
918 stream_iter
->msg_iter
, curr_msg
);
920 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
921 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
922 "Error getting the next message from CTF message iterator");
923 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
925 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
926 BT_COMP_LOGI("Reached the end of the live stream iterator.");
927 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
931 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
939 * handle_no_data_streams()
941 * - for each ACTIVE_NO_DATA stream:
942 * - query relayd for stream data, or quiescence info.
943 * - if need metadata, get metadata, goto retry.
944 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
945 * - if quiescent, move to QUIESCENT streams
946 * - if fetched data, move to ACTIVE_DATA streams
947 * (at this point each stream either has data, or is quiescent)
951 * handle_new_streams_and_metadata()
952 * - query relayd for known streams, add them as ACTIVE_NO_DATA
953 * - query relayd for metadata
955 * call handle_active_no_data_streams()
957 * handle_quiescent_streams()
958 * - if at least one stream is ACTIVE_DATA:
959 * - peek stream event with lowest timestamp -> next_ts
960 * - for each quiescent stream
961 * - if next_ts >= quiescent end
962 * - set state to ACTIVE_NO_DATA
964 * - for each quiescent stream
965 * - set state to ACTIVE_NO_DATA
967 * call handle_active_no_data_streams()
969 * handle_active_data_streams()
970 * - if at least one stream is ACTIVE_DATA:
971 * - get stream event with lowest timestamp from heap
972 * - make that stream event the current message.
973 * - move this stream heap position to its next event
974 * - if we need to fetch data from relayd, move
975 * stream to ACTIVE_NO_DATA.
979 * end criterion: ctrl-c on client. If relayd exits or the session
980 * closes on the relay daemon side, we keep on waiting for streams.
981 * Eventually handle --end timestamp (also an end criterion).
983 * When disconnected from relayd: try to re-connect endlessly.
986 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
987 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
988 struct lttng_live_stream_iterator
*stream_iter
,
989 const bt_message
**curr_msg
)
991 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
992 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
993 enum lttng_live_iterator_status live_status
;
995 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
996 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
997 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
999 if (stream_iter
->has_stream_hung_up
) {
1001 * The stream has hung up and the stream was properly closed
1002 * during the last call to the current function. Return _END
1003 * status now so that this stream iterator is removed for the
1004 * stream iterator list.
1006 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1011 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
1014 * Make sure we have the most recent metadata and possibly some new
1017 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
1018 lttng_live_msg_iter
);
1019 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1023 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
1024 lttng_live_msg_iter
, stream_iter
);
1025 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1026 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1028 * We overwrite `live_status` since `curr_msg` is
1029 * likely set to a valid message in this function.
1031 live_status
= lttng_live_iterator_close_stream(
1032 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1037 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
1038 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1039 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1040 BT_ASSERT(!*curr_msg
);
1046 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
1047 lttng_live_msg_iter
, stream_iter
, curr_msg
);
1048 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1049 BT_ASSERT(!*curr_msg
);
1053 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1054 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1058 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s"
1059 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1060 lttng_live_iterator_status_string(live_status
),
1061 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1067 bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1069 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1071 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1072 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1076 enum lttng_live_iterator_status
adjust_discarded_packets_message(
1077 bt_self_message_iterator
*iter
,
1078 const bt_stream
*stream
,
1079 const bt_message
*msg_in
, bt_message
**msg_out
,
1080 uint64_t new_begin_ts
)
1082 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1083 enum bt_property_availability availability
;
1084 const bt_clock_snapshot
*clock_snapshot
;
1088 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1089 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1091 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1092 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1094 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1095 iter
, stream
, new_begin_ts
, end_ts
);
1097 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1101 bt_message_discarded_packets_set_count(*msg_out
, count
);
1107 enum lttng_live_iterator_status
adjust_discarded_events_message(
1108 bt_self_message_iterator
*iter
,
1109 const bt_stream
*stream
,
1110 const bt_message
*msg_in
, bt_message
**msg_out
,
1111 uint64_t new_begin_ts
)
1113 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1114 enum bt_property_availability availability
;
1115 const bt_clock_snapshot
*clock_snapshot
;
1119 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1120 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1122 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1123 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1125 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1126 iter
, stream
, new_begin_ts
, end_ts
);
1128 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1132 bt_message_discarded_events_set_count(*msg_out
, count
);
1138 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
1139 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1140 struct lttng_live_trace
*live_trace
,
1141 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1143 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1144 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1145 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1146 enum lttng_live_iterator_status stream_iter_status
;;
1147 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1148 uint64_t stream_iter_idx
;
1150 BT_ASSERT_DBG(live_trace
);
1151 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1153 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1154 "trace-id=%"PRIu64
, live_trace
->id
);
1156 * Update the current message of every stream iterators of this trace.
1157 * The current msg of every stream must have a timestamp equal or
1158 * larger than the last message returned by this iterator. We must
1159 * ensure monotonicity.
1161 stream_iter_idx
= 0;
1162 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1163 bool stream_iter_is_ended
= false;
1164 struct lttng_live_stream_iterator
*stream_iter
=
1165 g_ptr_array_index(live_trace
->stream_iterators
,
1169 * If there is no current message for this stream, go fetch
1172 while (!stream_iter
->current_msg
) {
1173 const bt_message
*msg
= NULL
;
1174 int64_t curr_msg_ts_ns
= INT64_MAX
;
1176 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1177 lttng_live_msg_iter
, stream_iter
, &msg
);
1179 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1180 stream_iter_is_ended
= true;
1184 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1190 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s"
1191 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1192 bt_common_message_type_string(bt_message_get_type(msg
)),
1193 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1196 * Get the timestamp in nanoseconds from origin of this
1199 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1200 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1204 * Check if the message of the current live stream
1205 * iterator occurred at the exact same time or after the
1206 * last message returned by this component's message
1207 * iterator. If not, we return an error.
1209 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1210 stream_iter
->current_msg
= msg
;
1211 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1213 const bt_clock_class
*clock_class
;
1214 const bt_stream_class
*stream_class
;
1215 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1216 int64_t last_inactivity_ts_ns
;
1218 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1219 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1221 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1223 stream_iter
->last_inactivity_ts
,
1224 &last_inactivity_ts_ns
);
1225 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1226 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1230 if (last_inactivity_ts_ns
> curr_msg_ts_ns
&&
1231 is_discarded_packet_or_event_message(msg
)) {
1233 * The CTF message iterator emits Discarded
1234 * Packets and Events with synthesized begin and
1235 * end timestamps from the bounds of the last
1236 * known packet and the newly decoded packet
1239 * The CTF message iterator is not aware of
1240 * stream inactivity beacons. Hence, we have
1241 * to adjust the begin timestamp of those types
1242 * of messages if a stream signaled its
1243 * inactivity up until _after_ the last known
1244 * packet's begin timestamp.
1246 * Otherwise, the monotonicity guarantee would
1249 const enum bt_message_type msg_type
=
1250 bt_message_get_type(msg
);
1251 enum lttng_live_iterator_status adjust_status
=
1252 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1253 bt_message
*adjusted_message
;
1256 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1257 adjust_status
= adjust_discarded_events_message(
1258 lttng_live_msg_iter
->self_msg_iter
,
1259 stream_iter
->stream
,
1260 msg
, &adjusted_message
,
1261 stream_iter
->last_inactivity_ts
);
1263 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1264 adjust_status
= adjust_discarded_packets_message(
1265 lttng_live_msg_iter
->self_msg_iter
,
1266 stream_iter
->stream
,
1267 msg
, &adjusted_message
,
1268 stream_iter
->last_inactivity_ts
);
1274 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1275 stream_iter_status
= adjust_status
;
1279 BT_ASSERT_DBG(adjusted_message
);
1280 stream_iter
->current_msg
= adjusted_message
;
1281 stream_iter
->current_msg_ts_ns
=
1282 last_inactivity_ts_ns
;
1285 * We received a message in the past. To ensure
1286 * monotonicity, we can't send it forward.
1288 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1289 "Message's timestamp is less than "
1290 "lttng-live's message iterator's last "
1291 "returned timestamp: "
1292 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1293 "last-msg-ts=%" PRId64
,
1294 lttng_live_msg_iter
, curr_msg_ts_ns
,
1295 lttng_live_msg_iter
->last_msg_ts_ns
);
1296 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1302 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1304 if (!stream_iter_is_ended
) {
1305 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1306 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1308 * Update the current best candidate message
1309 * for the stream iterator of this live trace
1310 * to be forwarded downstream.
1312 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1313 youngest_candidate_stream_iter
= stream_iter
;
1314 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1316 * Order the messages in an arbitrary but
1317 * deterministic way.
1319 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1320 int ret
= common_muxing_compare_messages(
1321 stream_iter
->current_msg
,
1322 youngest_candidate_stream_iter
->current_msg
);
1325 * The `youngest_candidate_stream_iter->current_msg`
1326 * should go first. Update the next
1327 * iterator and the current timestamp.
1329 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1330 youngest_candidate_stream_iter
= stream_iter
;
1331 } else if (ret
== 0) {
1333 * Unable to pick which one should go
1336 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1337 "stream-iter-addr=%p"
1338 "stream-iter-addr=%p",
1340 youngest_candidate_stream_iter
);
1347 * The live stream iterator has ended. That
1348 * iterator is removed from the array, but
1349 * there is no need to increment
1350 * stream_iter_idx as
1351 * g_ptr_array_remove_index_fast replaces the
1352 * removed element with the array's last
1355 g_ptr_array_remove_index_fast(
1356 live_trace
->stream_iterators
,
1361 if (youngest_candidate_stream_iter
) {
1362 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1363 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1366 * The only case where we don't have a candidate for this trace
1367 * is if we reached the end of all the iterators.
1369 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1370 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1374 return stream_iter_status
;
1378 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1379 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1380 struct lttng_live_session
*session
,
1381 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1383 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1384 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1385 enum lttng_live_iterator_status stream_iter_status
;
1386 uint64_t trace_idx
= 0;
1387 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1388 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1390 BT_COMP_LOGD("Finding the next stream iterator for session: "
1391 "session-id=%"PRIu64
, session
->id
);
1393 * Make sure we are attached to the session and look for new streams
1396 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1397 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1398 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1399 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1403 BT_ASSERT_DBG(session
->traces
);
1405 while (trace_idx
< session
->traces
->len
) {
1406 bool trace_is_ended
= false;
1407 struct lttng_live_stream_iterator
*stream_iter
;
1408 struct lttng_live_trace
*trace
=
1409 g_ptr_array_index(session
->traces
, trace_idx
);
1411 stream_iter_status
= next_stream_iterator_for_trace(
1412 lttng_live_msg_iter
, trace
, &stream_iter
);
1413 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1415 * All the live stream iterators for this trace are
1416 * ENDed. Remove the trace from this session.
1418 trace_is_ended
= true;
1419 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1423 if (!trace_is_ended
) {
1424 BT_ASSERT_DBG(stream_iter
);
1426 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1427 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1428 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1429 youngest_candidate_stream_iter
= stream_iter
;
1430 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1432 * Order the messages in an arbitrary but
1433 * deterministic way.
1435 int ret
= common_muxing_compare_messages(
1436 stream_iter
->current_msg
,
1437 youngest_candidate_stream_iter
->current_msg
);
1440 * The `youngest_candidate_stream_iter->current_msg`
1441 * should go first. Update the next iterator
1442 * and the current timestamp.
1444 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1445 youngest_candidate_stream_iter
= stream_iter
;
1446 } else if (ret
== 0) {
1447 /* Unable to pick which one should go first. */
1448 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1449 "stream-iter-addr=%p" "stream-iter-addr=%p",
1450 stream_iter
, youngest_candidate_stream_iter
);
1456 * trace_idx is not incremented since
1457 * g_ptr_array_remove_index_fast replaces the
1458 * element at trace_idx with the array's last element.
1460 g_ptr_array_remove_index_fast(session
->traces
,
1464 if (youngest_candidate_stream_iter
) {
1465 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1466 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1469 * The only cases where we don't have a candidate for this
1471 * 1. if we reached the end of all the iterators of all the
1472 * traces of this session,
1473 * 2. if we never had live stream iterator in the first place.
1475 * In either cases, we return END.
1477 BT_ASSERT(session
->traces
->len
== 0);
1478 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1481 return stream_iter_status
;
1485 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1489 for (i
= 0; i
< count
; i
++) {
1490 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1495 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
1496 bt_self_message_iterator
*self_msg_it
,
1497 bt_message_array_const msgs
, uint64_t capacity
,
1500 bt_message_iterator_class_next_method_status status
;
1501 enum lttng_live_viewer_status viewer_status
;
1502 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1503 bt_self_message_iterator_get_data(self_msg_it
);
1504 struct lttng_live_component
*lttng_live
=
1505 lttng_live_msg_iter
->lttng_live_comp
;
1506 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1507 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1508 enum lttng_live_iterator_status stream_iter_status
;
1509 uint64_t session_idx
;
1513 BT_ASSERT_DBG(lttng_live_msg_iter
);
1515 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1517 * The iterator was interrupted in a previous call to the
1518 * `_next()` method. We currently do not support generating
1519 * messages after such event. The babeltrace2 CLI should never
1520 * be running the graph after being interrupted. So this check
1521 * is to prevent other graph users from using this live
1522 * iterator in an messed up internal state.
1524 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1525 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1526 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1531 * Clear all the invalid message reference that might be left over in
1534 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1537 * If no session are exposed on the relay found at the url provided by
1538 * the user, session count will be 0. In this case, we return status
1539 * end to return gracefully.
1541 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1542 if (lttng_live
->params
.sess_not_found_act
!=
1543 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1544 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1548 * The are no more active session for this session
1549 * name. Retry to create a viewer session for the
1550 * requested session name.
1552 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1553 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1554 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1555 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1556 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1557 "Error creating LTTng live viewer session");
1558 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1559 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1568 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1569 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1573 * Here the muxing of message is done.
1575 * We need to iterate over all the streams of all the traces of all the
1576 * viewer sessions in order to get the message with the smallest
1577 * timestamp. In this case, a session is a viewer session and there is
1578 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1579 * kernel). Each viewer session can have multiple traces, for example,
1580 * 64bit UST viewer sessions could have multiple per-pid traces.
1582 * We iterate over the streams of each traces to update and see what is
1583 * their next message's timestamp. From those timestamps, we select the
1584 * message with the smallest timestamp as the best candidate message
1585 * for that trace and do the same thing across all the sessions.
1587 * We then compare the timestamp of best candidate message of all the
1588 * sessions to pick the message with the smallest timestamp and we
1591 while (*count
< capacity
) {
1592 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1593 *candidate_stream_iter
= NULL
;
1594 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1596 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1598 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1599 struct lttng_live_session
*session
=
1600 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1603 /* Find the best candidate message to send downstream. */
1604 stream_iter_status
= next_stream_iterator_for_session(
1605 lttng_live_msg_iter
, session
,
1606 &candidate_stream_iter
);
1608 /* If we receive an END status, it means that either:
1609 * - Those traces never had active streams (UST with no
1610 * data produced yet),
1611 * - All live stream iterators have ENDed.*/
1612 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1613 if (session
->closed
&& session
->traces
->len
== 0) {
1615 * Remove the session from the list.
1616 * session_idx is not modified since
1617 * g_ptr_array_remove_index_fast
1618 * replaces the the removed element with
1619 * the array's last element.
1621 g_ptr_array_remove_index_fast(
1622 lttng_live_msg_iter
->sessions
,
1630 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1634 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1635 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1636 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1637 youngest_stream_iter
= candidate_stream_iter
;
1638 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1640 * The currently selected message to be sent
1641 * downstream next has the exact same timestamp
1642 * that of the current candidate message. We
1643 * must break the tie in a predictable manner.
1645 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1647 * Order the messages in an arbitrary but
1648 * deterministic way.
1650 int ret
= common_muxing_compare_messages(
1651 candidate_stream_iter
->current_msg
,
1652 youngest_stream_iter
->current_msg
);
1655 * The `candidate_stream_iter->current_msg`
1656 * should go first. Update the next
1657 * iterator and the current timestamp.
1659 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1660 youngest_stream_iter
= candidate_stream_iter
;
1661 } else if (ret
== 0) {
1662 /* Unable to pick which one should go first. */
1663 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1664 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1665 youngest_stream_iter
, candidate_stream_iter
);
1672 if (!youngest_stream_iter
) {
1673 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1677 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1678 /* Ensure monotonicity. */
1679 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1680 youngest_stream_iter
->current_msg_ts_ns
);
1683 * Insert the next message to the message batch. This will set
1684 * stream iterator current messsage to NULL so that next time
1685 * we fetch the next message of that stream iterator
1687 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1690 /* Update the last timestamp in nanoseconds sent downstream. */
1691 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1692 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1694 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1698 switch (stream_iter_status
) {
1699 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1700 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1702 * If we gathered messages, return _OK even if the graph was
1703 * interrupted. This allows for the components downstream to at
1704 * least get the thoses messages. If the graph was indeed
1705 * interrupted there should not be another _next() call as the
1706 * application will tear down the graph. This component class
1707 * doesn't support restarting after an interruption.
1710 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1712 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1715 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1716 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1718 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1719 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1720 "Memory error preparing the next batch of messages: "
1721 "live-iter-status=%s",
1722 lttng_live_iterator_status_string(stream_iter_status
));
1723 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1725 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1726 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1727 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1728 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1729 "Error preparing the next batch of messages: "
1730 "live-iter-status=%s",
1731 lttng_live_iterator_status_string(stream_iter_status
));
1733 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1734 /* Put all existing messages on error. */
1735 put_messages(msgs
, *count
);
1746 struct lttng_live_msg_iter
*lttng_live_msg_iter_create(
1747 struct lttng_live_component
*lttng_live_comp
,
1748 bt_self_message_iterator
*self_msg_it
)
1750 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1751 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1753 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1754 g_new0(struct lttng_live_msg_iter
, 1);
1755 if (!lttng_live_msg_iter
) {
1756 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1757 "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
= g_ptr_array_new_with_free_func(
1771 (GDestroyNotify
) lttng_live_destroy_session
);
1772 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1775 return lttng_live_msg_iter
;
1780 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
1781 bt_self_message_iterator
*self_msg_it
,
1782 bt_self_message_iterator_configuration
*config
,
1783 bt_self_component_port_output
*self_port
)
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
=
1791 bt_self_message_iterator_borrow_component(self_msg_it
);
1793 lttng_live
= bt_self_component_get_data(self_comp
);
1794 log_level
= lttng_live
->log_level
;
1795 self_comp
= lttng_live
->self_comp
;
1798 /* There can be only one downstream iterator at the same time. */
1799 BT_ASSERT(!lttng_live
->has_msg_iter
);
1800 lttng_live
->has_msg_iter
= true;
1802 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
,
1804 if (!lttng_live_msg_iter
) {
1805 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1806 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1807 "Failed to create lttng_live_msg_iter");
1811 viewer_status
= live_viewer_connection_create(self_comp
, NULL
,
1812 log_level
, lttng_live
->params
.url
->str
, false,
1813 lttng_live_msg_iter
, <tng_live_msg_iter
->viewer_connection
);
1814 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1815 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1816 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1817 "Failed to create viewer connection");
1818 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1820 * Interruption in the _iter_init() method is not
1821 * supported. Return an error.
1823 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1824 "Interrupted while creating viewer connection");
1829 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1830 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1831 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1832 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1833 "Failed to create viewer session");
1834 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1836 * Interruption in the _iter_init() method is not
1837 * supported. Return an error.
1839 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1840 "Interrupted when creating viewer session");
1845 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1846 switch (lttng_live
->params
.sess_not_found_act
) {
1847 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1848 BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of "
1849 "%s=\"%s\" component parameter: url=\"%s\"",
1850 SESS_NOT_FOUND_ACTION_PARAM
,
1851 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1852 lttng_live
->params
.url
->str
);
1854 case SESSION_NOT_FOUND_ACTION_FAIL
:
1855 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1856 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1857 "component parameter: url =\"%s\"",
1858 SESS_NOT_FOUND_ACTION_PARAM
,
1859 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1860 lttng_live
->params
.url
->str
);
1862 case SESSION_NOT_FOUND_ACTION_END
:
1863 BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1864 "call because of %s=\"%s\" component parameter: "
1865 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1866 SESS_NOT_FOUND_ACTION_END_STR
,
1867 lttng_live
->params
.url
->str
);
1874 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1875 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1879 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1880 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1885 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1886 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1887 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1891 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1892 const bt_value
*params
, const bt_value
**result
,
1893 bt_self_component_class
*self_comp_class
,
1894 bt_logging_level log_level
)
1896 bt_component_class_query_method_status status
;
1897 const bt_value
*url_value
= NULL
;
1899 struct live_viewer_connection
*viewer_connection
= NULL
;
1900 enum lttng_live_viewer_status viewer_status
;
1901 enum bt_param_validation_status validation_status
;
1902 gchar
*validate_error
= NULL
;
1904 validation_status
= bt_param_validation_validate(params
,
1905 list_sessions_params
, &validate_error
);
1906 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1907 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1909 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1910 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1911 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1916 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1917 url
= bt_value_string_get(url_value
);
1919 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
,
1920 log_level
, url
, true, NULL
, &viewer_connection
);
1921 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1922 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1923 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1924 "Failed to create viewer connection");
1925 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1926 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1927 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1934 status
= live_viewer_connection_list_sessions(viewer_connection
,
1936 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1937 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1938 "Failed to list viewer sessions");
1945 BT_VALUE_PUT_REF_AND_RESET(*result
);
1948 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1952 if (viewer_connection
) {
1953 live_viewer_connection_destroy(viewer_connection
);
1956 g_free(validate_error
);
1962 bt_component_class_query_method_status
lttng_live_query_support_info(
1963 const bt_value
*params
, const bt_value
**result
,
1964 bt_self_component_class
*self_comp_class
,
1965 bt_logging_level log_level
)
1967 bt_component_class_query_method_status status
=
1968 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1969 const bt_value
*input_type_value
;
1970 const bt_value
*input_value
;
1972 struct bt_common_lttng_live_url_parts parts
= { 0 };
1974 /* Used by the logging macros */
1975 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1978 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1980 if (!input_type_value
) {
1981 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1982 "Missing expected `type` parameter.");
1986 if (!bt_value_is_string(input_type_value
)) {
1987 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1988 "`type` parameter is not a string value.");
1992 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1993 /* We don't handle file system paths */
1997 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1999 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2000 "Missing expected `input` parameter.");
2004 if (!bt_value_is_string(input_value
)) {
2005 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2006 "`input` parameter is not a string value.");
2010 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
2012 if (parts
.session_name
) {
2014 * Looks pretty much like an LTTng live URL: we got the
2015 * session name part, which forms a complete URL.
2021 *result
= bt_value_real_create_init(weight
);
2023 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2031 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2034 BT_ASSERT(!*result
);
2037 bt_common_destroy_lttng_live_url_parts(&parts
);
2042 bt_component_class_query_method_status
lttng_live_query(
2043 bt_self_component_class_source
*comp_class
,
2044 bt_private_query_executor
*priv_query_exec
,
2045 const char *object
, const bt_value
*params
,
2046 __attribute__((unused
)) void *method_data
,
2047 const bt_value
**result
)
2049 bt_component_class_query_method_status status
=
2050 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2051 bt_self_component
*self_comp
= NULL
;
2052 bt_self_component_class
*self_comp_class
=
2053 bt_self_component_class_source_as_self_component_class(comp_class
);
2054 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2055 bt_private_query_executor_as_query_executor_const(
2058 if (strcmp(object
, "sessions") == 0) {
2059 status
= lttng_live_query_list_sessions(params
, result
,
2060 self_comp_class
, log_level
);
2061 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2062 status
= lttng_live_query_support_info(params
, result
,
2063 self_comp_class
, log_level
);
2065 BT_COMP_LOGI("Unknown query object `%s`", object
);
2066 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2075 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2080 if (lttng_live
->params
.url
) {
2081 g_string_free(lttng_live
->params
.url
, TRUE
);
2087 void lttng_live_component_finalize(bt_self_component_source
*component
)
2089 void *data
= bt_self_component_get_data(
2090 bt_self_component_source_as_self_component(component
));
2095 lttng_live_component_destroy_data(data
);
2099 enum session_not_found_action
parse_session_not_found_action_param(
2100 const bt_value
*no_session_param
)
2102 enum session_not_found_action action
;
2103 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2105 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2106 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2107 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2108 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2110 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2111 action
= SESSION_NOT_FOUND_ACTION_END
;
2117 static struct bt_param_validation_value_descr inputs_elem_descr
= {
2118 .type
= BT_VALUE_TYPE_STRING
,
2121 static const char *sess_not_found_action_choices
[] = {
2122 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2123 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2124 SESS_NOT_FOUND_ACTION_END_STR
,
2127 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2128 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
2131 .element_type
= &inputs_elem_descr
,
2133 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
2134 .choices
= sess_not_found_action_choices
,
2136 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2140 bt_component_class_initialize_method_status
lttng_live_component_create(
2141 const bt_value
*params
,
2142 bt_logging_level log_level
,
2143 bt_self_component
*self_comp
,
2144 struct lttng_live_component
**component
)
2146 struct lttng_live_component
*lttng_live
= NULL
;
2147 const bt_value
*inputs_value
;
2148 const bt_value
*url_value
;
2149 const bt_value
*value
;
2151 enum bt_param_validation_status validation_status
;
2152 gchar
*validation_error
= NULL
;
2153 bt_component_class_initialize_method_status status
;
2155 validation_status
= bt_param_validation_validate(params
, params_descr
,
2157 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2158 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2160 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2161 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2162 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2166 lttng_live
= g_new0(struct lttng_live_component
, 1);
2168 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2171 lttng_live
->log_level
= log_level
;
2172 lttng_live
->self_comp
= self_comp
;
2173 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2174 lttng_live
->has_msg_iter
= false;
2177 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2179 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2180 url
= bt_value_string_get(url_value
);
2182 lttng_live
->params
.url
= g_string_new(url
);
2183 if (!lttng_live
->params
.url
) {
2184 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2188 value
= bt_value_map_borrow_entry_value_const(params
,
2189 SESS_NOT_FOUND_ACTION_PARAM
);
2191 lttng_live
->params
.sess_not_found_act
=
2192 parse_session_not_found_action_param(value
);
2194 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2195 "defaulting to `%s`.",
2196 SESS_NOT_FOUND_ACTION_PARAM
,
2197 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2198 lttng_live
->params
.sess_not_found_act
=
2199 SESSION_NOT_FOUND_ACTION_CONTINUE
;
2202 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2206 lttng_live_component_destroy_data(lttng_live
);
2209 g_free(validation_error
);
2211 *component
= lttng_live
;
2216 bt_component_class_initialize_method_status
lttng_live_component_init(
2217 bt_self_component_source
*self_comp_src
,
2218 bt_self_component_source_configuration
*config
,
2219 const bt_value
*params
,
2220 __attribute__((unused
)) void *init_method_data
)
2222 struct lttng_live_component
*lttng_live
;
2223 bt_component_class_initialize_method_status ret
;
2224 bt_self_component
*self_comp
=
2225 bt_self_component_source_as_self_component(self_comp_src
);
2226 bt_logging_level log_level
= bt_component_get_logging_level(
2227 bt_self_component_as_component(self_comp
));
2228 bt_self_component_add_port_status add_port_status
;
2230 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2231 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2235 add_port_status
= bt_self_component_source_add_output_port(
2236 self_comp_src
, "out", NULL
, NULL
);
2237 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2238 ret
= (int) add_port_status
;
2242 bt_self_component_set_data(self_comp
, lttng_live
);
2246 lttng_live_component_destroy_data(lttng_live
);