ctf: de-duplicate index entries
[babeltrace.git] / src / plugins / ctf / fs-src / fs.c
index 28b728aefb80079b79fe42f4eaa8bb0b474b2545..64fff672705f68ee4e2304c5739ab392a8721745 100644 (file)
@@ -173,19 +173,29 @@ bt_component_class_message_iterator_next_method_status ctf_fs_iterator_next(
                bt_message_array_const msgs, uint64_t capacity,
                uint64_t *count)
 {
-       bt_component_class_message_iterator_next_method_status status =
-               BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
+       bt_component_class_message_iterator_next_method_status status;
        struct ctf_fs_msg_iter_data *msg_iter_data =
                bt_self_message_iterator_get_data(iterator);
        uint64_t i = 0;
 
-       while (i < capacity &&
-                       status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
+       if (G_UNLIKELY(msg_iter_data->next_saved_error)) {
+               /*
+                * Last time we were called, we hit an error but had some
+                * messages to deliver, so we stashed the error here.  Return
+                * it now.
+                */
+               BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(msg_iter_data->next_saved_error);
+               status = msg_iter_data->next_saved_status;
+               goto end;
+       }
+
+       do {
                status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]);
                if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
                        i++;
                }
-       }
+       } while (i < capacity &&
+                       status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK);
 
        if (i > 0) {
                /*
@@ -199,10 +209,23 @@ bt_component_class_message_iterator_next_method_status ctf_fs_iterator_next(
                 * called, possibly without any accumulated
                 * message, in which case we'll return it.
                 */
+               if (status < 0) {
+                       /*
+                        * Save this error for the next _next call.  Assume that
+                        * this component always appends error causes when
+                        * returning an error status code, which will cause the
+                        * current thread error to be non-NULL.
+                        */
+                       msg_iter_data->next_saved_error = bt_current_thread_take_error();
+                       BT_ASSERT(msg_iter_data->next_saved_error);
+                       msg_iter_data->next_saved_status = status;
+               }
+
                *count = i;
                status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
        }
 
+end:
        return status;
 }
 
@@ -281,7 +304,7 @@ bt_component_class_message_iterator_initialize_method_status ctf_fs_iterator_ini
                port_data->ds_file_group->ctf_fs_trace->metadata->tc,
                bt_common_get_page_size(msg_iter_data->log_level) * 8,
                ctf_fs_ds_file_medops, NULL, msg_iter_data->log_level,
-               self_comp);
+               self_comp, self_msg_iter);
        if (!msg_iter_data->msg_iter) {
                BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot create a CTF message iterator.");
                ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
@@ -677,24 +700,69 @@ void ds_file_group_insert_ds_file_info_sorted(
        array_insert(ds_file_group->ds_file_infos, ds_file_info, i);
 }
 
+static
+bool ds_index_entries_equal(
+       const struct ctf_fs_ds_index_entry *left,
+       const struct ctf_fs_ds_index_entry *right)
+{
+       if (left->packet_size != right->packet_size) {
+               return false;
+       }
+
+       if (left->timestamp_begin != right->timestamp_begin) {
+               return false;
+       }
+
+       if (left->timestamp_end != right->timestamp_end) {
+               return false;
+       }
+
+       if (left->packet_seq_num != right->packet_seq_num) {
+               return false;
+       }
+
+       return true;
+}
+
+/*
+ * Insert `entry` into `index`, without duplication.
+ *
+ * The entry is inserted only if there isn't an identical entry already.
+ *
+ * In any case, the ownership of `entry` is transferred to this function.  So if
+ * the entry is not inserted, it is freed.
+ */
+
 static
 void ds_index_insert_ds_index_entry_sorted(
        struct ctf_fs_ds_index *index,
        struct ctf_fs_ds_index_entry *entry)
 {
        guint i;
+       struct ctf_fs_ds_index_entry *other_entry;
 
        /* Find the spot where to insert this index entry. */
        for (i = 0; i < index->entries->len; i++) {
-               struct ctf_fs_ds_index_entry *other_entry =
-                       g_ptr_array_index(index->entries, i);
+               other_entry = g_ptr_array_index(index->entries, i);
 
-               if (entry->timestamp_begin_ns < other_entry->timestamp_begin_ns) {
+               if (entry->timestamp_begin_ns <= other_entry->timestamp_begin_ns) {
                        break;
                }
        }
 
-       array_insert(index->entries, entry, i);
+       /*
+        * Insert the entry only if a duplicate doesn't already exist.
+        *
+        * There can be duplicate packets if reading multiple overlapping
+        * snapshots of the same trace.  We then want the index to contain
+        * a reference to only one copy of that packet.
+        */
+       if (i == index->entries->len ||
+                       !ds_index_entries_equal(entry, other_entry)) {
+               array_insert(index->entries, entry, i);
+       } else {
+               g_free(entry);
+       }
 }
 
 static
@@ -708,10 +776,9 @@ void merge_ctf_fs_ds_indexes(struct ctf_fs_ds_index *dest, struct ctf_fs_ds_inde
 
                /*
                * Ownership of the ctf_fs_ds_index_entry is transferred to
-               * dest.
+               * ds_index_insert_ds_index_entry_sorted.
                */
                g_ptr_array_index(src->entries, i) = NULL;
-
                ds_index_insert_ds_index_entry_sorted(dest, entry);
        }
 }
@@ -738,7 +805,7 @@ int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace,
 
        msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
                bt_common_get_page_size(log_level) * 8,
-               ctf_fs_ds_file_medops, NULL, log_level, self_comp);
+               ctf_fs_ds_file_medops, NULL, log_level, self_comp, NULL);
        if (!msg_iter) {
                BT_COMP_LOGE_STR("Cannot create a CTF message iterator.");
                goto error;
@@ -1442,7 +1509,7 @@ int decode_clock_snapshot_after_event(struct ctf_fs_trace *ctf_fs_trace,
 
        msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
                bt_common_get_page_size(log_level) * 8, ctf_fs_ds_file_medops,
-               NULL, log_level, self_comp);
+               NULL, log_level, self_comp, NULL);
        if (!msg_iter) {
                /* ctf_msg_iter_create() logs errors. */
                ret = -1;
This page took 0.025809 seconds and 4 git commands to generate.