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_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC"
38 #include <babeltrace2/assert-internal.h>
39 #include <babeltrace2/babeltrace.h>
40 #include <babeltrace2/compiler-internal.h>
41 #include <babeltrace2/types.h>
42 #include <plugins-common.h>
44 #include "data-stream.h"
46 #include "lttng-live.h"
48 #define MAX_QUERY_SIZE (256*1024)
49 #define URL_PARAM "url"
50 #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
51 #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
52 #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
53 #define SESS_NOT_FOUND_ACTION_END_STR "end"
55 #define print_dbg(fmt, ...) BT_LOGD(fmt, ## __VA_ARGS__)
58 const char *print_live_iterator_status(enum lttng_live_iterator_status status
)
61 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
62 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
63 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
64 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
65 case LTTNG_LIVE_ITERATOR_STATUS_END
:
66 return "LTTNG_LIVE_ITERATOR_STATUS_END";
67 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
68 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
69 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
70 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
71 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
72 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
73 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
74 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
75 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
76 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
83 const char *print_state(struct lttng_live_stream_iterator
*s
)
86 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
87 return "ACTIVE_NO_DATA";
88 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
89 return "QUIESCENT_NO_DATA";
90 case LTTNG_LIVE_STREAM_QUIESCENT
:
92 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
94 case LTTNG_LIVE_STREAM_EOF
:
101 #define print_stream_state(live_stream_iter) \
103 BT_LOGD("stream state %s last_inact_ts %" PRId64 \
104 ", curr_inact_ts %" PRId64, \
105 print_state(live_stream_iter), \
106 live_stream_iter->last_inactivity_ts, \
107 live_stream_iter->current_inactivity_ts); \
111 bool lttng_live_graph_is_canceled(struct lttng_live_component
*lttng_live
)
113 const bt_component
*component
;
121 component
= bt_component_source_as_component_const(
122 bt_self_component_source_as_component_source(
123 lttng_live
->self_comp
));
125 ret
= bt_component_graph_is_canceled(component
);
132 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
136 struct lttng_live_trace
*ret_trace
= NULL
;
138 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
139 struct lttng_live_trace
*trace
=
140 g_ptr_array_index(session
->traces
, trace_idx
);
141 if (trace
->id
== trace_id
) {
152 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
154 BT_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
;
172 trace
= g_new0(struct lttng_live_trace
, 1);
176 trace
->session
= session
;
177 trace
->id
= trace_id
;
178 trace
->trace_class
= NULL
;
180 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
181 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
182 BT_ASSERT(trace
->stream_iterators
);
183 trace
->new_metadata_needed
= true;
184 g_ptr_array_add(session
->traces
, trace
);
186 BT_LOGI("Create trace");
196 struct lttng_live_trace
*lttng_live_borrow_trace(
197 struct lttng_live_session
*session
, uint64_t trace_id
)
199 struct lttng_live_trace
*trace
;
201 trace
= lttng_live_find_trace(session
, trace_id
);
206 /* The session is the owner of the newly created trace. */
207 trace
= lttng_live_create_trace(session
, trace_id
);
214 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
215 uint64_t session_id
, const char *hostname
,
216 const char *session_name
)
219 struct lttng_live_session
*session
;
221 session
= g_new0(struct lttng_live_session
, 1);
226 session
->id
= session_id
;
227 session
->traces
= g_ptr_array_new_with_free_func(
228 (GDestroyNotify
) lttng_live_destroy_trace
);
229 BT_ASSERT(session
->traces
);
230 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
231 session
->new_streams_needed
= true;
232 session
->hostname
= g_string_new(hostname
);
233 BT_ASSERT(session
->hostname
);
235 session
->session_name
= g_string_new(session_name
);
236 BT_ASSERT(session
->session_name
);
238 BT_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
239 session
->id
, hostname
, session_name
);
240 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
243 BT_LOGE("Error adding session");
251 void lttng_live_destroy_session(struct lttng_live_session
*session
)
253 struct lttng_live_component
*live_comp
;
259 BT_LOGD("Destroy lttng live session");
260 if (session
->id
!= -1ULL) {
261 if (lttng_live_detach_session(session
)) {
262 live_comp
= session
->lttng_live_msg_iter
->lttng_live_comp
;
263 if (session
->lttng_live_msg_iter
&&
264 !lttng_live_graph_is_canceled(live_comp
)) {
265 /* Old relayd cannot detach sessions. */
266 BT_LOGD("Unable to detach lttng live session %" PRIu64
,
273 if (session
->traces
) {
274 g_ptr_array_free(session
->traces
, TRUE
);
277 if (session
->hostname
) {
278 g_string_free(session
->hostname
, TRUE
);
280 if (session
->session_name
) {
281 g_string_free(session
->session_name
, TRUE
);
290 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
292 if (!lttng_live_msg_iter
) {
296 if (lttng_live_msg_iter
->sessions
) {
297 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
300 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
301 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
302 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
304 /* All stream iterators must be destroyed at this point. */
305 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
306 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
308 g_free(lttng_live_msg_iter
);
315 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
317 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
319 BT_ASSERT(self_msg_iter
);
321 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
322 BT_ASSERT(lttng_live_msg_iter
);
323 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
327 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
328 struct lttng_live_stream_iterator
*lttng_live_stream
)
330 switch (lttng_live_stream
->state
) {
331 case LTTNG_LIVE_STREAM_QUIESCENT
:
332 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
334 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
336 BT_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
338 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
340 BT_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
342 case LTTNG_LIVE_STREAM_EOF
:
345 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
349 * For active no data stream, fetch next data. It can be either:
350 * - quiescent: need to put it in the prio heap at quiescent end
352 * - have data: need to wire up first event into the prio heap,
353 * - have no data on this stream at this point: need to retry (AGAIN) or
357 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
358 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
359 struct lttng_live_stream_iterator
*lttng_live_stream
)
361 enum lttng_live_iterator_status ret
=
362 LTTNG_LIVE_ITERATOR_STATUS_OK
;
363 struct packet_index index
;
364 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
366 if (lttng_live_stream
->trace
->new_metadata_needed
) {
367 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
370 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
371 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
374 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
375 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
378 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
380 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
383 BT_ASSERT(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
384 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
385 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
386 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
388 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
389 last_inact_ts
== curr_inact_ts
) {
390 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
391 print_stream_state(lttng_live_stream
);
393 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
397 lttng_live_stream
->base_offset
= index
.offset
;
398 lttng_live_stream
->offset
= index
.offset
;
399 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
401 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
402 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
408 * Creation of the message requires the ctf trace class to be created
409 * beforehand, but the live protocol gives us all streams (including metadata)
410 * at once. So we split it in three steps: getting streams, getting metadata
411 * (which creates the ctf trace class), and then creating the per-stream
415 enum lttng_live_iterator_status
lttng_live_get_session(
416 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
417 struct lttng_live_session
*session
)
419 enum lttng_live_iterator_status status
;
423 if (!session
->attached
) {
424 ret
= lttng_live_attach_session(session
);
426 if (lttng_live_msg_iter
&& lttng_live_graph_is_canceled(
427 lttng_live_msg_iter
->lttng_live_comp
)) {
428 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
430 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
436 status
= lttng_live_get_new_streams(session
);
437 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
438 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
441 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
442 struct lttng_live_trace
*trace
=
443 g_ptr_array_index(session
->traces
, trace_idx
);
445 status
= lttng_live_metadata_update(trace
);
446 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
447 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
451 status
= lttng_live_lazy_msg_init(session
);
458 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
460 uint64_t session_idx
;
462 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
464 struct lttng_live_session
*session
=
465 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
466 session
->new_streams_needed
= true;
471 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
473 uint64_t session_idx
, trace_idx
;
475 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
477 struct lttng_live_session
*session
=
478 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
479 session
->new_streams_needed
= true;
480 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
482 struct lttng_live_trace
*trace
=
483 g_ptr_array_index(session
->traces
, trace_idx
);
484 trace
->new_metadata_needed
= true;
490 enum lttng_live_iterator_status
491 lttng_live_iterator_handle_new_streams_and_metadata(
492 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
494 enum lttng_live_iterator_status ret
=
495 LTTNG_LIVE_ITERATOR_STATUS_OK
;
496 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
497 struct lttng_live_session
*session
;
498 enum session_not_found_action sess_not_found_act
=
499 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
502 * In a remotely distant future, we could add a "new
503 * session" flag to the protocol, which would tell us that we
504 * need to query for new sessions even though we have sessions
507 if (lttng_live_msg_iter
->sessions
->len
== 0) {
508 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
509 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
513 * Retry to create a viewer session for the requested
516 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
517 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
523 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
525 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
527 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
529 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
531 case LTTNG_LIVE_ITERATOR_STATUS_END
:
532 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
537 if (!session
->closed
) {
538 nr_sessions_opened
++;
542 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
543 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
544 nr_sessions_opened
== 0) {
545 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
551 enum lttng_live_iterator_status
emit_inactivity_message(
552 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
553 struct lttng_live_stream_iterator
*stream_iter
,
554 bt_message
**message
, uint64_t timestamp
)
556 enum lttng_live_iterator_status ret
=
557 LTTNG_LIVE_ITERATOR_STATUS_OK
;
558 bt_message
*msg
= NULL
;
560 BT_ASSERT(stream_iter
->trace
->clock_class
);
562 msg
= bt_message_message_iterator_inactivity_create(
563 lttng_live_msg_iter
->self_msg_iter
,
564 stream_iter
->trace
->clock_class
,
575 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
576 bt_message_put_ref(msg
);
581 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
582 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
583 struct lttng_live_stream_iterator
*lttng_live_stream
,
584 bt_message
**message
)
586 enum lttng_live_iterator_status ret
=
587 LTTNG_LIVE_ITERATOR_STATUS_OK
;
589 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
590 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
593 if (lttng_live_stream
->current_inactivity_ts
==
594 lttng_live_stream
->last_inactivity_ts
) {
595 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
596 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
600 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
601 message
, lttng_live_stream
->current_inactivity_ts
);
603 lttng_live_stream
->last_inactivity_ts
=
604 lttng_live_stream
->current_inactivity_ts
;
610 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
611 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
612 const bt_message
*msg
, int64_t last_msg_ts_ns
,
615 const bt_clock_class
*clock_class
= NULL
;
616 const bt_clock_snapshot
*clock_snapshot
= NULL
;
618 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
623 BT_LOGV("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
624 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
627 switch (bt_message_get_type(msg
)) {
628 case BT_MESSAGE_TYPE_EVENT
:
630 bt_message_event_borrow_stream_class_default_clock_class_const(
632 BT_ASSERT(clock_class
);
634 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
637 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
639 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
641 BT_ASSERT(clock_class
);
643 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
646 case BT_MESSAGE_TYPE_PACKET_END
:
648 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
650 BT_ASSERT(clock_class
);
652 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
655 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
657 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
659 BT_ASSERT(clock_class
);
661 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
664 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
666 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
668 BT_ASSERT(clock_class
);
670 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
673 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
675 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
677 BT_ASSERT(clock_class
);
679 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
680 msg
, &clock_snapshot
);
681 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
682 goto no_clock_snapshot
;
686 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
688 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
690 BT_ASSERT(clock_class
);
692 sa_cs_state
= bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
693 msg
, &clock_snapshot
);
694 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
695 goto no_clock_snapshot
;
699 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
701 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
705 /* All the other messages have a higher priority */
706 BT_LOGV_STR("Message has no timestamp: using the last message timestamp.");
707 *ts_ns
= last_msg_ts_ns
;
711 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
712 BT_ASSERT(clock_class
);
714 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
716 BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
717 "clock-snapshot-addr=%p", clock_snapshot
);
724 BT_LOGV_STR("Message's default clock snapshot is missing: "
725 "using the last message timestamp.");
726 *ts_ns
= last_msg_ts_ns
;
734 BT_LOGV("Found message's timestamp: "
735 "iter-data-addr=%p, msg-addr=%p, "
736 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
737 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
744 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
745 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
746 struct lttng_live_stream_iterator
*lttng_live_stream
,
747 bt_message
**message
)
749 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
750 enum bt_msg_iter_status status
;
751 uint64_t session_idx
, trace_idx
;
753 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
755 struct lttng_live_session
*session
=
756 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
758 if (session
->new_streams_needed
) {
759 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
762 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
764 struct lttng_live_trace
*trace
=
765 g_ptr_array_index(session
->traces
, trace_idx
);
766 if (trace
->new_metadata_needed
) {
767 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
773 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
774 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
778 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
779 lttng_live_msg_iter
->self_msg_iter
, message
);
781 case BT_MSG_ITER_STATUS_EOF
:
782 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
784 case BT_MSG_ITER_STATUS_OK
:
785 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
787 case BT_MSG_ITER_STATUS_AGAIN
:
789 * Continue immediately (end of packet). The next
790 * get_index may return AGAIN to delay the following
793 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
795 case BT_MSG_ITER_STATUS_INVAL
:
796 /* No argument provided by the user, so don't return INVAL. */
797 case BT_MSG_ITER_STATUS_ERROR
:
799 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
800 BT_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
801 lttng_live_stream
->msg_iter
);
811 * handle_no_data_streams()
813 * - for each ACTIVE_NO_DATA stream:
814 * - query relayd for stream data, or quiescence info.
815 * - if need metadata, get metadata, goto retry.
816 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
817 * - if quiescent, move to QUIESCENT streams
818 * - if fetched data, move to ACTIVE_DATA streams
819 * (at this point each stream either has data, or is quiescent)
823 * handle_new_streams_and_metadata()
824 * - query relayd for known streams, add them as ACTIVE_NO_DATA
825 * - query relayd for metadata
827 * call handle_active_no_data_streams()
829 * handle_quiescent_streams()
830 * - if at least one stream is ACTIVE_DATA:
831 * - peek stream event with lowest timestamp -> next_ts
832 * - for each quiescent stream
833 * - if next_ts >= quiescent end
834 * - set state to ACTIVE_NO_DATA
836 * - for each quiescent stream
837 * - set state to ACTIVE_NO_DATA
839 * call handle_active_no_data_streams()
841 * handle_active_data_streams()
842 * - if at least one stream is ACTIVE_DATA:
843 * - get stream event with lowest timestamp from heap
844 * - make that stream event the current message.
845 * - move this stream heap position to its next event
846 * - if we need to fetch data from relayd, move
847 * stream to ACTIVE_NO_DATA.
851 * end criterion: ctrl-c on client. If relayd exits or the session
852 * closes on the relay daemon side, we keep on waiting for streams.
853 * Eventually handle --end timestamp (also an end criterion).
855 * When disconnected from relayd: try to re-connect endlessly.
858 enum lttng_live_iterator_status
lttng_live_iterator_next_on_stream(
859 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
860 struct lttng_live_stream_iterator
*stream_iter
,
861 bt_message
**curr_msg
)
863 enum lttng_live_iterator_status live_status
;
866 print_stream_state(stream_iter
);
867 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
868 lttng_live_msg_iter
);
869 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
872 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
873 lttng_live_msg_iter
, stream_iter
);
874 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
877 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
878 lttng_live_msg_iter
, stream_iter
, curr_msg
);
879 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
880 BT_ASSERT(*curr_msg
== NULL
);
886 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
887 lttng_live_msg_iter
, stream_iter
, curr_msg
);
888 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
889 BT_ASSERT(*curr_msg
== NULL
);
893 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
901 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
902 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
903 struct lttng_live_trace
*live_trace
,
904 struct lttng_live_stream_iterator
**candidate_stream_iter
)
906 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
907 enum lttng_live_iterator_status stream_iter_status
;;
908 int64_t curr_candidate_msg_ts
= INT64_MAX
;
909 uint64_t stream_iter_idx
;
911 BT_ASSERT(live_trace
);
912 BT_ASSERT(live_trace
->stream_iterators
);
914 * Update the current message of every stream iterators of this trace.
915 * The current msg of every stream must have a timestamp equal or
916 * larger than the last message returned by this iterator. We must
917 * ensure monotonicity.
920 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
921 bool stream_iter_is_ended
= false;
922 struct lttng_live_stream_iterator
*stream_iter
=
923 g_ptr_array_index(live_trace
->stream_iterators
,
927 * Find if there is are now current message for this stream
930 while (!stream_iter
->current_msg
) {
931 bt_message
*msg
= NULL
;
932 int64_t curr_msg_ts_ns
= INT64_MAX
;
933 stream_iter_status
= lttng_live_iterator_next_on_stream(
934 lttng_live_msg_iter
, stream_iter
, &msg
);
936 BT_LOGD("live stream iterator returned status :%s",
937 print_live_iterator_status(stream_iter_status
));
938 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
939 stream_iter_is_ended
= true;
943 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
950 * Get the timestamp in nanoseconds from origin of this
953 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
954 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
958 * Check if the message of the current live stream
959 * iterator occured at the exact same time or after the
960 * last message returned by this component's message
961 * iterator. If not, we return an error.
963 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
964 stream_iter
->current_msg
= msg
;
965 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
968 * We received a message in the past. To ensure
969 * monotonicity, we can't send it forward.
971 BT_LOGE("Message's timestamp is less than "
972 "lttng-live's message iterator's last "
973 "returned timestamp: "
974 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
975 "last-msg-ts=%" PRId64
,
976 lttng_live_msg_iter
, curr_msg_ts_ns
,
977 lttng_live_msg_iter
->last_msg_ts_ns
);
978 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
983 if (!stream_iter_is_ended
&&
984 stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
986 * Update the current best candidate message for the
987 * stream iterator of thise live trace to be forwarded
990 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
991 curr_candidate_stream_iter
= stream_iter
;
994 if (stream_iter_is_ended
) {
996 * The live stream iterator is ENDed. We remove that
997 * iterator from the list and we restart the iteration
998 * at the beginning of the live stream iterator array
999 * to because the removal will shuffle the array.
1001 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1003 stream_iter_idx
= 0;
1009 if (curr_candidate_stream_iter
) {
1010 *candidate_stream_iter
= curr_candidate_stream_iter
;
1011 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1014 * The only case where we don't have a candidate for this trace
1015 * is if we reached the end of all the iterators.
1017 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1018 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1022 return stream_iter_status
;
1026 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1027 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1028 struct lttng_live_session
*session
,
1029 struct lttng_live_stream_iterator
**candidate_session_stream_iter
)
1031 enum lttng_live_iterator_status stream_iter_status
;
1032 uint64_t trace_idx
= 0;
1033 int64_t curr_candidate_msg_ts
= INT64_MAX
;
1034 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
1037 * Make sure we are attached to the session and look for new streams
1040 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1041 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1042 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1043 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1047 BT_ASSERT(session
->traces
);
1050 * Use while loops here rather then for loops so we can restart the
1051 * iteration if an element is removed from the array during the
1054 while (trace_idx
< session
->traces
->len
) {
1055 bool trace_is_ended
= false;
1056 struct lttng_live_stream_iterator
*stream_iter
;
1057 struct lttng_live_trace
*trace
=
1058 g_ptr_array_index(session
->traces
, trace_idx
);
1060 stream_iter_status
= next_stream_iterator_for_trace(
1061 lttng_live_msg_iter
, trace
, &stream_iter
);
1062 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1064 * All the live stream iterators for this trace are
1065 * ENDed. Remove the trace from this session.
1067 trace_is_ended
= true;
1068 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1072 if (!trace_is_ended
) {
1073 BT_ASSERT(stream_iter
);
1075 if (stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1076 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1077 curr_candidate_stream_iter
= stream_iter
;
1081 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1085 if (curr_candidate_stream_iter
) {
1086 *candidate_session_stream_iter
= curr_candidate_stream_iter
;
1087 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1090 * The only cases where we don't have a candidate for this
1092 * 1. if we reached the end of all the iterators of all the
1093 * traces of this session,
1094 * 2. if we never had live stream iterator in the first place.
1096 * In either cases, we return END.
1098 BT_ASSERT(session
->traces
->len
== 0);
1099 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1102 return stream_iter_status
;
1106 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1110 for (i
= 0; i
< count
; i
++) {
1111 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1116 bt_self_message_iterator_status
lttng_live_msg_iter_next(
1117 bt_self_message_iterator
*self_msg_it
,
1118 bt_message_array_const msgs
, uint64_t capacity
,
1121 bt_self_message_iterator_status status
;
1122 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1123 bt_self_message_iterator_get_data(self_msg_it
);
1124 struct lttng_live_component
*lttng_live
=
1125 lttng_live_msg_iter
->lttng_live_comp
;
1126 enum lttng_live_iterator_status stream_iter_status
;
1127 uint64_t session_idx
;
1131 BT_ASSERT(lttng_live_msg_iter
);
1134 * Clear all the invalid message reference that might be left over in
1137 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1140 * If no session are exposed on the relay found at the url provided by
1141 * the user, session count will be 0. In this case, we return status
1142 * end to return gracefully.
1144 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1145 if (lttng_live
->params
.sess_not_found_act
!=
1146 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1147 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1151 * The are no more active session for this session
1152 * name. Retry to create a viewer session for the
1153 * requested session name.
1155 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1156 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1162 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1163 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1167 * Here the muxing of message is done.
1169 * We need to iterate over all the streams of all the traces of all the
1170 * viewer sessions in order to get the message with the smallest
1171 * timestamp. In this case, a session is a viewer session and there is
1172 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1173 * kernel). Each viewer session can have multiple traces, for example,
1174 * 64bit UST viewer sessions could have multiple per-pid traces.
1176 * We iterate over the streams of each traces to update and see what is
1177 * their next message's timestamp. From those timestamps, we select the
1178 * message with the smallest timestamp as the best candidate message
1179 * for that trace and do the same thing across all the sessions.
1181 * We then compare the timestamp of best candidate message of all the
1182 * sessions to pick the message with the smallest timestamp and we
1185 while (*count
< capacity
) {
1186 struct lttng_live_stream_iterator
*next_stream_iter
= NULL
,
1187 *candidate_stream_iter
= NULL
;
1188 int64_t next_msg_ts_ns
= INT64_MAX
;
1190 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1193 * Use a while loop instead of a for loop so we can restart the
1194 * iteration if we remove an element. We can safely call
1195 * next_stream_iterator_for_session() multiple times on the
1196 * same session as we only fetch a new message if there is no
1197 * current next message for each live stream iterator.
1198 * If all live stream iterator of that session already have a
1199 * current next message, the function will simply exit return
1200 * the same candidate live stream iterator every time.
1202 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1203 struct lttng_live_session
*session
=
1204 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1207 /* Find the best candidate message to send downstream. */
1208 stream_iter_status
= next_stream_iterator_for_session(
1209 lttng_live_msg_iter
, session
,
1210 &candidate_stream_iter
);
1212 /* If we receive an END status, it means that either:
1213 * - Those traces never had active streams (UST with no
1214 * data produced yet),
1215 * - All live stream iterators have ENDed.*/
1216 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1217 if (session
->closed
&& session
->traces
->len
== 0) {
1219 * Remove the session from the list and restart the
1220 * iteration at the beginning of the array since the
1221 * removal shuffle the elements of the array.
1223 g_ptr_array_remove_index_fast(
1224 lttng_live_msg_iter
->sessions
,
1233 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1237 if (candidate_stream_iter
->current_msg_ts_ns
<= next_msg_ts_ns
) {
1238 next_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1239 next_stream_iter
= candidate_stream_iter
;
1245 if (!next_stream_iter
) {
1246 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1250 BT_ASSERT(next_stream_iter
->current_msg
);
1251 /* Ensure monotonicity. */
1252 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1253 next_stream_iter
->current_msg_ts_ns
);
1256 * Insert the next message to the message batch. This will set
1257 * stream iterator current messsage to NULL so that next time
1258 * we fetch the next message of that stream iterator
1260 BT_MESSAGE_MOVE_REF(msgs
[*count
], next_stream_iter
->current_msg
);
1263 /* Update the last timestamp in nanoseconds sent downstream. */
1264 lttng_live_msg_iter
->last_msg_ts_ns
= next_msg_ts_ns
;
1265 next_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1267 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1270 switch (stream_iter_status
) {
1271 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1272 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1275 * We received a again status but we have some messages
1276 * to send downstream. We send them and return OK for
1277 * now. On the next call we return again if there are
1278 * still no new message to send.
1280 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1282 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN
;
1285 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1286 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1288 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1289 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1291 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1292 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1293 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1294 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1295 /* Put all existing messages on error. */
1296 put_messages(msgs
, *count
);
1307 bt_self_message_iterator_status
lttng_live_msg_iter_init(
1308 bt_self_message_iterator
*self_msg_it
,
1309 bt_self_component_source
*self_comp_src
,
1310 bt_self_component_port_output
*self_port
)
1312 bt_self_message_iterator_status ret
=
1313 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1314 bt_self_component
*self_comp
=
1315 bt_self_component_source_as_self_component(self_comp_src
);
1316 struct lttng_live_component
*lttng_live
;
1317 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1319 BT_ASSERT(self_msg_it
);
1321 lttng_live
= bt_self_component_get_data(self_comp
);
1323 /* There can be only one downstream iterator at the same time. */
1324 BT_ASSERT(!lttng_live
->has_msg_iter
);
1325 lttng_live
->has_msg_iter
= true;
1327 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1328 if (!lttng_live_msg_iter
) {
1329 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1333 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1334 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1336 lttng_live_msg_iter
->active_stream_iter
= 0;
1337 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1338 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1339 (GDestroyNotify
) lttng_live_destroy_session
);
1340 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1342 lttng_live_msg_iter
->viewer_connection
=
1343 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1344 lttng_live_msg_iter
);
1345 if (!lttng_live_msg_iter
->viewer_connection
) {
1349 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1352 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1353 switch (lttng_live
->params
.sess_not_found_act
) {
1354 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1355 BT_LOGI("Unable to connect to the requested live viewer "
1356 "session. Keep trying to connect because of "
1357 "%s=\"%s\" component parameter: url=\"%s\"",
1358 SESS_NOT_FOUND_ACTION_PARAM
,
1359 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1360 lttng_live
->params
.url
->str
);
1362 case SESSION_NOT_FOUND_ACTION_FAIL
:
1363 BT_LOGE("Unable to connect to the requested live viewer "
1364 "session. Fail the message iterator"
1365 "initialization because of %s=\"%s\" "
1366 "component parameter: url =\"%s\"",
1367 SESS_NOT_FOUND_ACTION_PARAM
,
1368 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1369 lttng_live
->params
.url
->str
);
1371 case SESSION_NOT_FOUND_ACTION_END
:
1372 BT_LOGI("Unable to connect to the requested live viewer "
1373 "session. End gracefully at the first _next() "
1374 "call because of %s=\"%s\" component parameter: "
1375 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1376 SESS_NOT_FOUND_ACTION_END_STR
,
1377 lttng_live
->params
.url
->str
);
1382 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1386 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1387 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1393 bt_query_status
lttng_live_query_list_sessions(const bt_value
*params
,
1394 const bt_value
**result
)
1396 bt_query_status status
= BT_QUERY_STATUS_OK
;
1397 const bt_value
*url_value
= NULL
;
1399 struct live_viewer_connection
*viewer_connection
= NULL
;
1401 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1403 BT_LOGW("Mandatory `%s` parameter missing", URL_PARAM
);
1404 status
= BT_QUERY_STATUS_INVALID_PARAMS
;
1408 if (!bt_value_is_string(url_value
)) {
1409 BT_LOGW("`%s` parameter is required to be a string value",
1411 status
= BT_QUERY_STATUS_INVALID_PARAMS
;
1415 url
= bt_value_string_get(url_value
);
1417 viewer_connection
= live_viewer_connection_create(url
, true, NULL
);
1418 if (!viewer_connection
) {
1422 status
= live_viewer_connection_list_sessions(viewer_connection
,
1424 if (status
!= BT_QUERY_STATUS_OK
) {
1431 BT_VALUE_PUT_REF_AND_RESET(*result
);
1434 status
= BT_QUERY_STATUS_ERROR
;
1438 if (viewer_connection
) {
1439 live_viewer_connection_destroy(viewer_connection
);
1445 bt_query_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1446 const bt_query_executor
*query_exec
,
1447 const char *object
, const bt_value
*params
,
1448 const bt_value
**result
)
1450 bt_query_status status
= BT_QUERY_STATUS_OK
;
1452 if (strcmp(object
, "sessions") == 0) {
1453 status
= lttng_live_query_list_sessions(params
, result
);
1455 BT_LOGW("Unknown query object `%s`", object
);
1456 status
= BT_QUERY_STATUS_INVALID_OBJECT
;
1465 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1470 if (lttng_live
->params
.url
) {
1471 g_string_free(lttng_live
->params
.url
, TRUE
);
1477 void lttng_live_component_finalize(bt_self_component_source
*component
)
1479 void *data
= bt_self_component_get_data(
1480 bt_self_component_source_as_self_component(component
));
1485 lttng_live_component_destroy_data(data
);
1489 enum session_not_found_action
parse_session_not_found_action_param(
1490 const bt_value
*no_session_param
)
1492 enum session_not_found_action action
;
1493 const char *no_session_act_str
;
1494 no_session_act_str
= bt_value_string_get(no_session_param
);
1495 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1496 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1497 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1498 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1499 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0) {
1500 action
= SESSION_NOT_FOUND_ACTION_END
;
1508 struct lttng_live_component
*lttng_live_component_create(const bt_value
*params
)
1510 struct lttng_live_component
*lttng_live
;
1511 const bt_value
*value
= NULL
;
1514 lttng_live
= g_new0(struct lttng_live_component
, 1);
1518 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1519 lttng_live
->has_msg_iter
= false;
1521 value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1522 if (!value
|| !bt_value_is_string(value
)) {
1523 BT_LOGW("Mandatory `%s` parameter missing or not a string",
1527 url
= bt_value_string_get(value
);
1528 lttng_live
->params
.url
= g_string_new(url
);
1529 if (!lttng_live
->params
.url
) {
1533 value
= bt_value_map_borrow_entry_value_const(params
,
1534 SESS_NOT_FOUND_ACTION_PARAM
);
1536 if (value
&& bt_value_is_string(value
)) {
1537 lttng_live
->params
.sess_not_found_act
=
1538 parse_session_not_found_action_param(value
);
1539 if (lttng_live
->params
.sess_not_found_act
== -1) {
1540 BT_LOGE("Unexpected value for `%s` parameter: "
1541 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1542 bt_value_string_get(value
));
1546 BT_LOGW("Optional `%s` parameter is missing or "
1547 "not a string value. Defaulting to %s=\"%s\".",
1548 SESS_NOT_FOUND_ACTION_PARAM
,
1549 SESS_NOT_FOUND_ACTION_PARAM
,
1550 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1551 lttng_live
->params
.sess_not_found_act
=
1552 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1558 lttng_live_component_destroy_data(lttng_live
);
1565 bt_self_component_status
lttng_live_component_init(
1566 bt_self_component_source
*self_comp
,
1567 const bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1569 struct lttng_live_component
*lttng_live
;
1570 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
1572 lttng_live
= lttng_live_component_create(params
);
1574 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
1577 lttng_live
->self_comp
= self_comp
;
1579 if (lttng_live_graph_is_canceled(lttng_live
)) {
1580 ret
= BT_SELF_COMPONENT_STATUS_END
;
1584 ret
= bt_self_component_source_add_output_port(
1585 lttng_live
->self_comp
, "out",
1587 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
1591 bt_self_component_set_data(
1592 bt_self_component_source_as_self_component(self_comp
),
1597 lttng_live_component_destroy_data(lttng_live
);