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