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