8475c569633bd022170793dbf5003a1118a90d2d
[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 "plugins/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("Stream class doesn't have a default clock class: "
80 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
81 bt_stream_class_get_id(sc),
82 bt_stream_class_get_name(sc));
83 goto end;
84 }
85 }
86
87 end:
88 return ret;
89 }
90 /*
91 * Iterate over the stream classes and returns the first clock class
92 * encountered. This is useful to create message iterator inactivity message as
93 * we don't need a particular clock class.
94 */
95 static
96 const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc)
97 {
98 uint64_t i, sc_count;
99 const bt_clock_class *cc = NULL;
100 const bt_stream_class *sc;
101
102 sc_count = bt_trace_class_get_stream_class_count(tc);
103 for (i = 0; i < sc_count; i++) {
104 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
105 BT_ASSERT(sc);
106
107 cc = bt_stream_class_borrow_default_clock_class_const(sc);
108 if (cc) {
109 goto end;
110 }
111 }
112 end:
113 BT_ASSERT(cc);
114 return cc;
115 }
116
117 BT_HIDDEN
118 enum lttng_live_iterator_status lttng_live_metadata_update(
119 struct lttng_live_trace *trace)
120 {
121 struct lttng_live_session *session = trace->session;
122 struct lttng_live_metadata *metadata = trace->metadata;
123 struct lttng_live_component *lttng_live =
124 session->lttng_live_msg_iter->lttng_live_comp;
125 ssize_t ret = 0;
126 size_t size, len_read = 0;
127 char *metadata_buf = NULL;
128 FILE *fp = NULL;
129 enum ctf_metadata_decoder_status decoder_status;
130 enum lttng_live_iterator_status status =
131 LTTNG_LIVE_ITERATOR_STATUS_OK;
132 bt_logging_level log_level = trace->log_level;
133 bt_self_component *self_comp = trace->self_comp;
134
135 /* No metadata stream yet. */
136 if (!metadata) {
137 if (session->new_streams_needed) {
138 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
139 } else {
140 session->new_streams_needed = true;
141 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
142 }
143 goto end;
144 }
145
146 if (!metadata->trace) {
147 trace->new_metadata_needed = false;
148 }
149
150 if (!trace->new_metadata_needed) {
151 goto end;
152 }
153
154 /* Open for writing */
155 fp = bt_open_memstream(&metadata_buf, &size);
156 if (!fp) {
157 BT_COMP_LOGE("Metadata open_memstream: %s", strerror(errno));
158 goto error;
159 }
160
161 /* Grab all available metadata. */
162 do {
163 /*
164 * get_one_metadata_packet returns the number of bytes
165 * received, 0 when we have received everything, a
166 * negative value on error.
167 */
168 ret = lttng_live_get_one_metadata_packet(trace, fp);
169 if (ret > 0) {
170 len_read += ret;
171 }
172 } while (ret > 0);
173
174 /*
175 * Consider metadata closed as soon as we get an error reading
176 * it (e.g. cannot be found).
177 */
178 if (ret < 0) {
179 if (!metadata->closed) {
180 metadata->closed = true;
181 /*
182 * Release our reference on the trace as soon as
183 * we know the metadata stream is not available
184 * anymore. This won't necessarily teardown the
185 * metadata objects immediately, but only when
186 * the data streams are done.
187 */
188 metadata->trace = NULL;
189 }
190 if (errno == EINTR) {
191 if (lttng_live_graph_is_canceled(lttng_live)) {
192 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
193 goto end;
194 }
195 }
196 }
197
198 if (bt_close_memstream(&metadata_buf, &size, fp)) {
199 BT_COMP_LOGE("bt_close_memstream: %s", strerror(errno));
200 }
201 ret = 0;
202 fp = NULL;
203
204 if (len_read == 0) {
205 if (!trace->trace) {
206 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
207 goto end;
208 }
209 trace->new_metadata_needed = false;
210 goto end;
211 }
212
213 fp = bt_fmemopen(metadata_buf, len_read, "rb");
214 if (!fp) {
215 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s",
216 strerror(errno));
217 goto error;
218 }
219
220 /*
221 * The call to ctf_metadata_decoder_decode will append new metadata to
222 * our current trace class.
223 */
224 decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp);
225 switch (decoder_status) {
226 case CTF_METADATA_DECODER_STATUS_OK:
227 if (!trace->trace_class) {
228 struct ctf_trace_class *tc =
229 ctf_metadata_decoder_borrow_ctf_trace_class(
230 metadata->decoder);
231
232 trace->trace_class =
233 ctf_metadata_decoder_get_ir_trace_class(
234 metadata->decoder);
235 trace->trace = bt_trace_create(trace->trace_class);
236 if (!trace->trace) {
237 goto error;
238 }
239 if (ctf_trace_class_configure_ir_trace(tc,
240 trace->trace)) {
241 goto error;
242 }
243 if (!stream_classes_all_have_default_clock_class(
244 trace->trace_class, log_level,
245 self_comp)) {
246 /* Error logged in function. */
247 goto error;
248 }
249 trace->clock_class =
250 borrow_any_clock_class(trace->trace_class);
251 }
252 trace->new_metadata_needed = false;
253
254 break;
255 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
256 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
257 break;
258 case CTF_METADATA_DECODER_STATUS_ERROR:
259 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION:
260 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR:
261 goto error;
262 }
263
264 goto end;
265 error:
266 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
267 end:
268 if (fp) {
269 int closeret;
270
271 closeret = fclose(fp);
272 if (closeret) {
273 BT_COMP_LOGE("Error on fclose");
274 }
275 }
276 free(metadata_buf);
277 return status;
278 }
279
280 BT_HIDDEN
281 int lttng_live_metadata_create_stream(struct lttng_live_session *session,
282 uint64_t ctf_trace_id, uint64_t stream_id,
283 const char *trace_name)
284 {
285 struct lttng_live_metadata *metadata = NULL;
286 struct lttng_live_trace *trace;
287 const char *match;
288 struct ctf_metadata_decoder_config cfg = {
289 .log_level = session->log_level,
290 .self_comp = session->self_comp,
291 .clock_class_offset_s = 0,
292 .clock_class_offset_ns = 0,
293 };
294
295 metadata = g_new0(struct lttng_live_metadata, 1);
296 if (!metadata) {
297 return -1;
298 }
299 metadata->log_level = session->log_level;
300 metadata->self_comp = session->self_comp;
301 metadata->stream_id = stream_id;
302
303 match = strstr(trace_name, session->session_name->str);
304 if (!match) {
305 goto error;
306 }
307
308 metadata->decoder = ctf_metadata_decoder_create(&cfg);
309 if (!metadata->decoder) {
310 goto error;
311 }
312 trace = lttng_live_borrow_trace(session, ctf_trace_id);
313 if (!trace) {
314 goto error;
315 }
316 metadata->trace = trace;
317 trace->metadata = metadata;
318 return 0;
319
320 error:
321 ctf_metadata_decoder_destroy(metadata->decoder);
322 g_free(metadata);
323 return -1;
324 }
325
326 BT_HIDDEN
327 void lttng_live_metadata_fini(struct lttng_live_trace *trace)
328 {
329 struct lttng_live_metadata *metadata = trace->metadata;
330
331 if (!metadata) {
332 return;
333 }
334 ctf_metadata_decoder_destroy(metadata->decoder);
335 trace->metadata = NULL;
336 g_free(metadata);
337 }
This page took 0.035246 seconds and 3 git commands to generate.