4 * Babeltrace CTF file system Reader Component
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/component/private-component.h>
32 #include <babeltrace/component/component.h>
33 #include <babeltrace/component/notification/iterator.h>
34 #include <babeltrace/component/notification/private-iterator.h>
35 #include <babeltrace/component/notification/stream.h>
36 #include <babeltrace/component/notification/event.h>
37 #include <babeltrace/component/notification/packet.h>
38 #include <babeltrace/component/notification/heap.h>
39 #include <plugins-common.h>
45 #include "data-stream.h"
48 #define PRINT_ERR_STREAM ctf_fs->error_fp
49 #define PRINT_PREFIX "ctf-fs"
51 #define METADATA_TEXT_SIG "/* CTF 1.8"
56 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
57 struct bt_private_notification_iterator
*iterator
);
60 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
61 struct ctf_fs_iterator
*it
,
62 struct ctf_fs_stream
*stream
,
63 struct bt_notification
**notification
)
65 enum bt_ctf_notif_iter_status status
;
66 enum bt_notification_iterator_status ret
;
68 if (stream
->end_reached
) {
69 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
73 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
75 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
76 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
80 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
81 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
82 *notification
= bt_notification_stream_end_create(
85 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
87 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
88 stream
->end_reached
= true;
92 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
93 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
95 case BT_CTF_NOTIF_ITER_STATUS_OK
:
96 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
98 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
100 * Should not make it this far as this is medium-specific;
101 * there is nothing for the user to do and it should have been
105 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
106 /* No argument provided by the user, so don't return INVAL. */
107 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
109 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
116 * Remove me. This is a temporary work-around due to our inhability to use
117 * libbabeltrace-ctf from libbabeltrace-plugin.
120 struct bt_ctf_stream
*internal_bt_notification_get_stream(
121 struct bt_notification
*notification
)
123 struct bt_ctf_stream
*stream
= NULL
;
125 assert(notification
);
126 switch (bt_notification_get_type(notification
)) {
127 case BT_NOTIFICATION_TYPE_EVENT
:
129 struct bt_ctf_event
*event
;
131 event
= bt_notification_event_get_event(notification
);
132 stream
= bt_ctf_event_get_stream(event
);
136 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
138 struct bt_ctf_packet
*packet
;
140 packet
= bt_notification_packet_begin_get_packet(notification
);
141 stream
= bt_ctf_packet_get_stream(packet
);
145 case BT_NOTIFICATION_TYPE_PACKET_END
:
147 struct bt_ctf_packet
*packet
;
149 packet
= bt_notification_packet_end_get_packet(notification
);
150 stream
= bt_ctf_packet_get_stream(packet
);
154 case BT_NOTIFICATION_TYPE_STREAM_END
:
155 stream
= bt_notification_stream_end_get_stream(notification
);
165 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
167 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
168 enum bt_notification_iterator_status ret
=
169 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
171 /* Insert one stream-associated notification for each stream. */
172 for (i
= 0; i
< pending_streams_count
; i
++) {
173 struct bt_notification
*notification
;
174 struct ctf_fs_stream
*fs_stream
;
175 struct bt_ctf_stream
*stream
;
176 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
178 fs_stream
= g_ptr_array_index(it
->pending_streams
,
179 pending_stream_index
);
184 ret
= ctf_fs_iterator_get_next_notification(
185 it
, fs_stream
, ¬ification
);
186 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
187 printf_debug("Failed to populate heap at stream %zu\n",
188 pending_stream_index
);
192 stream
= internal_bt_notification_get_stream(
198 * Associate pending ctf_fs_stream to
199 * bt_ctf_stream. Ownership of stream
200 * is passed to the stream ht.
202 inserted
= g_hash_table_insert(it
->stream_ht
,
205 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
206 printf_debug("Failed to associate fs stream to ctf stream\n");
211 heap_ret
= bt_notification_heap_insert(
212 it
->pending_notifications
,
214 bt_put(notification
);
216 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
217 printf_debug("Failed to insert notification in heap\n");
220 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
222 * Set NULL so the destruction callback registered with the
223 * array is not invoked on the stream (its ownership was
224 * transferred to the streams hashtable).
226 g_ptr_array_index(it
->pending_streams
,
227 pending_stream_index
) = NULL
;
228 g_ptr_array_remove_index(it
->pending_streams
,
229 pending_stream_index
);
232 g_ptr_array_free(it
->pending_streams
, TRUE
);
233 it
->pending_streams
= NULL
;
238 struct bt_notification_iterator_next_return
ctf_fs_iterator_next(
239 struct bt_private_notification_iterator
*iterator
)
242 struct bt_ctf_stream
*stream
= NULL
;
243 struct ctf_fs_stream
*fs_stream
;
244 struct bt_notification
*next_stream_notification
;
245 struct ctf_fs_iterator
*ctf_it
=
246 bt_private_notification_iterator_get_user_data(
248 struct bt_notification_iterator_next_return ret
= {
249 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
250 .notification
= NULL
,
254 bt_notification_heap_pop(ctf_it
->pending_notifications
);
255 if (!ret
.notification
&& !ctf_it
->pending_streams
) {
256 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
260 if (!ret
.notification
&& ctf_it
->pending_streams
) {
262 * Insert at one notification per stream in the heap and pop
265 ret
.status
= populate_heap(ctf_it
);
270 ret
.notification
= bt_notification_heap_pop(
271 ctf_it
->pending_notifications
);
272 if (!ret
.notification
) {
273 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
278 /* notification is set from here. */
280 stream
= internal_bt_notification_get_stream(ret
.notification
);
283 * The current notification is not associated to a particular
284 * stream, there is no need to insert a new notification from
285 * a stream in the heap.
290 fs_stream
= g_hash_table_lookup(ctf_it
->stream_ht
, stream
);
292 /* We have reached this stream's end. */
296 ret
.status
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
297 &next_stream_notification
);
298 if ((ret
.status
&& ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
299 heap_ret
= bt_notification_heap_insert(
300 ctf_it
->pending_notifications
,
303 assert(!next_stream_notification
);
306 * We're dropping the most recent notification, but at
307 * this point, something is seriously wrong...
309 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
311 BT_PUT(ret
.notification
);
315 if (ret
.status
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
319 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
321 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
323 heap_ret
= bt_notification_heap_insert(ctf_it
->pending_notifications
,
324 next_stream_notification
);
325 BT_PUT(next_stream_notification
);
328 * We're dropping the most recent notification...
330 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
335 * Ensure that the stream is removed from both pending_streams and
336 * the streams hashtable on reception of the "end of stream"
345 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
350 bt_put(ctf_it
->pending_notifications
);
351 if (ctf_it
->pending_streams
) {
352 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
354 if (ctf_it
->stream_ht
) {
355 g_hash_table_destroy(ctf_it
->stream_ht
);
360 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator
*it
)
362 void *data
= bt_private_notification_iterator_get_user_data(it
);
364 ctf_fs_iterator_destroy_data(data
);
368 bool compare_event_notifications(struct bt_notification
*a
,
369 struct bt_notification
*b
)
372 struct bt_ctf_clock_class
*clock_class
;
373 struct bt_ctf_clock_value
*a_clock_value
, *b_clock_value
;
374 struct bt_ctf_stream_class
*a_stream_class
;
375 struct bt_ctf_stream
*a_stream
;
376 struct bt_ctf_event
*a_event
, *b_event
;
377 struct bt_ctf_trace
*trace
;
380 // FIXME - assumes only one clock
381 a_event
= bt_notification_event_get_event(a
);
382 b_event
= bt_notification_event_get_event(b
);
386 a_stream
= bt_ctf_event_get_stream(a_event
);
388 a_stream_class
= bt_ctf_stream_get_class(a_stream
);
389 assert(a_stream_class
);
390 trace
= bt_ctf_stream_class_get_trace(a_stream_class
);
393 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
394 a_clock_value
= bt_ctf_event_get_clock_value(a_event
, clock_class
);
395 b_clock_value
= bt_ctf_event_get_clock_value(b_event
, clock_class
);
396 assert(a_clock_value
);
397 assert(b_clock_value
);
399 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value
, &a_ts
);
401 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value
, &b_ts
);
406 bt_put(a_clock_value
);
407 bt_put(b_clock_value
);
409 bt_put(a_stream_class
);
416 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
419 static int notification_priorities
[] = {
420 [BT_NOTIFICATION_TYPE_NEW_TRACE
] = 0,
421 [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS
] = 1,
422 [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS
] = 2,
423 [BT_NOTIFICATION_TYPE_PACKET_BEGIN
] = 3,
424 [BT_NOTIFICATION_TYPE_PACKET_END
] = 4,
425 [BT_NOTIFICATION_TYPE_EVENT
] = 5,
426 [BT_NOTIFICATION_TYPE_END_OF_TRACE
] = 6,
429 enum bt_notification_type a_type
, b_type
;
432 a_type
= bt_notification_get_type(a
);
433 b_type
= bt_notification_get_type(b
);
434 assert(a_type
> BT_NOTIFICATION_TYPE_ALL
);
435 assert(a_type
< BT_NOTIFICATION_TYPE_NR
);
436 assert(b_type
> BT_NOTIFICATION_TYPE_ALL
);
437 assert(b_type
< BT_NOTIFICATION_TYPE_NR
);
439 a_prio
= notification_priorities
[a_type
];
440 b_prio
= notification_priorities
[b_type
];
442 if (likely((a_type
== b_type
) && a_type
== BT_NOTIFICATION_TYPE_EVENT
)) {
443 return compare_event_notifications(a
, b
);
446 if (unlikely(a_prio
!= b_prio
)) {
447 return a_prio
< b_prio
;
450 /* Notification types are equal, but not of type "event". */
452 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
453 case BT_NOTIFICATION_TYPE_PACKET_END
:
454 case BT_NOTIFICATION_TYPE_STREAM_END
:
456 int64_t a_sc_id
, b_sc_id
;
457 struct bt_ctf_stream
*a_stream
, *b_stream
;
458 struct bt_ctf_stream_class
*a_sc
, *b_sc
;
460 a_stream
= internal_bt_notification_get_stream(a
);
461 b_stream
= internal_bt_notification_get_stream(b
);
462 assert(a_stream
&& b_stream
);
464 a_sc
= bt_ctf_stream_get_class(a_stream
);
465 b_sc
= bt_ctf_stream_get_class(b_stream
);
466 assert(a_sc
&& b_sc
);
468 a_sc_id
= bt_ctf_stream_class_get_id(a_sc
);
469 b_sc_id
= bt_ctf_stream_class_get_id(b_sc
);
470 assert(a_sc_id
>= 0 && b_sc_id
>= 0);
475 return a_sc_id
< b_sc_id
;
477 case BT_NOTIFICATION_TYPE_NEW_TRACE
:
478 case BT_NOTIFICATION_TYPE_END_OF_TRACE
:
479 /* Impossible to have two separate traces. */
489 void stream_destroy(void *stream
)
491 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
495 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
496 struct ctf_fs_iterator
*ctf_it
)
500 GError
*error
= NULL
;
501 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
504 PERR("Cannot open directory \"%s\": %s (code %d)\n",
505 ctf_fs
->trace_path
->str
, error
->message
,
510 while ((name
= g_dir_read_name(dir
))) {
511 struct ctf_fs_file
*file
= NULL
;
512 struct ctf_fs_stream
*stream
= NULL
;
514 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
515 /* Ignore the metadata stream. */
516 PDBG("Ignoring metadata file \"%s\"\n",
521 if (name
[0] == '.') {
522 PDBG("Ignoring hidden file \"%s\"\n",
527 /* Create the file. */
528 file
= ctf_fs_file_create(ctf_fs
);
530 PERR("Cannot create stream file object\n");
534 /* Create full path string. */
535 g_string_append_printf(file
->path
, "%s/%s",
536 ctf_fs
->trace_path
->str
, name
);
537 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
538 PDBG("Ignoring non-regular file \"%s\"\n", name
);
539 ctf_fs_file_destroy(file
);
544 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
545 ctf_fs_file_destroy(file
);
549 if (file
->size
== 0) {
550 /* Skip empty stream. */
551 ctf_fs_file_destroy(file
);
555 /* Create a private stream; file ownership is passed to it. */
556 stream
= ctf_fs_stream_create(ctf_fs
, file
);
558 ctf_fs_file_destroy(file
);
562 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
579 enum bt_notification_iterator_status
ctf_fs_iterator_init(
580 struct bt_private_component
*source
,
581 struct bt_private_port
*port
,
582 struct bt_private_notification_iterator
*it
)
584 struct ctf_fs_iterator
*ctf_it
;
585 struct ctf_fs_component
*ctf_fs
;
586 enum bt_notification_iterator_status ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
588 assert(source
&& it
);
590 ctf_fs
= bt_private_component_get_user_data(source
);
592 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVAL
;
596 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
598 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
602 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
603 g_direct_equal
, bt_put
, stream_destroy
);
604 if (!ctf_it
->stream_ht
) {
607 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
609 if (!ctf_it
->pending_streams
) {
612 ctf_it
->pending_notifications
= bt_notification_heap_create(
613 compare_notifications
, NULL
);
614 if (!ctf_it
->pending_notifications
) {
618 ret
= open_trace_streams(ctf_fs
, ctf_it
);
623 ret
= bt_private_notification_iterator_set_user_data(it
, ctf_it
);
631 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
632 ctf_fs_iterator_destroy_data(ctf_it
);
637 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
642 if (ctf_fs
->trace_path
) {
643 g_string_free(ctf_fs
->trace_path
, TRUE
);
645 if (ctf_fs
->metadata
) {
646 ctf_fs_metadata_fini(ctf_fs
->metadata
);
647 g_free(ctf_fs
->metadata
);
652 void ctf_fs_finalize(struct bt_private_component
*component
)
654 void *data
= bt_private_component_get_user_data(component
);
656 ctf_fs_destroy_data(data
);
660 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
662 struct ctf_fs_component
*ctf_fs
;
663 struct bt_value
*value
= NULL
;
665 enum bt_value_status ret
;
667 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
672 /* FIXME: should probably look for a source URI */
673 value
= bt_value_map_get(params
, "path");
674 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
678 ret
= bt_value_string_get(value
, &path
);
679 if (ret
!= BT_VALUE_STATUS_OK
) {
683 ctf_fs
->trace_path
= g_string_new(path
);
684 if (!ctf_fs
->trace_path
) {
687 ctf_fs
->error_fp
= stderr
;
688 ctf_fs
->page_size
= (size_t) getpagesize();
690 // FIXME: check error.
691 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
692 if (!ctf_fs
->metadata
) {
695 ctf_fs_metadata_set_trace(ctf_fs
);
699 ctf_fs_destroy_data(ctf_fs
);
707 enum bt_component_status
ctf_fs_init(struct bt_private_component
*source
,
708 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
710 struct ctf_fs_component
*ctf_fs
;
711 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
714 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
715 ctf_fs
= ctf_fs_create(params
);
717 ret
= BT_COMPONENT_STATUS_NOMEM
;
721 ret
= bt_private_component_set_user_data(source
, ctf_fs
);
722 if (ret
!= BT_COMPONENT_STATUS_OK
) {
728 (void) bt_private_component_set_user_data(source
, NULL
);
729 ctf_fs_destroy_data(ctf_fs
);
734 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
735 const char *object
, struct bt_value
*params
)
737 struct bt_value
*results
= NULL
;
738 struct bt_value
*path_value
= NULL
;
739 char *metadata_text
= NULL
;
740 FILE *metadata_fp
= NULL
;
741 GString
*g_metadata_text
= NULL
;
743 if (strcmp(object
, "metadata-info") == 0) {
749 results
= bt_value_map_create();
754 if (!bt_value_is_map(params
)) {
756 "Query parameters is not a map value object\n");
760 path_value
= bt_value_map_get(params
, "path");
761 ret
= bt_value_string_get(path_value
, &path
);
764 "Cannot get `path` string parameter\n");
769 metadata_fp
= ctf_fs_metadata_open_file(path
);
772 "Cannot open trace at path `%s`\n", path
);
776 is_packetized
= ctf_metadata_is_packetized(metadata_fp
, &bo
);
779 ret
= ctf_metadata_packetized_file_to_buf(NULL
,
780 metadata_fp
, (uint8_t **) &metadata_text
, bo
);
783 "Cannot decode packetized metadata file\n");
789 fseek(metadata_fp
, 0, SEEK_END
);
790 filesize
= ftell(metadata_fp
);
792 metadata_text
= malloc(filesize
+ 1);
793 if (!metadata_text
) {
795 "Cannot allocate buffer for metadata text\n");
799 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
802 "Cannot read metadata file\n");
806 metadata_text
[filesize
] = '\0';
809 g_metadata_text
= g_string_new(NULL
);
810 if (!g_metadata_text
) {
814 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
815 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
816 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
817 g_string_append(g_metadata_text
, " */\n\n");
820 g_string_append(g_metadata_text
, metadata_text
);
822 ret
= bt_value_map_insert_string(results
, "text",
823 g_metadata_text
->str
);
825 fprintf(stderr
, "Cannot insert metadata text into results\n");
829 ret
= bt_value_map_insert_bool(results
, "is-packetized",
832 fprintf(stderr
, "Cannot insert is packetized into results\n");
836 fprintf(stderr
, "Unknown query object `%s`\n", object
);
849 if (g_metadata_text
) {
850 g_string_free(g_metadata_text
, TRUE
);