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
handle_late_message(
1139 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1140 struct lttng_live_stream_iterator
*stream_iter
,
1141 int64_t late_msg_ts_ns
, const bt_message
*late_msg
)
1143 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1144 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1145 const bt_clock_class
*clock_class
;
1146 const bt_stream_class
*stream_class
;
1147 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1148 int64_t last_inactivity_ts_ns
;
1149 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1151 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1152 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1154 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(clock_class
,
1155 stream_iter
->last_inactivity_ts
, &last_inactivity_ts_ns
);
1156 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1157 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1161 if (last_inactivity_ts_ns
> late_msg_ts_ns
&&
1162 is_discarded_packet_or_event_message(late_msg
)) {
1164 * The CTF message iterator emits Discarded Packets and Events
1165 * with synthesized begin and end timestamps from the bounds of
1166 * the last known packet and the newly decoded packet header.
1168 * The CTF message iterator is not aware of stream inactivity
1169 * beacons. Hence, we have to adjust the begin timestamp of
1170 * those types of messages if a stream signaled its inactivity
1171 * up until _after_ the last known packet's begin timestamp.
1173 * Otherwise, the monotonicity guarantee would not be
1176 const enum bt_message_type msg_type
= bt_message_get_type(
1178 enum lttng_live_iterator_status adjust_status
=
1179 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1180 bt_message
*adjusted_message
;
1183 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1184 adjust_status
= adjust_discarded_events_message(
1185 lttng_live_msg_iter
->self_msg_iter
,
1186 stream_iter
->stream
,
1187 late_msg
, &adjusted_message
,
1188 stream_iter
->last_inactivity_ts
);
1190 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1191 adjust_status
= adjust_discarded_packets_message(
1192 lttng_live_msg_iter
->self_msg_iter
,
1193 stream_iter
->stream
,
1194 late_msg
, &adjusted_message
,
1195 stream_iter
->last_inactivity_ts
);
1201 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1202 stream_iter_status
= adjust_status
;
1206 BT_ASSERT_DBG(adjusted_message
);
1207 stream_iter
->current_msg
= adjusted_message
;
1208 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1211 * We received a message in the past. To ensure
1212 * monotonicity, we can't send it forward.
1214 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1215 "Message's timestamp is less than lttng-live's message "
1216 "iterator's last returned timestamp: "
1217 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1218 "last-msg-ts=%" PRId64
,
1219 lttng_live_msg_iter
, late_msg_ts_ns
,
1220 lttng_live_msg_iter
->last_msg_ts_ns
);
1221 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1225 return stream_iter_status
;
1229 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
1230 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1231 struct lttng_live_trace
*live_trace
,
1232 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1234 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1235 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1236 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1237 enum lttng_live_iterator_status stream_iter_status
;;
1238 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1239 uint64_t stream_iter_idx
;
1241 BT_ASSERT_DBG(live_trace
);
1242 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1244 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1245 "trace-id=%"PRIu64
, live_trace
->id
);
1247 * Update the current message of every stream iterators of this trace.
1248 * The current msg of every stream must have a timestamp equal or
1249 * larger than the last message returned by this iterator. We must
1250 * ensure monotonicity.
1252 stream_iter_idx
= 0;
1253 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1254 bool stream_iter_is_ended
= false;
1255 struct lttng_live_stream_iterator
*stream_iter
=
1256 g_ptr_array_index(live_trace
->stream_iterators
,
1260 * If there is no current message for this stream, go fetch
1263 while (!stream_iter
->current_msg
) {
1264 const bt_message
*msg
= NULL
;
1265 int64_t curr_msg_ts_ns
= INT64_MAX
;
1267 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1268 lttng_live_msg_iter
, stream_iter
, &msg
);
1270 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1271 stream_iter_is_ended
= true;
1275 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1281 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s"
1282 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1283 bt_common_message_type_string(bt_message_get_type(msg
)),
1284 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1287 * Get the timestamp in nanoseconds from origin of this
1290 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1291 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1295 * Check if the message of the current live stream
1296 * iterator occurred at the exact same time or after the
1297 * last message returned by this component's message
1298 * iterator. If not, we return an error.
1300 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1301 stream_iter
->current_msg
= msg
;
1302 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1305 * We received a message from the past. This
1306 * may be fixable but it can also be an error.
1308 stream_iter_status
= handle_late_message(
1309 lttng_live_msg_iter
, stream_iter
,
1310 curr_msg_ts_ns
, msg
);
1311 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1312 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1313 "Late message could not be handled correctly: "
1314 "lttng-live-msg-iter-addr=%p, "
1315 "stream-name=\"%s\", "
1316 "curr-msg-ts=%" PRId64
1317 ", last-msg-ts=%" PRId64
,
1318 lttng_live_msg_iter
,
1319 stream_iter
->name
->str
,
1321 lttng_live_msg_iter
->last_msg_ts_ns
);
1322 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1328 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1330 if (!stream_iter_is_ended
) {
1331 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1332 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1334 * Update the current best candidate message
1335 * for the stream iterator of this live trace
1336 * to be forwarded downstream.
1338 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1339 youngest_candidate_stream_iter
= stream_iter
;
1340 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1342 * Order the messages in an arbitrary but
1343 * deterministic way.
1345 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1346 int ret
= common_muxing_compare_messages(
1347 stream_iter
->current_msg
,
1348 youngest_candidate_stream_iter
->current_msg
);
1351 * The `youngest_candidate_stream_iter->current_msg`
1352 * should go first. Update the next
1353 * iterator and the current timestamp.
1355 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1356 youngest_candidate_stream_iter
= stream_iter
;
1357 } else if (ret
== 0) {
1359 * Unable to pick which one should go
1362 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1363 "stream-iter-addr=%p"
1364 "stream-iter-addr=%p",
1366 youngest_candidate_stream_iter
);
1373 * The live stream iterator has ended. That
1374 * iterator is removed from the array, but
1375 * there is no need to increment
1376 * stream_iter_idx as
1377 * g_ptr_array_remove_index_fast replaces the
1378 * removed element with the array's last
1381 g_ptr_array_remove_index_fast(
1382 live_trace
->stream_iterators
,
1387 if (youngest_candidate_stream_iter
) {
1388 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1389 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1392 * The only case where we don't have a candidate for this trace
1393 * is if we reached the end of all the iterators.
1395 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1396 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1400 return stream_iter_status
;
1404 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1405 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1406 struct lttng_live_session
*session
,
1407 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1409 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1410 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1411 enum lttng_live_iterator_status stream_iter_status
;
1412 uint64_t trace_idx
= 0;
1413 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1414 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1416 BT_COMP_LOGD("Finding the next stream iterator for session: "
1417 "session-id=%"PRIu64
, session
->id
);
1419 * Make sure we are attached to the session and look for new streams
1422 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1423 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1424 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1425 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1429 BT_ASSERT_DBG(session
->traces
);
1431 while (trace_idx
< session
->traces
->len
) {
1432 bool trace_is_ended
= false;
1433 struct lttng_live_stream_iterator
*stream_iter
;
1434 struct lttng_live_trace
*trace
=
1435 g_ptr_array_index(session
->traces
, trace_idx
);
1437 stream_iter_status
= next_stream_iterator_for_trace(
1438 lttng_live_msg_iter
, trace
, &stream_iter
);
1439 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1441 * All the live stream iterators for this trace are
1442 * ENDed. Remove the trace from this session.
1444 trace_is_ended
= true;
1445 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1449 if (!trace_is_ended
) {
1450 BT_ASSERT_DBG(stream_iter
);
1452 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1453 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1454 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1455 youngest_candidate_stream_iter
= stream_iter
;
1456 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1458 * Order the messages in an arbitrary but
1459 * deterministic way.
1461 int ret
= common_muxing_compare_messages(
1462 stream_iter
->current_msg
,
1463 youngest_candidate_stream_iter
->current_msg
);
1466 * The `youngest_candidate_stream_iter->current_msg`
1467 * should go first. Update the next iterator
1468 * and the current timestamp.
1470 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1471 youngest_candidate_stream_iter
= stream_iter
;
1472 } else if (ret
== 0) {
1473 /* Unable to pick which one should go first. */
1474 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1475 "stream-iter-addr=%p" "stream-iter-addr=%p",
1476 stream_iter
, youngest_candidate_stream_iter
);
1482 * trace_idx is not incremented since
1483 * g_ptr_array_remove_index_fast replaces the
1484 * element at trace_idx with the array's last element.
1486 g_ptr_array_remove_index_fast(session
->traces
,
1490 if (youngest_candidate_stream_iter
) {
1491 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1492 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1495 * The only cases where we don't have a candidate for this
1497 * 1. if we reached the end of all the iterators of all the
1498 * traces of this session,
1499 * 2. if we never had live stream iterator in the first place.
1501 * In either cases, we return END.
1503 BT_ASSERT(session
->traces
->len
== 0);
1504 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1507 return stream_iter_status
;
1511 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1515 for (i
= 0; i
< count
; i
++) {
1516 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1521 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
1522 bt_self_message_iterator
*self_msg_it
,
1523 bt_message_array_const msgs
, uint64_t capacity
,
1526 bt_message_iterator_class_next_method_status status
;
1527 enum lttng_live_viewer_status viewer_status
;
1528 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1529 bt_self_message_iterator_get_data(self_msg_it
);
1530 struct lttng_live_component
*lttng_live
=
1531 lttng_live_msg_iter
->lttng_live_comp
;
1532 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1533 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1534 enum lttng_live_iterator_status stream_iter_status
;
1535 uint64_t session_idx
;
1539 BT_ASSERT_DBG(lttng_live_msg_iter
);
1541 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1543 * The iterator was interrupted in a previous call to the
1544 * `_next()` method. We currently do not support generating
1545 * messages after such event. The babeltrace2 CLI should never
1546 * be running the graph after being interrupted. So this check
1547 * is to prevent other graph users from using this live
1548 * iterator in an messed up internal state.
1550 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1551 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1552 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1557 * Clear all the invalid message reference that might be left over in
1560 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1563 * If no session are exposed on the relay found at the url provided by
1564 * the user, session count will be 0. In this case, we return status
1565 * end to return gracefully.
1567 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1568 if (lttng_live
->params
.sess_not_found_act
!=
1569 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1570 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1574 * The are no more active session for this session
1575 * name. Retry to create a viewer session for the
1576 * requested session name.
1578 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1579 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1580 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1581 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1582 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1583 "Error creating LTTng live viewer session");
1584 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1585 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1594 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1595 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1599 * Here the muxing of message is done.
1601 * We need to iterate over all the streams of all the traces of all the
1602 * viewer sessions in order to get the message with the smallest
1603 * timestamp. In this case, a session is a viewer session and there is
1604 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1605 * kernel). Each viewer session can have multiple traces, for example,
1606 * 64bit UST viewer sessions could have multiple per-pid traces.
1608 * We iterate over the streams of each traces to update and see what is
1609 * their next message's timestamp. From those timestamps, we select the
1610 * message with the smallest timestamp as the best candidate message
1611 * for that trace and do the same thing across all the sessions.
1613 * We then compare the timestamp of best candidate message of all the
1614 * sessions to pick the message with the smallest timestamp and we
1617 while (*count
< capacity
) {
1618 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1619 *candidate_stream_iter
= NULL
;
1620 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1622 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1624 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1625 struct lttng_live_session
*session
=
1626 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1629 /* Find the best candidate message to send downstream. */
1630 stream_iter_status
= next_stream_iterator_for_session(
1631 lttng_live_msg_iter
, session
,
1632 &candidate_stream_iter
);
1634 /* If we receive an END status, it means that either:
1635 * - Those traces never had active streams (UST with no
1636 * data produced yet),
1637 * - All live stream iterators have ENDed.*/
1638 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1639 if (session
->closed
&& session
->traces
->len
== 0) {
1641 * Remove the session from the list.
1642 * session_idx is not modified since
1643 * g_ptr_array_remove_index_fast
1644 * replaces the the removed element with
1645 * the array's last element.
1647 g_ptr_array_remove_index_fast(
1648 lttng_live_msg_iter
->sessions
,
1656 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1660 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1661 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1662 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1663 youngest_stream_iter
= candidate_stream_iter
;
1664 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1666 * The currently selected message to be sent
1667 * downstream next has the exact same timestamp
1668 * that of the current candidate message. We
1669 * must break the tie in a predictable manner.
1671 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1673 * Order the messages in an arbitrary but
1674 * deterministic way.
1676 int ret
= common_muxing_compare_messages(
1677 candidate_stream_iter
->current_msg
,
1678 youngest_stream_iter
->current_msg
);
1681 * The `candidate_stream_iter->current_msg`
1682 * should go first. Update the next
1683 * iterator and the current timestamp.
1685 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1686 youngest_stream_iter
= candidate_stream_iter
;
1687 } else if (ret
== 0) {
1688 /* Unable to pick which one should go first. */
1689 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1690 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1691 youngest_stream_iter
, candidate_stream_iter
);
1698 if (!youngest_stream_iter
) {
1699 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1703 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1704 /* Ensure monotonicity. */
1705 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1706 youngest_stream_iter
->current_msg_ts_ns
);
1709 * Insert the next message to the message batch. This will set
1710 * stream iterator current messsage to NULL so that next time
1711 * we fetch the next message of that stream iterator
1713 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1716 /* Update the last timestamp in nanoseconds sent downstream. */
1717 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1718 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1720 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1724 switch (stream_iter_status
) {
1725 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1726 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1728 * If we gathered messages, return _OK even if the graph was
1729 * interrupted. This allows for the components downstream to at
1730 * least get the thoses messages. If the graph was indeed
1731 * interrupted there should not be another _next() call as the
1732 * application will tear down the graph. This component class
1733 * doesn't support restarting after an interruption.
1736 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1738 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1741 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1742 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1744 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1745 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1746 "Memory error preparing the next batch of messages: "
1747 "live-iter-status=%s",
1748 lttng_live_iterator_status_string(stream_iter_status
));
1749 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1751 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1752 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1753 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1754 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1755 "Error preparing the next batch of messages: "
1756 "live-iter-status=%s",
1757 lttng_live_iterator_status_string(stream_iter_status
));
1759 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1760 /* Put all existing messages on error. */
1761 put_messages(msgs
, *count
);
1772 struct lttng_live_msg_iter
*lttng_live_msg_iter_create(
1773 struct lttng_live_component
*lttng_live_comp
,
1774 bt_self_message_iterator
*self_msg_it
)
1776 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1777 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1779 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1780 g_new0(struct lttng_live_msg_iter
, 1);
1781 if (!lttng_live_msg_iter
) {
1782 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1783 "Failed to allocate lttng_live_msg_iter");
1787 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1788 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1789 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1790 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1792 lttng_live_msg_iter
->active_stream_iter
= 0;
1793 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1794 lttng_live_msg_iter
->was_interrupted
= false;
1796 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1797 (GDestroyNotify
) lttng_live_destroy_session
);
1798 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1801 return lttng_live_msg_iter
;
1806 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
1807 bt_self_message_iterator
*self_msg_it
,
1808 bt_self_message_iterator_configuration
*config
,
1809 bt_self_component_port_output
*self_port
)
1811 bt_message_iterator_class_initialize_method_status status
;
1812 struct lttng_live_component
*lttng_live
;
1813 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1814 enum lttng_live_viewer_status viewer_status
;
1815 bt_logging_level log_level
;
1816 bt_self_component
*self_comp
=
1817 bt_self_message_iterator_borrow_component(self_msg_it
);
1819 lttng_live
= bt_self_component_get_data(self_comp
);
1820 log_level
= lttng_live
->log_level
;
1821 self_comp
= lttng_live
->self_comp
;
1824 /* There can be only one downstream iterator at the same time. */
1825 BT_ASSERT(!lttng_live
->has_msg_iter
);
1826 lttng_live
->has_msg_iter
= true;
1828 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
,
1830 if (!lttng_live_msg_iter
) {
1831 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1832 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1833 "Failed to create lttng_live_msg_iter");
1837 viewer_status
= live_viewer_connection_create(self_comp
, NULL
,
1838 log_level
, lttng_live
->params
.url
->str
, false,
1839 lttng_live_msg_iter
, <tng_live_msg_iter
->viewer_connection
);
1840 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1841 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1842 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1843 "Failed to create viewer connection");
1844 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1846 * Interruption in the _iter_init() method is not
1847 * supported. Return an error.
1849 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1850 "Interrupted while creating viewer connection");
1855 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1856 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1857 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1858 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1859 "Failed to create viewer session");
1860 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1862 * Interruption in the _iter_init() method is not
1863 * supported. Return an error.
1865 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1866 "Interrupted when creating viewer session");
1871 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1872 switch (lttng_live
->params
.sess_not_found_act
) {
1873 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1874 BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of "
1875 "%s=\"%s\" component parameter: url=\"%s\"",
1876 SESS_NOT_FOUND_ACTION_PARAM
,
1877 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1878 lttng_live
->params
.url
->str
);
1880 case SESSION_NOT_FOUND_ACTION_FAIL
:
1881 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1882 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1883 "component parameter: url =\"%s\"",
1884 SESS_NOT_FOUND_ACTION_PARAM
,
1885 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1886 lttng_live
->params
.url
->str
);
1888 case SESSION_NOT_FOUND_ACTION_END
:
1889 BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1890 "call because of %s=\"%s\" component parameter: "
1891 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1892 SESS_NOT_FOUND_ACTION_END_STR
,
1893 lttng_live
->params
.url
->str
);
1900 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1901 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1905 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1906 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1911 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1912 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1913 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1917 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1918 const bt_value
*params
, const bt_value
**result
,
1919 bt_self_component_class
*self_comp_class
,
1920 bt_logging_level log_level
)
1922 bt_component_class_query_method_status status
;
1923 const bt_value
*url_value
= NULL
;
1925 struct live_viewer_connection
*viewer_connection
= NULL
;
1926 enum lttng_live_viewer_status viewer_status
;
1927 enum bt_param_validation_status validation_status
;
1928 gchar
*validate_error
= NULL
;
1930 validation_status
= bt_param_validation_validate(params
,
1931 list_sessions_params
, &validate_error
);
1932 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1933 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1935 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1936 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1937 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1942 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1943 url
= bt_value_string_get(url_value
);
1945 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
,
1946 log_level
, url
, true, NULL
, &viewer_connection
);
1947 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1948 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1949 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1950 "Failed to create viewer connection");
1951 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1952 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1953 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1960 status
= live_viewer_connection_list_sessions(viewer_connection
,
1962 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1963 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1964 "Failed to list viewer sessions");
1971 BT_VALUE_PUT_REF_AND_RESET(*result
);
1974 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1978 if (viewer_connection
) {
1979 live_viewer_connection_destroy(viewer_connection
);
1982 g_free(validate_error
);
1988 bt_component_class_query_method_status
lttng_live_query_support_info(
1989 const bt_value
*params
, const bt_value
**result
,
1990 bt_self_component_class
*self_comp_class
,
1991 bt_logging_level log_level
)
1993 bt_component_class_query_method_status status
=
1994 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1995 const bt_value
*input_type_value
;
1996 const bt_value
*input_value
;
1998 struct bt_common_lttng_live_url_parts parts
= { 0 };
2000 /* Used by the logging macros */
2001 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
2004 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
2006 if (!input_type_value
) {
2007 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2008 "Missing expected `type` parameter.");
2012 if (!bt_value_is_string(input_type_value
)) {
2013 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2014 "`type` parameter is not a string value.");
2018 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
2019 /* We don't handle file system paths */
2023 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
2025 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2026 "Missing expected `input` parameter.");
2030 if (!bt_value_is_string(input_value
)) {
2031 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
2032 "`input` parameter is not a string value.");
2036 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
2038 if (parts
.session_name
) {
2040 * Looks pretty much like an LTTng live URL: we got the
2041 * session name part, which forms a complete URL.
2047 *result
= bt_value_real_create_init(weight
);
2049 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
2057 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
2060 BT_ASSERT(!*result
);
2063 bt_common_destroy_lttng_live_url_parts(&parts
);
2068 bt_component_class_query_method_status
lttng_live_query(
2069 bt_self_component_class_source
*comp_class
,
2070 bt_private_query_executor
*priv_query_exec
,
2071 const char *object
, const bt_value
*params
,
2072 __attribute__((unused
)) void *method_data
,
2073 const bt_value
**result
)
2075 bt_component_class_query_method_status status
=
2076 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2077 bt_self_component
*self_comp
= NULL
;
2078 bt_self_component_class
*self_comp_class
=
2079 bt_self_component_class_source_as_self_component_class(comp_class
);
2080 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2081 bt_private_query_executor_as_query_executor_const(
2084 if (strcmp(object
, "sessions") == 0) {
2085 status
= lttng_live_query_list_sessions(params
, result
,
2086 self_comp_class
, log_level
);
2087 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2088 status
= lttng_live_query_support_info(params
, result
,
2089 self_comp_class
, log_level
);
2091 BT_COMP_LOGI("Unknown query object `%s`", object
);
2092 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2101 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2106 if (lttng_live
->params
.url
) {
2107 g_string_free(lttng_live
->params
.url
, TRUE
);
2113 void lttng_live_component_finalize(bt_self_component_source
*component
)
2115 void *data
= bt_self_component_get_data(
2116 bt_self_component_source_as_self_component(component
));
2121 lttng_live_component_destroy_data(data
);
2125 enum session_not_found_action
parse_session_not_found_action_param(
2126 const bt_value
*no_session_param
)
2128 enum session_not_found_action action
;
2129 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2131 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2132 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2133 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2134 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2136 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2137 action
= SESSION_NOT_FOUND_ACTION_END
;
2143 static struct bt_param_validation_value_descr inputs_elem_descr
= {
2144 .type
= BT_VALUE_TYPE_STRING
,
2147 static const char *sess_not_found_action_choices
[] = {
2148 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2149 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2150 SESS_NOT_FOUND_ACTION_END_STR
,
2153 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2154 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
2157 .element_type
= &inputs_elem_descr
,
2159 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
2160 .choices
= sess_not_found_action_choices
,
2162 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2166 bt_component_class_initialize_method_status
lttng_live_component_create(
2167 const bt_value
*params
,
2168 bt_logging_level log_level
,
2169 bt_self_component
*self_comp
,
2170 struct lttng_live_component
**component
)
2172 struct lttng_live_component
*lttng_live
= NULL
;
2173 const bt_value
*inputs_value
;
2174 const bt_value
*url_value
;
2175 const bt_value
*value
;
2177 enum bt_param_validation_status validation_status
;
2178 gchar
*validation_error
= NULL
;
2179 bt_component_class_initialize_method_status status
;
2181 validation_status
= bt_param_validation_validate(params
, params_descr
,
2183 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2184 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2186 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2187 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2188 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2192 lttng_live
= g_new0(struct lttng_live_component
, 1);
2194 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2197 lttng_live
->log_level
= log_level
;
2198 lttng_live
->self_comp
= self_comp
;
2199 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2200 lttng_live
->has_msg_iter
= false;
2203 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2205 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2206 url
= bt_value_string_get(url_value
);
2208 lttng_live
->params
.url
= g_string_new(url
);
2209 if (!lttng_live
->params
.url
) {
2210 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2214 value
= bt_value_map_borrow_entry_value_const(params
,
2215 SESS_NOT_FOUND_ACTION_PARAM
);
2217 lttng_live
->params
.sess_not_found_act
=
2218 parse_session_not_found_action_param(value
);
2220 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2221 "defaulting to `%s`.",
2222 SESS_NOT_FOUND_ACTION_PARAM
,
2223 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2224 lttng_live
->params
.sess_not_found_act
=
2225 SESSION_NOT_FOUND_ACTION_CONTINUE
;
2228 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2232 lttng_live_component_destroy_data(lttng_live
);
2235 g_free(validation_error
);
2237 *component
= lttng_live
;
2242 bt_component_class_initialize_method_status
lttng_live_component_init(
2243 bt_self_component_source
*self_comp_src
,
2244 bt_self_component_source_configuration
*config
,
2245 const bt_value
*params
,
2246 __attribute__((unused
)) void *init_method_data
)
2248 struct lttng_live_component
*lttng_live
;
2249 bt_component_class_initialize_method_status ret
;
2250 bt_self_component
*self_comp
=
2251 bt_self_component_source_as_self_component(self_comp_src
);
2252 bt_logging_level log_level
= bt_component_get_logging_level(
2253 bt_self_component_as_component(self_comp
));
2254 bt_self_component_add_port_status add_port_status
;
2256 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2257 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2261 add_port_status
= bt_self_component_source_add_output_port(
2262 self_comp_src
, "out", NULL
, NULL
);
2263 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2264 ret
= (int) add_port_status
;
2268 bt_self_component_set_data(self_comp
, lttng_live
);
2272 lttng_live_component_destroy_data(lttng_live
);