Commit | Line | Data |
---|---|---|
bc506aa5 JD |
1 | /* |
2 | * writer.c | |
3 | * | |
4 | * Babeltrace CTF Writer Output Plugin | |
5 | * | |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@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 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/ctf-ir/packet.h> | |
30 | #include <babeltrace/plugin/plugin-macros.h> | |
31 | #include <babeltrace/plugin/component.h> | |
32 | #include <babeltrace/plugin/sink.h> | |
33 | #include <babeltrace/plugin/notification/notification.h> | |
34 | #include <babeltrace/plugin/notification/iterator.h> | |
35 | #include <babeltrace/plugin/notification/event.h> | |
36 | #include <babeltrace/plugin/notification/packet.h> | |
37 | #include <stdio.h> | |
38 | #include <stdbool.h> | |
39 | #include <glib.h> | |
40 | #include "writer.h" | |
41 | ||
42 | static | |
43 | void destroy_writer_component_data(struct writer_component *writer_component) | |
44 | { | |
45 | g_hash_table_destroy(writer_component->stream_map); | |
46 | g_hash_table_destroy(writer_component->stream_class_map); | |
47 | g_hash_table_destroy(writer_component->trace_map); | |
9057f037 JD |
48 | g_string_free(writer_component->base_path, true); |
49 | g_string_free(writer_component->trace_name_base, true); | |
bc506aa5 JD |
50 | } |
51 | ||
52 | static | |
53 | void destroy_writer_component(struct bt_component *component) | |
54 | { | |
55 | struct writer_component *writer_component = (struct writer_component *) | |
56 | bt_component_get_private_data(component); | |
57 | ||
58 | destroy_writer_component_data(writer_component); | |
59 | g_free(writer_component); | |
60 | } | |
61 | ||
62 | static | |
63 | void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class) | |
64 | { | |
65 | BT_PUT(writer_stream_class); | |
66 | g_free(writer_stream_class); | |
67 | } | |
68 | ||
69 | static | |
70 | void unref_stream(struct bt_ctf_stream_class *writer_stream) | |
71 | { | |
72 | BT_PUT(writer_stream); | |
73 | g_free(writer_stream); | |
74 | } | |
75 | ||
76 | static | |
77 | void unref_trace(struct bt_ctf_writer *writer) | |
78 | { | |
79 | BT_PUT(writer); | |
80 | g_free(writer); | |
81 | } | |
82 | ||
83 | static | |
84 | struct writer_component *create_writer_component(void) | |
85 | { | |
86 | struct writer_component *writer_component; | |
87 | ||
88 | writer_component = g_new0(struct writer_component, 1); | |
89 | if (!writer_component) { | |
90 | goto end; | |
91 | } | |
92 | ||
93 | writer_component->err = stderr; | |
94 | writer_component->trace_id = 0; | |
9057f037 JD |
95 | writer_component->trace_name_base = g_string_new("trace"); |
96 | if (!writer_component->trace_name_base) { | |
97 | g_free(writer_component); | |
98 | writer_component = NULL; | |
99 | goto end; | |
100 | } | |
bc506aa5 JD |
101 | |
102 | /* | |
103 | * Reader to writer corresponding structures. | |
104 | */ | |
105 | writer_component->trace_map = g_hash_table_new_full(g_direct_hash, | |
106 | g_direct_equal, NULL, (GDestroyNotify) unref_trace); | |
107 | writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash, | |
108 | g_direct_equal, NULL, (GDestroyNotify) unref_stream_class); | |
109 | writer_component->stream_map = g_hash_table_new_full(g_direct_hash, | |
110 | g_direct_equal, NULL, (GDestroyNotify) unref_stream); | |
111 | ||
112 | end: | |
113 | return writer_component; | |
114 | } | |
115 | ||
116 | static | |
117 | enum bt_component_status handle_notification( | |
118 | struct writer_component *writer_component, | |
119 | struct bt_notification *notification) | |
120 | { | |
121 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
122 | ||
123 | if (!writer_component) { | |
124 | ret = BT_COMPONENT_STATUS_ERROR; | |
125 | goto end; | |
126 | } | |
127 | ||
128 | switch (bt_notification_get_type(notification)) { | |
129 | case BT_NOTIFICATION_TYPE_PACKET_START: | |
130 | { | |
131 | struct bt_ctf_packet *packet = | |
132 | bt_notification_packet_start_get_packet(notification); | |
133 | ||
134 | if (!packet) { | |
135 | ret = BT_COMPONENT_STATUS_ERROR; | |
136 | goto end; | |
137 | } | |
138 | ||
139 | ret = writer_new_packet(writer_component, packet); | |
140 | bt_put(packet); | |
141 | break; | |
142 | } | |
143 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
144 | { | |
145 | struct bt_ctf_packet *packet = | |
146 | bt_notification_packet_start_get_packet(notification); | |
147 | ||
148 | if (!packet) { | |
149 | ret = BT_COMPONENT_STATUS_ERROR; | |
150 | goto end; | |
151 | } | |
152 | ret = writer_close_packet(writer_component, packet); | |
153 | bt_put(packet); | |
154 | break; | |
155 | } | |
156 | case BT_NOTIFICATION_TYPE_EVENT: | |
157 | { | |
158 | struct bt_ctf_event *event = bt_notification_event_get_event( | |
159 | notification); | |
160 | ||
161 | if (!event) { | |
162 | ret = BT_COMPONENT_STATUS_ERROR; | |
163 | goto end; | |
164 | } | |
165 | ret = BT_COMPONENT_STATUS_OK; | |
166 | ret = writer_output_event(writer_component, event); | |
167 | bt_put(event); | |
168 | if (ret != BT_COMPONENT_STATUS_OK) { | |
169 | goto end; | |
170 | } | |
171 | break; | |
172 | } | |
173 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
174 | break; | |
175 | default: | |
176 | puts("Unhandled notification type"); | |
177 | } | |
178 | end: | |
179 | return ret; | |
180 | } | |
181 | ||
182 | static | |
183 | enum bt_component_status run(struct bt_component *component) | |
184 | { | |
185 | enum bt_component_status ret; | |
186 | struct bt_notification *notification = NULL; | |
187 | struct bt_notification_iterator *it; | |
188 | struct writer_component *writer_component = | |
189 | bt_component_get_private_data(component); | |
190 | ||
191 | ret = bt_component_sink_get_input_iterator(component, 0, &it); | |
192 | if (ret != BT_COMPONENT_STATUS_OK) { | |
193 | goto end; | |
194 | } | |
195 | ||
196 | ret = bt_notification_iterator_next(it); | |
197 | if (ret != BT_COMPONENT_STATUS_OK) { | |
198 | goto end; | |
199 | } | |
200 | ||
201 | notification = bt_notification_iterator_get_notification(it); | |
202 | if (!notification) { | |
203 | ret = BT_COMPONENT_STATUS_ERROR; | |
204 | goto end; | |
205 | } | |
206 | ||
207 | ret = handle_notification(writer_component, notification); | |
208 | end: | |
209 | bt_put(it); | |
210 | bt_put(notification); | |
211 | return ret; | |
212 | } | |
213 | ||
214 | static | |
215 | enum bt_component_status writer_component_init( | |
216 | struct bt_component *component, struct bt_value *params) | |
217 | { | |
218 | enum bt_component_status ret; | |
219 | enum bt_value_status value_ret; | |
220 | struct writer_component *writer_component = create_writer_component(); | |
221 | struct bt_value *value = NULL; | |
222 | const char *path; | |
223 | ||
224 | if (!writer_component) { | |
225 | ret = BT_COMPONENT_STATUS_NOMEM; | |
226 | goto end; | |
227 | } | |
228 | ||
229 | value = bt_value_map_get(params, "path"); | |
230 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
231 | fprintf(writer_component->err, | |
232 | "[error] output path parameter required\n"); | |
233 | ret = BT_COMPONENT_STATUS_INVALID; | |
234 | goto error; | |
235 | } | |
236 | ||
237 | value_ret = bt_value_string_get(value, &path); | |
238 | if (value_ret != BT_VALUE_STATUS_OK) { | |
239 | ret = BT_COMPONENT_STATUS_INVALID; | |
240 | goto error; | |
241 | } | |
242 | ||
9057f037 JD |
243 | writer_component->base_path = g_string_new(path); |
244 | if (!writer_component) { | |
245 | ret = BT_COMPONENT_STATUS_ERROR; | |
246 | goto error; | |
247 | } | |
bc506aa5 JD |
248 | |
249 | ret = bt_component_set_destroy_cb(component, | |
250 | destroy_writer_component); | |
251 | if (ret != BT_COMPONENT_STATUS_OK) { | |
252 | goto error; | |
253 | } | |
254 | ||
255 | ret = bt_component_set_private_data(component, writer_component); | |
256 | if (ret != BT_COMPONENT_STATUS_OK) { | |
257 | goto error; | |
258 | } | |
259 | ||
260 | ret = bt_component_sink_set_consume_cb(component, | |
261 | run); | |
262 | if (ret != BT_COMPONENT_STATUS_OK) { | |
263 | goto error; | |
264 | } | |
265 | ||
266 | end: | |
267 | return ret; | |
268 | error: | |
269 | destroy_writer_component_data(writer_component); | |
270 | return ret; | |
271 | } | |
272 | ||
273 | /* Initialize plug-in entry points. */ | |
274 | BT_PLUGIN_NAME("writer"); | |
275 | BT_PLUGIN_DESCRIPTION("Babeltrace CTF-Writer output plug-in."); | |
276 | BT_PLUGIN_AUTHOR("Jérémie Galarneau"); | |
277 | BT_PLUGIN_LICENSE("MIT"); | |
278 | ||
279 | BT_PLUGIN_COMPONENT_CLASSES_BEGIN | |
280 | BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY("writer", | |
281 | "Formats CTF-IR to CTF.", | |
282 | writer_component_init) | |
283 | BT_PLUGIN_COMPONENT_CLASSES_END |