ir: add bt_ctf_clock_class object, modify bt_ctf_clock object
[babeltrace.git] / plugins / ctf / fs / fs.c
index 2a8cd621b338c55a55d940796995b8c0a38dcfd1..ea9bf562c4507a81c5ea4db43da4d762678b868a 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <babeltrace/plugin/plugin-system.h>
 #include <babeltrace/ctf-ir/packet.h>
+#include <babeltrace/ctf-ir/clock-class.h>
 #include <babeltrace/plugin/notification/iterator.h>
 #include <babeltrace/plugin/notification/stream.h>
 #include <babeltrace/plugin/notification/event.h>
 #define PRINT_PREFIX           "ctf-fs"
 #include "print.h"
 
-static bool ctf_fs_debug;
+BT_HIDDEN
+bool ctf_fs_debug;
+
+static
+enum bt_notification_iterator_status ctf_fs_iterator_next(
+               struct bt_notification_iterator *iterator);
 
 static
 struct bt_notification *ctf_fs_iterator_get(
@@ -54,6 +60,10 @@ struct bt_notification *ctf_fs_iterator_get(
        struct ctf_fs_iterator *ctf_it =
                        bt_notification_iterator_get_private_data(iterator);
 
+       if (!ctf_it->current_notification) {
+               (void) ctf_fs_iterator_next(iterator);
+       }
+
        return bt_get(ctf_it->current_notification);
 }
 
@@ -134,11 +144,11 @@ struct bt_ctf_stream *internal_bt_notification_get_stream(
                bt_put(event);
                break;
        }
-       case BT_NOTIFICATION_TYPE_PACKET_START:
+       case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
        {
                struct bt_ctf_packet *packet;
 
-               packet = bt_notification_packet_start_get_packet(notification);
+               packet = bt_notification_packet_begin_get_packet(notification);
                stream = bt_ctf_packet_get_stream(packet);
                bt_put(packet);
                break;
@@ -241,7 +251,7 @@ enum bt_notification_iterator_status ctf_fs_iterator_next(
                struct bt_notification_iterator *iterator)
 {
        int heap_ret;
-       struct bt_ctf_stream *stream;
+       struct bt_ctf_stream *stream = NULL;
        struct ctf_fs_stream *fs_stream;
        struct bt_notification *notification;
        struct bt_notification *next_stream_notification;
@@ -362,10 +372,124 @@ void ctf_fs_iterator_destroy(struct bt_notification_iterator *it)
        ctf_fs_iterator_destroy_data(data);
 }
 
+static
+bool compare_event_notifications(struct bt_notification *a,
+               struct bt_notification *b)
+{
+       int ret;
+       struct bt_ctf_clock_class *clock_class;
+       struct bt_ctf_clock_value *a_clock_value, *b_clock_value;
+       struct bt_ctf_stream_class *a_stream_class;
+       struct bt_ctf_stream *a_stream;
+       struct bt_ctf_event *a_event, *b_event;
+       struct bt_ctf_trace *trace;
+       int64_t a_ts, b_ts;
+
+       // FIXME - assumes only one clock
+       a_event = bt_notification_event_get_event(a);
+       b_event = bt_notification_event_get_event(b);
+       assert(a_event);
+       assert(b_event);
+
+       a_stream = bt_ctf_event_get_stream(a_event);
+       assert(a_stream);
+       a_stream_class = bt_ctf_stream_get_class(a_stream);
+       assert(a_stream_class);
+       trace = bt_ctf_stream_class_get_trace(a_stream_class);
+       assert(trace);
+
+       clock_class = bt_ctf_trace_get_clock_class(trace, 0);
+       a_clock_value = bt_ctf_event_get_clock_value(a_event, clock_class);
+       b_clock_value = bt_ctf_event_get_clock_value(b_event, clock_class);
+       assert(a_clock_value);
+       assert(b_clock_value);
+
+       ret = bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value, &a_ts);
+       assert(!ret);
+       ret = bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value, &b_ts);
+       assert(!ret);
+
+       bt_put(a_event);
+       bt_put(b_event);
+       bt_put(a_clock_value);
+       bt_put(b_clock_value);
+       bt_put(a_stream);
+       bt_put(a_stream_class);
+       bt_put(clock_class);
+       bt_put(trace);
+       return a_ts < b_ts;
+}
+
 static
 bool compare_notifications(struct bt_notification *a, struct bt_notification *b,
                void *unused)
 {
+       static int notification_priorities[] = {
+               [BT_NOTIFICATION_TYPE_NEW_TRACE] = 0,
+               [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS] = 1,
+               [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS] = 2,
+               [BT_NOTIFICATION_TYPE_PACKET_BEGIN] = 3,
+               [BT_NOTIFICATION_TYPE_PACKET_END] = 4,
+               [BT_NOTIFICATION_TYPE_EVENT] = 5,
+               [BT_NOTIFICATION_TYPE_END_OF_TRACE] = 6,
+       };
+       int a_prio, b_prio;
+       enum bt_notification_type a_type, b_type;
+
+       assert(a && b);
+       a_type = bt_notification_get_type(a);
+       b_type = bt_notification_get_type(b);
+       assert(a_type > BT_NOTIFICATION_TYPE_ALL);
+       assert(a_type < BT_NOTIFICATION_TYPE_NR);
+       assert(b_type > BT_NOTIFICATION_TYPE_ALL);
+       assert(b_type < BT_NOTIFICATION_TYPE_NR);
+
+       a_prio = notification_priorities[a_type];
+       b_prio = notification_priorities[b_type];
+
+       if (likely((a_type == b_type) && a_type == BT_NOTIFICATION_TYPE_EVENT)) {
+               return compare_event_notifications(a, b);
+       }
+
+       if (unlikely(a_prio != b_prio)) {
+               return a_prio < b_prio;
+       }
+
+       /* Notification types are equal, but not of type "event". */
+       switch (a_type) {
+       case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
+       case BT_NOTIFICATION_TYPE_PACKET_END:
+       case BT_NOTIFICATION_TYPE_STREAM_END:
+       {
+               int64_t a_sc_id, b_sc_id;
+               struct bt_ctf_stream *a_stream, *b_stream;
+               struct bt_ctf_stream_class *a_sc, *b_sc;
+
+               a_stream = internal_bt_notification_get_stream(a);
+               b_stream = internal_bt_notification_get_stream(b);
+               assert(a_stream && b_stream);
+
+               a_sc = bt_ctf_stream_get_class(a_stream);
+               b_sc = bt_ctf_stream_get_class(b_stream);
+               assert(a_sc && b_sc);
+
+               a_sc_id = bt_ctf_stream_class_get_id(a_sc);
+               b_sc_id = bt_ctf_stream_class_get_id(b_sc);
+               assert(a_sc_id >= 0 && b_sc_id >= 0);
+               bt_put(a_sc);
+               bt_put(a_stream);
+               bt_put(b_sc);
+               bt_put(b_stream);
+               return a_sc_id < b_sc_id;
+       }
+       case BT_NOTIFICATION_TYPE_NEW_TRACE:
+       case BT_NOTIFICATION_TYPE_END_OF_TRACE:
+               /* Impossible to have two separate traces. */
+       default:
+               assert(0);
+       }
+
+       assert(0);
        return a < b;
 }
 
@@ -430,6 +554,12 @@ int open_trace_streams(struct ctf_fs_component *ctf_fs,
                        goto error;
                }
 
+               if (file->size == 0) {
+                       /* Skip empty stream. */
+                       ctf_fs_file_destroy(file);
+                       continue;
+               }
+
                /* Create a private stream; file ownership is passed to it. */
                stream = ctf_fs_stream_create(ctf_fs, file);
                if (!stream) {
@@ -517,6 +647,7 @@ enum bt_component_status ctf_fs_iterator_init(struct bt_component *source,
        if (ret) {
                goto error;
        }
+
 end:
        return ret;
 error:
This page took 0.031606 seconds and 4 git commands to generate.