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 struct stream_saved_pos
{
34 * Use file_stream pointer to check if the trace collection we
35 * restore to match the one we saved from, for each stream.
37 struct ctf_file_stream
*file_stream
;
38 size_t cur_index
; /* current index in packet index */
39 ssize_t offset
; /* offset from base, in bits. EOF for end of file. */
40 uint64_t current_timestamp
;
44 struct trace_collection
*tc
;
45 GArray
*stream_saved_pos
; /* Contains struct stream_saved_pos */
48 static int stream_read_event(struct ctf_file_stream
*sin
)
52 ret
= sin
->pos
.parent
.event_cb(&sin
->pos
.parent
, &sin
->parent
);
56 fprintf(stderr
, "[error] Reading event failed.\n");
63 * returns true if a < b, false otherwise.
65 int stream_compare(void *a
, void *b
)
67 struct ctf_file_stream
*s_a
= a
, *s_b
= b
;
69 if (s_a
->parent
.timestamp
< s_b
->parent
.timestamp
)
75 void bt_iter_free_pos(struct bt_iter_pos
*iter_pos
)
80 if (iter_pos
->u
.restore
) {
81 if (iter_pos
->u
.restore
->stream_saved_pos
) {
83 iter_pos
->u
.restore
->stream_saved_pos
,
86 g_free(iter_pos
->u
.restore
);
92 * seek_file_stream_by_timestamp
94 * Browse a filestream by index, if an index contains the timestamp passed in
95 * argument, seek inside the corresponding packet it until we find the event we
96 * are looking for (either the exact timestamp or the event just after the
99 * Return 0 if the seek succeded and EOF if we didn't find any packet
100 * containing the timestamp.
102 static int seek_file_stream_by_timestamp(struct ctf_file_stream
*cfs
,
105 struct ctf_stream_pos
*stream_pos
;
106 struct packet_index
*index
;
109 stream_pos
= &cfs
->pos
;
110 for (i
= 0; i
< stream_pos
->packet_index
->len
; i
++) {
111 index
= &g_array_index(stream_pos
->packet_index
,
112 struct packet_index
, i
);
113 if (index
->timestamp_begin
>= timestamp
||
114 index
->timestamp_end
<= timestamp
)
117 stream_pos
->packet_seek(&stream_pos
->parent
, i
, SEEK_SET
);
118 while (cfs
->parent
.timestamp
< timestamp
) {
119 ret
= stream_read_event(cfs
);
129 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
130 * the corresponding timestamp
132 * Return 0 on success.
133 * If the timestamp is not part of any file stream, return EOF to inform the
134 * user the timestamp is out of the scope
136 static int seek_ctf_trace_by_timestamp(struct ctf_trace
*tin
,
137 uint64_t timestamp
, struct ptr_heap
*stream_heap
)
142 /* for each stream_class */
143 for (i
= 0; i
< tin
->streams
->len
; i
++) {
144 struct ctf_stream_class
*stream_class
;
146 stream_class
= g_ptr_array_index(tin
->streams
, i
);
147 /* for each file_stream */
148 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
149 struct ctf_stream
*stream
;
150 struct ctf_file_stream
*cfs
;
152 stream
= g_ptr_array_index(stream_class
->streams
, j
);
153 cfs
= container_of(stream
, struct ctf_file_stream
,
155 ret
= seek_file_stream_by_timestamp(cfs
, timestamp
);
158 ret
= heap_insert(stream_heap
, cfs
);
172 int bt_iter_set_pos(struct bt_iter
*iter
, const struct bt_iter_pos
*iter_pos
)
174 struct trace_collection
*tc
;
177 switch (iter_pos
->type
) {
178 case BT_SEEK_RESTORE
:
179 if (!iter_pos
->u
.restore
)
182 heap_free(iter
->stream_heap
);
183 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
187 for (i
= 0; i
< iter_pos
->u
.restore
->stream_saved_pos
->len
;
189 struct stream_saved_pos
*saved_pos
;
190 struct ctf_stream_pos
*stream_pos
;
191 struct ctf_stream
*stream
;
193 saved_pos
= &g_array_index(
194 iter_pos
->u
.restore
->stream_saved_pos
,
195 struct stream_saved_pos
, i
);
196 stream
= &saved_pos
->file_stream
->parent
;
197 stream_pos
= &saved_pos
->file_stream
->pos
;
199 stream_pos
->packet_seek(&stream_pos
->parent
,
200 saved_pos
->cur_index
, SEEK_SET
);
203 * the timestamp needs to be restored after
204 * packet_seek, because this function resets
205 * the timestamp to the beginning of the packet
207 stream
->timestamp
= saved_pos
->current_timestamp
;
208 stream_pos
->offset
= saved_pos
->offset
;
209 stream_pos
->last_offset
= saved_pos
->offset
;
211 stream
->prev_timestamp
= 0;
212 stream
->prev_timestamp_end
= 0;
213 stream
->consumed
= 0;
215 printf_debug("restored to cur_index = %zd and "
216 "offset = %zd, timestamp = %" PRIu64
"\n",
217 stream_pos
->cur_index
,
218 stream_pos
->offset
, stream
->timestamp
);
220 stream_read_event(saved_pos
->file_stream
);
223 ret
= heap_insert(iter
->stream_heap
,
224 saved_pos
->file_stream
);
231 if (!iter_pos
->u
.seek_time
)
234 heap_free(iter
->stream_heap
);
235 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
239 /* for each trace in the trace_collection */
240 for (i
= 0; i
< tc
->array
->len
; i
++) {
241 struct ctf_trace
*tin
;
242 struct trace_descriptor
*td_read
;
244 td_read
= g_ptr_array_index(tc
->array
, i
);
245 tin
= container_of(td_read
, struct ctf_trace
, parent
);
247 ret
= seek_ctf_trace_by_timestamp(tin
,
248 iter_pos
->u
.seek_time
,
255 /* not implemented */
264 heap_free(iter
->stream_heap
);
265 if (heap_init(iter
->stream_heap
, 0, stream_compare
) < 0) {
266 heap_free(iter
->stream_heap
);
267 g_free(iter
->stream_heap
);
268 iter
->stream_heap
= NULL
;
274 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
276 struct bt_iter_pos
*pos
;
277 struct trace_collection
*tc
= iter
->ctx
->tc
;
278 int i
, stream_class_id
, stream_id
;
280 pos
= g_new0(struct bt_iter_pos
, 1);
281 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
282 pos
->u
.restore
->tc
= tc
;
283 pos
->u
.restore
->stream_saved_pos
= g_array_new(FALSE
, TRUE
,
284 sizeof(struct stream_saved_pos
));
285 if (!pos
->u
.restore
->stream_saved_pos
)
288 for (i
= 0; i
< tc
->array
->len
; i
++) {
289 struct ctf_trace
*tin
;
290 struct trace_descriptor
*td_read
;
292 td_read
= g_ptr_array_index(tc
->array
, i
);
293 tin
= container_of(td_read
, struct ctf_trace
, parent
);
295 for (stream_class_id
= 0; stream_class_id
< tin
->streams
->len
;
297 struct ctf_stream_class
*stream_class
;
299 stream_class
= g_ptr_array_index(tin
->streams
,
302 stream_id
< stream_class
->streams
->len
;
304 struct ctf_stream
*stream
;
305 struct ctf_file_stream
*cfs
;
306 struct stream_saved_pos saved_pos
;
308 stream
= g_ptr_array_index(
309 stream_class
->streams
,
311 cfs
= container_of(stream
,
312 struct ctf_file_stream
,
315 saved_pos
.file_stream
= cfs
;
316 saved_pos
.cur_index
= cfs
->pos
.cur_index
;
319 * It is possible that an event was read during
320 * the last restore, never consumed and its
321 * position saved again. For this case, we
322 * need to check if the event really was
323 * consumed by the caller otherwise it is lost.
325 if (stream
->consumed
)
326 saved_pos
.offset
= cfs
->pos
.offset
;
328 saved_pos
.offset
= cfs
->pos
.last_offset
;
330 saved_pos
.current_timestamp
= stream
->timestamp
;
333 pos
->u
.restore
->stream_saved_pos
,
336 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
338 "timestamp = %" PRIu64
"\n",
339 stream
->stream_id
, saved_pos
.cur_index
,
341 saved_pos
.current_timestamp
);
352 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
355 struct bt_iter_pos
*pos
;
357 pos
= g_new0(struct bt_iter_pos
, 1);
358 pos
->type
= BT_SEEK_TIME
;
359 pos
->u
.seek_time
= timestamp
;
364 * babeltrace_filestream_seek: seek a filestream to given position.
366 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
368 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
369 const struct bt_iter_pos
*begin_pos
,
370 unsigned long stream_id
)
374 switch (begin_pos
->type
) {
377 * just insert into the heap we should already know
382 file_stream
->pos
.packet_seek(&file_stream
->pos
.parent
,
384 ret
= stream_read_event(file_stream
);
387 case BT_SEEK_RESTORE
:
390 assert(0); /* Not yet defined */
397 * bt_iter_seek: seek iterator to given position.
399 int bt_iter_seek(struct bt_iter
*iter
,
400 const struct bt_iter_pos
*begin_pos
)
404 struct trace_collection
*tc
= iter
->ctx
->tc
;
406 for (i
= 0; i
< tc
->array
->len
; i
++) {
407 struct ctf_trace
*tin
;
408 struct trace_descriptor
*td_read
;
410 td_read
= g_ptr_array_index(tc
->array
, i
);
411 tin
= container_of(td_read
, struct ctf_trace
, parent
);
413 /* Populate heap with each stream */
414 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
416 struct ctf_stream_class
*stream
;
419 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
420 for (filenr
= 0; filenr
< stream
->streams
->len
;
422 struct ctf_file_stream
*file_stream
;
424 file_stream
= g_ptr_array_index(stream
->streams
,
426 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
437 int bt_iter_init(struct bt_iter
*iter
,
438 struct bt_context
*ctx
,
439 struct bt_iter_pos
*begin_pos
,
440 struct bt_iter_pos
*end_pos
)
445 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
446 iter
->end_pos
= end_pos
;
450 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
452 goto error_heap_init
;
454 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
455 struct ctf_trace
*tin
;
456 struct trace_descriptor
*td_read
;
458 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
459 tin
= container_of(td_read
, struct ctf_trace
, parent
);
461 /* Populate heap with each stream */
462 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
464 struct ctf_stream_class
*stream
;
467 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
470 for (filenr
= 0; filenr
< stream
->streams
->len
;
472 struct ctf_file_stream
*file_stream
;
474 file_stream
= g_ptr_array_index(stream
->streams
,
478 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
488 ret
= heap_insert(iter
->stream_heap
, file_stream
);
498 heap_free(iter
->stream_heap
);
500 g_free(iter
->stream_heap
);
504 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
505 struct bt_iter_pos
*begin_pos
,
506 struct bt_iter_pos
*end_pos
)
508 struct bt_iter
*iter
;
511 iter
= g_new0(struct bt_iter
, 1);
512 ret
= bt_iter_init(iter
, ctx
, begin_pos
, end_pos
);
520 void bt_iter_fini(struct bt_iter
*iter
)
522 if (iter
->stream_heap
) {
523 heap_free(iter
->stream_heap
);
524 g_free(iter
->stream_heap
);
526 bt_context_put(iter
->ctx
);
529 void bt_iter_destroy(struct bt_iter
*iter
)
535 int bt_iter_next(struct bt_iter
*iter
)
537 struct ctf_file_stream
*file_stream
, *removed
;
540 file_stream
= heap_maximum(iter
->stream_heap
);
542 /* end of file for all streams */
547 ret
= stream_read_event(file_stream
);
549 removed
= heap_remove(iter
->stream_heap
);
550 assert(removed
== file_stream
);
556 /* Reinsert the file stream into the heap, and rebalance. */
557 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
558 assert(removed
== file_stream
);