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