Python-bindings: Move declaration bindings out of the _Definition class
[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/*
6c9a50c0
MD
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).
6f3077a2 82 */
2002e48a 83static int stream_compare(void *a, void *b)
6f3077a2
JD
84{
85 struct ctf_file_stream *s_a = a, *s_b = b;
86
6c9a50c0 87 if (s_a->parent.real_timestamp < s_b->parent.real_timestamp) {
6f3077a2 88 return 1;
6c9a50c0 89 } else if (likely(s_a->parent.real_timestamp > s_b->parent.real_timestamp)) {
6f3077a2 90 return 0;
6c9a50c0
MD
91 } else {
92 return strcmp(s_a->parent.path, s_b->parent.path);
93 }
6f3077a2
JD
94}
95
90fcbacc
JD
96void bt_iter_free_pos(struct bt_iter_pos *iter_pos)
97{
6cba487f
MD
98 if (!iter_pos)
99 return;
100
5acdc773 101 if (iter_pos->type == BT_SEEK_RESTORE && iter_pos->u.restore) {
6cba487f
MD
102 if (iter_pos->u.restore->stream_saved_pos) {
103 g_array_free(
104 iter_pos->u.restore->stream_saved_pos,
105 TRUE);
90fcbacc 106 }
6cba487f 107 g_free(iter_pos->u.restore);
90fcbacc 108 }
6cba487f 109 g_free(iter_pos);
90fcbacc
JD
110}
111
dc81689e
JD
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 *
e32cb1e4
MD
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.
f7ed6563
MD
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.
dc81689e
JD
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;
03798a93
JD
135 for (i = 0; i < stream_pos->packet_real_index->len; i++) {
136 index = &g_array_index(stream_pos->packet_real_index,
dc81689e 137 struct packet_index, i);
e32cb1e4 138 if (index->timestamp_end < timestamp)
dc81689e
JD
139 continue;
140
20d0dcf9 141 stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET);
5d2a5af2 142 do {
dc81689e 143 ret = stream_read_event(cfs);
03798a93 144 } while (cfs->parent.real_timestamp < timestamp && ret == 0);
5d2a5af2 145
e32cb1e4
MD
146 /* Can return either EOF, 0, or error (> 0). */
147 return ret;
dc81689e 148 }
e32cb1e4
MD
149 /*
150 * Cannot find the timestamp within the stream packets, return
151 * EOF.
152 */
dc81689e
JD
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
e32cb1e4
MD
162 * user the timestamp is out of the scope.
163 * On other errors, return positive value.
dc81689e
JD
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;
e32cb1e4 169 int found = 0;
dc81689e
JD
170
171 /* for each stream_class */
172 for (i = 0; i < tin->streams->len; i++) {
f380e105 173 struct ctf_stream_declaration *stream_class;
dc81689e
JD
174
175 stream_class = g_ptr_array_index(tin->streams, i);
e32cb1e4
MD
176 if (!stream_class)
177 continue;
dc81689e
JD
178 /* for each file_stream */
179 for (j = 0; j < stream_class->streams->len; j++) {
9e88d150 180 struct ctf_stream_definition *stream;
dc81689e
JD
181 struct ctf_file_stream *cfs;
182
183 stream = g_ptr_array_index(stream_class->streams, j);
e32cb1e4
MD
184 if (!stream)
185 continue;
dc81689e
JD
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 */
dd06413f 191 ret = bt_heap_insert(stream_heap, cfs);
e32cb1e4
MD
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;
dc81689e 202 }
e32cb1e4 203 /* on EOF just do not put stream into heap. */
dc81689e
JD
204 }
205 }
206
e32cb1e4 207 return found ? 0 : EOF;
dc81689e
JD
208}
209
f7ed6563
MD
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{
38380786 272 int ret = EOF, i, found = 0;
f7ed6563
MD
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;
38380786 291 found = 1;
f7ed6563
MD
292 }
293 }
294 assert(ret >= 0 || ret == EOF);
38380786
YB
295 if (found) {
296 return 0;
297 }
f7ed6563
MD
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;
1b8455b7 320 struct bt_trace_descriptor *td_read;
f7ed6563
MD
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
90fcbacc
JD
356int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
357{
dc81689e 358 struct trace_collection *tc;
90fcbacc
JD
359 int i, ret;
360
7f89ddce
MD
361 if (!iter || !iter_pos)
362 return -EINVAL;
363
dc81689e
JD
364 switch (iter_pos->type) {
365 case BT_SEEK_RESTORE:
366 if (!iter_pos->u.restore)
7344374e 367 return -EINVAL;
dc81689e 368
dd06413f
JD
369 bt_heap_free(iter->stream_heap);
370 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
90fcbacc 371 if (ret < 0)
bed54c92 372 goto error_heap_init;
90fcbacc
JD
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;
9e88d150 378 struct ctf_stream_definition *stream;
90fcbacc
JD
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;
90fcbacc 385
06789ffd 386 stream_pos->packet_seek(&stream_pos->parent,
20d0dcf9 387 saved_pos->cur_index, SEEK_SET);
90fcbacc
JD
388
389 /*
390 * the timestamp needs to be restored after
06789ffd 391 * packet_seek, because this function resets
90fcbacc
JD
392 * the timestamp to the beginning of the packet
393 */
03798a93
JD
394 stream->real_timestamp = saved_pos->current_real_timestamp;
395 stream->cycles_timestamp = saved_pos->current_cycles_timestamp;
90fcbacc 396 stream_pos->offset = saved_pos->offset;
3a25e036 397 stream_pos->last_offset = LAST_OFFSET_POISON;
90fcbacc 398
03798a93
JD
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;
90fcbacc 403
fef3bf22
MD
404 printf_debug("restored to cur_index = %" PRId64 " and "
405 "offset = %" PRId64 ", timestamp = %" PRIu64 "\n",
90fcbacc 406 stream_pos->cur_index,
03798a93 407 stream_pos->offset, stream->real_timestamp);
90fcbacc 408
28fcbaca
MD
409 ret = stream_read_event(saved_pos->file_stream);
410 if (ret != 0) {
411 goto error;
412 }
90fcbacc
JD
413
414 /* Add to heap */
dd06413f 415 ret = bt_heap_insert(iter->stream_heap,
90fcbacc
JD
416 saved_pos->file_stream);
417 if (ret)
418 goto error;
419 }
5acdc773 420 return 0;
dc81689e
JD
421 case BT_SEEK_TIME:
422 tc = iter->ctx->tc;
423
dd06413f
JD
424 bt_heap_free(iter->stream_heap);
425 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
dc81689e 426 if (ret < 0)
bed54c92 427 goto error_heap_init;
dc81689e
JD
428
429 /* for each trace in the trace_collection */
430 for (i = 0; i < tc->array->len; i++) {
431 struct ctf_trace *tin;
1b8455b7 432 struct bt_trace_descriptor *td_read;
dc81689e
JD
433
434 td_read = g_ptr_array_index(tc->array, i);
e32cb1e4
MD
435 if (!td_read)
436 continue;
dc81689e
JD
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);
e32cb1e4
MD
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)
dc81689e
JD
450 goto error;
451 }
452 return 0;
55c21ea5
JD
453 case BT_SEEK_BEGIN:
454 tc = iter->ctx->tc;
dd06413f
JD
455 bt_heap_free(iter->stream_heap);
456 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
db8a4511 457 if (ret < 0)
bed54c92 458 goto error_heap_init;
db8a4511 459
55c21ea5
JD
460 for (i = 0; i < tc->array->len; i++) {
461 struct ctf_trace *tin;
1b8455b7 462 struct bt_trace_descriptor *td_read;
55c21ea5
JD
463 int stream_id;
464
465 td_read = g_ptr_array_index(tc->array, i);
e32cb1e4
MD
466 if (!td_read)
467 continue;
55c21ea5
JD
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++) {
f380e105 473 struct ctf_stream_declaration *stream;
55c21ea5
JD
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);
e32cb1e4
MD
486 if (!file_stream)
487 continue;
55c21ea5
JD
488 ret = babeltrace_filestream_seek(
489 file_stream, iter_pos,
490 stream_id);
e32cb1e4
MD
491 if (ret != 0 && ret != EOF) {
492 goto error;
493 }
08ac0e08
MD
494 if (ret == EOF) {
495 /* Do not add EOF streams */
496 continue;
497 }
dd06413f 498 ret = bt_heap_insert(iter->stream_heap, file_stream);
db8a4511
JD
499 if (ret)
500 goto error;
55c21ea5
JD
501 }
502 }
503 }
504 break;
f7ed6563
MD
505 case BT_SEEK_LAST:
506 {
4b8de153 507 struct ctf_file_stream *cfs = NULL;
f7ed6563
MD
508
509 tc = iter->ctx->tc;
510 ret = seek_last_ctf_trace_collection(tc, &cfs);
4b8de153 511 if (ret != 0 || !cfs)
f7ed6563
MD
512 goto error;
513 /* remove all streams from the heap */
dd06413f 514 bt_heap_free(iter->stream_heap);
f7ed6563 515 /* Create a new empty heap */
dd06413f 516 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
f7ed6563
MD
517 if (ret < 0)
518 goto error;
519 /* Insert the stream that contains the last event */
dd06413f 520 ret = bt_heap_insert(iter->stream_heap, cfs);
f7ed6563
MD
521 if (ret)
522 goto error;
523 break;
524 }
dc81689e
JD
525 default:
526 /* not implemented */
7344374e 527 return -EINVAL;
90fcbacc
JD
528 }
529
530 return 0;
dc81689e 531
90fcbacc 532error:
dd06413f 533 bt_heap_free(iter->stream_heap);
bed54c92 534error_heap_init:
dd06413f
JD
535 if (bt_heap_init(iter->stream_heap, 0, stream_compare) < 0) {
536 bt_heap_free(iter->stream_heap);
dc81689e
JD
537 g_free(iter->stream_heap);
538 iter->stream_heap = NULL;
539 ret = -ENOMEM;
540 }
bed54c92 541
dc81689e 542 return ret;
90fcbacc
JD
543}
544
545struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
546{
547 struct bt_iter_pos *pos;
7f89ddce 548 struct trace_collection *tc;
6fabd7af
JD
549 struct ctf_file_stream *file_stream = NULL, *removed;
550 struct ptr_heap iter_heap_copy;
551 int ret;
90fcbacc 552
7f89ddce
MD
553 if (!iter)
554 return NULL;
555
556 tc = iter->ctx->tc;
90fcbacc 557 pos = g_new0(struct bt_iter_pos, 1);
5acdc773 558 pos->type = BT_SEEK_RESTORE;
90fcbacc 559 pos->u.restore = g_new0(struct bt_saved_pos, 1);
90fcbacc
JD
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
dd06413f 566 ret = bt_heap_copy(&iter_heap_copy, iter->stream_heap);
6fabd7af
JD
567 if (ret < 0)
568 goto error_heap;
90fcbacc 569
6fabd7af 570 /* iterate over each stream in the heap */
dd06413f 571 file_stream = bt_heap_maximum(&iter_heap_copy);
6fabd7af
JD
572 while (file_stream != NULL) {
573 struct stream_saved_pos saved_pos;
90fcbacc 574
6fabd7af
JD
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;
90fcbacc 579
03798a93
JD
580 saved_pos.current_real_timestamp = file_stream->parent.real_timestamp;
581 saved_pos.current_cycles_timestamp = file_stream->parent.cycles_timestamp;
90fcbacc 582
6fabd7af
JD
583 g_array_append_val(
584 pos->u.restore->stream_saved_pos,
585 saved_pos);
90fcbacc 586
6fabd7af
JD
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,
03798a93 592 saved_pos.current_real_timestamp);
6fabd7af
JD
593
594 /* remove the stream from the heap copy */
dd06413f 595 removed = bt_heap_remove(&iter_heap_copy);
6fabd7af
JD
596 assert(removed == file_stream);
597
dd06413f 598 file_stream = bt_heap_maximum(&iter_heap_copy);
6fabd7af 599 }
dd06413f 600 bt_heap_free(&iter_heap_copy);
90fcbacc
JD
601 return pos;
602
6fabd7af
JD
603error_heap:
604 g_array_free(pos->u.restore->stream_saved_pos, TRUE);
90fcbacc 605error:
6fabd7af 606 g_free(pos);
90fcbacc
JD
607 return NULL;
608}
609
dc81689e
JD
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
7f89ddce
MD
615 if (!iter)
616 return NULL;
617
dc81689e
JD
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
6f3077a2
JD
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,
e8c92a62 630 const struct bt_iter_pos *begin_pos,
6f3077a2
JD
631 unsigned long stream_id)
632{
633 int ret = 0;
634
7f89ddce
MD
635 if (!file_stream || !begin_pos)
636 return -EINVAL;
637
6f3077a2
JD
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:
06789ffd 646 file_stream->pos.packet_seek(&file_stream->pos.parent,
d6425aaf 647 0, SEEK_SET);
6f3077a2
JD
648 ret = stream_read_event(file_stream);
649 break;
650 case BT_SEEK_TIME:
651 case BT_SEEK_RESTORE:
6f3077a2
JD
652 default:
653 assert(0); /* Not yet defined */
654 }
655
656 return ret;
657}
658
e4195791
MD
659int bt_iter_init(struct bt_iter *iter,
660 struct bt_context *ctx,
04ae3991
MD
661 const struct bt_iter_pos *begin_pos,
662 const struct bt_iter_pos *end_pos)
6f3077a2
JD
663{
664 int i, stream_id;
665 int ret = 0;
6f3077a2 666
7f89ddce
MD
667 if (!iter || !ctx)
668 return -EINVAL;
669
e003e871
JD
670 if (ctx->current_iterator) {
671 ret = -1;
672 goto error_ctx;
673 }
674
6f3077a2 675 iter->stream_heap = g_new(struct ptr_heap, 1);
6f3077a2 676 iter->end_pos = end_pos;
6cba487f 677 bt_context_get(ctx);
95d36295 678 iter->ctx = ctx;
6f3077a2 679
dd06413f 680 ret = bt_heap_init(iter->stream_heap, 0, stream_compare);
6f3077a2
JD
681 if (ret < 0)
682 goto error_heap_init;
683
95d36295 684 for (i = 0; i < ctx->tc->array->len; i++) {
6f3077a2 685 struct ctf_trace *tin;
1b8455b7 686 struct bt_trace_descriptor *td_read;
6f3077a2 687
95d36295 688 td_read = g_ptr_array_index(ctx->tc->array, i);
e32cb1e4
MD
689 if (!td_read)
690 continue;
6f3077a2
JD
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++) {
f380e105 696 struct ctf_stream_declaration *stream;
6f3077a2
JD
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);
e32cb1e4
MD
708 if (!file_stream)
709 continue;
6f3077a2 710 if (begin_pos) {
b42d4e4e
JD
711 ret = babeltrace_filestream_seek(
712 file_stream,
713 begin_pos,
6f3077a2 714 stream_id);
b42d4e4e
JD
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;
6f3077a2
JD
727 }
728 /* Add to heap */
dd06413f 729 ret = bt_heap_insert(iter->stream_heap, file_stream);
6f3077a2
JD
730 if (ret)
731 goto error;
732 }
733 }
734 }
735
e003e871 736 ctx->current_iterator = iter;
e4195791 737 return 0;
6f3077a2
JD
738
739error:
dd06413f 740 bt_heap_free(iter->stream_heap);
6f3077a2
JD
741error_heap_init:
742 g_free(iter->stream_heap);
bed54c92 743 iter->stream_heap = NULL;
e003e871 744error_ctx:
e4195791 745 return ret;
6f3077a2
JD
746}
747
e4195791 748struct bt_iter *bt_iter_create(struct bt_context *ctx,
04ae3991
MD
749 const struct bt_iter_pos *begin_pos,
750 const struct bt_iter_pos *end_pos)
e4195791
MD
751{
752 struct bt_iter *iter;
753 int ret;
754
7f89ddce
MD
755 if (!ctx)
756 return NULL;
757
e4195791
MD
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)
6f3077a2 768{
7f89ddce 769 assert(iter);
dc81689e 770 if (iter->stream_heap) {
dd06413f 771 bt_heap_free(iter->stream_heap);
dc81689e
JD
772 g_free(iter->stream_heap);
773 }
e003e871 774 iter->ctx->current_iterator = NULL;
95d36295 775 bt_context_put(iter->ctx);
e4195791 776}
95d36295 777
e4195791
MD
778void bt_iter_destroy(struct bt_iter *iter)
779{
7f89ddce 780 assert(iter);
e4195791 781 bt_iter_fini(iter);
90fcbacc 782 g_free(iter);
6f3077a2
JD
783}
784
e8c92a62 785int bt_iter_next(struct bt_iter *iter)
6f3077a2
JD
786{
787 struct ctf_file_stream *file_stream, *removed;
788 int ret;
789
7f89ddce
MD
790 if (!iter)
791 return -EINVAL;
792
dd06413f 793 file_stream = bt_heap_maximum(iter->stream_heap);
6f3077a2
JD
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) {
dd06413f 802 removed = bt_heap_remove(iter->stream_heap);
6f3077a2
JD
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. */
dd06413f 810 removed = bt_heap_replace_max(iter->stream_heap, file_stream);
6f3077a2
JD
811 assert(removed == file_stream);
812
813end:
814 return ret;
815}
This page took 0.065518 seconds and 4 git commands to generate.