581f9c1f8d5e667c1b0cb1e93989a2316323366c
[babeltrace.git] / src / lib / graph / 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 union {
147 /*
148 * Expected UUID of the clock, if `type`is
149 * CLOCK_EXPECTATION_ORIGIN_OTHER_UUID.
150 *
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).
154 */
155 bt_uuid_t uuid;
156
157 /*
158 * Expected clock class, if `type` is
159 * CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID.
160 *
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.
164 *
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.
171 */
172 const bt_clock_class *clock_class;
173 };
174 } clock_expectation;
175
176 BT_IF_DEV_MODE(GHashTable *per_stream_state);
177
178 /*
179 * Data necessary for auto seek (the seek-to-beginning then fast-forward
180 * seek strategy).
181 */
182 struct {
183 /*
184 * Queue of `const bt_message *` (owned by this queue).
185 *
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.
191 *
192 * The messages are in chronological order (i.e. the first to
193 * send is the first of the queue).
194 */
195 GQueue *msgs;
196
197 /*
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
201 * restore it.
202 */
203 void *original_next_callback;
204 } auto_seek;
205
206 void *user_data;
207 };
208
209 void bt_message_iterator_try_finalize(
210 struct bt_message_iterator *iterator);
211
212 void bt_message_iterator_set_connection(
213 struct bt_message_iterator *iterator,
214 struct bt_connection *connection);
215
216 static inline
217 const char *bt_message_iterator_state_string(
218 enum bt_message_iterator_state state)
219 {
220 switch (state) {
221 case BT_MESSAGE_ITERATOR_STATE_ACTIVE:
222 return "ACTIVE";
223 case BT_MESSAGE_ITERATOR_STATE_ENDED:
224 return "ENDED";
225 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
226 return "FINALIZING";
227 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
228 return "FINALIZED";
229 case BT_MESSAGE_ITERATOR_STATE_SEEKING:
230 return "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";
235 default:
236 return "(unknown)";
237 }
238 };
239
240 #endif /* BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H */
This page took 0.035134 seconds and 3 git commands to generate.