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/plugin/notification/iterator.h>
32 #include <babeltrace/plugin/notification/stream.h>
33 #include <babeltrace/plugin/notification/event.h>
34 #include <babeltrace/plugin/notification/packet.h>
35 #include <babeltrace/plugin/notification/heap.h>
41 #include "data-stream.h"
44 #define PRINT_ERR_STREAM ctf_fs->error_fp
45 #define PRINT_PREFIX "ctf-fs"
48 static bool ctf_fs_debug
;
51 struct bt_notification
*ctf_fs_iterator_get(
52 struct bt_notification_iterator
*iterator
)
54 struct ctf_fs_iterator
*ctf_it
=
55 bt_notification_iterator_get_private_data(iterator
);
57 return bt_get(ctf_it
->current_notification
);
61 enum bt_notification_iterator_status
ctf_fs_iterator_get_next_notification(
62 struct ctf_fs_iterator
*it
,
63 struct ctf_fs_stream
*stream
,
64 struct bt_notification
**notification
)
66 enum bt_ctf_notif_iter_status status
;
67 enum bt_notification_iterator_status ret
;
69 if (stream
->end_reached
) {
70 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
74 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
76 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
77 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
81 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
82 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
83 *notification
= bt_notification_stream_end_create(
86 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
88 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
89 stream
->end_reached
= true;
93 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
94 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
96 case BT_CTF_NOTIF_ITER_STATUS_OK
:
97 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
99 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
101 * Should not make it this far as this is medium-specific;
102 * there is nothing for the user to do and it should have been
106 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
107 /* No argument provided by the user, so don't return INVAL. */
108 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
110 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
117 * Remove me. This is a temporary work-around due to our inhability to use
118 * libbabeltrace-ctf from libbabeltrace-plugin.
121 struct bt_ctf_stream
*internal_bt_notification_get_stream(
122 struct bt_notification
*notification
)
124 struct bt_ctf_stream
*stream
= NULL
;
126 assert(notification
);
127 switch (bt_notification_get_type(notification
)) {
128 case BT_NOTIFICATION_TYPE_EVENT
:
130 struct bt_ctf_event
*event
;
132 event
= bt_notification_event_get_event(notification
);
133 stream
= bt_ctf_event_get_stream(event
);
137 case BT_NOTIFICATION_TYPE_PACKET_START
:
139 struct bt_ctf_packet
*packet
;
141 packet
= bt_notification_packet_start_get_packet(notification
);
142 stream
= bt_ctf_packet_get_stream(packet
);
146 case BT_NOTIFICATION_TYPE_PACKET_END
:
148 struct bt_ctf_packet
*packet
;
150 packet
= bt_notification_packet_end_get_packet(notification
);
151 stream
= bt_ctf_packet_get_stream(packet
);
155 case BT_NOTIFICATION_TYPE_STREAM_END
:
156 stream
= bt_notification_stream_end_get_stream(notification
);
166 enum bt_notification_iterator_status
populate_heap(struct ctf_fs_iterator
*it
)
168 size_t i
, pending_streams_count
= it
->pending_streams
->len
;
169 enum bt_notification_iterator_status ret
=
170 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
172 /* Insert one stream-associated notification for each stream. */
173 for (i
= 0; i
< pending_streams_count
; i
++) {
174 struct bt_notification
*notification
;
175 struct ctf_fs_stream
*fs_stream
;
176 struct bt_ctf_stream
*stream
;
177 size_t pending_stream_index
= pending_streams_count
- 1 - i
;
179 fs_stream
= g_ptr_array_index(it
->pending_streams
,
180 pending_stream_index
);
185 ret
= ctf_fs_iterator_get_next_notification(
186 it
, fs_stream
, ¬ification
);
187 if (ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
188 printf_debug("Failed to populate heap at stream %zu\n",
189 pending_stream_index
);
193 stream
= internal_bt_notification_get_stream(
199 * Associate pending ctf_fs_stream to
200 * bt_ctf_stream. Ownership of stream
201 * is passed to the stream ht.
203 inserted
= g_hash_table_insert(it
->stream_ht
,
206 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
207 printf_debug("Failed to associate fs stream to ctf stream\n");
212 heap_ret
= bt_notification_heap_insert(
213 it
->pending_notifications
,
215 bt_put(notification
);
217 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
218 printf_debug("Failed to insert notification in heap\n");
221 } while (!stream
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
);
223 * Set NULL so the destruction callback registered with the
224 * array is not invoked on the stream (its ownership was
225 * transferred to the streams hashtable).
227 g_ptr_array_index(it
->pending_streams
,
228 pending_stream_index
) = NULL
;
229 g_ptr_array_remove_index(it
->pending_streams
,
230 pending_stream_index
);
233 g_ptr_array_free(it
->pending_streams
, TRUE
);
234 it
->pending_streams
= NULL
;
240 enum bt_notification_iterator_status
ctf_fs_iterator_next(
241 struct bt_notification_iterator
*iterator
)
244 struct bt_ctf_stream
*stream
;
245 struct ctf_fs_stream
*fs_stream
;
246 struct bt_notification
*notification
;
247 struct bt_notification
*next_stream_notification
;
248 enum bt_notification_iterator_status ret
=
249 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
250 struct ctf_fs_iterator
*ctf_it
=
251 bt_notification_iterator_get_private_data(iterator
);
253 notification
= bt_notification_heap_pop(ctf_it
->pending_notifications
);
254 if (!notification
&& !ctf_it
->pending_streams
) {
255 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
259 if (!notification
&& ctf_it
->pending_streams
) {
261 * Insert at one notification per stream in the heap and pop
264 ret
= populate_heap(ctf_it
);
269 notification
= bt_notification_heap_pop(
270 ctf_it
->pending_notifications
);
272 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
277 /* notification is set from here. */
279 stream
= internal_bt_notification_get_stream(notification
);
282 * The current notification is not associated to a particular
283 * stream, there is no need to insert a new notification from
284 * a stream in the heap.
289 fs_stream
= g_hash_table_lookup(ctf_it
->stream_ht
, stream
);
291 /* We have reached this stream's end. */
295 ret
= ctf_fs_iterator_get_next_notification(ctf_it
, fs_stream
,
296 &next_stream_notification
);
297 if ((ret
&& ret
!= BT_NOTIFICATION_ITERATOR_STATUS_END
)) {
298 heap_ret
= bt_notification_heap_insert(
299 ctf_it
->pending_notifications
, notification
);
301 assert(!next_stream_notification
);
304 * We're dropping the most recent notification, but at
305 * this point, something is seriously wrong...
307 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
309 BT_PUT(notification
);
313 if (ret
== BT_NOTIFICATION_ITERATOR_STATUS_END
) {
317 success
= g_hash_table_remove(ctf_it
->stream_ht
, stream
);
319 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
321 heap_ret
= bt_notification_heap_insert(ctf_it
->pending_notifications
,
322 next_stream_notification
);
323 BT_PUT(next_stream_notification
);
326 * We're dropping the most recent notification...
328 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
333 * Ensure that the stream is removed from both pending_streams and
334 * the streams hashtable on reception of the "end of stream"
338 BT_MOVE(ctf_it
->current_notification
, notification
);
343 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
345 bt_put(ctf_it
->current_notification
);
346 bt_put(ctf_it
->pending_notifications
);
347 if (ctf_it
->pending_streams
) {
348 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
350 if (ctf_it
->stream_ht
) {
351 g_hash_table_destroy(ctf_it
->stream_ht
);
357 void ctf_fs_iterator_destroy(struct bt_notification_iterator
*it
)
359 void *data
= bt_notification_iterator_get_private_data(it
);
361 ctf_fs_iterator_destroy_data(data
);
365 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
372 void stream_destroy(void *stream
)
374 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
378 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
379 struct ctf_fs_iterator
*ctf_it
)
383 GError
*error
= NULL
;
384 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
387 PERR("Cannot open directory \"%s\": %s (code %d)\n",
388 ctf_fs
->trace_path
->str
, error
->message
,
393 while ((name
= g_dir_read_name(dir
))) {
394 struct ctf_fs_file
*file
= NULL
;
395 struct ctf_fs_stream
*stream
= NULL
;
397 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
398 /* Ignore the metadata stream. */
399 PDBG("Ignoring metadata file \"%s\"\n",
404 if (name
[0] == '.') {
405 PDBG("Ignoring hidden file \"%s\"\n",
410 /* Create the file. */
411 file
= ctf_fs_file_create(ctf_fs
);
413 PERR("Cannot create stream file object\n");
417 /* Create full path string. */
418 g_string_append_printf(file
->path
, "%s/%s",
419 ctf_fs
->trace_path
->str
, name
);
420 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
421 PDBG("Ignoring non-regular file \"%s\"\n", name
);
422 ctf_fs_file_destroy(file
);
427 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
428 ctf_fs_file_destroy(file
);
432 /* Create a private stream; file ownership is passed to it. */
433 stream
= ctf_fs_stream_create(ctf_fs
, file
);
435 ctf_fs_file_destroy(file
);
439 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
457 enum bt_component_status
ctf_fs_iterator_init(struct bt_component
*source
,
458 struct bt_notification_iterator
*it
)
460 struct ctf_fs_iterator
*ctf_it
;
461 struct ctf_fs_component
*ctf_fs
;
462 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
464 assert(source
&& it
);
466 ctf_fs
= bt_component_get_private_data(source
);
468 ret
= BT_COMPONENT_STATUS_INVALID
;
472 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
474 ret
= BT_COMPONENT_STATUS_NOMEM
;
478 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
479 g_direct_equal
, bt_put
, stream_destroy
);
480 if (!ctf_it
->stream_ht
) {
483 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
485 if (!ctf_it
->pending_streams
) {
488 ctf_it
->pending_notifications
= bt_notification_heap_create(
489 compare_notifications
, NULL
);
490 if (!ctf_it
->pending_notifications
) {
494 ret
= open_trace_streams(ctf_fs
, ctf_it
);
499 ret
= bt_notification_iterator_set_get_cb(it
, ctf_fs_iterator_get
);
504 ret
= bt_notification_iterator_set_next_cb(it
, ctf_fs_iterator_next
);
509 ret
= bt_notification_iterator_set_destroy_cb(it
,
510 ctf_fs_iterator_destroy
);
515 ret
= bt_notification_iterator_set_private_data(it
, ctf_it
);
522 (void) bt_notification_iterator_set_private_data(it
, NULL
);
523 ctf_fs_iterator_destroy_data(ctf_it
);
528 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
530 if (ctf_fs
->trace_path
) {
531 g_string_free(ctf_fs
->trace_path
, TRUE
);
533 if (ctf_fs
->metadata
) {
534 ctf_fs_metadata_fini(ctf_fs
->metadata
);
535 g_free(ctf_fs
->metadata
);
541 void ctf_fs_destroy(struct bt_component
*component
)
543 void *data
= bt_component_get_private_data(component
);
545 ctf_fs_destroy_data(data
);
549 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
551 struct ctf_fs_component
*ctf_fs
;
552 struct bt_value
*value
= NULL
;
554 enum bt_value_status ret
;
556 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
561 /* FIXME: should probably look for a source URI */
562 value
= bt_value_map_get(params
, "path");
563 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
567 ret
= bt_value_string_get(value
, &path
);
568 if (ret
!= BT_VALUE_STATUS_OK
) {
572 ctf_fs
->trace_path
= g_string_new(path
);
573 if (!ctf_fs
->trace_path
) {
576 ctf_fs
->error_fp
= stderr
;
577 ctf_fs
->page_size
= (size_t) getpagesize();
579 // FIXME: check error.
580 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
581 if (!ctf_fs
->metadata
) {
584 ctf_fs_metadata_set_trace(ctf_fs
);
588 ctf_fs_destroy_data(ctf_fs
);
596 enum bt_component_status
ctf_fs_init(struct bt_component
*source
,
597 struct bt_value
*params
)
599 struct ctf_fs_component
*ctf_fs
;
600 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
603 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
604 ctf_fs
= ctf_fs_create(params
);
606 ret
= BT_COMPONENT_STATUS_NOMEM
;
610 ret
= bt_component_set_destroy_cb(source
, ctf_fs_destroy
);
611 if (ret
!= BT_COMPONENT_STATUS_OK
) {
615 ret
= bt_component_set_private_data(source
, ctf_fs
);
616 if (ret
!= BT_COMPONENT_STATUS_OK
) {
620 ret
= bt_component_source_set_iterator_init_cb(source
,
621 ctf_fs_iterator_init
);
622 if (ret
!= BT_COMPONENT_STATUS_OK
) {
628 (void) bt_component_set_private_data(source
, NULL
);
629 ctf_fs_destroy_data(ctf_fs
);