6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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>
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
);
36 struct stream_saved_pos
{
38 * Use file_stream pointer to check if the trace collection we
39 * restore to match the one we saved from, for each stream.
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_real_timestamp
;
45 uint64_t current_cycles_timestamp
;
49 struct trace_collection
*tc
;
50 GArray
*stream_saved_pos
; /* Contains struct stream_saved_pos */
53 static int stream_read_event(struct ctf_file_stream
*sin
)
57 ret
= sin
->pos
.parent
.event_cb(&sin
->pos
.parent
, &sin
->parent
);
61 fprintf(stderr
, "[error] Reading event failed.\n");
68 * returns true if a < b, false otherwise.
70 static int stream_compare(void *a
, void *b
)
72 struct ctf_file_stream
*s_a
= a
, *s_b
= b
;
74 if (s_a
->parent
.real_timestamp
< s_b
->parent
.real_timestamp
)
80 void bt_iter_free_pos(struct bt_iter_pos
*iter_pos
)
85 if (iter_pos
->type
== BT_SEEK_RESTORE
&& iter_pos
->u
.restore
) {
86 if (iter_pos
->u
.restore
->stream_saved_pos
) {
88 iter_pos
->u
.restore
->stream_saved_pos
,
91 g_free(iter_pos
->u
.restore
);
97 * seek_file_stream_by_timestamp
99 * Browse a filestream by index, if an index contains the timestamp passed in
100 * argument, seek inside the corresponding packet it until we find the event we
101 * are looking for (either the exact timestamp or the event just after the
104 * Return 0 if the seek succeded, EOF if we didn't find any packet
105 * containing the timestamp, or a positive integer for error.
107 * TODO: this should be turned into a binary search! It is currently
108 * doing a linear search in the packets. This is a O(n) operation on a
109 * very frequent code path.
111 static int seek_file_stream_by_timestamp(struct ctf_file_stream
*cfs
,
114 struct ctf_stream_pos
*stream_pos
;
115 struct packet_index
*index
;
118 stream_pos
= &cfs
->pos
;
119 for (i
= 0; i
< stream_pos
->packet_real_index
->len
; i
++) {
120 index
= &g_array_index(stream_pos
->packet_real_index
,
121 struct packet_index
, i
);
122 if (index
->timestamp_end
< timestamp
)
125 stream_pos
->packet_seek(&stream_pos
->parent
, i
, SEEK_SET
);
127 ret
= stream_read_event(cfs
);
128 } while (cfs
->parent
.real_timestamp
< timestamp
&& ret
== 0);
130 /* Can return either EOF, 0, or error (> 0). */
134 * Cannot find the timestamp within the stream packets, return
141 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
142 * the corresponding timestamp
144 * Return 0 on success.
145 * If the timestamp is not part of any file stream, return EOF to inform the
146 * user the timestamp is out of the scope.
147 * On other errors, return positive value.
149 static int seek_ctf_trace_by_timestamp(struct ctf_trace
*tin
,
150 uint64_t timestamp
, struct ptr_heap
*stream_heap
)
155 /* for each stream_class */
156 for (i
= 0; i
< tin
->streams
->len
; i
++) {
157 struct ctf_stream_declaration
*stream_class
;
159 stream_class
= g_ptr_array_index(tin
->streams
, i
);
162 /* for each file_stream */
163 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
164 struct ctf_stream_definition
*stream
;
165 struct ctf_file_stream
*cfs
;
167 stream
= g_ptr_array_index(stream_class
->streams
, j
);
170 cfs
= container_of(stream
, struct ctf_file_stream
,
172 ret
= seek_file_stream_by_timestamp(cfs
, timestamp
);
175 ret
= heap_insert(stream_heap
, cfs
);
177 /* Return positive error. */
181 } else if (ret
> 0) {
183 * Error in seek (not EOF), failure.
187 /* on EOF just do not put stream into heap. */
191 return found
? 0 : EOF
;
195 * Find timestamp of last event in the stream.
197 * Return value: 0 if OK, positive error value on error, EOF if no
200 static int find_max_timestamp_ctf_file_stream(struct ctf_file_stream
*cfs
,
201 uint64_t *timestamp_end
)
203 int ret
, count
= 0, i
;
204 uint64_t timestamp
= 0;
205 struct ctf_stream_pos
*stream_pos
;
207 stream_pos
= &cfs
->pos
;
209 * We start by the last packet, and iterate backwards until we
210 * either find at least one event, or we reach the first packet
211 * (some packets can be empty).
213 for (i
= stream_pos
->packet_real_index
->len
- 1; i
>= 0; i
--) {
214 stream_pos
->packet_seek(&stream_pos
->parent
, i
, SEEK_SET
);
216 /* read each event until we reach the end of the stream */
218 ret
= stream_read_event(cfs
);
221 timestamp
= cfs
->parent
.real_timestamp
;
234 *timestamp_end
= timestamp
;
237 /* Return EOF if no events were found */
245 * Find the stream within a stream class that contains the event with
246 * the largest timestamp, and save that timestamp.
248 * Return 0 if OK, EOF if no events were found in the streams, or
249 * positive value on error.
251 static int find_max_timestamp_ctf_stream_class(
252 struct ctf_stream_declaration
*stream_class
,
253 struct ctf_file_stream
**cfsp
,
254 uint64_t *max_timestamp
)
258 for (i
= 0; i
< stream_class
->streams
->len
; i
++) {
259 struct ctf_stream_definition
*stream
;
260 struct ctf_file_stream
*cfs
;
261 uint64_t current_max_ts
= 0;
263 stream
= g_ptr_array_index(stream_class
->streams
, i
);
266 cfs
= container_of(stream
, struct ctf_file_stream
, parent
);
267 ret
= find_max_timestamp_ctf_file_stream(cfs
, ¤t_max_ts
);
272 if (current_max_ts
>= *max_timestamp
) {
273 *max_timestamp
= current_max_ts
;
277 assert(ret
>= 0 || ret
== EOF
);
282 * seek_last_ctf_trace_collection: seek trace collection to last event.
284 * Return 0 if OK, EOF if no events were found, or positive error value
287 static int seek_last_ctf_trace_collection(struct trace_collection
*tc
,
288 struct ctf_file_stream
**cfsp
)
292 uint64_t max_timestamp
= 0;
297 /* For each trace in the trace_collection */
298 for (i
= 0; i
< tc
->array
->len
; i
++) {
299 struct ctf_trace
*tin
;
300 struct trace_descriptor
*td_read
;
302 td_read
= g_ptr_array_index(tc
->array
, i
);
305 tin
= container_of(td_read
, struct ctf_trace
, parent
);
306 /* For each stream_class in the trace */
307 for (j
= 0; j
< tin
->streams
->len
; j
++) {
308 struct ctf_stream_declaration
*stream_class
;
310 stream_class
= g_ptr_array_index(tin
->streams
, j
);
313 ret
= find_max_timestamp_ctf_stream_class(stream_class
,
314 cfsp
, &max_timestamp
);
319 assert(ret
== EOF
|| ret
== 0);
323 * Now we know in which file stream the last event is located,
324 * and we know its timestamp.
329 ret
= seek_file_stream_by_timestamp(*cfsp
, max_timestamp
);
336 int bt_iter_set_pos(struct bt_iter
*iter
, const struct bt_iter_pos
*iter_pos
)
338 struct trace_collection
*tc
;
341 if (!iter
|| !iter_pos
)
344 switch (iter_pos
->type
) {
345 case BT_SEEK_RESTORE
:
346 if (!iter_pos
->u
.restore
)
349 heap_free(iter
->stream_heap
);
350 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
352 goto error_heap_init
;
354 for (i
= 0; i
< iter_pos
->u
.restore
->stream_saved_pos
->len
;
356 struct stream_saved_pos
*saved_pos
;
357 struct ctf_stream_pos
*stream_pos
;
358 struct ctf_stream_definition
*stream
;
360 saved_pos
= &g_array_index(
361 iter_pos
->u
.restore
->stream_saved_pos
,
362 struct stream_saved_pos
, i
);
363 stream
= &saved_pos
->file_stream
->parent
;
364 stream_pos
= &saved_pos
->file_stream
->pos
;
366 stream_pos
->packet_seek(&stream_pos
->parent
,
367 saved_pos
->cur_index
, SEEK_SET
);
370 * the timestamp needs to be restored after
371 * packet_seek, because this function resets
372 * the timestamp to the beginning of the packet
374 stream
->real_timestamp
= saved_pos
->current_real_timestamp
;
375 stream
->cycles_timestamp
= saved_pos
->current_cycles_timestamp
;
376 stream_pos
->offset
= saved_pos
->offset
;
377 stream_pos
->last_offset
= LAST_OFFSET_POISON
;
379 stream
->prev_real_timestamp
= 0;
380 stream
->prev_real_timestamp_end
= 0;
381 stream
->prev_cycles_timestamp
= 0;
382 stream
->prev_cycles_timestamp_end
= 0;
384 printf_debug("restored to cur_index = %" PRId64
" and "
385 "offset = %" PRId64
", timestamp = %" PRIu64
"\n",
386 stream_pos
->cur_index
,
387 stream_pos
->offset
, stream
->real_timestamp
);
389 ret
= stream_read_event(saved_pos
->file_stream
);
395 ret
= heap_insert(iter
->stream_heap
,
396 saved_pos
->file_stream
);
404 heap_free(iter
->stream_heap
);
405 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
407 goto error_heap_init
;
409 /* for each trace in the trace_collection */
410 for (i
= 0; i
< tc
->array
->len
; i
++) {
411 struct ctf_trace
*tin
;
412 struct trace_descriptor
*td_read
;
414 td_read
= g_ptr_array_index(tc
->array
, i
);
417 tin
= container_of(td_read
, struct ctf_trace
, parent
);
419 ret
= seek_ctf_trace_by_timestamp(tin
,
420 iter_pos
->u
.seek_time
,
423 * Positive errors are failure. Negative value
424 * is EOF (for which we continue with other
425 * traces). 0 is success. Note: on EOF, it just
426 * means that no stream has been added to the
427 * iterator for that trace, which is fine.
429 if (ret
!= 0 && ret
!= EOF
)
435 heap_free(iter
->stream_heap
);
436 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
438 goto error_heap_init
;
440 for (i
= 0; i
< tc
->array
->len
; i
++) {
441 struct ctf_trace
*tin
;
442 struct trace_descriptor
*td_read
;
445 td_read
= g_ptr_array_index(tc
->array
, i
);
448 tin
= container_of(td_read
, struct ctf_trace
, parent
);
450 /* Populate heap with each stream */
451 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
453 struct ctf_stream_declaration
*stream
;
456 stream
= g_ptr_array_index(tin
->streams
,
460 for (filenr
= 0; filenr
< stream
->streams
->len
;
462 struct ctf_file_stream
*file_stream
;
463 file_stream
= g_ptr_array_index(
468 ret
= babeltrace_filestream_seek(
469 file_stream
, iter_pos
,
471 if (ret
!= 0 && ret
!= EOF
) {
475 /* Do not add EOF streams */
478 ret
= heap_insert(iter
->stream_heap
, file_stream
);
487 struct ctf_file_stream
*cfs
= NULL
;
490 ret
= seek_last_ctf_trace_collection(tc
, &cfs
);
491 if (ret
!= 0 || !cfs
)
493 /* remove all streams from the heap */
494 heap_free(iter
->stream_heap
);
495 /* Create a new empty heap */
496 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
499 /* Insert the stream that contains the last event */
500 ret
= heap_insert(iter
->stream_heap
, cfs
);
506 /* not implemented */
513 heap_free(iter
->stream_heap
);
515 if (heap_init(iter
->stream_heap
, 0, stream_compare
) < 0) {
516 heap_free(iter
->stream_heap
);
517 g_free(iter
->stream_heap
);
518 iter
->stream_heap
= NULL
;
525 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
527 struct bt_iter_pos
*pos
;
528 struct trace_collection
*tc
;
529 struct ctf_file_stream
*file_stream
= NULL
, *removed
;
530 struct ptr_heap iter_heap_copy
;
537 pos
= g_new0(struct bt_iter_pos
, 1);
538 pos
->type
= BT_SEEK_RESTORE
;
539 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
540 pos
->u
.restore
->tc
= tc
;
541 pos
->u
.restore
->stream_saved_pos
= g_array_new(FALSE
, TRUE
,
542 sizeof(struct stream_saved_pos
));
543 if (!pos
->u
.restore
->stream_saved_pos
)
546 ret
= heap_copy(&iter_heap_copy
, iter
->stream_heap
);
550 /* iterate over each stream in the heap */
551 file_stream
= heap_maximum(&iter_heap_copy
);
552 while (file_stream
!= NULL
) {
553 struct stream_saved_pos saved_pos
;
555 assert(file_stream
->pos
.last_offset
!= LAST_OFFSET_POISON
);
556 saved_pos
.offset
= file_stream
->pos
.last_offset
;
557 saved_pos
.file_stream
= file_stream
;
558 saved_pos
.cur_index
= file_stream
->pos
.cur_index
;
560 saved_pos
.current_real_timestamp
= file_stream
->parent
.real_timestamp
;
561 saved_pos
.current_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
564 pos
->u
.restore
->stream_saved_pos
,
567 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
569 "timestamp = %" PRIu64
"\n",
570 file_stream
->parent
.stream_id
,
571 saved_pos
.cur_index
, saved_pos
.offset
,
572 saved_pos
.current_real_timestamp
);
574 /* remove the stream from the heap copy */
575 removed
= heap_remove(&iter_heap_copy
);
576 assert(removed
== file_stream
);
578 file_stream
= heap_maximum(&iter_heap_copy
);
580 heap_free(&iter_heap_copy
);
584 g_array_free(pos
->u
.restore
->stream_saved_pos
, TRUE
);
590 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
593 struct bt_iter_pos
*pos
;
598 pos
= g_new0(struct bt_iter_pos
, 1);
599 pos
->type
= BT_SEEK_TIME
;
600 pos
->u
.seek_time
= timestamp
;
605 * babeltrace_filestream_seek: seek a filestream to given position.
607 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
609 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
610 const struct bt_iter_pos
*begin_pos
,
611 unsigned long stream_id
)
615 if (!file_stream
|| !begin_pos
)
618 switch (begin_pos
->type
) {
621 * just insert into the heap we should already know
626 file_stream
->pos
.packet_seek(&file_stream
->pos
.parent
,
628 ret
= stream_read_event(file_stream
);
631 case BT_SEEK_RESTORE
:
633 assert(0); /* Not yet defined */
639 int bt_iter_init(struct bt_iter
*iter
,
640 struct bt_context
*ctx
,
641 const struct bt_iter_pos
*begin_pos
,
642 const struct bt_iter_pos
*end_pos
)
650 if (ctx
->current_iterator
) {
655 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
656 iter
->end_pos
= end_pos
;
660 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
662 goto error_heap_init
;
664 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
665 struct ctf_trace
*tin
;
666 struct trace_descriptor
*td_read
;
668 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
671 tin
= container_of(td_read
, struct ctf_trace
, parent
);
673 /* Populate heap with each stream */
674 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
676 struct ctf_stream_declaration
*stream
;
679 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
682 for (filenr
= 0; filenr
< stream
->streams
->len
;
684 struct ctf_file_stream
*file_stream
;
686 file_stream
= g_ptr_array_index(stream
->streams
,
691 ret
= babeltrace_filestream_seek(
696 struct bt_iter_pos pos
;
697 pos
.type
= BT_SEEK_BEGIN
;
698 ret
= babeltrace_filestream_seek(
709 ret
= heap_insert(iter
->stream_heap
, file_stream
);
716 ctx
->current_iterator
= iter
;
720 heap_free(iter
->stream_heap
);
722 g_free(iter
->stream_heap
);
723 iter
->stream_heap
= NULL
;
728 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
729 const struct bt_iter_pos
*begin_pos
,
730 const struct bt_iter_pos
*end_pos
)
732 struct bt_iter
*iter
;
738 iter
= g_new0(struct bt_iter
, 1);
739 ret
= bt_iter_init(iter
, ctx
, begin_pos
, end_pos
);
747 void bt_iter_fini(struct bt_iter
*iter
)
750 if (iter
->stream_heap
) {
751 heap_free(iter
->stream_heap
);
752 g_free(iter
->stream_heap
);
754 iter
->ctx
->current_iterator
= NULL
;
755 bt_context_put(iter
->ctx
);
758 void bt_iter_destroy(struct bt_iter
*iter
)
765 int bt_iter_next(struct bt_iter
*iter
)
767 struct ctf_file_stream
*file_stream
, *removed
;
773 file_stream
= heap_maximum(iter
->stream_heap
);
775 /* end of file for all streams */
780 ret
= stream_read_event(file_stream
);
782 removed
= heap_remove(iter
->stream_heap
);
783 assert(removed
== file_stream
);
789 /* Reinsert the file stream into the heap, and rebalance. */
790 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
791 assert(removed
== file_stream
);