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