Commit | Line | Data |
---|---|---|
6f3077a2 JD |
1 | /* |
2 | * iterator.c | |
3 | * | |
4 | * Babeltrace Library | |
5 | * | |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation | |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@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 | ||
21 | #include <stdlib.h> | |
22 | #include <babeltrace/babeltrace.h> | |
95d36295 | 23 | #include <babeltrace/context.h> |
08c22d05 | 24 | #include <babeltrace/context-internal.h> |
6f3077a2 | 25 | #include <babeltrace/iterator-internal.h> |
6204d33c | 26 | #include <babeltrace/iterator.h> |
6f3077a2 | 27 | #include <babeltrace/prio_heap.h> |
e4195791 | 28 | #include <babeltrace/ctf/metadata.h> |
9843982d | 29 | #include <babeltrace/ctf/events.h> |
90fcbacc | 30 | #include <inttypes.h> |
6f3077a2 | 31 | |
55c21ea5 JD |
32 | static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream, |
33 | const struct bt_iter_pos *begin_pos, | |
34 | unsigned long stream_id); | |
35 | ||
6f3077a2 JD |
36 | struct stream_saved_pos { |
37 | /* | |
38 | * Use file_stream pointer to check if the trace collection we | |
39 | * restore to match the one we saved from, for each stream. | |
40 | */ | |
41 | struct ctf_file_stream *file_stream; | |
42 | size_t cur_index; /* current index in packet index */ | |
43 | ssize_t offset; /* offset from base, in bits. EOF for end of file. */ | |
90fcbacc | 44 | uint64_t current_timestamp; |
6f3077a2 JD |
45 | }; |
46 | ||
e8c92a62 | 47 | struct bt_saved_pos { |
6f3077a2 JD |
48 | struct trace_collection *tc; |
49 | GArray *stream_saved_pos; /* Contains struct stream_saved_pos */ | |
50 | }; | |
51 | ||
52 | static int stream_read_event(struct ctf_file_stream *sin) | |
53 | { | |
54 | int ret; | |
55 | ||
56 | ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent); | |
57 | if (ret == EOF) | |
58 | return EOF; | |
59 | else if (ret) { | |
3394d22e | 60 | fprintf(stderr, "[error] Reading event failed.\n"); |
6f3077a2 JD |
61 | return ret; |
62 | } | |
63 | return 0; | |
64 | } | |
65 | ||
66 | /* | |
67 | * returns true if a < b, false otherwise. | |
68 | */ | |
69 | int stream_compare(void *a, void *b) | |
70 | { | |
71 | struct ctf_file_stream *s_a = a, *s_b = b; | |
72 | ||
73 | if (s_a->parent.timestamp < s_b->parent.timestamp) | |
74 | return 1; | |
75 | else | |
76 | return 0; | |
77 | } | |
78 | ||
90fcbacc JD |
79 | void bt_iter_free_pos(struct bt_iter_pos *iter_pos) |
80 | { | |
6cba487f MD |
81 | if (!iter_pos) |
82 | return; | |
83 | ||
84 | if (iter_pos->u.restore) { | |
85 | if (iter_pos->u.restore->stream_saved_pos) { | |
86 | g_array_free( | |
87 | iter_pos->u.restore->stream_saved_pos, | |
88 | TRUE); | |
90fcbacc | 89 | } |
6cba487f | 90 | g_free(iter_pos->u.restore); |
90fcbacc | 91 | } |
6cba487f | 92 | g_free(iter_pos); |
90fcbacc JD |
93 | } |
94 | ||
dc81689e JD |
95 | /* |
96 | * seek_file_stream_by_timestamp | |
97 | * | |
98 | * Browse a filestream by index, if an index contains the timestamp passed in | |
99 | * argument, seek inside the corresponding packet it until we find the event we | |
100 | * are looking for (either the exact timestamp or the event just after the | |
101 | * timestamp). | |
102 | * | |
e32cb1e4 MD |
103 | * Return 0 if the seek succeded, EOF if we didn't find any packet |
104 | * containing the timestamp, or a positive integer for error. | |
dc81689e JD |
105 | */ |
106 | static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs, | |
107 | uint64_t timestamp) | |
108 | { | |
109 | struct ctf_stream_pos *stream_pos; | |
110 | struct packet_index *index; | |
111 | int i, ret; | |
112 | ||
113 | stream_pos = &cfs->pos; | |
114 | for (i = 0; i < stream_pos->packet_index->len; i++) { | |
115 | index = &g_array_index(stream_pos->packet_index, | |
116 | struct packet_index, i); | |
e32cb1e4 | 117 | if (index->timestamp_end < timestamp) |
dc81689e JD |
118 | continue; |
119 | ||
20d0dcf9 | 120 | stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET); |
5d2a5af2 | 121 | do { |
dc81689e | 122 | ret = stream_read_event(cfs); |
e32cb1e4 | 123 | } while (cfs->parent.timestamp < timestamp && ret == 0); |
5d2a5af2 | 124 | |
e32cb1e4 MD |
125 | /* Can return either EOF, 0, or error (> 0). */ |
126 | return ret; | |
dc81689e | 127 | } |
e32cb1e4 MD |
128 | /* |
129 | * Cannot find the timestamp within the stream packets, return | |
130 | * EOF. | |
131 | */ | |
dc81689e JD |
132 | return EOF; |
133 | } | |
134 | ||
135 | /* | |
136 | * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with | |
137 | * the corresponding timestamp | |
138 | * | |
139 | * Return 0 on success. | |
140 | * If the timestamp is not part of any file stream, return EOF to inform the | |
e32cb1e4 MD |
141 | * user the timestamp is out of the scope. |
142 | * On other errors, return positive value. | |
dc81689e JD |
143 | */ |
144 | static int seek_ctf_trace_by_timestamp(struct ctf_trace *tin, | |
145 | uint64_t timestamp, struct ptr_heap *stream_heap) | |
146 | { | |
147 | int i, j, ret; | |
e32cb1e4 | 148 | int found = 0; |
dc81689e JD |
149 | |
150 | /* for each stream_class */ | |
151 | for (i = 0; i < tin->streams->len; i++) { | |
f380e105 | 152 | struct ctf_stream_declaration *stream_class; |
dc81689e JD |
153 | |
154 | stream_class = g_ptr_array_index(tin->streams, i); | |
e32cb1e4 MD |
155 | if (!stream_class) |
156 | continue; | |
dc81689e JD |
157 | /* for each file_stream */ |
158 | for (j = 0; j < stream_class->streams->len; j++) { | |
9e88d150 | 159 | struct ctf_stream_definition *stream; |
dc81689e JD |
160 | struct ctf_file_stream *cfs; |
161 | ||
162 | stream = g_ptr_array_index(stream_class->streams, j); | |
e32cb1e4 MD |
163 | if (!stream) |
164 | continue; | |
dc81689e JD |
165 | cfs = container_of(stream, struct ctf_file_stream, |
166 | parent); | |
167 | ret = seek_file_stream_by_timestamp(cfs, timestamp); | |
168 | if (ret == 0) { | |
169 | /* Add to heap */ | |
170 | ret = heap_insert(stream_heap, cfs); | |
e32cb1e4 MD |
171 | if (ret) { |
172 | /* Return positive error. */ | |
173 | return -ret; | |
174 | } | |
175 | found = 1; | |
176 | } else if (ret > 0) { | |
177 | /* | |
178 | * Error in seek (not EOF), failure. | |
179 | */ | |
180 | return ret; | |
dc81689e | 181 | } |
e32cb1e4 | 182 | /* on EOF just do not put stream into heap. */ |
dc81689e JD |
183 | } |
184 | } | |
185 | ||
e32cb1e4 | 186 | return found ? 0 : EOF; |
dc81689e JD |
187 | } |
188 | ||
90fcbacc JD |
189 | int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) |
190 | { | |
dc81689e | 191 | struct trace_collection *tc; |
90fcbacc JD |
192 | int i, ret; |
193 | ||
dc81689e JD |
194 | switch (iter_pos->type) { |
195 | case BT_SEEK_RESTORE: | |
196 | if (!iter_pos->u.restore) | |
7344374e | 197 | return -EINVAL; |
dc81689e | 198 | |
90fcbacc | 199 | heap_free(iter->stream_heap); |
90fcbacc JD |
200 | ret = heap_init(iter->stream_heap, 0, stream_compare); |
201 | if (ret < 0) | |
dc81689e | 202 | goto error; |
90fcbacc JD |
203 | |
204 | for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len; | |
205 | i++) { | |
206 | struct stream_saved_pos *saved_pos; | |
207 | struct ctf_stream_pos *stream_pos; | |
9e88d150 | 208 | struct ctf_stream_definition *stream; |
90fcbacc JD |
209 | |
210 | saved_pos = &g_array_index( | |
211 | iter_pos->u.restore->stream_saved_pos, | |
212 | struct stream_saved_pos, i); | |
213 | stream = &saved_pos->file_stream->parent; | |
214 | stream_pos = &saved_pos->file_stream->pos; | |
90fcbacc | 215 | |
06789ffd | 216 | stream_pos->packet_seek(&stream_pos->parent, |
20d0dcf9 | 217 | saved_pos->cur_index, SEEK_SET); |
90fcbacc JD |
218 | |
219 | /* | |
220 | * the timestamp needs to be restored after | |
06789ffd | 221 | * packet_seek, because this function resets |
90fcbacc JD |
222 | * the timestamp to the beginning of the packet |
223 | */ | |
224 | stream->timestamp = saved_pos->current_timestamp; | |
225 | stream_pos->offset = saved_pos->offset; | |
226 | stream_pos->last_offset = saved_pos->offset; | |
227 | ||
228 | stream->prev_timestamp = 0; | |
229 | stream->prev_timestamp_end = 0; | |
230 | stream->consumed = 0; | |
231 | ||
232 | printf_debug("restored to cur_index = %zd and " | |
233 | "offset = %zd, timestamp = %" PRIu64 "\n", | |
234 | stream_pos->cur_index, | |
235 | stream_pos->offset, stream->timestamp); | |
236 | ||
237 | stream_read_event(saved_pos->file_stream); | |
238 | ||
239 | /* Add to heap */ | |
240 | ret = heap_insert(iter->stream_heap, | |
241 | saved_pos->file_stream); | |
242 | if (ret) | |
243 | goto error; | |
244 | } | |
dc81689e JD |
245 | case BT_SEEK_TIME: |
246 | tc = iter->ctx->tc; | |
247 | ||
dc81689e JD |
248 | heap_free(iter->stream_heap); |
249 | ret = heap_init(iter->stream_heap, 0, stream_compare); | |
250 | if (ret < 0) | |
251 | goto error; | |
252 | ||
253 | /* for each trace in the trace_collection */ | |
254 | for (i = 0; i < tc->array->len; i++) { | |
255 | struct ctf_trace *tin; | |
256 | struct trace_descriptor *td_read; | |
257 | ||
258 | td_read = g_ptr_array_index(tc->array, i); | |
e32cb1e4 MD |
259 | if (!td_read) |
260 | continue; | |
dc81689e JD |
261 | tin = container_of(td_read, struct ctf_trace, parent); |
262 | ||
263 | ret = seek_ctf_trace_by_timestamp(tin, | |
264 | iter_pos->u.seek_time, | |
265 | iter->stream_heap); | |
e32cb1e4 MD |
266 | /* |
267 | * Positive errors are failure. Negative value | |
268 | * is EOF (for which we continue with other | |
269 | * traces). 0 is success. Note: on EOF, it just | |
270 | * means that no stream has been added to the | |
271 | * iterator for that trace, which is fine. | |
272 | */ | |
273 | if (ret != 0 && ret != EOF) | |
dc81689e JD |
274 | goto error; |
275 | } | |
276 | return 0; | |
55c21ea5 JD |
277 | case BT_SEEK_BEGIN: |
278 | tc = iter->ctx->tc; | |
279 | for (i = 0; i < tc->array->len; i++) { | |
280 | struct ctf_trace *tin; | |
281 | struct trace_descriptor *td_read; | |
282 | int stream_id; | |
283 | ||
284 | td_read = g_ptr_array_index(tc->array, i); | |
e32cb1e4 MD |
285 | if (!td_read) |
286 | continue; | |
55c21ea5 JD |
287 | tin = container_of(td_read, struct ctf_trace, parent); |
288 | ||
289 | /* Populate heap with each stream */ | |
290 | for (stream_id = 0; stream_id < tin->streams->len; | |
291 | stream_id++) { | |
f380e105 | 292 | struct ctf_stream_declaration *stream; |
55c21ea5 JD |
293 | int filenr; |
294 | ||
295 | stream = g_ptr_array_index(tin->streams, | |
296 | stream_id); | |
297 | if (!stream) | |
298 | continue; | |
299 | for (filenr = 0; filenr < stream->streams->len; | |
300 | filenr++) { | |
301 | struct ctf_file_stream *file_stream; | |
302 | file_stream = g_ptr_array_index( | |
303 | stream->streams, | |
304 | filenr); | |
e32cb1e4 MD |
305 | if (!file_stream) |
306 | continue; | |
55c21ea5 JD |
307 | ret = babeltrace_filestream_seek( |
308 | file_stream, iter_pos, | |
309 | stream_id); | |
e32cb1e4 MD |
310 | if (ret != 0 && ret != EOF) { |
311 | goto error; | |
312 | } | |
55c21ea5 JD |
313 | } |
314 | } | |
315 | } | |
316 | break; | |
dc81689e JD |
317 | default: |
318 | /* not implemented */ | |
7344374e | 319 | return -EINVAL; |
90fcbacc JD |
320 | } |
321 | ||
322 | return 0; | |
dc81689e | 323 | |
90fcbacc JD |
324 | error: |
325 | heap_free(iter->stream_heap); | |
dc81689e JD |
326 | if (heap_init(iter->stream_heap, 0, stream_compare) < 0) { |
327 | heap_free(iter->stream_heap); | |
328 | g_free(iter->stream_heap); | |
329 | iter->stream_heap = NULL; | |
330 | ret = -ENOMEM; | |
331 | } | |
332 | return ret; | |
90fcbacc JD |
333 | } |
334 | ||
335 | struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter) | |
336 | { | |
337 | struct bt_iter_pos *pos; | |
338 | struct trace_collection *tc = iter->ctx->tc; | |
339 | int i, stream_class_id, stream_id; | |
340 | ||
341 | pos = g_new0(struct bt_iter_pos, 1); | |
90fcbacc | 342 | pos->u.restore = g_new0(struct bt_saved_pos, 1); |
90fcbacc JD |
343 | pos->u.restore->tc = tc; |
344 | pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE, | |
345 | sizeof(struct stream_saved_pos)); | |
346 | if (!pos->u.restore->stream_saved_pos) | |
347 | goto error; | |
348 | ||
349 | for (i = 0; i < tc->array->len; i++) { | |
350 | struct ctf_trace *tin; | |
351 | struct trace_descriptor *td_read; | |
352 | ||
353 | td_read = g_ptr_array_index(tc->array, i); | |
e32cb1e4 MD |
354 | if (!td_read) |
355 | continue; | |
90fcbacc JD |
356 | tin = container_of(td_read, struct ctf_trace, parent); |
357 | ||
358 | for (stream_class_id = 0; stream_class_id < tin->streams->len; | |
359 | stream_class_id++) { | |
f380e105 | 360 | struct ctf_stream_declaration *stream_class; |
90fcbacc JD |
361 | |
362 | stream_class = g_ptr_array_index(tin->streams, | |
363 | stream_class_id); | |
e32cb1e4 MD |
364 | if (!stream_class) |
365 | continue; | |
90fcbacc JD |
366 | for (stream_id = 0; |
367 | stream_id < stream_class->streams->len; | |
368 | stream_id++) { | |
9e88d150 | 369 | struct ctf_stream_definition *stream; |
90fcbacc JD |
370 | struct ctf_file_stream *cfs; |
371 | struct stream_saved_pos saved_pos; | |
372 | ||
373 | stream = g_ptr_array_index( | |
374 | stream_class->streams, | |
375 | stream_id); | |
e32cb1e4 MD |
376 | if (!stream) |
377 | continue; | |
90fcbacc JD |
378 | cfs = container_of(stream, |
379 | struct ctf_file_stream, | |
380 | parent); | |
381 | ||
382 | saved_pos.file_stream = cfs; | |
383 | saved_pos.cur_index = cfs->pos.cur_index; | |
384 | ||
385 | /* | |
386 | * It is possible that an event was read during | |
387 | * the last restore, never consumed and its | |
388 | * position saved again. For this case, we | |
389 | * need to check if the event really was | |
390 | * consumed by the caller otherwise it is lost. | |
391 | */ | |
392 | if (stream->consumed) | |
393 | saved_pos.offset = cfs->pos.offset; | |
394 | else | |
395 | saved_pos.offset = cfs->pos.last_offset; | |
396 | ||
397 | saved_pos.current_timestamp = stream->timestamp; | |
398 | ||
399 | g_array_append_val( | |
400 | pos->u.restore->stream_saved_pos, | |
401 | saved_pos); | |
402 | ||
363402af | 403 | printf_debug("stream : %" PRIu64 ", cur_index : %zd, " |
90fcbacc JD |
404 | "offset : %zd, " |
405 | "timestamp = %" PRIu64 "\n", | |
406 | stream->stream_id, saved_pos.cur_index, | |
407 | saved_pos.offset, | |
408 | saved_pos.current_timestamp); | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
413 | return pos; | |
414 | ||
415 | error: | |
416 | return NULL; | |
417 | } | |
418 | ||
dc81689e JD |
419 | struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, |
420 | uint64_t timestamp) | |
421 | { | |
422 | struct bt_iter_pos *pos; | |
423 | ||
424 | pos = g_new0(struct bt_iter_pos, 1); | |
425 | pos->type = BT_SEEK_TIME; | |
426 | pos->u.seek_time = timestamp; | |
427 | return pos; | |
428 | } | |
429 | ||
6f3077a2 JD |
430 | /* |
431 | * babeltrace_filestream_seek: seek a filestream to given position. | |
432 | * | |
433 | * The stream_id parameter is only useful for BT_SEEK_RESTORE. | |
434 | */ | |
435 | static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream, | |
e8c92a62 | 436 | const struct bt_iter_pos *begin_pos, |
6f3077a2 JD |
437 | unsigned long stream_id) |
438 | { | |
439 | int ret = 0; | |
440 | ||
441 | switch (begin_pos->type) { | |
442 | case BT_SEEK_CUR: | |
443 | /* | |
444 | * just insert into the heap we should already know | |
445 | * the timestamps | |
446 | */ | |
447 | break; | |
448 | case BT_SEEK_BEGIN: | |
06789ffd | 449 | file_stream->pos.packet_seek(&file_stream->pos.parent, |
d6425aaf | 450 | 0, SEEK_SET); |
6f3077a2 JD |
451 | ret = stream_read_event(file_stream); |
452 | break; | |
453 | case BT_SEEK_TIME: | |
454 | case BT_SEEK_RESTORE: | |
455 | case BT_SEEK_END: | |
456 | default: | |
457 | assert(0); /* Not yet defined */ | |
458 | } | |
459 | ||
460 | return ret; | |
461 | } | |
462 | ||
463 | /* | |
e8c92a62 | 464 | * bt_iter_seek: seek iterator to given position. |
6f3077a2 | 465 | */ |
e8c92a62 JD |
466 | int bt_iter_seek(struct bt_iter *iter, |
467 | const struct bt_iter_pos *begin_pos) | |
6f3077a2 JD |
468 | { |
469 | int i, stream_id; | |
470 | int ret = 0; | |
95d36295 | 471 | struct trace_collection *tc = iter->ctx->tc; |
6f3077a2 JD |
472 | |
473 | for (i = 0; i < tc->array->len; i++) { | |
474 | struct ctf_trace *tin; | |
475 | struct trace_descriptor *td_read; | |
476 | ||
477 | td_read = g_ptr_array_index(tc->array, i); | |
e32cb1e4 MD |
478 | if (!td_read) |
479 | continue; | |
6f3077a2 JD |
480 | tin = container_of(td_read, struct ctf_trace, parent); |
481 | ||
482 | /* Populate heap with each stream */ | |
483 | for (stream_id = 0; stream_id < tin->streams->len; | |
484 | stream_id++) { | |
f380e105 | 485 | struct ctf_stream_declaration *stream; |
6f3077a2 JD |
486 | int filenr; |
487 | ||
488 | stream = g_ptr_array_index(tin->streams, stream_id); | |
e32cb1e4 MD |
489 | if (!stream) |
490 | continue; | |
6f3077a2 JD |
491 | for (filenr = 0; filenr < stream->streams->len; |
492 | filenr++) { | |
493 | struct ctf_file_stream *file_stream; | |
494 | ||
495 | file_stream = g_ptr_array_index(stream->streams, | |
496 | filenr); | |
e32cb1e4 MD |
497 | if (!file_stream) |
498 | continue; | |
6f3077a2 JD |
499 | ret = babeltrace_filestream_seek(file_stream, begin_pos, |
500 | stream_id); | |
501 | if (ret < 0) | |
502 | goto end; | |
503 | } | |
504 | } | |
505 | } | |
506 | end: | |
507 | return ret; | |
508 | } | |
509 | ||
e4195791 MD |
510 | int bt_iter_init(struct bt_iter *iter, |
511 | struct bt_context *ctx, | |
04ae3991 MD |
512 | const struct bt_iter_pos *begin_pos, |
513 | const struct bt_iter_pos *end_pos) | |
6f3077a2 JD |
514 | { |
515 | int i, stream_id; | |
516 | int ret = 0; | |
6f3077a2 | 517 | |
e003e871 JD |
518 | if (ctx->current_iterator) { |
519 | ret = -1; | |
520 | goto error_ctx; | |
521 | } | |
522 | ||
6f3077a2 | 523 | iter->stream_heap = g_new(struct ptr_heap, 1); |
6f3077a2 | 524 | iter->end_pos = end_pos; |
6cba487f | 525 | bt_context_get(ctx); |
95d36295 | 526 | iter->ctx = ctx; |
6f3077a2 JD |
527 | |
528 | ret = heap_init(iter->stream_heap, 0, stream_compare); | |
529 | if (ret < 0) | |
530 | goto error_heap_init; | |
531 | ||
95d36295 | 532 | for (i = 0; i < ctx->tc->array->len; i++) { |
6f3077a2 JD |
533 | struct ctf_trace *tin; |
534 | struct trace_descriptor *td_read; | |
535 | ||
95d36295 | 536 | td_read = g_ptr_array_index(ctx->tc->array, i); |
e32cb1e4 MD |
537 | if (!td_read) |
538 | continue; | |
6f3077a2 JD |
539 | tin = container_of(td_read, struct ctf_trace, parent); |
540 | ||
541 | /* Populate heap with each stream */ | |
542 | for (stream_id = 0; stream_id < tin->streams->len; | |
543 | stream_id++) { | |
f380e105 | 544 | struct ctf_stream_declaration *stream; |
6f3077a2 JD |
545 | int filenr; |
546 | ||
547 | stream = g_ptr_array_index(tin->streams, stream_id); | |
548 | if (!stream) | |
549 | continue; | |
550 | for (filenr = 0; filenr < stream->streams->len; | |
551 | filenr++) { | |
552 | struct ctf_file_stream *file_stream; | |
553 | ||
554 | file_stream = g_ptr_array_index(stream->streams, | |
555 | filenr); | |
e32cb1e4 MD |
556 | if (!file_stream) |
557 | continue; | |
6f3077a2 | 558 | if (begin_pos) { |
b42d4e4e JD |
559 | ret = babeltrace_filestream_seek( |
560 | file_stream, | |
561 | begin_pos, | |
6f3077a2 | 562 | stream_id); |
b42d4e4e JD |
563 | } else { |
564 | struct bt_iter_pos pos; | |
565 | pos.type = BT_SEEK_BEGIN; | |
566 | ret = babeltrace_filestream_seek( | |
567 | file_stream, &pos, | |
568 | stream_id); | |
569 | } | |
570 | if (ret == EOF) { | |
571 | ret = 0; | |
572 | continue; | |
573 | } else if (ret) { | |
574 | goto error; | |
6f3077a2 JD |
575 | } |
576 | /* Add to heap */ | |
577 | ret = heap_insert(iter->stream_heap, file_stream); | |
578 | if (ret) | |
579 | goto error; | |
580 | } | |
581 | } | |
582 | } | |
583 | ||
e003e871 | 584 | ctx->current_iterator = iter; |
e4195791 | 585 | return 0; |
6f3077a2 JD |
586 | |
587 | error: | |
588 | heap_free(iter->stream_heap); | |
589 | error_heap_init: | |
590 | g_free(iter->stream_heap); | |
e003e871 | 591 | error_ctx: |
e4195791 | 592 | return ret; |
6f3077a2 JD |
593 | } |
594 | ||
e4195791 | 595 | struct bt_iter *bt_iter_create(struct bt_context *ctx, |
04ae3991 MD |
596 | const struct bt_iter_pos *begin_pos, |
597 | const struct bt_iter_pos *end_pos) | |
e4195791 MD |
598 | { |
599 | struct bt_iter *iter; | |
600 | int ret; | |
601 | ||
602 | iter = g_new0(struct bt_iter, 1); | |
603 | ret = bt_iter_init(iter, ctx, begin_pos, end_pos); | |
604 | if (ret) { | |
605 | g_free(iter); | |
606 | return NULL; | |
607 | } | |
608 | return iter; | |
609 | } | |
610 | ||
611 | void bt_iter_fini(struct bt_iter *iter) | |
6f3077a2 | 612 | { |
dc81689e JD |
613 | if (iter->stream_heap) { |
614 | heap_free(iter->stream_heap); | |
615 | g_free(iter->stream_heap); | |
616 | } | |
e003e871 | 617 | iter->ctx->current_iterator = NULL; |
95d36295 | 618 | bt_context_put(iter->ctx); |
e4195791 | 619 | } |
95d36295 | 620 | |
e4195791 MD |
621 | void bt_iter_destroy(struct bt_iter *iter) |
622 | { | |
623 | bt_iter_fini(iter); | |
90fcbacc | 624 | g_free(iter); |
6f3077a2 JD |
625 | } |
626 | ||
e8c92a62 | 627 | int bt_iter_next(struct bt_iter *iter) |
6f3077a2 JD |
628 | { |
629 | struct ctf_file_stream *file_stream, *removed; | |
630 | int ret; | |
631 | ||
632 | file_stream = heap_maximum(iter->stream_heap); | |
633 | if (!file_stream) { | |
634 | /* end of file for all streams */ | |
635 | ret = 0; | |
636 | goto end; | |
637 | } | |
638 | ||
639 | ret = stream_read_event(file_stream); | |
640 | if (ret == EOF) { | |
641 | removed = heap_remove(iter->stream_heap); | |
642 | assert(removed == file_stream); | |
643 | ret = 0; | |
644 | goto end; | |
645 | } else if (ret) { | |
646 | goto end; | |
647 | } | |
648 | /* Reinsert the file stream into the heap, and rebalance. */ | |
649 | removed = heap_replace_max(iter->stream_heap, file_stream); | |
650 | assert(removed == file_stream); | |
651 | ||
652 | end: | |
653 | return ret; | |
654 | } |