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 <babeltrace/assert-internal.h>
39 #include <babeltrace/babeltrace.h>
40 #include <babeltrace/compiler-internal.h>
41 #include <babeltrace/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_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_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_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_clock_snapshot_state cs_state
= BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
619 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
624 BT_LOGV("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
625 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
628 switch (bt_message_get_type(msg
)) {
629 case BT_MESSAGE_TYPE_EVENT
:
631 bt_message_event_borrow_stream_class_default_clock_class_const(
633 BT_ASSERT(clock_class
);
635 cs_state
= bt_message_event_borrow_default_clock_snapshot_const(
636 msg
, &clock_snapshot
);
638 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
640 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
642 BT_ASSERT(clock_class
);
644 cs_state
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
645 msg
, &clock_snapshot
);
647 case BT_MESSAGE_TYPE_PACKET_END
:
649 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
651 BT_ASSERT(clock_class
);
653 cs_state
= bt_message_packet_end_borrow_default_clock_snapshot_const(
654 msg
, &clock_snapshot
);
656 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
658 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
660 BT_ASSERT(clock_class
);
662 cs_state
= bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
663 msg
, &clock_snapshot
);
665 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
667 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
669 BT_ASSERT(clock_class
);
671 cs_state
= bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
672 msg
, &clock_snapshot
);
674 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
676 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
678 BT_ASSERT(clock_class
);
680 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
681 msg
, &clock_snapshot
);
682 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
683 goto no_clock_snapshot
;
687 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
689 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
691 BT_ASSERT(clock_class
);
693 sa_cs_state
= bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
694 msg
, &clock_snapshot
);
695 if (sa_cs_state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
) {
696 goto no_clock_snapshot
;
700 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
702 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
703 msg
, &clock_snapshot
);
706 /* All the other messages have a higher priority */
707 BT_LOGV_STR("Message has no timestamp: using the last message timestamp.");
708 *ts_ns
= last_msg_ts_ns
;
712 if (cs_state
!= BT_CLOCK_SNAPSHOT_STATE_KNOWN
) {
713 BT_LOGE_STR("Unsupported unknown clock snapshot.");
718 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
719 BT_ASSERT(clock_class
);
721 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
723 BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
724 "clock-snapshot-addr=%p", clock_snapshot
);
731 BT_LOGV_STR("Message's default clock snapshot is missing: "
732 "using the last message timestamp.");
733 *ts_ns
= last_msg_ts_ns
;
741 BT_LOGV("Found message's timestamp: "
742 "iter-data-addr=%p, msg-addr=%p, "
743 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
744 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
751 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
752 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
753 struct lttng_live_stream_iterator
*lttng_live_stream
,
754 bt_message
**message
)
756 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
757 enum bt_msg_iter_status status
;
758 uint64_t session_idx
, trace_idx
;
760 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
762 struct lttng_live_session
*session
=
763 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
765 if (session
->new_streams_needed
) {
766 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
769 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
771 struct lttng_live_trace
*trace
=
772 g_ptr_array_index(session
->traces
, trace_idx
);
773 if (trace
->new_metadata_needed
) {
774 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
780 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
781 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
785 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
786 lttng_live_msg_iter
->self_msg_iter
, message
);
788 case BT_MSG_ITER_STATUS_EOF
:
789 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
791 case BT_MSG_ITER_STATUS_OK
:
792 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
794 case BT_MSG_ITER_STATUS_AGAIN
:
796 * Continue immediately (end of packet). The next
797 * get_index may return AGAIN to delay the following
800 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
802 case BT_MSG_ITER_STATUS_INVAL
:
803 /* No argument provided by the user, so don't return INVAL. */
804 case BT_MSG_ITER_STATUS_ERROR
:
806 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
807 BT_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
808 lttng_live_stream
->msg_iter
);
818 * handle_no_data_streams()
820 * - for each ACTIVE_NO_DATA stream:
821 * - query relayd for stream data, or quiescence info.
822 * - if need metadata, get metadata, goto retry.
823 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
824 * - if quiescent, move to QUIESCENT streams
825 * - if fetched data, move to ACTIVE_DATA streams
826 * (at this point each stream either has data, or is quiescent)
830 * handle_new_streams_and_metadata()
831 * - query relayd for known streams, add them as ACTIVE_NO_DATA
832 * - query relayd for metadata
834 * call handle_active_no_data_streams()
836 * handle_quiescent_streams()
837 * - if at least one stream is ACTIVE_DATA:
838 * - peek stream event with lowest timestamp -> next_ts
839 * - for each quiescent stream
840 * - if next_ts >= quiescent end
841 * - set state to ACTIVE_NO_DATA
843 * - for each quiescent stream
844 * - set state to ACTIVE_NO_DATA
846 * call handle_active_no_data_streams()
848 * handle_active_data_streams()
849 * - if at least one stream is ACTIVE_DATA:
850 * - get stream event with lowest timestamp from heap
851 * - make that stream event the current message.
852 * - move this stream heap position to its next event
853 * - if we need to fetch data from relayd, move
854 * stream to ACTIVE_NO_DATA.
858 * end criterion: ctrl-c on client. If relayd exits or the session
859 * closes on the relay daemon side, we keep on waiting for streams.
860 * Eventually handle --end timestamp (also an end criterion).
862 * When disconnected from relayd: try to re-connect endlessly.
865 enum lttng_live_iterator_status
lttng_live_iterator_next_on_stream(
866 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
867 struct lttng_live_stream_iterator
*stream_iter
,
868 bt_message
**curr_msg
)
870 enum lttng_live_iterator_status live_status
;
873 print_stream_state(stream_iter
);
874 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
875 lttng_live_msg_iter
);
876 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
879 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
880 lttng_live_msg_iter
, stream_iter
);
881 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
884 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
885 lttng_live_msg_iter
, stream_iter
, curr_msg
);
886 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
887 BT_ASSERT(*curr_msg
== NULL
);
893 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
894 lttng_live_msg_iter
, stream_iter
, curr_msg
);
895 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
896 BT_ASSERT(*curr_msg
== NULL
);
900 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
908 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
909 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
910 struct lttng_live_trace
*live_trace
,
911 struct lttng_live_stream_iterator
**candidate_stream_iter
)
913 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
914 enum lttng_live_iterator_status stream_iter_status
;;
915 int64_t curr_candidate_msg_ts
= INT64_MAX
;
916 uint64_t stream_iter_idx
;
918 BT_ASSERT(live_trace
);
919 BT_ASSERT(live_trace
->stream_iterators
);
921 * Update the current message of every stream iterators of this trace.
922 * The current msg of every stream must have a timestamp equal or
923 * larger than the last message returned by this iterator. We must
924 * ensure monotonicity.
927 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
928 bool stream_iter_is_ended
= false;
929 struct lttng_live_stream_iterator
*stream_iter
=
930 g_ptr_array_index(live_trace
->stream_iterators
,
934 * Find if there is are now current message for this stream
937 while (!stream_iter
->current_msg
) {
938 bt_message
*msg
= NULL
;
939 int64_t curr_msg_ts_ns
= INT64_MAX
;
940 stream_iter_status
= lttng_live_iterator_next_on_stream(
941 lttng_live_msg_iter
, stream_iter
, &msg
);
943 BT_LOGD("live stream iterator returned status :%s",
944 print_live_iterator_status(stream_iter_status
));
945 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
946 stream_iter_is_ended
= true;
950 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
957 * Get the timestamp in nanoseconds from origin of this
960 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
961 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
965 * Check if the message of the current live stream
966 * iterator occured at the exact same time or after the
967 * last message returned by this component's message
968 * iterator. If not, we return an error.
970 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
971 stream_iter
->current_msg
= msg
;
972 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
975 * We received a message in the past. To ensure
976 * monotonicity, we can't send it forward.
978 BT_LOGE("Message's timestamp is less than "
979 "lttng-live's message iterator's last "
980 "returned timestamp: "
981 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
982 "last-msg-ts=%" PRId64
,
983 lttng_live_msg_iter
, curr_msg_ts_ns
,
984 lttng_live_msg_iter
->last_msg_ts_ns
);
985 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
990 if (!stream_iter_is_ended
&&
991 stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
993 * Update the current best candidate message for the
994 * stream iterator of thise live trace to be forwarded
997 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
998 curr_candidate_stream_iter
= stream_iter
;
1001 if (stream_iter_is_ended
) {
1003 * The live stream iterator is ENDed. We remove that
1004 * iterator from the list and we restart the iteration
1005 * at the beginning of the live stream iterator array
1006 * to because the removal will shuffle the array.
1008 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1010 stream_iter_idx
= 0;
1016 if (curr_candidate_stream_iter
) {
1017 *candidate_stream_iter
= curr_candidate_stream_iter
;
1018 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1021 * The only case where we don't have a candidate for this trace
1022 * is if we reached the end of all the iterators.
1024 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1025 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1029 return stream_iter_status
;
1033 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1034 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1035 struct lttng_live_session
*session
,
1036 struct lttng_live_stream_iterator
**candidate_session_stream_iter
)
1038 enum lttng_live_iterator_status stream_iter_status
;
1039 uint64_t trace_idx
= 0;
1040 int64_t curr_candidate_msg_ts
= INT64_MAX
;
1041 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
1044 * Make sure we are attached to the session and look for new streams
1047 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1048 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1049 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1050 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1054 BT_ASSERT(session
->traces
);
1057 * Use while loops here rather then for loops so we can restart the
1058 * iteration if an element is removed from the array during the
1061 while (trace_idx
< session
->traces
->len
) {
1062 bool trace_is_ended
= false;
1063 struct lttng_live_stream_iterator
*stream_iter
;
1064 struct lttng_live_trace
*trace
=
1065 g_ptr_array_index(session
->traces
, trace_idx
);
1067 stream_iter_status
= next_stream_iterator_for_trace(
1068 lttng_live_msg_iter
, trace
, &stream_iter
);
1069 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1071 * All the live stream iterators for this trace are
1072 * ENDed. Remove the trace from this session.
1074 trace_is_ended
= true;
1075 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1079 if (!trace_is_ended
) {
1080 BT_ASSERT(stream_iter
);
1082 if (stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1083 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1084 curr_candidate_stream_iter
= stream_iter
;
1088 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1092 if (curr_candidate_stream_iter
) {
1093 *candidate_session_stream_iter
= curr_candidate_stream_iter
;
1094 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1097 * The only cases where we don't have a candidate for this
1099 * 1. if we reached the end of all the iterators of all the
1100 * traces of this session,
1101 * 2. if we never had live stream iterator in the first place.
1103 * In either cases, we return END.
1105 BT_ASSERT(session
->traces
->len
== 0);
1106 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1109 return stream_iter_status
;
1113 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1117 for (i
= 0; i
< count
; i
++) {
1118 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1123 bt_self_message_iterator_status
lttng_live_msg_iter_next(
1124 bt_self_message_iterator
*self_msg_it
,
1125 bt_message_array_const msgs
, uint64_t capacity
,
1128 bt_self_message_iterator_status status
;
1129 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1130 bt_self_message_iterator_get_data(self_msg_it
);
1131 struct lttng_live_component
*lttng_live
=
1132 lttng_live_msg_iter
->lttng_live_comp
;
1133 enum lttng_live_iterator_status stream_iter_status
;
1134 uint64_t session_idx
;
1138 BT_ASSERT(lttng_live_msg_iter
);
1141 * Clear all the invalid message reference that might be left over in
1144 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1147 * If no session are exposed on the relay found at the url provided by
1148 * the user, session count will be 0. In this case, we return status
1149 * end to return gracefully.
1151 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1152 if (lttng_live
->params
.sess_not_found_act
!=
1153 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1154 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1158 * The are no more active session for this session
1159 * name. Retry to create a viewer session for the
1160 * requested session name.
1162 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1163 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1169 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1170 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1174 * Here the muxing of message is done.
1176 * We need to iterate over all the streams of all the traces of all the
1177 * viewer sessions in order to get the message with the smallest
1178 * timestamp. In this case, a session is a viewer session and there is
1179 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1180 * kernel). Each viewer session can have multiple traces, for example,
1181 * 64bit UST viewer sessions could have multiple per-pid traces.
1183 * We iterate over the streams of each traces to update and see what is
1184 * their next message's timestamp. From those timestamps, we select the
1185 * message with the smallest timestamp as the best candidate message
1186 * for that trace and do the same thing across all the sessions.
1188 * We then compare the timestamp of best candidate message of all the
1189 * sessions to pick the message with the smallest timestamp and we
1192 while (*count
< capacity
) {
1193 struct lttng_live_stream_iterator
*next_stream_iter
= NULL
,
1194 *candidate_stream_iter
= NULL
;
1195 int64_t next_msg_ts_ns
= INT64_MAX
;
1197 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1200 * Use a while loop instead of a for loop so we can restart the
1201 * iteration if we remove an element. We can safely call
1202 * next_stream_iterator_for_session() multiple times on the
1203 * same session as we only fetch a new message if there is no
1204 * current next message for each live stream iterator.
1205 * If all live stream iterator of that session already have a
1206 * current next message, the function will simply exit return
1207 * the same candidate live stream iterator every time.
1209 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1210 struct lttng_live_session
*session
=
1211 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1214 /* Find the best candidate message to send downstream. */
1215 stream_iter_status
= next_stream_iterator_for_session(
1216 lttng_live_msg_iter
, session
,
1217 &candidate_stream_iter
);
1219 /* If we receive an END status, it means that either:
1220 * - Those traces never had active streams (UST with no
1221 * data produced yet),
1222 * - All live stream iterators have ENDed.*/
1223 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1224 if (session
->closed
&& session
->traces
->len
== 0) {
1226 * Remove the session from the list and restart the
1227 * iteration at the beginning of the array since the
1228 * removal shuffle the elements of the array.
1230 g_ptr_array_remove_index_fast(
1231 lttng_live_msg_iter
->sessions
,
1240 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1244 if (candidate_stream_iter
->current_msg_ts_ns
<= next_msg_ts_ns
) {
1245 next_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1246 next_stream_iter
= candidate_stream_iter
;
1252 if (!next_stream_iter
) {
1253 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1257 BT_ASSERT(next_stream_iter
->current_msg
);
1258 /* Ensure monotonicity. */
1259 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1260 next_stream_iter
->current_msg_ts_ns
);
1263 * Insert the next message to the message batch. This will set
1264 * stream iterator current messsage to NULL so that next time
1265 * we fetch the next message of that stream iterator
1267 BT_MESSAGE_MOVE_REF(msgs
[*count
], next_stream_iter
->current_msg
);
1270 /* Update the last timestamp in nanoseconds sent downstream. */
1271 lttng_live_msg_iter
->last_msg_ts_ns
= next_msg_ts_ns
;
1272 next_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1274 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1277 switch (stream_iter_status
) {
1278 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1279 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1282 * We received a again status but we have some messages
1283 * to send downstream. We send them and return OK for
1284 * now. On the next call we return again if there are
1285 * still no new message to send.
1287 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1289 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN
;
1292 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1293 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1295 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1296 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1298 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1299 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1300 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1301 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1302 /* Put all existing messages on error. */
1303 put_messages(msgs
, *count
);
1314 bt_self_message_iterator_status
lttng_live_msg_iter_init(
1315 bt_self_message_iterator
*self_msg_it
,
1316 bt_self_component_source
*self_comp_src
,
1317 bt_self_component_port_output
*self_port
)
1319 bt_self_message_iterator_status ret
=
1320 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1321 bt_self_component
*self_comp
=
1322 bt_self_component_source_as_self_component(self_comp_src
);
1323 struct lttng_live_component
*lttng_live
;
1324 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1326 BT_ASSERT(self_msg_it
);
1328 lttng_live
= bt_self_component_get_data(self_comp
);
1330 /* There can be only one downstream iterator at the same time. */
1331 BT_ASSERT(!lttng_live
->has_msg_iter
);
1332 lttng_live
->has_msg_iter
= true;
1334 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1335 if (!lttng_live_msg_iter
) {
1336 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1340 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1341 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1343 lttng_live_msg_iter
->active_stream_iter
= 0;
1344 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1345 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1346 (GDestroyNotify
) lttng_live_destroy_session
);
1347 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1349 lttng_live_msg_iter
->viewer_connection
=
1350 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1351 lttng_live_msg_iter
);
1352 if (!lttng_live_msg_iter
->viewer_connection
) {
1356 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1359 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1360 switch (lttng_live
->params
.sess_not_found_act
) {
1361 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1362 BT_LOGI("Unable to connect to the requested live viewer "
1363 "session. Keep trying to connect because of "
1364 "%s=\"%s\" component parameter: url=\"%s\"",
1365 SESS_NOT_FOUND_ACTION_PARAM
,
1366 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1367 lttng_live
->params
.url
->str
);
1369 case SESSION_NOT_FOUND_ACTION_FAIL
:
1370 BT_LOGE("Unable to connect to the requested live viewer "
1371 "session. Fail the message iterator"
1372 "initialization because of %s=\"%s\" "
1373 "component parameter: url =\"%s\"",
1374 SESS_NOT_FOUND_ACTION_PARAM
,
1375 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1376 lttng_live
->params
.url
->str
);
1378 case SESSION_NOT_FOUND_ACTION_END
:
1379 BT_LOGI("Unable to connect to the requested live viewer "
1380 "session. End gracefully at the first _next() "
1381 "call because of %s=\"%s\" component parameter: "
1382 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1383 SESS_NOT_FOUND_ACTION_END_STR
,
1384 lttng_live
->params
.url
->str
);
1389 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1393 ret
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1394 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1400 bt_query_status
lttng_live_query_list_sessions(const bt_value
*params
,
1401 const bt_value
**result
)
1403 bt_query_status status
= BT_QUERY_STATUS_OK
;
1404 const bt_value
*url_value
= NULL
;
1406 struct live_viewer_connection
*viewer_connection
= NULL
;
1408 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1410 BT_LOGW("Mandatory `%s` parameter missing", URL_PARAM
);
1411 status
= BT_QUERY_STATUS_INVALID_PARAMS
;
1415 if (!bt_value_is_string(url_value
)) {
1416 BT_LOGW("`%s` parameter is required to be a string value",
1418 status
= BT_QUERY_STATUS_INVALID_PARAMS
;
1422 url
= bt_value_string_get(url_value
);
1424 viewer_connection
= live_viewer_connection_create(url
, true, NULL
);
1425 if (!viewer_connection
) {
1429 status
= live_viewer_connection_list_sessions(viewer_connection
,
1431 if (status
!= BT_QUERY_STATUS_OK
) {
1438 BT_VALUE_PUT_REF_AND_RESET(*result
);
1441 status
= BT_QUERY_STATUS_ERROR
;
1445 if (viewer_connection
) {
1446 live_viewer_connection_destroy(viewer_connection
);
1452 bt_query_status
lttng_live_query(bt_self_component_class_source
*comp_class
,
1453 const bt_query_executor
*query_exec
,
1454 const char *object
, const bt_value
*params
,
1455 const bt_value
**result
)
1457 bt_query_status status
= BT_QUERY_STATUS_OK
;
1459 if (strcmp(object
, "sessions") == 0) {
1460 status
= lttng_live_query_list_sessions(params
, result
);
1462 BT_LOGW("Unknown query object `%s`", object
);
1463 status
= BT_QUERY_STATUS_INVALID_OBJECT
;
1472 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1474 if (lttng_live
->params
.url
) {
1475 g_string_free(lttng_live
->params
.url
, TRUE
);
1481 void lttng_live_component_finalize(bt_self_component_source
*component
)
1483 void *data
= bt_self_component_get_data(
1484 bt_self_component_source_as_self_component(component
));
1489 lttng_live_component_destroy_data(data
);
1493 enum session_not_found_action
parse_session_not_found_action_param(
1494 const bt_value
*no_session_param
)
1496 enum session_not_found_action action
;
1497 const char *no_session_act_str
;
1498 no_session_act_str
= bt_value_string_get(no_session_param
);
1499 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1500 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1501 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1502 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1503 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0) {
1504 action
= SESSION_NOT_FOUND_ACTION_END
;
1512 struct lttng_live_component
*lttng_live_component_create(const bt_value
*params
)
1514 struct lttng_live_component
*lttng_live
;
1515 const bt_value
*value
= NULL
;
1518 lttng_live
= g_new0(struct lttng_live_component
, 1);
1522 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1523 lttng_live
->has_msg_iter
= false;
1525 value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1526 if (!value
|| !bt_value_is_string(value
)) {
1527 BT_LOGW("Mandatory `%s` parameter missing or not a string",
1531 url
= bt_value_string_get(value
);
1532 lttng_live
->params
.url
= g_string_new(url
);
1533 if (!lttng_live
->params
.url
) {
1537 value
= bt_value_map_borrow_entry_value_const(params
,
1538 SESS_NOT_FOUND_ACTION_PARAM
);
1540 if (value
&& bt_value_is_string(value
)) {
1541 lttng_live
->params
.sess_not_found_act
=
1542 parse_session_not_found_action_param(value
);
1543 if (lttng_live
->params
.sess_not_found_act
== -1) {
1544 BT_LOGE("Unexpected value for `%s` parameter: "
1545 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1546 bt_value_string_get(value
));
1550 BT_LOGW("Optional `%s` parameter is missing or "
1551 "not a string value. Defaulting to %s=\"%s\".",
1552 SESS_NOT_FOUND_ACTION_PARAM
,
1553 SESS_NOT_FOUND_ACTION_PARAM
,
1554 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1555 lttng_live
->params
.sess_not_found_act
=
1556 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1562 lttng_live_component_destroy_data(lttng_live
);
1569 bt_self_component_status
lttng_live_component_init(
1570 bt_self_component_source
*self_comp
,
1571 const bt_value
*params
, UNUSED_VAR
void *init_method_data
)
1573 struct lttng_live_component
*lttng_live
;
1574 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
1576 lttng_live
= lttng_live_component_create(params
);
1578 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
1581 lttng_live
->self_comp
= self_comp
;
1583 if (lttng_live_is_canceled(lttng_live
)) {
1587 ret
= bt_self_component_source_add_output_port(
1588 lttng_live
->self_comp
, "out",
1590 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
1594 bt_self_component_set_data(
1595 bt_self_component_source_as_self_component(self_comp
),