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
33 uint32_t content_size
;
35 uint8_t compression_scheme
;
36 uint8_t encryption_scheme
;
37 uint8_t checksum_scheme
;
40 } __attribute__((__packed__
));
42 static bool stream_classes_all_have_default_clock_class(bt_trace_class
*tc
,
43 bt_logging_level log_level
,
44 bt_self_component
*self_comp
)
47 const bt_clock_class
*cc
= NULL
;
48 const bt_stream_class
*sc
;
51 sc_count
= bt_trace_class_get_stream_class_count(tc
);
52 for (i
= 0; i
< sc_count
; i
++) {
53 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
57 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
60 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
61 "Stream class doesn't have a default clock class: "
62 "sc-id=%" PRIu64
", sc-name=\"%s\"",
63 bt_stream_class_get_id(sc
), bt_stream_class_get_name(sc
));
72 * Iterate over the stream classes and returns the first clock class
73 * encountered. This is useful to create message iterator inactivity message as
74 * we don't need a particular clock class.
76 static const bt_clock_class
*borrow_any_clock_class(bt_trace_class
*tc
)
79 const bt_clock_class
*cc
= NULL
;
80 const bt_stream_class
*sc
;
82 sc_count
= bt_trace_class_get_stream_class_count(tc
);
83 for (i
= 0; i
< sc_count
; i
++) {
84 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
87 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
97 enum lttng_live_iterator_status
lttng_live_metadata_update(struct lttng_live_trace
*trace
)
99 struct lttng_live_session
*session
= trace
->session
;
100 struct lttng_live_metadata
*metadata
= trace
->metadata
;
101 size_t size
, len_read
= 0;
102 char *metadata_buf
= NULL
;
105 enum ctf_metadata_decoder_status decoder_status
;
106 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
107 bt_logging_level log_level
= trace
->log_level
;
108 bt_self_component
*self_comp
= trace
->self_comp
;
109 enum lttng_live_get_one_metadata_status metadata_status
;
111 BT_COMP_LOGD("Updating metadata for trace: session-id=%" PRIu64
", trace-id=%" PRIu64
,
112 session
->id
, trace
->id
);
114 /* No metadata stream yet. */
116 if (session
->closed
) {
118 * The session is closed AND we never received any
119 * metadata this indicates that we will never receive
122 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
123 } else if (session
->new_streams_needed
) {
124 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
126 session
->new_streams_needed
= true;
127 status
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
132 if (trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
137 * Open a new write only file handle to populate the `metadata_buf`
138 * memory buffer so we can write in loop in it easily.
140 fp
= bt_open_memstream(&metadata_buf
, &size
);
142 if (errno
== EINTR
&& 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
, "Metadata open_memstream", ".");
147 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
152 keep_receiving
= true;
153 /* Grab all available metadata. */
154 while (keep_receiving
) {
155 size_t reply_len
= 0;
157 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
158 * for new metadata. If new metadata is received, the function
159 * writes it to the provided file handle and updates the
160 * reply_len output parameter. We call this function in loop
161 * until it returns _END meaning that no new metadata is
163 * We may receive a _CLOSED status if the metadata stream we
164 * are requesting is no longer available on the relay.
165 * If we receive an _ERROR status, it means there was a
166 * networking, allocating, or some other unrecoverable error.
168 metadata_status
= lttng_live_get_one_metadata_packet(trace
, fp
, &reply_len
);
170 switch (metadata_status
) {
171 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
:
172 len_read
+= reply_len
;
174 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
:
175 keep_receiving
= false;
177 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
:
178 BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
179 "trace-id=%" PRIu64
", metadata-stream-id=%" PRIu64
,
180 trace
->id
, metadata
->stream_id
);
182 * The stream was closed and we received everything
183 * there was to receive for this metadata stream.
184 * We go on with the decoding of what we received. So
185 * that data stream can be decoded.
187 keep_receiving
= false;
188 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
;
190 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
:
191 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
192 "Error getting one trace metadata packet: "
201 /* The memory buffer `metadata_buf` contains all the metadata. */
202 if (bt_close_memstream(&metadata_buf
, &size
, fp
)) {
203 BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", ".");
210 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
214 /* The relay sent zero bytes of metdata. */
215 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
220 * Open a new reading file handle on the `metadata_buf` and pass it to
221 * the metadata decoder.
223 fp
= bt_fmemopen(metadata_buf
, len_read
, "rb");
225 if (errno
== EINTR
&& lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
226 session
->lttng_live_msg_iter
->was_interrupted
= true;
227 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
229 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
, "Cannot memory-open metadata buffer", ".");
230 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
236 * The call to ctf_metadata_decoder_append_content() will append
237 * new metadata to our current trace class.
239 BT_COMP_LOGD("Appending new metadata to the ctf_trace class");
240 decoder_status
= ctf_metadata_decoder_append_content(metadata
->decoder
, fp
);
241 switch (decoder_status
) {
242 case CTF_METADATA_DECODER_STATUS_OK
:
243 if (!trace
->trace_class
) {
244 struct ctf_trace_class
*tc
=
245 ctf_metadata_decoder_borrow_ctf_trace_class(metadata
->decoder
);
247 trace
->trace_class
= ctf_metadata_decoder_get_ir_trace_class(metadata
->decoder
);
248 trace
->trace
= bt_trace_create(trace
->trace_class
);
250 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create bt_trace");
253 if (ctf_trace_class_configure_ir_trace(tc
, trace
->trace
)) {
254 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to configure ctf trace class");
257 if (!stream_classes_all_have_default_clock_class(trace
->trace_class
, log_level
,
259 /* Error logged in function. */
262 trace
->clock_class
= borrow_any_clock_class(trace
->trace_class
);
265 /* The metadata was updated succesfully. */
266 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
276 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
281 closeret
= fclose(fp
);
283 BT_COMP_LOGW_ERRNO("Error on fclose", ".");
290 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
, uint64_t ctf_trace_id
,
291 uint64_t stream_id
, const char *)
293 bt_self_component
*self_comp
= session
->self_comp
;
294 bt_logging_level log_level
= session
->log_level
;
295 struct lttng_live_metadata
*metadata
= NULL
;
296 struct lttng_live_trace
*trace
;
298 ctf_metadata_decoder_config cfg
{};
299 cfg
.log_level
= session
->log_level
;
300 cfg
.self_comp
= session
->self_comp
;
301 cfg
.clock_class_offset_s
= 0;
302 cfg
.clock_class_offset_ns
= 0;
303 cfg
.create_trace_class
= true;
305 metadata
= g_new0(struct lttng_live_metadata
, 1);
309 metadata
->log_level
= session
->log_level
;
310 metadata
->self_comp
= session
->self_comp
;
311 metadata
->stream_id
= stream_id
;
313 metadata
->decoder
= ctf_metadata_decoder_create(&cfg
);
314 if (!metadata
->decoder
) {
315 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to create CTF metadata decoder");
318 trace
= lttng_live_session_borrow_or_create_trace_by_id(session
, ctf_trace_id
);
320 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to borrow trace");
323 trace
->metadata
= metadata
;
327 ctf_metadata_decoder_destroy(metadata
->decoder
);
332 void lttng_live_metadata_fini(struct lttng_live_trace
*trace
)
334 struct lttng_live_metadata
*metadata
= trace
->metadata
;
339 ctf_metadata_decoder_destroy(metadata
->decoder
);
340 trace
->metadata
= NULL
;