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_timestamp
;
48 struct trace_collection
*tc
;
49 GArray
*stream_saved_pos
; /* Contains struct stream_saved_pos */
52 static int stream_read_event(struct ctf_file_stream
*sin
)
56 ret
= sin
->pos
.parent
.event_cb(&sin
->pos
.parent
, &sin
->parent
);
60 fprintf(stderr
, "[error] Reading event failed.\n");
67 * returns true if a < b, false otherwise.
69 int stream_compare(void *a
, void *b
)
71 struct ctf_file_stream
*s_a
= a
, *s_b
= b
;
73 if (s_a
->parent
.timestamp
< s_b
->parent
.timestamp
)
79 void bt_iter_free_pos(struct bt_iter_pos
*iter_pos
)
84 if (iter_pos
->type
== BT_SEEK_RESTORE
&& iter_pos
->u
.restore
) {
85 if (iter_pos
->u
.restore
->stream_saved_pos
) {
87 iter_pos
->u
.restore
->stream_saved_pos
,
90 g_free(iter_pos
->u
.restore
);
96 * seek_file_stream_by_timestamp
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
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.
106 static int seek_file_stream_by_timestamp(struct ctf_file_stream
*cfs
,
109 struct ctf_stream_pos
*stream_pos
;
110 struct packet_index
*index
;
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
)
120 stream_pos
->packet_seek(&stream_pos
->parent
, i
, SEEK_SET
);
122 ret
= stream_read_event(cfs
);
123 } while (cfs
->parent
.timestamp
< timestamp
&& ret
== 0);
125 /* Can return either EOF, 0, or error (> 0). */
129 * Cannot find the timestamp within the stream packets, return
136 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
137 * the corresponding timestamp
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.
144 static int seek_ctf_trace_by_timestamp(struct ctf_trace
*tin
,
145 uint64_t timestamp
, struct ptr_heap
*stream_heap
)
150 /* for each stream_class */
151 for (i
= 0; i
< tin
->streams
->len
; i
++) {
152 struct ctf_stream_declaration
*stream_class
;
154 stream_class
= g_ptr_array_index(tin
->streams
, i
);
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
;
162 stream
= g_ptr_array_index(stream_class
->streams
, j
);
165 cfs
= container_of(stream
, struct ctf_file_stream
,
167 ret
= seek_file_stream_by_timestamp(cfs
, timestamp
);
170 ret
= heap_insert(stream_heap
, cfs
);
172 /* Return positive error. */
176 } else if (ret
> 0) {
178 * Error in seek (not EOF), failure.
182 /* on EOF just do not put stream into heap. */
186 return found
? 0 : EOF
;
189 int bt_iter_set_pos(struct bt_iter
*iter
, const struct bt_iter_pos
*iter_pos
)
191 struct trace_collection
*tc
;
194 switch (iter_pos
->type
) {
195 case BT_SEEK_RESTORE
:
196 if (!iter_pos
->u
.restore
)
199 heap_free(iter
->stream_heap
);
200 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
204 for (i
= 0; i
< iter_pos
->u
.restore
->stream_saved_pos
->len
;
206 struct stream_saved_pos
*saved_pos
;
207 struct ctf_stream_pos
*stream_pos
;
208 struct ctf_stream_definition
*stream
;
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
;
216 stream_pos
->packet_seek(&stream_pos
->parent
,
217 saved_pos
->cur_index
, SEEK_SET
);
220 * the timestamp needs to be restored after
221 * packet_seek, because this function resets
222 * the timestamp to the beginning of the packet
224 stream
->timestamp
= saved_pos
->current_timestamp
;
225 stream_pos
->offset
= saved_pos
->offset
;
226 stream_pos
->last_offset
= saved_pos
->offset
;
228 stream
->prev_timestamp
= 0;
229 stream
->prev_timestamp_end
= 0;
230 stream
->consumed
= 0;
232 printf_debug("restored to cur_index = %zd and "
233 "offset = %zd, timestamp = %" PRIu64
"\n",
234 stream_pos
->cur_index
,
235 stream_pos
->offset
, stream
->timestamp
);
237 stream_read_event(saved_pos
->file_stream
);
240 ret
= heap_insert(iter
->stream_heap
,
241 saved_pos
->file_stream
);
249 heap_free(iter
->stream_heap
);
250 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
254 /* for each trace in the trace_collection */
255 for (i
= 0; i
< tc
->array
->len
; i
++) {
256 struct ctf_trace
*tin
;
257 struct trace_descriptor
*td_read
;
259 td_read
= g_ptr_array_index(tc
->array
, i
);
262 tin
= container_of(td_read
, struct ctf_trace
, parent
);
264 ret
= seek_ctf_trace_by_timestamp(tin
,
265 iter_pos
->u
.seek_time
,
268 * Positive errors are failure. Negative value
269 * is EOF (for which we continue with other
270 * traces). 0 is success. Note: on EOF, it just
271 * means that no stream has been added to the
272 * iterator for that trace, which is fine.
274 if (ret
!= 0 && ret
!= EOF
)
280 for (i
= 0; i
< tc
->array
->len
; i
++) {
281 struct ctf_trace
*tin
;
282 struct trace_descriptor
*td_read
;
285 td_read
= g_ptr_array_index(tc
->array
, i
);
288 tin
= container_of(td_read
, struct ctf_trace
, parent
);
290 /* Populate heap with each stream */
291 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
293 struct ctf_stream_declaration
*stream
;
296 stream
= g_ptr_array_index(tin
->streams
,
300 for (filenr
= 0; filenr
< stream
->streams
->len
;
302 struct ctf_file_stream
*file_stream
;
303 file_stream
= g_ptr_array_index(
308 ret
= babeltrace_filestream_seek(
309 file_stream
, iter_pos
,
311 if (ret
!= 0 && ret
!= EOF
) {
319 /* not implemented */
326 heap_free(iter
->stream_heap
);
327 if (heap_init(iter
->stream_heap
, 0, stream_compare
) < 0) {
328 heap_free(iter
->stream_heap
);
329 g_free(iter
->stream_heap
);
330 iter
->stream_heap
= NULL
;
336 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
338 struct bt_iter_pos
*pos
;
339 struct trace_collection
*tc
= iter
->ctx
->tc
;
340 int i
, stream_class_id
, stream_id
;
342 pos
= g_new0(struct bt_iter_pos
, 1);
343 pos
->type
= BT_SEEK_RESTORE
;
344 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
345 pos
->u
.restore
->tc
= tc
;
346 pos
->u
.restore
->stream_saved_pos
= g_array_new(FALSE
, TRUE
,
347 sizeof(struct stream_saved_pos
));
348 if (!pos
->u
.restore
->stream_saved_pos
)
351 for (i
= 0; i
< tc
->array
->len
; i
++) {
352 struct ctf_trace
*tin
;
353 struct trace_descriptor
*td_read
;
355 td_read
= g_ptr_array_index(tc
->array
, i
);
358 tin
= container_of(td_read
, struct ctf_trace
, parent
);
360 for (stream_class_id
= 0; stream_class_id
< tin
->streams
->len
;
362 struct ctf_stream_declaration
*stream_class
;
364 stream_class
= g_ptr_array_index(tin
->streams
,
369 stream_id
< stream_class
->streams
->len
;
371 struct ctf_stream_definition
*stream
;
372 struct ctf_file_stream
*cfs
;
373 struct stream_saved_pos saved_pos
;
375 stream
= g_ptr_array_index(
376 stream_class
->streams
,
380 cfs
= container_of(stream
,
381 struct ctf_file_stream
,
384 saved_pos
.file_stream
= cfs
;
385 saved_pos
.cur_index
= cfs
->pos
.cur_index
;
388 * It is possible that an event was read during
389 * the last restore, never consumed and its
390 * position saved again. For this case, we
391 * need to check if the event really was
392 * consumed by the caller otherwise it is lost.
394 if (stream
->consumed
)
395 saved_pos
.offset
= cfs
->pos
.offset
;
397 saved_pos
.offset
= cfs
->pos
.last_offset
;
399 saved_pos
.current_timestamp
= stream
->timestamp
;
402 pos
->u
.restore
->stream_saved_pos
,
405 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
407 "timestamp = %" PRIu64
"\n",
408 stream
->stream_id
, saved_pos
.cur_index
,
410 saved_pos
.current_timestamp
);
421 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
424 struct bt_iter_pos
*pos
;
426 pos
= g_new0(struct bt_iter_pos
, 1);
427 pos
->type
= BT_SEEK_TIME
;
428 pos
->u
.seek_time
= timestamp
;
433 * babeltrace_filestream_seek: seek a filestream to given position.
435 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
437 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
438 const struct bt_iter_pos
*begin_pos
,
439 unsigned long stream_id
)
443 switch (begin_pos
->type
) {
446 * just insert into the heap we should already know
451 file_stream
->pos
.packet_seek(&file_stream
->pos
.parent
,
453 ret
= stream_read_event(file_stream
);
456 case BT_SEEK_RESTORE
:
459 assert(0); /* Not yet defined */
466 * bt_iter_seek: seek iterator to given position.
468 int bt_iter_seek(struct bt_iter
*iter
,
469 const struct bt_iter_pos
*begin_pos
)
473 struct trace_collection
*tc
= iter
->ctx
->tc
;
475 for (i
= 0; i
< tc
->array
->len
; i
++) {
476 struct ctf_trace
*tin
;
477 struct trace_descriptor
*td_read
;
479 td_read
= g_ptr_array_index(tc
->array
, i
);
482 tin
= container_of(td_read
, struct ctf_trace
, parent
);
484 /* Populate heap with each stream */
485 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
487 struct ctf_stream_declaration
*stream
;
490 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
493 for (filenr
= 0; filenr
< stream
->streams
->len
;
495 struct ctf_file_stream
*file_stream
;
497 file_stream
= g_ptr_array_index(stream
->streams
,
501 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
512 int bt_iter_init(struct bt_iter
*iter
,
513 struct bt_context
*ctx
,
514 const struct bt_iter_pos
*begin_pos
,
515 const struct bt_iter_pos
*end_pos
)
520 if (ctx
->current_iterator
) {
525 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
526 iter
->end_pos
= end_pos
;
530 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
532 goto error_heap_init
;
534 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
535 struct ctf_trace
*tin
;
536 struct trace_descriptor
*td_read
;
538 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
541 tin
= container_of(td_read
, struct ctf_trace
, parent
);
543 /* Populate heap with each stream */
544 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
546 struct ctf_stream_declaration
*stream
;
549 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
552 for (filenr
= 0; filenr
< stream
->streams
->len
;
554 struct ctf_file_stream
*file_stream
;
556 file_stream
= g_ptr_array_index(stream
->streams
,
561 ret
= babeltrace_filestream_seek(
566 struct bt_iter_pos pos
;
567 pos
.type
= BT_SEEK_BEGIN
;
568 ret
= babeltrace_filestream_seek(
579 ret
= heap_insert(iter
->stream_heap
, file_stream
);
586 ctx
->current_iterator
= iter
;
590 heap_free(iter
->stream_heap
);
592 g_free(iter
->stream_heap
);
597 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
598 const struct bt_iter_pos
*begin_pos
,
599 const struct bt_iter_pos
*end_pos
)
601 struct bt_iter
*iter
;
604 iter
= g_new0(struct bt_iter
, 1);
605 ret
= bt_iter_init(iter
, ctx
, begin_pos
, end_pos
);
613 void bt_iter_fini(struct bt_iter
*iter
)
615 if (iter
->stream_heap
) {
616 heap_free(iter
->stream_heap
);
617 g_free(iter
->stream_heap
);
619 iter
->ctx
->current_iterator
= NULL
;
620 bt_context_put(iter
->ctx
);
623 void bt_iter_destroy(struct bt_iter
*iter
)
629 int bt_iter_next(struct bt_iter
*iter
)
631 struct ctf_file_stream
*file_stream
, *removed
;
634 file_stream
= heap_maximum(iter
->stream_heap
);
636 /* end of file for all streams */
641 ret
= stream_read_event(file_stream
);
643 removed
= heap_remove(iter
->stream_heap
);
644 assert(removed
== file_stream
);
650 /* Reinsert the file stream into the heap, and rebalance. */
651 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
652 assert(removed
== file_stream
);