lib: split "message-clock-classes-are-compatible" post-condition in multiple post...
[babeltrace.git] / src / lib / graph / iterator.c
index dc1c778d919aab3657623b4244519a459e0d5939..39746df3dcac71fa3f9779b96a87f9fb363be792 100644 (file)
@@ -18,6 +18,7 @@
 #include <babeltrace2/trace-ir/packet.h>
 #include "lib/trace-ir/packet.h"
 #include "lib/trace-ir/stream.h"
+#include "lib/trace-ir/stream-class.h"
 #include <babeltrace2/trace-ir/clock-class.h>
 #include <babeltrace2/trace-ir/stream-class.h>
 #include <babeltrace2/trace-ir/stream.h>
 
 #include "component-class.h"
 #include "component.h"
-#include "component-sink.h"
-#include "component-source.h"
 #include "connection.h"
 #include "graph.h"
+#include "iterator.h"
 #include "message-iterator-class.h"
 #include "message/discarded-items.h"
 #include "message/event.h"
-#include "message/iterator.h"
 #include "message/message.h"
 #include "message/message-iterator-inactivity.h"
 #include "message/stream.h"
@@ -70,6 +69,9 @@
 struct per_stream_state
 {
        bt_packet *cur_packet;
+
+       /* Bit mask of expected message types. */
+       guint expected_msg_types;
 };
 #endif
 
@@ -117,6 +119,12 @@ void bt_message_iterator_destroy(struct bt_object *obj)
                "%!+i", iterator);
        bt_message_iterator_try_finalize(iterator);
 
+       if (iterator->clock_expectation.type ==
+                       CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID) {
+               BT_CLOCK_CLASS_PUT_REF_AND_RESET(
+                       iterator->clock_expectation.clock_class);
+       }
+
        if (iterator->connection) {
                /*
                 * Remove ourself from the originating connection so
@@ -659,18 +667,21 @@ end:
        return result;
 }
 
+#define NEXT_METHOD_NAME       "bt_message_iterator_class_next_method"
+
+#ifdef BT_DEV_MODE
+
 /*
  * When a new stream begins, verify that the clock class tied to this
  * stream is compatible with what we've seen before.
  */
 
-BT_ASSERT_COND_DEV_FUNC
 static
-bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator,
+void assert_post_dev_clock_classes_are_compatible_one(
+               struct bt_message_iterator *iterator,
                const struct bt_message *msg)
 {
        enum bt_message_type message_type = bt_message_get_type(msg);
-       bool result;
 
        if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
                const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
@@ -699,127 +710,83 @@ bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator,
                                bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
                        } else {
                                iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
+                               iterator->clock_expectation.clock_class = clock_class;
+                               bt_clock_class_get_ref(iterator->clock_expectation.clock_class);
                        }
                        break;
 
                case CLOCK_EXPECTATION_NONE:
-                       if (clock_class) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting no clock class, got one: %![cc-]+K",
-                                       clock_class);
-                               result = false;
-                               goto end;
-                       }
-
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "stream-class-has-no-clock-class", !clock_class,
+                               "Expecting no clock class, got one: %![cc-]+K",
+                               clock_class);
                        break;
 
                case CLOCK_EXPECTATION_ORIGIN_UNIX:
-                       if (!clock_class) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class, got none.");
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "stream-class-has-clock-class-with-unix-epoch-origin", clock_class,
+                               "Expecting a clock class with Unix epoch origin, got none.");
 
-                       if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class with Unix epoch origin: %![cc-]+K",
-                                       clock_class);
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "clock-class-has-unix-epoch-origin",
+                               bt_clock_class_origin_is_unix_epoch(clock_class),
+                               "Expecting a clock class with Unix epoch origin: %![cc-]+K",
+                               clock_class);
                        break;
 
                case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
-                       if (!clock_class) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class, got none.");
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "stream-class-has-clock-class-with-uuid", clock_class,
+                               "Expecting a clock class with UUID, got none.");
 
-                       if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class without Unix epoch origin: %![cc-]+K",
-                                       clock_class);
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "clock-class-has-non-unix-epoch-origin",
+                               !bt_clock_class_origin_is_unix_epoch(clock_class),
+                               "Expecting a clock class without Unix epoch origin: %![cc-]+K",
+                               clock_class);
 
-                       if (!clock_class_uuid) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class with UUID: %![cc-]+K",
-                                       clock_class);
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "clock-class-has-uuid",
+                               clock_class_uuid,
+                               "Expecting a clock class with UUID, got one without UUID: %![cc-]+K",
+                               clock_class);
 
-                       if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class with UUID, got one "
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "clock-class-has-expected-uuid",
+                               !bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid),
+                               "Expecting a clock class with UUID, got one "
                                        "with a different UUID: %![cc-]+K, expected-uuid=%!u",
                                        clock_class, iterator->clock_expectation.uuid);
-                               result = false;
-                               goto end;
-                       }
                        break;
 
                case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
-                       if (!clock_class) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class, got none.");
-                               result = false;
-                               goto end;
-                       }
-
-                       if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class without Unix epoch origin: %![cc-]+K",
-                                       clock_class);
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "stream-class-has-clock-class", clock_class,
+                               "Expecting a clock class, got none.");
 
