Commit | Line | Data |
---|---|---|
15fe47e0 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
15fe47e0 | 3 | * |
0235b0db | 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
15fe47e0 PP |
5 | */ |
6 | ||
aa1a7452 | 7 | #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp) |
ffa3b2b3 | 8 | #define BT_LOG_OUTPUT_LEVEL (stream->log_level) |
350ad6c1 | 9 | #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM" |
d9c39b0a | 10 | #include "logging/comp-logging.h" |
15fe47e0 | 11 | |
3fadfbc0 | 12 | #include <babeltrace2/babeltrace.h> |
15fe47e0 PP |
13 | #include <stdio.h> |
14 | #include <stdbool.h> | |
15 | #include <glib.h> | |
578e048b MJ |
16 | #include "common/assert.h" |
17 | #include "ctfser/ctfser.h" | |
18 | #include "compat/endian.h" | |
15fe47e0 | 19 | |
aa1a7452 | 20 | #include "fs-sink.h" |
15fe47e0 PP |
21 | #include "fs-sink-trace.h" |
22 | #include "fs-sink-stream.h" | |
23 | #include "translate-trace-ir-to-ctf-ir.h" | |
24 | ||
25 | BT_HIDDEN | |
26 | void fs_sink_stream_destroy(struct fs_sink_stream *stream) | |
27 | { | |
28 | if (!stream) { | |
29 | goto end; | |
30 | } | |
31 | ||
32 | bt_ctfser_fini(&stream->ctfser); | |
33 | ||
34 | if (stream->file_name) { | |
35 | g_string_free(stream->file_name, TRUE); | |
36 | stream->file_name = NULL; | |
37 | } | |
38 | ||
39 | bt_packet_put_ref(stream->packet_state.packet); | |
40 | g_free(stream); | |
41 | ||
42 | end: | |
43 | return; | |
44 | } | |
45 | ||
46 | static | |
47 | bool stream_file_name_exists(struct fs_sink_trace *trace, const char *name) | |
48 | { | |
49 | bool exists = false; | |
50 | GHashTableIter iter; | |
51 | gpointer key, value; | |
52 | ||
53 | g_hash_table_iter_init(&iter, trace->streams); | |
54 | ||
55 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
56 | struct fs_sink_stream *stream = value; | |
57 | ||
58 | if (strcmp(name, stream->file_name->str) == 0) { | |
59 | exists = true; | |
60 | goto end; | |
61 | } | |
62 | } | |
63 | ||
64 | end: | |
65 | return exists; | |
66 | } | |
67 | ||
68 | static | |
69 | GString *sanitize_stream_file_name(const char *file_name) | |
70 | { | |
71 | GString *san_file_name = g_string_new(NULL); | |
72 | const char *ch; | |
73 | gchar *basename; | |
74 | ||
75 | BT_ASSERT(san_file_name); | |
76 | BT_ASSERT(file_name); | |
77 | basename = g_path_get_basename(file_name); | |
78 | ||
79 | for (ch = basename; *ch != '\0'; ch++) { | |
80 | if (*ch == '/') { | |
81 | g_string_append_c(san_file_name, '_'); | |
82 | } else { | |
83 | g_string_append_c(san_file_name, *ch); | |
84 | } | |
85 | } | |
86 | ||
87 | /* Do not allow `.` and `..` either */ | |
88 | if (strcmp(san_file_name->str, ".") == 0 || | |
89 | strcmp(san_file_name->str, "..") == 0) { | |
90 | g_string_assign(san_file_name, "stream"); | |
91 | } | |
92 | ||
93 | g_free(basename); | |
94 | return san_file_name; | |
95 | } | |
96 | ||
97 | static | |
98 | GString *make_unique_stream_file_name(struct fs_sink_trace *trace, | |
99 | const char *base) | |
100 | { | |
101 | GString *san_base = sanitize_stream_file_name(base); | |
102 | GString *name = g_string_new(san_base->str); | |
103 | unsigned int suffix = 0; | |
104 | ||
105 | BT_ASSERT(name); | |
106 | ||
d19b245b | 107 | while (stream_file_name_exists(trace, name->str) || |
15fe47e0 | 108 | strcmp(name->str, "metadata") == 0) { |
b1acab38 | 109 | g_string_printf(name, "%s-%u", san_base->str, suffix); |
15fe47e0 PP |
110 | suffix++; |
111 | } | |
112 | ||
113 | g_string_free(san_base, TRUE); | |
114 | return name; | |
115 | } | |
116 | ||
117 | static | |
118 | void set_stream_file_name(struct fs_sink_stream *stream) | |
119 | { | |
120 | const char *base_name = bt_stream_get_name(stream->ir_stream); | |
121 | ||
122 | if (!base_name) { | |
123 | base_name = "stream"; | |
124 | } | |
125 | ||
126 | BT_ASSERT(!stream->file_name); | |
127 | stream->file_name = make_unique_stream_file_name(stream->trace, | |
128 | base_name); | |
129 | } | |
130 | ||
131 | BT_HIDDEN | |
132 | struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace, | |
133 | const bt_stream *ir_stream) | |
134 | { | |
135 | struct fs_sink_stream *stream = g_new0(struct fs_sink_stream, 1); | |
136 | int ret; | |
137 | GString *path = g_string_new(trace->path->str); | |
138 | ||
139 | if (!stream) { | |
140 | goto end; | |
141 | } | |
142 | ||
ffa3b2b3 | 143 | stream->log_level = trace->log_level; |
15fe47e0 PP |
144 | stream->trace = trace; |
145 | stream->ir_stream = ir_stream; | |
146 | stream->packet_state.beginning_cs = UINT64_C(-1); | |
147 | stream->packet_state.end_cs = UINT64_C(-1); | |
148 | stream->prev_packet_state.end_cs = UINT64_C(-1); | |
149 | stream->prev_packet_state.discarded_events_counter = UINT64_C(-1); | |
150 | stream->prev_packet_state.seq_num = UINT64_C(-1); | |
aa1a7452 | 151 | ret = try_translate_stream_class_trace_ir_to_ctf_ir(trace->fs_sink, |
335a2da5 | 152 | trace->trace, bt_stream_borrow_class_const(ir_stream), |
aa1a7452 | 153 | &stream->sc); |
15fe47e0 PP |
154 | if (ret) { |
155 | goto error; | |
156 | } | |
157 | ||
158 | set_stream_file_name(stream); | |
159 | g_string_append_printf(path, "/%s", stream->file_name->str); | |
ffa3b2b3 PP |
160 | ret = bt_ctfser_init(&stream->ctfser, path->str, |
161 | stream->log_level); | |
15fe47e0 PP |
162 | if (ret) { |
163 | goto error; | |
164 | } | |
165 | ||
166 | g_hash_table_insert(trace->streams, (gpointer) ir_stream, stream); | |
167 | goto end; | |
168 | ||
169 | error: | |
170 | fs_sink_stream_destroy(stream); | |
171 | stream = NULL; | |
172 | ||
173 | end: | |
174 | if (path) { | |
175 | g_string_free(path, TRUE); | |
176 | } | |
177 | ||
178 | return stream; | |
179 | } | |
180 | ||
181 | static | |
182 | int write_field(struct fs_sink_stream *stream, | |
183 | struct fs_sink_ctf_field_class *fc, const bt_field *field); | |
184 | ||
ecd27ae4 PP |
185 | static inline |
186 | int write_bool_field(struct fs_sink_stream *stream, | |
187 | struct fs_sink_ctf_field_class_int *fc, const bt_field *field) | |
188 | { | |
189 | /* | |
190 | * CTF 1.8 has no boolean field class type, so this component | |
191 | * translates this boolean field to an 8-bit unsigned integer | |
192 | * field which has the value 0 (false) or 1 (true). | |
193 | */ | |
194 | return bt_ctfser_write_unsigned_int(&stream->ctfser, | |
195 | bt_field_bool_get_value(field) ? 1 : 0, | |
196 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); | |
197 | } | |
198 | ||
43a94dc9 PP |
199 | static inline |
200 | int write_bit_array_field(struct fs_sink_stream *stream, | |
201 | struct fs_sink_ctf_field_class_bit_array *fc, | |
202 | const bt_field *field) | |
203 | { | |
204 | /* | |
205 | * CTF 1.8 has no bit array field class type, so this component | |
206 | * translates this bit array field to an unsigned integer field. | |
207 | */ | |
208 | return bt_ctfser_write_unsigned_int(&stream->ctfser, | |
209 | bt_field_bit_array_get_value_as_integer(field), | |
210 | fc->base.alignment, fc->size, BYTE_ORDER); | |
211 | } | |
212 | ||
15fe47e0 PP |
213 | static inline |
214 | int write_int_field(struct fs_sink_stream *stream, | |
215 | struct fs_sink_ctf_field_class_int *fc, const bt_field *field) | |
216 | { | |
217 | int ret; | |
218 | ||
219 | if (fc->is_signed) { | |
220 | ret = bt_ctfser_write_signed_int(&stream->ctfser, | |
9c08c816 | 221 | bt_field_integer_signed_get_value(field), |
15fe47e0 PP |
222 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
223 | } else { | |
224 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
9c08c816 | 225 | bt_field_integer_unsigned_get_value(field), |
15fe47e0 PP |
226 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
227 | } | |
228 | ||
229 | return ret; | |
230 | } | |
231 | ||
232 | static inline | |
233 | int write_float_field(struct fs_sink_stream *stream, | |
234 | struct fs_sink_ctf_field_class_float *fc, const bt_field *field) | |
235 | { | |
236 | int ret; | |
fe4df857 | 237 | double val; |
15fe47e0 PP |
238 | |
239 | if (fc->base.size == 32) { | |
fe4df857 | 240 | val = (double) bt_field_real_single_precision_get_value(field); |
15fe47e0 PP |
241 | ret = bt_ctfser_write_float32(&stream->ctfser, val, |
242 | fc->base.base.alignment, BYTE_ORDER); | |
243 | } else { | |
fe4df857 | 244 | val = bt_field_real_double_precision_get_value(field); |
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 = | |
7b4311c1 | 378 | bt_field_variant_get_selected_option_index(field); |
15fe47e0 PP |
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; | |
43a94dc9 PP |
408 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY: |
409 | ret = write_bit_array_field(stream, (void *) fc, field); | |
410 | break; | |
15fe47e0 PP |
411 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: |
412 | ret = write_int_field(stream, (void *) fc, field); | |
413 | break; | |
414 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: | |
415 | ret = write_float_field(stream, (void *) fc, field); | |
416 | break; | |
417 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: | |
418 | ret = write_string_field(stream, (void *) fc, field); | |
419 | break; | |
420 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
421 | ret = write_struct_field(stream, (void *) fc, field, true); | |
422 | break; | |
423 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
424 | ret = write_array_field_elements(stream, (void *) fc, field); | |
425 | break; | |
426 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
427 | ret = write_sequence_field(stream, (void *) fc, field); | |
428 | break; | |
c25f8e53 PP |
429 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
430 | ret = write_option_field(stream, (void *) fc, field); | |
431 | break; | |
15fe47e0 PP |
432 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
433 | ret = write_variant_field(stream, (void *) fc, field); | |
434 | break; | |
435 | default: | |
498e7994 | 436 | bt_common_abort(); |
15fe47e0 PP |
437 | } |
438 | ||
439 | return ret; | |
440 | } | |
441 | ||
442 | static inline | |
443 | int write_event_header(struct fs_sink_stream *stream, | |
444 | const bt_clock_snapshot *cs, struct fs_sink_ctf_event_class *ec) | |
445 | { | |
446 | int ret; | |
447 | ||
448 | /* Event class ID */ | |
449 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
450 | bt_event_class_get_id(ec->ir_ec), 8, 64, BYTE_ORDER); | |
91d81473 | 451 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
452 | goto end; |
453 | } | |
454 | ||
455 | /* Time */ | |
456 | if (stream->sc->default_clock_class) { | |
98b15851 | 457 | BT_ASSERT_DBG(cs); |
15fe47e0 PP |
458 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
459 | bt_clock_snapshot_get_value(cs), 8, 64, BYTE_ORDER); | |
91d81473 | 460 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
461 | goto end; |
462 | } | |
463 | } | |
464 | ||
465 | end: | |
466 | return ret; | |
467 | } | |
468 | ||
469 | BT_HIDDEN | |
470 | int fs_sink_stream_write_event(struct fs_sink_stream *stream, | |
471 | const bt_clock_snapshot *cs, const bt_event *event, | |
472 | struct fs_sink_ctf_event_class *ec) | |
473 | { | |
474 | int ret; | |
475 | const bt_field *field; | |
476 | ||
477 | /* Header */ | |
478 | ret = write_event_header(stream, cs, ec); | |
91d81473 | 479 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
480 | goto end; |
481 | } | |
482 | ||
483 | /* Common context */ | |
484 | if (stream->sc->event_common_context_fc) { | |
485 | field = bt_event_borrow_common_context_field_const(event); | |
98b15851 | 486 | BT_ASSERT_DBG(field); |
15fe47e0 PP |
487 | ret = write_struct_field(stream, |
488 | (void *) stream->sc->event_common_context_fc, | |
489 | field, true); | |
91d81473 | 490 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
491 | goto end; |
492 | } | |
493 | } | |
494 | ||
495 | /* Specific context */ | |
496 | if (ec->spec_context_fc) { | |
497 | field = bt_event_borrow_specific_context_field_const(event); | |
98b15851 | 498 | BT_ASSERT_DBG(field); |
15fe47e0 PP |
499 | ret = write_struct_field(stream, (void *) ec->spec_context_fc, |
500 | field, true); | |
91d81473 | 501 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
502 | goto end; |
503 | } | |
504 | } | |
505 | ||
506 | /* Specific context */ | |
507 | if (ec->payload_fc) { | |
508 | field = bt_event_borrow_payload_field_const(event); | |
98b15851 | 509 | BT_ASSERT_DBG(field); |
15fe47e0 PP |
510 | ret = write_struct_field(stream, (void *) ec->payload_fc, |
511 | field, true); | |
91d81473 | 512 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
513 | goto end; |
514 | } | |
515 | } | |
516 | ||
517 | end: | |
518 | return ret; | |
519 | } | |
520 | ||
521 | static | |
522 | int write_packet_context(struct fs_sink_stream *stream) | |
523 | { | |
524 | int ret; | |
525 | ||
526 | /* Packet total size */ | |
527 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
528 | stream->packet_state.total_size, 8, 64, BYTE_ORDER); | |
529 | if (ret) { | |
530 | goto end; | |
531 | } | |
532 | ||
533 | /* Packet content size */ | |
534 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
535 | stream->packet_state.content_size, 8, 64, BYTE_ORDER); | |
536 | if (ret) { | |
537 | goto end; | |
538 | } | |
539 | ||
ffb5c13c | 540 | if (stream->sc->packets_have_ts_begin) { |
15fe47e0 PP |
541 | /* Beginning time */ |
542 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
543 | stream->packet_state.beginning_cs, 8, 64, BYTE_ORDER); | |
544 | if (ret) { | |
545 | goto end; | |
546 | } | |
ffb5c13c | 547 | } |
15fe47e0 | 548 | |
ffb5c13c | 549 | if (stream->sc->packets_have_ts_end) { |
15fe47e0 PP |
550 | /* End time */ |
551 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
552 | stream->packet_state.end_cs, 8, 64, BYTE_ORDER); | |
553 | if (ret) { | |
554 | goto end; | |
555 | } | |
556 | } | |
557 | ||
ffb5c13c PP |
558 | if (stream->sc->has_discarded_events) { |
559 | /* Discarded event counter */ | |
560 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
561 | stream->packet_state.discarded_events_counter, 8, 64, | |
562 | BYTE_ORDER); | |
563 | if (ret) { | |
564 | goto end; | |
565 | } | |
15fe47e0 PP |
566 | } |
567 | ||
568 | /* Sequence number */ | |
569 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
570 | stream->packet_state.seq_num, 8, 64, BYTE_ORDER); | |
571 | if (ret) { | |
572 | goto end; | |
573 | } | |
574 | ||
575 | /* Other members */ | |
576 | if (stream->sc->packet_context_fc) { | |
26fc5aed | 577 | const bt_field *packet_context_field; |
15fe47e0 | 578 | |
26fc5aed PP |
579 | BT_ASSERT(stream->packet_state.packet); |
580 | packet_context_field = bt_packet_borrow_context_field_const( | |
581 | stream->packet_state.packet); | |
15fe47e0 PP |
582 | BT_ASSERT(packet_context_field); |
583 | ret = write_struct_field(stream, | |
584 | (void *) stream->sc->packet_context_fc, | |
585 | packet_context_field, false); | |
586 | if (ret) { | |
587 | goto end; | |
588 | } | |
589 | } | |
590 | ||
591 | end: | |
592 | return ret; | |
593 | } | |
594 | ||
595 | BT_HIDDEN | |
596 | int fs_sink_stream_open_packet(struct fs_sink_stream *stream, | |
597 | const bt_clock_snapshot *cs, const bt_packet *packet) | |
598 | { | |
599 | int ret; | |
600 | uint64_t i; | |
601 | ||
602 | BT_ASSERT(!stream->packet_state.is_open); | |
603 | bt_packet_put_ref(stream->packet_state.packet); | |
604 | stream->packet_state.packet = packet; | |
605 | bt_packet_get_ref(stream->packet_state.packet); | |
606 | if (cs) { | |
607 | stream->packet_state.beginning_cs = | |
608 | bt_clock_snapshot_get_value(cs); | |
609 | } | |
610 | ||
611 | /* Open packet */ | |
612 | ret = bt_ctfser_open_packet(&stream->ctfser); | |
613 | if (ret) { | |
614 | /* bt_ctfser_open_packet() logs errors */ | |
615 | goto end; | |
616 | } | |
617 | ||
618 | /* Packet header: magic */ | |
d1f8929c | 619 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 620 | UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER); |
d1f8929c | 621 | if (ret) { |
aa1a7452 | 622 | BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s", |
d1f8929c FD |
623 | stream->file_name->str); |
624 | goto end; | |
625 | } | |
15fe47e0 PP |
626 | |
627 | /* Packet header: UUID */ | |
6162e6b7 | 628 | for (i = 0; i < BT_UUID_LEN; i++) { |
d1f8929c | 629 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
335a2da5 | 630 | (uint64_t) stream->sc->trace->uuid[i], 8, 8, BYTE_ORDER); |
d1f8929c | 631 | if (ret) { |
aa1a7452 | 632 | BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s", |
d1f8929c FD |
633 | stream->file_name->str); |
634 | goto end; | |
635 | } | |
15fe47e0 PP |
636 | } |
637 | ||
638 | /* Packet header: stream class ID */ | |
d1f8929c | 639 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 640 | bt_stream_class_get_id(stream->sc->ir_sc), 8, 64, BYTE_ORDER); |
d1f8929c | 641 | if (ret) { |
aa1a7452 | 642 | BT_COMP_LOGE("Error writing packet header stream class id: " |
d1f8929c FD |
643 | "stream-file-name=%s, stream-class-id=%"PRIu64, |
644 | stream->file_name->str, | |
645 | bt_stream_class_get_id(stream->sc->ir_sc)); | |
646 | goto end; | |
647 | } | |
15fe47e0 PP |
648 | |
649 | /* Packet header: stream ID */ | |
d1f8929c | 650 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 651 | bt_stream_get_id(stream->ir_stream), 8, 64, BYTE_ORDER); |
d1f8929c | 652 | if (ret) { |
aa1a7452 | 653 | BT_COMP_LOGE("Error writing packet header stream id: " |
d1f8929c FD |
654 | "stream-file-name=%s, stream-id=%"PRIu64, |
655 | stream->file_name->str, | |
656 | bt_stream_get_id(stream->ir_stream)); | |
657 | goto end; | |
658 | } | |
15fe47e0 PP |
659 | |
660 | /* Save packet context's offset to rewrite it later */ | |
661 | stream->packet_state.context_offset_bits = | |
662 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
663 | ||
664 | /* Write packet context just to advance to content (first event) */ | |
665 | ret = write_packet_context(stream); | |
666 | if (ret) { | |
667 | goto end; | |
668 | } | |
669 | ||
670 | stream->packet_state.is_open = true; | |
671 | ||
672 | end: | |
673 | return ret; | |
674 | } | |
675 | ||
676 | BT_HIDDEN | |
677 | int fs_sink_stream_close_packet(struct fs_sink_stream *stream, | |
678 | const bt_clock_snapshot *cs) | |
679 | { | |
680 | int ret; | |
681 | ||
682 | BT_ASSERT(stream->packet_state.is_open); | |
683 | ||
684 | if (cs) { | |
685 | stream->packet_state.end_cs = bt_clock_snapshot_get_value(cs); | |
686 | } | |
687 | ||
688 | stream->packet_state.content_size = | |
689 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
690 | stream->packet_state.total_size = | |
691 | (stream->packet_state.content_size + 7) & ~UINT64_C(7); | |
692 | ||
693 | /* Rewrite packet context */ | |
694 | bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser, | |
695 | stream->packet_state.context_offset_bits); | |
696 | ret = write_packet_context(stream); | |
697 | if (ret) { | |
698 | goto end; | |
699 | } | |
700 | ||
701 | /* Close packet */ | |
702 | bt_ctfser_close_current_packet(&stream->ctfser, | |
703 | stream->packet_state.total_size / 8); | |
704 | ||
705 | /* Partially copy current packet state to previous packet state */ | |
706 | stream->prev_packet_state.end_cs = stream->packet_state.end_cs; | |
707 | stream->prev_packet_state.discarded_events_counter = | |
708 | stream->packet_state.discarded_events_counter; | |
709 | stream->prev_packet_state.seq_num = | |
710 | stream->packet_state.seq_num; | |
711 | ||
712 | /* Reset current packet state */ | |
713 | stream->packet_state.beginning_cs = UINT64_C(-1); | |
714 | stream->packet_state.end_cs = UINT64_C(-1); | |
715 | stream->packet_state.content_size = 0; | |
716 | stream->packet_state.total_size = 0; | |
717 | stream->packet_state.seq_num += 1; | |
718 | stream->packet_state.context_offset_bits = 0; | |
719 | stream->packet_state.is_open = false; | |
720 | BT_PACKET_PUT_REF_AND_RESET(stream->packet_state.packet); | |
721 | ||
722 | end: | |
723 | return ret; | |
724 | } |