2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #ifndef BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H
9 #define BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H
11 #include "common/macros.h"
12 #include "lib/object.h"
13 #include <babeltrace2/graph/connection.h>
14 #include <babeltrace2/graph/message.h>
15 #include <babeltrace2/types.h>
17 #include "common/uuid.h"
22 enum bt_message_iterator_state
{
23 /* Iterator is not initialized */
24 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
,
26 /* Iterator is active, not at the end yet, and not finalized */
27 BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
30 * Iterator is ended, not finalized yet: the "next" method
31 * returns BT_MESSAGE_ITERATOR_STATUS_END.
33 BT_MESSAGE_ITERATOR_STATE_ENDED
,
35 /* Iterator is currently being finalized */
36 BT_MESSAGE_ITERATOR_STATE_FINALIZING
,
38 /* Iterator is finalized */
39 BT_MESSAGE_ITERATOR_STATE_FINALIZED
,
41 /* Iterator is seeking */
42 BT_MESSAGE_ITERATOR_STATE_SEEKING
,
44 /* Iterator did seek, but returned `BT_MESSAGE_ITERATOR_STATUS_AGAIN` */
45 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
,
47 /* Iterator did seek, but returned error status */
48 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
,
51 typedef enum bt_message_iterator_class_next_method_status
52 (*bt_message_iterator_next_method
)(
53 void *, bt_message_array_const
, uint64_t, uint64_t *);
55 typedef enum bt_message_iterator_class_seek_ns_from_origin_method_status
56 (*bt_message_iterator_seek_ns_from_origin_method
)(
59 typedef enum bt_message_iterator_class_seek_beginning_method_status
60 (*bt_message_iterator_seek_beginning_method
)(
63 typedef enum bt_message_iterator_class_can_seek_ns_from_origin_method_status
64 (*bt_message_iterator_can_seek_ns_from_origin_method
)(
65 void *, int64_t, bt_bool
*);
67 typedef enum bt_message_iterator_class_can_seek_beginning_method_status
68 (*bt_message_iterator_can_seek_beginning_method
)(
71 struct bt_self_message_iterator_configuration
{
73 bool can_seek_forward
;
76 struct bt_message_iterator
{
77 struct bt_object base
;
79 struct bt_component
*upstream_component
; /* Weak */
80 struct bt_port
*upstream_port
; /* Weak */
81 struct bt_connection
*connection
; /* Weak */
82 struct bt_graph
*graph
; /* Weak */
83 struct bt_self_message_iterator_configuration config
;
87 * `struct bt_message_iterator *`
90 * This is an array of upstream message iterators on which this
91 * iterator depends. The references are weak: an upstream
92 * message iterator is responsible for removing its entry within
93 * this array on finalization/destruction.
95 GPtrArray
*upstream_msg_iters
;
98 * Downstream message iterator which depends on this message
101 * This can be `NULL` if this message iterator's owner is a sink
104 struct bt_message_iterator
*downstream_msg_iter
;
107 bt_message_iterator_next_method next
;
109 /* These two are always both set or both unset. */
110 bt_message_iterator_seek_ns_from_origin_method seek_ns_from_origin
;
111 bt_message_iterator_can_seek_ns_from_origin_method can_seek_ns_from_origin
;
113 /* These two are always both set or both unset. */
114 bt_message_iterator_seek_beginning_method seek_beginning
;
115 bt_message_iterator_can_seek_beginning_method can_seek_beginning
;
118 enum bt_message_iterator_state state
;
121 * Timestamp of the last received message (or INT64_MIN in the
122 * beginning, or after a seek to beginning).
124 int64_t last_ns_from_origin
;
128 /* We haven't recorded clock properties yet. */
129 CLOCK_EXPECTATION_UNSET
,
131 /* Expect to have no clock. */
132 CLOCK_EXPECTATION_NONE
,
134 /* Clock with origin_is_unix_epoch true.*/
135 CLOCK_EXPECTATION_ORIGIN_UNIX
,
137 /* Clock with origin_is_unix_epoch false, with a UUID.*/
138 CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
,
140 /* Clock with origin_is_unix_epoch false, without a UUID.*/
141 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
,
147 * Expected UUID of the clock, if `type`is
148 * CLOCK_EXPECTATION_ORIGIN_OTHER_UUID.
150 * If the clock's origin is the unix epoch, the UUID is
151 * irrelevant (as the clock will be correlatable with other
152 * clocks having the same origin).
157 * Expected clock class, if `type` is
158 * CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID.
160 * If the first clock class seen has an unknown origin
161 * and no UUID, then all subsequent clock classes seen
162 * must be the same instance.
164 * To make sure that the clock class pointed by this
165 * field doesn't get freed and another one reallocated
166 * at the same address (which could potentially bypass
167 * the clock expectation check), we keep a strong
168 * reference, ensuring that the clock class lives at
169 * least as long as the iterator.
171 const bt_clock_class
*clock_class
;
175 BT_IF_DEV_MODE(GHashTable
*per_stream_state
);
178 * Data necessary for auto seek (the seek-to-beginning then fast-forward
183 * Queue of `const bt_message *` (owned by this queue).
185 * When fast-forwarding, we get the messages from upstream in
186 * batches. Once we have found the first message with timestamp
187 * greater or equal to the seek time, we put it and all of the
188 * following message of the batch in this queue. They will be
189 * sent on the next "next" call on this iterator.
191 * The messages are in chronological order (i.e. the first to
192 * send is the first of the queue).
197 * After auto-seeking, we replace the iterator's `next` callback
198 * with our own, which returns the contents of the `msgs` queue.
199 * This field is where we save the original callback, so we can
202 void *original_next_callback
;
208 void bt_message_iterator_try_finalize(
209 struct bt_message_iterator
*iterator
);
211 void bt_message_iterator_set_connection(
212 struct bt_message_iterator
*iterator
,
213 struct bt_connection
*connection
);
216 const char *bt_message_iterator_state_string(
217 enum bt_message_iterator_state state
)
220 case BT_MESSAGE_ITERATOR_STATE_ACTIVE
:
222 case BT_MESSAGE_ITERATOR_STATE_ENDED
:
224 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
226 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
228 case BT_MESSAGE_ITERATOR_STATE_SEEKING
:
230 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
:
231 return "LAST_SEEKING_RETURNED_AGAIN";
232 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
:
233 return "LAST_SEEKING_RETURNED_ERROR";
239 #endif /* BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H */