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
= NULL
;
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
);
344 void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator
*ctf_it
)
346 bt_put(ctf_it
->current_notification
);
347 bt_put(ctf_it
->pending_notifications
);
348 if (ctf_it
->pending_streams
) {
349 g_ptr_array_free(ctf_it
->pending_streams
, TRUE
);
351 if (ctf_it
->stream_ht
) {
352 g_hash_table_destroy(ctf_it
->stream_ht
);
358 void ctf_fs_iterator_destroy(struct bt_notification_iterator
*it
)
360 void *data
= bt_notification_iterator_get_private_data(it
);
362 ctf_fs_iterator_destroy_data(data
);
366 bool compare_notifications(struct bt_notification
*a
, struct bt_notification
*b
,
373 void stream_destroy(void *stream
)
375 ctf_fs_stream_destroy((struct ctf_fs_stream
*) stream
);
379 int open_trace_streams(struct ctf_fs_component
*ctf_fs
,
380 struct ctf_fs_iterator
*ctf_it
)
384 GError
*error
= NULL
;
385 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
388 PERR("Cannot open directory \"%s\": %s (code %d)\n",
389 ctf_fs
->trace_path
->str
, error
->message
,
394 while ((name
= g_dir_read_name(dir
))) {
395 struct ctf_fs_file
*file
= NULL
;
396 struct ctf_fs_stream
*stream
= NULL
;
398 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
399 /* Ignore the metadata stream. */
400 PDBG("Ignoring metadata file \"%s\"\n",
405 if (name
[0] == '.') {
406 PDBG("Ignoring hidden file \"%s\"\n",
411 /* Create the file. */
412 file
= ctf_fs_file_create(ctf_fs
);
414 PERR("Cannot create stream file object\n");
418 /* Create full path string. */
419 g_string_append_printf(file
->path
, "%s/%s",
420 ctf_fs
->trace_path
->str
, name
);
421 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
422 PDBG("Ignoring non-regular file \"%s\"\n", name
);
423 ctf_fs_file_destroy(file
);
428 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
429 ctf_fs_file_destroy(file
);
433 /* Create a private stream; file ownership is passed to it. */
434 stream
= ctf_fs_stream_create(ctf_fs
, file
);
436 ctf_fs_file_destroy(file
);
440 g_ptr_array_add(ctf_it
->pending_streams
, stream
);
458 enum bt_component_status
ctf_fs_iterator_init(struct bt_component
*source
,
459 struct bt_notification_iterator
*it
)
461 struct ctf_fs_iterator
*ctf_it
;
462 struct ctf_fs_component
*ctf_fs
;
463 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
465 assert(source
&& it
);
467 ctf_fs
= bt_component_get_private_data(source
);
469 ret
= BT_COMPONENT_STATUS_INVALID
;
473 ctf_it
= g_new0(struct ctf_fs_iterator
, 1);
475 ret
= BT_COMPONENT_STATUS_NOMEM
;
479 ctf_it
->stream_ht
= g_hash_table_new_full(g_direct_hash
,
480 g_direct_equal
, bt_put
, stream_destroy
);
481 if (!ctf_it
->stream_ht
) {
484 ctf_it
->pending_streams
= g_ptr_array_new_with_free_func(
486 if (!ctf_it
->pending_streams
) {
489 ctf_it
->pending_notifications
= bt_notification_heap_create(
490 compare_notifications
, NULL
);
491 if (!ctf_it
->pending_notifications
) {
495 ret
= open_trace_streams(ctf_fs
, ctf_it
);
500 ret
= bt_notification_iterator_set_get_cb(it
, ctf_fs_iterator_get
);
505 ret
= bt_notification_iterator_set_next_cb(it
, ctf_fs_iterator_next
);
510 ret
= bt_notification_iterator_set_destroy_cb(it
,
511 ctf_fs_iterator_destroy
);
516 ret
= bt_notification_iterator_set_private_data(it
, ctf_it
);
523 (void) bt_notification_iterator_set_private_data(it
, NULL
);
524 ctf_fs_iterator_destroy_data(ctf_it
);
529 void ctf_fs_destroy_data(struct ctf_fs_component
*ctf_fs
)
531 if (ctf_fs
->trace_path
) {
532 g_string_free(ctf_fs
->trace_path
, TRUE
);
534 if (ctf_fs
->metadata
) {
535 ctf_fs_metadata_fini(ctf_fs
->metadata
);
536 g_free(ctf_fs
->metadata
);
542 void ctf_fs_destroy(struct bt_component
*component
)
544 void *data
= bt_component_get_private_data(component
);
546 ctf_fs_destroy_data(data
);
550 struct ctf_fs_component
*ctf_fs_create(struct bt_value
*params
)
552 struct ctf_fs_component
*ctf_fs
;
553 struct bt_value
*value
= NULL
;
555 enum bt_value_status ret
;
557 ctf_fs
= g_new0(struct ctf_fs_component
, 1);
562 /* FIXME: should probably look for a source URI */
563 value
= bt_value_map_get(params
, "path");
564 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
568 ret
= bt_value_string_get(value
, &path
);
569 if (ret
!= BT_VALUE_STATUS_OK
) {
573 ctf_fs
->trace_path
= g_string_new(path
);
574 if (!ctf_fs
->trace_path
) {
577 ctf_fs
->error_fp
= stderr
;
578 ctf_fs
->page_size
= (size_t) getpagesize();
580 // FIXME: check error.
581 ctf_fs
->metadata
= g_new0(struct ctf_fs_metadata
, 1);
582 if (!ctf_fs
->metadata
) {
585 ctf_fs_metadata_set_trace(ctf_fs
);
589 ctf_fs_destroy_data(ctf_fs
);
597 enum bt_component_status
ctf_fs_init(struct bt_component
*source
,
598 struct bt_value
*params
)
600 struct ctf_fs_component
*ctf_fs
;
601 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
604 ctf_fs_debug
= g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
605 ctf_fs
= ctf_fs_create(params
);
607 ret
= BT_COMPONENT_STATUS_NOMEM
;
611 ret
= bt_component_set_destroy_cb(source
, ctf_fs_destroy
);
612 if (ret
!= BT_COMPONENT_STATUS_OK
) {
616 ret
= bt_component_set_private_data(source
, ctf_fs
);
617 if (ret
!= BT_COMPONENT_STATUS_OK
) {
621 ret
= bt_component_source_set_iterator_init_cb(source
,
622 ctf_fs_iterator_init
);
623 if (ret
!= BT_COMPONENT_STATUS_OK
) {
629 (void) bt_component_set_private_data(source
, NULL
);
630 ctf_fs_destroy_data(ctf_fs
);