Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | #ifndef CTF_NOTIF_ITER_H |
2 | #define CTF_NOTIF_ITER_H | |
3 | ||
4 | /* | |
5 | * Babeltrace - CTF notification 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 <stdint.h> | |
30 | #include <stdio.h> | |
31 | #include <stddef.h> | |
9d408fca | 32 | #include <babeltrace/babeltrace.h> |
e98a2d6e PP |
33 | #include <babeltrace/babeltrace-internal.h> |
34 | ||
44c440bc PP |
35 | #include "../metadata/ctf-meta.h" |
36 | ||
e98a2d6e PP |
37 | /** |
38 | * @file ctf-notif-iter.h | |
39 | * | |
40 | * CTF notification iterator | |
41 | * ¯¯¯¯¯ ¯¯¯¯ | |
42 | * This is a common internal API used by CTF source plugins. It allows | |
43 | * one to get notifications from a user-provided medium. | |
44 | */ | |
45 | ||
46 | /** | |
47 | * Medium operations status codes. | |
48 | */ | |
50842bdc | 49 | enum bt_notif_iter_medium_status { |
e98a2d6e PP |
50 | /** |
51 | * End of file. | |
52 | * | |
53 | * The medium function called by the notification iterator | |
54 | * function reached the end of the file. | |
55 | */ | |
50842bdc | 56 | BT_NOTIF_ITER_MEDIUM_STATUS_EOF = 1, |
e98a2d6e PP |
57 | |
58 | /** | |
59 | * There is no data available right now, try again later. | |
60 | */ | |
50842bdc | 61 | BT_NOTIF_ITER_MEDIUM_STATUS_AGAIN = 11, |
9e0c8dbb JG |
62 | |
63 | /** Unsupported operation. */ | |
50842bdc | 64 | BT_NOTIF_ITER_MEDIUM_STATUS_UNSUPPORTED = -3, |
e98a2d6e PP |
65 | |
66 | /** Invalid argument. */ | |
50842bdc | 67 | BT_NOTIF_ITER_MEDIUM_STATUS_INVAL = -2, |
e98a2d6e PP |
68 | |
69 | /** General error. */ | |
50842bdc | 70 | BT_NOTIF_ITER_MEDIUM_STATUS_ERROR = -1, |
e98a2d6e PP |
71 | |
72 | /** Everything okay. */ | |
50842bdc | 73 | BT_NOTIF_ITER_MEDIUM_STATUS_OK = 0, |
e98a2d6e PP |
74 | }; |
75 | ||
76 | /** | |
77 | * CTF notification iterator API status code. | |
78 | */ | |
50842bdc | 79 | enum bt_notif_iter_status { |
e98a2d6e PP |
80 | /** |
81 | * End of file. | |
82 | * | |
83 | * The medium function called by the notification iterator | |
84 | * function reached the end of the file. | |
85 | */ | |
50842bdc | 86 | BT_NOTIF_ITER_STATUS_EOF = BT_NOTIF_ITER_MEDIUM_STATUS_EOF, |
e98a2d6e PP |
87 | |
88 | /** | |
89 | * There is no data available right now, try again later. | |
90 | * | |
91 | * Some condition resulted in the | |
50842bdc | 92 | * bt_notif_iter_medium_ops::request_bytes() user function not |
e98a2d6e PP |
93 | * having access to any data now. You should retry calling the |
94 | * last called notification iterator function once the situation | |
95 | * is resolved. | |
96 | */ | |
50842bdc | 97 | BT_NOTIF_ITER_STATUS_AGAIN = BT_NOTIF_ITER_MEDIUM_STATUS_AGAIN, |
e98a2d6e PP |
98 | |
99 | /** Invalid argument. */ | |
50842bdc | 100 | BT_NOTIF_ITER_STATUS_INVAL = BT_NOTIF_ITER_MEDIUM_STATUS_INVAL, |
e98a2d6e | 101 | |
9e0c8dbb | 102 | /** Unsupported operation. */ |
50842bdc | 103 | BT_NOTIF_ITER_STATUS_UNSUPPORTED = BT_NOTIF_ITER_MEDIUM_STATUS_UNSUPPORTED, |
9e0c8dbb | 104 | |
e98a2d6e | 105 | /** General error. */ |
50842bdc | 106 | BT_NOTIF_ITER_STATUS_ERROR = BT_NOTIF_ITER_MEDIUM_STATUS_ERROR, |
e98a2d6e PP |
107 | |
108 | /** Everything okay. */ | |
50842bdc | 109 | BT_NOTIF_ITER_STATUS_OK = 0, |
e98a2d6e PP |
110 | }; |
111 | ||
9e0c8dbb JG |
112 | /** |
113 | * CTF notification iterator seek operation directives. | |
114 | */ | |
50842bdc | 115 | enum bt_notif_iter_seek_whence { |
9e0c8dbb JG |
116 | /** |
117 | * Set the iterator's position to an absolute offset in the underlying | |
118 | * medium. | |
119 | */ | |
50842bdc | 120 | BT_NOTIF_ITER_SEEK_WHENCE_SET, |
9e0c8dbb JG |
121 | }; |
122 | ||
e98a2d6e PP |
123 | /** |
124 | * Medium operations. | |
125 | * | |
126 | * Those user functions are called by the notification iterator | |
127 | * functions to request medium actions. | |
128 | */ | |
50842bdc | 129 | struct bt_notif_iter_medium_ops { |
e98a2d6e PP |
130 | /** |
131 | * Returns the next byte buffer to be used by the binary file | |
132 | * reader to deserialize binary data. | |
133 | * | |
134 | * This function \em must be defined. | |
135 | * | |
136 | * The purpose of this function is to return a buffer of bytes | |
137 | * to the notification iterator, of a maximum of \p request_sz | |
138 | * bytes. If this function cannot return a buffer of at least | |
139 | * \p request_sz bytes, it may return a smaller buffer. In | |
140 | * either cases, \p buffer_sz must be set to the returned buffer | |
141 | * size (in bytes). | |
142 | * | |
143 | * The returned buffer's ownership remains the medium, in that | |
144 | * it won't be freed by the notification iterator functions. The | |
145 | * returned buffer won't be modified by the notification | |
146 | * iterator functions either. | |
147 | * | |
148 | * When this function is called for the first time for a given | |
149 | * file, the offset within the file is considered to be 0. The | |
150 | * next times this function is called, the returned buffer's | |
151 | * byte offset within the complete file must be the previous | |
152 | * offset plus the last returned value of \p buffer_sz by this | |
153 | * medium. | |
154 | * | |
155 | * This function must return one of the following statuses: | |
156 | * | |
50842bdc | 157 | * - <b>#BT_NOTIF_ITER_MEDIUM_STATUS_OK</b>: Everything |
e98a2d6e PP |
158 | * is okay, i.e. \p buffer_sz is set to a positive value |
159 | * reflecting the number of available bytes in the buffer | |
160 | * starting at the address written in \p buffer_addr. | |
50842bdc | 161 | * - <b>#BT_NOTIF_ITER_MEDIUM_STATUS_AGAIN</b>: No data is |
e98a2d6e PP |
162 | * available right now. In this case, the notification |
163 | * iterator function called by the user returns | |
50842bdc | 164 | * #BT_NOTIF_ITER_STATUS_AGAIN, and it is the user's |
e98a2d6e PP |
165 | * responsibility to make sure enough data becomes available |
166 | * before calling the \em same notification iterator | |
167 | * function again to continue the decoding process. | |
50842bdc | 168 | * - <b>#BT_NOTIF_ITER_MEDIUM_STATUS_EOF</b>: The end of |
e98a2d6e PP |
169 | * the file was reached, and no more data will ever be |
170 | * available for this file. In this case, the notification | |
171 | * iterator function called by the user returns | |
50842bdc | 172 | * #BT_NOTIF_ITER_STATUS_EOF. This must \em not be |
e98a2d6e PP |
173 | * returned when returning at least one byte of data to the |
174 | * caller, i.e. this must be returned when there's | |
175 | * absolutely nothing left; should the request size be | |
176 | * larger than what's left in the file, this function must | |
177 | * return what's left, setting \p buffer_sz to the number of | |
178 | * remaining bytes, and return | |
50842bdc | 179 | * #BT_NOTIF_ITER_MEDIUM_STATUS_EOF on the \em following |
e98a2d6e | 180 | * call. |
50842bdc | 181 | * - <b>#BT_NOTIF_ITER_MEDIUM_STATUS_ERROR</b>: A fatal |
e98a2d6e PP |
182 | * error occured during this operation. In this case, the |
183 | * notification iterator function called by the user returns | |
50842bdc | 184 | * #BT_NOTIF_ITER_STATUS_ERROR. |
e98a2d6e | 185 | * |
50842bdc | 186 | * If #BT_NOTIF_ITER_MEDIUM_STATUS_OK is not returned, the |
e98a2d6e PP |
187 | * values of \p buffer_sz and \p buffer_addr are \em ignored by |
188 | * the caller. | |
189 | * | |
190 | * @param request_sz Requested buffer size (bytes) | |
191 | * @param buffer_addr Returned buffer address | |
192 | * @param buffer_sz Returned buffer's size (bytes) | |
193 | * @param data User data | |
194 | * @returns Status code (see description above) | |
195 | */ | |
50842bdc | 196 | enum bt_notif_iter_medium_status (* request_bytes)( |
e98a2d6e PP |
197 | size_t request_sz, uint8_t **buffer_addr, |
198 | size_t *buffer_sz, void *data); | |
199 | ||
9e0c8dbb JG |
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 | * | |
50842bdc | 207 | * @param whence One of #bt_notif_iter_seek_whence values |
9e0c8dbb JG |
208 | * @param offset Offset to use for the given directive |
209 | * @param data User data | |
50842bdc | 210 | * @returns One of #bt_notif_iter_medium_status values |
9e0c8dbb | 211 | */ |
50842bdc PP |
212 | enum bt_notif_iter_medium_status (* seek)( |
213 | enum bt_notif_iter_seek_whence whence, | |
9e0c8dbb JG |
214 | off_t offset, void *data); |
215 | ||
e98a2d6e PP |
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 notification | |
222 | * iterator. | |
223 | * | |
b92735af PP |
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) | |
e98a2d6e PP |
227 | * @param data User data |
228 | * @returns Stream instance (weak reference) or | |
229 | * \c NULL on error | |
230 | */ | |
40f4ba76 PP |
231 | struct bt_stream * (* borrow_stream)( |
232 | struct bt_stream_class *stream_class, | |
44c440bc | 233 | int64_t stream_id, void *data); |
e98a2d6e PP |
234 | }; |
235 | ||
236 | /** CTF notification iterator. */ | |
50842bdc | 237 | struct bt_notif_iter; |
e98a2d6e | 238 | |
e98a2d6e PP |
239 | /** |
240 | * Creates a CTF notification iterator. | |
241 | * | |
242 | * Upon successful completion, the reference count of \p trace is | |
243 | * incremented. | |
244 | * | |
245 | * @param trace Trace to read | |
246 | * @param max_request_sz Maximum buffer size, in bytes, to | |
247 | * request to | |
50842bdc | 248 | * bt_notif_iter_medium_ops::request_bytes() |
e98a2d6e PP |
249 | * at a time |
250 | * @param medops Medium operations | |
251 | * @param medops_data User data (passed to medium operations) | |
e98a2d6e PP |
252 | * @returns New CTF notification iterator on |
253 | * success, or \c NULL on error | |
254 | */ | |
2cf1d51e | 255 | BT_HIDDEN |
44c440bc | 256 | struct bt_notif_iter *bt_notif_iter_create(struct ctf_trace_class *tc, |
50842bdc | 257 | size_t max_request_sz, struct bt_notif_iter_medium_ops medops, |
55314f2a | 258 | void *medops_data); |
e98a2d6e PP |
259 | |
260 | /** | |
261 | * Destroys a CTF notification iterator, freeing all internal resources. | |
262 | * | |
263 | * The registered trace's reference count is decremented. | |
264 | * | |
265 | * @param notif_iter CTF notification iterator | |
266 | */ | |
2cf1d51e | 267 | BT_HIDDEN |
50842bdc | 268 | void bt_notif_iter_destroy(struct bt_notif_iter *notif_iter); |
e98a2d6e | 269 | |
e98a2d6e PP |
270 | /** |
271 | * Returns the next notification from a CTF notification iterator. | |
272 | * | |
50842bdc | 273 | * Upon successful completion, #BT_NOTIF_ITER_STATUS_OK is |
e98a2d6e PP |
274 | * returned, and the next notification is written to \p notif. |
275 | * In this case, the caller is responsible for calling | |
276 | * bt_notification_put() on the returned notification. | |
277 | * | |
50842bdc | 278 | * If this function returns #BT_NOTIF_ITER_STATUS_AGAIN, the caller |
e98a2d6e PP |
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 notif_iter CTF notification iterator | |
283 | * @param notification Returned notification if the function's | |
50842bdc PP |
284 | * return value is #BT_NOTIF_ITER_STATUS_OK |
285 | * @returns One of #bt_notif_iter_status values | |
e98a2d6e | 286 | */ |
2cf1d51e | 287 | BT_HIDDEN |
50842bdc PP |
288 | enum bt_notif_iter_status bt_notif_iter_get_next_notification( |
289 | struct bt_notif_iter *notit, | |
d94d92ac | 290 | struct bt_self_notification_iterator *notif_iter, |
0d72b8c3 | 291 | struct bt_notification **notification); |
e98a2d6e | 292 | |
87187cbf PP |
293 | /** |
294 | * Returns the first packet header and context fields. This function | |
312c056a | 295 | * never needs to call the `borrow_stream()` medium operation because |
87187cbf PP |
296 | * it does not create packet or event objects. |
297 | * | |
298 | * @param notif_iter CTF notification iterator | |
299 | * @param packet_header_field Packet header field (\c NULL if there's | |
300 | * no packet header field) | |
301 | * @param packet_context_field Packet context field (\c NULL if there's | |
302 | * no packet context field) | |
50842bdc | 303 | * @returns One of #bt_notif_iter_status values |
87187cbf PP |
304 | */ |
305 | BT_HIDDEN | |
312c056a | 306 | enum bt_notif_iter_status bt_notif_iter_borrow_packet_header_context_fields( |
50842bdc | 307 | struct bt_notif_iter *notit, |
40f4ba76 PP |
308 | struct bt_field **packet_header_field, |
309 | struct bt_field **packet_context_field); | |
87187cbf | 310 | |
44c440bc PP |
311 | struct bt_notif_iter_packet_properties { |
312 | uint64_t exp_packet_total_size; | |
313 | uint64_t exp_packet_content_size; | |
314 | uint64_t stream_class_id; | |
315 | int64_t data_stream_id; | |
316 | ||
317 | struct { | |
318 | uint64_t discarded_events; | |
319 | uint64_t packets; | |
320 | uint64_t beginning_clock; | |
321 | uint64_t end_clock; | |
322 | } snapshots; | |
323 | }; | |
324 | ||
325 | BT_HIDDEN | |
326 | enum bt_notif_iter_status bt_notif_iter_get_packet_properties( | |
327 | struct bt_notif_iter *notit, | |
328 | struct bt_notif_iter_packet_properties *props); | |
329 | ||
6de92955 | 330 | BT_HIDDEN |
50842bdc | 331 | void bt_notif_iter_set_medops_data(struct bt_notif_iter *notit, |
6de92955 PP |
332 | void *medops_data); |
333 | ||
9e0c8dbb | 334 | BT_HIDDEN |
50842bdc PP |
335 | enum bt_notif_iter_status bt_notif_iter_seek( |
336 | struct bt_notif_iter *notit, off_t offset); | |
9e0c8dbb JG |
337 | |
338 | /* | |
339 | * Get the current packet's offset in bytes relative to the media's initial | |
340 | * position. | |
341 | */ | |
342 | BT_HIDDEN | |
50842bdc PP |
343 | off_t bt_notif_iter_get_current_packet_offset( |
344 | struct bt_notif_iter *notit); | |
9e0c8dbb JG |
345 | |
346 | /* Get the current packet's size (in bits). */ | |
347 | BT_HIDDEN | |
50842bdc PP |
348 | off_t bt_notif_iter_get_current_packet_size( |
349 | struct bt_notif_iter *notit); | |
9e0c8dbb | 350 | |
f42867e2 PP |
351 | /* |
352 | * Resets the iterator so that the next requested medium bytes are | |
353 | * assumed to be the first bytes of a new stream. The first notification | |
354 | * which this iterator emits after calling bt_notif_iter_reset() is a | |
bb5bb160 | 355 | * BT_NOTIFICATION_TYPE_STREAM_BEGINNING one. |
f42867e2 PP |
356 | */ |
357 | BT_HIDDEN | |
358 | void bt_notif_iter_reset(struct bt_notif_iter *notit); | |
359 | ||
44c440bc PP |
360 | /* |
361 | * Notify the iterator that the trace class changed somehow (new | |
362 | * stream/event classes). | |
363 | */ | |
364 | BT_HIDDEN | |
365 | void bt_notif_trace_class_changed(struct bt_notif_iter *notit); | |
366 | ||
fdf0e7a0 | 367 | static inline |
50842bdc PP |
368 | const char *bt_notif_iter_medium_status_string( |
369 | enum bt_notif_iter_medium_status status) | |
fdf0e7a0 PP |
370 | { |
371 | switch (status) { | |
50842bdc PP |
372 | case BT_NOTIF_ITER_MEDIUM_STATUS_EOF: |
373 | return "BT_NOTIF_ITER_MEDIUM_STATUS_EOF"; | |
374 | case BT_NOTIF_ITER_MEDIUM_STATUS_AGAIN: | |
375 | return "BT_NOTIF_ITER_MEDIUM_STATUS_AGAIN"; | |
376 | case BT_NOTIF_ITER_MEDIUM_STATUS_INVAL: | |
377 | return "BT_NOTIF_ITER_MEDIUM_STATUS_INVAL"; | |
378 | case BT_NOTIF_ITER_MEDIUM_STATUS_ERROR: | |
379 | return "BT_NOTIF_ITER_MEDIUM_STATUS_ERROR"; | |
380 | case BT_NOTIF_ITER_MEDIUM_STATUS_OK: | |
381 | return "BT_NOTIF_ITER_MEDIUM_STATUS_OK"; | |
fdf0e7a0 PP |
382 | default: |
383 | return "(unknown)"; | |
384 | } | |
385 | } | |
386 | ||
387 | static inline | |
50842bdc PP |
388 | const char *bt_notif_iter_status_string( |
389 | enum bt_notif_iter_status status) | |
fdf0e7a0 PP |
390 | { |
391 | switch (status) { | |
50842bdc PP |
392 | case BT_NOTIF_ITER_STATUS_EOF: |
393 | return "BT_NOTIF_ITER_STATUS_EOF"; | |
394 | case BT_NOTIF_ITER_STATUS_AGAIN: | |
395 | return "BT_NOTIF_ITER_STATUS_AGAIN"; | |
396 | case BT_NOTIF_ITER_STATUS_INVAL: | |
397 | return "BT_NOTIF_ITER_STATUS_INVAL"; | |
398 | case BT_NOTIF_ITER_STATUS_ERROR: | |
399 | return "BT_NOTIF_ITER_STATUS_ERROR"; | |
400 | case BT_NOTIF_ITER_STATUS_OK: | |
401 | return "BT_NOTIF_ITER_STATUS_OK"; | |
fdf0e7a0 PP |
402 | default: |
403 | return "(unknown)"; | |
404 | } | |
405 | } | |
406 | ||
e98a2d6e | 407 | #endif /* CTF_NOTIF_ITER_H */ |