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"
49 static bool ctf_fs_debug
;
52 struct bt_notification
*ctf_fs_iterator_get(
53 struct bt_notification_iterator
*iterator
)
55 struct ctf_fs_iterator
*ctf_it
=
56 bt_notification_iterator_get_private_data(iterator
);
58 return bt_get(ctf_it
->current_notification
);
62 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
63 struct ctf_fs_iterator
*it
,
64 struct ctf_fs_stream
*stream
,
65 struct bt_notification
**notification
)
67 enum bt_ctf_notif_iter_status status
;
68 enum bt_notification_iterator_status ret
;
70 if (stream
->end_reached
) {
71 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
75 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
77 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
78 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
82 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
83 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
84 *notification
= bt_notification_stream_end_create(
87 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
89 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
90 stream
->end_reached
= true;
94 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
95 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
97 case BT_CTF_NOTIF_ITER_STATUS_OK
:
98 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
100 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
102 * Should not make it this far as this is medium-specific;
103 * there is nothing for the user to do and it should have been
107 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
108 /* No argument provided by the user, so don't return INVAL. */
109 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
111 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
118 * Remove me. This is a temporary work-around due to our inhability to use
119 * libbabeltrace-ctf from libbabeltrace-plugin.
122 struct bt_ctf_stream
*internal_bt_notification_get_stream(
123 struct bt_notification
*notification
)
125 struct bt_ctf_stream
*stream
= NULL
;
127 assert(notification
);
128 switch (bt_notification_get_type(notification
)) {
129 case BT_NOTIFICATION_TYPE_EVENT
:
131 struct bt_ctf_event
*event
;
133 event
= bt_notification_event_get_event(notification
);
134 stream
= bt_ctf_event_get_stream(event
);
138 case BT_NOTIFICATION_TYPE_PACKET_START
:
140 struct bt_ctf_packet
*packet
;
142 packet
= bt_notification_packet_start_get_packet(notification
);
143 stream
= bt_ctf_packet_get_stream(packet
);
147 case BT_NOTIFICATION_TYPE_PACKET_END
:
149 struct bt_ctf_packet
*packet
;
151 packet
= bt_notification_packet_end_get_packet(notification
);
152 stream
= bt_ctf_packet_get_stream(packet
);
156 case BT_NOTIFICATION_TYPE_STREAM_END
:
157 stream
= bt_notification_stream_end_get_stream(notification
);
167 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
169 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
170 enum bt_notification_iterator_status ret
=
171 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
173 /* Insert one stream-associated notification for each stream. */
174 for (i
= 0; i
< pending_streams_count
; i
++) {
175 struct bt_notification
*notification
;
176 struct ctf_fs_stream
*fs_stream
;
177 struct bt_ctf_stream
*stream
;
178 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
180 fs_stream
= g_ptr_array_index(it
->pending_streams
,
181 pending_stream_index
);
186 ret
= ctf_fs_iterator_get_next_notification(
187 it
, fs_stream
, ¬ification
);
188 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
189 printf_debug("Failed to populate heap at stream %zu\n",
190 pending_stream_index
);
194 stream
= internal_bt_notification_get_stream(
200 * Associate pending ctf_fs_stream to
201 * bt_ctf_stream. Ownership of stream
202 * is passed to the stream ht.
204 inserted
= g_hash_table_insert(it
->stream_ht
,
207 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
208 printf_debug("Failed to associate fs stream to ctf stream\n");
213 heap_ret
= bt_notification_heap_insert(
214 it
->pending_notifications
,
216 bt_put(notification
);
218 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
219 printf_debug("Failed to insert notification in heap\n");
222 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
224 * Set NULL so the destruction callback registered with the
225 * array is not invoked on the stream (its ownership was
226 * transferred to the streams hashtable).
228 g_ptr_array_index(it
->pending_streams
,
229 pending_stream_index
) = NULL
;
230 g_ptr_array_remove_index(it
->pending_streams
,
231 pending_stream_index
);
234 g_ptr_array_free(it
->pending_streams
, TRUE
);
235 it
->pending_streams
= NULL
;
241 enum bt_notification_iterator_status
ctf_fs_iterator_next(
242 struct bt_notification_iterator
*iterator
)
245 struct bt_ctf_stream
*stream
= NULL
;
246 struct ctf_fs_stream
*fs_stream
;
247 struct bt_notification
*notification
;
248 struct bt_notification
*next_stream_notification
;
249 enum bt_notification_iterator_status ret
=
250 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
251 struct ctf_fs_iterator
*ctf_it
=
252 bt_notification_iterator_get_private_data(iterator
);
254 notification
= bt_notification_heap_pop(ctf_it
->pending_notifications
);
255 if (!notification
&& !ctf_it
->pending_streams
) {
256 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
260 if (!notification
&& ctf_it
->pending_streams
) {
262 * Insert at one notification per stream in the heap and pop
265 ret
= populate_heap(ctf_it
);
270 notification
= bt_notification_heap_pop(
271 ctf_it
->pending_notifications
);
273 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
278 /* notification is set from here. */
280 stream
= internal_bt_notification_get_stream(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
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
297 &next_stream_notification
);
298 if ((ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
299 heap_ret
= bt_notification_heap_insert(
300 ctf_it
->pending_notifications
, notification
);
302 assert(!next_stream_notification
);
305 * We're dropping the most recent notification, but at
306 * this point, something is seriously wrong...
308 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
310 BT_PUT(notification
);
314 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
318 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
320 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
322 heap_ret
= bt_notification_heap_insert(ctf_it
->pending_notifications
,
323 next_stream_notification
);
324 BT_PUT(next_stream_notification
);
327 * We're dropping the most recent notification...
329 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
334 * Ensure that the stream is removed from both pending_streams and
335 * the streams hashtable on reception of the "end of stream"
339 BT_MOVE(ctf_it
->current_notification
, notification
);
345 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
347 bt_put(ctf_it
->current_notification
);
348 bt_put(ctf_it
->pending_notifications
);
349 if (ctf_it
->pending_streams
) {
350 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
352 if (ctf_it
->stream_ht
) {
353 g_hash_table_destroy(ctf_it
->stream_ht
);
359 void ctf_fs_iterator_destroy(struct bt_notification_iterator
*it
)
361 void *data
= bt_notification_iterator_get_private_data(it
);
363 ctf_fs_iterator_destroy_data(data
);
367 bool compare_event_notifications(struct bt_notification
*a
,
368 struct bt_notification
*b
)
371 struct bt_ctf_clock
*clock
;
372 struct bt_ctf_clock_value
*a_clock_value
, *b_clock_value
;
373 struct bt_ctf_stream_class
*a_stream_class
;
374 struct bt_ctf_stream
*a_stream
;
375 struct bt_ctf_event
*a_event
, *b_event
;
376 struct bt_ctf_trace
*trace
;
379 // FIXME - assumes only one clock
380 a_event
= bt_notification_event_get_event(a
);
381 b_event
= bt_notification_event_get_event(b
);
385 a_stream
= bt_ctf_event_get_stream(a_event
);
387 a_stream_class
= bt_ctf_stream_get_class(a_stream
);
388 assert(a_stream_class
);
389 trace
= bt_ctf_stream_class_get_trace(a_stream_class
);
392 clock
= bt_ctf_trace_get_clock(trace
, 0);
393 a_clock_value
= bt_ctf_event_get_clock_value(a_event
, clock
);
394 b_clock_value
= bt_ctf_event_get_clock_value(b_event
, clock
);
395 assert(a_clock_value
);
396 assert(b_clock_value
);
398 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value
, &a_ts
);
400 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value
, &b_ts
);
405 bt_put(a_clock_value
);
406 bt_put(b_clock_value
);
408 bt_put(a_stream_class
);
415 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
418 static int notification_priorities
[] = {
419 [BT_NOTIFICATION_TYPE_NEW_TRACE
] = 0,
420 [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS
] = 1,
421 [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS
] = 2,
422 [BT_NOTIFICATION_TYPE_PACKET_START
] = 3,
423 [BT_NOTIFICATION_TYPE_PACKET_END
] = 4,
424 [BT_NOTIFICATION_TYPE_EVENT
] = 5,
425 [BT_NOTIFICATION_TYPE_END_OF_TRACE
] = 6,
428 enum bt_notification_type a_type
, b_type
;
431 a_type
= bt_notification_get_type(a
);
432 b_type
= bt_notification_get_type(b
);
433 assert(a_type
> BT_NOTIFICATION_TYPE_ALL
);
434 assert(a_type
< BT_NOTIFICATION_TYPE_NR
);
435 assert(b_type
> BT_NOTIFICATION_TYPE_ALL
);
436 assert(b_type
< BT_NOTIFICATION_TYPE_NR
);
438 a_prio
= notification_priorities
[a_type
];
439 b_prio
= notification_priorities
[b_type
];
441 if (likely((a_type
== b_type
) && a_type
== BT_NOTIFICATION_TYPE_EVENT
)) {
442 return compare_event_notifications(a
, b
);
445 if (unlikely(a_prio
!= b_prio
)) {
446 return a_prio
< b_prio
;
449 /* Notification types are equal, but not of type "event". */
451 case BT_NOTIFICATION_TYPE_PACKET_START
:
452 case BT_NOTIFICATION_TYPE_PACKET_END
:
453 case BT_NOTIFICATION_TYPE_STREAM_END
:
455 int64_t a_sc_id
, b_sc_id
;
456 struct bt_ctf_stream
*a_stream
, *b_stream
;
457 struct bt_ctf_stream_class
*a_sc
, *b_sc
;
459 a_stream
= internal_bt_notification_get_stream(a
);
460 b_stream
= internal_bt_notification_get_stream(b
);
461 assert(a_stream
&& b_stream
);
463 a_sc
= bt_ctf_stream_get_class(a_stream
);
464 b_sc
= bt_ctf_stream_get_class(b_stream
);
465 assert(a_sc
&& b_sc
);
467 a_sc_id
= bt_ctf_stream_class_get_id(a_sc
);
468 b_sc_id
= bt_ctf_stream_class_get_id(b_sc
);
469 assert(a_sc_id
>= 0 && b_sc_id
>= 0);
474 return a_sc_id
< b_sc_id
;
476 case BT_NOTIFICATION_TYPE_NEW_TRACE
:
477 case BT_NOTIFICATION_TYPE_END_OF_TRACE
:
478 /* Impossible to have two separate traces. */
488 void stream_destroy(void *stream
)
490 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
494 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
495 struct ctf_fs_iterator
*ctf_it
)
499 GError
*error
= NULL
;
500 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
503 PERR("Cannot open directory \"%s\": %s (code %d)\n",
504 ctf_fs
->trace_path
->str
, error
->message
,
509 while ((name
= g_dir_read_name(dir
))) {
510 struct ctf_fs_file
*file
= NULL
;
511 struct ctf_fs_stream
*stream
= NULL
;
513 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
514 /* Ignore the metadata stream. */
515 PDBG("Ignoring metadata file \"%s\"\n",
520 if (name
[0] == '.') {
521 PDBG("Ignoring hidden file \"%s\"\n",
526 /* Create the file. */
527 file
= ctf_fs_file_create(ctf_fs
);
529 PERR("Cannot create stream file object\n");
533 /* Create full path string. */
534 g_string_append_printf(file
->path
, "%s/%s",
535 ctf_fs
->trace_path
->str
, name
);
536 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
537 PDBG("Ignoring non-regular file \"%s\"\n", name
);
538 ctf_fs_file_destroy(file
);
543 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
544 ctf_fs_file_destroy(file
);
548 /* Create a private stream; file ownership is passed to it. */
549 stream
= ctf_fs_stream_create(ctf_fs
, file
);
551 ctf_fs_file_destroy(file
);
555 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
573 enum bt_component_status
ctf_fs_iterator_init(struct bt_component
*source
,
574 struct bt_notification_iterator
*it
)
576 struct ctf_fs_iterator
*ctf_it
;
577 struct ctf_fs_component
*ctf_fs
;
578 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
580 assert(source
&& it
);
582 ctf_fs
= bt_component_get_private_data(source
);
584 ret
= BT_COMPONENT_STATUS_INVALID
;
588 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
590 ret
= BT_COMPONENT_STATUS_NOMEM
;
594 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
595 g_direct_equal
, bt_put
, stream_destroy
);
596 if (!ctf_it
->stream_ht
) {
599 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
601 if (!ctf_it
->pending_streams
) {
604 ctf_it
->pending_notifications
= bt_notification_heap_create(
605 compare_notifications
, NULL
);
606 if (!ctf_it
->pending_notifications
) {
610 ret
= open_trace_streams(ctf_fs
, ctf_it
);
615 ret
= bt_notification_iterator_set_get_cb(it
, ctf_fs_iterator_get
);
620 ret
= bt_notification_iterator_set_next_cb(it
, ctf_fs_iterator_next
);
625 ret
= bt_notification_iterator_set_destroy_cb(it
,
626 ctf_fs_iterator_destroy
);
631 ret
= bt_notification_iterator_set_private_data(it
, ctf_it
);
638 (void) bt_notification_iterator_set_private_data(it
, NULL
);
639 ctf_fs_iterator_destroy_data(ctf_it
);
644 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
646 if (ctf_fs
->trace_path
) {
647 g_string_free(ctf_fs
->trace_path
, TRUE
);
649 if (ctf_fs
->metadata
) {
650 ctf_fs_metadata_fini(ctf_fs
->metadata
);
651 g_free(ctf_fs
->metadata
);
657 void ctf_fs_destroy(struct bt_component
*component
)
659 void *data
= bt_component_get_private_data(component
);
661 ctf_fs_destroy_data(data
);
665 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
667 struct ctf_fs_component
*ctf_fs
;
668 struct bt_value
*value
= NULL
;
670 enum bt_value_status ret
;
672 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
677 /* FIXME: should probably look for a source URI */
678 value
= bt_value_map_get(params
, "path");
679 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
683 ret
= bt_value_string_get(value
, &path
);
684 if (ret
!= BT_VALUE_STATUS_OK
) {
688 ctf_fs
->trace_path
= g_string_new(path
);
689 if (!ctf_fs
->trace_path
) {
692 ctf_fs
->error_fp
= stderr
;
693 ctf_fs
->page_size
= (size_t) getpagesize();
695 // FIXME: check error.
696 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
697 if (!ctf_fs
->metadata
) {
700 ctf_fs_metadata_set_trace(ctf_fs
);
704 ctf_fs_destroy_data(ctf_fs
);
712 enum bt_component_status
ctf_fs_init(struct bt_component
*source
,
713 struct bt_value
*params
)
715 struct ctf_fs_component
*ctf_fs
;
716 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
719 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
720 ctf_fs
= ctf_fs_create(params
);
722 ret
= BT_COMPONENT_STATUS_NOMEM
;
726 ret
= bt_component_set_destroy_cb(source
, ctf_fs_destroy
);
727 if (ret
!= BT_COMPONENT_STATUS_OK
) {
731 ret
= bt_component_set_private_data(source
, ctf_fs
);
732 if (ret
!= BT_COMPONENT_STATUS_OK
) {
736 ret
= bt_component_source_set_iterator_init_cb(source
,
737 ctf_fs_iterator_init
);
738 if (ret
!= BT_COMPONENT_STATUS_OK
) {
744 (void) bt_component_set_private_data(source
, NULL
);
745 ctf_fs_destroy_data(ctf_fs
);