4 * Babeltrace Trace Converter Library
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.
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>
37 struct stream_saved_pos
{
39 * Use file_stream pointer to check if the trace collection we
40 * restore to match the one we saved from, for each stream.
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. */
47 struct babeltrace_saved_pos
{
48 struct trace_collection
*tc
;
49 GArray
*stream_saved_pos
; /* Contains struct stream_saved_pos */
53 int prio
; /* Callback order priority. Lower first. Dynamically assigned from dependency graph. */
56 struct bt_dependencies
*depends
;
57 struct bt_dependencies
*weak_depends
;
58 struct bt_dependencies
*provides
;
59 enum bt_cb_ret (*callback
)(struct bt_ctf_data
*ctf_data
,
63 struct bt_callback_chain
{
64 GArray
*callback
; /* Array of struct bt_callback, ordered by priority */
68 * per id callbacks need to be per stream class because event ID vs
69 * event name mapping can vary from stream to stream.
71 struct bt_stream_callbacks
{
72 GArray
*per_id_callbacks
; /* Array of struct bt_callback_chain */
76 * struct babeltrace_iter: data structure representing an iterator on a trace
79 struct babeltrace_iter
{
80 struct ptr_heap
*stream_heap
;
81 struct trace_collection
*tc
;
82 struct trace_collection_pos
*end_pos
;
83 GArray
*callbacks
; /* Array of struct bt_stream_callbacks */
84 struct bt_callback_chain main_callbacks
; /* For all events */
86 * Flag indicating if dependency graph needs to be recalculated.
87 * Set by babeltrace_iter_add_callback(), and checked (and
88 * cleared) by upon entry into babeltrace_iter_read_event().
89 * babeltrace_iter_read_event() is responsible for calling dep
90 * graph calculation if it sees this flag set.
92 int recalculate_dep_graph
;
94 * Array of pointers to struct bt_dependencies, for garbage
95 * collection. We're not using a linked list here because each
96 * struct bt_dependencies can belong to more than one
102 struct bt_dependencies
{
103 GArray
*deps
; /* Array of GQuarks */
104 int refcount
; /* free when decremented to 0 */
108 struct bt_dependencies
*_babeltrace_dependencies_create(const char *first
,
112 struct bt_dependencies
*dep
;
114 dep
= g_new0(struct bt_dependencies
, 1);
116 dep
->deps
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
119 GQuark q
= g_quark_from_string(iter
);
120 g_array_append_val(dep
->deps
, q
);
121 iter
= va_arg(ap
, const char *);
126 struct bt_dependencies
*babeltrace_dependencies_create(const char *first
, ...)
129 struct bt_dependencies
*deps
;
132 deps
= _babeltrace_dependencies_create(first
, ap
);
138 * babeltrace_iter_add_callback: Add a callback to iterator.
140 int babeltrace_iter_add_callback(struct babeltrace_iter
*iter
,
141 bt_event_name event
, void *private_data
, int flags
,
142 enum bt_cb_ret (*callback
)(struct bt_ctf_data
*ctf_data
,
144 struct bt_dependencies
*depends
,
145 struct bt_dependencies
*weak_depends
,
146 struct bt_dependencies
*provides
)
149 gpointer
*event_id_ptr
;
150 unsigned long event_id
;
151 struct trace_collection
*tc
= iter
->tc
;
153 for (i
= 0; i
< tc
->array
->len
; i
++) {
154 struct ctf_trace
*tin
;
155 struct trace_descriptor
*td_read
;
157 td_read
= g_ptr_array_index(tc
->array
, i
);
158 tin
= container_of(td_read
, struct ctf_trace
, parent
);
160 for (stream_id
= 0; stream_id
< tin
->streams
->len
; stream_id
++) {
161 struct ctf_stream_class
*stream
;
162 struct bt_stream_callbacks
*bt_stream_cb
= NULL
;
163 struct bt_callback_chain
*bt_chain
= NULL
;
164 struct bt_callback new_callback
;
166 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
168 /* find or create the bt_stream_callbacks for this stream */
169 if (iter
->callbacks
->len
>= stream_id
) {
170 bt_stream_cb
= &g_array_index(iter
->callbacks
,
171 struct bt_stream_callbacks
, stream
->stream_id
);
173 g_array_set_size(iter
->callbacks
, stream
->stream_id
);
175 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
) {
176 struct bt_stream_callbacks new_stream_cb
;
177 new_stream_cb
.per_id_callbacks
= g_array_new(1, 1,
178 sizeof(struct bt_callback_chain
));
179 g_array_insert_val(iter
->callbacks
, stream
->stream_id
, new_stream_cb
);
180 bt_stream_cb
= &g_array_index(iter
->callbacks
,
181 struct bt_stream_callbacks
, stream
->stream_id
);
185 /* find the event id */
186 event_id_ptr
= g_hash_table_lookup(stream
->event_quark_to_id
,
187 (gconstpointer
) (unsigned long) event
);
188 /* event not found in this stream class */
190 printf("event not found\n");
193 event_id
= (uint64_t)(unsigned long)*event_id_ptr
;
195 /* find or create the bt_callback_chain for this event */
196 if (bt_stream_cb
->per_id_callbacks
->len
>= event_id
) {
197 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
198 struct bt_callback_chain
, event_id
);
200 g_array_set_size(bt_stream_cb
->per_id_callbacks
, event_id
);
202 if (!bt_chain
|| !bt_chain
->callback
) {
203 struct bt_callback_chain new_chain
;
204 new_chain
.callback
= g_array_new(1, 1, sizeof(struct bt_callback
));
205 g_array_insert_val(bt_stream_cb
->per_id_callbacks
, event_id
,
207 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
208 struct bt_callback_chain
, event_id
);
211 /* callback for all events */
212 if (!iter
->main_callbacks
.callback
) {
213 iter
->main_callbacks
.callback
= g_array_new(1, 1,
214 sizeof(struct bt_callback
));
216 bt_chain
= &iter
->main_callbacks
;
219 new_callback
.private_data
= private_data
;
220 new_callback
.flags
= flags
;
221 new_callback
.callback
= callback
;
222 new_callback
.depends
= depends
;
223 new_callback
.weak_depends
= weak_depends
;
224 new_callback
.provides
= provides
;
226 /* TODO : take care of priority, for now just FIFO */
227 g_array_append_val(bt_chain
->callback
, new_callback
);
234 static int stream_read_event(struct ctf_file_stream
*sin
)
238 ret
= sin
->pos
.parent
.event_cb(&sin
->pos
.parent
, &sin
->parent
);
242 fprintf(stdout
, "[error] Reading event failed.\n");
249 * returns true if a < b, false otherwise.
251 int stream_compare(void *a
, void *b
)
253 struct ctf_file_stream
*s_a
= a
, *s_b
= b
;
255 if (s_a
->parent
.timestamp
< s_b
->parent
.timestamp
)
262 * babeltrace_filestream_seek: seek a filestream to given position.
264 * The stream_id parameter is only useful for BT_SEEK_RESTORE.
266 static int babeltrace_filestream_seek(struct ctf_file_stream
*file_stream
,
267 const struct trace_collection_pos
*begin_pos
,
268 unsigned long stream_id
)
272 switch (begin_pos
->type
) {
275 * just insert into the heap we should already know
280 ctf_move_pos_slow(&file_stream
->pos
, 0, SEEK_SET
);
281 ret
= stream_read_event(file_stream
);
284 case BT_SEEK_RESTORE
:
287 assert(0); /* Not yet defined */
294 * babeltrace_iter_seek: seek iterator to given position.
296 int babeltrace_iter_seek(struct babeltrace_iter
*iter
,
297 const struct trace_collection_pos
*begin_pos
)
301 struct trace_collection
*tc
= iter
->tc
;
303 for (i
= 0; i
< tc
->array
->len
; i
++) {
304 struct ctf_trace
*tin
;
305 struct trace_descriptor
*td_read
;
307 td_read
= g_ptr_array_index(tc
->array
, i
);
308 tin
= container_of(td_read
, struct ctf_trace
, parent
);
310 /* Populate heap with each stream */
311 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
313 struct ctf_stream_class
*stream
;
316 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
317 for (filenr
= 0; filenr
< stream
->streams
->len
;
319 struct ctf_file_stream
*file_stream
;
321 file_stream
= g_ptr_array_index(stream
->streams
,
323 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
334 struct babeltrace_iter
*babeltrace_iter_create(struct trace_collection
*tc
,
335 struct trace_collection_pos
*begin_pos
,
336 struct trace_collection_pos
*end_pos
)
340 struct babeltrace_iter
*iter
;
342 iter
= malloc(sizeof(struct babeltrace_iter
));
345 iter
->stream_heap
= g_new(struct ptr_heap
, 1);
347 iter
->end_pos
= end_pos
;
348 iter
->callbacks
= g_array_new(0, 1, sizeof(struct bt_stream_callbacks
));
349 iter
->recalculate_dep_graph
= 0;
350 iter
->main_callbacks
.callback
= NULL
;
351 iter
->dep_gc
= g_ptr_array_new();
353 ret
= heap_init(iter
->stream_heap
, 0, stream_compare
);
355 goto error_heap_init
;
357 for (i
= 0; i
< tc
->array
->len
; i
++) {
358 struct ctf_trace
*tin
;
359 struct trace_descriptor
*td_read
;
361 td_read
= g_ptr_array_index(tc
->array
, i
);
362 tin
= container_of(td_read
, struct ctf_trace
, parent
);
364 /* Populate heap with each stream */
365 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
367 struct ctf_stream_class
*stream
;
370 stream
= g_ptr_array_index(tin
->streams
, stream_id
);
373 for (filenr
= 0; filenr
< stream
->streams
->len
;
375 struct ctf_file_stream
*file_stream
;
377 file_stream
= g_ptr_array_index(stream
->streams
,
381 ret
= babeltrace_filestream_seek(file_stream
, begin_pos
,
391 ret
= heap_insert(iter
->stream_heap
, file_stream
);
401 heap_free(iter
->stream_heap
);
403 g_free(iter
->stream_heap
);
409 void babeltrace_iter_destroy(struct babeltrace_iter
*iter
)
411 struct bt_stream_callbacks
*bt_stream_cb
;
412 struct bt_callback_chain
*bt_chain
;
415 heap_free(iter
->stream_heap
);
416 g_free(iter
->stream_heap
);
418 /* free all events callbacks */
419 if (iter
->main_callbacks
.callback
)
420 g_array_free(iter
->main_callbacks
.callback
, TRUE
);
422 /* free per-event callbacks */
423 for (i
= 0; i
< iter
->callbacks
->len
; i
++) {
424 bt_stream_cb
= &g_array_index(iter
->callbacks
,
425 struct bt_stream_callbacks
, i
);
426 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
428 for (j
= 0; j
< bt_stream_cb
->per_id_callbacks
->len
; j
++) {
429 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
430 struct bt_callback_chain
, j
);
431 if (bt_chain
->callback
) {
432 g_array_free(bt_chain
->callback
, TRUE
);
435 g_array_free(bt_stream_cb
->per_id_callbacks
, TRUE
);
441 int babeltrace_iter_next(struct babeltrace_iter
*iter
)
443 struct ctf_file_stream
*file_stream
, *removed
;
446 file_stream
= heap_maximum(iter
->stream_heap
);
448 /* end of file for all streams */
453 ret
= stream_read_event(file_stream
);
455 removed
= heap_remove(iter
->stream_heap
);
456 assert(removed
== file_stream
);
462 /* Reinsert the file stream into the heap, and rebalance. */
463 removed
= heap_replace_max(iter
->stream_heap
, file_stream
);
464 assert(removed
== file_stream
);
471 struct ctf_stream_event
*extract_ctf_stream_event(struct ctf_stream
*stream
)
473 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
474 struct ctf_event
*event_class
;
475 struct ctf_stream_event
*event
;
476 uint64_t id
= stream
->event_id
;
478 if (id
>= stream_class
->events_by_id
->len
) {
479 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
482 event
= g_ptr_array_index(stream
->events_by_id
, id
);
484 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
487 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
489 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
497 void process_callbacks(struct babeltrace_iter
*iter
,
498 struct ctf_stream
*stream
)
500 struct bt_stream_callbacks
*bt_stream_cb
;
501 struct bt_callback_chain
*bt_chain
;
502 struct bt_callback
*cb
;
505 struct bt_ctf_data ctf_data
;
507 ctf_data
.event
= extract_ctf_stream_event(stream
);
509 /* process all events callback first */
510 if (iter
->main_callbacks
.callback
) {
511 for (i
= 0; i
< iter
->main_callbacks
.callback
->len
; i
++) {
512 cb
= &g_array_index(iter
->main_callbacks
.callback
, struct bt_callback
, i
);
515 ret
= cb
->callback(&ctf_data
, cb
->private_data
);
518 case BT_CB_ERROR_STOP
:
526 /* process per event callbacks */
527 bt_stream_cb
= &g_array_index(iter
->callbacks
,
528 struct bt_stream_callbacks
, stream
->stream_id
);
529 if (!bt_stream_cb
|| !bt_stream_cb
->per_id_callbacks
)
532 if (stream
->event_id
> bt_stream_cb
->per_id_callbacks
->len
)
534 bt_chain
= &g_array_index(bt_stream_cb
->per_id_callbacks
,
535 struct bt_callback_chain
, stream
->event_id
);
536 if (!bt_chain
|| !bt_chain
->callback
)
539 for (i
= 0; i
< bt_chain
->callback
->len
; i
++) {
540 cb
= &g_array_index(bt_chain
->callback
, struct bt_callback
, i
);
543 ret
= cb
->callback(&ctf_data
, cb
->private_data
);
546 case BT_CB_ERROR_STOP
:
557 int babeltrace_iter_read_event(struct babeltrace_iter
*iter
,
558 struct ctf_stream
**stream
,
559 struct ctf_stream_event
**event
)
561 struct ctf_file_stream
*file_stream
;
564 file_stream
= heap_maximum(iter
->stream_heap
);
566 /* end of file for all streams */
570 *stream
= &file_stream
->parent
;
571 *event
= g_ptr_array_index((*stream
)->events_by_id
, (*stream
)->event_id
);
573 if ((*stream
)->stream_id
> iter
->callbacks
->len
)
576 process_callbacks(iter
, *stream
);
582 int convert_trace(struct trace_descriptor
*td_write
,
583 struct trace_collection
*trace_collection_read
)
585 struct babeltrace_iter
*iter
;
586 struct ctf_stream
*stream
;
587 struct ctf_stream_event
*event
;
588 struct ctf_text_stream_pos
*sout
;
589 struct trace_collection_pos begin_pos
;
592 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
595 begin_pos
.type
= BT_SEEK_BEGIN
;
596 iter
= babeltrace_iter_create(trace_collection_read
, &begin_pos
, NULL
);
597 while (babeltrace_iter_read_event(iter
, &stream
, &event
) == 0) {
598 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
600 fprintf(stdout
, "[error] Writing event failed.\n");
603 ret
= babeltrace_iter_next(iter
);
608 babeltrace_iter_destroy(iter
);