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/callbacks-internal.h>
24 #include <babeltrace/context.h>
25 #include <babeltrace/context-internal.h>
26 #include <babeltrace/ctf/metadata.h>
27 #include <babeltrace/iterator-internal.h>
28 #include <babeltrace/iterator.h>
29 #include <babeltrace/prio_heap.h>
30 #include <babeltrace/ctf/events.h>
33 struct stream_saved_pos
{
35 * Use file_stream pointer to check if the trace collection we
36 * restore to match the one we saved from, for each stream.
38 struct ctf_file_stream
*file_stream
;
39 size_t cur_index
; /* current index in packet index */
40 ssize_t offset
; /* offset from base, in bits. EOF for end of file. */
41 uint64_t current_timestamp
;
45 struct trace_collection
*tc
;
46 GArray
*stream_saved_pos
; /* Contains struct stream_saved_pos */
49 static int stream_read_event(struct ctf_file_stream
*sin
)
53 ret
= sin
->pos
.parent
.event_cb(&sin
->pos
.parent
, &sin
->parent
);
57 fprintf(stderr
, "[error] Reading event failed.\n");
64 * returns true if a < b, false otherwise.
66 int stream_compare(void *a
, void *b
)
68 struct ctf_file_stream
*s_a
= a
, *s_b
= b
;
70 if (s_a
->parent
.timestamp
< s_b
->parent
.timestamp
)
76 void bt_iter_free_pos(struct bt_iter_pos
*iter_pos
)
81 if (iter_pos
->u
.restore
) {
82 if (iter_pos
->u
.restore
->stream_saved_pos
) {
84 iter_pos
->u
.restore
->stream_saved_pos
,
87 g_free(iter_pos
->u
.restore
);
93 * seek_file_stream_by_timestamp
95 * Browse a filestream by index, if an index contains the timestamp passed in
96 * argument, seek inside the corresponding packet it until we find the event we
97 * are looking for (either the exact timestamp or the event just after the
100 * Return 0 if the seek succeded and EOF if we didn't find any packet
101 * containing the timestamp.
103 static int seek_file_stream_by_timestamp(struct ctf_file_stream
*cfs
,
106 struct ctf_stream_pos
*stream_pos
;
107 struct packet_index
*index
;
110 stream_pos
= &cfs
->pos
;
111 for (i
= 0; i
< stream_pos
->packet_index
->len
; i
++) {
112 index
= &g_array_index(stream_pos
->packet_index
,
113 struct packet_index
, i
);
114 if (index
->timestamp_begin
>= timestamp
||
115 index
->timestamp_end
<= timestamp
)
118 stream_pos
->cur_index
= i
;
119 stream_pos
->move_pos_slow(stream_pos
, index
->offset
, SEEK_SET
);
120 while (cfs
->parent
.timestamp
< timestamp
) {
121 ret
= stream_read_event(cfs
);
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_class
*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
*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 packet_index
*index
;
194 struct ctf_stream
*stream
;
196 saved_pos
= &g_array_index(
197 iter_pos
->u
.restore
->stream_saved_pos
,
198 struct stream_saved_pos
, i
);
199 stream
= &saved_pos
->file_stream
->parent
;
200 stream_pos
= &saved_pos
->file_stream
->pos
;
201 index
= &g_array_index(stream_pos
->packet_index
,
203 saved_pos
->cur_index
);
205 stream_pos
->cur_index
= saved_pos
->cur_index
;
206 stream_pos
->move_pos_slow(stream_pos
, index
->offset
,
210 * the timestamp needs to be restored after
211 * move_pos_slow, because this function resets
212 * the timestamp to the beginning of the packet
214 stream
->timestamp
= saved_pos
->current_timestamp
;
215 stream_pos
->offset
= saved_pos
->offset
;
216 stream_pos
->last_offset
= saved_pos
->offset
;
218 stream
->prev_timestamp
= 0;
219 stream
->prev_timestamp_end
= 0;
220 stream
->consumed
= 0;
222 printf_debug("restored to cur_index = %zd and "
223 "offset = %zd, timestamp = %" PRIu64
"\n",
224 stream_pos
->cur_index
,
225 stream_pos
->offset
, stream
->timestamp
);
227 stream_read_event(saved_pos
->file_stream
);
230 ret
= heap_insert(iter
->stream_heap
,
231 saved_pos
->file_stream
);
238 if (!iter_pos
->u
.seek_time
)
241 heap_free(iter
->stream_heap
);
242 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
246 /* for each trace in the trace_collection */
247 for (i
= 0; i
< tc
->array
->len
; i
++) {
248 struct ctf_trace
*tin
;
249 struct trace_descriptor
*td_read
;
251 td_read
= g_ptr_array_index(tc
->array
, i
);
252 tin
= container_of(td_read
, struct ctf_trace
, parent
);
254 ret
= seek_ctf_trace_by_timestamp(tin
,
255 iter_pos
->u
.seek_time
,
262 /* not implemented */
271 heap_free(iter
->stream_heap
);
272 if (heap_init(iter
->stream_heap
, 0, stream_compare
) < 0) {
273 heap_free(iter
->stream_heap
);
274 g_free(iter
->stream_heap
);
275 iter
->stream_heap
= NULL
;
281 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
283 struct bt_iter_pos
*pos
;
284 struct trace_collection
*tc
= iter
->ctx
->tc
;
285 int i
, stream_class_id
, stream_id
;
287 pos
= g_new0(struct bt_iter_pos
, 1);
288 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
289 pos
->u
.restore
->tc
= tc
;
290 pos
->u
.restore
->stream_saved_pos
= g_array_new(FALSE
, TRUE
,
291 sizeof(struct stream_saved_pos
));
292 if (!pos
->u
.restore
->stream_saved_pos
)
295 for (i
= 0; i
< tc
->array
->len
; i
++) {
296 struct ctf_trace
*tin
;
297 struct trace_descriptor
*td_read
;
299 td_read
= g_ptr_array_index(tc
->array
, i
);
300 tin
= container_of(td_read
, struct ctf_trace
, parent
);
302 for (stream_class_id
= 0; stream_class_id
< tin
->streams
->len
;
304 struct ctf_stream_class
*stream_class
;
306 stream_class
= g_ptr_array_index(tin
->streams
,
309 stream_id
< stream_class
->streams
->len
;
311 struct ctf_stream
*stream
;
312 struct ctf_file_stream
*cfs
;
313 struct stream_saved_pos saved_pos
;
315 stream
= g_ptr_array_index(
316 stream_class
->streams
,
318 cfs
= container_of(stream
,
319 struct ctf_file_stream
,
322 saved_pos
.file_stream
= cfs
;
323 saved_pos
.cur_index
= cfs
->pos
.cur_index
;
326 * It is possible that an event was read during
327 * the last restore, never consumed and its
328 * position saved again. For this case, we
329 * need to check if the event really was
330 * consumed by the caller otherwise it is lost.
332 if (stream
->consumed
)
333 saved_pos
.offset
= cfs
->pos
.offset
;
335 saved_pos
.offset
= cfs
->pos
.last_offset
;
337 saved_pos
.current_timestamp
= stream
->timestamp
;
340 pos
->u
.restore
->stream_saved_pos
,
343 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
345 "timestamp = %" PRIu64
"\n",
346 stream
->stream_id
, saved_pos
.cur_index
,
348 saved_pos
.current_timestamp
);
359 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
362 struct bt_iter_pos
*pos
;
364 pos
= g_new0(struct bt_iter_pos
, 1);
365 pos
->type
= BT_SEEK_TIME
;
366 pos
->u
.seek_time
= timestamp
;
371 * babeltrace_filestream_seek: seek a filestream to given position.
373 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
375 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
376 const struct bt_iter_pos
*begin_pos
,
377 unsigned long stream_id
)
381 switch (begin_pos
->type
) {
384 * just insert into the heap we should already know
389 file_stream
->pos
.move_pos_slow(&file_stream
->pos
, 0, SEEK_SET
);
390 ret
= stream_read_event(file_stream
);
393 case BT_SEEK_RESTORE
:
396 assert(0); /* Not yet defined */
403 * bt_iter_seek: seek iterator to given position.
405 int bt_iter_seek(struct bt_iter
*iter
,
406 const struct bt_iter_pos
*begin_pos
)
410 struct trace_collection
*tc
= iter
->ctx
->tc
;
412 for (i
= 0; i
< tc
->array
->len
; i
++) {
413 struct ctf_trace
*tin
;
414 struct trace_descriptor
*td_read
;
416 td_read
= g_ptr_array_index(tc
->array
, i
);
417 tin
= container_of(td_read
, struct ctf_trace
, parent
);
419 /* Populate heap with each stream */
420 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
422 struct ctf_stream_class
*stream
;
425 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
426 for (filenr
= 0; filenr
< stream
->streams
->len
;
428 struct ctf_file_stream
*file_stream
;
430 file_stream
= g_ptr_array_index(stream
->streams
,
432 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
443 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
444 struct bt_iter_pos
*begin_pos
,
445 struct bt_iter_pos
*end_pos
)
449 struct bt_iter
*iter
;
451 iter
= g_new0(struct bt_iter
, 1);
452 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
453 iter
->end_pos
= end_pos
;
454 iter
->callbacks
= g_array_new(0, 1, sizeof(struct bt_stream_callbacks
));
455 iter
->recalculate_dep_graph
= 0;
456 iter
->main_callbacks
.callback
= NULL
;
457 iter
->dep_gc
= g_ptr_array_new();
461 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
463 goto error_heap_init
;
465 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
466 struct ctf_trace
*tin
;
467 struct trace_descriptor
*td_read
;
469 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
470 tin
= container_of(td_read
, struct ctf_trace
, parent
);
472 /* Populate heap with each stream */
473 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
475 struct ctf_stream_class
*stream
;
478 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
481 for (filenr
= 0; filenr
< stream
->streams
->len
;
483 struct ctf_file_stream
*file_stream
;
485 file_stream
= g_ptr_array_index(stream
->streams
,
489 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
499 ret
= heap_insert(iter
->stream_heap
, file_stream
);
509 heap_free(iter
->stream_heap
);
511 g_free(iter
->stream_heap
);
516 void bt_iter_destroy(struct bt_iter
*iter
)
518 struct bt_stream_callbacks
*bt_stream_cb
;
519 struct bt_callback_chain
*bt_chain
;
522 if (iter
->stream_heap
) {
523 heap_free(iter
->stream_heap
);
524 g_free(iter
->stream_heap
);
527 /* free all events callbacks */
528 if (iter
->main_callbacks
.callback
)
529 g_array_free(iter
->main_callbacks
.callback
, TRUE
);
531 /* free per-event callbacks */
532 for (i
= 0; i
< iter
->callbacks
->len
; i
++) {
533 bt_stream_cb
= &g_array_index(iter
->callbacks
,
534 struct bt_stream_callbacks
, i
);
535 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
537 for (j
= 0; j
< bt_stream_cb
->per_id_callbacks
->len
; j
++) {
538 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
539 struct bt_callback_chain
, j
);
540 if (bt_chain
->callback
) {
541 g_array_free(bt_chain
->callback
, TRUE
);
544 g_array_free(bt_stream_cb
->per_id_callbacks
, TRUE
);
547 bt_context_put(iter
->ctx
);
552 int bt_iter_next(struct bt_iter
*iter
)
554 struct ctf_file_stream
*file_stream
, *removed
;
557 file_stream
= heap_maximum(iter
->stream_heap
);
559 /* end of file for all streams */
564 ret
= stream_read_event(file_stream
);
566 removed
= heap_remove(iter
->stream_heap
);
567 assert(removed
== file_stream
);
573 /* Reinsert the file stream into the heap, and rebalance. */
574 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
575 assert(removed
== file_stream
);
581 struct bt_ctf_event
*bt_iter_read_ctf_event(struct bt_iter
*iter
)
583 struct ctf_file_stream
*file_stream
;
584 struct bt_ctf_event
*ret
= &iter
->current_ctf_event
;
586 file_stream
= heap_maximum(iter
->stream_heap
);
588 /* end of file for all streams */
591 ret
->stream
= &file_stream
->parent
;
592 ret
->event
= g_ptr_array_index(ret
->stream
->events_by_id
,
593 ret
->stream
->event_id
);
595 if (ret
->stream
->stream_id
> iter
->callbacks
->len
)
598 process_callbacks(iter
, ret
->stream
);