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 #include <babeltrace/ctf-ir/packet.h>
31 #include <babeltrace/graph/component-source.h>
32 #include <babeltrace/graph/private-port.h>
33 #include <babeltrace/graph/port.h>
34 #include <babeltrace/graph/private-component.h>
35 #include <babeltrace/graph/private-component-source.h>
36 #include <babeltrace/graph/private-notification-iterator.h>
37 #include <babeltrace/graph/notification-stream.h>
38 #include <babeltrace/graph/notification-packet.h>
39 #include <babeltrace/graph/notification-event.h>
40 #include <babeltrace/graph/notification-heap.h>
41 #include <babeltrace/graph/notification-iterator.h>
42 #include <babeltrace/graph/notification-inactivity.h>
43 #include <babeltrace/graph/graph.h>
44 #include <babeltrace/compiler-internal.h>
49 #include <plugins-common.h>
51 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE"
52 #define BT_LOGLEVEL_NAME "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_LOG_LEVEL"
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 bt_lttng_live_log_level
= BT_LOG_NONE
;
90 int lttng_live_add_port(struct lttng_live_component
*lttng_live
,
91 struct lttng_live_stream_iterator
*stream_iter
)
94 struct bt_private_port
*private_port
;
95 char name
[STREAM_NAME_MAX_LEN
];
97 ret
= sprintf(name
, STREAM_NAME_PREFIX
"%" PRIu64
, stream_iter
->viewer_stream_id
);
99 strcpy(stream_iter
->name
, name
);
100 private_port
= bt_private_component_source_add_output_private_port(
101 lttng_live
->private_component
, name
, stream_iter
);
105 BT_LOGI("Added port %s", name
);
107 if (lttng_live
->no_stream_port
) {
108 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
112 BT_PUT(lttng_live
->no_stream_port
);
113 lttng_live
->no_stream_iter
->port
= NULL
;
115 stream_iter
->port
= private_port
;
120 int lttng_live_remove_port(struct lttng_live_component
*lttng_live
,
121 struct bt_private_port
*port
)
123 struct bt_component
*component
;
127 component
= bt_component_from_private_component(lttng_live
->private_component
);
128 nr_ports
= bt_component_source_get_output_port_count(component
);
134 assert(!lttng_live
->no_stream_port
);
135 lttng_live
->no_stream_port
=
136 bt_private_component_source_add_output_private_port(lttng_live
->private_component
,
137 "no-stream", lttng_live
->no_stream_iter
);
138 if (!lttng_live
->no_stream_port
) {
141 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
143 ret
= bt_private_port_remove_from_component(port
);
151 struct lttng_live_trace
*lttng_live_find_trace(struct lttng_live_session
*session
,
154 struct lttng_live_trace
*trace
;
156 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
157 if (trace
->id
== trace_id
) {
165 void lttng_live_destroy_trace(struct bt_object
*obj
)
167 struct lttng_live_trace
*trace
= container_of(obj
, struct lttng_live_trace
, obj
);
170 BT_LOGI("Destroy trace");
171 assert(bt_list_empty(&trace
->streams
));
172 bt_list_del(&trace
->node
);
174 retval
= bt_ctf_trace_set_is_static(trace
->trace
);
177 lttng_live_metadata_fini(trace
);
178 BT_PUT(trace
->cc_prio_map
);
183 struct lttng_live_trace
*lttng_live_create_trace(struct lttng_live_session
*session
,
186 struct lttng_live_trace
*trace
= NULL
;
188 trace
= g_new0(struct lttng_live_trace
, 1);
192 trace
->session
= session
;
193 trace
->id
= trace_id
;
194 BT_INIT_LIST_HEAD(&trace
->streams
);
195 trace
->new_metadata_needed
= true;
196 bt_list_add(&trace
->node
, &session
->traces
);
197 bt_object_init(&trace
->obj
, lttng_live_destroy_trace
);
198 BT_LOGI("Create trace");
208 struct lttng_live_trace
*lttng_live_ref_trace(struct lttng_live_session
*session
,
211 struct lttng_live_trace
*trace
;
213 trace
= lttng_live_find_trace(session
, trace_id
);
218 return lttng_live_create_trace(session
, trace_id
);
222 void lttng_live_unref_trace(struct lttng_live_trace
*trace
)
228 void lttng_live_close_trace_streams(struct lttng_live_trace
*trace
)
230 struct lttng_live_stream_iterator
*stream
, *s
;
232 bt_list_for_each_entry_safe(stream
, s
, &trace
->streams
, node
) {
233 lttng_live_stream_iterator_destroy(stream
);
235 lttng_live_metadata_fini(trace
);
239 int lttng_live_add_session(struct lttng_live_component
*lttng_live
, uint64_t session_id
)
242 struct lttng_live_session
*s
;
244 s
= g_new0(struct lttng_live_session
, 1);
250 BT_INIT_LIST_HEAD(&s
->traces
);
251 s
->lttng_live
= lttng_live
;
252 s
->new_streams_needed
= true;
254 BT_LOGI("Reading from session %" PRIu64
, s
->id
);
255 bt_list_add(&s
->node
, <tng_live
->sessions
);
258 BT_LOGE("Error adding session");
266 void lttng_live_destroy_session(struct lttng_live_session
*session
)
268 struct lttng_live_trace
*trace
, *t
;
270 BT_LOGI("Destroy session");
271 if (session
->id
!= -1ULL) {
272 if (lttng_live_detach_session(session
)) {
273 if (!bt_graph_is_canceled(session
->lttng_live
->graph
)) {
274 /* Old relayd cannot detach sessions. */
275 BT_LOGD("Unable to detach session %" PRIu64
,
281 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
282 lttng_live_close_trace_streams(trace
);
284 bt_list_del(&session
->node
);
289 void lttng_live_iterator_finalize(struct bt_private_notification_iterator
*it
)
291 struct lttng_live_stream_iterator_generic
*s
=
292 bt_private_notification_iterator_get_user_data(it
);
295 case LIVE_STREAM_TYPE_NO_STREAM
:
297 /* Leave no_stream_iter in place when port is removed. */
300 case LIVE_STREAM_TYPE_STREAM
:
302 struct lttng_live_stream_iterator
*stream_iter
=
303 container_of(s
, struct lttng_live_stream_iterator
, p
);
305 lttng_live_stream_iterator_destroy(stream_iter
);
312 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_check_stream_state(
313 struct lttng_live_component
*lttng_live
,
314 struct lttng_live_stream_iterator
*lttng_live_stream
)
316 switch (lttng_live_stream
->state
) {
317 case LTTNG_LIVE_STREAM_QUIESCENT
:
318 case LTTNG_LIVE_STREAM_ACTIVE_DATA
:
320 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
:
322 BT_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
324 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
:
326 BT_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
328 case LTTNG_LIVE_STREAM_EOF
:
331 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
335 * For active no data stream, fetch next data. It can be either:
336 * - quiescent: need to put it in the prio heap at quiescent end
338 * - have data: need to wire up first event into the prio heap,
339 * - have no data on this stream at this point: need to retry (AGAIN) or
343 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_no_data_stream(
344 struct lttng_live_component
*lttng_live
,
345 struct lttng_live_stream_iterator
*lttng_live_stream
)
347 enum bt_ctf_lttng_live_iterator_status ret
=
348 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
349 struct packet_index index
;
350 enum lttng_live_stream_state orig_state
= lttng_live_stream
->state
;
352 if (lttng_live_stream
->trace
->new_metadata_needed
) {
353 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
356 if (lttng_live_stream
->trace
->session
->new_streams_needed
) {
357 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
360 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
361 && lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
) {
364 ret
= lttng_live_get_next_index(lttng_live
, lttng_live_stream
, &index
);
365 if (ret
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
368 assert(lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_EOF
);
369 if (lttng_live_stream
->state
== LTTNG_LIVE_STREAM_QUIESCENT
) {
370 if (orig_state
== LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
371 && lttng_live_stream
->last_returned_inactivity_timestamp
==
372 lttng_live_stream
->current_inactivity_timestamp
) {
373 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
374 print_stream_state(lttng_live_stream
);
376 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
380 lttng_live_stream
->base_offset
= index
.offset
;
381 lttng_live_stream
->offset
= index
.offset
;
382 lttng_live_stream
->len
= index
.packet_size
/ CHAR_BIT
;
384 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
385 ret
= lttng_live_iterator_next_check_stream_state(
386 lttng_live
, lttng_live_stream
);
392 * Creation of the notification requires the ctf trace to be created
393 * beforehand, but the live protocol gives us all streams (including
394 * metadata) at once. So we split it in three steps: getting streams,
395 * getting metadata (which creates the ctf trace), and then creating the
396 * per-stream notifications.
399 enum bt_ctf_lttng_live_iterator_status
lttng_live_get_session(
400 struct lttng_live_component
*lttng_live
,
401 struct lttng_live_session
*session
)
403 enum bt_ctf_lttng_live_iterator_status status
;
404 struct lttng_live_trace
*trace
, *t
;
406 if (lttng_live_attach_session(session
)) {
407 if (bt_graph_is_canceled(lttng_live
->graph
)) {
408 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
410 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
413 status
= lttng_live_get_new_streams(session
);
414 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
415 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
418 bt_list_for_each_entry_safe(trace
, t
, &session
->traces
, node
) {
419 status
= lttng_live_metadata_update(trace
);
420 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&&
421 status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
) {
425 return lttng_live_lazy_notif_init(session
);
429 void lttng_live_need_new_streams(struct lttng_live_component
*lttng_live
)
431 struct lttng_live_session
*session
;
433 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
434 session
->new_streams_needed
= true;
439 void lttng_live_force_new_streams_and_metadata(struct lttng_live_component
*lttng_live
)
441 struct lttng_live_session
*session
;
443 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
444 struct lttng_live_trace
*trace
;
446 session
->new_streams_needed
= true;
447 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
448 trace
->new_metadata_needed
= true;
454 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_new_streams_and_metadata(
455 struct lttng_live_component
*lttng_live
)
457 enum bt_ctf_lttng_live_iterator_status ret
=
458 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
459 unsigned int nr_sessions_opened
= 0;
460 struct lttng_live_session
*session
, *s
;
462 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
463 if (session
->closed
&& bt_list_empty(&session
->traces
)) {
464 lttng_live_destroy_session(session
);
468 * Currently, when there are no sessions, we quit immediately.
469 * We may want to add a component parameter to keep trying until
470 * we get data in the future.
471 * Also, in a remotely distant future, we could add a "new
472 * session" flag to the protocol, which would tell us that we
473 * need to query for new sessions even though we have sessions
476 if (bt_list_empty(<tng_live
->sessions
)) {
477 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
480 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
481 ret
= lttng_live_get_session(lttng_live
, session
);
483 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
485 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
486 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
491 if (!session
->closed
) {
492 nr_sessions_opened
++;
496 if (ret
== BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
&& !nr_sessions_opened
) {
497 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
503 enum bt_ctf_lttng_live_iterator_status
emit_inactivity_notification(
504 struct lttng_live_component
*lttng_live
,
505 struct lttng_live_stream_iterator
*lttng_live_stream
,
506 struct bt_notification
**notification
,
509 enum bt_ctf_lttng_live_iterator_status ret
=
510 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
511 struct lttng_live_trace
*trace
;
512 struct bt_ctf_clock_class
*clock_class
= NULL
;
513 struct bt_ctf_clock_value
*clock_value
= NULL
;
514 struct bt_notification
*notif
= NULL
;
517 trace
= lttng_live_stream
->trace
;
521 clock_class
= bt_clock_class_priority_map_get_clock_class_by_index(trace
->cc_prio_map
, 0);
525 clock_value
= bt_ctf_clock_value_create(clock_class
, timestamp
);
529 notif
= bt_notification_inactivity_create(trace
->cc_prio_map
);
533 retval
= bt_notification_inactivity_set_clock_value(notif
, clock_value
);
537 *notification
= notif
;
544 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
550 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_quiescent_stream(
551 struct lttng_live_component
*lttng_live
,
552 struct lttng_live_stream_iterator
*lttng_live_stream
,
553 struct bt_notification
**notification
)
555 enum bt_ctf_lttng_live_iterator_status ret
=
556 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
557 struct bt_ctf_clock_class
*clock_class
= NULL
;
558 struct bt_ctf_clock_value
*clock_value
= NULL
;
560 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_QUIESCENT
) {
561 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
564 if (lttng_live_stream
->current_inactivity_timestamp
==
565 lttng_live_stream
->last_returned_inactivity_timestamp
) {
566 lttng_live_stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA
;
567 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
571 ret
= emit_inactivity_notification(lttng_live
, lttng_live_stream
, notification
,
572 (uint64_t) lttng_live_stream
->current_inactivity_timestamp
);
574 lttng_live_stream
->last_returned_inactivity_timestamp
=
575 lttng_live_stream
->current_inactivity_timestamp
;
583 enum bt_ctf_lttng_live_iterator_status
lttng_live_iterator_next_handle_one_active_data_stream(
584 struct lttng_live_component
*lttng_live
,
585 struct lttng_live_stream_iterator
*lttng_live_stream
,
586 struct bt_notification
**notification
)
588 enum bt_ctf_lttng_live_iterator_status ret
=
589 BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
590 enum bt_ctf_notif_iter_status status
;
591 struct lttng_live_session
*session
;
593 bt_list_for_each_entry(session
, <tng_live
->sessions
, node
) {
594 struct lttng_live_trace
*trace
;
596 if (session
->new_streams_needed
) {
597 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
599 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
600 if (trace
->new_metadata_needed
) {
601 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
606 if (lttng_live_stream
->state
!= LTTNG_LIVE_STREAM_ACTIVE_DATA
) {
607 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
609 if (lttng_live_stream
->packet_end_notif_queue
) {
610 *notification
= lttng_live_stream
->packet_end_notif_queue
;
611 lttng_live_stream
->packet_end_notif_queue
= NULL
;
612 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
614 status
= bt_ctf_notif_iter_get_next_notification(
615 lttng_live_stream
->notif_iter
,
616 lttng_live_stream
->trace
->cc_prio_map
,
618 if (status
== BT_CTF_NOTIF_ITER_STATUS_OK
) {
620 * Consider empty packets as inactivity.
622 if (bt_notification_get_type(*notification
) == BT_NOTIFICATION_TYPE_PACKET_END
) {
623 lttng_live_stream
->packet_end_notif_queue
= *notification
;
624 *notification
= NULL
;
625 return emit_inactivity_notification(lttng_live
,
626 lttng_live_stream
, notification
,
627 lttng_live_stream
->current_packet_end_timestamp
);
632 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
633 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
635 case BT_CTF_NOTIF_ITER_STATUS_OK
:
636 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
638 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
640 * Continue immediately (end of packet). The next
641 * get_index may return AGAIN to delay the following
644 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
646 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
647 /* No argument provided by the user, so don't return INVAL. */
648 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
650 ret
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
658 * handle_no_data_streams()
660 * - for each ACTIVE_NO_DATA stream:
661 * - query relayd for stream data, or quiescence info.
662 * - if need metadata, get metadata, goto retry.
663 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
664 * - if quiescent, move to QUIESCENT streams
665 * - if fetched data, move to ACTIVE_DATA streams
666 * (at this point each stream either has data, or is quiescent)
670 * handle_new_streams_and_metadata()
671 * - query relayd for known streams, add them as ACTIVE_NO_DATA
672 * - query relayd for metadata
674 * call handle_active_no_data_streams()
676 * handle_quiescent_streams()
677 * - if at least one stream is ACTIVE_DATA:
678 * - peek stream event with lowest timestamp -> next_ts
679 * - for each quiescent stream
680 * - if next_ts >= quiescent end
681 * - set state to ACTIVE_NO_DATA
683 * - for each quiescent stream
684 * - set state to ACTIVE_NO_DATA
686 * call handle_active_no_data_streams()
688 * handle_active_data_streams()
689 * - if at least one stream is ACTIVE_DATA:
690 * - get stream event with lowest timestamp from heap
691 * - make that stream event the current notification.
692 * - move this stream heap position to its next event
693 * - if we need to fetch data from relayd, move
694 * stream to ACTIVE_NO_DATA.
698 * end criterion: ctrl-c on client. If relayd exits or the session
699 * closes on the relay daemon side, we keep on waiting for streams.
700 * Eventually handle --end timestamp (also an end criterion).
702 * When disconnected from relayd: try to re-connect endlessly.
705 struct bt_notification_iterator_next_return
lttng_live_iterator_next_stream(
706 struct bt_private_notification_iterator
*iterator
,
707 struct lttng_live_stream_iterator
*stream_iter
)
709 enum bt_ctf_lttng_live_iterator_status status
;
710 struct bt_notification_iterator_next_return next_return
;
711 struct lttng_live_component
*lttng_live
;
713 lttng_live
= stream_iter
->trace
->session
->lttng_live
;
715 print_stream_state(stream_iter
);
716 next_return
.notification
= NULL
;
717 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
718 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
721 status
= lttng_live_iterator_next_handle_one_no_data_stream(
722 lttng_live
, stream_iter
);
723 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
726 status
= lttng_live_iterator_next_handle_one_quiescent_stream(
727 lttng_live
, stream_iter
, &next_return
.notification
);
728 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
729 assert(next_return
.notification
== NULL
);
732 if (next_return
.notification
) {
735 status
= lttng_live_iterator_next_handle_one_active_data_stream(lttng_live
,
736 stream_iter
, &next_return
.notification
);
737 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
738 assert(next_return
.notification
== NULL
);
743 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
744 print_dbg("continue");
746 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
747 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
750 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
751 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
754 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
755 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
758 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
759 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
761 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
762 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
764 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
765 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
767 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
768 default: /* fall-through */
769 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
776 struct bt_notification_iterator_next_return
lttng_live_iterator_next_no_stream(
777 struct bt_private_notification_iterator
*iterator
,
778 struct lttng_live_no_stream_iterator
*no_stream_iter
)
780 enum bt_ctf_lttng_live_iterator_status status
;
781 struct bt_notification_iterator_next_return next_return
;
782 struct lttng_live_component
*lttng_live
;
784 lttng_live
= no_stream_iter
->lttng_live
;
786 lttng_live_force_new_streams_and_metadata(lttng_live
);
787 status
= lttng_live_iterator_next_handle_new_streams_and_metadata(lttng_live
);
788 if (status
!= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
791 if (no_stream_iter
->port
) {
792 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
794 status
= BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
;
798 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
:
800 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
:
801 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
803 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END
:
804 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
806 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
:
807 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
809 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL
:
810 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
812 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM
:
813 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
815 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED
:
816 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;
818 case BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
:
819 default: /* fall-through */
820 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
827 struct bt_notification_iterator_next_return
lttng_live_iterator_next(
828 struct bt_private_notification_iterator
*iterator
)
830 struct lttng_live_stream_iterator_generic
*s
=
831 bt_private_notification_iterator_get_user_data(iterator
);
832 struct bt_notification_iterator_next_return next_return
;
835 case LIVE_STREAM_TYPE_NO_STREAM
:
836 next_return
= lttng_live_iterator_next_no_stream(iterator
,
837 container_of(s
, struct lttng_live_no_stream_iterator
, p
));
839 case LIVE_STREAM_TYPE_STREAM
:
840 next_return
= lttng_live_iterator_next_stream(iterator
,
841 container_of(s
, struct lttng_live_stream_iterator
, p
));
844 next_return
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
851 enum bt_notification_iterator_status
lttng_live_iterator_init(
852 struct bt_private_notification_iterator
*it
,
853 struct bt_private_port
*port
)
855 enum bt_notification_iterator_status ret
=
856 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
857 struct lttng_live_stream_iterator_generic
*s
;
861 s
= bt_private_port_get_user_data(port
);
864 case LIVE_STREAM_TYPE_NO_STREAM
:
866 struct lttng_live_no_stream_iterator
*no_stream_iter
=
867 container_of(s
, struct lttng_live_no_stream_iterator
, p
);
868 ret
= bt_private_notification_iterator_set_user_data(it
, no_stream_iter
);
874 case LIVE_STREAM_TYPE_STREAM
:
876 struct lttng_live_stream_iterator
*stream_iter
=
877 container_of(s
, struct lttng_live_stream_iterator
, p
);
878 ret
= bt_private_notification_iterator_set_user_data(it
, stream_iter
);
885 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
892 if (bt_private_notification_iterator_set_user_data(it
, NULL
)
893 != BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
894 BT_LOGE("Error setting private data to NULL");
900 struct bt_value
*lttng_live_query_list_sessions(struct bt_component_class
*comp_class
,
901 struct bt_value
*params
)
903 struct bt_value
*url_value
= NULL
;
904 struct bt_value
*results
= NULL
;
906 struct bt_live_viewer_connection
*viewer_connection
= NULL
;
908 url_value
= bt_value_map_get(params
, "url");
909 if (!url_value
|| bt_value_is_null(url_value
) || !bt_value_is_string(url_value
)) {
910 BT_LOGW("Mandatory \"url\" parameter missing");
914 if (bt_value_string_get(url_value
, &url
) != BT_VALUE_STATUS_OK
) {
915 BT_LOGW("\"url\" parameter is required to be a string value");
919 viewer_connection
= bt_live_viewer_connection_create(url
, NULL
);
920 if (!viewer_connection
) {
924 results
= bt_live_viewer_connection_list_sessions(viewer_connection
);
929 if (viewer_connection
) {
930 bt_live_viewer_connection_destroy(viewer_connection
);
937 struct bt_value
*lttng_live_query(struct bt_component_class
*comp_class
,
938 const char *object
, struct bt_value
*params
)
940 if (strcmp(object
, "sessions") == 0) {
941 return lttng_live_query_list_sessions(comp_class
,
944 BT_LOGW("Unknown query object `%s`", object
);
949 void lttng_live_component_destroy_data(struct lttng_live_component
*lttng_live
)
952 struct lttng_live_session
*session
, *s
;
954 bt_list_for_each_entry_safe(session
, s
, <tng_live
->sessions
, node
) {
955 lttng_live_destroy_session(session
);
957 BT_PUT(lttng_live
->viewer_connection
);
958 if (lttng_live
->url
) {
959 g_string_free(lttng_live
->url
, TRUE
);
961 if (lttng_live
->no_stream_port
) {
962 ret
= bt_private_port_remove_from_component(lttng_live
->no_stream_port
);
964 BT_PUT(lttng_live
->no_stream_port
);
966 if (lttng_live
->no_stream_iter
) {
967 g_free(lttng_live
->no_stream_iter
);
973 void lttng_live_component_finalize(struct bt_private_component
*component
)
975 void *data
= bt_private_component_get_user_data(component
);
980 lttng_live_component_destroy_data(data
);
984 struct lttng_live_component
*lttng_live_component_create(struct bt_value
*params
,
985 struct bt_private_component
*private_component
,
986 struct bt_graph
*graph
)
988 struct lttng_live_component
*lttng_live
;
989 struct bt_value
*value
= NULL
;
991 enum bt_value_status ret
;
993 lttng_live
= g_new0(struct lttng_live_component
, 1);
997 /* TODO: make this an overridable parameter. */
998 lttng_live
->max_query_size
= MAX_QUERY_SIZE
;
999 BT_INIT_LIST_HEAD(<tng_live
->sessions
);
1000 value
= bt_value_map_get(params
, "url");
1001 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
1002 BT_LOGW("Mandatory \"url\" parameter missing");
1005 ret
= bt_value_string_get(value
, &url
);
1006 if (ret
!= BT_VALUE_STATUS_OK
) {
1007 BT_LOGW("\"url\" parameter is required to be a string value");
1010 lttng_live
->url
= g_string_new(url
);
1011 if (!lttng_live
->url
) {
1014 lttng_live
->viewer_connection
=
1015 bt_live_viewer_connection_create(lttng_live
->url
->str
, lttng_live
);
1016 if (!lttng_live
->viewer_connection
) {
1019 if (lttng_live_create_viewer_session(lttng_live
)) {
1022 lttng_live
->private_component
= private_component
;
1023 lttng_live
->graph
= graph
;
1028 lttng_live_component_destroy_data(lttng_live
);
1035 enum bt_component_status
lttng_live_component_init(
1036 struct bt_private_component
*private_component
,
1037 struct bt_value
*params
, void *init_method_data
)
1039 struct lttng_live_component
*lttng_live
;
1040 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
1041 struct bt_component
*component
;
1042 struct bt_graph
*graph
;
1044 component
= bt_component_from_private_component(private_component
);
1045 graph
= bt_component_get_graph(component
);
1046 bt_put(graph
); /* weak */
1049 /* Passes ownership of iter ref to lttng_live_component_create. */
1050 lttng_live
= lttng_live_component_create(params
, private_component
,
1053 if (bt_graph_is_canceled(graph
)) {
1054 ret
= BT_COMPONENT_STATUS_AGAIN
;
1056 ret
= BT_COMPONENT_STATUS_NOMEM
;
1061 lttng_live
->no_stream_iter
= g_new0(struct lttng_live_no_stream_iterator
, 1);
1062 lttng_live
->no_stream_iter
->p
.type
= LIVE_STREAM_TYPE_NO_STREAM
;
1063 lttng_live
->no_stream_iter
->lttng_live
= lttng_live
;
1065 lttng_live
->no_stream_port
=
1066 bt_private_component_source_add_output_private_port(
1067 lttng_live
->private_component
, "no-stream",
1068 lttng_live
->no_stream_iter
);
1069 lttng_live
->no_stream_iter
->port
= lttng_live
->no_stream_port
;
1071 ret
= bt_private_component_set_user_data(private_component
, lttng_live
);
1072 if (ret
!= BT_COMPONENT_STATUS_OK
) {
1079 (void) bt_private_component_set_user_data(private_component
, NULL
);
1080 lttng_live_component_destroy_data(lttng_live
);
1085 enum bt_component_status
lttng_live_accept_port_connection(
1086 struct bt_private_component
*private_component
,
1087 struct bt_private_port
*self_private_port
,
1088 struct bt_port
*other_port
)
1090 struct lttng_live_component
*lttng_live
=
1091 bt_private_component_get_user_data(private_component
);
1092 struct bt_component
*other_component
;
1093 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
1094 struct bt_port
*self_port
= bt_port_from_private_port(self_private_port
);
1096 other_component
= bt_port_get_component(other_port
);
1097 bt_put(other_component
); /* weak */
1099 if (!lttng_live
->downstream_component
) {
1100 lttng_live
->downstream_component
= other_component
;
1105 * Compare prior component to ensure we are connected to the
1106 * same downstream component as prior ports.
1108 if (lttng_live
->downstream_component
!= other_component
) {
1109 BT_LOGW("Cannot connect ctf.lttng-live component port \"%s\" to component \"%s\": already connected to component \"%s\".",
1110 bt_port_get_name(self_port
),
1111 bt_component_get_name(other_component
),
1112 bt_component_get_name(lttng_live
->downstream_component
));
1113 status
= BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
;
1122 void __attribute__((constructor
)) bt_lttng_live_logging_ctor(void)
1124 enum bt_logging_level log_level
= BT_LOG_NONE
;
1125 const char *log_level_env
= getenv(BT_LOGLEVEL_NAME
);
1127 if (!log_level_env
) {
1131 if (strcmp(log_level_env
, "VERBOSE") == 0) {
1132 log_level
= BT_LOGGING_LEVEL_VERBOSE
;
1133 } else if (strcmp(log_level_env
, "DEBUG") == 0) {
1134 log_level
= BT_LOGGING_LEVEL_DEBUG
;
1135 } else if (strcmp(log_level_env
, "INFO") == 0) {
1136 log_level
= BT_LOGGING_LEVEL_INFO
;
1137 } else if (strcmp(log_level_env
, "WARN") == 0) {
1138 log_level
= BT_LOGGING_LEVEL_WARN
;
1139 } else if (strcmp(log_level_env
, "ERROR") == 0) {
1140 log_level
= BT_LOGGING_LEVEL_ERROR
;
1141 } else if (strcmp(log_level_env
, "FATAL") == 0) {
1142 log_level
= BT_LOGGING_LEVEL_FATAL
;
1144 bt_lttng_live_log_level
= BT_LOGGING_LEVEL_FATAL
;
1145 BT_LOGF("Incorrect log level specified in %s",
1150 bt_lttng_live_log_level
= log_level
;