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 enum bt_notification_iterator_status
ctf_fs_iterator_next(
54 struct bt_notification_iterator
*iterator
);
57 struct bt_notification
*ctf_fs_iterator_get(
58 struct bt_notification_iterator
*iterator
)
60 struct ctf_fs_iterator
*ctf_it
=
61 bt_notification_iterator_get_private_data(iterator
);
63 if (!ctf_it
->current_notification
) {
64 (void) ctf_fs_iterator_next(iterator
);
67 return bt_get(ctf_it
->current_notification
);
71 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
72 struct ctf_fs_iterator
*it
,
73 struct ctf_fs_stream
*stream
,
74 struct bt_notification
**notification
)
76 enum bt_ctf_notif_iter_status status
;
77 enum bt_notification_iterator_status ret
;
79 if (stream
->end_reached
) {
80 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
84 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
86 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
87 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
91 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
92 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
93 *notification
= bt_notification_stream_end_create(
96 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
98 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
99 stream
->end_reached
= true;
103 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
104 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
106 case BT_CTF_NOTIF_ITER_STATUS_OK
:
107 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
109 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
111 * Should not make it this far as this is medium-specific;
112 * there is nothing for the user to do and it should have been
116 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
117 /* No argument provided by the user, so don't return INVAL. */
118 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
120 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
127 * Remove me. This is a temporary work-around due to our inhability to use
128 * libbabeltrace-ctf from libbabeltrace-plugin.
131 struct bt_ctf_stream
*internal_bt_notification_get_stream(
132 struct bt_notification
*notification
)
134 struct bt_ctf_stream
*stream
= NULL
;
136 assert(notification
);
137 switch (bt_notification_get_type(notification
)) {
138 case BT_NOTIFICATION_TYPE_EVENT
:
140 struct bt_ctf_event
*event
;
142 event
= bt_notification_event_get_event(notification
);
143 stream
= bt_ctf_event_get_stream(event
);
147 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
149 struct bt_ctf_packet
*packet
;
151 packet
= bt_notification_packet_begin_get_packet(notification
);
152 stream
= bt_ctf_packet_get_stream(packet
);
156 case BT_NOTIFICATION_TYPE_PACKET_END
:
158 struct bt_ctf_packet
*packet
;
160 packet
= bt_notification_packet_end_get_packet(notification
);
161 stream
= bt_ctf_packet_get_stream(packet
);
165 case BT_NOTIFICATION_TYPE_STREAM_END
:
166 stream
= bt_notification_stream_end_get_stream(notification
);
176 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
178 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
179 enum bt_notification_iterator_status ret
=
180 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
182 /* Insert one stream-associated notification for each stream. */
183 for (i
= 0; i
< pending_streams_count
; i
++) {
184 struct bt_notification
*notification
;
185 struct ctf_fs_stream
*fs_stream
;
186 struct bt_ctf_stream
*stream
;
187 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
189 fs_stream
= g_ptr_array_index(it
->pending_streams
,
190 pending_stream_index
);
195 ret
= ctf_fs_iterator_get_next_notification(
196 it
, fs_stream
, ¬ification
);
197 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
198 printf_debug("Failed to populate heap at stream %zu\n",
199 pending_stream_index
);
203 stream
= internal_bt_notification_get_stream(
209 * Associate pending ctf_fs_stream to
210 * bt_ctf_stream. Ownership of stream
211 * is passed to the stream ht.
213 inserted
= g_hash_table_insert(it
->stream_ht
,
216 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
217 printf_debug("Failed to associate fs stream to ctf stream\n");
222 heap_ret
= bt_notification_heap_insert(
223 it
->pending_notifications
,
225 bt_put(notification
);
227 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
228 printf_debug("Failed to insert notification in heap\n");
231 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
233 * Set NULL so the destruction callback registered with the
234 * array is not invoked on the stream (its ownership was
235 * transferred to the streams hashtable).
237 g_ptr_array_index(it
->pending_streams
,
238 pending_stream_index
) = NULL
;
239 g_ptr_array_remove_index(it
->pending_streams
,
240 pending_stream_index
);
243 g_ptr_array_free(it
->pending_streams
, TRUE
);
244 it
->pending_streams
= NULL
;
250 enum bt_notification_iterator_status
ctf_fs_iterator_next(
251 struct bt_notification_iterator
*iterator
)
254 struct bt_ctf_stream
*stream
= NULL
;
255 struct ctf_fs_stream
*fs_stream
;
256 struct bt_notification
*notification
;
257 struct bt_notification
*next_stream_notification
;
258 enum bt_notification_iterator_status ret
=
259 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
260 struct ctf_fs_iterator
*ctf_it
=
261 bt_notification_iterator_get_private_data(iterator
);
263 notification
= bt_notification_heap_pop(ctf_it
->pending_notifications
);
264 if (!notification
&& !ctf_it
->pending_streams
) {
265 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
269 if (!notification
&& ctf_it
->pending_streams
) {
271 * Insert at one notification per stream in the heap and pop
274 ret
= populate_heap(ctf_it
);
279 notification
= bt_notification_heap_pop(
280 ctf_it
->pending_notifications
);
282 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
287 /* notification is set from here. */
289 stream
= internal_bt_notification_get_stream(notification
);
292 * The current notification is not associated to a particular
293 * stream, there is no need to insert a new notification from
294 * a stream in the heap.
299 fs_stream
= g_hash_table_lookup(ctf_it
->stream_ht
, stream
);
301 /* We have reached this stream's end. */
305 ret
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
306 &next_stream_notification
);
307 if ((ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
308 heap_ret
= bt_notification_heap_insert(
309 ctf_it
->pending_notifications
, notification
);
311 assert(!next_stream_notification
);
314 * We're dropping the most recent notification, but at
315 * this point, something is seriously wrong...
317 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
319 BT_PUT(notification
);
323 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
327 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
329 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
331 heap_ret
= bt_notification_heap_insert(ctf_it
->pending_notifications
,
332 next_stream_notification
);
333 BT_PUT(next_stream_notification
);
336 * We're dropping the most recent notification...
338 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
343 * Ensure that the stream is removed from both pending_streams and
344 * the streams hashtable on reception of the "end of stream"
348 BT_MOVE(ctf_it
->current_notification
, notification
);
354 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
356 bt_put(ctf_it
->current_notification
);
357 bt_put(ctf_it
->pending_notifications
);
358 if (ctf_it
->pending_streams
) {
359 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
361 if (ctf_it
->stream_ht
) {
362 g_hash_table_destroy(ctf_it
->stream_ht
);
368 void ctf_fs_iterator_destroy(struct bt_notification_iterator
*it
)
370 void *data
= bt_notification_iterator_get_private_data(it
);
372 ctf_fs_iterator_destroy_data(data
);
376 bool compare_event_notifications(struct bt_notification
*a
,
377 struct bt_notification
*b
)
380 struct bt_ctf_clock
*clock
;
381 struct bt_ctf_clock_value
*a_clock_value
, *b_clock_value
;
382 struct bt_ctf_stream_class
*a_stream_class
;
383 struct bt_ctf_stream
*a_stream
;
384 struct bt_ctf_event
*a_event
, *b_event
;
385 struct bt_ctf_trace
*trace
;
388 // FIXME - assumes only one clock
389 a_event
= bt_notification_event_get_event(a
);
390 b_event
= bt_notification_event_get_event(b
);
394 a_stream
= bt_ctf_event_get_stream(a_event
);
396 a_stream_class
= bt_ctf_stream_get_class(a_stream
);
397 assert(a_stream_class
);
398 trace
= bt_ctf_stream_class_get_trace(a_stream_class
);
401 clock
= bt_ctf_trace_get_clock(trace
, 0);
402 a_clock_value
= bt_ctf_event_get_clock_value(a_event
, clock
);
403 b_clock_value
= bt_ctf_event_get_clock_value(b_event
, clock
);
404 assert(a_clock_value
);
405 assert(b_clock_value
);
407 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value
, &a_ts
);
409 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value
, &b_ts
);
414 bt_put(a_clock_value
);
415 bt_put(b_clock_value
);
417 bt_put(a_stream_class
);
424 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
427 static int notification_priorities
[] = {
428 [BT_NOTIFICATION_TYPE_NEW_TRACE
] = 0,
429 [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS
] = 1,
430 [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS
] = 2,
431 [BT_NOTIFICATION_TYPE_PACKET_BEGIN
] = 3,
432 [BT_NOTIFICATION_TYPE_PACKET_END
] = 4,
433 [BT_NOTIFICATION_TYPE_EVENT
] = 5,
434 [BT_NOTIFICATION_TYPE_END_OF_TRACE
] = 6,
437 enum bt_notification_type a_type
, b_type
;
440 a_type
= bt_notification_get_type(a
);
441 b_type
= bt_notification_get_type(b
);
442 assert(a_type
> BT_NOTIFICATION_TYPE_ALL
);
443 assert(a_type
< BT_NOTIFICATION_TYPE_NR
);
444 assert(b_type
> BT_NOTIFICATION_TYPE_ALL
);
445 assert(b_type
< BT_NOTIFICATION_TYPE_NR
);
447 a_prio
= notification_priorities
[a_type
];
448 b_prio
= notification_priorities
[b_type
];
450 if (likely((a_type
== b_type
) && a_type
== BT_NOTIFICATION_TYPE_EVENT
)) {
451 return compare_event_notifications(a
, b
);
454 if (unlikely(a_prio
!= b_prio
)) {
455 return a_prio
< b_prio
;
458 /* Notification types are equal, but not of type "event". */
460 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
461 case BT_NOTIFICATION_TYPE_PACKET_END
:
462 case BT_NOTIFICATION_TYPE_STREAM_END
:
464 int64_t a_sc_id
, b_sc_id
;
465 struct bt_ctf_stream
*a_stream
, *b_stream
;
466 struct bt_ctf_stream_class
*a_sc
, *b_sc
;
468 a_stream
= internal_bt_notification_get_stream(a
);
469 b_stream
= internal_bt_notification_get_stream(b
);
470 assert(a_stream
&& b_stream
);
472 a_sc
= bt_ctf_stream_get_class(a_stream
);
473 b_sc
= bt_ctf_stream_get_class(b_stream
);
474 assert(a_sc
&& b_sc
);
476 a_sc_id
= bt_ctf_stream_class_get_id(a_sc
);
477 b_sc_id
= bt_ctf_stream_class_get_id(b_sc
);
478 assert(a_sc_id
>= 0 && b_sc_id
>= 0);
483 return a_sc_id
< b_sc_id
;
485 case BT_NOTIFICATION_TYPE_NEW_TRACE
:
486 case BT_NOTIFICATION_TYPE_END_OF_TRACE
:
487 /* Impossible to have two separate traces. */
497 void stream_destroy(void *stream
)
499 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
503 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
504 struct ctf_fs_iterator
*ctf_it
)
508 GError
*error
= NULL
;
509 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
512 PERR("Cannot open directory \"%s\": %s (code %d)\n",
513 ctf_fs
->trace_path
->str
, error
->message
,
518 while ((name
= g_dir_read_name(dir
))) {
519 struct ctf_fs_file
*file
= NULL
;
520 struct ctf_fs_stream
*stream
= NULL
;
522 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
523 /* Ignore the metadata stream. */
524 PDBG("Ignoring metadata file \"%s\"\n",
529 if (name
[0] == '.') {
530 PDBG("Ignoring hidden file \"%s\"\n",
535 /* Create the file. */
536 file
= ctf_fs_file_create(ctf_fs
);
538 PERR("Cannot create stream file object\n");
542 /* Create full path string. */
543 g_string_append_printf(file
->path
, "%s/%s",
544 ctf_fs
->trace_path
->str
, name
);
545 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
546 PDBG("Ignoring non-regular file \"%s\"\n", name
);
547 ctf_fs_file_destroy(file
);
552 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
553 ctf_fs_file_destroy(file
);
557 if (file
->size
== 0) {
558 /* Skip empty stream. */
559 ctf_fs_file_destroy(file
);
563 /* Create a private stream; file ownership is passed to it. */
564 stream
= ctf_fs_stream_create(ctf_fs
, file
);
566 ctf_fs_file_destroy(file
);
570 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
588 enum bt_component_status
ctf_fs_iterator_init(struct bt_component
*source
,
589 struct bt_notification_iterator
*it
)
591 struct ctf_fs_iterator
*ctf_it
;
592 struct ctf_fs_component
*ctf_fs
;
593 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
595 assert(source
&& it
);
597 ctf_fs
= bt_component_get_private_data(source
);
599 ret
= BT_COMPONENT_STATUS_INVALID
;
603 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
605 ret
= BT_COMPONENT_STATUS_NOMEM
;
609 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
610 g_direct_equal
, bt_put
, stream_destroy
);
611 if (!ctf_it
->stream_ht
) {
614 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
616 if (!ctf_it
->pending_streams
) {
619 ctf_it
->pending_notifications
= bt_notification_heap_create(
620 compare_notifications
, NULL
);
621 if (!ctf_it
->pending_notifications
) {
625 ret
= open_trace_streams(ctf_fs
, ctf_it
);
630 ret
= bt_notification_iterator_set_get_cb(it
, ctf_fs_iterator_get
);
635 ret
= bt_notification_iterator_set_next_cb(it
, ctf_fs_iterator_next
);
640 ret
= bt_notification_iterator_set_destroy_cb(it
,
641 ctf_fs_iterator_destroy
);
646 ret
= bt_notification_iterator_set_private_data(it
, ctf_it
);
654 (void) bt_notification_iterator_set_private_data(it
, NULL
);
655 ctf_fs_iterator_destroy_data(ctf_it
);
660 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
662 if (ctf_fs
->trace_path
) {
663 g_string_free(ctf_fs
->trace_path
, TRUE
);
665 if (ctf_fs
->metadata
) {
666 ctf_fs_metadata_fini(ctf_fs
->metadata
);
667 g_free(ctf_fs
->metadata
);
673 void ctf_fs_destroy(struct bt_component
*component
)
675 void *data
= bt_component_get_private_data(component
);
677 ctf_fs_destroy_data(data
);
681 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
683 struct ctf_fs_component
*ctf_fs
;
684 struct bt_value
*value
= NULL
;
686 enum bt_value_status ret
;
688 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
693 /* FIXME: should probably look for a source URI */
694 value
= bt_value_map_get(params
, "path");
695 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
699 ret
= bt_value_string_get(value
, &path
);
700 if (ret
!= BT_VALUE_STATUS_OK
) {
704 ctf_fs
->trace_path
= g_string_new(path
);
705 if (!ctf_fs
->trace_path
) {
708 ctf_fs
->error_fp
= stderr
;
709 ctf_fs
->page_size
= (size_t) getpagesize();
711 // FIXME: check error.
712 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
713 if (!ctf_fs
->metadata
) {
716 ctf_fs_metadata_set_trace(ctf_fs
);
720 ctf_fs_destroy_data(ctf_fs
);
728 enum bt_component_status
ctf_fs_init(struct bt_component
*source
,
729 struct bt_value
*params
)
731 struct ctf_fs_component
*ctf_fs
;
732 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
735 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
736 ctf_fs
= ctf_fs_create(params
);
738 ret
= BT_COMPONENT_STATUS_NOMEM
;
742 ret
= bt_component_set_destroy_cb(source
, ctf_fs_destroy
);
743 if (ret
!= BT_COMPONENT_STATUS_OK
) {
747 ret
= bt_component_set_private_data(source
, ctf_fs
);
748 if (ret
!= BT_COMPONENT_STATUS_OK
) {
752 ret
= bt_component_source_set_iterator_init_cb(source
,
753 ctf_fs_iterator_init
);
754 if (ret
!= BT_COMPONENT_STATUS_OK
) {
760 (void) bt_component_set_private_data(source
, NULL
);
761 ctf_fs_destroy_data(ctf_fs
);