ctf: msg-iter.c: rename `notit` to `msg_it`
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.h
1 #ifndef CTF_MSG_ITER_H
2 #define CTF_MSG_ITER_H
3
4 /*
5 * Babeltrace - CTF message iterator
6 *
7 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
8 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stddef.h>
33 #include <babeltrace2/babeltrace.h>
34 #include "common/macros.h"
35
36 #include "../metadata/ctf-meta.h"
37
38 /**
39 * @file ctf-msg-iter.h
40 *
41 * CTF message iterator
42 *
43 * This is a common internal API used by CTF source plugins. It allows
44 * one to get messages from a user-provided medium.
45 */
46
47 /**
48 * Medium operations status codes.
49 */
50 enum bt_msg_iter_medium_status {
51 /**
52 * End of file.
53 *
54 * The medium function called by the message iterator
55 * function reached the end of the file.
56 */
57 BT_MSG_ITER_MEDIUM_STATUS_EOF = 1,
58
59 /**
60 * There is no data available right now, try again later.
61 */
62 BT_MSG_ITER_MEDIUM_STATUS_AGAIN = 11,
63
64 /** Unsupported operation. */
65 BT_MSG_ITER_MEDIUM_STATUS_UNSUPPORTED = -3,
66
67 /** Invalid argument. */
68 BT_MSG_ITER_MEDIUM_STATUS_INVAL = -2,
69
70 /** General error. */
71 BT_MSG_ITER_MEDIUM_STATUS_ERROR = -1,
72
73 /** Everything okay. */
74 BT_MSG_ITER_MEDIUM_STATUS_OK = 0,
75 };
76
77 /**
78 * CTF message iterator API status code.
79 */
80 enum bt_msg_iter_status {
81 /**
82 * End of file.
83 *
84 * The medium function called by the message iterator
85 * function reached the end of the file.
86 */
87 BT_MSG_ITER_STATUS_EOF = BT_MSG_ITER_MEDIUM_STATUS_EOF,
88
89 /**
90 * There is no data available right now, try again later.
91 *
92 * Some condition resulted in the
93 * bt_msg_iter_medium_ops::request_bytes() user function not
94 * having access to any data now. You should retry calling the
95 * last called message iterator function once the situation
96 * is resolved.
97 */
98 BT_MSG_ITER_STATUS_AGAIN = BT_MSG_ITER_MEDIUM_STATUS_AGAIN,
99
100 /** Invalid argument. */
101 BT_MSG_ITER_STATUS_INVAL = BT_MSG_ITER_MEDIUM_STATUS_INVAL,
102
103 /** Unsupported operation. */
104 BT_MSG_ITER_STATUS_UNSUPPORTED = BT_MSG_ITER_MEDIUM_STATUS_UNSUPPORTED,
105
106 /** General error. */
107 BT_MSG_ITER_STATUS_ERROR = BT_MSG_ITER_MEDIUM_STATUS_ERROR,
108
109 /** Everything okay. */
110 BT_MSG_ITER_STATUS_OK = 0,
111 };
112
113 /**
114 * CTF message iterator seek operation directives.
115 */
116 enum bt_msg_iter_seek_whence {
117 /**
118 * Set the iterator's position to an absolute offset in the underlying
119 * medium.
120 */
121 BT_MSG_ITER_SEEK_WHENCE_SET,
122 };
123
124 /**
125 * Medium operations.
126 *
127 * Those user functions are called by the message iterator
128 * functions to request medium actions.
129 */
130 struct bt_msg_iter_medium_ops {
131 /**
132 * Returns the next byte buffer to be used by the binary file
133 * reader to deserialize binary data.
134 *
135 * This function \em must be defined.
136 *
137 * The purpose of this function is to return a buffer of bytes
138 * to the message iterator, of a maximum of \p request_sz
139 * bytes. If this function cannot return a buffer of at least
140 * \p request_sz bytes, it may return a smaller buffer. In
141 * either cases, \p buffer_sz must be set to the returned buffer
142 * size (in bytes).
143 *
144 * The returned buffer's ownership remains the medium, in that
145 * it won't be freed by the message iterator functions. The
146 * returned buffer won't be modified by the message
147 * iterator functions either.
148 *
149 * When this function is called for the first time for a given
150 * file, the offset within the file is considered to be 0. The
151 * next times this function is called, the returned buffer's
152 * byte offset within the complete file must be the previous
153 * offset plus the last returned value of \p buffer_sz by this
154 * medium.
155 *
156 * This function must return one of the following statuses:
157 *
158 * - <b>#BT_MSG_ITER_MEDIUM_STATUS_OK</b>: Everything
159 * is okay, i.e. \p buffer_sz is set to a positive value
160 * reflecting the number of available bytes in the buffer
161 * starting at the address written in \p buffer_addr.
162 * - <b>#BT_MSG_ITER_MEDIUM_STATUS_AGAIN</b>: No data is
163 * available right now. In this case, the message
164 * iterator function called by the user returns
165 * #BT_MSG_ITER_STATUS_AGAIN, and it is the user's
166 * responsibility to make sure enough data becomes available
167 * before calling the \em same message iterator
168 * function again to continue the decoding process.
169 * - <b>#BT_MSG_ITER_MEDIUM_STATUS_EOF</b>: The end of
170 * the file was reached, and no more data will ever be
171 * available for this file. In this case, the message
172 * iterator function called by the user returns
173 * #BT_MSG_ITER_STATUS_EOF. This must \em not be
174 * returned when returning at least one byte of data to the
175 * caller, i.e. this must be returned when there's
176 * absolutely nothing left; should the request size be
177 * larger than what's left in the file, this function must
178 * return what's left, setting \p buffer_sz to the number of
179 * remaining bytes, and return
180 * #BT_MSG_ITER_MEDIUM_STATUS_EOF on the \em following
181 * call.
182 * - <b>#BT_MSG_ITER_MEDIUM_STATUS_ERROR</b>: A fatal
183 * error occured during this operation. In this case, the
184 * message iterator function called by the user returns
185 * #BT_MSG_ITER_STATUS_ERROR.
186 *
187 * If #BT_MSG_ITER_MEDIUM_STATUS_OK is not returned, the
188 * values of \p buffer_sz and \p buffer_addr are \em ignored by
189 * the caller.
190 *
191 * @param request_sz Requested buffer size (bytes)
192 * @param buffer_addr Returned buffer address
193 * @param buffer_sz Returned buffer's size (bytes)
194 * @param data User data
195 * @returns Status code (see description above)
196 */
197 enum bt_msg_iter_medium_status (* request_bytes)(size_t request_sz,
198 uint8_t **buffer_addr, size_t *buffer_sz, void *data);
199
200 /**
201 * Repositions the underlying stream's position.
202 *
203 * This *optional* method repositions the underlying stream
204 * to a given absolute or relative position, as indicated by
205 * the whence directive.
206 *
207 * @param whence One of #bt_msg_iter_seek_whence values
208 * @param offset Offset to use for the given directive
209 * @param data User data
210 * @returns One of #bt_msg_iter_medium_status values
211 */
212 enum bt_msg_iter_medium_status (* seek)(
213 enum bt_msg_iter_seek_whence whence,
214 off_t offset, void *data);
215
216 /**
217 * Returns a stream instance (weak reference) for the given
218 * stream class.
219 *
220 * This is called after a packet header is read, and the
221 * corresponding stream class is found by the message
222 * iterator.
223 *
224 * @param stream_class Stream class of the stream to get
225 * @param stream_id Stream (instance) ID of the stream
226 * to get (-1ULL if not available)
227 * @param data User data
228 * @returns Stream instance (weak reference) or
229 * \c NULL on error
230 */
231 bt_stream * (* borrow_stream)(bt_stream_class *stream_class,
232 int64_t stream_id, void *data);
233 };
234
235 /** CTF message iterator. */
236 struct bt_msg_iter;
237
238 /**
239 * Creates a CTF message iterator.
240 *
241 * Upon successful completion, the reference count of \p trace is
242 * incremented.
243 *
244 * @param trace Trace to read
245 * @param max_request_sz Maximum buffer size, in bytes, to
246 * request to
247 * bt_msg_iter_medium_ops::request_bytes()
248 * at a time
249 * @param medops Medium operations
250 * @param medops_data User data (passed to medium operations)
251 * @returns New CTF message iterator on
252 * success, or \c NULL on error
253 */
254 BT_HIDDEN
255 struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc,
256 size_t max_request_sz, struct bt_msg_iter_medium_ops medops,
257 void *medops_data, bt_logging_level log_level,
258 bt_self_component *self_comp);
259
260 /**
261 * Destroys a CTF message iterator, freeing all internal resources.
262 *
263 * The registered trace's reference count is decremented.
264 *
265 * @param msg_iter CTF message iterator
266 */
267 BT_HIDDEN
268 void bt_msg_iter_destroy(struct bt_msg_iter *msg_iter);
269
270 /**
271 * Returns the next message from a CTF message iterator.
272 *
273 * Upon successful completion, #BT_MSG_ITER_STATUS_OK is
274 * returned, and the next message is written to \p msg.
275 * In this case, the caller is responsible for calling
276 * bt_message_put() on the returned message.
277 *
278 * If this function returns #BT_MSG_ITER_STATUS_AGAIN, the caller
279 * should make sure that data becomes available to its medium, and
280 * call this function again, until another status is returned.
281 *
282 * @param msg_iter CTF message iterator
283 * @param message Returned message if the function's
284 * return value is #BT_MSG_ITER_STATUS_OK
285 * @returns One of #bt_msg_iter_status values
286 */
287 BT_HIDDEN
288 enum bt_msg_iter_status bt_msg_iter_get_next_message(
289 struct bt_msg_iter *msg_it,
290 bt_self_message_iterator *msg_iter,
291 bt_message **message);
292
293 struct bt_msg_iter_packet_properties {
294 int64_t exp_packet_total_size;
295 int64_t exp_packet_content_size;
296 uint64_t stream_class_id;
297 int64_t data_stream_id;
298
299 struct {
300 uint64_t discarded_events;
301 uint64_t packets;
302 uint64_t beginning_clock;
303 uint64_t end_clock;
304 } snapshots;
305 };
306
307 BT_HIDDEN
308 enum bt_msg_iter_status bt_msg_iter_get_packet_properties(
309 struct bt_msg_iter *msg_it,
310 struct bt_msg_iter_packet_properties *props);
311
312 BT_HIDDEN
313 enum bt_msg_iter_status bt_msg_iter_curr_packet_first_event_clock_snapshot(
314 struct bt_msg_iter *msg_it, uint64_t *first_event_cs);
315
316 BT_HIDDEN
317 enum bt_msg_iter_status bt_msg_iter_curr_packet_last_event_clock_snapshot(
318 struct bt_msg_iter *msg_it, uint64_t *last_event_cs);
319
320 BT_HIDDEN
321 void bt_msg_iter_set_medops_data(struct bt_msg_iter *msg_it,
322 void *medops_data);
323
324 BT_HIDDEN
325 enum bt_msg_iter_status bt_msg_iter_seek(
326 struct bt_msg_iter *msg_it, off_t offset);
327
328 /*
329 * Resets the iterator so that the next requested medium bytes are
330 * assumed to be the first bytes of a new stream. Depending on
331 * bt_msg_iter_set_emit_stream_beginning_message(), the first message
332 * which this iterator emits after calling bt_msg_iter_reset() is of
333 * type `BT_MESSAGE_TYPE_STREAM_BEGINNING`.
334 */
335 BT_HIDDEN
336 void bt_msg_iter_reset(struct bt_msg_iter *msg_it);
337
338 /*
339 * Like bt_msg_iter_reset(), but preserves stream-dependent state.
340 */
341 BT_HIDDEN
342 void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter *msg_it);
343
344 BT_HIDDEN
345 void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *msg_it,
346 bool val);
347
348 BT_HIDDEN
349 void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *msg_it,
350 bool val);
351
352 BT_HIDDEN
353 void bt_msg_iter_set_dry_run(struct bt_msg_iter *msg_it,
354 bool val);
355
356 static inline
357 const char *bt_msg_iter_medium_status_string(
358 enum bt_msg_iter_medium_status status)
359 {
360 switch (status) {
361 case BT_MSG_ITER_MEDIUM_STATUS_EOF:
362 return "EOF";
363 case BT_MSG_ITER_MEDIUM_STATUS_AGAIN:
364 return "AGAIN";
365 case BT_MSG_ITER_MEDIUM_STATUS_INVAL:
366 return "INVAL";
367 case BT_MSG_ITER_MEDIUM_STATUS_ERROR:
368 return "ERROR";
369 case BT_MSG_ITER_MEDIUM_STATUS_OK:
370 return "OK";
371 default:
372 return "(unknown)";
373 }
374 }
375
376 static inline
377 const char *bt_msg_iter_status_string(
378 enum bt_msg_iter_status status)
379 {
380 switch (status) {
381 case BT_MSG_ITER_STATUS_EOF:
382 return "EOF";
383 case BT_MSG_ITER_STATUS_AGAIN:
384 return "AGAIN";
385 case BT_MSG_ITER_STATUS_INVAL:
386 return "INVAL";
387 case BT_MSG_ITER_STATUS_ERROR:
388 return "ERROR";
389 case BT_MSG_ITER_STATUS_OK:
390 return "OK";
391 default:
392 return "(unknown)";
393 }
394 }
395
396 #endif /* CTF_MSG_ITER_H */
This page took 0.036793 seconds and 4 git commands to generate.