d1e3ae02a3b7ca7b1b0a58b06f50fd76ea62bc01
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.c
1 /*
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
5 *
6 * Some functions are based on older functions written by Mathieu Desnoyers.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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
24 * SOFTWARE.
25 */
26
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"
31
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <glib.h>
37 #include "compat/memstream.h"
38 #include <babeltrace2/babeltrace.h>
39
40 #include "metadata.h"
41 #include "../common/metadata/decoder.h"
42 #include "../common/metadata/ctf-meta-configure-ir-trace.h"
43
44 #define TSDL_MAGIC 0x75d11d57
45
46 struct packet_header {
47 uint32_t magic;
48 uint8_t uuid[16];
49 uint32_t checksum;
50 uint32_t content_size;
51 uint32_t packet_size;
52 uint8_t compression_scheme;
53 uint8_t encryption_scheme;
54 uint8_t checksum_scheme;
55 uint8_t major;
56 uint8_t minor;
57 } __attribute__((__packed__));
58
59
60 static
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)
64 {
65 uint64_t i, sc_count;
66 const bt_clock_class *cc = NULL;
67 const bt_stream_class *sc;
68 bool ret = true;
69
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);
73
74 BT_ASSERT(sc);
75
76 cc = bt_stream_class_borrow_default_clock_class_const(sc);
77 if (!cc) {
78 ret = false;
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));
84 goto end;
85 }
86 }
87
88 end:
89 return ret;
90 }
91 /*
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.
95 */
96 static
97 const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc)
98 {
99 uint64_t i, sc_count;
100 const bt_clock_class *cc = NULL;
101 const bt_stream_class *sc;
102
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);
106 BT_ASSERT_DBG(sc);
107
108 cc = bt_stream_class_borrow_default_clock_class_const(sc);
109 if (cc) {
110 goto end;
111 }
112 }
113 end:
114 BT_ASSERT_DBG(cc);
115 return cc;
116 }
117
118 BT_HIDDEN
119 enum lttng_live_iterator_status lttng_live_metadata_update(
120 struct lttng_live_trace *trace)
121 {
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;
126 bool keep_receiving;
127 FILE *fp = 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;
134
135 BT_COMP_LOGD("Updating metadata for trace: trace-id=%"PRIu64, trace->id);
136
137 /* No metadata stream yet. */
138 if (!metadata) {
139 if (session->new_streams_needed) {
140 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
141 } else {
142 session->new_streams_needed = true;
143 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
144 }
145 goto end;
146 }
147
148 if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) {
149 goto end;
150 }
151
152 /*
153 * Open a new write only file handle to populate the `metadata_buf`
154 * memory buffer so we can write in loop in it easily.
155 */
156 fp = bt_open_memstream(&metadata_buf, &size);
157 if (!fp) {
158 if (errno == EINTR &&
159 lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
160 session->lttng_live_msg_iter->was_interrupted = true;
161 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
162 } else {
163 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
164 "Metadata open_memstream", ".");
165 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
166 }
167 goto end;
168 }
169
170 keep_receiving = true;
171 /* Grab all available metadata. */
172 while (keep_receiving) {
173 size_t reply_len = 0;
174 /*
175 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
176 * for new metadata. If new metadata is received, the function
177 * writes it to the provided file handle and updates the
178 * reply_len output parameter. We call this function in loop
179 * until it returns _END meaning that no new metadata is
180 * available.
181 * We may receive a _CLOSED status if the metadata stream we
182 * are requesting is no longer available on the relay.
183 * If we receive an _ERROR status, it means there was a
184 * networking, allocating, or some other unrecoverable error.
185 */
186 metadata_status = lttng_live_get_one_metadata_packet(trace, fp,
187 &reply_len);
188
189 switch (metadata_status) {
190 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK:
191 len_read += reply_len;
192 break;
193 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END:
194 keep_receiving = false;
195 break;
196 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED:
197 BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
198 "trace-id=%"PRIu64", metadata-stream-id=%"PRIu64,
199 trace->id, metadata->stream_id);
200 /*
201 * The stream was closed and we received everything
202 * there was to receive for this metadata stream.
203 * We go on with the decoding of what we received. So
204 * that data stream can be decoded.
205 */
206 keep_receiving = false;
207 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED;
208 break;
209 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR:
210 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
211 "Error getting one trace metadata packet: "
212 "trace-id=%"PRIu64, trace->id);
213 goto error;
214 default:
215 bt_common_abort();
216 }
217 }
218
219 /* The memory buffer `metadata_buf` contains all the metadata. */
220 if (bt_close_memstream(&metadata_buf, &size, fp)) {
221 BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", ".");
222 }
223
224 fp = NULL;
225
226 if (len_read == 0) {
227 if (!trace->trace) {
228 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
229 goto end;
230 }
231
232 /* The relay sent zero bytes of metdata. */
233 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
234 goto end;
235 }
236
237 /*
238 * Open a new reading file handle on the `metadata_buf` and pass it to
239 * the metadata decoder.
240 */
241 fp = bt_fmemopen(metadata_buf, len_read, "rb");
242 if (!fp) {
243 if (errno == EINTR &&
244 lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
245 session->lttng_live_msg_iter->was_interrupted = true;
246 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
247 } else {
248 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
249 "Cannot memory-open metadata buffer", ".");
250 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
251 }
252 goto end;
253 }
254
255 /*
256 * The call to ctf_metadata_decoder_append_content() will append
257 * new metadata to our current trace class.
258 */
259 BT_COMP_LOGD("Appending new metadata to the ctf_trace class");
260 decoder_status = ctf_metadata_decoder_append_content(
261 metadata->decoder, fp);
262 switch (decoder_status) {
263 case CTF_METADATA_DECODER_STATUS_OK:
264 if (!trace->trace_class) {
265 struct ctf_trace_class *tc =
266 ctf_metadata_decoder_borrow_ctf_trace_class(
267 metadata->decoder);
268
269 trace->trace_class =
270 ctf_metadata_decoder_get_ir_trace_class(
271 metadata->decoder);
272 trace->trace = bt_trace_create(trace->trace_class);
273 if (!trace->trace) {
274 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
275 "Failed to create bt_trace");
276 goto error;
277 }
278 if (ctf_trace_class_configure_ir_trace(tc,
279 trace->trace)) {
280 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
281 "Failed to configure ctf trace class");
282 goto error;
283 }
284 if (!stream_classes_all_have_default_clock_class(
285 trace->trace_class, log_level,
286 self_comp)) {
287 /* Error logged in function. */
288 goto error;
289 }
290 trace->clock_class =
291 borrow_any_clock_class(trace->trace_class);
292 }
293
294 /* The metadata was updated succesfully. */
295 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
296
297 break;
298 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
299 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
300 break;
301 default:
302 goto error;
303 }
304
305 goto end;
306
307 error:
308 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
309 end:
310 if (fp) {
311 int closeret;
312
313 closeret = fclose(fp);
314 if (closeret) {
315 BT_COMP_LOGW_ERRNO("Error on fclose", ".");
316 }
317 }
318 free(metadata_buf);
319 return status;
320 }
321
322 BT_HIDDEN
323 int lttng_live_metadata_create_stream(struct lttng_live_session *session,
324 uint64_t ctf_trace_id, uint64_t stream_id,
325 const char *trace_name)
326 {
327 bt_self_component *self_comp = session->self_comp;
328 bt_logging_level log_level = session->log_level;
329 struct lttng_live_metadata *metadata = NULL;
330 struct lttng_live_trace *trace;
331 struct ctf_metadata_decoder_config cfg = {
332 .log_level = session->log_level,
333 .self_comp = session->self_comp,
334 .clock_class_offset_s = 0,
335 .clock_class_offset_ns = 0,
336 .create_trace_class = true,
337 };
338
339 metadata = g_new0(struct lttng_live_metadata, 1);
340 if (!metadata) {
341 return -1;
342 }
343 metadata->log_level = session->log_level;
344 metadata->self_comp = session->self_comp;
345 metadata->stream_id = stream_id;
346
347 metadata->decoder = ctf_metadata_decoder_create(&cfg);
348 if (!metadata->decoder) {
349 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
350 "Failed to create CTF metadata decoder");
351 goto error;
352 }
353 trace = lttng_live_session_borrow_or_create_trace_by_id(session,
354 ctf_trace_id);
355 if (!trace) {
356 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
357 "Failed to borrow trace");
358 goto error;
359 }
360 trace->metadata = metadata;
361 return 0;
362
363 error:
364 ctf_metadata_decoder_destroy(metadata->decoder);
365 g_free(metadata);
366 return -1;
367 }
368
369 BT_HIDDEN
370 void lttng_live_metadata_fini(struct lttng_live_trace *trace)
371 {
372 struct lttng_live_metadata *metadata = trace->metadata;
373
374 if (!metadata) {
375 return;
376 }
377 ctf_metadata_decoder_destroy(metadata->decoder);
378 trace->metadata = NULL;
379 g_free(metadata);
380 }
This page took 0.035821 seconds and 4 git commands to generate.