lib: remove self component param. from msg. iterator init. method
[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
f67894f9 28#define BT_COMP_LOG_SELF_COMP self_comp
3d731ea9
PP
29#define BT_LOG_OUTPUT_LEVEL log_level
30#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS"
3fa1b6a3 31#include "logging/comp-logging.h"
3d731ea9 32
57952005 33#include "common/common.h"
71c5da58 34#include <babeltrace2/babeltrace.h>
d126826c 35#include "common/uuid.h"
ea0b4b9e 36#include <glib.h>
57952005 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"
cd03c43c 45#include "../common/metadata/ctf-meta-configure-ir-trace.h"
b09a5592 46#include "../common/msg-iter/msg-iter.h"
04c0ba87 47#include "query.h"
c423217e 48#include "plugins/common/param-validation/param-validation.h"
e7a4393b 49
7b4d28d0
FD
50struct tracer_info {
51 const char *name;
52 int64_t major;
53 int64_t minor;
54 int64_t patch;
55};
56
94cf822e 57static
b09a5592
PP
58void ctf_fs_msg_iter_data_destroy(
59 struct ctf_fs_msg_iter_data *msg_iter_data)
94cf822e 60{
b09a5592 61 if (!msg_iter_data) {
94cf822e
PP
62 return;
63 }
64
b09a5592 65 if (msg_iter_data->msg_iter) {
5d452e1f 66 ctf_msg_iter_destroy(msg_iter_data->msg_iter);
55fa6491
PP
67 }
68
148f02cb
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
148f02cb 74 g_free(msg_iter_data);
bbbd35bf
PP
75}
76
3fd7b79d 77static
68e2deed 78bt_message_iterator_class_next_method_status ctf_fs_iterator_next_one(
b09a5592 79 struct ctf_fs_msg_iter_data *msg_iter_data,
bbbd35bf 80 const bt_message **out_msg)
ea0b4b9e 81{
68e2deed 82 bt_message_iterator_class_next_method_status status;
148f02cb
SM
83 enum ctf_msg_iter_status msg_iter_status;
84 bt_logging_level log_level = msg_iter_data->log_level;
94cf822e 85
148f02cb
SM
86 msg_iter_status = ctf_msg_iter_get_next_message(
87 msg_iter_data->msg_iter, out_msg);
6ff151ad 88
148f02cb
SM
89 switch (msg_iter_status) {
90 case CTF_MSG_ITER_STATUS_OK:
91 /* Cool, message has been written to *out_msg. */
68e2deed 92 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
148f02cb 93 break;
2d06585f 94
148f02cb 95 case CTF_MSG_ITER_STATUS_EOF:
68e2deed 96 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
148f02cb 97 break;
2d06585f 98
148f02cb
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.");
68e2deed 110 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
148f02cb
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.");
68e2deed 116 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
148f02cb
SM
117 break;
118
119 default:
120 bt_common_abort();
94cf822e 121 }
d01e0f33 122
3fd7b79d
PP
123 return status;
124}
125
126BT_HIDDEN
68e2deed 127bt_message_iterator_class_next_method_status ctf_fs_iterator_next(
b09a5592
PP
128 bt_self_message_iterator *iterator,
129 bt_message_array_const msgs, uint64_t capacity,
3fd7b79d
PP
130 uint64_t *count)
131{
68e2deed 132 bt_message_iterator_class_next_method_status status;
b09a5592
PP
133 struct ctf_fs_msg_iter_data *msg_iter_data =
134 bt_self_message_iterator_get_data(iterator);
3fd7b79d
PP
135 uint64_t i = 0;
136
44381fae
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 {
b09a5592 149 status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]);
68e2deed 150 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
3fd7b79d
PP
151 i++;
152 }
44381fae 153 } while (i < capacity &&
68e2deed 154 status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK);
3fd7b79d
PP
155
156 if (i > 0) {
157 /*
158 * Even if ctf_fs_iterator_next_one() returned something
68e2deed 159 * else than BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK, we
b09a5592
PP
160 * accumulated message objects in the output
161 * message array, so we need to return
68e2deed 162 * BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK so that they are
3fd7b79d 163 * transfered to downstream. This other status occurs
b09a5592 164 * again the next time muxer_msg_iter_do_next() is
3fd7b79d 165 * called, possibly without any accumulated
b09a5592 166 * message, in which case we'll return it.
3fd7b79d 167 */
44381fae
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
3fd7b79d 180 *count = i;
68e2deed 181 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
3fd7b79d
PP
182 }
183
44381fae 184end:
3fd7b79d 185 return status;
ea0b4b9e 186}
bfd20a42 187
a02839ae 188BT_HIDDEN
68e2deed 189bt_message_iterator_class_seek_beginning_method_status
fb25b9e3 190ctf_fs_iterator_seek_beginning(bt_self_message_iterator *it)
a02839ae
PP
191{
192 struct ctf_fs_msg_iter_data *msg_iter_data =
193 bt_self_message_iterator_get_data(it);
a02839ae
PP
194
195 BT_ASSERT(msg_iter_data);
a02839ae 196
148f02cb
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
68e2deed 200 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
a02839ae
PP
201}
202
203BT_HIDDEN
b09a5592 204void ctf_fs_iterator_finalize(bt_self_message_iterator *it)
760051fa 205{
b09a5592
PP
206 ctf_fs_msg_iter_data_destroy(
207 bt_self_message_iterator_get_data(it));
760051fa
JG
208}
209
a02839ae 210BT_HIDDEN
68e2deed 211bt_message_iterator_class_initialize_method_status ctf_fs_iterator_init(
b09a5592 212 bt_self_message_iterator *self_msg_iter,
9415de1c 213 bt_self_message_iterator_configuration *config,
8eee8ea2 214 bt_self_component_port_output *self_port)
4c1456f0 215{
4f1f88a6 216 struct ctf_fs_port_data *port_data;
b09a5592 217 struct ctf_fs_msg_iter_data *msg_iter_data = NULL;
68e2deed 218 bt_message_iterator_class_initialize_method_status status;
3d731ea9 219 bt_logging_level log_level;
148f02cb 220 enum ctf_msg_iter_medium_status medium_status;
e1b15e55
PP
221 bt_self_component *self_comp =
222 bt_self_message_iterator_borrow_component(self_msg_iter);
760051fa 223
834e9996 224 port_data = bt_self_component_port_get_data(
bb61965b 225 bt_self_component_port_output_as_self_component_port(
834e9996
PP
226 self_port));
227 BT_ASSERT(port_data);
3d731ea9 228 log_level = port_data->ctf_fs->log_level;
b09a5592
PP
229 msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1);
230 if (!msg_iter_data) {
68e2deed 231 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
94cf822e
PP
232 goto error;
233 }
234
3d731ea9 235 msg_iter_data->log_level = log_level;
f67894f9 236 msg_iter_data->self_comp = self_comp;
1d744019 237 msg_iter_data->self_msg_iter = self_msg_iter;
148f02cb
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
5d452e1f 254 msg_iter_data->msg_iter = ctf_msg_iter_create(
148f02cb 255 msg_iter_data->ds_file_group->ctf_fs_trace->metadata->tc,
3d731ea9 256 bt_common_get_page_size(msg_iter_data->log_level) * 8,
148f02cb
SM
257 ctf_fs_ds_group_medops,
258 msg_iter_data->msg_iter_medops_data,
259 msg_iter_data->log_level,
2428e031 260 self_comp, self_msg_iter);
b09a5592 261 if (!msg_iter_data->msg_iter) {
5c3441d8 262 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot create a CTF message iterator.");
68e2deed 263 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
55fa6491
PP
264 goto error;
265 }
266
c49bf79b
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
b09a5592
PP
276 bt_self_message_iterator_set_data(self_msg_iter,
277 msg_iter_data);
b09a5592 278 msg_iter_data = NULL;
148f02cb 279
68e2deed 280 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
4f1f88a6 281 goto end;
5b29e799 282
4f1f88a6 283error:
b09a5592 284 bt_self_message_iterator_set_data(self_msg_iter, NULL);
4f1f88a6 285
760051fa 286end:
b09a5592 287 ctf_fs_msg_iter_data_destroy(msg_iter_data);
148f02cb 288 return status;
760051fa
JG
289}
290
aab89afc
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
bf5b7748 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
aab89afc 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
bf5b7748
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
229f76ed 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
f67894f9
PP
354struct ctf_fs_component *ctf_fs_component_create(bt_logging_level log_level,
355 bt_self_component *self_comp)
a4792757 356{
bf5b7748 357 struct ctf_fs_component *ctf_fs;
a4792757 358
bf5b7748
SM
359 ctf_fs = g_new0(struct ctf_fs_component, 1);
360 if (!ctf_fs) {
361 goto error;
362 }
4f1f88a6 363
3d731ea9 364 ctf_fs->log_level = log_level;
bf5b7748
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
bf5b7748
SM
371 goto end;
372
373error:
e1cfefd2
SM
374 ctf_fs_destroy(ctf_fs);
375 ctf_fs = NULL;
bf5b7748
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
ddf49b27 387gchar *ctf_fs_make_port_name(struct ctf_fs_ds_file_group *ds_file_group)
d09752f8 388{
ddf49b27 389 GString *name = g_string_new(NULL);
d09752f8 390
ddf49b27
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) {
d126826c 402 char uuid_str[BT_UUID_STR_LEN + 1];
ddf49b27 403
d126826c 404 bt_uuid_to_str(ds_file_group->ctf_fs_trace->metadata->tc->uuid, uuid_str);
ddf49b27
SM
405 g_string_assign(name, uuid_str);
406 } else {
407 g_string_assign(name, ds_file_group->ctf_fs_trace->path->str);
d09752f8
PP
408 }
409
410 /*
ddf49b27
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.
d09752f8 413 */
ddf49b27
SM
414 if (ds_file_group->sc->id != UINT64_C(-1)) {
415 g_string_append_printf(name, " | %" PRIu64, ds_file_group->sc->id);
416 }
d09752f8 417
ddf49b27
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);
d09752f8
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,
5c3441d8
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;
ddf49b27 439 gchar *port_name;
3d731ea9 440 bt_logging_level log_level = ctf_fs->log_level;
5c3441d8
SM
441 bt_self_component *self_comp =
442 bt_self_component_source_as_self_component(self_comp_src);
4f1f88a6 443
ddf49b27 444 port_name = ctf_fs_make_port_name(ds_file_group);
4f1f88a6
PP
445 if (!port_name) {
446 goto error;
447 }
448
f67894f9 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
f7c3ac09 457 port_data->ctf_fs = ctf_fs;
94cf822e 458 port_data->ds_file_group = ds_file_group;
834e9996 459 ret = bt_self_component_source_add_output_port(
5c3441d8 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:
be1b2e7e 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,
5c3441d8
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;
3d731ea9 486 bt_logging_level log_level = ctf_fs_trace->log_level;
5c3441d8
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,
5c3441d8 496 ds_file_group, self_comp_src);
94cf822e 497 if (ret) {
5c3441d8 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,
779f4a23 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
1d8437d6 556 ctf_fs_ds_index_destroy(ds_file_group->index);
779f4a23 557
8c6884d9 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,
28faa416 565 struct ctf_stream_class *sc,
779f4a23
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
779f4a23
FD
582 ds_file_group->index = index;
583
d09752f8 584 ds_file_group->stream_id = stream_instance_id;
28faa416
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);
779f4a23 592 ctf_fs_ds_index_destroy(index);
94cf822e
PP
593 ds_file_group = NULL;
594
595end:
596 return ds_file_group;
597}
598
2b5f969e
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
9f197a0c 615 /* Insert the value. */
2b5f969e
MJ
616 array->pdata[pos] = element;
617}
618
adb623c2
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
57992c6f
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
779f4a23 677static
54dbe4d9
SM
678void ds_index_insert_ds_index_entry_sorted(
679 struct ctf_fs_ds_index *index,
779f4a23
FD
680 struct ctf_fs_ds_index_entry *entry)
681{
682 guint i;
57992c6f 683 struct ctf_fs_ds_index_entry *other_entry;
779f4a23
FD
684
685 /* Find the spot where to insert this index entry. */
54dbe4d9 686 for (i = 0; i < index->entries->len; i++) {
57992c6f 687 other_entry = g_ptr_array_index(index->entries, i);
779f4a23 688
57992c6f 689 if (entry->timestamp_begin_ns <= other_entry->timestamp_begin_ns) {
779f4a23
FD
690 break;
691 }
692 }
693
57992c6f
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 }
54dbe4d9
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
57992c6f 720 * ds_index_insert_ds_index_entry_sorted.
54dbe4d9
SM
721 */
722 g_ptr_array_index(src->entries, i) = NULL;
54dbe4d9
SM
723 ds_index_insert_ds_index_entry_sorted(dest, entry);
724 }
779f4a23
FD
725}
726
94cf822e
PP
727static
728int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
9e550e5f 729 const char *path)
94cf822e 730{
7b33a0e0
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;
55fa6491 737 struct ctf_fs_ds_file *ds_file = NULL;
13772304 738 struct ctf_fs_ds_file_info *ds_file_info = NULL;
97ade20b 739 struct ctf_fs_ds_index *index = NULL;
5d452e1f 740 struct ctf_msg_iter *msg_iter = NULL;
7b33a0e0 741 struct ctf_stream_class *sc = NULL;
5d452e1f 742 struct ctf_msg_iter_packet_properties props;
3d731ea9 743 bt_logging_level log_level = ctf_fs_trace->log_level;
f67894f9 744 bt_self_component *self_comp = ctf_fs_trace->self_comp;
6699263d 745 bt_self_component_class *self_comp_class = ctf_fs_trace->self_comp_class;
94cf822e 746
3b89958b
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. */
5d452e1f 758 msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
3d731ea9 759 bt_common_get_page_size(log_level) * 8,
3b89958b 760 ctf_fs_ds_file_medops, ds_file, log_level, self_comp, NULL);
b09a5592 761 if (!msg_iter) {
f67894f9 762 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
55fa6491
PP
763 goto error;
764 }
765
e5f8db56
SM
766 ctf_msg_iter_set_dry_run(msg_iter, true);
767
3b89958b 768 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
94cf822e 769 if (ret) {
6699263d 770 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
5c3441d8 771 "Cannot get stream file's first packet's header and context fields (`%s`).",
94cf822e
PP
772 path);
773 goto error;
774 }
775
7b33a0e0
PP
776 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
777 props.stream_class_id);
778 BT_ASSERT(sc);
7b33a0e0
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);
35fa110e
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);
7b33a0e0 788 if (ret) {
6699263d 789 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
5c3441d8 790 "Cannot convert clock cycles to nanoseconds from origin (`%s`).",
7b33a0e0
PP
791 path);
792 goto error;
793 }
94cf822e
PP
794 }
795
13772304
FD
796 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns);
797 if (!ds_file_info) {
798 goto error;
799 }
800
3b89958b 801 index = ctf_fs_ds_file_build_index(ds_file, ds_file_info, msg_iter);
97ade20b 802 if (!index) {
80022dd5
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);
80022dd5 807 goto error;
97ade20b
JG
808 }
809
7b33a0e0 810 if (begin_ns == -1) {
94cf822e 811 /*
5b487445 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 */
7b33a0e0 816 stream_instance_id = -1;
94cf822e
PP
817 }
818
7b33a0e0 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,
779f4a23
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
1281f86d
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
7b33a0e0
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
28faa416 851 if (ds_file_group->sc == sc &&
d09752f8
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,
779f4a23
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;
54dbe4d9
SM
870 } else {
871 merge_ctf_fs_ds_indexes(ds_file_group->index, index);
94cf822e
PP
872 }
873
1281f86d
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);
41d87c7b 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 }
d09752f8 888
97ade20b 889 ctf_fs_ds_file_destroy(ds_file);
1281f86d 890 ctf_fs_ds_file_info_destroy(ds_file_info);
55fa6491 891
b09a5592 892 if (msg_iter) {
5d452e1f 893 ctf_msg_iter_destroy(msg_iter);
55fa6491
PP
894 }
895
97ade20b 896 ctf_fs_ds_index_destroy(index);
94cf822e
PP
897 return ret;
898}
899
900static
9e550e5f 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;
3d731ea9 907 bt_logging_level log_level = ctf_fs_trace->log_level;
f67894f9 908 bt_self_component *self_comp = ctf_fs_trace->self_comp;
6699263d 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) {
6699263d
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
acfa8112 924 if (strcmp(basename, CTF_FS_METADATA_FILENAME) == 0) {
e7a4393b 925 /* Ignore the metadata stream. */
f67894f9 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] == '.') {
f67894f9 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. */
f67894f9 938 file = ctf_fs_file_create(log_level, self_comp);
e7a4393b 939 if (!file) {
6699263d 940 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
5c3441d8 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. */
2c7bce29 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)) {
f67894f9 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) {
6699263d
SM
959 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
960 "Cannot open stream file `%s`",
5c3441d8 961 file->path->str);
e7a4393b
JG
962 goto error;
963 }
964
9fa0891a
JG
965 if (file->size == 0) {
966 /* Skip empty stream. */
f67894f9 967 BT_COMP_LOGI("Ignoring empty file `%s`", file->path->str);
9fa0891a
JG
968 ctf_fs_file_destroy(file);
969 continue;
970 }
971
9e550e5f 972 ret = add_ds_file_to_ds_file_group(ctf_fs_trace,
94cf822e 973 file->path->str);
4f1f88a6 974 if (ret) {
6699263d 975 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
5c3441d8 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
10b7a2e4 1003static
3d731ea9 1004int set_trace_name(bt_trace *trace, const char *name_suffix,
f67894f9 1005 bt_logging_level log_level, bt_self_component *self_comp)
10b7a2e4
PP
1006{
1007 int ret = 0;
8eee8ea2 1008 const bt_value *val;
10b7a2e4
PP
1009 GString *name;
1010
1011 name = g_string_new(NULL);
1012 if (!name) {
f67894f9 1013 BT_COMP_LOGE_STR("Failed to allocate a GString.");
10b7a2e4
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 */
cd03c43c
PP
1022 val = bt_trace_borrow_environment_entry_value_by_name_const(
1023 trace, "hostname");
10b7a2e4
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
bf5b7748 1051static
6699263d
SM
1052struct ctf_fs_trace *ctf_fs_trace_create(
1053 bt_self_component *self_comp,
1054 bt_self_component_class *self_comp_class,
e5e71bf8 1055 const char *path, const char *name,
3d731ea9
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
6699263d
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
3d731ea9 1070 ctf_fs_trace->log_level = log_level;
f67894f9 1071 ctf_fs_trace->self_comp = self_comp;
6699263d 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
7b33a0e0 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
f67894f9
PP
1090 ret = ctf_fs_metadata_set_trace_class(self_comp, ctf_fs_trace,
1091 metadata_config);
10b7a2e4
PP
1092 if (ret) {
1093 goto error;
1094 }
1095
e5e71bf8
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 }
10b7a2e4
PP
1102 }
1103
e5e71bf8 1104 if (ctf_fs_trace->trace) {
cd03c43c
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
f67894f9
PP
1111 ret = set_trace_name(ctf_fs_trace->trace, name, log_level,
1112 self_comp);
e5e71bf8
PP
1113 if (ret) {
1114 goto error;
1115 }
1a9f7075
PP
1116 }
1117
9e550e5f 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;
7b33a0e0 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
2c7bce29 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
aab89afc 1156/* Helper for ctf_fs_component_create_ctf_fs_trace, to handle a single path. */
bf5b7748 1157
1a9f7075 1158static
aab89afc 1159int ctf_fs_component_create_ctf_fs_trace_one_path(
e5e71bf8 1160 struct ctf_fs_component *ctf_fs,
5c3441d8 1161 const char *path_param,
c77d62f1 1162 const char *trace_name,
aab89afc 1163 GPtrArray *traces,
5c3441d8
SM
1164 bt_self_component *self_comp,
1165 bt_self_component_class *self_comp_class)
1a9f7075 1166{
54e1a1aa
SM
1167 struct ctf_fs_trace *ctf_fs_trace;
1168 int ret;
1169 GString *norm_path;
3d731ea9 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) {
5c3441d8 1174 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
6699263d
SM
1175 "Failed to normalize path: `%s`.",
1176 path_param);
4bd72b60
PP
1177 goto error;
1178 }
1179
54e1a1aa
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,
6699263d
SM
1183 "Failed to check if path is a CTF trace: path=%s",
1184 norm_path->str);
54e1a1aa
SM
1185 goto error;
1186 } else if (ret == 0) {
1187 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
6699263d
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
54e1a1aa
SM
1193 // FIXME: Remove or ifdef for __MINGW32__
1194 if (strcmp(norm_path->str, "/") == 0) {
5c3441d8 1195 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
54e1a1aa
SM
1196 "Opening a trace in `/` is not supported.");
1197 ret = -1;
1198 goto end;
1a9f7075
PP
1199 }
1200
6699263d 1201 ctf_fs_trace = ctf_fs_trace_create(self_comp, self_comp_class, norm_path->str,
c77d62f1 1202 trace_name, &ctf_fs->metadata_config, log_level);
54e1a1aa 1203 if (!ctf_fs_trace) {
6699263d
SM
1204 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
1205 "Cannot create trace for `%s`.",
54e1a1aa 1206 norm_path->str);
1a9f7075
PP
1207 goto error;
1208 }
1209
aab89afc 1210 g_ptr_array_add(traces, ctf_fs_trace);
54e1a1aa 1211 ctf_fs_trace = NULL;
1a9f7075 1212
54e1a1aa 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
adb623c2
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 }
adb623c2 1269
779f4a23 1270 /* Merge both indexes. */
54dbe4d9 1271 merge_ctf_fs_ds_indexes(dest->index, src->index);
779f4a23 1272}
adb623c2
SM
1273/* Merge src_trace's data stream file groups into dest_trace's. */
1274
1275static
f80e9344 1276int merge_matching_ctf_fs_ds_file_groups(
adb623c2
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;
f80e9344 1284 int ret = 0;
adb623c2
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?
779f4a23
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.
adb623c2
SM
1330 */
1331 if (!dest_group) {
1332 struct ctf_stream_class *sc;
779f4a23 1333 struct ctf_fs_ds_index *index;
adb623c2
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
f67894f9
PP
1339 index = ctf_fs_ds_index_create(dest_trace->log_level,
1340 dest_trace->self_comp);
779f4a23
FD
1341 if (!index) {
1342 ret = -1;
1343 goto end;
1344 }
1345
adb623c2 1346 dest_group = ctf_fs_ds_file_group_create(dest_trace, sc,
779f4a23
FD
1347 src_group->stream_id, index);
1348 /* Ownership of index is transferred. */
1349 index = NULL;
f80e9344
FD
1350 if (!dest_group) {
1351 ret = -1;
1352 goto end;
1353 }
adb623c2
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 }
f80e9344
FD
1361
1362end:
1363 return ret;
adb623c2
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
aab89afc
SM
1377int merge_ctf_fs_traces(struct ctf_fs_trace **traces, unsigned int num_traces,
1378 struct ctf_fs_trace **out_trace)
adb623c2
SM
1379{
1380 unsigned int winner_count;
1381 struct ctf_fs_trace *winner;
aab89afc 1382 guint i, winner_i;
f80e9344 1383 int ret = 0;
adb623c2
SM
1384
1385 BT_ASSERT(num_traces >= 2);
1386
1387 winner_count = metadata_count_stream_and_event_classes(traces[0]);
1388 winner = traces[0];
aab89afc 1389 winner_i = 0;
adb623c2
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;
aab89afc 1406 winner_i = i;
adb623c2
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. */
f80e9344
FD
1420 ret = merge_matching_ctf_fs_ds_file_groups(winner, trace);
1421 if (ret) {
1422 goto end;
1423 }
adb623c2
SM
1424 }
1425
aab89afc
SM
1426 /*
1427 * Move the winner out of the array, into `*out_trace`.
1428 */
1429 *out_trace = winner;
1430 traces[winner_i] = NULL;
f80e9344
FD
1431
1432end:
1433 return ret;
adb623c2
SM
1434}
1435
df7002e4
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{
5d452e1f 1447 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
df7002e4 1448 struct ctf_fs_ds_file *ds_file = NULL;
5d452e1f 1449 struct ctf_msg_iter *msg_iter = NULL;
df7002e4
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);
3b89958b
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
df7002e4
FD
1466 BT_ASSERT(ctf_fs_trace->metadata);
1467 BT_ASSERT(ctf_fs_trace->metadata->tc);
1468
5d452e1f 1469 msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
df7002e4 1470 bt_common_get_page_size(log_level) * 8, ctf_fs_ds_file_medops,
3b89958b 1471 ds_file, log_level, self_comp, NULL);
df7002e4 1472 if (!msg_iter) {
5d452e1f 1473 /* ctf_msg_iter_create() logs errors. */
df7002e4
FD
1474 ret = -1;
1475 goto end;
1476 }
1477
df7002e4
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 */
5d452e1f 1482 ctf_msg_iter_set_dry_run(msg_iter, true);
df7002e4
FD
1483
1484 /* Seek to the beginning of the target packet. */
3b89958b 1485 iter_status = ctf_msg_iter_seek(msg_iter, index_entry->offset);
df7002e4 1486 if (iter_status) {
5d452e1f 1487 /* ctf_msg_iter_seek() logs errors. */
df7002e4
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 */
5d452e1f 1499 iter_status = ctf_msg_iter_curr_packet_first_event_clock_snapshot(
3b89958b 1500 msg_iter, cs);
df7002e4
FD
1501 break;
1502 case LAST_EVENT:
1503 /* Decode the packet to extract the last event's clock snapshot. */
5d452e1f 1504 iter_status = ctf_msg_iter_curr_packet_last_event_clock_snapshot(
3b89958b 1505 msg_iter, cs);
df7002e4
FD
1506 break;
1507 default:
24847fc7 1508 bt_common_abort();
df7002e4
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) {
5c3441d8
SM
1520 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1521 "Failed to convert clock snapshot to timestamp");
df7002e4
FD
1522 goto end;
1523 }
1524
1525end:
1526 if (ds_file) {
1527 ctf_fs_ds_file_destroy(ds_file);
1528 }
1529 if (msg_iter) {
5d452e1f 1530 ctf_msg_iter_destroy(msg_iter);
df7002e4
FD
1531 }
1532
1533 return ret;
1534}
1535
37ea728c
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
df7002e4
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) {
5c3441d8
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.");
df7002e4
FD
1637 goto end;
1638 }
1639 }
1640
1641end:
1642 return ret;
1643}
1644
37ea728c
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) {
5c3441d8
SM
1700 BT_COMP_LOGE_APPEND_CAUSE(trace->self_comp,
1701 "Failed to decode first event's clock snapshot");
37ea728c
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
7645f10f
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) {
5c3441d8
SM
1779 BT_COMP_LOGE_APPEND_CAUSE(trace->self_comp,
1780 "Failed to decode last event's clock snapshot");
7645f10f
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
7b4d28d0
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
7b4d28d0
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
df7002e4
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
37ea728c
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
7645f10f
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
df7002e4
FD
1961/*
1962 * Looks for trace produced by known buggy tracers and fix up the index
1963 * produced earlier.
1964 */
1965static
5c3441d8 1966int fix_packet_index_tracer_bugs(struct ctf_fs_component *ctf_fs,
6699263d
SM
1967 bt_self_component *self_comp,
1968 bt_self_component_class *self_comp_class)
df7002e4
FD
1969{
1970 int ret = 0;
df7002e4 1971 struct tracer_info current_tracer_info;
df7002e4
FD
1972 bt_logging_level log_level = ctf_fs->log_level;
1973
aab89afc
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 }
df7002e4 1987
aab89afc
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);
df7002e4 1993 if (ret) {
6699263d
SM
1994 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
1995 self_comp, self_comp_class,
aab89afc
SM
1996 "Failed to fix LTTng event-after-packet bug.");
1997 goto end;
df7002e4 1998 }
aab89afc
SM
1999 ctf_fs->trace->metadata->tc->quirks.lttng_event_after_packet = true;
2000 }
37ea728c 2001
aab89afc
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) {
6699263d
SM
2007 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2008 self_comp, self_comp_class,
aab89afc
SM
2009 "Failed to fix barectf event-before-packet bug.");
2010 goto end;
37ea728c 2011 }
aab89afc
SM
2012 ctf_fs->trace->metadata->tc->quirks.barectf_event_before_packet = true;
2013 }
7645f10f 2014
aab89afc
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) {
6699263d
SM
2019 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
2020 self_comp, self_comp_class,
aab89afc
SM
2021 "Failed to fix lttng-crash timestamp quirks.");
2022 goto end;
7645f10f 2023 }
aab89afc 2024 ctf_fs->trace->metadata->tc->quirks.lttng_crash = true;
df7002e4 2025 }
aab89afc 2026
df7002e4
FD
2027end:
2028 return ret;
2029}
2030
562e23c1
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
18118efb
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
aab89afc 2056int ctf_fs_component_create_ctf_fs_trace(
bf5b7748 2057 struct ctf_fs_component *ctf_fs,
5c3441d8 2058 const bt_value *paths_value,
c77d62f1 2059 const bt_value *trace_name_value,
5c3441d8
SM
2060 bt_self_component *self_comp,
2061 bt_self_component_class *self_comp_class)
bf5b7748
SM
2062{
2063 int ret = 0;
2064 uint64_t i;
df7002e4 2065 bt_logging_level log_level = ctf_fs->log_level;
18118efb 2066 GPtrArray *paths = NULL;
aab89afc 2067 GPtrArray *traces;
c77d62f1 2068 const char *trace_name;
bf5b7748 2069
aab89afc
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
18118efb
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
c77d62f1
SM
2087 trace_name = trace_name_value ? bt_value_string_get(trace_name_value) : NULL;
2088
18118efb
SM
2089 /*
2090 * Create a sorted array of the paths, to make the execution of this
2091 * component deterministic.
2092 */
1270d0e9 2093 for (i = 0; i < bt_value_array_get_length(paths_value); i++) {
18118efb
SM
2094 const bt_value *path_value =
2095 bt_value_array_borrow_element_by_index_const(paths_value, i);
a1040187 2096 const char *input = bt_value_string_get(path_value);
18118efb
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);
bf5b7748 2114
aab89afc 2115 ret = ctf_fs_component_create_ctf_fs_trace_one_path(ctf_fs,
18118efb 2116 path, trace_name, traces, self_comp, self_comp_class);
bf5b7748
SM
2117 if (ret) {
2118 goto end;
2119 }
2120 }
2121
aab89afc
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;
df7002e4 2173 }
adb623c2 2174
6699263d 2175 ret = fix_packet_index_tracer_bugs(ctf_fs, self_comp, self_comp_class);
df7002e4 2176 if (ret) {
5c3441d8
SM
2177 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class,
2178 "Failed to fix packet index tracer bugs.");
df7002e4 2179 }
aab89afc 2180
562e23c1
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);
aab89afc
SM
2195 goto end;
2196error:
2197 ret = -1;
2198
bf5b7748 2199end:
18118efb
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
bf5b7748
SM
2208 return ret;
2209}
2210
ddf49b27
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
bf5b7748
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;
3d731ea9 2244 bt_logging_level log_level = ctf_fs_trace->log_level;
f67894f9 2245 bt_self_component *self_comp = ctf_fs_trace->self_comp;
bf5b7748
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) {
5c3441d8
SM
2278 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
2279 "Cannot create stream for DS file group: "
bf5b7748
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) {
5c3441d8 2288 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set stream's name: "
bf5b7748
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
c423217e
SM
2312static const struct bt_param_validation_value_descr inputs_elem_descr = {
2313 .type = BT_VALUE_TYPE_STRING,
2314};
bf5b7748 2315
c423217e
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,
bf5b7748 2323 }
c423217e
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};
bf5b7748 2331
bf5b7748 2332
c80d4c65 2333bool read_src_fs_parameters(const bt_value *params,
c77d62f1
SM
2334 const bt_value **inputs,
2335 const bt_value **trace_name,
2336 struct ctf_fs_component *ctf_fs,
5c3441d8
SM
2337 bt_self_component *self_comp,
2338 bt_self_component_class *self_comp_class) {
c80d4c65
SM
2339 bool ret;
2340 const bt_value *value;
3d731ea9 2341 bt_logging_level log_level = ctf_fs->log_level;
c423217e
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 }
c80d4c65 2353
a1040187
SM
2354 /* inputs parameter */
2355 *inputs = bt_value_map_borrow_entry_value_const(params, "inputs");
c80d4c65
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) {
c80d4c65 2361 ctf_fs->metadata_config.clock_class_offset_s =
60bbfc7c 2362 bt_value_integer_signed_get(value);
c80d4c65
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) {
c80d4c65 2369 ctf_fs->metadata_config.clock_class_offset_ns =
60bbfc7c 2370 bt_value_integer_signed_get(value);
c80d4c65
SM
2371 }
2372
1ffc68e8
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) {
1ffc68e8
FD
2377 ctf_fs->metadata_config.force_clock_class_origin_unix_epoch =
2378 bt_value_bool_get(value);
2379 }
2380
c77d62f1
SM
2381 /* trace-name parameter */
2382 *trace_name = bt_value_map_borrow_entry_value_const(params, "trace-name");
c80d4c65
SM
2383
2384 ret = true;
c80d4c65
SM
2385
2386end:
c423217e 2387 g_free(error);
c80d4c65
SM
2388 return ret;
2389}
2390
5b29e799 2391static
834e9996 2392struct ctf_fs_component *ctf_fs_create(
5c3441d8 2393 const bt_value *params,
6699263d 2394 bt_self_component_source *self_comp_src)
56a1cced 2395{
bf5b7748 2396 struct ctf_fs_component *ctf_fs = NULL;
a1040187 2397 const bt_value *inputs_value;
c77d62f1 2398 const bt_value *trace_name_value;
3d731ea9
PP
2399 bt_self_component *self_comp =
2400 bt_self_component_source_as_self_component(self_comp_src);
56a1cced 2401
3d731ea9 2402 ctf_fs = ctf_fs_component_create(bt_component_get_logging_level(
f67894f9 2403 bt_self_component_as_component(self_comp)), self_comp);
c80d4c65 2404 if (!ctf_fs) {
bf5b7748
SM
2405 goto error;
2406 }
2407
c77d62f1 2408 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value,
6699263d 2409 ctf_fs, self_comp, NULL)) {
bf5b7748 2410 goto error;
56a1cced
JG
2411 }
2412
3d731ea9 2413 bt_self_component_set_data(self_comp, ctf_fs);
56a1cced 2414
aab89afc 2415 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs, inputs_value,
6699263d 2416 trace_name_value, self_comp, NULL)) {
4f1f88a6
PP
2417 goto error;
2418 }
2419
aab89afc
SM
2420 if (create_streams_for_trace(ctf_fs->trace)) {
2421 goto error;
2422 }
bf5b7748 2423
aab89afc
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;
3d731ea9 2433 bt_self_component_set_data(self_comp, NULL);
1a9f7075 2434
1ef09eb5 2435end:
56a1cced
JG
2436 return ctf_fs;
2437}
2438
ea0b4b9e 2439BT_HIDDEN
4175c1d5 2440bt_component_class_initialize_method_status ctf_fs_init(
5c3441d8 2441 bt_self_component_source *self_comp_src,
e3250e61 2442 bt_self_component_source_configuration *config,
f807570c 2443 const bt_value *params, __attribute__((unused)) void *init_method_data)
ea0b4b9e
JG
2444{
2445 struct ctf_fs_component *ctf_fs;
4175c1d5
FD
2446 bt_component_class_initialize_method_status ret =
2447 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
ea0b4b9e 2448
6699263d 2449 ctf_fs = ctf_fs_create(params, self_comp_src);
ea0b4b9e 2450 if (!ctf_fs) {
4175c1d5 2451 ret = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
ea0b4b9e 2452 }
4c1456f0 2453
ea0b4b9e
JG
2454 return ret;
2455}
33f93973
PP
2456
2457BT_HIDDEN
fb25b9e3 2458bt_component_class_query_method_status ctf_fs_query(
8eee8ea2 2459 bt_self_component_class_source *comp_class,
bf403eb2 2460 bt_private_query_executor *priv_query_exec,
8eee8ea2 2461 const char *object, const bt_value *params,
f6e305ce 2462 __attribute__((unused)) void *method_data,
8eee8ea2 2463 const bt_value **result)
33f93973 2464{
fb25b9e3
PP
2465 bt_component_class_query_method_status status =
2466 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
bf403eb2
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
acfa8112 2471 if (strcmp(object, "metadata-info") == 0) {
3d731ea9
PP
2472 status = metadata_info_query(comp_class, params, log_level,
2473 result);
9db4399f
SM
2474 } else if (strcmp(object, "babeltrace.trace-infos") == 0) {
2475 status = trace_infos_query(comp_class, params, log_level,
3d731ea9 2476 result);
9e534aae 2477 } else if (!strcmp(object, "babeltrace.support-info")) {
a1040187 2478 status = support_info_query(comp_class, params, log_level, result);
33f93973 2479 } else {
55314f2a 2480 BT_LOGE("Unknown query object `%s`", object);
7dd3e712 2481 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
04c0ba87 2482 goto end;
33f93973 2483 }
33f93973 2484end:
834e9996 2485 return status;
33f93973 2486}
This page took 0.23905 seconds and 4 git commands to generate.