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/ctf/metadata.h>
26 #include <babeltrace/iterator-internal.h>
27 #include <babeltrace/iterator.h>
28 #include <babeltrace/prio_heap.h>
30 struct stream_saved_pos
{
32 * Use file_stream pointer to check if the trace collection we
33 * restore to match the one we saved from, for each stream.
35 struct ctf_file_stream
*file_stream
;
36 size_t cur_index
; /* current index in packet index */
37 ssize_t offset
; /* offset from base, in bits. EOF for end of file. */
41 struct trace_collection
*tc
;
42 GArray
*stream_saved_pos
; /* Contains struct stream_saved_pos */
45 static int stream_read_event(struct ctf_file_stream
*sin
)
49 ret
= sin
->pos
.parent
.event_cb(&sin
->pos
.parent
, &sin
->parent
);
53 fprintf(stderr
, "[error] Reading event failed.\n");
60 * returns true if a < b, false otherwise.
62 int stream_compare(void *a
, void *b
)
64 struct ctf_file_stream
*s_a
= a
, *s_b
= b
;
66 if (s_a
->parent
.timestamp
< s_b
->parent
.timestamp
)
73 * babeltrace_filestream_seek: seek a filestream to given position.
75 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
77 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
78 const struct bt_iter_pos
*begin_pos
,
79 unsigned long stream_id
)
83 switch (begin_pos
->type
) {
86 * just insert into the heap we should already know
91 file_stream
->pos
.move_pos_slow(&file_stream
->pos
, 0, SEEK_SET
);
92 ret
= stream_read_event(file_stream
);
98 assert(0); /* Not yet defined */
105 * bt_iter_seek: seek iterator to given position.
107 int bt_iter_seek(struct bt_iter
*iter
,
108 const struct bt_iter_pos
*begin_pos
)
112 struct trace_collection
*tc
= iter
->ctx
->tc
;
114 for (i
= 0; i
< tc
->array
->len
; i
++) {
115 struct ctf_trace
*tin
;
116 struct trace_descriptor
*td_read
;
118 td_read
= g_ptr_array_index(tc
->array
, i
);
119 tin
= container_of(td_read
, struct ctf_trace
, parent
);
121 /* Populate heap with each stream */
122 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
124 struct ctf_stream_class
*stream
;
127 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
128 for (filenr
= 0; filenr
< stream
->streams
->len
;
130 struct ctf_file_stream
*file_stream
;
132 file_stream
= g_ptr_array_index(stream
->streams
,
134 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
145 struct bt_iter
*bt_iter_create(struct bt_context
*ctx
,
146 struct bt_iter_pos
*begin_pos
,
147 struct bt_iter_pos
*end_pos
)
151 struct bt_iter
*iter
;
153 iter
= malloc(sizeof(struct bt_iter
));
156 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
157 iter
->end_pos
= end_pos
;
158 iter
->callbacks
= g_array_new(0, 1, sizeof(struct bt_stream_callbacks
));
159 iter
->recalculate_dep_graph
= 0;
160 iter
->main_callbacks
.callback
= NULL
;
161 iter
->dep_gc
= g_ptr_array_new();
162 if (bt_context_get(ctx
) != 0)
166 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
168 goto error_heap_init
;
170 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
171 struct ctf_trace
*tin
;
172 struct trace_descriptor
*td_read
;
174 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
175 tin
= container_of(td_read
, struct ctf_trace
, parent
);
177 /* Populate heap with each stream */
178 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
180 struct ctf_stream_class
*stream
;
183 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
186 for (filenr
= 0; filenr
< stream
->streams
->len
;
188 struct ctf_file_stream
*file_stream
;
190 file_stream
= g_ptr_array_index(stream
->streams
,
194 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
204 ret
= heap_insert(iter
->stream_heap
, file_stream
);
214 heap_free(iter
->stream_heap
);
216 g_free(iter
->stream_heap
);
223 void bt_iter_destroy(struct bt_iter
*iter
)
225 struct bt_stream_callbacks
*bt_stream_cb
;
226 struct bt_callback_chain
*bt_chain
;
229 heap_free(iter
->stream_heap
);
230 g_free(iter
->stream_heap
);
232 /* free all events callbacks */
233 if (iter
->main_callbacks
.callback
)
234 g_array_free(iter
->main_callbacks
.callback
, TRUE
);
236 /* free per-event callbacks */
237 for (i
= 0; i
< iter
->callbacks
->len
; i
++) {
238 bt_stream_cb
= &g_array_index(iter
->callbacks
,
239 struct bt_stream_callbacks
, i
);
240 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
242 for (j
= 0; j
< bt_stream_cb
->per_id_callbacks
->len
; j
++) {
243 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
244 struct bt_callback_chain
, j
);
245 if (bt_chain
->callback
) {
246 g_array_free(bt_chain
->callback
, TRUE
);
249 g_array_free(bt_stream_cb
->per_id_callbacks
, TRUE
);
252 bt_context_put(iter
->ctx
);
257 int bt_iter_next(struct bt_iter
*iter
)
259 struct ctf_file_stream
*file_stream
, *removed
;
262 file_stream
= heap_maximum(iter
->stream_heap
);
264 /* end of file for all streams */
269 ret
= stream_read_event(file_stream
);
271 removed
= heap_remove(iter
->stream_heap
);
272 assert(removed
== file_stream
);
278 /* Reinsert the file stream into the heap, and rebalance. */
279 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
280 assert(removed
== file_stream
);
286 int bt_iter_read_event(struct bt_iter
*iter
,
287 struct ctf_stream
**stream
,
288 struct ctf_stream_event
**event
)
290 struct ctf_file_stream
*file_stream
;
293 file_stream
= heap_maximum(iter
->stream_heap
);
295 /* end of file for all streams */
299 *stream
= &file_stream
->parent
;
300 *event
= g_ptr_array_index((*stream
)->events_by_id
, (*stream
)->event_id
);
302 if ((*stream
)->stream_id
> iter
->callbacks
->len
)
305 process_callbacks(iter
, *stream
);
This page took 0.035192 seconds and 5 git commands to generate.