Fix: iterator.c BT_SEEK_RESTORE: check return value
[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.
19 */
20
21#include <stdlib.h>
22#include <babeltrace/babeltrace.h>
95d36295 23#include <babeltrace/context.h>
08c22d05 24#include <babeltrace/context-internal.h>
6f3077a2 25#include <babeltrace/iterator-internal.h>
6204d33c 26#include <babeltrace/iterator.h>
6f3077a2 27#include <babeltrace/prio_heap.h>
e4195791 28#include <babeltrace/ctf/metadata.h>
9843982d 29#include <babeltrace/ctf/events.h>
90fcbacc 30#include <inttypes.h>
6f3077a2 31
55c21ea5
JD
32static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
33 const struct bt_iter_pos *begin_pos,
34 unsigned long stream_id);
35
6f3077a2
JD
36struct stream_saved_pos {
37 /*
38 * Use file_stream pointer to check if the trace collection we
39 * restore to match the one we saved from, for each stream.
40 */
41 struct ctf_file_stream *file_stream;
42 size_t cur_index; /* current index in packet index */
43 ssize_t offset; /* offset from base, in bits. EOF for end of file. */
03798a93
JD
44 uint64_t current_real_timestamp;
45 uint64_t current_cycles_timestamp;
6f3077a2
JD
46};
47
e8c92a62 48struct bt_saved_pos {
6f3077a2
JD
49 struct trace_collection *tc;
50 GArray *stream_saved_pos; /* Contains struct stream_saved_pos */
51};
52
53static int stream_read_event(struct ctf_file_stream *sin)
54{
55 int ret;
56
57 ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent);
58 if (ret == EOF)
59 return EOF;
60 else if (ret) {
3394d22e 61 fprintf(stderr, "[error] Reading event failed.\n");
6f3077a2
JD
62 return ret;
63 }
64 return 0;
65}
66
67/*
68 * returns true if a < b, false otherwise.
69 */
2002e48a 70static int stream_compare(void *a, void *b)
6f3077a2
JD
71{
72 struct ctf_file_stream *s_a = a, *s_b = b;
73
03798a93 74 if (s_a->parent.real_timestamp < s_b->parent.real_timestamp)
6f3077a2
JD
75 return 1;
76 else
77 return 0;
78}
79
90fcbacc
JD
80void bt_iter_free_pos(struct bt_iter_pos *iter_pos)
81{
6cba487f
MD
82 if (!iter_pos)
83 return;
84
5acdc773 85 if (iter_pos->type == BT_SEEK_RESTORE && iter_pos->u.restore) {
6cba487f
MD
86 if (iter_pos->u.restore->stream_saved_pos) {
87 g_array_free(
88 iter_pos->u.restore->stream_saved_pos,
89 TRUE);
90fcbacc 90 }
6cba487f 91 g_free(iter_pos->u.restore);
90fcbacc 92 }
6cba487f 93 g_free(iter_pos);
90fcbacc
JD
94}
95
dc81689e
JD
96/*
97 * seek_file_stream_by_timestamp
98 *
99 * Browse a filestream by index, if an index contains the timestamp passed in
100 * argument, seek inside the corresponding packet it until we find the event we
101 * are looking for (either the exact timestamp or the event just after the
102 * timestamp).
103 *
e32cb1e4
MD
104 * Return 0 if the seek succeded, EOF if we didn't find any packet
105 * containing the timestamp, or a positive integer for error.
dc81689e
JD
106 */
107static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs,
108 uint64_t timestamp)
109{
110 struct ctf_stream_pos *stream_pos;
111 struct packet_index *index;
112 int i, ret;
113
114 stream_pos = &cfs->pos;
03798a93
JD
115 for (i = 0; i < stream_pos->packet_real_index->len; i++) {
116 index = &g_array_index(stream_pos->packet_real_index,
dc81689e 117 struct packet_index, i);
e32cb1e4 118 if (index->timestamp_end < timestamp)
dc81689e
JD
119 continue;
120
20d0dcf9 121 stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET);
5d2a5af2 122 do {
dc81689e 123 ret = stream_read_event(cfs);
03798a93 124 } while (cfs->parent.real_timestamp < timestamp && ret == 0);
5d2a5af2 125
e32cb1e4
MD
126 /* Can return either EOF, 0, or error (> 0). */
127 return ret;
dc81689e 128 }
e32cb1e4
MD
129 /*
130 * Cannot find the timestamp within the stream packets, return
131 * EOF.
132 */
dc81689e
JD
133 return EOF;
134}
135
136/*
137 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
138 * the corresponding timestamp
139 *
140 * Return 0 on success.
141 * If the timestamp is not part of any file stream, return EOF to inform the
e32cb1e4
MD
142 * user the timestamp is out of the scope.
143 * On other errors, return positive value.
dc81689e
JD
144 */
145static int seek_ctf_trace_by_timestamp(struct ctf_trace *tin,
146 uint64_t timestamp, struct ptr_heap *stream_heap)
147{
148 int i, j, ret;
e32cb1e4 149 int found = 0;
dc81689e
JD
150
151 /* for each stream_class */
152 for (i = 0; i < tin->streams->len; i++) {
f380e105 153 struct ctf_stream_declaration *stream_class;
dc81689e
JD
154
155 stream_class = g_ptr_array_index(tin->streams, i);
e32cb1e4
MD
156 if (!stream_class)
157 continue;
dc81689e
JD
158 /* for each file_stream */
159 for (j = 0; j < stream_class->streams->len; j++) {
9e88d150 160 struct ctf_stream_definition *stream;
dc81689e
JD
161 struct ctf_file_stream *cfs;
162
163 stream = g_ptr_array_index(stream_class->streams, j);
e32cb1e4
MD
164 if (!stream)
165 continue;
dc81689e
JD
166 cfs = container_of(stream, struct ctf_file_stream,
167 parent);
168 ret = seek_file_stream_by_timestamp(cfs, timestamp);
169 if (ret == 0) {
170 /* Add to heap */
171 ret = heap_insert(stream_heap, cfs);
e32cb1e4
MD
172 if (ret) {
173 /* Return positive error. */
174 return -ret;
175 }
176 found = 1;
177 } else if (ret > 0) {
178 /*
179 * Error in seek (not EOF), failure.
180 */
181 return ret;
dc81689e 182 }
e32cb1e4 183 /* on EOF just do not put stream into heap. */
dc81689e
JD
184 }
185 }
186
e32cb1e4 187 return found ? 0 : EOF;
dc81689e
JD
188}
189
90fcbacc
JD
190int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
191{
dc81689e 192 struct trace_collection *tc;
90fcbacc
JD
193 int i, ret;
194
7f89ddce
MD
195 if (!iter || !iter_pos)
196 return -EINVAL;
197
dc81689e
JD
198 switch (iter_pos->type) {
199 case BT_SEEK_RESTORE:
200 if (!iter_pos->u.restore)
7344374e 201 return -EINVAL;
dc81689e 202
90fcbacc 203 heap_free(iter->stream_heap);
90fcbacc
JD
204 ret = heap_init(iter->stream_heap, 0, stream_compare);
205 if (ret < 0)
bed54c92 206 goto error_heap_init;
90fcbacc
JD
207
208 for (i = 0; i < iter_pos->u.restore->stream_saved_pos->len;
209 i++) {
210 struct stream_saved_pos *saved_pos;
211 struct ctf_stream_pos *stream_pos;
9e88d150 212 struct ctf_stream_definition *stream;
90fcbacc
JD
213
214 saved_pos = &g_array_index(
215 iter_pos->u.restore->stream_saved_pos,
216 struct stream_saved_pos, i);
217 stream = &saved_pos->file_stream->parent;
218 stream_pos = &saved_pos->file_stream->pos;
90fcbacc 219
06789ffd 220 stream_pos->packet_seek(&stream_pos->parent,
20d0dcf9 221 saved_pos->cur_index, SEEK_SET);
90fcbacc
JD
222
223 /*
224 * the timestamp needs to be restored after
06789ffd 225 * packet_seek, because this function resets
90fcbacc
JD
226 * the timestamp to the beginning of the packet
227 */
03798a93
JD
228 stream->real_timestamp = saved_pos->current_real_timestamp;
229 stream->cycles_timestamp = saved_pos->current_cycles_timestamp;
90fcbacc 230 stream_pos->offset = saved_pos->offset;
3a25e036 231 stream_pos->last_offset = LAST_OFFSET_POISON;
90fcbacc 232
03798a93
JD
233 stream->prev_real_timestamp = 0;
234 stream->prev_real_timestamp_end = 0;
235 stream->prev_cycles_timestamp = 0;
236 stream->prev_cycles_timestamp_end = 0;
90fcbacc
JD
237
238 printf_debug("restored to cur_index = %zd and "
239 "offset = %zd, timestamp = %" PRIu64 "\n",
240 stream_pos->cur_index,
03798a93 241 stream_pos->offset, stream->real_timestamp);
90fcbacc 242
28fcbaca
MD
243 ret = stream_read_event(saved_pos->file_stream);
244 if (ret != 0) {
245 goto error;
246 }
90fcbacc
JD
247
248 /* Add to heap */
249 ret = heap_insert(iter->stream_heap,
250 saved_pos->file_stream);
251 if (ret)
252 goto error;
253 }
5acdc773 254 return 0;
dc81689e
JD
255 case BT_SEEK_TIME:
256 tc = iter->ctx->tc;
257
dc81689e
JD
258 heap_free(iter->stream_heap);
259 ret = heap_init(iter->stream_heap, 0, stream_compare);
260 if (ret < 0)
bed54c92 261 goto error_heap_init;
dc81689e
JD
262
263 /* for each trace in the trace_collection */
264 for (i = 0; i < tc->array->len; i++) {
265 struct ctf_trace *tin;
266 struct trace_descriptor *td_read;
267
268 td_read = g_ptr_array_index(tc->array, i);
e32cb1e4
MD
269 if (!td_read)
270 continue;
dc81689e
JD
271 tin = container_of(td_read, struct ctf_trace, parent);
272
273 ret = seek_ctf_trace_by_timestamp(tin,
274 iter_pos->u.seek_time,
275 iter->stream_heap);
e32cb1e4
MD
276 /*
277 * Positive errors are failure. Negative value
278 * is EOF (for which we continue with other
279 * traces). 0 is success. Note: on EOF, it just
280 * means that no stream has been added to the
281 * iterator for that trace, which is fine.
282 */
283 if (ret != 0 && ret != EOF)
dc81689e
JD
284 goto error;
285 }
286 return 0;
55c21ea5
JD
287 case BT_SEEK_BEGIN:
288 tc = iter->ctx->tc;
db8a4511
JD
289 heap_free(iter->stream_heap);
290 ret = heap_init(iter->stream_heap, 0, stream_compare);
291 if (ret < 0)
bed54c92 292 goto error_heap_init;
db8a4511 293
55c21ea5
JD
294 for (i = 0; i < tc->array->len; i++) {
295 struct ctf_trace *tin;
296 struct trace_descriptor *td_read;
297 int stream_id;
298
299 td_read = g_ptr_array_index(tc->array, i);
e32cb1e4
MD
300 if (!td_read)
301 continue;
55c21ea5
JD
302 tin = container_of(td_read, struct ctf_trace, parent);
303
304 /* Populate heap with each stream */
305 for (stream_id = 0; stream_id < tin->streams->len;
306 stream_id++) {
f380e105 307 struct ctf_stream_declaration *stream;
55c21ea5
JD
308 int filenr;
309
310 stream = g_ptr_array_index(tin->streams,
311 stream_id);
312 if (!stream)
313 continue;
314 for (filenr = 0; filenr < stream->streams->len;
315 filenr++) {
316 struct ctf_file_stream *file_stream;
317 file_stream = g_ptr_array_index(
318 stream->streams,
319 filenr);
e32cb1e4
MD
320 if (!file_stream)
321 continue;
55c21ea5
JD
322 ret = babeltrace_filestream_seek(
323 file_stream, iter_pos,
324 stream_id);
e32cb1e4
MD
325 if (ret != 0 && ret != EOF) {
326 goto error;
327 }
db8a4511
JD
328 ret = heap_insert(iter->stream_heap, file_stream);
329 if (ret)
330 goto error;
55c21ea5
JD
331 }
332 }
333 }
334 break;
dc81689e
JD
335 default:
336 /* not implemented */
7344374e 337 return -EINVAL;
90fcbacc
JD
338 }
339
340 return 0;
dc81689e 341
90fcbacc
JD
342error:
343 heap_free(iter->stream_heap);
bed54c92 344error_heap_init:
dc81689e
JD
345 if (heap_init(iter->stream_heap, 0, stream_compare) < 0) {
346 heap_free(iter->stream_heap);
347 g_free(iter->stream_heap);
348 iter->stream_heap = NULL;
349 ret = -ENOMEM;
350 }
bed54c92 351
dc81689e 352 return ret;
90fcbacc
JD
353}
354
355struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
356{
357 struct bt_iter_pos *pos;
7f89ddce 358 struct trace_collection *tc;
6fabd7af
JD
359 struct ctf_file_stream *file_stream = NULL, *removed;
360 struct ptr_heap iter_heap_copy;
361 int ret;
90fcbacc 362
7f89ddce
MD
363 if (!iter)
364 return NULL;
365
366 tc = iter->ctx->tc;
90fcbacc 367 pos = g_new0(struct bt_iter_pos, 1);
5acdc773 368 pos->type = BT_SEEK_RESTORE;
90fcbacc 369 pos->u.restore = g_new0(struct bt_saved_pos, 1);
90fcbacc
JD
370 pos->u.restore->tc = tc;
371 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
372 sizeof(struct stream_saved_pos));
373 if (!pos->u.restore->stream_saved_pos)
374 goto error;
375
6fabd7af
JD
376 ret = heap_copy(&iter_heap_copy, iter->stream_heap);
377 if (ret < 0)
378 goto error_heap;
90fcbacc 379
6fabd7af
JD
380 /* iterate over each stream in the heap */
381 file_stream = heap_maximum(&iter_heap_copy);
382 while (file_stream != NULL) {
383 struct stream_saved_pos saved_pos;
90fcbacc 384
6fabd7af
JD
385 assert(file_stream->pos.last_offset != LAST_OFFSET_POISON);
386 saved_pos.offset = file_stream->pos.last_offset;
387 saved_pos.file_stream = file_stream;
388 saved_pos.cur_index = file_stream->pos.cur_index;
90fcbacc 389
03798a93
JD
390 saved_pos.current_real_timestamp = file_stream->parent.real_timestamp;
391 saved_pos.current_cycles_timestamp = file_stream->parent.cycles_timestamp;
90fcbacc 392
6fabd7af
JD
393 g_array_append_val(
394 pos->u.restore->stream_saved_pos,
395 saved_pos);
90fcbacc 396
6fabd7af
JD
397 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
398 "offset : %zd, "
399 "timestamp = %" PRIu64 "\n",
400 file_stream->parent.stream_id,
401 saved_pos.cur_index, saved_pos.offset,
03798a93 402 saved_pos.current_real_timestamp);
6fabd7af
JD
403
404 /* remove the stream from the heap copy */
405 removed = heap_remove(&iter_heap_copy);
406 assert(removed == file_stream);
407
408 file_stream = heap_maximum(&iter_heap_copy);
409 }
410 heap_free(&iter_heap_copy);
90fcbacc
JD
411 return pos;
412
6fabd7af
JD
413error_heap:
414 g_array_free(pos->u.restore->stream_saved_pos, TRUE);
90fcbacc 415error:
6fabd7af 416 g_free(pos);
90fcbacc
JD
417 return NULL;
418}
419
dc81689e
JD
420struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
421 uint64_t timestamp)
422{
423 struct bt_iter_pos *pos;
424
7f89ddce
MD
425 if (!iter)
426 return NULL;
427
dc81689e
JD
428 pos = g_new0(struct bt_iter_pos, 1);
429 pos->type = BT_SEEK_TIME;
430 pos->u.seek_time = timestamp;
431 return pos;
432}
433
6f3077a2
JD
434/*
435 * babeltrace_filestream_seek: seek a filestream to given position.
436 *
437 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
438 */
439static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
e8c92a62 440 const struct bt_iter_pos *begin_pos,
6f3077a2
JD
441 unsigned long stream_id)
442{
443 int ret = 0;
444
7f89ddce
MD
445 if (!file_stream || !begin_pos)
446 return -EINVAL;
447
6f3077a2
JD
448 switch (begin_pos->type) {
449 case BT_SEEK_CUR:
450 /*
451 * just insert into the heap we should already know
452 * the timestamps
453 */
454 break;
455 case BT_SEEK_BEGIN:
06789ffd 456 file_stream->pos.packet_seek(&file_stream->pos.parent,
d6425aaf 457 0, SEEK_SET);
6f3077a2
JD
458 ret = stream_read_event(file_stream);
459 break;
460 case BT_SEEK_TIME:
461 case BT_SEEK_RESTORE:
462 case BT_SEEK_END:
463 default:
464 assert(0); /* Not yet defined */
465 }
466
467 return ret;
468}
469
e4195791
MD
470int bt_iter_init(struct bt_iter *iter,
471 struct bt_context *ctx,
04ae3991
MD
472 const struct bt_iter_pos *begin_pos,
473 const struct bt_iter_pos *end_pos)
6f3077a2
JD
474{
475 int i, stream_id;
476 int ret = 0;
6f3077a2 477
7f89ddce
MD
478 if (!iter || !ctx)
479 return -EINVAL;
480
e003e871
JD
481 if (ctx->current_iterator) {
482 ret = -1;
483 goto error_ctx;
484 }
485
6f3077a2 486 iter->stream_heap = g_new(struct ptr_heap, 1);
6f3077a2 487 iter->end_pos = end_pos;
6cba487f 488 bt_context_get(ctx);
95d36295 489 iter->ctx = ctx;
6f3077a2
JD
490
491 ret = heap_init(iter->stream_heap, 0, stream_compare);
492 if (ret < 0)
493 goto error_heap_init;
494
95d36295 495 for (i = 0; i < ctx->tc->array->len; i++) {
6f3077a2
JD
496 struct ctf_trace *tin;
497 struct trace_descriptor *td_read;
498
95d36295 499 td_read = g_ptr_array_index(ctx->tc->array, i);
e32cb1e4
MD
500 if (!td_read)
501 continue;
6f3077a2
JD
502 tin = container_of(td_read, struct ctf_trace, parent);
503
504 /* Populate heap with each stream */
505 for (stream_id = 0; stream_id < tin->streams->len;
506 stream_id++) {
f380e105 507 struct ctf_stream_declaration *stream;
6f3077a2
JD
508 int filenr;
509
510 stream = g_ptr_array_index(tin->streams, stream_id);
511 if (!stream)
512 continue;
513 for (filenr = 0; filenr < stream->streams->len;
514 filenr++) {
515 struct ctf_file_stream *file_stream;
516
517 file_stream = g_ptr_array_index(stream->streams,
518 filenr);
e32cb1e4
MD
519 if (!file_stream)
520 continue;
6f3077a2 521 if (begin_pos) {
b42d4e4e
JD
522 ret = babeltrace_filestream_seek(
523 file_stream,
524 begin_pos,
6f3077a2 525 stream_id);
b42d4e4e
JD
526 } else {
527 struct bt_iter_pos pos;
528 pos.type = BT_SEEK_BEGIN;
529 ret = babeltrace_filestream_seek(
530 file_stream, &pos,
531 stream_id);
532 }
533 if (ret == EOF) {
534 ret = 0;
535 continue;
536 } else if (ret) {
537 goto error;
6f3077a2
JD
538 }
539 /* Add to heap */
540 ret = heap_insert(iter->stream_heap, file_stream);
541 if (ret)
542 goto error;
543 }
544 }
545 }
546
e003e871 547 ctx->current_iterator = iter;
e4195791 548 return 0;
6f3077a2
JD
549
550error:
551 heap_free(iter->stream_heap);
552error_heap_init:
553 g_free(iter->stream_heap);
bed54c92 554 iter->stream_heap = NULL;
e003e871 555error_ctx:
e4195791 556 return ret;
6f3077a2
JD
557}
558
e4195791 559struct bt_iter *bt_iter_create(struct bt_context *ctx,
04ae3991
MD
560 const struct bt_iter_pos *begin_pos,
561 const struct bt_iter_pos *end_pos)
e4195791
MD
562{
563 struct bt_iter *iter;
564 int ret;
565
7f89ddce
MD
566 if (!ctx)
567 return NULL;
568
e4195791
MD
569 iter = g_new0(struct bt_iter, 1);
570 ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
571 if (ret) {
572 g_free(iter);
573 return NULL;
574 }
575 return iter;
576}
577
578void bt_iter_fini(struct bt_iter *iter)
6f3077a2 579{
7f89ddce 580 assert(iter);
dc81689e
JD
581 if (iter->stream_heap) {
582 heap_free(iter->stream_heap);
583 g_free(iter->stream_heap);
584 }
e003e871 585 iter->ctx->current_iterator = NULL;
95d36295 586 bt_context_put(iter->ctx);
e4195791 587}
95d36295 588
e4195791
MD
589void bt_iter_destroy(struct bt_iter *iter)
590{
7f89ddce 591 assert(iter);
e4195791 592 bt_iter_fini(iter);
90fcbacc 593 g_free(iter);
6f3077a2
JD
594}
595
e8c92a62 596int bt_iter_next(struct bt_iter *iter)
6f3077a2
JD
597{
598 struct ctf_file_stream *file_stream, *removed;
599 int ret;
600
7f89ddce
MD
601 if (!iter)
602 return -EINVAL;
603
6f3077a2
JD
604 file_stream = heap_maximum(iter->stream_heap);
605 if (!file_stream) {
606 /* end of file for all streams */
607 ret = 0;
608 goto end;
609 }
610
611 ret = stream_read_event(file_stream);
612 if (ret == EOF) {
613 removed = heap_remove(iter->stream_heap);
614 assert(removed == file_stream);
615 ret = 0;
616 goto end;
617 } else if (ret) {
618 goto end;
619 }
620 /* Reinsert the file stream into the heap, and rebalance. */
621 removed = heap_replace_max(iter->stream_heap, file_stream);
622 assert(removed == file_stream);
623
624end:
625 return ret;
626}
This page took 0.086457 seconds and 4 git commands to generate.