d078a690474e29a24c6fc5e252a80526f71dbd4a
[babeltrace.git] / converter / babeltrace-lib.c
1 /*
2 * babeltrace-lib.c
3 *
4 * Babeltrace Trace Converter Library
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <inttypes.h>
25 #include <babeltrace/babeltrace-internal.h>
26 #include <babeltrace/format.h>
27 #include <babeltrace/ctf/types.h>
28 #include <babeltrace/ctf/metadata.h>
29 #include <babeltrace/ctf-text/types.h>
30 #include <babeltrace/prio_heap.h>
31 #include <babeltrace/babeltrace.h>
32 #include <babeltrace/types.h>
33 #include <babeltrace/ctf/types.h>
34 #include <babeltrace/ctf-ir/metadata.h>
35 #include <stdarg.h>
36
37 struct stream_saved_pos {
38 /*
39 * Use file_stream pointer to check if the trace collection we
40 * restore to match the one we saved from, for each stream.
41 */
42 struct ctf_file_stream *file_stream;
43 size_t cur_index; /* current index in packet index */
44 ssize_t offset; /* offset from base, in bits. EOF for end of file. */
45 };
46
47 struct babeltrace_saved_pos {
48 struct trace_collection *tc;
49 GArray *stream_saved_pos; /* Contains struct stream_saved_pos */
50 };
51
52 struct bt_callback {
53 int prio; /* Callback order priority. Lower first. Dynamically assigned from dependency graph. */
54 void *private_data;
55 enum bt_cb_ret (*callback)(void *private_data, void *caller_data);
56 };
57
58 struct bt_callback_chain {
59 GArray *callback; /* Array of struct bt_callback, ordered by priority */
60 };
61
62 /*
63 * per id callbacks need to be per stream class because event ID vs
64 * event name mapping can vary from stream to stream.
65 */
66 struct bt_stream_callbacks {
67 GArray *per_id_callbacks; /* Array of struct bt_callback_chain */
68 };
69
70 /*
71 * struct babeltrace_iter: data structure representing an iterator on a trace
72 * collection.
73 */
74 struct babeltrace_iter {
75 struct ptr_heap *stream_heap;
76 struct trace_collection *tc;
77 struct trace_collection_pos *end_pos;
78 GArray *callbacks; /* Array of struct bt_stream_hooks */
79 struct bt_callback_chain main_callbacks; /* For all events */
80 /*
81 * Flag indicating if dependency graph needs to be recalculated.
82 * Set by babeltrace_iter_add_callback(), and checked (and
83 * cleared) by upon entry into babeltrace_iter_read_event().
84 * babeltrace_iter_read_event() is responsible for calling dep
85 * graph calculation if it sees this flag set.
86 */
87 int recalculate_dep_graph;
88 /*
89 * Array of pointers to struct bt_dependencies, for garbage
90 * collection. We're not using a linked list here because each
91 * struct bt_dependencies can belong to more than one
92 * babeltrace_iter.
93 */
94 GPtrArray *dep_gc;
95 };
96
97 struct bt_dependencies {
98 GArray *deps; /* Array of GQuarks */
99 int refcount; /* free when decremented to 0 */
100 };
101
102 static
103 struct bt_dependencies *_babeltrace_dependencies_create(const char *first,
104 va_list ap)
105 {
106 const char *iter;
107 struct bt_dependencies *dep;
108
109 dep = g_new0(struct bt_dependencies, 1);
110 dep->refcount = 1;
111 dep->deps = g_array_new(FALSE, TRUE, sizeof(GQuark));
112 iter = first;
113 while (iter) {
114 GQuark q = g_quark_from_string(iter);
115 g_array_append_val(dep->deps, q);
116 iter = va_arg(ap, const char *);
117 }
118 return dep;
119 }
120
121 struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...)
122 {
123 va_list ap;
124 struct bt_dependencies *deps;
125
126 va_start(ap, first);
127 deps = _babeltrace_dependencies_create(first, ap);
128 va_end(ap);
129 return deps;
130 }
131
132 static int stream_read_event(struct ctf_file_stream *sin)
133 {
134 int ret;
135
136 ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent);
137 if (ret == EOF)
138 return EOF;
139 else if (ret) {
140 fprintf(stdout, "[error] Reading event failed.\n");
141 return ret;
142 }
143 return 0;
144 }
145
146 /*
147 * returns true if a < b, false otherwise.
148 */
149 int stream_compare(void *a, void *b)
150 {
151 struct ctf_file_stream *s_a = a, *s_b = b;
152
153 if (s_a->parent.timestamp < s_b->parent.timestamp)
154 return 1;
155 else
156 return 0;
157 }
158
159 /* TODO: use begin_pos/end_pos */
160 struct babeltrace_iter *babeltrace_iter_create(struct trace_collection *tc,
161 struct trace_collection_pos *begin_pos,
162 struct trace_collection_pos *end_pos)
163 {
164 int i, stream_id;
165 int ret = 0;
166 struct babeltrace_iter *iter;
167
168 iter = malloc(sizeof(struct babeltrace_iter));
169 if (!iter)
170 goto error_malloc;
171 iter->stream_heap = g_new(struct ptr_heap, 1);
172 iter->tc = tc;
173
174 ret = heap_init(iter->stream_heap, 0, stream_compare);
175 if (ret < 0)
176 goto error_heap_init;
177
178 for (i = 0; i < tc->array->len; i++) {
179 struct ctf_trace *tin;
180 struct trace_descriptor *td_read;
181
182 td_read = g_ptr_array_index(tc->array, i);
183 tin = container_of(td_read, struct ctf_trace, parent);
184
185 /* Populate heap with each stream */
186 for (stream_id = 0; stream_id < tin->streams->len;
187 stream_id++) {
188 struct ctf_stream_class *stream;
189 int filenr;
190
191 stream = g_ptr_array_index(tin->streams, stream_id);
192 if (!stream)
193 continue;
194 for (filenr = 0; filenr < stream->streams->len;
195 filenr++) {
196 struct ctf_file_stream *file_stream;
197
198 file_stream = g_ptr_array_index(stream->streams,
199 filenr);
200
201 ret = stream_read_event(file_stream);
202 if (ret == EOF) {
203 ret = 0;
204 continue;
205 } else if (ret) {
206 goto error;
207 }
208 /* Add to heap */
209 ret = heap_insert(iter->stream_heap, file_stream);
210 if (ret)
211 goto error;
212 }
213 }
214 }
215
216 return iter;
217
218 error:
219 heap_free(iter->stream_heap);
220 error_heap_init:
221 g_free(iter->stream_heap);
222 free(iter);
223 error_malloc:
224 return NULL;
225 }
226
227 void babeltrace_iter_destroy(struct babeltrace_iter *iter)
228 {
229 heap_free(iter->stream_heap);
230 g_free(iter->stream_heap);
231 free(iter);
232 }
233
234 int babeltrace_iter_next(struct babeltrace_iter *iter)
235 {
236 struct ctf_file_stream *file_stream, *removed;
237 int ret;
238
239 file_stream = heap_maximum(iter->stream_heap);
240 if (!file_stream) {
241 /* end of file for all streams */
242 ret = 0;
243 goto end;
244 }
245
246 ret = stream_read_event(file_stream);
247 if (ret == EOF) {
248 removed = heap_remove(iter->stream_heap);
249 assert(removed == file_stream);
250 ret = 0;
251 goto end;
252 } else if (ret) {
253 goto end;
254 }
255 /* Reinsert the file stream into the heap, and rebalance. */
256 removed = heap_replace_max(iter->stream_heap, file_stream);
257 assert(removed == file_stream);
258
259 end:
260 return ret;
261 }
262
263 int babeltrace_iter_read_event(struct babeltrace_iter *iter,
264 struct ctf_stream **stream,
265 struct ctf_stream_event **event)
266 {
267 struct ctf_file_stream *file_stream;
268 int ret = 0;
269
270 file_stream = heap_maximum(iter->stream_heap);
271 if (!file_stream) {
272 /* end of file for all streams */
273 ret = EOF;
274 goto end;
275 }
276 *stream = &file_stream->parent;
277 *event = g_ptr_array_index((*stream)->events_by_id, (*stream)->event_id);
278 end:
279 return ret;
280 }
281
282 int convert_trace(struct trace_descriptor *td_write,
283 struct trace_collection *trace_collection_read)
284 {
285 struct babeltrace_iter *iter;
286 struct ctf_stream *stream;
287 struct ctf_stream_event *event;
288 struct ctf_text_stream_pos *sout;
289 int ret = 0;
290
291 sout = container_of(td_write, struct ctf_text_stream_pos,
292 trace_descriptor);
293
294 iter = babeltrace_iter_create(trace_collection_read, NULL, NULL);
295 while (babeltrace_iter_read_event(iter, &stream, &event) == 0) {
296 ret = sout->parent.event_cb(&sout->parent, stream);
297 if (ret) {
298 fprintf(stdout, "[error] Writing event failed.\n");
299 goto end;
300 }
301 ret = babeltrace_iter_next(iter);
302 if (ret < 0)
303 goto end;
304 }
305 end:
306 babeltrace_iter_destroy(iter);
307 return ret;
308 }
This page took 0.052253 seconds and 3 git commands to generate.