4 * Babeltrace CTF LTTng-live Client Component
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC"
33 #include <babeltrace/ctf-ir/packet.h>
34 #include <babeltrace/graph/component-source.h>
35 #include <babeltrace/graph/private-port.h>
36 #include <babeltrace/graph/port.h>
37 #include <babeltrace/graph/private-component.h>
38 #include <babeltrace/graph/private-component-source.h>
39 #include <babeltrace/graph/private-notification-iterator.h>
40 #include <babeltrace/graph/notification-stream.h>
41 #include <babeltrace/graph/notification-packet.h>
42 #include <babeltrace/graph/notification-event.h>
43 #include <babeltrace/graph/notification-heap.h>
44 #include <babeltrace/graph/notification-iterator.h>
45 #include <babeltrace/graph/notification-inactivity.h>
46 #include <babeltrace/graph/graph.h>
47 #include <babeltrace/compiler-internal.h>
52 #include <plugins-common.h>
54 #include "data-stream.h"
56 #include "lttng-live-internal.h"
58 #define MAX_QUERY_SIZE (256*1024)
60 #define print_dbg(fmt, ...) BT_LOGD(fmt, ## __VA_ARGS__)
62 static const char *print_state(struct lttng_live_stream_iterator
*s
)
65 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
66 return "ACTIVE_NO_DATA";
67 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
68 return "QUIESCENT_NO_DATA";
69 case LTTNG_LIVE_STREAM_QUIESCENT
:
71 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
73 case LTTNG_LIVE_STREAM_EOF
:
80 #define print_stream_state(stream) \
81 print_dbg("stream %s state %s last_inact_ts %" PRId64 " cur_inact_ts %" PRId64, \
82 bt_port_get_name(bt_port_from_private_port(stream->port)), \
83 print_state(stream), stream->last_returned_inactivity_timestamp, \
84 stream->current_inactivity_timestamp)
87 int lttng_live_add_port(struct lttng_live_component
*lttng_live
,
88 struct lttng_live_stream_iterator
*stream_iter
)
91 struct bt_private_port
*private_port
;
92 char name
[STREAM_NAME_MAX_LEN
];
94 ret
= sprintf(name
, STREAM_NAME_PREFIX
"%" PRIu64
, stream_iter
->viewer_stream_id
);
96 strcpy(stream_iter
->name
, name
);
97 private_port
= bt_private_component_source_add_output_private_port(
98 lttng_live
->private_component
, name
, stream_iter
);
102 BT_LOGI("Added port %s", name
);
104 if (lttng_live
->no_stream_port
) {
105 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
109 BT_PUT(lttng_live
->no_stream_port
);
110 lttng_live
->no_stream_iter
->port
= NULL
;
112 stream_iter
->port
= private_port
;
117 int lttng_live_remove_port(struct lttng_live_component
*lttng_live
,
118 struct bt_private_port
*port
)
120 struct bt_component
*component
;
124 component
= bt_component_from_private_component(lttng_live
->private_component
);
125 nr_ports
= bt_component_source_get_output_port_count(component
);
131 assert(!lttng_live
->no_stream_port
);
132 lttng_live
->no_stream_port
=
133 bt_private_component_source_add_output_private_port(lttng_live
->private_component
,
134 "no-stream", lttng_live
->no_stream_iter
);
135 if (!lttng_live
->no_stream_port
) {
138 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
140 ret
= bt_private_port_remove_from_component(port
);
148 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
151 struct lttng_live_trace
*trace
;
153 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
154 if (trace
->id
== trace_id
) {
162 void lttng_live_destroy_trace(struct bt_object
*obj
)
164 struct lttng_live_trace
*trace
= container_of(obj
, struct lttng_live_trace
, obj
);
167 BT_LOGI("Destroy trace");
168 assert(bt_list_empty(&trace
->streams
));
169 bt_list_del(&trace
->node
);
171 retval
= bt_ctf_trace_set_is_static(trace
->trace
);
174 lttng_live_metadata_fini(trace
);
175 BT_PUT(trace
->cc_prio_map
);
180 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
183 struct lttng_live_trace
*trace
= NULL
;
185 trace
= g_new0(struct lttng_live_trace
, 1);
189 trace
->session
= session
;
190 trace
->id
= trace_id
;
191 BT_INIT_LIST_HEAD(&trace
->streams
);
192 trace
->new_metadata_needed
= true;
193 bt_list_add(&trace
->node
, &session
->traces
);
194 bt_object_init(&trace
->obj
, lttng_live_destroy_trace
);
195 BT_LOGI("Create trace");
205 struct lttng_live_trace
*lttng_live_ref_trace(struct lttng_live_session
*session
,
208 struct lttng_live_trace
*trace
;
210 trace
= lttng_live_find_trace(session
, trace_id
);
215 return lttng_live_create_trace(session
, trace_id
);
219 void lttng_live_unref_trace(struct lttng_live_trace
*trace
)
225 void lttng_live_close_trace_streams(struct lttng_live_trace
*trace
)
227 struct lttng_live_stream_iterator
*stream
, *s
;
229 bt_list_for_each_entry_safe(stream
, s
, &trace
->streams
, node
) {
230 lttng_live_stream_iterator_destroy(stream
);
232 lttng_live_metadata_fini(trace
);
236 int lttng_live_add_session(struct lttng_live_component
*lttng_live
, uint64_t session_id
)
239 struct lttng_live_session
*s
;
241 s
= g_new0(struct lttng_live_session
, 1);
247 BT_INIT_LIST_HEAD(&s
->traces
);
248 s
->lttng_live
= lttng_live
;
249 s
->new_streams_needed
= true;
251 BT_LOGI("Reading from session %" PRIu64
, s
->id
);
252 bt_list_add(&s
->node
, <tng_live
->sessions
);
255 BT_LOGE("Error adding session");
263 void lttng_live_destroy_session(struct lttng_live_session
*session
)
265 struct lttng_live_trace
*trace
, *t
;
267 BT_LOGI("Destroy session");
268 if (session
->id
!= -1ULL) {
269 if (lttng_live_detach_session(session
)) {
270 if (!bt_graph_is_canceled(session
->lttng_live
->graph
)) {
271 /* Old relayd cannot detach sessions. */
272 BT_LOGD("Unable to detach session %" PRIu64
,
278 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
279 lttng_live_close_trace_streams(trace
);
281 bt_list_del(&session
->node
);
286 void lttng_live_iterator_finalize(struct bt_private_notification_iterator
*it
)
288 struct lttng_live_stream_iterator_generic
*s
=
289 bt_private_notification_iterator_get_user_data(it
);
292 case LIVE_STREAM_TYPE_NO_STREAM
:
294 /* Leave no_stream_iter in place when port is removed. */
297 case LIVE_STREAM_TYPE_STREAM
:
299 struct lttng_live_stream_iterator
*stream_iter
=
300 container_of(s
, struct lttng_live_stream_iterator
, p
);
302 lttng_live_stream_iterator_destroy(stream_iter
);
309 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
310 struct lttng_live_component
*lttng_live
,
311 struct lttng_live_stream_iterator
*lttng_live_stream
)
313 switch (lttng_live_stream
->state
) {
314 case LTTNG_LIVE_STREAM_QUIESCENT
:
315 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
317 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
319 BT_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
321 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
323 BT_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
325 case LTTNG_LIVE_STREAM_EOF
:
328 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
332 * For active no data stream, fetch next data. It can be either:
333 * - quiescent: need to put it in the prio heap at quiescent end
335 * - have data: need to wire up first event into the prio heap,
336 * - have no data on this stream at this point: need to retry (AGAIN) or
340 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
341 struct lttng_live_component
*lttng_live
,
342 struct lttng_live_stream_iterator
*lttng_live_stream
)
344 enum bt_ctf_lttng_live_iterator_status ret
=
345 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
346 struct packet_index index
;
347 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
349 if (lttng_live_stream
->trace
->new_metadata_needed
) {
350 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
353 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
354 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
357 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
358 && lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
361 ret
= lttng_live_get_next_index(lttng_live
, lttng_live_stream
, &index
);
362 if (ret
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
365 assert(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
366 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
367 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
368 && lttng_live_stream
->last_returned_inactivity_timestamp
==
369 lttng_live_stream
->current_inactivity_timestamp
) {
370 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
371 print_stream_state(lttng_live_stream
);
373 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
377 lttng_live_stream
->base_offset
= index
.offset
;
378 lttng_live_stream
->offset
= index
.offset
;
379 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
381 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
382 ret
= lttng_live_iterator_next_check_stream_state(
383 lttng_live
, lttng_live_stream
);
389 * Creation of the notification requires the ctf trace to be created
390 * beforehand, but the live protocol gives us all streams (including
391 * metadata) at once. So we split it in three steps: getting streams,
392 * getting metadata (which creates the ctf trace), and then creating the
393 * per-stream notifications.
396 enum bt_ctf_lttng_live_iterator_status
lttng_live_get_session(
397 struct lttng_live_component
*lttng_live
,
398 struct lttng_live_session
*session
)
400 enum bt_ctf_lttng_live_iterator_status status
;
401 struct lttng_live_trace
*trace
, *t
;
403 if (lttng_live_attach_session(session
)) {
404 if (bt_graph_is_canceled(lttng_live
->graph
)) {
405 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
407 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
410 status
= lttng_live_get_new_streams(session
);
411 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
412 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
415 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
416 status
= lttng_live_metadata_update(trace
);
417 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
418 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
422 return lttng_live_lazy_notif_init(session
);
426 void lttng_live_need_new_streams(struct lttng_live_component
*lttng_live
)
428 struct lttng_live_session
*session
;
430 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
431 session
->new_streams_needed
= true;
436 void lttng_live_force_new_streams_and_metadata(struct lttng_live_component
*lttng_live
)
438 struct lttng_live_session
*session
;
440 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
441 struct lttng_live_trace
*trace
;
443 session
->new_streams_needed
= true;
444 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
445 trace
->new_metadata_needed
= true;
451 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_new_streams_and_metadata(
452 struct lttng_live_component
*lttng_live
)
454 enum bt_ctf_lttng_live_iterator_status ret
=
455 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
456 unsigned int nr_sessions_opened
= 0;
457 struct lttng_live_session
*session
, *s
;
459 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
460 if (session
->closed
&& bt_list_empty(&session
->traces
)) {
461 lttng_live_destroy_session(session
);
465 * Currently, when there are no sessions, we quit immediately.
466 * We may want to add a component parameter to keep trying until
467 * we get data in the future.
468 * Also, in a remotely distant future, we could add a "new
469 * session" flag to the protocol, which would tell us that we
470 * need to query for new sessions even though we have sessions
473 if (bt_list_empty(<tng_live
->sessions
)) {
474 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
477 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
478 ret
= lttng_live_get_session(lttng_live
, session
);
480 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
482 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
483 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
488 if (!session
->closed
) {
489 nr_sessions_opened
++;
493 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&& !nr_sessions_opened
) {
494 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
500 enum bt_ctf_lttng_live_iterator_status
emit_inactivity_notification(
501 struct lttng_live_component
*lttng_live
,
502 struct lttng_live_stream_iterator
*lttng_live_stream
,
503 struct bt_notification
**notification
,
506 enum bt_ctf_lttng_live_iterator_status ret
=
507 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
508 struct lttng_live_trace
*trace
;
509 struct bt_ctf_clock_class
*clock_class
= NULL
;
510 struct bt_ctf_clock_value
*clock_value
= NULL
;
511 struct bt_notification
*notif
= NULL
;
514 trace
= lttng_live_stream
->trace
;
518 clock_class
= bt_clock_class_priority_map_get_clock_class_by_index(trace
->cc_prio_map
, 0);
522 clock_value
= bt_ctf_clock_value_create(clock_class
, timestamp
);
526 notif
= bt_notification_inactivity_create(trace
->cc_prio_map
);
530 retval
= bt_notification_inactivity_set_clock_value(notif
, clock_value
);
534 *notification
= notif
;
541 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
547 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
548 struct lttng_live_component
*lttng_live
,
549 struct lttng_live_stream_iterator
*lttng_live_stream
,
550 struct bt_notification
**notification
)
552 enum bt_ctf_lttng_live_iterator_status ret
=
553 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
554 struct bt_ctf_clock_class
*clock_class
= NULL
;
555 struct bt_ctf_clock_value
*clock_value
= NULL
;
557 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
558 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
561 if (lttng_live_stream
->current_inactivity_timestamp
==
562 lttng_live_stream
->last_returned_inactivity_timestamp
) {
563 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
564 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
568 ret
= emit_inactivity_notification(lttng_live
, lttng_live_stream
, notification
,
569 (uint64_t) lttng_live_stream
->current_inactivity_timestamp
);
571 lttng_live_stream
->last_returned_inactivity_timestamp
=
572 lttng_live_stream
->current_inactivity_timestamp
;
580 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
581 struct lttng_live_component
*lttng_live
,
582 struct lttng_live_stream_iterator
*lttng_live_stream
,
583 struct bt_notification
**notification
)
585 enum bt_ctf_lttng_live_iterator_status ret
=
586 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
587 enum bt_ctf_notif_iter_status status
;
588 struct lttng_live_session
*session
;
590 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
591 struct lttng_live_trace
*trace
;
593 if (session
->new_streams_needed
) {
594 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
596 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
597 if (trace
->new_metadata_needed
) {
598 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
603 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
604 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
606 if (lttng_live_stream
->packet_end_notif_queue
) {
607 *notification
= lttng_live_stream
->packet_end_notif_queue
;
608 lttng_live_stream
->packet_end_notif_queue
= NULL
;
609 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
611 status
= bt_ctf_notif_iter_get_next_notification(
612 lttng_live_stream
->notif_iter
,
613 lttng_live_stream
->trace
->cc_prio_map
,
615 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
617 * Consider empty packets as inactivity.
619 if (bt_notification_get_type(*notification
) == BT_NOTIFICATION_TYPE_PACKET_END
) {
620 lttng_live_stream
->packet_end_notif_queue
= *notification
;
621 *notification
= NULL
;
622 return emit_inactivity_notification(lttng_live
,
623 lttng_live_stream
, notification
,
624 lttng_live_stream
->current_packet_end_timestamp
);
629 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
630 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
632 case BT_CTF_NOTIF_ITER_STATUS_OK
:
633 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
635 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
637 * Continue immediately (end of packet). The next
638 * get_index may return AGAIN to delay the following
641 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
643 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
644 /* No argument provided by the user, so don't return INVAL. */
645 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
647 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
655 * handle_no_data_streams()
657 * - for each ACTIVE_NO_DATA stream:
658 * - query relayd for stream data, or quiescence info.
659 * - if need metadata, get metadata, goto retry.
660 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
661 * - if quiescent, move to QUIESCENT streams
662 * - if fetched data, move to ACTIVE_DATA streams
663 * (at this point each stream either has data, or is quiescent)
667 * handle_new_streams_and_metadata()
668 * - query relayd for known streams, add them as ACTIVE_NO_DATA
669 * - query relayd for metadata
671 * call handle_active_no_data_streams()
673 * handle_quiescent_streams()
674 * - if at least one stream is ACTIVE_DATA:
675 * - peek stream event with lowest timestamp -> next_ts
676 * - for each quiescent stream
677 * - if next_ts >= quiescent end
678 * - set state to ACTIVE_NO_DATA
680 * - for each quiescent stream
681 * - set state to ACTIVE_NO_DATA
683 * call handle_active_no_data_streams()
685 * handle_active_data_streams()
686 * - if at least one stream is ACTIVE_DATA:
687 * - get stream event with lowest timestamp from heap
688 * - make that stream event the current notification.
689 * - move this stream heap position to its next event
690 * - if we need to fetch data from relayd, move
691 * stream to ACTIVE_NO_DATA.
695 * end criterion: ctrl-c on client. If relayd exits or the session
696 * closes on the relay daemon side, we keep on waiting for streams.
697 * Eventually handle --end timestamp (also an end criterion).
699 * When disconnected from relayd: try to re-connect endlessly.
702 struct bt_notification_iterator_next_return
lttng_live_iterator_next_stream(
703 struct bt_private_notification_iterator
*iterator
,
704 struct lttng_live_stream_iterator
*stream_iter
)
706 enum bt_ctf_lttng_live_iterator_status status
;
707 struct bt_notification_iterator_next_return next_return
;
708 struct lttng_live_component
*lttng_live
;
710 lttng_live
= stream_iter
->trace
->session
->lttng_live
;
712 print_stream_state(stream_iter
);
713 next_return
.notification
= NULL
;
714 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
715 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
718 status
= lttng_live_iterator_next_handle_one_no_data_stream(
719 lttng_live
, stream_iter
);
720 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
723 status
= lttng_live_iterator_next_handle_one_quiescent_stream(
724 lttng_live
, stream_iter
, &next_return
.notification
);
725 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
726 assert(next_return
.notification
== NULL
);
729 if (next_return
.notification
) {
732 status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live
,
733 stream_iter
, &next_return
.notification
);
734 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
735 assert(next_return
.notification
== NULL
);
740 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
741 print_dbg("continue");
743 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
744 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
747 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
748 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
751 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
752 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
755 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
756 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
758 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
759 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
761 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
762 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
764 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
765 default: /* fall-through */
766 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
773 struct bt_notification_iterator_next_return
lttng_live_iterator_next_no_stream(
774 struct bt_private_notification_iterator
*iterator
,
775 struct lttng_live_no_stream_iterator
*no_stream_iter
)
777 enum bt_ctf_lttng_live_iterator_status status
;
778 struct bt_notification_iterator_next_return next_return
;
779 struct lttng_live_component
*lttng_live
;
781 lttng_live
= no_stream_iter
->lttng_live
;
783 lttng_live_force_new_streams_and_metadata(lttng_live
);
784 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
785 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
788 if (no_stream_iter
->port
) {
789 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
791 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
795 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
797 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
798 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
800 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
801 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
803 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
804 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
806 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
807 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
809 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
810 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
812 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
813 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
815 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
816 default: /* fall-through */
817 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
824 struct bt_notification_iterator_next_return
lttng_live_iterator_next(
825 struct bt_private_notification_iterator
*iterator
)
827 struct lttng_live_stream_iterator_generic
*s
=
828 bt_private_notification_iterator_get_user_data(iterator
);
829 struct bt_notification_iterator_next_return next_return
;
832 case LIVE_STREAM_TYPE_NO_STREAM
:
833 next_return
= lttng_live_iterator_next_no_stream(iterator
,
834 container_of(s
, struct lttng_live_no_stream_iterator
, p
));
836 case LIVE_STREAM_TYPE_STREAM
:
837 next_return
= lttng_live_iterator_next_stream(iterator
,
838 container_of(s
, struct lttng_live_stream_iterator
, p
));
841 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
848 enum bt_notification_iterator_status
lttng_live_iterator_init(
849 struct bt_private_notification_iterator
*it
,
850 struct bt_private_port
*port
)
852 enum bt_notification_iterator_status ret
=
853 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
854 struct lttng_live_stream_iterator_generic
*s
;
858 s
= bt_private_port_get_user_data(port
);
861 case LIVE_STREAM_TYPE_NO_STREAM
:
863 struct lttng_live_no_stream_iterator
*no_stream_iter
=
864 container_of(s
, struct lttng_live_no_stream_iterator
, p
);
865 ret
= bt_private_notification_iterator_set_user_data(it
, no_stream_iter
);
871 case LIVE_STREAM_TYPE_STREAM
:
873 struct lttng_live_stream_iterator
*stream_iter
=
874 container_of(s
, struct lttng_live_stream_iterator
, p
);
875 ret
= bt_private_notification_iterator_set_user_data(it
, stream_iter
);
882 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
889 if (bt_private_notification_iterator_set_user_data(it
, NULL
)
890 != BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
891 BT_LOGE("Error setting private data to NULL");
897 struct bt_value
*lttng_live_query_list_sessions(struct bt_component_class
*comp_class
,
898 struct bt_value
*params
)
900 struct bt_value
*url_value
= NULL
;
901 struct bt_value
*results
= NULL
;
903 struct bt_live_viewer_connection
*viewer_connection
= NULL
;
905 url_value
= bt_value_map_get(params
, "url");
906 if (!url_value
|| bt_value_is_null(url_value
) || !bt_value_is_string(url_value
)) {
907 BT_LOGW("Mandatory \"url\" parameter missing");
911 if (bt_value_string_get(url_value
, &url
) != BT_VALUE_STATUS_OK
) {
912 BT_LOGW("\"url\" parameter is required to be a string value");
916 viewer_connection
= bt_live_viewer_connection_create(url
, NULL
);
917 if (!viewer_connection
) {
921 results
= bt_live_viewer_connection_list_sessions(viewer_connection
);
926 if (viewer_connection
) {
927 bt_live_viewer_connection_destroy(viewer_connection
);
934 struct bt_value
*lttng_live_query(struct bt_component_class
*comp_class
,
935 const char *object
, struct bt_value
*params
)
937 if (strcmp(object
, "sessions") == 0) {
938 return lttng_live_query_list_sessions(comp_class
,
941 BT_LOGW("Unknown query object `%s`", object
);
946 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
949 struct lttng_live_session
*session
, *s
;
951 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
952 lttng_live_destroy_session(session
);
954 BT_PUT(lttng_live
->viewer_connection
);
955 if (lttng_live
->url
) {
956 g_string_free(lttng_live
->url
, TRUE
);
958 if (lttng_live
->no_stream_port
) {
959 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
961 BT_PUT(lttng_live
->no_stream_port
);
963 if (lttng_live
->no_stream_iter
) {
964 g_free(lttng_live
->no_stream_iter
);
970 void lttng_live_component_finalize(struct bt_private_component
*component
)
972 void *data
= bt_private_component_get_user_data(component
);
977 lttng_live_component_destroy_data(data
);
981 struct lttng_live_component
*lttng_live_component_create(struct bt_value
*params
,
982 struct bt_private_component
*private_component
,
983 struct bt_graph
*graph
)
985 struct lttng_live_component
*lttng_live
;
986 struct bt_value
*value
= NULL
;
988 enum bt_value_status ret
;
990 lttng_live
= g_new0(struct lttng_live_component
, 1);
994 /* TODO: make this an overridable parameter. */
995 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
996 BT_INIT_LIST_HEAD(<tng_live
->sessions
);
997 value
= bt_value_map_get(params
, "url");
998 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
999 BT_LOGW("Mandatory \"url\" parameter missing");
1002 ret
= bt_value_string_get(value
, &url
);
1003 if (ret
!= BT_VALUE_STATUS_OK
) {
1004 BT_LOGW("\"url\" parameter is required to be a string value");
1007 lttng_live
->url
= g_string_new(url
);
1008 if (!lttng_live
->url
) {
1011 lttng_live
->viewer_connection
=
1012 bt_live_viewer_connection_create(lttng_live
->url
->str
, lttng_live
);
1013 if (!lttng_live
->viewer_connection
) {
1016 if (lttng_live_create_viewer_session(lttng_live
)) {
1019 lttng_live
->private_component
= private_component
;
1020 lttng_live
->graph
= graph
;
1025 lttng_live_component_destroy_data(lttng_live
);
1032 enum bt_component_status
lttng_live_component_init(
1033 struct bt_private_component
*private_component
,
1034 struct bt_value
*params
, void *init_method_data
)
1036 struct lttng_live_component
*lttng_live
;
1037 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1038 struct bt_component
*component
;
1039 struct bt_graph
*graph
;
1041 component
= bt_component_from_private_component(private_component
);
1042 graph
= bt_component_get_graph(component
);
1043 bt_put(graph
); /* weak */
1046 /* Passes ownership of iter ref to lttng_live_component_create. */
1047 lttng_live
= lttng_live_component_create(params
, private_component
,
1050 if (bt_graph_is_canceled(graph
)) {
1051 ret
= BT_COMPONENT_STATUS_AGAIN
;
1053 ret
= BT_COMPONENT_STATUS_NOMEM
;
1058 lttng_live
->no_stream_iter
= g_new0(struct lttng_live_no_stream_iterator
, 1);
1059 lttng_live
->no_stream_iter
->p
.type
= LIVE_STREAM_TYPE_NO_STREAM
;
1060 lttng_live
->no_stream_iter
->lttng_live
= lttng_live
;
1062 lttng_live
->no_stream_port
=
1063 bt_private_component_source_add_output_private_port(
1064 lttng_live
->private_component
, "no-stream",
1065 lttng_live
->no_stream_iter
);
1066 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
1068 ret
= bt_private_component_set_user_data(private_component
, lttng_live
);
1069 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1076 (void) bt_private_component_set_user_data(private_component
, NULL
);
1077 lttng_live_component_destroy_data(lttng_live
);
1082 enum bt_component_status
lttng_live_accept_port_connection(
1083 struct bt_private_component
*private_component
,
1084 struct bt_private_port
*self_private_port
,
1085 struct bt_port
*other_port
)
1087 struct lttng_live_component
*lttng_live
=
1088 bt_private_component_get_user_data(private_component
);
1089 struct bt_component
*other_component
;
1090 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
1091 struct bt_port
*self_port
= bt_port_from_private_port(self_private_port
);
1093 other_component
= bt_port_get_component(other_port
);
1094 bt_put(other_component
); /* weak */
1096 if (!lttng_live
->downstream_component
) {
1097 lttng_live
->downstream_component
= other_component
;
1102 * Compare prior component to ensure we are connected to the
1103 * same downstream component as prior ports.
1105 if (lttng_live
->downstream_component
!= other_component
) {
1106 BT_LOGW("Cannot connect ctf.lttng-live component port \"%s\" to component \"%s\": already connected to component \"%s\".",
1107 bt_port_get_name(self_port
),
1108 bt_component_get_name(other_component
),
1109 bt_component_get_name(lttng_live
->downstream_component
));
1110 status
= BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
;