2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
3 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
6 * Some functions are based on older functions written by Mathieu Desnoyers.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #define BT_COMP_LOG_SELF_COMP self_comp
28 #define BT_LOG_OUTPUT_LEVEL log_level
29 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
30 #include "logging/comp-logging.h"
37 #include "compat/memstream.h"
38 #include <babeltrace2/babeltrace.h>
41 #include "../common/metadata/decoder.h"
42 #include "../common/metadata/ctf-meta-configure-ir-trace.h"
44 #define TSDL_MAGIC 0x75d11d57
46 struct packet_header
{
50 uint32_t content_size
;
52 uint8_t compression_scheme
;
53 uint8_t encryption_scheme
;
54 uint8_t checksum_scheme
;
57 } __attribute__((__packed__
));
61 bool stream_classes_all_have_default_clock_class(bt_trace_class
*tc
,
62 bt_logging_level log_level
,
63 bt_self_component
*self_comp
)
66 const bt_clock_class
*cc
= NULL
;
67 const bt_stream_class
*sc
;
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
);
76 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
79 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
80 "Stream class doesn't have a default clock class: "
81 "sc-id=%" PRIu64
", sc-name=\"%s\"",
82 bt_stream_class_get_id(sc
),
83 bt_stream_class_get_name(sc
));
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.
97 const bt_clock_class
*borrow_any_clock_class(bt_trace_class
*tc
)
100 const bt_clock_class
*cc
= NULL
;
101 const bt_stream_class
*sc
;
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
);
108 cc
= bt_stream_class_borrow_default_clock_class_const(sc
);
119 enum lttng_live_iterator_status
lttng_live_metadata_update(
120 struct lttng_live_trace
*trace
)
122 struct lttng_live_session
*session
= trace
->session
;
123 struct lttng_live_metadata
*metadata
= trace
->metadata
;
124 size_t size
, len_read
= 0;
125 char *metadata_buf
= NULL
;
128 enum ctf_metadata_decoder_status decoder_status
;
129 enum lttng_live_iterator_status status
=
130 LTTNG_LIVE_ITERATOR_STATUS_OK
;
131 bt_logging_level log_level
= trace
->log_level
;
132 bt_self_component
*self_comp
= trace
->self_comp
;
133 enum lttng_live_get_one_metadata_status metadata_status
;
135 /* No metadata stream yet. */
137 if (session
->new_streams_needed
) {
138 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
140 session
->new_streams_needed
= true;
141 status
= LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
146 if (!metadata
->trace
) {
147 trace
->new_metadata_needed
= false;
150 if (!trace
->new_metadata_needed
) {
154 /* Open for writing */
155 fp
= bt_open_memstream(&metadata_buf
, &size
);
157 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
158 "Metadata open_memstream", ".");
162 keep_receiving
= true;
163 /* Grab all available metadata. */
164 while (keep_receiving
) {
165 size_t reply_len
= 0;
167 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
168 * for new metadata. If new metadata is received, the function
169 * writes it to the provided file handle and updates the
170 * reply_len output parameter. We call this function in loop
171 * until it returns _END meaning that no new metadata is
173 * We may receive a _CLOSED status if the metadata stream we
174 * are requesting is no longer available on the relay.
175 * If we receive an _ERROR status, it means there was a
176 * networking, allocating, or some other unrecoverable error.
178 metadata_status
= lttng_live_get_one_metadata_packet(trace
, fp
,
181 switch (metadata_status
) {
182 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK
:
183 len_read
+= reply_len
;
185 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END
:
186 keep_receiving
= false;
188 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
:
189 keep_receiving
= false;
191 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR
:
199 * A closed metadata stream means the trace is no longer active. Return
200 * _END so that the caller can remove the trace from its list.
202 if (metadata_status
== LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED
) {
203 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
207 if (bt_close_memstream(&metadata_buf
, &size
, fp
)) {
208 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
209 "Metadata bt_close_memstream", ".");
216 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
219 trace
->new_metadata_needed
= false;
223 fp
= bt_fmemopen(metadata_buf
, len_read
, "rb");
225 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
226 "Cannot memory-open metadata buffer", ".");
231 * The call to ctf_metadata_decoder_append_content() will append
232 * new metadata to our current trace class.
234 decoder_status
= ctf_metadata_decoder_append_content(
235 metadata
->decoder
, fp
);
236 switch (decoder_status
) {
237 case CTF_METADATA_DECODER_STATUS_OK
:
238 if (!trace
->trace_class
) {
239 struct ctf_trace_class
*tc
=
240 ctf_metadata_decoder_borrow_ctf_trace_class(
244 ctf_metadata_decoder_get_ir_trace_class(
246 trace
->trace
= bt_trace_create(trace
->trace_class
);
248 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
249 "Failed to create bt_trace");
252 if (ctf_trace_class_configure_ir_trace(tc
,
254 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
255 "Failed to configure ctf trace class");
258 if (!stream_classes_all_have_default_clock_class(
259 trace
->trace_class
, log_level
,
261 /* Error logged in function. */
265 borrow_any_clock_class(trace
->trace_class
);
267 trace
->new_metadata_needed
= false;
270 case CTF_METADATA_DECODER_STATUS_INCOMPLETE
:
271 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
280 if (errno
== EINTR
) {
281 if (lttng_live_graph_is_canceled(session
->lttng_live_msg_iter
)) {
282 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
285 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
291 closeret
= fclose(fp
);
293 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp
,
294 "Error on fclose", ".");
302 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
,
303 uint64_t ctf_trace_id
, uint64_t stream_id
,
304 const char *trace_name
)
306 bt_self_component
*self_comp
= session
->self_comp
;
307 bt_logging_level log_level
= session
->log_level
;
308 struct lttng_live_metadata
*metadata
= NULL
;
309 struct lttng_live_trace
*trace
;
310 struct ctf_metadata_decoder_config cfg
= {
311 .log_level
= session
->log_level
,
312 .self_comp
= session
->self_comp
,
313 .clock_class_offset_s
= 0,
314 .clock_class_offset_ns
= 0,
315 .create_trace_class
= true,
318 metadata
= g_new0(struct lttng_live_metadata
, 1);
322 metadata
->log_level
= session
->log_level
;
323 metadata
->self_comp
= session
->self_comp
;
324 metadata
->stream_id
= stream_id
;
326 metadata
->decoder
= ctf_metadata_decoder_create(&cfg
);
327 if (!metadata
->decoder
) {
328 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
329 "Failed to create CTF metadata decoder");
332 trace
= lttng_live_borrow_trace(session
, ctf_trace_id
);
334 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
335 "Failed to borrow trace");
338 metadata
->trace
= trace
;
339 trace
->metadata
= metadata
;
343 ctf_metadata_decoder_destroy(metadata
->decoder
);
349 void lttng_live_metadata_fini(struct lttng_live_trace
*trace
)
351 struct lttng_live_metadata
*metadata
= trace
->metadata
;
356 ctf_metadata_decoder_destroy(metadata
->decoder
);
357 trace
->metadata
= NULL
;