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_CLOG_CFG logCfg
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
17 #include "compat/memstream.h"
18 #include <babeltrace2/babeltrace.h>
20 #include "cpp-common/libc-up.hpp"
22 #include "metadata.hpp"
23 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
24 #include "cpp-common/cfg-logging.hpp"
25 #include "cpp-common/cfg-logging-error-reporting.hpp"
27 #define TSDL_MAGIC 0x75d11d57
34 uint32_t content_size
;
36 uint8_t compression_scheme
;
37 uint8_t encryption_scheme
;
38 uint8_t checksum_scheme
;
41 } __attribute__((__packed__
));
43 static bool stream_classes_all_have_default_clock_class(bt2::ConstTraceClass tc
,
44 const bt2_common::LogCfg
& logCfg
)
46 for (std::uint64_t i
= 0; i
< tc
.size(); ++i
) {
47 bt2::ConstStreamClass sc
= tc
[i
];
48 nonstd::optional
<bt2::ConstClockClass
> cc
= sc
.defaultClockClass();
51 BT_CLOGE_APPEND_CAUSE("Stream class doesn't have a default clock class: "
52 "sc-id=%" PRIu64
", sc-name=\"%s\"",
53 sc
.id(), sc
.name()->c_str());
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 bt2::ConstClockClass
borrow_any_clock_class(bt2::ConstTraceClass tc
)
67 return *tc
[0].defaultClockClass();
71 enum lttng_live_iterator_status
lttng_live_metadata_update(struct lttng_live_trace
*trace
)
73 struct lttng_live_session
*session
= trace
->session
;
74 struct lttng_live_metadata
*metadata
= trace
->metadata
.get();
76 const bt2_common::LogCfg
& logCfg
= trace
->logCfg
;
77 enum lttng_live_get_one_metadata_status metadata_status
;
79 BT_CLOGD("Updating metadata for trace: session-id=%" PRIu64
", trace-id=%" PRIu64
, session
->id
,
82 /* No metadata stream yet. */
84 if (session
->closed
) {
86 * The session is closed AND we never received any
87 * metadata this indicates that we will never receive
90 return LTTNG_LIVE_ITERATOR_STATUS_END
;
91 } else if (session
->new_streams_needed
) {
92 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
94 session
->new_streams_needed
= true;
95 return LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
99 if (trace
->metadata_stream_state
!= LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED
) {
100 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
103 keep_receiving
= true;
104 /* Grab all available metadata. */
105 std::vector
<uint8_t> metadataBuf
;
106 while (keep_receiving
) {
108 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
109 * for new metadata. If new metadata is received, the function
110 * writes it to the provided file handle and updates the
111 * reply_len output parameter. We call this function in loop
112 * until it returns _END meaning that no new metadata is
114 * We may receive a _CLOSED status if the metadata stream we
115 * are requesting is no longer available on the relay.
116 * If we receive an _ERROR status, it means there was a
117 * networking, allocating, or some other unrecoverable error.
119 metadata_status
= lttng_live_get_one_metadata_packet(trace
, metadataBuf
);
121 switch (metadata_status
) {
122 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
:
124 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
:
125 keep_receiving
= false;
127 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
:
128 BT_CLOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
129 "trace-id=%" PRIu64
", metadata-stream-id=%" PRIu64
,
130 trace
->id
, metadata
->stream_id
);
132 * The stream was closed and we received everything
133 * there was to receive for this metadata stream.
134 * We go on with the decoding of what we received. So
135 * that data stream can be decoded.
137 keep_receiving
= false;
138 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED
;
140 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
:
141 BT_CLOGE_APPEND_CAUSE("Error getting one trace metadata packet: "
144 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
150 if (metadataBuf
.empty()) {
152 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
155 /* The relay sent zero bytes of metdata. */
156 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
157 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
161 * The call to ctf_metadata_decoder_append_content() will append
162 * new metadata to our current trace class.
164 BT_CLOGD("Appending new metadata to the ctf_trace class");
165 metadata
->parseSection(metadataBuf
.data(), metadataBuf
.data() + metadataBuf
.size());
167 const ctf::src::TraceCls
*ctfTraceCls
= metadata
->traceCls();
168 BT_ASSERT(ctfTraceCls
);
169 nonstd::optional
<bt2::TraceClass
> irTraceCls
= ctfTraceCls
->libCls();
172 trace
->trace
= irTraceCls
->instantiate();
174 ctf_trace_class_configure_ir_trace(*ctfTraceCls
, **trace
->trace
, logCfg
);
176 if (!stream_classes_all_have_default_clock_class((*trace
->trace
)->cls(), logCfg
)) {
177 /* Error logged in function. */
178 return LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
181 trace
->clock_class
= borrow_any_clock_class((*trace
->trace
)->cls());
185 /* The metadata was updated succesfully. */
186 trace
->metadata_stream_state
= LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED
;
188 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
192 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
, uint64_t ctf_trace_id
,
193 uint64_t stream_id
, const char *trace_name
)
195 const bt2_common::LogCfg
& logCfg
= session
->logCfg
;
196 struct lttng_live_trace
*trace
;
197 lttng_live_metadata::UP metadata
=
198 bt2_common::makeUnique
<lttng_live_metadata
>(session
->self_comp
, logCfg
);
200 metadata
->stream_id
= stream_id
;
202 trace
= lttng_live_session_borrow_or_create_trace_by_id(session
, ctf_trace_id
);
204 BT_CLOGE_APPEND_CAUSE("Failed to borrow trace");
207 trace
->metadata
= std::move(metadata
);