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>
16 #include "common/assert.h"
18 #include "common/uuid.h"
23 enum bt_message_iterator_state
{
24 /* Iterator is not initialized */
25 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
,
27 /* Iterator is active, not at the end yet, and not finalized */
28 BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
31 * Iterator is ended, not finalized yet: the "next" method
32 * returns BT_MESSAGE_ITERATOR_STATUS_END.
34 BT_MESSAGE_ITERATOR_STATE_ENDED
,
36 /* Iterator is currently being finalized */
37 BT_MESSAGE_ITERATOR_STATE_FINALIZING
,
39 /* Iterator is finalized */
40 BT_MESSAGE_ITERATOR_STATE_FINALIZED
,
42 /* Iterator is seeking */
43 BT_MESSAGE_ITERATOR_STATE_SEEKING
,
45 /* Iterator did seek, but returned `BT_MESSAGE_ITERATOR_STATUS_AGAIN` */
46 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
,
48 /* Iterator did seek, but returned error status */
49 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
,
52 typedef enum bt_message_iterator_class_next_method_status
53 (*bt_message_iterator_next_method
)(
54 void *, bt_message_array_const
, uint64_t, uint64_t *);
56 typedef enum bt_message_iterator_class_seek_ns_from_origin_method_status
57 (*bt_message_iterator_seek_ns_from_origin_method
)(
60 typedef enum bt_message_iterator_class_seek_beginning_method_status
61 (*bt_message_iterator_seek_beginning_method
)(
64 typedef enum bt_message_iterator_class_can_seek_ns_from_origin_method_status
65 (*bt_message_iterator_can_seek_ns_from_origin_method
)(
66 void *, int64_t, bt_bool
*);
68 typedef enum bt_message_iterator_class_can_seek_beginning_method_status
69 (*bt_message_iterator_can_seek_beginning_method
)(
72 struct bt_self_message_iterator_configuration
{
74 bool can_seek_forward
;
77 struct bt_message_iterator
{
78 struct bt_object base
;
80 struct bt_component
*upstream_component
; /* Weak */
81 struct bt_port
*upstream_port
; /* Weak */
82 struct bt_connection
*connection
; /* Weak */
83 struct bt_graph
*graph
; /* Weak */
84 struct bt_self_message_iterator_configuration config
;
88 * `struct bt_message_iterator *`
91 * This is an array of upstream message iterators on which this
92 * iterator depends. The references are weak: an upstream
93 * message iterator is responsible for removing its entry within
94 * this array on finalization/destruction.
96 GPtrArray
*upstream_msg_iters
;
99 * Downstream message iterator which depends on this message
102 * This can be `NULL` if this message iterator's owner is a sink
105 struct bt_message_iterator
*downstream_msg_iter
;
108 bt_message_iterator_next_method next
;
110 /* These two are always both set or both unset. */
111 bt_message_iterator_seek_ns_from_origin_method seek_ns_from_origin
;
112 bt_message_iterator_can_seek_ns_from_origin_method can_seek_ns_from_origin
;
114 /* These two are always both set or both unset. */
115 bt_message_iterator_seek_beginning_method seek_beginning
;
116 bt_message_iterator_can_seek_beginning_method can_seek_beginning
;
119 enum bt_message_iterator_state state
;
122 * Timestamp of the last received message (or INT64_MIN in the
123 * beginning, or after a seek to beginning).
125 int64_t last_ns_from_origin
;
129 /* We haven't recorded clock properties yet. */
130 CLOCK_EXPECTATION_UNSET
,
132 /* Expect to have no clock. */
133 CLOCK_EXPECTATION_NONE
,
135 /* Clock with origin_is_unix_epoch true.*/
136 CLOCK_EXPECTATION_ORIGIN_UNIX
,
138 /* Clock with origin_is_unix_epoch false, with a UUID.*/
139 CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
,
141 /* Clock with origin_is_unix_epoch false, without a UUID.*/
142 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
,
148 * Expected UUID of the clock, if `type`is
149 * CLOCK_EXPECTATION_ORIGIN_OTHER_UUID.
151 * If the clock's origin is the unix epoch, the UUID is
152 * irrelevant (as the clock will be correlatable with other
153 * clocks having the same origin).
158 * Expected clock class, if `type` is
159 * CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID.
161 * If the first clock class seen has an unknown origin
162 * and no UUID, then all subsequent clock classes seen
163 * must be the same instance.
165 * To make sure that the clock class pointed by this
166 * field doesn't get freed and another one reallocated
167 * at the same address (which could potentially bypass
168 * the clock expectation check), we keep a strong
169 * reference, ensuring that the clock class lives at
170 * least as long as the iterator.
172 const bt_clock_class
*clock_class
;
176 BT_IF_DEV_MODE(GHashTable
*per_stream_state
);
179 * Data necessary for auto seek (the seek-to-beginning then fast-forward
184 * Queue of `const bt_message *` (owned by this queue).
186 * When fast-forwarding, we get the messages from upstream in
187 * batches. Once we have found the first message with timestamp
188 * greater or equal to the seek time, we put it and all of the
189 * following message of the batch in this queue. They will be
190 * sent on the next "next" call on this iterator.
192 * The messages are in chronological order (i.e. the first to
193 * send is the first of the queue).
198 * After auto-seeking, we replace the iterator's `next` callback
199 * with our own, which returns the contents of the `msgs` queue.
200 * This field is where we save the original callback, so we can
203 void *original_next_callback
;
209 void bt_message_iterator_try_finalize(
210 struct bt_message_iterator
*iterator
);
212 void bt_message_iterator_set_connection(
213 struct bt_message_iterator
*iterator
,
214 struct bt_connection
*connection
);
217 const char *bt_message_iterator_state_string(
218 enum bt_message_iterator_state state
)
221 case BT_MESSAGE_ITERATOR_STATE_ACTIVE
:
223 case BT_MESSAGE_ITERATOR_STATE_ENDED
:
225 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
227 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
229 case BT_MESSAGE_ITERATOR_STATE_SEEKING
:
231 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
:
232 return "LAST_SEEKING_RETURNED_AGAIN";
233 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
:
234 return "LAST_SEEKING_RETURNED_ERROR";
240 #endif /* BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H */