4 * Babeltrace CTF LTTng-live Client Component
6 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define BT_COMP_LOG_SELF_COMP self_comp
32 #define BT_LOG_OUTPUT_LEVEL log_level
33 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
34 #include "logging/comp-logging.h"
42 #include "common/assert.h"
43 #include <babeltrace2/babeltrace.h>
44 #include "compat/compiler.h"
45 #include <babeltrace2/types.h>
47 #include "plugins/common/muxing/muxing.h"
48 #include "plugins/common/param-validation/param-validation.h"
50 #include "data-stream.h"
52 #include "lttng-live.h"
54 #define MAX_QUERY_SIZE (256*1024)
55 #define URL_PARAM "url"
56 #define INPUTS_PARAM "inputs"
57 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
58 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
59 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
60 #define SESS_NOT_FOUND_ACTION_END_STR "end"
62 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
65 const char *lttng_live_iterator_status_string(
66 enum lttng_live_iterator_status status
)
69 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
70 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
71 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
72 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
73 case LTTNG_LIVE_ITERATOR_STATUS_END
:
74 return "LTTNG_LIVE_ITERATOR_STATUS_END";
75 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
76 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
77 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
78 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
79 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
80 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
81 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
82 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
83 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
84 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
91 const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
94 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
95 return "ACTIVE_NO_DATA";
96 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
97 return "QUIESCENT_NO_DATA";
98 case LTTNG_LIVE_STREAM_QUIESCENT
:
100 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
101 return "ACTIVE_DATA";
102 case LTTNG_LIVE_STREAM_EOF
:
109 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
111 BT_COMP_LOGD("Live stream iterator state=%s, last-inact-ts=%" PRId64 \
112 ", curr-inact-ts %" PRId64, \
113 lttng_live_stream_state_string(live_stream_iter->state), \
114 live_stream_iter->last_inactivity_ts, \
115 live_stream_iter->current_inactivity_ts); \
119 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
128 ret
= bt_self_message_iterator_is_interrupted(
129 msg_iter
->self_msg_iter
);
136 struct lttng_live_trace
*lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
,
140 struct lttng_live_trace
*ret_trace
= NULL
;
142 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
143 struct lttng_live_trace
*trace
=
144 g_ptr_array_index(session
->traces
, trace_idx
);
145 if (trace
->id
== trace_id
) {
156 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
158 bt_logging_level log_level
= trace
->log_level
;
159 bt_self_component
*self_comp
= trace
->self_comp
;
161 BT_COMP_LOGD("Destroying live trace: trace-id=%"PRIu64
, trace
->id
);
163 BT_ASSERT(trace
->stream_iterators
);
164 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
166 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
167 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
169 lttng_live_metadata_fini(trace
);
174 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
177 struct lttng_live_trace
*trace
= NULL
;
178 bt_logging_level log_level
= session
->log_level
;
179 bt_self_component
*self_comp
= session
->self_comp
;
181 BT_COMP_LOGD("Creating live trace: "
182 "session-id=%"PRIu64
", trace-id=%"PRIu64
,
183 session
->id
, trace_id
);
184 trace
= g_new0(struct lttng_live_trace
, 1);
186 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
187 "Failed to allocate live trace");
190 trace
->log_level
= session
->log_level
;
191 trace
->self_comp
= session
->self_comp
;
192 trace
->session
= session
;
193 trace
->id
= trace_id
;
194 trace
->trace_class
= NULL
;
196 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
197 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
198 BT_ASSERT(trace
->stream_iterators
);
199 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
200 g_ptr_array_add(session
->traces
, trace
);
211 struct lttng_live_trace
*lttng_live_session_borrow_or_create_trace_by_id(
212 struct lttng_live_session
*session
, uint64_t trace_id
)
214 struct lttng_live_trace
*trace
;
216 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
221 /* The session is the owner of the newly created trace. */
222 trace
= lttng_live_create_trace(session
, trace_id
);
229 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
230 uint64_t session_id
, const char *hostname
,
231 const char *session_name
)
234 struct lttng_live_session
*session
;
235 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
236 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
238 BT_COMP_LOGD("Adding live session: "
239 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
240 session_id
, hostname
, session_name
);
242 session
= g_new0(struct lttng_live_session
, 1);
244 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
245 "Failed to allocate live session");
249 session
->log_level
= lttng_live_msg_iter
->log_level
;
250 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
251 session
->id
= session_id
;
252 session
->traces
= g_ptr_array_new_with_free_func(
253 (GDestroyNotify
) lttng_live_destroy_trace
);
254 BT_ASSERT(session
->traces
);
255 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
256 session
->new_streams_needed
= true;
257 session
->hostname
= g_string_new(hostname
);
258 BT_ASSERT(session
->hostname
);
260 session
->session_name
= g_string_new(session_name
);
261 BT_ASSERT(session
->session_name
);
263 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
273 void lttng_live_destroy_session(struct lttng_live_session
*session
)
275 bt_logging_level log_level
;
276 bt_self_component
*self_comp
;
282 log_level
= session
->log_level
;
283 self_comp
= session
->self_comp
;
284 BT_COMP_LOGD("Destroying live session: "
285 "session-id=%"PRIu64
", session-name=\"%s\"",
286 session
->id
, session
->session_name
->str
);
287 if (session
->id
!= -1ULL) {
288 if (lttng_live_session_detach(session
)) {
289 if (!lttng_live_graph_is_canceled(
290 session
->lttng_live_msg_iter
)) {
291 /* Old relayd cannot detach sessions. */
292 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
299 if (session
->traces
) {
300 g_ptr_array_free(session
->traces
, TRUE
);
303 if (session
->hostname
) {
304 g_string_free(session
->hostname
, TRUE
);
306 if (session
->session_name
) {
307 g_string_free(session
->session_name
, TRUE
);
316 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
318 if (!lttng_live_msg_iter
) {
322 if (lttng_live_msg_iter
->sessions
) {
323 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
326 if (lttng_live_msg_iter
->viewer_connection
) {
327 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
329 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
330 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
332 /* All stream iterators must be destroyed at this point. */
333 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
334 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
336 g_free(lttng_live_msg_iter
);
343 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
345 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
347 BT_ASSERT(self_msg_iter
);
349 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
350 BT_ASSERT(lttng_live_msg_iter
);
351 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
355 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
356 struct lttng_live_stream_iterator
*lttng_live_stream
)
358 bt_logging_level log_level
= lttng_live_stream
->log_level
;
359 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
361 switch (lttng_live_stream
->state
) {
362 case LTTNG_LIVE_STREAM_QUIESCENT
:
363 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
365 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
367 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
369 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
371 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
373 case LTTNG_LIVE_STREAM_EOF
:
376 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
380 * For active no data stream, fetch next data. It can be either:
381 * - quiescent: need to put it in the prio heap at quiescent end
383 * - have data: need to wire up first event into the prio heap,
384 * - have no data on this stream at this point: need to retry (AGAIN) or
388 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
389 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
390 struct lttng_live_stream_iterator
*lttng_live_stream
)
392 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
393 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
394 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
395 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
396 struct packet_index index
;
398 if (lttng_live_stream
->trace
->metadata_stream_state
==
399 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
400 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
403 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
404 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
407 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
408 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
411 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
413 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
416 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
417 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
418 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
419 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
421 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
422 last_inact_ts
== curr_inact_ts
) {
423 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
424 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
426 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
430 lttng_live_stream
->base_offset
= index
.offset
;
431 lttng_live_stream
->offset
= index
.offset
;
432 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
434 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
435 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
441 * Creation of the message requires the ctf trace class to be created
442 * beforehand, but the live protocol gives us all streams (including metadata)
443 * at once. So we split it in three steps: getting streams, getting metadata
444 * (which creates the ctf trace class), and then creating the per-stream
448 enum lttng_live_iterator_status
lttng_live_get_session(
449 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
450 struct lttng_live_session
*session
)
452 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
453 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
454 enum lttng_live_iterator_status status
;
457 BT_COMP_LOGD("Updating all streams for session: "
458 "session-id=%"PRIu64
", session-name=\"%s\"",
459 session
->id
, session
->session_name
->str
);
461 if (!session
->attached
) {
462 enum lttng_live_viewer_status attach_status
=
463 lttng_live_session_attach(session
,
464 lttng_live_msg_iter
->self_msg_iter
);
465 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
466 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
468 * Clear any causes appended in
469 * `lttng_live_attach_session()` as we want to
470 * return gracefully since the graph was
473 bt_current_thread_clear_error();
474 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
476 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
477 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
478 "Error attaching to LTTng live session");
484 status
= lttng_live_session_get_new_streams(session
,
485 lttng_live_msg_iter
->self_msg_iter
);
486 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
487 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
491 while (trace_idx
< session
->traces
->len
) {
492 struct lttng_live_trace
*trace
=
493 g_ptr_array_index(session
->traces
, trace_idx
);
495 status
= lttng_live_metadata_update(trace
);
497 case LTTNG_LIVE_ITERATOR_STATUS_END
:
498 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
501 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
502 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
505 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
506 "Error updating trace metadata: "
507 "stream-iter-status=%s, trace-id=%"PRIu64
,
508 lttng_live_iterator_status_string(status
),
513 status
= lttng_live_lazy_msg_init(session
,
514 lttng_live_msg_iter
->self_msg_iter
);
521 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
523 uint64_t session_idx
, trace_idx
;
525 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
527 struct lttng_live_session
*session
=
528 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
529 session
->new_streams_needed
= true;
530 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
532 struct lttng_live_trace
*trace
=
533 g_ptr_array_index(session
->traces
, trace_idx
);
535 BT_ASSERT(trace
->metadata_stream_state
!=
536 LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
538 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
544 enum lttng_live_iterator_status
545 lttng_live_iterator_handle_new_streams_and_metadata(
546 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
548 enum lttng_live_iterator_status status
;
549 enum lttng_live_viewer_status viewer_status
;
550 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
551 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
552 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
553 struct lttng_live_session
*session
;
554 enum session_not_found_action sess_not_found_act
=
555 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
557 BT_COMP_LOGD("Update data and metadata of all sessions"
558 "live-msg-iter-addr=%p", lttng_live_msg_iter
);
560 * In a remotely distant future, we could add a "new
561 * session" flag to the protocol, which would tell us that we
562 * need to query for new sessions even though we have sessions
565 if (lttng_live_msg_iter
->sessions
->len
== 0) {
566 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
567 BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter");
568 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
571 BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
573 * Retry to create a viewer session for the requested
576 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
577 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
578 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
579 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
580 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
581 "Error creating LTTng live viewer session");
582 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
583 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
592 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
594 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
596 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
598 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
600 case LTTNG_LIVE_ITERATOR_STATUS_END
:
601 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
606 if (!session
->closed
) {
607 nr_sessions_opened
++;
611 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
612 nr_sessions_opened
== 0) {
613 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
615 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
623 enum lttng_live_iterator_status
emit_inactivity_message(
624 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
625 struct lttng_live_stream_iterator
*stream_iter
,
626 const bt_message
**message
, uint64_t timestamp
)
628 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
629 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
630 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
631 bt_message
*msg
= NULL
;
633 BT_ASSERT(stream_iter
->trace
->clock_class
);
635 msg
= bt_message_message_iterator_inactivity_create(
636 lttng_live_msg_iter
->self_msg_iter
,
637 stream_iter
->trace
->clock_class
, timestamp
);
639 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
640 "Error emitting message iterator inactivity message");
649 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
650 bt_message_put_ref(msg
);
655 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
656 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
657 struct lttng_live_stream_iterator
*lttng_live_stream
,
658 const bt_message
**message
)
660 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
662 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
663 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
666 if (lttng_live_stream
->current_inactivity_ts
==
667 lttng_live_stream
->last_inactivity_ts
) {
668 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
669 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
673 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
674 message
, lttng_live_stream
->current_inactivity_ts
);
676 lttng_live_stream
->last_inactivity_ts
=
677 lttng_live_stream
->current_inactivity_ts
;
683 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
684 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
685 const bt_message
*msg
, int64_t last_msg_ts_ns
,
688 const bt_clock_class
*clock_class
= NULL
;
689 const bt_clock_snapshot
*clock_snapshot
= NULL
;
691 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
692 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
695 BT_ASSERT_DBG(ts_ns
);
697 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
698 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
701 switch (bt_message_get_type(msg
)) {
702 case BT_MESSAGE_TYPE_EVENT
:
703 clock_class
= bt_message_event_borrow_stream_class_default_clock_class_const(
705 BT_ASSERT_DBG(clock_class
);
707 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
710 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
711 clock_class
= bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
713 BT_ASSERT(clock_class
);
715 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
718 case BT_MESSAGE_TYPE_PACKET_END
:
719 clock_class
= bt_message_packet_end_borrow_stream_class_default_clock_class_const(
721 BT_ASSERT(clock_class
);
723 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
726 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
727 clock_class
= bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
729 BT_ASSERT(clock_class
);
731 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
734 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
735 clock_class
= bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
737 BT_ASSERT(clock_class
);
739 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
742 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
743 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
747 /* All the other messages have a higher priority */
748 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
749 *ts_ns
= last_msg_ts_ns
;
753 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
754 BT_ASSERT_DBG(clock_class
);
756 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
758 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
759 "Cannot get nanoseconds from Epoch of clock snapshot: "
760 "clock-snapshot-addr=%p", clock_snapshot
);
771 BT_COMP_LOGD("Found message's timestamp: "
772 "iter-data-addr=%p, msg-addr=%p, "
773 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
774 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
781 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
782 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
783 struct lttng_live_stream_iterator
*lttng_live_stream
,
784 const bt_message
**message
)
786 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
787 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
788 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
789 enum ctf_msg_iter_status status
;
790 uint64_t session_idx
, trace_idx
;
792 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
794 struct lttng_live_session
*session
=
795 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
797 if (session
->new_streams_needed
) {
798 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
801 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
803 struct lttng_live_trace
*trace
=
804 g_ptr_array_index(session
->traces
, trace_idx
);
805 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
806 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
812 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
813 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
814 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
815 "Invalid state of live stream iterator"
816 "stream-iter-status=%s",
817 lttng_live_stream_state_string(lttng_live_stream
->state
));
821 status
= ctf_msg_iter_get_next_message(
822 lttng_live_stream
->msg_iter
, message
);
824 case CTF_MSG_ITER_STATUS_EOF
:
825 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
827 case CTF_MSG_ITER_STATUS_OK
:
828 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
830 case CTF_MSG_ITER_STATUS_AGAIN
:
832 * Continue immediately (end of packet). The next
833 * get_index may return AGAIN to delay the following
836 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
838 case CTF_MSG_ITER_STATUS_ERROR
:
840 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
841 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
842 "CTF message iterator failed to get next message: "
843 "msg-iter=%p, msg-iter-status=%s",
844 lttng_live_stream
->msg_iter
,
845 ctf_msg_iter_status_string(status
));
854 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
855 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
856 struct lttng_live_stream_iterator
*stream_iter
,
857 const bt_message
**curr_msg
)
859 enum lttng_live_iterator_status live_status
=
860 LTTNG_LIVE_ITERATOR_STATUS_OK
;
861 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
862 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
864 * The viewer has hung up on us so we are closing the stream. The
865 * `ctf_msg_iter` should simply realize that it needs to close the
866 * stream properly by emitting the necessary stream end message.
868 enum ctf_msg_iter_status status
= ctf_msg_iter_get_next_message(
869 stream_iter
->msg_iter
, curr_msg
);
871 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
872 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
873 "Error getting the next message from CTF message iterator");
874 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
876 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
877 BT_COMP_LOGI("Reached the end of the live stream iterator.");
878 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
882 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
890 * handle_no_data_streams()
892 * - for each ACTIVE_NO_DATA stream:
893 * - query relayd for stream data, or quiescence info.
894 * - if need metadata, get metadata, goto retry.
895 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
896 * - if quiescent, move to QUIESCENT streams
897 * - if fetched data, move to ACTIVE_DATA streams
898 * (at this point each stream either has data, or is quiescent)
902 * handle_new_streams_and_metadata()
903 * - query relayd for known streams, add them as ACTIVE_NO_DATA
904 * - query relayd for metadata
906 * call handle_active_no_data_streams()
908 * handle_quiescent_streams()
909 * - if at least one stream is ACTIVE_DATA:
910 * - peek stream event with lowest timestamp -> next_ts
911 * - for each quiescent stream
912 * - if next_ts >= quiescent end
913 * - set state to ACTIVE_NO_DATA
915 * - for each quiescent stream
916 * - set state to ACTIVE_NO_DATA
918 * call handle_active_no_data_streams()
920 * handle_active_data_streams()
921 * - if at least one stream is ACTIVE_DATA:
922 * - get stream event with lowest timestamp from heap
923 * - make that stream event the current message.
924 * - move this stream heap position to its next event
925 * - if we need to fetch data from relayd, move
926 * stream to ACTIVE_NO_DATA.
930 * end criterion: ctrl-c on client. If relayd exits or the session
931 * closes on the relay daemon side, we keep on waiting for streams.
932 * Eventually handle --end timestamp (also an end criterion).
934 * When disconnected from relayd: try to re-connect endlessly.
937 enum lttng_live_iterator_status
lttng_live_iterator_next_msg_on_stream(
938 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
939 struct lttng_live_stream_iterator
*stream_iter
,
940 const bt_message
**curr_msg
)
942 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
943 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
944 enum lttng_live_iterator_status live_status
;
946 BT_COMP_LOGD("Finding the next message for stream iterator: "
947 "stream-name=\"%s\"", stream_iter
->name
->str
);
949 if (stream_iter
->has_stream_hung_up
) {
951 * The stream has hung up and the stream was properly closed
952 * during the last call to the current function. Return _END
953 * status now so that this stream iterator is removed for the
954 * stream iterator list.
956 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
961 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
962 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
963 lttng_live_msg_iter
);
964 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
967 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
968 lttng_live_msg_iter
, stream_iter
);
970 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
971 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
973 * We overwrite `live_status` since `curr_msg` is
974 * likely set to a valid message in this function.
976 live_status
= lttng_live_iterator_close_stream(
977 lttng_live_msg_iter
, stream_iter
, curr_msg
);
981 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
982 lttng_live_msg_iter
, stream_iter
, curr_msg
);
983 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
984 BT_ASSERT(!*curr_msg
);
990 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
991 lttng_live_msg_iter
, stream_iter
, curr_msg
);
992 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
993 BT_ASSERT(!*curr_msg
);
997 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
1005 bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1007 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1009 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1010 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1014 enum lttng_live_iterator_status
adjust_discarded_packets_message(
1015 bt_self_message_iterator
*iter
,
1016 const bt_stream
*stream
,
1017 const bt_message
*msg_in
, bt_message
**msg_out
,
1018 uint64_t new_begin_ts
)
1020 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1021 enum bt_property_availability availability
;
1022 const bt_clock_snapshot
*clock_snapshot
;
1026 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1027 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1029 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1030 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1032 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1033 iter
, stream
, new_begin_ts
, end_ts
);
1035 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1039 bt_message_discarded_packets_set_count(*msg_out
, count
);
1045 enum lttng_live_iterator_status
adjust_discarded_events_message(
1046 bt_self_message_iterator
*iter
,
1047 const bt_stream
*stream
,
1048 const bt_message
*msg_in
, bt_message
**msg_out
,
1049 uint64_t new_begin_ts
)
1051 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1052 enum bt_property_availability availability
;
1053 const bt_clock_snapshot
*clock_snapshot
;
1057 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1058 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1060 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1061 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1063 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1064 iter
, stream
, new_begin_ts
, end_ts
);
1066 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1070 bt_message_discarded_events_set_count(*msg_out
, count
);
1076 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
1077 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1078 struct lttng_live_trace
*live_trace
,
1079 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1081 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1082 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1083 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1084 enum lttng_live_iterator_status stream_iter_status
;;
1085 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1086 uint64_t stream_iter_idx
;
1088 BT_ASSERT_DBG(live_trace
);
1089 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1091 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1092 "trace-id=%"PRIu64
, live_trace
->id
);
1094 * Update the current message of every stream iterators of this trace.
1095 * The current msg of every stream must have a timestamp equal or
1096 * larger than the last message returned by this iterator. We must
1097 * ensure monotonicity.
1099 stream_iter_idx
= 0;
1100 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1101 bool stream_iter_is_ended
= false;
1102 struct lttng_live_stream_iterator
*stream_iter
=
1103 g_ptr_array_index(live_trace
->stream_iterators
,
1107 * Find if there is are now current message for this stream
1110 while (!stream_iter
->current_msg
) {
1111 const bt_message
*msg
= NULL
;
1112 int64_t curr_msg_ts_ns
= INT64_MAX
;
1113 stream_iter_status
= lttng_live_iterator_next_msg_on_stream(
1114 lttng_live_msg_iter
, stream_iter
, &msg
);
1116 BT_COMP_LOGD("live stream iterator returned status :%s",
1117 lttng_live_iterator_status_string(stream_iter_status
));
1118 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1119 stream_iter_is_ended
= true;
1123 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1130 * Get the timestamp in nanoseconds from origin of this
1133 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
1134 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1138 * Check if the message of the current live stream
1139 * iterator occurred at the exact same time or after the
1140 * last message returned by this component's message
1141 * iterator. If not, we return an error.
1143 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1144 stream_iter
->current_msg
= msg
;
1145 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1147 const bt_clock_class
*clock_class
;
1148 const bt_stream_class
*stream_class
;
1149 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1150 int64_t last_inactivity_ts_ns
;
1152 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1153 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1155 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1157 stream_iter
->last_inactivity_ts
,
1158 &last_inactivity_ts_ns
);
1159 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1160 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1164 if (last_inactivity_ts_ns
> curr_msg_ts_ns
&&
1165 is_discarded_packet_or_event_message(msg
)) {
1167 * The CTF message iterator emits Discarded
1168 * Packets and Events with synthesized begin and
1169 * end timestamps from the bounds of the last
1170 * known packet and the newly decoded packet
1173 * The CTF message iterator is not aware of
1174 * stream inactivity beacons. Hence, we have
1175 * to adjust the begin timestamp of those types
1176 * of messages if a stream signaled its
1177 * inactivity up until _after_ the last known
1178 * packet's begin timestamp.
1180 * Otherwise, the monotonicity guarantee would
1183 const enum bt_message_type msg_type
=
1184 bt_message_get_type(msg
);
1185 enum lttng_live_iterator_status adjust_status
=
1186 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1187 bt_message
*adjusted_message
;
1190 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1191 adjust_status
= adjust_discarded_events_message(
1192 lttng_live_msg_iter
->self_msg_iter
,
1193 stream_iter
->stream
,
1194 msg
, &adjusted_message
,
1195 stream_iter
->last_inactivity_ts
);
1197 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1198 adjust_status
= adjust_discarded_packets_message(
1199 lttng_live_msg_iter
->self_msg_iter
,
1200 stream_iter
->stream
,
1201 msg
, &adjusted_message
,
1202 stream_iter
->last_inactivity_ts
);
1208 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1209 stream_iter_status
= adjust_status
;
1213 BT_ASSERT_DBG(adjusted_message
);
1214 stream_iter
->current_msg
= adjusted_message
;
1215 stream_iter
->current_msg_ts_ns
=
1216 last_inactivity_ts_ns
;
1219 * We received a message in the past. To ensure
1220 * monotonicity, we can't send it forward.
1222 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1223 "Message's timestamp is less than "
1224 "lttng-live's message iterator's last "
1225 "returned timestamp: "
1226 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1227 "last-msg-ts=%" PRId64
,
1228 lttng_live_msg_iter
, curr_msg_ts_ns
,
1229 lttng_live_msg_iter
->last_msg_ts_ns
);
1230 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1236 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1238 if (!stream_iter_is_ended
) {
1239 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1240 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1242 * Update the current best candidate message
1243 * for the stream iterator of this live trace
1244 * to be forwarded downstream.
1246 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1247 youngest_candidate_stream_iter
= stream_iter
;
1248 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1250 * Order the messages in an arbitrary but
1251 * deterministic way.
1253 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1254 int ret
= common_muxing_compare_messages(
1255 stream_iter
->current_msg
,
1256 youngest_candidate_stream_iter
->current_msg
);
1259 * The `youngest_candidate_stream_iter->current_msg`
1260 * should go first. Update the next
1261 * iterator and the current timestamp.
1263 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1264 youngest_candidate_stream_iter
= stream_iter
;
1265 } else if (ret
== 0) {
1267 * Unable to pick which one should go
1270 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1271 "stream-iter-addr=%p"
1272 "stream-iter-addr=%p",
1274 youngest_candidate_stream_iter
);
1281 * The live stream iterator has ended. That
1282 * iterator is removed from the array, but
1283 * there is no need to increment
1284 * stream_iter_idx as
1285 * g_ptr_array_remove_index_fast replaces the
1286 * removed element with the array's last
1289 g_ptr_array_remove_index_fast(
1290 live_trace
->stream_iterators
,
1295 if (youngest_candidate_stream_iter
) {
1296 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1297 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1300 * The only case where we don't have a candidate for this trace
1301 * is if we reached the end of all the iterators.
1303 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1304 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1308 return stream_iter_status
;
1312 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1313 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1314 struct lttng_live_session
*session
,
1315 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1317 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1318 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1319 enum lttng_live_iterator_status stream_iter_status
;
1320 uint64_t trace_idx
= 0;
1321 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1322 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1324 BT_COMP_LOGD("Finding the next stream iterator for session: "
1325 "session-id=%"PRIu64
, session
->id
);
1327 * Make sure we are attached to the session and look for new streams
1330 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1331 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1332 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1333 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1337 BT_ASSERT_DBG(session
->traces
);
1339 while (trace_idx
< session
->traces
->len
) {
1340 bool trace_is_ended
= false;
1341 struct lttng_live_stream_iterator
*stream_iter
;
1342 struct lttng_live_trace
*trace
=
1343 g_ptr_array_index(session
->traces
, trace_idx
);
1345 stream_iter_status
= next_stream_iterator_for_trace(
1346 lttng_live_msg_iter
, trace
, &stream_iter
);
1347 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1349 * All the live stream iterators for this trace are
1350 * ENDed. Remove the trace from this session.
1352 trace_is_ended
= true;
1353 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1357 if (!trace_is_ended
) {
1358 BT_ASSERT_DBG(stream_iter
);
1360 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1361 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1362 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1363 youngest_candidate_stream_iter
= stream_iter
;
1364 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1366 * Order the messages in an arbitrary but
1367 * deterministic way.
1369 int ret
= common_muxing_compare_messages(
1370 stream_iter
->current_msg
,
1371 youngest_candidate_stream_iter
->current_msg
);
1374 * The `youngest_candidate_stream_iter->current_msg`
1375 * should go first. Update the next iterator
1376 * and the current timestamp.
1378 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1379 youngest_candidate_stream_iter
= stream_iter
;
1380 } else if (ret
== 0) {
1381 /* Unable to pick which one should go first. */
1382 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1383 "stream-iter-addr=%p" "stream-iter-addr=%p",
1384 stream_iter
, youngest_candidate_stream_iter
);
1390 * trace_idx is not incremented since
1391 * g_ptr_array_remove_index_fast replaces the
1392 * element at trace_idx with the array's last element.
1394 g_ptr_array_remove_index_fast(session
->traces
,
1398 if (youngest_candidate_stream_iter
) {
1399 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1400 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1403 * The only cases where we don't have a candidate for this
1405 * 1. if we reached the end of all the iterators of all the
1406 * traces of this session,
1407 * 2. if we never had live stream iterator in the first place.
1409 * In either cases, we return END.
1411 BT_ASSERT(session
->traces
->len
== 0);
1412 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1415 return stream_iter_status
;
1419 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1423 for (i
= 0; i
< count
; i
++) {
1424 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1429 bt_message_iterator_class_next_method_status
lttng_live_msg_iter_next(
1430 bt_self_message_iterator
*self_msg_it
,
1431 bt_message_array_const msgs
, uint64_t capacity
,
1434 bt_message_iterator_class_next_method_status status
;
1435 enum lttng_live_viewer_status viewer_status
;
1436 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1437 bt_self_message_iterator_get_data(self_msg_it
);
1438 struct lttng_live_component
*lttng_live
=
1439 lttng_live_msg_iter
->lttng_live_comp
;
1440 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1441 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1442 enum lttng_live_iterator_status stream_iter_status
;
1443 uint64_t session_idx
;
1447 BT_ASSERT_DBG(lttng_live_msg_iter
);
1449 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1451 * The iterator was interrupted in a previous call to the
1452 * `_next()` method. We currently do not support generating
1453 * messages after such event. The babeltrace2 CLI should never
1454 * be running the graph after being interrupted. So this check
1455 * is to prevent other graph users from using this live
1456 * iterator in an messed up internal state.
1458 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1459 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1460 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1465 * Clear all the invalid message reference that might be left over in
1468 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1471 * If no session are exposed on the relay found at the url provided by
1472 * the user, session count will be 0. In this case, we return status
1473 * end to return gracefully.
1475 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1476 if (lttng_live
->params
.sess_not_found_act
!=
1477 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1478 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1482 * The are no more active session for this session
1483 * name. Retry to create a viewer session for the
1484 * requested session name.
1486 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1487 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1488 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1489 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1490 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1491 "Error creating LTTng live viewer session");
1492 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1493 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1502 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1503 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1507 * Here the muxing of message is done.
1509 * We need to iterate over all the streams of all the traces of all the
1510 * viewer sessions in order to get the message with the smallest
1511 * timestamp. In this case, a session is a viewer session and there is
1512 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1513 * kernel). Each viewer session can have multiple traces, for example,
1514 * 64bit UST viewer sessions could have multiple per-pid traces.
1516 * We iterate over the streams of each traces to update and see what is
1517 * their next message's timestamp. From those timestamps, we select the
1518 * message with the smallest timestamp as the best candidate message
1519 * for that trace and do the same thing across all the sessions.
1521 * We then compare the timestamp of best candidate message of all the
1522 * sessions to pick the message with the smallest timestamp and we
1525 while (*count
< capacity
) {
1526 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1527 *candidate_stream_iter
= NULL
;
1528 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1530 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1532 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1533 struct lttng_live_session
*session
=
1534 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1537 /* Find the best candidate message to send downstream. */
1538 stream_iter_status
= next_stream_iterator_for_session(
1539 lttng_live_msg_iter
, session
,
1540 &candidate_stream_iter
);
1542 /* If we receive an END status, it means that either:
1543 * - Those traces never had active streams (UST with no
1544 * data produced yet),
1545 * - All live stream iterators have ENDed.*/
1546 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1547 if (session
->closed
&& session
->traces
->len
== 0) {
1549 * Remove the session from the list.
1550 * session_idx is not modified since
1551 * g_ptr_array_remove_index_fast
1552 * replaces the the removed element with
1553 * the array's last element.
1555 g_ptr_array_remove_index_fast(
1556 lttng_live_msg_iter
->sessions
,
1564 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1568 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1569 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1570 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1571 youngest_stream_iter
= candidate_stream_iter
;
1572 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1574 * The currently selected message to be sent
1575 * downstream next has the exact same timestamp
1576 * that of the current candidate message. We
1577 * must break the tie in a predictable manner.
1579 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
1581 * Order the messages in an arbitrary but
1582 * deterministic way.
1584 int ret
= common_muxing_compare_messages(
1585 candidate_stream_iter
->current_msg
,
1586 youngest_stream_iter
->current_msg
);
1589 * The `candidate_stream_iter->current_msg`
1590 * should go first. Update the next
1591 * iterator and the current timestamp.
1593 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1594 youngest_stream_iter
= candidate_stream_iter
;
1595 } else if (ret
== 0) {
1596 /* Unable to pick which one should go first. */
1597 BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1598 "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p",
1599 youngest_stream_iter
, candidate_stream_iter
);
1606 if (!youngest_stream_iter
) {
1607 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1611 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1612 /* Ensure monotonicity. */
1613 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1614 youngest_stream_iter
->current_msg_ts_ns
);
1617 * Insert the next message to the message batch. This will set
1618 * stream iterator current messsage to NULL so that next time
1619 * we fetch the next message of that stream iterator
1621 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1624 /* Update the last timestamp in nanoseconds sent downstream. */
1625 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1626 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1628 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1632 switch (stream_iter_status
) {
1633 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1634 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1636 * If we gathered messages, return _OK even if the graph was
1637 * interrupted. This allows for the components downstream to at
1638 * least get the thoses messages. If the graph was indeed
1639 * interrupted there should not be another _next() call as the
1640 * application will tear down the graph. This component class
1641 * doesn't support restarting after an interruption.
1644 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1646 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1649 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1650 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1652 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1653 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1654 "Memory error preparing the next batch of messages: "
1655 "live-iter-status=%s",
1656 lttng_live_iterator_status_string(stream_iter_status
));
1657 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1659 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1660 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1661 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1662 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1663 "Error preparing the next batch of messages: "
1664 "live-iter-status=%s",
1665 lttng_live_iterator_status_string(stream_iter_status
));
1667 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1668 /* Put all existing messages on error. */
1669 put_messages(msgs
, *count
);
1680 struct lttng_live_msg_iter
*lttng_live_msg_iter_create(
1681 struct lttng_live_component
*lttng_live_comp
,
1682 bt_self_message_iterator
*self_msg_it
)
1684 bt_self_component
*self_comp
= lttng_live_comp
->self_comp
;
1685 bt_logging_level log_level
= lttng_live_comp
->log_level
;
1687 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1688 g_new0(struct lttng_live_msg_iter
, 1);
1689 if (!lttng_live_msg_iter
) {
1690 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1691 "Failed to allocate lttng_live_msg_iter");
1695 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1696 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1697 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1698 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1700 lttng_live_msg_iter
->active_stream_iter
= 0;
1701 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1702 lttng_live_msg_iter
->was_interrupted
= false;
1704 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1705 (GDestroyNotify
) lttng_live_destroy_session
);
1706 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1709 return lttng_live_msg_iter
;
1714 bt_message_iterator_class_initialize_method_status
lttng_live_msg_iter_init(
1715 bt_self_message_iterator
*self_msg_it
,
1716 bt_self_message_iterator_configuration
*config
,
1717 bt_self_component_port_output
*self_port
)
1719 bt_message_iterator_class_initialize_method_status status
;
1720 struct lttng_live_component
*lttng_live
;
1721 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1722 enum lttng_live_viewer_status viewer_status
;
1723 bt_logging_level log_level
;
1724 bt_self_component
*self_comp
=
1725 bt_self_message_iterator_borrow_component(self_msg_it
);
1727 lttng_live
= bt_self_component_get_data(self_comp
);
1728 log_level
= lttng_live
->log_level
;
1729 self_comp
= lttng_live
->self_comp
;
1732 /* There can be only one downstream iterator at the same time. */
1733 BT_ASSERT(!lttng_live
->has_msg_iter
);
1734 lttng_live
->has_msg_iter
= true;
1736 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
,
1738 if (!lttng_live_msg_iter
) {
1739 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1740 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1741 "Failed to create lttng_live_msg_iter");
1745 viewer_status
= live_viewer_connection_create(self_comp
, NULL
,
1746 log_level
, lttng_live
->params
.url
->str
, false,
1747 lttng_live_msg_iter
, <tng_live_msg_iter
->viewer_connection
);
1748 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1749 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1750 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1751 "Failed to create viewer connection");
1752 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1754 * Interruption in the _iter_init() method is not
1755 * supported. Return an error.
1757 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1758 "Interrupted while creating viewer connection");
1763 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1764 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1765 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1766 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1767 "Failed to create viewer session");
1768 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1770 * Interruption in the _iter_init() method is not
1771 * supported. Return an error.
1773 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1774 "Interrupted when creating viewer session");
1779 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1780 switch (lttng_live
->params
.sess_not_found_act
) {
1781 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1782 BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of "
1783 "%s=\"%s\" component parameter: url=\"%s\"",
1784 SESS_NOT_FOUND_ACTION_PARAM
,
1785 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1786 lttng_live
->params
.url
->str
);
1788 case SESSION_NOT_FOUND_ACTION_FAIL
:
1789 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1790 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1791 "component parameter: url =\"%s\"",
1792 SESS_NOT_FOUND_ACTION_PARAM
,
1793 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1794 lttng_live
->params
.url
->str
);
1796 case SESSION_NOT_FOUND_ACTION_END
:
1797 BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1798 "call because of %s=\"%s\" component parameter: "
1799 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1800 SESS_NOT_FOUND_ACTION_END_STR
,
1801 lttng_live
->params
.url
->str
);
1808 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1809 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1813 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1814 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1819 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1820 { URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
} },
1821 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1825 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1826 const bt_value
*params
, const bt_value
**result
,
1827 bt_self_component_class
*self_comp_class
,
1828 bt_logging_level log_level
)
1830 bt_component_class_query_method_status status
;
1831 const bt_value
*url_value
= NULL
;
1833 struct live_viewer_connection
*viewer_connection
= NULL
;
1834 enum lttng_live_viewer_status viewer_status
;
1835 enum bt_param_validation_status validation_status
;
1836 gchar
*validate_error
= NULL
;
1838 validation_status
= bt_param_validation_validate(params
,
1839 list_sessions_params
, &validate_error
);
1840 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1841 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1843 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1844 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1845 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s",
1850 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1851 url
= bt_value_string_get(url_value
);
1853 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
,
1854 log_level
, url
, true, NULL
, &viewer_connection
);
1855 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1856 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1857 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1858 "Failed to create viewer connection");
1859 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1860 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1861 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1868 status
= live_viewer_connection_list_sessions(viewer_connection
,
1870 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1871 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1872 "Failed to list viewer sessions");
1879 BT_VALUE_PUT_REF_AND_RESET(*result
);
1882 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1886 if (viewer_connection
) {
1887 live_viewer_connection_destroy(viewer_connection
);
1890 g_free(validate_error
);
1896 bt_component_class_query_method_status
lttng_live_query_support_info(
1897 const bt_value
*params
, const bt_value
**result
,
1898 bt_self_component_class
*self_comp_class
,
1899 bt_logging_level log_level
)
1901 bt_component_class_query_method_status status
=
1902 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1903 const bt_value
*input_type_value
;
1904 const bt_value
*input_value
;
1906 struct bt_common_lttng_live_url_parts parts
= { 0 };
1908 /* Used by the logging macros */
1909 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1912 input_type_value
= bt_value_map_borrow_entry_value_const(params
,
1914 if (!input_type_value
) {
1915 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1916 "Missing expected `type` parameter.");
1920 if (!bt_value_is_string(input_type_value
)) {
1921 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1922 "`type` parameter is not a string value.");
1926 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1927 /* We don't handle file system paths */
1931 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1933 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1934 "Missing expected `input` parameter.");
1938 if (!bt_value_is_string(input_value
)) {
1939 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1940 "`input` parameter is not a string value.");
1944 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
),
1946 if (parts
.session_name
) {
1948 * Looks pretty much like an LTTng live URL: we got the
1949 * session name part, which forms a complete URL.
1955 *result
= bt_value_real_create_init(weight
);
1957 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1965 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1968 BT_ASSERT(!*result
);
1971 bt_common_destroy_lttng_live_url_parts(&parts
);
1976 bt_component_class_query_method_status
lttng_live_query(
1977 bt_self_component_class_source
*comp_class
,
1978 bt_private_query_executor
*priv_query_exec
,
1979 const char *object
, const bt_value
*params
,
1980 __attribute__((unused
)) void *method_data
,
1981 const bt_value
**result
)
1983 bt_component_class_query_method_status status
=
1984 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1985 bt_self_component
*self_comp
= NULL
;
1986 bt_self_component_class
*self_comp_class
=
1987 bt_self_component_class_source_as_self_component_class(comp_class
);
1988 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1989 bt_private_query_executor_as_query_executor_const(
1992 if (strcmp(object
, "sessions") == 0) {
1993 status
= lttng_live_query_list_sessions(params
, result
,
1994 self_comp_class
, log_level
);
1995 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
1996 status
= lttng_live_query_support_info(params
, result
,
1997 self_comp_class
, log_level
);
1999 BT_COMP_LOGI("Unknown query object `%s`", object
);
2000 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2009 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2014 if (lttng_live
->params
.url
) {
2015 g_string_free(lttng_live
->params
.url
, TRUE
);
2021 void lttng_live_component_finalize(bt_self_component_source
*component
)
2023 void *data
= bt_self_component_get_data(
2024 bt_self_component_source_as_self_component(component
));
2029 lttng_live_component_destroy_data(data
);
2033 enum session_not_found_action
parse_session_not_found_action_param(
2034 const bt_value
*no_session_param
)
2036 enum session_not_found_action action
;
2037 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2039 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2040 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2041 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2042 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2044 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2045 action
= SESSION_NOT_FOUND_ACTION_END
;
2051 static struct bt_param_validation_value_descr inputs_elem_descr
= {
2052 .type
= BT_VALUE_TYPE_STRING
,
2055 static const char *sess_not_found_action_choices
[] = {
2056 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2057 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2058 SESS_NOT_FOUND_ACTION_END_STR
,
2061 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2062 { INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
2065 .element_type
= &inputs_elem_descr
,
2067 { SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
2068 .choices
= sess_not_found_action_choices
,
2070 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2074 bt_component_class_initialize_method_status
lttng_live_component_create(
2075 const bt_value
*params
,
2076 bt_logging_level log_level
,
2077 bt_self_component
*self_comp
,
2078 struct lttng_live_component
**component
)
2080 struct lttng_live_component
*lttng_live
= NULL
;
2081 const bt_value
*inputs_value
;
2082 const bt_value
*url_value
;
2083 const bt_value
*value
;
2085 enum bt_param_validation_status validation_status
;
2086 gchar
*validation_error
= NULL
;
2087 bt_component_class_initialize_method_status status
;
2089 validation_status
= bt_param_validation_validate(params
, params_descr
,
2091 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2092 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2094 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2095 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2096 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2100 lttng_live
= g_new0(struct lttng_live_component
, 1);
2102 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2105 lttng_live
->log_level
= log_level
;
2106 lttng_live
->self_comp
= self_comp
;
2107 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2108 lttng_live
->has_msg_iter
= false;
2111 bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2113 bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2114 url
= bt_value_string_get(url_value
);
2116 lttng_live
->params
.url
= g_string_new(url
);
2117 if (!lttng_live
->params
.url
) {
2118 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2122 value
= bt_value_map_borrow_entry_value_const(params
,
2123 SESS_NOT_FOUND_ACTION_PARAM
);
2125 lttng_live
->params
.sess_not_found_act
=
2126 parse_session_not_found_action_param(value
);
2128 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2129 "defaulting to `%s`.",
2130 SESS_NOT_FOUND_ACTION_PARAM
,
2131 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2132 lttng_live
->params
.sess_not_found_act
=
2133 SESSION_NOT_FOUND_ACTION_CONTINUE
;
2136 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2140 lttng_live_component_destroy_data(lttng_live
);
2143 g_free(validation_error
);
2145 *component
= lttng_live
;
2150 bt_component_class_initialize_method_status
lttng_live_component_init(
2151 bt_self_component_source
*self_comp_src
,
2152 bt_self_component_source_configuration
*config
,
2153 const bt_value
*params
,
2154 __attribute__((unused
)) void *init_method_data
)
2156 struct lttng_live_component
*lttng_live
;
2157 bt_component_class_initialize_method_status ret
;
2158 bt_self_component
*self_comp
=
2159 bt_self_component_source_as_self_component(self_comp_src
);
2160 bt_logging_level log_level
= bt_component_get_logging_level(
2161 bt_self_component_as_component(self_comp
));
2162 bt_self_component_add_port_status add_port_status
;
2164 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2165 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2169 add_port_status
= bt_self_component_source_add_output_port(
2170 self_comp_src
, "out", NULL
, NULL
);
2171 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2172 ret
= (int) add_port_status
;
2176 bt_self_component_set_data(self_comp
, lttng_live
);
2180 lttng_live_component_destroy_data(lttng_live
);