523f6c34e8e73d01331973ff918c366378f429d7
[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 <plugins-common.h>
31 #include <glib.h>
32 #include <babeltrace/assert-internal.h>
33 #include <inttypes.h>
34 #include <stdbool.h>
35 #include "fs.h"
36 #include "metadata.h"
37 #include "data-stream-file.h"
38 #include "file.h"
39 #include "../common/metadata/decoder.h"
40 #include "../common/notif-iter/notif-iter.h"
41 #include "../common/utils/utils.h"
42 #include "query.h"
43
44 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC"
45 #include "logging.h"
46
47 static
48 int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data *notif_iter_data)
49 {
50 struct ctf_fs_ds_file_info *ds_file_info;
51 int ret = 0;
52
53 BT_ASSERT(notif_iter_data->ds_file_info_index <
54 notif_iter_data->ds_file_group->ds_file_infos->len);
55 ds_file_info = g_ptr_array_index(
56 notif_iter_data->ds_file_group->ds_file_infos,
57 notif_iter_data->ds_file_info_index);
58
59 ctf_fs_ds_file_destroy(notif_iter_data->ds_file);
60 notif_iter_data->ds_file = ctf_fs_ds_file_create(
61 notif_iter_data->ds_file_group->ctf_fs_trace,
62 notif_iter_data->notif_iter,
63 notif_iter_data->ds_file_group->stream,
64 ds_file_info->path->str);
65 if (!notif_iter_data->ds_file) {
66 ret = -1;
67 }
68
69 return ret;
70 }
71
72 static
73 void ctf_fs_notif_iter_data_destroy(
74 struct ctf_fs_notif_iter_data *notif_iter_data)
75 {
76 if (!notif_iter_data) {
77 return;
78 }
79
80 ctf_fs_ds_file_destroy(notif_iter_data->ds_file);
81
82 if (notif_iter_data->notif_iter) {
83 bt_notif_iter_destroy(notif_iter_data->notif_iter);
84 }
85
86 g_free(notif_iter_data);
87 }
88
89 struct bt_notification_iterator_next_method_return ctf_fs_iterator_next(
90 struct bt_private_connection_private_notification_iterator *iterator)
91 {
92 struct bt_notification_iterator_next_method_return next_ret;
93 struct ctf_fs_notif_iter_data *notif_iter_data =
94 bt_private_connection_private_notification_iterator_get_user_data(iterator);
95 int ret;
96
97 BT_ASSERT(notif_iter_data->ds_file);
98 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
99
100 if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_OK &&
101 bt_notification_get_type(next_ret.notification) ==
102 BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
103 if (notif_iter_data->skip_stream_begin_notifs) {
104 /*
105 * We already emitted a
106 * BT_NOTIFICATION_TYPE_STREAM_BEGIN
107 * notification: skip this one, get a new one.
108 */
109 BT_PUT(next_ret.notification);
110 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
111 BT_ASSERT(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_END);
112 goto end;
113 } else {
114 /*
115 * First BT_NOTIFICATION_TYPE_STREAM_BEGIN
116 * notification: skip all following.
117 */
118 notif_iter_data->skip_stream_begin_notifs = true;
119 goto end;
120 }
121 }
122
123 if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_OK &&
124 bt_notification_get_type(next_ret.notification) ==
125 BT_NOTIFICATION_TYPE_STREAM_END) {
126 notif_iter_data->ds_file_info_index++;
127
128 if (notif_iter_data->ds_file_info_index ==
129 notif_iter_data->ds_file_group->ds_file_infos->len) {
130 /*
131 * No more stream files to read: we reached the
132 * real end. Emit this
133 * BT_NOTIFICATION_TYPE_STREAM_END notification.
134 * The next time ctf_fs_iterator_next() is
135 * called for this notification iterator,
136 * ctf_fs_ds_file_next() will return
137 * BT_NOTIFICATION_ITERATOR_STATUS_END().
138 */
139 goto end;
140 }
141
142 BT_PUT(next_ret.notification);
143 bt_notif_iter_reset(notif_iter_data->notif_iter);
144
145 /*
146 * Open and start reading the next stream file within
147 * our stream file group.
148 */
149 ret = notif_iter_data_set_current_ds_file(notif_iter_data);
150 if (ret) {
151 next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
152 goto end;
153 }
154
155 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
156
157 /*
158 * If we get a notification, we expect to get a
159 * BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
160 * because the iterator's state machine emits one before
161 * even requesting the first block of data from the
162 * medium. Skip this notification because we're not
163 * really starting a new stream here, and try getting a
164 * new notification (which, if it works, is a
165 * BT_NOTIFICATION_TYPE_PACKET_BEGIN one). We're sure to
166 * get at least one pair of
167 * BT_NOTIFICATION_TYPE_PACKET_BEGIN and
168 * BT_NOTIFICATION_TYPE_PACKET_END notifications in the
169 * case of a single, empty packet. We know there's at
170 * least one packet because the stream file group does
171 * not contain empty stream files.
172 */
173 BT_ASSERT(notif_iter_data->skip_stream_begin_notifs);
174
175 if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
176 BT_ASSERT(bt_notification_get_type(next_ret.notification) ==
177 BT_NOTIFICATION_TYPE_STREAM_BEGIN);
178 BT_PUT(next_ret.notification);
179 next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file);
180 BT_ASSERT(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_END);
181 }
182 }
183
184 end:
185 return next_ret;
186 }
187
188 void ctf_fs_iterator_finalize(struct bt_private_connection_private_notification_iterator *it)
189 {
190 void *notif_iter_data =
191 bt_private_connection_private_notification_iterator_get_user_data(it);
192
193 ctf_fs_notif_iter_data_destroy(notif_iter_data);
194 }
195
196 enum bt_notification_iterator_status ctf_fs_iterator_init(
197 struct bt_private_connection_private_notification_iterator *it,
198 struct bt_private_port *port)
199 {
200 struct ctf_fs_port_data *port_data;
201 struct ctf_fs_notif_iter_data *notif_iter_data = NULL;
202 enum bt_notification_iterator_status ret =
203 BT_NOTIFICATION_ITERATOR_STATUS_OK;
204 int iret;
205
206 port_data = bt_private_port_get_user_data(port);
207 if (!port_data) {
208 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
209 goto error;
210 }
211
212 notif_iter_data = g_new0(struct ctf_fs_notif_iter_data, 1);
213 if (!notif_iter_data) {
214 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
215 goto error;
216 }
217
218 notif_iter_data->notif_iter = bt_notif_iter_create(
219 port_data->ds_file_group->ctf_fs_trace->metadata->trace,
220 bt_common_get_page_size() * 8,
221 ctf_fs_ds_file_medops, NULL);
222 if (!notif_iter_data->notif_iter) {
223 BT_LOGE_STR("Cannot create a CTF notification iterator.");
224 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
225 goto error;
226 }
227
228 notif_iter_data->ds_file_group = port_data->ds_file_group;
229 iret = notif_iter_data_set_current_ds_file(notif_iter_data);
230 if (iret) {
231 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
232 goto error;
233 }
234
235 ret = bt_private_connection_private_notification_iterator_set_user_data(it, notif_iter_data);
236 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
237 goto error;
238 }
239
240 notif_iter_data = NULL;
241 goto end;
242
243 error:
244 (void) bt_private_connection_private_notification_iterator_set_user_data(it, NULL);
245
246 end:
247 ctf_fs_notif_iter_data_destroy(notif_iter_data);
248 return ret;
249 }
250
251 static
252 void ctf_fs_destroy(struct ctf_fs_component *ctf_fs)
253 {
254 if (!ctf_fs) {
255 return;
256 }
257
258 if (ctf_fs->traces) {
259 g_ptr_array_free(ctf_fs->traces, TRUE);
260 }
261
262 if (ctf_fs->port_data) {
263 g_ptr_array_free(ctf_fs->port_data, TRUE);
264 }
265
266 g_free(ctf_fs);
267 }
268
269 BT_HIDDEN
270 void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace)
271 {
272 if (!ctf_fs_trace) {
273 return;
274 }
275
276 if (ctf_fs_trace->ds_file_groups) {
277 g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE);
278 }
279
280 if (ctf_fs_trace->path) {
281 g_string_free(ctf_fs_trace->path, TRUE);
282 }
283
284 if (ctf_fs_trace->name) {
285 g_string_free(ctf_fs_trace->name, TRUE);
286 }
287
288 if (ctf_fs_trace->metadata) {
289 ctf_fs_metadata_fini(ctf_fs_trace->metadata);
290 g_free(ctf_fs_trace->metadata);
291 }
292
293 bt_put(ctf_fs_trace->cc_prio_map);
294 g_free(ctf_fs_trace);
295 }
296
297 static
298 void ctf_fs_trace_destroy_notifier(void *data)
299 {
300 struct ctf_fs_trace *trace = data;
301 ctf_fs_trace_destroy(trace);
302 }
303
304 void ctf_fs_finalize(struct bt_private_component *component)
305 {
306 void *data = bt_private_component_get_user_data(component);
307
308 ctf_fs_destroy(data);
309 }
310
311 static
312 void port_data_destroy(void *data) {
313 struct ctf_fs_port_data *port_data = data;
314
315 if (!port_data) {
316 return;
317 }
318
319 g_free(port_data);
320 }
321
322 static
323 GString *get_stream_instance_unique_name(
324 struct ctf_fs_ds_file_group *ds_file_group)
325 {
326 GString *name;
327 struct ctf_fs_ds_file_info *ds_file_info;
328
329 name = g_string_new(NULL);
330 if (!name) {
331 goto end;
332 }
333
334 /*
335 * If there's more than one stream file in the stream file
336 * group, the first (earliest) stream file's path is used as
337 * the stream's unique name.
338 */
339 BT_ASSERT(ds_file_group->ds_file_infos->len > 0);
340 ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0);
341 g_string_assign(name, ds_file_info->path->str);
342
343 end:
344 return name;
345 }
346
347 static
348 int create_one_port_for_trace(struct ctf_fs_component *ctf_fs,
349 struct ctf_fs_trace *ctf_fs_trace,
350 struct ctf_fs_ds_file_group *ds_file_group)
351 {
352 int ret = 0;
353 struct ctf_fs_port_data *port_data = NULL;
354 GString *port_name = NULL;
355
356 port_name = get_stream_instance_unique_name(ds_file_group);
357 if (!port_name) {
358 goto error;
359 }
360
361 BT_LOGD("Creating one port named `%s`", port_name->str);
362
363 /* Create output port for this file */
364 port_data = g_new0(struct ctf_fs_port_data, 1);
365 if (!port_data) {
366 goto error;
367 }
368
369 port_data->ds_file_group = ds_file_group;
370 ret = bt_private_component_source_add_output_private_port(
371 ctf_fs->priv_comp, port_name->str, port_data, NULL);
372 if (ret) {
373 goto error;
374 }
375
376 g_ptr_array_add(ctf_fs->port_data, port_data);
377 port_data = NULL;
378 goto end;
379
380 error:
381 ret = -1;
382
383 end:
384 if (port_name) {
385 g_string_free(port_name, TRUE);
386 }
387
388 port_data_destroy(port_data);
389 return ret;
390 }
391
392 static
393 int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
394 struct ctf_fs_trace *ctf_fs_trace)
395 {
396 int ret = 0;
397 size_t i;
398
399 /* Create one output port for each stream file group */
400 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
401 struct ctf_fs_ds_file_group *ds_file_group =
402 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
403
404 ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace,
405 ds_file_group);
406 if (ret) {
407 BT_LOGE("Cannot create output port.");
408 goto end;
409 }
410 }
411
412 end:
413 return ret;
414 }
415
416 static
417 uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace *ctf_fs_trace,
418 struct bt_field *packet_header_field)
419 {
420 struct bt_field *stream_instance_id_field = NULL;
421 uint64_t stream_instance_id = -1ULL;
422 int ret;
423
424 if (!packet_header_field) {
425 goto end;
426 }
427
428 stream_instance_id_field = bt_field_structure_borrow_field_by_name(
429 packet_header_field, "stream_instance_id");
430 if (!stream_instance_id_field) {
431 goto end;
432 }
433
434 ret = bt_field_integer_unsigned_get_value(stream_instance_id_field,
435 &stream_instance_id);
436 if (ret) {
437 stream_instance_id = -1ULL;
438 goto end;
439 }
440
441 end:
442 bt_put(stream_instance_id_field);
443 return stream_instance_id;
444 }
445
446 uint64_t get_packet_context_timestamp_begin_ns(
447 struct ctf_fs_trace *ctf_fs_trace,
448 struct bt_field *packet_context_field)
449 {
450 int ret;
451 struct bt_field *timestamp_begin_field = NULL;
452 struct bt_field_type *timestamp_begin_ft = NULL;
453 uint64_t timestamp_begin_raw_value = -1ULL;
454 uint64_t timestamp_begin_ns = -1ULL;
455 int64_t timestamp_begin_ns_signed;
456 struct bt_clock_class *timestamp_begin_clock_class = NULL;
457 struct bt_clock_value *clock_value = NULL;
458
459 if (!packet_context_field) {
460 goto end;
461 }
462
463 timestamp_begin_field = bt_field_structure_borrow_field_by_name(
464 packet_context_field, "timestamp_begin");
465 if (!timestamp_begin_field) {
466 goto end;
467 }
468
469 timestamp_begin_ft = bt_field_borrow_type(timestamp_begin_field);
470 BT_ASSERT(timestamp_begin_ft);
471 timestamp_begin_clock_class =
472 bt_field_type_integer_borrow_mapped_clock_class(timestamp_begin_ft);
473 if (!timestamp_begin_clock_class) {
474 goto end;
475 }
476
477 ret = bt_field_integer_unsigned_get_value(timestamp_begin_field,
478 &timestamp_begin_raw_value);
479 if (ret) {
480 goto end;
481 }
482
483 clock_value = bt_clock_value_create(timestamp_begin_clock_class,
484 timestamp_begin_raw_value);
485 if (!clock_value) {
486 goto end;
487 }
488
489 ret = bt_clock_value_get_value_ns_from_epoch(clock_value,
490 &timestamp_begin_ns_signed);
491 if (ret) {
492 goto end;
493 }
494
495 timestamp_begin_ns = (uint64_t) timestamp_begin_ns_signed;
496
497 end:
498 bt_put(clock_value);
499 return timestamp_begin_ns;
500 }
501
502 static
503 void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info)
504 {
505 if (!ds_file_info) {
506 return;
507 }
508
509 if (ds_file_info->path) {
510 g_string_free(ds_file_info->path, TRUE);
511 }
512
513 ctf_fs_ds_index_destroy(ds_file_info->index);
514 g_free(ds_file_info);
515 }
516
517 static
518 struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path,
519 uint64_t begin_ns, struct ctf_fs_ds_index *index)
520 {
521 struct ctf_fs_ds_file_info *ds_file_info;
522
523 ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1);
524 if (!ds_file_info) {
525 goto end;
526 }
527
528 ds_file_info->path = g_string_new(path);
529 if (!ds_file_info->path) {
530 ctf_fs_ds_file_info_destroy(ds_file_info);
531 ds_file_info = NULL;
532 goto end;
533 }
534
535 ds_file_info->begin_ns = begin_ns;
536 ds_file_info->index = index;
537 index = NULL;
538
539 end:
540 ctf_fs_ds_index_destroy(index);
541 return ds_file_info;
542 }
543
544 static
545 void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
546 {
547 if (!ds_file_group) {
548 return;
549 }
550
551 if (ds_file_group->ds_file_infos) {
552 g_ptr_array_free(ds_file_group->ds_file_infos, TRUE);
553 }
554
555 bt_put(ds_file_group->stream);
556 bt_put(ds_file_group->stream_class);
557 g_free(ds_file_group);
558 }
559
560 static
561 struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(
562 struct ctf_fs_trace *ctf_fs_trace,
563 struct bt_stream_class *stream_class,
564 uint64_t stream_instance_id)
565 {
566 struct ctf_fs_ds_file_group *ds_file_group;
567
568 ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1);
569 if (!ds_file_group) {
570 goto error;
571 }
572
573 ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func(
574 (GDestroyNotify) ctf_fs_ds_file_info_destroy);
575 if (!ds_file_group->ds_file_infos) {
576 goto error;
577 }
578
579 ds_file_group->stream_id = stream_instance_id;
580 BT_ASSERT(stream_class);
581 ds_file_group->stream_class = bt_get(stream_class);
582 ds_file_group->ctf_fs_trace = ctf_fs_trace;
583 goto end;
584
585 error:
586 ctf_fs_ds_file_group_destroy(ds_file_group);
587 ds_file_group = NULL;
588
589 end:
590 return ds_file_group;
591 }
592
593 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
594 static
595 void array_insert(GPtrArray *array, gpointer element, size_t pos)
596 {
597 size_t original_array_len = array->len;
598
599 /* Allocate an unused element at the end of the array. */
600 g_ptr_array_add(array, NULL);
601
602 /* If we are not inserting at the end, move the elements by one. */
603 if (pos < original_array_len) {
604 memmove(&(array->pdata[pos + 1]),
605 &(array->pdata[pos]),
606 (original_array_len - pos) * sizeof(gpointer));
607 }
608
609 /* Insert the value and bump the array len */
610 array->pdata[pos] = element;
611 }
612
613 static
614 int ctf_fs_ds_file_group_add_ds_file_info(
615 struct ctf_fs_ds_file_group *ds_file_group,
616 const char *path, uint64_t begin_ns,
617 struct ctf_fs_ds_index *index)
618 {
619 struct ctf_fs_ds_file_info *ds_file_info;
620 gint i = 0;
621 int ret = 0;
622
623 /* Onwership of index is transferred. */
624 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index);
625 index = NULL;
626 if (!ds_file_info) {
627 goto error;
628 }
629
630 /* Find a spot to insert this one */
631 for (i = 0; i < ds_file_group->ds_file_infos->len; i++) {
632 struct ctf_fs_ds_file_info *other_ds_file_info =
633 g_ptr_array_index(ds_file_group->ds_file_infos, i);
634
635 if (begin_ns < other_ds_file_info->begin_ns) {
636 break;
637 }
638 }
639
640 array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
641 ds_file_info = NULL;
642 goto end;
643
644 error:
645 ctf_fs_ds_file_info_destroy(ds_file_info);
646 ctf_fs_ds_index_destroy(index);
647 ret = -1;
648 end:
649 return ret;
650 }
651
652 static
653 int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
654 const char *path)
655 {
656 struct bt_field *packet_header_field = NULL;
657 struct bt_field *packet_context_field = NULL;
658 struct bt_stream_class *stream_class = NULL;
659 uint64_t stream_instance_id = -1ULL;
660 uint64_t begin_ns = -1ULL;
661 struct ctf_fs_ds_file_group *ds_file_group = NULL;
662 bool add_group = false;
663 int ret;
664 size_t i;
665 struct ctf_fs_ds_file *ds_file = NULL;
666 struct ctf_fs_ds_index *index = NULL;
667 struct bt_notif_iter *notif_iter = NULL;
668
669 notif_iter = bt_notif_iter_create(ctf_fs_trace->metadata->trace,
670 bt_common_get_page_size() * 8, ctf_fs_ds_file_medops, NULL);
671 if (!notif_iter) {
672 BT_LOGE_STR("Cannot create a CTF notification iterator.");
673 goto error;
674 }
675
676 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, notif_iter, NULL, path);
677 if (!ds_file) {
678 goto error;
679 }
680
681 ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file,
682 &packet_header_field, &packet_context_field);
683 if (ret) {
684 BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).",
685 path);
686 goto error;
687 }
688
689 stream_instance_id = get_packet_header_stream_instance_id(ctf_fs_trace,
690 packet_header_field);
691 begin_ns = get_packet_context_timestamp_begin_ns(ctf_fs_trace,
692 packet_context_field);
693 stream_class = ctf_utils_borrow_stream_class_from_packet_header(
694 ctf_fs_trace->metadata->trace, packet_header_field);
695 if (!stream_class) {
696 goto error;
697 }
698
699 index = ctf_fs_ds_file_build_index(ds_file);
700 if (!index) {
701 BT_LOGW("Failed to index CTF stream file \'%s\'",
702 ds_file->file->path->str);
703 }
704
705 if (begin_ns == -1ULL) {
706 /*
707 * No beggining timestamp to sort the stream files
708 * within a stream file group, so consider that this
709 * file must be the only one within its group.
710 */
711 stream_instance_id = -1ULL;
712 }
713
714 if (stream_instance_id == -1ULL) {
715 /*
716 * No stream instance ID or no beginning timestamp:
717 * create a unique stream file group for this stream
718 * file because, even if there's a stream instance ID,
719 * there's no timestamp to order the file within its
720 * group.
721 */
722 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
723 stream_class, stream_instance_id);
724 if (!ds_file_group) {
725 goto error;
726 }
727
728 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group,
729 path, begin_ns, index);
730 /* Ownership of index is transferred. */
731 index = NULL;
732 if (ret) {
733 goto error;
734 }
735
736 add_group = true;
737 goto end;
738 }
739
740 BT_ASSERT(stream_instance_id != -1ULL);
741 BT_ASSERT(begin_ns != -1ULL);
742
743 /* Find an existing stream file group with this ID */
744 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
745 ds_file_group = g_ptr_array_index(
746 ctf_fs_trace->ds_file_groups, i);
747
748 if (ds_file_group->stream_class == stream_class &&
749 ds_file_group->stream_id ==
750 stream_instance_id) {
751 break;
752 }
753
754 ds_file_group = NULL;
755 }
756
757 if (!ds_file_group) {
758 ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace,
759 stream_class, stream_instance_id);
760 if (!ds_file_group) {
761 goto error;
762 }
763
764 add_group = true;
765 }
766
767 ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, path,
768 begin_ns, index);
769 index = NULL;
770 if (ret) {
771 goto error;
772 }
773
774 goto end;
775
776 error:
777 ctf_fs_ds_file_group_destroy(ds_file_group);
778 ret = -1;
779
780 end:
781 if (add_group && ds_file_group) {
782 g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group);
783 }
784
785 ctf_fs_ds_file_destroy(ds_file);
786
787 if (notif_iter) {
788 bt_notif_iter_destroy(notif_iter);
789 }
790
791 ctf_fs_ds_index_destroy(index);
792 bt_put(packet_header_field);
793 bt_put(packet_context_field);
794 return ret;
795 }
796
797 static
798 int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
799 {
800 int ret = 0;
801 const char *basename;
802 GError *error = NULL;
803 GDir *dir = NULL;
804 size_t i;
805
806 /* Check each file in the path directory, except specific ones */
807 dir = g_dir_open(ctf_fs_trace->path->str, 0, &error);
808 if (!dir) {
809 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
810 ctf_fs_trace->path->str, error->message,
811 error->code);
812 goto error;
813 }
814
815 while ((basename = g_dir_read_name(dir))) {
816 struct ctf_fs_file *file;
817
818 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
819 /* Ignore the metadata stream. */
820 BT_LOGD("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`",
821 ctf_fs_trace->path->str, basename);
822 continue;
823 }
824
825 if (basename[0] == '.') {
826 BT_LOGD("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`",
827 ctf_fs_trace->path->str, basename);
828 continue;
829 }
830
831 /* Create the file. */
832 file = ctf_fs_file_create();
833 if (!file) {
834 BT_LOGE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S "%s`",
835 ctf_fs_trace->path->str, basename);
836 goto error;
837 }
838
839 /* Create full path string. */
840 g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s",
841 ctf_fs_trace->path->str, basename);
842 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
843 BT_LOGD("Ignoring non-regular file `%s`",
844 file->path->str);
845 ctf_fs_file_destroy(file);
846 file = NULL;
847 continue;
848 }
849
850 ret = ctf_fs_file_open(file, "rb");
851 if (ret) {
852 BT_LOGE("Cannot open stream file `%s`", file->path->str);
853 goto error;
854 }
855
856 if (file->size == 0) {
857 /* Skip empty stream. */
858 BT_LOGD("Ignoring empty file `%s`", file->path->str);
859 ctf_fs_file_destroy(file);
860 continue;
861 }
862
863 ret = add_ds_file_to_ds_file_group(ctf_fs_trace,
864 file->path->str);
865 if (ret) {
866 BT_LOGE("Cannot add stream file `%s` to stream file group",
867 file->path->str);
868 ctf_fs_file_destroy(file);
869 goto error;
870 }
871
872 ctf_fs_file_destroy(file);
873 }
874
875 /*
876 * At this point, DS file groupes are created, but their
877 * associated stream objects do not exist yet. This is because
878 * we need to name the created stream object with the data
879 * stream file's path. We have everything we need here to do
880 * this.
881 */
882 for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) {
883 struct ctf_fs_ds_file_group *ds_file_group =
884 g_ptr_array_index(ctf_fs_trace->ds_file_groups, i);
885 GString *name = get_stream_instance_unique_name(ds_file_group);
886
887 if (!name) {
888 goto error;
889 }
890
891 if (ds_file_group->stream_id == -1ULL) {
892 /* No stream ID: use 0 */
893 ds_file_group->stream = bt_stream_create(
894 ds_file_group->stream_class, name->str,
895 ctf_fs_trace->next_stream_id);
896 ctf_fs_trace->next_stream_id++;
897 } else {
898 /* Specific stream ID */
899 ds_file_group->stream = bt_stream_create(
900 ds_file_group->stream_class, name->str,
901 ds_file_group->stream_id);
902 }
903
904 g_string_free(name, TRUE);
905
906 if (!ds_file_group->stream) {
907 BT_LOGE("Cannot create stream for DS file group: "
908 "addr=%p, stream-name=\"%s\"",
909 ds_file_group, name->str);
910 goto error;
911 }
912 }
913
914 goto end;
915
916 error:
917 ret = -1;
918
919 end:
920 if (dir) {
921 g_dir_close(dir);
922 dir = NULL;
923 }
924
925 if (error) {
926 g_error_free(error);
927 }
928
929 return ret;
930 }
931
932 static
933 int create_cc_prio_map(struct ctf_fs_trace *ctf_fs_trace)
934 {
935 int ret = 0;
936 size_t i;
937 int count;
938
939 BT_ASSERT(ctf_fs_trace);
940 ctf_fs_trace->cc_prio_map = bt_clock_class_priority_map_create();
941 if (!ctf_fs_trace->cc_prio_map) {
942 ret = -1;
943 goto end;
944 }
945
946 count = bt_trace_get_clock_class_count(
947 ctf_fs_trace->metadata->trace);
948 BT_ASSERT(count >= 0);
949
950 for (i = 0; i < count; i++) {
951 struct bt_clock_class *clock_class =
952 bt_trace_borrow_clock_class_by_index(
953 ctf_fs_trace->metadata->trace, i);
954
955 BT_ASSERT(clock_class);
956 ret = bt_clock_class_priority_map_add_clock_class(
957 ctf_fs_trace->cc_prio_map, clock_class, 0);
958
959 if (ret) {
960 goto end;
961 }
962 }
963
964 end:
965 return ret;
966 }
967
968 BT_HIDDEN
969 struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name,
970 struct ctf_fs_metadata_config *metadata_config)
971 {
972 struct ctf_fs_trace *ctf_fs_trace;
973 int ret;
974
975 ctf_fs_trace = g_new0(struct ctf_fs_trace, 1);
976 if (!ctf_fs_trace) {
977 goto end;
978 }
979
980 ctf_fs_trace->path = g_string_new(path);
981 if (!ctf_fs_trace->path) {
982 goto error;
983 }
984
985 ctf_fs_trace->name = g_string_new(name);
986 if (!ctf_fs_trace->name) {
987 goto error;
988 }
989
990 ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1);
991 if (!ctf_fs_trace->metadata) {
992 goto error;
993 }
994
995 ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func(
996 (GDestroyNotify) ctf_fs_ds_file_group_destroy);
997 if (!ctf_fs_trace->ds_file_groups) {
998 goto error;
999 }
1000
1001 ret = ctf_fs_metadata_set_trace(ctf_fs_trace, metadata_config);
1002 if (ret) {
1003 goto error;
1004 }
1005
1006 ret = create_ds_file_groups(ctf_fs_trace);
1007 if (ret) {
1008 goto error;
1009 }
1010
1011 ret = create_cc_prio_map(ctf_fs_trace);
1012 if (ret) {
1013 goto error;
1014 }
1015
1016 /*
1017 * create_ds_file_groups() created all the streams that this
1018 * trace needs. There won't be any more. Therefore it is safe to
1019 * make this trace static.
1020 */
1021 (void) bt_trace_set_is_static(ctf_fs_trace->metadata->trace);
1022
1023 goto end;
1024
1025 error:
1026 ctf_fs_trace_destroy(ctf_fs_trace);
1027 ctf_fs_trace = NULL;
1028 end:
1029 return ctf_fs_trace;
1030 }
1031
1032 static
1033 int path_is_ctf_trace(const char *path)
1034 {
1035 GString *metadata_path = g_string_new(NULL);
1036 int ret = 0;
1037
1038 if (!metadata_path) {
1039 ret = -1;
1040 goto end;
1041 }
1042
1043 g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
1044
1045 if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
1046 ret = 1;
1047 goto end;
1048 }
1049
1050 end:
1051 g_string_free(metadata_path, TRUE);
1052 return ret;
1053 }
1054
1055 static
1056 int add_trace_path(GList **trace_paths, const char *path)
1057 {
1058 GString *norm_path = NULL;
1059 int ret = 0;
1060
1061 norm_path = bt_common_normalize_path(path, NULL);
1062 if (!norm_path) {
1063 BT_LOGE("Failed to normalize path `%s`.", path);
1064 ret = -1;
1065 goto end;
1066 }
1067
1068 // FIXME: Remove or ifdef for __MINGW32__
1069 if (strcmp(norm_path->str, "/") == 0) {
1070 BT_LOGE("Opening a trace in `/` is not supported.");
1071 ret = -1;
1072 goto end;
1073 }
1074
1075 *trace_paths = g_list_prepend(*trace_paths, norm_path);
1076 BT_ASSERT(*trace_paths);
1077 norm_path = NULL;
1078
1079 end:
1080 if (norm_path) {
1081 g_string_free(norm_path, TRUE);
1082 }
1083
1084 return ret;
1085 }
1086
1087 BT_HIDDEN
1088 int ctf_fs_find_traces(GList **trace_paths, const char *start_path)
1089 {
1090 int ret;
1091 GError *error = NULL;
1092 GDir *dir = NULL;
1093 const char *basename = NULL;
1094
1095 /* Check if the starting path is a CTF trace itself */
1096 ret = path_is_ctf_trace(start_path);
1097 if (ret < 0) {
1098 goto end;
1099 }
1100
1101 if (ret) {
1102 /*
1103 * Stop recursion: a CTF trace cannot contain another
1104 * CTF trace.
1105 */
1106 ret = add_trace_path(trace_paths, start_path);
1107 goto end;
1108 }
1109
1110 /* Look for subdirectories */
1111 if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) {
1112 /* Starting path is not a directory: end of recursion */
1113 goto end;
1114 }
1115
1116 dir = g_dir_open(start_path, 0, &error);
1117 if (!dir) {
1118 if (error->code == G_FILE_ERROR_ACCES) {
1119 BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing",
1120 start_path, error->message, error->code);
1121 goto end;
1122 }
1123
1124 BT_LOGE("Cannot open directory `%s`: %s (code %d)",
1125 start_path, error->message, error->code);
1126 ret = -1;
1127 goto end;
1128 }
1129
1130 while ((basename = g_dir_read_name(dir))) {
1131 GString *sub_path = g_string_new(NULL);
1132
1133 if (!sub_path) {
1134 ret = -1;
1135 goto end;
1136 }
1137
1138 g_string_printf(sub_path, "%s" G_DIR_SEPARATOR_S "%s", start_path, basename);
1139 ret = ctf_fs_find_traces(trace_paths, sub_path->str);
1140 g_string_free(sub_path, TRUE);
1141 if (ret) {
1142 goto end;
1143 }
1144 }
1145
1146 end:
1147 if (dir) {
1148 g_dir_close(dir);
1149 }
1150
1151 if (error) {
1152 g_error_free(error);
1153 }
1154
1155 return ret;
1156 }
1157
1158 BT_HIDDEN
1159 GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) {
1160 GList *trace_names = NULL;
1161 GList *node;
1162 const char *last_sep;
1163 size_t base_dist;
1164
1165 /*
1166 * At this point we know that all the trace paths are
1167 * normalized, and so is the base path. This means that
1168 * they are absolute and they don't end with a separator.
1169 * We can simply find the location of the last separator
1170 * in the base path, which gives us the name of the actual
1171 * directory to look into, and use this location as the
1172 * start of each trace name within each trace path.
1173 *
1174 * For example:
1175 *
1176 * Base path: /home/user/my-traces/some-trace
1177 * Trace paths:
1178 * - /home/user/my-traces/some-trace/host1/trace1
1179 * - /home/user/my-traces/some-trace/host1/trace2
1180 * - /home/user/my-traces/some-trace/host2/trace
1181 * - /home/user/my-traces/some-trace/other-trace
1182 *
1183 * In this case the trace names are:
1184 *
1185 * - some-trace/host1/trace1
1186 * - some-trace/host1/trace2
1187 * - some-trace/host2/trace
1188 * - some-trace/other-trace
1189 */
1190 last_sep = strrchr(base_path, G_DIR_SEPARATOR);
1191
1192 /* We know there's at least one separator */
1193 BT_ASSERT(last_sep);
1194
1195 /* Distance to base */
1196 base_dist = last_sep - base_path + 1;
1197
1198 /* Create the trace names */
1199 for (node = trace_paths; node; node = g_list_next(node)) {
1200 GString *trace_name = g_string_new(NULL);
1201 GString *trace_path = node->data;
1202
1203 BT_ASSERT(trace_name);
1204 g_string_assign(trace_name, &trace_path->str[base_dist]);
1205 trace_names = g_list_append(trace_names, trace_name);
1206 }
1207
1208 return trace_names;
1209 }
1210
1211 static
1212 int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs,
1213 const char *path_param)
1214 {
1215 struct ctf_fs_trace *ctf_fs_trace = NULL;
1216 int ret = 0;
1217 GString *norm_path = NULL;
1218 GList *trace_paths = NULL;
1219 GList *trace_names = NULL;
1220 GList *tp_node;
1221 GList *tn_node;
1222
1223 norm_path = bt_common_normalize_path(path_param, NULL);
1224 if (!norm_path) {
1225 BT_LOGE("Failed to normalize path: `%s`.",
1226 path_param);
1227 goto error;
1228 }
1229
1230 ret = ctf_fs_find_traces(&trace_paths, norm_path->str);
1231 if (ret) {
1232 goto error;
1233 }
1234
1235 if (!trace_paths) {
1236 BT_LOGE("No CTF traces recursively found in `%s`.",
1237 path_param);
1238 goto error;
1239 }
1240
1241 trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str);
1242 if (!trace_names) {
1243 BT_LOGE("Cannot create trace names from trace paths.");
1244 goto error;
1245 }
1246
1247 for (tp_node = trace_paths, tn_node = trace_names; tp_node;
1248 tp_node = g_list_next(tp_node),
1249 tn_node = g_list_next(tn_node)) {
1250 GString *trace_path = tp_node->data;
1251 GString *trace_name = tn_node->data;
1252
1253 ctf_fs_trace = ctf_fs_trace_create(trace_path->str,
1254 trace_name->str, &ctf_fs->metadata_config);
1255 if (!ctf_fs_trace) {
1256 BT_LOGE("Cannot create trace for `%s`.",
1257 trace_path->str);
1258 goto error;
1259 }
1260
1261 ret = create_ports_for_trace(ctf_fs, ctf_fs_trace);
1262 if (ret) {
1263 goto error;
1264 }
1265
1266 g_ptr_array_add(ctf_fs->traces, ctf_fs_trace);
1267 ctf_fs_trace = NULL;
1268 }
1269
1270 goto end;
1271
1272 error:
1273 ret = -1;
1274 ctf_fs_trace_destroy(ctf_fs_trace);
1275
1276 end:
1277 for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) {
1278 if (tp_node->data) {
1279 g_string_free(tp_node->data, TRUE);
1280 }
1281 }
1282
1283 for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) {
1284 if (tn_node->data) {
1285 g_string_free(tn_node->data, TRUE);
1286 }
1287 }
1288
1289 if (trace_paths) {
1290 g_list_free(trace_paths);
1291 }
1292
1293 if (trace_names) {
1294 g_list_free(trace_names);
1295 }
1296
1297 if (norm_path) {
1298 g_string_free(norm_path, TRUE);
1299 }
1300
1301 return ret;
1302 }
1303
1304 static
1305 struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
1306 struct bt_value *params)
1307 {
1308 struct ctf_fs_component *ctf_fs;
1309 struct bt_value *value = NULL;
1310 const char *path_param;
1311 enum bt_component_status ret;
1312 enum bt_value_status value_ret;
1313
1314 ctf_fs = g_new0(struct ctf_fs_component, 1);
1315 if (!ctf_fs) {
1316 goto end;
1317 }
1318
1319 ret = bt_private_component_set_user_data(priv_comp, ctf_fs);
1320 BT_ASSERT(ret == BT_COMPONENT_STATUS_OK);
1321
1322 /*
1323 * We don't need to get a new reference here because as long as
1324 * our private ctf_fs_component object exists, the containing
1325 * private component should also exist.
1326 */
1327 ctf_fs->priv_comp = priv_comp;
1328 value = bt_value_map_borrow(params, "path");
1329 if (value && !bt_value_is_string(value)) {
1330 goto error;
1331 }
1332
1333 value_ret = bt_value_string_get(value, &path_param);
1334 BT_ASSERT(value_ret == BT_VALUE_STATUS_OK);
1335 value = bt_value_map_borrow(params, "clock-class-offset-s");
1336 if (value) {
1337 if (!bt_value_is_integer(value)) {
1338 BT_LOGE("clock-class-offset-s should be an integer");
1339 goto error;
1340 }
1341 value_ret = bt_value_integer_get(value,
1342 &ctf_fs->metadata_config.clock_class_offset_s);
1343 BT_ASSERT(value_ret == BT_VALUE_STATUS_OK);
1344 }
1345
1346 value = bt_value_map_borrow(params, "clock-class-offset-ns");
1347 if (value) {
1348 if (!bt_value_is_integer(value)) {
1349 BT_LOGE("clock-class-offset-ns should be an integer");
1350 goto error;
1351 }
1352 value_ret = bt_value_integer_get(value,
1353 &ctf_fs->metadata_config.clock_class_offset_ns);
1354 BT_ASSERT(value_ret == BT_VALUE_STATUS_OK);
1355 }
1356
1357 ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy);
1358 if (!ctf_fs->port_data) {
1359 goto error;
1360 }
1361
1362 ctf_fs->traces = g_ptr_array_new_with_free_func(
1363 ctf_fs_trace_destroy_notifier);
1364 if (!ctf_fs->traces) {
1365 goto error;
1366 }
1367
1368 if (create_ctf_fs_traces(ctf_fs, path_param)) {
1369 goto error;
1370 }
1371
1372 goto end;
1373
1374 error:
1375 ctf_fs_destroy(ctf_fs);
1376 ctf_fs = NULL;
1377 ret = bt_private_component_set_user_data(priv_comp, NULL);
1378 BT_ASSERT(ret == BT_COMPONENT_STATUS_OK);
1379
1380 end:
1381 return ctf_fs;
1382 }
1383
1384 BT_HIDDEN
1385 enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp,
1386 struct bt_value *params, UNUSED_VAR void *init_method_data)
1387 {
1388 struct ctf_fs_component *ctf_fs;
1389 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1390
1391 ctf_fs = ctf_fs_create(priv_comp, params);
1392 if (!ctf_fs) {
1393 ret = BT_COMPONENT_STATUS_ERROR;
1394 }
1395
1396 return ret;
1397 }
1398
1399 BT_HIDDEN
1400 struct bt_component_class_query_method_return ctf_fs_query(
1401 struct bt_component_class *comp_class,
1402 struct bt_query_executor *query_exec,
1403 const char *object, struct bt_value *params)
1404 {
1405 struct bt_component_class_query_method_return ret = {
1406 .result = NULL,
1407 .status = BT_QUERY_STATUS_OK,
1408 };
1409
1410 if (!strcmp(object, "metadata-info")) {
1411 ret = metadata_info_query(comp_class, params);
1412 } else if (!strcmp(object, "trace-info")) {
1413 ret = trace_info_query(comp_class, params);
1414 } else {
1415 BT_LOGE("Unknown query object `%s`", object);
1416 ret.status = BT_QUERY_STATUS_INVALID_OBJECT;
1417 goto end;
1418 }
1419 end:
1420 return ret;
1421 }
This page took 0.062043 seconds and 4 git commands to generate.