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 #include "compat/memstream.h"
11 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
12 #include "../common/src/metadata/tsdl/decoder.hpp"
13 #include "lttng-live.hpp"
14 #include "metadata.hpp"
16 #define TSDL_MAGIC 0x75d11d57
23 uint32_t content_size
;
25 uint8_t compression_scheme
;
26 uint8_t encryption_scheme
;
27 uint8_t checksum_scheme
;
30 } __attribute__((__packed__
));
32 static bool stream_classes_all_have_default_clock_class(bt_trace_class
*tc
,
33 const bt2c::Logger
& logger
)
36 const bt_clock_class
*cc
= NULL
;
37 const bt_stream_class
*sc
;
40 sc_count
= bt_trace_class_get_stream_class_count(tc
);
41 for (i
= 0; i
< sc_count
; i
++) {
42 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
46 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
49 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
,
50 "Stream class doesn't have a default clock class: "
51 "sc-id={}, sc-name=\"{}\"",
52 bt_stream_class_get_id(sc
), bt_stream_class_get_name(sc
));
61 * Iterate over the stream classes and returns the first clock class
62 * encountered. This is useful to create message iterator inactivity message as
63 * we don't need a particular clock class.
65 static const bt_clock_class
*borrow_any_clock_class(bt_trace_class
*tc
)
68 const bt_clock_class
*cc
= NULL
;
69 const bt_stream_class
*sc
;
71 sc_count
= bt_trace_class_get_stream_class_count(tc
);
72 for (i
= 0; i
< sc_count
; i
++) {
73 sc
= bt_trace_class_borrow_stream_class_by_index_const(tc
, i
);
76 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
86 enum lttng_live_iterator_status
lttng_live_metadata_update(struct lttng_live_trace
*trace
)
88 struct lttng_live_session
*session
= trace
->session
;
89 struct lttng_live_metadata
*metadata
= trace
->metadata
;
90 size_t size
, len_read
= 0;
91 char *metadata_buf
= NULL
;
94 enum ctf_metadata_decoder_status decoder_status
;
95 enum lttng_live_iterator_status status
= LTTNG_LIVE_ITERATOR_STATUS_OK
;
96 enum lttng_live_get_one_metadata_status metadata_status
;
98 BT_CPPLOGD_SPEC(metadata
->logger
, "Updating metadata for trace: session-id={}, trace-id={}",
99 session
->id
, trace
->id
);
101 /* No metadata stream yet. */
103 if (session
->closed
) {
105 * The session is closed AND we never received any
106 * metadata this indicates that we will never receive
109 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
110 } else if (session
->new_streams_needed
) {
111 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
113 session
->new_streams_needed
= true;
114 status
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
119 if (trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
124 * Open a new write only file handle to populate the `metadata_buf`
125 * memory buffer so we can write in loop in it easily.
127 fp
= bt_open_memstream(&metadata_buf
, &size
);
129 if (errno
== EINTR
&& lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
130 session
->lttng_live_msg_iter
->was_interrupted
= true;
131 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
133 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(metadata
->logger
, "Metadata open_memstream", ".");
134 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
139 keep_receiving
= true;
140 /* Grab all available metadata. */
141 while (keep_receiving
) {
142 size_t reply_len
= 0;
144 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
145 * for new metadata. If new metadata is received, the function
146 * writes it to the provided file handle and updates the
147 * reply_len output parameter. We call this function in loop
148 * until it returns _END meaning that no new metadata is
150 * We may receive a _CLOSED status if the metadata stream we
151 * are requesting is no longer available on the relay.
152 * If we receive an _ERROR status, it means there was a
153 * networking, allocating, or some other unrecoverable error.
155 metadata_status
= lttng_live_get_one_metadata_packet(trace
, fp
, &reply_len
);
157 switch (metadata_status
) {
158 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
:
159 len_read
+= reply_len
;
161 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
:
162 keep_receiving
= false;
164 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
:
167 "Metadata stream was closed by the Relay, the trace is no longer active: "
168 "trace-id={}, metadata-stream-id={}",
169 trace
->id
, metadata
->stream_id
);
171 * The stream was closed and we received everything
172 * there was to receive for this metadata stream.
173 * We go on with the decoding of what we received. So
174 * that data stream can be decoded.
176 keep_receiving
= false;
177 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
;
179 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
:
180 BT_CPPLOGE_APPEND_CAUSE_SPEC(metadata
->logger
,
181 "Error getting one trace metadata packet: trace-id={}",
189 /* The memory buffer `metadata_buf` contains all the metadata. */
190 if (bt_close_memstream(&metadata_buf
, &size
, fp
)) {
191 BT_CPPLOGW_ERRNO_SPEC(metadata
->logger
, "Metadata bt_close_memstream", ".");
198 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
202 /* The relay sent zero bytes of metadata. */
203 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
208 * Open a new reading file handle on the `metadata_buf` and pass it to
209 * the metadata decoder.
211 fp
= bt_fmemopen(metadata_buf
, len_read
, "rb");
213 if (errno
== EINTR
&& lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
214 session
->lttng_live_msg_iter
->was_interrupted
= true;
215 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
217 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(metadata
->logger
,
218 "Cannot memory-open metadata buffer", ".");
219 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
225 * The call to ctf_metadata_decoder_append_content() will append
226 * new metadata to our current trace class.
228 BT_CPPLOGD_SPEC(metadata
->logger
, "Appending new metadata to the ctf_trace class");
229 decoder_status
= ctf_metadata_decoder_append_content(metadata
->decoder
.get(), fp
);
230 switch (decoder_status
) {
231 case CTF_METADATA_DECODER_STATUS_OK
:
232 if (!trace
->trace_class
) {
233 struct ctf_trace_class
*tc
=
234 ctf_metadata_decoder_borrow_ctf_trace_class(metadata
->decoder
.get());
236 trace
->trace_class
= ctf_metadata_decoder_get_ir_trace_class(metadata
->decoder
.get());
237 trace
->trace
= bt_trace_create(trace
->trace_class
);
239 BT_CPPLOGE_APPEND_CAUSE_SPEC(metadata
->logger
, "Failed to create bt_trace");
242 if (ctf_trace_class_configure_ir_trace(tc
, trace
->trace
)) {
243 BT_CPPLOGE_APPEND_CAUSE_SPEC(metadata
->logger
,
244 "Failed to configure ctf trace class");
247 if (!stream_classes_all_have_default_clock_class(trace
->trace_class
,
249 /* Error logged in function. */
252 trace
->clock_class
= borrow_any_clock_class(trace
->trace_class
);
255 /* The metadata was updated successfully. */
256 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
266 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
271 closeret
= fclose(fp
);
273 BT_CPPLOGW_ERRNO_SPEC(metadata
->logger
, "Error on fclose", ".");
280 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
, uint64_t ctf_trace_id
,
283 struct lttng_live_trace
*trace
;
285 ctf_metadata_decoder_config cfg
{session
->logger
};
286 cfg
.self_comp
= session
->self_comp
;
287 cfg
.clock_class_offset_s
= 0;
288 cfg
.clock_class_offset_ns
= 0;
289 cfg
.create_trace_class
= true;
291 lttng_live_metadata
*metadata
= new lttng_live_metadata
{session
->logger
};
292 metadata
->stream_id
= stream_id
;
294 metadata
->decoder
= ctf_metadata_decoder_create(&cfg
);
295 if (!metadata
->decoder
) {
296 BT_CPPLOGE_APPEND_CAUSE_SPEC(session
->logger
, "Failed to create CTF metadata decoder");
299 trace
= lttng_live_session_borrow_or_create_trace_by_id(session
, ctf_trace_id
);
301 BT_CPPLOGE_APPEND_CAUSE_SPEC(session
->logger
, "Failed to borrow trace");
304 trace
->metadata
= metadata
;
312 void lttng_live_metadata_fini(struct lttng_live_trace
*trace
)
314 struct lttng_live_metadata
*metadata
= trace
->metadata
;
319 trace
->metadata
= NULL
;