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