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> | |
32 | #include <babeltrace/ctf-ir/trace.h> | |
33 | #include <babeltrace/ctf-ir/fields.h> | |
34 | #include <babeltrace/ctf-ir/event.h> | |
599faa1c | 35 | #include <babeltrace/graph/clock-class-priority-map.h> |
e98a2d6e PP |
36 | #include <babeltrace/babeltrace-internal.h> |
37 | ||
38 | /** | |
39 | * @file ctf-notif-iter.h | |
40 | * | |
41 | * CTF notification iterator | |
42 | * ¯¯¯¯¯ ¯¯¯¯ | |
43 | * This is a common internal API used by CTF source plugins. It allows | |
44 | * one to get notifications from a user-provided medium. | |
45 | */ | |
46 | ||
47 | /** | |
48 | * Medium operations status codes. | |
49 | */ | |
50 | enum bt_ctf_notif_iter_medium_status { | |
51 | /** | |
52 | * End of file. | |
53 | * | |
54 | * The medium function called by the notification iterator | |
55 | * function reached the end of the file. | |
56 | */ | |
9e0c8dbb | 57 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF = 1, |
e98a2d6e PP |
58 | |
59 | /** | |
60 | * There is no data available right now, try again later. | |
61 | */ | |
9e0c8dbb JG |
62 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN = 11, |
63 | ||
64 | /** Unsupported operation. */ | |
65 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_UNSUPPORTED = -3, | |
e98a2d6e PP |
66 | |
67 | /** Invalid argument. */ | |
9e0c8dbb | 68 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL = -2, |
e98a2d6e PP |
69 | |
70 | /** General error. */ | |
9e0c8dbb | 71 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR = -1, |
e98a2d6e PP |
72 | |
73 | /** Everything okay. */ | |
9e0c8dbb | 74 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK = 0, |
e98a2d6e PP |
75 | }; |
76 | ||
77 | /** | |
78 | * CTF notification iterator API status code. | |
79 | */ | |
80 | enum bt_ctf_notif_iter_status { | |
81 | /** | |
82 | * End of file. | |
83 | * | |
84 | * The medium function called by the notification iterator | |
85 | * function reached the end of the file. | |
86 | */ | |
dc77b521 | 87 | BT_CTF_NOTIF_ITER_STATUS_EOF = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF, |
e98a2d6e PP |
88 | |
89 | /** | |
90 | * There is no data available right now, try again later. | |
91 | * | |
92 | * Some condition resulted in the | |
93 | * bt_ctf_notif_iter_medium_ops::request_bytes() user function not | |
94 | * having access to any data now. You should retry calling the | |
95 | * last called notification iterator function once the situation | |
96 | * is resolved. | |
97 | */ | |
dc77b521 | 98 | BT_CTF_NOTIF_ITER_STATUS_AGAIN = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN, |
e98a2d6e PP |
99 | |
100 | /** Invalid argument. */ | |
dc77b521 | 101 | BT_CTF_NOTIF_ITER_STATUS_INVAL = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL, |
e98a2d6e | 102 | |
9e0c8dbb JG |
103 | /** Unsupported operation. */ |
104 | BT_CTF_NOTIF_ITER_STATUS_UNSUPPORTED = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_UNSUPPORTED, | |
105 | ||
e98a2d6e | 106 | /** General error. */ |
dc77b521 | 107 | BT_CTF_NOTIF_ITER_STATUS_ERROR = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR, |
e98a2d6e PP |
108 | |
109 | /** Everything okay. */ | |
110 | BT_CTF_NOTIF_ITER_STATUS_OK = 0, | |
111 | }; | |
112 | ||
9e0c8dbb JG |
113 | /** |
114 | * CTF notification iterator seek operation directives. | |
115 | */ | |
116 | enum bt_ctf_notif_iter_seek_whence { | |
117 | /** | |
118 | * Set the iterator's position to an absolute offset in the underlying | |
119 | * medium. | |
120 | */ | |
121 | BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET, | |
122 | }; | |
123 | ||
e98a2d6e PP |
124 | /** |
125 | * Medium operations. | |
126 | * | |
127 | * Those user functions are called by the notification iterator | |
128 | * functions to request medium actions. | |
129 | */ | |
130 | struct bt_ctf_notif_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 notification 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 notification iterator functions. The | |
146 | * returned buffer won't be modified by the notification | |
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_CTF_NOTIF_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_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN</b>: No data is | |
163 | * available right now. In this case, the notification | |
164 | * iterator function called by the user returns | |
165 | * #BT_CTF_NOTIF_ITER_STATUS_AGAIN, and it is the user's | |
166 | * responsibility to make sure enough data becomes available | |
167 | * before calling the \em same notification iterator | |
168 | * function again to continue the decoding process. | |
169 | * - <b>#BT_CTF_NOTIF_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 notification | |
172 | * iterator function called by the user returns | |
173 | * #BT_CTF_NOTIF_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_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF on the \em following | |
181 | * call. | |
182 | * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR</b>: A fatal | |
183 | * error occured during this operation. In this case, the | |
184 | * notification iterator function called by the user returns | |
185 | * #BT_CTF_NOTIF_ITER_STATUS_ERROR. | |
186 | * | |
187 | * If #BT_CTF_NOTIF_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_ctf_notif_iter_medium_status (* request_bytes)( | |
198 | size_t request_sz, uint8_t **buffer_addr, | |
199 | size_t *buffer_sz, void *data); | |
200 | ||
9e0c8dbb JG |
201 | /** |
202 | * Repositions the underlying stream's position. | |
203 | * | |
204 | * This *optional* method repositions the underlying stream | |
205 | * to a given absolute or relative position, as indicated by | |
206 | * the whence directive. | |
207 | * | |
208 | * @param whence One of #bt_ctf_notif_iter_seek_whence values | |
209 | * @param offset Offset to use for the given directive | |
210 | * @param data User data | |
211 | * @returns One of #bt_ctf_notif_iter_medium_status values | |
212 | */ | |
213 | enum bt_ctf_notif_iter_medium_status (* seek)( | |
214 | enum bt_ctf_notif_iter_seek_whence whence, | |
215 | off_t offset, void *data); | |
216 | ||
e98a2d6e PP |
217 | /** |
218 | * Returns a stream instance (weak reference) for the given | |
219 | * stream class. | |
220 | * | |
221 | * This is called after a packet header is read, and the | |
222 | * corresponding stream class is found by the notification | |
223 | * iterator. | |
224 | * | |
b92735af PP |
225 | * @param stream_class Stream class of the stream to get |
226 | * @param stream_id Stream (instance) ID of the stream | |
227 | * to get (-1ULL if not available) | |
e98a2d6e PP |
228 | * @param data User data |
229 | * @returns Stream instance (weak reference) or | |
230 | * \c NULL on error | |
231 | */ | |
232 | struct bt_ctf_stream * (* get_stream)( | |
b92735af PP |
233 | struct bt_ctf_stream_class *stream_class, |
234 | uint64_t stream_id, void *data); | |
e98a2d6e PP |
235 | }; |
236 | ||
237 | /** CTF notification iterator. */ | |
238 | struct bt_ctf_notif_iter; | |
239 | ||
240 | // TODO: Replace by the real thing | |
241 | enum bt_ctf_notif_iter_notif_type { | |
242 | BT_CTF_NOTIF_ITER_NOTIF_NEW_PACKET, | |
243 | BT_CTF_NOTIF_ITER_NOTIF_END_OF_PACKET, | |
244 | BT_CTF_NOTIF_ITER_NOTIF_EVENT, | |
245 | }; | |
246 | ||
247 | struct bt_ctf_notif_iter_notif { | |
248 | enum bt_ctf_notif_iter_notif_type type; | |
249 | }; | |
250 | ||
251 | struct bt_ctf_notif_iter_notif_new_packet { | |
252 | struct bt_ctf_notif_iter_notif base; | |
253 | struct bt_ctf_packet *packet; | |
254 | }; | |
255 | ||
256 | struct bt_ctf_notif_iter_notif_end_of_packet { | |
257 | struct bt_ctf_notif_iter_notif base; | |
258 | struct bt_ctf_packet *packet; | |
259 | }; | |
260 | ||
261 | struct bt_ctf_notif_iter_notif_event { | |
262 | struct bt_ctf_notif_iter_notif base; | |
263 | struct bt_ctf_event *event; | |
264 | }; | |
265 | ||
e98a2d6e PP |
266 | /** |
267 | * Creates a CTF notification iterator. | |
268 | * | |
269 | * Upon successful completion, the reference count of \p trace is | |
270 | * incremented. | |
271 | * | |
272 | * @param trace Trace to read | |
273 | * @param max_request_sz Maximum buffer size, in bytes, to | |
274 | * request to | |
275 | * bt_ctf_notif_iter_medium_ops::request_bytes() | |
276 | * at a time | |
277 | * @param medops Medium operations | |
278 | * @param medops_data User data (passed to medium operations) | |
e98a2d6e PP |
279 | * @returns New CTF notification iterator on |
280 | * success, or \c NULL on error | |
281 | */ | |
2cf1d51e | 282 | BT_HIDDEN |
e98a2d6e PP |
283 | struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace, |
284 | size_t max_request_sz, struct bt_ctf_notif_iter_medium_ops medops, | |
55314f2a | 285 | void *medops_data); |
e98a2d6e PP |
286 | |
287 | /** | |
288 | * Destroys a CTF notification iterator, freeing all internal resources. | |
289 | * | |
290 | * The registered trace's reference count is decremented. | |
291 | * | |
292 | * @param notif_iter CTF notification iterator | |
293 | */ | |
2cf1d51e | 294 | BT_HIDDEN |
e98a2d6e PP |
295 | void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notif_iter); |
296 | ||
e98a2d6e PP |
297 | /** |
298 | * Returns the next notification from a CTF notification iterator. | |
299 | * | |
300 | * Upon successful completion, #BT_CTF_NOTIF_ITER_STATUS_OK is | |
301 | * returned, and the next notification is written to \p notif. | |
302 | * In this case, the caller is responsible for calling | |
303 | * bt_notification_put() on the returned notification. | |
304 | * | |
305 | * If this function returns #BT_CTF_NOTIF_ITER_STATUS_AGAIN, the caller | |
306 | * should make sure that data becomes available to its medium, and | |
307 | * call this function again, until another status is returned. | |
308 | * | |
309 | * @param notif_iter CTF notification iterator | |
0982a26d PP |
310 | * @param cc_prio_map Clock class priority map to use when |
311 | * creating an event notification | |
e98a2d6e PP |
312 | * @param notification Returned notification if the function's |
313 | * return value is #BT_CTF_NOTIF_ITER_STATUS_OK | |
314 | * @returns One of #bt_ctf_notif_iter_status values | |
315 | */ | |
2cf1d51e | 316 | BT_HIDDEN |
e98a2d6e | 317 | enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification( |
78586d8a | 318 | struct bt_ctf_notif_iter *notit, |
0982a26d | 319 | struct bt_clock_class_priority_map *cc_prio_map, |
78586d8a | 320 | struct bt_notification **notification); |
e98a2d6e | 321 | |
87187cbf PP |
322 | /** |
323 | * Returns the first packet header and context fields. This function | |
324 | * never needs to call the `get_stream()` medium operation because | |
325 | * it does not create packet or event objects. | |
326 | * | |
327 | * @param notif_iter CTF notification iterator | |
328 | * @param packet_header_field Packet header field (\c NULL if there's | |
329 | * no packet header field) | |
330 | * @param packet_context_field Packet context field (\c NULL if there's | |
331 | * no packet context field) | |
332 | * @returns One of #bt_ctf_notif_iter_status values | |
333 | */ | |
334 | BT_HIDDEN | |
335 | enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_packet_header_context_fields( | |
336 | struct bt_ctf_notif_iter *notit, | |
337 | struct bt_ctf_field **packet_header_field, | |
338 | struct bt_ctf_field **packet_context_field); | |
339 | ||
6de92955 PP |
340 | BT_HIDDEN |
341 | void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter *notit, | |
342 | void *medops_data); | |
343 | ||
9e0c8dbb JG |
344 | BT_HIDDEN |
345 | enum bt_ctf_notif_iter_status bt_ctf_notif_iter_seek( | |
346 | struct bt_ctf_notif_iter *notit, off_t offset); | |
347 | ||
348 | /* | |
349 | * Get the current packet's offset in bytes relative to the media's initial | |
350 | * position. | |
351 | */ | |
352 | BT_HIDDEN | |
353 | off_t bt_ctf_notif_iter_get_current_packet_offset( | |
354 | struct bt_ctf_notif_iter *notit); | |
355 | ||
356 | /* Get the current packet's size (in bits). */ | |
357 | BT_HIDDEN | |
358 | off_t bt_ctf_notif_iter_get_current_packet_size( | |
359 | struct bt_ctf_notif_iter *notit); | |
360 | ||
fdf0e7a0 PP |
361 | static inline |
362 | const char *bt_ctf_notif_iter_medium_status_string( | |
363 | enum bt_ctf_notif_iter_medium_status status) | |
364 | { | |
365 | switch (status) { | |
366 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF: | |
367 | return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF"; | |
368 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN: | |
369 | return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN"; | |
370 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL: | |
371 | return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL"; | |
372 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR: | |
373 | return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR"; | |
374 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK: | |
375 | return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK"; | |
376 | default: | |
377 | return "(unknown)"; | |
378 | } | |
379 | } | |
380 | ||
381 | static inline | |
382 | const char *bt_ctf_notif_iter_status_string( | |
383 | enum bt_ctf_notif_iter_status status) | |
384 | { | |
385 | switch (status) { | |
386 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
387 | return "BT_CTF_NOTIF_ITER_STATUS_EOF"; | |
388 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
389 | return "BT_CTF_NOTIF_ITER_STATUS_AGAIN"; | |
390 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: | |
391 | return "BT_CTF_NOTIF_ITER_STATUS_INVAL"; | |
392 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: | |
393 | return "BT_CTF_NOTIF_ITER_STATUS_ERROR"; | |
394 | case BT_CTF_NOTIF_ITER_STATUS_OK: | |
395 | return "BT_CTF_NOTIF_ITER_STATUS_OK"; | |
396 | default: | |
397 | return "(unknown)"; | |
398 | } | |
399 | } | |
400 | ||
e98a2d6e | 401 | #endif /* CTF_NOTIF_ITER_H */ |