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 enum bt_notification_iterator_status
ctf_fs_iterator_next(
57 struct bt_private_notification_iterator
*iterator
);
59 struct bt_notification
*ctf_fs_iterator_get(
60 struct bt_private_notification_iterator
*iterator
)
62 struct ctf_fs_iterator
*ctf_it
=
63 bt_private_notification_iterator_get_user_data(
66 if (!ctf_it
->current_notification
) {
67 (void) ctf_fs_iterator_next(iterator
);
70 return bt_get(ctf_it
->current_notification
);
74 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
75 struct ctf_fs_iterator
*it
,
76 struct ctf_fs_stream
*stream
,
77 struct bt_notification
**notification
)
79 enum bt_ctf_notif_iter_status status
;
80 enum bt_notification_iterator_status ret
;
82 if (stream
->end_reached
) {
83 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
87 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
89 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
90 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
94 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
95 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
96 *notification
= bt_notification_stream_end_create(
99 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
101 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
102 stream
->end_reached
= true;
106 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
107 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
109 case BT_CTF_NOTIF_ITER_STATUS_OK
:
110 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
112 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
114 * Should not make it this far as this is medium-specific;
115 * there is nothing for the user to do and it should have been
119 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
120 /* No argument provided by the user, so don't return INVAL. */
121 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
123 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
130 * Remove me. This is a temporary work-around due to our inhability to use
131 * libbabeltrace-ctf from libbabeltrace-plugin.
134 struct bt_ctf_stream
*internal_bt_notification_get_stream(
135 struct bt_notification
*notification
)
137 struct bt_ctf_stream
*stream
= NULL
;
139 assert(notification
);
140 switch (bt_notification_get_type(notification
)) {
141 case BT_NOTIFICATION_TYPE_EVENT
:
143 struct bt_ctf_event
*event
;
145 event
= bt_notification_event_get_event(notification
);
146 stream
= bt_ctf_event_get_stream(event
);
150 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
152 struct bt_ctf_packet
*packet
;
154 packet
= bt_notification_packet_begin_get_packet(notification
);
155 stream
= bt_ctf_packet_get_stream(packet
);
159 case BT_NOTIFICATION_TYPE_PACKET_END
:
161 struct bt_ctf_packet
*packet
;
163 packet
= bt_notification_packet_end_get_packet(notification
);
164 stream
= bt_ctf_packet_get_stream(packet
);
168 case BT_NOTIFICATION_TYPE_STREAM_END
:
169 stream
= bt_notification_stream_end_get_stream(notification
);
179 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
181 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
182 enum bt_notification_iterator_status ret
=
183 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
185 /* Insert one stream-associated notification for each stream. */
186 for (i
= 0; i
< pending_streams_count
; i
++) {
187 struct bt_notification
*notification
;
188 struct ctf_fs_stream
*fs_stream
;
189 struct bt_ctf_stream
*stream
;
190 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
192 fs_stream
= g_ptr_array_index(it
->pending_streams
,
193 pending_stream_index
);
198 ret
= ctf_fs_iterator_get_next_notification(
199 it
, fs_stream
, ¬ification
);
200 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
201 printf_debug("Failed to populate heap at stream %zu\n",
202 pending_stream_index
);
206 stream
= internal_bt_notification_get_stream(
212 * Associate pending ctf_fs_stream to
213 * bt_ctf_stream. Ownership of stream
214 * is passed to the stream ht.
216 inserted
= g_hash_table_insert(it
->stream_ht
,
219 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
220 printf_debug("Failed to associate fs stream to ctf stream\n");
225 heap_ret
= bt_notification_heap_insert(
226 it
->pending_notifications
,
228 bt_put(notification
);
230 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
231 printf_debug("Failed to insert notification in heap\n");
234 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
236 * Set NULL so the destruction callback registered with the
237 * array is not invoked on the stream (its ownership was
238 * transferred to the streams hashtable).
240 g_ptr_array_index(it
->pending_streams
,
241 pending_stream_index
) = NULL
;
242 g_ptr_array_remove_index(it
->pending_streams
,
243 pending_stream_index
);
246 g_ptr_array_free(it
->pending_streams
, TRUE
);
247 it
->pending_streams
= NULL
;
252 enum bt_notification_iterator_status
ctf_fs_iterator_next(
253 struct bt_private_notification_iterator
*iterator
)
256 struct bt_ctf_stream
*stream
= NULL
;
257 struct ctf_fs_stream
*fs_stream
;
258 struct bt_notification
*notification
;
259 struct bt_notification
*next_stream_notification
;
260 enum bt_notification_iterator_status ret
=
261 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
262 struct ctf_fs_iterator
*ctf_it
=
263 bt_private_notification_iterator_get_user_data(
266 notification
= bt_notification_heap_pop(ctf_it
->pending_notifications
);
267 if (!notification
&& !ctf_it
->pending_streams
) {
268 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
272 if (!notification
&& ctf_it
->pending_streams
) {
274 * Insert at one notification per stream in the heap and pop
277 ret
= populate_heap(ctf_it
);
282 notification
= bt_notification_heap_pop(
283 ctf_it
->pending_notifications
);
285 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
290 /* notification is set from here. */
292 stream
= internal_bt_notification_get_stream(notification
);
295 * The current notification is not associated to a particular
296 * stream, there is no need to insert a new notification from
297 * a stream in the heap.
302 fs_stream
= g_hash_table_lookup(ctf_it
->stream_ht
, stream
);
304 /* We have reached this stream's end. */
308 ret
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
309 &next_stream_notification
);
310 if ((ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
311 heap_ret
= bt_notification_heap_insert(
312 ctf_it
->pending_notifications
, notification
);
314 assert(!next_stream_notification
);
317 * We're dropping the most recent notification, but at
318 * this point, something is seriously wrong...
320 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
322 BT_PUT(notification
);
326 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
330 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
332 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
334 heap_ret
= bt_notification_heap_insert(ctf_it
->pending_notifications
,
335 next_stream_notification
);
336 BT_PUT(next_stream_notification
);
339 * We're dropping the most recent notification...
341 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
346 * Ensure that the stream is removed from both pending_streams and
347 * the streams hashtable on reception of the "end of stream"
351 BT_MOVE(ctf_it
->current_notification
, notification
);
357 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
362 bt_put(ctf_it
->current_notification
);
363 bt_put(ctf_it
->pending_notifications
);
364 if (ctf_it
->pending_streams
) {
365 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
367 if (ctf_it
->stream_ht
) {
368 g_hash_table_destroy(ctf_it
->stream_ht
);
373 void ctf_fs_iterator_destroy(struct bt_private_notification_iterator
*it
)
375 void *data
= bt_private_notification_iterator_get_user_data(it
);
377 ctf_fs_iterator_destroy_data(data
);
381 bool compare_event_notifications(struct bt_notification
*a
,
382 struct bt_notification
*b
)
385 struct bt_ctf_clock_class
*clock_class
;
386 struct bt_ctf_clock_value
*a_clock_value
, *b_clock_value
;
387 struct bt_ctf_stream_class
*a_stream_class
;
388 struct bt_ctf_stream
*a_stream
;
389 struct bt_ctf_event
*a_event
, *b_event
;
390 struct bt_ctf_trace
*trace
;
393 // FIXME - assumes only one clock
394 a_event
= bt_notification_event_get_event(a
);
395 b_event
= bt_notification_event_get_event(b
);
399 a_stream
= bt_ctf_event_get_stream(a_event
);
401 a_stream_class
= bt_ctf_stream_get_class(a_stream
);
402 assert(a_stream_class
);
403 trace
= bt_ctf_stream_class_get_trace(a_stream_class
);
406 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
407 a_clock_value
= bt_ctf_event_get_clock_value(a_event
, clock_class
);
408 b_clock_value
= bt_ctf_event_get_clock_value(b_event
, clock_class
);
409 assert(a_clock_value
);
410 assert(b_clock_value
);
412 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value
, &a_ts
);
414 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value
, &b_ts
);
419 bt_put(a_clock_value
);
420 bt_put(b_clock_value
);
422 bt_put(a_stream_class
);
429 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
432 static int notification_priorities
[] = {
433 [BT_NOTIFICATION_TYPE_NEW_TRACE
] = 0,
434 [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS
] = 1,
435 [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS
] = 2,
436 [BT_NOTIFICATION_TYPE_PACKET_BEGIN
] = 3,
437 [BT_NOTIFICATION_TYPE_PACKET_END
] = 4,
438 [BT_NOTIFICATION_TYPE_EVENT
] = 5,
439 [BT_NOTIFICATION_TYPE_END_OF_TRACE
] = 6,
442 enum bt_notification_type a_type
, b_type
;
445 a_type
= bt_notification_get_type(a
);
446 b_type
= bt_notification_get_type(b
);
447 assert(a_type
> BT_NOTIFICATION_TYPE_ALL
);
448 assert(a_type
< BT_NOTIFICATION_TYPE_NR
);
449 assert(b_type
> BT_NOTIFICATION_TYPE_ALL
);
450 assert(b_type
< BT_NOTIFICATION_TYPE_NR
);
452 a_prio
= notification_priorities
[a_type
];
453 b_prio
= notification_priorities
[b_type
];
455 if (likely((a_type
== b_type
) && a_type
== BT_NOTIFICATION_TYPE_EVENT
)) {
456 return compare_event_notifications(a
, b
);
459 if (unlikely(a_prio
!= b_prio
)) {
460 return a_prio
< b_prio
;
463 /* Notification types are equal, but not of type "event". */
465 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
466 case BT_NOTIFICATION_TYPE_PACKET_END
:
467 case BT_NOTIFICATION_TYPE_STREAM_END
:
469 int64_t a_sc_id
, b_sc_id
;
470 struct bt_ctf_stream
*a_stream
, *b_stream
;
471 struct bt_ctf_stream_class
*a_sc
, *b_sc
;
473 a_stream
= internal_bt_notification_get_stream(a
);
474 b_stream
= internal_bt_notification_get_stream(b
);
475 assert(a_stream
&& b_stream
);
477 a_sc
= bt_ctf_stream_get_class(a_stream
);
478 b_sc
= bt_ctf_stream_get_class(b_stream
);
479 assert(a_sc
&& b_sc
);
481 a_sc_id
= bt_ctf_stream_class_get_id(a_sc
);
482 b_sc_id
= bt_ctf_stream_class_get_id(b_sc
);
483 assert(a_sc_id
>= 0 && b_sc_id
>= 0);
488 return a_sc_id
< b_sc_id
;
490 case BT_NOTIFICATION_TYPE_NEW_TRACE
:
491 case BT_NOTIFICATION_TYPE_END_OF_TRACE
:
492 /* Impossible to have two separate traces. */
502 void stream_destroy(void *stream
)
504 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
508 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
509 struct ctf_fs_iterator
*ctf_it
)
513 GError
*error
= NULL
;
514 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
517 PERR("Cannot open directory \"%s\": %s (code %d)\n",
518 ctf_fs
->trace_path
->str
, error
->message
,
523 while ((name
= g_dir_read_name(dir
))) {
524 struct ctf_fs_file
*file
= NULL
;
525 struct ctf_fs_stream
*stream
= NULL
;
527 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
528 /* Ignore the metadata stream. */
529 PDBG("Ignoring metadata file \"%s\"\n",
534 if (name
[0] == '.') {
535 PDBG("Ignoring hidden file \"%s\"\n",
540 /* Create the file. */
541 file
= ctf_fs_file_create(ctf_fs
);
543 PERR("Cannot create stream file object\n");
547 /* Create full path string. */
548 g_string_append_printf(file
->path
, "%s/%s",
549 ctf_fs
->trace_path
->str
, name
);
550 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
551 PDBG("Ignoring non-regular file \"%s\"\n", name
);
552 ctf_fs_file_destroy(file
);
557 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
558 ctf_fs_file_destroy(file
);
562 if (file
->size
== 0) {
563 /* Skip empty stream. */
564 ctf_fs_file_destroy(file
);
568 /* Create a private stream; file ownership is passed to it. */
569 stream
= ctf_fs_stream_create(ctf_fs
, file
);
571 ctf_fs_file_destroy(file
);
575 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
592 enum bt_notification_iterator_status
ctf_fs_iterator_init(
593 struct bt_private_component
*source
,
594 struct bt_private_port
*port
,
595 struct bt_private_notification_iterator
*it
)
597 struct ctf_fs_iterator
*ctf_it
;
598 struct ctf_fs_component
*ctf_fs
;
599 enum bt_notification_iterator_status ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
601 assert(source
&& it
);
603 ctf_fs
= bt_private_component_get_user_data(source
);
605 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVAL
;
609 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
611 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
615 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
616 g_direct_equal
, bt_put
, stream_destroy
);
617 if (!ctf_it
->stream_ht
) {
620 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
622 if (!ctf_it
->pending_streams
) {
625 ctf_it
->pending_notifications
= bt_notification_heap_create(
626 compare_notifications
, NULL
);
627 if (!ctf_it
->pending_notifications
) {
631 ret
= open_trace_streams(ctf_fs
, ctf_it
);
636 ret
= bt_private_notification_iterator_set_user_data(it
, ctf_it
);
644 (void) bt_private_notification_iterator_set_user_data(it
, NULL
);
645 ctf_fs_iterator_destroy_data(ctf_it
);
650 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
655 if (ctf_fs
->trace_path
) {
656 g_string_free(ctf_fs
->trace_path
, TRUE
);
658 if (ctf_fs
->metadata
) {
659 ctf_fs_metadata_fini(ctf_fs
->metadata
);
660 g_free(ctf_fs
->metadata
);
665 void ctf_fs_destroy(struct bt_private_component
*component
)
667 void *data
= bt_private_component_get_user_data(component
);
669 ctf_fs_destroy_data(data
);
673 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
675 struct ctf_fs_component
*ctf_fs
;
676 struct bt_value
*value
= NULL
;
678 enum bt_value_status ret
;
680 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
685 /* FIXME: should probably look for a source URI */
686 value
= bt_value_map_get(params
, "path");
687 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
691 ret
= bt_value_string_get(value
, &path
);
692 if (ret
!= BT_VALUE_STATUS_OK
) {
696 ctf_fs
->trace_path
= g_string_new(path
);
697 if (!ctf_fs
->trace_path
) {
700 ctf_fs
->error_fp
= stderr
;
701 ctf_fs
->page_size
= (size_t) getpagesize();
703 // FIXME: check error.
704 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
705 if (!ctf_fs
->metadata
) {
708 ctf_fs_metadata_set_trace(ctf_fs
);
712 ctf_fs_destroy_data(ctf_fs
);
720 enum bt_component_status
ctf_fs_init(struct bt_private_component
*source
,
721 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
723 struct ctf_fs_component
*ctf_fs
;
724 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
727 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
728 ctf_fs
= ctf_fs_create(params
);
730 ret
= BT_COMPONENT_STATUS_NOMEM
;
734 ret
= bt_private_component_set_user_data(source
, ctf_fs
);
735 if (ret
!= BT_COMPONENT_STATUS_OK
) {
741 (void) bt_private_component_set_user_data(source
, NULL
);
742 ctf_fs_destroy_data(ctf_fs
);
747 struct bt_value
*ctf_fs_query(struct bt_component_class
*comp_class
,
748 const char *object
, struct bt_value
*params
)
750 struct bt_value
*results
= NULL
;
751 struct bt_value
*path_value
= NULL
;
752 char *metadata_text
= NULL
;
753 FILE *metadata_fp
= NULL
;
754 GString
*g_metadata_text
= NULL
;
756 if (strcmp(object
, "metadata-info") == 0) {
762 results
= bt_value_map_create();
767 if (!bt_value_is_map(params
)) {
769 "Query parameters is not a map value object\n");
773 path_value
= bt_value_map_get(params
, "path");
774 ret
= bt_value_string_get(path_value
, &path
);
777 "Cannot get `path` string parameter\n");
782 metadata_fp
= ctf_fs_metadata_open_file(path
);
785 "Cannot open trace at path `%s`\n", path
);
789 is_packetized
= ctf_metadata_is_packetized(metadata_fp
, &bo
);
792 ret
= ctf_metadata_packetized_file_to_buf(NULL
,
793 metadata_fp
, (uint8_t **) &metadata_text
, bo
);
796 "Cannot decode packetized metadata file\n");
802 fseek(metadata_fp
, 0, SEEK_END
);
803 filesize
= ftell(metadata_fp
);
805 metadata_text
= malloc(filesize
+ 1);
806 if (!metadata_text
) {
808 "Cannot allocate buffer for metadata text\n");
812 if (fread(metadata_text
, filesize
, 1, metadata_fp
) !=
815 "Cannot read metadata file\n");
819 metadata_text
[filesize
] = '\0';
822 g_metadata_text
= g_string_new(NULL
);
823 if (!g_metadata_text
) {
827 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
828 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
829 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
830 g_string_append(g_metadata_text
, " */\n\n");
833 g_string_append(g_metadata_text
, metadata_text
);
835 ret
= bt_value_map_insert_string(results
, "text",
836 g_metadata_text
->str
);
838 fprintf(stderr
, "Cannot insert metadata text into results\n");
842 ret
= bt_value_map_insert_bool(results
, "is-packetized",
845 fprintf(stderr
, "Cannot insert is packetized into results\n");
849 fprintf(stderr
, "Unknown query object `%s`\n", object
);
862 if (g_metadata_text
) {
863 g_string_free(g_metadata_text
, TRUE
);