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_end
<= timestamp
)
116 stream_pos
->packet_seek(&stream_pos
->parent
, i
, SEEK_SET
);
117 while (cfs
->parent
.timestamp
< timestamp
) {
118 ret
= stream_read_event(cfs
);
128 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
129 * the corresponding timestamp
131 * Return 0 on success.
132 * If the timestamp is not part of any file stream, return EOF to inform the
133 * user the timestamp is out of the scope
135 static int seek_ctf_trace_by_timestamp(struct ctf_trace
*tin
,
136 uint64_t timestamp
, struct ptr_heap
*stream_heap
)
141 /* for each stream_class */
142 for (i
= 0; i
< tin
->streams
->len
; i
++) {
143 struct ctf_stream_class
*stream_class
;
145 stream_class
= g_ptr_array_index(tin
->streams
, i
);
146 /* for each file_stream */
147 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
148 struct ctf_stream
*stream
;
149 struct ctf_file_stream
*cfs
;
151 stream
= g_ptr_array_index(stream_class
->streams
, j
);
152 cfs
= container_of(stream
, struct ctf_file_stream
,
154 ret
= seek_file_stream_by_timestamp(cfs
, timestamp
);
157 ret
= heap_insert(stream_heap
, cfs
);
171 int bt_iter_set_pos(struct bt_iter
*iter
, const struct bt_iter_pos
*iter_pos
)
173 struct trace_collection
*tc
;
176 switch (iter_pos
->type
) {
177 case BT_SEEK_RESTORE
:
178 if (!iter_pos
->u
.restore
)
181 heap_free(iter
->stream_heap
);
182 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
186 for (i
= 0; i
< iter_pos
->u
.restore
->stream_saved_pos
->len
;
188 struct stream_saved_pos
*saved_pos
;
189 struct ctf_stream_pos
*stream_pos
;
190 struct ctf_stream
*stream
;
192 saved_pos
= &g_array_index(
193 iter_pos
->u
.restore
->stream_saved_pos
,
194 struct stream_saved_pos
, i
);
195 stream
= &saved_pos
->file_stream
->parent
;
196 stream_pos
= &saved_pos
->file_stream
->pos
;
198 stream_pos
->packet_seek(&stream_pos
->parent
,
199 saved_pos
->cur_index
, SEEK_SET
);
202 * the timestamp needs to be restored after
203 * packet_seek, because this function resets
204 * the timestamp to the beginning of the packet
206 stream
->timestamp
= saved_pos
->current_timestamp
;
207 stream_pos
->offset
= saved_pos
->offset
;
208 stream_pos
->last_offset
= saved_pos
->offset
;
210 stream
->prev_timestamp
= 0;
211 stream
->prev_timestamp_end
= 0;
212 stream
->consumed
= 0;
214 printf_debug("restored to cur_index = %zd and "
215 "offset = %zd, timestamp = %" PRIu64
"\n",
216 stream_pos
->cur_index
,
217 stream_pos
->offset
, stream
->timestamp
);
219 stream_read_event(saved_pos
->file_stream
);
222 ret
= heap_insert(iter
->stream_heap
,
223 saved_pos
->file_stream
);
230 heap_free(iter
->stream_heap
);
231 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
235 /* for each trace in the trace_collection */
236 for (i
= 0; i
< tc
->array
->len
; i
++) {
237 struct ctf_trace
*tin
;
238 struct trace_descriptor
*td_read
;
240 td_read
= g_ptr_array_index(tc
->array
, i
);
241 tin
= container_of(td_read
, struct ctf_trace
, parent
);
243 ret
= seek_ctf_trace_by_timestamp(tin
,
244 iter_pos
->u
.seek_time
,
251 /* not implemented */
258 heap_free(iter
->stream_heap
);
259 if (heap_init(iter
->stream_heap
, 0, stream_compare
) < 0) {
260 heap_free(iter
->stream_heap
);
261 g_free(iter
->stream_heap
);
262 iter
->stream_heap
= NULL
;
268 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
270 struct bt_iter_pos
*pos
;
271 struct trace_collection
*tc
= iter
->ctx
->tc
;
272 int i
, stream_class_id
, stream_id
;
274 pos
= g_new0(struct bt_iter_pos
, 1);
275 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
276 pos
->u
.restore
->tc
= tc
;
277 pos
->u
.restore
->stream_saved_pos
= g_array_new(FALSE
, TRUE
,
278 sizeof(struct stream_saved_pos
));
279 if (!pos
->u
.restore
->stream_saved_pos
)
282 for (i
= 0; i
< tc
->array
->len
; i
++) {
283 struct ctf_trace
*tin
;
284 struct trace_descriptor
*td_read
;
286 td_read
= g_ptr_array_index(tc
->array
, i
);
287 tin
= container_of(td_read
, struct ctf_trace
, parent
);
289 for (stream_class_id
= 0; stream_class_id
< tin
->streams
->len
;
291 struct ctf_stream_class
*stream_class
;
293 stream_class
= g_ptr_array_index(tin
->streams
,
296 stream_id
< stream_class
->streams
->len
;
298 struct ctf_stream
*stream
;
299 struct ctf_file_stream
*cfs
;
300 struct stream_saved_pos saved_pos
;
302 stream
= g_ptr_array_index(
303 stream_class
->streams
,
305 cfs
= container_of(stream
,
306 struct ctf_file_stream
,
309 saved_pos
.file_stream
= cfs
;
310 saved_pos
.cur_index
= cfs
->pos
.cur_index
;
313 * It is possible that an event was read during
314 * the last restore, never consumed and its
315 * position saved again. For this case, we
316 * need to check if the event really was
317 * consumed by the caller otherwise it is lost.
319 if (stream
->consumed
)
320 saved_pos
.offset
= cfs
->pos
.offset
;
322 saved_pos
.offset
= cfs
->pos
.last_offset
;
324 saved_pos
.current_timestamp
= stream
->timestamp
;
327 pos
->u
.restore
->stream_saved_pos
,
330 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
332 "timestamp = %" PRIu64
"\n",
333 stream
->stream_id
, saved_pos
.cur_index
,
335 saved_pos
.current_timestamp
);
346 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
349 struct bt_iter_pos
*pos
;
351 pos
= g_new0(struct bt_iter_pos
, 1);
352 pos
->type
= BT_SEEK_TIME
;
353 pos
->u
.seek_time
= timestamp
;
358 * babeltrace_filestream_seek: seek a filestream to given position.
360 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
362 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
363 const struct bt_iter_pos
*begin_pos
,
364 unsigned long stream_id
)
368 switch (begin_pos
->type
) {
371 * just insert into the heap we should already know
376 file_stream
->pos
.packet_seek(&file_stream
->pos
.parent
,
378 ret
= stream_read_event(file_stream
);
381 case BT_SEEK_RESTORE
:
384 assert(0); /* Not yet defined */
391 * bt_iter_seek: seek iterator to given position.
393 int bt_iter_seek(struct bt_iter
*iter
,
394 const struct bt_iter_pos
*begin_pos
)
398 struct trace_collection
*tc
= iter
->ctx
->tc
;
400 for (i
= 0; i
< tc
->array
->len
; i
++) {
401 struct ctf_trace
*tin
;
402 struct trace_descriptor
*td_read
;
404 td_read
= g_ptr_array_index(tc
->array
, i
);
405 tin
= container_of(td_read
, struct ctf_trace
, parent
);
407 /* Populate heap with each stream */
408 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
410 struct ctf_stream_class
*stream
;
413 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
414 for (filenr
= 0; filenr
< stream
->streams
->len
;
416 struct ctf_file_stream
*file_stream
;
418 file_stream
= g_ptr_array_index(stream
->streams
,
420 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
431 int bt_iter_init(struct bt_iter
*iter
,
432 struct bt_context
*ctx
,
433 struct bt_iter_pos
*begin_pos
,
434 struct bt_iter_pos
*end_pos
)
439 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
440 iter
->end_pos
= end_pos
;
444 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
446 goto error_heap_init
;
448 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
449 struct ctf_trace
*tin
;
450 struct trace_descriptor
*td_read
;
452 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
453 tin
= container_of(td_read
, struct ctf_trace
, parent
);
455 /* Populate heap with each stream */
456 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
458 struct ctf_stream_class
*stream
;
461 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
464 for (filenr
= 0; filenr
< stream
->streams
->len
;
466 struct ctf_file_stream
*file_stream
;
468 file_stream
= g_ptr_array_index(stream
->streams
,
472 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
482 ret
= heap_insert(iter
->stream_heap
, file_stream
);
492 heap_free(iter
->stream_heap
);
494 g_free(iter
->stream_heap
);
498 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
499 struct bt_iter_pos
*begin_pos
,
500 struct bt_iter_pos
*end_pos
)
502 struct bt_iter
*iter
;
505 iter
= g_new0(struct bt_iter
, 1);
506 ret
= bt_iter_init(iter
, ctx
, begin_pos
, end_pos
);
514 void bt_iter_fini(struct bt_iter
*iter
)
516 if (iter
->stream_heap
) {
517 heap_free(iter
->stream_heap
);
518 g_free(iter
->stream_heap
);
520 bt_context_put(iter
->ctx
);
523 void bt_iter_destroy(struct bt_iter
*iter
)
529 int bt_iter_next(struct bt_iter
*iter
)
531 struct ctf_file_stream
*file_stream
, *removed
;
534 file_stream
= heap_maximum(iter
->stream_heap
);
536 /* end of file for all streams */
541 ret
= stream_read_event(file_stream
);
543 removed
= heap_remove(iter
->stream_heap
);
544 assert(removed
== file_stream
);
550 /* Reinsert the file stream into the heap, and rebalance. */
551 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
552 assert(removed
== file_stream
);