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
->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 and EOF if we didn't find any packet
104 * containing the timestamp.
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);
131 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
132 * the corresponding timestamp
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
138 static int seek_ctf_trace_by_timestamp(struct ctf_trace
*tin
,
139 uint64_t timestamp
, struct ptr_heap
*stream_heap
)
144 /* for each stream_class */
145 for (i
= 0; i
< tin
->streams
->len
; i
++) {
146 struct ctf_stream_declaration
*stream_class
;
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
++) {
151 struct ctf_stream_definition
*stream
;
152 struct ctf_file_stream
*cfs
;
154 stream
= g_ptr_array_index(stream_class
->streams
, j
);
155 cfs
= container_of(stream
, struct ctf_file_stream
,
157 ret
= seek_file_stream_by_timestamp(cfs
, timestamp
);
160 ret
= heap_insert(stream_heap
, cfs
);
174 int bt_iter_set_pos(struct bt_iter
*iter
, const struct bt_iter_pos
*iter_pos
)
176 struct trace_collection
*tc
;
179 switch (iter_pos
->type
) {
180 case BT_SEEK_RESTORE
:
181 if (!iter_pos
->u
.restore
)
184 heap_free(iter
->stream_heap
);
185 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
189 for (i
= 0; i
< iter_pos
->u
.restore
->stream_saved_pos
->len
;
191 struct stream_saved_pos
*saved_pos
;
192 struct ctf_stream_pos
*stream_pos
;
193 struct ctf_stream_definition
*stream
;
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
;
201 stream_pos
->packet_seek(&stream_pos
->parent
,
202 saved_pos
->cur_index
, SEEK_SET
);
205 * the timestamp needs to be restored after
206 * packet_seek, because this function resets
207 * the timestamp to the beginning of the packet
209 stream
->timestamp
= saved_pos
->current_timestamp
;
210 stream_pos
->offset
= saved_pos
->offset
;
211 stream_pos
->last_offset
= saved_pos
->offset
;
213 stream
->prev_timestamp
= 0;
214 stream
->prev_timestamp_end
= 0;
215 stream
->consumed
= 0;
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
);
222 stream_read_event(saved_pos
->file_stream
);
225 ret
= heap_insert(iter
->stream_heap
,
226 saved_pos
->file_stream
);
233 heap_free(iter
->stream_heap
);
234 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
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
;
243 td_read
= g_ptr_array_index(tc
->array
, i
);
244 tin
= container_of(td_read
, struct ctf_trace
, parent
);
246 ret
= seek_ctf_trace_by_timestamp(tin
,
247 iter_pos
->u
.seek_time
,
255 for (i
= 0; i
< tc
->array
->len
; i
++) {
256 struct ctf_trace
*tin
;
257 struct trace_descriptor
*td_read
;
260 td_read
= g_ptr_array_index(tc
->array
, i
);
261 tin
= container_of(td_read
, struct ctf_trace
, parent
);
263 /* Populate heap with each stream */
264 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
266 struct ctf_stream_declaration
*stream
;
269 stream
= g_ptr_array_index(tin
->streams
,
273 for (filenr
= 0; filenr
< stream
->streams
->len
;
275 struct ctf_file_stream
*file_stream
;
276 file_stream
= g_ptr_array_index(
279 ret
= babeltrace_filestream_seek(
280 file_stream
, iter_pos
,
287 /* not implemented */
294 heap_free(iter
->stream_heap
);
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
;
304 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
306 struct bt_iter_pos
*pos
;
307 struct trace_collection
*tc
= iter
->ctx
->tc
;
308 int i
, stream_class_id
, stream_id
;
310 pos
= g_new0(struct bt_iter_pos
, 1);
311 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
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
)
318 for (i
= 0; i
< tc
->array
->len
; i
++) {
319 struct ctf_trace
*tin
;
320 struct trace_descriptor
*td_read
;
322 td_read
= g_ptr_array_index(tc
->array
, i
);
323 tin
= container_of(td_read
, struct ctf_trace
, parent
);
325 for (stream_class_id
= 0; stream_class_id
< tin
->streams
->len
;
327 struct ctf_stream_declaration
*stream_class
;
329 stream_class
= g_ptr_array_index(tin
->streams
,
332 stream_id
< stream_class
->streams
->len
;
334 struct ctf_stream_definition
*stream
;
335 struct ctf_file_stream
*cfs
;
336 struct stream_saved_pos saved_pos
;
338 stream
= g_ptr_array_index(
339 stream_class
->streams
,
341 cfs
= container_of(stream
,
342 struct ctf_file_stream
,
345 saved_pos
.file_stream
= cfs
;
346 saved_pos
.cur_index
= cfs
->pos
.cur_index
;
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.
355 if (stream
->consumed
)
356 saved_pos
.offset
= cfs
->pos
.offset
;
358 saved_pos
.offset
= cfs
->pos
.last_offset
;
360 saved_pos
.current_timestamp
= stream
->timestamp
;
363 pos
->u
.restore
->stream_saved_pos
,
366 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
368 "timestamp = %" PRIu64
"\n",
369 stream
->stream_id
, saved_pos
.cur_index
,
371 saved_pos
.current_timestamp
);
382 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
385 struct bt_iter_pos
*pos
;
387 pos
= g_new0(struct bt_iter_pos
, 1);
388 pos
->type
= BT_SEEK_TIME
;
389 pos
->u
.seek_time
= timestamp
;
394 * babeltrace_filestream_seek: seek a filestream to given position.
396 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
398 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
399 const struct bt_iter_pos
*begin_pos
,
400 unsigned long stream_id
)
404 switch (begin_pos
->type
) {
407 * just insert into the heap we should already know
412 file_stream
->pos
.packet_seek(&file_stream
->pos
.parent
,
414 ret
= stream_read_event(file_stream
);
417 case BT_SEEK_RESTORE
:
420 assert(0); /* Not yet defined */
427 * bt_iter_seek: seek iterator to given position.
429 int bt_iter_seek(struct bt_iter
*iter
,
430 const struct bt_iter_pos
*begin_pos
)
434 struct trace_collection
*tc
= iter
->ctx
->tc
;
436 for (i
= 0; i
< tc
->array
->len
; i
++) {
437 struct ctf_trace
*tin
;
438 struct trace_descriptor
*td_read
;
440 td_read
= g_ptr_array_index(tc
->array
, i
);
441 tin
= container_of(td_read
, struct ctf_trace
, parent
);
443 /* Populate heap with each stream */
444 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
446 struct ctf_stream_declaration
*stream
;
449 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
450 for (filenr
= 0; filenr
< stream
->streams
->len
;
452 struct ctf_file_stream
*file_stream
;
454 file_stream
= g_ptr_array_index(stream
->streams
,
456 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
467 int bt_iter_init(struct bt_iter
*iter
,
468 struct bt_context
*ctx
,
469 const struct bt_iter_pos
*begin_pos
,
470 const struct bt_iter_pos
*end_pos
)
475 if (ctx
->current_iterator
) {
480 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
481 iter
->end_pos
= end_pos
;
485 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
487 goto error_heap_init
;
489 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
490 struct ctf_trace
*tin
;
491 struct trace_descriptor
*td_read
;
493 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
494 tin
= container_of(td_read
, struct ctf_trace
, parent
);
496 /* Populate heap with each stream */
497 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
499 struct ctf_stream_declaration
*stream
;
502 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
505 for (filenr
= 0; filenr
< stream
->streams
->len
;
507 struct ctf_file_stream
*file_stream
;
509 file_stream
= g_ptr_array_index(stream
->streams
,
513 ret
= babeltrace_filestream_seek(
518 struct bt_iter_pos pos
;
519 pos
.type
= BT_SEEK_BEGIN
;
520 ret
= babeltrace_filestream_seek(
531 ret
= heap_insert(iter
->stream_heap
, file_stream
);
538 ctx
->current_iterator
= iter
;
542 heap_free(iter
->stream_heap
);
544 g_free(iter
->stream_heap
);
549 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
550 const struct bt_iter_pos
*begin_pos
,
551 const struct bt_iter_pos
*end_pos
)
553 struct bt_iter
*iter
;
556 iter
= g_new0(struct bt_iter
, 1);
557 ret
= bt_iter_init(iter
, ctx
, begin_pos
, end_pos
);
565 void bt_iter_fini(struct bt_iter
*iter
)
567 if (iter
->stream_heap
) {
568 heap_free(iter
->stream_heap
);
569 g_free(iter
->stream_heap
);
571 iter
->ctx
->current_iterator
= NULL
;
572 bt_context_put(iter
->ctx
);
575 void bt_iter_destroy(struct bt_iter
*iter
)
581 int bt_iter_next(struct bt_iter
*iter
)
583 struct ctf_file_stream
*file_stream
, *removed
;
586 file_stream
= heap_maximum(iter
->stream_heap
);
588 /* end of file for all streams */
593 ret
= stream_read_event(file_stream
);
595 removed
= heap_remove(iter
->stream_heap
);
596 assert(removed
== file_stream
);
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
);