ctf: allocate some structures with new
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
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
7 */
8
9 #include <glib.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include <babeltrace2/babeltrace.h>
15
16 #define BT_COMP_LOG_SELF_COMP self_comp
17 #define BT_LOG_OUTPUT_LEVEL log_level
18 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
19 #include "logging/comp-logging.h"
20
21 #include "compat/memstream.h"
22
23 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
24 #include "../common/src/metadata/tsdl/decoder.hpp"
25 #include "lttng-live.hpp"
26 #include "metadata.hpp"
27
28 #define TSDL_MAGIC 0x75d11d57
29
30 struct packet_header
31 {
32 uint32_t magic;
33 uint8_t uuid[16];
34 uint32_t checksum;
35 uint32_t content_size;
36 uint32_t packet_size;
37 uint8_t compression_scheme;
38 uint8_t encryption_scheme;
39 uint8_t checksum_scheme;
40 uint8_t major;
41 uint8_t minor;
42 } __attribute__((__packed__));
43
44 static bool stream_classes_all_have_default_clock_class(bt_trace_class *tc,
45 bt_logging_level log_level,
46 bt_self_component *self_comp)
47 {
48 uint64_t i, sc_count;
49 const bt_clock_class *cc = NULL;
50 const bt_stream_class *sc;
51 bool ret = true;
52
53 sc_count = bt_trace_class_get_stream_class_count(tc);
54 for (i = 0; i < sc_count; i++) {
55 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
56
57 BT_ASSERT(sc);
58
59 cc = bt_stream_class_borrow_default_clock_class_const(sc);
60 if (!cc) {
61 ret = false;
62 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
63 "Stream class doesn't have a default clock class: "
64 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
65 bt_stream_class_get_id(sc), bt_stream_class_get_name(sc));
66 goto end;
67 }
68 }
69
70 end:
71 return ret;
72 }
73 /*
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.
77 */
78 static const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc)
79 {
80 uint64_t i, sc_count;
81 const bt_clock_class *cc = NULL;
82 const bt_stream_class *sc;
83
84 sc_count = bt_trace_class_get_stream_class_count(tc);
85 for (i = 0; i < sc_count; i++) {
86 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
87 BT_ASSERT_DBG(sc);
88
89 cc = bt_stream_class_borrow_default_clock_class_const(sc);
90 if (cc) {
91 goto end;
92 }
93 }
94 end:
95 BT_ASSERT_DBG(cc);
96 return cc;
97 }
98
99 enum lttng_live_iterator_status lttng_live_metadata_update(struct lttng_live_trace *trace)
100 {
101 struct lttng_live_session *session = trace->session;
102 struct lttng_live_metadata *metadata = trace->metadata;
103 size_t size, len_read = 0;
104 char *metadata_buf = NULL;
105 bool keep_receiving;
106 FILE *fp = NULL;
107 enum ctf_metadata_decoder_status decoder_status;
108 enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK;
109 bt_logging_level log_level = trace->log_level;
110 bt_self_component *self_comp = trace->self_comp;
111 enum lttng_live_get_one_metadata_status metadata_status;
112
113 BT_COMP_LOGD("Updating metadata for trace: session-id=%" PRIu64 ", trace-id=%" PRIu64,
114 session->id, trace->id);
115
116 /* No metadata stream yet. */
117 if (!metadata) {
118 if (session->closed) {
119 /*
120 * The session is closed AND we never received any
121 * metadata this indicates that we will never receive
122 * any metadata.
123 */
124 status = LTTNG_LIVE_ITERATOR_STATUS_END;
125 } else if (session->new_streams_needed) {
126 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
127 } else {
128 session->new_streams_needed = true;
129 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
130 }
131 goto end;
132 }
133
134 if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) {
135 goto end;
136 }
137
138 /*
139 * Open a new write only file handle to populate the `metadata_buf`
140 * memory buffer so we can write in loop in it easily.
141 */
142 fp = bt_open_memstream(&metadata_buf, &size);
143 if (!fp) {
144 if (errno == EINTR && lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
145 session->lttng_live_msg_iter->was_interrupted = true;
146 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
147 } else {
148 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, "Metadata open_memstream", ".");
149 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
150 }
151 goto end;
152 }
153
154 keep_receiving = true;
155 /* Grab all available metadata. */
156 while (keep_receiving) {
157 size_t reply_len = 0;
158 /*
159 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
160 * for new metadata. If new metadata is received, the function
161 * writes it to the provided file handle and updates the
162 * reply_len output parameter. We call this function in loop
163 * until it returns _END meaning that no new metadata is
164 * available.
165 * We may receive a _CLOSED status if the metadata stream we
166 * are requesting is no longer available on the relay.
167 * If we receive an _ERROR status, it means there was a
168 * networking, allocating, or some other unrecoverable error.
169 */
170 metadata_status = lttng_live_get_one_metadata_packet(trace, fp, &reply_len);
171
172 switch (metadata_status) {
173 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK:
174 len_read += reply_len;
175 break;
176 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END:
177 keep_receiving = false;
178 break;
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);
183 /*
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.
188 */
189 keep_receiving = false;
190 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED;
191 break;
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,
196 trace->id);
197 goto error;
198 default:
199 bt_common_abort();
200 }
201 }
202
203 /* The memory buffer `metadata_buf` contains all the metadata. */
204 if (bt_close_memstream(&metadata_buf, &size, fp)) {
205 BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", ".");
206 }
207
208 fp = NULL;
209
210 if (len_read == 0) {
211 if (!trace->trace) {
212 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
213 goto end;
214 }
215
216 /* The relay sent zero bytes of metadata. */
217 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
218 goto end;
219 }
220
221 /*
222 * Open a new reading file handle on the `metadata_buf` and pass it to
223 * the metadata decoder.
224 */
225 fp = bt_fmemopen(metadata_buf, len_read, "rb");
226 if (!fp) {
227 if (errno == EINTR && 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;
230 } else {
231 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, "Cannot memory-open metadata buffer", ".");
232 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
233 }
234 goto end;
235 }
236
237 /*
238 * The call to ctf_metadata_decoder_append_content() will append
239 * new metadata to our current trace class.
240 */
241 BT_COMP_LOGD("Appending new metadata to the ctf_trace class");
242 decoder_status = ctf_metadata_decoder_append_content(metadata->decoder, fp);
243 switch (decoder_status) {
244 case CTF_METADATA_DECODER_STATUS_OK:
245 if (!trace->trace_class) {
246 struct ctf_trace_class *tc =
247 ctf_metadata_decoder_borrow_ctf_trace_class(metadata->decoder);
248
249 trace->trace_class = ctf_metadata_decoder_get_ir_trace_class(metadata->decoder);
250 trace->trace = bt_trace_create(trace->trace_class);
251 if (!trace->trace) {
252 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create bt_trace");
253 goto error;
254 }
255 if (ctf_trace_class_configure_ir_trace(tc, trace->trace)) {
256 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to configure ctf trace class");
257 goto error;
258 }
259 if (!stream_classes_all_have_default_clock_class(trace->trace_class, log_level,
260 self_comp)) {
261 /* Error logged in function. */
262 goto error;
263 }
264 trace->clock_class = borrow_any_clock_class(trace->trace_class);
265 }
266
267 /* The metadata was updated successfully. */
268 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
269
270 break;
271 default:
272 goto error;
273 }
274
275 goto end;
276
277 error:
278 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
279 end:
280 if (fp) {
281 int closeret;
282
283 closeret = fclose(fp);
284 if (closeret) {
285 BT_COMP_LOGW_ERRNO("Error on fclose", ".");
286 }
287 }
288 free(metadata_buf);
289 return status;
290 }
291
292 int lttng_live_metadata_create_stream(struct lttng_live_session *session, uint64_t ctf_trace_id,
293 uint64_t stream_id)
294 {
295 bt_self_component *self_comp = session->self_comp;
296 bt_logging_level log_level = session->log_level;
297 struct lttng_live_trace *trace;
298
299 ctf_metadata_decoder_config cfg;
300 cfg.log_level = session->log_level;
301 cfg.self_comp = session->self_comp;
302 cfg.clock_class_offset_s = 0;
303 cfg.clock_class_offset_ns = 0;
304 cfg.create_trace_class = true;
305
306 lttng_live_metadata *metadata = new lttng_live_metadata;
307 metadata->log_level = session->log_level;
308 metadata->self_comp = session->self_comp;
309 metadata->stream_id = stream_id;
310
311 metadata->decoder = ctf_metadata_decoder_create(&cfg);
312 if (!metadata->decoder) {
313 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create CTF metadata decoder");
314 goto error;
315 }
316 trace = lttng_live_session_borrow_or_create_trace_by_id(session, ctf_trace_id);
317 if (!trace) {
318 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to borrow trace");
319 goto error;
320 }
321 trace->metadata = metadata;
322 return 0;
323
324 error:
325 ctf_metadata_decoder_destroy(metadata->decoder);
326 delete metadata;
327 return -1;
328 }
329
330 void lttng_live_metadata_fini(struct lttng_live_trace *trace)
331 {
332 struct lttng_live_metadata *metadata = trace->metadata;
333
334 if (!metadata) {
335 return;
336 }
337 ctf_metadata_decoder_destroy(metadata->decoder);
338 trace->metadata = NULL;
339 delete metadata;
340 }
This page took 0.035582 seconds and 4 git commands to generate.