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 | ||
dc81689e JD |
195 | switch (iter_pos->type) { |
196 | case BT_SEEK_RESTORE: | |
197 | if (!iter_pos->u.restore) | |
7344374e | 198 | return -EINVAL; |
dc81689e | 199 | |
90fcbacc | 200 | heap_free(iter->stream_heap); |
90fcbacc JD |
201 | ret = heap_init(iter->stream_heap, 0, stream_compare); |
202 | if (ret < 0) | |
bed54c92 | 203 | goto error_heap_init; |
90fcbacc JD |
204 | |
205 | for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len; | |
206 | i++) { | |
207 | struct stream_saved_pos *saved_pos; | |
208 | struct ctf_stream_pos *stream_pos; | |
9e88d150 | 209 | struct ctf_stream_definition *stream; |
90fcbacc JD |
210 | |
211 | saved_pos = &g_array_index( | |
212 | iter_pos->u.restore->stream_saved_pos, | |
213 | struct stream_saved_pos, i); | |
214 | stream = &saved_pos->file_stream->parent; | |
215 | stream_pos = &saved_pos->file_stream->pos; | |
90fcbacc | 216 | |
06789ffd | 217 | stream_pos->packet_seek(&stream_pos->parent, |
20d0dcf9 | 218 | saved_pos->cur_index, SEEK_SET); |
90fcbacc JD |
219 | |
220 | /* | |
221 | * the timestamp needs to be restored after | |
06789ffd | 222 | * packet_seek, because this function resets |
90fcbacc JD |
223 | * the timestamp to the beginning of the packet |
224 | */ | |
03798a93 JD |
225 | stream->real_timestamp = saved_pos->current_real_timestamp; |
226 | stream->cycles_timestamp = saved_pos->current_cycles_timestamp; | |
90fcbacc | 227 | stream_pos->offset = saved_pos->offset; |
3a25e036 | 228 | stream_pos->last_offset = LAST_OFFSET_POISON; |
90fcbacc | 229 | |
03798a93 JD |
230 | stream->prev_real_timestamp = 0; |
231 | stream->prev_real_timestamp_end = 0; | |
232 | stream->prev_cycles_timestamp = 0; | |
233 | stream->prev_cycles_timestamp_end = 0; | |
90fcbacc JD |
234 | |
235 | printf_debug("restored to cur_index = %zd and " | |
236 | "offset = %zd, timestamp = %" PRIu64 "\n", | |
237 | stream_pos->cur_index, | |
03798a93 | 238 | stream_pos->offset, stream->real_timestamp); |
90fcbacc JD |
239 | |
240 | stream_read_event(saved_pos->file_stream); | |
241 | ||
242 | /* Add to heap */ | |
243 | ret = heap_insert(iter->stream_heap, | |
244 | saved_pos->file_stream); | |
245 | if (ret) | |
246 | goto error; | |
247 | } | |
5acdc773 | 248 | return 0; |
dc81689e JD |
249 | case BT_SEEK_TIME: |
250 | tc = iter->ctx->tc; | |
251 | ||
dc81689e JD |
252 | heap_free(iter->stream_heap); |
253 | ret = heap_init(iter->stream_heap, 0, stream_compare); | |
254 | if (ret < 0) | |
bed54c92 | 255 | goto error_heap_init; |
dc81689e JD |
256 | |
257 | /* for each trace in the trace_collection */ | |
258 | for (i = 0; i < tc->array->len; i++) { | |
259 | struct ctf_trace *tin; | |
260 | struct trace_descriptor *td_read; | |
261 | ||
262 | td_read = g_ptr_array_index(tc->array, i); | |
e32cb1e4 MD |
263 | if (!td_read) |
264 | continue; | |
dc81689e JD |
265 | tin = container_of(td_read, struct ctf_trace, parent); |
266 | ||
267 | ret = seek_ctf_trace_by_timestamp(tin, | |
268 | iter_pos->u.seek_time, | |
269 | iter->stream_heap); | |
e32cb1e4 MD |
270 | /* |
271 | * Positive errors are failure. Negative value | |
272 | * is EOF (for which we continue with other | |
273 | * traces). 0 is success. Note: on EOF, it just | |
274 | * means that no stream has been added to the | |
275 | * iterator for that trace, which is fine. | |
276 | */ | |
277 | if (ret != 0 && ret != EOF) | |
dc81689e JD |
278 | goto error; |
279 | } | |
280 | return 0; | |
55c21ea5 JD |
281 | case BT_SEEK_BEGIN: |
282 | tc = iter->ctx->tc; | |
db8a4511 JD |
283 | heap_free(iter->stream_heap); |
284 | ret = heap_init(iter->stream_heap, 0, stream_compare); | |
285 | if (ret < 0) | |
bed54c92 | 286 | goto error_heap_init; |
db8a4511 | 287 | |
55c21ea5 JD |
288 | for (i = 0; i < tc->array->len; i++) { |
289 | struct ctf_trace *tin; | |
290 | struct trace_descriptor *td_read; | |
291 | int stream_id; | |
292 | ||
293 | td_read = g_ptr_array_index(tc->array, i); | |
e32cb1e4 MD |
294 | if (!td_read) |
295 | continue; | |
55c21ea5 JD |
296 | tin = container_of(td_read, struct ctf_trace, parent); |
297 | ||
298 | /* Populate heap with each stream */ | |
299 | for (stream_id = 0; stream_id < tin->streams->len; | |
300 | stream_id++) { | |
f380e105 | 301 | struct ctf_stream_declaration *stream; |
55c21ea5 JD |
302 | int filenr; |
303 | ||
304 | stream = g_ptr_array_index(tin->streams, | |
305 | stream_id); | |
306 | if (!stream) | |
307 | continue; | |
308 | for (filenr = 0; filenr < stream->streams->len; | |
309 | filenr++) { | |
310 | struct ctf_file_stream *file_stream; | |
311 | file_stream = g_ptr_array_index( | |
312 | stream->streams, | |
313 | filenr); | |
e32cb1e4 MD |
314 | if (!file_stream) |
315 | continue; | |
55c21ea5 JD |
316 | ret = babeltrace_filestream_seek( |
317 | file_stream, iter_pos, | |
318 | stream_id); | |
e32cb1e4 MD |
319 | if (ret != 0 && ret != EOF) { |
320 | goto error; | |
321 | } | |
db8a4511 JD |
322 | ret = heap_insert(iter->stream_heap, file_stream); |
323 | if (ret) | |
324 | goto error; | |
55c21ea5 JD |
325 | } |
326 | } | |
327 | } | |
328 | break; | |
dc81689e JD |
329 | default: |
330 | /* not implemented */ | |
7344374e | 331 | return -EINVAL; |
90fcbacc JD |
332 | } |
333 | ||
334 | return 0; | |
dc81689e | 335 | |
90fcbacc JD |
336 | error: |
337 | heap_free(iter->stream_heap); | |
bed54c92 | 338 | error_heap_init: |
dc81689e JD |
339 | if (heap_init(iter->stream_heap, 0, stream_compare) < 0) { |
340 | heap_free(iter->stream_heap); | |
341 | g_free(iter->stream_heap); | |
342 | iter->stream_heap = NULL; | |
343 | ret = -ENOMEM; | |
344 | } | |
bed54c92 | 345 | |
dc81689e | 346 | return ret; |
90fcbacc JD |
347 | } |
348 | ||
349 | struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter) | |
350 | { | |
351 | struct bt_iter_pos *pos; | |
352 | struct trace_collection *tc = iter->ctx->tc; | |
6fabd7af JD |
353 | struct ctf_file_stream *file_stream = NULL, *removed; |
354 | struct ptr_heap iter_heap_copy; | |
355 | int ret; | |
90fcbacc JD |
356 | |
357 | pos = g_new0(struct bt_iter_pos, 1); | |
5acdc773 | 358 | pos->type = BT_SEEK_RESTORE; |
90fcbacc | 359 | pos->u.restore = g_new0(struct bt_saved_pos, 1); |
90fcbacc JD |
360 | pos->u.restore->tc = tc; |
361 | pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE, | |
362 | sizeof(struct stream_saved_pos)); | |
363 | if (!pos->u.restore->stream_saved_pos) | |
364 | goto error; | |
365 | ||
6fabd7af JD |
366 | ret = heap_copy(&iter_heap_copy, iter->stream_heap); |
367 | if (ret < 0) | |
368 | goto error_heap; | |
90fcbacc | 369 | |
6fabd7af JD |
370 | /* iterate over each stream in the heap */ |
371 | file_stream = heap_maximum(&iter_heap_copy); | |
372 | while (file_stream != NULL) { | |
373 | struct stream_saved_pos saved_pos; | |
90fcbacc | 374 | |
6fabd7af JD |
375 | assert(file_stream->pos.last_offset != LAST_OFFSET_POISON); |
376 | saved_pos.offset = file_stream->pos.last_offset; | |
377 | saved_pos.file_stream = file_stream; | |
378 | saved_pos.cur_index = file_stream->pos.cur_index; | |
90fcbacc | 379 | |
03798a93 JD |
380 | saved_pos.current_real_timestamp = file_stream->parent.real_timestamp; |
381 | saved_pos.current_cycles_timestamp = file_stream->parent.cycles_timestamp; | |
90fcbacc | 382 | |
6fabd7af JD |
383 | g_array_append_val( |
384 | pos->u.restore->stream_saved_pos, | |
385 | saved_pos); | |
90fcbacc | 386 | |
6fabd7af JD |
387 | printf_debug("stream : %" PRIu64 ", cur_index : %zd, " |
388 | "offset : %zd, " | |
389 | "timestamp = %" PRIu64 "\n", | |
390 | file_stream->parent.stream_id, | |
391 | saved_pos.cur_index, saved_pos.offset, | |
03798a93 | 392 | saved_pos.current_real_timestamp); |
6fabd7af JD |
393 | |
394 | /* remove the stream from the heap copy */ | |
395 | removed = heap_remove(&iter_heap_copy); | |
396 | assert(removed == file_stream); | |
397 | ||
398 | file_stream = heap_maximum(&iter_heap_copy); | |
399 | } | |
400 | heap_free(&iter_heap_copy); | |
90fcbacc JD |
401 | return pos; |
402 | ||
6fabd7af JD |
403 | error_heap: |
404 | g_array_free(pos->u.restore->stream_saved_pos, TRUE); | |
90fcbacc | 405 | error: |
6fabd7af | 406 | g_free(pos); |
90fcbacc JD |
407 | return NULL; |
408 | } | |
409 | ||
dc81689e JD |
410 | struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, |
411 | uint64_t timestamp) | |
412 | { | |
413 | struct bt_iter_pos *pos; | |
414 | ||
415 | pos = g_new0(struct bt_iter_pos, 1); | |
416 | pos->type = BT_SEEK_TIME; | |
417 | pos->u.seek_time = timestamp; | |
418 | return pos; | |
419 | } | |
420 | ||
6f3077a2 JD |
421 | /* |
422 | * babeltrace_filestream_seek: seek a filestream to given position. | |
423 | * | |
424 | * The stream_id parameter is only useful for BT_SEEK_RESTORE. | |
425 | */ | |
426 | static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream, | |
e8c92a62 | 427 | const struct bt_iter_pos *begin_pos, |
6f3077a2 JD |
428 | unsigned long stream_id) |
429 | { | |
430 | int ret = 0; | |
431 | ||
432 | switch (begin_pos->type) { | |
433 | case BT_SEEK_CUR: | |
434 | /* | |
435 | * just insert into the heap we should already know | |
436 | * the timestamps | |
437 | */ | |
438 | break; | |
439 | case BT_SEEK_BEGIN: | |
06789ffd | 440 | file_stream->pos.packet_seek(&file_stream->pos.parent, |
d6425aaf | 441 | 0, SEEK_SET); |
6f3077a2 JD |
442 | ret = stream_read_event(file_stream); |
443 | break; | |
444 | case BT_SEEK_TIME: | |
445 | case BT_SEEK_RESTORE: | |
446 | case BT_SEEK_END: | |
447 | default: | |
448 | assert(0); /* Not yet defined */ | |
449 | } | |
450 | ||
451 | return ret; | |
452 | } | |
453 | ||
e4195791 MD |
454 | int bt_iter_init(struct bt_iter *iter, |
455 | struct bt_context *ctx, | |
04ae3991 MD |
456 | const struct bt_iter_pos *begin_pos, |
457 | const struct bt_iter_pos *end_pos) | |
6f3077a2 JD |
458 | { |
459 | int i, stream_id; | |
460 | int ret = 0; | |
6f3077a2 | 461 | |
e003e871 JD |
462 | if (ctx->current_iterator) { |
463 | ret = -1; | |
464 | goto error_ctx; | |
465 | } | |
466 | ||
6f3077a2 | 467 | iter->stream_heap = g_new(struct ptr_heap, 1); |
6f3077a2 | 468 | iter->end_pos = end_pos; |
6cba487f | 469 | bt_context_get(ctx); |
95d36295 | 470 | iter->ctx = ctx; |
6f3077a2 JD |
471 | |
472 | ret = heap_init(iter->stream_heap, 0, stream_compare); | |
473 | if (ret < 0) | |
474 | goto error_heap_init; | |
475 | ||
95d36295 | 476 | for (i = 0; i < ctx->tc->array->len; i++) { |
6f3077a2 JD |
477 | struct ctf_trace *tin; |
478 | struct trace_descriptor *td_read; | |
479 | ||
95d36295 | 480 | td_read = g_ptr_array_index(ctx->tc->array, i); |
e32cb1e4 MD |
481 | if (!td_read) |
482 | continue; | |
6f3077a2 JD |
483 | tin = container_of(td_read, struct ctf_trace, parent); |
484 | ||
485 | /* Populate heap with each stream */ | |
486 | for (stream_id = 0; stream_id < tin->streams->len; | |
487 | stream_id++) { | |
f380e105 | 488 | struct ctf_stream_declaration *stream; |
6f3077a2 JD |
489 | int filenr; |
490 | ||
491 | stream = g_ptr_array_index(tin->streams, stream_id); | |
492 | if (!stream) | |
493 | continue; | |
494 | for (filenr = 0; filenr < stream->streams->len; | |
495 | filenr++) { | |
496 | struct ctf_file_stream *file_stream; | |
497 | ||
498 | file_stream = g_ptr_array_index(stream->streams, | |
499 | filenr); | |
e32cb1e4 MD |
500 | if (!file_stream) |
501 | continue; | |
6f3077a2 | 502 | if (begin_pos) { |
b42d4e4e JD |
503 | ret = babeltrace_filestream_seek( |
504 | file_stream, | |
505 | begin_pos, | |
6f3077a2 | 506 | stream_id); |
b42d4e4e JD |
507 | } else { |
508 | struct bt_iter_pos pos; | |
509 | pos.type = BT_SEEK_BEGIN; | |
510 | ret = babeltrace_filestream_seek( | |
511 | file_stream, &pos, | |
512 | stream_id); | |
513 | } | |
514 | if (ret == EOF) { | |
515 | ret = 0; | |
516 | continue; | |
517 | } else if (ret) { | |
518 | goto error; | |
6f3077a2 JD |
519 | } |
520 | /* Add to heap */ | |
521 | ret = heap_insert(iter->stream_heap, file_stream); | |
522 | if (ret) | |
523 | goto error; | |
524 | } | |
525 | } | |
526 | } | |
527 | ||
e003e871 | 528 | ctx->current_iterator = iter; |
e4195791 | 529 | return 0; |
6f3077a2 JD |
530 | |
531 | error: | |
532 | heap_free(iter->stream_heap); | |
533 | error_heap_init: | |
534 | g_free(iter->stream_heap); | |
bed54c92 | 535 | iter->stream_heap = NULL; |
e003e871 | 536 | error_ctx: |
e4195791 | 537 | return ret; |
6f3077a2 JD |
538 | } |
539 | ||
e4195791 | 540 | struct bt_iter *bt_iter_create(struct bt_context *ctx, |
04ae3991 MD |
541 | const struct bt_iter_pos *begin_pos, |
542 | const struct bt_iter_pos *end_pos) | |
e4195791 MD |
543 | { |
544 | struct bt_iter *iter; | |
545 | int ret; | |
546 | ||
547 | iter = g_new0(struct bt_iter, 1); | |
548 | ret = bt_iter_init(iter, ctx, begin_pos, end_pos); | |
549 | if (ret) { | |
550 | g_free(iter); | |
551 | return NULL; | |
552 | } | |
553 | return iter; | |
554 | } | |
555 | ||
556 | void bt_iter_fini(struct bt_iter *iter) | |
6f3077a2 | 557 | { |
dc81689e JD |
558 | if (iter->stream_heap) { |
559 | heap_free(iter->stream_heap); | |
560 | g_free(iter->stream_heap); | |
561 | } | |
e003e871 | 562 | iter->ctx->current_iterator = NULL; |
95d36295 | 563 | bt_context_put(iter->ctx); |
e4195791 | 564 | } |
95d36295 | 565 | |
e4195791 MD |
566 | void bt_iter_destroy(struct bt_iter *iter) |
567 | { | |
568 | bt_iter_fini(iter); | |
90fcbacc | 569 | g_free(iter); |
6f3077a2 JD |
570 | } |
571 | ||
e8c92a62 | 572 | int bt_iter_next(struct bt_iter *iter) |
6f3077a2 JD |
573 | { |
574 | struct ctf_file_stream *file_stream, *removed; | |
575 | int ret; | |
576 | ||
577 | file_stream = heap_maximum(iter->stream_heap); | |
578 | if (!file_stream) { | |
579 | /* end of file for all streams */ | |
580 | ret = 0; | |
581 | goto end; | |
582 | } | |
583 | ||
584 | ret = stream_read_event(file_stream); | |
585 | if (ret == EOF) { | |
586 | removed = heap_remove(iter->stream_heap); | |
587 | assert(removed == file_stream); | |
588 | ret = 0; | |
589 | goto end; | |
590 | } else if (ret) { | |
591 | goto end; | |
592 | } | |
593 | /* Reinsert the file stream into the heap, and rebalance. */ | |
594 | removed = heap_replace_max(iter->stream_heap, file_stream); | |
595 | assert(removed == file_stream); | |
596 | ||
597 | end: | |
598 | return ret; | |
599 | } |