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