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