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