Commit | Line | Data |
---|---|---|
46322b33 MD |
1 | /* |
2 | * babeltrace-lib.c | |
3 | * | |
4 | * Babeltrace Trace Converter Library | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
46322b33 MD |
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> | |
847bf71a | 24 | #include <inttypes.h> |
46322b33 MD |
25 | #include <babeltrace/babeltrace.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> | |
0d0f5149 | 30 | #include <babeltrace/prio_heap.h> |
46322b33 | 31 | |
0d0f5149 | 32 | static int read_event(struct ctf_file_stream *sin) |
46322b33 MD |
33 | { |
34 | int ret; | |
35 | ||
2d0bea29 | 36 | ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent); |
0d0f5149 MD |
37 | if (ret == EOF) |
38 | return EOF; | |
39 | else if (ret) { | |
40 | fprintf(stdout, "[error] Reading event failed.\n"); | |
41 | return ret; | |
46322b33 | 42 | } |
46322b33 | 43 | return 0; |
0d0f5149 | 44 | } |
46322b33 | 45 | |
0d0f5149 MD |
46 | /* |
47 | * returns true if a < b, false otherwise. | |
48 | */ | |
49 | int stream_compare(void *a, void *b) | |
50 | { | |
51 | struct ctf_file_stream *s_a = a, *s_b = b; | |
52 | ||
2d0bea29 | 53 | if (s_a->parent.timestamp < s_b->parent.timestamp) |
0d0f5149 MD |
54 | return 1; |
55 | else | |
56 | return 0; | |
46322b33 MD |
57 | } |
58 | ||
847bf71a MD |
59 | int convert_trace(struct trace_descriptor *td_write, |
60 | struct trace_descriptor *td_read) | |
46322b33 MD |
61 | { |
62 | struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent); | |
847bf71a MD |
63 | struct ctf_text_stream_pos *sout = |
64 | container_of(td_write, struct ctf_text_stream_pos, trace_descriptor); | |
0d0f5149 MD |
65 | int stream_id; |
66 | int ret = 0; | |
67 | ||
68 | tin->stream_heap = g_new(struct ptr_heap, 1); | |
69 | heap_init(tin->stream_heap, 0, stream_compare); | |
46322b33 | 70 | |
0d0f5149 | 71 | /* Populate heap with each stream */ |
46322b33 | 72 | for (stream_id = 0; stream_id < tin->streams->len; stream_id++) { |
aa6bffae | 73 | struct ctf_stream_class *stream = g_ptr_array_index(tin->streams, stream_id); |
0d0f5149 | 74 | int filenr; |
46322b33 MD |
75 | |
76 | if (!stream) | |
77 | continue; | |
2d0bea29 MD |
78 | for (filenr = 0; filenr < stream->streams->len; filenr++) { |
79 | struct ctf_file_stream *file_stream = g_ptr_array_index(stream->streams, filenr); | |
0d0f5149 MD |
80 | ret = read_event(file_stream); |
81 | if (ret == EOF) { | |
82 | ret = 0; | |
83 | continue; | |
84 | } else if (ret) | |
85 | goto end; | |
86 | /* Add to heap */ | |
87 | ret = heap_insert(tin->stream_heap, file_stream); | |
46322b33 | 88 | if (ret) { |
0d0f5149 MD |
89 | fprintf(stdout, "[error] Out of memory.\n"); |
90 | goto end; | |
46322b33 MD |
91 | } |
92 | } | |
93 | } | |
94 | ||
0d0f5149 MD |
95 | /* Replace heap entries until EOF for each stream (heap empty) */ |
96 | for (;;) { | |
97 | struct ctf_file_stream *file_stream, *removed; | |
98 | ||
99 | file_stream = heap_maximum(tin->stream_heap); | |
100 | if (!file_stream) { | |
101 | /* end of file for all streams */ | |
102 | ret = 0; | |
103 | break; | |
104 | } | |
2d0bea29 | 105 | ret = sout->parent.event_cb(&sout->parent, &file_stream->parent); |
0d0f5149 MD |
106 | if (ret) { |
107 | fprintf(stdout, "[error] Writing event failed.\n"); | |
108 | goto end; | |
109 | } | |
110 | ret = read_event(file_stream); | |
111 | if (ret == EOF) { | |
112 | removed = heap_remove(tin->stream_heap); | |
113 | assert(removed == file_stream); | |
114 | ret = 0; | |
115 | continue; | |
116 | } else if (ret) | |
117 | goto end; | |
118 | /* Reinsert the file stream into the heap, and rebalance. */ | |
119 | removed = heap_replace_max(tin->stream_heap, file_stream); | |
120 | assert(removed == file_stream); | |
121 | } | |
46322b33 | 122 | |
0d0f5149 MD |
123 | end: |
124 | heap_free(tin->stream_heap); | |
125 | g_free(tin->stream_heap); | |
46322b33 MD |
126 | return ret; |
127 | } |