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"
40 #include "common/assert.h"
41 #include <babeltrace2/babeltrace.h>
42 #include "compat/compiler.h"
43 #include <babeltrace2/types.h>
45 #include "data-stream.h"
47 #include "lttng-live.h"
49 #define MAX_QUERY_SIZE (256*1024)
50 #define URL_PARAM "url"
51 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
52 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
53 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
54 #define SESS_NOT_FOUND_ACTION_END_STR "end"
56 #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
59 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
62 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
63 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
64 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
65 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
66 case LTTNG_LIVE_ITERATOR_STATUS_END
:
67 return "LTTNG_LIVE_ITERATOR_STATUS_END";
68 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
69 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
70 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
71 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
72 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
73 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
74 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
75 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
76 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
77 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
84 const char *print_state(struct lttng_live_stream_iterator
*s
)
87 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
88 return "ACTIVE_NO_DATA";
89 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
90 return "QUIESCENT_NO_DATA";
91 case LTTNG_LIVE_STREAM_QUIESCENT
:
93 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
95 case LTTNG_LIVE_STREAM_EOF
:
102 #define print_stream_state(live_stream_iter) \
104 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
105 ", curr_inact_ts %" PRId64, \
106 print_state(live_stream_iter), \
107 live_stream_iter->last_inactivity_ts, \
108 live_stream_iter->current_inactivity_ts); \
112 bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter
*msg_iter
)
121 ret
= bt_self_message_iterator_is_interrupted(
122 msg_iter
->self_msg_iter
);
129 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
133 struct lttng_live_trace
*ret_trace
= NULL
;
135 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
136 struct lttng_live_trace
*trace
=
137 g_ptr_array_index(session
->traces
, trace_idx
);
138 if (trace
->id
== trace_id
) {
149 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
151 bt_logging_level log_level
= trace
->log_level
;
152 bt_self_component
*self_comp
= trace
->self_comp
;
154 BT_COMP_LOGD("Destroy lttng_live_trace");
156 BT_ASSERT(trace
->stream_iterators
);
157 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
159 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
160 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
162 lttng_live_metadata_fini(trace
);
167 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
170 struct lttng_live_trace
*trace
= NULL
;
171 bt_logging_level log_level
= session
->log_level
;
172 bt_self_component
*self_comp
= session
->self_comp
;
174 trace
= g_new0(struct lttng_live_trace
, 1);
178 trace
->log_level
= session
->log_level
;
179 trace
->self_comp
= session
->self_comp
;
180 trace
->session
= session
;
181 trace
->id
= trace_id
;
182 trace
->trace_class
= NULL
;
184 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
185 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
186 BT_ASSERT(trace
->stream_iterators
);
187 trace
->new_metadata_needed
= true;
188 g_ptr_array_add(session
->traces
, trace
);
190 BT_COMP_LOGI("Create trace");
200 struct lttng_live_trace
*lttng_live_borrow_trace(
201 struct lttng_live_session
*session
, uint64_t trace_id
)
203 struct lttng_live_trace
*trace
;
205 trace
= lttng_live_find_trace(session
, trace_id
);
210 /* The session is the owner of the newly created trace. */
211 trace
= lttng_live_create_trace(session
, trace_id
);
218 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
219 uint64_t session_id
, const char *hostname
,
220 const char *session_name
)
223 struct lttng_live_session
*session
;
224 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
225 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
227 session
= g_new0(struct lttng_live_session
, 1);
232 session
->log_level
= lttng_live_msg_iter
->log_level
;
233 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
234 session
->id
= session_id
;
235 session
->traces
= g_ptr_array_new_with_free_func(
236 (GDestroyNotify
) lttng_live_destroy_trace
);
237 BT_ASSERT(session
->traces
);
238 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
239 session
->new_streams_needed
= true;
240 session
->hostname
= g_string_new(hostname
);
241 BT_ASSERT(session
->hostname
);
243 session
->session_name
= g_string_new(session_name
);
244 BT_ASSERT(session
->session_name
);
246 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
247 session
->id
, hostname
, session_name
);
248 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
251 BT_COMP_LOGE("Error adding session");
259 void lttng_live_destroy_session(struct lttng_live_session
*session
)
261 bt_logging_level log_level
;
262 bt_self_component
*self_comp
;
268 log_level
= session
->log_level
;
269 self_comp
= session
->self_comp
;
270 BT_COMP_LOGD("Destroy lttng live session");
271 if (session
->id
!= -1ULL) {
272 if (lttng_live_detach_session(session
)) {
273 if (session
->lttng_live_msg_iter
&&
274 !lttng_live_graph_is_canceled(
275 session
->lttng_live_msg_iter
)) {
276 /* Old relayd cannot detach sessions. */
277 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
284 if (session
->traces
) {
285 g_ptr_array_free(session
->traces
, TRUE
);
288 if (session
->hostname
) {
289 g_string_free(session
->hostname
, TRUE
);
291 if (session
->session_name
) {
292 g_string_free(session
->session_name
, TRUE
);
301 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
303 if (!lttng_live_msg_iter
) {
307 if (lttng_live_msg_iter
->sessions
) {
308 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
311 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
312 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
313 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
315 /* All stream iterators must be destroyed at this point. */
316 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
317 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
319 g_free(lttng_live_msg_iter
);
326 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
328 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
330 BT_ASSERT(self_msg_iter
);
332 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
333 BT_ASSERT(lttng_live_msg_iter
);
334 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
338 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
339 struct lttng_live_stream_iterator
*lttng_live_stream
)
341 bt_logging_level log_level
= lttng_live_stream
->log_level
;
342 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
344 switch (lttng_live_stream
->state
) {
345 case LTTNG_LIVE_STREAM_QUIESCENT
:
346 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
348 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
350 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
352 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
354 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
356 case LTTNG_LIVE_STREAM_EOF
:
359 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
363 * For active no data stream, fetch next data. It can be either:
364 * - quiescent: need to put it in the prio heap at quiescent end
366 * - have data: need to wire up first event into the prio heap,
367 * - have no data on this stream at this point: need to retry (AGAIN) or
371 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
372 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
373 struct lttng_live_stream_iterator
*lttng_live_stream
)
375 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
376 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
377 enum lttng_live_iterator_status ret
=
378 LTTNG_LIVE_ITERATOR_STATUS_OK
;
379 struct packet_index index
;
380 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
382 if (lttng_live_stream
->trace
->new_metadata_needed
) {
383 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
386 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
387 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
390 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
391 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
394 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
396 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
399 BT_ASSERT(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
400 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
401 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
402 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
404 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
405 last_inact_ts
== curr_inact_ts
) {
406 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
407 print_stream_state(lttng_live_stream
);
409 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
413 lttng_live_stream
->base_offset
= index
.offset
;
414 lttng_live_stream
->offset
= index
.offset
;
415 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
417 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
418 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
424 * Creation of the message requires the ctf trace class to be created
425 * beforehand, but the live protocol gives us all streams (including metadata)
426 * at once. So we split it in three steps: getting streams, getting metadata
427 * (which creates the ctf trace class), and then creating the per-stream
431 enum lttng_live_iterator_status
lttng_live_get_session(
432 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
433 struct lttng_live_session
*session
)
435 enum lttng_live_iterator_status status
;
439 if (!session
->attached
) {
440 ret
= lttng_live_attach_session(session
);
442 if (lttng_live_msg_iter
&& lttng_live_graph_is_canceled(
443 lttng_live_msg_iter
)) {
444 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
446 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
452 status
= lttng_live_get_new_streams(session
);
453 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
454 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
457 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
458 struct lttng_live_trace
*trace
=
459 g_ptr_array_index(session
->traces
, trace_idx
);
461 status
= lttng_live_metadata_update(trace
);
462 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
463 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
467 status
= lttng_live_lazy_msg_init(session
);
474 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
476 uint64_t session_idx
;
478 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
480 struct lttng_live_session
*session
=
481 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
482 session
->new_streams_needed
= true;
487 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
489 uint64_t session_idx
, trace_idx
;
491 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
493 struct lttng_live_session
*session
=
494 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
495 session
->new_streams_needed
= true;
496 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
498 struct lttng_live_trace
*trace
=
499 g_ptr_array_index(session
->traces
, trace_idx
);
500 trace
->new_metadata_needed
= true;
506 enum lttng_live_iterator_status
507 lttng_live_iterator_handle_new_streams_and_metadata(
508 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
510 enum lttng_live_iterator_status ret
=
511 LTTNG_LIVE_ITERATOR_STATUS_OK
;
512 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
513 struct lttng_live_session
*session
;
514 enum session_not_found_action sess_not_found_act
=
515 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
518 * In a remotely distant future, we could add a "new
519 * session" flag to the protocol, which would tell us that we
520 * need to query for new sessions even though we have sessions
523 if (lttng_live_msg_iter
->sessions
->len
== 0) {
524 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
525 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
529 * Retry to create a viewer session for the requested
532 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
533 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
539 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
541 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
543 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
545 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
547 case LTTNG_LIVE_ITERATOR_STATUS_END
:
548 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
553 if (!session
->closed
) {
554 nr_sessions_opened
++;
558 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
559 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
560 nr_sessions_opened
== 0) {
561 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
567 enum lttng_live_iterator_status
emit_inactivity_message(
568 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
569 struct lttng_live_stream_iterator
*stream_iter
,
570 bt_message
**message
, uint64_t timestamp
)
572 enum lttng_live_iterator_status ret
=
573 LTTNG_LIVE_ITERATOR_STATUS_OK
;
574 bt_message
*msg
= NULL
;
576 BT_ASSERT(stream_iter
->trace
->clock_class
);
578 msg
= bt_message_message_iterator_inactivity_create(
579 lttng_live_msg_iter
->self_msg_iter
,
580 stream_iter
->trace
->clock_class
,
591 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
592 bt_message_put_ref(msg
);
597 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
598 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
599 struct lttng_live_stream_iterator
*lttng_live_stream
,
600 bt_message
**message
)
602 enum lttng_live_iterator_status ret
=
603 LTTNG_LIVE_ITERATOR_STATUS_OK
;
605 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
606 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
609 if (lttng_live_stream
->current_inactivity_ts
==
610 lttng_live_stream
->last_inactivity_ts
) {
611 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
612 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
616 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
617 message
, lttng_live_stream
->current_inactivity_ts
);
619 lttng_live_stream
->last_inactivity_ts
=
620 lttng_live_stream
->current_inactivity_ts
;
626 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
627 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
628 const bt_message
*msg
, int64_t last_msg_ts_ns
,
631 const bt_clock_class
*clock_class
= NULL
;
632 const bt_clock_snapshot
*clock_snapshot
= NULL
;
634 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
635 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
640 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
641 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
644 switch (bt_message_get_type(msg
)) {
645 case BT_MESSAGE_TYPE_EVENT
:
647 bt_message_event_borrow_stream_class_default_clock_class_const(
649 BT_ASSERT(clock_class
);
651 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
654 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
656 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
658 BT_ASSERT(clock_class
);
660 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
663 case BT_MESSAGE_TYPE_PACKET_END
:
665 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
667 BT_ASSERT(clock_class
);
669 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
672 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
674 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
676 BT_ASSERT(clock_class
);
678 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
681 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
683 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
685 BT_ASSERT(clock_class
);
687 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
690 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
692 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
696 /* All the other messages have a higher priority */
697 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
698 *ts_ns
= last_msg_ts_ns
;
702 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
703 BT_ASSERT(clock_class
);
705 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
707 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
708 "clock-snapshot-addr=%p", clock_snapshot
);
719 BT_COMP_LOGD("Found message's timestamp: "
720 "iter-data-addr=%p, msg-addr=%p, "
721 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
722 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
729 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
730 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
731 struct lttng_live_stream_iterator
*lttng_live_stream
,
732 bt_message
**message
)
734 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
735 enum bt_msg_iter_status status
;
736 uint64_t session_idx
, trace_idx
;
737 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
738 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
740 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
742 struct lttng_live_session
*session
=
743 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
745 if (session
->new_streams_needed
) {
746 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
749 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
751 struct lttng_live_trace
*trace
=
752 g_ptr_array_index(session
->traces
, trace_idx
);
753 if (trace
->new_metadata_needed
) {
754 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
760 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
761 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
765 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
766 lttng_live_msg_iter
->self_msg_iter
, message
);
768 case BT_MSG_ITER_STATUS_EOF
:
769 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
771 case BT_MSG_ITER_STATUS_OK
:
772 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
774 case BT_MSG_ITER_STATUS_AGAIN
:
776 * Continue immediately (end of packet). The next
777 * get_index may return AGAIN to delay the following
780 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
782 case BT_MSG_ITER_STATUS_INVAL
:
783 /* No argument provided by the user, so don't return INVAL. */
784 case BT_MSG_ITER_STATUS_ERROR
:
786 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
787 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
788 lttng_live_stream
->msg_iter
);
797 enum lttng_live_iterator_status
lttng_live_iterator_close_stream(
798 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
799 struct lttng_live_stream_iterator
*stream_iter
,
800 bt_message
**curr_msg
)
802 enum lttng_live_iterator_status live_status
=
803 LTTNG_LIVE_ITERATOR_STATUS_OK
;
805 * The viewer has hung up on us so we are closing the stream. The
806 * `bt_msg_iter` should simply realize that it needs to close the
807 * stream properly by emitting the necessary stream end message.
809 enum bt_msg_iter_status status
=
810 bt_msg_iter_get_next_message(stream_iter
->msg_iter
,
811 lttng_live_msg_iter
->self_msg_iter
, curr_msg
);
813 if (status
== BT_MSG_ITER_STATUS_ERROR
) {
814 live_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
818 BT_ASSERT(status
== BT_MSG_ITER_STATUS_OK
);
826 * handle_no_data_streams()
828 * - for each ACTIVE_NO_DATA stream:
829 * - query relayd for stream data, or quiescence info.
830 * - if need metadata, get metadata, goto retry.
831 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
832 * - if quiescent, move to QUIESCENT streams
833 * - if fetched data, move to ACTIVE_DATA streams
834 * (at this point each stream either has data, or is quiescent)
838 * handle_new_streams_and_metadata()
839 * - query relayd for known streams, add them as ACTIVE_NO_DATA
840 * - query relayd for metadata
842 * call handle_active_no_data_streams()
844 * handle_quiescent_streams()
845 * - if at least one stream is ACTIVE_DATA:
846 * - peek stream event with lowest timestamp -> next_ts
847 * - for each quiescent stream
848 * - if next_ts >= quiescent end
849 * - set state to ACTIVE_NO_DATA
851 * - for each quiescent stream
852 * - set state to ACTIVE_NO_DATA
854 * call handle_active_no_data_streams()
856 * handle_active_data_streams()
857 * - if at least one stream is ACTIVE_DATA:
858 * - get stream event with lowest timestamp from heap
859 * - make that stream event the current message.
860 * - move this stream heap position to its next event
861 * - if we need to fetch data from relayd, move
862 * stream to ACTIVE_NO_DATA.
866 * end criterion: ctrl-c on client. If relayd exits or the session
867 * closes on the relay daemon side, we keep on waiting for streams.
868 * Eventually handle --end timestamp (also an end criterion).
870 * When disconnected from relayd: try to re-connect endlessly.
873 enum lttng_live_iterator_status
lttng_live_iterator_next_on_stream(
874 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
875 struct lttng_live_stream_iterator
*stream_iter
,
876 bt_message
**curr_msg
)
878 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
879 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
880 enum lttng_live_iterator_status live_status
;
882 if (stream_iter
->has_stream_hung_up
) {
884 * The stream has hung up and the stream was properly closed
885 * during the last call to the current function. Return _END
886 * status now so that this stream iterator is removed for the
887 * stream iterator list.
889 live_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
894 print_stream_state(stream_iter
);
895 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
896 lttng_live_msg_iter
);
897 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
900 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
901 lttng_live_msg_iter
, stream_iter
);
903 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
904 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
906 * We overwrite `live_status` since `curr_msg` is
907 * likely set to a valid message in this function.
909 live_status
= lttng_live_iterator_close_stream(
910 lttng_live_msg_iter
, stream_iter
, curr_msg
);
914 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
915 lttng_live_msg_iter
, stream_iter
, curr_msg
);
916 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
917 BT_ASSERT(!*curr_msg
);
923 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
924 lttng_live_msg_iter
, stream_iter
, curr_msg
);
925 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
926 BT_ASSERT(!*curr_msg
);
930 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
938 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
939 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
940 struct lttng_live_trace
*live_trace
,
941 struct lttng_live_stream_iterator
**candidate_stream_iter
)
943 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
944 enum lttng_live_iterator_status stream_iter_status
;;
945 int64_t curr_candidate_msg_ts
= INT64_MAX
;
946 uint64_t stream_iter_idx
;
947 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
948 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
950 BT_ASSERT(live_trace
);
951 BT_ASSERT(live_trace
->stream_iterators
);
953 * Update the current message of every stream iterators of this trace.
954 * The current msg of every stream must have a timestamp equal or
955 * larger than the last message returned by this iterator. We must
956 * ensure monotonicity.
959 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
960 bool stream_iter_is_ended
= false;
961 struct lttng_live_stream_iterator
*stream_iter
=
962 g_ptr_array_index(live_trace
->stream_iterators
,
966 * Find if there is are now current message for this stream
969 while (!stream_iter
->current_msg
) {
970 bt_message
*msg
= NULL
;
971 int64_t curr_msg_ts_ns
= INT64_MAX
;
972 stream_iter_status
= lttng_live_iterator_next_on_stream(
973 lttng_live_msg_iter
, stream_iter
, &msg
);
975 BT_COMP_LOGD("live stream iterator returned status :%s",
976 print_live_iterator_status(stream_iter_status
));
977 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
978 stream_iter_is_ended
= true;
982 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
989 * Get the timestamp in nanoseconds from origin of this
992 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
993 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
997 * Check if the message of the current live stream
998 * iterator occured at the exact same time or after the
999 * last message returned by this component's message
1000 * iterator. If not, we return an error.
1002 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
1003 stream_iter
->current_msg
= msg
;
1004 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
1007 * We received a message in the past. To ensure
1008 * monotonicity, we can't send it forward.
1010 BT_COMP_LOGE("Message's timestamp is less than "
1011 "lttng-live's message iterator's last "
1012 "returned timestamp: "
1013 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1014 "last-msg-ts=%" PRId64
,
1015 lttng_live_msg_iter
, curr_msg_ts_ns
,
1016 lttng_live_msg_iter
->last_msg_ts_ns
);
1017 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1022 if (!stream_iter_is_ended
&&
1023 stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1025 * Update the current best candidate message for the
1026 * stream iterator of thise live trace to be forwarded
1029 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1030 curr_candidate_stream_iter
= stream_iter
;
1033 if (stream_iter_is_ended
) {
1035 * The live stream iterator is ENDed. We remove that
1036 * iterator from the list and we restart the iteration
1037 * at the beginning of the live stream iterator array
1038 * to because the removal will shuffle the array.
1040 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1042 stream_iter_idx
= 0;
1048 if (curr_candidate_stream_iter
) {
1049 *candidate_stream_iter
= curr_candidate_stream_iter
;
1050 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1053 * The only case where we don't have a candidate for this trace
1054 * is if we reached the end of all the iterators.
1056 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1057 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1061 return stream_iter_status
;
1065 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1066 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1067 struct lttng_live_session
*session
,
1068 struct lttng_live_stream_iterator
**candidate_session_stream_iter
)
1070 enum lttng_live_iterator_status stream_iter_status
;
1071 uint64_t trace_idx
= 0;
1072 int64_t curr_candidate_msg_ts
= INT64_MAX
;
1073 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
1076 * Make sure we are attached to the session and look for new streams
1079 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1080 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1081 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1082 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1086 BT_ASSERT(session
->traces
);
1089 * Use while loops here rather then for loops so we can restart the
1090 * iteration if an element is removed from the array during the
1093 while (trace_idx
< session
->traces
->len
) {
1094 bool trace_is_ended
= false;
1095 struct lttng_live_stream_iterator
*stream_iter
;
1096 struct lttng_live_trace
*trace
=
1097 g_ptr_array_index(session
->traces
, trace_idx
);
1099 stream_iter_status
= next_stream_iterator_for_trace(
1100 lttng_live_msg_iter
, trace
, &stream_iter
);
1101 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1103 * All the live stream iterators for this trace are
1104 * ENDed. Remove the trace from this session.
1106 trace_is_ended
= true;
1107 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1111 if (!trace_is_ended
) {
1112 BT_ASSERT(stream_iter
);
1114 if (stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1115 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1116 curr_candidate_stream_iter
= stream_iter
;
1120 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1124 if (curr_candidate_stream_iter
) {
1125 *candidate_session_stream_iter
= curr_candidate_stream_iter
;
1126 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1129 * The only cases where we don't have a candidate for this
1131 * 1. if we reached the end of all the iterators of all the
1132 * traces of this session,
1133 * 2. if we never had live stream iterator in the first place.
1135 * In either cases, we return END.
1137 BT_ASSERT(session
->traces
->len
== 0);
1138 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1141 return stream_iter_status
;
1145 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1149 for (i
= 0; i
< count
; i
++) {
1150 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1155 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1156 bt_self_message_iterator
*self_msg_it
,
1157 bt_message_array_const msgs
, uint64_t capacity
,
1160 bt_component_class_message_iterator_next_method_status status
;
1161 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1162 bt_self_message_iterator_get_data(self_msg_it
);
1163 struct lttng_live_component
*lttng_live
=
1164 lttng_live_msg_iter
->lttng_live_comp
;
1165 enum lttng_live_iterator_status stream_iter_status
;
1166 uint64_t session_idx
;
1170 BT_ASSERT(lttng_live_msg_iter
);
1173 * Clear all the invalid message reference that might be left over in
1176 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1179 * If no session are exposed on the relay found at the url provided by
1180 * the user, session count will be 0. In this case, we return status
1181 * end to return gracefully.
1183 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1184 if (lttng_live
->params
.sess_not_found_act
!=
1185 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1186 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1190 * The are no more active session for this session
1191 * name. Retry to create a viewer session for the
1192 * requested session name.
1194 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1195 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1201 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1202 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1206 * Here the muxing of message is done.
1208 * We need to iterate over all the streams of all the traces of all the
1209 * viewer sessions in order to get the message with the smallest
1210 * timestamp. In this case, a session is a viewer session and there is
1211 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1212 * kernel). Each viewer session can have multiple traces, for example,
1213 * 64bit UST viewer sessions could have multiple per-pid traces.
1215 * We iterate over the streams of each traces to update and see what is
1216 * their next message's timestamp. From those timestamps, we select the
1217 * message with the smallest timestamp as the best candidate message
1218 * for that trace and do the same thing across all the sessions.
1220 * We then compare the timestamp of best candidate message of all the
1221 * sessions to pick the message with the smallest timestamp and we
1224 while (*count
< capacity
) {
1225 struct lttng_live_stream_iterator
*next_stream_iter
= NULL
,
1226 *candidate_stream_iter
= NULL
;
1227 int64_t next_msg_ts_ns
= INT64_MAX
;
1229 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1232 * Use a while loop instead of a for loop so we can restart the
1233 * iteration if we remove an element. We can safely call
1234 * next_stream_iterator_for_session() multiple times on the
1235 * same session as we only fetch a new message if there is no
1236 * current next message for each live stream iterator.
1237 * If all live stream iterator of that session already have a
1238 * current next message, the function will simply exit return
1239 * the same candidate live stream iterator every time.
1241 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1242 struct lttng_live_session
*session
=
1243 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1246 /* Find the best candidate message to send downstream. */
1247 stream_iter_status
= next_stream_iterator_for_session(
1248 lttng_live_msg_iter
, session
,
1249 &candidate_stream_iter
);
1251 /* If we receive an END status, it means that either:
1252 * - Those traces never had active streams (UST with no
1253 * data produced yet),
1254 * - All live stream iterators have ENDed.*/
1255 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1256 if (session
->closed
&& session
->traces
->len
== 0) {
1258 * Remove the session from the list and restart the
1259 * iteration at the beginning of the array since the
1260 * removal shuffle the elements of the array.
1262 g_ptr_array_remove_index_fast(
1263 lttng_live_msg_iter
->sessions
,
1272 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1276 if (candidate_stream_iter
->current_msg_ts_ns
<= next_msg_ts_ns
) {
1277 next_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1278 next_stream_iter
= candidate_stream_iter
;
1284 if (!next_stream_iter
) {
1285 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1289 BT_ASSERT(next_stream_iter
->current_msg
);
1290 /* Ensure monotonicity. */
1291 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1292 next_stream_iter
->current_msg_ts_ns
);
1295 * Insert the next message to the message batch. This will set
1296 * stream iterator current messsage to NULL so that next time
1297 * we fetch the next message of that stream iterator
1299 BT_MESSAGE_MOVE_REF(msgs
[*count
], next_stream_iter
->current_msg
);
1302 /* Update the last timestamp in nanoseconds sent downstream. */
1303 lttng_live_msg_iter
->last_msg_ts_ns
= next_msg_ts_ns
;
1304 next_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1306 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1309 switch (stream_iter_status
) {
1310 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1311 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1314 * We received a again status but we have some messages
1315 * to send downstream. We send them and return OK for
1316 * now. On the next call we return again if there are
1317 * still no new message to send.
1319 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1321 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1324 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1325 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1327 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1328 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1330 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1331 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1332 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1333 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1334 /* Put all existing messages on error. */
1335 put_messages(msgs
, *count
);
1346 bt_component_class_message_iterator_init_method_status
lttng_live_msg_iter_init(
1347 bt_self_message_iterator
*self_msg_it
,
1348 bt_self_component_source
*self_comp_src
,
1349 bt_self_component_port_output
*self_port
)
1351 bt_component_class_message_iterator_init_method_status ret
=
1352 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1353 bt_self_component
*self_comp
=
1354 bt_self_component_source_as_self_component(self_comp_src
);
1355 struct lttng_live_component
*lttng_live
;
1356 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1357 bt_logging_level log_level
;
1359 BT_ASSERT(self_msg_it
);
1361 lttng_live
= bt_self_component_get_data(self_comp
);
1362 log_level
= lttng_live
->log_level
;
1363 self_comp
= lttng_live
->self_comp
;
1365 /* There can be only one downstream iterator at the same time. */
1366 BT_ASSERT(!lttng_live
->has_msg_iter
);
1367 lttng_live
->has_msg_iter
= true;
1369 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1370 if (!lttng_live_msg_iter
) {
1371 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1375 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1376 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1377 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1378 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1380 lttng_live_msg_iter
->active_stream_iter
= 0;
1381 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1382 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1383 (GDestroyNotify
) lttng_live_destroy_session
);
1384 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1386 lttng_live_msg_iter
->viewer_connection
=
1387 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1388 lttng_live_msg_iter
, log_level
);
1389 if (!lttng_live_msg_iter
->viewer_connection
) {
1393 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1396 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1397 switch (lttng_live
->params
.sess_not_found_act
) {
1398 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1399 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1400 "session. Keep trying to connect because of "
1401 "%s=\"%s\" component parameter: url=\"%s\"",
1402 SESS_NOT_FOUND_ACTION_PARAM
,
1403 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1404 lttng_live
->params
.url
->str
);
1406 case SESSION_NOT_FOUND_ACTION_FAIL
:
1407 BT_COMP_LOGE("Unable to connect to the requested live viewer "
1408 "session. Fail the message iterator"
1409 "initialization because of %s=\"%s\" "
1410 "component parameter: url =\"%s\"",
1411 SESS_NOT_FOUND_ACTION_PARAM
,
1412 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1413 lttng_live
->params
.url
->str
);
1415 case SESSION_NOT_FOUND_ACTION_END
:
1416 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1417 "session. End gracefully at the first _next() "
1418 "call because of %s=\"%s\" component parameter: "
1419 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1420 SESS_NOT_FOUND_ACTION_END_STR
,
1421 lttng_live
->params
.url
->str
);
1423 case SESSION_NOT_FOUND_ACTION_UNKNOWN
:
1426 BT_COMP_LOGE("Unknown action for session not found"
1427 "error. Fail the message iterator"
1428 "initialization because of %s=\"%s\" "
1429 "component parameter: url =\"%s\"",
1430 SESS_NOT_FOUND_ACTION_PARAM
,
1431 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1432 lttng_live
->params
.url
->str
);
1437 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1441 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1442 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1448 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1449 const bt_value
*params
, const bt_value
**result
,
1450 bt_logging_level log_level
)
1452 bt_component_class_query_method_status status
=
1453 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1454 const bt_value
*url_value
= NULL
;
1456 struct live_viewer_connection
*viewer_connection
= NULL
;
1457 bt_self_component
*self_comp
= NULL
;
1459 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1461 BT_COMP_LOGE("Mandatory `%s` parameter missing", URL_PARAM
);
1462 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1466 if (!bt_value_is_string(url_value
)) {
1467 BT_COMP_LOGE("`%s` parameter is required to be a string value",
1469 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1473 url
= bt_value_string_get(url_value
);
1475 viewer_connection
= live_viewer_connection_create(url
, true, NULL
,
1477 if (!viewer_connection
) {
1481 status
= live_viewer_connection_list_sessions(viewer_connection
,
1483 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1490 BT_VALUE_PUT_REF_AND_RESET(*result
);
1493 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1497 if (viewer_connection
) {
1498 live_viewer_connection_destroy(viewer_connection
);
1504 bt_component_class_query_method_status
lttng_live_query(
1505 bt_self_component_class_source
*comp_class
,
1506 bt_private_query_executor
*priv_query_exec
,
1507 const char *object
, const bt_value
*params
,
1508 __attribute__((unused
)) void *method_data
,
1509 const bt_value
**result
)
1511 bt_component_class_query_method_status status
=
1512 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1513 bt_self_component
*self_comp
= NULL
;
1514 bt_logging_level log_level
= bt_query_executor_get_logging_level(
1515 bt_private_query_executor_as_query_executor_const(
1518 if (strcmp(object
, "sessions") == 0) {
1519 status
= lttng_live_query_list_sessions(params
, result
,
1522 BT_COMP_LOGI("Unknown query object `%s`", object
);
1523 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT
;
1532 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1537 if (lttng_live
->params
.url
) {
1538 g_string_free(lttng_live
->params
.url
, TRUE
);
1544 void lttng_live_component_finalize(bt_self_component_source
*component
)
1546 void *data
= bt_self_component_get_data(
1547 bt_self_component_source_as_self_component(component
));
1552 lttng_live_component_destroy_data(data
);
1556 enum session_not_found_action
parse_session_not_found_action_param(
1557 const bt_value
*no_session_param
)
1559 enum session_not_found_action action
;
1560 const char *no_session_act_str
;
1561 no_session_act_str
= bt_value_string_get(no_session_param
);
1562 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1563 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1564 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1565 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1566 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0) {
1567 action
= SESSION_NOT_FOUND_ACTION_END
;
1569 action
= SESSION_NOT_FOUND_ACTION_UNKNOWN
;
1575 struct lttng_live_component
*lttng_live_component_create(const bt_value
*params
,
1576 bt_logging_level log_level
, bt_self_component
*self_comp
)
1578 struct lttng_live_component
*lttng_live
;
1579 const bt_value
*value
= NULL
;
1582 lttng_live
= g_new0(struct lttng_live_component
, 1);
1586 lttng_live
->log_level
= log_level
;
1587 lttng_live
->self_comp
= self_comp
;
1588 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1589 lttng_live
->has_msg_iter
= false;
1591 value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1592 if (!value
|| !bt_value_is_string(value
)) {
1593 BT_COMP_LOGW("Mandatory `%s` parameter missing or not a string",
1597 url
= bt_value_string_get(value
);
1598 lttng_live
->params
.url
= g_string_new(url
);
1599 if (!lttng_live
->params
.url
) {
1603 value
= bt_value_map_borrow_entry_value_const(params
,
1604 SESS_NOT_FOUND_ACTION_PARAM
);
1606 if (value
&& bt_value_is_string(value
)) {
1607 lttng_live
->params
.sess_not_found_act
=
1608 parse_session_not_found_action_param(value
);
1609 if (lttng_live
->params
.sess_not_found_act
== SESSION_NOT_FOUND_ACTION_UNKNOWN
) {
1610 BT_COMP_LOGE("Unexpected value for `%s` parameter: "
1611 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1612 bt_value_string_get(value
));
1616 BT_COMP_LOGW("Optional `%s` parameter is missing or "
1617 "not a string value. Defaulting to %s=\"%s\".",
1618 SESS_NOT_FOUND_ACTION_PARAM
,
1619 SESS_NOT_FOUND_ACTION_PARAM
,
1620 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1621 lttng_live
->params
.sess_not_found_act
=
1622 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1628 lttng_live_component_destroy_data(lttng_live
);
1635 bt_component_class_init_method_status
lttng_live_component_init(
1636 bt_self_component_source
*self_comp_src
,
1637 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1639 struct lttng_live_component
*lttng_live
;
1640 bt_component_class_init_method_status ret
=
1641 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
1642 bt_self_component
*self_comp
=
1643 bt_self_component_source_as_self_component(self_comp_src
);
1644 bt_logging_level log_level
= bt_component_get_logging_level(
1645 bt_self_component_as_component(self_comp
));
1646 bt_self_component_add_port_status add_port_status
;
1648 lttng_live
= lttng_live_component_create(params
, log_level
, self_comp
);
1650 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1654 add_port_status
= bt_self_component_source_add_output_port(
1655 self_comp_src
, "out", NULL
, NULL
);
1656 switch (add_port_status
) {
1657 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
1658 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1660 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
1661 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1667 bt_self_component_set_data(self_comp
, lttng_live
);
1671 lttng_live_component_destroy_data(lttng_live
);