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 "plugins/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_component
*lttng_live
)
114 const bt_component
*component
;
122 component
= bt_self_component_as_component(lttng_live
->self_comp
);
123 ret
= bt_component_graph_is_canceled(component
);
130 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
134 struct lttng_live_trace
*ret_trace
= NULL
;
136 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
137 struct lttng_live_trace
*trace
=
138 g_ptr_array_index(session
->traces
, trace_idx
);
139 if (trace
->id
== trace_id
) {
150 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
152 bt_logging_level log_level
= trace
->log_level
;
153 bt_self_component
*self_comp
= trace
->self_comp
;
155 BT_COMP_LOGD("Destroy lttng_live_trace");
157 BT_ASSERT(trace
->stream_iterators
);
158 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
160 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
161 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
163 lttng_live_metadata_fini(trace
);
168 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
171 struct lttng_live_trace
*trace
= NULL
;
172 bt_logging_level log_level
= session
->log_level
;
173 bt_self_component
*self_comp
= session
->self_comp
;
175 trace
= g_new0(struct lttng_live_trace
, 1);
179 trace
->log_level
= session
->log_level
;
180 trace
->self_comp
= session
->self_comp
;
181 trace
->session
= session
;
182 trace
->id
= trace_id
;
183 trace
->trace_class
= NULL
;
185 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
186 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
187 BT_ASSERT(trace
->stream_iterators
);
188 trace
->new_metadata_needed
= true;
189 g_ptr_array_add(session
->traces
, trace
);
191 BT_COMP_LOGI("Create trace");
201 struct lttng_live_trace
*lttng_live_borrow_trace(
202 struct lttng_live_session
*session
, uint64_t trace_id
)
204 struct lttng_live_trace
*trace
;
206 trace
= lttng_live_find_trace(session
, trace_id
);
211 /* The session is the owner of the newly created trace. */
212 trace
= lttng_live_create_trace(session
, trace_id
);
219 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
220 uint64_t session_id
, const char *hostname
,
221 const char *session_name
)
224 struct lttng_live_session
*session
;
225 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
226 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
228 session
= g_new0(struct lttng_live_session
, 1);
233 session
->log_level
= lttng_live_msg_iter
->log_level
;
234 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
235 session
->id
= session_id
;
236 session
->traces
= g_ptr_array_new_with_free_func(
237 (GDestroyNotify
) lttng_live_destroy_trace
);
238 BT_ASSERT(session
->traces
);
239 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
240 session
->new_streams_needed
= true;
241 session
->hostname
= g_string_new(hostname
);
242 BT_ASSERT(session
->hostname
);
244 session
->session_name
= g_string_new(session_name
);
245 BT_ASSERT(session
->session_name
);
247 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
248 session
->id
, hostname
, session_name
);
249 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
252 BT_COMP_LOGE("Error adding session");
260 void lttng_live_destroy_session(struct lttng_live_session
*session
)
262 struct lttng_live_component
*live_comp
;
263 bt_logging_level log_level
;
264 bt_self_component
*self_comp
;
270 log_level
= session
->log_level
;
271 self_comp
= session
->self_comp
;
272 BT_COMP_LOGD("Destroy lttng live session");
273 if (session
->id
!= -1ULL) {
274 if (lttng_live_detach_session(session
)) {
275 live_comp
= session
->lttng_live_msg_iter
->lttng_live_comp
;
276 if (session
->lttng_live_msg_iter
&&
277 !lttng_live_graph_is_canceled(live_comp
)) {
278 /* Old relayd cannot detach sessions. */
279 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
286 if (session
->traces
) {
287 g_ptr_array_free(session
->traces
, TRUE
);
290 if (session
->hostname
) {
291 g_string_free(session
->hostname
, TRUE
);
293 if (session
->session_name
) {
294 g_string_free(session
->session_name
, TRUE
);
303 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
305 if (!lttng_live_msg_iter
) {
309 if (lttng_live_msg_iter
->sessions
) {
310 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
313 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
314 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
315 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
317 /* All stream iterators must be destroyed at this point. */
318 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
319 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
321 g_free(lttng_live_msg_iter
);
328 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
330 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
332 BT_ASSERT(self_msg_iter
);
334 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
335 BT_ASSERT(lttng_live_msg_iter
);
336 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
340 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
341 struct lttng_live_stream_iterator
*lttng_live_stream
)
343 bt_logging_level log_level
= lttng_live_stream
->log_level
;
344 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
346 switch (lttng_live_stream
->state
) {
347 case LTTNG_LIVE_STREAM_QUIESCENT
:
348 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
350 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
352 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
354 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
356 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
358 case LTTNG_LIVE_STREAM_EOF
:
361 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
365 * For active no data stream, fetch next data. It can be either:
366 * - quiescent: need to put it in the prio heap at quiescent end
368 * - have data: need to wire up first event into the prio heap,
369 * - have no data on this stream at this point: need to retry (AGAIN) or
373 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
374 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
375 struct lttng_live_stream_iterator
*lttng_live_stream
)
377 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
378 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
379 enum lttng_live_iterator_status ret
=
380 LTTNG_LIVE_ITERATOR_STATUS_OK
;
381 struct packet_index index
;
382 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
384 if (lttng_live_stream
->trace
->new_metadata_needed
) {
385 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
388 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
389 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
392 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
393 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
396 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
398 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
401 BT_ASSERT(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
402 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
403 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
404 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
406 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
407 last_inact_ts
== curr_inact_ts
) {
408 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
409 print_stream_state(lttng_live_stream
);
411 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
415 lttng_live_stream
->base_offset
= index
.offset
;
416 lttng_live_stream
->offset
= index
.offset
;
417 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
419 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
420 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
426 * Creation of the message requires the ctf trace class to be created
427 * beforehand, but the live protocol gives us all streams (including metadata)
428 * at once. So we split it in three steps: getting streams, getting metadata
429 * (which creates the ctf trace class), and then creating the per-stream
433 enum lttng_live_iterator_status
lttng_live_get_session(
434 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
435 struct lttng_live_session
*session
)
437 enum lttng_live_iterator_status status
;
441 if (!session
->attached
) {
442 ret
= lttng_live_attach_session(session
);
444 if (lttng_live_msg_iter
&& lttng_live_graph_is_canceled(
445 lttng_live_msg_iter
->lttng_live_comp
)) {
446 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
448 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
454 status
= lttng_live_get_new_streams(session
);
455 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
456 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
459 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
460 struct lttng_live_trace
*trace
=
461 g_ptr_array_index(session
->traces
, trace_idx
);
463 status
= lttng_live_metadata_update(trace
);
464 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
465 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
469 status
= lttng_live_lazy_msg_init(session
);
476 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
478 uint64_t session_idx
;
480 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
482 struct lttng_live_session
*session
=
483 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
484 session
->new_streams_needed
= true;
489 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
491 uint64_t session_idx
, trace_idx
;
493 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
495 struct lttng_live_session
*session
=
496 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
497 session
->new_streams_needed
= true;
498 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
500 struct lttng_live_trace
*trace
=
501 g_ptr_array_index(session
->traces
, trace_idx
);
502 trace
->new_metadata_needed
= true;
508 enum lttng_live_iterator_status
509 lttng_live_iterator_handle_new_streams_and_metadata(
510 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
512 enum lttng_live_iterator_status ret
=
513 LTTNG_LIVE_ITERATOR_STATUS_OK
;
514 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
515 struct lttng_live_session
*session
;
516 enum session_not_found_action sess_not_found_act
=
517 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
520 * In a remotely distant future, we could add a "new
521 * session" flag to the protocol, which would tell us that we
522 * need to query for new sessions even though we have sessions
525 if (lttng_live_msg_iter
->sessions
->len
== 0) {
526 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
527 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
531 * Retry to create a viewer session for the requested
534 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
535 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
541 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
543 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
545 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
547 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
549 case LTTNG_LIVE_ITERATOR_STATUS_END
:
550 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
555 if (!session
->closed
) {
556 nr_sessions_opened
++;
560 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
561 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
562 nr_sessions_opened
== 0) {
563 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
569 enum lttng_live_iterator_status
emit_inactivity_message(
570 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
571 struct lttng_live_stream_iterator
*stream_iter
,
572 bt_message
**message
, uint64_t timestamp
)
574 enum lttng_live_iterator_status ret
=
575 LTTNG_LIVE_ITERATOR_STATUS_OK
;
576 bt_message
*msg
= NULL
;
578 BT_ASSERT(stream_iter
->trace
->clock_class
);
580 msg
= bt_message_message_iterator_inactivity_create(
581 lttng_live_msg_iter
->self_msg_iter
,
582 stream_iter
->trace
->clock_class
,
593 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
594 bt_message_put_ref(msg
);
599 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
600 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
601 struct lttng_live_stream_iterator
*lttng_live_stream
,
602 bt_message
**message
)
604 enum lttng_live_iterator_status ret
=
605 LTTNG_LIVE_ITERATOR_STATUS_OK
;
607 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
608 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
611 if (lttng_live_stream
->current_inactivity_ts
==
612 lttng_live_stream
->last_inactivity_ts
) {
613 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
614 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
618 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
619 message
, lttng_live_stream
->current_inactivity_ts
);
621 lttng_live_stream
->last_inactivity_ts
=
622 lttng_live_stream
->current_inactivity_ts
;
628 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
629 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
630 const bt_message
*msg
, int64_t last_msg_ts_ns
,
633 const bt_clock_class
*clock_class
= NULL
;
634 const bt_clock_snapshot
*clock_snapshot
= NULL
;
636 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
637 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
638 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
643 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
644 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
647 switch (bt_message_get_type(msg
)) {
648 case BT_MESSAGE_TYPE_EVENT
:
650 bt_message_event_borrow_stream_class_default_clock_class_const(
652 BT_ASSERT(clock_class
);
654 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
657 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
659 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
661 BT_ASSERT(clock_class
);
663 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
666 case BT_MESSAGE_TYPE_PACKET_END
:
668 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
670 BT_ASSERT(clock_class
);
672 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
675 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
677 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
679 BT_ASSERT(clock_class
);
681 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
684 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
686 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
688 BT_ASSERT(clock_class
);
690 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
693 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
695 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
697 BT_ASSERT(clock_class
);
699 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
700 msg
, &clock_snapshot
);
701 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
702 goto no_clock_snapshot
;
706 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
708 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
710 BT_ASSERT(clock_class
);
712 sa_cs_state
= bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
713 msg
, &clock_snapshot
);
714 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
715 goto no_clock_snapshot
;
719 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
721 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
725 /* All the other messages have a higher priority */
726 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
727 *ts_ns
= last_msg_ts_ns
;
731 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
732 BT_ASSERT(clock_class
);
734 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
736 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
737 "clock-snapshot-addr=%p", clock_snapshot
);
744 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
745 "using the last message timestamp.");
746 *ts_ns
= last_msg_ts_ns
;
754 BT_COMP_LOGD("Found message's timestamp: "
755 "iter-data-addr=%p, msg-addr=%p, "
756 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
757 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
764 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
765 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
766 struct lttng_live_stream_iterator
*lttng_live_stream
,
767 bt_message
**message
)
769 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
770 enum bt_msg_iter_status status
;
771 uint64_t session_idx
, trace_idx
;
772 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
773 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
775 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
777 struct lttng_live_session
*session
=
778 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
780 if (session
->new_streams_needed
) {
781 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
784 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
786 struct lttng_live_trace
*trace
=
787 g_ptr_array_index(session
->traces
, trace_idx
);
788 if (trace
->new_metadata_needed
) {
789 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
795 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
796 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
800 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
801 lttng_live_msg_iter
->self_msg_iter
, message
);
803 case BT_MSG_ITER_STATUS_EOF
:
804 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
806 case BT_MSG_ITER_STATUS_OK
:
807 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
809 case BT_MSG_ITER_STATUS_AGAIN
:
811 * Continue immediately (end of packet). The next
812 * get_index may return AGAIN to delay the following
815 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
817 case BT_MSG_ITER_STATUS_INVAL
:
818 /* No argument provided by the user, so don't return INVAL. */
819 case BT_MSG_ITER_STATUS_ERROR
:
821 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
822 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
823 lttng_live_stream
->msg_iter
);
833 * handle_no_data_streams()
835 * - for each ACTIVE_NO_DATA stream:
836 * - query relayd for stream data, or quiescence info.
837 * - if need metadata, get metadata, goto retry.
838 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
839 * - if quiescent, move to QUIESCENT streams
840 * - if fetched data, move to ACTIVE_DATA streams
841 * (at this point each stream either has data, or is quiescent)
845 * handle_new_streams_and_metadata()
846 * - query relayd for known streams, add them as ACTIVE_NO_DATA
847 * - query relayd for metadata
849 * call handle_active_no_data_streams()
851 * handle_quiescent_streams()
852 * - if at least one stream is ACTIVE_DATA:
853 * - peek stream event with lowest timestamp -> next_ts
854 * - for each quiescent stream
855 * - if next_ts >= quiescent end
856 * - set state to ACTIVE_NO_DATA
858 * - for each quiescent stream
859 * - set state to ACTIVE_NO_DATA
861 * call handle_active_no_data_streams()
863 * handle_active_data_streams()
864 * - if at least one stream is ACTIVE_DATA:
865 * - get stream event with lowest timestamp from heap
866 * - make that stream event the current message.
867 * - move this stream heap position to its next event
868 * - if we need to fetch data from relayd, move
869 * stream to ACTIVE_NO_DATA.
873 * end criterion: ctrl-c on client. If relayd exits or the session
874 * closes on the relay daemon side, we keep on waiting for streams.
875 * Eventually handle --end timestamp (also an end criterion).
877 * When disconnected from relayd: try to re-connect endlessly.
880 enum lttng_live_iterator_status
lttng_live_iterator_next_on_stream(
881 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
882 struct lttng_live_stream_iterator
*stream_iter
,
883 bt_message
**curr_msg
)
885 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
886 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
887 enum lttng_live_iterator_status live_status
;
890 print_stream_state(stream_iter
);
891 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
892 lttng_live_msg_iter
);
893 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
896 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
897 lttng_live_msg_iter
, stream_iter
);
898 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
901 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
902 lttng_live_msg_iter
, stream_iter
, curr_msg
);
903 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
904 BT_ASSERT(*curr_msg
== NULL
);
910 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
911 lttng_live_msg_iter
, stream_iter
, curr_msg
);
912 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
913 BT_ASSERT(*curr_msg
== NULL
);
917 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
925 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
926 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
927 struct lttng_live_trace
*live_trace
,
928 struct lttng_live_stream_iterator
**candidate_stream_iter
)
930 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
931 enum lttng_live_iterator_status stream_iter_status
;;
932 int64_t curr_candidate_msg_ts
= INT64_MAX
;
933 uint64_t stream_iter_idx
;
934 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
935 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
937 BT_ASSERT(live_trace
);
938 BT_ASSERT(live_trace
->stream_iterators
);
940 * Update the current message of every stream iterators of this trace.
941 * The current msg of every stream must have a timestamp equal or
942 * larger than the last message returned by this iterator. We must
943 * ensure monotonicity.
946 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
947 bool stream_iter_is_ended
= false;
948 struct lttng_live_stream_iterator
*stream_iter
=
949 g_ptr_array_index(live_trace
->stream_iterators
,
953 * Find if there is are now current message for this stream
956 while (!stream_iter
->current_msg
) {
957 bt_message
*msg
= NULL
;
958 int64_t curr_msg_ts_ns
= INT64_MAX
;
959 stream_iter_status
= lttng_live_iterator_next_on_stream(
960 lttng_live_msg_iter
, stream_iter
, &msg
);
962 BT_COMP_LOGD("live stream iterator returned status :%s",
963 print_live_iterator_status(stream_iter_status
));
964 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
965 stream_iter_is_ended
= true;
969 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
976 * Get the timestamp in nanoseconds from origin of this
979 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
980 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
984 * Check if the message of the current live stream
985 * iterator occured at the exact same time or after the
986 * last message returned by this component's message
987 * iterator. If not, we return an error.
989 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
990 stream_iter
->current_msg
= msg
;
991 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
994 * We received a message in the past. To ensure
995 * monotonicity, we can't send it forward.
997 BT_COMP_LOGE("Message's timestamp is less than "
998 "lttng-live's message iterator's last "
999 "returned timestamp: "
1000 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
1001 "last-msg-ts=%" PRId64
,
1002 lttng_live_msg_iter
, curr_msg_ts_ns
,
1003 lttng_live_msg_iter
->last_msg_ts_ns
);
1004 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1009 if (!stream_iter_is_ended
&&
1010 stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1012 * Update the current best candidate message for the
1013 * stream iterator of thise live trace to be forwarded
1016 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1017 curr_candidate_stream_iter
= stream_iter
;
1020 if (stream_iter_is_ended
) {
1022 * The live stream iterator is ENDed. We remove that
1023 * iterator from the list and we restart the iteration
1024 * at the beginning of the live stream iterator array
1025 * to because the removal will shuffle the array.
1027 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1029 stream_iter_idx
= 0;
1035 if (curr_candidate_stream_iter
) {
1036 *candidate_stream_iter
= curr_candidate_stream_iter
;
1037 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1040 * The only case where we don't have a candidate for this trace
1041 * is if we reached the end of all the iterators.
1043 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1044 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1048 return stream_iter_status
;
1052 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1053 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1054 struct lttng_live_session
*session
,
1055 struct lttng_live_stream_iterator
**candidate_session_stream_iter
)
1057 enum lttng_live_iterator_status stream_iter_status
;
1058 uint64_t trace_idx
= 0;
1059 int64_t curr_candidate_msg_ts
= INT64_MAX
;
1060 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
1063 * Make sure we are attached to the session and look for new streams
1066 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1067 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1068 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1069 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1073 BT_ASSERT(session
->traces
);
1076 * Use while loops here rather then for loops so we can restart the
1077 * iteration if an element is removed from the array during the
1080 while (trace_idx
< session
->traces
->len
) {
1081 bool trace_is_ended
= false;
1082 struct lttng_live_stream_iterator
*stream_iter
;
1083 struct lttng_live_trace
*trace
=
1084 g_ptr_array_index(session
->traces
, trace_idx
);
1086 stream_iter_status
= next_stream_iterator_for_trace(
1087 lttng_live_msg_iter
, trace
, &stream_iter
);
1088 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1090 * All the live stream iterators for this trace are
1091 * ENDed. Remove the trace from this session.
1093 trace_is_ended
= true;
1094 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1098 if (!trace_is_ended
) {
1099 BT_ASSERT(stream_iter
);
1101 if (stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1102 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1103 curr_candidate_stream_iter
= stream_iter
;
1107 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1111 if (curr_candidate_stream_iter
) {
1112 *candidate_session_stream_iter
= curr_candidate_stream_iter
;
1113 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1116 * The only cases where we don't have a candidate for this
1118 * 1. if we reached the end of all the iterators of all the
1119 * traces of this session,
1120 * 2. if we never had live stream iterator in the first place.
1122 * In either cases, we return END.
1124 BT_ASSERT(session
->traces
->len
== 0);
1125 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1128 return stream_iter_status
;
1132 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1136 for (i
= 0; i
< count
; i
++) {
1137 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1142 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1143 bt_self_message_iterator
*self_msg_it
,
1144 bt_message_array_const msgs
, uint64_t capacity
,
1147 bt_component_class_message_iterator_next_method_status status
;
1148 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1149 bt_self_message_iterator_get_data(self_msg_it
);
1150 struct lttng_live_component
*lttng_live
=
1151 lttng_live_msg_iter
->lttng_live_comp
;
1152 enum lttng_live_iterator_status stream_iter_status
;
1153 uint64_t session_idx
;
1157 BT_ASSERT(lttng_live_msg_iter
);
1160 * Clear all the invalid message reference that might be left over in
1163 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1166 * If no session are exposed on the relay found at the url provided by
1167 * the user, session count will be 0. In this case, we return status
1168 * end to return gracefully.
1170 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1171 if (lttng_live
->params
.sess_not_found_act
!=
1172 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1173 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1177 * The are no more active session for this session
1178 * name. Retry to create a viewer session for the
1179 * requested session name.
1181 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1182 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1188 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1189 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1193 * Here the muxing of message is done.
1195 * We need to iterate over all the streams of all the traces of all the
1196 * viewer sessions in order to get the message with the smallest
1197 * timestamp. In this case, a session is a viewer session and there is
1198 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1199 * kernel). Each viewer session can have multiple traces, for example,
1200 * 64bit UST viewer sessions could have multiple per-pid traces.
1202 * We iterate over the streams of each traces to update and see what is
1203 * their next message's timestamp. From those timestamps, we select the
1204 * message with the smallest timestamp as the best candidate message
1205 * for that trace and do the same thing across all the sessions.
1207 * We then compare the timestamp of best candidate message of all the
1208 * sessions to pick the message with the smallest timestamp and we
1211 while (*count
< capacity
) {
1212 struct lttng_live_stream_iterator
*next_stream_iter
= NULL
,
1213 *candidate_stream_iter
= NULL
;
1214 int64_t next_msg_ts_ns
= INT64_MAX
;
1216 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1219 * Use a while loop instead of a for loop so we can restart the
1220 * iteration if we remove an element. We can safely call
1221 * next_stream_iterator_for_session() multiple times on the
1222 * same session as we only fetch a new message if there is no
1223 * current next message for each live stream iterator.
1224 * If all live stream iterator of that session already have a
1225 * current next message, the function will simply exit return
1226 * the same candidate live stream iterator every time.
1228 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1229 struct lttng_live_session
*session
=
1230 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1233 /* Find the best candidate message to send downstream. */
1234 stream_iter_status
= next_stream_iterator_for_session(
1235 lttng_live_msg_iter
, session
,
1236 &candidate_stream_iter
);
1238 /* If we receive an END status, it means that either:
1239 * - Those traces never had active streams (UST with no
1240 * data produced yet),
1241 * - All live stream iterators have ENDed.*/
1242 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1243 if (session
->closed
&& session
->traces
->len
== 0) {
1245 * Remove the session from the list and restart the
1246 * iteration at the beginning of the array since the
1247 * removal shuffle the elements of the array.
1249 g_ptr_array_remove_index_fast(
1250 lttng_live_msg_iter
->sessions
,
1259 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1263 if (candidate_stream_iter
->current_msg_ts_ns
<= next_msg_ts_ns
) {
1264 next_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1265 next_stream_iter
= candidate_stream_iter
;
1271 if (!next_stream_iter
) {
1272 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1276 BT_ASSERT(next_stream_iter
->current_msg
);
1277 /* Ensure monotonicity. */
1278 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1279 next_stream_iter
->current_msg_ts_ns
);
1282 * Insert the next message to the message batch. This will set
1283 * stream iterator current messsage to NULL so that next time
1284 * we fetch the next message of that stream iterator
1286 BT_MESSAGE_MOVE_REF(msgs
[*count
], next_stream_iter
->current_msg
);
1289 /* Update the last timestamp in nanoseconds sent downstream. */
1290 lttng_live_msg_iter
->last_msg_ts_ns
= next_msg_ts_ns
;
1291 next_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1293 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1296 switch (stream_iter_status
) {
1297 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1298 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1301 * We received a again status but we have some messages
1302 * to send downstream. We send them and return OK for
1303 * now. On the next call we return again if there are
1304 * still no new message to send.
1306 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1308 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1311 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1312 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1314 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1315 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1317 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1318 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1319 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1320 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1321 /* Put all existing messages on error. */
1322 put_messages(msgs
, *count
);
1333 bt_component_class_message_iterator_init_method_status
lttng_live_msg_iter_init(
1334 bt_self_message_iterator
*self_msg_it
,
1335 bt_self_component_source
*self_comp_src
,
1336 bt_self_component_port_output
*self_port
)
1338 bt_component_class_message_iterator_init_method_status ret
=
1339 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1340 bt_self_component
*self_comp
=
1341 bt_self_component_source_as_self_component(self_comp_src
);
1342 struct lttng_live_component
*lttng_live
;
1343 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1344 bt_logging_level log_level
;
1346 BT_ASSERT(self_msg_it
);
1348 lttng_live
= bt_self_component_get_data(self_comp
);
1349 log_level
= lttng_live
->log_level
;
1350 self_comp
= lttng_live
->self_comp
;
1352 /* There can be only one downstream iterator at the same time. */
1353 BT_ASSERT(!lttng_live
->has_msg_iter
);
1354 lttng_live
->has_msg_iter
= true;
1356 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1357 if (!lttng_live_msg_iter
) {
1358 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1362 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1363 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1364 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1365 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1367 lttng_live_msg_iter
->active_stream_iter
= 0;
1368 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1369 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1370 (GDestroyNotify
) lttng_live_destroy_session
);
1371 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1373 lttng_live_msg_iter
->viewer_connection
=
1374 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1375 lttng_live_msg_iter
);
1376 if (!lttng_live_msg_iter
->viewer_connection
) {
1380 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1383 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1384 switch (lttng_live
->params
.sess_not_found_act
) {
1385 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1386 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1387 "session. Keep trying to connect because of "
1388 "%s=\"%s\" component parameter: url=\"%s\"",
1389 SESS_NOT_FOUND_ACTION_PARAM
,
1390 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1391 lttng_live
->params
.url
->str
);
1393 case SESSION_NOT_FOUND_ACTION_FAIL
:
1394 BT_COMP_LOGE("Unable to connect to the requested live viewer "
1395 "session. Fail the message iterator"
1396 "initialization because of %s=\"%s\" "
1397 "component parameter: url =\"%s\"",
1398 SESS_NOT_FOUND_ACTION_PARAM
,
1399 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1400 lttng_live
->params
.url
->str
);
1402 case SESSION_NOT_FOUND_ACTION_END
:
1403 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1404 "session. End gracefully at the first _next() "
1405 "call because of %s=\"%s\" component parameter: "
1406 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1407 SESS_NOT_FOUND_ACTION_END_STR
,
1408 lttng_live
->params
.url
->str
);
1413 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1417 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1418 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1424 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1425 const bt_value
*params
, const bt_value
**result
,
1426 bt_logging_level log_level
)
1428 bt_component_class_query_method_status status
=
1429 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1430 const bt_value
*url_value
= NULL
;
1432 struct live_viewer_connection
*viewer_connection
= NULL
;
1433 bt_self_component
*self_comp
= NULL
;
1435 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1437 BT_COMP_LOGW("Mandatory `%s` parameter missing", URL_PARAM
);
1438 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS
;
1442 if (!bt_value_is_string(url_value
)) {
1443 BT_COMP_LOGW("`%s` parameter is required to be a string value",
1445 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS
;
1449 url
= bt_value_string_get(url_value
);
1451 viewer_connection
= live_viewer_connection_create(url
, true, NULL
);
1452 if (!viewer_connection
) {
1456 status
= live_viewer_connection_list_sessions(viewer_connection
,
1458 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1465 BT_VALUE_PUT_REF_AND_RESET(*result
);
1468 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1472 if (viewer_connection
) {
1473 live_viewer_connection_destroy(viewer_connection
);
1479 bt_component_class_query_method_status
lttng_live_query(
1480 bt_self_component_class_source
*comp_class
,
1481 const bt_query_executor
*query_exec
,
1482 const char *object
, const bt_value
*params
,
1483 bt_logging_level log_level
, const bt_value
**result
)
1485 bt_component_class_query_method_status status
=
1486 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1487 bt_self_component
*self_comp
= NULL
;
1489 if (strcmp(object
, "sessions") == 0) {
1490 status
= lttng_live_query_list_sessions(params
, result
,
1493 BT_COMP_LOGW("Unknown query object `%s`", object
);
1494 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_OBJECT
;
1503 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1508 if (lttng_live
->params
.url
) {
1509 g_string_free(lttng_live
->params
.url
, TRUE
);
1515 void lttng_live_component_finalize(bt_self_component_source
*component
)
1517 void *data
= bt_self_component_get_data(
1518 bt_self_component_source_as_self_component(component
));
1523 lttng_live_component_destroy_data(data
);
1527 enum session_not_found_action
parse_session_not_found_action_param(
1528 const bt_value
*no_session_param
)
1530 enum session_not_found_action action
;
1531 const char *no_session_act_str
;
1532 no_session_act_str
= bt_value_string_get(no_session_param
);
1533 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1534 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1535 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1536 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1537 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0) {
1538 action
= SESSION_NOT_FOUND_ACTION_END
;
1546 struct lttng_live_component
*lttng_live_component_create(const bt_value
*params
,
1547 bt_logging_level log_level
, bt_self_component
*self_comp
)
1549 struct lttng_live_component
*lttng_live
;
1550 const bt_value
*value
= NULL
;
1553 lttng_live
= g_new0(struct lttng_live_component
, 1);
1557 lttng_live
->log_level
= log_level
;
1558 lttng_live
->self_comp
= self_comp
;
1559 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1560 lttng_live
->has_msg_iter
= false;
1562 value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1563 if (!value
|| !bt_value_is_string(value
)) {
1564 BT_COMP_LOGW("Mandatory `%s` parameter missing or not a string",
1568 url
= bt_value_string_get(value
);
1569 lttng_live
->params
.url
= g_string_new(url
);
1570 if (!lttng_live
->params
.url
) {
1574 value
= bt_value_map_borrow_entry_value_const(params
,
1575 SESS_NOT_FOUND_ACTION_PARAM
);
1577 if (value
&& bt_value_is_string(value
)) {
1578 lttng_live
->params
.sess_not_found_act
=
1579 parse_session_not_found_action_param(value
);
1580 if (lttng_live
->params
.sess_not_found_act
== -1) {
1581 BT_COMP_LOGE("Unexpected value for `%s` parameter: "
1582 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1583 bt_value_string_get(value
));
1587 BT_COMP_LOGW("Optional `%s` parameter is missing or "
1588 "not a string value. Defaulting to %s=\"%s\".",
1589 SESS_NOT_FOUND_ACTION_PARAM
,
1590 SESS_NOT_FOUND_ACTION_PARAM
,
1591 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1592 lttng_live
->params
.sess_not_found_act
=
1593 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1599 lttng_live_component_destroy_data(lttng_live
);
1606 bt_component_class_init_method_status
lttng_live_component_init(
1607 bt_self_component_source
*self_comp_src
,
1608 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1610 struct lttng_live_component
*lttng_live
;
1611 bt_component_class_init_method_status ret
=
1612 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
1613 bt_self_component
*self_comp
=
1614 bt_self_component_source_as_self_component(self_comp_src
);
1615 bt_logging_level log_level
= bt_component_get_logging_level(
1616 bt_self_component_as_component(self_comp
));
1617 bt_self_component_add_port_status add_port_status
;
1619 lttng_live
= lttng_live_component_create(params
, log_level
, self_comp
);
1621 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1625 if (lttng_live_graph_is_canceled(lttng_live
)) {
1626 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1630 add_port_status
= bt_self_component_source_add_output_port(
1631 self_comp_src
, "out", NULL
, NULL
);
1632 switch (add_port_status
) {
1633 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
1634 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1636 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
1637 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1643 bt_self_component_set_data(self_comp
, lttng_live
);
1647 lttng_live_component_destroy_data(lttng_live
);