Commit | Line | Data |
---|---|---|
15fe47e0 PP |
1 | /* |
2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
aa1a7452 | 23 | #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp) |
ffa3b2b3 | 24 | #define BT_LOG_OUTPUT_LEVEL (stream->log_level) |
350ad6c1 | 25 | #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM" |
d9c39b0a | 26 | #include "logging/comp-logging.h" |
15fe47e0 | 27 | |
3fadfbc0 | 28 | #include <babeltrace2/babeltrace.h> |
15fe47e0 PP |
29 | #include <stdio.h> |
30 | #include <stdbool.h> | |
31 | #include <glib.h> | |
578e048b MJ |
32 | #include "common/assert.h" |
33 | #include "ctfser/ctfser.h" | |
34 | #include "compat/endian.h" | |
15fe47e0 | 35 | |
aa1a7452 | 36 | #include "fs-sink.h" |
15fe47e0 PP |
37 | #include "fs-sink-trace.h" |
38 | #include "fs-sink-stream.h" | |
39 | #include "translate-trace-ir-to-ctf-ir.h" | |
40 | ||
41 | BT_HIDDEN | |
42 | void fs_sink_stream_destroy(struct fs_sink_stream *stream) | |
43 | { | |
44 | if (!stream) { | |
45 | goto end; | |
46 | } | |
47 | ||
48 | bt_ctfser_fini(&stream->ctfser); | |
49 | ||
50 | if (stream->file_name) { | |
51 | g_string_free(stream->file_name, TRUE); | |
52 | stream->file_name = NULL; | |
53 | } | |
54 | ||
55 | bt_packet_put_ref(stream->packet_state.packet); | |
56 | g_free(stream); | |
57 | ||
58 | end: | |
59 | return; | |
60 | } | |
61 | ||
62 | static | |
63 | bool stream_file_name_exists(struct fs_sink_trace *trace, const char *name) | |
64 | { | |
65 | bool exists = false; | |
66 | GHashTableIter iter; | |
67 | gpointer key, value; | |
68 | ||
69 | g_hash_table_iter_init(&iter, trace->streams); | |
70 | ||
71 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
72 | struct fs_sink_stream *stream = value; | |
73 | ||
74 | if (strcmp(name, stream->file_name->str) == 0) { | |
75 | exists = true; | |
76 | goto end; | |
77 | } | |
78 | } | |
79 | ||
80 | end: | |
81 | return exists; | |
82 | } | |
83 | ||
84 | static | |
85 | GString *sanitize_stream_file_name(const char *file_name) | |
86 | { | |
87 | GString *san_file_name = g_string_new(NULL); | |
88 | const char *ch; | |
89 | gchar *basename; | |
90 | ||
91 | BT_ASSERT(san_file_name); | |
92 | BT_ASSERT(file_name); | |
93 | basename = g_path_get_basename(file_name); | |
94 | ||
95 | for (ch = basename; *ch != '\0'; ch++) { | |
96 | if (*ch == '/') { | |
97 | g_string_append_c(san_file_name, '_'); | |
98 | } else { | |
99 | g_string_append_c(san_file_name, *ch); | |
100 | } | |
101 | } | |
102 | ||
103 | /* Do not allow `.` and `..` either */ | |
104 | if (strcmp(san_file_name->str, ".") == 0 || | |
105 | strcmp(san_file_name->str, "..") == 0) { | |
106 | g_string_assign(san_file_name, "stream"); | |
107 | } | |
108 | ||
109 | g_free(basename); | |
110 | return san_file_name; | |
111 | } | |
112 | ||
113 | static | |
114 | GString *make_unique_stream_file_name(struct fs_sink_trace *trace, | |
115 | const char *base) | |
116 | { | |
117 | GString *san_base = sanitize_stream_file_name(base); | |
118 | GString *name = g_string_new(san_base->str); | |
119 | unsigned int suffix = 0; | |
120 | ||
121 | BT_ASSERT(name); | |
122 | ||
123 | while (stream_file_name_exists(trace, name->str) && | |
124 | strcmp(name->str, "metadata") == 0) { | |
b1acab38 | 125 | g_string_printf(name, "%s-%u", san_base->str, suffix); |
15fe47e0 PP |
126 | suffix++; |
127 | } | |
128 | ||
129 | g_string_free(san_base, TRUE); | |
130 | return name; | |
131 | } | |
132 | ||
133 | static | |
134 | void set_stream_file_name(struct fs_sink_stream *stream) | |
135 | { | |
136 | const char *base_name = bt_stream_get_name(stream->ir_stream); | |
137 | ||
138 | if (!base_name) { | |
139 | base_name = "stream"; | |
140 | } | |
141 | ||
142 | BT_ASSERT(!stream->file_name); | |
143 | stream->file_name = make_unique_stream_file_name(stream->trace, | |
144 | base_name); | |
145 | } | |
146 | ||
147 | BT_HIDDEN | |
148 | struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace, | |
149 | const bt_stream *ir_stream) | |
150 | { | |
151 | struct fs_sink_stream *stream = g_new0(struct fs_sink_stream, 1); | |
152 | int ret; | |
153 | GString *path = g_string_new(trace->path->str); | |
154 | ||
155 | if (!stream) { | |
156 | goto end; | |
157 | } | |
158 | ||
ffa3b2b3 | 159 | stream->log_level = trace->log_level; |
15fe47e0 PP |
160 | stream->trace = trace; |
161 | stream->ir_stream = ir_stream; | |
162 | stream->packet_state.beginning_cs = UINT64_C(-1); | |
163 | stream->packet_state.end_cs = UINT64_C(-1); | |
164 | stream->prev_packet_state.end_cs = UINT64_C(-1); | |
165 | stream->prev_packet_state.discarded_events_counter = UINT64_C(-1); | |
166 | stream->prev_packet_state.seq_num = UINT64_C(-1); | |
aa1a7452 | 167 | ret = try_translate_stream_class_trace_ir_to_ctf_ir(trace->fs_sink, |
335a2da5 | 168 | trace->trace, bt_stream_borrow_class_const(ir_stream), |
aa1a7452 | 169 | &stream->sc); |
15fe47e0 PP |
170 | if (ret) { |
171 | goto error; | |
172 | } | |
173 | ||
174 | set_stream_file_name(stream); | |
175 | g_string_append_printf(path, "/%s", stream->file_name->str); | |
ffa3b2b3 PP |
176 | ret = bt_ctfser_init(&stream->ctfser, path->str, |
177 | stream->log_level); | |
15fe47e0 PP |
178 | if (ret) { |
179 | goto error; | |
180 | } | |
181 | ||
182 | g_hash_table_insert(trace->streams, (gpointer) ir_stream, stream); | |
183 | goto end; | |
184 | ||
185 | error: | |
186 | fs_sink_stream_destroy(stream); | |
187 | stream = NULL; | |
188 | ||
189 | end: | |
190 | if (path) { | |
191 | g_string_free(path, TRUE); | |
192 | } | |
193 | ||
194 | return stream; | |
195 | } | |
196 | ||
197 | static | |
198 | int write_field(struct fs_sink_stream *stream, | |
199 | struct fs_sink_ctf_field_class *fc, const bt_field *field); | |
200 | ||
ecd27ae4 PP |
201 | static inline |
202 | int write_bool_field(struct fs_sink_stream *stream, | |
203 | struct fs_sink_ctf_field_class_int *fc, const bt_field *field) | |
204 | { | |
205 | /* | |
206 | * CTF 1.8 has no boolean field class type, so this component | |
207 | * translates this boolean field to an 8-bit unsigned integer | |
208 | * field which has the value 0 (false) or 1 (true). | |
209 | */ | |
210 | return bt_ctfser_write_unsigned_int(&stream->ctfser, | |
211 | bt_field_bool_get_value(field) ? 1 : 0, | |
212 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); | |
213 | } | |
214 | ||
15fe47e0 PP |
215 | static inline |
216 | int write_int_field(struct fs_sink_stream *stream, | |
217 | struct fs_sink_ctf_field_class_int *fc, const bt_field *field) | |
218 | { | |
219 | int ret; | |
220 | ||
221 | if (fc->is_signed) { | |
222 | ret = bt_ctfser_write_signed_int(&stream->ctfser, | |
9c08c816 | 223 | bt_field_integer_signed_get_value(field), |
15fe47e0 PP |
224 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
225 | } else { | |
226 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
9c08c816 | 227 | bt_field_integer_unsigned_get_value(field), |
15fe47e0 PP |
228 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
229 | } | |
230 | ||
231 | return ret; | |
232 | } | |
233 | ||
234 | static inline | |
235 | int write_float_field(struct fs_sink_stream *stream, | |
236 | struct fs_sink_ctf_field_class_float *fc, const bt_field *field) | |
237 | { | |
238 | int ret; | |
239 | double val = bt_field_real_get_value(field); | |
240 | ||
241 | if (fc->base.size == 32) { | |
242 | ret = bt_ctfser_write_float32(&stream->ctfser, val, | |
243 | fc->base.base.alignment, BYTE_ORDER); | |
244 | } else { | |
263e3582 | 245 | ret = bt_ctfser_write_float64(&stream->ctfser, val, |
15fe47e0 PP |
246 | fc->base.base.alignment, BYTE_ORDER); |
247 | } | |
248 | ||
249 | return ret; | |
250 | } | |
251 | ||
252 | static inline | |
253 | int write_string_field(struct fs_sink_stream *stream, | |
254 | struct fs_sink_ctf_field_class_string *fc, const bt_field *field) | |
255 | { | |
256 | return bt_ctfser_write_string(&stream->ctfser, | |
257 | bt_field_string_get_value(field)); | |
258 | } | |
259 | ||
260 | static inline | |
261 | int write_array_field_elements(struct fs_sink_stream *stream, | |
262 | struct fs_sink_ctf_field_class_array_base *fc, | |
263 | const bt_field *field) | |
264 | { | |
265 | uint64_t i; | |
266 | uint64_t len = bt_field_array_get_length(field); | |
267 | int ret = 0; | |
268 | ||
269 | for (i = 0; i < len; i++) { | |
270 | const bt_field *elem_field = | |
271 | bt_field_array_borrow_element_field_by_index_const( | |
272 | field, i); | |
273 | ret = write_field(stream, fc->elem_fc, elem_field); | |
91d81473 | 274 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
275 | goto end; |
276 | } | |
277 | } | |
278 | ||
279 | end: | |
280 | return ret; | |
281 | } | |
282 | ||
283 | static inline | |
284 | int write_sequence_field(struct fs_sink_stream *stream, | |
285 | struct fs_sink_ctf_field_class_sequence *fc, | |
286 | const bt_field *field) | |
287 | { | |
288 | int ret; | |
289 | ||
290 | if (fc->length_is_before) { | |
291 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
292 | bt_field_array_get_length(field), 8, 32, BYTE_ORDER); | |
91d81473 | 293 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
294 | goto end; |
295 | } | |
296 | } | |
297 | ||
298 | ret = write_array_field_elements(stream, (void *) fc, field); | |
299 | ||
300 | end: | |
301 | return ret; | |
302 | } | |
303 | ||
304 | static inline | |
305 | int write_struct_field(struct fs_sink_stream *stream, | |
306 | struct fs_sink_ctf_field_class_struct *fc, | |
307 | const bt_field *field, bool align_struct) | |
308 | { | |
309 | int ret = 0; | |
310 | uint64_t i; | |
311 | ||
91d81473 | 312 | if (G_LIKELY(align_struct)) { |
15fe47e0 PP |
313 | ret = bt_ctfser_align_offset_in_current_packet(&stream->ctfser, |
314 | fc->base.alignment); | |
91d81473 | 315 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
316 | goto end; |
317 | } | |
318 | } | |
319 | ||
320 | for (i = 0; i < fc->members->len; i++) { | |
321 | const bt_field *memb_field = | |
322 | bt_field_structure_borrow_member_field_by_index_const( | |
323 | field, i); | |
324 | struct fs_sink_ctf_field_class *member_fc = | |
325 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
326 | fc, i)->fc; | |
327 | ||
328 | ret = write_field(stream, member_fc, memb_field); | |
91d81473 | 329 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
330 | goto end; |
331 | } | |
332 | } | |
333 | ||
334 | end: | |
335 | return ret; | |
336 | } | |
337 | ||
c25f8e53 PP |
338 | static inline |
339 | int write_option_field(struct fs_sink_stream *stream, | |
340 | struct fs_sink_ctf_field_class_option *fc, | |
341 | const bt_field *field) | |
342 | { | |
343 | int ret; | |
344 | const bt_field *content_field = | |
345 | bt_field_option_borrow_field_const(field); | |
346 | ||
347 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
348 | content_field ? 1 : 0, 8, 8, BYTE_ORDER); | |
349 | if (G_UNLIKELY(ret)) { | |
350 | goto end; | |
351 | } | |
352 | ||
353 | /* | |
354 | * CTF 1.8 has no option field class type, so this component | |
355 | * translates the option field class to a variant field class | |
356 | * where the options are: | |
357 | * | |
358 | * * An empty structure field class (field occupies 0 bits). | |
359 | * * The optional field class itself. | |
360 | * | |
361 | * If `content_field` is `NULL`, do not write anything (empty | |
362 | * structure). | |
363 | */ | |
364 | if (content_field) { | |
365 | ret = write_field(stream, fc->content_fc, content_field); | |
366 | } | |
367 | ||
368 | end: | |
369 | return ret; | |
370 | } | |
371 | ||
15fe47e0 PP |
372 | static inline |
373 | int write_variant_field(struct fs_sink_stream *stream, | |
374 | struct fs_sink_ctf_field_class_variant *fc, | |
375 | const bt_field *field) | |
376 | { | |
377 | uint64_t opt_index = | |
378 | bt_field_variant_get_selected_option_field_index(field); | |
379 | int ret; | |
380 | ||
381 | if (fc->tag_is_before) { | |
382 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
383 | opt_index, 8, 16, BYTE_ORDER); | |
91d81473 | 384 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
385 | goto end; |
386 | } | |
387 | } | |
388 | ||
389 | ret = write_field(stream, | |
390 | fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, | |
391 | opt_index)->fc, | |
392 | bt_field_variant_borrow_selected_option_field_const(field)); | |
393 | ||
394 | end: | |
395 | return ret; | |
396 | } | |
397 | ||
398 | static | |
399 | int write_field(struct fs_sink_stream *stream, | |
400 | struct fs_sink_ctf_field_class *fc, const bt_field *field) | |
401 | { | |
402 | int ret; | |
403 | ||
404 | switch (fc->type) { | |
ecd27ae4 PP |
405 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL: |
406 | ret = write_bool_field(stream, (void *) fc, field); | |
407 | break; | |
15fe47e0 PP |
408 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: |
409 | ret = write_int_field(stream, (void *) fc, field); | |
410 | break; | |
411 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: | |
412 | ret = write_float_field(stream, (void *) fc, field); | |
413 | break; | |
414 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: | |
415 | ret = write_string_field(stream, (void *) fc, field); | |
416 | break; | |
417 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
418 | ret = write_struct_field(stream, (void *) fc, field, true); | |
419 | break; | |
420 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
421 | ret = write_array_field_elements(stream, (void *) fc, field); | |
422 | break; | |
423 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
424 | ret = write_sequence_field(stream, (void *) fc, field); | |
425 | break; | |
c25f8e53 PP |
426 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
427 | ret = write_option_field(stream, (void *) fc, field); | |
428 | break; | |
15fe47e0 PP |
429 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
430 | ret = write_variant_field(stream, (void *) fc, field); | |
431 | break; | |
432 | default: | |
433 | abort(); | |
434 | } | |
435 | ||
436 | return ret; | |
437 | } | |
438 | ||
439 | static inline | |
440 | int write_event_header(struct fs_sink_stream *stream, | |
441 | const bt_clock_snapshot *cs, struct fs_sink_ctf_event_class *ec) | |
442 | { | |
443 | int ret; | |
444 | ||
445 | /* Event class ID */ | |
446 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
447 | bt_event_class_get_id(ec->ir_ec), 8, 64, BYTE_ORDER); | |
91d81473 | 448 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
449 | goto end; |
450 | } | |
451 | ||
452 | /* Time */ | |
453 | if (stream->sc->default_clock_class) { | |
454 | BT_ASSERT(cs); | |
455 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
456 | bt_clock_snapshot_get_value(cs), 8, 64, BYTE_ORDER); | |
91d81473 | 457 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
458 | goto end; |
459 | } | |
460 | } | |
461 | ||
462 | end: | |
463 | return ret; | |
464 | } | |
465 | ||
466 | BT_HIDDEN | |
467 | int fs_sink_stream_write_event(struct fs_sink_stream *stream, | |
468 | const bt_clock_snapshot *cs, const bt_event *event, | |
469 | struct fs_sink_ctf_event_class *ec) | |
470 | { | |
471 | int ret; | |
472 | const bt_field *field; | |
473 | ||
474 | /* Header */ | |
475 | ret = write_event_header(stream, cs, ec); | |
91d81473 | 476 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
477 | goto end; |
478 | } | |
479 | ||
480 | /* Common context */ | |
481 | if (stream->sc->event_common_context_fc) { | |
482 | field = bt_event_borrow_common_context_field_const(event); | |
483 | BT_ASSERT(field); | |
484 | ret = write_struct_field(stream, | |
485 | (void *) stream->sc->event_common_context_fc, | |
486 | field, true); | |
91d81473 | 487 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
488 | goto end; |
489 | } | |
490 | } | |
491 | ||
492 | /* Specific context */ | |
493 | if (ec->spec_context_fc) { | |
494 | field = bt_event_borrow_specific_context_field_const(event); | |
495 | BT_ASSERT(field); | |
496 | ret = write_struct_field(stream, (void *) ec->spec_context_fc, | |
497 | field, true); | |
91d81473 | 498 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
499 | goto end; |
500 | } | |
501 | } | |
502 | ||
503 | /* Specific context */ | |
504 | if (ec->payload_fc) { | |
505 | field = bt_event_borrow_payload_field_const(event); | |
506 | BT_ASSERT(field); | |
507 | ret = write_struct_field(stream, (void *) ec->payload_fc, | |
508 | field, true); | |
91d81473 | 509 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
510 | goto end; |
511 | } | |
512 | } | |
513 | ||
514 | end: | |
515 | return ret; | |
516 | } | |
517 | ||
518 | static | |
519 | int write_packet_context(struct fs_sink_stream *stream) | |
520 | { | |
521 | int ret; | |
522 | ||
523 | /* Packet total size */ | |
524 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
525 | stream->packet_state.total_size, 8, 64, BYTE_ORDER); | |
526 | if (ret) { | |
527 | goto end; | |
528 | } | |
529 | ||
530 | /* Packet content size */ | |
531 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
532 | stream->packet_state.content_size, 8, 64, BYTE_ORDER); | |
533 | if (ret) { | |
534 | goto end; | |
535 | } | |
536 | ||
ffb5c13c | 537 | if (stream->sc->packets_have_ts_begin) { |
15fe47e0 PP |
538 | /* Beginning time */ |
539 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
540 | stream->packet_state.beginning_cs, 8, 64, BYTE_ORDER); | |
541 | if (ret) { | |
542 | goto end; | |
543 | } | |
ffb5c13c | 544 | } |
15fe47e0 | 545 | |
ffb5c13c | 546 | if (stream->sc->packets_have_ts_end) { |
15fe47e0 PP |
547 | /* End time */ |
548 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
549 | stream->packet_state.end_cs, 8, 64, BYTE_ORDER); | |
550 | if (ret) { | |
551 | goto end; | |
552 | } | |
553 | } | |
554 | ||
ffb5c13c PP |
555 | if (stream->sc->has_discarded_events) { |
556 | /* Discarded event counter */ | |
557 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
558 | stream->packet_state.discarded_events_counter, 8, 64, | |
559 | BYTE_ORDER); | |
560 | if (ret) { | |
561 | goto end; | |
562 | } | |
15fe47e0 PP |
563 | } |
564 | ||
565 | /* Sequence number */ | |
566 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
567 | stream->packet_state.seq_num, 8, 64, BYTE_ORDER); | |
568 | if (ret) { | |
569 | goto end; | |
570 | } | |
571 | ||
572 | /* Other members */ | |
573 | if (stream->sc->packet_context_fc) { | |
26fc5aed | 574 | const bt_field *packet_context_field; |
15fe47e0 | 575 | |
26fc5aed PP |
576 | BT_ASSERT(stream->packet_state.packet); |
577 | packet_context_field = bt_packet_borrow_context_field_const( | |
578 | stream->packet_state.packet); | |
15fe47e0 PP |
579 | BT_ASSERT(packet_context_field); |
580 | ret = write_struct_field(stream, | |
581 | (void *) stream->sc->packet_context_fc, | |
582 | packet_context_field, false); | |
583 | if (ret) { | |
584 | goto end; | |
585 | } | |
586 | } | |
587 | ||
588 | end: | |
589 | return ret; | |
590 | } | |
591 | ||
592 | BT_HIDDEN | |
593 | int fs_sink_stream_open_packet(struct fs_sink_stream *stream, | |
594 | const bt_clock_snapshot *cs, const bt_packet *packet) | |
595 | { | |
596 | int ret; | |
597 | uint64_t i; | |
598 | ||
599 | BT_ASSERT(!stream->packet_state.is_open); | |
600 | bt_packet_put_ref(stream->packet_state.packet); | |
601 | stream->packet_state.packet = packet; | |
602 | bt_packet_get_ref(stream->packet_state.packet); | |
603 | if (cs) { | |
604 | stream->packet_state.beginning_cs = | |
605 | bt_clock_snapshot_get_value(cs); | |
606 | } | |
607 | ||
608 | /* Open packet */ | |
609 | ret = bt_ctfser_open_packet(&stream->ctfser); | |
610 | if (ret) { | |
611 | /* bt_ctfser_open_packet() logs errors */ | |
612 | goto end; | |
613 | } | |
614 | ||
615 | /* Packet header: magic */ | |
d1f8929c | 616 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 617 | UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER); |
d1f8929c | 618 | if (ret) { |
aa1a7452 | 619 | BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s", |
d1f8929c FD |
620 | stream->file_name->str); |
621 | goto end; | |
622 | } | |
15fe47e0 PP |
623 | |
624 | /* Packet header: UUID */ | |
6162e6b7 | 625 | for (i = 0; i < BT_UUID_LEN; i++) { |
d1f8929c | 626 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
335a2da5 | 627 | (uint64_t) stream->sc->trace->uuid[i], 8, 8, BYTE_ORDER); |
d1f8929c | 628 | if (ret) { |
aa1a7452 | 629 | BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s", |
d1f8929c FD |
630 | stream->file_name->str); |
631 | goto end; | |
632 | } | |
15fe47e0 PP |
633 | } |
634 | ||
635 | /* Packet header: stream class ID */ | |
d1f8929c | 636 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 637 | bt_stream_class_get_id(stream->sc->ir_sc), 8, 64, BYTE_ORDER); |
d1f8929c | 638 | if (ret) { |
aa1a7452 | 639 | BT_COMP_LOGE("Error writing packet header stream class id: " |
d1f8929c FD |
640 | "stream-file-name=%s, stream-class-id=%"PRIu64, |
641 | stream->file_name->str, | |
642 | bt_stream_class_get_id(stream->sc->ir_sc)); | |
643 | goto end; | |
644 | } | |
15fe47e0 PP |
645 | |
646 | /* Packet header: stream ID */ | |
d1f8929c | 647 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 648 | bt_stream_get_id(stream->ir_stream), 8, 64, BYTE_ORDER); |
d1f8929c | 649 | if (ret) { |
aa1a7452 | 650 | BT_COMP_LOGE("Error writing packet header stream id: " |
d1f8929c FD |
651 | "stream-file-name=%s, stream-id=%"PRIu64, |
652 | stream->file_name->str, | |
653 | bt_stream_get_id(stream->ir_stream)); | |
654 | goto end; | |
655 | } | |
15fe47e0 PP |
656 | |
657 | /* Save packet context's offset to rewrite it later */ | |
658 | stream->packet_state.context_offset_bits = | |
659 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
660 | ||
661 | /* Write packet context just to advance to content (first event) */ | |
662 | ret = write_packet_context(stream); | |
663 | if (ret) { | |
664 | goto end; | |
665 | } | |
666 | ||
667 | stream->packet_state.is_open = true; | |
668 | ||
669 | end: | |
670 | return ret; | |
671 | } | |
672 | ||
673 | BT_HIDDEN | |
674 | int fs_sink_stream_close_packet(struct fs_sink_stream *stream, | |
675 | const bt_clock_snapshot *cs) | |
676 | { | |
677 | int ret; | |
678 | ||
679 | BT_ASSERT(stream->packet_state.is_open); | |
680 | ||
681 | if (cs) { | |
682 | stream->packet_state.end_cs = bt_clock_snapshot_get_value(cs); | |
683 | } | |
684 | ||
685 | stream->packet_state.content_size = | |
686 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
687 | stream->packet_state.total_size = | |
688 | (stream->packet_state.content_size + 7) & ~UINT64_C(7); | |
689 | ||
690 | /* Rewrite packet context */ | |
691 | bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser, | |
692 | stream->packet_state.context_offset_bits); | |
693 | ret = write_packet_context(stream); | |
694 | if (ret) { | |
695 | goto end; | |
696 | } | |
697 | ||
698 | /* Close packet */ | |
699 | bt_ctfser_close_current_packet(&stream->ctfser, | |
700 | stream->packet_state.total_size / 8); | |
701 | ||
702 | /* Partially copy current packet state to previous packet state */ | |
703 | stream->prev_packet_state.end_cs = stream->packet_state.end_cs; | |
704 | stream->prev_packet_state.discarded_events_counter = | |
705 | stream->packet_state.discarded_events_counter; | |
706 | stream->prev_packet_state.seq_num = | |
707 | stream->packet_state.seq_num; | |
708 | ||
709 | /* Reset current packet state */ | |
710 | stream->packet_state.beginning_cs = UINT64_C(-1); | |
711 | stream->packet_state.end_cs = UINT64_C(-1); | |
712 | stream->packet_state.content_size = 0; | |
713 | stream->packet_state.total_size = 0; | |
714 | stream->packet_state.seq_num += 1; | |
715 | stream->packet_state.context_offset_bits = 0; | |
716 | stream->packet_state.is_open = false; | |
717 | BT_PACKET_PUT_REF_AND_RESET(stream->packet_state.packet); | |
718 | ||
719 | end: | |
720 | return ret; | |
721 | } |