2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Babeltrace CTF LTTng-live Client Component
15 #include <babeltrace2/babeltrace.h>
17 #define BT_COMP_LOG_SELF_COMP self_comp
18 #define BT_LOG_OUTPUT_LEVEL log_level
19 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
20 #include "logging/comp-logging.h"
22 #include "common/assert.h"
24 #include "plugins/common/muxing/muxing.h"
25 #include "plugins/common/param-validation/param-validation.h"
27 #include "data-stream.hpp"
28 #include "lttng-live.hpp"
29 #include "metadata.hpp"
31 #define MAX_QUERY_SIZE (256 * 1024)
32 #define URL_PARAM "url"
33 #define INPUTS_PARAM "inputs"
34 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
35 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
36 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
37 #define SESS_NOT_FOUND_ACTION_END_STR "end"
39 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ##__VA_ARGS__)
41 static const char *lttng_live_iterator_status_string(enum lttng_live_iterator_status status
)
44 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
45 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
46 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
47 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
48 case LTTNG_LIVE_ITERATOR_STATUS_END
:
49 return "LTTNG_LIVE_ITERATOR_STATUS_END";
50 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
51 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
52 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
53 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
54 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
55 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
56 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
57 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
58 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
59 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
65 static const char *lttng_live_stream_state_string(enum lttng_live_stream_state state
)
68 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
69 return "ACTIVE_NO_DATA";
70 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
71 return "QUIESCENT_NO_DATA";
72 case LTTNG_LIVE_STREAM_QUIESCENT
:
74 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
76 case LTTNG_LIVE_STREAM_EOF
:
83 void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator
*stream_iter
,
84 enum lttng_live_stream_state new_state
)
86 bt_self_component
*self_comp
= stream_iter
->self_comp
;
87 bt_logging_level log_level
= stream_iter
->log_level
;
89 BT_COMP_LOGD("Setting live stream iterator state: viewer-stream-id=%" PRIu64
90 ", old-state=%s, new-state=%s",
91 stream_iter
->viewer_stream_id
, lttng_live_stream_state_string(stream_iter
->state
),
92 lttng_live_stream_state_string(new_state
));
94 stream_iter
->state
= new_state
;
97 #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \
99 BT_COMP_LOGD("Live stream iterator state=%s, " \
100 "last-inact-ts-is-set=%d, last-inact-ts-value=%" PRId64 ", " \
101 "curr-inact-ts=%" PRId64, \
102 lttng_live_stream_state_string(live_stream_iter->state), \
103 live_stream_iter->last_inactivity_ts.is_set, \
104 live_stream_iter->last_inactivity_ts.value, \
105 live_stream_iter->current_inactivity_ts); \
108 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
117 ret
= bt_self_message_iterator_is_interrupted(msg_iter
->self_msg_iter
);
123 static struct lttng_live_trace
*
124 lttng_live_session_borrow_trace_by_id(struct lttng_live_session
*session
, uint64_t trace_id
)
127 struct lttng_live_trace
*ret_trace
= NULL
;
129 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
130 struct lttng_live_trace
*trace
=
131 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
132 if (trace
->id
== trace_id
) {
142 static void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
144 bt_logging_level log_level
= trace
->log_level
;
145 bt_self_component
*self_comp
= trace
->self_comp
;
147 BT_COMP_LOGD("Destroying live trace: trace-id=%" PRIu64
, trace
->id
);
149 BT_ASSERT(trace
->stream_iterators
);
150 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
152 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
153 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
155 lttng_live_metadata_fini(trace
);
159 static struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
162 bt_logging_level log_level
= session
->log_level
;
163 bt_self_component
*self_comp
= session
->self_comp
;
165 BT_COMP_LOGD("Creating live trace: "
166 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
167 session
->id
, trace_id
);
169 lttng_live_trace
*trace
= new lttng_live_trace
;
170 trace
->log_level
= session
->log_level
;
171 trace
->self_comp
= session
->self_comp
;
172 trace
->session
= session
;
173 trace
->id
= trace_id
;
174 trace
->trace_class
= NULL
;
176 trace
->stream_iterators
=
177 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_stream_iterator_destroy
);
178 BT_ASSERT(trace
->stream_iterators
);
179 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
180 g_ptr_array_add(session
->traces
, trace
);
185 struct lttng_live_trace
*
186 lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session
*session
,
189 struct lttng_live_trace
*trace
;
191 trace
= lttng_live_session_borrow_trace_by_id(session
, trace_id
);
196 /* The session is the owner of the newly created trace. */
197 trace
= lttng_live_create_trace(session
, trace_id
);
203 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
, uint64_t session_id
,
204 const char *hostname
, const char *session_name
)
206 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
207 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
209 BT_COMP_LOGD("Adding live session: "
210 "session-id=%" PRIu64
", hostname=\"%s\" session-name=\"%s\"",
211 session_id
, hostname
, session_name
);
213 lttng_live_session
*session
= new lttng_live_session
;
214 session
->log_level
= lttng_live_msg_iter
->log_level
;
215 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
216 session
->id
= session_id
;
217 session
->traces
= g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_trace
);
218 BT_ASSERT(session
->traces
);
219 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
220 session
->new_streams_needed
= true;
221 session
->hostname
= g_string_new(hostname
);
222 BT_ASSERT(session
->hostname
);
224 session
->session_name
= g_string_new(session_name
);
225 BT_ASSERT(session
->session_name
);
227 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
232 static void lttng_live_destroy_session(struct lttng_live_session
*session
)
234 bt_logging_level log_level
;
235 bt_self_component
*self_comp
;
241 log_level
= session
->log_level
;
242 self_comp
= session
->self_comp
;
243 BT_COMP_LOGD("Destroying live session: "
244 "session-id=%" PRIu64
", session-name=\"%s\"",
245 session
->id
, session
->session_name
->str
);
246 if (session
->id
!= -1ULL) {
247 if (lttng_live_session_detach(session
)) {
248 if (!lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
249 /* Old relayd cannot detach sessions. */
250 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
, session
->id
);
256 if (session
->traces
) {
257 g_ptr_array_free(session
->traces
, TRUE
);
260 if (session
->hostname
) {
261 g_string_free(session
->hostname
, TRUE
);
264 if (session
->session_name
) {
265 g_string_free(session
->session_name
, TRUE
);
274 static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
276 if (!lttng_live_msg_iter
) {
280 if (lttng_live_msg_iter
->sessions
) {
281 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
284 if (lttng_live_msg_iter
->viewer_connection
) {
285 live_viewer_connection_destroy(lttng_live_msg_iter
->viewer_connection
);
287 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
288 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
290 /* All stream iterators must be destroyed at this point. */
291 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
292 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
294 delete lttng_live_msg_iter
;
300 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
302 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
304 BT_ASSERT(self_msg_iter
);
306 lttng_live_msg_iter
=
307 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_iter
);
308 BT_ASSERT(lttng_live_msg_iter
);
309 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
312 static enum lttng_live_iterator_status
313 lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator
*lttng_live_stream
)
315 bt_logging_level log_level
= lttng_live_stream
->log_level
;
316 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
318 switch (lttng_live_stream
->state
) {
319 case LTTNG_LIVE_STREAM_QUIESCENT
:
320 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
322 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
324 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
326 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
328 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
330 case LTTNG_LIVE_STREAM_EOF
:
333 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
337 * For active no data stream, fetch next index. As a result of that it can
339 * - quiescent: won't have events for a bit,
340 * - have data: need to get that data and produce the event,
341 * - have no data on this stream at this point: need to retry (AGAIN) or return
344 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
345 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
346 struct lttng_live_stream_iterator
*lttng_live_stream
)
348 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
349 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
350 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
351 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
352 struct packet_index index
;
354 if (lttng_live_stream
->trace
->metadata_stream_state
==
355 LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
357 "Need to get an update for the metadata stream before proceeding further with this stream: "
358 "stream-name=\"%s\"",
359 lttng_live_stream
->name
->str
);
360 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
364 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
366 "Need to get an update of all streams before proceeding further with this stream: "
367 "stream-name=\"%s\"",
368 lttng_live_stream
->name
->str
);
369 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
373 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
374 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
377 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
, &index
);
378 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
382 BT_ASSERT_DBG(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
384 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
385 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
.value
,
386 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
388 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&& last_inact_ts
== curr_inact_ts
) {
390 * Because the stream is in the QUIESCENT_NO_DATA
391 * state, we can assert that the last_inactivity_ts was
392 * set and can be safely used in the `if` above.
394 BT_ASSERT(lttng_live_stream
->last_inactivity_ts
.is_set
);
396 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
397 LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream
);
399 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
404 lttng_live_stream
->base_offset
= index
.offset
;
405 lttng_live_stream
->offset
= index
.offset
;
406 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
408 BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", "
409 "viewer-stream-id=%" PRIu64
", stream-base-offset=%" PRIu64
410 ", stream-offset=%" PRIu64
", stream-len=%" PRIu64
,
411 lttng_live_stream
->name
->str
, lttng_live_stream
->viewer_stream_id
,
412 lttng_live_stream
->base_offset
, lttng_live_stream
->offset
, lttng_live_stream
->len
);
415 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
416 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
422 * Creation of the message requires the ctf trace class to be created
423 * beforehand, but the live protocol gives us all streams (including metadata)
424 * at once. So we split it in three steps: getting streams, getting metadata
425 * (which creates the ctf trace class), and then creating the per-stream
428 static enum lttng_live_iterator_status
429 lttng_live_get_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
430 struct lttng_live_session
*session
)
432 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
433 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
434 enum lttng_live_iterator_status status
;
437 if (!session
->attached
) {
438 BT_COMP_LOGD("Attach to session: session-id=%" PRIu64
, session
->id
);
439 enum lttng_live_viewer_status attach_status
=
440 lttng_live_session_attach(session
, lttng_live_msg_iter
->self_msg_iter
);
441 if (attach_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
442 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
444 * Clear any causes appended in
445 * `lttng_live_attach_session()` as we want to
446 * return gracefully since the graph was
449 bt_current_thread_clear_error();
450 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
452 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
453 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error attaching to LTTng live session");
459 BT_COMP_LOGD("Updating all data streams: "
460 "session-id=%" PRIu64
", session-name=\"%s\"",
461 session
->id
, session
->session_name
->str
);
463 status
= lttng_live_session_get_new_streams(session
, lttng_live_msg_iter
->self_msg_iter
);
465 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
467 case LTTNG_LIVE_ITERATOR_STATUS_END
:
469 * We received a `_END` from the `_get_new_streams()` function,
470 * which means no more data will ever be received from the data
471 * streams of this session. But it's possible that the metadata
473 * The live protocol guarantees that we receive all the
474 * metadata needed before we receive data streams needing it.
475 * But it's possible to receive metadata NOT needed by
476 * data streams after the session was closed. For example, this
477 * could happen if a new event is registered and the session is
478 * stopped before any tracepoint for that event is actually
482 "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:"
483 "session-id=%" PRIu64
", session-name=\"%s\"",
484 session
->id
, session
->session_name
->str
);
485 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
491 BT_COMP_LOGD("Updating metadata stream for session: "
492 "session-id=%" PRIu64
", session-name=\"%s\"",
493 session
->id
, session
->session_name
->str
);
496 while (trace_idx
< session
->traces
->len
) {
497 struct lttng_live_trace
*trace
=
498 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
500 status
= lttng_live_metadata_update(trace
);
502 case LTTNG_LIVE_ITERATOR_STATUS_END
:
503 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
506 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
507 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
510 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
511 "Error updating trace metadata: "
512 "stream-iter-status=%s, trace-id=%" PRIu64
,
513 lttng_live_iterator_status_string(status
), trace
->id
);
519 * Now that we have the metadata we can initialize the downstream
522 status
= lttng_live_lazy_msg_init(session
, lttng_live_msg_iter
->self_msg_iter
);
529 lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
531 uint64_t session_idx
, trace_idx
;
532 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
533 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
535 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
536 struct lttng_live_session
*session
=
537 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
538 BT_COMP_LOGD("Force marking session as needing new streams: "
539 "session-id=%" PRIu64
,
541 session
->new_streams_needed
= true;
542 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
543 struct lttng_live_trace
*trace
=
544 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
545 BT_COMP_LOGD("Force marking trace metadata state as needing an update: "
546 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
547 session
->id
, trace
->id
);
549 BT_ASSERT(trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
);
551 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
;
556 static enum lttng_live_iterator_status
557 lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
559 enum lttng_live_iterator_status status
;
560 enum lttng_live_viewer_status viewer_status
;
561 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
562 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
563 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
564 struct lttng_live_session
*session
;
565 enum session_not_found_action sess_not_found_act
=
566 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
568 BT_COMP_LOGD("Update data and metadata of all sessions: "
569 "live-msg-iter-addr=%p",
570 lttng_live_msg_iter
);
572 * In a remotely distant future, we could add a "new
573 * session" flag to the protocol, which would tell us that we
574 * need to query for new sessions even though we have sessions
577 if (lttng_live_msg_iter
->sessions
->len
== 0) {
578 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
580 "No session found. Exiting in accordance with the `session-not-found-action` parameter");
581 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
585 "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter");
587 * Retry to create a viewer session for the requested
590 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
591 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
592 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
593 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
594 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
595 "Error creating LTTng live viewer session");
596 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
597 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
606 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
608 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
609 status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
611 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
612 case LTTNG_LIVE_ITERATOR_STATUS_END
:
614 * A session returned `_END`. Other sessions may still
615 * be active so we override the status and continue
622 if (!session
->closed
) {
623 nr_sessions_opened
++;
627 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&& nr_sessions_opened
== 0) {
628 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
630 status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
637 static enum lttng_live_iterator_status
638 emit_inactivity_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
639 struct lttng_live_stream_iterator
*stream_iter
, const bt_message
**message
,
642 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
643 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
644 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
645 bt_message
*msg
= NULL
;
647 BT_ASSERT(stream_iter
->trace
->clock_class
);
649 BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64
650 ", viewer-stream-id=%" PRIu64
", timestamp=%" PRIu64
,
651 stream_iter
->ctf_stream_class_id
.value
, stream_iter
->viewer_stream_id
, timestamp
);
653 msg
= bt_message_message_iterator_inactivity_create(lttng_live_msg_iter
->self_msg_iter
,
654 stream_iter
->trace
->clock_class
, timestamp
);
656 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error emitting message iterator inactivity message");
665 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
666 bt_message_put_ref(msg
);
670 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
671 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
672 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
674 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
676 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
677 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
681 * Check if we already sent an inactivty message downstream for this
682 * `current_inactivity_ts` value.
684 if (lttng_live_stream
->last_inactivity_ts
.is_set
&&
685 lttng_live_stream
->current_inactivity_ts
== lttng_live_stream
->last_inactivity_ts
.value
) {
686 lttng_live_stream_iterator_set_state(lttng_live_stream
,
687 LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
);
689 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
693 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
, message
,
694 lttng_live_stream
->current_inactivity_ts
);
696 lttng_live_stream
->last_inactivity_ts
.value
= lttng_live_stream
->current_inactivity_ts
;
697 lttng_live_stream
->last_inactivity_ts
.is_set
= true;
702 static int live_get_msg_ts_ns(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
703 const bt_message
*msg
, int64_t last_msg_ts_ns
, int64_t *ts_ns
)
705 const bt_clock_snapshot
*clock_snapshot
= NULL
;
707 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
708 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
711 BT_ASSERT_DBG(ts_ns
);
713 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
714 "last-msg-ts=%" PRId64
,
715 lttng_live_msg_iter
, msg
, last_msg_ts_ns
);
717 switch (bt_message_get_type(msg
)) {
718 case BT_MESSAGE_TYPE_EVENT
:
719 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(msg
);
721 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
722 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg
);
724 case BT_MESSAGE_TYPE_PACKET_END
:
725 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(msg
);
727 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
729 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg
);
731 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
733 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg
);
735 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
736 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg
);
739 /* All the other messages have a higher priority */
740 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
741 *ts_ns
= last_msg_ts_ns
;
745 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
747 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
748 "Cannot get nanoseconds from Epoch of clock snapshot: "
749 "clock-snapshot-addr=%p",
761 BT_COMP_LOGD("Found message's timestamp: "
762 "iter-data-addr=%p, msg-addr=%p, "
763 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
764 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
770 static enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
771 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
772 struct lttng_live_stream_iterator
*lttng_live_stream
, const bt_message
**message
)
774 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
775 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
776 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
777 enum ctf_msg_iter_status status
;
778 uint64_t session_idx
, trace_idx
;
780 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
; session_idx
++) {
781 struct lttng_live_session
*session
=
782 (lttng_live_session
*) g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
784 if (session
->new_streams_needed
) {
785 BT_COMP_LOGD("Need an update for streams: "
786 "session-id=%" PRIu64
,
788 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
791 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
792 struct lttng_live_trace
*trace
=
793 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
794 if (trace
->metadata_stream_state
== LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
795 BT_COMP_LOGD("Need an update for metadata stream: "
796 "session-id=%" PRIu64
", trace-id=%" PRIu64
,
797 session
->id
, trace
->id
);
798 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
804 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
805 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
806 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
807 "Invalid state of live stream iterator"
808 "stream-iter-status=%s",
809 lttng_live_stream_state_string(lttng_live_stream
->state
));
813 status
= ctf_msg_iter_get_next_message(lttng_live_stream
->msg_iter
, message
);
815 case CTF_MSG_ITER_STATUS_EOF
:
816 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
818 case CTF_MSG_ITER_STATUS_OK
:
819 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
821 case CTF_MSG_ITER_STATUS_AGAIN
:
823 * Continue immediately (end of packet). The next
824 * get_index may return AGAIN to delay the following
827 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
829 case CTF_MSG_ITER_STATUS_ERROR
:
831 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
832 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
833 "CTF message iterator failed to get next message: "
834 "msg-iter=%p, msg-iter-status=%s",
835 lttng_live_stream
->msg_iter
, ctf_msg_iter_status_string(status
));
843 static enum lttng_live_iterator_status
844 lttng_live_iterator_close_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
845 struct lttng_live_stream_iterator
*stream_iter
,
846 const bt_message
**curr_msg
)
848 enum lttng_live_iterator_status live_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
849 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
850 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
852 BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", "
853 "viewer-stream-id=%" PRIu64
,
854 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
857 * The viewer has hung up on us so we are closing the stream. The
858 * `ctf_msg_iter` should simply realize that it needs to close the
859 * stream properly by emitting the necessary stream end message.
861 enum ctf_msg_iter_status status
=
862 ctf_msg_iter_get_next_message(stream_iter
->msg_iter
, curr_msg
);
864 if (status
== CTF_MSG_ITER_STATUS_ERROR
) {
865 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
866 "Error getting the next message from CTF message iterator");
867 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
869 } else if (status
== CTF_MSG_ITER_STATUS_EOF
) {
870 BT_COMP_LOGI("Reached the end of the live stream iterator.");
871 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
875 BT_ASSERT(status
== CTF_MSG_ITER_STATUS_OK
);
883 * handle_no_data_streams()
885 * - for each ACTIVE_NO_DATA stream:
886 * - query relayd for stream data, or quiescence info.
887 * - if need metadata, get metadata, goto retry.
888 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
889 * - if quiescent, move to QUIESCENT streams
890 * - if fetched data, move to ACTIVE_DATA streams
891 * (at this point each stream either has data, or is quiescent)
895 * handle_new_streams_and_metadata()
896 * - query relayd for known streams, add them as ACTIVE_NO_DATA
897 * - query relayd for metadata
899 * call handle_active_no_data_streams()
901 * handle_quiescent_streams()
902 * - if at least one stream is ACTIVE_DATA:
903 * - peek stream event with lowest timestamp -> next_ts
904 * - for each quiescent stream
905 * - if next_ts >= quiescent end
906 * - set state to ACTIVE_NO_DATA
908 * - for each quiescent stream
909 * - set state to ACTIVE_NO_DATA
911 * call handle_active_no_data_streams()
913 * handle_active_data_streams()
914 * - if at least one stream is ACTIVE_DATA:
915 * - get stream event with lowest timestamp from heap
916 * - make that stream event the current message.
917 * - move this stream heap position to its next event
918 * - if we need to fetch data from relayd, move
919 * stream to ACTIVE_NO_DATA.
923 * end criterion: ctrl-c on client. If relayd exits or the session
924 * closes on the relay daemon side, we keep on waiting for streams.
925 * Eventually handle --end timestamp (also an end criterion).
927 * When disconnected from relayd: try to re-connect endlessly.
929 static enum lttng_live_iterator_status
930 lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
931 struct lttng_live_stream_iterator
*stream_iter
,
932 const bt_message
**curr_msg
)
934 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
935 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
936 enum lttng_live_iterator_status live_status
;
938 BT_COMP_LOGD("Advancing live stream iterator until next message if possible: "
939 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
940 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
942 if (stream_iter
->has_stream_hung_up
) {
944 * The stream has hung up and the stream was properly closed
945 * during the last call to the current function. Return _END
946 * status now so that this stream iterator is removed for the
947 * stream iterator list.
949 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
954 LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter
);
957 * Make sure we have the most recent metadata and possibly some new
960 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter
);
961 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
966 lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter
, stream_iter
);
967 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
968 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
970 * We overwrite `live_status` since `curr_msg` is
971 * likely set to a valid message in this function.
974 lttng_live_iterator_close_stream(lttng_live_msg_iter
, stream_iter
, curr_msg
);
979 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter
,
980 stream_iter
, curr_msg
);
981 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
982 BT_ASSERT(!*curr_msg
);
988 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter
,
989 stream_iter
, curr_msg
);
990 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
991 BT_ASSERT(!*curr_msg
);
995 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
996 BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams");
1000 BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, "
1001 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1002 lttng_live_iterator_status_string(live_status
), stream_iter
->name
->str
,
1003 stream_iter
->viewer_stream_id
);
1008 static bool is_discarded_packet_or_event_message(const bt_message
*msg
)
1010 const enum bt_message_type msg_type
= bt_message_get_type(msg
);
1012 return msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
||
1013 msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
;
1016 static enum lttng_live_iterator_status
1017 adjust_discarded_packets_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1018 const bt_message
*msg_in
, bt_message
**msg_out
,
1019 uint64_t new_begin_ts
)
1021 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1022 enum bt_property_availability availability
;
1023 const bt_clock_snapshot
*clock_snapshot
;
1027 clock_snapshot
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in
);
1028 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1030 availability
= bt_message_discarded_packets_get_count(msg_in
, &count
);
1031 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1033 *msg_out
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1034 iter
, stream
, new_begin_ts
, end_ts
);
1036 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1040 bt_message_discarded_packets_set_count(*msg_out
, count
);
1045 static enum lttng_live_iterator_status
1046 adjust_discarded_events_message(bt_self_message_iterator
*iter
, const bt_stream
*stream
,
1047 const bt_message
*msg_in
, bt_message
**msg_out
,
1048 uint64_t new_begin_ts
)
1050 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1051 enum bt_property_availability availability
;
1052 const bt_clock_snapshot
*clock_snapshot
;
1056 clock_snapshot
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in
);
1057 end_ts
= bt_clock_snapshot_get_value(clock_snapshot
);
1059 availability
= bt_message_discarded_events_get_count(msg_in
, &count
);
1060 BT_ASSERT_DBG(availability
== BT_PROPERTY_AVAILABILITY_AVAILABLE
);
1062 *msg_out
= bt_message_discarded_events_create_with_default_clock_snapshots(
1063 iter
, stream
, new_begin_ts
, end_ts
);
1065 status
= LTTNG_LIVE_ITERATOR_STATUS_NOMEM
;
1069 bt_message_discarded_events_set_count(*msg_out
, count
);
1074 static enum lttng_live_iterator_status
1075 handle_late_message(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1076 struct lttng_live_stream_iterator
*stream_iter
, int64_t late_msg_ts_ns
,
1077 const bt_message
*late_msg
)
1079 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1080 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1081 const bt_clock_class
*clock_class
;
1082 const bt_stream_class
*stream_class
;
1083 enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status
;
1084 int64_t last_inactivity_ts_ns
;
1085 enum lttng_live_iterator_status stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1086 enum lttng_live_iterator_status adjust_status
;
1087 bt_message
*adjusted_message
;
1090 * The timestamp of the current message is before the last message sent
1091 * by this component. We CANNOT send it as is.
1093 * The only expected scenario in which that could happen is the
1094 * following, everything else is a bug in this component, relay daemon,
1097 * Expected scenario: The CTF message iterator emitted discarded
1098 * packets and discarded events with synthesized beginning and end
1099 * timestamps from the bounds of the last known packet and the newly
1100 * decoded packet header. The CTF message iterator is not aware of
1101 * stream inactivity beacons. Hence, we have to adjust the beginning
1102 * timestamp of those types of messages if a stream signalled its
1103 * inactivity up until _after_ the last known packet's beginning
1106 * Otherwise, the monotonicity guarantee of message timestamps would
1109 * In short, the only scenario in which it's okay and fixable to
1110 * received a late message is when:
1111 * 1. the late message is a discarded packets or discarded events
1113 * 2. this stream produced an inactivity message downstream, and
1114 * 3. the timestamp of the late message is within the inactivity
1115 * timespan we sent downstream through the inactivity message.
1118 BT_COMP_LOGD("Handling late message on live stream iterator: "
1119 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1120 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1122 if (!stream_iter
->last_inactivity_ts
.is_set
) {
1123 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Invalid live stream state: "
1124 "have a late message when no inactivity message "
1125 "was ever sent for that stream.");
1126 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1130 if (!is_discarded_packet_or_event_message(late_msg
)) {
1131 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1132 "Invalid live stream state: "
1133 "have a late message that is not a packet discarded or "
1134 "event discarded message: late-msg-type=%s",
1135 bt_common_message_type_string(bt_message_get_type(late_msg
)));
1136 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1140 stream_class
= bt_stream_borrow_class_const(stream_iter
->stream
);
1141 clock_class
= bt_stream_class_borrow_default_clock_class_const(stream_class
);
1143 ts_ns_status
= bt_clock_class_cycles_to_ns_from_origin(
1144 clock_class
, stream_iter
->last_inactivity_ts
.value
, &last_inactivity_ts_ns
);
1145 if (ts_ns_status
!= BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK
) {
1146 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Error converting last "
1147 "inactivity message timestamp to nanoseconds");
1148 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1152 if (last_inactivity_ts_ns
<= late_msg_ts_ns
) {
1153 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1154 "Invalid live stream state: "
1155 "have a late message that is none included in a stream "
1156 "inactivity timespan: last-inactivity-ts-ns=%" PRIu64
1157 "late-msg-ts-ns=%" PRIu64
,
1158 last_inactivity_ts_ns
, late_msg_ts_ns
);
1159 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1164 * We now know that it's okay for this message to be late, we can now
1165 * adjust its timestamp to ensure monotonicity.
1167 BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, "
1168 "msg-new-ts-ns=%" PRIu64
,
1169 bt_common_message_type_string(bt_message_get_type(late_msg
)),
1170 stream_iter
->last_inactivity_ts
.value
);
1171 switch (bt_message_get_type(late_msg
)) {
1172 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1173 adjust_status
= adjust_discarded_events_message(
1174 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1175 stream_iter
->last_inactivity_ts
.value
);
1177 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1178 adjust_status
= adjust_discarded_packets_message(
1179 lttng_live_msg_iter
->self_msg_iter
, stream_iter
->stream
, late_msg
, &adjusted_message
,
1180 stream_iter
->last_inactivity_ts
.value
);
1186 if (adjust_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1187 stream_iter_status
= adjust_status
;
1191 BT_ASSERT_DBG(adjusted_message
);
1192 stream_iter
->current_msg
= adjusted_message
;
1193 stream_iter
->current_msg_ts_ns
= last_inactivity_ts_ns
;
1194 bt_message_put_ref(late_msg
);
1197 return stream_iter_status
;
1200 static enum lttng_live_iterator_status
1201 next_stream_iterator_for_trace(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1202 struct lttng_live_trace
*live_trace
,
1203 struct lttng_live_stream_iterator
**youngest_trace_stream_iter
)
1205 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1206 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1207 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1208 enum lttng_live_iterator_status stream_iter_status
;
1210 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1211 uint64_t stream_iter_idx
;
1213 BT_ASSERT_DBG(live_trace
);
1214 BT_ASSERT_DBG(live_trace
->stream_iterators
);
1216 BT_COMP_LOGD("Finding the next stream iterator for trace: "
1217 "trace-id=%" PRIu64
,
1220 * Update the current message of every stream iterators of this trace.
1221 * The current msg of every stream must have a timestamp equal or
1222 * larger than the last message returned by this iterator. We must
1223 * ensure monotonicity.
1225 stream_iter_idx
= 0;
1226 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
1227 bool stream_iter_is_ended
= false;
1228 struct lttng_live_stream_iterator
*stream_iter
=
1229 (lttng_live_stream_iterator
*) g_ptr_array_index(live_trace
->stream_iterators
,
1233 * If there is no current message for this stream, go fetch
1236 while (!stream_iter
->current_msg
) {
1237 const bt_message
*msg
= NULL
;
1238 int64_t curr_msg_ts_ns
= INT64_MAX
;
1240 stream_iter_status
=
1241 lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter
, stream_iter
, &msg
);
1243 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1244 stream_iter_is_ended
= true;
1248 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1254 BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, "
1255 "stream-name=\"%s\", viewer-stream-id=%" PRIu64
,
1256 bt_common_message_type_string(bt_message_get_type(msg
)),
1257 stream_iter
->name
->str
, stream_iter
->viewer_stream_id
);
1260 * Get the timestamp in nanoseconds from origin of this
1263 live_get_msg_ts_ns(lttng_live_msg_iter
, msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
1267 * Check if the message of the current live stream
1268 * iterator occurred at the exact same time or after the
1269 * last message returned by this component's message
1270 * iterator. If not, we need to handle it with care.
1272 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1273 stream_iter
->current_msg
= msg
;
1274 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1277 * We received a message from the past. This
1278 * may be fixable but it can also be an error.
1280 stream_iter_status
=
1281 handle_late_message(lttng_live_msg_iter
, stream_iter
, curr_msg_ts_ns
, msg
);
1282 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1283 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1284 "Late message could not be handled correctly: "
1285 "lttng-live-msg-iter-addr=%p, "
1286 "stream-name=\"%s\", "
1287 "curr-msg-ts=%" PRId64
", last-msg-ts=%" PRId64
,
1288 lttng_live_msg_iter
, stream_iter
->name
->str
,
1289 curr_msg_ts_ns
, lttng_live_msg_iter
->last_msg_ts_ns
);
1290 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1296 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1298 if (!stream_iter_is_ended
) {
1299 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1300 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1302 * Update the current best candidate message
1303 * for the stream iterator of this live trace
1304 * to be forwarded downstream.
1306 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1307 youngest_candidate_stream_iter
= stream_iter
;
1308 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1310 * Order the messages in an arbitrary but
1311 * deterministic way.
1313 BT_ASSERT_DBG(stream_iter
!= youngest_candidate_stream_iter
);
1314 int ret
= common_muxing_compare_messages(
1315 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1318 * The `youngest_candidate_stream_iter->current_msg`
1319 * should go first. Update the next
1320 * iterator and the current timestamp.
1322 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1323 youngest_candidate_stream_iter
= stream_iter
;
1324 } else if (ret
== 0) {
1326 * Unable to pick which one should go
1330 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1331 "stream-iter-addr=%p"
1332 "stream-iter-addr=%p",
1333 stream_iter
, youngest_candidate_stream_iter
);
1340 * The live stream iterator has ended. That
1341 * iterator is removed from the array, but
1342 * there is no need to increment
1343 * stream_iter_idx as
1344 * g_ptr_array_remove_index_fast replaces the
1345 * removed element with the array's last
1348 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
, stream_iter_idx
);
1352 if (youngest_candidate_stream_iter
) {
1353 *youngest_trace_stream_iter
= youngest_candidate_stream_iter
;
1354 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1357 * The only case where we don't have a candidate for this trace
1358 * is if we reached the end of all the iterators.
1360 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1361 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1365 return stream_iter_status
;
1368 static enum lttng_live_iterator_status
1369 next_stream_iterator_for_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1370 struct lttng_live_session
*session
,
1371 struct lttng_live_stream_iterator
**youngest_session_stream_iter
)
1373 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1374 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1375 enum lttng_live_iterator_status stream_iter_status
;
1376 uint64_t trace_idx
= 0;
1377 int64_t youngest_candidate_msg_ts
= INT64_MAX
;
1378 struct lttng_live_stream_iterator
*youngest_candidate_stream_iter
= NULL
;
1380 BT_COMP_LOGD("Finding the next stream iterator for session: "
1381 "session-id=%" PRIu64
,
1384 * Make sure we are attached to the session and look for new streams
1387 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1388 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1389 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1390 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1394 BT_ASSERT_DBG(session
->traces
);
1396 while (trace_idx
< session
->traces
->len
) {
1397 bool trace_is_ended
= false;
1398 struct lttng_live_stream_iterator
*stream_iter
;
1399 struct lttng_live_trace
*trace
=
1400 (lttng_live_trace
*) g_ptr_array_index(session
->traces
, trace_idx
);
1402 stream_iter_status
=
1403 next_stream_iterator_for_trace(lttng_live_msg_iter
, trace
, &stream_iter
);
1404 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1406 * All the live stream iterators for this trace are
1407 * ENDed. Remove the trace from this session.
1409 trace_is_ended
= true;
1410 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1414 if (!trace_is_ended
) {
1415 BT_ASSERT_DBG(stream_iter
);
1417 if (G_UNLIKELY(youngest_candidate_stream_iter
== NULL
) ||
1418 stream_iter
->current_msg_ts_ns
< youngest_candidate_msg_ts
) {
1419 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1420 youngest_candidate_stream_iter
= stream_iter
;
1421 } else if (stream_iter
->current_msg_ts_ns
== youngest_candidate_msg_ts
) {
1423 * Order the messages in an arbitrary but
1424 * deterministic way.
1426 int ret
= common_muxing_compare_messages(
1427 stream_iter
->current_msg
, youngest_candidate_stream_iter
->current_msg
);
1430 * The `youngest_candidate_stream_iter->current_msg`
1431 * should go first. Update the next iterator
1432 * and the current timestamp.
1434 youngest_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1435 youngest_candidate_stream_iter
= stream_iter
;
1436 } else if (ret
== 0) {
1437 /* Unable to pick which one should go first. */
1439 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1440 "stream-iter-addr=%p"
1441 "stream-iter-addr=%p",
1442 stream_iter
, youngest_candidate_stream_iter
);
1448 * trace_idx is not incremented since
1449 * g_ptr_array_remove_index_fast replaces the
1450 * element at trace_idx with the array's last element.
1452 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1455 if (youngest_candidate_stream_iter
) {
1456 *youngest_session_stream_iter
= youngest_candidate_stream_iter
;
1457 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1460 * The only cases where we don't have a candidate for this
1462 * 1. if we reached the end of all the iterators of all the
1463 * traces of this session,
1464 * 2. if we never had live stream iterator in the first place.
1466 * In either cases, we return END.
1468 BT_ASSERT(session
->traces
->len
== 0);
1469 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1472 return stream_iter_status
;
1475 static inline void put_messages(bt_message_array_const msgs
, uint64_t count
)
1479 for (i
= 0; i
< count
; i
++) {
1480 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1484 bt_message_iterator_class_next_method_status
1485 lttng_live_msg_iter_next(bt_self_message_iterator
*self_msg_it
, bt_message_array_const msgs
,
1486 uint64_t capacity
, uint64_t *count
)
1488 bt_message_iterator_class_next_method_status status
;
1489 enum lttng_live_viewer_status viewer_status
;
1490 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1491 (struct lttng_live_msg_iter
*) bt_self_message_iterator_get_data(self_msg_it
);
1492 struct lttng_live_component
*lttng_live
= lttng_live_msg_iter
->lttng_live_comp
;
1493 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
1494 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
1495 enum lttng_live_iterator_status stream_iter_status
;
1496 uint64_t session_idx
;
1500 BT_ASSERT_DBG(lttng_live_msg_iter
);
1502 if (G_UNLIKELY(lttng_live_msg_iter
->was_interrupted
)) {
1504 * The iterator was interrupted in a previous call to the
1505 * `_next()` method. We currently do not support generating
1506 * messages after such event. The babeltrace2 CLI should never
1507 * be running the graph after being interrupted. So this check
1508 * is to prevent other graph users from using this live
1509 * iterator in an messed up internal state.
1511 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1512 BT_COMP_LOGE_APPEND_CAUSE(
1514 "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event.");
1519 * Clear all the invalid message reference that might be left over in
1522 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1525 * If no session are exposed on the relay found at the url provided by
1526 * the user, session count will be 0. In this case, we return status
1527 * end to return gracefully.
1529 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1530 if (lttng_live
->params
.sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1531 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1535 * The are no more active session for this session
1536 * name. Retry to create a viewer session for the
1537 * requested session name.
1539 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1540 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1541 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1542 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1543 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1544 "Error creating LTTng live viewer session");
1545 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1546 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1555 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1556 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1560 * Here the muxing of message is done.
1562 * We need to iterate over all the streams of all the traces of all the
1563 * viewer sessions in order to get the message with the smallest
1564 * timestamp. In this case, a session is a viewer session and there is
1565 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1566 * kernel). Each viewer session can have multiple traces, for example,
1567 * 64bit UST viewer sessions could have multiple per-pid traces.
1569 * We iterate over the streams of each traces to update and see what is
1570 * their next message's timestamp. From those timestamps, we select the
1571 * message with the smallest timestamp as the best candidate message
1572 * for that trace and do the same thing across all the sessions.
1574 * We then compare the timestamp of best candidate message of all the
1575 * sessions to pick the message with the smallest timestamp and we
1578 while (*count
< capacity
) {
1579 struct lttng_live_stream_iterator
*youngest_stream_iter
= NULL
,
1580 *candidate_stream_iter
= NULL
;
1581 int64_t youngest_msg_ts_ns
= INT64_MAX
;
1583 BT_ASSERT_DBG(lttng_live_msg_iter
->sessions
);
1585 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1586 struct lttng_live_session
*session
= (lttng_live_session
*) g_ptr_array_index(
1587 lttng_live_msg_iter
->sessions
, session_idx
);
1589 /* Find the best candidate message to send downstream. */
1590 stream_iter_status
= next_stream_iterator_for_session(lttng_live_msg_iter
, session
,
1591 &candidate_stream_iter
);
1593 /* If we receive an END status, it means that either:
1594 * - Those traces never had active streams (UST with no
1595 * data produced yet),
1596 * - All live stream iterators have ENDed.*/
1597 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1598 if (session
->closed
&& session
->traces
->len
== 0) {
1600 * Remove the session from the list.
1601 * session_idx is not modified since
1602 * g_ptr_array_remove_index_fast
1603 * replaces the the removed element with
1604 * the array's last element.
1606 g_ptr_array_remove_index_fast(lttng_live_msg_iter
->sessions
, session_idx
);
1613 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1617 if (G_UNLIKELY(youngest_stream_iter
== NULL
) ||
1618 candidate_stream_iter
->current_msg_ts_ns
< youngest_msg_ts_ns
) {
1619 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1620 youngest_stream_iter
= candidate_stream_iter
;
1621 } else if (candidate_stream_iter
->current_msg_ts_ns
== youngest_msg_ts_ns
) {
1623 * The currently selected message to be sent
1624 * downstream next has the exact same timestamp
1625 * that of the current candidate message. We
1626 * must break the tie in a predictable manner.
1629 "Two of the next message candidates have the same timestamps, pick one deterministically.");
1631 * Order the messages in an arbitrary but
1632 * deterministic way.
1634 int ret
= common_muxing_compare_messages(candidate_stream_iter
->current_msg
,
1635 youngest_stream_iter
->current_msg
);
1638 * The `candidate_stream_iter->current_msg`
1639 * should go first. Update the next
1640 * iterator and the current timestamp.
1642 youngest_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1643 youngest_stream_iter
= candidate_stream_iter
;
1644 } else if (ret
== 0) {
1645 /* Unable to pick which one should go first. */
1647 "Cannot deterministically pick next live stream message iterator because they have identical next messages: "
1648 "next-stream-iter-addr=%p"
1649 "candidate-stream-iter-addr=%p",
1650 youngest_stream_iter
, candidate_stream_iter
);
1657 if (!youngest_stream_iter
) {
1658 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1662 BT_ASSERT_DBG(youngest_stream_iter
->current_msg
);
1663 /* Ensure monotonicity. */
1664 BT_ASSERT_DBG(lttng_live_msg_iter
->last_msg_ts_ns
<=
1665 youngest_stream_iter
->current_msg_ts_ns
);
1668 * Insert the next message to the message batch. This will set
1669 * stream iterator current message to NULL so that next time
1670 * we fetch the next message of that stream iterator
1672 BT_MESSAGE_MOVE_REF(msgs
[*count
], youngest_stream_iter
->current_msg
);
1675 /* Update the last timestamp in nanoseconds sent downstream. */
1676 lttng_live_msg_iter
->last_msg_ts_ns
= youngest_msg_ts_ns
;
1677 youngest_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1679 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1683 switch (stream_iter_status
) {
1684 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1685 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1687 * If we gathered messages, return _OK even if the graph was
1688 * interrupted. This allows for the components downstream to at
1689 * least get the those messages. If the graph was indeed
1690 * interrupted there should not be another _next() call as the
1691 * application will tear down the graph. This component class
1692 * doesn't support restarting after an interruption.
1695 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1697 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
1700 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1701 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1703 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1704 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1705 "Memory error preparing the next batch of messages: "
1706 "live-iter-status=%s",
1707 lttng_live_iterator_status_string(stream_iter_status
));
1708 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1710 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1711 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1712 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1713 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1714 "Error preparing the next batch of messages: "
1715 "live-iter-status=%s",
1716 lttng_live_iterator_status_string(stream_iter_status
));
1718 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1719 /* Put all existing messages on error. */
1720 put_messages(msgs
, *count
);
1730 static struct lttng_live_msg_iter
*
1731 lttng_live_msg_iter_create(struct lttng_live_component
*lttng_live_comp
,
1732 bt_self_message_iterator
*self_msg_it
)
1734 lttng_live_msg_iter
*lttng_live_msg_iter
= new struct lttng_live_msg_iter
;
1735 lttng_live_msg_iter
->log_level
= lttng_live_comp
->log_level
;
1736 lttng_live_msg_iter
->self_comp
= lttng_live_comp
->self_comp
;
1737 lttng_live_msg_iter
->lttng_live_comp
= lttng_live_comp
;
1738 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1740 lttng_live_msg_iter
->active_stream_iter
= 0;
1741 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1742 lttng_live_msg_iter
->was_interrupted
= false;
1744 lttng_live_msg_iter
->sessions
=
1745 g_ptr_array_new_with_free_func((GDestroyNotify
) lttng_live_destroy_session
);
1746 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1748 return lttng_live_msg_iter
;
1751 bt_message_iterator_class_initialize_method_status
1752 lttng_live_msg_iter_init(bt_self_message_iterator
*self_msg_it
,
1753 bt_self_message_iterator_configuration
*, bt_self_component_port_output
*)
1755 bt_message_iterator_class_initialize_method_status status
;
1756 struct lttng_live_component
*lttng_live
;
1757 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1758 enum lttng_live_viewer_status viewer_status
;
1759 bt_logging_level log_level
;
1760 bt_self_component
*self_comp
= bt_self_message_iterator_borrow_component(self_msg_it
);
1762 lttng_live
= (lttng_live_component
*) bt_self_component_get_data(self_comp
);
1763 log_level
= lttng_live
->log_level
;
1764 self_comp
= lttng_live
->self_comp
;
1766 /* There can be only one downstream iterator at the same time. */
1767 BT_ASSERT(!lttng_live
->has_msg_iter
);
1768 lttng_live
->has_msg_iter
= true;
1770 lttng_live_msg_iter
= lttng_live_msg_iter_create(lttng_live
, self_msg_it
);
1771 if (!lttng_live_msg_iter
) {
1772 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create lttng_live_msg_iter");
1773 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1777 viewer_status
= live_viewer_connection_create(
1778 self_comp
, NULL
, log_level
, lttng_live
->params
.url
->str
, false, lttng_live_msg_iter
,
1779 <tng_live_msg_iter
->viewer_connection
);
1780 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1781 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1782 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer connection");
1783 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1785 * Interruption in the _iter_init() method is not
1786 * supported. Return an error.
1788 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted while creating viewer connection");
1791 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1795 viewer_status
= lttng_live_create_viewer_session(lttng_live_msg_iter
);
1796 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1797 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1798 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create viewer session");
1799 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1801 * Interruption in the _iter_init() method is not
1802 * supported. Return an error.
1804 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Interrupted when creating viewer session");
1807 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1811 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1812 switch (lttng_live
->params
.sess_not_found_act
) {
1813 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1815 "Unable to connect to the requested live viewer session. Keep trying to connect because of "
1816 "%s=\"%s\" component parameter: url=\"%s\"",
1817 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1818 lttng_live
->params
.url
->str
);
1820 case SESSION_NOT_FOUND_ACTION_FAIL
:
1821 BT_COMP_LOGE_APPEND_CAUSE(
1823 "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" "
1824 "component parameter: url =\"%s\"",
1825 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_FAIL_STR
,
1826 lttng_live
->params
.url
->str
);
1827 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1829 case SESSION_NOT_FOUND_ACTION_END
:
1831 "Unable to connect to the requested live viewer session. End gracefully at the first _next() "
1832 "call because of %s=\"%s\" component parameter: "
1834 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_END_STR
,
1835 lttng_live
->params
.url
->str
);
1842 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1843 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1847 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1852 static struct bt_param_validation_map_value_entry_descr list_sessions_params
[] = {
1853 {URL_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
1854 bt_param_validation_value_descr::makeString()},
1855 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
1857 static bt_component_class_query_method_status
1858 lttng_live_query_list_sessions(const bt_value
*params
, const bt_value
**result
,
1859 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1861 bt_component_class_query_method_status status
;
1862 const bt_value
*url_value
= NULL
;
1864 struct live_viewer_connection
*viewer_connection
= NULL
;
1865 enum lttng_live_viewer_status viewer_status
;
1866 enum bt_param_validation_status validation_status
;
1867 gchar
*validate_error
= NULL
;
1869 validation_status
= bt_param_validation_validate(params
, list_sessions_params
, &validate_error
);
1870 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
1871 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1873 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
1874 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1875 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "%s", validate_error
);
1879 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1880 url
= bt_value_string_get(url_value
);
1882 viewer_status
= live_viewer_connection_create(NULL
, self_comp_class
, log_level
, url
, true, NULL
,
1883 &viewer_connection
);
1884 if (viewer_status
!= LTTNG_LIVE_VIEWER_STATUS_OK
) {
1885 if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_ERROR
) {
1886 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to create viewer connection");
1887 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1888 } else if (viewer_status
== LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED
) {
1889 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN
;
1896 status
= live_viewer_connection_list_sessions(viewer_connection
, result
);
1897 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1898 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Failed to list viewer sessions");
1905 BT_VALUE_PUT_REF_AND_RESET(*result
);
1908 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1912 if (viewer_connection
) {
1913 live_viewer_connection_destroy(viewer_connection
);
1916 g_free(validate_error
);
1921 static bt_component_class_query_method_status
1922 lttng_live_query_support_info(const bt_value
*params
, const bt_value
**result
,
1923 bt_self_component_class
*self_comp_class
, bt_logging_level log_level
)
1925 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1926 const bt_value
*input_type_value
;
1927 const bt_value
*input_value
;
1929 struct bt_common_lttng_live_url_parts parts
= {};
1931 /* Used by the logging macros */
1932 __attribute__((unused
)) bt_self_component
*self_comp
= NULL
;
1935 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
1936 if (!input_type_value
) {
1937 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `type` parameter.");
1941 if (!bt_value_is_string(input_type_value
)) {
1942 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "`type` parameter is not a string value.");
1946 if (strcmp(bt_value_string_get(input_type_value
), "string") != 0) {
1947 /* We don't handle file system paths */
1951 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
1953 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
, "Missing expected `input` parameter.");
1957 if (!bt_value_is_string(input_value
)) {
1958 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
1959 "`input` parameter is not a string value.");
1963 parts
= bt_common_parse_lttng_live_url(bt_value_string_get(input_value
), NULL
, 0);
1964 if (parts
.session_name
) {
1966 * Looks pretty much like an LTTng live URL: we got the
1967 * session name part, which forms a complete URL.
1973 *result
= bt_value_real_create_init(weight
);
1975 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
1983 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1986 BT_ASSERT(!*result
);
1989 bt_common_destroy_lttng_live_url_parts(&parts
);
1993 bt_component_class_query_method_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1994 bt_private_query_executor
*priv_query_exec
,
1995 const char *object
, const bt_value
*params
,
1996 __attribute__((unused
)) void *method_data
,
1997 const bt_value
**result
)
1999 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
2000 bt_self_component
*self_comp
= NULL
;
2001 bt_self_component_class
*self_comp_class
=
2002 bt_self_component_class_source_as_self_component_class(comp_class
);
2003 bt_logging_level log_level
= bt_query_executor_get_logging_level(
2004 bt_private_query_executor_as_query_executor_const(priv_query_exec
));
2006 if (strcmp(object
, "sessions") == 0) {
2007 status
= lttng_live_query_list_sessions(params
, result
, self_comp_class
, log_level
);
2008 } else if (strcmp(object
, "babeltrace.support-info") == 0) {
2009 status
= lttng_live_query_support_info(params
, result
, self_comp_class
, log_level
);
2011 BT_COMP_LOGI("Unknown query object `%s`", object
);
2012 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
2020 static void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
2026 if (lttng_live
->params
.url
) {
2027 g_string_free(lttng_live
->params
.url
, TRUE
);
2033 void lttng_live_component_finalize(bt_self_component_source
*component
)
2035 lttng_live_component
*data
= (lttng_live_component
*) bt_self_component_get_data(
2036 bt_self_component_source_as_self_component(component
));
2041 lttng_live_component_destroy_data(data
);
2044 static enum session_not_found_action
2045 parse_session_not_found_action_param(const bt_value
*no_session_param
)
2047 enum session_not_found_action action
;
2048 const char *no_session_act_str
= bt_value_string_get(no_session_param
);
2050 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
2051 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2052 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
2053 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
2055 BT_ASSERT(strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0);
2056 action
= SESSION_NOT_FOUND_ACTION_END
;
2062 static bt_param_validation_value_descr inputs_elem_descr
=
2063 bt_param_validation_value_descr::makeString();
2065 static const char *sess_not_found_action_choices
[] = {
2066 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
2067 SESS_NOT_FOUND_ACTION_FAIL_STR
,
2068 SESS_NOT_FOUND_ACTION_END_STR
,
2071 static struct bt_param_validation_map_value_entry_descr params_descr
[] = {
2072 {INPUTS_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
2073 bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr
)},
2074 {SESS_NOT_FOUND_ACTION_PARAM
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
,
2075 bt_param_validation_value_descr::makeString(sess_not_found_action_choices
)},
2076 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
2078 static bt_component_class_initialize_method_status
2079 lttng_live_component_create(const bt_value
*params
, bt_logging_level log_level
,
2080 bt_self_component
*self_comp
, struct lttng_live_component
**component
)
2082 struct lttng_live_component
*lttng_live
= NULL
;
2083 const bt_value
*inputs_value
;
2084 const bt_value
*url_value
;
2085 const bt_value
*value
;
2087 enum bt_param_validation_status validation_status
;
2088 gchar
*validation_error
= NULL
;
2089 bt_component_class_initialize_method_status status
;
2091 validation_status
= bt_param_validation_validate(params
, params_descr
, &validation_error
);
2092 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
2093 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2095 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
2096 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validation_error
);
2097 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
2101 lttng_live
= new lttng_live_component
;
2102 lttng_live
->log_level
= log_level
;
2103 lttng_live
->self_comp
= self_comp
;
2104 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
2105 lttng_live
->has_msg_iter
= false;
2107 inputs_value
= bt_value_map_borrow_entry_value_const(params
, INPUTS_PARAM
);
2108 url_value
= bt_value_array_borrow_element_by_index_const(inputs_value
, 0);
2109 url
= bt_value_string_get(url_value
);
2111 lttng_live
->params
.url
= g_string_new(url
);
2112 if (!lttng_live
->params
.url
) {
2113 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
2117 value
= bt_value_map_borrow_entry_value_const(params
, SESS_NOT_FOUND_ACTION_PARAM
);
2119 lttng_live
->params
.sess_not_found_act
= parse_session_not_found_action_param(value
);
2121 BT_COMP_LOGI("Optional `%s` parameter is missing: "
2122 "defaulting to `%s`.",
2123 SESS_NOT_FOUND_ACTION_PARAM
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
2124 lttng_live
->params
.sess_not_found_act
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
2127 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
2131 lttng_live_component_destroy_data(lttng_live
);
2134 g_free(validation_error
);
2136 *component
= lttng_live
;
2140 bt_component_class_initialize_method_status
2141 lttng_live_component_init(bt_self_component_source
*self_comp_src
,
2142 bt_self_component_source_configuration
*, const bt_value
*params
, void *)
2144 struct lttng_live_component
*lttng_live
;
2145 bt_component_class_initialize_method_status ret
;
2146 bt_self_component
*self_comp
= bt_self_component_source_as_self_component(self_comp_src
);
2147 bt_logging_level log_level
=
2148 bt_component_get_logging_level(bt_self_component_as_component(self_comp
));
2149 bt_self_component_add_port_status add_port_status
;
2151 ret
= lttng_live_component_create(params
, log_level
, self_comp
, <tng_live
);
2152 if (ret
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
2156 add_port_status
= bt_self_component_source_add_output_port(self_comp_src
, "out", NULL
, NULL
);
2157 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
2158 ret
= (bt_component_class_initialize_method_status
) add_port_status
;
2162 bt_self_component_set_data(self_comp
, lttng_live
);
2166 lttng_live_component_destroy_data(lttng_live
);