-                       if (clock_class_uuid) {
-                               BT_ASSERT_COND_DEV_MSG(
-                                       "Expecting a clock class without UUID: %![cc-]+K",
-                                       clock_class);
-                               result = false;
-                               goto end;
-                       }
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "clock-class-is-expected",
+                               clock_class == iterator->clock_expectation.clock_class,
+                               "Expecting clock class %![cc-]+K, got %![cc-]+K.",
+                               iterator->clock_expectation.clock_class,
+                               clock_class);
                        break;
                }
        }
-
-       result = true;
-
-end:
-       return result;
 }
 
-BT_ASSERT_COND_DEV_FUNC
 static
-bool clock_classes_are_compatible(
+void assert_post_dev_clock_classes_are_compatible(
                struct bt_message_iterator *iterator,
                bt_message_array_const msgs, uint64_t msg_count)
 {
        uint64_t i;
-       bool result;
 
        for (i = 0; i < msg_count; i++) {
-               if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
-                       result = false;
-                       goto end;
-               }
+               assert_post_dev_clock_classes_are_compatible_one(iterator, msgs[i]);
        }
-
-       result = true;
-
-end:
-       return result;
 }
 
-#ifdef BT_DEV_MODE
 static
 const bt_stream *get_stream_from_msg(const struct bt_message *msg)
 {
@@ -867,6 +834,145 @@ const bt_stream *get_stream_from_msg(const struct bt_message *msg)
        return stream;
 }
 
+static
+GString *message_types_to_string(guint msg_types)
+{
+       GString *str = g_string_new("");
+
+       for (int msg_type = 1; msg_type <= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY;
+                       msg_type <<= 1) {
+               if (msg_type & msg_types) {
+                       if (str->len > 0) {
+                               g_string_append_c(str, '|');
+                       }
+
+                       g_string_append(str,
+                               bt_common_message_type_string(msg_type));
+               }
+       }
+
+       return str;
+}
+
+static
+void update_expected_msg_type(const struct bt_stream *stream,
+               struct per_stream_state *state,
+               const struct bt_message *msg)
+{
+       switch (msg->type) {
+        case BT_MESSAGE_TYPE_STREAM_BEGINNING:
+               state->expected_msg_types = BT_MESSAGE_TYPE_STREAM_END;
+
+               if (stream->class->supports_packets) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_PACKET_BEGINNING;
+
+                       if (stream->class->supports_discarded_packets) {
+                               state->expected_msg_types |=
+                                       BT_MESSAGE_TYPE_DISCARDED_PACKETS;
+                       }
+               } else {
+                       state->expected_msg_types |= BT_MESSAGE_TYPE_EVENT;
+               }
+
+               if (stream->class->supports_discarded_events) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_DISCARDED_EVENTS;
+               }
+
+               break;
+        case BT_MESSAGE_TYPE_STREAM_END:
+               state->expected_msg_types = 0;
+               break;
+        case BT_MESSAGE_TYPE_EVENT:
+       {
+               state->expected_msg_types = BT_MESSAGE_TYPE_EVENT;
+
+               if (stream->class->supports_packets) {
+                       state->expected_msg_types |= BT_MESSAGE_TYPE_PACKET_END;
+               } else {
+                       state->expected_msg_types |= BT_MESSAGE_TYPE_STREAM_END;
+               }
+
+               if (stream->class->supports_discarded_events) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_DISCARDED_EVENTS;
+               }
+
+               break;
+       }
+        case BT_MESSAGE_TYPE_PACKET_BEGINNING:
+       {
+               state->expected_msg_types = BT_MESSAGE_TYPE_EVENT |
+                       BT_MESSAGE_TYPE_PACKET_END;
+
+               if (stream->class->supports_discarded_events) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_DISCARDED_EVENTS;
+               }
+
+               break;
+       }
+        case BT_MESSAGE_TYPE_PACKET_END:
+       {
+               state->expected_msg_types = BT_MESSAGE_TYPE_PACKET_BEGINNING |
+                       BT_MESSAGE_TYPE_STREAM_END;
+
+               if (stream->class->supports_discarded_events) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_DISCARDED_EVENTS;
+               }
+
+               if (stream->class->supports_discarded_packets) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_DISCARDED_PACKETS;
+               }
+
+               break;
+       }
+        case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
+               state->expected_msg_types = BT_MESSAGE_TYPE_DISCARDED_EVENTS;
+
+               if (state->cur_packet) {
+                       state->expected_msg_types |= BT_MESSAGE_TYPE_EVENT |
+                               BT_MESSAGE_TYPE_PACKET_END;
+               } else {
+                       state->expected_msg_types |= BT_MESSAGE_TYPE_STREAM_END;
+
+                       if (stream->class->supports_packets) {
+                               state->expected_msg_types |=
+                                       BT_MESSAGE_TYPE_PACKET_BEGINNING;
+
+                               if (stream->class->supports_discarded_packets) {
+                                       state->expected_msg_types |=
+                                               BT_MESSAGE_TYPE_DISCARDED_PACKETS;
+                               }
+                       } else {
+                               state->expected_msg_types |=
+                                       BT_MESSAGE_TYPE_EVENT;
+                       }
+               }
+
+               break;
+        case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
+               state->expected_msg_types = BT_MESSAGE_TYPE_DISCARDED_PACKETS |
+                       BT_MESSAGE_TYPE_PACKET_BEGINNING |
+                       BT_MESSAGE_TYPE_STREAM_END;
+
+               if (stream->class->supports_discarded_events) {
+                       state->expected_msg_types |=
+                               BT_MESSAGE_TYPE_DISCARDED_EVENTS;
+               }
+               break;
+       default:
+               /*
+                * Other message types are not associated to a stream, so we
+                * should not get them here.
+                */
+                bt_common_abort();
+        }
+}
+
 static
 struct per_stream_state *get_per_stream_state(
                struct bt_message_iterator *iterator,
@@ -877,17 +983,46 @@ struct per_stream_state *get_per_stream_state(
 
        if (!state) {
                state = g_new0(struct per_stream_state, 1);
+               state->expected_msg_types = BT_MESSAGE_TYPE_STREAM_BEGINNING;
                g_hash_table_insert(iterator->per_stream_state,
                        (gpointer) stream, state);
        }
 
        return state;
 }
-#endif
 
-#define NEXT_METHOD_NAME       "bt_message_iterator_class_next_method"
+static
+void assert_post_dev_expected_sequence(struct bt_message_iterator *iterator,
+               const struct bt_message *msg)
+{
+       const bt_stream *stream = get_stream_from_msg(msg);
+       struct per_stream_state *state;
+
+       if (!stream) {
+               goto end;
+       }
+
+       state = get_per_stream_state(iterator, stream);
+
+       /*
+        * We don't free the return value of message_types_to_string(), but
+        * that's because we know the program is going to abort anyway, and
+        * we don't want to call it if the assertion holds.
+        */
+       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+               "message-type-is-expected",
+               msg->type & state->expected_msg_types,
+               "Unexpected message type: %![stream-]s, %![iterator-]i, "
+               "%![message-]n, expected-msg-types=%s",
+               stream, iterator, msg,
+               message_types_to_string(state->expected_msg_types)->str);
+
+       update_expected_msg_type(stream, state, msg);
+
+end:
+       return;
+}
 
