Fix: src.ctf.fs: initialize the other_entry variable
[babeltrace.git] / src / plugins / ctf / fs-src / fs.c
CommitLineData
7a278c8e 1/*
ea0b4b9e 2 * fs.c
7a278c8e 3 *
ea0b4b9e 4 * Babeltrace CTF file system Reader Component
7a278c8e 5 *
1a9f7075 6 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
f3bc2010 7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7a278c8e 8 *
7a278c8e
JG
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
4c65a157 28#define BT_COMP_LOG_SELF_COMP self_comp
98903a3e
PP
29#define BT_LOG_OUTPUT_LEVEL log_level
30#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS"
d9c39b0a 31#include "logging/comp-logging.h"
98903a3e 32
578e048b 33#include "common/common.h"
3fadfbc0 34#include <babeltrace2/babeltrace.h>
6162e6b7 35#include "common/uuid.h"
ea0b4b9e 36#include <glib.h>
578e048b 37#include "common/assert.h"
94cf822e 38#include <inttypes.h>
c55a9f58 39#include <stdbool.h>
56a1cced 40#include "fs.h"
413bc2c4 41#include "metadata.h"
94cf822e 42#include "data-stream-file.h"
e7a4393b 43#include "file.h"
1e649dff 44#include "../common/metadata/decoder.h"
335a2da5 45#include "../common/metadata/ctf-meta-configure-ir-trace.h"
d6e69534 46#include "../common/msg-iter/msg-iter.h"
04c0ba87 47#include "query.h"
c24f7ab4 48#include "plugins/common/param-validation/param-validation.h"
e7a4393b 49
626cc488
FD
50struct tracer_info {
51 const char *name;
52 int64_t major;
53 int64_t minor;
54 int64_t patch;
55};
56
94cf822e 57static
d6e69534
PP
58void ctf_fs_msg_iter_data_destroy(
59 struct ctf_fs_msg_iter_data *msg_iter_data)
94cf822e 60{
d6e69534 61 if (!msg_iter_data) {
94cf822e
PP
62 return;
63 }
64
d6e69534 65 if (msg_iter_data->msg_iter) {
18a1979b 66 ctf_msg_iter_destroy(msg_iter_data->msg_iter);
6de92955
PP
67 }
68
f6e68e70
SM
69 if (msg_iter_data->msg_iter_medops_data) {
70 ctf_fs_ds_group_medops_data_destroy(
71 msg_iter_data->msg_iter_medops_data);
72 }
94cf822e 73
f6e68e70 74 g_free(msg_iter_data);
fc917f65
PP
75}
76
d4393e08 77static
a3f0c7db 78bt_message_iterator_class_next_method_status ctf_fs_iterator_next_one(
d6e69534 79 struct ctf_fs_msg_iter_data *msg_iter_data,
fc917f65 80 const bt_message **out_msg)
ea0b4b9e 81{
a3f0c7db 82 bt_message_iterator_class_next_method_status status;
f6e68e70
SM
83 enum ctf_msg_iter_status msg_iter_status;
84 bt_logging_level log_level = msg_iter_data->log_level;
94cf822e 85
f6e68e70
SM
86 msg_iter_status = ctf_msg_iter_get_next_message(
87 msg_iter_data->msg_iter, out_msg);
f42867e2 88
f6e68e70
SM
89 switch (msg_iter_status) {
90 case CTF_MSG_ITER_STATUS_OK:
91 /* Cool, message has been written to *out_msg. */
a3f0c7db 92 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
f6e68e70 93 break;
4829c3e2 94
f6e68e70 95 case CTF_MSG_ITER_STATUS_EOF:
a3f0c7db 96 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
f6e68e70 97 break;
4829c3e2 98
f6e68e70
SM
99 case CTF_MSG_ITER_STATUS_AGAIN:
100 /*
101 * Should not make it this far as this is
102 * medium-specific; there is nothing for the user to do
103 * and it should have been handled upstream.
104 */
105 bt_common_abort();
106
107 case CTF_MSG_ITER_MEDIUM_STATUS_ERROR:
108 BT_MSG_ITER_LOGE_APPEND_CAUSE(msg_iter_data->self_msg_iter,
109 "Failed to get next message from CTF message iterator.");
a3f0c7db 110 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
f6e68e70
SM
111 break;
112
113 case CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR:
114 BT_MSG_ITER_LOGE_APPEND_CAUSE(msg_iter_data->self_msg_iter,
115 "Failed to get next message from CTF message iterator.");
a3f0c7db 116 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
f6e68e70
SM
117 break;
118
119 default:
120 bt_common_abort();
94cf822e 121 }
d01e0f33 122
d4393e08
PP
123 return status;
124}
125
126BT_HIDDEN
a3f0c7db 127bt_message_iterator_class_next_method_status ctf_fs_iterator_next(
d6e69534
PP
128 bt_self_message_iterator *iterator,
129 bt_message_array_const msgs, uint64_t capacity,
d4393e08
PP
130 uint64_t *count)
131{
a3f0c7db 132 bt_message_iterator_class_next_method_status status;
d6e69534
PP
133 struct ctf_fs_msg_iter_data *msg_iter_data =
134 bt_self_message_iterator_get_data(iterator);
d4393e08
PP
135 uint64_t i = 0;
136
cbca1c06
SM
137 if (G_UNLIKELY(msg_iter_data->next_saved_error)) {
138 /*
139 * Last time we were called, we hit an error but had some
140 * messages to deliver, so we stashed the error here. Return
141 * it now.
142 */
143 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(msg_iter_data->next_saved_error);
144 status = msg_iter_data->next_saved_status;
145 goto end;
146 }
147
148 do {
d6e69534 149 status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]);
a3f0c7db 150 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
d4393e08
PP
151 i++;
152 }
cbca1c06 153 } while (i < capacity &&
a3f0c7db 154 status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK);
d4393e08
PP
155
156 if (i > 0) {
157 /*
158 * Even if ctf_fs_iterator_next_one() returned something
a3f0c7db 159 * else than BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK, we
d6e69534
PP
160 * accumulated message objects in the output
161 * message array, so we need to return
a3f0c7db 162 * BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK so that they are
d4393e08 163 * transfered to downstream. This other status occurs
d6e69534 164 * again the next time muxer_msg_iter_do_next() is
d4393e08 165 * called, possibly without any accumulated
d6e69534 166 * message, in which case we'll return it.
d4393e08 167 */
cbca1c06
SM
168 if (status < 0) {
169 /*
170 * Save this error for the next _next call. Assume that
171 * this component always appends error causes when
172 * returning an error status code, which will cause the
173 * current thread error to be non-NULL.
174 */
175 msg_iter_data->next_saved_error = bt_current_thread_take_error();
176 BT_ASSERT(msg_iter_data->next_saved_error);
177 msg_iter_data->next_saved_status = status;
178 }
179
d4393e08 180 *count = i;
a3f0c7db 181 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
d4393e08
PP
182 }
183
cbca1c06 184end:
d4393e08 185 return status;
ea0b4b9e 186}
bfd20a42 187
6a9bb5e9 188BT_HIDDEN
a3f0c7db 189bt_message_iterator_class_seek_beginning_method_status
d24d5663 190ctf_fs_iterator_seek_beginning(bt_self_message_iterator *it)
6a9bb5e9
PP
191{
192 struct ctf_fs_msg_iter_data *msg_iter_data =
193 bt_self_message_iterator_get_data(it);
6a9bb5e9
PP
194
195 BT_ASSERT(msg_iter_data);
6a9bb5e9 196
f6e68e70
SM
197 ctf_msg_iter_reset(msg_iter_data->msg_iter);
198 ctf_fs_ds_group_medops_data_reset(msg_iter_data->msg_iter_medops_data);
199
a3f0c7db 200 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
6a9bb5e9
PP
201}
202
203BT_HIDDEN
d6e69534 204void ctf_fs_iterator_finalize(bt_self_message_iterator *it)
760051fa 205{
d6e69534
PP
206 ctf_fs_msg_iter_data_destroy(
207 bt_self_message_iterator_get_data(it));
760051fa
JG
208}
209
6a9bb5e9 210BT_HIDDEN
a3f0c7db 211bt_message_iterator_class_initialize_method_status ctf_fs_iterator_init(
d6e69534 212 bt_self_message_iterator *self_msg_iter,
8d8b141d 213 bt_self_message_iterator_configuration *config,
b19ff26f 214 bt_self_component_port_output *self_port)
4c1456f0 215{
4f1f88a6 216 struct ctf_fs_port_data *port_data;
d6e69534 217 struct ctf_fs_msg_iter_data *msg_iter_data = NULL;
a3f0c7db 218 bt_message_iterator_class_initialize_method_status status;
98903a3e 219 bt_logging_level log_level;
f6e68e70 220 enum ctf_msg_iter_medium_status medium_status;
f615b250
PP
221 bt_self_component *self_comp =
222 bt_self_message_iterator_borrow_component(self_msg_iter);
760051fa 223
d94d92ac 224 port_data = bt_self_component_port_get_data(
707b7d35 225 bt_self_component_port_output_as_self_component_port(
d94d92ac
PP
226 self_port));
227 BT_ASSERT(port_data);
98903a3e 228 log_level = port_data->ctf_fs->log_level;
d6e69534
PP
229 msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1);
230 if (!msg_iter_data) {
a3f0c7db 231 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
94cf822e
PP
232 goto error;
233 }
234
98903a3e 235 msg_iter_data->log_level = log_level;
4c65a157 236 msg_iter_data->self_comp = self_comp;
f30762e5 237 msg_iter_data->self_msg_iter = self_msg_iter;
f6e68e70
SM
238 msg_iter_data->ds_file_group = port_data->ds_file_group;
239
240 medium_status = ctf_fs_ds_group_medops_data_create(
241 msg_iter_data->ds_file_group, self_msg_iter, log_level,
242 &msg_iter_data->msg_iter_medops_data);
243 BT_ASSERT(
244 medium_status == CTF_MSG_ITER_MEDIUM_STATUS_OK ||
245 medium_status == CTF_MSG_ITER_MEDIUM_STATUS_ERROR ||
246 medium_status == CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR);
247 if (medium_status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
248 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter,
249 "Failed to create ctf_fs_ds_group_medops");
250 status = (int) medium_status;
251 goto error;
252 }
253
18a1979b 254 msg_iter_data->msg_iter = ctf_msg_iter_create(
f6e68e70 255 msg_iter_data->ds_file_group->ctf_fs_trace->metadata->tc,
98903a3e 256 bt_common_get_page_size(msg_iter_data->log_level) * 8,
f6e68e70
SM
257 ctf_fs_ds_group_medops,
258 msg_iter_data->msg_iter_medops_data,
259 msg_iter_data->log_level,
851de941 260 self_comp, self_msg_iter);
d6e69534 261 if (!msg_iter_data->msg_iter) {
d23b766e 262 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot create a CTF message iterator.");
a3f0c7db 263 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
6de92955
PP
264 goto error;
265 }
266
c0e46a7c
SM
267 /*
268 * This iterator can seek forward if its stream class has a default
269 * clock class.
270 */
271 if (msg_iter_data->ds_file_group->sc->default_clock_class) {
272 bt_self_message_iterator_configuration_set_can_seek_forward(
273 config, true);
274 }
275
d6e69534
PP
276 bt_self_message_iterator_set_data(self_msg_iter,
277 msg_iter_data);
d6e69534 278 msg_iter_data = NULL;
f6e68e70 279
a3f0c7db 280 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
4f1f88a6 281 goto end;
5b29e799 282
4f1f88a6 283error:
d6e69534 284 bt_self_message_iterator_set_data(self_msg_iter, NULL);
4f1f88a6 285
760051fa 286end:
d6e69534 287 ctf_fs_msg_iter_data_destroy(msg_iter_data);
f6e68e70 288 return status;
760051fa
JG
289}
290
a0cd55ad
SM
291static
292void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace)
293{
294 if (!ctf_fs_trace) {
295 return;
296 }
297
298 if (ctf_fs_trace->ds_file_groups) {
299 g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE);
300 }
301
302 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace);
303
304 if (ctf_fs_trace->path) {
305 g_string_free(ctf_fs_trace->path, TRUE);
306 }
307
308 if (ctf_fs_trace->metadata) {
309 ctf_fs_metadata_fini(ctf_fs_trace->metadata);
310 g_free(ctf_fs_trace->metadata);
311 }
312
313 g_free(ctf_fs_trace);
314}
315
f280892e 316BT_HIDDEN
1a9f7075 317void ctf_fs_destroy(struct ctf_fs_component *ctf_fs)
760051fa 318{
4f1f88a6 319 if (!ctf_fs) {
8fa760ba
JG
320 return;
321 }
4f1f88a6 322
a0cd55ad 323 ctf_fs_trace_destroy(ctf_fs->trace);
4f1f88a6
PP
324
325 if (ctf_fs->port_data) {
326 g_ptr_array_free(ctf_fs->port_data, TRUE);
c14d7e26 327 }
760051fa 328
1a9f7075
PP
329 g_free(ctf_fs);
330}
331
f280892e
SM
332static
333void port_data_destroy(struct ctf_fs_port_data *port_data)
334{
335 if (!port_data) {
336 return;
337 }
338
339 g_free(port_data);
340}
341
342static
343void port_data_destroy_notifier(void *data) {
344 port_data_destroy(data);
345}
346
97ade20b 347static
56a924f4 348void ctf_fs_trace_destroy_notifier(void *data)
97ade20b
JG
349{
350 struct ctf_fs_trace *trace = data;
351 ctf_fs_trace_destroy(trace);
352}
353
4c65a157
PP
354struct ctf_fs_component *ctf_fs_component_create(bt_logging_level log_level,
355 bt_self_component *self_comp)
a4792757 356{
f280892e 357 struct ctf_fs_component *ctf_fs;
a4792757 358
f280892e
SM
359 ctf_fs = g_new0(struct ctf_fs_component, 1);
360 if (!ctf_fs) {
361 goto error;
362 }
4f1f88a6 363
98903a3e 364 ctf_fs->log_level = log_level;
f280892e
SM
365 ctf_fs->port_data =
366 g_ptr_array_new_with_free_func(port_data_destroy_notifier);
367 if (!ctf_fs->port_data) {
368 goto error;
4f1f88a6
PP
369 }
370
f280892e
SM
371 goto end;
372
373error:
bce64edb
SM
374 ctf_fs_destroy(ctf_fs);
375 ctf_fs = NULL;
f280892e
SM
376
377end:
378 return ctf_fs;
379}
380
381void ctf_fs_finalize(bt_self_component_source *component)
382{
383 ctf_fs_destroy(bt_self_component_get_data(
384 bt_self_component_source_as_self_component(component)));
5b29e799
JG
385}
386
a38d7650 387gchar *ctf_fs_make_port_name(struct ctf_fs_ds_file_group *ds_file_group)
547eacf1 388{
a38d7650 389 GString *name = g_string_new(NULL);
547eacf1 390
a38d7650
SM
391 /*
392 * The unique port name is generated by concatenating unique identifiers
393 * for:
394 *
395 * - the trace
396 * - the stream class
397 * - the stream
398 */
399
400 /* For the trace, use the uuid if present, else the path. */
401 if (ds_file_group->ctf_fs_trace->metadata->tc->is_uuid_set) {
6162e6b7 402 char uuid_str[BT_UUID_STR_LEN + 1];
a38d7650 403
6162e6b7 404 bt_uuid_to_str(ds_file_group->ctf_fs_trace->metadata->tc->uuid, uuid_str);
a38d7650
SM
405 g_string_assign(name, uuid_str);
406 } else {
407 g_string_assign(name, ds_file_group->ctf_fs_trace->path->str);
547eacf1
PP
408 }
409
410 /*
a38d7650
SM
411 * For the stream class, use the id if present. We can omit this field
412 * otherwise, as there will only be a single stream class.
547eacf1 413 */
a38d7650
SM
414 if (ds_file_group->sc->id != UINT64_C(-1)) {
415 g_string_append_printf(name, " | %" PRIu64, ds_file_group->sc->id);
416 }
547eacf1 417
a38d7650
SM
418 /* For the stream, use the id if present, else, use the path. */
419 if (ds_file_group->stream_id != UINT64_C(-1)) {
420 g_string_append_printf(name, " | %" PRIu64, ds_file_group->stream_id);
421 } else {
422 BT_ASSERT(ds_file_group->ds_file_infos->len == 1);
423 struct ctf_fs_ds_file_info *ds_file_info =
424 g_ptr_array_index(ds_file_group->ds_file_infos, 0);
425 g_string_append_printf(name, " | %s", ds_file_info->path->str);
426 }
427
428 return g_string_free(name, FALSE);
547eacf1
PP
429}
430
5b29e799 431static
55314f2a
JG
432int create_one_port_for_trace(struct ctf_fs_component *ctf_fs,
433 struct ctf_fs_trace *ctf_fs_trace,
d23b766e
SM
434 struct ctf_fs_ds_file_group *ds_file_group,
435 bt_self_component_source *self_comp_src)
5b29e799 436{
4f1f88a6 437 int ret = 0;
4f1f88a6 438 struct ctf_fs_port_data *port_data = NULL;
a38d7650 439 gchar *port_name;
98903a3e 440 bt_logging_level log_level = ctf_fs->log_level;
d23b766e
SM
441 bt_self_component *self_comp =
442 bt_self_component_source_as_self_component(self_comp_src);
4f1f88a6 443
a38d7650 444 port_name = ctf_fs_make_port_name(ds_file_group);
4f1f88a6
PP
445 if (!port_name) {
446 goto error;
447 }
448
4c65a157 449 BT_COMP_LOGI("Creating one port named `%s`", port_name);
4f1f88a6
PP
450
451 /* Create output port for this file */
4f1f88a6
PP
452 port_data = g_new0(struct ctf_fs_port_data, 1);
453 if (!port_data) {
454 goto error;
455 }
456
5c563278 457 port_data->ctf_fs = ctf_fs;
94cf822e 458 port_data->ds_file_group = ds_file_group;
d94d92ac 459 ret = bt_self_component_source_add_output_port(
d23b766e 460 self_comp_src, port_name, port_data, NULL);
147337a3 461 if (ret) {
4f1f88a6
PP
462 goto error;
463 }
464
465 g_ptr_array_add(ctf_fs->port_data, port_data);
466 port_data = NULL;
467 goto end;
468
469error:
470 ret = -1;
471
472end:
19bbdc9b 473 g_free(port_name);
4f1f88a6 474
4f1f88a6
PP
475 port_data_destroy(port_data);
476 return ret;
5b29e799
JG
477}
478
479static
55314f2a 480int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
d23b766e
SM
481 struct ctf_fs_trace *ctf_fs_trace,
482 bt_self_component_source *self_comp_src)
94cf822e
PP
483{
484 int ret = 0;
94cf822e 485 size_t i;
98903a3e 486 bt_logging_level log_level = ctf_fs_trace->log_level;
d23b766e
SM
487 bt_self_component *self_comp =
488 bt_self_component_source_as_self_component(self_comp_src);
94cf822e
PP
489
490 /* Create one output port for each stream file group */
491 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
492 struct ctf_fs_ds_file_group *ds_file_group =
493 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
494
55314f2a 495 ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace,
d23b766e 496 ds_file_group, self_comp_src);
94cf822e 497 if (ret) {
d23b766e 498 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot create output port.");
94cf822e
PP
499 goto end;
500 }
501 }
502
503end:
504 return ret;
505}
506
94cf822e
PP
507static
508void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info)
509{
510 if (!ds_file_info) {
511 return;
512 }
513
514 if (ds_file_info->path) {
515 g_string_free(ds_file_info->path, TRUE);
516 }
517
518 g_free(ds_file_info);
519}
520
521static
522struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path,
7ed5243a 523 int64_t begin_ns)
94cf822e
PP
524{
525 struct ctf_fs_ds_file_info *ds_file_info;
526
527 ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1);
528 if (!ds_file_info) {
529 goto end;
530 }
531
532 ds_file_info->path = g_string_new(path);
533 if (!ds_file_info->path) {
534 ctf_fs_ds_file_info_destroy(ds_file_info);
535 ds_file_info = NULL;
536 goto end;
537 }
538
539 ds_file_info->begin_ns = begin_ns;
540
541end:
542 return ds_file_info;
543}
544
545static
546void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
547{
548 if (!ds_file_group) {
549 return;
550 }
551
552 if (ds_file_group->ds_file_infos) {
553 g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
554 }
555
24bf8d91 556 ctf_fs_ds_index_destroy(ds_file_group->index);
7ed5243a 557
c5b9b441 558 bt_stream_put_ref(ds_file_group->stream);
94cf822e
PP
559 g_free(ds_file_group);
560}
561
562static
563struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(
564 struct ctf_fs_trace *ctf_fs_trace,
60363f2f 565 struct ctf_stream_class *sc,
7ed5243a
FD
566 uint64_t stream_instance_id,
567 struct ctf_fs_ds_index *index)
94cf822e
PP
568{
569 struct ctf_fs_ds_file_group *ds_file_group;
570
94cf822e
PP
571 ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1);
572 if (!ds_file_group) {
573 goto error;
574 }
575
576 ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func(
577 (GDestroyNotify) ctf_fs_ds_file_info_destroy);
578 if (!ds_file_group->ds_file_infos) {
579 goto error;
580 }
581
7ed5243a
FD
582 ds_file_group->index = index;
583
547eacf1 584 ds_file_group->stream_id = stream_instance_id;
60363f2f
PP
585 BT_ASSERT(sc);
586 ds_file_group->sc = sc;
94cf822e 587 ds_file_group->ctf_fs_trace = ctf_fs_trace;
94cf822e
PP
588 goto end;
589
590error:
591 ctf_fs_ds_file_group_destroy(ds_file_group);
7ed5243a 592 ctf_fs_ds_index_destroy(index);
94cf822e
PP
593 ds_file_group = NULL;
594
595end:
596 return ds_file_group;
597}
598
8bf7105e
MJ
599/* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
600static
601void array_insert(GPtrArray *array, gpointer element, size_t pos)
602{
603 size_t original_array_len = array->len;
604
605 /* Allocate an unused element at the end of the array. */
606 g_ptr_array_add(array, NULL);
607
608 /* If we are not inserting at the end, move the elements by one. */
609 if (pos < original_array_len) {
610 memmove(&(array->pdata[pos + 1]),
611 &(array->pdata[pos]),
612 (original_array_len - pos) * sizeof(gpointer));
613 }
614
f897d50c 615 /* Insert the value. */
8bf7105e
MJ
616 array->pdata[pos] = element;
617}
618
41a65f30
SM
619/*
620 * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
621 * place to keep it sorted.
622 */
623
624static
625void ds_file_group_insert_ds_file_info_sorted(
626 struct ctf_fs_ds_file_group *ds_file_group,
627 struct ctf_fs_ds_file_info *ds_file_info)
628{
629 guint i;
630
631 /* Find the spot where to insert this ds_file_info. */
632 for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
633 struct ctf_fs_ds_file_info *other_ds_file_info =
634 g_ptr_array_index(ds_file_group->ds_file_infos, i);
635
636 if (ds_file_info->begin_ns < other_ds_file_info->begin_ns) {
637 break;
638 }
639 }
640
641 array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
642}
643
1505f33a
SM
644static
645bool ds_index_entries_equal(
646 const struct ctf_fs_ds_index_entry *left,
647 const struct ctf_fs_ds_index_entry *right)
648{
649 if (left->packet_size != right->packet_size) {
650 return false;
651 }
652
653 if (left->timestamp_begin != right->timestamp_begin) {
654 return false;
655 }
656
657 if (left->timestamp_end != right->timestamp_end) {
658 return false;
659 }
660
661 if (left->packet_seq_num != right->packet_seq_num) {
662 return false;
663 }
664
665 return true;
666}
667
668/*
669 * Insert `entry` into `index`, without duplication.
670 *
671 * The entry is inserted only if there isn't an identical entry already.
672 *
673 * In any case, the ownership of `entry` is transferred to this function. So if
674 * the entry is not inserted, it is freed.
675 */
676
7ed5243a 677static
ce75de14
SM
678void ds_index_insert_ds_index_entry_sorted(
679 struct ctf_fs_ds_index *index,
7ed5243a
FD
680 struct ctf_fs_ds_index_entry *entry)
681{
682 guint i;
0f77b5c9 683 struct ctf_fs_ds_index_entry *other_entry = NULL;
7ed5243a
FD
684
685 /* Find the spot where to insert this index entry. */
ce75de14 686 for (i = 0; i < index->entries->len; i++) {
1505f33a 687 other_entry = g_ptr_array_index(index->entries, i);
7ed5243a 688
1505f33a 689 if (entry->timestamp_begin_ns <= other_entry->timestamp_begin_ns) {
7ed5243a
FD
690 break;
691 }
692 }
693
1505f33a
SM
694 /*
695 * Insert the entry only if a duplicate doesn't already exist.
696 *
697 * There can be duplicate packets if reading multiple overlapping
698 * snapshots of the same trace. We then want the index to contain
699 * a reference to only one copy of that packet.
700 */
701 if (i == index->entries->len ||
702 !ds_index_entries_equal(entry, other_entry)) {
703 array_insert(index->entries, entry, i);
704 } else {
705 g_free(entry);
706 }
ce75de14
SM
707}
708
709static
710void merge_ctf_fs_ds_indexes(struct ctf_fs_ds_index *dest, struct ctf_fs_ds_index *src)
711{
712 guint i;
713
714 for (i = 0; i < src->entries->len; i++) {
715 struct ctf_fs_ds_index_entry *entry =
716 g_ptr_array_index(src->entries, i);
717
718 /*
719 * Ownership of the ctf_fs_ds_index_entry is transferred to
1505f33a 720 * ds_index_insert_ds_index_entry_sorted.
ce75de14
SM
721 */
722 g_ptr_array_index(src->entries, i) = NULL;
ce75de14
SM
723 ds_index_insert_ds_index_entry_sorted(dest, entry);
724 }
7ed5243a
FD
725}
726
94cf822e
PP
727static
728int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
e5be10ef 729 const char *path)
94cf822e 730{
44c440bc
PP
731 int64_t stream_instance_id = -1;
732 int64_t begin_ns = -1;
94cf822e
PP
733 struct ctf_fs_ds_file_group *ds_file_group = NULL;
734 bool add_group = false;
735 int ret;
736 size_t i;
6de92955 737 struct ctf_fs_ds_file *ds_file = NULL;
bf012bde 738 struct ctf_fs_ds_file_info *ds_file_info = NULL;
97ade20b 739 struct ctf_fs_ds_index *index = NULL;
18a1979b 740 struct ctf_msg_iter *msg_iter = NULL;
44c440bc 741 struct ctf_stream_class *sc = NULL;
18a1979b 742 struct ctf_msg_iter_packet_properties props;
98903a3e 743 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 744 bt_self_component *self_comp = ctf_fs_trace->self_comp;
b7d2695a 745 bt_self_component_class *self_comp_class = ctf_fs_trace->self_comp_class;
94cf822e 746
6d54260a
SM
747 /*
748 * Create a temporary ds_file to read some properties about the data
749 * stream file.
750 */
751 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, NULL, path,
752 log_level);
753 if (!ds_file) {
754 goto error;
755 }
756
757 /* Create a temporary iterator to read the ds_file. */
18a1979b 758 msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
98903a3e 759 bt_common_get_page_size(log_level) * 8,
6d54260a 760 ctf_fs_ds_file_medops, ds_file, log_level, self_comp, NULL);
d6e69534 761 if (!msg_iter) {
4c65a157 762 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
6de92955
PP
763 goto error;
764 }
765
bc0ae364
SM
766 ctf_msg_iter_set_dry_run(msg_iter, true);
767
6d54260a 768 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
94cf822e 769 if (ret) {
b7d2695a 770 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
d23b766e 771 "Cannot get stream file's first packet's header and context fields (`%s`).",
94cf822e
PP
772 path);
773 goto error;
774 }
775
44c440bc
PP
776 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
777 props.stream_class_id);
778 BT_ASSERT(sc);
44c440bc
PP
779 stream_instance_id = props.data_stream_id;
780
781 if (props.snapshots.beginning_clock != UINT64_C(-1)) {
782 BT_ASSERT(sc->default_clock_class);
0f2d58c9
PP
783 ret = bt_util_clock_cycles_to_ns_from_origin(
784 props.snapshots.beginning_clock,
785 sc->default_clock_class->frequency,
786 sc->default_clock_class->offset_seconds,
787 sc->default_clock_class->offset_cycles, &begin_ns);
44c440bc 788 if (ret) {
b7d2695a 789 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
d23b766e 790 "Cannot convert clock cycles to nanoseconds from origin (`%s`).",
44c440bc
PP
791 path);
792 goto error;
793 }
94cf822e
PP
794 }
795
bf012bde
FD
796 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns);
797 if (!ds_file_info) {
798 goto error;
799 }
800
6d54260a 801 index = ctf_fs_ds_file_build_index(ds_file, ds_file_info, msg_iter);
97ade20b 802 if (!index) {
29a8227a
SM
803 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
804 self_comp, self_comp_class,
805 "Failed to index CTF stream file \'%s\'",
97ade20b 806 ds_file->file->path->str);
29a8227a 807 goto error;
97ade20b
JG
808 }
809
44c440bc 810 if (begin_ns == -1) {
94cf822e 811 /*
a8534010 812 * No beginning timestamp to sort the stream files
94cf822e
PP
813 * within a stream file group, so consider that this
814 * file must be the only one within its group.
815 */
44c440bc 816 stream_instance_id = -1;
94cf822e
PP
817 }
818
44c440bc 819 if (stream_instance_id == -1) {
94cf822e
PP
820 /*
821 * No stream instance ID or no beginning timestamp:
822 * create a unique stream file group for this stream
823 * file because, even if there's a stream instance ID,
824 * there's no timestamp to order the file within its
825 * group.
826 */
94cf822e 827 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
7ed5243a
FD
828 sc, UINT64_C(-1), index);
829 /* Ownership of index is transferred. */
830 index = NULL;
831
94cf822e
PP
832 if (!ds_file_group) {
833 goto error;
834 }
835
55b71f05
SM
836 ds_file_group_insert_ds_file_info_sorted(ds_file_group,
837 BT_MOVE_REF(ds_file_info));
94cf822e
PP
838
839 add_group = true;
840 goto end;
841 }
842
44c440bc
PP
843 BT_ASSERT(stream_instance_id != -1);
844 BT_ASSERT(begin_ns != -1);
94cf822e
PP
845
846 /* Find an existing stream file group with this ID */
847 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
94cf822e
PP
848 ds_file_group = g_ptr_array_index(
849 ctf_fs_trace->ds_file_groups, i);
94cf822e 850
60363f2f 851 if (ds_file_group->sc == sc &&
547eacf1
PP
852 ds_file_group->stream_id ==
853 stream_instance_id) {
94cf822e
PP
854 break;
855 }
856
857 ds_file_group = NULL;
858 }
859
860 if (!ds_file_group) {
861 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
7ed5243a
FD
862 sc, stream_instance_id, index);
863 /* Ownership of index is transferred. */
864 index = NULL;
94cf822e
PP
865 if (!ds_file_group) {
866 goto error;
867 }
868
869 add_group = true;
ce75de14
SM
870 } else {
871 merge_ctf_fs_ds_indexes(ds_file_group->index, index);
94cf822e
PP
872 }
873
55b71f05
SM
874 ds_file_group_insert_ds_file_info_sorted(ds_file_group,
875 BT_MOVE_REF(ds_file_info));
94cf822e
PP
876
877 goto end;
878
879error:
880 ctf_fs_ds_file_group_destroy(ds_file_group);
90e1eb09 881 ds_file_group = NULL;
94cf822e
PP
882 ret = -1;
883
884end:
885 if (add_group && ds_file_group) {
886 g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group);
887 }
547eacf1 888
97ade20b 889 ctf_fs_ds_file_destroy(ds_file);
55b71f05 890 ctf_fs_ds_file_info_destroy(ds_file_info);
6de92955 891
d6e69534 892 if (msg_iter) {
18a1979b 893 ctf_msg_iter_destroy(msg_iter);
6de92955
PP
894 }
895
97ade20b 896 ctf_fs_ds_index_destroy(index);
94cf822e
PP
897 return ret;
898}
899
900static
e5be10ef 901int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
e7a4393b
JG
902{
903 int ret = 0;
4f1f88a6 904 const char *basename;
e7a4393b 905 GError *error = NULL;
4f1f88a6 906 GDir *dir = NULL;
98903a3e 907 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 908 bt_self_component *self_comp = ctf_fs_trace->self_comp;
b7d2695a 909 bt_self_component_class *self_comp_class = ctf_fs_trace->self_comp_class;
e7a4393b 910
94cf822e 911 /* Check each file in the path directory, except specific ones */
1a9f7075 912 dir = g_dir_open(ctf_fs_trace->path->str, 0, &error);
e7a4393b 913 if (!dir) {
b7d2695a
SM
914 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
915 "Cannot open directory `%s`: %s (code %d)",
1a9f7075 916 ctf_fs_trace->path->str, error->message,
4f1f88a6 917 error->code);
e7a4393b
JG
918 goto error;
919 }
920
4f1f88a6 921 while ((basename = g_dir_read_name(dir))) {
94cf822e
PP
922 struct ctf_fs_file *file;
923
2242b43d 924 if (strcmp(basename, CTF_FS_METADATA_FILENAME) == 0) {
e7a4393b 925 /* Ignore the metadata stream. */
4c65a157 926 BT_COMP_LOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 927 ctf_fs_trace->path->str, basename);
e7a4393b
JG
928 continue;
929 }
930
4f1f88a6 931 if (basename[0] == '.') {
4c65a157 932 BT_COMP_LOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 933 ctf_fs_trace->path->str, basename);
e7a4393b
JG
934 continue;
935 }
936
937 /* Create the file. */
4c65a157 938 file = ctf_fs_file_create(log_level, self_comp);
e7a4393b 939 if (!file) {
b7d2695a 940 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
d23b766e 941 "Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 942 ctf_fs_trace->path->str, basename);
e7a4393b
JG
943 goto error;
944 }
945
946 /* Create full path string. */
3743a302 947 g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s",
1a9f7075 948 ctf_fs_trace->path->str, basename);
e7a4393b 949 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
4c65a157 950 BT_COMP_LOGI("Ignoring non-regular file `%s`",
1a9f7075 951 file->path->str);
e7a4393b 952 ctf_fs_file_destroy(file);
4f1f88a6 953 file = NULL;
e7a4393b
JG
954 continue;
955 }
956
55314f2a 957 ret = ctf_fs_file_open(file, "rb");
4f1f88a6 958 if (ret) {
b7d2695a
SM
959 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
960 "Cannot open stream file `%s`",
d23b766e 961 file->path->str);
e7a4393b
JG
962 goto error;
963 }
964
9fa0891a
JG
965 if (file->size == 0) {
966 /* Skip empty stream. */
4c65a157 967 BT_COMP_LOGI("Ignoring empty file `%s`", file->path->str);
9fa0891a
JG
968 ctf_fs_file_destroy(file);
969 continue;
970 }
971
e5be10ef 972 ret = add_ds_file_to_ds_file_group(ctf_fs_trace,
94cf822e 973 file->path->str);
4f1f88a6 974 if (ret) {
b7d2695a 975 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
d23b766e 976 "Cannot add stream file `%s` to stream file group",
1a9f7075 977 file->path->str);
94cf822e 978 ctf_fs_file_destroy(file);
e7a4393b
JG
979 goto error;
980 }
981
4f1f88a6 982 ctf_fs_file_destroy(file);
e7a4393b
JG
983 }
984
985 goto end;
4f1f88a6 986
e7a4393b
JG
987error:
988 ret = -1;
4f1f88a6 989
e7a4393b
JG
990end:
991 if (dir) {
992 g_dir_close(dir);
993 dir = NULL;
994 }
4f1f88a6 995
e7a4393b
JG
996 if (error) {
997 g_error_free(error);
998 }
5b29e799 999
91457551 1000 return ret;
5b29e799
JG
1001}
1002
862ca4ed 1003static
98903a3e 1004int set_trace_name(bt_trace *trace, const char *name_suffix,
4c65a157 1005 bt_logging_level log_level, bt_self_component *self_comp)
862ca4ed
PP
1006{
1007 int ret = 0;
b19ff26f 1008 const bt_value *val;
862ca4ed
PP
1009 GString *name;
1010
1011 name = g_string_new(NULL);
1012 if (!name) {
4c65a157 1013 BT_COMP_LOGE_STR("Failed to allocate a GString.");
862ca4ed
PP
1014 ret = -1;
1015 goto end;
1016 }
1017
1018 /*
1019 * Check if we have a trace environment string value named `hostname`.
1020 * If so, use it as the trace name's prefix.
1021 */
335a2da5
PP
1022 val = bt_trace_borrow_environment_entry_value_by_name_const(
1023 trace, "hostname");
862ca4ed
PP
1024 if (val && bt_value_is_string(val)) {
1025 g_string_append(name, bt_value_string_get(val));
1026
1027 if (name_suffix) {
1028 g_string_append_c(name, G_DIR_SEPARATOR);
1029 }
1030 }
1031
1032 if (name_suffix) {
1033 g_string_append(name, name_suffix);
1034 }
1035
1036 ret = bt_trace_set_name(trace, name->str);
1037 if (ret) {
1038 goto end;
1039 }
1040
1041 goto end;
1042
1043end:
1044 if (name) {
1045 g_string_free(name, TRUE);
1046 }
1047
1048 return ret;
1049}
1050
f280892e 1051static
b7d2695a
SM
1052struct ctf_fs_trace *ctf_fs_trace_create(
1053 bt_self_component *self_comp,
1054 bt_self_component_class *self_comp_class,
41693723 1055 const char *path, const char *name,
98903a3e
PP
1056 struct ctf_fs_metadata_config *metadata_config,
1057 bt_logging_level log_level)
1a9f7075
PP
1058{
1059 struct ctf_fs_trace *ctf_fs_trace;
1060 int ret;
1061
b7d2695a
SM
1062 /* Only one of them must be set. */
1063 BT_ASSERT(!self_comp != !self_comp_class);
1064
1a9f7075
PP
1065 ctf_fs_trace = g_new0(struct ctf_fs_trace, 1);
1066 if (!ctf_fs_trace) {
1067 goto end;
1068 }
1069
98903a3e 1070 ctf_fs_trace->log_level = log_level;
4c65a157 1071 ctf_fs_trace->self_comp = self_comp;
b7d2695a 1072 ctf_fs_trace->self_comp_class = self_comp_class;
1a9f7075
PP
1073 ctf_fs_trace->path = g_string_new(path);
1074 if (!ctf_fs_trace->path) {
1075 goto error;
1076 }
1077
1a9f7075
PP
1078 ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1);
1079 if (!ctf_fs_trace->metadata) {
1080 goto error;
1081 }
1082
44c440bc 1083 ctf_fs_metadata_init(ctf_fs_trace->metadata);
94cf822e
PP
1084 ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func(
1085 (GDestroyNotify) ctf_fs_ds_file_group_destroy);
1086 if (!ctf_fs_trace->ds_file_groups) {
1087 goto error;
1088 }
1089
4c65a157
PP
1090 ret = ctf_fs_metadata_set_trace_class(self_comp, ctf_fs_trace,
1091 metadata_config);
862ca4ed
PP
1092 if (ret) {
1093 goto error;
1094 }
1095
41693723
PP
1096 if (ctf_fs_trace->metadata->trace_class) {
1097 ctf_fs_trace->trace =
1098 bt_trace_create(ctf_fs_trace->metadata->trace_class);
1099 if (!ctf_fs_trace->trace) {
1100 goto error;
1101 }
862ca4ed
PP
1102 }
1103
41693723 1104 if (ctf_fs_trace->trace) {
335a2da5
PP
1105 ret = ctf_trace_class_configure_ir_trace(
1106 ctf_fs_trace->metadata->tc, ctf_fs_trace->trace);
1107 if (ret) {
1108 goto error;
1109 }
1110
4c65a157
PP
1111 ret = set_trace_name(ctf_fs_trace->trace, name, log_level,
1112 self_comp);
41693723
PP
1113 if (ret) {
1114 goto error;
1115 }
1a9f7075
PP
1116 }
1117
e5be10ef 1118 ret = create_ds_file_groups(ctf_fs_trace);
94cf822e
PP
1119 if (ret) {
1120 goto error;
1121 }
1122
1a9f7075
PP
1123 goto end;
1124
1125error:
1126 ctf_fs_trace_destroy(ctf_fs_trace);
1127 ctf_fs_trace = NULL;
44c440bc 1128
1a9f7075
PP
1129end:
1130 return ctf_fs_trace;
1131}
1132
1133static
1134int path_is_ctf_trace(const char *path)
1135{
1136 GString *metadata_path = g_string_new(NULL);
1137 int ret = 0;
1138
1139 if (!metadata_path) {
1140 ret = -1;
1141 goto end;
1142 }
1143
3743a302 1144 g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
1a9f7075
PP
1145
1146 if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
1147 ret = 1;
1148 goto end;
1149 }
1150
1151end:
1152 g_string_free(metadata_path, TRUE);
1153 return ret;
1154}
1155
a0cd55ad 1156/* Helper for ctf_fs_component_create_ctf_fs_trace, to handle a single path. */
f280892e 1157
1a9f7075 1158static
a0cd55ad 1159int ctf_fs_component_create_ctf_fs_trace_one_path(
41693723 1160 struct ctf_fs_component *ctf_fs,
d23b766e 1161 const char *path_param,
005d49d6 1162 const char *trace_name,
a0cd55ad 1163 GPtrArray *traces,
d23b766e
SM
1164 bt_self_component *self_comp,
1165 bt_self_component_class *self_comp_class)
1a9f7075 1166{
48881202
SM
1167 struct ctf_fs_trace *ctf_fs_trace;
1168 int ret;
1169 GString *norm_path;
98903a3e 1170 bt_logging_level log_level = ctf_fs->log_level;
1a9f7075 1171
4bd72b60
PP
1172 norm_path = bt_common_normalize_path(path_param, NULL);
1173 if (!norm_path) {
d23b766e 1174 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
b7d2695a
SM
1175 "Failed to normalize path: `%s`.",
1176 path_param);
4bd72b60
PP
1177 goto error;
1178 }
1179
48881202
SM
1180 ret = path_is_ctf_trace(norm_path->str);
1181 if (ret < 0) {
1182 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
b7d2695a
SM
1183 "Failed to check if path is a CTF trace: path=%s",
1184 norm_path->str);
48881202
SM
1185 goto error;
1186 } else if (ret == 0) {
1187 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
b7d2695a
SM
1188 "Path is not a CTF trace (does not contain a metadata file): `%s`.",
1189 norm_path->str);
1a9f7075
PP
1190 goto error;
1191 }
1192
48881202
SM
1193 // FIXME: Remove or ifdef for __MINGW32__
1194 if (strcmp(norm_path->str, "/") == 0) {
d23b766e 1195 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
48881202
SM
1196 "Opening a trace in `/` is not supported.");
1197 ret = -1;
1198 goto end;
1a9f7075
PP
1199 }
1200
b7d2695a 1201 ctf_fs_trace = ctf_fs_trace_create(self_comp, self_comp_class, norm_path->str,
005d49d6 1202 trace_name, &ctf_fs->metadata_config, log_level);
48881202 1203 if (!ctf_fs_trace) {
b7d2695a
SM
1204 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
1205 "Cannot create trace for `%s`.",
48881202 1206 norm_path->str);
1a9f7075
PP
1207 goto error;
1208 }
1209
a0cd55ad 1210 g_ptr_array_add(traces, ctf_fs_trace);
48881202 1211 ctf_fs_trace = NULL;
1a9f7075 1212
48881202 1213 ret = 0;
1a9f7075
PP
1214 goto end;
1215
1216error:
1217 ret = -1;
1a9f7075
PP
1218
1219end:
4bd72b60
PP
1220 if (norm_path) {
1221 g_string_free(norm_path, TRUE);
1222 }
1223
1a9f7075
PP
1224 return ret;
1225}
1226
41a65f30
SM
1227/*
1228 * Count the number of stream and event classes defined by this trace's metadata.
1229 *
1230 * This is used to determine which metadata is the "latest", out of multiple
1231 * traces sharing the same UUID. It is assumed that amongst all these metadatas,
1232 * a bigger metadata is a superset of a smaller metadata. Therefore, it is
1233 * enough to just count the classes.
1234 */
1235
1236static
1237unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace *trace)
1238{
1239 unsigned int num = trace->metadata->tc->stream_classes->len;
1240 guint i;
1241
1242 for (i = 0; i < trace->metadata->tc->stream_classes->len; i++) {
1243 struct ctf_stream_class *sc = trace->metadata->tc->stream_classes->pdata[i];
1244 num += sc->event_classes->len;
1245 }
1246
1247 return num;
1248}
1249
1250/*
1251 * Merge the src ds_file_group into dest. This consists of merging their
1252 * ds_file_infos, making sure to keep the result sorted.
1253 */
1254
1255static
1256void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group *dest, struct ctf_fs_ds_file_group *src)
1257{
1258 guint i;
1259
1260 for (i = 0; i < src->ds_file_infos->len; i++) {
1261 struct ctf_fs_ds_file_info *ds_file_info =
1262 g_ptr_array_index(src->ds_file_infos, i);
1263
1264 /* Ownership of the ds_file_info is transferred to dest. */
1265 g_ptr_array_index(src->ds_file_infos, i) = NULL;
1266
1267 ds_file_group_insert_ds_file_info_sorted(dest, ds_file_info);
1268 }
41a65f30 1269
7ed5243a 1270 /* Merge both indexes. */
ce75de14 1271 merge_ctf_fs_ds_indexes(dest->index, src->index);
7ed5243a 1272}
41a65f30
SM
1273/* Merge src_trace's data stream file groups into dest_trace's. */
1274
1275static
54ef52bd 1276int merge_matching_ctf_fs_ds_file_groups(
41a65f30
SM
1277 struct ctf_fs_trace *dest_trace,
1278 struct ctf_fs_trace *src_trace)
1279{
1280
1281 GPtrArray *dest = dest_trace->ds_file_groups;
1282 GPtrArray *src = src_trace->ds_file_groups;
1283 guint s_i;
54ef52bd 1284 int ret = 0;
41a65f30
SM
1285
1286 /*
1287 * Save the initial length of dest: we only want to check against the
1288 * original elements in the inner loop.
1289 */
1290 const guint dest_len = dest->len;
1291
1292 for (s_i = 0; s_i < src->len; s_i++) {
1293 struct ctf_fs_ds_file_group *src_group = g_ptr_array_index(src, s_i);
1294 struct ctf_fs_ds_file_group *dest_group = NULL;
1295
1296 /* A stream instance without ID can't match a stream in the other trace. */
1297 if (src_group->stream_id != -1) {
1298 guint d_i;
1299
1300 /* Let's search for a matching ds_file_group in the destination. */
1301 for (d_i = 0; d_i < dest_len; d_i++) {
1302 struct ctf_fs_ds_file_group *candidate_dest = g_ptr_array_index(dest, d_i);
1303
1304 /* Can't match a stream instance without ID. */
1305 if (candidate_dest->stream_id == -1) {
1306 continue;
1307 }
1308
1309 /*
1310 * If the two groups have the same stream instance id
1311 * and belong to the same stream class (stream instance
1312 * ids are per-stream class), they represent the same
1313 * stream instance.
1314 */
1315 if (candidate_dest->stream_id != src_group->stream_id ||
1316 candidate_dest->sc->id != src_group->sc->id) {
1317 continue;
1318 }
1319
1320 dest_group = candidate_dest;
1321 break;
1322 }
1323 }
1324
1325 /*
1326 * Didn't find a friend in dest to merge our src_group into?
7ed5243a
FD
1327 * Create a new empty one. This can happen if a stream was
1328 * active in the source trace chunk but not in the destination
1329 * trace chunk.
41a65f30
SM
1330 */
1331 if (!dest_group) {
1332 struct ctf_stream_class *sc;
7ed5243a 1333 struct ctf_fs_ds_index *index;
41a65f30
SM
1334
1335 sc = ctf_trace_class_borrow_stream_class_by_id(
1336 dest_trace->metadata->tc, src_group->sc->id);
1337 BT_ASSERT(sc);
1338
4c65a157
PP
1339 index = ctf_fs_ds_index_create(dest_trace->log_level,
1340 dest_trace->self_comp);
7ed5243a
FD
1341 if (!index) {
1342 ret = -1;
1343 goto end;
1344 }
1345
41a65f30 1346 dest_group = ctf_fs_ds_file_group_create(dest_trace, sc,
7ed5243a
FD
1347 src_group->stream_id, index);
1348 /* Ownership of index is transferred. */
1349 index = NULL;
54ef52bd
FD
1350 if (!dest_group) {
1351 ret = -1;
1352 goto end;
1353 }
41a65f30
SM
1354
1355 g_ptr_array_add(dest_trace->ds_file_groups, dest_group);
1356 }
1357
1358 BT_ASSERT(dest_group);
1359 merge_ctf_fs_ds_file_groups(dest_group, src_group);
1360 }
54ef52bd
FD
1361
1362end:
1363 return ret;
41a65f30
SM
1364}
1365
1366/*
1367 * Collapse the given traces, which must all share the same UUID, in a single
1368 * one.
1369 *
1370 * The trace with the most expansive metadata is chosen and all other traces
1371 * are merged into that one. The array slots of all the traces that get merged
1372 * in the chosen one are set to NULL, so only the slot of the chosen trace
1373 * remains non-NULL.
1374 */
1375
1376static
a0cd55ad
SM
1377int merge_ctf_fs_traces(struct ctf_fs_trace **traces, unsigned int num_traces,
1378 struct ctf_fs_trace **out_trace)
41a65f30
SM
1379{
1380 unsigned int winner_count;
1381 struct ctf_fs_trace *winner;
a0cd55ad 1382 guint i, winner_i;
54ef52bd 1383 int ret = 0;
41a65f30
SM
1384
1385 BT_ASSERT(num_traces >= 2);
1386
1387 winner_count = metadata_count_stream_and_event_classes(traces[0]);
1388 winner = traces[0];
a0cd55ad 1389 winner_i = 0;
41a65f30
SM
1390
1391 /* Find the trace with the largest metadata. */
1392 for (i = 1; i < num_traces; i++) {
1393 struct ctf_fs_trace *candidate;
1394 unsigned int candidate_count;
1395
1396 candidate = traces[i];
1397
1398 /* A bit of sanity check. */
1399 BT_ASSERT(bt_uuid_compare(winner->metadata->tc->uuid, candidate->metadata->tc->uuid) == 0);
1400
1401 candidate_count = metadata_count_stream_and_event_classes(candidate);
1402
1403 if (candidate_count > winner_count) {
1404 winner_count = candidate_count;
1405 winner = candidate;
a0cd55ad 1406 winner_i = i;
41a65f30
SM
1407 }
1408 }
1409
1410 /* Merge all the other traces in the winning trace. */
1411 for (i = 0; i < num_traces; i++) {
1412 struct ctf_fs_trace *trace = traces[i];
1413
1414 /* Don't merge the winner into itself. */
1415 if (trace == winner) {
1416 continue;
1417 }
1418
1419 /* Merge trace's data stream file groups into winner's. */
54ef52bd
FD
1420 ret = merge_matching_ctf_fs_ds_file_groups(winner, trace);
1421 if (ret) {
1422 goto end;
1423 }
41a65f30
SM
1424 }
1425
a0cd55ad
SM
1426 /*
1427 * Move the winner out of the array, into `*out_trace`.
1428 */
1429 *out_trace = winner;
1430 traces[winner_i] = NULL;
54ef52bd
FD
1431
1432end:
1433 return ret;
41a65f30
SM
1434}
1435
1719bf64
FD
1436enum target_event {
1437 FIRST_EVENT,
1438 LAST_EVENT,
1439};
1440
1441static
1442int decode_clock_snapshot_after_event(struct ctf_fs_trace *ctf_fs_trace,
1443 struct ctf_clock_class *default_cc,
1444 struct ctf_fs_ds_index_entry *index_entry,
1445 enum target_event target_event, uint64_t *cs, int64_t *ts_ns)
1446{
18a1979b 1447 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
1719bf64 1448 struct ctf_fs_ds_file *ds_file = NULL;
18a1979b 1449 struct ctf_msg_iter *msg_iter = NULL;
1719bf64
FD
1450 bt_logging_level log_level = ctf_fs_trace->log_level;
1451 bt_self_component *self_comp = ctf_fs_trace->self_comp;
1452 int ret = 0;
1453
1454 BT_ASSERT(ctf_fs_trace);
6d54260a
SM
1455 BT_ASSERT(index_entry);
1456 BT_ASSERT(index_entry->path);
1457
1458 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL,
1459 NULL, index_entry->path, log_level);
1460 if (!ds_file) {
1461 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create a ctf_fs_ds_file");
1462 ret = -1;
1463 goto end;
1464 }
1465
1719bf64
FD
1466 BT_ASSERT(ctf_fs_trace->metadata);
1467 BT_ASSERT(ctf_fs_trace->metadata->tc);
1468
18a1979b 1469 msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
1719bf64 1470 bt_common_get_page_size(log_level) * 8, ctf_fs_ds_file_medops,
6d54260a 1471 ds_file, log_level, self_comp, NULL);
1719bf64 1472 if (!msg_iter) {
18a1979b 1473 /* ctf_msg_iter_create() logs errors. */
1719bf64
FD
1474 ret = -1;
1475 goto end;
1476 }
1477
1719bf64
FD
1478 /*
1479 * Turn on dry run mode to prevent the creation and usage of Babeltrace
1480 * library objects (bt_field, bt_message_*, etc.).
1481 */
18a1979b 1482 ctf_msg_iter_set_dry_run(msg_iter, true);
1719bf64
FD
1483
1484 /* Seek to the beginning of the target packet. */
6d54260a 1485 iter_status = ctf_msg_iter_seek(msg_iter, index_entry->offset);
1719bf64 1486 if (iter_status) {
18a1979b 1487 /* ctf_msg_iter_seek() logs errors. */
1719bf64
FD
1488 ret = -1;
1489 goto end;
1490 }
1491
1492 switch (target_event) {
1493 case FIRST_EVENT:
1494 /*
1495 * Start to decode the packet until we reach the end of
1496 * the first event. To extract the first event's clock
1497 * snapshot.
1498 */
18a1979b 1499 iter_status = ctf_msg_iter_curr_packet_first_event_clock_snapshot(
6d54260a 1500 msg_iter, cs);
1719bf64
FD
1501 break;
1502 case LAST_EVENT:
1503 /* Decode the packet to extract the last event's clock snapshot. */
18a1979b 1504 iter_status = ctf_msg_iter_curr_packet_last_event_clock_snapshot(
6d54260a 1505 msg_iter, cs);
1719bf64
FD
1506 break;
1507 default:
498e7994 1508 bt_common_abort();
1719bf64
FD
1509 }
1510 if (iter_status) {
1511 ret = -1;
1512 goto end;
1513 }
1514
1515 /* Convert clock snapshot to timestamp. */
1516 ret = bt_util_clock_cycles_to_ns_from_origin(*cs,
1517 default_cc->frequency, default_cc->offset_seconds,
1518 default_cc->offset_cycles, ts_ns);
1519 if (ret) {
d23b766e
SM
1520 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1521 "Failed to convert clock snapshot to timestamp");
1719bf64
FD
1522 goto end;
1523 }
1524
1525end:
1526 if (ds_file) {
1527 ctf_fs_ds_file_destroy(ds_file);
1528 }
1529 if (msg_iter) {
18a1979b 1530 ctf_msg_iter_destroy(msg_iter);
1719bf64
FD
1531 }
1532
1533 return ret;
1534}
1535
c43092a5
FD
1536static
1537int decode_packet_first_event_timestamp(struct ctf_fs_trace *ctf_fs_trace,
1538 struct ctf_clock_class *default_cc,
1539 struct ctf_fs_ds_index_entry *index_entry, uint64_t *cs, int64_t *ts_ns)
1540{
1541 return decode_clock_snapshot_after_event(ctf_fs_trace, default_cc,
1542 index_entry, FIRST_EVENT, cs, ts_ns);
1543}
1544
1719bf64
FD
1545static
1546int decode_packet_last_event_timestamp(struct ctf_fs_trace *ctf_fs_trace,
1547 struct ctf_clock_class *default_cc,
1548 struct ctf_fs_ds_index_entry *index_entry, uint64_t *cs, int64_t *ts_ns)
1549{
1550 return decode_clock_snapshot_after_event(ctf_fs_trace, default_cc,
1551 index_entry, LAST_EVENT, cs, ts_ns);
1552}
1553
1554/*
1555 * Fix up packet index entries for lttng's "event-after-packet" bug.
1556 * Some buggy lttng tracer versions may emit events with a timestamp that is
1557 * larger (after) than the timestamp_end of the their packets.
1558 *
1559 * To fix up this erroneous data we do the following:
1560 * 1. If it's not the stream file's last packet: set the packet index entry's
1561 * end time to the next packet's beginning time.
1562 * 2. If it's the stream file's last packet, set the packet index entry's end
1563 * time to the packet's last event's time, if any, or to the packet's
1564 * beginning time otherwise.
1565 *
1566 * Known buggy tracer versions:
1567 * - before lttng-ust 2.11.0
1568 * - before lttng-module 2.11.0
1569 * - before lttng-module 2.10.10
1570 * - before lttng-module 2.9.13
1571 */
1572static
1573int fix_index_lttng_event_after_packet_bug(struct ctf_fs_trace *trace)
1574{
1575 int ret = 0;
1576 guint ds_file_group_i;
1577 GPtrArray *ds_file_groups = trace->ds_file_groups;
1578 bt_logging_level log_level = trace->log_level;
1579
1580 for (ds_file_group_i = 0; ds_file_group_i < ds_file_groups->len;
1581 ds_file_group_i++) {
1582 guint entry_i;
1583 struct ctf_clock_class *default_cc;
1584 struct ctf_fs_ds_index_entry *last_entry;
1585 struct ctf_fs_ds_index *index;
1586
1587 struct ctf_fs_ds_file_group *ds_file_group =
1588 g_ptr_array_index(ds_file_groups, ds_file_group_i);
1589
1590 BT_ASSERT(ds_file_group);
1591 index = ds_file_group->index;
1592
1593 BT_ASSERT(index);
1594 BT_ASSERT(index->entries);
1595 BT_ASSERT(index->entries->len > 0);
1596
1597 /*
1598 * Iterate over all entries but the last one. The last one is
1599 * fixed differently after.
1600 */
1601 for (entry_i = 0; entry_i < index->entries->len - 1;
1602 entry_i++) {
1603 struct ctf_fs_ds_index_entry *curr_entry, *next_entry;
1604
1605 curr_entry = g_ptr_array_index(index->entries, entry_i);
1606 next_entry = g_ptr_array_index(index->entries, entry_i + 1);
1607
1608 /*
1609 * 1. Set the current index entry `end` timestamp to
1610 * the next index entry `begin` timestamp.
1611 */
1612 curr_entry->timestamp_end = next_entry->timestamp_begin;
1613 curr_entry->timestamp_end_ns = next_entry->timestamp_begin_ns;
1614 }
1615
1616 /*
1617 * 2. Fix the last entry by decoding the last event of the last
1618 * packet.
1619 */
1620 last_entry = g_ptr_array_index(index->entries,
1621 index->entries->len - 1);
1622 BT_ASSERT(last_entry);
1623
1624 BT_ASSERT(ds_file_group->sc->default_clock_class);
1625 default_cc = ds_file_group->sc->default_clock_class;
1626
1627 /*
1628 * Decode packet to read the timestamp of the last event of the
1629 * entry.
1630 */
1631 ret = decode_packet_last_event_timestamp(trace, default_cc,
1632 last_entry, &last_entry->timestamp_end,
1633 &last_entry->timestamp_end_ns);
1634 if (ret) {
d23b766e
SM
1635 BT_COMP_LOGE_APPEND_CAUSE(trace->self_comp,
1636 "Failed to decode stream's last packet to get its last event's clock snapshot.");
1719bf64
FD
1637 goto end;
1638 }
1639 }
1640
1641end:
1642 return ret;
1643}
1644
c43092a5
FD
1645/*
1646 * Fix up packet index entries for barectf's "event-before-packet" bug.
1647 * Some buggy barectf tracer versions may emit events with a timestamp that is
1648 * less than the timestamp_begin of the their packets.
1649 *
1650 * To fix up this erroneous data we do the following:
1651 * 1. Starting at the second index entry, set the timestamp_begin of the
1652 * current entry to the timestamp of the first event of the packet.
1653 * 2. Set the previous entry's timestamp_end to the timestamp_begin of the
1654 * current packet.
1655 *
1656 * Known buggy tracer versions:
1657 * - before barectf 2.3.1
1658 */
1659static
1660int fix_index_barectf_event_before_packet_bug(struct ctf_fs_trace *trace)
1661{
1662 int ret = 0;
1663 guint ds_file_group_i;
1664 GPtrArray *ds_file_groups = trace->ds_file_groups;
1665 bt_logging_level log_level = trace->log_level;
1666
1667 for (ds_file_group_i = 0; ds_file_group_i < ds_file_groups->len;
1668 ds_file_group_i++) {
1669 guint entry_i;
1670 struct ctf_clock_class *default_cc;
1671 struct ctf_fs_ds_file_group *ds_file_group =
1672 g_ptr_array_index(ds_file_groups, ds_file_group_i);
1673
1674 struct ctf_fs_ds_index *index = ds_file_group->index;
1675
1676 BT_ASSERT(index);
1677 BT_ASSERT(index->entries);
1678 BT_ASSERT(index->entries->len > 0);
1679
1680 BT_ASSERT(ds_file_group->sc->default_clock_class);
1681 default_cc = ds_file_group->sc->default_clock_class;
1682
1683 /*
1684 * 1. Iterate over the index, starting from the second entry
1685 * (index = 1).
1686 */
1687 for (entry_i = 1; entry_i < index->entries->len;
1688 entry_i++) {
1689 struct ctf_fs_ds_index_entry *curr_entry, *prev_entry;
1690 prev_entry = g_ptr_array_index(index->entries, entry_i - 1);
1691 curr_entry = g_ptr_array_index(index->entries, entry_i);
1692 /*
1693 * 2. Set the current entry `begin` timestamp to the
1694 * timestamp of the first event of the current packet.
1695 */
1696 ret = decode_packet_first_event_timestamp(trace, default_cc,
1697 curr_entry, &curr_entry->timestamp_begin,
1698 &curr_entry->timestamp_begin_ns);
1699 if (ret) {
d23b766e
SM
1700 BT_COMP_LOGE_APPEND_CAUSE(trace->self_comp,
1701 "Failed to decode first event's clock snapshot");
c43092a5
FD
1702 goto end;
1703 }
1704
1705 /*
1706 * 3. Set the previous entry `end` timestamp to the
1707 * timestamp of the first event of the current packet.
1708 */
1709 prev_entry->timestamp_end = curr_entry->timestamp_begin;
1710 prev_entry->timestamp_end_ns = curr_entry->timestamp_begin_ns;
1711 }
1712 }
1713end:
1714 return ret;
1715}
1716
aada78b5
FD
1717/*
1718 * When using the lttng-crash feature it's likely that the last packets of each
1719 * stream have their timestamp_end set to zero. This is caused by the fact that
1720 * the tracer crashed and was not able to properly close the packets.
1721 *
1722 * To fix up this erroneous data we do the following:
1723 * For each index entry, if the entry's timestamp_end is 0 and the
1724 * timestamp_begin is not 0:
1725 * - If it's the stream file's last packet: set the packet index entry's end
1726 * time to the packet's last event's time, if any, or to the packet's
1727 * beginning time otherwise.
1728 * - If it's not the stream file's last packet: set the packet index
1729 * entry's end time to the next packet's beginning time.
1730 *
1731 * Affected versions:
1732 * - All current and future lttng-ust and lttng-modules versions.
1733 */
1734static
1735int fix_index_lttng_crash_quirk(struct ctf_fs_trace *trace)
1736{
1737 int ret = 0;
1738 guint ds_file_group_idx;
1739 GPtrArray *ds_file_groups = trace->ds_file_groups;
1740 bt_logging_level log_level = trace->log_level;
1741
1742 for (ds_file_group_idx = 0; ds_file_group_idx < ds_file_groups->len;
1743 ds_file_group_idx++) {
1744 guint entry_idx;
1745 struct ctf_clock_class *default_cc;
1746 struct ctf_fs_ds_index_entry *last_entry;
1747 struct ctf_fs_ds_index *index;
1748
1749 struct ctf_fs_ds_file_group *ds_file_group =
1750 g_ptr_array_index(ds_file_groups, ds_file_group_idx);
1751
1752 BT_ASSERT(ds_file_group);
1753 index = ds_file_group->index;
1754
1755 BT_ASSERT(ds_file_group->sc->default_clock_class);
1756 default_cc = ds_file_group->sc->default_clock_class;
1757
1758 BT_ASSERT(index);
1759 BT_ASSERT(index->entries);
1760 BT_ASSERT(index->entries->len > 0);
1761
1762 last_entry = g_ptr_array_index(index->entries,
1763 index->entries->len - 1);
1764 BT_ASSERT(last_entry);
1765
1766
1767 /* 1. Fix the last entry first. */
1768 if (last_entry->timestamp_end == 0 &&
1769 last_entry->timestamp_begin != 0) {
1770 /*
1771 * Decode packet to read the timestamp of the
1772 * last event of the stream file.
1773 */
1774 ret = decode_packet_last_event_timestamp(trace,
1775 default_cc, last_entry,
1776 &last_entry->timestamp_end,
1777 &last_entry->timestamp_end_ns);
1778 if (ret) {
d23b766e
SM
1779 BT_COMP_LOGE_APPEND_CAUSE(trace->self_comp,
1780 "Failed to decode last event's clock snapshot");
aada78b5
FD
1781 goto end;
1782 }
1783 }
1784
1785 /* Iterate over all entries but the last one. */
1786 for (entry_idx = 0; entry_idx < index->entries->len - 1;
1787 entry_idx++) {
1788 struct ctf_fs_ds_index_entry *curr_entry, *next_entry;
1789 curr_entry = g_ptr_array_index(index->entries, entry_idx);
1790 next_entry = g_ptr_array_index(index->entries, entry_idx + 1);
1791
1792 if (curr_entry->timestamp_end == 0 &&
1793 curr_entry->timestamp_begin != 0) {
1794 /*
1795 * 2. Set the current index entry `end` timestamp to
1796 * the next index entry `begin` timestamp.
1797 */
1798 curr_entry->timestamp_end = next_entry->timestamp_begin;
1799 curr_entry->timestamp_end_ns = next_entry->timestamp_begin_ns;
1800 }
1801 }
1802 }
1803
1804end:
1805 return ret;
1806}
1807
626cc488
FD
1808/*
1809 * Extract the tracer information necessary to compare versions.
1810 * Returns 0 on success, and -1 if the extraction is not successful because the
1811 * necessary fields are absents in the trace metadata.
1812 */
1813static
626cc488
FD
1814int extract_tracer_info(struct ctf_fs_trace *trace,
1815 struct tracer_info *current_tracer_info)
1816{
1817 int ret = 0;
1818 struct ctf_trace_class_env_entry *entry;
1819
1820 /* Clear the current_tracer_info struct */
1821 memset(current_tracer_info, 0, sizeof(*current_tracer_info));
1822
1823 /*
1824 * To compare 2 tracer versions, at least the tracer name and it's
1825 * major version are needed. If one of these is missing, consider it an
1826 * extraction failure.
1827 */
1828 entry = ctf_trace_class_borrow_env_entry_by_name(
1829 trace->metadata->tc, "tracer_name");
1830 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR) {
1831 goto missing_bare_minimum;
1832 }
1833
1834 /* Set tracer name. */
1835 current_tracer_info->name = entry->value.str->str;
1836
1837 entry = ctf_trace_class_borrow_env_entry_by_name(
1838 trace->metadata->tc, "tracer_major");
1839 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) {
1840 goto missing_bare_minimum;
1841 }
1842
1843 /* Set major version number. */
1844 current_tracer_info->major = entry->value.i;
1845
1846 entry = ctf_trace_class_borrow_env_entry_by_name(
1847 trace->metadata->tc, "tracer_minor");
1848 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) {
1849 goto end;
1850 }
1851
1852 /* Set minor version number. */
1853 current_tracer_info->minor = entry->value.i;
1854
1855 entry = ctf_trace_class_borrow_env_entry_by_name(
1856 trace->metadata->tc, "tracer_patch");
1857 if (!entry) {
1858 /*
1859 * If `tracer_patch` doesn't exist `tracer_patchlevel` might.
1860 * For example, `lttng-modules` uses entry name
1861 * `tracer_patchlevel`.
1862 */
1863 entry = ctf_trace_class_borrow_env_entry_by_name(
1864 trace->metadata->tc, "tracer_patchlevel");
1865 }
1866
1867 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) {
1868 goto end;
1869 }
1870
1871 /* Set patch version number. */
1872 current_tracer_info->patch = entry->value.i;
1873
1874 goto end;
1875
1876missing_bare_minimum:
1877 ret = -1;
1878end:
1879 return ret;
1880}
1881
1719bf64
FD
1882static
1883bool is_tracer_affected_by_lttng_event_after_packet_bug(
1884 struct tracer_info *curr_tracer_info)
1885{
1886 bool is_affected = false;
1887
1888 if (strcmp(curr_tracer_info->name, "lttng-ust") == 0) {
1889 if (curr_tracer_info->major < 2) {
1890 is_affected = true;
1891 } else if (curr_tracer_info->major == 2) {
1892 /* fixed in lttng-ust 2.11.0 */
1893 if (curr_tracer_info->minor < 11) {
1894 is_affected = true;
1895 }
1896 }
1897 } else if (strcmp(curr_tracer_info->name, "lttng-modules") == 0) {
1898 if (curr_tracer_info->major < 2) {
1899 is_affected = true;
1900 } else if (curr_tracer_info->major == 2) {
1901 /* fixed in lttng-modules 2.11.0 */
1902 if (curr_tracer_info->minor == 10) {
1903 /* fixed in lttng-modules 2.10.10 */
1904 if (curr_tracer_info->patch < 10) {
1905 is_affected = true;
1906 }
1907 } else if (curr_tracer_info->minor == 9) {
1908 /* fixed in lttng-modules 2.9.13 */
1909 if (curr_tracer_info->patch < 13) {
1910 is_affected = true;
1911 }
1912 } else if (curr_tracer_info->minor < 9) {
1913 is_affected = true;
1914 }
1915 }
1916 }
1917
1918 return is_affected;
1919}
1920
c43092a5
FD
1921static
1922bool is_tracer_affected_by_barectf_event_before_packet_bug(
1923 struct tracer_info *curr_tracer_info)
1924{
1925 bool is_affected = false;
1926
1927 if (strcmp(curr_tracer_info->name, "barectf") == 0) {
1928 if (curr_tracer_info->major < 2) {
1929 is_affected = true;
1930 } else if (curr_tracer_info->major == 2) {
1931 if (curr_tracer_info->minor < 3) {
1932 is_affected = true;
1933 } else if (curr_tracer_info->minor == 3) {
1934 /* fixed in barectf 2.3.1 */
1935 if (curr_tracer_info->patch < 1) {
1936 is_affected = true;
1937 }
1938 }
1939 }
1940 }
1941
1942 return is_affected;
1943}
1944
aada78b5
FD
1945static
1946bool is_tracer_affected_by_lttng_crash_quirk(
1947 struct tracer_info *curr_tracer_info)
1948{
1949 bool is_affected = false;
1950
1951 /* All LTTng tracer may be affected by this lttng crash quirk. */
1952 if (strcmp(curr_tracer_info->name, "lttng-ust") == 0) {
1953 is_affected = true;
1954 } else if (strcmp(curr_tracer_info->name, "lttng-modules") == 0) {
1955 is_affected = true;
1956 }
1957
1958 return is_affected;
1959}
1960
1719bf64
FD
1961/*
1962 * Looks for trace produced by known buggy tracers and fix up the index
1963 * produced earlier.
1964 */
1965static
d23b766e 1966int fix_packet_index_tracer_bugs(struct ctf_fs_component *ctf_fs,
b7d2695a
SM
1967 bt_self_component *self_comp,
1968 bt_self_component_class *self_comp_class)
1719bf64
FD
1969{
1970 int ret = 0;
1719bf64 1971 struct tracer_info current_tracer_info;
1719bf64
FD
1972 bt_logging_level log_level = ctf_fs->log_level;
1973
a0cd55ad
SM
1974 ret = extract_tracer_info(ctf_fs->trace, &current_tracer_info);
1975 if (ret) {
1976 /*
1977 * A trace may not have all the necessary environment
1978 * entries to do the tracer version comparison.
1979 * At least, the tracer name and major version number
1980 * are needed. Failing to extract these entries is not
1981 * an error.
1982 */
1983 ret = 0;
1984 BT_LOGI_STR("Cannot extract tracer information necessary to compare with buggy versions.");
1985 goto end;;
1986 }
1719bf64 1987
a0cd55ad
SM
1988 /* Check if the trace may be affected by old tracer bugs. */
1989 if (is_tracer_affected_by_lttng_event_after_packet_bug(
1990 &current_tracer_info)) {
1991 BT_LOGI_STR("Trace may be affected by LTTng tracer packet timestamp bug. Fixing up.");
1992 ret = fix_index_lttng_event_after_packet_bug(ctf_fs->trace);
1719bf64 1993 if (ret) {
b7d2695a
SM
1994 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1995 self_comp, self_comp_class,
a0cd55ad
SM
1996 "Failed to fix LTTng event-after-packet bug.");
1997 goto end;
1719bf64 1998 }
a0cd55ad
SM
1999 ctf_fs->trace->metadata->tc->quirks.lttng_event_after_packet = true;
2000 }
c43092a5 2001
a0cd55ad
SM
2002 if (is_tracer_affected_by_barectf_event_before_packet_bug(
2003 &current_tracer_info)) {
2004 BT_LOGI_STR("Trace may be affected by barectf tracer packet timestamp bug. Fixing up.");
2005 ret = fix_index_barectf_event_before_packet_bug(ctf_fs->trace);
2006 if (ret) {
b7d2695a
SM
2007 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2008 self_comp, self_comp_class,
a0cd55ad
SM
2009 "Failed to fix barectf event-before-packet bug.");
2010 goto end;
c43092a5 2011 }
a0cd55ad
SM
2012 ctf_fs->trace->metadata->tc->quirks.barectf_event_before_packet = true;
2013 }
aada78b5 2014
a0cd55ad
SM
2015 if (is_tracer_affected_by_lttng_crash_quirk(
2016 &current_tracer_info)) {
2017 ret = fix_index_lttng_crash_quirk(ctf_fs->trace);
2018 if (ret) {
b7d2695a
SM
2019 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2020 self_comp, self_comp_class,
a0cd55ad
SM
2021 "Failed to fix lttng-crash timestamp quirks.");
2022 goto end;
aada78b5 2023 }
a0cd55ad 2024 ctf_fs->trace->metadata->tc->quirks.lttng_crash = true;
1719bf64 2025 }
a0cd55ad 2026
1719bf64
FD
2027end:
2028 return ret;
2029}
2030
e9b3611f
PP
2031static
2032gint compare_ds_file_groups_by_first_path(gconstpointer a, gconstpointer b)
2033{
2034 struct ctf_fs_ds_file_group * const *ds_file_group_a = a;
2035 struct ctf_fs_ds_file_group * const *ds_file_group_b = b;
2036 const struct ctf_fs_ds_file_info *first_ds_file_info_a;
2037 const struct ctf_fs_ds_file_info *first_ds_file_info_b;
2038
2039 BT_ASSERT((*ds_file_group_a)->ds_file_infos->len > 0);
2040 BT_ASSERT((*ds_file_group_b)->ds_file_infos->len > 0);
2041 first_ds_file_info_a = (*ds_file_group_a)->ds_file_infos->pdata[0];
2042 first_ds_file_info_b = (*ds_file_group_b)->ds_file_infos->pdata[0];
2043 return strcmp(first_ds_file_info_a->path->str,
2044 first_ds_file_info_b->path->str);
2045}
2046
7b69723d
SM
2047static
2048gint compare_strings(gconstpointer p_a, gconstpointer p_b)
2049{
2050 const char *a = *((const char **) p_a);
2051 const char *b = *((const char **) p_b);
2052
2053 return strcmp(a, b);
2054}
2055
a0cd55ad 2056int ctf_fs_component_create_ctf_fs_trace(
f280892e 2057 struct ctf_fs_component *ctf_fs,
d23b766e 2058 const bt_value *paths_value,
005d49d6 2059 const bt_value *trace_name_value,
d23b766e
SM
2060 bt_self_component *self_comp,
2061 bt_self_component_class *self_comp_class)
f280892e
SM
2062{
2063 int ret = 0;
2064 uint64_t i;
1719bf64 2065 bt_logging_level log_level = ctf_fs->log_level;
7b69723d 2066 GPtrArray *paths = NULL;
a0cd55ad 2067 GPtrArray *traces;
005d49d6 2068 const char *trace_name;
f280892e 2069
a0cd55ad
SM
2070 BT_ASSERT(bt_value_get_type(paths_value) == BT_VALUE_TYPE_ARRAY);
2071 BT_ASSERT(!bt_value_array_is_empty(paths_value));
2072
2073 traces = g_ptr_array_new_with_free_func(ctf_fs_trace_destroy_notifier);
2074 if (!traces) {
2075 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2076 "Failed to allocate a GPtrArray.");
2077 goto error;
2078 }
2079
7b69723d
SM
2080 paths = g_ptr_array_new_with_free_func(g_free);
2081 if (!paths) {
2082 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2083 "Failed to allocate a GPtrArray.");
2084 goto error;
2085 }
2086
005d49d6
SM
2087 trace_name = trace_name_value ? bt_value_string_get(trace_name_value) : NULL;
2088
7b69723d
SM
2089 /*
2090 * Create a sorted array of the paths, to make the execution of this
2091 * component deterministic.
2092 */
393729a6 2093 for (i = 0; i < bt_value_array_get_length(paths_value); i++) {
7b69723d
SM
2094 const bt_value *path_value =
2095 bt_value_array_borrow_element_by_index_const(paths_value, i);
73760435 2096 const char *input = bt_value_string_get(path_value);
7b69723d
SM
2097 gchar *input_copy;
2098
2099 input_copy = g_strdup(input);
2100 if (!input_copy) {
2101 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2102 "Failed to copy a string.");
2103 goto error;
2104 }
2105
2106 g_ptr_array_add(paths, input_copy);
2107 }
2108
2109 g_ptr_array_sort(paths, compare_strings);
2110
2111 /* Create a separate ctf_fs_trace object for each path. */
2112 for (i = 0; i < paths->len; i++) {
2113 const char *path = g_ptr_array_index(paths, i);
f280892e 2114
a0cd55ad 2115 ret = ctf_fs_component_create_ctf_fs_trace_one_path(ctf_fs,
7b69723d 2116 path, trace_name, traces, self_comp, self_comp_class);
f280892e
SM
2117 if (ret) {
2118 goto end;
2119 }
2120 }
2121
a0cd55ad
SM
2122 if (traces->len > 1) {
2123 struct ctf_fs_trace *first_trace = (struct ctf_fs_trace *) traces->pdata[0];
2124 const uint8_t *first_trace_uuid = first_trace->metadata->tc->uuid;
2125 struct ctf_fs_trace *trace;
2126
2127 /*
2128 * We have more than one trace, they must all share the same
2129 * UUID, verify that.
2130 */
2131 for (i = 0; i < traces->len; i++) {
2132 struct ctf_fs_trace *this_trace =
2133 (struct ctf_fs_trace *) traces->pdata[i];
2134 const uint8_t *this_trace_uuid = this_trace->metadata->tc->uuid;
2135
2136 if (!this_trace->metadata->tc->is_uuid_set) {
2137 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2138 "Multiple traces given, but a trace does not have a UUID: path=%s",
2139 this_trace->path->str);
2140 goto error;
2141 }
2142
2143 if (bt_uuid_compare(first_trace_uuid, this_trace_uuid) != 0) {
2144 char first_trace_uuid_str[BT_UUID_STR_LEN + 1];
2145 char this_trace_uuid_str[BT_UUID_STR_LEN + 1];
2146
2147 bt_uuid_to_str(first_trace_uuid, first_trace_uuid_str);
2148 bt_uuid_to_str(this_trace_uuid, this_trace_uuid_str);
2149
2150 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2151 "Multiple traces given, but UUIDs don't match: "
2152 "first-trace-uuid=%s, first-trace-path=%s, "
2153 "trace-uuid=%s, trace-path=%s",
2154 first_trace_uuid_str, first_trace->path->str,
2155 this_trace_uuid_str, this_trace->path->str);
2156 goto error;
2157 }
2158 }
2159
2160 ret = merge_ctf_fs_traces((struct ctf_fs_trace **) traces->pdata,
2161 traces->len, &trace);
2162 if (ret) {
2163 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2164 "Failed to merge traces with the same UUID.");
2165 goto error;
2166 }
2167
2168 ctf_fs->trace = trace;
2169 } else {
2170 /* Just one trace, it may or may not have a UUID, both are fine. */
2171 ctf_fs->trace = traces->pdata[0];
2172 traces->pdata[0] = NULL;
1719bf64 2173 }
41a65f30 2174
b7d2695a 2175 ret = fix_packet_index_tracer_bugs(ctf_fs, self_comp, self_comp_class);
1719bf64 2176 if (ret) {
d23b766e
SM
2177 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2178 "Failed to fix packet index tracer bugs.");
1719bf64 2179 }
a0cd55ad 2180
e9b3611f
PP
2181 /*
2182 * Sort data stream file groups by first data stream file info
2183 * path to get a deterministic order. This order influences the
2184 * order of the output ports. It also influences the order of
2185 * the automatic stream IDs if the trace's packet headers do not
2186 * contain a `stream_instance_id` field, in which case the data
2187 * stream file to stream ID association is always the same,
2188 * whatever the build and the system.
2189 *
2190 * Having a deterministic order here can help debugging and
2191 * testing.
2192 */
2193 g_ptr_array_sort(ctf_fs->trace->ds_file_groups,
2194 compare_ds_file_groups_by_first_path);
a0cd55ad
SM
2195 goto end;
2196error:
2197 ret = -1;
2198
f280892e 2199end:
7b69723d
SM
2200 if (traces) {
2201 g_ptr_array_free(traces, TRUE);
2202 }
2203
2204 if (paths) {
2205 g_ptr_array_free(paths, TRUE);
2206 }
2207
f280892e
SM
2208 return ret;
2209}
2210
a38d7650
SM
2211static
2212GString *get_stream_instance_unique_name(
2213 struct ctf_fs_ds_file_group *ds_file_group)
2214{
2215 GString *name;
2216 struct ctf_fs_ds_file_info *ds_file_info;
2217
2218 name = g_string_new(NULL);
2219 if (!name) {
2220 goto end;
2221 }
2222
2223 /*
2224 * If there's more than one stream file in the stream file
2225 * group, the first (earliest) stream file's path is used as
2226 * the stream's unique name.
2227 */
2228 BT_ASSERT(ds_file_group->ds_file_infos->len > 0);
2229 ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0);
2230 g_string_assign(name, ds_file_info->path->str);
2231
2232end:
2233 return name;
2234}
2235
f280892e
SM
2236/* Create the IR stream objects for ctf_fs_trace. */
2237
2238static
2239int create_streams_for_trace(struct ctf_fs_trace *ctf_fs_trace)
2240{
2241 int ret;
2242 GString *name = NULL;
2243 guint i;
98903a3e 2244 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 2245 bt_self_component *self_comp = ctf_fs_trace->self_comp;
f280892e
SM
2246
2247 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
2248 struct ctf_fs_ds_file_group *ds_file_group =
2249 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
2250 name = get_stream_instance_unique_name(ds_file_group);
2251
2252 if (!name) {
2253 goto error;
2254 }
2255
2256 if (ds_file_group->sc->ir_sc) {
2257 BT_ASSERT(ctf_fs_trace->trace);
2258
2259 if (ds_file_group->stream_id == UINT64_C(-1)) {
2260 /* No stream ID: use 0 */
2261 ds_file_group->stream = bt_stream_create_with_id(
2262 ds_file_group->sc->ir_sc,
2263 ctf_fs_trace->trace,
2264 ctf_fs_trace->next_stream_id);
2265 ctf_fs_trace->next_stream_id++;
2266 } else {
2267 /* Specific stream ID */
2268 ds_file_group->stream = bt_stream_create_with_id(
2269 ds_file_group->sc->ir_sc,
2270 ctf_fs_trace->trace,
2271 (uint64_t) ds_file_group->stream_id);
2272 }
2273 } else {
2274 ds_file_group->stream = NULL;
2275 }
2276
2277 if (!ds_file_group->stream) {
d23b766e
SM
2278 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2279 "Cannot create stream for DS file group: "
f280892e
SM
2280 "addr=%p, stream-name=\"%s\"",
2281 ds_file_group, name->str);
2282 goto error;
2283 }
2284
2285 ret = bt_stream_set_name(ds_file_group->stream,
2286 name->str);
2287 if (ret) {
d23b766e 2288 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set stream's name: "
f280892e
SM
2289 "addr=%p, stream-name=\"%s\"",
2290 ds_file_group->stream, name->str);
2291 goto error;
2292 }
2293
2294 g_string_free(name, TRUE);
2295 name = NULL;
2296 }
2297
2298 ret = 0;
2299 goto end;
2300
2301error:
2302 ret = -1;
2303
2304end:
2305
2306 if (name) {
2307 g_string_free(name, TRUE);
2308 }
2309 return ret;
2310}
2311
c24f7ab4
SM
2312static const struct bt_param_validation_value_descr inputs_elem_descr = {
2313 .type = BT_VALUE_TYPE_STRING,
2314};
f280892e 2315
c24f7ab4
SM
2316static const struct bt_param_validation_map_value_entry_descr fs_params_entries_descr[] = {
2317 { "inputs", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, {
2318 BT_VALUE_TYPE_ARRAY,
2319 .array = {
2320 .min_length = 1,
2321 .max_length = BT_PARAM_VALIDATION_INFINITE,
2322 .element_type = &inputs_elem_descr,
f280892e 2323 }
c24f7ab4
SM
2324 }},
2325 { "trace-name", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
2326 { "clock-class-offset-s", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
2327 { "clock-class-offset-ns", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
2328 { "force-clock-class-origin-unix-epoch", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
2329 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
2330};
f280892e 2331
f280892e 2332
d907165c 2333bool read_src_fs_parameters(const bt_value *params,
005d49d6
SM
2334 const bt_value **inputs,
2335 const bt_value **trace_name,
2336 struct ctf_fs_component *ctf_fs,
d23b766e
SM
2337 bt_self_component *self_comp,
2338 bt_self_component_class *self_comp_class) {
d907165c
SM
2339 bool ret;
2340 const bt_value *value;
98903a3e 2341 bt_logging_level log_level = ctf_fs->log_level;
c24f7ab4
SM
2342 enum bt_param_validation_status validate_value_status;
2343 gchar *error = NULL;
2344
2345 validate_value_status = bt_param_validation_validate(params,
2346 fs_params_entries_descr, &error);
2347 if (validate_value_status != BT_PARAM_VALIDATION_STATUS_OK) {
2348 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2349 "%s", error);
2350 ret = false;
2351 goto end;
2352 }
d907165c 2353
73760435
SM
2354 /* inputs parameter */
2355 *inputs = bt_value_map_borrow_entry_value_const(params, "inputs");
d907165c
SM
2356
2357 /* clock-class-offset-s parameter */
2358 value = bt_value_map_borrow_entry_value_const(params,
2359 "clock-class-offset-s");
2360 if (value) {
d907165c 2361 ctf_fs->metadata_config.clock_class_offset_s =
9c08c816 2362 bt_value_integer_signed_get(value);
d907165c
SM
2363 }
2364
2365 /* clock-class-offset-ns parameter */
2366 value = bt_value_map_borrow_entry_value_const(params,
2367 "clock-class-offset-ns");
2368 if (value) {
d907165c 2369 ctf_fs->metadata_config.clock_class_offset_ns =
9c08c816 2370 bt_value_integer_signed_get(value);
d907165c
SM
2371 }
2372
c0aa240b
FD
2373 /* force-clock-class-origin-unix-epoch parameter */
2374 value = bt_value_map_borrow_entry_value_const(params,
2375 "force-clock-class-origin-unix-epoch");
2376 if (value) {
c0aa240b
FD
2377 ctf_fs->metadata_config.force_clock_class_origin_unix_epoch =
2378 bt_value_bool_get(value);
2379 }
2380
005d49d6
SM
2381 /* trace-name parameter */
2382 *trace_name = bt_value_map_borrow_entry_value_const(params, "trace-name");
d907165c
SM
2383
2384 ret = true;
d907165c
SM
2385
2386end:
c24f7ab4 2387 g_free(error);
d907165c
SM
2388 return ret;
2389}
2390
5b29e799 2391static
d94d92ac 2392struct ctf_fs_component *ctf_fs_create(
d23b766e 2393 const bt_value *params,
b7d2695a 2394 bt_self_component_source *self_comp_src)
56a1cced 2395{
f280892e 2396 struct ctf_fs_component *ctf_fs = NULL;
73760435 2397 const bt_value *inputs_value;
005d49d6 2398 const bt_value *trace_name_value;
98903a3e
PP
2399 bt_self_component *self_comp =
2400 bt_self_component_source_as_self_component(self_comp_src);
56a1cced 2401
98903a3e 2402 ctf_fs = ctf_fs_component_create(bt_component_get_logging_level(
4c65a157 2403 bt_self_component_as_component(self_comp)), self_comp);
d907165c 2404 if (!ctf_fs) {
f280892e
SM
2405 goto error;
2406 }
2407
005d49d6 2408 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value,
b7d2695a 2409 ctf_fs, self_comp, NULL)) {
f280892e 2410 goto error;
56a1cced
JG
2411 }
2412
98903a3e 2413 bt_self_component_set_data(self_comp, ctf_fs);
56a1cced 2414
a0cd55ad 2415 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs, inputs_value,
b7d2695a 2416 trace_name_value, self_comp, NULL)) {
4f1f88a6
PP
2417 goto error;
2418 }
2419
a0cd55ad
SM
2420 if (create_streams_for_trace(ctf_fs->trace)) {
2421 goto error;
2422 }
f280892e 2423
a0cd55ad
SM
2424 if (create_ports_for_trace(ctf_fs, ctf_fs->trace, self_comp_src)) {
2425 goto error;
4f1f88a6
PP
2426 }
2427
1ef09eb5
JG
2428 goto end;
2429
56a1cced 2430error:
1a9f7075 2431 ctf_fs_destroy(ctf_fs);
e7a4393b 2432 ctf_fs = NULL;
98903a3e 2433 bt_self_component_set_data(self_comp, NULL);
1a9f7075 2434
1ef09eb5 2435end:
56a1cced
JG
2436 return ctf_fs;
2437}
2438
ea0b4b9e 2439BT_HIDDEN
21a9f056 2440bt_component_class_initialize_method_status ctf_fs_init(
d23b766e 2441 bt_self_component_source *self_comp_src,
59225a3e 2442 bt_self_component_source_configuration *config,
c88dd1cb 2443 const bt_value *params, __attribute__((unused)) void *init_method_data)
ea0b4b9e
JG
2444{
2445 struct ctf_fs_component *ctf_fs;
21a9f056
FD
2446 bt_component_class_initialize_method_status ret =
2447 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
ea0b4b9e 2448
b7d2695a 2449 ctf_fs = ctf_fs_create(params, self_comp_src);
ea0b4b9e 2450 if (!ctf_fs) {
21a9f056 2451 ret = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
ea0b4b9e 2452 }
4c1456f0 2453
ea0b4b9e
JG
2454 return ret;
2455}
33f93973
PP
2456
2457BT_HIDDEN
d24d5663 2458bt_component_class_query_method_status ctf_fs_query(
b19ff26f 2459 bt_self_component_class_source *comp_class,
3c729b9a 2460 bt_private_query_executor *priv_query_exec,
b19ff26f 2461 const char *object, const bt_value *params,
7c14d641 2462 __attribute__((unused)) void *method_data,
b19ff26f 2463 const bt_value **result)
33f93973 2464{
d24d5663
PP
2465 bt_component_class_query_method_status status =
2466 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
3c729b9a
PP
2467 bt_logging_level log_level = bt_query_executor_get_logging_level(
2468 bt_private_query_executor_as_query_executor_const(
2469 priv_query_exec));
33f93973 2470
2242b43d 2471 if (strcmp(object, "metadata-info") == 0) {
98903a3e
PP
2472 status = metadata_info_query(comp_class, params, log_level,
2473 result);
5f2a1585
SM
2474 } else if (strcmp(object, "babeltrace.trace-infos") == 0) {
2475 status = trace_infos_query(comp_class, params, log_level,
98903a3e 2476 result);
1a29b831 2477 } else if (!strcmp(object, "babeltrace.support-info")) {
73760435 2478 status = support_info_query(comp_class, params, log_level, result);
33f93973 2479 } else {
55314f2a 2480 BT_LOGE("Unknown query object `%s`", object);
76b6c2f7 2481 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
04c0ba87 2482 goto end;
33f93973 2483 }
33f93973 2484end:
d94d92ac 2485 return status;
33f93973 2486}
This page took 0.217309 seconds and 4 git commands to generate.