Fix: iterator set_pos
[babeltrace.git] / lib / iterator.c
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>
23 #include <babeltrace/context.h>
24 #include <babeltrace/context-internal.h>
25 #include <babeltrace/iterator-internal.h>
26 #include <babeltrace/iterator.h>
27 #include <babeltrace/prio_heap.h>
28 #include <babeltrace/ctf/metadata.h>
29 #include <babeltrace/ctf/events.h>
30 #include <inttypes.h>
31
32 static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
33 const struct bt_iter_pos *begin_pos,
34 unsigned long stream_id);
35
36 struct 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. */
44 uint64_t current_timestamp;
45 };
46
47 struct bt_saved_pos {
48 struct trace_collection *tc;
49 GArray *stream_saved_pos; /* Contains struct stream_saved_pos */
50 };
51
52 static 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) {
60 fprintf(stderr, "[error] Reading event failed.\n");
61 return ret;
62 }
63 return 0;
64 }
65
66 /*
67 * returns true if a < b, false otherwise.
68 */
69 int 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
79 void bt_iter_free_pos(struct bt_iter_pos *iter_pos)
80 {
81 if (!iter_pos)
82 return;
83
84 if (iter_pos->type == BT_SEEK_RESTORE && 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);
89 }
90 g_free(iter_pos->u.restore);
91 }
92 g_free(iter_pos);
93 }
94
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, EOF if we didn't find any packet
104 * containing the timestamp, or a positive integer for error.
105 */
106 static 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);
117 if (index->timestamp_end < timestamp)
118 continue;
119
120 stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET);
121 do {
122 ret = stream_read_event(cfs);
123 } while (cfs->parent.timestamp < timestamp && ret == 0);
124
125 /* Can return either EOF, 0, or error (> 0). */
126 return ret;
127 }
128 /*
129 * Cannot find the timestamp within the stream packets, return
130 * EOF.
131 */
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
141 * user the timestamp is out of the scope.
142 * On other errors, return positive value.
143 */
144 static 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;
148 int found = 0;
149
150 /* for each stream_class */
151 for (i = 0; i < tin->streams->len; i++) {
152 struct ctf_stream_declaration *stream_class;
153
154 stream_class = g_ptr_array_index(tin->streams, i);
155 if (!stream_class)
156 continue;
157 /* for each file_stream */
158 for (j = 0; j < stream_class->streams->len; j++) {
159 struct ctf_stream_definition *stream;
160 struct ctf_file_stream *cfs;
161
162 stream = g_ptr_array_index(stream_class->streams, j);
163 if (!stream)
164 continue;
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);
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;
181 }
182 /* on EOF just do not put stream into heap. */
183 }
184 }
185
186 return found ? 0 : EOF;
187 }
188
189 int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos)
190 {
191 struct trace_collection *tc;
192 int i, ret;
193
194 switch (iter_pos->type) {
195 case BT_SEEK_RESTORE:
196 if (!iter_pos->u.restore)
197 return -EINVAL;
198
199 heap_free(iter->stream_heap);
200 ret = heap_init(iter->stream_heap, 0, stream_compare);
201 if (ret < 0)
202 goto error;
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;
208 struct ctf_stream_definition *stream;
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;
215
216 stream_pos->packet_seek(&stream_pos->parent,
217 saved_pos->cur_index, SEEK_SET);
218
219 /*
220 * the timestamp needs to be restored after
221 * packet_seek, because this function resets
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 = LAST_OFFSET_POISON;
227
228 stream->prev_timestamp = 0;
229 stream->prev_timestamp_end = 0;
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 }
244 return 0;
245 case BT_SEEK_TIME:
246 tc = iter->ctx->tc;
247
248 heap_free(iter->stream_heap);
249 ret = heap_init(iter->stream_heap, 0, stream_compare);
250 if (ret < 0)
251 goto error;
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);
259 if (!td_read)
260 continue;
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);
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)
274 goto error;
275 }
276 return 0;
277 case BT_SEEK_BEGIN:
278 tc = iter->ctx->tc;
279 for (i = 0; i < tc->array->len; i++) {
280 struct ctf_trace *tin;
281 struct trace_descriptor *td_read;
282 int stream_id;
283
284 td_read = g_ptr_array_index(tc->array, i);
285 if (!td_read)
286 continue;
287 tin = container_of(td_read, struct ctf_trace, parent);
288
289 /* Populate heap with each stream */
290 for (stream_id = 0; stream_id < tin->streams->len;
291 stream_id++) {
292 struct ctf_stream_declaration *stream;
293 int filenr;
294
295 stream = g_ptr_array_index(tin->streams,
296 stream_id);
297 if (!stream)
298 continue;
299 for (filenr = 0; filenr < stream->streams->len;
300 filenr++) {
301 struct ctf_file_stream *file_stream;
302 file_stream = g_ptr_array_index(
303 stream->streams,
304 filenr);
305 if (!file_stream)
306 continue;
307 ret = babeltrace_filestream_seek(
308 file_stream, iter_pos,
309 stream_id);
310 if (ret != 0 && ret != EOF) {
311 goto error;
312 }
313 }
314 }
315 }
316 break;
317 default:
318 /* not implemented */
319 return -EINVAL;
320 }
321
322 return 0;
323
324 error:
325 heap_free(iter->stream_heap);
326 if (heap_init(iter->stream_heap, 0, stream_compare) < 0) {
327 heap_free(iter->stream_heap);
328 g_free(iter->stream_heap);
329 iter->stream_heap = NULL;
330 ret = -ENOMEM;
331 }
332 return ret;
333 }
334
335 struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter)
336 {
337 struct bt_iter_pos *pos;
338 struct trace_collection *tc = iter->ctx->tc;
339 struct ctf_file_stream *file_stream = NULL, *removed;
340 struct ptr_heap iter_heap_copy;
341 int ret;
342
343 pos = g_new0(struct bt_iter_pos, 1);
344 pos->type = BT_SEEK_RESTORE;
345 pos->u.restore = g_new0(struct bt_saved_pos, 1);
346 pos->u.restore->tc = tc;
347 pos->u.restore->stream_saved_pos = g_array_new(FALSE, TRUE,
348 sizeof(struct stream_saved_pos));
349 if (!pos->u.restore->stream_saved_pos)
350 goto error;
351
352 ret = heap_copy(&iter_heap_copy, iter->stream_heap);
353 if (ret < 0)
354 goto error_heap;
355
356 /* iterate over each stream in the heap */
357 file_stream = heap_maximum(&iter_heap_copy);
358 while (file_stream != NULL) {
359 struct stream_saved_pos saved_pos;
360
361 assert(file_stream->pos.last_offset != LAST_OFFSET_POISON);
362 saved_pos.offset = file_stream->pos.last_offset;
363 saved_pos.file_stream = file_stream;
364 saved_pos.cur_index = file_stream->pos.cur_index;
365
366 saved_pos.current_timestamp = file_stream->parent.timestamp;
367
368 g_array_append_val(
369 pos->u.restore->stream_saved_pos,
370 saved_pos);
371
372 printf_debug("stream : %" PRIu64 ", cur_index : %zd, "
373 "offset : %zd, "
374 "timestamp = %" PRIu64 "\n",
375 file_stream->parent.stream_id,
376 saved_pos.cur_index, saved_pos.offset,
377 saved_pos.current_timestamp);
378
379 /* remove the stream from the heap copy */
380 removed = heap_remove(&iter_heap_copy);
381 assert(removed == file_stream);
382
383 file_stream = heap_maximum(&iter_heap_copy);
384 }
385 heap_free(&iter_heap_copy);
386 return pos;
387
388 error_heap:
389 g_array_free(pos->u.restore->stream_saved_pos, TRUE);
390 error:
391 g_free(pos);
392 return NULL;
393 }
394
395 struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter,
396 uint64_t timestamp)
397 {
398 struct bt_iter_pos *pos;
399
400 pos = g_new0(struct bt_iter_pos, 1);
401 pos->type = BT_SEEK_TIME;
402 pos->u.seek_time = timestamp;
403 return pos;
404 }
405
406 /*
407 * babeltrace_filestream_seek: seek a filestream to given position.
408 *
409 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
410 */
411 static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
412 const struct bt_iter_pos *begin_pos,
413 unsigned long stream_id)
414 {
415 int ret = 0;
416
417 switch (begin_pos->type) {
418 case BT_SEEK_CUR:
419 /*
420 * just insert into the heap we should already know
421 * the timestamps
422 */
423 break;
424 case BT_SEEK_BEGIN:
425 file_stream->pos.packet_seek(&file_stream->pos.parent,
426 0, SEEK_SET);
427 ret = stream_read_event(file_stream);
428 break;
429 case BT_SEEK_TIME:
430 case BT_SEEK_RESTORE:
431 case BT_SEEK_END:
432 default:
433 assert(0); /* Not yet defined */
434 }
435
436 return ret;
437 }
438
439 /*
440 * bt_iter_seek: seek iterator to given position.
441 */
442 int bt_iter_seek(struct bt_iter *iter,
443 const struct bt_iter_pos *begin_pos)
444 {
445 int i, stream_id;
446 int ret = 0;
447 struct trace_collection *tc = iter->ctx->tc;
448
449 for (i = 0; i < tc->array->len; i++) {
450 struct ctf_trace *tin;
451 struct trace_descriptor *td_read;
452
453 td_read = g_ptr_array_index(tc->array, i);
454 if (!td_read)
455 continue;
456 tin = container_of(td_read, struct ctf_trace, parent);
457
458 /* Populate heap with each stream */
459 for (stream_id = 0; stream_id < tin->streams->len;
460 stream_id++) {
461 struct ctf_stream_declaration *stream;
462 int filenr;
463
464 stream = g_ptr_array_index(tin->streams, stream_id);
465 if (!stream)
466 continue;
467 for (filenr = 0; filenr < stream->streams->len;
468 filenr++) {
469 struct ctf_file_stream *file_stream;
470
471 file_stream = g_ptr_array_index(stream->streams,
472 filenr);
473 if (!file_stream)
474 continue;
475 ret = babeltrace_filestream_seek(file_stream, begin_pos,
476 stream_id);
477 if (ret < 0)
478 goto end;
479 }
480 }
481 }
482 end:
483 return ret;
484 }
485
486 int bt_iter_init(struct bt_iter *iter,
487 struct bt_context *ctx,
488 const struct bt_iter_pos *begin_pos,
489 const struct bt_iter_pos *end_pos)
490 {
491 int i, stream_id;
492 int ret = 0;
493
494 if (ctx->current_iterator) {
495 ret = -1;
496 goto error_ctx;
497 }
498
499 iter->stream_heap = g_new(struct ptr_heap, 1);
500 iter->end_pos = end_pos;
501 bt_context_get(ctx);
502 iter->ctx = ctx;
503
504 ret = heap_init(iter->stream_heap, 0, stream_compare);
505 if (ret < 0)
506 goto error_heap_init;
507
508 for (i = 0; i < ctx->tc->array->len; i++) {
509 struct ctf_trace *tin;
510 struct trace_descriptor *td_read;
511
512 td_read = g_ptr_array_index(ctx->tc->array, i);
513 if (!td_read)
514 continue;
515 tin = container_of(td_read, struct ctf_trace, parent);
516
517 /* Populate heap with each stream */
518 for (stream_id = 0; stream_id < tin->streams->len;
519 stream_id++) {
520 struct ctf_stream_declaration *stream;
521 int filenr;
522
523 stream = g_ptr_array_index(tin->streams, stream_id);
524 if (!stream)
525 continue;
526 for (filenr = 0; filenr < stream->streams->len;
527 filenr++) {
528 struct ctf_file_stream *file_stream;
529
530 file_stream = g_ptr_array_index(stream->streams,
531 filenr);
532 if (!file_stream)
533 continue;
534 if (begin_pos) {
535 ret = babeltrace_filestream_seek(
536 file_stream,
537 begin_pos,
538 stream_id);
539 } else {
540 struct bt_iter_pos pos;
541 pos.type = BT_SEEK_BEGIN;
542 ret = babeltrace_filestream_seek(
543 file_stream, &pos,
544 stream_id);
545 }
546 if (ret == EOF) {
547 ret = 0;
548 continue;
549 } else if (ret) {
550 goto error;
551 }
552 /* Add to heap */
553 ret = heap_insert(iter->stream_heap, file_stream);
554 if (ret)
555 goto error;
556 }
557 }
558 }
559
560 ctx->current_iterator = iter;
561 return 0;
562
563 error:
564 heap_free(iter->stream_heap);
565 error_heap_init:
566 g_free(iter->stream_heap);
567 error_ctx:
568 return ret;
569 }
570
571 struct bt_iter *bt_iter_create(struct bt_context *ctx,
572 const struct bt_iter_pos *begin_pos,
573 const struct bt_iter_pos *end_pos)
574 {
575 struct bt_iter *iter;
576 int ret;
577
578 iter = g_new0(struct bt_iter, 1);
579 ret = bt_iter_init(iter, ctx, begin_pos, end_pos);
580 if (ret) {
581 g_free(iter);
582 return NULL;
583 }
584 return iter;
585 }
586
587 void bt_iter_fini(struct bt_iter *iter)
588 {
589 if (iter->stream_heap) {
590 heap_free(iter->stream_heap);
591 g_free(iter->stream_heap);
592 }
593 iter->ctx->current_iterator = NULL;
594 bt_context_put(iter->ctx);
595 }
596
597 void bt_iter_destroy(struct bt_iter *iter)
598 {
599 bt_iter_fini(iter);
600 g_free(iter);
601 }
602
603 int bt_iter_next(struct bt_iter *iter)
604 {
605 struct ctf_file_stream *file_stream, *removed;
606 int ret;
607
608 file_stream = heap_maximum(iter->stream_heap);
609 if (!file_stream) {
610 /* end of file for all streams */
611 ret = 0;
612 goto end;
613 }
614
615 ret = stream_read_event(file_stream);
616 if (ret == EOF) {
617 removed = heap_remove(iter->stream_heap);
618 assert(removed == file_stream);
619 ret = 0;
620 goto end;
621 } else if (ret) {
622 goto end;
623 }
624 /* Reinsert the file stream into the heap, and rebalance. */
625 removed = heap_replace_max(iter->stream_heap, file_stream);
626 assert(removed == file_stream);
627
628 end:
629 return ret;
630 }
This page took 0.066927 seconds and 4 git commands to generate.