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>
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
->cur_index
= i
;
118 stream_pos
->move_pos_slow(stream_pos
, index
->offset
, SEEK_SET
);
119 while (cfs
->parent
.timestamp
< timestamp
) {
120 ret
= stream_read_event(cfs
);
130 * seek_ctf_trace_by_timestamp : for each file stream, seek to the event with
131 * the corresponding timestamp
133 * Return 0 on success.
134 * If the timestamp is not part of any file stream, return EOF to inform the
135 * user the timestamp is out of the scope
137 static int seek_ctf_trace_by_timestamp(struct ctf_trace
*tin
,
138 uint64_t timestamp
, struct ptr_heap
*stream_heap
)
143 /* for each stream_class */
144 for (i
= 0; i
< tin
->streams
->len
; i
++) {
145 struct ctf_stream_class
*stream_class
;
147 stream_class
= g_ptr_array_index(tin
->streams
, i
);
148 /* for each file_stream */
149 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
150 struct ctf_stream
*stream
;
151 struct ctf_file_stream
*cfs
;
153 stream
= g_ptr_array_index(stream_class
->streams
, j
);
154 cfs
= container_of(stream
, struct ctf_file_stream
,
156 ret
= seek_file_stream_by_timestamp(cfs
, timestamp
);
159 ret
= heap_insert(stream_heap
, cfs
);
173 int bt_iter_set_pos(struct bt_iter
*iter
, const struct bt_iter_pos
*iter_pos
)
175 struct trace_collection
*tc
;
178 switch (iter_pos
->type
) {
179 case BT_SEEK_RESTORE
:
180 if (!iter_pos
->u
.restore
)
183 heap_free(iter
->stream_heap
);
184 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
188 for (i
= 0; i
< iter_pos
->u
.restore
->stream_saved_pos
->len
;
190 struct stream_saved_pos
*saved_pos
;
191 struct ctf_stream_pos
*stream_pos
;
192 struct packet_index
*index
;
193 struct ctf_stream
*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
;
200 index
= &g_array_index(stream_pos
->packet_index
,
202 saved_pos
->cur_index
);
204 stream_pos
->cur_index
= saved_pos
->cur_index
;
205 stream_pos
->move_pos_slow(stream_pos
, index
->offset
,
209 * the timestamp needs to be restored after
210 * move_pos_slow, because this function resets
211 * the timestamp to the beginning of the packet
213 stream
->timestamp
= saved_pos
->current_timestamp
;
214 stream_pos
->offset
= saved_pos
->offset
;
215 stream_pos
->last_offset
= saved_pos
->offset
;
217 stream
->prev_timestamp
= 0;
218 stream
->prev_timestamp_end
= 0;
219 stream
->consumed
= 0;
221 printf_debug("restored to cur_index = %zd and "
222 "offset = %zd, timestamp = %" PRIu64
"\n",
223 stream_pos
->cur_index
,
224 stream_pos
->offset
, stream
->timestamp
);
226 stream_read_event(saved_pos
->file_stream
);
229 ret
= heap_insert(iter
->stream_heap
,
230 saved_pos
->file_stream
);
237 if (!iter_pos
->u
.seek_time
)
240 heap_free(iter
->stream_heap
);
241 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
245 /* for each trace in the trace_collection */
246 for (i
= 0; i
< tc
->array
->len
; i
++) {
247 struct ctf_trace
*tin
;
248 struct trace_descriptor
*td_read
;
250 td_read
= g_ptr_array_index(tc
->array
, i
);
251 tin
= container_of(td_read
, struct ctf_trace
, parent
);
253 ret
= seek_ctf_trace_by_timestamp(tin
,
254 iter_pos
->u
.seek_time
,
261 /* not implemented */
270 heap_free(iter
->stream_heap
);
271 if (heap_init(iter
->stream_heap
, 0, stream_compare
) < 0) {
272 heap_free(iter
->stream_heap
);
273 g_free(iter
->stream_heap
);
274 iter
->stream_heap
= NULL
;
280 struct bt_iter_pos
*bt_iter_get_pos(struct bt_iter
*iter
)
282 struct bt_iter_pos
*pos
;
283 struct trace_collection
*tc
= iter
->ctx
->tc
;
284 int i
, stream_class_id
, stream_id
;
286 pos
= g_new0(struct bt_iter_pos
, 1);
287 pos
->u
.restore
= g_new0(struct bt_saved_pos
, 1);
288 pos
->u
.restore
->tc
= tc
;
289 pos
->u
.restore
->stream_saved_pos
= g_array_new(FALSE
, TRUE
,
290 sizeof(struct stream_saved_pos
));
291 if (!pos
->u
.restore
->stream_saved_pos
)
294 for (i
= 0; i
< tc
->array
->len
; i
++) {
295 struct ctf_trace
*tin
;
296 struct trace_descriptor
*td_read
;
298 td_read
= g_ptr_array_index(tc
->array
, i
);
299 tin
= container_of(td_read
, struct ctf_trace
, parent
);
301 for (stream_class_id
= 0; stream_class_id
< tin
->streams
->len
;
303 struct ctf_stream_class
*stream_class
;
305 stream_class
= g_ptr_array_index(tin
->streams
,
308 stream_id
< stream_class
->streams
->len
;
310 struct ctf_stream
*stream
;
311 struct ctf_file_stream
*cfs
;
312 struct stream_saved_pos saved_pos
;
314 stream
= g_ptr_array_index(
315 stream_class
->streams
,
317 cfs
= container_of(stream
,
318 struct ctf_file_stream
,
321 saved_pos
.file_stream
= cfs
;
322 saved_pos
.cur_index
= cfs
->pos
.cur_index
;
325 * It is possible that an event was read during
326 * the last restore, never consumed and its
327 * position saved again. For this case, we
328 * need to check if the event really was
329 * consumed by the caller otherwise it is lost.
331 if (stream
->consumed
)
332 saved_pos
.offset
= cfs
->pos
.offset
;
334 saved_pos
.offset
= cfs
->pos
.last_offset
;
336 saved_pos
.current_timestamp
= stream
->timestamp
;
339 pos
->u
.restore
->stream_saved_pos
,
342 printf_debug("stream : %" PRIu64
", cur_index : %zd, "
344 "timestamp = %" PRIu64
"\n",
345 stream
->stream_id
, saved_pos
.cur_index
,
347 saved_pos
.current_timestamp
);
358 struct bt_iter_pos
*bt_iter_create_time_pos(struct bt_iter
*iter
,
361 struct bt_iter_pos
*pos
;
363 pos
= g_new0(struct bt_iter_pos
, 1);
364 pos
->type
= BT_SEEK_TIME
;
365 pos
->u
.seek_time
= timestamp
;
370 * babeltrace_filestream_seek: seek a filestream to given position.
372 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
374 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
375 const struct bt_iter_pos
*begin_pos
,
376 unsigned long stream_id
)
380 switch (begin_pos
->type
) {
383 * just insert into the heap we should already know
388 file_stream
->pos
.move_pos_slow(&file_stream
->pos
, 0, SEEK_SET
);
389 ret
= stream_read_event(file_stream
);
392 case BT_SEEK_RESTORE
:
395 assert(0); /* Not yet defined */
402 * bt_iter_seek: seek iterator to given position.
404 int bt_iter_seek(struct bt_iter
*iter
,
405 const struct bt_iter_pos
*begin_pos
)
409 struct trace_collection
*tc
= iter
->ctx
->tc
;
411 for (i
= 0; i
< tc
->array
->len
; i
++) {
412 struct ctf_trace
*tin
;
413 struct trace_descriptor
*td_read
;
415 td_read
= g_ptr_array_index(tc
->array
, i
);
416 tin
= container_of(td_read
, struct ctf_trace
, parent
);
418 /* Populate heap with each stream */
419 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
421 struct ctf_stream_class
*stream
;
424 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
425 for (filenr
= 0; filenr
< stream
->streams
->len
;
427 struct ctf_file_stream
*file_stream
;
429 file_stream
= g_ptr_array_index(stream
->streams
,
431 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
442 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
443 struct bt_iter_pos
*begin_pos
,
444 struct bt_iter_pos
*end_pos
)
448 struct bt_iter
*iter
;
450 iter
= g_new0(struct bt_iter
, 1);
451 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
452 iter
->end_pos
= end_pos
;
453 iter
->callbacks
= g_array_new(0, 1, sizeof(struct bt_stream_callbacks
));
454 iter
->recalculate_dep_graph
= 0;
455 iter
->main_callbacks
.callback
= NULL
;
456 iter
->dep_gc
= g_ptr_array_new();
460 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
462 goto error_heap_init
;
464 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
465 struct ctf_trace
*tin
;
466 struct trace_descriptor
*td_read
;
468 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
469 tin
= container_of(td_read
, struct ctf_trace
, parent
);
471 /* Populate heap with each stream */
472 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
474 struct ctf_stream_class
*stream
;
477 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
480 for (filenr
= 0; filenr
< stream
->streams
->len
;
482 struct ctf_file_stream
*file_stream
;
484 file_stream
= g_ptr_array_index(stream
->streams
,
488 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
498 ret
= heap_insert(iter
->stream_heap
, file_stream
);
508 heap_free(iter
->stream_heap
);
510 g_free(iter
->stream_heap
);
515 void bt_iter_destroy(struct bt_iter
*iter
)
517 struct bt_stream_callbacks
*bt_stream_cb
;
518 struct bt_callback_chain
*bt_chain
;
521 if (iter
->stream_heap
) {
522 heap_free(iter
->stream_heap
);
523 g_free(iter
->stream_heap
);
526 /* free all events callbacks */
527 if (iter
->main_callbacks
.callback
)
528 g_array_free(iter
->main_callbacks
.callback
, TRUE
);
530 /* free per-event callbacks */
531 for (i
= 0; i
< iter
->callbacks
->len
; i
++) {
532 bt_stream_cb
= &g_array_index(iter
->callbacks
,
533 struct bt_stream_callbacks
, i
);
534 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
536 for (j
= 0; j
< bt_stream_cb
->per_id_callbacks
->len
; j
++) {
537 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
538 struct bt_callback_chain
, j
);
539 if (bt_chain
->callback
) {
540 g_array_free(bt_chain
->callback
, TRUE
);
543 g_array_free(bt_stream_cb
->per_id_callbacks
, TRUE
);
546 bt_context_put(iter
->ctx
);
551 int bt_iter_next(struct bt_iter
*iter
)
553 struct ctf_file_stream
*file_stream
, *removed
;
556 file_stream
= heap_maximum(iter
->stream_heap
);
558 /* end of file for all streams */
563 ret
= stream_read_event(file_stream
);
565 removed
= heap_remove(iter
->stream_heap
);
566 assert(removed
== file_stream
);
572 /* Reinsert the file stream into the heap, and rebalance. */
573 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
574 assert(removed
== file_stream
);
580 int bt_iter_read_event(struct bt_iter
*iter
,
581 struct ctf_stream
**stream
,
582 struct ctf_stream_event
**event
)
584 struct ctf_file_stream
*file_stream
;
587 file_stream
= heap_maximum(iter
->stream_heap
);
589 /* end of file for all streams */
593 *stream
= &file_stream
->parent
;
594 *event
= g_ptr_array_index((*stream
)->events_by_id
, (*stream
)->event_id
);
596 if ((*stream
)->stream_id
> iter
->callbacks
->len
)
599 process_callbacks(iter
, *stream
);