Cleanup: src.ctf.lttng-live: rename live_iterator_status printing function
[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"
d9c39b0a 30#include "logging/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;
1419db2b
FD
79 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
80 "Stream class doesn't have a default clock class: "
14f28187
FD
81 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
82 bt_stream_class_get_id(sc),
83 bt_stream_class_get_name(sc));
84 goto end;
7cdc2bab
MD
85 }
86 }
87
7cdc2bab 88end:
14f28187
FD
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 */
96static
97const 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);
98b15851 106 BT_ASSERT_DBG(sc);
14f28187
FD
107
108 cc = bt_stream_class_borrow_default_clock_class_const(sc);
109 if (cc) {
110 goto end;
111 }
112 }
113end:
98b15851 114 BT_ASSERT_DBG(cc);
14f28187 115 return cc;
7cdc2bab
MD
116}
117
118BT_HIDDEN
14f28187 119enum lttng_live_iterator_status lttng_live_metadata_update(
7cdc2bab
MD
120 struct lttng_live_trace *trace)
121{
122 struct lttng_live_session *session = trace->session;
7cdc2bab 123 struct lttng_live_metadata *metadata = trace->metadata;
7cdc2bab
MD
124 size_t size, len_read = 0;
125 char *metadata_buf = NULL;
c28512ab 126 bool keep_receiving;
7cdc2bab
MD
127 FILE *fp = NULL;
128 enum ctf_metadata_decoder_status decoder_status;
14f28187
FD
129 enum lttng_live_iterator_status status =
130 LTTNG_LIVE_ITERATOR_STATUS_OK;
c01594de 131 bt_logging_level log_level = trace->log_level;
2ece7dd0 132 bt_self_component *self_comp = trace->self_comp;
c28512ab 133 enum lttng_live_get_one_metadata_status metadata_status;
7cdc2bab
MD
134
135 /* No metadata stream yet. */
136 if (!metadata) {
137 if (session->new_streams_needed) {
14f28187 138 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
139 } else {
140 session->new_streams_needed = true;
14f28187 141 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab
MD
142 }
143 goto end;
144 }
145
6f79a7cf
MD
146 if (!metadata->trace) {
147 trace->new_metadata_needed = false;
148 }
149
7cdc2bab
MD
150 if (!trace->new_metadata_needed) {
151 goto end;
152 }
153
f79c2d7a
FD
154 /*
155 * Open a new write only file handle to populate the `metadata_buf`
156 * memory buffer so we can write in loop in it easily.
157 */
7cdc2bab
MD
158 fp = bt_open_memstream(&metadata_buf, &size);
159 if (!fp) {
5b959469
FD
160 if (errno == EINTR &&
161 lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
f79c2d7a 162 session->lttng_live_msg_iter->was_interrupted = true;
5b959469
FD
163 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
164 } else {
165 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
166 "Metadata open_memstream", ".");
167 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
168 }
169 goto end;
7cdc2bab
MD
170 }
171
c28512ab 172 keep_receiving = true;
7cdc2bab 173 /* Grab all available metadata. */
c28512ab
FD
174 while (keep_receiving) {
175 size_t reply_len = 0;
7cdc2bab 176 /*
c28512ab
FD
177 * lttng_live_get_one_metadata_packet() asks the Relay Daemon
178 * for new metadata. If new metadata is received, the function
179 * writes it to the provided file handle and updates the
180 * reply_len output parameter. We call this function in loop
181 * until it returns _END meaning that no new metadata is
182 * available.
183 * We may receive a _CLOSED status if the metadata stream we
184 * are requesting is no longer available on the relay.
185 * If we receive an _ERROR status, it means there was a
186 * networking, allocating, or some other unrecoverable error.
7cdc2bab 187 */
c28512ab
FD
188 metadata_status = lttng_live_get_one_metadata_packet(trace, fp,
189 &reply_len);
190
191 switch (metadata_status) {
192 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK:
193 len_read += reply_len;
194 break;
195 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END:
196 keep_receiving = false;
197 break;
198 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED:
199 keep_receiving = false;
200 break;
201 case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR:
202 goto error;
203 default:
498e7994 204 bt_common_abort();
7cdc2bab 205 }
c28512ab 206 }
7cdc2bab
MD
207
208 /*
c28512ab
FD
209 * A closed metadata stream means the trace is no longer active. Return
210 * _END so that the caller can remove the trace from its list.
7cdc2bab 211 */
c28512ab
FD
212 if (metadata_status == LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED) {
213 status = LTTNG_LIVE_ITERATOR_STATUS_END;
214 goto end;
7cdc2bab
MD
215 }
216
f79c2d7a 217 /* The memory buffer `metadata_buf` contains all the metadata. */
7cdc2bab 218 if (bt_close_memstream(&metadata_buf, &size, fp)) {
1419db2b
FD
219 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
220 "Metadata bt_close_memstream", ".");
7cdc2bab 221 }
e7c34fd7 222
7cdc2bab
MD
223 fp = NULL;
224
225 if (len_read == 0) {
226 if (!trace->trace) {
14f28187 227 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
228 goto end;
229 }
230 trace->new_metadata_needed = false;
231 goto end;
232 }
233
f79c2d7a
FD
234 /*
235 * Open a new reading file handle on the `metadata_buf` and pass it to
236 * the metadata decoder.
237 */
7cdc2bab
MD
238 fp = bt_fmemopen(metadata_buf, len_read, "rb");
239 if (!fp) {
5b959469
FD
240 if (errno == EINTR &&
241 lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
f79c2d7a 242 session->lttng_live_msg_iter->was_interrupted = true;
5b959469
FD
243 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
244 } else {
245 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
246 "Cannot memory-open metadata buffer", ".");
247 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
248 }
249 goto end;
7cdc2bab
MD
250 }
251
14f28187 252 /*
06be9946
PP
253 * The call to ctf_metadata_decoder_append_content() will append
254 * new metadata to our current trace class.
14f28187 255 */
06be9946
PP
256 decoder_status = ctf_metadata_decoder_append_content(
257 metadata->decoder, fp);
7cdc2bab
MD
258 switch (decoder_status) {
259 case CTF_METADATA_DECODER_STATUS_OK:
14f28187 260 if (!trace->trace_class) {
335a2da5
PP
261 struct ctf_trace_class *tc =
262 ctf_metadata_decoder_borrow_ctf_trace_class(
263 metadata->decoder);
264
14f28187
FD
265 trace->trace_class =
266 ctf_metadata_decoder_get_ir_trace_class(
267 metadata->decoder);
268 trace->trace = bt_trace_create(trace->trace_class);
335a2da5 269 if (!trace->trace) {
1419db2b
FD
270 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
271 "Failed to create bt_trace");
335a2da5
PP
272 goto error;
273 }
274 if (ctf_trace_class_configure_ir_trace(tc,
275 trace->trace)) {
1419db2b
FD
276 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
277 "Failed to configure ctf trace class");
335a2da5
PP
278 goto error;
279 }
14f28187 280 if (!stream_classes_all_have_default_clock_class(
2ece7dd0
PP
281 trace->trace_class, log_level,
282 self_comp)) {
14f28187
FD
283 /* Error logged in function. */
284 goto error;
285 }
286 trace->clock_class =
287 borrow_any_clock_class(trace->trace_class);
7cdc2bab 288 }
14f28187
FD
289 trace->new_metadata_needed = false;
290
7cdc2bab
MD
291 break;
292 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
14f28187 293 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab 294 break;
1a6da3f9 295 default:
7cdc2bab
MD
296 goto error;
297 }
298
299 goto end;
c28512ab 300
7cdc2bab 301error:
5b959469 302 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
7cdc2bab
MD
303end:
304 if (fp) {
305 int closeret;
306
307 closeret = fclose(fp);
308 if (closeret) {
1419db2b
FD
309 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
310 "Error on fclose", ".");
7cdc2bab
MD
311 }
312 }
6f79a7cf 313 free(metadata_buf);
7cdc2bab
MD
314 return status;
315}
316
317BT_HIDDEN
318int lttng_live_metadata_create_stream(struct lttng_live_session *session,
2ece7dd0 319 uint64_t ctf_trace_id, uint64_t stream_id,
06994c71 320 const char *trace_name)
7cdc2bab 321{
1419db2b
FD
322 bt_self_component *self_comp = session->self_comp;
323 bt_logging_level log_level = session->log_level;
7cdc2bab
MD
324 struct lttng_live_metadata *metadata = NULL;
325 struct lttng_live_trace *trace;
0746848c 326 struct ctf_metadata_decoder_config cfg = {
c01594de 327 .log_level = session->log_level,
2ece7dd0 328 .self_comp = session->self_comp,
0746848c
PP
329 .clock_class_offset_s = 0,
330 .clock_class_offset_ns = 0,
06be9946 331 .create_trace_class = true,
0746848c 332 };
7cdc2bab
MD
333
334 metadata = g_new0(struct lttng_live_metadata, 1);
335 if (!metadata) {
336 return -1;
337 }
c01594de 338 metadata->log_level = session->log_level;
2ece7dd0 339 metadata->self_comp = session->self_comp;
7cdc2bab 340 metadata->stream_id = stream_id;
14f28187 341
f7b785ac 342 metadata->decoder = ctf_metadata_decoder_create(&cfg);
7cdc2bab 343 if (!metadata->decoder) {
1419db2b
FD
344 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
345 "Failed to create CTF metadata decoder");
7cdc2bab
MD
346 goto error;
347 }
14f28187 348 trace = lttng_live_borrow_trace(session, ctf_trace_id);
7cdc2bab 349 if (!trace) {
1419db2b
FD
350 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
351 "Failed to borrow trace");
7cdc2bab
MD
352 goto error;
353 }
354 metadata->trace = trace;
355 trace->metadata = metadata;
356 return 0;
357
358error:
359 ctf_metadata_decoder_destroy(metadata->decoder);
360 g_free(metadata);
361 return -1;
362}
363
364BT_HIDDEN
365void lttng_live_metadata_fini(struct lttng_live_trace *trace)
366{
367 struct lttng_live_metadata *metadata = trace->metadata;
368
369 if (!metadata) {
370 return;
371 }
7cdc2bab
MD
372 ctf_metadata_decoder_destroy(metadata->decoder);
373 trace->metadata = NULL;
7cdc2bab
MD
374 g_free(metadata);
375}
This page took 0.083535 seconds and 4 git commands to generate.