Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / graph / message / iterator.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #ifndef BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H
9 #define BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H
10
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"
17 #include <stdbool.h>
18 #include "common/uuid.h"
19
20 struct bt_port;
21 struct bt_graph;
22
23 enum bt_message_iterator_state {
24 /* Iterator is not initialized */
25 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED,
26
27 /* Iterator is active, not at the end yet, and not finalized */
28 BT_MESSAGE_ITERATOR_STATE_ACTIVE,
29
30 /*
31 * Iterator is ended, not finalized yet: the "next" method
32 * returns BT_MESSAGE_ITERATOR_STATUS_END.
33 */
34 BT_MESSAGE_ITERATOR_STATE_ENDED,
35
36 /* Iterator is currently being finalized */
37 BT_MESSAGE_ITERATOR_STATE_FINALIZING,
38
39 /* Iterator is finalized */
40 BT_MESSAGE_ITERATOR_STATE_FINALIZED,
41
42 /* Iterator is seeking */
43 BT_MESSAGE_ITERATOR_STATE_SEEKING,
44
45 /* Iterator did seek, but returned `BT_MESSAGE_ITERATOR_STATUS_AGAIN` */
46 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN,
47
48 /* Iterator did seek, but returned error status */
49 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR,
50 };
51
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 *);
55
56 typedef enum bt_message_iterator_class_seek_ns_from_origin_method_status
57 (*bt_message_iterator_seek_ns_from_origin_method)(
58 void *, int64_t);
59
60 typedef enum bt_message_iterator_class_seek_beginning_method_status
61 (*bt_message_iterator_seek_beginning_method)(
62 void *);
63
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 *);
67
68 typedef enum bt_message_iterator_class_can_seek_beginning_method_status
69 (*bt_message_iterator_can_seek_beginning_method)(
70 void *, bt_bool *);
71
72 struct bt_self_message_iterator_configuration {
73 bool frozen;
74 bool can_seek_forward;
75 };
76
77 struct bt_message_iterator {
78 struct bt_object base;
79 GPtrArray *msgs;
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;
85
86 /*
87 * Array of
88 * `struct bt_message_iterator *`
89 * (weak).
90 *
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.
95 */
96 GPtrArray *upstream_msg_iters;
97
98 /*
99 * Downstream message iterator which depends on this message
100 * iterator (weak).
101 *
102 * This can be `NULL` if this message iterator's owner is a sink
103 * component.
104 */
105 struct bt_message_iterator *downstream_msg_iter;
106
107 struct {
108 bt_message_iterator_next_method next;
109
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;
113
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;
117 } methods;
118
119 enum bt_message_iterator_state state;
120
121 /*
122 * Timestamp of the last received message (or INT64_MIN in the
123 * beginning, or after a seek to beginning).
124 */
125 int64_t last_ns_from_origin;
126
127 struct {
128 enum {
129 /* We haven't recorded clock properties yet. */
130 CLOCK_EXPECTATION_UNSET,
131
132 /* Expect to have no clock. */
133 CLOCK_EXPECTATION_NONE,
134
135 /* Clock with origin_is_unix_epoch true.*/
136 CLOCK_EXPECTATION_ORIGIN_UNIX,
137
138 /* Clock with origin_is_unix_epoch false, with a UUID.*/
139 CLOCK_EXPECTATION_ORIGIN_OTHER_UUID,
140
141 /* Clock with origin_is_unix_epoch false, without a UUID.*/
142 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID,
143 } type;
144
145 /*
146 * Expected UUID of the clock, if `type`is CLOCK_EXPECTATION_ORIGIN_OTHER_UUID.
147 *
148 * If the clock's origin is the unix epoch, the UUID is
149 * irrelevant (as the clock will be correlatable with other
150 * clocks having the same origin).
151 */
152 bt_uuid_t uuid;
153 } clock_expectation;
154
155 /*
156 * Data necessary for auto seek (the seek-to-beginning then fast-forward
157 * seek strategy).
158 */
159 struct {
160 /*
161 * Queue of `const bt_message *` (owned by this queue).
162 *
163 * When fast-forwarding, we get the messages from upstream in
164 * batches. Once we have found the first message with timestamp
165 * greater or equal to the seek time, we put it and all of the
166 * following message of the batch in this queue. They will be
167 * sent on the next "next" call on this iterator.
168 *
169 * The messages are in chronological order (i.e. the first to
170 * send is the first of the queue).
171 */
172 GQueue *msgs;
173
174 /*
175 * After auto-seeking, we replace the iterator's `next` callback
176 * with our own, which returns the contents of the `msgs` queue.
177 * This field is where we save the original callback, so we can
178 * restore it.
179 */
180 void *original_next_callback;
181 } auto_seek;
182
183 void *user_data;
184 };
185
186 BT_HIDDEN
187 void bt_message_iterator_try_finalize(
188 struct bt_message_iterator *iterator);
189
190 BT_HIDDEN
191 void bt_message_iterator_set_connection(
192 struct bt_message_iterator *iterator,
193 struct bt_connection *connection);
194
195 static inline
196 const char *bt_message_iterator_state_string(
197 enum bt_message_iterator_state state)
198 {
199 switch (state) {
200 case BT_MESSAGE_ITERATOR_STATE_ACTIVE:
201 return "ACTIVE";
202 case BT_MESSAGE_ITERATOR_STATE_ENDED:
203 return "ENDED";
204 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
205 return "FINALIZING";
206 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
207 return "FINALIZED";
208 case BT_MESSAGE_ITERATOR_STATE_SEEKING:
209 return "SEEKING";
210 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN:
211 return "LAST_SEEKING_RETURNED_AGAIN";
212 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR:
213 return "LAST_SEEKING_RETURNED_ERROR";
214 default:
215 return "(unknown)";
216 }
217 };
218
219 #endif /* BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H */
This page took 0.049815 seconds and 4 git commands to generate.