Commit | Line | Data |
---|---|---|
bc506aa5 JD |
1 | /* |
2 | * writer.c | |
3 | * | |
4 | * Babeltrace CTF Writer Output Plugin Event Handling | |
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/event.h> | |
30 | #include <babeltrace/ctf-ir/packet.h> | |
31 | #include <babeltrace/ctf-ir/event-class.h> | |
32 | #include <babeltrace/ctf-ir/stream.h> | |
33 | #include <babeltrace/ctf-ir/stream-class.h> | |
ac0c6bdd | 34 | #include <babeltrace/ctf-ir/clock-class.h> |
bc506aa5 JD |
35 | #include <babeltrace/ctf-ir/fields.h> |
36 | #include <babeltrace/ctf-writer/stream-class.h> | |
37 | #include <babeltrace/ctf-writer/stream.h> | |
f3168545 | 38 | #include <assert.h> |
bc506aa5 | 39 | |
91b73004 JD |
40 | #include <ctfcopytrace.h> |
41 | ||
bc506aa5 JD |
42 | #include "writer.h" |
43 | ||
a619fcb7 JD |
44 | static |
45 | void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class) | |
46 | { | |
47 | bt_put(writer_stream_class); | |
48 | } | |
49 | ||
50 | static | |
51 | void unref_stream(struct bt_ctf_stream_class *writer_stream) | |
52 | { | |
53 | bt_put(writer_stream); | |
54 | } | |
55 | ||
56 | gboolean empty_ht(gpointer key, gpointer value, gpointer user_data) | |
57 | { | |
58 | return TRUE; | |
59 | } | |
60 | ||
cdcf612f JD |
61 | void destroy_stream_state_key(gpointer key) |
62 | { | |
63 | g_free((enum fs_writer_stream_state *) key); | |
64 | } | |
65 | ||
f3168545 JD |
66 | static |
67 | void trace_is_static_listener(struct bt_ctf_trace *trace, void *data) | |
68 | { | |
69 | *((int *) data) = 1; | |
70 | } | |
71 | ||
bc506aa5 | 72 | static |
91b73004 JD |
73 | struct bt_ctf_stream_class *insert_new_stream_class( |
74 | struct writer_component *writer_component, | |
a619fcb7 | 75 | struct fs_writer *fs_writer, |
91b73004 | 76 | struct bt_ctf_stream_class *stream_class) |
bc506aa5 | 77 | { |
b2f1f465 | 78 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
9ae49d3d | 79 | struct bt_ctf_trace *trace = NULL, *writer_trace = NULL; |
a619fcb7 | 80 | struct bt_ctf_writer *ctf_writer = fs_writer->writer; |
3241bc18 | 81 | enum bt_component_status ret; |
bc506aa5 | 82 | |
3241bc18 JD |
83 | trace = bt_ctf_stream_class_get_trace(stream_class); |
84 | if (!trace) { | |
91b73004 JD |
85 | fprintf(writer_component->err, |
86 | "[error] %s in %s:%d\n", __func__, __FILE__, | |
bc506aa5 | 87 | __LINE__); |
9ae49d3d | 88 | goto error; |
bc506aa5 JD |
89 | } |
90 | ||
91b73004 JD |
91 | writer_trace = bt_ctf_writer_get_trace(ctf_writer); |
92 | if (!writer_trace) { | |
91b73004 JD |
93 | fprintf(writer_component->err, |
94 | "[error] %s in %s:%d\n", __func__, __FILE__, | |
d00b90bf | 95 | __LINE__); |
9ae49d3d | 96 | goto error; |
bc506aa5 JD |
97 | } |
98 | ||
91b73004 JD |
99 | ret = ctf_copy_clock_classes(writer_component->err, writer_trace, |
100 | writer_stream_class, trace); | |
bc506aa5 | 101 | if (ret != BT_COMPONENT_STATUS_OK) { |
91b73004 JD |
102 | fprintf(writer_component->err, |
103 | "[error] %s in %s:%d\n", __func__, __FILE__, | |
bc506aa5 | 104 | __LINE__); |
9ae49d3d | 105 | goto error; |
b2f1f465 JD |
106 | } |
107 | ||
108 | writer_stream_class = ctf_copy_stream_class(writer_component->err, | |
109 | stream_class, writer_trace, true); | |
110 | if (!writer_stream_class) { | |
111 | fprintf(writer_component->err, "[error] Failed to copy stream class\n"); | |
112 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
113 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 114 | goto error; |
bc506aa5 | 115 | } |
bc506aa5 | 116 | |
a619fcb7 | 117 | g_hash_table_insert(fs_writer->stream_class_map, |
bc506aa5 JD |
118 | (gpointer) stream_class, writer_stream_class); |
119 | ||
9ae49d3d JD |
120 | goto end; |
121 | ||
122 | error: | |
123 | BT_PUT(writer_stream_class); | |
124 | end: | |
b2f1f465 | 125 | bt_put(writer_trace); |
b2f1f465 | 126 | bt_put(trace); |
bc506aa5 JD |
127 | return writer_stream_class; |
128 | } | |
129 | ||
130 | static | |
f3168545 | 131 | struct fs_writer *insert_new_writer( |
bc506aa5 JD |
132 | struct writer_component *writer_component, |
133 | struct bt_ctf_trace *trace) | |
134 | { | |
9ae49d3d JD |
135 | struct bt_ctf_writer *ctf_writer = NULL; |
136 | struct bt_ctf_trace *writer_trace = NULL; | |
bc506aa5 JD |
137 | char trace_name[PATH_MAX]; |
138 | enum bt_component_status ret; | |
cdcf612f JD |
139 | struct bt_ctf_stream *stream = NULL; |
140 | struct fs_writer *fs_writer = NULL; | |
141 | int nr_stream, i; | |
bc506aa5 | 142 | |
9ae49d3d | 143 | /* FIXME: replace with trace name when it will work. */ |
bc506aa5 | 144 | snprintf(trace_name, PATH_MAX, "%s/%s_%03d", |
9057f037 JD |
145 | writer_component->base_path->str, |
146 | writer_component->trace_name_base->str, | |
bc506aa5 JD |
147 | writer_component->trace_id++); |
148 | printf_verbose("CTF-Writer creating trace in %s\n", trace_name); | |
9057f037 | 149 | |
bc506aa5 JD |
150 | ctf_writer = bt_ctf_writer_create(trace_name); |
151 | if (!ctf_writer) { | |
152 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
153 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 154 | goto error; |
bc506aa5 JD |
155 | } |
156 | ||
91b73004 JD |
157 | writer_trace = bt_ctf_writer_get_trace(ctf_writer); |
158 | if (!writer_trace) { | |
91b73004 JD |
159 | fprintf(writer_component->err, |
160 | "[error] %s in %s:%d\n", __func__, __FILE__, | |
161 | __LINE__); | |
9ae49d3d | 162 | goto error; |
91b73004 JD |
163 | } |
164 | ||
165 | ret = ctf_copy_trace(writer_component->err, trace, writer_trace); | |
bc506aa5 JD |
166 | if (ret != BT_COMPONENT_STATUS_OK) { |
167 | fprintf(writer_component->err, "[error] Failed to copy trace\n"); | |
168 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
169 | __func__, __FILE__, __LINE__); | |
170 | BT_PUT(ctf_writer); | |
f3168545 | 171 | goto error; |
bc506aa5 | 172 | } |
f3168545 JD |
173 | |
174 | fs_writer = g_new0(struct fs_writer, 1); | |
175 | if (!fs_writer) { | |
176 | fprintf(writer_component->err, | |
177 | "[error] %s in %s:%d\n", __func__, __FILE__, | |
178 | __LINE__); | |
179 | goto error; | |
180 | } | |
181 | fs_writer->writer = ctf_writer; | |
cdcf612f JD |
182 | fs_writer->trace = trace; |
183 | fs_writer->writer_trace = writer_trace; | |
184 | BT_PUT(writer_trace); | |
a619fcb7 JD |
185 | fs_writer->stream_class_map = g_hash_table_new_full(g_direct_hash, |
186 | g_direct_equal, NULL, (GDestroyNotify) unref_stream_class); | |
187 | fs_writer->stream_map = g_hash_table_new_full(g_direct_hash, | |
188 | g_direct_equal, NULL, (GDestroyNotify) unref_stream); | |
cdcf612f JD |
189 | fs_writer->stream_states = g_hash_table_new_full(g_direct_hash, |
190 | g_direct_equal, NULL, destroy_stream_state_key); | |
191 | ||
192 | /* Set all the existing streams in the unknown state. */ | |
193 | nr_stream = bt_ctf_trace_get_stream_count(trace); | |
194 | for (i = 0; i < nr_stream; i++) { | |
195 | enum fs_writer_stream_state *v; | |
196 | ||
197 | stream = bt_ctf_trace_get_stream_by_index(trace, i); | |
198 | if (!stream) { | |
199 | fprintf(writer_component->err, | |
200 | "[error] %s in %s:%d\n", __func__, | |
201 | __FILE__, __LINE__); | |
202 | goto error; | |
203 | } | |
204 | ||
205 | v = g_new0(enum fs_writer_stream_state, 1); | |
206 | if (!v) { | |
207 | fprintf(writer_component->err, | |
208 | "[error] %s in %s:%d\n", __func__, | |
209 | __FILE__, __LINE__); | |
210 | goto error; | |
211 | } | |
212 | *v = FS_WRITER_UNKNOWN_STREAM; | |
213 | ||
214 | g_hash_table_insert(fs_writer->stream_states, stream, v); | |
215 | BT_PUT(stream); | |
216 | } | |
217 | ||
218 | /* Check if the trace is already static or register a listener. */ | |
f3168545 JD |
219 | if (bt_ctf_trace_is_static(trace)) { |
220 | fs_writer->trace_static = 1; | |
221 | fs_writer->static_listener_id = -1; | |
222 | } else { | |
223 | ret = bt_ctf_trace_add_is_static_listener(trace, | |
224 | trace_is_static_listener, &fs_writer->trace_static); | |
225 | if (ret < 0) { | |
226 | fprintf(writer_component->err, | |
227 | "[error] %s in %s:%d\n", __func__, __FILE__, | |
228 | __LINE__); | |
f3168545 JD |
229 | goto error; |
230 | } | |
231 | fs_writer->static_listener_id = ret; | |
232 | } | |
bc506aa5 JD |
233 | |
234 | g_hash_table_insert(writer_component->trace_map, (gpointer) trace, | |
f3168545 | 235 | fs_writer); |
bc506aa5 | 236 | |
9ae49d3d JD |
237 | goto end; |
238 | ||
239 | error: | |
cdcf612f JD |
240 | g_free(fs_writer); |
241 | fs_writer = NULL; | |
9ae49d3d | 242 | bt_put(writer_trace); |
cdcf612f | 243 | bt_put(stream); |
9ae49d3d | 244 | BT_PUT(ctf_writer); |
bc506aa5 | 245 | end: |
f3168545 | 246 | return fs_writer; |
bc506aa5 JD |
247 | } |
248 | ||
249 | static | |
f3168545 | 250 | struct fs_writer *get_fs_writer(struct writer_component *writer_component, |
bc506aa5 JD |
251 | struct bt_ctf_stream_class *stream_class) |
252 | { | |
9ae49d3d | 253 | struct bt_ctf_trace *trace = NULL; |
f3168545 | 254 | struct fs_writer *fs_writer; |
bc506aa5 JD |
255 | |
256 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
257 | if (!trace) { | |
bc506aa5 JD |
258 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
259 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 260 | goto error; |
bc506aa5 JD |
261 | } |
262 | ||
f3168545 | 263 | fs_writer = g_hash_table_lookup(writer_component->trace_map, |
bc506aa5 | 264 | (gpointer) trace); |
f3168545 JD |
265 | if (!fs_writer) { |
266 | fs_writer = insert_new_writer(writer_component, trace); | |
bc506aa5 | 267 | } |
9ae49d3d JD |
268 | BT_PUT(trace); |
269 | goto end; | |
bc506aa5 | 270 | |
9ae49d3d | 271 | error: |
f3168545 | 272 | fs_writer = NULL; |
bc506aa5 | 273 | end: |
f3168545 | 274 | return fs_writer; |
bc506aa5 JD |
275 | } |
276 | ||
a619fcb7 JD |
277 | static |
278 | struct fs_writer *get_fs_writer_from_stream( | |
279 | struct writer_component *writer_component, | |
280 | struct bt_ctf_stream *stream) | |
281 | { | |
282 | struct bt_ctf_stream_class *stream_class = NULL; | |
283 | struct fs_writer *fs_writer; | |
284 | ||
285 | stream_class = bt_ctf_stream_get_class(stream); | |
286 | if (!stream_class) { | |
287 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
288 | __func__, __FILE__, __LINE__); | |
289 | goto error; | |
290 | } | |
291 | ||
292 | fs_writer = get_fs_writer(writer_component, stream_class); | |
293 | goto end; | |
294 | ||
295 | error: | |
296 | fs_writer = NULL; | |
297 | ||
298 | end: | |
299 | bt_put(stream_class); | |
300 | return fs_writer; | |
301 | } | |
302 | ||
303 | static | |
304 | struct bt_ctf_stream_class *lookup_stream_class( | |
305 | struct writer_component *writer_component, | |
306 | struct bt_ctf_stream_class *stream_class) | |
307 | { | |
308 | struct fs_writer *fs_writer = get_fs_writer( | |
309 | writer_component, stream_class); | |
310 | assert(fs_writer); | |
311 | return (struct bt_ctf_stream_class *) g_hash_table_lookup( | |
312 | fs_writer->stream_class_map, (gpointer) stream_class); | |
313 | } | |
314 | ||
315 | static | |
316 | struct bt_ctf_stream *lookup_stream(struct writer_component *writer_component, | |
317 | struct bt_ctf_stream *stream) | |
318 | { | |
319 | struct fs_writer *fs_writer = get_fs_writer_from_stream( | |
320 | writer_component, stream); | |
321 | assert(fs_writer); | |
322 | return (struct bt_ctf_stream *) g_hash_table_lookup( | |
323 | fs_writer->stream_map, (gpointer) stream); | |
324 | } | |
325 | ||
326 | static | |
327 | struct bt_ctf_stream *insert_new_stream( | |
328 | struct writer_component *writer_component, | |
329 | struct fs_writer *fs_writer, | |
330 | struct bt_ctf_stream_class *stream_class, | |
331 | struct bt_ctf_stream *stream) | |
332 | { | |
333 | struct bt_ctf_stream *writer_stream = NULL; | |
334 | struct bt_ctf_stream_class *writer_stream_class = NULL; | |
335 | struct bt_ctf_writer *ctf_writer = bt_get(fs_writer->writer); | |
336 | ||
337 | writer_stream_class = lookup_stream_class(writer_component, | |
338 | stream_class); | |
339 | if (!writer_stream_class) { | |
340 | writer_stream_class = insert_new_stream_class( | |
341 | writer_component, fs_writer, stream_class); | |
342 | if (!writer_stream_class) { | |
343 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
344 | __func__, __FILE__, __LINE__); | |
345 | goto error; | |
346 | } | |
347 | } | |
348 | bt_get(writer_stream_class); | |
349 | ||
350 | writer_stream = bt_ctf_writer_create_stream(ctf_writer, | |
351 | writer_stream_class); | |
352 | if (!writer_stream) { | |
353 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
354 | __func__, __FILE__, __LINE__); | |
355 | goto error; | |
356 | } | |
357 | ||
358 | g_hash_table_insert(fs_writer->stream_map, (gpointer) stream, | |
359 | writer_stream); | |
360 | ||
361 | goto end; | |
362 | ||
363 | error: | |
364 | BT_PUT(writer_stream); | |
365 | end: | |
366 | bt_put(ctf_writer); | |
367 | bt_put(writer_stream_class); | |
368 | return writer_stream; | |
369 | } | |
370 | ||
371 | static | |
372 | struct bt_ctf_event_class *get_event_class(struct writer_component *writer_component, | |
373 | struct bt_ctf_stream_class *writer_stream_class, | |
374 | struct bt_ctf_event_class *event_class) | |
375 | { | |
376 | return bt_ctf_stream_class_get_event_class_by_id(writer_stream_class, | |
377 | bt_ctf_event_class_get_id(event_class)); | |
378 | } | |
379 | ||
bc506aa5 JD |
380 | static |
381 | struct bt_ctf_stream *get_writer_stream( | |
382 | struct writer_component *writer_component, | |
383 | struct bt_ctf_packet *packet, struct bt_ctf_stream *stream) | |
384 | { | |
9ae49d3d | 385 | struct bt_ctf_stream *writer_stream = NULL; |
bc506aa5 | 386 | |
f384901f JD |
387 | writer_stream = lookup_stream(writer_component, stream); |
388 | if (!writer_stream) { | |
bc506aa5 JD |
389 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
390 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 391 | goto error; |
bc506aa5 | 392 | } |
9ae49d3d JD |
393 | bt_get(writer_stream); |
394 | ||
395 | goto end; | |
bc506aa5 | 396 | |
9ae49d3d JD |
397 | error: |
398 | BT_PUT(writer_stream); | |
399 | end: | |
bc506aa5 JD |
400 | return writer_stream; |
401 | } | |
402 | ||
f3168545 | 403 | BT_HIDDEN |
a619fcb7 JD |
404 | void writer_close(struct writer_component *writer_component, |
405 | struct fs_writer *fs_writer) | |
f3168545 | 406 | { |
f3168545 | 407 | if (fs_writer->static_listener_id > 0) { |
a619fcb7 | 408 | bt_ctf_trace_remove_is_static_listener(fs_writer->trace, |
f3168545 JD |
409 | fs_writer->static_listener_id); |
410 | } | |
a619fcb7 JD |
411 | |
412 | /* Empty the stream class HT. */ | |
413 | g_hash_table_foreach_remove(fs_writer->stream_class_map, | |
414 | empty_ht, NULL); | |
415 | g_hash_table_destroy(fs_writer->stream_class_map); | |
416 | ||
417 | /* Empty the stream HT. */ | |
418 | g_hash_table_foreach_remove(fs_writer->stream_map, | |
419 | empty_ht, NULL); | |
420 | g_hash_table_destroy(fs_writer->stream_map); | |
cdcf612f JD |
421 | |
422 | /* Empty the stream state HT. */ | |
423 | g_hash_table_foreach_remove(fs_writer->stream_states, | |
424 | empty_ht, NULL); | |
425 | g_hash_table_destroy(fs_writer->stream_states); | |
f3168545 JD |
426 | } |
427 | ||
f384901f JD |
428 | BT_HIDDEN |
429 | enum bt_component_status writer_stream_begin( | |
430 | struct writer_component *writer_component, | |
431 | struct bt_ctf_stream *stream) | |
432 | { | |
433 | struct bt_ctf_stream_class *stream_class = NULL; | |
434 | struct fs_writer *fs_writer; | |
f384901f JD |
435 | struct bt_ctf_stream *writer_stream = NULL; |
436 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
cdcf612f | 437 | enum fs_writer_stream_state *state; |
f384901f JD |
438 | |
439 | stream_class = bt_ctf_stream_get_class(stream); | |
440 | if (!stream_class) { | |
441 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
442 | __func__, __FILE__, __LINE__); | |
443 | goto error; | |
444 | } | |
445 | ||
446 | fs_writer = get_fs_writer(writer_component, stream_class); | |
447 | if (!fs_writer) { | |
448 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
449 | __func__, __FILE__, __LINE__); | |
450 | goto error; | |
451 | } | |
cdcf612f JD |
452 | |
453 | /* Set the stream as active */ | |
454 | state = g_hash_table_lookup(fs_writer->stream_states, stream); | |
455 | if (*state != FS_WRITER_UNKNOWN_STREAM) { | |
456 | fprintf(writer_component->err, "[error] Unexpected stream " | |
457 | "state %d\n", *state); | |
458 | goto error; | |
459 | } | |
460 | *state = FS_WRITER_ACTIVE_STREAM; | |
461 | ||
a619fcb7 | 462 | writer_stream = insert_new_stream(writer_component, fs_writer, |
f384901f JD |
463 | stream_class, stream); |
464 | if (!writer_stream) { | |
465 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
466 | __func__, __FILE__, __LINE__); | |
467 | goto error; | |
468 | } | |
469 | fs_writer->active_streams++; | |
470 | ||
471 | goto end; | |
472 | ||
473 | error: | |
474 | ret = BT_COMPONENT_STATUS_ERROR; | |
475 | end: | |
f384901f JD |
476 | bt_put(stream_class); |
477 | return ret; | |
478 | } | |
479 | ||
cdcf612f JD |
480 | void check_completed_trace(gpointer key, gpointer value, gpointer user_data) |
481 | { | |
482 | enum fs_writer_stream_state *state = value; | |
483 | int *trace_completed = user_data; | |
484 | ||
485 | if (*state != FS_WRITER_COMPLETED_STREAM) { | |
486 | *trace_completed = 0; | |
487 | } | |
488 | } | |
489 | ||
f3168545 JD |
490 | BT_HIDDEN |
491 | enum bt_component_status writer_stream_end( | |
492 | struct writer_component *writer_component, | |
493 | struct bt_ctf_stream *stream) | |
494 | { | |
495 | struct bt_ctf_stream_class *stream_class = NULL; | |
496 | struct fs_writer *fs_writer; | |
497 | struct bt_ctf_trace *trace = NULL; | |
498 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
cdcf612f | 499 | enum fs_writer_stream_state *state; |
f3168545 | 500 | |
f3168545 JD |
501 | stream_class = bt_ctf_stream_get_class(stream); |
502 | if (!stream_class) { | |
503 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
504 | __func__, __FILE__, __LINE__); | |
505 | goto error; | |
506 | } | |
507 | ||
508 | fs_writer = get_fs_writer(writer_component, stream_class); | |
509 | if (!fs_writer) { | |
510 | fprintf(writer_component->err, "[error] %s in %s:%d\n", | |
511 | __func__, __FILE__, __LINE__); | |
512 | goto error; | |
513 | } | |
cdcf612f JD |
514 | |
515 | state = g_hash_table_lookup(fs_writer->stream_states, stream); | |
516 | if (*state != FS_WRITER_ACTIVE_STREAM) { | |
517 | fprintf(writer_component->err, "[error] Unexpected stream " | |
518 | "state %d\n", *state); | |
519 | goto error; | |
520 | } | |
521 | *state = FS_WRITER_COMPLETED_STREAM; | |
522 | ||
a619fcb7 | 523 | g_hash_table_remove(fs_writer->stream_map, stream); |
f3168545 | 524 | |
cdcf612f JD |
525 | if (fs_writer->trace_static) { |
526 | int trace_completed = 1; | |
527 | ||
528 | g_hash_table_foreach(fs_writer->stream_states, | |
529 | check_completed_trace, &trace_completed); | |
530 | if (trace_completed) { | |
531 | writer_close(writer_component, fs_writer); | |
532 | g_hash_table_remove(writer_component->trace_map, | |
533 | fs_writer->trace); | |
534 | } | |
f3168545 JD |
535 | } |
536 | ||
537 | goto end; | |
538 | ||
539 | error: | |
540 | ret = BT_COMPONENT_STATUS_ERROR; | |
541 | end: | |
542 | BT_PUT(trace); | |
543 | BT_PUT(stream_class); | |
544 | return ret; | |
545 | } | |
546 | ||
bc506aa5 JD |
547 | BT_HIDDEN |
548 | enum bt_component_status writer_new_packet( | |
549 | struct writer_component *writer_component, | |
550 | struct bt_ctf_packet *packet) | |
551 | { | |
9ae49d3d JD |
552 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
553 | struct bt_ctf_field *writer_packet_context = NULL; | |
bc506aa5 | 554 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
b2f1f465 | 555 | int int_ret; |
bc506aa5 JD |
556 | |
557 | stream = bt_ctf_packet_get_stream(packet); | |
558 | if (!stream) { | |
bc506aa5 JD |
559 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
560 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 561 | goto error; |
bc506aa5 JD |
562 | } |
563 | ||
bc506aa5 JD |
564 | writer_stream = get_writer_stream(writer_component, packet, stream); |
565 | if (!writer_stream) { | |
bc506aa5 JD |
566 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
567 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 568 | goto error; |
bc506aa5 | 569 | } |
9ae49d3d | 570 | BT_PUT(stream); |
bc506aa5 | 571 | |
b2f1f465 | 572 | writer_packet_context = ctf_copy_packet_context(writer_component->err, |
ab80adac | 573 | packet, writer_stream, 1); |
b2f1f465 | 574 | if (!writer_packet_context) { |
91b73004 JD |
575 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
576 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 577 | goto error; |
bc506aa5 JD |
578 | } |
579 | ||
b2f1f465 JD |
580 | int_ret = bt_ctf_stream_set_packet_context(writer_stream, |
581 | writer_packet_context); | |
582 | if (int_ret < 0) { | |
b2f1f465 JD |
583 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
584 | __FILE__, __LINE__); | |
9ae49d3d | 585 | goto error; |
b2f1f465 | 586 | } |
9ae49d3d JD |
587 | BT_PUT(writer_stream); |
588 | BT_PUT(writer_packet_context); | |
b2f1f465 | 589 | |
9ae49d3d | 590 | goto end; |
bc506aa5 | 591 | |
9ae49d3d JD |
592 | error: |
593 | ret = BT_COMPONENT_STATUS_ERROR; | |
594 | end: | |
595 | bt_put(writer_stream); | |
b2f1f465 | 596 | bt_put(writer_packet_context); |
91b73004 | 597 | bt_put(stream); |
bc506aa5 JD |
598 | return ret; |
599 | } | |
600 | ||
601 | BT_HIDDEN | |
602 | enum bt_component_status writer_close_packet( | |
603 | struct writer_component *writer_component, | |
604 | struct bt_ctf_packet *packet) | |
605 | { | |
9ae49d3d | 606 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
bc506aa5 JD |
607 | enum bt_component_status ret; |
608 | ||
609 | stream = bt_ctf_packet_get_stream(packet); | |
610 | if (!stream) { | |
bc506aa5 JD |
611 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
612 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 613 | goto error; |
bc506aa5 JD |
614 | } |
615 | ||
616 | writer_stream = lookup_stream(writer_component, stream); | |
617 | if (!writer_stream) { | |
bc506aa5 JD |
618 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
619 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 620 | goto error; |
bc506aa5 | 621 | } |
9ae49d3d | 622 | BT_PUT(stream); |
bc506aa5 | 623 | |
9ae49d3d | 624 | bt_get(writer_stream); |
bc506aa5 | 625 | |
bc506aa5 JD |
626 | ret = bt_ctf_stream_flush(writer_stream); |
627 | if (ret < 0) { | |
628 | fprintf(writer_component->err, | |
629 | "[error] Failed to flush packet\n"); | |
9ae49d3d | 630 | goto error; |
bc506aa5 | 631 | } |
9ae49d3d | 632 | BT_PUT(writer_stream); |
bc506aa5 JD |
633 | |
634 | ret = BT_COMPONENT_STATUS_OK; | |
9ae49d3d | 635 | goto end; |
bc506aa5 | 636 | |
9ae49d3d JD |
637 | error: |
638 | ret = BT_COMPONENT_STATUS_ERROR; | |
639 | end: | |
bc506aa5 | 640 | bt_put(writer_stream); |
bc506aa5 | 641 | bt_put(stream); |
bc506aa5 JD |
642 | return ret; |
643 | } | |
644 | ||
bc506aa5 JD |
645 | BT_HIDDEN |
646 | enum bt_component_status writer_output_event( | |
647 | struct writer_component *writer_component, | |
648 | struct bt_ctf_event *event) | |
649 | { | |
650 | enum bt_component_status ret; | |
9ae49d3d JD |
651 | struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL; |
652 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; | |
653 | struct bt_ctf_stream_class *stream_class = NULL, *writer_stream_class = NULL; | |
654 | struct bt_ctf_event *writer_event = NULL; | |
bc506aa5 JD |
655 | const char *event_name; |
656 | int int_ret; | |
657 | ||
658 | event_class = bt_ctf_event_get_class(event); | |
659 | if (!event_class) { | |
bc506aa5 JD |
660 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
661 | __FILE__, __LINE__); | |
9ae49d3d | 662 | goto error; |
bc506aa5 JD |
663 | } |
664 | ||
665 | event_name = bt_ctf_event_class_get_name(event_class); | |
666 | if (!event_name) { | |
bc506aa5 JD |
667 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
668 | __FILE__, __LINE__); | |
9ae49d3d | 669 | goto error; |
bc506aa5 | 670 | } |
bc506aa5 JD |
671 | |
672 | stream = bt_ctf_event_get_stream(event); | |
673 | if (!stream) { | |
bc506aa5 JD |
674 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
675 | __FILE__, __LINE__); | |
9ae49d3d | 676 | goto error; |
bc506aa5 JD |
677 | } |
678 | ||
679 | writer_stream = lookup_stream(writer_component, stream); | |
680 | if (!writer_stream || !bt_get(writer_stream)) { | |
bc506aa5 JD |
681 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
682 | __FILE__, __LINE__); | |
9ae49d3d | 683 | goto error; |
bc506aa5 JD |
684 | } |
685 | ||
686 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
687 | if (!stream_class) { | |
bc506aa5 JD |
688 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
689 | __FILE__, __LINE__); | |
9ae49d3d | 690 | goto error; |
bc506aa5 JD |
691 | } |
692 | ||
a619fcb7 | 693 | writer_stream_class = lookup_stream_class(writer_component, stream_class); |
bc506aa5 | 694 | if (!writer_stream_class || !bt_get(writer_stream_class)) { |
bc506aa5 JD |
695 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
696 | __FILE__, __LINE__); | |
9ae49d3d | 697 | goto error; |
bc506aa5 JD |
698 | } |
699 | ||
700 | writer_event_class = get_event_class(writer_component, | |
701 | writer_stream_class, event_class); | |
702 | if (!writer_event_class) { | |
af34e875 JD |
703 | writer_event_class = ctf_copy_event_class(writer_component->err, |
704 | event_class); | |
705 | if (!writer_event_class) { | |
af34e875 JD |
706 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
707 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 708 | goto error; |
af34e875 JD |
709 | } |
710 | int_ret = bt_ctf_stream_class_add_event_class( | |
711 | writer_stream_class, writer_event_class); | |
712 | if (int_ret) { | |
af34e875 JD |
713 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
714 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 715 | goto error; |
af34e875 | 716 | } |
bc506aa5 JD |
717 | } |
718 | ||
b2f1f465 JD |
719 | writer_event = ctf_copy_event(writer_component->err, event, |
720 | writer_event_class, true); | |
bc506aa5 | 721 | if (!writer_event) { |
bc506aa5 JD |
722 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
723 | __FILE__, __LINE__); | |
724 | fprintf(writer_component->err, "[error] Failed to copy event %s\n", | |
725 | bt_ctf_event_class_get_name(writer_event_class)); | |
9ae49d3d | 726 | goto error; |
bc506aa5 JD |
727 | } |
728 | ||
729 | int_ret = bt_ctf_stream_append_event(writer_stream, writer_event); | |
730 | if (int_ret < 0) { | |
bc506aa5 JD |
731 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
732 | __FILE__, __LINE__); | |
733 | fprintf(writer_component->err, "[error] Failed to append event %s\n", | |
734 | bt_ctf_event_class_get_name(writer_event_class)); | |
9ae49d3d | 735 | goto error; |
bc506aa5 JD |
736 | } |
737 | ||
738 | ret = BT_COMPONENT_STATUS_OK; | |
9ae49d3d | 739 | goto end; |
bc506aa5 | 740 | |
9ae49d3d JD |
741 | error: |
742 | ret = BT_COMPONENT_STATUS_ERROR; | |
743 | end: | |
bc506aa5 | 744 | bt_put(writer_event); |
bc506aa5 | 745 | bt_put(writer_event_class); |
bc506aa5 | 746 | bt_put(writer_stream_class); |
bc506aa5 | 747 | bt_put(stream_class); |
bc506aa5 | 748 | bt_put(writer_stream); |
bc506aa5 | 749 | bt_put(stream); |
bc506aa5 | 750 | bt_put(event_class); |
bc506aa5 JD |
751 | return ret; |
752 | } |