2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC-DS"
34 #include <babeltrace/compat/mman-internal.h>
35 #include <babeltrace/ctf-ir/stream.h>
36 #include "../common/notif-iter/notif-iter.h"
39 #include "data-stream.h"
42 enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
43 size_t request_sz
, uint8_t **buffer_addr
,
44 size_t *buffer_sz
, void *data
)
46 enum bt_ctf_notif_iter_medium_status status
=
47 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
48 struct lttng_live_stream_iterator
*stream
= data
;
49 struct lttng_live_trace
*trace
= stream
->trace
;
50 struct lttng_live_session
*session
= trace
->session
;
51 struct lttng_live_component
*lttng_live
= session
->lttng_live
;
52 uint64_t recv_len
= 0;
57 len_left
= stream
->base_offset
+ stream
->len
- stream
->offset
;
59 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
60 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN
;
63 read_len
= MIN(request_sz
, stream
->buflen
);
64 read_len
= MIN(read_len
, len_left
);
65 status
= lttng_live_get_stream_bytes(lttng_live
,
66 stream
, stream
->buf
, stream
->offset
,
68 *buffer_addr
= stream
->buf
;
69 *buffer_sz
= recv_len
;
70 stream
->offset
+= recv_len
;
75 struct bt_ctf_stream
*medop_get_stream(
76 struct bt_ctf_stream_class
*stream_class
,
77 uint64_t stream_id
, void *data
)
79 struct lttng_live_stream_iterator
*lttng_live_stream
= data
;
81 if (!lttng_live_stream
->stream
) {
82 int64_t stream_class_id
=
83 bt_ctf_stream_class_get_id(stream_class
);
85 BT_LOGD("Creating stream %s (ID: %" PRIu64
") out of stream class %" PRId64
,
86 lttng_live_stream
->name
, stream_id
, stream_class_id
);
88 if (stream_id
== -1ULL) {
90 lttng_live_stream
->stream
= bt_ctf_stream_create(
91 stream_class
, lttng_live_stream
->name
);
93 lttng_live_stream
->stream
=
94 bt_ctf_stream_create_with_id(stream_class
,
95 lttng_live_stream
->name
, stream_id
);
98 if (!lttng_live_stream
->stream
) {
99 BT_LOGE("Cannot create stream %s (stream class %" PRId64
", stream ID %" PRIu64
")",
100 lttng_live_stream
->name
,
101 stream_class_id
, stream_id
);
105 return lttng_live_stream
->stream
;
108 static struct bt_ctf_notif_iter_medium_ops medops
= {
109 .request_bytes
= medop_request_bytes
,
110 .get_stream
= medop_get_stream
,
114 enum bt_ctf_lttng_live_iterator_status
lttng_live_lazy_notif_init(
115 struct lttng_live_session
*session
)
117 struct lttng_live_component
*lttng_live
= session
->lttng_live
;
118 struct lttng_live_trace
*trace
;
120 if (!session
->lazy_stream_notif_init
) {
121 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
124 bt_list_for_each_entry(trace
, &session
->traces
, node
) {
125 struct lttng_live_stream_iterator
*stream
;
127 bt_list_for_each_entry(stream
, &trace
->streams
, node
) {
128 if (stream
->notif_iter
) {
131 stream
->notif_iter
= bt_ctf_notif_iter_create(trace
->trace
,
132 lttng_live
->max_query_size
, medops
,
134 if (!stream
->notif_iter
) {
140 session
->lazy_stream_notif_init
= false;
142 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK
;
145 return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
149 struct lttng_live_stream_iterator
*lttng_live_stream_iterator_create(
150 struct lttng_live_session
*session
,
151 uint64_t ctf_trace_id
,
154 struct lttng_live_component
*lttng_live
= session
->lttng_live
;
155 struct lttng_live_stream_iterator
*stream
=
156 g_new0(struct lttng_live_stream_iterator
, 1);
157 struct lttng_live_trace
*trace
;
160 trace
= lttng_live_ref_trace(session
, ctf_trace_id
);
165 stream
->p
.type
= LIVE_STREAM_TYPE_STREAM
;
166 stream
->trace
= trace
;
167 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
168 stream
->viewer_stream_id
= stream_id
;
169 stream
->ctf_stream_class_id
= -1ULL;
170 stream
->last_returned_inactivity_timestamp
= INT64_MIN
;
173 stream
->notif_iter
= bt_ctf_notif_iter_create(trace
->trace
,
174 lttng_live
->max_query_size
, medops
,
176 if (!stream
->notif_iter
) {
180 stream
->buf
= g_new0(uint8_t, session
->lttng_live
->max_query_size
);
181 stream
->buflen
= session
->lttng_live
->max_query_size
;
183 ret
= lttng_live_add_port(lttng_live
, stream
);
186 bt_list_add(&stream
->node
, &trace
->streams
);
190 /* Do not touch "borrowed" file. */
191 lttng_live_stream_iterator_destroy(stream
);
198 void lttng_live_stream_iterator_destroy(struct lttng_live_stream_iterator
*stream
)
200 struct lttng_live_component
*lttng_live
;
207 lttng_live
= stream
->trace
->session
->lttng_live
;
208 ret
= lttng_live_remove_port(lttng_live
, stream
->port
);
211 if (stream
->stream
) {
212 BT_PUT(stream
->stream
);
215 if (stream
->notif_iter
) {
216 bt_ctf_notif_iter_destroy(stream
->notif_iter
);
219 BT_PUT(stream
->packet_end_notif_queue
);
220 bt_list_del(&stream
->node
);
222 * Ensure we poke the trace metadata in the future, which is
223 * required to release the metadata reference on the trace.
225 stream
->trace
->new_metadata_needed
= true;
226 lttng_live_unref_trace(stream
->trace
);