Python-bindings: Move declaration bindings out of the _Definition class
[babeltrace.git] / lib / iterator.c
... / ...
CommitLineData
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 * 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 <stdlib.h>
30#include <babeltrace/babeltrace.h>
31#include <babeltrace/context.h>
32#include <babeltrace/context-internal.h>
33#include <babeltrace/iterator-internal.h>
34#include <babeltrace/iterator.h>
35#include <babeltrace/prio_heap.h>
36#include <babeltrace/ctf/metadata.h>
37#include <babeltrace/ctf/events.h>
38#include <inttypes.h>
39
40static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
41 const struct bt_iter_pos *begin_pos,
42 unsigned long stream_id);
43
44struct stream_saved_pos {
45 /*
46 * Use file_stream pointer to check if the trace collection we
47 * restore to match the one we saved from, for each stream.
48 */
49 struct ctf_file_stream *file_stream;
50 size_t cur_index; /* current index in packet index */
51 ssize_t offset; /* offset from base, in bits. EOF for end of file. */
52 uint64_t current_real_timestamp;
53 uint64_t current_cycles_timestamp;
54};
55
56struct bt_saved_pos {
57 struct trace_collection *tc;
58 GArray *stream_saved_pos; /* Contains struct stream_saved_pos */
59};
60
61static int stream_read_event(struct ctf_file_stream *sin)
62{
63 int ret;
64
65 ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent);
66 if (ret == EOF)
67 return EOF;
68 else if (ret) {
69 fprintf(stderr, "[error] Reading event failed.\n");
70 return ret;
71 }
72 return 0;
73}
74
75/*
76 * Return true if a < b, false otherwise.
77 * If time stamps are exactly the same, compare by stream path. This
78 * ensures we get the same result between runs on the same trace
79 * collection on different environments.
80 * The result will be random for memory-mapped traces since there is no
81 * fixed path leading to those (they have empty path string).
82 */
83static int stream_compare(void *a, void *b)
84{
85 struct ctf_file_stream *s_a = a, *s_b = b;
86
87 if (s_a->parent.real_timestamp < s_b->parent.real_timestamp) {
88 return 1;
89 } else if (likely(s_a->parent.real_timestamp > s_b->parent.real_timestamp)) {
90 return 0;
91 } else {
92 return strcmp(s_a->parent.path, s_b->parent.path);
93 }
94}
95
96void bt_iter_free_pos(struct bt_iter_pos *iter_pos)
97{
98 if (!iter_pos)
99 return;
100
101 if (iter_pos->type == BT_SEEK_RESTORE && iter_pos->u.restore) {
102 if (iter_pos->u.restore->stream_saved_pos) {
103 g_array_free(
104 iter_pos->u.restore->stream_saved_pos,
105 TRUE);
106 }
107 g_free(iter_pos->u.restore);
108 }
109 g_free(iter_pos);
110}
111
112/*
113 * seek_file_stream_by_timestamp
114 *
115 * Browse a filestream by index, if an index contains the timestamp passed in
116 * argument, seek inside the corresponding packet it until we find the event we
117 * are looking for (either the exact timestamp or the event just after the
118 * timestamp).
119 *
120 * Return 0 if the seek succeded, EOF if we didn't find any packet
121 * containing the timestamp, or a positive integer for error.
122 *
123 * TODO: this should be turned into a binary search! It is currently
124 * doing a linear search in the packets. This is a O(n) operation on a
125 * very frequent code path.
126 */
127static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs,
128 uint64_t timestamp)
129{
130 struct ctf_stream_pos *stream_pos;
131 struct packet_index *index;
132 int i, ret;
133
134 stream_pos = &cfs->pos;
135 for (i = 0; i < stream_pos->packet_real_index->len; i++) {
136 index = &g_array_index(stream_pos->packet_real_index,
137 struct packet_index, i);
138 if (index->timestamp_end < timestamp)
139 continue;
140
141 stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET);
142 do {
143 ret = stream_read_event(cfs);
144 } while (cfs->parent.real_timestamp < timestamp && ret == 0);
145
146 /* Can return either EOF, 0, or error (> 0). */
147 return ret;
148 }
149 /*
150 * Cannot find the timestamp within the stream packets, return
151 * EOF.
152 */
153 return EOF;
154}
155
156/*
157 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
158 * the corresponding timestamp
159 *
160 * Return 0 on success.
161 * If the timestamp is not part of any file stream, return EOF to inform the
162 * user the timestamp is out of the scope.
163 * On other errors, return positive value.
164 */
165static int seek_ctf_trace_by_timestamp(struct ctf_trace *tin,
166 uint64_t timestamp, struct ptr_heap *stream_heap)
167{
168 int i, j, ret;
169 int found = 0;
170
171 /* for each stream_class */
172 for (i = 0; i < tin->streams->len; i++) {
173 struct ctf_stream_declaration *stream_class;
174
175 stream_class = g_ptr_array_index(tin->streams, i);
176 if (!stream_class)
177 continue;
178 /* for each file_stream */
179 for (j = 0; j < stream_class->streams->len; j++) {
180 struct ctf_stream_definition *stream;
181 struct ctf_file_stream *cfs;
182
183 stream = g_ptr_array_index(stream_class->streams, j);
184 if (!stream)
185 continue;
186 cfs = container_of(stream, struct ctf_file_stream,
187 parent);
188 ret = seek_file_stream_by_timestamp(cfs, timestamp);
189 if (ret == 0) {
190 /* Add to heap */
191 ret = bt_heap_insert(stream_heap, cfs);
192 if (ret) {
193 /* Return positive error. */
194 return -ret;
195 }
196 found = 1;
197 } else if (ret > 0) {
198 /*
199 * Error in seek (not EOF), failure.
200 */
201 return ret;
202 }
203 /* on EOF just do not put stream into heap. */
204 }
205 }
206
207 return found ? 0 : EOF;
208}
209
210/*
211 * Find timestamp of last event in the stream.
212 *
213 * Return value: 0 if OK, positive error value on error, EOF if no
214 * events were found.
215 */
216static int find_max_timestamp_ctf_file_stream(struct ctf_file_stream *cfs,
217 uint64_t *timestamp_end)
218{
219 int ret, count = 0, i;
220 uint64_t timestamp = 0;
221 struct ctf_stream_pos *stream_pos;
222
223 stream_pos = &cfs->pos;
224 /*
225 * We start by the last packet, and iterate backwards until we
226 * either find at least one event, or we reach the first packet
227 * (some packets can be empty).
228 */
229 for (i = stream_pos->packet_real_index->len - 1; i >= 0; i--) {
230 stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET);
231 count = 0;
232 /* read each event until we reach the end of the stream */
233 do {
234 ret = stream_read_event(cfs);
235 if (ret == 0) {
236 count++;
237 timestamp = cfs->parent.real_timestamp;
238 }
239 } while (ret == 0);
240
241 /* Error */
242 if (ret > 0)
243 goto end;
244 assert(ret == EOF);
245 if (count)
246 break;
247 }
248
249 if (count) {
250 *timestamp_end = timestamp;
251 ret = 0;
252 } else {
253 /* Return EOF if no events were found */
254 ret = EOF;
255 }
256end:
257 return ret;
258}
259
260/*
261 * Find the stream within a stream class that contains the event with
262 * the largest timestamp, and save that timestamp.
263 *
264 * Return 0 if OK, EOF if no events were found in the streams, or
265 * positive value on error.
266 */
267static int find_max_timestamp_ctf_stream_class(
268 struct ctf_stream_declaration *stream_class,
269 struct ctf_file_stream **cfsp,
270 uint64_t *max_timestamp)
271{
272 int ret = EOF, i, found = 0;
273
274 for (i = 0; i < stream_class->streams->len; i++) {
275 struct ctf_stream_definition *stream;
276 struct ctf_file_stream *cfs;
277 uint64_t current_max_ts = 0;
278
279 stream = g_ptr_array_index(stream_class->streams, i);
280 if (!stream)
281 continue;
282 cfs = container_of(stream, struct ctf_file_stream, parent);
283 ret = find_max_timestamp_ctf_file_stream(cfs, &current_max_ts);
284 if (ret == EOF)
285 continue;
286 if (ret != 0)
287 break;
288 if (current_max_ts >= *max_timestamp) {
289 *max_timestamp = current_max_ts;
290 *cfsp = cfs;
291 found = 1;
292 }
293 }
294 assert(ret >= 0 || ret == EOF);
295 if (found) {
296 return 0;
297 }
298 return ret;
299}
300
301/*
302 * seek_last_ctf_trace_collection: seek trace collection to last event.
303 *
304 * Return 0 if OK, EOF if no events were found, or positive error value
305 * on error.
306 */
307static int seek_last_ctf_trace_collection(struct trace_collection *tc,
308 struct ctf_file_stream **cfsp)
309{
310 int i, j, ret;
311 int found = 0;
312 uint64_t max_timestamp = 0;
313
314 if (!tc)
315 return 1;
316
317 /* For each trace in the trace_collection */
318 for (i = 0; i < tc->array->len; i++) {
319 struct ctf_trace *tin;
320 struct bt_trace_descriptor *td_read;
321
322 td_read = g_ptr_array_index(tc->array, i);
323 if (!td_read)
324 continue;
325 tin = container_of(td_read, struct ctf_trace, parent);
326 /* For each stream_class in the trace */
327 for (j = 0; j < tin->streams->len; j++) {
328 struct ctf_stream_declaration *stream_class;
329
330 stream_class = g_ptr_array_index(tin->streams, j);
331 if (!stream_class)
332 continue;
333 ret = find_max_timestamp_ctf_stream_class(stream_class,
334 cfsp, &max_timestamp);
335 if (ret > 0)
336 goto end;
337 if (ret == 0)
338 found = 1;
339 assert(ret == EOF || ret == 0);
340 }
341 }
342 /*
343 * Now we know in which file stream the last event is located,
344 * and we know its timestamp.
345 */
346 if (!found) {
347 ret = EOF;
348 } else {
349 ret = seek_file_stream_by_timestamp(*cfsp, max_timestamp);
350 assert(ret == 0);
351 }
352end:
353 return ret;
354}
355
356int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
357{
358 struct trace_collection *tc;
359 int i, ret;
360
361 if (!iter || !iter_pos)
362 return -EINVAL;
363
364 switch (iter_pos->type) {
365 case BT_SEEK_RESTORE:
366 if (!iter_pos->u.restore)
367 return -EINVAL;
368
369 bt_heap_free(iter->stream_heap);
370 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
371 if (ret < 0)
372 goto error_heap_init;
373
374 for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len;
375 i++) {
376 struct stream_saved_pos *saved_pos;
377 struct ctf_stream_pos *stream_pos;
378 struct ctf_stream_definition *stream;
379
380 saved_pos = &g_array_index(
381 iter_pos->u.restore->stream_saved_pos,
382 struct stream_saved_pos, i);
383 stream = &saved_pos->file_stream->parent;
384 stream_pos = &saved_pos->file_stream->pos;
385
386 stream_pos->packet_seek(&stream_pos->parent,
387 saved_pos->cur_index, SEEK_SET);
388
389 /*
390 * the timestamp needs to be restored after
391 * packet_seek, because this function resets
392 * the timestamp to the beginning of the packet
393 */
394 stream->real_timestamp = saved_pos->current_real_timestamp;
395 stream->cycles_timestamp = saved_pos->current_cycles_timestamp;
396 stream_pos->offset = saved_pos->offset;
397 stream_pos->last_offset = LAST_OFFSET_POISON;
398
399 stream->prev_real_timestamp = 0;
400 stream->prev_real_timestamp_end = 0;
401 stream->prev_cycles_timestamp = 0;
402 stream->prev_cycles_timestamp_end = 0;
403
404 printf_debug("restored to cur_index = %" PRId64 " and "
405 "offset = %" PRId64 ", timestamp = %" PRIu64 "\n",
406 stream_pos->cur_index,
407 stream_pos->offset, stream->real_timestamp);
408
409 ret = stream_read_event(saved_pos->file_stream);
410 if (ret != 0) {
411 goto error;
412 }
413
414 /* Add to heap */
415 ret = bt_heap_insert(iter->stream_heap,
416 saved_pos->file_stream);
417 if (ret)
418 goto error;
419 }
420 return 0;
421 case BT_SEEK_TIME:
422 tc = iter->ctx->tc;
423
424 bt_heap_free(iter->stream_heap);
425 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
426 if (ret < 0)
427 goto error_heap_init;
428
429 /* for each trace in the trace_collection */
430 for (i = 0; i < tc->array->len; i++) {
431 struct ctf_trace *tin;
432 struct bt_trace_descriptor *td_read;
433
434 td_read = g_ptr_array_index(tc->array, i);
435 if (!td_read)
436 continue;
437 tin = container_of(td_read, struct ctf_trace, parent);
438
439 ret = seek_ctf_trace_by_timestamp(tin,
440 iter_pos->u.seek_time,
441 iter->stream_heap);
442 /*
443 * Positive errors are failure. Negative value
444 * is EOF (for which we continue with other
445 * traces). 0 is success. Note: on EOF, it just
446 * means that no stream has been added to the
447 * iterator for that trace, which is fine.
448 */
449 if (ret != 0 && ret != EOF)
450 goto error;
451 }
452 return 0;
453 case BT_SEEK_BEGIN:
454 tc = iter->ctx->tc;
455 bt_heap_free(iter->stream_heap);
456 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
457 if (ret < 0)
458 goto error_heap_init;
459
460 for (i = 0; i < tc->array->len; i++) {
461 struct ctf_trace *tin;
462 struct bt_trace_descriptor *td_read;
463 int stream_id;
464
465 td_read = g_ptr_array_index(tc->array, i);
466 if (!td_read)
467 continue;
468 tin = container_of(td_read, struct ctf_trace, parent);
469
470 /* Populate heap with each stream */
471 for (stream_id = 0; stream_id < tin->streams->len;
472 stream_id++) {
473 struct ctf_stream_declaration *stream;
474 int filenr;
475
476 stream = g_ptr_array_index(tin->streams,
477 stream_id);
478 if (!stream)
479 continue;
480 for (filenr = 0; filenr < stream->streams->len;
481 filenr++) {
482 struct ctf_file_stream *file_stream;
483 file_stream = g_ptr_array_index(
484 stream->streams,
485 filenr);
486 if (!file_stream)
487 continue;
488 ret = babeltrace_filestream_seek(
489 file_stream, iter_pos,
490 stream_id);
491 if (ret != 0 && ret != EOF) {
492 goto error;
493 }
494 if (ret == EOF) {
495 /* Do not add EOF streams */
496 continue;
497 }
498 ret = bt_heap_insert(iter->stream_heap, file_stream);
499 if (ret)
500 goto error;
501 }
502 }
503 }
504 break;
505 case BT_SEEK_LAST:
506 {
507 struct ctf_file_stream *cfs = NULL;
508
509 tc = iter->ctx->tc;
510 ret = seek_last_ctf_trace_collection(tc, &cfs);
511 if (ret != 0 || !cfs)
512 goto error;
513 /* remove all streams from the heap */
514 bt_heap_free(iter->stream_heap);
515 /* Create a new empty heap */
516 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
517 if (ret < 0)
518 goto error;
519 /* Insert the stream that contains the last event */
520 ret = bt_heap_insert(iter->stream_heap, cfs);
521 if (ret)
522 goto error;
523 break;
524 }
525 default:
526 /* not implemented */
527 return -EINVAL;
528 }
529
530 return 0;
531
532error:
533 bt_heap_free(iter->stream_heap);
534error_heap_init:
535 if (bt_heap_init(iter->stream_heap, 0, stream_compare) < 0) {
536 bt_heap_free(iter->stream_heap);
537 g_free(iter->stream_heap);
538 iter->stream_heap = NULL;
539 ret = -ENOMEM;
540 }
541
542 return ret;
543}
544
545struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
546{
547 struct bt_iter_pos *pos;
548 struct trace_collection *tc;
549 struct ctf_file_stream *file_stream = NULL, *removed;
550 struct ptr_heap iter_heap_copy;
551 int ret;
552
553 if (!iter)
554 return NULL;
555
556 tc = iter->ctx->tc;
557 pos = g_new0(struct bt_iter_pos, 1);
558 pos->type = BT_SEEK_RESTORE;
559 pos->u.restore = g_new0(struct bt_saved_pos, 1);
560 pos->u.restore->tc = tc;
561 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
562 sizeof(struct stream_saved_pos));
563 if (!pos->u.restore->stream_saved_pos)
564 goto error;
565
566 ret = bt_heap_copy(&iter_heap_copy, iter->stream_heap);
567 if (ret < 0)
568 goto error_heap;
569
570 /* iterate over each stream in the heap */
571 file_stream = bt_heap_maximum(&iter_heap_copy);
572 while (file_stream != NULL) {
573 struct stream_saved_pos saved_pos;
574
575 assert(file_stream->pos.last_offset != LAST_OFFSET_POISON);
576 saved_pos.offset = file_stream->pos.last_offset;
577 saved_pos.file_stream = file_stream;
578 saved_pos.cur_index = file_stream->pos.cur_index;
579
580 saved_pos.current_real_timestamp = file_stream->parent.real_timestamp;
581 saved_pos.current_cycles_timestamp = file_stream->parent.cycles_timestamp;
582
583 g_array_append_val(
584 pos->u.restore->stream_saved_pos,
585 saved_pos);
586
587 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
588 "offset : %zd, "
589 "timestamp = %" PRIu64 "\n",
590 file_stream->parent.stream_id,
591 saved_pos.cur_index, saved_pos.offset,
592 saved_pos.current_real_timestamp);
593
594 /* remove the stream from the heap copy */
595 removed = bt_heap_remove(&iter_heap_copy);
596 assert(removed == file_stream);
597
598 file_stream = bt_heap_maximum(&iter_heap_copy);
599 }
600 bt_heap_free(&iter_heap_copy);
601 return pos;
602
603error_heap:
604 g_array_free(pos->u.restore->stream_saved_pos, TRUE);
605error:
606 g_free(pos);
607 return NULL;
608}
609
610struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
611 uint64_t timestamp)
612{
613 struct bt_iter_pos *pos;
614
615 if (!iter)
616 return NULL;
617
618 pos = g_new0(struct bt_iter_pos, 1);
619 pos->type = BT_SEEK_TIME;
620 pos->u.seek_time = timestamp;
621 return pos;
622}
623
624/*
625 * babeltrace_filestream_seek: seek a filestream to given position.
626 *
627 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
628 */
629static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
630 const struct bt_iter_pos *begin_pos,
631 unsigned long stream_id)
632{
633 int ret = 0;
634
635 if (!file_stream || !begin_pos)
636 return -EINVAL;
637
638 switch (begin_pos->type) {
639 case BT_SEEK_CUR:
640 /*
641 * just insert into the heap we should already know
642 * the timestamps
643 */
644 break;
645 case BT_SEEK_BEGIN:
646 file_stream->pos.packet_seek(&file_stream->pos.parent,
647 0, SEEK_SET);
648 ret = stream_read_event(file_stream);
649 break;
650 case BT_SEEK_TIME:
651 case BT_SEEK_RESTORE:
652 default:
653 assert(0); /* Not yet defined */
654 }
655
656 return ret;
657}
658
659int bt_iter_init(struct bt_iter *iter,
660 struct bt_context *ctx,
661 const struct bt_iter_pos *begin_pos,
662 const struct bt_iter_pos *end_pos)
663{
664 int i, stream_id;
665 int ret = 0;
666
667 if (!iter || !ctx)
668 return -EINVAL;
669
670 if (ctx->current_iterator) {
671 ret = -1;
672 goto error_ctx;
673 }
674
675 iter->stream_heap = g_new(struct ptr_heap, 1);
676 iter->end_pos = end_pos;
677 bt_context_get(ctx);
678 iter->ctx = ctx;
679
680 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
681 if (ret < 0)
682 goto error_heap_init;
683
684 for (i = 0; i < ctx->tc->array->len; i++) {
685 struct ctf_trace *tin;
686 struct bt_trace_descriptor *td_read;
687
688 td_read = g_ptr_array_index(ctx->tc->array, i);
689 if (!td_read)
690 continue;
691 tin = container_of(td_read, struct ctf_trace, parent);
692
693 /* Populate heap with each stream */
694 for (stream_id = 0; stream_id < tin->streams->len;
695 stream_id++) {
696 struct ctf_stream_declaration *stream;
697 int filenr;
698
699 stream = g_ptr_array_index(tin->streams, stream_id);
700 if (!stream)
701 continue;
702 for (filenr = 0; filenr < stream->streams->len;
703 filenr++) {
704 struct ctf_file_stream *file_stream;
705
706 file_stream = g_ptr_array_index(stream->streams,
707 filenr);
708 if (!file_stream)
709 continue;
710 if (begin_pos) {
711 ret = babeltrace_filestream_seek(
712 file_stream,
713 begin_pos,
714 stream_id);
715 } else {
716 struct bt_iter_pos pos;
717 pos.type = BT_SEEK_BEGIN;
718 ret = babeltrace_filestream_seek(
719 file_stream, &pos,
720 stream_id);
721 }
722 if (ret == EOF) {
723 ret = 0;
724 continue;
725 } else if (ret) {
726 goto error;
727 }
728 /* Add to heap */
729 ret = bt_heap_insert(iter->stream_heap, file_stream);
730 if (ret)
731 goto error;
732 }
733 }
734 }
735
736 ctx->current_iterator = iter;
737 return 0;
738
739error:
740 bt_heap_free(iter->stream_heap);
741error_heap_init:
742 g_free(iter->stream_heap);
743 iter->stream_heap = NULL;
744error_ctx:
745 return ret;
746}
747
748struct bt_iter *bt_iter_create(struct bt_context *ctx,
749 const struct bt_iter_pos *begin_pos,
750 const struct bt_iter_pos *end_pos)
751{
752 struct bt_iter *iter;
753 int ret;
754
755 if (!ctx)
756 return NULL;
757
758 iter = g_new0(struct bt_iter, 1);
759 ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
760 if (ret) {
761 g_free(iter);
762 return NULL;
763 }
764 return iter;
765}
766
767void bt_iter_fini(struct bt_iter *iter)
768{
769 assert(iter);
770 if (iter->stream_heap) {
771 bt_heap_free(iter->stream_heap);
772 g_free(iter->stream_heap);
773 }
774 iter->ctx->current_iterator = NULL;
775 bt_context_put(iter->ctx);
776}
777
778void bt_iter_destroy(struct bt_iter *iter)
779{
780 assert(iter);
781 bt_iter_fini(iter);
782 g_free(iter);
783}
784
785int bt_iter_next(struct bt_iter *iter)
786{
787 struct ctf_file_stream *file_stream, *removed;
788 int ret;
789
790 if (!iter)
791 return -EINVAL;
792
793 file_stream = bt_heap_maximum(iter->stream_heap);
794 if (!file_stream) {
795 /* end of file for all streams */
796 ret = 0;
797 goto end;
798 }
799
800 ret = stream_read_event(file_stream);
801 if (ret == EOF) {
802 removed = bt_heap_remove(iter->stream_heap);
803 assert(removed == file_stream);
804 ret = 0;
805 goto end;
806 } else if (ret) {
807 goto end;
808 }
809 /* Reinsert the file stream into the heap, and rebalance. */
810 removed = bt_heap_replace_max(iter->stream_heap, file_stream);
811 assert(removed == file_stream);
812
813end:
814 return ret;
815}
This page took 0.026016 seconds and 4 git commands to generate.