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