lib: validate monotonicity of messages from upstream component
[babeltrace.git] / src / lib / graph / message / iterator.h
1 #ifndef BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H
2 #define BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "common/macros.h"
28 #include "lib/object.h"
29 #include <babeltrace2/graph/connection-const.h>
30 #include <babeltrace2/graph/message-const.h>
31 #include <babeltrace2/graph/message-iterator-const.h>
32 #include <babeltrace2/types.h>
33 #include "common/assert.h"
34 #include <stdbool.h>
35 #include <compat/uuid.h>
36
37 struct bt_port;
38 struct bt_graph;
39
40 enum bt_message_iterator_type {
41 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT,
42 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
43 };
44
45 enum bt_self_component_port_input_message_iterator_state {
46 /* Iterator is not initialized */
47 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED,
48
49 /* Iterator is active, not at the end yet, and not finalized */
50 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
51
52 /*
53 * Iterator is ended, not finalized yet: the "next" method
54 * returns BT_MESSAGE_ITERATOR_STATUS_END.
55 */
56 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED,
57
58 /* Iterator is currently being finalized */
59 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING,
60
61 /* Iterator is finalized */
62 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED,
63
64 /* Iterator is seeking */
65 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING,
66
67 /* Iterator did seek, but returned `BT_MESSAGE_ITERATOR_STATUS_AGAIN` */
68 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN,
69
70 /* Iterator did seek, but returned error status */
71 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR,
72 };
73
74 struct bt_message_iterator {
75 struct bt_object base;
76 enum bt_message_iterator_type type;
77 GPtrArray *msgs;
78 };
79
80 typedef enum bt_self_message_iterator_status
81 (*bt_self_component_port_input_message_iterator_next_method)(
82 void *, bt_message_array_const, uint64_t, uint64_t *);
83
84 typedef enum bt_self_message_iterator_status
85 (*bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)(
86 void *, int64_t);
87
88 typedef enum bt_self_message_iterator_status
89 (*bt_self_component_port_input_message_iterator_seek_beginning_method)(
90 void *);
91
92 typedef bt_bool
93 (*bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)(
94 void *, int64_t);
95
96 typedef bt_bool
97 (*bt_self_component_port_input_message_iterator_can_seek_beginning_method)(
98 void *);
99
100 struct bt_self_component_port_input_message_iterator {
101 struct bt_message_iterator base;
102 struct bt_component *upstream_component; /* Weak */
103 struct bt_port *upstream_port; /* Weak */
104 struct bt_connection *connection; /* Weak */
105 struct bt_graph *graph; /* Weak */
106
107 struct {
108 bt_self_component_port_input_message_iterator_next_method next;
109 bt_self_component_port_input_message_iterator_seek_ns_from_origin_method seek_ns_from_origin;
110 bt_self_component_port_input_message_iterator_seek_beginning_method seek_beginning;
111 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method can_seek_ns_from_origin;
112 bt_self_component_port_input_message_iterator_can_seek_beginning_method can_seek_beginning;
113 } methods;
114
115 enum bt_self_component_port_input_message_iterator_state state;
116
117 /*
118 * Timestamp of the last received message (or INT64_MIN in the
119 * beginning, or after a seek to beginning).
120 */
121 int64_t last_ns_from_origin;
122
123 struct {
124 enum {
125 /* We haven't recorded clock properties yet. */
126 CLOCK_EXPECTATION_UNSET,
127
128 /* Expect to have no clock. */
129 CLOCK_EXPECTATION_NONE,
130
131 /* Clock with origin_is_unix_epoch true.*/
132 CLOCK_EXPECTATION_ORIGIN_UNIX,
133
134 /* Clock with origin_is_unix_epoch false, with a UUID.*/
135 CLOCK_EXPECTATION_ORIGIN_OTHER_UUID,
136
137 /* Clock with origin_is_unix_epoch false, without a UUID.*/
138 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID,
139 } type;
140
141 /*
142 * Expected UUID of the clock, if `type`is CLOCK_EXPECTATION_ORIGIN_OTHER_UUID.
143 *
144 * If the clock's origin is the unix epoch, the UUID is
145 * irrelevant (as the clock will be correlatable with other
146 * clocks having the same origin).
147 */
148 uint8_t uuid[BABELTRACE_UUID_LEN];
149 } clock_expectation;
150
151 /*
152 * Data necessary for auto seek (the seek-to-beginning then fast-forward
153 * seek strategy).
154 */
155 struct {
156 /*
157 * Queue of `const bt_message *` (owned by this queue).
158 *
159 * When fast-forwarding, we get the messages from upstream in
160 * batches. Once we have found the first message with timestamp
161 * greater or equal to the seek time, we put it and all of the
162 * following message of the batch in this queue. They will be
163 * sent on the next "next" call on this iterator.
164 *
165 * The messages are in chronological order (i.e. the first to
166 * send is the first of the queue).
167 */
168 GQueue *msgs;
169
170 /*
171 * After auto-seeking, we replace the iterator's `next` callback
172 * with our own, which returns the contents of the `msgs` queue.
173 * This field is where we save the original callback, so we can
174 * restore it.
175 */
176 void *original_next_callback;
177 } auto_seek;
178
179 void *user_data;
180 };
181
182 struct bt_port_output_message_iterator {
183 struct bt_message_iterator base;
184 struct bt_graph *graph; /* Owned by this */
185 struct bt_component_sink *colander; /* Owned by this */
186
187 /*
188 * Only used temporarily as a bridge between a colander sink and
189 * the user.
190 */
191 uint64_t count;
192 };
193
194 BT_HIDDEN
195 void bt_self_component_port_input_message_iterator_try_finalize(
196 struct bt_self_component_port_input_message_iterator *iterator);
197
198 BT_HIDDEN
199 void bt_self_component_port_input_message_iterator_set_connection(
200 struct bt_self_component_port_input_message_iterator *iterator,
201 struct bt_connection *connection);
202
203 static inline
204 const char *bt_self_component_port_input_message_iterator_state_string(
205 enum bt_self_component_port_input_message_iterator_state state)
206 {
207 switch (state) {
208 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE:
209 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE";
210 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED:
211 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED";
212 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING:
213 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING";
214 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
215 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED";
216 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING:
217 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING";
218 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN:
219 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN";
220 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR:
221 return "BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR";
222 default:
223 return "(unknown)";
224 }
225 };
226
227 #endif /* BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H */
This page took 0.036092 seconds and 5 git commands to generate.