2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
9 #define BT_COMP_LOG_SELF_COMP self_comp
10 #define BT_LOG_OUTPUT_LEVEL log_level
11 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
12 #include "logging/comp-logging.h"
19 #include "compat/memstream.h"
20 #include <babeltrace2/babeltrace.h>
22 #include "metadata.hpp"
23 #include "../common/metadata/decoder.hpp"
24 #include "../common/metadata/ctf-meta-configure-ir-trace.hpp"
26 #define TSDL_MAGIC 0x75d11d57
28 struct packet_header
{
32 uint32_t content_size
;
34 uint8_t compression_scheme
;
35 uint8_t encryption_scheme
;
36 uint8_t checksum_scheme
;
39 } __attribute__((__packed__
));
43 bool stream_classes_all_have_default_clock_class(bt_trace_class
*tc
,
44 bt_logging_level log_level
,
45 bt_self_component
*self_comp
)
48 const bt_clock_class
*cc
= NULL
;
49 const bt_stream_class
*sc
;
52 sc_count
= bt_trace_class_get_stream_class_count(tc
);
53 for (i
= 0; i
< sc_count
; i
++) {
54 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
58 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
61 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
62 "Stream class doesn't have a default clock class: "
63 "sc-id=%" PRIu64
", sc-name=\"%s\"",
64 bt_stream_class_get_id(sc
),
65 bt_stream_class_get_name(sc
));
74 * Iterate over the stream classes and returns the first clock class
75 * encountered. This is useful to create message iterator inactivity message as
76 * we don't need a particular clock class.
79 const bt_clock_class
*borrow_any_clock_class(bt_trace_class
*tc
)
82 const bt_clock_class
*cc
= NULL
;
83 const bt_stream_class
*sc
;
85 sc_count
= bt_trace_class_get_stream_class_count(tc
);
86 for (i
= 0; i
< sc_count
; i
++) {
87 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
90 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
101 enum lttng_live_iterator_status
lttng_live_metadata_update(
102 struct lttng_live_trace
*trace
)
104 struct lttng_live_session
*session
= trace
->session
;
105 struct lttng_live_metadata
*metadata
= trace
->metadata
;
106 size_t size
, len_read
= 0;
107 char *metadata_buf
= NULL
;
110 enum ctf_metadata_decoder_status decoder_status
;
111 enum lttng_live_iterator_status status
=
112 LTTNG_LIVE_ITERATOR_STATUS_OK
;
113 bt_logging_level log_level
= trace
->log_level
;
114 bt_self_component
*self_comp
= trace
->self_comp
;
115 enum lttng_live_get_one_metadata_status metadata_status
;
117 BT_COMP_LOGD("Updating metadata for trace: session-id=%" PRIu64
118 ", trace-id=%" PRIu64
, session
->id
, trace
->id
);
120 /* No metadata stream yet. */
122 if (session
->new_streams_needed
) {
123 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
125 session
->new_streams_needed
= true;
126 status
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
131 if (trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
136 * Open a new write only file handle to populate the `metadata_buf`
137 * memory buffer so we can write in loop in it easily.
139 fp
= bt_open_memstream(&metadata_buf
, &size
);
141 if (errno
== EINTR
&&
142 lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
143 session
->lttng_live_msg_iter
->was_interrupted
= true;
144 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
146 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
147 "Metadata open_memstream", ".");
148 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
153 keep_receiving
= true;
154 /* Grab all available metadata. */
155 while (keep_receiving
) {
156 size_t reply_len
= 0;
158 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
159 * for new metadata. If new metadata is received, the function
160 * writes it to the provided file handle and updates the
161 * reply_len output parameter. We call this function in loop
162 * until it returns _END meaning that no new metadata is
164 * We may receive a _CLOSED status if the metadata stream we
165 * are requesting is no longer available on the relay.
166 * If we receive an _ERROR status, it means there was a
167 * networking, allocating, or some other unrecoverable error.
169 metadata_status
= lttng_live_get_one_metadata_packet(trace
, fp
,
172 switch (metadata_status
) {
173 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
:
174 len_read
+= reply_len
;
176 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
:
177 keep_receiving
= false;
179 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
:
180 BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
181 "trace-id=%" PRIu64
", metadata-stream-id=%" PRIu64
,
182 trace
->id
, metadata
->stream_id
);
184 * The stream was closed and we received everything
185 * there was to receive for this metadata stream.
186 * We go on with the decoding of what we received. So
187 * that data stream can be decoded.
189 keep_receiving
= false;
190 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
;
192 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
:
193 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
194 "Error getting one trace metadata packet: "
195 "trace-id=%" PRIu64
, trace
->id
);
202 /* The memory buffer `metadata_buf` contains all the metadata. */
203 if (bt_close_memstream(&metadata_buf
, &size
, fp
)) {
204 BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", ".");
211 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
215 /* The relay sent zero bytes of metdata. */
216 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
221 * Open a new reading file handle on the `metadata_buf` and pass it to
222 * the metadata decoder.
224 fp
= bt_fmemopen(metadata_buf
, len_read
, "rb");
226 if (errno
== EINTR
&&
227 lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
228 session
->lttng_live_msg_iter
->was_interrupted
= true;
229 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
231 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
232 "Cannot memory-open metadata buffer", ".");
233 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
239 * The call to ctf_metadata_decoder_append_content() will append
240 * new metadata to our current trace class.
242 BT_COMP_LOGD("Appending new metadata to the ctf_trace class");
243 decoder_status
= ctf_metadata_decoder_append_content(
244 metadata
->decoder
, fp
);
245 switch (decoder_status
) {
246 case CTF_METADATA_DECODER_STATUS_OK
:
247 if (!trace
->trace_class
) {
248 struct ctf_trace_class
*tc
=
249 ctf_metadata_decoder_borrow_ctf_trace_class(
253 ctf_metadata_decoder_get_ir_trace_class(
255 trace
->trace
= bt_trace_create(trace
->trace_class
);
257 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
258 "Failed to create bt_trace");
261 if (ctf_trace_class_configure_ir_trace(tc
,
263 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
264 "Failed to configure ctf trace class");
267 if (!stream_classes_all_have_default_clock_class(
268 trace
->trace_class
, log_level
,
270 /* Error logged in function. */
274 borrow_any_clock_class(trace
->trace_class
);
277 /* The metadata was updated succesfully. */
278 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
288 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
293 closeret
= fclose(fp
);
295 BT_COMP_LOGW_ERRNO("Error on fclose", ".");
303 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
,
304 uint64_t ctf_trace_id
, uint64_t stream_id
,
305 const char *trace_name
)
307 bt_self_component
*self_comp
= session
->self_comp
;
308 bt_logging_level log_level
= session
->log_level
;
309 struct lttng_live_metadata
*metadata
= NULL
;
310 struct lttng_live_trace
*trace
;
312 ctf_metadata_decoder_config cfg
{};
313 cfg
.log_level
= session
->log_level
;
314 cfg
.self_comp
= session
->self_comp
;
315 cfg
.clock_class_offset_s
= 0;
316 cfg
.clock_class_offset_ns
= 0;
317 cfg
.create_trace_class
= true;
319 metadata
= g_new0(struct lttng_live_metadata
, 1);
323 metadata
->log_level
= session
->log_level
;
324 metadata
->self_comp
= session
->self_comp
;
325 metadata
->stream_id
= stream_id
;
327 metadata
->decoder
= ctf_metadata_decoder_create(&cfg
);
328 if (!metadata
->decoder
) {
329 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
330 "Failed to create CTF metadata decoder");
333 trace
= lttng_live_session_borrow_or_create_trace_by_id(session
,
336 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
337 "Failed to borrow trace");
340 trace
->metadata
= metadata
;
344 ctf_metadata_decoder_destroy(metadata
->decoder
);
350 void lttng_live_metadata_fini(struct lttng_live_trace
*trace
)
352 struct lttng_live_metadata
*metadata
= trace
->metadata
;
357 ctf_metadata_decoder_destroy(metadata
->decoder
);
358 trace
->metadata
= NULL
;