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_msg_iter
*msg_iter
)
121 ret
= bt_self_message_iterator_is_interrupted(
122 msg_iter
->self_msg_iter
);
129 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
133 struct lttng_live_trace
*ret_trace
= NULL
;
135 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
136 struct lttng_live_trace
*trace
=
137 g_ptr_array_index(session
->traces
, trace_idx
);
138 if (trace
->id
== trace_id
) {
149 void lttng_live_destroy_trace(struct lttng_live_trace
*trace
)
151 bt_logging_level log_level
= trace
->log_level
;
152 bt_self_component
*self_comp
= trace
->self_comp
;
154 BT_COMP_LOGD("Destroy lttng_live_trace");
156 BT_ASSERT(trace
->stream_iterators
);
157 g_ptr_array_free(trace
->stream_iterators
, TRUE
);
159 BT_TRACE_PUT_REF_AND_RESET(trace
->trace
);
160 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace
->trace_class
);
162 lttng_live_metadata_fini(trace
);
167 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
170 struct lttng_live_trace
*trace
= NULL
;
171 bt_logging_level log_level
= session
->log_level
;
172 bt_self_component
*self_comp
= session
->self_comp
;
174 trace
= g_new0(struct lttng_live_trace
, 1);
178 trace
->log_level
= session
->log_level
;
179 trace
->self_comp
= session
->self_comp
;
180 trace
->session
= session
;
181 trace
->id
= trace_id
;
182 trace
->trace_class
= NULL
;
184 trace
->stream_iterators
= g_ptr_array_new_with_free_func(
185 (GDestroyNotify
) lttng_live_stream_iterator_destroy
);
186 BT_ASSERT(trace
->stream_iterators
);
187 trace
->new_metadata_needed
= true;
188 g_ptr_array_add(session
->traces
, trace
);
190 BT_COMP_LOGI("Create trace");
200 struct lttng_live_trace
*lttng_live_borrow_trace(
201 struct lttng_live_session
*session
, uint64_t trace_id
)
203 struct lttng_live_trace
*trace
;
205 trace
= lttng_live_find_trace(session
, trace_id
);
210 /* The session is the owner of the newly created trace. */
211 trace
= lttng_live_create_trace(session
, trace_id
);
218 int lttng_live_add_session(struct lttng_live_msg_iter
*lttng_live_msg_iter
,
219 uint64_t session_id
, const char *hostname
,
220 const char *session_name
)
223 struct lttng_live_session
*session
;
224 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
225 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
227 session
= g_new0(struct lttng_live_session
, 1);
232 session
->log_level
= lttng_live_msg_iter
->log_level
;
233 session
->self_comp
= lttng_live_msg_iter
->self_comp
;
234 session
->id
= session_id
;
235 session
->traces
= g_ptr_array_new_with_free_func(
236 (GDestroyNotify
) lttng_live_destroy_trace
);
237 BT_ASSERT(session
->traces
);
238 session
->lttng_live_msg_iter
= lttng_live_msg_iter
;
239 session
->new_streams_needed
= true;
240 session
->hostname
= g_string_new(hostname
);
241 BT_ASSERT(session
->hostname
);
243 session
->session_name
= g_string_new(session_name
);
244 BT_ASSERT(session
->session_name
);
246 BT_COMP_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
247 session
->id
, hostname
, session_name
);
248 g_ptr_array_add(lttng_live_msg_iter
->sessions
, session
);
251 BT_COMP_LOGE("Error adding session");
259 void lttng_live_destroy_session(struct lttng_live_session
*session
)
261 bt_logging_level log_level
;
262 bt_self_component
*self_comp
;
268 log_level
= session
->log_level
;
269 self_comp
= session
->self_comp
;
270 BT_COMP_LOGD("Destroy lttng live session");
271 if (session
->id
!= -1ULL) {
272 if (lttng_live_detach_session(session
)) {
273 if (session
->lttng_live_msg_iter
&&
274 !lttng_live_graph_is_canceled(
275 session
->lttng_live_msg_iter
)) {
276 /* Old relayd cannot detach sessions. */
277 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64
,
284 if (session
->traces
) {
285 g_ptr_array_free(session
->traces
, TRUE
);
288 if (session
->hostname
) {
289 g_string_free(session
->hostname
, TRUE
);
291 if (session
->session_name
) {
292 g_string_free(session
->session_name
, TRUE
);
301 void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
303 if (!lttng_live_msg_iter
) {
307 if (lttng_live_msg_iter
->sessions
) {
308 g_ptr_array_free(lttng_live_msg_iter
->sessions
, TRUE
);
311 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter
->viewer_connection
);
312 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
);
313 BT_ASSERT(lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
);
315 /* All stream iterators must be destroyed at this point. */
316 BT_ASSERT(lttng_live_msg_iter
->active_stream_iter
== 0);
317 lttng_live_msg_iter
->lttng_live_comp
->has_msg_iter
= false;
319 g_free(lttng_live_msg_iter
);
326 void lttng_live_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
328 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
330 BT_ASSERT(self_msg_iter
);
332 lttng_live_msg_iter
= bt_self_message_iterator_get_data(self_msg_iter
);
333 BT_ASSERT(lttng_live_msg_iter
);
334 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
338 enum lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
339 struct lttng_live_stream_iterator
*lttng_live_stream
)
341 bt_logging_level log_level
= lttng_live_stream
->log_level
;
342 bt_self_component
*self_comp
= lttng_live_stream
->self_comp
;
344 switch (lttng_live_stream
->state
) {
345 case LTTNG_LIVE_STREAM_QUIESCENT
:
346 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
348 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
350 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
352 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
354 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
356 case LTTNG_LIVE_STREAM_EOF
:
359 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
363 * For active no data stream, fetch next data. It can be either:
364 * - quiescent: need to put it in the prio heap at quiescent end
366 * - have data: need to wire up first event into the prio heap,
367 * - have no data on this stream at this point: need to retry (AGAIN) or
371 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
372 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
373 struct lttng_live_stream_iterator
*lttng_live_stream
)
375 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
376 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
377 enum lttng_live_iterator_status ret
=
378 LTTNG_LIVE_ITERATOR_STATUS_OK
;
379 struct packet_index index
;
380 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
382 if (lttng_live_stream
->trace
->new_metadata_needed
) {
383 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
386 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
387 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
390 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
&&
391 lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
394 ret
= lttng_live_get_next_index(lttng_live_msg_iter
, lttng_live_stream
,
396 if (ret
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
399 BT_ASSERT(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
400 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
401 uint64_t last_inact_ts
= lttng_live_stream
->last_inactivity_ts
,
402 curr_inact_ts
= lttng_live_stream
->current_inactivity_ts
;
404 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
&&
405 last_inact_ts
== curr_inact_ts
) {
406 ret
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
407 print_stream_state(lttng_live_stream
);
409 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
413 lttng_live_stream
->base_offset
= index
.offset
;
414 lttng_live_stream
->offset
= index
.offset
;
415 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
417 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
) {
418 ret
= lttng_live_iterator_next_check_stream_state(lttng_live_stream
);
424 * Creation of the message requires the ctf trace class to be created
425 * beforehand, but the live protocol gives us all streams (including metadata)
426 * at once. So we split it in three steps: getting streams, getting metadata
427 * (which creates the ctf trace class), and then creating the per-stream
431 enum lttng_live_iterator_status
lttng_live_get_session(
432 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
433 struct lttng_live_session
*session
)
435 enum lttng_live_iterator_status status
;
439 if (!session
->attached
) {
440 ret
= lttng_live_attach_session(session
);
442 if (lttng_live_msg_iter
&& lttng_live_graph_is_canceled(
443 lttng_live_msg_iter
)) {
444 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
446 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
452 status
= lttng_live_get_new_streams(session
);
453 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
454 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
457 for (trace_idx
= 0; trace_idx
< session
->traces
->len
; trace_idx
++) {
458 struct lttng_live_trace
*trace
=
459 g_ptr_array_index(session
->traces
, trace_idx
);
461 status
= lttng_live_metadata_update(trace
);
462 if (status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
463 status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
467 status
= lttng_live_lazy_msg_init(session
);
474 void lttng_live_need_new_streams(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
476 uint64_t session_idx
;
478 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
480 struct lttng_live_session
*session
=
481 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
482 session
->new_streams_needed
= true;
487 void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
489 uint64_t session_idx
, trace_idx
;
491 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
493 struct lttng_live_session
*session
=
494 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
495 session
->new_streams_needed
= true;
496 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
498 struct lttng_live_trace
*trace
=
499 g_ptr_array_index(session
->traces
, trace_idx
);
500 trace
->new_metadata_needed
= true;
506 enum lttng_live_iterator_status
507 lttng_live_iterator_handle_new_streams_and_metadata(
508 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
510 enum lttng_live_iterator_status ret
=
511 LTTNG_LIVE_ITERATOR_STATUS_OK
;
512 uint64_t session_idx
= 0, nr_sessions_opened
= 0;
513 struct lttng_live_session
*session
;
514 enum session_not_found_action sess_not_found_act
=
515 lttng_live_msg_iter
->lttng_live_comp
->params
.sess_not_found_act
;
518 * In a remotely distant future, we could add a "new
519 * session" flag to the protocol, which would tell us that we
520 * need to query for new sessions even though we have sessions
523 if (lttng_live_msg_iter
->sessions
->len
== 0) {
524 if (sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
) {
525 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
529 * Retry to create a viewer session for the requested
532 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
533 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
539 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
541 session
= g_ptr_array_index(lttng_live_msg_iter
->sessions
,
543 ret
= lttng_live_get_session(lttng_live_msg_iter
, session
);
545 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
547 case LTTNG_LIVE_ITERATOR_STATUS_END
:
548 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
553 if (!session
->closed
) {
554 nr_sessions_opened
++;
558 if (ret
== LTTNG_LIVE_ITERATOR_STATUS_OK
&&
559 sess_not_found_act
!= SESSION_NOT_FOUND_ACTION_CONTINUE
&&
560 nr_sessions_opened
== 0) {
561 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
567 enum lttng_live_iterator_status
emit_inactivity_message(
568 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
569 struct lttng_live_stream_iterator
*stream_iter
,
570 bt_message
**message
, uint64_t timestamp
)
572 enum lttng_live_iterator_status ret
=
573 LTTNG_LIVE_ITERATOR_STATUS_OK
;
574 bt_message
*msg
= NULL
;
576 BT_ASSERT(stream_iter
->trace
->clock_class
);
578 msg
= bt_message_message_iterator_inactivity_create(
579 lttng_live_msg_iter
->self_msg_iter
,
580 stream_iter
->trace
->clock_class
,
591 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
592 bt_message_put_ref(msg
);
597 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
598 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
599 struct lttng_live_stream_iterator
*lttng_live_stream
,
600 bt_message
**message
)
602 enum lttng_live_iterator_status ret
=
603 LTTNG_LIVE_ITERATOR_STATUS_OK
;
605 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
606 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
609 if (lttng_live_stream
->current_inactivity_ts
==
610 lttng_live_stream
->last_inactivity_ts
) {
611 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
612 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
616 ret
= emit_inactivity_message(lttng_live_msg_iter
, lttng_live_stream
,
617 message
, lttng_live_stream
->current_inactivity_ts
);
619 lttng_live_stream
->last_inactivity_ts
=
620 lttng_live_stream
->current_inactivity_ts
;
626 int live_get_msg_ts_ns(struct lttng_live_stream_iterator
*stream_iter
,
627 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
628 const bt_message
*msg
, int64_t last_msg_ts_ns
,
631 const bt_clock_class
*clock_class
= NULL
;
632 const bt_clock_snapshot
*clock_snapshot
= NULL
;
634 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
635 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
640 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
641 "last-msg-ts=%" PRId64
, lttng_live_msg_iter
, msg
,
644 switch (bt_message_get_type(msg
)) {
645 case BT_MESSAGE_TYPE_EVENT
:
647 bt_message_event_borrow_stream_class_default_clock_class_const(
649 BT_ASSERT(clock_class
);
651 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
654 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
656 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
658 BT_ASSERT(clock_class
);
660 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
663 case BT_MESSAGE_TYPE_PACKET_END
:
665 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
667 BT_ASSERT(clock_class
);
669 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
672 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
674 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
676 BT_ASSERT(clock_class
);
678 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
681 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
683 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
685 BT_ASSERT(clock_class
);
687 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
690 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
692 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
696 /* All the other messages have a higher priority */
697 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
698 *ts_ns
= last_msg_ts_ns
;
702 clock_class
= bt_clock_snapshot_borrow_clock_class_const(clock_snapshot
);
703 BT_ASSERT(clock_class
);
705 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
707 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
708 "clock-snapshot-addr=%p", clock_snapshot
);
719 BT_COMP_LOGD("Found message's timestamp: "
720 "iter-data-addr=%p, msg-addr=%p, "
721 "last-msg-ts=%" PRId64
", ts=%" PRId64
,
722 lttng_live_msg_iter
, msg
, last_msg_ts_ns
, *ts_ns
);
729 enum lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
730 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
731 struct lttng_live_stream_iterator
*lttng_live_stream
,
732 bt_message
**message
)
734 enum lttng_live_iterator_status ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
735 enum bt_msg_iter_status status
;
736 uint64_t session_idx
, trace_idx
;
737 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
738 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
740 for (session_idx
= 0; session_idx
< lttng_live_msg_iter
->sessions
->len
;
742 struct lttng_live_session
*session
=
743 g_ptr_array_index(lttng_live_msg_iter
->sessions
, session_idx
);
745 if (session
->new_streams_needed
) {
746 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
749 for (trace_idx
= 0; trace_idx
< session
->traces
->len
;
751 struct lttng_live_trace
*trace
=
752 g_ptr_array_index(session
->traces
, trace_idx
);
753 if (trace
->new_metadata_needed
) {
754 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
760 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
761 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
765 status
= bt_msg_iter_get_next_message(lttng_live_stream
->msg_iter
,
766 lttng_live_msg_iter
->self_msg_iter
, message
);
768 case BT_MSG_ITER_STATUS_EOF
:
769 ret
= LTTNG_LIVE_ITERATOR_STATUS_END
;
771 case BT_MSG_ITER_STATUS_OK
:
772 ret
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
774 case BT_MSG_ITER_STATUS_AGAIN
:
776 * Continue immediately (end of packet). The next
777 * get_index may return AGAIN to delay the following
780 ret
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
782 case BT_MSG_ITER_STATUS_INVAL
:
783 /* No argument provided by the user, so don't return INVAL. */
784 case BT_MSG_ITER_STATUS_ERROR
:
786 ret
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
787 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
788 lttng_live_stream
->msg_iter
);
798 * handle_no_data_streams()
800 * - for each ACTIVE_NO_DATA stream:
801 * - query relayd for stream data, or quiescence info.
802 * - if need metadata, get metadata, goto retry.
803 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
804 * - if quiescent, move to QUIESCENT streams
805 * - if fetched data, move to ACTIVE_DATA streams
806 * (at this point each stream either has data, or is quiescent)
810 * handle_new_streams_and_metadata()
811 * - query relayd for known streams, add them as ACTIVE_NO_DATA
812 * - query relayd for metadata
814 * call handle_active_no_data_streams()
816 * handle_quiescent_streams()
817 * - if at least one stream is ACTIVE_DATA:
818 * - peek stream event with lowest timestamp -> next_ts
819 * - for each quiescent stream
820 * - if next_ts >= quiescent end
821 * - set state to ACTIVE_NO_DATA
823 * - for each quiescent stream
824 * - set state to ACTIVE_NO_DATA
826 * call handle_active_no_data_streams()
828 * handle_active_data_streams()
829 * - if at least one stream is ACTIVE_DATA:
830 * - get stream event with lowest timestamp from heap
831 * - make that stream event the current message.
832 * - move this stream heap position to its next event
833 * - if we need to fetch data from relayd, move
834 * stream to ACTIVE_NO_DATA.
838 * end criterion: ctrl-c on client. If relayd exits or the session
839 * closes on the relay daemon side, we keep on waiting for streams.
840 * Eventually handle --end timestamp (also an end criterion).
842 * When disconnected from relayd: try to re-connect endlessly.
845 enum lttng_live_iterator_status
lttng_live_iterator_next_on_stream(
846 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
847 struct lttng_live_stream_iterator
*stream_iter
,
848 bt_message
**curr_msg
)
850 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
851 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
852 enum lttng_live_iterator_status live_status
;
855 print_stream_state(stream_iter
);
856 live_status
= lttng_live_iterator_handle_new_streams_and_metadata(
857 lttng_live_msg_iter
);
858 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
861 live_status
= lttng_live_iterator_next_handle_one_no_data_stream(
862 lttng_live_msg_iter
, stream_iter
);
863 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
866 live_status
= lttng_live_iterator_next_handle_one_quiescent_stream(
867 lttng_live_msg_iter
, stream_iter
, curr_msg
);
868 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
869 BT_ASSERT(!*curr_msg
);
875 live_status
= lttng_live_iterator_next_handle_one_active_data_stream(
876 lttng_live_msg_iter
, stream_iter
, curr_msg
);
877 if (live_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
878 BT_ASSERT(!*curr_msg
);
882 if (live_status
== LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
) {
890 enum lttng_live_iterator_status
next_stream_iterator_for_trace(
891 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
892 struct lttng_live_trace
*live_trace
,
893 struct lttng_live_stream_iterator
**candidate_stream_iter
)
895 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
896 enum lttng_live_iterator_status stream_iter_status
;;
897 int64_t curr_candidate_msg_ts
= INT64_MAX
;
898 uint64_t stream_iter_idx
;
899 bt_logging_level log_level
= lttng_live_msg_iter
->log_level
;
900 bt_self_component
*self_comp
= lttng_live_msg_iter
->self_comp
;
902 BT_ASSERT(live_trace
);
903 BT_ASSERT(live_trace
->stream_iterators
);
905 * Update the current message of every stream iterators of this trace.
906 * The current msg of every stream must have a timestamp equal or
907 * larger than the last message returned by this iterator. We must
908 * ensure monotonicity.
911 while (stream_iter_idx
< live_trace
->stream_iterators
->len
) {
912 bool stream_iter_is_ended
= false;
913 struct lttng_live_stream_iterator
*stream_iter
=
914 g_ptr_array_index(live_trace
->stream_iterators
,
918 * Find if there is are now current message for this stream
921 while (!stream_iter
->current_msg
) {
922 bt_message
*msg
= NULL
;
923 int64_t curr_msg_ts_ns
= INT64_MAX
;
924 stream_iter_status
= lttng_live_iterator_next_on_stream(
925 lttng_live_msg_iter
, stream_iter
, &msg
);
927 BT_COMP_LOGD("live stream iterator returned status :%s",
928 print_live_iterator_status(stream_iter_status
));
929 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
930 stream_iter_is_ended
= true;
934 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
941 * Get the timestamp in nanoseconds from origin of this
944 live_get_msg_ts_ns(stream_iter
, lttng_live_msg_iter
,
945 msg
, lttng_live_msg_iter
->last_msg_ts_ns
,
949 * Check if the message of the current live stream
950 * iterator occured at the exact same time or after the
951 * last message returned by this component's message
952 * iterator. If not, we return an error.
954 if (curr_msg_ts_ns
>= lttng_live_msg_iter
->last_msg_ts_ns
) {
955 stream_iter
->current_msg
= msg
;
956 stream_iter
->current_msg_ts_ns
= curr_msg_ts_ns
;
959 * We received a message in the past. To ensure
960 * monotonicity, we can't send it forward.
962 BT_COMP_LOGE("Message's timestamp is less than "
963 "lttng-live's message iterator's last "
964 "returned timestamp: "
965 "lttng-live-msg-iter-addr=%p, ts=%" PRId64
", "
966 "last-msg-ts=%" PRId64
,
967 lttng_live_msg_iter
, curr_msg_ts_ns
,
968 lttng_live_msg_iter
->last_msg_ts_ns
);
969 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
974 if (!stream_iter_is_ended
&&
975 stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
977 * Update the current best candidate message for the
978 * stream iterator of thise live trace to be forwarded
981 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
982 curr_candidate_stream_iter
= stream_iter
;
985 if (stream_iter_is_ended
) {
987 * The live stream iterator is ENDed. We remove that
988 * iterator from the list and we restart the iteration
989 * at the beginning of the live stream iterator array
990 * to because the removal will shuffle the array.
992 g_ptr_array_remove_index_fast(live_trace
->stream_iterators
,
1000 if (curr_candidate_stream_iter
) {
1001 *candidate_stream_iter
= curr_candidate_stream_iter
;
1002 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1005 * The only case where we don't have a candidate for this trace
1006 * is if we reached the end of all the iterators.
1008 BT_ASSERT(live_trace
->stream_iterators
->len
== 0);
1009 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1013 return stream_iter_status
;
1017 enum lttng_live_iterator_status
next_stream_iterator_for_session(
1018 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1019 struct lttng_live_session
*session
,
1020 struct lttng_live_stream_iterator
**candidate_session_stream_iter
)
1022 enum lttng_live_iterator_status stream_iter_status
;
1023 uint64_t trace_idx
= 0;
1024 int64_t curr_candidate_msg_ts
= INT64_MAX
;
1025 struct lttng_live_stream_iterator
*curr_candidate_stream_iter
= NULL
;
1028 * Make sure we are attached to the session and look for new streams
1031 stream_iter_status
= lttng_live_get_session(lttng_live_msg_iter
, session
);
1032 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
&&
1033 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
&&
1034 stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_END
) {
1038 BT_ASSERT(session
->traces
);
1041 * Use while loops here rather then for loops so we can restart the
1042 * iteration if an element is removed from the array during the
1045 while (trace_idx
< session
->traces
->len
) {
1046 bool trace_is_ended
= false;
1047 struct lttng_live_stream_iterator
*stream_iter
;
1048 struct lttng_live_trace
*trace
=
1049 g_ptr_array_index(session
->traces
, trace_idx
);
1051 stream_iter_status
= next_stream_iterator_for_trace(
1052 lttng_live_msg_iter
, trace
, &stream_iter
);
1053 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1055 * All the live stream iterators for this trace are
1056 * ENDed. Remove the trace from this session.
1058 trace_is_ended
= true;
1059 } else if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1063 if (!trace_is_ended
) {
1064 BT_ASSERT(stream_iter
);
1066 if (stream_iter
->current_msg_ts_ns
<= curr_candidate_msg_ts
) {
1067 curr_candidate_msg_ts
= stream_iter
->current_msg_ts_ns
;
1068 curr_candidate_stream_iter
= stream_iter
;
1072 g_ptr_array_remove_index_fast(session
->traces
, trace_idx
);
1076 if (curr_candidate_stream_iter
) {
1077 *candidate_session_stream_iter
= curr_candidate_stream_iter
;
1078 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1081 * The only cases where we don't have a candidate for this
1083 * 1. if we reached the end of all the iterators of all the
1084 * traces of this session,
1085 * 2. if we never had live stream iterator in the first place.
1087 * In either cases, we return END.
1089 BT_ASSERT(session
->traces
->len
== 0);
1090 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1093 return stream_iter_status
;
1097 void put_messages(bt_message_array_const msgs
, uint64_t count
)
1101 for (i
= 0; i
< count
; i
++) {
1102 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
1107 bt_component_class_message_iterator_next_method_status
lttng_live_msg_iter_next(
1108 bt_self_message_iterator
*self_msg_it
,
1109 bt_message_array_const msgs
, uint64_t capacity
,
1112 bt_component_class_message_iterator_next_method_status status
;
1113 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1114 bt_self_message_iterator_get_data(self_msg_it
);
1115 struct lttng_live_component
*lttng_live
=
1116 lttng_live_msg_iter
->lttng_live_comp
;
1117 enum lttng_live_iterator_status stream_iter_status
;
1118 uint64_t session_idx
;
1122 BT_ASSERT(lttng_live_msg_iter
);
1125 * Clear all the invalid message reference that might be left over in
1128 memset(msgs
, 0, capacity
* sizeof(*msgs
));
1131 * If no session are exposed on the relay found at the url provided by
1132 * the user, session count will be 0. In this case, we return status
1133 * end to return gracefully.
1135 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1136 if (lttng_live
->params
.sess_not_found_act
!=
1137 SESSION_NOT_FOUND_ACTION_CONTINUE
) {
1138 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1142 * The are no more active session for this session
1143 * name. Retry to create a viewer session for the
1144 * requested session name.
1146 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1147 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1153 if (lttng_live_msg_iter
->active_stream_iter
== 0) {
1154 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter
);
1158 * Here the muxing of message is done.
1160 * We need to iterate over all the streams of all the traces of all the
1161 * viewer sessions in order to get the message with the smallest
1162 * timestamp. In this case, a session is a viewer session and there is
1163 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1164 * kernel). Each viewer session can have multiple traces, for example,
1165 * 64bit UST viewer sessions could have multiple per-pid traces.
1167 * We iterate over the streams of each traces to update and see what is
1168 * their next message's timestamp. From those timestamps, we select the
1169 * message with the smallest timestamp as the best candidate message
1170 * for that trace and do the same thing across all the sessions.
1172 * We then compare the timestamp of best candidate message of all the
1173 * sessions to pick the message with the smallest timestamp and we
1176 while (*count
< capacity
) {
1177 struct lttng_live_stream_iterator
*next_stream_iter
= NULL
,
1178 *candidate_stream_iter
= NULL
;
1179 int64_t next_msg_ts_ns
= INT64_MAX
;
1181 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1184 * Use a while loop instead of a for loop so we can restart the
1185 * iteration if we remove an element. We can safely call
1186 * next_stream_iterator_for_session() multiple times on the
1187 * same session as we only fetch a new message if there is no
1188 * current next message for each live stream iterator.
1189 * If all live stream iterator of that session already have a
1190 * current next message, the function will simply exit return
1191 * the same candidate live stream iterator every time.
1193 while (session_idx
< lttng_live_msg_iter
->sessions
->len
) {
1194 struct lttng_live_session
*session
=
1195 g_ptr_array_index(lttng_live_msg_iter
->sessions
,
1198 /* Find the best candidate message to send downstream. */
1199 stream_iter_status
= next_stream_iterator_for_session(
1200 lttng_live_msg_iter
, session
,
1201 &candidate_stream_iter
);
1203 /* If we receive an END status, it means that either:
1204 * - Those traces never had active streams (UST with no
1205 * data produced yet),
1206 * - All live stream iterators have ENDed.*/
1207 if (stream_iter_status
== LTTNG_LIVE_ITERATOR_STATUS_END
) {
1208 if (session
->closed
&& session
->traces
->len
== 0) {
1210 * Remove the session from the list and restart the
1211 * iteration at the beginning of the array since the
1212 * removal shuffle the elements of the array.
1214 g_ptr_array_remove_index_fast(
1215 lttng_live_msg_iter
->sessions
,
1224 if (stream_iter_status
!= LTTNG_LIVE_ITERATOR_STATUS_OK
) {
1228 if (candidate_stream_iter
->current_msg_ts_ns
<= next_msg_ts_ns
) {
1229 next_msg_ts_ns
= candidate_stream_iter
->current_msg_ts_ns
;
1230 next_stream_iter
= candidate_stream_iter
;
1236 if (!next_stream_iter
) {
1237 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1241 BT_ASSERT(next_stream_iter
->current_msg
);
1242 /* Ensure monotonicity. */
1243 BT_ASSERT(lttng_live_msg_iter
->last_msg_ts_ns
<=
1244 next_stream_iter
->current_msg_ts_ns
);
1247 * Insert the next message to the message batch. This will set
1248 * stream iterator current messsage to NULL so that next time
1249 * we fetch the next message of that stream iterator
1251 BT_MESSAGE_MOVE_REF(msgs
[*count
], next_stream_iter
->current_msg
);
1254 /* Update the last timestamp in nanoseconds sent downstream. */
1255 lttng_live_msg_iter
->last_msg_ts_ns
= next_msg_ts_ns
;
1256 next_stream_iter
->current_msg_ts_ns
= INT64_MAX
;
1258 stream_iter_status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
1261 switch (stream_iter_status
) {
1262 case LTTNG_LIVE_ITERATOR_STATUS_OK
:
1263 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
1266 * We received a again status but we have some messages
1267 * to send downstream. We send them and return OK for
1268 * now. On the next call we return again if there are
1269 * still no new message to send.
1271 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1273 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
1276 case LTTNG_LIVE_ITERATOR_STATUS_END
:
1277 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1279 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
1280 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1282 case LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
1283 case LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
1284 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
1285 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1286 /* Put all existing messages on error. */
1287 put_messages(msgs
, *count
);
1298 bt_component_class_message_iterator_init_method_status
lttng_live_msg_iter_init(
1299 bt_self_message_iterator
*self_msg_it
,
1300 bt_self_component_source
*self_comp_src
,
1301 bt_self_component_port_output
*self_port
)
1303 bt_component_class_message_iterator_init_method_status ret
=
1304 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1305 bt_self_component
*self_comp
=
1306 bt_self_component_source_as_self_component(self_comp_src
);
1307 struct lttng_live_component
*lttng_live
;
1308 struct lttng_live_msg_iter
*lttng_live_msg_iter
;
1309 bt_logging_level log_level
;
1311 BT_ASSERT(self_msg_it
);
1313 lttng_live
= bt_self_component_get_data(self_comp
);
1314 log_level
= lttng_live
->log_level
;
1315 self_comp
= lttng_live
->self_comp
;
1317 /* There can be only one downstream iterator at the same time. */
1318 BT_ASSERT(!lttng_live
->has_msg_iter
);
1319 lttng_live
->has_msg_iter
= true;
1321 lttng_live_msg_iter
= g_new0(struct lttng_live_msg_iter
, 1);
1322 if (!lttng_live_msg_iter
) {
1323 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1327 lttng_live_msg_iter
->log_level
= lttng_live
->log_level
;
1328 lttng_live_msg_iter
->self_comp
= lttng_live
->self_comp
;
1329 lttng_live_msg_iter
->lttng_live_comp
= lttng_live
;
1330 lttng_live_msg_iter
->self_msg_iter
= self_msg_it
;
1332 lttng_live_msg_iter
->active_stream_iter
= 0;
1333 lttng_live_msg_iter
->last_msg_ts_ns
= INT64_MIN
;
1334 lttng_live_msg_iter
->sessions
= g_ptr_array_new_with_free_func(
1335 (GDestroyNotify
) lttng_live_destroy_session
);
1336 BT_ASSERT(lttng_live_msg_iter
->sessions
);
1338 lttng_live_msg_iter
->viewer_connection
=
1339 live_viewer_connection_create(lttng_live
->params
.url
->str
, false,
1340 lttng_live_msg_iter
);
1341 if (!lttng_live_msg_iter
->viewer_connection
) {
1345 if (lttng_live_create_viewer_session(lttng_live_msg_iter
)) {
1348 if (lttng_live_msg_iter
->sessions
->len
== 0) {
1349 switch (lttng_live
->params
.sess_not_found_act
) {
1350 case SESSION_NOT_FOUND_ACTION_CONTINUE
:
1351 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1352 "session. Keep trying to connect because of "
1353 "%s=\"%s\" component parameter: url=\"%s\"",
1354 SESS_NOT_FOUND_ACTION_PARAM
,
1355 SESS_NOT_FOUND_ACTION_CONTINUE_STR
,
1356 lttng_live
->params
.url
->str
);
1358 case SESSION_NOT_FOUND_ACTION_FAIL
:
1359 BT_COMP_LOGE("Unable to connect to the requested live viewer "
1360 "session. Fail the message iterator"
1361 "initialization because of %s=\"%s\" "
1362 "component parameter: url =\"%s\"",
1363 SESS_NOT_FOUND_ACTION_PARAM
,
1364 SESS_NOT_FOUND_ACTION_FAIL_STR
,
1365 lttng_live
->params
.url
->str
);
1367 case SESSION_NOT_FOUND_ACTION_END
:
1368 BT_COMP_LOGI("Unable to connect to the requested live viewer "
1369 "session. End gracefully at the first _next() "
1370 "call because of %s=\"%s\" component parameter: "
1371 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1372 SESS_NOT_FOUND_ACTION_END_STR
,
1373 lttng_live
->params
.url
->str
);
1378 bt_self_message_iterator_set_data(self_msg_it
, lttng_live_msg_iter
);
1382 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1383 lttng_live_msg_iter_destroy(lttng_live_msg_iter
);
1389 bt_component_class_query_method_status
lttng_live_query_list_sessions(
1390 const bt_value
*params
, const bt_value
**result
,
1391 bt_logging_level log_level
)
1393 bt_component_class_query_method_status status
=
1394 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1395 const bt_value
*url_value
= NULL
;
1397 struct live_viewer_connection
*viewer_connection
= NULL
;
1398 bt_self_component
*self_comp
= NULL
;
1400 url_value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1402 BT_COMP_LOGW("Mandatory `%s` parameter missing", URL_PARAM
);
1403 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS
;
1407 if (!bt_value_is_string(url_value
)) {
1408 BT_COMP_LOGW("`%s` parameter is required to be a string value",
1410 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_PARAMS
;
1414 url
= bt_value_string_get(url_value
);
1416 viewer_connection
= live_viewer_connection_create(url
, true, NULL
);
1417 if (!viewer_connection
) {
1421 status
= live_viewer_connection_list_sessions(viewer_connection
,
1423 if (status
!= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
) {
1430 BT_VALUE_PUT_REF_AND_RESET(*result
);
1433 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
1437 if (viewer_connection
) {
1438 live_viewer_connection_destroy(viewer_connection
);
1444 bt_component_class_query_method_status
lttng_live_query(
1445 bt_self_component_class_source
*comp_class
,
1446 const bt_query_executor
*query_exec
,
1447 const char *object
, const bt_value
*params
,
1448 bt_logging_level log_level
, const bt_value
**result
)
1450 bt_component_class_query_method_status status
=
1451 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
1452 bt_self_component
*self_comp
= NULL
;
1454 if (strcmp(object
, "sessions") == 0) {
1455 status
= lttng_live_query_list_sessions(params
, result
,
1458 BT_COMP_LOGI("Unknown query object `%s`", object
);
1459 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_INVALID_OBJECT
;
1468 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1473 if (lttng_live
->params
.url
) {
1474 g_string_free(lttng_live
->params
.url
, TRUE
);
1480 void lttng_live_component_finalize(bt_self_component_source
*component
)
1482 void *data
= bt_self_component_get_data(
1483 bt_self_component_source_as_self_component(component
));
1488 lttng_live_component_destroy_data(data
);
1492 enum session_not_found_action
parse_session_not_found_action_param(
1493 const bt_value
*no_session_param
)
1495 enum session_not_found_action action
;
1496 const char *no_session_act_str
;
1497 no_session_act_str
= bt_value_string_get(no_session_param
);
1498 if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_CONTINUE_STR
) == 0) {
1499 action
= SESSION_NOT_FOUND_ACTION_CONTINUE
;
1500 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_FAIL_STR
) == 0) {
1501 action
= SESSION_NOT_FOUND_ACTION_FAIL
;
1502 } else if (strcmp(no_session_act_str
, SESS_NOT_FOUND_ACTION_END_STR
) == 0) {
1503 action
= SESSION_NOT_FOUND_ACTION_END
;
1511 struct lttng_live_component
*lttng_live_component_create(const bt_value
*params
,
1512 bt_logging_level log_level
, bt_self_component
*self_comp
)
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
->log_level
= log_level
;
1523 lttng_live
->self_comp
= self_comp
;
1524 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1525 lttng_live
->has_msg_iter
= false;
1527 value
= bt_value_map_borrow_entry_value_const(params
, URL_PARAM
);
1528 if (!value
|| !bt_value_is_string(value
)) {
1529 BT_COMP_LOGW("Mandatory `%s` parameter missing or not a string",
1533 url
= bt_value_string_get(value
);
1534 lttng_live
->params
.url
= g_string_new(url
);
1535 if (!lttng_live
->params
.url
) {
1539 value
= bt_value_map_borrow_entry_value_const(params
,
1540 SESS_NOT_FOUND_ACTION_PARAM
);
1542 if (value
&& bt_value_is_string(value
)) {
1543 lttng_live
->params
.sess_not_found_act
=
1544 parse_session_not_found_action_param(value
);
1545 if (lttng_live
->params
.sess_not_found_act
== -1) {
1546 BT_COMP_LOGE("Unexpected value for `%s` parameter: "
1547 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM
,
1548 bt_value_string_get(value
));
1552 BT_COMP_LOGW("Optional `%s` parameter is missing or "
1553 "not a string value. Defaulting to %s=\"%s\".",
1554 SESS_NOT_FOUND_ACTION_PARAM
,
1555 SESS_NOT_FOUND_ACTION_PARAM
,
1556 SESS_NOT_FOUND_ACTION_CONTINUE_STR
);
1557 lttng_live
->params
.sess_not_found_act
=
1558 SESSION_NOT_FOUND_ACTION_CONTINUE
;
1564 lttng_live_component_destroy_data(lttng_live
);
1571 bt_component_class_init_method_status
lttng_live_component_init(
1572 bt_self_component_source
*self_comp_src
,
1573 const bt_value
*params
, __attribute__((unused
)) void *init_method_data
)
1575 struct lttng_live_component
*lttng_live
;
1576 bt_component_class_init_method_status ret
=
1577 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
1578 bt_self_component
*self_comp
=
1579 bt_self_component_source_as_self_component(self_comp_src
);
1580 bt_logging_level log_level
= bt_component_get_logging_level(
1581 bt_self_component_as_component(self_comp
));
1582 bt_self_component_add_port_status add_port_status
;
1584 lttng_live
= lttng_live_component_create(params
, log_level
, self_comp
);
1586 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1590 add_port_status
= bt_self_component_source_add_output_port(
1591 self_comp_src
, "out", NULL
, NULL
);
1592 switch (add_port_status
) {
1593 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
1594 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
1596 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
1597 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
1603 bt_self_component_set_data(self_comp
, lttng_live
);
1607 lttng_live_component_destroy_data(lttng_live
);