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