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/plugin/plugin-system.h>
30 #include <babeltrace/ctf-ir/packet.h>
31 #include <babeltrace/ctf-ir/clock.h>
32 #include <babeltrace/plugin/notification/iterator.h>
33 #include <babeltrace/plugin/notification/stream.h>
34 #include <babeltrace/plugin/notification/event.h>
35 #include <babeltrace/plugin/notification/packet.h>
36 #include <babeltrace/plugin/notification/heap.h>
42 #include "data-stream.h"
45 #define PRINT_ERR_STREAM ctf_fs->error_fp
46 #define PRINT_PREFIX "ctf-fs"
53 struct bt_notification
*ctf_fs_iterator_get(
54 struct bt_notification_iterator
*iterator
)
56 struct ctf_fs_iterator
*ctf_it
=
57 bt_notification_iterator_get_private_data(iterator
);
59 return bt_get(ctf_it
->current_notification
);
63 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
64 struct ctf_fs_iterator
*it
,
65 struct ctf_fs_stream
*stream
,
66 struct bt_notification
**notification
)
68 enum bt_ctf_notif_iter_status status
;
69 enum bt_notification_iterator_status ret
;
71 if (stream
->end_reached
) {
72 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
76 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
78 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
79 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
83 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
84 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
85 *notification
= bt_notification_stream_end_create(
88 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
90 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
91 stream
->end_reached
= true;
95 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
96 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
98 case BT_CTF_NOTIF_ITER_STATUS_OK
:
99 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
101 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
103 * Should not make it this far as this is medium-specific;
104 * there is nothing for the user to do and it should have been
108 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
109 /* No argument provided by the user, so don't return INVAL. */
110 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
112 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
119 * Remove me. This is a temporary work-around due to our inhability to use
120 * libbabeltrace-ctf from libbabeltrace-plugin.
123 struct bt_ctf_stream
*internal_bt_notification_get_stream(
124 struct bt_notification
*notification
)
126 struct bt_ctf_stream
*stream
= NULL
;
128 assert(notification
);
129 switch (bt_notification_get_type(notification
)) {
130 case BT_NOTIFICATION_TYPE_EVENT
:
132 struct bt_ctf_event
*event
;
134 event
= bt_notification_event_get_event(notification
);
135 stream
= bt_ctf_event_get_stream(event
);
139 case BT_NOTIFICATION_TYPE_PACKET_START
:
141 struct bt_ctf_packet
*packet
;
143 packet
= bt_notification_packet_start_get_packet(notification
);
144 stream
= bt_ctf_packet_get_stream(packet
);
148 case BT_NOTIFICATION_TYPE_PACKET_END
:
150 struct bt_ctf_packet
*packet
;
152 packet
= bt_notification_packet_end_get_packet(notification
);
153 stream
= bt_ctf_packet_get_stream(packet
);
157 case BT_NOTIFICATION_TYPE_STREAM_END
:
158 stream
= bt_notification_stream_end_get_stream(notification
);
168 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
170 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
171 enum bt_notification_iterator_status ret
=
172 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
174 /* Insert one stream-associated notification for each stream. */
175 for (i
= 0; i
< pending_streams_count
; i
++) {
176 struct bt_notification
*notification
;
177 struct ctf_fs_stream
*fs_stream
;
178 struct bt_ctf_stream
*stream
;
179 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
181 fs_stream
= g_ptr_array_index(it
->pending_streams
,
182 pending_stream_index
);
187 ret
= ctf_fs_iterator_get_next_notification(
188 it
, fs_stream
, ¬ification
);
189 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
190 printf_debug("Failed to populate heap at stream %zu\n",
191 pending_stream_index
);
195 stream
= internal_bt_notification_get_stream(
201 * Associate pending ctf_fs_stream to
202 * bt_ctf_stream. Ownership of stream
203 * is passed to the stream ht.
205 inserted
= g_hash_table_insert(it
->stream_ht
,
208 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
209 printf_debug("Failed to associate fs stream to ctf stream\n");
214 heap_ret
= bt_notification_heap_insert(
215 it
->pending_notifications
,
217 bt_put(notification
);
219 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
220 printf_debug("Failed to insert notification in heap\n");
223 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
225 * Set NULL so the destruction callback registered with the
226 * array is not invoked on the stream (its ownership was
227 * transferred to the streams hashtable).
229 g_ptr_array_index(it
->pending_streams
,
230 pending_stream_index
) = NULL
;
231 g_ptr_array_remove_index(it
->pending_streams
,
232 pending_stream_index
);
235 g_ptr_array_free(it
->pending_streams
, TRUE
);
236 it
->pending_streams
= NULL
;
242 enum bt_notification_iterator_status
ctf_fs_iterator_next(
243 struct bt_notification_iterator
*iterator
)
246 struct bt_ctf_stream
*stream
= NULL
;
247 struct ctf_fs_stream
*fs_stream
;
248 struct bt_notification
*notification
;
249 struct bt_notification
*next_stream_notification
;
250 enum bt_notification_iterator_status ret
=
251 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
252 struct ctf_fs_iterator
*ctf_it
=
253 bt_notification_iterator_get_private_data(iterator
);
255 notification
= bt_notification_heap_pop(ctf_it
->pending_notifications
);
256 if (!notification
&& !ctf_it
->pending_streams
) {
257 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
261 if (!notification
&& ctf_it
->pending_streams
) {
263 * Insert at one notification per stream in the heap and pop
266 ret
= populate_heap(ctf_it
);
271 notification
= bt_notification_heap_pop(
272 ctf_it
->pending_notifications
);
274 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
279 /* notification is set from here. */
281 stream
= internal_bt_notification_get_stream(notification
);
284 * The current notification is not associated to a particular
285 * stream, there is no need to insert a new notification from
286 * a stream in the heap.
291 fs_stream
= g_hash_table_lookup(ctf_it
->stream_ht
, stream
);
293 /* We have reached this stream's end. */
297 ret
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
298 &next_stream_notification
);
299 if ((ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
300 heap_ret
= bt_notification_heap_insert(
301 ctf_it
->pending_notifications
, notification
);
303 assert(!next_stream_notification
);
306 * We're dropping the most recent notification, but at
307 * this point, something is seriously wrong...
309 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
311 BT_PUT(notification
);
315 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
319 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
321 ret
= 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
= 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"
340 BT_MOVE(ctf_it
->current_notification
, notification
);
346 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
348 bt_put(ctf_it
->current_notification
);
349 bt_put(ctf_it
->pending_notifications
);
350 if (ctf_it
->pending_streams
) {
351 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
353 if (ctf_it
->stream_ht
) {
354 g_hash_table_destroy(ctf_it
->stream_ht
);
360 void ctf_fs_iterator_destroy(struct bt_notification_iterator
*it
)
362 void *data
= bt_notification_iterator_get_private_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
*clock
;
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
= bt_ctf_trace_get_clock(trace
, 0);
394 a_clock_value
= bt_ctf_event_get_clock_value(a_event
, clock
);
395 b_clock_value
= bt_ctf_event_get_clock_value(b_event
, clock
);
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_START
] = 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_START
:
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 /* Create a private stream; file ownership is passed to it. */
550 stream
= ctf_fs_stream_create(ctf_fs
, file
);
552 ctf_fs_file_destroy(file
);
556 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
574 enum bt_component_status
ctf_fs_iterator_init(struct bt_component
*source
,
575 struct bt_notification_iterator
*it
)
577 struct ctf_fs_iterator
*ctf_it
;
578 struct ctf_fs_component
*ctf_fs
;
579 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
581 assert(source
&& it
);
583 ctf_fs
= bt_component_get_private_data(source
);
585 ret
= BT_COMPONENT_STATUS_INVALID
;
589 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
591 ret
= BT_COMPONENT_STATUS_NOMEM
;
595 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
596 g_direct_equal
, bt_put
, stream_destroy
);
597 if (!ctf_it
->stream_ht
) {
600 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
602 if (!ctf_it
->pending_streams
) {
605 ctf_it
->pending_notifications
= bt_notification_heap_create(
606 compare_notifications
, NULL
);
607 if (!ctf_it
->pending_notifications
) {
611 ret
= open_trace_streams(ctf_fs
, ctf_it
);
616 ret
= bt_notification_iterator_set_get_cb(it
, ctf_fs_iterator_get
);
621 ret
= bt_notification_iterator_set_next_cb(it
, ctf_fs_iterator_next
);
626 ret
= bt_notification_iterator_set_destroy_cb(it
,
627 ctf_fs_iterator_destroy
);
632 ret
= bt_notification_iterator_set_private_data(it
, ctf_it
);
639 (void) bt_notification_iterator_set_private_data(it
, NULL
);
640 ctf_fs_iterator_destroy_data(ctf_it
);
645 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
647 if (ctf_fs
->trace_path
) {
648 g_string_free(ctf_fs
->trace_path
, TRUE
);
650 if (ctf_fs
->metadata
) {
651 ctf_fs_metadata_fini(ctf_fs
->metadata
);
652 g_free(ctf_fs
->metadata
);
658 void ctf_fs_destroy(struct bt_component
*component
)
660 void *data
= bt_component_get_private_data(component
);
662 ctf_fs_destroy_data(data
);
666 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
668 struct ctf_fs_component
*ctf_fs
;
669 struct bt_value
*value
= NULL
;
671 enum bt_value_status ret
;
673 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
678 /* FIXME: should probably look for a source URI */
679 value
= bt_value_map_get(params
, "path");
680 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
684 ret
= bt_value_string_get(value
, &path
);
685 if (ret
!= BT_VALUE_STATUS_OK
) {
689 ctf_fs
->trace_path
= g_string_new(path
);
690 if (!ctf_fs
->trace_path
) {
693 ctf_fs
->error_fp
= stderr
;
694 ctf_fs
->page_size
= (size_t) getpagesize();
696 // FIXME: check error.
697 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
698 if (!ctf_fs
->metadata
) {
701 ctf_fs_metadata_set_trace(ctf_fs
);
705 ctf_fs_destroy_data(ctf_fs
);
713 enum bt_component_status
ctf_fs_init(struct bt_component
*source
,
714 struct bt_value
*params
)
716 struct ctf_fs_component
*ctf_fs
;
717 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
720 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
721 ctf_fs
= ctf_fs_create(params
);
723 ret
= BT_COMPONENT_STATUS_NOMEM
;
727 ret
= bt_component_set_destroy_cb(source
, ctf_fs_destroy
);
728 if (ret
!= BT_COMPONENT_STATUS_OK
) {
732 ret
= bt_component_set_private_data(source
, ctf_fs
);
733 if (ret
!= BT_COMPONENT_STATUS_OK
) {
737 ret
= bt_component_source_set_iterator_init_cb(source
,
738 ctf_fs_iterator_init
);
739 if (ret
!= BT_COMPONENT_STATUS_OK
) {
745 (void) bt_component_set_private_data(source
, NULL
);
746 ctf_fs_destroy_data(ctf_fs
);