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