143b0cf913766b0c274e48abac4c1cb6b722e073
[babeltrace.git] / src / 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_COMP_LOG_SELF_COMP self_comp
27 #define BT_LOG_OUTPUT_LEVEL log_level
28 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/DS"
29 #include "logging/comp-logging.h"
30
31 #include <inttypes.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include <glib.h>
36
37 #include <babeltrace2/babeltrace.h>
38
39 #include "../common/msg-iter/msg-iter.h"
40 #include "common/assert.h"
41 #include "compat/mman.h"
42 #include "data-stream.h"
43
44 #define STREAM_NAME_PREFIX "stream-"
45
46 static
47 enum ctf_msg_iter_medium_status medop_request_bytes(
48 size_t request_sz, uint8_t **buffer_addr,
49 size_t *buffer_sz, void *data)
50 {
51 enum ctf_msg_iter_medium_status status =
52 CTF_MSG_ITER_MEDIUM_STATUS_OK;
53 struct lttng_live_stream_iterator *stream = data;
54 struct lttng_live_trace *trace = stream->trace;
55 struct lttng_live_session *session = trace->session;
56 struct lttng_live_msg_iter *live_msg_iter = session->lttng_live_msg_iter;
57 uint64_t recv_len = 0;
58 uint64_t len_left;
59 uint64_t read_len;
60
61 BT_ASSERT(request_sz);
62
63 if (stream->has_stream_hung_up) {
64 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
65 goto end;
66 }
67
68 len_left = stream->base_offset + stream->len - stream->offset;
69 if (!len_left) {
70 stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
71 status = CTF_MSG_ITER_MEDIUM_STATUS_AGAIN;
72 goto end;
73 }
74
75 read_len = MIN(request_sz, stream->buflen);
76 read_len = MIN(read_len, len_left);
77 status = lttng_live_get_stream_bytes(live_msg_iter,
78 stream, stream->buf, stream->offset,
79 read_len, &recv_len);
80 *buffer_addr = stream->buf;
81 *buffer_sz = recv_len;
82 stream->offset += recv_len;
83 end:
84 return status;
85 }
86
87 static
88 bt_stream *medop_borrow_stream(bt_stream_class *stream_class,
89 int64_t stream_id, void *data)
90 {
91 struct lttng_live_stream_iterator *lttng_live_stream = data;
92 bt_logging_level log_level = lttng_live_stream->log_level;
93 bt_self_component *self_comp = lttng_live_stream->self_comp;
94
95 if (!lttng_live_stream->stream) {
96 uint64_t stream_class_id = bt_stream_class_get_id(stream_class);
97
98 BT_COMP_LOGI("Creating stream %s (ID: %" PRIu64 ") out of stream "
99 "class %" PRId64, lttng_live_stream->name->str,
100 stream_id, stream_class_id);
101
102 if (stream_id < 0) {
103 /*
104 * No stream instance ID in the stream. It's possible
105 * to encounter this situation with older version of
106 * LTTng. In these cases, use the viewer_stream_id that
107 * is unique for a live viewer session.
108 */
109 lttng_live_stream->stream = bt_stream_create_with_id(
110 stream_class, lttng_live_stream->trace->trace,
111 lttng_live_stream->viewer_stream_id);
112 } else {
113 lttng_live_stream->stream = bt_stream_create_with_id(
114 stream_class, lttng_live_stream->trace->trace,
115 (uint64_t) stream_id);
116 }
117
118 if (!lttng_live_stream->stream) {
119 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
120 "Cannot create stream %s (stream class ID "
121 "%" PRId64 ", stream ID %" PRIu64 ")",
122 lttng_live_stream->name->str,
123 stream_class_id, stream_id);
124 goto end;
125 }
126
127 bt_stream_set_name(lttng_live_stream->stream,
128 lttng_live_stream->name->str);
129 }
130
131 end:
132 return lttng_live_stream->stream;
133 }
134
135 static struct ctf_msg_iter_medium_ops medops = {
136 .request_bytes = medop_request_bytes,
137 .seek = NULL,
138 .borrow_stream = medop_borrow_stream,
139 };
140
141 BT_HIDDEN
142 enum lttng_live_iterator_status lttng_live_lazy_msg_init(
143 struct lttng_live_session *session,
144 bt_self_message_iterator *self_msg_iter)
145 {
146 struct lttng_live_component *lttng_live =
147 session->lttng_live_msg_iter->lttng_live_comp;
148 uint64_t trace_idx, stream_iter_idx;
149 bt_logging_level log_level = session->log_level;
150 bt_self_component *self_comp = session->self_comp;
151
152 if (!session->lazy_stream_msg_init) {
153 return LTTNG_LIVE_ITERATOR_STATUS_OK;
154 }
155
156 BT_COMP_LOGD("Lazily initializing self message iterator for live session: "
157 "session-id=%"PRIu64", self-msg-iter-addr=%p", session->id,
158 self_msg_iter);
159
160 for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) {
161 struct lttng_live_trace *trace =
162 g_ptr_array_index(session->traces, trace_idx);
163
164 for (stream_iter_idx = 0;
165 stream_iter_idx < trace->stream_iterators->len;
166 stream_iter_idx++) {
167 struct ctf_trace_class *ctf_tc;
168 struct lttng_live_stream_iterator *stream_iter =
169 g_ptr_array_index(trace->stream_iterators,
170 stream_iter_idx);
171
172 if (stream_iter->msg_iter) {
173 continue;
174 }
175 ctf_tc = ctf_metadata_decoder_borrow_ctf_trace_class(
176 trace->metadata->decoder);
177 BT_COMP_LOGD("Creating CTF message iterator: "
178 "session-id=%"PRIu64", ctf-tc-addr=%p, "
179 "stream-iter-name=%s, self-msg-iter-addr=%p",
180 session->id, ctf_tc, stream_iter->name->str, self_msg_iter);
181 stream_iter->msg_iter = ctf_msg_iter_create(ctf_tc,
182 lttng_live->max_query_size, medops, stream_iter,
183 log_level, self_comp, self_msg_iter);
184 if (!stream_iter->msg_iter) {
185 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
186 "Failed to create CTF message iterator");
187 goto error;
188 }
189 }
190 }
191
192 session->lazy_stream_msg_init = false;
193
194 return LTTNG_LIVE_ITERATOR_STATUS_OK;
195
196 error:
197 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
198 }
199
200 BT_HIDDEN
201 struct lttng_live_stream_iterator *lttng_live_stream_iterator_create(
202 struct lttng_live_session *session,
203 uint64_t ctf_trace_id,
204 uint64_t stream_id,
205 bt_self_message_iterator *self_msg_iter)
206 {
207 struct lttng_live_stream_iterator *stream_iter;
208 struct lttng_live_component *lttng_live;
209 struct lttng_live_trace *trace;
210 bt_logging_level log_level;
211 bt_self_component *self_comp;
212
213 BT_ASSERT(session);
214 BT_ASSERT(session->lttng_live_msg_iter);
215 BT_ASSERT(session->lttng_live_msg_iter->lttng_live_comp);
216 log_level = session->log_level;
217 self_comp = session->self_comp;
218
219 lttng_live = session->lttng_live_msg_iter->lttng_live_comp;
220
221 stream_iter = g_new0(struct lttng_live_stream_iterator, 1);
222 if (!stream_iter) {
223 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
224 "Failed to allocate struct lttng_live_stream_iterator");
225 goto error;
226 }
227
228 stream_iter->log_level = log_level;
229 stream_iter->self_comp = self_comp;
230 trace = lttng_live_session_borrow_or_create_trace_by_id(session, ctf_trace_id);
231 if (!trace) {
232 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
233 "Failed to borrow CTF trace.");
234 goto error;
235 }
236
237 stream_iter->trace = trace;
238 stream_iter->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
239 stream_iter->viewer_stream_id = stream_id;
240 stream_iter->ctf_stream_class_id = -1ULL;
241 stream_iter->last_inactivity_ts = INT64_MIN;
242
243 if (trace->trace) {
244 struct ctf_trace_class *ctf_tc =
245 ctf_metadata_decoder_borrow_ctf_trace_class(
246 trace->metadata->decoder);
247 BT_ASSERT(!stream_iter->msg_iter);
248 stream_iter->msg_iter = ctf_msg_iter_create(ctf_tc,
249 lttng_live->max_query_size, medops, stream_iter,
250 log_level, self_comp, self_msg_iter);
251 if (!stream_iter->msg_iter) {
252 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
253 "Failed to create CTF message iterator");
254 goto error;
255 }
256 }
257 stream_iter->buf = g_new0(uint8_t, lttng_live->max_query_size);
258 if (!stream_iter->buf) {
259 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
260 "Failed to allocate live stream iterator buffer");
261 goto error;
262 }
263
264 stream_iter->buflen = lttng_live->max_query_size;
265 stream_iter->name = g_string_new(NULL);
266 if (!stream_iter->name) {
267 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
268 "Failed to allocate live stream iterator name buffer");
269 goto error;
270 }
271
272 g_string_printf(stream_iter->name, STREAM_NAME_PREFIX "%" PRIu64,
273 stream_iter->viewer_stream_id);
274 g_ptr_array_add(trace->stream_iterators, stream_iter);
275
276 /* Track the number of active stream iterator. */
277 session->lttng_live_msg_iter->active_stream_iter++;
278
279 goto end;
280 error:
281 lttng_live_stream_iterator_destroy(stream_iter);
282 stream_iter = NULL;
283 end:
284 return stream_iter;
285 }
286
287 BT_HIDDEN
288 void lttng_live_stream_iterator_destroy(
289 struct lttng_live_stream_iterator *stream_iter)
290 {
291 if (!stream_iter) {
292 return;
293 }
294
295 if (stream_iter->stream) {
296 BT_STREAM_PUT_REF_AND_RESET(stream_iter->stream);
297 }
298
299 if (stream_iter->msg_iter) {
300 ctf_msg_iter_destroy(stream_iter->msg_iter);
301 }
302 g_free(stream_iter->buf);
303 if (stream_iter->name) {
304 g_string_free(stream_iter->name, TRUE);
305 }
306
307 bt_message_put_ref(stream_iter->current_msg);
308
309 /* Track the number of active stream iterator. */
310 stream_iter->trace->session->lttng_live_msg_iter->active_stream_iter--;
311
312 g_free(stream_iter);
313 }
This page took 0.035966 seconds and 3 git commands to generate.