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> | |
33b34c43 | 30 | #include <babeltrace/plugin/plugin-dev.h> |
b2e0c907 PP |
31 | #include <babeltrace/graph/component.h> |
32 | #include <babeltrace/graph/private-component.h> | |
33 | #include <babeltrace/graph/component-sink.h> | |
34 | #include <babeltrace/graph/private-component-sink.h> | |
35 | #include <babeltrace/graph/private-port.h> | |
36 | #include <babeltrace/graph/private-connection.h> | |
37 | #include <babeltrace/graph/notification.h> | |
38 | #include <babeltrace/graph/notification-iterator.h> | |
39 | #include <babeltrace/graph/notification-event.h> | |
40 | #include <babeltrace/graph/notification-packet.h> | |
f3168545 | 41 | #include <babeltrace/graph/notification-stream.h> |
7d61fa8e | 42 | #include <plugins-common.h> |
bc506aa5 JD |
43 | #include <stdio.h> |
44 | #include <stdbool.h> | |
45 | #include <glib.h> | |
46 | #include "writer.h" | |
c70ed359 | 47 | #include <assert.h> |
bc506aa5 | 48 | |
a619fcb7 | 49 | gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data) |
f3168545 | 50 | { |
a619fcb7 JD |
51 | struct fs_writer *fs_writer = value; |
52 | struct writer_component *writer_component = user_data; | |
53 | ||
3b41eaba | 54 | fs_writer->trace_static = 1; |
a619fcb7 JD |
55 | writer_close(writer_component, fs_writer); |
56 | ||
f3168545 JD |
57 | return TRUE; |
58 | } | |
59 | ||
bc506aa5 JD |
60 | static |
61 | void destroy_writer_component_data(struct writer_component *writer_component) | |
62 | { | |
c70ed359 | 63 | bt_put(writer_component->input_iterator); |
f3168545 | 64 | |
f3168545 | 65 | g_hash_table_foreach_remove(writer_component->trace_map, |
a619fcb7 | 66 | empty_trace_map, writer_component); |
bc506aa5 | 67 | g_hash_table_destroy(writer_component->trace_map); |
f3168545 | 68 | |
9057f037 JD |
69 | g_string_free(writer_component->base_path, true); |
70 | g_string_free(writer_component->trace_name_base, true); | |
bc506aa5 JD |
71 | } |
72 | ||
d8866baa PP |
73 | BT_HIDDEN |
74 | void writer_component_finalize(struct bt_private_component *component) | |
bc506aa5 JD |
75 | { |
76 | struct writer_component *writer_component = (struct writer_component *) | |
890882ef | 77 | bt_private_component_get_user_data(component); |
bc506aa5 JD |
78 | |
79 | destroy_writer_component_data(writer_component); | |
80 | g_free(writer_component); | |
81 | } | |
82 | ||
bc506aa5 | 83 | static |
f3168545 | 84 | void free_fs_writer(struct fs_writer *fs_writer) |
bc506aa5 | 85 | { |
f3168545 JD |
86 | bt_put(fs_writer->writer); |
87 | g_free(fs_writer); | |
bc506aa5 JD |
88 | } |
89 | ||
90 | static | |
91 | struct writer_component *create_writer_component(void) | |
92 | { | |
93 | struct writer_component *writer_component; | |
94 | ||
95 | writer_component = g_new0(struct writer_component, 1); | |
96 | if (!writer_component) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | writer_component->err = stderr; | |
101 | writer_component->trace_id = 0; | |
9057f037 JD |
102 | writer_component->trace_name_base = g_string_new("trace"); |
103 | if (!writer_component->trace_name_base) { | |
104 | g_free(writer_component); | |
105 | writer_component = NULL; | |
106 | goto end; | |
107 | } | |
bc506aa5 JD |
108 | |
109 | /* | |
110 | * Reader to writer corresponding structures. | |
111 | */ | |
112 | writer_component->trace_map = g_hash_table_new_full(g_direct_hash, | |
f3168545 | 113 | g_direct_equal, NULL, (GDestroyNotify) free_fs_writer); |
bc506aa5 JD |
114 | |
115 | end: | |
116 | return writer_component; | |
117 | } | |
118 | ||
119 | static | |
120 | enum bt_component_status handle_notification( | |
121 | struct writer_component *writer_component, | |
122 | struct bt_notification *notification) | |
123 | { | |
124 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
125 | ||
126 | if (!writer_component) { | |
127 | ret = BT_COMPONENT_STATUS_ERROR; | |
128 | goto end; | |
129 | } | |
130 | ||
131 | switch (bt_notification_get_type(notification)) { | |
ea0e619e | 132 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
bc506aa5 JD |
133 | { |
134 | struct bt_ctf_packet *packet = | |
ea0e619e | 135 | bt_notification_packet_begin_get_packet(notification); |
bc506aa5 JD |
136 | |
137 | if (!packet) { | |
138 | ret = BT_COMPONENT_STATUS_ERROR; | |
139 | goto end; | |
140 | } | |
141 | ||
142 | ret = writer_new_packet(writer_component, packet); | |
143 | bt_put(packet); | |
144 | break; | |
145 | } | |
146 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
147 | { | |
148 | struct bt_ctf_packet *packet = | |
ea0e619e | 149 | bt_notification_packet_end_get_packet(notification); |
bc506aa5 JD |
150 | |
151 | if (!packet) { | |
152 | ret = BT_COMPONENT_STATUS_ERROR; | |
153 | goto end; | |
154 | } | |
155 | ret = writer_close_packet(writer_component, packet); | |
156 | bt_put(packet); | |
157 | break; | |
158 | } | |
159 | case BT_NOTIFICATION_TYPE_EVENT: | |
160 | { | |
161 | struct bt_ctf_event *event = bt_notification_event_get_event( | |
162 | notification); | |
163 | ||
164 | if (!event) { | |
165 | ret = BT_COMPONENT_STATUS_ERROR; | |
166 | goto end; | |
167 | } | |
bc506aa5 JD |
168 | ret = writer_output_event(writer_component, event); |
169 | bt_put(event); | |
170 | if (ret != BT_COMPONENT_STATUS_OK) { | |
171 | goto end; | |
172 | } | |
173 | break; | |
174 | } | |
f384901f JD |
175 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: |
176 | { | |
177 | struct bt_ctf_stream *stream = | |
178 | bt_notification_stream_begin_get_stream(notification); | |
179 | ||
180 | if (!stream) { | |
181 | ret = BT_COMPONENT_STATUS_ERROR; | |
182 | goto end; | |
183 | } | |
184 | ret = writer_stream_begin(writer_component, stream); | |
185 | bt_put(stream); | |
186 | break; | |
187 | } | |
bc506aa5 | 188 | case BT_NOTIFICATION_TYPE_STREAM_END: |
f3168545 JD |
189 | { |
190 | struct bt_ctf_stream *stream = | |
191 | bt_notification_stream_end_get_stream(notification); | |
192 | ||
193 | if (!stream) { | |
194 | ret = BT_COMPONENT_STATUS_ERROR; | |
195 | goto end; | |
196 | } | |
197 | ret = writer_stream_end(writer_component, stream); | |
198 | bt_put(stream); | |
bc506aa5 | 199 | break; |
f3168545 | 200 | } |
bc506aa5 JD |
201 | default: |
202 | puts("Unhandled notification type"); | |
203 | } | |
204 | end: | |
205 | return ret; | |
206 | } | |
207 | ||
d8866baa | 208 | BT_HIDDEN |
0d8b4d8e | 209 | void writer_component_port_connected( |
890882ef | 210 | struct bt_private_component *component, |
8f4799f7 PP |
211 | struct bt_private_port *self_port, |
212 | struct bt_port *other_port) | |
c70ed359 | 213 | { |
890882ef | 214 | struct bt_private_connection *connection; |
c70ed359 | 215 | struct writer_component *writer; |
93fc16a5 | 216 | static const enum bt_notification_type notif_types[] = { |
f3168545 | 217 | BT_NOTIFICATION_TYPE_EVENT, |
93fc16a5 JD |
218 | BT_NOTIFICATION_TYPE_PACKET_BEGIN, |
219 | BT_NOTIFICATION_TYPE_PACKET_END, | |
f384901f | 220 | BT_NOTIFICATION_TYPE_STREAM_BEGIN, |
f3168545 | 221 | BT_NOTIFICATION_TYPE_STREAM_END, |
93fc16a5 JD |
222 | BT_NOTIFICATION_TYPE_SENTINEL, |
223 | }; | |
c70ed359 | 224 | |
890882ef | 225 | writer = bt_private_component_get_user_data(component); |
c70ed359 JG |
226 | assert(writer); |
227 | assert(!writer->input_iterator); | |
890882ef | 228 | connection = bt_private_port_get_private_connection(self_port); |
72b913fb | 229 | assert(connection); |
fa054faf PP |
230 | writer->input_iterator = |
231 | bt_private_connection_create_notification_iterator(connection, | |
93fc16a5 | 232 | notif_types); |
c70ed359 JG |
233 | |
234 | if (!writer->input_iterator) { | |
0d8b4d8e | 235 | writer->error = true; |
c70ed359 | 236 | } |
0d8b4d8e | 237 | |
72b913fb | 238 | bt_put(connection); |
c70ed359 JG |
239 | } |
240 | ||
d8866baa PP |
241 | BT_HIDDEN |
242 | enum bt_component_status writer_run(struct bt_private_component *component) | |
bc506aa5 JD |
243 | { |
244 | enum bt_component_status ret; | |
245 | struct bt_notification *notification = NULL; | |
246 | struct bt_notification_iterator *it; | |
247 | struct writer_component *writer_component = | |
890882ef | 248 | bt_private_component_get_user_data(component); |
7cdc2bab | 249 | enum bt_notification_iterator_status it_ret; |
bc506aa5 | 250 | |
0d8b4d8e PP |
251 | if (unlikely(writer_component->error)) { |
252 | ret = BT_COMPONENT_STATUS_ERROR; | |
253 | goto end; | |
254 | } | |
255 | ||
7cdc2bab MD |
256 | it = writer_component->input_iterator; |
257 | assert(it); | |
258 | it_ret = bt_notification_iterator_next(it); | |
bc506aa5 | 259 | |
7cdc2bab MD |
260 | switch (it_ret) { |
261 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
262 | ret = BT_COMPONENT_STATUS_END; | |
263 | BT_PUT(writer_component->input_iterator); | |
264 | goto end; | |
265 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
266 | ret = BT_COMPONENT_STATUS_AGAIN; | |
267 | goto end; | |
268 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
269 | break; | |
270 | default: | |
72b913fb | 271 | ret = BT_COMPONENT_STATUS_ERROR; |
996a07a1 JG |
272 | goto end; |
273 | } | |
274 | ||
7cdc2bab MD |
275 | notification = bt_notification_iterator_get_notification(it); |
276 | assert(notification); | |
bc506aa5 JD |
277 | ret = handle_notification(writer_component, notification); |
278 | end: | |
bc506aa5 JD |
279 | bt_put(notification); |
280 | return ret; | |
281 | } | |
282 | ||
d8866baa | 283 | BT_HIDDEN |
bc506aa5 | 284 | enum bt_component_status writer_component_init( |
890882ef | 285 | struct bt_private_component *component, struct bt_value *params, |
7d61fa8e | 286 | UNUSED_VAR void *init_method_data) |
bc506aa5 JD |
287 | { |
288 | enum bt_component_status ret; | |
289 | enum bt_value_status value_ret; | |
290 | struct writer_component *writer_component = create_writer_component(); | |
291 | struct bt_value *value = NULL; | |
292 | const char *path; | |
293 | ||
294 | if (!writer_component) { | |
295 | ret = BT_COMPONENT_STATUS_NOMEM; | |
296 | goto end; | |
297 | } | |
298 | ||
147337a3 PP |
299 | ret = bt_private_component_sink_add_input_private_port(component, |
300 | "in", NULL, NULL); | |
301 | if (ret != BT_COMPONENT_STATUS_OK) { | |
b9d103be PP |
302 | goto end; |
303 | } | |
304 | ||
bc506aa5 JD |
305 | value = bt_value_map_get(params, "path"); |
306 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
307 | fprintf(writer_component->err, | |
308 | "[error] output path parameter required\n"); | |
309 | ret = BT_COMPONENT_STATUS_INVALID; | |
310 | goto error; | |
311 | } | |
312 | ||
313 | value_ret = bt_value_string_get(value, &path); | |
314 | if (value_ret != BT_VALUE_STATUS_OK) { | |
315 | ret = BT_COMPONENT_STATUS_INVALID; | |
316 | goto error; | |
317 | } | |
d00b90bf | 318 | bt_put(value); |
bc506aa5 | 319 | |
9057f037 JD |
320 | writer_component->base_path = g_string_new(path); |
321 | if (!writer_component) { | |
322 | ret = BT_COMPONENT_STATUS_ERROR; | |
323 | goto error; | |
324 | } | |
bc506aa5 | 325 | |
890882ef | 326 | ret = bt_private_component_set_user_data(component, writer_component); |
bc506aa5 JD |
327 | if (ret != BT_COMPONENT_STATUS_OK) { |
328 | goto error; | |
329 | } | |
330 | ||
bc506aa5 JD |
331 | end: |
332 | return ret; | |
333 | error: | |
334 | destroy_writer_component_data(writer_component); | |
56fa591a | 335 | g_free(writer_component); |
bc506aa5 JD |
336 | return ret; |
337 | } |