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>
48 #include <babeltrace/types.h>
53 #include <plugins-common.h>
55 #include "data-stream.h"
57 #include "lttng-live-internal.h"
59 #define MAX_QUERY_SIZE (256*1024)
61 #define print_dbg(fmt, ...) BT_LOGD(fmt, ## __VA_ARGS__)
63 static const char *print_state(struct lttng_live_stream_iterator
*s
)
66 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
67 return "ACTIVE_NO_DATA";
68 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
69 return "QUIESCENT_NO_DATA";
70 case LTTNG_LIVE_STREAM_QUIESCENT
:
72 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
74 case LTTNG_LIVE_STREAM_EOF
:
82 void print_stream_state(struct lttng_live_stream_iterator
*stream
)
86 port
= bt_port_from_private_port(stream
->port
);
87 print_dbg("stream %s state %s last_inact_ts %" PRId64
" cur_inact_ts %" PRId64
,
88 bt_port_get_name(port
),
90 stream
->last_returned_inactivity_timestamp
,
91 stream
->current_inactivity_timestamp
);
96 bt_bool
lttng_live_is_canceled(struct lttng_live_component
*lttng_live
)
98 struct bt_component
*component
;
99 struct bt_graph
*graph
;
106 component
= bt_component_from_private_component(lttng_live
->private_component
);
107 graph
= bt_component_get_graph(component
);
108 ret
= bt_graph_is_canceled(graph
);
115 int lttng_live_add_port(struct lttng_live_component
*lttng_live
,
116 struct lttng_live_stream_iterator
*stream_iter
)
119 struct bt_private_port
*private_port
;
120 char name
[STREAM_NAME_MAX_LEN
];
121 enum bt_component_status status
;
123 ret
= sprintf(name
, STREAM_NAME_PREFIX
"%" PRIu64
, stream_iter
->viewer_stream_id
);
125 strcpy(stream_iter
->name
, name
);
126 status
= bt_private_component_source_add_output_private_port(
127 lttng_live
->private_component
, name
, stream_iter
,
130 case BT_COMPONENT_STATUS_GRAPH_IS_CANCELED
:
132 case BT_COMPONENT_STATUS_OK
:
137 bt_put(private_port
); /* weak */
138 BT_LOGI("Added port %s", name
);
140 if (lttng_live
->no_stream_port
) {
141 bt_get(lttng_live
->no_stream_port
);
142 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
143 bt_put(lttng_live
->no_stream_port
);
147 lttng_live
->no_stream_port
= NULL
;
148 lttng_live
->no_stream_iter
->port
= NULL
;
150 stream_iter
->port
= private_port
;
155 int lttng_live_remove_port(struct lttng_live_component
*lttng_live
,
156 struct bt_private_port
*port
)
158 struct bt_component
*component
;
162 component
= bt_component_from_private_component(lttng_live
->private_component
);
163 nr_ports
= bt_component_source_get_output_port_count(component
);
169 enum bt_component_status status
;
171 assert(!lttng_live
->no_stream_port
);
172 status
= bt_private_component_source_add_output_private_port(lttng_live
->private_component
,
173 "no-stream", lttng_live
->no_stream_iter
,
174 <tng_live
->no_stream_port
);
176 case BT_COMPONENT_STATUS_GRAPH_IS_CANCELED
:
178 case BT_COMPONENT_STATUS_OK
:
183 bt_put(lttng_live
->no_stream_port
); /* weak */
184 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
187 ret
= bt_private_port_remove_from_component(port
);
196 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
199 struct lttng_live_trace
*trace
;
201 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
202 if (trace
->id
== trace_id
) {
210 void lttng_live_destroy_trace(struct bt_object
*obj
)
212 struct lttng_live_trace
*trace
= container_of(obj
, struct lttng_live_trace
, obj
);
215 BT_LOGI("Destroy trace");
216 assert(bt_list_empty(&trace
->streams
));
217 bt_list_del(&trace
->node
);
219 retval
= bt_ctf_trace_set_is_static(trace
->trace
);
222 BT_PUT(trace
->trace
);
223 lttng_live_metadata_fini(trace
);
224 BT_PUT(trace
->cc_prio_map
);
229 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
232 struct lttng_live_trace
*trace
= NULL
;
234 trace
= g_new0(struct lttng_live_trace
, 1);
238 trace
->session
= session
;
239 trace
->id
= trace_id
;
240 BT_INIT_LIST_HEAD(&trace
->streams
);
241 trace
->new_metadata_needed
= true;
242 bt_list_add(&trace
->node
, &session
->traces
);
243 bt_object_init(&trace
->obj
, lttng_live_destroy_trace
);
244 BT_LOGI("Create trace");
254 struct lttng_live_trace
*lttng_live_ref_trace(struct lttng_live_session
*session
,
257 struct lttng_live_trace
*trace
;
259 trace
= lttng_live_find_trace(session
, trace_id
);
264 return lttng_live_create_trace(session
, trace_id
);
268 void lttng_live_unref_trace(struct lttng_live_trace
*trace
)
274 void lttng_live_close_trace_streams(struct lttng_live_trace
*trace
)
276 struct lttng_live_stream_iterator
*stream
, *s
;
278 bt_list_for_each_entry_safe(stream
, s
, &trace
->streams
, node
) {
279 lttng_live_stream_iterator_destroy(stream
);
281 lttng_live_metadata_fini(trace
);
285 int lttng_live_add_session(struct lttng_live_component
*lttng_live
,
286 uint64_t session_id
, const char *hostname
,
287 const char *session_name
)
290 struct lttng_live_session
*s
;
292 s
= g_new0(struct lttng_live_session
, 1);
298 BT_INIT_LIST_HEAD(&s
->traces
);
299 s
->lttng_live
= lttng_live
;
300 s
->new_streams_needed
= true;
301 s
->hostname
= g_string_new(hostname
);
302 s
->session_name
= g_string_new(session_name
);
304 BT_LOGI("Reading from session: %" PRIu64
" hostname: %s session_name: %s",
305 s
->id
, hostname
, session_name
);
306 bt_list_add(&s
->node
, <tng_live
->sessions
);
309 BT_LOGE("Error adding session");
317 void lttng_live_destroy_session(struct lttng_live_session
*session
)
319 struct lttng_live_trace
*trace
, *t
;
321 BT_LOGI("Destroy session");
322 if (session
->id
!= -1ULL) {
323 if (lttng_live_detach_session(session
)) {
324 if (!lttng_live_is_canceled(session
->lttng_live
)) {
325 /* Old relayd cannot detach sessions. */
326 BT_LOGD("Unable to detach session %" PRIu64
,
332 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
333 lttng_live_close_trace_streams(trace
);
335 bt_list_del(&session
->node
);
336 if (session
->hostname
) {
337 g_string_free(session
->hostname
, TRUE
);
339 if (session
->session_name
) {
340 g_string_free(session
->session_name
, TRUE
);
346 void lttng_live_iterator_finalize(struct bt_private_notification_iterator
*it
)
348 struct lttng_live_stream_iterator_generic
*s
=
349 bt_private_notification_iterator_get_user_data(it
);
352 case LIVE_STREAM_TYPE_NO_STREAM
:
354 /* Leave no_stream_iter in place when port is removed. */
357 case LIVE_STREAM_TYPE_STREAM
:
359 struct lttng_live_stream_iterator
*stream_iter
=
360 container_of(s
, struct lttng_live_stream_iterator
, p
);
362 lttng_live_stream_iterator_destroy(stream_iter
);
369 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
370 struct lttng_live_component
*lttng_live
,
371 struct lttng_live_stream_iterator
*lttng_live_stream
)
373 switch (lttng_live_stream
->state
) {
374 case LTTNG_LIVE_STREAM_QUIESCENT
:
375 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
377 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
379 BT_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
381 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
383 BT_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
385 case LTTNG_LIVE_STREAM_EOF
:
388 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
392 * For active no data stream, fetch next data. It can be either:
393 * - quiescent: need to put it in the prio heap at quiescent end
395 * - have data: need to wire up first event into the prio heap,
396 * - have no data on this stream at this point: need to retry (AGAIN) or
400 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
401 struct lttng_live_component
*lttng_live
,
402 struct lttng_live_stream_iterator
*lttng_live_stream
)
404 enum bt_ctf_lttng_live_iterator_status ret
=
405 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
406 struct packet_index index
;
407 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
409 if (lttng_live_stream
->trace
->new_metadata_needed
) {
410 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
413 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
414 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
417 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
418 && lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
421 ret
= lttng_live_get_next_index(lttng_live
, lttng_live_stream
, &index
);
422 if (ret
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
425 assert(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
426 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
427 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
428 && lttng_live_stream
->last_returned_inactivity_timestamp
==
429 lttng_live_stream
->current_inactivity_timestamp
) {
430 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
431 print_stream_state(lttng_live_stream
);
433 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
437 lttng_live_stream
->base_offset
= index
.offset
;
438 lttng_live_stream
->offset
= index
.offset
;
439 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
441 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
442 ret
= lttng_live_iterator_next_check_stream_state(
443 lttng_live
, lttng_live_stream
);
449 * Creation of the notification requires the ctf trace to be created
450 * beforehand, but the live protocol gives us all streams (including
451 * metadata) at once. So we split it in three steps: getting streams,
452 * getting metadata (which creates the ctf trace), and then creating the
453 * per-stream notifications.
456 enum bt_ctf_lttng_live_iterator_status
lttng_live_get_session(
457 struct lttng_live_component
*lttng_live
,
458 struct lttng_live_session
*session
)
460 enum bt_ctf_lttng_live_iterator_status status
;
461 struct lttng_live_trace
*trace
, *t
;
463 if (lttng_live_attach_session(session
)) {
464 if (lttng_live_is_canceled(lttng_live
)) {
465 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
467 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
470 status
= lttng_live_get_new_streams(session
);
471 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
472 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
475 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
476 status
= lttng_live_metadata_update(trace
);
477 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
478 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
482 return lttng_live_lazy_notif_init(session
);
486 void lttng_live_need_new_streams(struct lttng_live_component
*lttng_live
)
488 struct lttng_live_session
*session
;
490 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
491 session
->new_streams_needed
= true;
496 void lttng_live_force_new_streams_and_metadata(struct lttng_live_component
*lttng_live
)
498 struct lttng_live_session
*session
;
500 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
501 struct lttng_live_trace
*trace
;
503 session
->new_streams_needed
= true;
504 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
505 trace
->new_metadata_needed
= true;
511 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_new_streams_and_metadata(
512 struct lttng_live_component
*lttng_live
)
514 enum bt_ctf_lttng_live_iterator_status ret
=
515 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
516 unsigned int nr_sessions_opened
= 0;
517 struct lttng_live_session
*session
, *s
;
519 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
520 if (session
->closed
&& bt_list_empty(&session
->traces
)) {
521 lttng_live_destroy_session(session
);
525 * Currently, when there are no sessions, we quit immediately.
526 * We may want to add a component parameter to keep trying until
527 * we get data in the future.
528 * Also, in a remotely distant future, we could add a "new
529 * session" flag to the protocol, which would tell us that we
530 * need to query for new sessions even though we have sessions
533 if (bt_list_empty(<tng_live
->sessions
)) {
534 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
537 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
538 ret
= lttng_live_get_session(lttng_live
, session
);
540 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
542 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
543 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
548 if (!session
->closed
) {
549 nr_sessions_opened
++;
553 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&& !nr_sessions_opened
) {
554 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
560 enum bt_ctf_lttng_live_iterator_status
emit_inactivity_notification(
561 struct lttng_live_component
*lttng_live
,
562 struct lttng_live_stream_iterator
*lttng_live_stream
,
563 struct bt_notification
**notification
,
566 enum bt_ctf_lttng_live_iterator_status ret
=
567 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
568 struct lttng_live_trace
*trace
;
569 struct bt_ctf_clock_class
*clock_class
= NULL
;
570 struct bt_ctf_clock_value
*clock_value
= NULL
;
571 struct bt_notification
*notif
= NULL
;
574 trace
= lttng_live_stream
->trace
;
578 clock_class
= bt_clock_class_priority_map_get_clock_class_by_index(trace
->cc_prio_map
, 0);
582 clock_value
= bt_ctf_clock_value_create(clock_class
, timestamp
);
586 notif
= bt_notification_inactivity_create(trace
->cc_prio_map
);
590 retval
= bt_notification_inactivity_set_clock_value(notif
, clock_value
);
594 *notification
= notif
;
601 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
607 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
608 struct lttng_live_component
*lttng_live
,
609 struct lttng_live_stream_iterator
*lttng_live_stream
,
610 struct bt_notification
**notification
)
612 enum bt_ctf_lttng_live_iterator_status ret
=
613 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
614 struct bt_ctf_clock_class
*clock_class
= NULL
;
615 struct bt_ctf_clock_value
*clock_value
= NULL
;
617 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
618 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
621 if (lttng_live_stream
->current_inactivity_timestamp
==
622 lttng_live_stream
->last_returned_inactivity_timestamp
) {
623 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
624 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
628 ret
= emit_inactivity_notification(lttng_live
, lttng_live_stream
, notification
,
629 (uint64_t) lttng_live_stream
->current_inactivity_timestamp
);
631 lttng_live_stream
->last_returned_inactivity_timestamp
=
632 lttng_live_stream
->current_inactivity_timestamp
;
640 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
641 struct lttng_live_component
*lttng_live
,
642 struct lttng_live_stream_iterator
*lttng_live_stream
,
643 struct bt_notification
**notification
)
645 enum bt_ctf_lttng_live_iterator_status ret
=
646 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
647 enum bt_ctf_notif_iter_status status
;
648 struct lttng_live_session
*session
;
650 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
651 struct lttng_live_trace
*trace
;
653 if (session
->new_streams_needed
) {
654 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
656 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
657 if (trace
->new_metadata_needed
) {
658 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
663 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
664 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
666 if (lttng_live_stream
->packet_end_notif_queue
) {
667 *notification
= lttng_live_stream
->packet_end_notif_queue
;
668 lttng_live_stream
->packet_end_notif_queue
= NULL
;
669 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
671 status
= bt_ctf_notif_iter_get_next_notification(
672 lttng_live_stream
->notif_iter
,
673 lttng_live_stream
->trace
->cc_prio_map
,
675 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
677 * Consider empty packets as inactivity.
679 if (bt_notification_get_type(*notification
) == BT_NOTIFICATION_TYPE_PACKET_END
) {
680 lttng_live_stream
->packet_end_notif_queue
= *notification
;
681 *notification
= NULL
;
682 return emit_inactivity_notification(lttng_live
,
683 lttng_live_stream
, notification
,
684 lttng_live_stream
->current_packet_end_timestamp
);
689 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
690 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
692 case BT_CTF_NOTIF_ITER_STATUS_OK
:
693 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
695 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
697 * Continue immediately (end of packet). The next
698 * get_index may return AGAIN to delay the following
701 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
703 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
704 /* No argument provided by the user, so don't return INVAL. */
705 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
707 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
715 * handle_no_data_streams()
717 * - for each ACTIVE_NO_DATA stream:
718 * - query relayd for stream data, or quiescence info.
719 * - if need metadata, get metadata, goto retry.
720 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
721 * - if quiescent, move to QUIESCENT streams
722 * - if fetched data, move to ACTIVE_DATA streams
723 * (at this point each stream either has data, or is quiescent)
727 * handle_new_streams_and_metadata()
728 * - query relayd for known streams, add them as ACTIVE_NO_DATA
729 * - query relayd for metadata
731 * call handle_active_no_data_streams()
733 * handle_quiescent_streams()
734 * - if at least one stream is ACTIVE_DATA:
735 * - peek stream event with lowest timestamp -> next_ts
736 * - for each quiescent stream
737 * - if next_ts >= quiescent end
738 * - set state to ACTIVE_NO_DATA
740 * - for each quiescent stream
741 * - set state to ACTIVE_NO_DATA
743 * call handle_active_no_data_streams()
745 * handle_active_data_streams()
746 * - if at least one stream is ACTIVE_DATA:
747 * - get stream event with lowest timestamp from heap
748 * - make that stream event the current notification.
749 * - move this stream heap position to its next event
750 * - if we need to fetch data from relayd, move
751 * stream to ACTIVE_NO_DATA.
755 * end criterion: ctrl-c on client. If relayd exits or the session
756 * closes on the relay daemon side, we keep on waiting for streams.
757 * Eventually handle --end timestamp (also an end criterion).
759 * When disconnected from relayd: try to re-connect endlessly.
762 struct bt_notification_iterator_next_return
lttng_live_iterator_next_stream(
763 struct bt_private_notification_iterator
*iterator
,
764 struct lttng_live_stream_iterator
*stream_iter
)
766 enum bt_ctf_lttng_live_iterator_status status
;
767 struct bt_notification_iterator_next_return next_return
;
768 struct lttng_live_component
*lttng_live
;
770 lttng_live
= stream_iter
->trace
->session
->lttng_live
;
772 print_stream_state(stream_iter
);
773 next_return
.notification
= NULL
;
774 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
775 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
778 status
= lttng_live_iterator_next_handle_one_no_data_stream(
779 lttng_live
, stream_iter
);
780 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
783 status
= lttng_live_iterator_next_handle_one_quiescent_stream(
784 lttng_live
, stream_iter
, &next_return
.notification
);
785 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
786 assert(next_return
.notification
== NULL
);
789 if (next_return
.notification
) {
792 status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live
,
793 stream_iter
, &next_return
.notification
);
794 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
795 assert(next_return
.notification
== NULL
);
800 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
801 print_dbg("continue");
803 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
804 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
807 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
808 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
811 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
812 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
815 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
816 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
818 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
819 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
821 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
822 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
824 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
825 default: /* fall-through */
826 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
833 struct bt_notification_iterator_next_return
lttng_live_iterator_next_no_stream(
834 struct bt_private_notification_iterator
*iterator
,
835 struct lttng_live_no_stream_iterator
*no_stream_iter
)
837 enum bt_ctf_lttng_live_iterator_status status
;
838 struct bt_notification_iterator_next_return next_return
;
839 struct lttng_live_component
*lttng_live
;
841 lttng_live
= no_stream_iter
->lttng_live
;
843 lttng_live_force_new_streams_and_metadata(lttng_live
);
844 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
845 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
848 if (no_stream_iter
->port
) {
849 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
851 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
855 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
857 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
858 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
860 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
861 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
863 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
864 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
866 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
867 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
869 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
870 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
872 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
873 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
875 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
876 default: /* fall-through */
877 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
884 struct bt_notification_iterator_next_return
lttng_live_iterator_next(
885 struct bt_private_notification_iterator
*iterator
)
887 struct lttng_live_stream_iterator_generic
*s
=
888 bt_private_notification_iterator_get_user_data(iterator
);
889 struct bt_notification_iterator_next_return next_return
;
892 case LIVE_STREAM_TYPE_NO_STREAM
:
893 next_return
= lttng_live_iterator_next_no_stream(iterator
,
894 container_of(s
, struct lttng_live_no_stream_iterator
, p
));
896 case LIVE_STREAM_TYPE_STREAM
:
897 next_return
= lttng_live_iterator_next_stream(iterator
,
898 container_of(s
, struct lttng_live_stream_iterator
, p
));
901 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
908 enum bt_notification_iterator_status
lttng_live_iterator_init(
909 struct bt_private_notification_iterator
*it
,
910 struct bt_private_port
*port
)
912 enum bt_notification_iterator_status ret
=
913 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
914 struct lttng_live_stream_iterator_generic
*s
;
918 s
= bt_private_port_get_user_data(port
);
921 case LIVE_STREAM_TYPE_NO_STREAM
:
923 struct lttng_live_no_stream_iterator
*no_stream_iter
=
924 container_of(s
, struct lttng_live_no_stream_iterator
, p
);
925 ret
= bt_private_notification_iterator_set_user_data(it
, no_stream_iter
);
931 case LIVE_STREAM_TYPE_STREAM
:
933 struct lttng_live_stream_iterator
*stream_iter
=
934 container_of(s
, struct lttng_live_stream_iterator
, p
);
935 ret
= bt_private_notification_iterator_set_user_data(it
, stream_iter
);
942 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
949 if (bt_private_notification_iterator_set_user_data(it
, NULL
)
950 != BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
951 BT_LOGE("Error setting private data to NULL");
957 struct bt_value
*lttng_live_query_list_sessions(struct bt_component_class
*comp_class
,
958 struct bt_value
*params
)
960 struct bt_value
*url_value
= NULL
;
961 struct bt_value
*results
= NULL
;
963 struct bt_live_viewer_connection
*viewer_connection
= NULL
;
965 url_value
= bt_value_map_get(params
, "url");
966 if (!url_value
|| bt_value_is_null(url_value
) || !bt_value_is_string(url_value
)) {
967 BT_LOGW("Mandatory \"url\" parameter missing");
971 if (bt_value_string_get(url_value
, &url
) != BT_VALUE_STATUS_OK
) {
972 BT_LOGW("\"url\" parameter is required to be a string value");
976 viewer_connection
= bt_live_viewer_connection_create(url
, NULL
);
977 if (!viewer_connection
) {
981 results
= bt_live_viewer_connection_list_sessions(viewer_connection
);
986 if (viewer_connection
) {
987 bt_live_viewer_connection_destroy(viewer_connection
);
994 struct bt_value
*lttng_live_query(struct bt_component_class
*comp_class
,
995 const char *object
, struct bt_value
*params
)
997 if (strcmp(object
, "sessions") == 0) {
998 return lttng_live_query_list_sessions(comp_class
,
1001 BT_LOGW("Unknown query object `%s`", object
);
1006 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
1009 struct lttng_live_session
*session
, *s
;
1011 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
1012 lttng_live_destroy_session(session
);
1014 BT_PUT(lttng_live
->viewer_connection
);
1015 if (lttng_live
->url
) {
1016 g_string_free(lttng_live
->url
, TRUE
);
1018 if (lttng_live
->no_stream_port
) {
1019 bt_get(lttng_live
->no_stream_port
);
1020 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
1021 bt_put(lttng_live
->no_stream_port
);
1024 if (lttng_live
->no_stream_iter
) {
1025 g_free(lttng_live
->no_stream_iter
);
1031 void lttng_live_component_finalize(struct bt_private_component
*component
)
1033 void *data
= bt_private_component_get_user_data(component
);
1038 lttng_live_component_destroy_data(data
);
1042 struct lttng_live_component
*lttng_live_component_create(struct bt_value
*params
,
1043 struct bt_private_component
*private_component
)
1045 struct lttng_live_component
*lttng_live
;
1046 struct bt_value
*value
= NULL
;
1048 enum bt_value_status ret
;
1050 lttng_live
= g_new0(struct lttng_live_component
, 1);
1054 /* TODO: make this an overridable parameter. */
1055 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
1056 BT_INIT_LIST_HEAD(<tng_live
->sessions
);
1057 value
= bt_value_map_get(params
, "url");
1058 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
1059 BT_LOGW("Mandatory \"url\" parameter missing");
1062 ret
= bt_value_string_get(value
, &url
);
1063 if (ret
!= BT_VALUE_STATUS_OK
) {
1064 BT_LOGW("\"url\" parameter is required to be a string value");
1067 lttng_live
->url
= g_string_new(url
);
1068 if (!lttng_live
->url
) {
1072 lttng_live
->viewer_connection
=
1073 bt_live_viewer_connection_create(lttng_live
->url
->str
, lttng_live
);
1074 if (!lttng_live
->viewer_connection
) {
1077 if (lttng_live_create_viewer_session(lttng_live
)) {
1080 lttng_live
->private_component
= private_component
;
1085 lttng_live_component_destroy_data(lttng_live
);
1092 enum bt_component_status
lttng_live_component_init(
1093 struct bt_private_component
*private_component
,
1094 struct bt_value
*params
, void *init_method_data
)
1096 struct lttng_live_component
*lttng_live
;
1097 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1099 /* Passes ownership of iter ref to lttng_live_component_create. */
1100 lttng_live
= lttng_live_component_create(params
, private_component
);
1102 //TODO : we need access to the application cancel state
1103 //because we are not part of a graph yet.
1104 ret
= BT_COMPONENT_STATUS_NOMEM
;
1108 lttng_live
->no_stream_iter
= g_new0(struct lttng_live_no_stream_iterator
, 1);
1109 lttng_live
->no_stream_iter
->p
.type
= LIVE_STREAM_TYPE_NO_STREAM
;
1110 lttng_live
->no_stream_iter
->lttng_live
= lttng_live
;
1111 ret
= bt_private_component_source_add_output_private_port(
1112 lttng_live
->private_component
, "no-stream",
1113 lttng_live
->no_stream_iter
,
1114 <tng_live
->no_stream_port
);
1115 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1118 bt_put(lttng_live
->no_stream_port
); /* weak */
1119 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
1121 ret
= bt_private_component_set_user_data(private_component
, lttng_live
);
1122 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1129 (void) bt_private_component_set_user_data(private_component
, NULL
);
1130 lttng_live_component_destroy_data(lttng_live
);
1135 enum bt_component_status
lttng_live_accept_port_connection(
1136 struct bt_private_component
*private_component
,
1137 struct bt_private_port
*self_private_port
,
1138 struct bt_port
*other_port
)
1140 struct lttng_live_component
*lttng_live
=
1141 bt_private_component_get_user_data(private_component
);
1142 struct bt_component
*other_component
;
1143 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
1144 struct bt_port
*self_port
= bt_port_from_private_port(self_private_port
);
1146 other_component
= bt_port_get_component(other_port
);
1147 bt_put(other_component
); /* weak */
1149 if (!lttng_live
->downstream_component
) {
1150 lttng_live
->downstream_component
= other_component
;
1155 * Compare prior component to ensure we are connected to the
1156 * same downstream component as prior ports.
1158 if (lttng_live
->downstream_component
!= other_component
) {
1159 BT_LOGW("Cannot connect ctf.lttng-live component port \"%s\" to component \"%s\": already connected to component \"%s\".",
1160 bt_port_get_name(self_port
),
1161 bt_component_get_name(other_component
),
1162 bt_component_get_name(lttng_live
->downstream_component
));
1163 status
= BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
;