6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/babeltrace.h>
31 #include <babeltrace/format.h>
32 #include <babeltrace/ctf/events.h>
33 #include <babeltrace/ctf-ir/metadata.h>
34 #include <babeltrace/prio_heap.h>
35 #include <babeltrace/iterator-internal.h>
36 #include <babeltrace/ctf/events-internal.h>
37 #include <babeltrace/ctf/metadata.h>
40 #include "events-private.h"
42 struct bt_ctf_iter
*bt_ctf_iter_create(struct bt_context
*ctx
,
43 const struct bt_iter_pos
*begin_pos
,
44 const struct bt_iter_pos
*end_pos
)
46 struct bt_ctf_iter
*iter
;
52 iter
= g_new0(struct bt_ctf_iter
, 1);
53 ret
= bt_iter_init(&iter
->parent
, ctx
, begin_pos
, end_pos
);
58 iter
->callbacks
= g_array_new(FALSE
, TRUE
,
59 sizeof(struct bt_stream_callbacks
));
60 iter
->recalculate_dep_graph
= 0;
61 iter
->main_callbacks
.callback
= NULL
;
62 iter
->dep_gc
= g_ptr_array_new();
66 struct bt_ctf_iter
*bt_ctf_iter_create_intersect(struct bt_context
*ctx
,
67 struct bt_iter_pos
**inter_begin_pos
,
68 struct bt_iter_pos
**inter_end_pos
)
74 * The iterator's range is the union of each trace's intersection of
75 * streams. This means that we determine the "active" region of each
76 * trace (that is the region where all of its streams are active), and
77 * use the TraceCollection to merge all of these active regions.
79 * This results in a union of the traces' active regions.
81 ret
= ctf_find_tc_stream_packet_intersection_union(ctx
, &begin
, &end
);
83 fprintf(stderr
, "[error] No intersection found between trace files.\n");
85 } else if (ret
!= 0) {
88 *inter_begin_pos
= bt_iter_create_time_pos(NULL
, begin
);
89 if (!(*inter_begin_pos
)) {
92 *inter_end_pos
= bt_iter_create_time_pos(NULL
, end
);
93 if (!(*inter_end_pos
)) {
97 ret
= ctf_tc_set_stream_intersection_mode(ctx
);
103 * bt_ctf_iter does not take ownership of begin and end positions,
104 * so we return them to the caller who must still assume their ownership
105 * until the iterator is destroyed.
107 return bt_ctf_iter_create(ctx
, *inter_begin_pos
,
113 void bt_ctf_iter_destroy(struct bt_ctf_iter
*iter
)
115 struct bt_stream_callbacks
*bt_stream_cb
;
116 struct bt_callback_chain
*bt_chain
;
121 /* free all events callbacks */
122 if (iter
->main_callbacks
.callback
)
123 g_array_free(iter
->main_callbacks
.callback
, TRUE
);
125 /* free per-event callbacks */
126 for (i
= 0; i
< iter
->callbacks
->len
; i
++) {
127 bt_stream_cb
= &g_array_index(iter
->callbacks
,
128 struct bt_stream_callbacks
, i
);
129 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
131 for (j
= 0; j
< bt_stream_cb
->per_id_callbacks
->len
; j
++) {
132 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
133 struct bt_callback_chain
, j
);
134 if (bt_chain
->callback
) {
135 g_array_free(bt_chain
->callback
, TRUE
);
138 g_array_free(bt_stream_cb
->per_id_callbacks
, TRUE
);
140 g_array_free(iter
->callbacks
, TRUE
);
141 g_ptr_array_free(iter
->dep_gc
, TRUE
);
143 bt_iter_fini(&iter
->parent
);
147 struct bt_iter
*bt_ctf_get_iter(struct bt_ctf_iter
*iter
)
152 return &iter
->parent
;
155 struct bt_ctf_event
*bt_ctf_iter_read_event_flags(struct bt_ctf_iter
*iter
,
158 struct ctf_file_stream
*file_stream
;
159 struct bt_ctf_event
*ret
;
160 struct ctf_stream_definition
*stream
;
161 struct packet_index
*packet_index
;
164 * We do not want to fail for any other reason than end of
165 * trace, hence the assert.
172 ret
= &iter
->current_ctf_event
;
173 file_stream
= bt_heap_maximum(iter
->parent
.stream_heap
);
175 /* end of file for all streams */
180 * If the packet is empty (contains only headers or is of size 0), the
181 * caller has to know that we can't read the current event and we need
182 * to do a bt_iter_next.
184 if (file_stream
->pos
.data_offset
== file_stream
->pos
.content_size
185 || file_stream
->pos
.content_size
== 0) {
186 /* More events may come. */
189 *flags
|= BT_ITER_FLAG_RETRY
;
193 stream
= &file_stream
->parent
;
194 if (iter
->parent
.end_pos
&&
195 iter
->parent
.end_pos
->type
== BT_SEEK_TIME
&&
196 stream
->real_timestamp
> iter
->parent
.end_pos
->u
.seek_time
) {
199 ret
->parent
= g_ptr_array_index(stream
->events_by_id
,
202 if (!file_stream
->pos
.packet_index
)
205 packet_index
= &g_array_index(file_stream
->pos
.packet_index
,
206 struct packet_index
, file_stream
->pos
.cur_index
);
207 iter
->events_lost
= 0;
208 if (packet_index
&& packet_index
->events_discarded
>
209 file_stream
->pos
.last_events_discarded
) {
211 *flags
|= BT_ITER_FLAG_LOST_EVENTS
;
212 iter
->events_lost
+= packet_index
->events_discarded
-
213 file_stream
->pos
.last_events_discarded
;
214 file_stream
->pos
.last_events_discarded
=
215 packet_index
->events_discarded
;
218 if (ret
->parent
->stream
->stream_id
> iter
->callbacks
->len
)
221 process_callbacks(iter
, ret
->parent
->stream
);
229 struct bt_ctf_event
*bt_ctf_iter_read_event(struct bt_ctf_iter
*iter
)
231 return bt_ctf_iter_read_event_flags(iter
, NULL
);
234 uint64_t bt_ctf_get_lost_events_count(struct bt_ctf_iter
*iter
)
239 return iter
->events_lost
;
This page took 0.036933 seconds and 5 git commands to generate.