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