Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / message / iterator.h
CommitLineData
d6e69534
PP
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
91d81473 27#include "common/macros.h"
578e048b 28#include "lib/object.h"
43c59509
PP
29#include <babeltrace2/graph/connection.h>
30#include <babeltrace2/graph/message.h>
3fadfbc0 31#include <babeltrace2/types.h>
578e048b 32#include "common/assert.h"
d6e69534 33#include <stdbool.h>
6162e6b7 34#include "common/uuid.h"
d6e69534
PP
35
36struct bt_port;
37struct bt_graph;
38
9a2c8b8e 39enum bt_message_iterator_state {
7474e7d3 40 /* Iterator is not initialized */
9a2c8b8e 41 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED,
d6e69534 42
7474e7d3 43 /* Iterator is active, not at the end yet, and not finalized */
9a2c8b8e 44 BT_MESSAGE_ITERATOR_STATE_ACTIVE,
d6e69534
PP
45
46 /*
47 * Iterator is ended, not finalized yet: the "next" method
48 * returns BT_MESSAGE_ITERATOR_STATUS_END.
49 */
9a2c8b8e 50 BT_MESSAGE_ITERATOR_STATE_ENDED,
d6e69534 51
7474e7d3 52 /* Iterator is currently being finalized */
9a2c8b8e 53 BT_MESSAGE_ITERATOR_STATE_FINALIZING,
d6e69534 54
7474e7d3 55 /* Iterator is finalized */
9a2c8b8e 56 BT_MESSAGE_ITERATOR_STATE_FINALIZED,
7474e7d3
PP
57
58 /* Iterator is seeking */
9a2c8b8e 59 BT_MESSAGE_ITERATOR_STATE_SEEKING,
7474e7d3
PP
60
61 /* Iterator did seek, but returned `BT_MESSAGE_ITERATOR_STATUS_AGAIN` */
9a2c8b8e 62 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN,
7474e7d3
PP
63
64 /* Iterator did seek, but returned error status */
9a2c8b8e 65 BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR,
d6e69534
PP
66};
67
a3f0c7db 68typedef enum bt_message_iterator_class_next_method_status
9a2c8b8e 69(*bt_message_iterator_next_method)(
7474e7d3
PP
70 void *, bt_message_array_const, uint64_t, uint64_t *);
71
a3f0c7db 72typedef enum bt_message_iterator_class_seek_ns_from_origin_method_status
9a2c8b8e 73(*bt_message_iterator_seek_ns_from_origin_method)(
7474e7d3
PP
74 void *, int64_t);
75
a3f0c7db 76typedef enum bt_message_iterator_class_seek_beginning_method_status
9a2c8b8e 77(*bt_message_iterator_seek_beginning_method)(
7474e7d3
PP
78 void *);
79
a3f0c7db 80typedef enum bt_message_iterator_class_can_seek_ns_from_origin_method_status
9a2c8b8e 81(*bt_message_iterator_can_seek_ns_from_origin_method)(
f2fb1b32 82 void *, int64_t, bt_bool *);
7474e7d3 83
a3f0c7db 84typedef enum bt_message_iterator_class_can_seek_beginning_method_status
9a2c8b8e 85(*bt_message_iterator_can_seek_beginning_method)(
f2fb1b32 86 void *, bt_bool *);
7474e7d3 87
8d8b141d
SM
88struct bt_self_message_iterator_configuration {
89 bool frozen;
90 bool can_seek_forward;
91};
92
9a2c8b8e 93struct bt_message_iterator {
6c373cc9
PP
94 struct bt_object base;
95 GPtrArray *msgs;
d6e69534
PP
96 struct bt_component *upstream_component; /* Weak */
97 struct bt_port *upstream_port; /* Weak */
98 struct bt_connection *connection; /* Weak */
99 struct bt_graph *graph; /* Weak */
8d8b141d 100 struct bt_self_message_iterator_configuration config;
d6e69534 101
ca02df0a
PP
102 /*
103 * Array of
9a2c8b8e 104 * `struct bt_message_iterator *`
ca02df0a
PP
105 * (weak).
106 *
107 * This is an array of upstream message iterators on which this
108 * iterator depends. The references are weak: an upstream
109 * message iterator is responsible for removing its entry within
110 * this array on finalization/destruction.
111 */
112 GPtrArray *upstream_msg_iters;
113
114 /*
115 * Downstream message iterator which depends on this message
116 * iterator (weak).
117 *
118 * This can be `NULL` if this message iterator's owner is a sink
119 * component.
120 */
9a2c8b8e 121 struct bt_message_iterator *downstream_msg_iter;
ca02df0a 122
7474e7d3 123 struct {
9a2c8b8e 124 bt_message_iterator_next_method next;
2e1b5615
SM
125
126 /* These two are always both set or both unset. */
9a2c8b8e
PP
127 bt_message_iterator_seek_ns_from_origin_method seek_ns_from_origin;
128 bt_message_iterator_can_seek_ns_from_origin_method can_seek_ns_from_origin;
2e1b5615
SM
129
130 /* These two are always both set or both unset. */
9a2c8b8e
PP
131 bt_message_iterator_seek_beginning_method seek_beginning;
132 bt_message_iterator_can_seek_beginning_method can_seek_beginning;
7474e7d3 133 } methods;
d6e69534 134
9a2c8b8e 135 enum bt_message_iterator_state state;
da9c4c52 136
54b135a0
SM
137 /*
138 * Timestamp of the last received message (or INT64_MIN in the
139 * beginning, or after a seek to beginning).
140 */
141 int64_t last_ns_from_origin;
142
143 struct {
144 enum {
145 /* We haven't recorded clock properties yet. */
146 CLOCK_EXPECTATION_UNSET,
147
148 /* Expect to have no clock. */
149 CLOCK_EXPECTATION_NONE,
150
151 /* Clock with origin_is_unix_epoch true.*/
152 CLOCK_EXPECTATION_ORIGIN_UNIX,
153
154 /* Clock with origin_is_unix_epoch false, with a UUID.*/
155 CLOCK_EXPECTATION_ORIGIN_OTHER_UUID,
156
157 /* Clock with origin_is_unix_epoch false, without a UUID.*/
158 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID,
159 } type;
160
161 /*
162 * Expected UUID of the clock, if `type`is CLOCK_EXPECTATION_ORIGIN_OTHER_UUID.
163 *
164 * If the clock's origin is the unix epoch, the UUID is
165 * irrelevant (as the clock will be correlatable with other
166 * clocks having the same origin).
167 */
6162e6b7 168 bt_uuid_t uuid;
54b135a0
SM
169 } clock_expectation;
170
da9c4c52
SM
171 /*
172 * Data necessary for auto seek (the seek-to-beginning then fast-forward
173 * seek strategy).
174 */
175 struct {
176 /*
177 * Queue of `const bt_message *` (owned by this queue).
178 *
179 * When fast-forwarding, we get the messages from upstream in
180 * batches. Once we have found the first message with timestamp
181 * greater or equal to the seek time, we put it and all of the
182 * following message of the batch in this queue. They will be
183 * sent on the next "next" call on this iterator.
184 *
185 * The messages are in chronological order (i.e. the first to
186 * send is the first of the queue).
187 */
188 GQueue *msgs;
572075a8
SM
189
190 /*
191 * After auto-seeking, we replace the iterator's `next` callback
192 * with our own, which returns the contents of the `msgs` queue.
193 * This field is where we save the original callback, so we can
194 * restore it.
195 */
196 void *original_next_callback;
da9c4c52
SM
197 } auto_seek;
198
d6e69534
PP
199 void *user_data;
200};
201
d6e69534 202BT_HIDDEN
9a2c8b8e
PP
203void bt_message_iterator_try_finalize(
204 struct bt_message_iterator *iterator);
d6e69534
PP
205
206BT_HIDDEN
9a2c8b8e
PP
207void bt_message_iterator_set_connection(
208 struct bt_message_iterator *iterator,
d6e69534
PP
209 struct bt_connection *connection);
210
d6e69534 211static inline
9a2c8b8e
PP
212const char *bt_message_iterator_state_string(
213 enum bt_message_iterator_state state)
d6e69534
PP
214{
215 switch (state) {
9a2c8b8e 216 case BT_MESSAGE_ITERATOR_STATE_ACTIVE:
8a432889 217 return "ACTIVE";
9a2c8b8e 218 case BT_MESSAGE_ITERATOR_STATE_ENDED:
8a432889 219 return "ENDED";
9a2c8b8e 220 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
8a432889 221 return "FINALIZING";
9a2c8b8e 222 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
8a432889 223 return "FINALIZED";
9a2c8b8e 224 case BT_MESSAGE_ITERATOR_STATE_SEEKING:
8a432889 225 return "SEEKING";
9a2c8b8e 226 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN:
8a432889 227 return "LAST_SEEKING_RETURNED_AGAIN";
9a2c8b8e 228 case BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR:
8a432889 229 return "LAST_SEEKING_RETURNED_ERROR";
d6e69534
PP
230 default:
231 return "(unknown)";
232 }
233};
234
235#endif /* BABELTRACE_GRAPH_MESSAGE_ITERATOR_INTERNAL_H */
This page took 0.075361 seconds and 4 git commands to generate.