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