-#ifdef BT_DEV_MODE
 static
 void assert_post_dev_expected_packet(struct bt_message_iterator *iterator,
                const struct bt_message *msg)
@@ -958,8 +1093,30 @@ void assert_post_dev_next(
                uint64_t i;
 
                for (i = 0; i < msg_count; i++) {
+                       assert_post_dev_expected_sequence(iterator, msgs[i]);
                        assert_post_dev_expected_packet(iterator, msgs[i]);
                }
+       } else if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) {
+               GHashTableIter iter;
+
+               gpointer stream_v, stream_state_v;
+
+               g_hash_table_iter_init(&iter, iterator->per_stream_state);
+               while (g_hash_table_iter_next(&iter, &stream_v,
+                               &stream_state_v)) {
+                       struct bt_stream *stream = stream_v;
+                       struct per_stream_state *stream_state = stream_state_v;
+
+                       BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
+                               "stream-is-ended",
+                               stream_state->expected_msg_types == 0,
+                               "Stream is not ended: %![stream-]s, "
+                               "%![iterator-]i, expected-msg-types=%s",
+                               stream, iterator,
+                               message_types_to_string(
+                                       stream_state->expected_msg_types)->str);
+               }
+
        }
 }
 #endif
@@ -984,11 +1141,9 @@ call_iterator_next_method(
                bt_common_func_status_string(status), *user_count);
 
        if (status == BT_FUNC_STATUS_OK) {
-               BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
-                       "message-clock-classes-are-compatible",
-                       clock_classes_are_compatible(iterator, msgs,
-                               *user_count),
-                       "Clocks are not compatible");
+               BT_IF_DEV_MODE(assert_post_dev_clock_classes_are_compatible(
+                       iterator, msgs, *user_count));
+
                BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
                        "message-clock-snapshots-are-monotonic",
                        clock_snapshots_are_monotonic(iterator, msgs,
@@ -1060,7 +1215,7 @@ bt_message_iterator_next(
         * by its downstream owner.
         *
         * For the same reason, there is no way that this iterator could
-        * have seeked (cannot seek a self message iterator).
+        * have sought (cannot seek a self message iterator).
         */
        BT_ASSERT_DBG(iterator->state ==
                BT_MESSAGE_ITERATOR_STATE_ACTIVE);
@@ -1831,7 +1986,7 @@ end:
 
 /*
  * This function is installed as the iterator's next callback after we have
- * auto-seeked (seeked to the beginning and fast-forwarded) to send the
+ * auto-sought (sought to the beginning and fast-forwarded) to send the
  * messages saved in iterator->auto_seek.msgs.  Once this is done, the original
  * next callback is put back.
  */
This page took 0.03551 seconds and 4 git commands to generate.