Adapt `src.ctf.lttng-live` to current API
[babeltrace.git] / plugins / ctf / lttng-live / data-stream.c
1 /*
2 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
3 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
4 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC-DS"
27 #include "logging.h"
28
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <stdbool.h>
33 #include <glib.h>
34 #include <inttypes.h>
35 #include <babeltrace/compat/mman-internal.h>
36 #include <babeltrace/babeltrace.h>
37 #include "../common/msg-iter/msg-iter.h"
38 #include <babeltrace/assert-internal.h>
39
40 #include "data-stream.h"
41
42 #define STREAM_NAME_PREFIX "stream-"
43
44 static
45 enum bt_msg_iter_medium_status medop_request_bytes(
46 size_t request_sz, uint8_t **buffer_addr,
47 size_t *buffer_sz, void *data)
48 {
49 enum bt_msg_iter_medium_status status =
50 BT_MSG_ITER_MEDIUM_STATUS_OK;
51 struct lttng_live_stream_iterator *stream = data;
52 struct lttng_live_trace *trace = stream->trace;
53 struct lttng_live_session *session = trace->session;
54 struct lttng_live_msg_iter *live_msg_iter = session->lttng_live_msg_iter;
55 uint64_t recv_len = 0;
56 uint64_t len_left;
57 uint64_t read_len;
58
59 len_left = stream->base_offset + stream->len - stream->offset;
60 if (!len_left) {
61 stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
62 status = BT_MSG_ITER_MEDIUM_STATUS_AGAIN;
63 return status;
64 }
65 read_len = MIN(request_sz, stream->buflen);
66 read_len = MIN(read_len, len_left);
67 status = lttng_live_get_stream_bytes(live_msg_iter,
68 stream, stream->buf, stream->offset,
69 read_len, &recv_len);
70 *buffer_addr = stream->buf;
71 *buffer_sz = recv_len;
72 stream->offset += recv_len;
73 return status;
74 }
75
76 static
77 bt_stream *medop_borrow_stream(bt_stream_class *stream_class,
78 int64_t stream_id, void *data)
79 {
80 struct lttng_live_stream_iterator *lttng_live_stream = data;
81
82 if (!lttng_live_stream->stream) {
83 uint64_t stream_class_id = bt_stream_class_get_id(stream_class);
84
85 BT_LOGD("Creating stream %s (ID: %" PRIu64 ") out of stream "
86 "class %" PRId64, lttng_live_stream->name->str,
87 stream_id, stream_class_id);
88
89 if (stream_id < 0) {
90 /*
91 * No stream instance ID in the stream. It's possible
92 * to encounter this situation with older version of
93 * LTTng. In these cases, use the viewer_stream_id that
94 * is unique for a live viewer session.
95 */
96 lttng_live_stream->stream = bt_stream_create_with_id(
97 stream_class, lttng_live_stream->trace->trace,
98 lttng_live_stream->viewer_stream_id);
99 } else {
100 lttng_live_stream->stream = bt_stream_create_with_id(
101 stream_class, lttng_live_stream->trace->trace,
102 (uint64_t) stream_id);
103 }
104
105 if (!lttng_live_stream->stream) {
106 BT_LOGE("Cannot create stream %s (stream class ID "
107 "%" PRId64 ", stream ID %" PRIu64 ")",
108 lttng_live_stream->name->str,
109 stream_class_id, stream_id);
110 }
111 bt_stream_set_name(lttng_live_stream->stream,
112 lttng_live_stream->name->str);
113 }
114
115 return lttng_live_stream->stream;
116 }
117
118 static struct bt_msg_iter_medium_ops medops = {
119 .request_bytes = medop_request_bytes,
120 .seek = NULL,
121 .borrow_stream = medop_borrow_stream,
122 };
123
124 BT_HIDDEN
125 enum lttng_live_iterator_status lttng_live_lazy_msg_init(
126 struct lttng_live_session *session)
127 {
128 struct lttng_live_component *lttng_live =
129 session->lttng_live_msg_iter->lttng_live_comp;
130 uint64_t trace_idx, stream_iter_idx;
131
132 if (!session->lazy_stream_msg_init) {
133 return LTTNG_LIVE_ITERATOR_STATUS_OK;
134 }
135
136 for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) {
137 struct lttng_live_trace *trace =
138 g_ptr_array_index(session->traces, trace_idx);
139
140 for (stream_iter_idx = 0;
141 stream_iter_idx < trace->stream_iterators->len;
142 stream_iter_idx++) {
143 struct ctf_trace_class *ctf_tc;
144 struct lttng_live_stream_iterator *stream_iter =
145 g_ptr_array_index(trace->stream_iterators,
146 stream_iter_idx);
147
148 if (stream_iter->msg_iter) {
149 continue;
150 }
151 ctf_tc = ctf_metadata_decoder_borrow_ctf_trace_class(
152 trace->metadata->decoder);
153 stream_iter->msg_iter = bt_msg_iter_create(ctf_tc,
154 lttng_live->max_query_size, medops,
155 stream_iter);
156 if (!stream_iter->msg_iter) {
157 goto error;
158 }
159 }
160 }
161
162 session->lazy_stream_msg_init = false;
163
164 return LTTNG_LIVE_ITERATOR_STATUS_OK;
165
166 error:
167 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
168 }
169
170 BT_HIDDEN
171 struct lttng_live_stream_iterator *lttng_live_stream_iterator_create(
172 struct lttng_live_session *session,
173 uint64_t ctf_trace_id,
174 uint64_t stream_id)
175 {
176 struct lttng_live_stream_iterator *stream_iter;
177 struct lttng_live_component *lttng_live;
178 struct lttng_live_trace *trace;
179
180 BT_ASSERT(session);
181 BT_ASSERT(session->lttng_live_msg_iter);
182 BT_ASSERT(session->lttng_live_msg_iter->lttng_live_comp);
183
184 lttng_live = session->lttng_live_msg_iter->lttng_live_comp;
185
186 stream_iter = g_new0(struct lttng_live_stream_iterator, 1);
187 if (!stream_iter) {
188 goto error;
189 }
190
191 trace = lttng_live_borrow_trace(session, ctf_trace_id);
192 if (!trace) {
193 goto error;
194 }
195
196 stream_iter->trace = trace;
197 stream_iter->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
198 stream_iter->viewer_stream_id = stream_id;
199 stream_iter->ctf_stream_class_id = -1ULL;
200 stream_iter->last_inactivity_ts = INT64_MIN;
201
202 if (trace->trace) {
203 struct ctf_trace_class *ctf_tc =
204 ctf_metadata_decoder_borrow_ctf_trace_class(
205 trace->metadata->decoder);
206 BT_ASSERT(!stream_iter->msg_iter);
207 stream_iter->msg_iter = bt_msg_iter_create(ctf_tc,
208 lttng_live->max_query_size, medops,
209 stream_iter);
210 if (!stream_iter->msg_iter) {
211 goto error;
212 }
213 }
214 stream_iter->buf = g_new0(uint8_t, lttng_live->max_query_size);
215 if (!stream_iter->buf) {
216 goto error;
217 }
218
219 stream_iter->buflen = lttng_live->max_query_size;
220 stream_iter->name = g_string_new(NULL);
221 if (!stream_iter->name) {
222 goto error;
223 }
224
225 g_string_printf(stream_iter->name, STREAM_NAME_PREFIX "%" PRIu64,
226 stream_iter->viewer_stream_id);
227 g_ptr_array_add(trace->stream_iterators, stream_iter);
228
229 /* Track the number of active stream iterator. */
230 session->lttng_live_msg_iter->active_stream_iter++;
231
232 goto end;
233 error:
234 lttng_live_stream_iterator_destroy(stream_iter);
235 stream_iter = NULL;
236 end:
237 return stream_iter;
238 }
239
240 BT_HIDDEN
241 void lttng_live_stream_iterator_destroy(
242 struct lttng_live_stream_iterator *stream_iter)
243 {
244 if (!stream_iter) {
245 return;
246 }
247
248 if (stream_iter->stream) {
249 BT_STREAM_PUT_REF_AND_RESET(stream_iter->stream);
250 }
251
252 if (stream_iter->msg_iter) {
253 bt_msg_iter_destroy(stream_iter->msg_iter);
254 }
255 if (stream_iter->buf) {
256 g_free(stream_iter->buf);
257 }
258 if (stream_iter->name) {
259 g_string_free(stream_iter->name, TRUE);
260 }
261
262 bt_message_put_ref(stream_iter->current_msg);
263
264 /* Track the number of active stream iterator. */
265 stream_iter->trace->session->lttng_live_msg_iter->active_stream_iter--;
266
267 /*
268 * Ensure we poke the trace metadata in the future, which is
269 * required to release the metadata reference on the trace.
270 */
271 stream_iter->trace->new_metadata_needed = true;
272 g_free(stream_iter);
273 }
This page took 0.03486 seconds and 4 git commands to generate.