tests/lib: remove `test_bt_values` and `test_graph_topo`
[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"
4c65a157 31#include "plugins/comp-logging.h"
98903a3e 32
578e048b 33#include "common/common.h"
3fadfbc0 34#include <babeltrace2/babeltrace.h>
578e048b 35#include "compat/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"
d6e69534 45#include "../common/msg-iter/msg-iter.h"
04c0ba87 46#include "query.h"
e7a4393b 47
94cf822e 48static
d6e69534 49int msg_iter_data_set_current_ds_file(struct ctf_fs_msg_iter_data *msg_iter_data)
94cf822e
PP
50{
51 struct ctf_fs_ds_file_info *ds_file_info;
52 int ret = 0;
53
d6e69534
PP
54 BT_ASSERT(msg_iter_data->ds_file_info_index <
55 msg_iter_data->ds_file_group->ds_file_infos->len);
94cf822e 56 ds_file_info = g_ptr_array_index(
d6e69534
PP
57 msg_iter_data->ds_file_group->ds_file_infos,
58 msg_iter_data->ds_file_info_index);
59
60 ctf_fs_ds_file_destroy(msg_iter_data->ds_file);
61 msg_iter_data->ds_file = ctf_fs_ds_file_create(
62 msg_iter_data->ds_file_group->ctf_fs_trace,
63 msg_iter_data->pc_msg_iter,
64 msg_iter_data->msg_iter,
65 msg_iter_data->ds_file_group->stream,
98903a3e
PP
66 ds_file_info->path->str,
67 msg_iter_data->log_level);
d6e69534 68 if (!msg_iter_data->ds_file) {
94cf822e
PP
69 ret = -1;
70 }
71
72 return ret;
73}
74
75static
d6e69534
PP
76void ctf_fs_msg_iter_data_destroy(
77 struct ctf_fs_msg_iter_data *msg_iter_data)
94cf822e 78{
d6e69534 79 if (!msg_iter_data) {
94cf822e
PP
80 return;
81 }
82
d6e69534 83 ctf_fs_ds_file_destroy(msg_iter_data->ds_file);
6de92955 84
d6e69534
PP
85 if (msg_iter_data->msg_iter) {
86 bt_msg_iter_destroy(msg_iter_data->msg_iter);
6de92955
PP
87 }
88
d6e69534 89 g_free(msg_iter_data);
94cf822e
PP
90}
91
fc917f65
PP
92static
93void set_msg_iter_emits_stream_beginning_end_messages(
94 struct ctf_fs_msg_iter_data *msg_iter_data)
95{
96 bt_msg_iter_set_emit_stream_beginning_message(
97 msg_iter_data->ds_file->msg_iter,
98 msg_iter_data->ds_file_info_index == 0);
99 bt_msg_iter_set_emit_stream_end_message(
100 msg_iter_data->ds_file->msg_iter,
101 msg_iter_data->ds_file_info_index ==
102 msg_iter_data->ds_file_group->ds_file_infos->len - 1);
103}
104
d4393e08 105static
4cdfc5e8 106bt_self_message_iterator_status ctf_fs_iterator_next_one(
d6e69534 107 struct ctf_fs_msg_iter_data *msg_iter_data,
fc917f65 108 const bt_message **out_msg)
ea0b4b9e 109{
4cdfc5e8 110 bt_self_message_iterator_status status;
94cf822e 111
d6e69534 112 BT_ASSERT(msg_iter_data->ds_file);
f42867e2 113
fc917f65
PP
114 while (true) {
115 bt_message *msg;
116
117 status = ctf_fs_ds_file_next(msg_iter_data->ds_file, &msg);
118 switch (status) {
119 case BT_SELF_MESSAGE_ITERATOR_STATUS_OK:
120 *out_msg = msg;
121 msg = NULL;
f42867e2 122 goto end;
fc917f65
PP
123 case BT_SELF_MESSAGE_ITERATOR_STATUS_END:
124 {
125 int ret;
f42867e2 126
fc917f65
PP
127 if (msg_iter_data->ds_file_info_index ==
128 msg_iter_data->ds_file_group->ds_file_infos->len - 1) {
129 /* End of all group's stream files */
130 goto end;
131 }
132
133 msg_iter_data->ds_file_info_index++;
495490c5
PP
134 bt_msg_iter_reset_for_next_stream_file(
135 msg_iter_data->msg_iter);
fc917f65
PP
136 set_msg_iter_emits_stream_beginning_end_messages(
137 msg_iter_data);
94cf822e 138
94cf822e 139 /*
fc917f65
PP
140 * Open and start reading the next stream file
141 * within our stream file group.
94cf822e 142 */
fc917f65
PP
143 ret = msg_iter_data_set_current_ds_file(msg_iter_data);
144 if (ret) {
145 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
146 goto end;
147 }
148
149 /* Continue the loop to get the next message */
150 break;
94cf822e 151 }
fc917f65 152 default:
94cf822e
PP
153 goto end;
154 }
94cf822e 155 }
d01e0f33 156
94cf822e 157end:
d4393e08
PP
158 return status;
159}
160
161BT_HIDDEN
4cdfc5e8 162bt_self_message_iterator_status ctf_fs_iterator_next(
d6e69534
PP
163 bt_self_message_iterator *iterator,
164 bt_message_array_const msgs, uint64_t capacity,
d4393e08
PP
165 uint64_t *count)
166{
4cdfc5e8 167 bt_self_message_iterator_status status =
d6e69534
PP
168 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
169 struct ctf_fs_msg_iter_data *msg_iter_data =
170 bt_self_message_iterator_get_data(iterator);
d4393e08
PP
171 uint64_t i = 0;
172
d6e69534
PP
173 while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
174 status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]);
175 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
d4393e08
PP
176 i++;
177 }
178 }
179
180 if (i > 0) {
181 /*
182 * Even if ctf_fs_iterator_next_one() returned something
d6e69534
PP
183 * else than BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
184 * accumulated message objects in the output
185 * message array, so we need to return
186 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they are
d4393e08 187 * transfered to downstream. This other status occurs
d6e69534 188 * again the next time muxer_msg_iter_do_next() is
d4393e08 189 * called, possibly without any accumulated
d6e69534 190 * message, in which case we'll return it.
d4393e08
PP
191 */
192 *count = i;
d6e69534 193 status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
d4393e08
PP
194 }
195
196 return status;
ea0b4b9e 197}
bfd20a42 198
6a9bb5e9
PP
199static
200int ctf_fs_iterator_reset(struct ctf_fs_msg_iter_data *msg_iter_data)
201{
202 int ret;
203
204 msg_iter_data->ds_file_info_index = 0;
205 ret = msg_iter_data_set_current_ds_file(msg_iter_data);
206 if (ret) {
207 goto end;
208 }
209
210 bt_msg_iter_reset(msg_iter_data->msg_iter);
211 set_msg_iter_emits_stream_beginning_end_messages(msg_iter_data);
212
213end:
214 return ret;
215}
216
217BT_HIDDEN
218bt_self_message_iterator_status ctf_fs_iterator_seek_beginning(
219 bt_self_message_iterator *it)
220{
221 struct ctf_fs_msg_iter_data *msg_iter_data =
222 bt_self_message_iterator_get_data(it);
223 bt_self_message_iterator_status status =
224 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
225
226 BT_ASSERT(msg_iter_data);
227 if (ctf_fs_iterator_reset(msg_iter_data)) {
228 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
229 }
230
231 return status;
232}
233
234BT_HIDDEN
d6e69534 235void ctf_fs_iterator_finalize(bt_self_message_iterator *it)
760051fa 236{
d6e69534
PP
237 ctf_fs_msg_iter_data_destroy(
238 bt_self_message_iterator_get_data(it));
760051fa
JG
239}
240
6a9bb5e9 241BT_HIDDEN
4cdfc5e8 242bt_self_message_iterator_status ctf_fs_iterator_init(
d6e69534 243 bt_self_message_iterator *self_msg_iter,
4c65a157 244 bt_self_component_source *self_comp_src,
b19ff26f 245 bt_self_component_port_output *self_port)
4c1456f0 246{
4f1f88a6 247 struct ctf_fs_port_data *port_data;
d6e69534 248 struct ctf_fs_msg_iter_data *msg_iter_data = NULL;
4cdfc5e8 249 bt_self_message_iterator_status ret =
d6e69534 250 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
98903a3e 251 bt_logging_level log_level;
4c65a157 252 bt_self_component *self_comp;
760051fa 253
d94d92ac 254 port_data = bt_self_component_port_get_data(
707b7d35 255 bt_self_component_port_output_as_self_component_port(
d94d92ac
PP
256 self_port));
257 BT_ASSERT(port_data);
98903a3e 258 log_level = port_data->ctf_fs->log_level;
4c65a157 259 self_comp = port_data->ctf_fs->self_comp;
d6e69534
PP
260 msg_iter_data = g_new0(struct ctf_fs_msg_iter_data, 1);
261 if (!msg_iter_data) {
262 ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM;
94cf822e
PP
263 goto error;
264 }
265
98903a3e 266 msg_iter_data->log_level = log_level;
4c65a157 267 msg_iter_data->self_comp = self_comp;
d6e69534
PP
268 msg_iter_data->pc_msg_iter = self_msg_iter;
269 msg_iter_data->msg_iter = bt_msg_iter_create(
44c440bc 270 port_data->ds_file_group->ctf_fs_trace->metadata->tc,
98903a3e 271 bt_common_get_page_size(msg_iter_data->log_level) * 8,
4c65a157
PP
272 ctf_fs_ds_file_medops, NULL, msg_iter_data->log_level,
273 self_comp);
d6e69534 274 if (!msg_iter_data->msg_iter) {
4c65a157 275 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
d6e69534 276 ret = BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM;
6de92955
PP
277 goto error;
278 }
279
d6e69534 280 msg_iter_data->ds_file_group = port_data->ds_file_group;
6a9bb5e9 281 if (ctf_fs_iterator_reset(msg_iter_data)) {
d6e69534 282 ret = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
4f1f88a6 283 goto error;
760051fa
JG
284 }
285
d6e69534
PP
286 bt_self_message_iterator_set_data(self_msg_iter,
287 msg_iter_data);
288 if (ret != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
4f1f88a6 289 goto error;
760051fa
JG
290 }
291
d6e69534 292 msg_iter_data = NULL;
4f1f88a6 293 goto end;
5b29e799 294
4f1f88a6 295error:
d6e69534 296 bt_self_message_iterator_set_data(self_msg_iter, NULL);
4f1f88a6 297
760051fa 298end:
d6e69534 299 ctf_fs_msg_iter_data_destroy(msg_iter_data);
760051fa
JG
300 return ret;
301}
302
f280892e 303BT_HIDDEN
1a9f7075 304void ctf_fs_destroy(struct ctf_fs_component *ctf_fs)
760051fa 305{
4f1f88a6 306 if (!ctf_fs) {
8fa760ba
JG
307 return;
308 }
4f1f88a6 309
1a9f7075
PP
310 if (ctf_fs->traces) {
311 g_ptr_array_free(ctf_fs->traces, TRUE);
56a1cced 312 }
4f1f88a6
PP
313
314 if (ctf_fs->port_data) {
315 g_ptr_array_free(ctf_fs->port_data, TRUE);
c14d7e26 316 }
760051fa 317
1a9f7075
PP
318 g_free(ctf_fs);
319}
320
f280892e
SM
321static
322void port_data_destroy(struct ctf_fs_port_data *port_data)
323{
324 if (!port_data) {
325 return;
326 }
327
328 g_free(port_data);
329}
330
331static
332void port_data_destroy_notifier(void *data) {
333 port_data_destroy(data);
334}
335
336static
97ade20b 337void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace)
1a9f7075 338{
1a9f7075
PP
339 if (!ctf_fs_trace) {
340 return;
341 }
342
94cf822e
PP
343 if (ctf_fs_trace->ds_file_groups) {
344 g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE);
345 }
346
c5b9b441 347 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace);
862ca4ed 348
1a9f7075
PP
349 if (ctf_fs_trace->path) {
350 g_string_free(ctf_fs_trace->path, TRUE);
4f1f88a6 351 }
760051fa 352
1a9f7075
PP
353 if (ctf_fs_trace->name) {
354 g_string_free(ctf_fs_trace->name, TRUE);
355 }
356
357 if (ctf_fs_trace->metadata) {
358 ctf_fs_metadata_fini(ctf_fs_trace->metadata);
359 g_free(ctf_fs_trace->metadata);
360 }
361
1a9f7075 362 g_free(ctf_fs_trace);
4c1456f0
JG
363}
364
97ade20b 365static
56a924f4 366void ctf_fs_trace_destroy_notifier(void *data)
97ade20b
JG
367{
368 struct ctf_fs_trace *trace = data;
369 ctf_fs_trace_destroy(trace);
370}
371
4c65a157
PP
372struct ctf_fs_component *ctf_fs_component_create(bt_logging_level log_level,
373 bt_self_component *self_comp)
a4792757 374{
f280892e 375 struct ctf_fs_component *ctf_fs;
a4792757 376
f280892e
SM
377 ctf_fs = g_new0(struct ctf_fs_component, 1);
378 if (!ctf_fs) {
379 goto error;
380 }
4f1f88a6 381
98903a3e 382 ctf_fs->log_level = log_level;
4c65a157 383 ctf_fs->self_comp = self_comp;
f280892e
SM
384 ctf_fs->port_data =
385 g_ptr_array_new_with_free_func(port_data_destroy_notifier);
386 if (!ctf_fs->port_data) {
387 goto error;
4f1f88a6
PP
388 }
389
f280892e
SM
390 ctf_fs->traces =
391 g_ptr_array_new_with_free_func(ctf_fs_trace_destroy_notifier);
392 if (!ctf_fs->traces) {
393 goto error;
394 }
395
396 goto end;
397
398error:
399 if (ctf_fs) {
400 ctf_fs_destroy(ctf_fs);
401 }
402
403end:
404 return ctf_fs;
405}
406
407void ctf_fs_finalize(bt_self_component_source *component)
408{
409 ctf_fs_destroy(bt_self_component_get_data(
410 bt_self_component_source_as_self_component(component)));
5b29e799
JG
411}
412
a38d7650 413gchar *ctf_fs_make_port_name(struct ctf_fs_ds_file_group *ds_file_group)
547eacf1 414{
a38d7650 415 GString *name = g_string_new(NULL);
547eacf1 416
a38d7650
SM
417 /*
418 * The unique port name is generated by concatenating unique identifiers
419 * for:
420 *
421 * - the trace
422 * - the stream class
423 * - the stream
424 */
425
426 /* For the trace, use the uuid if present, else the path. */
427 if (ds_file_group->ctf_fs_trace->metadata->tc->is_uuid_set) {
428 char uuid_str[BABELTRACE_UUID_STR_LEN];
429
430 bt_uuid_unparse(ds_file_group->ctf_fs_trace->metadata->tc->uuid, uuid_str);
431 g_string_assign(name, uuid_str);
432 } else {
433 g_string_assign(name, ds_file_group->ctf_fs_trace->path->str);
547eacf1
PP
434 }
435
436 /*
a38d7650
SM
437 * For the stream class, use the id if present. We can omit this field
438 * otherwise, as there will only be a single stream class.
547eacf1 439 */
a38d7650
SM
440 if (ds_file_group->sc->id != UINT64_C(-1)) {
441 g_string_append_printf(name, " | %" PRIu64, ds_file_group->sc->id);
442 }
547eacf1 443
a38d7650
SM
444 /* For the stream, use the id if present, else, use the path. */
445 if (ds_file_group->stream_id != UINT64_C(-1)) {
446 g_string_append_printf(name, " | %" PRIu64, ds_file_group->stream_id);
447 } else {
448 BT_ASSERT(ds_file_group->ds_file_infos->len == 1);
449 struct ctf_fs_ds_file_info *ds_file_info =
450 g_ptr_array_index(ds_file_group->ds_file_infos, 0);
451 g_string_append_printf(name, " | %s", ds_file_info->path->str);
452 }
453
454 return g_string_free(name, FALSE);
547eacf1
PP
455}
456
5b29e799 457static
55314f2a
JG
458int create_one_port_for_trace(struct ctf_fs_component *ctf_fs,
459 struct ctf_fs_trace *ctf_fs_trace,
94cf822e 460 struct ctf_fs_ds_file_group *ds_file_group)
5b29e799 461{
4f1f88a6 462 int ret = 0;
4f1f88a6 463 struct ctf_fs_port_data *port_data = NULL;
a38d7650 464 gchar *port_name;
98903a3e 465 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 466 bt_self_component *self_comp = ctf_fs->self_comp;
4f1f88a6 467
a38d7650 468 port_name = ctf_fs_make_port_name(ds_file_group);
4f1f88a6
PP
469 if (!port_name) {
470 goto error;
471 }
472
4c65a157 473 BT_COMP_LOGI("Creating one port named `%s`", port_name);
4f1f88a6
PP
474
475 /* Create output port for this file */
4f1f88a6
PP
476 port_data = g_new0(struct ctf_fs_port_data, 1);
477 if (!port_data) {
478 goto error;
479 }
480
5c563278 481 port_data->ctf_fs = ctf_fs;
94cf822e 482 port_data->ds_file_group = ds_file_group;
d94d92ac 483 ret = bt_self_component_source_add_output_port(
4c65a157 484 ctf_fs->self_comp_src, port_name, port_data, NULL);
147337a3 485 if (ret) {
4f1f88a6
PP
486 goto error;
487 }
488
489 g_ptr_array_add(ctf_fs->port_data, port_data);
490 port_data = NULL;
491 goto end;
492
493error:
494 ret = -1;
495
496end:
497 if (port_name) {
a38d7650 498 g_free(port_name);
4f1f88a6
PP
499 }
500
4f1f88a6
PP
501 port_data_destroy(port_data);
502 return ret;
5b29e799
JG
503}
504
505static
55314f2a
JG
506int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
507 struct ctf_fs_trace *ctf_fs_trace)
94cf822e
PP
508{
509 int ret = 0;
94cf822e 510 size_t i;
98903a3e 511 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 512 bt_self_component *self_comp = ctf_fs_trace->self_comp;
94cf822e
PP
513
514 /* Create one output port for each stream file group */
515 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
516 struct ctf_fs_ds_file_group *ds_file_group =
517 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
518
55314f2a
JG
519 ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace,
520 ds_file_group);
94cf822e 521 if (ret) {
4c65a157 522 BT_COMP_LOGE("Cannot create output port.");
94cf822e
PP
523 goto end;
524 }
525 }
526
527end:
528 return ret;
529}
530
94cf822e
PP
531static
532void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info)
533{
534 if (!ds_file_info) {
535 return;
536 }
537
538 if (ds_file_info->path) {
539 g_string_free(ds_file_info->path, TRUE);
540 }
541
542 g_free(ds_file_info);
543}
544
545static
546struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path,
7ed5243a 547 int64_t begin_ns)
94cf822e
PP
548{
549 struct ctf_fs_ds_file_info *ds_file_info;
550
551 ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1);
552 if (!ds_file_info) {
553 goto end;
554 }
555
556 ds_file_info->path = g_string_new(path);
557 if (!ds_file_info->path) {
558 ctf_fs_ds_file_info_destroy(ds_file_info);
559 ds_file_info = NULL;
560 goto end;
561 }
562
563 ds_file_info->begin_ns = begin_ns;
564
565end:
566 return ds_file_info;
567}
568
569static
570void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
571{
572 if (!ds_file_group) {
573 return;
574 }
575
576 if (ds_file_group->ds_file_infos) {
577 g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
578 }
579
7ed5243a
FD
580 if (ds_file_group->index) {
581 if (ds_file_group->index->entries) {
582 g_ptr_array_free(ds_file_group->index->entries, TRUE);
583 }
584 g_free(ds_file_group->index);
585 }
586
c5b9b441 587 bt_stream_put_ref(ds_file_group->stream);
94cf822e
PP
588 g_free(ds_file_group);
589}
590
591static
592struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(
593 struct ctf_fs_trace *ctf_fs_trace,
60363f2f 594 struct ctf_stream_class *sc,
7ed5243a
FD
595 uint64_t stream_instance_id,
596 struct ctf_fs_ds_index *index)
94cf822e
PP
597{
598 struct ctf_fs_ds_file_group *ds_file_group;
599
94cf822e
PP
600 ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1);
601 if (!ds_file_group) {
602 goto error;
603 }
604
605 ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func(
606 (GDestroyNotify) ctf_fs_ds_file_info_destroy);
607 if (!ds_file_group->ds_file_infos) {
608 goto error;
609 }
610
7ed5243a
FD
611 ds_file_group->index = index;
612
547eacf1 613 ds_file_group->stream_id = stream_instance_id;
60363f2f
PP
614 BT_ASSERT(sc);
615 ds_file_group->sc = sc;
94cf822e 616 ds_file_group->ctf_fs_trace = ctf_fs_trace;
94cf822e
PP
617 goto end;
618
619error:
620 ctf_fs_ds_file_group_destroy(ds_file_group);
7ed5243a 621 ctf_fs_ds_index_destroy(index);
94cf822e
PP
622 ds_file_group = NULL;
623
624end:
625 return ds_file_group;
626}
627
8bf7105e
MJ
628/* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
629static
630void array_insert(GPtrArray *array, gpointer element, size_t pos)
631{
632 size_t original_array_len = array->len;
633
634 /* Allocate an unused element at the end of the array. */
635 g_ptr_array_add(array, NULL);
636
637 /* If we are not inserting at the end, move the elements by one. */
638 if (pos < original_array_len) {
639 memmove(&(array->pdata[pos + 1]),
640 &(array->pdata[pos]),
641 (original_array_len - pos) * sizeof(gpointer));
642 }
643
f897d50c 644 /* Insert the value. */
8bf7105e
MJ
645 array->pdata[pos] = element;
646}
647
41a65f30
SM
648/*
649 * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
650 * place to keep it sorted.
651 */
652
653static
654void ds_file_group_insert_ds_file_info_sorted(
655 struct ctf_fs_ds_file_group *ds_file_group,
656 struct ctf_fs_ds_file_info *ds_file_info)
657{
658 guint i;
659
660 /* Find the spot where to insert this ds_file_info. */
661 for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
662 struct ctf_fs_ds_file_info *other_ds_file_info =
663 g_ptr_array_index(ds_file_group->ds_file_infos, i);
664
665 if (ds_file_info->begin_ns < other_ds_file_info->begin_ns) {
666 break;
667 }
668 }
669
670 array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
671}
672
7ed5243a
FD
673static
674void ds_file_group_insert_ds_index_entry_sorted(
675 struct ctf_fs_ds_file_group *ds_file_group,
676 struct ctf_fs_ds_index_entry *entry)
677{
678 guint i;
679
680 /* Find the spot where to insert this index entry. */
681 for (i = 0; i < ds_file_group->index->entries->len; i++) {
682 struct ctf_fs_ds_index_entry *other_entry = g_ptr_array_index(
683 ds_file_group->index->entries, i);
684
685 if (entry->timestamp_begin_ns < other_entry->timestamp_begin_ns) {
686 break;
687 }
688 }
689
690 array_insert(ds_file_group->index->entries, entry, i);
691}
692
41a65f30
SM
693/*
694 * Create a new ds_file_info using the provided path, begin_ns and index, then
695 * add it to ds_file_group's list of ds_file_infos.
696 */
697
94cf822e
PP
698static
699int ctf_fs_ds_file_group_add_ds_file_info(
700 struct ctf_fs_ds_file_group *ds_file_group,
7ed5243a 701 const char *path, int64_t begin_ns)
94cf822e
PP
702{
703 struct ctf_fs_ds_file_info *ds_file_info;
94cf822e
PP
704 int ret = 0;
705
7ed5243a 706 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns);
94cf822e
PP
707 if (!ds_file_info) {
708 goto error;
709 }
710
41a65f30 711 ds_file_group_insert_ds_file_info_sorted(ds_file_group, ds_file_info);
94cf822e 712
94cf822e
PP
713 ds_file_info = NULL;
714 goto end;
715
716error:
717 ctf_fs_ds_file_info_destroy(ds_file_info);
718 ret = -1;
94cf822e
PP
719end:
720 return ret;
721}
722
723static
724int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
e5be10ef 725 const char *path)
94cf822e 726{
44c440bc
PP
727 int64_t stream_instance_id = -1;
728 int64_t begin_ns = -1;
94cf822e
PP
729 struct ctf_fs_ds_file_group *ds_file_group = NULL;
730 bool add_group = false;
731 int ret;
732 size_t i;
6de92955 733 struct ctf_fs_ds_file *ds_file = NULL;
97ade20b 734 struct ctf_fs_ds_index *index = NULL;
d6e69534 735 struct bt_msg_iter *msg_iter = NULL;
44c440bc 736 struct ctf_stream_class *sc = NULL;
d6e69534 737 struct bt_msg_iter_packet_properties props;
98903a3e 738 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 739 bt_self_component *self_comp = ctf_fs_trace->self_comp;
94cf822e 740
d6e69534 741 msg_iter = bt_msg_iter_create(ctf_fs_trace->metadata->tc,
98903a3e 742 bt_common_get_page_size(log_level) * 8,
4c65a157 743 ctf_fs_ds_file_medops, NULL, log_level, self_comp);
d6e69534 744 if (!msg_iter) {
4c65a157 745 BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
6de92955
PP
746 goto error;
747 }
748
d6e69534 749 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, msg_iter,
98903a3e 750 NULL, path, log_level);
97ade20b
JG
751 if (!ds_file) {
752 goto error;
753 }
754
41693723 755 ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props);
94cf822e 756 if (ret) {
4c65a157 757 BT_COMP_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
94cf822e
PP
758 path);
759 goto error;
760 }
761
44c440bc
PP
762 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
763 props.stream_class_id);
764 BT_ASSERT(sc);
44c440bc
PP
765 stream_instance_id = props.data_stream_id;
766
767 if (props.snapshots.beginning_clock != UINT64_C(-1)) {
768 BT_ASSERT(sc->default_clock_class);
0f2d58c9
PP
769 ret = bt_util_clock_cycles_to_ns_from_origin(
770 props.snapshots.beginning_clock,
771 sc->default_clock_class->frequency,
772 sc->default_clock_class->offset_seconds,
773 sc->default_clock_class->offset_cycles, &begin_ns);
44c440bc 774 if (ret) {
4c65a157 775 BT_COMP_LOGE("Cannot convert clock cycles to nanoseconds from origin (`%s`).",
44c440bc
PP
776 path);
777 goto error;
778 }
94cf822e
PP
779 }
780
97ade20b
JG
781 index = ctf_fs_ds_file_build_index(ds_file);
782 if (!index) {
4c65a157 783 BT_COMP_LOGW("Failed to index CTF stream file \'%s\'",
97ade20b
JG
784 ds_file->file->path->str);
785 }
786
44c440bc 787 if (begin_ns == -1) {
94cf822e
PP
788 /*
789 * No beggining timestamp to sort the stream files
790 * within a stream file group, so consider that this
791 * file must be the only one within its group.
792 */
44c440bc 793 stream_instance_id = -1;
94cf822e
PP
794 }
795
44c440bc 796 if (stream_instance_id == -1) {
94cf822e
PP
797 /*
798 * No stream instance ID or no beginning timestamp:
799 * create a unique stream file group for this stream
800 * file because, even if there's a stream instance ID,
801 * there's no timestamp to order the file within its
802 * group.
803 */
94cf822e 804 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
7ed5243a
FD
805 sc, UINT64_C(-1), index);
806 /* Ownership of index is transferred. */
807 index = NULL;
808
94cf822e
PP
809 if (!ds_file_group) {
810 goto error;
811 }
812
813 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group,
7ed5243a 814 path, begin_ns);
94cf822e
PP
815 if (ret) {
816 goto error;
817 }
818
819 add_group = true;
820 goto end;
821 }
822
44c440bc
PP
823 BT_ASSERT(stream_instance_id != -1);
824 BT_ASSERT(begin_ns != -1);
94cf822e
PP
825
826 /* Find an existing stream file group with this ID */
827 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
94cf822e
PP
828 ds_file_group = g_ptr_array_index(
829 ctf_fs_trace->ds_file_groups, i);
94cf822e 830
60363f2f 831 if (ds_file_group->sc == sc &&
547eacf1
PP
832 ds_file_group->stream_id ==
833 stream_instance_id) {
94cf822e
PP
834 break;
835 }
836
837 ds_file_group = NULL;
838 }
839
840 if (!ds_file_group) {
841 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
7ed5243a
FD
842 sc, stream_instance_id, index);
843 /* Ownership of index is transferred. */
844 index = NULL;
94cf822e
PP
845 if (!ds_file_group) {
846 goto error;
847 }
848
849 add_group = true;
850 }
851
547eacf1 852 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path,
7ed5243a 853 begin_ns);
94cf822e
PP
854 if (ret) {
855 goto error;
856 }
857
858 goto end;
859
860error:
861 ctf_fs_ds_file_group_destroy(ds_file_group);
90e1eb09 862 ds_file_group = NULL;
94cf822e
PP
863 ret = -1;
864
865end:
866 if (add_group && ds_file_group) {
867 g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group);
868 }
547eacf1 869
97ade20b 870 ctf_fs_ds_file_destroy(ds_file);
6de92955 871
d6e69534
PP
872 if (msg_iter) {
873 bt_msg_iter_destroy(msg_iter);
6de92955
PP
874 }
875
97ade20b 876 ctf_fs_ds_index_destroy(index);
94cf822e
PP
877 return ret;
878}
879
880static
e5be10ef 881int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
e7a4393b
JG
882{
883 int ret = 0;
4f1f88a6 884 const char *basename;
e7a4393b 885 GError *error = NULL;
4f1f88a6 886 GDir *dir = NULL;
98903a3e 887 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 888 bt_self_component *self_comp = ctf_fs_trace->self_comp;
e7a4393b 889
94cf822e 890 /* Check each file in the path directory, except specific ones */
1a9f7075 891 dir = g_dir_open(ctf_fs_trace->path->str, 0, &error);
e7a4393b 892 if (!dir) {
4c65a157 893 BT_COMP_LOGE("Cannot open directory `%s`: %s (code %d)",
1a9f7075 894 ctf_fs_trace->path->str, error->message,
4f1f88a6 895 error->code);
e7a4393b
JG
896 goto error;
897 }
898
4f1f88a6 899 while ((basename = g_dir_read_name(dir))) {
94cf822e
PP
900 struct ctf_fs_file *file;
901
4f1f88a6 902 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
e7a4393b 903 /* Ignore the metadata stream. */
4c65a157 904 BT_COMP_LOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 905 ctf_fs_trace->path->str, basename);
e7a4393b
JG
906 continue;
907 }
908
4f1f88a6 909 if (basename[0] == '.') {
4c65a157 910 BT_COMP_LOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 911 ctf_fs_trace->path->str, basename);
e7a4393b
JG
912 continue;
913 }
914
915 /* Create the file. */
4c65a157 916 file = ctf_fs_file_create(log_level, self_comp);
e7a4393b 917 if (!file) {
4c65a157 918 BT_COMP_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`",
1a9f7075 919 ctf_fs_trace->path->str, basename);
e7a4393b
JG
920 goto error;
921 }
922
923 /* Create full path string. */
3743a302 924 g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s",
1a9f7075 925 ctf_fs_trace->path->str, basename);
e7a4393b 926 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
4c65a157 927 BT_COMP_LOGI("Ignoring non-regular file `%s`",
1a9f7075 928 file->path->str);
e7a4393b 929 ctf_fs_file_destroy(file);
4f1f88a6 930 file = NULL;
e7a4393b
JG
931 continue;
932 }
933
55314f2a 934 ret = ctf_fs_file_open(file, "rb");
4f1f88a6 935 if (ret) {
4c65a157 936 BT_COMP_LOGE("Cannot open stream file `%s`", file->path->str);
e7a4393b
JG
937 goto error;
938 }
939
9fa0891a
JG
940 if (file->size == 0) {
941 /* Skip empty stream. */
4c65a157 942 BT_COMP_LOGI("Ignoring empty file `%s`", file->path->str);
9fa0891a
JG
943 ctf_fs_file_destroy(file);
944 continue;
945 }
946
e5be10ef 947 ret = add_ds_file_to_ds_file_group(ctf_fs_trace,
94cf822e 948 file->path->str);
4f1f88a6 949 if (ret) {
4c65a157 950 BT_COMP_LOGE("Cannot add stream file `%s` to stream file group",
1a9f7075 951 file->path->str);
94cf822e 952 ctf_fs_file_destroy(file);
e7a4393b
JG
953 goto error;
954 }
955
4f1f88a6 956 ctf_fs_file_destroy(file);
e7a4393b
JG
957 }
958
959 goto end;
4f1f88a6 960
e7a4393b
JG
961error:
962 ret = -1;
4f1f88a6 963
e7a4393b
JG
964end:
965 if (dir) {
966 g_dir_close(dir);
967 dir = NULL;
968 }
4f1f88a6 969
e7a4393b
JG
970 if (error) {
971 g_error_free(error);
972 }
5b29e799 973
91457551 974 return ret;
5b29e799
JG
975}
976
862ca4ed 977static
98903a3e 978int set_trace_name(bt_trace *trace, const char *name_suffix,
4c65a157 979 bt_logging_level log_level, bt_self_component *self_comp)
862ca4ed
PP
980{
981 int ret = 0;
b19ff26f
PP
982 const bt_trace_class *tc = bt_trace_borrow_class_const(trace);
983 const bt_value *val;
862ca4ed
PP
984 GString *name;
985
986 name = g_string_new(NULL);
987 if (!name) {
4c65a157 988 BT_COMP_LOGE_STR("Failed to allocate a GString.");
862ca4ed
PP
989 ret = -1;
990 goto end;
991 }
992
993 /*
994 * Check if we have a trace environment string value named `hostname`.
995 * If so, use it as the trace name's prefix.
996 */
997 val = bt_trace_class_borrow_environment_entry_value_by_name_const(
998 tc, "hostname");
999 if (val && bt_value_is_string(val)) {
1000 g_string_append(name, bt_value_string_get(val));
1001
1002 if (name_suffix) {
1003 g_string_append_c(name, G_DIR_SEPARATOR);
1004 }
1005 }
1006
1007 if (name_suffix) {
1008 g_string_append(name, name_suffix);
1009 }
1010
1011 ret = bt_trace_set_name(trace, name->str);
1012 if (ret) {
1013 goto end;
1014 }
1015
1016 goto end;
1017
1018end:
1019 if (name) {
1020 g_string_free(name, TRUE);
1021 }
1022
1023 return ret;
1024}
1025
f280892e 1026static
4c65a157 1027struct ctf_fs_trace *ctf_fs_trace_create(bt_self_component *self_comp,
41693723 1028 const char *path, const char *name,
98903a3e
PP
1029 struct ctf_fs_metadata_config *metadata_config,
1030 bt_logging_level log_level)
1a9f7075
PP
1031{
1032 struct ctf_fs_trace *ctf_fs_trace;
1033 int ret;
1034
1035 ctf_fs_trace = g_new0(struct ctf_fs_trace, 1);
1036 if (!ctf_fs_trace) {
1037 goto end;
1038 }
1039
98903a3e 1040 ctf_fs_trace->log_level = log_level;
4c65a157 1041 ctf_fs_trace->self_comp = self_comp;
1a9f7075
PP
1042 ctf_fs_trace->path = g_string_new(path);
1043 if (!ctf_fs_trace->path) {
1044 goto error;
1045 }
1046
1047 ctf_fs_trace->name = g_string_new(name);
1048 if (!ctf_fs_trace->name) {
1049 goto error;
1050 }
1051
1052 ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1);
1053 if (!ctf_fs_trace->metadata) {
1054 goto error;
1055 }
1056
44c440bc 1057 ctf_fs_metadata_init(ctf_fs_trace->metadata);
94cf822e
PP
1058 ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func(
1059 (GDestroyNotify) ctf_fs_ds_file_group_destroy);
1060 if (!ctf_fs_trace->ds_file_groups) {
1061 goto error;
1062 }
1063
4c65a157
PP
1064 ret = ctf_fs_metadata_set_trace_class(self_comp, ctf_fs_trace,
1065 metadata_config);
862ca4ed
PP
1066 if (ret) {
1067 goto error;
1068 }
1069
41693723
PP
1070 if (ctf_fs_trace->metadata->trace_class) {
1071 ctf_fs_trace->trace =
1072 bt_trace_create(ctf_fs_trace->metadata->trace_class);
1073 if (!ctf_fs_trace->trace) {
1074 goto error;
1075 }
862ca4ed
PP
1076 }
1077
41693723 1078 if (ctf_fs_trace->trace) {
4c65a157
PP
1079 ret = set_trace_name(ctf_fs_trace->trace, name, log_level,
1080 self_comp);
41693723
PP
1081 if (ret) {
1082 goto error;
1083 }
1a9f7075
PP
1084 }
1085
e5be10ef 1086 ret = create_ds_file_groups(ctf_fs_trace);
94cf822e
PP
1087 if (ret) {
1088 goto error;
1089 }
1090
1a9f7075
PP
1091 goto end;
1092
1093error:
1094 ctf_fs_trace_destroy(ctf_fs_trace);
1095 ctf_fs_trace = NULL;
44c440bc 1096
1a9f7075
PP
1097end:
1098 return ctf_fs_trace;
1099}
1100
1101static
1102int path_is_ctf_trace(const char *path)
1103{
1104 GString *metadata_path = g_string_new(NULL);
1105 int ret = 0;
1106
1107 if (!metadata_path) {
1108 ret = -1;
1109 goto end;
1110 }
1111
3743a302 1112 g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
1a9f7075
PP
1113
1114 if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
1115 ret = 1;
1116 goto end;
1117 }
1118
1119end:
1120 g_string_free(metadata_path, TRUE);
1121 return ret;
1122}
1123
1124static
98903a3e 1125int add_trace_path(GList **trace_paths, const char *path,
4c65a157 1126 bt_logging_level log_level, bt_self_component *self_comp)
1a9f7075 1127{
4bd72b60 1128 GString *norm_path = NULL;
1a9f7075 1129 int ret = 0;
1a9f7075 1130
4bd72b60
PP
1131 norm_path = bt_common_normalize_path(path, NULL);
1132 if (!norm_path) {
4c65a157 1133 BT_COMP_LOGE("Failed to normalize path `%s`.", path);
1a9f7075
PP
1134 ret = -1;
1135 goto end;
1136 }
1137
3743a302 1138 // FIXME: Remove or ifdef for __MINGW32__
4bd72b60 1139 if (strcmp(norm_path->str, "/") == 0) {
4c65a157 1140 BT_COMP_LOGE("Opening a trace in `/` is not supported.");
1a9f7075
PP
1141 ret = -1;
1142 goto end;
1143 }
1144
4bd72b60 1145 *trace_paths = g_list_prepend(*trace_paths, norm_path);
f6ccaed9 1146 BT_ASSERT(*trace_paths);
4bd72b60 1147 norm_path = NULL;
1a9f7075
PP
1148
1149end:
4bd72b60
PP
1150 if (norm_path) {
1151 g_string_free(norm_path, TRUE);
1152 }
1153
1a9f7075
PP
1154 return ret;
1155}
1156
f280892e 1157static
98903a3e 1158int ctf_fs_find_traces(GList **trace_paths, const char *start_path,
4c65a157 1159 bt_logging_level log_level, bt_self_component *self_comp)
1a9f7075
PP
1160{
1161 int ret;
1162 GError *error = NULL;
1163 GDir *dir = NULL;
1164 const char *basename = NULL;
1165
1166 /* Check if the starting path is a CTF trace itself */
1167 ret = path_is_ctf_trace(start_path);
1168 if (ret < 0) {
1169 goto end;
1170 }
1171
1172 if (ret) {
1173 /*
4bd72b60
PP
1174 * Stop recursion: a CTF trace cannot contain another
1175 * CTF trace.
1a9f7075 1176 */
4c65a157
PP
1177 ret = add_trace_path(trace_paths, start_path, log_level,
1178 self_comp);
1a9f7075
PP
1179 goto end;
1180 }
1181
1182 /* Look for subdirectories */
1183 if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) {
1184 /* Starting path is not a directory: end of recursion */
1185 goto end;
1186 }
1187
1188 dir = g_dir_open(start_path, 0, &error);
1189 if (!dir) {
1190 if (error->code == G_FILE_ERROR_ACCES) {
4c65a157 1191 BT_COMP_LOGI("Cannot open directory `%s`: %s (code %d): continuing",
1a9f7075
PP
1192 start_path, error->message, error->code);
1193 goto end;
1194 }
1195
4c65a157 1196 BT_COMP_LOGE("Cannot open directory `%s`: %s (code %d)",
1a9f7075
PP
1197 start_path, error->message, error->code);
1198 ret = -1;
1199 goto end;
1200 }
1201
1202 while ((basename = g_dir_read_name(dir))) {
1203 GString *sub_path = g_string_new(NULL);
1204
1205 if (!sub_path) {
1206 ret = -1;
1207 goto end;
1208 }
1209
3743a302 1210 g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename);
98903a3e 1211 ret = ctf_fs_find_traces(trace_paths, sub_path->str,
4c65a157 1212 log_level, self_comp);
1a9f7075
PP
1213 g_string_free(sub_path, TRUE);
1214 if (ret) {
1215 goto end;
1216 }
1217 }
1218
1219end:
1220 if (dir) {
1221 g_dir_close(dir);
1222 }
1223
1224 if (error) {
1225 g_error_free(error);
1226 }
1227
1228 return ret;
1229}
1230
f280892e 1231static
55314f2a 1232GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) {
1a9f7075 1233 GList *trace_names = NULL;
1a9f7075 1234 GList *node;
4bd72b60
PP
1235 const char *last_sep;
1236 size_t base_dist;
1a9f7075
PP
1237
1238 /*
4bd72b60
PP
1239 * At this point we know that all the trace paths are
1240 * normalized, and so is the base path. This means that
1241 * they are absolute and they don't end with a separator.
1242 * We can simply find the location of the last separator
1243 * in the base path, which gives us the name of the actual
1244 * directory to look into, and use this location as the
1245 * start of each trace name within each trace path.
1246 *
1247 * For example:
1248 *
1249 * Base path: /home/user/my-traces/some-trace
1250 * Trace paths:
1251 * - /home/user/my-traces/some-trace/host1/trace1
1252 * - /home/user/my-traces/some-trace/host1/trace2
1253 * - /home/user/my-traces/some-trace/host2/trace
1254 * - /home/user/my-traces/some-trace/other-trace
1255 *
1256 * In this case the trace names are:
1257 *
1258 * - some-trace/host1/trace1
1259 * - some-trace/host1/trace2
1260 * - some-trace/host2/trace
1261 * - some-trace/other-trace
1a9f7075 1262 */
4bd72b60 1263 last_sep = strrchr(base_path, G_DIR_SEPARATOR);
1a9f7075 1264
4bd72b60 1265 /* We know there's at least one separator */
f6ccaed9 1266 BT_ASSERT(last_sep);
1a9f7075 1267
4bd72b60
PP
1268 /* Distance to base */
1269 base_dist = last_sep - base_path + 1;
1a9f7075
PP
1270
1271 /* Create the trace names */
1272 for (node = trace_paths; node; node = g_list_next(node)) {
1273 GString *trace_name = g_string_new(NULL);
1274 GString *trace_path = node->data;
1275
f6ccaed9 1276 BT_ASSERT(trace_name);
4bd72b60 1277 g_string_assign(trace_name, &trace_path->str[base_dist]);
1a9f7075
PP
1278 trace_names = g_list_append(trace_names, trace_name);
1279 }
1280
1281 return trace_names;
1282}
1283
f280892e
SM
1284/* Helper for ctf_fs_component_create_ctf_fs_traces, to handle a single path/root. */
1285
1a9f7075 1286static
4c65a157 1287int ctf_fs_component_create_ctf_fs_traces_one_root(
41693723 1288 struct ctf_fs_component *ctf_fs,
1a9f7075
PP
1289 const char *path_param)
1290{
1291 struct ctf_fs_trace *ctf_fs_trace = NULL;
1292 int ret = 0;
4bd72b60 1293 GString *norm_path = NULL;
1a9f7075
PP
1294 GList *trace_paths = NULL;
1295 GList *trace_names = NULL;
1296 GList *tp_node;
1297 GList *tn_node;
98903a3e 1298 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 1299 bt_self_component *self_comp = ctf_fs->self_comp;
1a9f7075 1300
4bd72b60
PP
1301 norm_path = bt_common_normalize_path(path_param, NULL);
1302 if (!norm_path) {
4c65a157 1303 BT_COMP_LOGE("Failed to normalize path: `%s`.",
4bd72b60
PP
1304 path_param);
1305 goto error;
1306 }
1307
4c65a157
PP
1308 ret = ctf_fs_find_traces(&trace_paths, norm_path->str, log_level,
1309 self_comp);
1a9f7075
PP
1310 if (ret) {
1311 goto error;
1312 }
1313
1314 if (!trace_paths) {
4c65a157 1315 BT_COMP_LOGE("No CTF traces recursively found in `%s`.",
1a9f7075
PP
1316 path_param);
1317 goto error;
1318 }
1319
55314f2a 1320 trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str);
1a9f7075 1321 if (!trace_names) {
4c65a157 1322 BT_COMP_LOGE("Cannot create trace names from trace paths.");
1a9f7075
PP
1323 goto error;
1324 }
1325
1326 for (tp_node = trace_paths, tn_node = trace_names; tp_node;
1327 tp_node = g_list_next(tp_node),
1328 tn_node = g_list_next(tn_node)) {
1329 GString *trace_path = tp_node->data;
1330 GString *trace_name = tn_node->data;
1331
41693723
PP
1332 ctf_fs_trace = ctf_fs_trace_create(self_comp,
1333 trace_path->str, trace_name->str,
98903a3e
PP
1334 &ctf_fs->metadata_config,
1335 log_level);
1a9f7075 1336 if (!ctf_fs_trace) {
4c65a157 1337 BT_COMP_LOGE("Cannot create trace for `%s`.",
1a9f7075
PP
1338 trace_path->str);
1339 goto error;
1340 }
52c5fe74
PP
1341
1342 g_ptr_array_add(ctf_fs->traces, ctf_fs_trace);
1343 ctf_fs_trace = NULL;
1a9f7075
PP
1344 }
1345
1a9f7075
PP
1346 goto end;
1347
1348error:
1349 ret = -1;
1350 ctf_fs_trace_destroy(ctf_fs_trace);
1351
1352end:
1353 for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) {
1354 if (tp_node->data) {
1355 g_string_free(tp_node->data, TRUE);
1356 }
1357 }
1358
1359 for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) {
1360 if (tn_node->data) {
1361 g_string_free(tn_node->data, TRUE);
1362 }
1363 }
1364
1365 if (trace_paths) {
1366 g_list_free(trace_paths);
1367 }
1368
1369 if (trace_names) {
1370 g_list_free(trace_names);
1371 }
1372
4bd72b60
PP
1373 if (norm_path) {
1374 g_string_free(norm_path, TRUE);
1375 }
1376
1a9f7075
PP
1377 return ret;
1378}
1379
41a65f30
SM
1380/* GCompareFunc to sort traces by UUID. */
1381
1382static
1383gint sort_traces_by_uuid(gconstpointer a, gconstpointer b)
1384{
1385 const struct ctf_fs_trace *trace_a = *((const struct ctf_fs_trace **) a);
1386 const struct ctf_fs_trace *trace_b = *((const struct ctf_fs_trace **) b);
1387
1388 bool trace_a_has_uuid = trace_a->metadata->tc->is_uuid_set;
1389 bool trace_b_has_uuid = trace_b->metadata->tc->is_uuid_set;
1390 gint ret;
1391
1392 /* Order traces without uuid first. */
1393 if (!trace_a_has_uuid && trace_b_has_uuid) {
1394 ret = -1;
1395 } else if (trace_a_has_uuid && !trace_b_has_uuid) {
1396 ret = 1;
1397 } else if (!trace_a_has_uuid && !trace_b_has_uuid) {
1398 ret = 0;
1399 } else {
1400 ret = bt_uuid_compare(trace_a->metadata->tc->uuid, trace_b->metadata->tc->uuid);
1401 }
1402
1403 return ret;
1404}
1405
1406/*
1407 * Count the number of stream and event classes defined by this trace's metadata.
1408 *
1409 * This is used to determine which metadata is the "latest", out of multiple
1410 * traces sharing the same UUID. It is assumed that amongst all these metadatas,
1411 * a bigger metadata is a superset of a smaller metadata. Therefore, it is
1412 * enough to just count the classes.
1413 */
1414
1415static
1416unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace *trace)
1417{
1418 unsigned int num = trace->metadata->tc->stream_classes->len;
1419 guint i;
1420
1421 for (i = 0; i < trace->metadata->tc->stream_classes->len; i++) {
1422 struct ctf_stream_class *sc = trace->metadata->tc->stream_classes->pdata[i];
1423 num += sc->event_classes->len;
1424 }
1425
1426 return num;
1427}
1428
1429/*
1430 * Merge the src ds_file_group into dest. This consists of merging their
1431 * ds_file_infos, making sure to keep the result sorted.
1432 */
1433
1434static
1435void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group *dest, struct ctf_fs_ds_file_group *src)
1436{
1437 guint i;
1438
1439 for (i = 0; i < src->ds_file_infos->len; i++) {
1440 struct ctf_fs_ds_file_info *ds_file_info =
1441 g_ptr_array_index(src->ds_file_infos, i);
1442
1443 /* Ownership of the ds_file_info is transferred to dest. */
1444 g_ptr_array_index(src->ds_file_infos, i) = NULL;
1445
1446 ds_file_group_insert_ds_file_info_sorted(dest, ds_file_info);
1447 }
41a65f30 1448
7ed5243a
FD
1449 /* Merge both indexes. */
1450 for (i = 0; i < src->index->entries->len; i++) {
1451 struct ctf_fs_ds_index_entry *entry = g_ptr_array_index(
1452 src->index->entries, i);
1453
1454 /*
1455 * Ownership of the ctf_fs_ds_index_entry is transferred to
1456 * dest.
1457 */
1458 g_ptr_array_index(src->index->entries, i) = NULL;
1459
1460 ds_file_group_insert_ds_index_entry_sorted(dest, entry);
1461 }
1462}
41a65f30
SM
1463/* Merge src_trace's data stream file groups into dest_trace's. */
1464
1465static
54ef52bd 1466int merge_matching_ctf_fs_ds_file_groups(
41a65f30
SM
1467 struct ctf_fs_trace *dest_trace,
1468 struct ctf_fs_trace *src_trace)
1469{
1470
1471 GPtrArray *dest = dest_trace->ds_file_groups;
1472 GPtrArray *src = src_trace->ds_file_groups;
1473 guint s_i;
54ef52bd 1474 int ret = 0;
41a65f30
SM
1475
1476 /*
1477 * Save the initial length of dest: we only want to check against the
1478 * original elements in the inner loop.
1479 */
1480 const guint dest_len = dest->len;
1481
1482 for (s_i = 0; s_i < src->len; s_i++) {
1483 struct ctf_fs_ds_file_group *src_group = g_ptr_array_index(src, s_i);
1484 struct ctf_fs_ds_file_group *dest_group = NULL;
1485
1486 /* A stream instance without ID can't match a stream in the other trace. */
1487 if (src_group->stream_id != -1) {
1488 guint d_i;
1489
1490 /* Let's search for a matching ds_file_group in the destination. */
1491 for (d_i = 0; d_i < dest_len; d_i++) {
1492 struct ctf_fs_ds_file_group *candidate_dest = g_ptr_array_index(dest, d_i);
1493
1494 /* Can't match a stream instance without ID. */
1495 if (candidate_dest->stream_id == -1) {
1496 continue;
1497 }
1498
1499 /*
1500 * If the two groups have the same stream instance id
1501 * and belong to the same stream class (stream instance
1502 * ids are per-stream class), they represent the same
1503 * stream instance.
1504 */
1505 if (candidate_dest->stream_id != src_group->stream_id ||
1506 candidate_dest->sc->id != src_group->sc->id) {
1507 continue;
1508 }
1509
1510 dest_group = candidate_dest;
1511 break;
1512 }
1513 }
1514
1515 /*
1516 * Didn't find a friend in dest to merge our src_group into?
7ed5243a
FD
1517 * Create a new empty one. This can happen if a stream was
1518 * active in the source trace chunk but not in the destination
1519 * trace chunk.
41a65f30
SM
1520 */
1521 if (!dest_group) {
1522 struct ctf_stream_class *sc;
7ed5243a 1523 struct ctf_fs_ds_index *index;
41a65f30
SM
1524
1525 sc = ctf_trace_class_borrow_stream_class_by_id(
1526 dest_trace->metadata->tc, src_group->sc->id);
1527 BT_ASSERT(sc);
1528
4c65a157
PP
1529 index = ctf_fs_ds_index_create(dest_trace->log_level,
1530 dest_trace->self_comp);
7ed5243a
FD
1531 if (!index) {
1532 ret = -1;
1533 goto end;
1534 }
1535
41a65f30 1536 dest_group = ctf_fs_ds_file_group_create(dest_trace, sc,
7ed5243a
FD
1537 src_group->stream_id, index);
1538 /* Ownership of index is transferred. */
1539 index = NULL;
54ef52bd
FD
1540 if (!dest_group) {
1541 ret = -1;
1542 goto end;
1543 }
41a65f30
SM
1544
1545 g_ptr_array_add(dest_trace->ds_file_groups, dest_group);
1546 }
1547
1548 BT_ASSERT(dest_group);
1549 merge_ctf_fs_ds_file_groups(dest_group, src_group);
1550 }
54ef52bd
FD
1551
1552end:
1553 return ret;
41a65f30
SM
1554}
1555
1556/*
1557 * Collapse the given traces, which must all share the same UUID, in a single
1558 * one.
1559 *
1560 * The trace with the most expansive metadata is chosen and all other traces
1561 * are merged into that one. The array slots of all the traces that get merged
1562 * in the chosen one are set to NULL, so only the slot of the chosen trace
1563 * remains non-NULL.
1564 */
1565
1566static
54ef52bd 1567int merge_ctf_fs_traces(struct ctf_fs_trace **traces, unsigned int num_traces)
41a65f30
SM
1568{
1569 unsigned int winner_count;
1570 struct ctf_fs_trace *winner;
1571 guint i;
54ef52bd 1572 int ret = 0;
41a65f30
SM
1573 char uuid_str[BABELTRACE_UUID_STR_LEN];
1574
1575 BT_ASSERT(num_traces >= 2);
1576
1577 winner_count = metadata_count_stream_and_event_classes(traces[0]);
1578 winner = traces[0];
1579
1580 /* Find the trace with the largest metadata. */
1581 for (i = 1; i < num_traces; i++) {
1582 struct ctf_fs_trace *candidate;
1583 unsigned int candidate_count;
1584
1585 candidate = traces[i];
1586
1587 /* A bit of sanity check. */
1588 BT_ASSERT(bt_uuid_compare(winner->metadata->tc->uuid, candidate->metadata->tc->uuid) == 0);
1589
1590 candidate_count = metadata_count_stream_and_event_classes(candidate);
1591
1592 if (candidate_count > winner_count) {
1593 winner_count = candidate_count;
1594 winner = candidate;
1595 }
1596 }
1597
1598 /* Merge all the other traces in the winning trace. */
1599 for (i = 0; i < num_traces; i++) {
1600 struct ctf_fs_trace *trace = traces[i];
1601
1602 /* Don't merge the winner into itself. */
1603 if (trace == winner) {
1604 continue;
1605 }
1606
1607 /* Merge trace's data stream file groups into winner's. */
54ef52bd
FD
1608 ret = merge_matching_ctf_fs_ds_file_groups(winner, trace);
1609 if (ret) {
1610 goto end;
1611 }
41a65f30
SM
1612
1613 /* Free the trace that got merged into winner, clear the slot in the array. */
1614 ctf_fs_trace_destroy(trace);
1615 traces[i] = NULL;
1616 }
1617
1618 /* Use the string representation of the UUID as the trace name. */
1619 bt_uuid_unparse(winner->metadata->tc->uuid, uuid_str);
1620 g_string_printf(winner->name, "%s", uuid_str);
54ef52bd
FD
1621
1622end:
1623 return ret;
41a65f30
SM
1624}
1625
1626/*
1627 * Merge all traces of `ctf_fs` that share the same UUID in a single trace.
1628 * Traces with no UUID are not merged.
1629 */
1630
1631static
54ef52bd 1632int merge_traces_with_same_uuid(struct ctf_fs_component *ctf_fs)
41a65f30
SM
1633{
1634 GPtrArray *traces = ctf_fs->traces;
1635 guint range_start_idx = 0;
1636 unsigned int num_traces = 0;
1637 guint i;
54ef52bd 1638 int ret = 0;
41a65f30
SM
1639
1640 /* Sort the traces by uuid, then collapse traces with the same uuid in a single one. */
1641 g_ptr_array_sort(traces, sort_traces_by_uuid);
1642
1643 /* Find ranges of consecutive traces that share the same UUID. */
1644 while (range_start_idx < traces->len) {
1645 guint range_len;
1646 struct ctf_fs_trace *range_start_trace = g_ptr_array_index(traces, range_start_idx);
1647
1648 /* Exclusive end of range. */
1649 guint range_end_exc_idx = range_start_idx + 1;
1650
1651 while (range_end_exc_idx < traces->len) {
1652 struct ctf_fs_trace *this_trace = g_ptr_array_index(traces, range_end_exc_idx);
1653
1654 if (!range_start_trace->metadata->tc->is_uuid_set ||
1655 (bt_uuid_compare(range_start_trace->metadata->tc->uuid, this_trace->metadata->tc->uuid) != 0)) {
1656 break;
1657 }
1658
1659 range_end_exc_idx++;
1660 }
1661
1662 /* If we have two or more traces with matching UUIDs, merge them. */
1663 range_len = range_end_exc_idx - range_start_idx;
1664 if (range_len > 1) {
1665 struct ctf_fs_trace **range_start = (struct ctf_fs_trace **) &traces->pdata[range_start_idx];
54ef52bd
FD
1666 ret = merge_ctf_fs_traces(range_start, range_len);
1667 if (ret) {
1668 goto end;
1669 }
41a65f30
SM
1670 }
1671
1672 num_traces++;
1673 range_start_idx = range_end_exc_idx;
1674 }
1675
1676 /* Clear any NULL slot (traces that got merged in another one) in the array. */
1677 for (i = 0; i < traces->len;) {
1678 if (g_ptr_array_index(traces, i) == NULL) {
1679 g_ptr_array_remove_index_fast(traces, i);
1680 } else {
1681 i++;
1682 }
1683 }
1684
1685 BT_ASSERT(num_traces == traces->len);
54ef52bd
FD
1686
1687end:
1688 return ret;
41a65f30
SM
1689}
1690
f280892e
SM
1691int ctf_fs_component_create_ctf_fs_traces(bt_self_component_source *self_comp,
1692 struct ctf_fs_component *ctf_fs,
1693 const bt_value *paths_value)
1694{
1695 int ret = 0;
1696 uint64_t i;
1697
1698 for (i = 0; i < bt_value_array_get_size(paths_value); i++) {
1699 const bt_value *path_value = bt_value_array_borrow_element_by_index_const(paths_value, i);
1700 const char *path = bt_value_string_get(path_value);
1701
4c65a157
PP
1702 ret = ctf_fs_component_create_ctf_fs_traces_one_root(ctf_fs,
1703 path);
f280892e
SM
1704 if (ret) {
1705 goto end;
1706 }
1707 }
1708
54ef52bd 1709 ret = merge_traces_with_same_uuid(ctf_fs);
41a65f30 1710
f280892e
SM
1711end:
1712 return ret;
1713}
1714
a38d7650
SM
1715static
1716GString *get_stream_instance_unique_name(
1717 struct ctf_fs_ds_file_group *ds_file_group)
1718{
1719 GString *name;
1720 struct ctf_fs_ds_file_info *ds_file_info;
1721
1722 name = g_string_new(NULL);
1723 if (!name) {
1724 goto end;
1725 }
1726
1727 /*
1728 * If there's more than one stream file in the stream file
1729 * group, the first (earliest) stream file's path is used as
1730 * the stream's unique name.
1731 */
1732 BT_ASSERT(ds_file_group->ds_file_infos->len > 0);
1733 ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0);
1734 g_string_assign(name, ds_file_info->path->str);
1735
1736end:
1737 return name;
1738}
1739
f280892e
SM
1740/* Create the IR stream objects for ctf_fs_trace. */
1741
1742static
1743int create_streams_for_trace(struct ctf_fs_trace *ctf_fs_trace)
1744{
1745 int ret;
1746 GString *name = NULL;
1747 guint i;
98903a3e 1748 bt_logging_level log_level = ctf_fs_trace->log_level;
4c65a157 1749 bt_self_component *self_comp = ctf_fs_trace->self_comp;
f280892e
SM
1750
1751 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
1752 struct ctf_fs_ds_file_group *ds_file_group =
1753 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
1754 name = get_stream_instance_unique_name(ds_file_group);
1755
1756 if (!name) {
1757 goto error;
1758 }
1759
1760 if (ds_file_group->sc->ir_sc) {
1761 BT_ASSERT(ctf_fs_trace->trace);
1762
1763 if (ds_file_group->stream_id == UINT64_C(-1)) {
1764 /* No stream ID: use 0 */
1765 ds_file_group->stream = bt_stream_create_with_id(
1766 ds_file_group->sc->ir_sc,
1767 ctf_fs_trace->trace,
1768 ctf_fs_trace->next_stream_id);
1769 ctf_fs_trace->next_stream_id++;
1770 } else {
1771 /* Specific stream ID */
1772 ds_file_group->stream = bt_stream_create_with_id(
1773 ds_file_group->sc->ir_sc,
1774 ctf_fs_trace->trace,
1775 (uint64_t) ds_file_group->stream_id);
1776 }
1777 } else {
1778 ds_file_group->stream = NULL;
1779 }
1780
1781 if (!ds_file_group->stream) {
4c65a157 1782 BT_COMP_LOGE("Cannot create stream for DS file group: "
f280892e
SM
1783 "addr=%p, stream-name=\"%s\"",
1784 ds_file_group, name->str);
1785 goto error;
1786 }
1787
1788 ret = bt_stream_set_name(ds_file_group->stream,
1789 name->str);
1790 if (ret) {
4c65a157 1791 BT_COMP_LOGE("Cannot set stream's name: "
f280892e
SM
1792 "addr=%p, stream-name=\"%s\"",
1793 ds_file_group->stream, name->str);
1794 goto error;
1795 }
1796
1797 g_string_free(name, TRUE);
1798 name = NULL;
1799 }
1800
1801 ret = 0;
1802 goto end;
1803
1804error:
1805 ret = -1;
1806
1807end:
1808
1809 if (name) {
1810 g_string_free(name, TRUE);
1811 }
1812 return ret;
1813}
1814
d907165c
SM
1815/*
1816 * Validate the "paths" parameter passed to this component. It must be
1817 * present, and it must be an array of strings.
1818 */
1819
1820static
98903a3e
PP
1821bool validate_paths_parameter(struct ctf_fs_component *ctf_fs,
1822 const bt_value *paths)
f280892e
SM
1823{
1824 bool ret;
1825 bt_value_type type;
1826 uint64_t i;
98903a3e 1827 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 1828 bt_self_component *self_comp = ctf_fs->self_comp;
f280892e
SM
1829
1830 if (!paths) {
4c65a157 1831 BT_COMP_LOGE("missing \"paths\" parameter");
f280892e
SM
1832 goto error;
1833 }
1834
1835 type = bt_value_get_type(paths);
1836 if (type != BT_VALUE_TYPE_ARRAY) {
4c65a157 1837 BT_COMP_LOGE("`paths` parameter: expecting array value: type=%s",
f280892e
SM
1838 bt_common_value_type_string(type));
1839 goto error;
1840 }
1841
1842 for (i = 0; i < bt_value_array_get_size(paths); i++) {
1843 const bt_value *elem;
1844
1845 elem = bt_value_array_borrow_element_by_index_const(paths, i);
1846 type = bt_value_get_type(elem);
1847 if (type != BT_VALUE_TYPE_STRING) {
4c65a157 1848 BT_COMP_LOGE("`paths` parameter: expecting string value: index=%" PRIu64 ", type=%s",
f280892e
SM
1849 i, bt_common_value_type_string(type));
1850 goto error;
1851 }
1852 }
1853
1854 ret = true;
1855 goto end;
1856
1857error:
1858 ret = false;
1859
1860end:
1861 return ret;
1862}
1863
d907165c
SM
1864bool read_src_fs_parameters(const bt_value *params,
1865 const bt_value **paths, struct ctf_fs_component *ctf_fs) {
1866 bool ret;
1867 const bt_value *value;
98903a3e 1868 bt_logging_level log_level = ctf_fs->log_level;
4c65a157 1869 bt_self_component *self_comp = ctf_fs->self_comp;
d907165c
SM
1870
1871 /* paths parameter */
1872 *paths = bt_value_map_borrow_entry_value_const(params, "paths");
98903a3e 1873 if (!validate_paths_parameter(ctf_fs, *paths)) {
d907165c
SM
1874 goto error;
1875 }
1876
1877 /* clock-class-offset-s parameter */
1878 value = bt_value_map_borrow_entry_value_const(params,
1879 "clock-class-offset-s");
1880 if (value) {
fdd3a2da 1881 if (!bt_value_is_signed_integer(value)) {
4c65a157 1882 BT_COMP_LOGE("clock-class-offset-s must be an integer");
d907165c
SM
1883 goto error;
1884 }
1885 ctf_fs->metadata_config.clock_class_offset_s =
fdd3a2da 1886 bt_value_signed_integer_get(value);
d907165c
SM
1887 }
1888
1889 /* clock-class-offset-ns parameter */
1890 value = bt_value_map_borrow_entry_value_const(params,
1891 "clock-class-offset-ns");
1892 if (value) {
fdd3a2da 1893 if (!bt_value_is_signed_integer(value)) {
4c65a157 1894 BT_COMP_LOGE("clock-class-offset-ns must be an integer");
d907165c
SM
1895 goto error;
1896 }
1897 ctf_fs->metadata_config.clock_class_offset_ns =
fdd3a2da 1898 bt_value_signed_integer_get(value);
d907165c
SM
1899 }
1900
1901
1902 ret = true;
1903 goto end;
1904
1905error:
1906 ret = false;
1907
1908end:
1909 return ret;
1910}
1911
5b29e799 1912static
d94d92ac 1913struct ctf_fs_component *ctf_fs_create(
98903a3e 1914 bt_self_component_source *self_comp_src,
b19ff26f 1915 const bt_value *params)
56a1cced 1916{
f280892e 1917 struct ctf_fs_component *ctf_fs = NULL;
f280892e
SM
1918 guint i;
1919 const bt_value *paths_value;
98903a3e
PP
1920 bt_self_component *self_comp =
1921 bt_self_component_source_as_self_component(self_comp_src);
56a1cced 1922
98903a3e 1923 ctf_fs = ctf_fs_component_create(bt_component_get_logging_level(
4c65a157 1924 bt_self_component_as_component(self_comp)), self_comp);
d907165c 1925 if (!ctf_fs) {
f280892e
SM
1926 goto error;
1927 }
1928
d907165c 1929 if (!read_src_fs_parameters(params, &paths_value, ctf_fs)) {
f280892e 1930 goto error;
56a1cced
JG
1931 }
1932
98903a3e 1933 bt_self_component_set_data(self_comp, ctf_fs);
4c65a157
PP
1934 ctf_fs->self_comp = self_comp;
1935 ctf_fs->self_comp_src = self_comp_src;
56a1cced 1936
98903a3e 1937 if (ctf_fs_component_create_ctf_fs_traces(self_comp_src, ctf_fs, paths_value)) {
4f1f88a6
PP
1938 goto error;
1939 }
1940
f280892e
SM
1941 for (i = 0; i < ctf_fs->traces->len; i++) {
1942 struct ctf_fs_trace *trace = g_ptr_array_index(ctf_fs->traces, i);
599faa1c 1943
f280892e
SM
1944 if (create_streams_for_trace(trace)) {
1945 goto error;
1946 }
1947
1948 if (create_ports_for_trace(ctf_fs, trace)) {
1949 goto error;
1950 }
4f1f88a6
PP
1951 }
1952
1ef09eb5
JG
1953 goto end;
1954
56a1cced 1955error:
1a9f7075 1956 ctf_fs_destroy(ctf_fs);
e7a4393b 1957 ctf_fs = NULL;
98903a3e 1958 bt_self_component_set_data(self_comp, NULL);
1a9f7075 1959
1ef09eb5 1960end:
56a1cced
JG
1961 return ctf_fs;
1962}
1963
ea0b4b9e 1964BT_HIDDEN
4cdfc5e8 1965bt_self_component_status ctf_fs_init(
b19ff26f 1966 bt_self_component_source *self_comp,
c88dd1cb 1967 const bt_value *params, __attribute__((unused)) void *init_method_data)
ea0b4b9e
JG
1968{
1969 struct ctf_fs_component *ctf_fs;
4cdfc5e8 1970 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
ea0b4b9e 1971
d94d92ac 1972 ctf_fs = ctf_fs_create(self_comp, params);
ea0b4b9e 1973 if (!ctf_fs) {
d94d92ac 1974 ret = BT_SELF_COMPONENT_STATUS_ERROR;
ea0b4b9e 1975 }
4c1456f0 1976
ea0b4b9e
JG
1977 return ret;
1978}
33f93973
PP
1979
1980BT_HIDDEN
4cdfc5e8 1981bt_query_status ctf_fs_query(
b19ff26f
PP
1982 bt_self_component_class_source *comp_class,
1983 const bt_query_executor *query_exec,
1984 const char *object, const bt_value *params,
98903a3e 1985 bt_logging_level log_level,
b19ff26f 1986 const bt_value **result)
33f93973 1987{
4cdfc5e8 1988 bt_query_status status = BT_QUERY_STATUS_OK;
33f93973 1989
04c0ba87 1990 if (!strcmp(object, "metadata-info")) {
98903a3e
PP
1991 status = metadata_info_query(comp_class, params, log_level,
1992 result);
97ade20b 1993 } else if (!strcmp(object, "trace-info")) {
98903a3e
PP
1994 status = trace_info_query(comp_class, params, log_level,
1995 result);
33f93973 1996 } else {
55314f2a 1997 BT_LOGE("Unknown query object `%s`", object);
d94d92ac 1998 status = BT_QUERY_STATUS_INVALID_OBJECT;
04c0ba87 1999 goto end;
33f93973 2000 }
33f93973 2001end:
d94d92ac 2002 return status;
33f93973 2003}
This page took 0.14971 seconds and 4 git commands to generate.