plugins: call bt_current_thread_clear_error() when not propagating error
[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 "plugins/comp-logging.h"
30
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <glib.h>
36 #include <inttypes.h>
37 #include "compat/mman.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "../common/msg-iter/msg-iter.h"
40 #include "common/assert.h"
41
42 #include "data-stream.h"
43
44 #define STREAM_NAME_PREFIX "stream-"
45
46 static
47 enum bt_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 bt_msg_iter_medium_status status =
52 BT_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 len_left = stream->base_offset + stream->len - stream->offset;
62 if (!len_left) {
63 stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
64 status = BT_MSG_ITER_MEDIUM_STATUS_AGAIN;
65 return status;
66 }
67 read_len = MIN(request_sz, stream->buflen);
68 read_len = MIN(read_len, len_left);
69 status = lttng_live_get_stream_bytes(live_msg_iter,
70 stream, stream->buf, stream->offset,
71 read_len, &recv_len);
72 *buffer_addr = stream->buf;
73 *buffer_sz = recv_len;
74 stream->offset += recv_len;
75 return status;
76 }
77
78 static
79 bt_stream *medop_borrow_stream(bt_stream_class *stream_class,
80 int64_t stream_id, void *data)
81 {
82 struct lttng_live_stream_iterator *lttng_live_stream = data;
83 bt_logging_level log_level = lttng_live_stream->log_level;
84 bt_self_component *self_comp = lttng_live_stream->self_comp;
85
86 if (!lttng_live_stream->stream) {
87 uint64_t stream_class_id = bt_stream_class_get_id(stream_class);
88
89 BT_COMP_LOGI("Creating stream %s (ID: %" PRIu64 ") out of stream "
90 "class %" PRId64, lttng_live_stream->name->str,
91 stream_id, stream_class_id);
92
93 if (stream_id < 0) {
94 /*
95 * No stream instance ID in the stream. It's possible
96 * to encounter this situation with older version of
97 * LTTng. In these cases, use the viewer_stream_id that
98 * is unique for a live viewer session.
99 */
100 lttng_live_stream->stream = bt_stream_create_with_id(
101 stream_class, lttng_live_stream->trace->trace,
102 lttng_live_stream->viewer_stream_id);
103 } else {
104 lttng_live_stream->stream = bt_stream_create_with_id(
105 stream_class, lttng_live_stream->trace->trace,
106 (uint64_t) stream_id);
107 }
108
109 if (!lttng_live_stream->stream) {
110 BT_COMP_LOGE("Cannot create stream %s (stream class ID "
111 "%" PRId64 ", stream ID %" PRIu64 ")",
112 lttng_live_stream->name->str,
113 stream_class_id, stream_id);
114 goto end;
115 }
116
117 bt_stream_set_name(lttng_live_stream->stream,
118 lttng_live_stream->name->str);
119 }
120
121 end:
122 return lttng_live_stream->stream;
123 }
124
125 static struct bt_msg_iter_medium_ops medops = {
126 .request_bytes = medop_request_bytes,
127 .seek = NULL,
128 .borrow_stream = medop_borrow_stream,
129 };
130
131 BT_HIDDEN
132 enum lttng_live_iterator_status lttng_live_lazy_msg_init(
133 struct lttng_live_session *session)
134 {
135 struct lttng_live_component *lttng_live =
136 session->lttng_live_msg_iter->lttng_live_comp;
137 uint64_t trace_idx, stream_iter_idx;
138 bt_logging_level log_level = session->log_level;
139 bt_self_component *self_comp = session->self_comp;
140
141 if (!session->lazy_stream_msg_init) {
142 return LTTNG_LIVE_ITERATOR_STATUS_OK;
143 }
144
145 for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) {
146 struct lttng_live_trace *trace =
147 g_ptr_array_index(session->traces, trace_idx);
148
149 for (stream_iter_idx = 0;
150 stream_iter_idx < trace->stream_iterators->len;
151 stream_iter_idx++) {
152 struct ctf_trace_class *ctf_tc;
153 struct lttng_live_stream_iterator *stream_iter =
154 g_ptr_array_index(trace->stream_iterators,
155 stream_iter_idx);
156
157 if (stream_iter->msg_iter) {
158 continue;
159 }
160 ctf_tc = ctf_metadata_decoder_borrow_ctf_trace_class(
161 trace->metadata->decoder);
162 stream_iter->msg_iter = bt_msg_iter_create(ctf_tc,
163 lttng_live->max_query_size, medops,
164 stream_iter, log_level, self_comp);
165 if (!stream_iter->msg_iter) {
166 goto error;
167 }
168 }
169 }
170
171 session->lazy_stream_msg_init = false;
172
173 return LTTNG_LIVE_ITERATOR_STATUS_OK;
174
175 error:
176 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
177 }
178
179 BT_HIDDEN
180 struct lttng_live_stream_iterator *lttng_live_stream_iterator_create(
181 struct lttng_live_session *session,
182 uint64_t ctf_trace_id,
183 uint64_t stream_id)
184 {
185 struct lttng_live_stream_iterator *stream_iter;
186 struct lttng_live_component *lttng_live;
187 struct lttng_live_trace *trace;
188 bt_logging_level log_level;
189 bt_self_component *self_comp;
190
191 BT_ASSERT(session);
192 BT_ASSERT(session->lttng_live_msg_iter);
193 BT_ASSERT(session->lttng_live_msg_iter->lttng_live_comp);
194 log_level = session->log_level;
195 self_comp = session->self_comp;
196
197 lttng_live = session->lttng_live_msg_iter->lttng_live_comp;
198
199 stream_iter = g_new0(struct lttng_live_stream_iterator, 1);
200 if (!stream_iter) {
201 goto error;
202 }
203
204 stream_iter->log_level = log_level;
205 stream_iter->self_comp = self_comp;
206 trace = lttng_live_borrow_trace(session, ctf_trace_id);
207 if (!trace) {
208 goto error;
209 }
210
211 stream_iter->trace = trace;
212 stream_iter->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
213 stream_iter->viewer_stream_id = stream_id;
214 stream_iter->ctf_stream_class_id = -1ULL;
215 stream_iter->last_inactivity_ts = INT64_MIN;
216
217 if (trace->trace) {
218 struct ctf_trace_class *ctf_tc =
219 ctf_metadata_decoder_borrow_ctf_trace_class(
220 trace->metadata->decoder);
221 BT_ASSERT(!stream_iter->msg_iter);
222 stream_iter->msg_iter = bt_msg_iter_create(ctf_tc,
223 lttng_live->max_query_size, medops,
224 stream_iter, log_level, self_comp);
225 if (!stream_iter->msg_iter) {
226 goto error;
227 }
228 }
229 stream_iter->buf = g_new0(uint8_t, lttng_live->max_query_size);
230 if (!stream_iter->buf) {
231 goto error;
232 }
233
234 stream_iter->buflen = lttng_live->max_query_size;
235 stream_iter->name = g_string_new(NULL);
236 if (!stream_iter->name) {
237 goto error;
238 }
239
240 g_string_printf(stream_iter->name, STREAM_NAME_PREFIX "%" PRIu64,
241 stream_iter->viewer_stream_id);
242 g_ptr_array_add(trace->stream_iterators, stream_iter);
243
244 /* Track the number of active stream iterator. */
245 session->lttng_live_msg_iter->active_stream_iter++;
246
247 goto end;
248 error:
249 lttng_live_stream_iterator_destroy(stream_iter);
250 stream_iter = NULL;
251 end:
252 return stream_iter;
253 }
254
255 BT_HIDDEN
256 void lttng_live_stream_iterator_destroy(
257 struct lttng_live_stream_iterator *stream_iter)
258 {
259 if (!stream_iter) {
260 return;
261 }
262
263 if (stream_iter->stream) {
264 BT_STREAM_PUT_REF_AND_RESET(stream_iter->stream);
265 }
266
267 if (stream_iter->msg_iter) {
268 bt_msg_iter_destroy(stream_iter->msg_iter);
269 }
270 if (stream_iter->buf) {
271 g_free(stream_iter->buf);
272 }
273 if (stream_iter->name) {
274 g_string_free(stream_iter->name, TRUE);
275 }
276
277 bt_message_put_ref(stream_iter->current_msg);
278
279 /* Track the number of active stream iterator. */
280 stream_iter->trace->session->lttng_live_msg_iter->active_stream_iter--;
281
282 /*
283 * Ensure we poke the trace metadata in the future, which is
284 * required to release the metadata reference on the trace.
285 */
286 stream_iter->trace->new_metadata_needed = true;
287 g_free(stream_iter);
288 }
This page took 0.035666 seconds and 4 git commands to generate.