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