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