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
,
237 uint64_t session_id
, const char *hostname
,
238 const char *session_name
)
241 struct lttng_live_session
*s
;
243 s
= g_new0(struct lttng_live_session
, 1);
249 BT_INIT_LIST_HEAD(&s
->traces
);
250 s
->lttng_live
= lttng_live
;
251 s
->new_streams_needed
= true;
252 s
->hostname
= g_string_new(hostname
);
253 s
->session_name
= g_string_new(session_name
);
255 BT_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
256 s
->id
, hostname
, session_name
);
257 bt_list_add(&s
->node
, <tng_live
->sessions
);
260 BT_LOGE("Error adding session");
268 void lttng_live_destroy_session(struct lttng_live_session
*session
)
270 struct lttng_live_trace
*trace
, *t
;
272 BT_LOGI("Destroy session");
273 if (session
->id
!= -1ULL) {
274 if (lttng_live_detach_session(session
)) {
275 if (!bt_graph_is_canceled(session
->lttng_live
->graph
)) {
276 /* Old relayd cannot detach sessions. */
277 BT_LOGD("Unable to detach session %" PRIu64
,
283 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
284 lttng_live_close_trace_streams(trace
);
286 bt_list_del(&session
->node
);
287 if (session
->hostname
) {
288 g_string_free(session
->hostname
, TRUE
);
290 if (session
->session_name
) {
291 g_string_free(session
->session_name
, TRUE
);
297 void lttng_live_iterator_finalize(struct bt_private_notification_iterator
*it
)
299 struct lttng_live_stream_iterator_generic
*s
=
300 bt_private_notification_iterator_get_user_data(it
);
303 case LIVE_STREAM_TYPE_NO_STREAM
:
305 /* Leave no_stream_iter in place when port is removed. */
308 case LIVE_STREAM_TYPE_STREAM
:
310 struct lttng_live_stream_iterator
*stream_iter
=
311 container_of(s
, struct lttng_live_stream_iterator
, p
);
313 lttng_live_stream_iterator_destroy(stream_iter
);
320 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
321 struct lttng_live_component
*lttng_live
,
322 struct lttng_live_stream_iterator
*lttng_live_stream
)
324 switch (lttng_live_stream
->state
) {
325 case LTTNG_LIVE_STREAM_QUIESCENT
:
326 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
328 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
330 BT_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
332 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
334 BT_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
336 case LTTNG_LIVE_STREAM_EOF
:
339 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
343 * For active no data stream, fetch next data. It can be either:
344 * - quiescent: need to put it in the prio heap at quiescent end
346 * - have data: need to wire up first event into the prio heap,
347 * - have no data on this stream at this point: need to retry (AGAIN) or
351 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
352 struct lttng_live_component
*lttng_live
,
353 struct lttng_live_stream_iterator
*lttng_live_stream
)
355 enum bt_ctf_lttng_live_iterator_status ret
=
356 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
357 struct packet_index index
;
358 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
360 if (lttng_live_stream
->trace
->new_metadata_needed
) {
361 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
364 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
365 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
368 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
369 && lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
372 ret
= lttng_live_get_next_index(lttng_live
, lttng_live_stream
, &index
);
373 if (ret
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
376 assert(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
377 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
378 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
379 && lttng_live_stream
->last_returned_inactivity_timestamp
==
380 lttng_live_stream
->current_inactivity_timestamp
) {
381 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
382 print_stream_state(lttng_live_stream
);
384 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
388 lttng_live_stream
->base_offset
= index
.offset
;
389 lttng_live_stream
->offset
= index
.offset
;
390 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
392 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
393 ret
= lttng_live_iterator_next_check_stream_state(
394 lttng_live
, lttng_live_stream
);
400 * Creation of the notification requires the ctf trace to be created
401 * beforehand, but the live protocol gives us all streams (including
402 * metadata) at once. So we split it in three steps: getting streams,
403 * getting metadata (which creates the ctf trace), and then creating the
404 * per-stream notifications.
407 enum bt_ctf_lttng_live_iterator_status
lttng_live_get_session(
408 struct lttng_live_component
*lttng_live
,
409 struct lttng_live_session
*session
)
411 enum bt_ctf_lttng_live_iterator_status status
;
412 struct lttng_live_trace
*trace
, *t
;
414 if (lttng_live_attach_session(session
)) {
415 if (bt_graph_is_canceled(lttng_live
->graph
)) {
416 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
418 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
421 status
= lttng_live_get_new_streams(session
);
422 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
423 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
426 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
427 status
= lttng_live_metadata_update(trace
);
428 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
429 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
433 return lttng_live_lazy_notif_init(session
);
437 void lttng_live_need_new_streams(struct lttng_live_component
*lttng_live
)
439 struct lttng_live_session
*session
;
441 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
442 session
->new_streams_needed
= true;
447 void lttng_live_force_new_streams_and_metadata(struct lttng_live_component
*lttng_live
)
449 struct lttng_live_session
*session
;
451 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
452 struct lttng_live_trace
*trace
;
454 session
->new_streams_needed
= true;
455 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
456 trace
->new_metadata_needed
= true;
462 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_new_streams_and_metadata(
463 struct lttng_live_component
*lttng_live
)
465 enum bt_ctf_lttng_live_iterator_status ret
=
466 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
467 unsigned int nr_sessions_opened
= 0;
468 struct lttng_live_session
*session
, *s
;
470 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
471 if (session
->closed
&& bt_list_empty(&session
->traces
)) {
472 lttng_live_destroy_session(session
);
476 * Currently, when there are no sessions, we quit immediately.
477 * We may want to add a component parameter to keep trying until
478 * we get data in the future.
479 * Also, in a remotely distant future, we could add a "new
480 * session" flag to the protocol, which would tell us that we
481 * need to query for new sessions even though we have sessions
484 if (bt_list_empty(<tng_live
->sessions
)) {
485 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
488 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
489 ret
= lttng_live_get_session(lttng_live
, session
);
491 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
493 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
494 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
499 if (!session
->closed
) {
500 nr_sessions_opened
++;
504 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&& !nr_sessions_opened
) {
505 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
511 enum bt_ctf_lttng_live_iterator_status
emit_inactivity_notification(
512 struct lttng_live_component
*lttng_live
,
513 struct lttng_live_stream_iterator
*lttng_live_stream
,
514 struct bt_notification
**notification
,
517 enum bt_ctf_lttng_live_iterator_status ret
=
518 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
519 struct lttng_live_trace
*trace
;
520 struct bt_ctf_clock_class
*clock_class
= NULL
;
521 struct bt_ctf_clock_value
*clock_value
= NULL
;
522 struct bt_notification
*notif
= NULL
;
525 trace
= lttng_live_stream
->trace
;
529 clock_class
= bt_clock_class_priority_map_get_clock_class_by_index(trace
->cc_prio_map
, 0);
533 clock_value
= bt_ctf_clock_value_create(clock_class
, timestamp
);
537 notif
= bt_notification_inactivity_create(trace
->cc_prio_map
);
541 retval
= bt_notification_inactivity_set_clock_value(notif
, clock_value
);
545 *notification
= notif
;
552 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
558 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
559 struct lttng_live_component
*lttng_live
,
560 struct lttng_live_stream_iterator
*lttng_live_stream
,
561 struct bt_notification
**notification
)
563 enum bt_ctf_lttng_live_iterator_status ret
=
564 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
565 struct bt_ctf_clock_class
*clock_class
= NULL
;
566 struct bt_ctf_clock_value
*clock_value
= NULL
;
568 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
569 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
572 if (lttng_live_stream
->current_inactivity_timestamp
==
573 lttng_live_stream
->last_returned_inactivity_timestamp
) {
574 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
575 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
579 ret
= emit_inactivity_notification(lttng_live
, lttng_live_stream
, notification
,
580 (uint64_t) lttng_live_stream
->current_inactivity_timestamp
);
582 lttng_live_stream
->last_returned_inactivity_timestamp
=
583 lttng_live_stream
->current_inactivity_timestamp
;
591 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
592 struct lttng_live_component
*lttng_live
,
593 struct lttng_live_stream_iterator
*lttng_live_stream
,
594 struct bt_notification
**notification
)
596 enum bt_ctf_lttng_live_iterator_status ret
=
597 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
598 enum bt_ctf_notif_iter_status status
;
599 struct lttng_live_session
*session
;
601 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
602 struct lttng_live_trace
*trace
;
604 if (session
->new_streams_needed
) {
605 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
607 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
608 if (trace
->new_metadata_needed
) {
609 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
614 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
615 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
617 if (lttng_live_stream
->packet_end_notif_queue
) {
618 *notification
= lttng_live_stream
->packet_end_notif_queue
;
619 lttng_live_stream
->packet_end_notif_queue
= NULL
;
620 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
622 status
= bt_ctf_notif_iter_get_next_notification(
623 lttng_live_stream
->notif_iter
,
624 lttng_live_stream
->trace
->cc_prio_map
,
626 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
628 * Consider empty packets as inactivity.
630 if (bt_notification_get_type(*notification
) == BT_NOTIFICATION_TYPE_PACKET_END
) {
631 lttng_live_stream
->packet_end_notif_queue
= *notification
;
632 *notification
= NULL
;
633 return emit_inactivity_notification(lttng_live
,
634 lttng_live_stream
, notification
,
635 lttng_live_stream
->current_packet_end_timestamp
);
640 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
641 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
643 case BT_CTF_NOTIF_ITER_STATUS_OK
:
644 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
646 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
648 * Continue immediately (end of packet). The next
649 * get_index may return AGAIN to delay the following
652 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
654 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
655 /* No argument provided by the user, so don't return INVAL. */
656 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
658 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
666 * handle_no_data_streams()
668 * - for each ACTIVE_NO_DATA stream:
669 * - query relayd for stream data, or quiescence info.
670 * - if need metadata, get metadata, goto retry.
671 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
672 * - if quiescent, move to QUIESCENT streams
673 * - if fetched data, move to ACTIVE_DATA streams
674 * (at this point each stream either has data, or is quiescent)
678 * handle_new_streams_and_metadata()
679 * - query relayd for known streams, add them as ACTIVE_NO_DATA
680 * - query relayd for metadata
682 * call handle_active_no_data_streams()
684 * handle_quiescent_streams()
685 * - if at least one stream is ACTIVE_DATA:
686 * - peek stream event with lowest timestamp -> next_ts
687 * - for each quiescent stream
688 * - if next_ts >= quiescent end
689 * - set state to ACTIVE_NO_DATA
691 * - for each quiescent stream
692 * - set state to ACTIVE_NO_DATA
694 * call handle_active_no_data_streams()
696 * handle_active_data_streams()
697 * - if at least one stream is ACTIVE_DATA:
698 * - get stream event with lowest timestamp from heap
699 * - make that stream event the current notification.
700 * - move this stream heap position to its next event
701 * - if we need to fetch data from relayd, move
702 * stream to ACTIVE_NO_DATA.
706 * end criterion: ctrl-c on client. If relayd exits or the session
707 * closes on the relay daemon side, we keep on waiting for streams.
708 * Eventually handle --end timestamp (also an end criterion).
710 * When disconnected from relayd: try to re-connect endlessly.
713 struct bt_notification_iterator_next_return
lttng_live_iterator_next_stream(
714 struct bt_private_notification_iterator
*iterator
,
715 struct lttng_live_stream_iterator
*stream_iter
)
717 enum bt_ctf_lttng_live_iterator_status status
;
718 struct bt_notification_iterator_next_return next_return
;
719 struct lttng_live_component
*lttng_live
;
721 lttng_live
= stream_iter
->trace
->session
->lttng_live
;
723 print_stream_state(stream_iter
);
724 next_return
.notification
= NULL
;
725 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
726 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
729 status
= lttng_live_iterator_next_handle_one_no_data_stream(
730 lttng_live
, stream_iter
);
731 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
734 status
= lttng_live_iterator_next_handle_one_quiescent_stream(
735 lttng_live
, stream_iter
, &next_return
.notification
);
736 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
737 assert(next_return
.notification
== NULL
);
740 if (next_return
.notification
) {
743 status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live
,
744 stream_iter
, &next_return
.notification
);
745 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
746 assert(next_return
.notification
== NULL
);
751 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
752 print_dbg("continue");
754 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
755 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
758 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
759 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
762 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
763 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
766 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
767 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
769 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
770 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
772 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
773 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
775 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
776 default: /* fall-through */
777 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
784 struct bt_notification_iterator_next_return
lttng_live_iterator_next_no_stream(
785 struct bt_private_notification_iterator
*iterator
,
786 struct lttng_live_no_stream_iterator
*no_stream_iter
)
788 enum bt_ctf_lttng_live_iterator_status status
;
789 struct bt_notification_iterator_next_return next_return
;
790 struct lttng_live_component
*lttng_live
;
792 lttng_live
= no_stream_iter
->lttng_live
;
794 lttng_live_force_new_streams_and_metadata(lttng_live
);
795 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
796 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
799 if (no_stream_iter
->port
) {
800 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
802 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
806 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
808 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
809 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
811 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
812 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
814 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
815 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
817 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
818 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
820 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
821 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
823 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
824 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
826 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
827 default: /* fall-through */
828 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
835 struct bt_notification_iterator_next_return
lttng_live_iterator_next(
836 struct bt_private_notification_iterator
*iterator
)
838 struct lttng_live_stream_iterator_generic
*s
=
839 bt_private_notification_iterator_get_user_data(iterator
);
840 struct bt_notification_iterator_next_return next_return
;
843 case LIVE_STREAM_TYPE_NO_STREAM
:
844 next_return
= lttng_live_iterator_next_no_stream(iterator
,
845 container_of(s
, struct lttng_live_no_stream_iterator
, p
));
847 case LIVE_STREAM_TYPE_STREAM
:
848 next_return
= lttng_live_iterator_next_stream(iterator
,
849 container_of(s
, struct lttng_live_stream_iterator
, p
));
852 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
859 enum bt_notification_iterator_status
lttng_live_iterator_init(
860 struct bt_private_notification_iterator
*it
,
861 struct bt_private_port
*port
)
863 enum bt_notification_iterator_status ret
=
864 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
865 struct lttng_live_stream_iterator_generic
*s
;
869 s
= bt_private_port_get_user_data(port
);
872 case LIVE_STREAM_TYPE_NO_STREAM
:
874 struct lttng_live_no_stream_iterator
*no_stream_iter
=
875 container_of(s
, struct lttng_live_no_stream_iterator
, p
);
876 ret
= bt_private_notification_iterator_set_user_data(it
, no_stream_iter
);
882 case LIVE_STREAM_TYPE_STREAM
:
884 struct lttng_live_stream_iterator
*stream_iter
=
885 container_of(s
, struct lttng_live_stream_iterator
, p
);
886 ret
= bt_private_notification_iterator_set_user_data(it
, stream_iter
);
893 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
900 if (bt_private_notification_iterator_set_user_data(it
, NULL
)
901 != BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
902 BT_LOGE("Error setting private data to NULL");
908 struct bt_value
*lttng_live_query_list_sessions(struct bt_component_class
*comp_class
,
909 struct bt_value
*params
)
911 struct bt_value
*url_value
= NULL
;
912 struct bt_value
*results
= NULL
;
914 struct bt_live_viewer_connection
*viewer_connection
= NULL
;
916 url_value
= bt_value_map_get(params
, "url");
917 if (!url_value
|| bt_value_is_null(url_value
) || !bt_value_is_string(url_value
)) {
918 BT_LOGW("Mandatory \"url\" parameter missing");
922 if (bt_value_string_get(url_value
, &url
) != BT_VALUE_STATUS_OK
) {
923 BT_LOGW("\"url\" parameter is required to be a string value");
927 viewer_connection
= bt_live_viewer_connection_create(url
, NULL
);
928 if (!viewer_connection
) {
932 results
= bt_live_viewer_connection_list_sessions(viewer_connection
);
937 if (viewer_connection
) {
938 bt_live_viewer_connection_destroy(viewer_connection
);
945 struct bt_value
*lttng_live_query(struct bt_component_class
*comp_class
,
946 const char *object
, struct bt_value
*params
)
948 if (strcmp(object
, "sessions") == 0) {
949 return lttng_live_query_list_sessions(comp_class
,
952 BT_LOGW("Unknown query object `%s`", object
);
957 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
960 struct lttng_live_session
*session
, *s
;
962 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
963 lttng_live_destroy_session(session
);
965 BT_PUT(lttng_live
->viewer_connection
);
966 if (lttng_live
->url
) {
967 g_string_free(lttng_live
->url
, TRUE
);
969 if (lttng_live
->no_stream_port
) {
970 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
972 BT_PUT(lttng_live
->no_stream_port
);
974 if (lttng_live
->no_stream_iter
) {
975 g_free(lttng_live
->no_stream_iter
);
981 void lttng_live_component_finalize(struct bt_private_component
*component
)
983 void *data
= bt_private_component_get_user_data(component
);
988 lttng_live_component_destroy_data(data
);
992 struct lttng_live_component
*lttng_live_component_create(struct bt_value
*params
,
993 struct bt_private_component
*private_component
,
994 struct bt_graph
*graph
)
996 struct lttng_live_component
*lttng_live
;
997 struct bt_value
*value
= NULL
;
999 enum bt_value_status ret
;
1001 lttng_live
= g_new0(struct lttng_live_component
, 1);
1005 /* TODO: make this an overridable parameter. */
1006 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1007 BT_INIT_LIST_HEAD(<tng_live
->sessions
);
1008 value
= bt_value_map_get(params
, "url");
1009 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
1010 BT_LOGW("Mandatory \"url\" parameter missing");
1013 ret
= bt_value_string_get(value
, &url
);
1014 if (ret
!= BT_VALUE_STATUS_OK
) {
1015 BT_LOGW("\"url\" parameter is required to be a string value");
1018 lttng_live
->url
= g_string_new(url
);
1019 if (!lttng_live
->url
) {
1022 lttng_live
->viewer_connection
=
1023 bt_live_viewer_connection_create(lttng_live
->url
->str
, lttng_live
);
1024 if (!lttng_live
->viewer_connection
) {
1027 if (lttng_live_create_viewer_session(lttng_live
)) {
1030 lttng_live
->private_component
= private_component
;
1031 lttng_live
->graph
= graph
;
1036 lttng_live_component_destroy_data(lttng_live
);
1043 enum bt_component_status
lttng_live_component_init(
1044 struct bt_private_component
*private_component
,
1045 struct bt_value
*params
, void *init_method_data
)
1047 struct lttng_live_component
*lttng_live
;
1048 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1049 struct bt_component
*component
;
1050 struct bt_graph
*graph
;
1052 component
= bt_component_from_private_component(private_component
);
1053 graph
= bt_component_get_graph(component
);
1054 bt_put(graph
); /* weak */
1057 /* Passes ownership of iter ref to lttng_live_component_create. */
1058 lttng_live
= lttng_live_component_create(params
, private_component
,
1061 if (bt_graph_is_canceled(graph
)) {
1062 ret
= BT_COMPONENT_STATUS_AGAIN
;
1064 ret
= BT_COMPONENT_STATUS_NOMEM
;
1069 lttng_live
->no_stream_iter
= g_new0(struct lttng_live_no_stream_iterator
, 1);
1070 lttng_live
->no_stream_iter
->p
.type
= LIVE_STREAM_TYPE_NO_STREAM
;
1071 lttng_live
->no_stream_iter
->lttng_live
= lttng_live
;
1073 lttng_live
->no_stream_port
=
1074 bt_private_component_source_add_output_private_port(
1075 lttng_live
->private_component
, "no-stream",
1076 lttng_live
->no_stream_iter
);
1077 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
1079 ret
= bt_private_component_set_user_data(private_component
, lttng_live
);
1080 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1087 (void) bt_private_component_set_user_data(private_component
, NULL
);
1088 lttng_live_component_destroy_data(lttng_live
);
1093 enum bt_component_status
lttng_live_accept_port_connection(
1094 struct bt_private_component
*private_component
,
1095 struct bt_private_port
*self_private_port
,
1096 struct bt_port
*other_port
)
1098 struct lttng_live_component
*lttng_live
=
1099 bt_private_component_get_user_data(private_component
);
1100 struct bt_component
*other_component
;
1101 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
1102 struct bt_port
*self_port
= bt_port_from_private_port(self_private_port
);
1104 other_component
= bt_port_get_component(other_port
);
1105 bt_put(other_component
); /* weak */
1107 if (!lttng_live
->downstream_component
) {
1108 lttng_live
->downstream_component
= other_component
;
1113 * Compare prior component to ensure we are connected to the
1114 * same downstream component as prior ports.
1116 if (lttng_live
->downstream_component
!= other_component
) {
1117 BT_LOGW("Cannot connect ctf.lttng-live component port \"%s\" to component \"%s\": already connected to component \"%s\".",
1118 bt_port_get_name(self_port
),
1119 bt_component_get_name(other_component
),
1120 bt_component_get_name(lttng_live
->downstream_component
));
1121 status
= BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
;