Fix: error path if heap_init fails
[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)
bed54c92 202 goto error_heap_init;
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;
3a25e036 226 stream_pos->last_offset = LAST_OFFSET_POISON;
90fcbacc
JD
227
228 stream->prev_timestamp = 0;
229 stream->prev_timestamp_end = 0;
90fcbacc
JD
230
231 printf_debug("restored to cur_index = %zd and "
232 "offset = %zd, timestamp = %" PRIu64 "\n",
233 stream_pos->cur_index,
234 stream_pos->offset, stream->timestamp);
235
236 stream_read_event(saved_pos->file_stream);
237
238 /* Add to heap */
239 ret = heap_insert(iter->stream_heap,
240 saved_pos->file_stream);
241 if (ret)
242 goto error;
243 }
5acdc773 244 return 0;
dc81689e
JD
245 case BT_SEEK_TIME:
246 tc = iter->ctx->tc;
247
dc81689e
JD
248 heap_free(iter->stream_heap);
249 ret = heap_init(iter->stream_heap, 0, stream_compare);
250 if (ret < 0)
bed54c92 251 goto error_heap_init;
dc81689e
JD
252
253 /* for each trace in the trace_collection */
254 for (i = 0; i < tc->array->len; i++) {
255 struct ctf_trace *tin;
256 struct trace_descriptor *td_read;
257
258 td_read = g_ptr_array_index(tc->array, i);
e32cb1e4
MD
259 if (!td_read)
260 continue;
dc81689e
JD
261 tin = container_of(td_read, struct ctf_trace, parent);
262
263 ret = seek_ctf_trace_by_timestamp(tin,
264 iter_pos->u.seek_time,
265 iter->stream_heap);
e32cb1e4
MD
266 /*
267 * Positive errors are failure. Negative value
268 * is EOF (for which we continue with other
269 * traces). 0 is success. Note: on EOF, it just
270 * means that no stream has been added to the
271 * iterator for that trace, which is fine.
272 */
273 if (ret != 0 && ret != EOF)
dc81689e
JD
274 goto error;
275 }
276 return 0;
55c21ea5
JD
277 case BT_SEEK_BEGIN:
278 tc = iter->ctx->tc;
db8a4511
JD
279 heap_free(iter->stream_heap);
280 ret = heap_init(iter->stream_heap, 0, stream_compare);
281 if (ret < 0)
bed54c92 282 goto error_heap_init;
db8a4511 283
55c21ea5
JD
284 for (i = 0; i < tc->array->len; i++) {
285 struct ctf_trace *tin;
286 struct trace_descriptor *td_read;
287 int stream_id;
288
289 td_read = g_ptr_array_index(tc->array, i);
e32cb1e4
MD
290 if (!td_read)
291 continue;
55c21ea5
JD
292 tin = container_of(td_read, struct ctf_trace, parent);
293
294 /* Populate heap with each stream */
295 for (stream_id = 0; stream_id < tin->streams->len;
296 stream_id++) {
f380e105 297 struct ctf_stream_declaration *stream;
55c21ea5
JD
298 int filenr;
299
300 stream = g_ptr_array_index(tin->streams,
301 stream_id);
302 if (!stream)
303 continue;
304 for (filenr = 0; filenr < stream->streams->len;
305 filenr++) {
306 struct ctf_file_stream *file_stream;
307 file_stream = g_ptr_array_index(
308 stream->streams,
309 filenr);
e32cb1e4
MD
310 if (!file_stream)
311 continue;
55c21ea5
JD
312 ret = babeltrace_filestream_seek(
313 file_stream, iter_pos,
314 stream_id);
e32cb1e4
MD
315 if (ret != 0 && ret != EOF) {
316 goto error;
317 }
db8a4511
JD
318 ret = heap_insert(iter->stream_heap, file_stream);
319 if (ret)
320 goto error;
55c21ea5
JD
321 }
322 }
323 }
324 break;
dc81689e
JD
325 default:
326 /* not implemented */
7344374e 327 return -EINVAL;
90fcbacc
JD
328 }
329
330 return 0;
dc81689e 331
90fcbacc
JD
332error:
333 heap_free(iter->stream_heap);
bed54c92 334error_heap_init:
dc81689e
JD
335 if (heap_init(iter->stream_heap, 0, stream_compare) < 0) {
336 heap_free(iter->stream_heap);
337 g_free(iter->stream_heap);
338 iter->stream_heap = NULL;
339 ret = -ENOMEM;
340 }
bed54c92 341
dc81689e 342 return ret;
90fcbacc
JD
343}
344
345struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
346{
347 struct bt_iter_pos *pos;
348 struct trace_collection *tc = iter->ctx->tc;
6fabd7af
JD
349 struct ctf_file_stream *file_stream = NULL, *removed;
350 struct ptr_heap iter_heap_copy;
351 int ret;
90fcbacc
JD
352
353 pos = g_new0(struct bt_iter_pos, 1);
5acdc773 354 pos->type = BT_SEEK_RESTORE;
90fcbacc 355 pos->u.restore = g_new0(struct bt_saved_pos, 1);
90fcbacc
JD
356 pos->u.restore->tc = tc;
357 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
358 sizeof(struct stream_saved_pos));
359 if (!pos->u.restore->stream_saved_pos)
360 goto error;
361
6fabd7af
JD
362 ret = heap_copy(&iter_heap_copy, iter->stream_heap);
363 if (ret < 0)
364 goto error_heap;
90fcbacc 365
6fabd7af
JD
366 /* iterate over each stream in the heap */
367 file_stream = heap_maximum(&iter_heap_copy);
368 while (file_stream != NULL) {
369 struct stream_saved_pos saved_pos;
90fcbacc 370
6fabd7af
JD
371 assert(file_stream->pos.last_offset != LAST_OFFSET_POISON);
372 saved_pos.offset = file_stream->pos.last_offset;
373 saved_pos.file_stream = file_stream;
374 saved_pos.cur_index = file_stream->pos.cur_index;
90fcbacc 375
6fabd7af 376 saved_pos.current_timestamp = file_stream->parent.timestamp;
90fcbacc 377
6fabd7af
JD
378 g_array_append_val(
379 pos->u.restore->stream_saved_pos,
380 saved_pos);
90fcbacc 381
6fabd7af
JD
382 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
383 "offset : %zd, "
384 "timestamp = %" PRIu64 "\n",
385 file_stream->parent.stream_id,
386 saved_pos.cur_index, saved_pos.offset,
387 saved_pos.current_timestamp);
388
389 /* remove the stream from the heap copy */
390 removed = heap_remove(&iter_heap_copy);
391 assert(removed == file_stream);
392
393 file_stream = heap_maximum(&iter_heap_copy);
394 }
395 heap_free(&iter_heap_copy);
90fcbacc
JD
396 return pos;
397
6fabd7af
JD
398error_heap:
399 g_array_free(pos->u.restore->stream_saved_pos, TRUE);
90fcbacc 400error:
6fabd7af 401 g_free(pos);
90fcbacc
JD
402 return NULL;
403}
404
dc81689e
JD
405struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
406 uint64_t timestamp)
407{
408 struct bt_iter_pos *pos;
409
410 pos = g_new0(struct bt_iter_pos, 1);
411 pos->type = BT_SEEK_TIME;
412 pos->u.seek_time = timestamp;
413 return pos;
414}
415
6f3077a2
JD
416/*
417 * babeltrace_filestream_seek: seek a filestream to given position.
418 *
419 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
420 */
421static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
e8c92a62 422 const struct bt_iter_pos *begin_pos,
6f3077a2
JD
423 unsigned long stream_id)
424{
425 int ret = 0;
426
427 switch (begin_pos->type) {
428 case BT_SEEK_CUR:
429 /*
430 * just insert into the heap we should already know
431 * the timestamps
432 */
433 break;
434 case BT_SEEK_BEGIN:
06789ffd 435 file_stream->pos.packet_seek(&file_stream->pos.parent,
d6425aaf 436 0, SEEK_SET);
6f3077a2
JD
437 ret = stream_read_event(file_stream);
438 break;
439 case BT_SEEK_TIME:
440 case BT_SEEK_RESTORE:
441 case BT_SEEK_END:
442 default:
443 assert(0); /* Not yet defined */
444 }
445
446 return ret;
447}
448
e4195791
MD
449int bt_iter_init(struct bt_iter *iter,
450 struct bt_context *ctx,
04ae3991
MD
451 const struct bt_iter_pos *begin_pos,
452 const struct bt_iter_pos *end_pos)
6f3077a2
JD
453{
454 int i, stream_id;
455 int ret = 0;
6f3077a2 456
e003e871
JD
457 if (ctx->current_iterator) {
458 ret = -1;
459 goto error_ctx;
460 }
461
6f3077a2 462 iter->stream_heap = g_new(struct ptr_heap, 1);
6f3077a2 463 iter->end_pos = end_pos;
6cba487f 464 bt_context_get(ctx);
95d36295 465 iter->ctx = ctx;
6f3077a2
JD
466
467 ret = heap_init(iter->stream_heap, 0, stream_compare);
468 if (ret < 0)
469 goto error_heap_init;
470
95d36295 471 for (i = 0; i < ctx->tc->array->len; i++) {
6f3077a2
JD
472 struct ctf_trace *tin;
473 struct trace_descriptor *td_read;
474
95d36295 475 td_read = g_ptr_array_index(ctx->tc->array, i);
e32cb1e4
MD
476 if (!td_read)
477 continue;
6f3077a2
JD
478 tin = container_of(td_read, struct ctf_trace, parent);
479
480 /* Populate heap with each stream */
481 for (stream_id = 0; stream_id < tin->streams->len;
482 stream_id++) {
f380e105 483 struct ctf_stream_declaration *stream;
6f3077a2
JD
484 int filenr;
485
486 stream = g_ptr_array_index(tin->streams, stream_id);
487 if (!stream)
488 continue;
489 for (filenr = 0; filenr < stream->streams->len;
490 filenr++) {
491 struct ctf_file_stream *file_stream;
492
493 file_stream = g_ptr_array_index(stream->streams,
494 filenr);
e32cb1e4
MD
495 if (!file_stream)
496 continue;
6f3077a2 497 if (begin_pos) {
b42d4e4e
JD
498 ret = babeltrace_filestream_seek(
499 file_stream,
500 begin_pos,
6f3077a2 501 stream_id);
b42d4e4e
JD
502 } else {
503 struct bt_iter_pos pos;
504 pos.type = BT_SEEK_BEGIN;
505 ret = babeltrace_filestream_seek(
506 file_stream, &pos,
507 stream_id);
508 }
509 if (ret == EOF) {
510 ret = 0;
511 continue;
512 } else if (ret) {
513 goto error;
6f3077a2
JD
514 }
515 /* Add to heap */
516 ret = heap_insert(iter->stream_heap, file_stream);
517 if (ret)
518 goto error;
519 }
520 }
521 }
522
e003e871 523 ctx->current_iterator = iter;
e4195791 524 return 0;
6f3077a2
JD
525
526error:
527 heap_free(iter->stream_heap);
528error_heap_init:
529 g_free(iter->stream_heap);
bed54c92 530 iter->stream_heap = NULL;
e003e871 531error_ctx:
e4195791 532 return ret;
6f3077a2
JD
533}
534
e4195791 535struct bt_iter *bt_iter_create(struct bt_context *ctx,
04ae3991
MD
536 const struct bt_iter_pos *begin_pos,
537 const struct bt_iter_pos *end_pos)
e4195791
MD
538{
539 struct bt_iter *iter;
540 int ret;
541
542 iter = g_new0(struct bt_iter, 1);
543 ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
544 if (ret) {
545 g_free(iter);
546 return NULL;
547 }
548 return iter;
549}
550
551void bt_iter_fini(struct bt_iter *iter)
6f3077a2 552{
dc81689e
JD
553 if (iter->stream_heap) {
554 heap_free(iter->stream_heap);
555 g_free(iter->stream_heap);
556 }
e003e871 557 iter->ctx->current_iterator = NULL;
95d36295 558 bt_context_put(iter->ctx);
e4195791 559}
95d36295 560
e4195791
MD
561void bt_iter_destroy(struct bt_iter *iter)
562{
563 bt_iter_fini(iter);
90fcbacc 564 g_free(iter);
6f3077a2
JD
565}
566
e8c92a62 567int bt_iter_next(struct bt_iter *iter)
6f3077a2
JD
568{
569 struct ctf_file_stream *file_stream, *removed;
570 int ret;
571
572 file_stream = heap_maximum(iter->stream_heap);
573 if (!file_stream) {
574 /* end of file for all streams */
575 ret = 0;
576 goto end;
577 }
578
579 ret = stream_read_event(file_stream);
580 if (ret == EOF) {
581 removed = heap_remove(iter->stream_heap);
582 assert(removed == file_stream);
583 ret = 0;
584 goto end;
585 } else if (ret) {
586 goto end;
587 }
588 /* Reinsert the file stream into the heap, and rebalance. */
589 removed = heap_replace_max(iter->stream_heap, file_stream);
590 assert(removed == file_stream);
591
592end:
593 return ret;
594}
This page took 0.057622 seconds and 4 git commands to generate.