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