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/notification/iterator.h>
32 #include <babeltrace/component/notification/stream.h>
33 #include <babeltrace/component/notification/event.h>
34 #include <babeltrace/component/notification/packet.h>
35 #include <babeltrace/component/notification/heap.h>
41 #include "data-stream.h"
44 #define PRINT_ERR_STREAM ctf_fs->error_fp
45 #define PRINT_PREFIX "ctf-fs"
52 enum bt_notification_iterator_status
ctf_fs_iterator_next(
53 struct bt_notification_iterator
*iterator
);
56 struct bt_notification
*ctf_fs_iterator_get(
57 struct bt_notification_iterator
*iterator
)
59 struct ctf_fs_iterator
*ctf_it
=
60 bt_notification_iterator_get_private_data(iterator
);
62 if (!ctf_it
->current_notification
) {
63 (void) ctf_fs_iterator_next(iterator
);
66 return bt_get(ctf_it
->current_notification
);
70 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
71 struct ctf_fs_iterator
*it
,
72 struct ctf_fs_stream
*stream
,
73 struct bt_notification
**notification
)
75 enum bt_ctf_notif_iter_status status
;
76 enum bt_notification_iterator_status ret
;
78 if (stream
->end_reached
) {
79 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
83 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
85 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
86 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
90 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
91 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
92 *notification
= bt_notification_stream_end_create(
95 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
97 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
98 stream
->end_reached
= true;
102 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
103 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
105 case BT_CTF_NOTIF_ITER_STATUS_OK
:
106 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
108 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
110 * Should not make it this far as this is medium-specific;
111 * there is nothing for the user to do and it should have been
115 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
116 /* No argument provided by the user, so don't return INVAL. */
117 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
119 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
126 * Remove me. This is a temporary work-around due to our inhability to use
127 * libbabeltrace-ctf from libbabeltrace-plugin.
130 struct bt_ctf_stream
*internal_bt_notification_get_stream(
131 struct bt_notification
*notification
)
133 struct bt_ctf_stream
*stream
= NULL
;
135 assert(notification
);
136 switch (bt_notification_get_type(notification
)) {
137 case BT_NOTIFICATION_TYPE_EVENT
:
139 struct bt_ctf_event
*event
;
141 event
= bt_notification_event_get_event(notification
);
142 stream
= bt_ctf_event_get_stream(event
);
146 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
148 struct bt_ctf_packet
*packet
;
150 packet
= bt_notification_packet_begin_get_packet(notification
);
151 stream
= bt_ctf_packet_get_stream(packet
);
155 case BT_NOTIFICATION_TYPE_PACKET_END
:
157 struct bt_ctf_packet
*packet
;
159 packet
= bt_notification_packet_end_get_packet(notification
);
160 stream
= bt_ctf_packet_get_stream(packet
);
164 case BT_NOTIFICATION_TYPE_STREAM_END
:
165 stream
= bt_notification_stream_end_get_stream(notification
);
175 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
177 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
178 enum bt_notification_iterator_status ret
=
179 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
181 /* Insert one stream-associated notification for each stream. */
182 for (i
= 0; i
< pending_streams_count
; i
++) {
183 struct bt_notification
*notification
;
184 struct ctf_fs_stream
*fs_stream
;
185 struct bt_ctf_stream
*stream
;
186 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
188 fs_stream
= g_ptr_array_index(it
->pending_streams
,
189 pending_stream_index
);
194 ret
= ctf_fs_iterator_get_next_notification(
195 it
, fs_stream
, ¬ification
);
196 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
197 printf_debug("Failed to populate heap at stream %zu\n",
198 pending_stream_index
);
202 stream
= internal_bt_notification_get_stream(
208 * Associate pending ctf_fs_stream to
209 * bt_ctf_stream. Ownership of stream
210 * is passed to the stream ht.
212 inserted
= g_hash_table_insert(it
->stream_ht
,
215 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
216 printf_debug("Failed to associate fs stream to ctf stream\n");
221 heap_ret
= bt_notification_heap_insert(
222 it
->pending_notifications
,
224 bt_put(notification
);
226 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
227 printf_debug("Failed to insert notification in heap\n");
230 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
232 * Set NULL so the destruction callback registered with the
233 * array is not invoked on the stream (its ownership was
234 * transferred to the streams hashtable).
236 g_ptr_array_index(it
->pending_streams
,
237 pending_stream_index
) = NULL
;
238 g_ptr_array_remove_index(it
->pending_streams
,
239 pending_stream_index
);
242 g_ptr_array_free(it
->pending_streams
, TRUE
);
243 it
->pending_streams
= NULL
;
249 enum bt_notification_iterator_status
ctf_fs_iterator_next(
250 struct bt_notification_iterator
*iterator
)
253 struct bt_ctf_stream
*stream
= NULL
;
254 struct ctf_fs_stream
*fs_stream
;
255 struct bt_notification
*notification
;
256 struct bt_notification
*next_stream_notification
;
257 enum bt_notification_iterator_status ret
=
258 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
259 struct ctf_fs_iterator
*ctf_it
=
260 bt_notification_iterator_get_private_data(iterator
);
262 notification
= bt_notification_heap_pop(ctf_it
->pending_notifications
);
263 if (!notification
&& !ctf_it
->pending_streams
) {
264 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
268 if (!notification
&& ctf_it
->pending_streams
) {
270 * Insert at one notification per stream in the heap and pop
273 ret
= populate_heap(ctf_it
);
278 notification
= bt_notification_heap_pop(
279 ctf_it
->pending_notifications
);
281 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
286 /* notification is set from here. */
288 stream
= internal_bt_notification_get_stream(notification
);
291 * The current notification is not associated to a particular
292 * stream, there is no need to insert a new notification from
293 * a stream in the heap.
298 fs_stream
= g_hash_table_lookup(ctf_it
->stream_ht
, stream
);
300 /* We have reached this stream's end. */
304 ret
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
305 &next_stream_notification
);
306 if ((ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
307 heap_ret
= bt_notification_heap_insert(
308 ctf_it
->pending_notifications
, notification
);
310 assert(!next_stream_notification
);
313 * We're dropping the most recent notification, but at
314 * this point, something is seriously wrong...
316 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
318 BT_PUT(notification
);
322 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
326 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
328 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
330 heap_ret
= bt_notification_heap_insert(ctf_it
->pending_notifications
,
331 next_stream_notification
);
332 BT_PUT(next_stream_notification
);
335 * We're dropping the most recent notification...
337 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
342 * Ensure that the stream is removed from both pending_streams and
343 * the streams hashtable on reception of the "end of stream"
347 BT_MOVE(ctf_it
->current_notification
, notification
);
353 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
355 bt_put(ctf_it
->current_notification
);
356 bt_put(ctf_it
->pending_notifications
);
357 if (ctf_it
->pending_streams
) {
358 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
360 if (ctf_it
->stream_ht
) {
361 g_hash_table_destroy(ctf_it
->stream_ht
);
367 void ctf_fs_iterator_destroy(struct bt_notification_iterator
*it
)
369 void *data
= bt_notification_iterator_get_private_data(it
);
371 ctf_fs_iterator_destroy_data(data
);
375 bool compare_event_notifications(struct bt_notification
*a
,
376 struct bt_notification
*b
)
379 struct bt_ctf_clock_class
*clock_class
;
380 struct bt_ctf_clock_value
*a_clock_value
, *b_clock_value
;
381 struct bt_ctf_stream_class
*a_stream_class
;
382 struct bt_ctf_stream
*a_stream
;
383 struct bt_ctf_event
*a_event
, *b_event
;
384 struct bt_ctf_trace
*trace
;
387 // FIXME - assumes only one clock
388 a_event
= bt_notification_event_get_event(a
);
389 b_event
= bt_notification_event_get_event(b
);
393 a_stream
= bt_ctf_event_get_stream(a_event
);
395 a_stream_class
= bt_ctf_stream_get_class(a_stream
);
396 assert(a_stream_class
);
397 trace
= bt_ctf_stream_class_get_trace(a_stream_class
);
400 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
401 a_clock_value
= bt_ctf_event_get_clock_value(a_event
, clock_class
);
402 b_clock_value
= bt_ctf_event_get_clock_value(b_event
, clock_class
);
403 assert(a_clock_value
);
404 assert(b_clock_value
);
406 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value
, &a_ts
);
408 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value
, &b_ts
);
413 bt_put(a_clock_value
);
414 bt_put(b_clock_value
);
416 bt_put(a_stream_class
);
423 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
426 static int notification_priorities
[] = {
427 [BT_NOTIFICATION_TYPE_NEW_TRACE
] = 0,
428 [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS
] = 1,
429 [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS
] = 2,
430 [BT_NOTIFICATION_TYPE_PACKET_BEGIN
] = 3,
431 [BT_NOTIFICATION_TYPE_PACKET_END
] = 4,
432 [BT_NOTIFICATION_TYPE_EVENT
] = 5,
433 [BT_NOTIFICATION_TYPE_END_OF_TRACE
] = 6,
436 enum bt_notification_type a_type
, b_type
;
439 a_type
= bt_notification_get_type(a
);
440 b_type
= bt_notification_get_type(b
);
441 assert(a_type
> BT_NOTIFICATION_TYPE_ALL
);
442 assert(a_type
< BT_NOTIFICATION_TYPE_NR
);
443 assert(b_type
> BT_NOTIFICATION_TYPE_ALL
);
444 assert(b_type
< BT_NOTIFICATION_TYPE_NR
);
446 a_prio
= notification_priorities
[a_type
];
447 b_prio
= notification_priorities
[b_type
];
449 if (likely((a_type
== b_type
) && a_type
== BT_NOTIFICATION_TYPE_EVENT
)) {
450 return compare_event_notifications(a
, b
);
453 if (unlikely(a_prio
!= b_prio
)) {
454 return a_prio
< b_prio
;
457 /* Notification types are equal, but not of type "event". */
459 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
460 case BT_NOTIFICATION_TYPE_PACKET_END
:
461 case BT_NOTIFICATION_TYPE_STREAM_END
:
463 int64_t a_sc_id
, b_sc_id
;
464 struct bt_ctf_stream
*a_stream
, *b_stream
;
465 struct bt_ctf_stream_class
*a_sc
, *b_sc
;
467 a_stream
= internal_bt_notification_get_stream(a
);
468 b_stream
= internal_bt_notification_get_stream(b
);
469 assert(a_stream
&& b_stream
);
471 a_sc
= bt_ctf_stream_get_class(a_stream
);
472 b_sc
= bt_ctf_stream_get_class(b_stream
);
473 assert(a_sc
&& b_sc
);
475 a_sc_id
= bt_ctf_stream_class_get_id(a_sc
);
476 b_sc_id
= bt_ctf_stream_class_get_id(b_sc
);
477 assert(a_sc_id
>= 0 && b_sc_id
>= 0);
482 return a_sc_id
< b_sc_id
;
484 case BT_NOTIFICATION_TYPE_NEW_TRACE
:
485 case BT_NOTIFICATION_TYPE_END_OF_TRACE
:
486 /* Impossible to have two separate traces. */
496 void stream_destroy(void *stream
)
498 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
502 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
503 struct ctf_fs_iterator
*ctf_it
)
507 GError
*error
= NULL
;
508 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
511 PERR("Cannot open directory \"%s\": %s (code %d)\n",
512 ctf_fs
->trace_path
->str
, error
->message
,
517 while ((name
= g_dir_read_name(dir
))) {
518 struct ctf_fs_file
*file
= NULL
;
519 struct ctf_fs_stream
*stream
= NULL
;
521 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
522 /* Ignore the metadata stream. */
523 PDBG("Ignoring metadata file \"%s\"\n",
528 if (name
[0] == '.') {
529 PDBG("Ignoring hidden file \"%s\"\n",
534 /* Create the file. */
535 file
= ctf_fs_file_create(ctf_fs
);
537 PERR("Cannot create stream file object\n");
541 /* Create full path string. */
542 g_string_append_printf(file
->path
, "%s/%s",
543 ctf_fs
->trace_path
->str
, name
);
544 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
545 PDBG("Ignoring non-regular file \"%s\"\n", name
);
546 ctf_fs_file_destroy(file
);
551 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
552 ctf_fs_file_destroy(file
);
556 if (file
->size
== 0) {
557 /* Skip empty stream. */
558 ctf_fs_file_destroy(file
);
562 /* Create a private stream; file ownership is passed to it. */
563 stream
= ctf_fs_stream_create(ctf_fs
, file
);
565 ctf_fs_file_destroy(file
);
569 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
586 enum bt_component_status
ctf_fs_iterator_init(struct bt_component
*source
,
587 struct bt_notification_iterator
*it
)
589 struct ctf_fs_iterator
*ctf_it
;
590 struct ctf_fs_component
*ctf_fs
;
591 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
593 assert(source
&& it
);
595 ctf_fs
= bt_component_get_private_data(source
);
597 ret
= BT_COMPONENT_STATUS_INVALID
;
601 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
603 ret
= BT_COMPONENT_STATUS_NOMEM
;
607 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
608 g_direct_equal
, bt_put
, stream_destroy
);
609 if (!ctf_it
->stream_ht
) {
612 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
614 if (!ctf_it
->pending_streams
) {
617 ctf_it
->pending_notifications
= bt_notification_heap_create(
618 compare_notifications
, NULL
);
619 if (!ctf_it
->pending_notifications
) {
623 ret
= open_trace_streams(ctf_fs
, ctf_it
);
628 ret
= bt_notification_iterator_set_get_cb(it
, ctf_fs_iterator_get
);
633 ret
= bt_notification_iterator_set_next_cb(it
, ctf_fs_iterator_next
);
638 ret
= bt_notification_iterator_set_destroy_cb(it
,
639 ctf_fs_iterator_destroy
);
644 ret
= bt_notification_iterator_set_private_data(it
, ctf_it
);
652 (void) bt_notification_iterator_set_private_data(it
, NULL
);
653 ctf_fs_iterator_destroy_data(ctf_it
);
658 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
660 if (ctf_fs
->trace_path
) {
661 g_string_free(ctf_fs
->trace_path
, TRUE
);
663 if (ctf_fs
->metadata
) {
664 ctf_fs_metadata_fini(ctf_fs
->metadata
);
665 g_free(ctf_fs
->metadata
);
670 void ctf_fs_destroy(struct bt_component
*component
)
672 void *data
= bt_component_get_private_data(component
);
674 ctf_fs_destroy_data(data
);
678 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
680 struct ctf_fs_component
*ctf_fs
;
681 struct bt_value
*value
= NULL
;
683 enum bt_value_status ret
;
685 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
690 /* FIXME: should probably look for a source URI */
691 value
= bt_value_map_get(params
, "path");
692 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
696 ret
= bt_value_string_get(value
, &path
);
697 if (ret
!= BT_VALUE_STATUS_OK
) {
701 ctf_fs
->trace_path
= g_string_new(path
);
702 if (!ctf_fs
->trace_path
) {
705 ctf_fs
->error_fp
= stderr
;
706 ctf_fs
->page_size
= (size_t) getpagesize();
708 // FIXME: check error.
709 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
710 if (!ctf_fs
->metadata
) {
713 ctf_fs_metadata_set_trace(ctf_fs
);
717 ctf_fs_destroy_data(ctf_fs
);
725 enum bt_component_status
ctf_fs_init(struct bt_component
*source
,
726 struct bt_value
*params
, void *init_method_data
)
728 struct ctf_fs_component
*ctf_fs
;
729 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
730 (void) init_method_data
;
733 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
734 ctf_fs
= ctf_fs_create(params
);
736 ret
= BT_COMPONENT_STATUS_NOMEM
;
740 ret
= bt_component_set_private_data(source
, ctf_fs
);
741 if (ret
!= BT_COMPONENT_STATUS_OK
) {
747 (void) bt_component_set_private_data(source
, NULL
);
748 ctf_fs_destroy_data(ctf_fs
);