Fix: src.ctf.lttng-live: session closed before any metadata is received
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.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 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
6 * Some functions are based on older functions written by Mathieu Desnoyers.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
5278d5ba 27#define BT_COMP_LOG_SELF_COMP self_comp
1c0ceb65 28#define BT_LOG_OUTPUT_LEVEL log_level
b03487ab 29#define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
3fa1b6a3 30#include "logging/comp-logging.h"
020bc26f 31
7cdc2bab
MD
32#include <stdio.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <stdbool.h>
36#include <glib.h>
57952005 37#include "compat/memstream.h"
71c5da58 38#include <babeltrace2/babeltrace.h>
7cdc2bab 39
7cdc2bab
MD
40#include "metadata.h"
41#include "../common/metadata/decoder.h"
cd03c43c 42#include "../common/metadata/ctf-meta-configure-ir-trace.h"
7cdc2bab
MD
43
44#define TSDL_MAGIC 0x75d11d57
45
46struct packet_header {
47 uint32_t magic;
48 uint8_t uuid[16];
49 uint32_t checksum;
50 uint32_t content_size;
51 uint32_t packet_size;
52 uint8_t compression_scheme;
53 uint8_t encryption_scheme;
54 uint8_t checksum_scheme;
55 uint8_t major;
56 uint8_t minor;
57} __attribute__((__packed__));
58
bb18709b 59
7cdc2bab 60static
1c0ceb65 61bool stream_classes_all_have_default_clock_class(bt_trace_class *tc,
5278d5ba
PP
62 bt_logging_level log_level,
63 bt_self_component *self_comp)
7cdc2bab 64{
bb18709b
FD
65 uint64_t i, sc_count;
66 const bt_clock_class *cc = NULL;
67 const bt_stream_class *sc;
68 bool ret = true;
7cdc2bab 69
bb18709b
FD
70 sc_count = bt_trace_class_get_stream_class_count(tc);
71 for (i = 0; i < sc_count; i++) {
72 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
7cdc2bab 73
bb18709b 74 BT_ASSERT(sc);
7cdc2bab 75
bb18709b
FD
76 cc = bt_stream_class_borrow_default_clock_class_const(sc);
77 if (!cc) {
78 ret = false;
f6e2a9d2
FD
79 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
80 "Stream class doesn't have a default clock class: "
bb18709b
FD
81 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
82 bt_stream_class_get_id(sc),
83 bt_stream_class_get_name(sc));
84 goto end;
7cdc2bab
MD
85 }
86 }
87
7cdc2bab 88end:
bb18709b
FD
89 return ret;
90}
91/*
92 * Iterate over the stream classes and returns the first clock class
93 * encountered. This is useful to create message iterator inactivity message as
94 * we don't need a particular clock class.
95 */
96static
97const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc)
98{
99 uint64_t i, sc_count;
100 const bt_clock_class *cc = NULL;
101 const bt_stream_class *sc;
102
103 sc_count = bt_trace_class_get_stream_class_count(tc);
104 for (i = 0; i < sc_count; i++) {
105 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
ec4a3354 106 BT_ASSERT_DBG(sc);
bb18709b
FD
107
108 cc = bt_stream_class_borrow_default_clock_class_const(sc);
109 if (cc) {
110 goto end;
111 }
112 }
113end:
ec4a3354 114 BT_ASSERT_DBG(cc);
bb18709b 115 return cc;
7cdc2bab
MD
116}
117
118BT_HIDDEN
bb18709b 119enum lttng_live_iterator_status lttng_live_metadata_update(
7cdc2bab
MD
120 struct lttng_live_trace *trace)
121{
122 struct lttng_live_session *session = trace->session;
7cdc2bab 123 struct lttng_live_metadata *metadata = trace->metadata;
7cdc2bab
MD
124 size_t size, len_read = 0;
125 char *metadata_buf = NULL;
f797424a 126 bool keep_receiving;
7cdc2bab
MD
127 FILE *fp = NULL;
128 enum ctf_metadata_decoder_status decoder_status;
bb18709b
FD
129 enum lttng_live_iterator_status status =
130 LTTNG_LIVE_ITERATOR_STATUS_OK;
1c0ceb65 131 bt_logging_level log_level = trace->log_level;
5278d5ba 132 bt_self_component *self_comp = trace->self_comp;
f797424a 133 enum lttng_live_get_one_metadata_status metadata_status;
7cdc2bab 134
f555be5d
FD
135 BT_COMP_LOGD("Updating metadata for trace: trace-id=%"PRIu64, trace->id);
136
7cdc2bab
MD
137 /* No metadata stream yet. */
138 if (!metadata) {
dfad4cb9
FD
139 if (session->closed) {
140 /*
141 * The session is closed AND we never received any
142 * metadata this indicates that we will never receive
143 * any metadata.
144 */
145 status = LTTNG_LIVE_ITERATOR_STATUS_END;
146 } else if (session->new_streams_needed) {
bb18709b 147 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
148 } else {
149 session->new_streams_needed = true;
bb18709b 150 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab
MD
151 }
152 goto end;
153 }
154
d6e1f86b 155 if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) {
7cdc2bab
MD
156 goto end;
157 }
158
8dd99d00
FD
159 /*
160 * Open a new write only file handle to populate the `metadata_buf`
161 * memory buffer so we can write in loop in it easily.
162 */
7cdc2bab
MD
163 fp = bt_open_memstream(&metadata_buf, &size);
164 if (!fp) {
403657fd
FD
165 if (errno == EINTR &&
166 lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
8dd99d00 167 session->lttng_live_msg_iter->was_interrupted = true;
403657fd
FD
168 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
169 } else {
170 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
171 "Metadata open_memstream", ".");
172 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
173 }
174 goto end;
7cdc2bab
MD
175 }
176
f797424a 177 keep_receiving = true;
7cdc2bab 178 /* Grab all available metadata. */
f797424a
FD
179 while (keep_receiving) {
180 size_t reply_len = 0;
7cdc2bab 181 /*
f797424a
FD
182 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
183 * for new metadata. If new metadata is received, the function
184 * writes it to the provided file handle and updates the
185 * reply_len output parameter. We call this function in loop
186 * until it returns _END meaning that no new metadata is
187 * available.
188 * We may receive a _CLOSED status if the metadata stream we
189 * are requesting is no longer available on the relay.
190 * If we receive an _ERROR status, it means there was a
191 * networking, allocating, or some other unrecoverable error.
7cdc2bab 192 */
f797424a
FD
193 metadata_status = lttng_live_get_one_metadata_packet(trace, fp,
194 &reply_len);
195
196 switch (metadata_status) {
197 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK:
198 len_read += reply_len;
199 break;
200 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END:
201 keep_receiving = false;
202 break;
203 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED:
f555be5d
FD
204 BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
205 "trace-id=%"PRIu64", metadata-stream-id=%"PRIu64,
206 trace->id, metadata->stream_id);
d6e1f86b
FD
207 /*
208 * The stream was closed and we received everything
209 * there was to receive for this metadata stream.
210 * We go on with the decoding of what we received. So
211 * that data stream can be decoded.
212 */
f797424a 213 keep_receiving = false;
d6e1f86b 214 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED;
f797424a
FD
215 break;
216 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR:
f555be5d
FD
217 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
218 "Error getting one trace metadata packet: "
219 "trace-id=%"PRIu64, trace->id);
f797424a
FD
220 goto error;
221 default:
24847fc7 222 bt_common_abort();
7cdc2bab 223 }
f797424a 224 }
7cdc2bab 225
8dd99d00 226 /* The memory buffer `metadata_buf` contains all the metadata. */
7cdc2bab 227 if (bt_close_memstream(&metadata_buf, &size, fp)) {
f555be5d 228 BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", ".");
7cdc2bab 229 }
67875d75 230
7cdc2bab
MD
231 fp = NULL;
232
233 if (len_read == 0) {
234 if (!trace->trace) {
bb18709b 235 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
236 goto end;
237 }
d6e1f86b
FD
238
239 /* The relay sent zero bytes of metdata. */
240 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
7cdc2bab
MD
241 goto end;
242 }
243
8dd99d00
FD
244 /*
245 * Open a new reading file handle on the `metadata_buf` and pass it to
246 * the metadata decoder.
247 */
7cdc2bab
MD
248 fp = bt_fmemopen(metadata_buf, len_read, "rb");
249 if (!fp) {
403657fd
FD
250 if (errno == EINTR &&
251 lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
8dd99d00 252 session->lttng_live_msg_iter->was_interrupted = true;
403657fd
FD
253 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
254 } else {
255 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
256 "Cannot memory-open metadata buffer", ".");
257 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
258 }
259 goto end;
7cdc2bab
MD
260 }
261
bb18709b 262 /*
526c3a49
PP
263 * The call to ctf_metadata_decoder_append_content() will append
264 * new metadata to our current trace class.
bb18709b 265 */
f555be5d 266 BT_COMP_LOGD("Appending new metadata to the ctf_trace class");
526c3a49
PP
267 decoder_status = ctf_metadata_decoder_append_content(
268 metadata->decoder, fp);
7cdc2bab
MD
269 switch (decoder_status) {
270 case CTF_METADATA_DECODER_STATUS_OK:
bb18709b 271 if (!trace->trace_class) {
cd03c43c
PP
272 struct ctf_trace_class *tc =
273 ctf_metadata_decoder_borrow_ctf_trace_class(
274 metadata->decoder);
275
bb18709b
FD
276 trace->trace_class =
277 ctf_metadata_decoder_get_ir_trace_class(
278 metadata->decoder);
279 trace->trace = bt_trace_create(trace->trace_class);
cd03c43c 280 if (!trace->trace) {
f6e2a9d2
FD
281 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
282 "Failed to create bt_trace");
cd03c43c
PP
283 goto error;
284 }
285 if (ctf_trace_class_configure_ir_trace(tc,
286 trace->trace)) {
f6e2a9d2
FD
287 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
288 "Failed to configure ctf trace class");
cd03c43c
PP
289 goto error;
290 }
bb18709b 291 if (!stream_classes_all_have_default_clock_class(
5278d5ba
PP
292 trace->trace_class, log_level,
293 self_comp)) {
bb18709b
FD
294 /* Error logged in function. */
295 goto error;
296 }
297 trace->clock_class =
298 borrow_any_clock_class(trace->trace_class);
7cdc2bab 299 }
d6e1f86b
FD
300
301 /* The metadata was updated succesfully. */
302 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
bb18709b 303
7cdc2bab 304 break;
26aa224b 305 default:
7cdc2bab
MD
306 goto error;
307 }
308
309 goto end;
f797424a 310
7cdc2bab 311error:
403657fd 312 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
7cdc2bab
MD
313end:
314 if (fp) {
315 int closeret;
316
317 closeret = fclose(fp);
318 if (closeret) {
f555be5d 319 BT_COMP_LOGW_ERRNO("Error on fclose", ".");
7cdc2bab
MD
320 }
321 }
6f79a7cf 322 free(metadata_buf);
7cdc2bab
MD
323 return status;
324}
325
326BT_HIDDEN
327int lttng_live_metadata_create_stream(struct lttng_live_session *session,
5278d5ba 328 uint64_t ctf_trace_id, uint64_t stream_id,
06994c71 329 const char *trace_name)
7cdc2bab 330{
f6e2a9d2
FD
331 bt_self_component *self_comp = session->self_comp;
332 bt_logging_level log_level = session->log_level;
7cdc2bab
MD
333 struct lttng_live_metadata *metadata = NULL;
334 struct lttng_live_trace *trace;
1f1f2720 335 struct ctf_metadata_decoder_config cfg = {
1c0ceb65 336 .log_level = session->log_level,
5278d5ba 337 .self_comp = session->self_comp,
1f1f2720
PP
338 .clock_class_offset_s = 0,
339 .clock_class_offset_ns = 0,
526c3a49 340 .create_trace_class = true,
1f1f2720 341 };
7cdc2bab
MD
342
343 metadata = g_new0(struct lttng_live_metadata, 1);
344 if (!metadata) {
345 return -1;
346 }
1c0ceb65 347 metadata->log_level = session->log_level;
5278d5ba 348 metadata->self_comp = session->self_comp;
7cdc2bab 349 metadata->stream_id = stream_id;
bb18709b 350
16699f85 351 metadata->decoder = ctf_metadata_decoder_create(&cfg);
7cdc2bab 352 if (!metadata->decoder) {
f6e2a9d2
FD
353 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
354 "Failed to create CTF metadata decoder");
7cdc2bab
MD
355 goto error;
356 }
118fe47c
FD
357 trace = lttng_live_session_borrow_or_create_trace_by_id(session,
358 ctf_trace_id);
7cdc2bab 359 if (!trace) {
f6e2a9d2
FD
360 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
361 "Failed to borrow trace");
7cdc2bab
MD
362 goto error;
363 }
7cdc2bab
MD
364 trace->metadata = metadata;
365 return 0;
366
367error:
368 ctf_metadata_decoder_destroy(metadata->decoder);
369 g_free(metadata);
370 return -1;
371}
372
373BT_HIDDEN
374void lttng_live_metadata_fini(struct lttng_live_trace *trace)
375{
376 struct lttng_live_metadata *metadata = trace->metadata;
377
378 if (!metadata) {
379 return;
380 }
7cdc2bab
MD
381 ctf_metadata_decoder_destroy(metadata->decoder);
382 trace->metadata = NULL;
7cdc2bab
MD
383 g_free(metadata);
384}
This page took 0.089382 seconds and 4 git commands to generate.