Clean-up: fix comment style in bin-info.c
[babeltrace.git] / formats / ctf / iterator.c
1 /*
2 * ctf/iterator.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
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
27 * SOFTWARE.
28 */
29
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>
38 #include <glib.h>
39
40 #include "events-private.h"
41
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)
45 {
46 struct bt_ctf_iter *iter;
47 int ret;
48
49 if (!ctx)
50 return NULL;
51
52 iter = g_new0(struct bt_ctf_iter, 1);
53 ret = bt_iter_init(&iter->parent, ctx, begin_pos, end_pos);
54 if (ret) {
55 g_free(iter);
56 return NULL;
57 }
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();
63 return iter;
64 }
65
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)
69 {
70 uint64_t begin = 0, end = ULLONG_MAX;
71 int ret;
72
73 ret = ctf_find_packets_intersection(ctx, &begin, &end);
74 if (ret == 1) {
75 fprintf(stderr, "[error] No intersection found between trace files.\n");
76 goto error;
77 } else if (ret != 0) {
78 goto error;
79 }
80 *inter_begin_pos = bt_iter_create_time_pos(NULL, begin);
81 if (!(*inter_begin_pos)) {
82 goto error;
83 }
84 *inter_end_pos = bt_iter_create_time_pos(NULL, end);
85 if (!(*inter_end_pos)) {
86 goto error;
87 }
88
89 /*
90 * bt_ctf_iter does not take ownership of begin and end positions,
91 * so we return them to the caller who must still assume their ownership
92 * until the iterator is destroyed.
93 */
94 return bt_ctf_iter_create(ctx, *inter_begin_pos,
95 *inter_end_pos);
96 error:
97 return NULL;
98 }
99
100
101 void bt_ctf_iter_destroy(struct bt_ctf_iter *iter)
102 {
103 struct bt_stream_callbacks *bt_stream_cb;
104 struct bt_callback_chain *bt_chain;
105 int i, j;
106
107 assert(iter);
108
109 /* free all events callbacks */
110 if (iter->main_callbacks.callback)
111 g_array_free(iter->main_callbacks.callback, TRUE);
112
113 /* free per-event callbacks */
114 for (i = 0; i < iter->callbacks->len; i++) {
115 bt_stream_cb = &g_array_index(iter->callbacks,
116 struct bt_stream_callbacks, i);
117 if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
118 continue;
119 for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) {
120 bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
121 struct bt_callback_chain, j);
122 if (bt_chain->callback) {
123 g_array_free(bt_chain->callback, TRUE);
124 }
125 }
126 g_array_free(bt_stream_cb->per_id_callbacks, TRUE);
127 }
128 g_array_free(iter->callbacks, TRUE);
129 g_ptr_array_free(iter->dep_gc, TRUE);
130
131 bt_iter_fini(&iter->parent);
132 g_free(iter);
133 }
134
135 struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter)
136 {
137 if (!iter)
138 return NULL;
139
140 return &iter->parent;
141 }
142
143 struct bt_ctf_event *bt_ctf_iter_read_event_flags(struct bt_ctf_iter *iter,
144 int *flags)
145 {
146 struct ctf_file_stream *file_stream;
147 struct bt_ctf_event *ret;
148 struct ctf_stream_definition *stream;
149 struct packet_index *packet_index;
150
151 /*
152 * We do not want to fail for any other reason than end of
153 * trace, hence the assert.
154 */
155 assert(iter);
156
157 if (flags)
158 *flags = 0;
159
160 ret = &iter->current_ctf_event;
161 file_stream = bt_heap_maximum(iter->parent.stream_heap);
162 if (!file_stream) {
163 /* end of file for all streams */
164 goto stop;
165 }
166
167 /*
168 * If the packet is empty (contains only headers or is of size 0), the
169 * caller has to know that we can't read the current event and we need
170 * to do a bt_iter_next.
171 */
172 if (file_stream->pos.data_offset == file_stream->pos.content_size
173 || file_stream->pos.content_size == 0) {
174 /* More events may come. */
175 ret = NULL;
176 if (flags)
177 *flags |= BT_ITER_FLAG_RETRY;
178 goto end;
179 }
180
181 stream = &file_stream->parent;
182 if (iter->parent.end_pos &&
183 iter->parent.end_pos->type == BT_SEEK_TIME &&
184 stream->real_timestamp > iter->parent.end_pos->u.seek_time) {
185 goto stop;
186 }
187 ret->parent = g_ptr_array_index(stream->events_by_id,
188 stream->event_id);
189
190 if (!file_stream->pos.packet_index)
191 packet_index = NULL;
192 else
193 packet_index = &g_array_index(file_stream->pos.packet_index,
194 struct packet_index, file_stream->pos.cur_index);
195 iter->events_lost = 0;
196 if (packet_index && packet_index->events_discarded >
197 file_stream->pos.last_events_discarded) {
198 if (flags)
199 *flags |= BT_ITER_FLAG_LOST_EVENTS;
200 iter->events_lost += packet_index->events_discarded -
201 file_stream->pos.last_events_discarded;
202 file_stream->pos.last_events_discarded =
203 packet_index->events_discarded;
204 }
205
206 if (ret->parent->stream->stream_id > iter->callbacks->len)
207 goto end;
208
209 process_callbacks(iter, ret->parent->stream);
210
211 end:
212 return ret;
213 stop:
214 return NULL;
215 }
216
217 struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter)
218 {
219 return bt_ctf_iter_read_event_flags(iter, NULL);
220 }
221
222 uint64_t bt_ctf_get_lost_events_count(struct bt_ctf_iter *iter)
223 {
224 if (!iter)
225 return -1ULL;
226
227 return iter->events_lost;
228 }
This page took 0.03326 seconds and 4 git commands to generate.