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