lib: add bt_{graph,query_executor}_add_interrupter()
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.c
CommitLineData
7cdc2bab 1/*
14f28187 2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
7cdc2bab
MD
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
2ece7dd0 27#define BT_COMP_LOG_SELF_COMP self_comp
c01594de 28#define BT_LOG_OUTPUT_LEVEL log_level
350ad6c1 29#define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
2ece7dd0 30#include "plugins/comp-logging.h"
020bc26f 31
7cdc2bab
MD
32#include <stdio.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <stdbool.h>
36#include <glib.h>
578e048b 37#include "compat/memstream.h"
3fadfbc0 38#include <babeltrace2/babeltrace.h>
7cdc2bab 39
7cdc2bab
MD
40#include "metadata.h"
41#include "../common/metadata/decoder.h"
335a2da5 42#include "../common/metadata/ctf-meta-configure-ir-trace.h"
7cdc2bab
MD
43
44#define TSDL_MAGIC 0x75d11d57
45
46struct 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
14f28187 59
7cdc2bab 60static
c01594de 61bool stream_classes_all_have_default_clock_class(bt_trace_class *tc,
2ece7dd0
PP
62 bt_logging_level log_level,
63 bt_self_component *self_comp)
7cdc2bab 64{
14f28187
FD
65 uint64_t i, sc_count;
66 const bt_clock_class *cc = NULL;
67 const bt_stream_class *sc;
68 bool ret = true;
7cdc2bab 69
14f28187
FD
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);
7cdc2bab 73
14f28187 74 BT_ASSERT(sc);
7cdc2bab 75
14f28187
FD
76 cc = bt_stream_class_borrow_default_clock_class_const(sc);
77 if (!cc) {
78 ret = false;
2ece7dd0 79 BT_COMP_LOGE("Stream class doesn't have a default clock class: "
14f28187
FD
80 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
81 bt_stream_class_get_id(sc),
82 bt_stream_class_get_name(sc));
83 goto end;
7cdc2bab
MD
84 }
85 }
86
7cdc2bab 87end:
14f28187
FD
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 */
95static
96const 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 }
112end:
113 BT_ASSERT(cc);
114 return cc;
7cdc2bab
MD
115}
116
117BT_HIDDEN
14f28187 118enum lttng_live_iterator_status lttng_live_metadata_update(
7cdc2bab
MD
119 struct lttng_live_trace *trace)
120{
121 struct lttng_live_session *session = trace->session;
7cdc2bab
MD
122 struct lttng_live_metadata *metadata = trace->metadata;
123 ssize_t ret = 0;
124 size_t size, len_read = 0;
125 char *metadata_buf = NULL;
126 FILE *fp = NULL;
127 enum ctf_metadata_decoder_status decoder_status;
14f28187
FD
128 enum lttng_live_iterator_status status =
129 LTTNG_LIVE_ITERATOR_STATUS_OK;
c01594de 130 bt_logging_level log_level = trace->log_level;
2ece7dd0 131 bt_self_component *self_comp = trace->self_comp;
7cdc2bab
MD
132
133 /* No metadata stream yet. */
134 if (!metadata) {
135 if (session->new_streams_needed) {
14f28187 136 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
137 } else {
138 session->new_streams_needed = true;
14f28187 139 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab
MD
140 }
141 goto end;
142 }
143
6f79a7cf
MD
144 if (!metadata->trace) {
145 trace->new_metadata_needed = false;
146 }
147
7cdc2bab
MD
148 if (!trace->new_metadata_needed) {
149 goto end;
150 }
151
152 /* Open for writing */
153 fp = bt_open_memstream(&metadata_buf, &size);
154 if (!fp) {
2ece7dd0 155 BT_COMP_LOGE("Metadata open_memstream: %s", strerror(errno));
7cdc2bab
MD
156 goto error;
157 }
158
159 /* Grab all available metadata. */
160 do {
161 /*
162 * get_one_metadata_packet returns the number of bytes
163 * received, 0 when we have received everything, a
164 * negative value on error.
165 */
166 ret = lttng_live_get_one_metadata_packet(trace, fp);
167 if (ret > 0) {
168 len_read += ret;
169 }
170 } while (ret > 0);
171
172 /*
173 * Consider metadata closed as soon as we get an error reading
174 * it (e.g. cannot be found).
175 */
176 if (ret < 0) {
177 if (!metadata->closed) {
178 metadata->closed = true;
179 /*
180 * Release our reference on the trace as soon as
181 * we know the metadata stream is not available
182 * anymore. This won't necessarily teardown the
183 * metadata objects immediately, but only when
184 * the data streams are done.
185 */
6f79a7cf 186 metadata->trace = NULL;
7cdc2bab 187 }
4c66436f 188 if (errno == EINTR) {
9b4f9b42
PP
189 if (lttng_live_graph_is_canceled(
190 session->lttng_live_msg_iter)) {
14f28187 191 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
4c66436f
MD
192 goto end;
193 }
194 }
7cdc2bab
MD
195 }
196
197 if (bt_close_memstream(&metadata_buf, &size, fp)) {
2ece7dd0 198 BT_COMP_LOGE("bt_close_memstream: %s", strerror(errno));
7cdc2bab
MD
199 }
200 ret = 0;
201 fp = NULL;
202
203 if (len_read == 0) {
204 if (!trace->trace) {
14f28187 205 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
206 goto end;
207 }
208 trace->new_metadata_needed = false;
209 goto end;
210 }
211
7cdc2bab
MD
212 fp = bt_fmemopen(metadata_buf, len_read, "rb");
213 if (!fp) {
2ece7dd0 214 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s",
7cdc2bab
MD
215 strerror(errno));
216 goto error;
217 }
218
14f28187
FD
219 /*
220 * The call to ctf_metadata_decoder_decode will append new metadata to
221 * our current trace class.
222 */
7cdc2bab
MD
223 decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp);
224 switch (decoder_status) {
225 case CTF_METADATA_DECODER_STATUS_OK:
14f28187 226 if (!trace->trace_class) {
335a2da5
PP
227 struct ctf_trace_class *tc =
228 ctf_metadata_decoder_borrow_ctf_trace_class(
229 metadata->decoder);
230
14f28187
FD
231 trace->trace_class =
232 ctf_metadata_decoder_get_ir_trace_class(
233 metadata->decoder);
234 trace->trace = bt_trace_create(trace->trace_class);
335a2da5
PP
235 if (!trace->trace) {
236 goto error;
237 }
238 if (ctf_trace_class_configure_ir_trace(tc,
239 trace->trace)) {
240 goto error;
241 }
14f28187 242 if (!stream_classes_all_have_default_clock_class(
2ece7dd0
PP
243 trace->trace_class, log_level,
244 self_comp)) {
14f28187
FD
245 /* Error logged in function. */
246 goto error;
247 }
248 trace->clock_class =
249 borrow_any_clock_class(trace->trace_class);
7cdc2bab 250 }
14f28187
FD
251 trace->new_metadata_needed = false;
252
7cdc2bab
MD
253 break;
254 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
14f28187 255 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
256 break;
257 case CTF_METADATA_DECODER_STATUS_ERROR:
258 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION:
259 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR:
260 goto error;
261 }
262
263 goto end;
264error:
14f28187 265 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
7cdc2bab
MD
266end:
267 if (fp) {
268 int closeret;
269
270 closeret = fclose(fp);
271 if (closeret) {
2ece7dd0 272 BT_COMP_LOGE("Error on fclose");
7cdc2bab
MD
273 }
274 }
6f79a7cf 275 free(metadata_buf);
7cdc2bab
MD
276 return status;
277}
278
279BT_HIDDEN
280int lttng_live_metadata_create_stream(struct lttng_live_session *session,
2ece7dd0 281 uint64_t ctf_trace_id, uint64_t stream_id,
06994c71 282 const char *trace_name)
7cdc2bab
MD
283{
284 struct lttng_live_metadata *metadata = NULL;
285 struct lttng_live_trace *trace;
06994c71 286 const char *match;
0746848c 287 struct ctf_metadata_decoder_config cfg = {
c01594de 288 .log_level = session->log_level,
2ece7dd0 289 .self_comp = session->self_comp,
0746848c
PP
290 .clock_class_offset_s = 0,
291 .clock_class_offset_ns = 0,
292 };
7cdc2bab
MD
293
294 metadata = g_new0(struct lttng_live_metadata, 1);
295 if (!metadata) {
296 return -1;
297 }
c01594de 298 metadata->log_level = session->log_level;
2ece7dd0 299 metadata->self_comp = session->self_comp;
7cdc2bab 300 metadata->stream_id = stream_id;
14f28187 301
06994c71
MD
302 match = strstr(trace_name, session->session_name->str);
303 if (!match) {
304 goto error;
305 }
14f28187 306
f7b785ac 307 metadata->decoder = ctf_metadata_decoder_create(&cfg);
7cdc2bab
MD
308 if (!metadata->decoder) {
309 goto error;
310 }
14f28187 311 trace = lttng_live_borrow_trace(session, ctf_trace_id);
7cdc2bab
MD
312 if (!trace) {
313 goto error;
314 }
315 metadata->trace = trace;
316 trace->metadata = metadata;
317 return 0;
318
319error:
320 ctf_metadata_decoder_destroy(metadata->decoder);
321 g_free(metadata);
322 return -1;
323}
324
325BT_HIDDEN
326void lttng_live_metadata_fini(struct lttng_live_trace *trace)
327{
328 struct lttng_live_metadata *metadata = trace->metadata;
329
330 if (!metadata) {
331 return;
332 }
7cdc2bab
MD
333 ctf_metadata_decoder_destroy(metadata->decoder);
334 trace->metadata = NULL;
7cdc2bab
MD
335 g_free(metadata);
336}
This page took 0.067 seconds and 4 git commands to generate.