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 | |
087cd0f5 SM |
20 | #include "fs-sink.hpp" |
21 | #include "fs-sink-trace.hpp" | |
22 | #include "fs-sink-stream.hpp" | |
23 | #include "translate-trace-ir-to-ctf-ir.hpp" | |
15fe47e0 PP |
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)) { | |
087cd0f5 | 56 | struct fs_sink_stream *stream = (fs_sink_stream *) value; |
15fe47e0 PP |
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, | |
087cd0f5 | 187 | struct fs_sink_ctf_field_class_bool *fc, const bt_field *field) |
ecd27ae4 PP |
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 | |
087cd0f5 | 261 | int write_array_base_field_elements(struct fs_sink_stream *stream, |
15fe47e0 PP |
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 | ||
087cd0f5 | 298 | ret = write_array_base_field_elements(stream, &fc->base, field); |
15fe47e0 PP |
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 | 405 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL: |
087cd0f5 SM |
406 | ret = write_bool_field(stream, |
407 | fs_sink_ctf_field_class_as_bool(fc), field); | |
ecd27ae4 | 408 | break; |
43a94dc9 | 409 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY: |
087cd0f5 SM |
410 | ret = write_bit_array_field(stream, |
411 | fs_sink_ctf_field_class_as_bit_array(fc), field); | |
43a94dc9 | 412 | break; |
15fe47e0 | 413 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: |
087cd0f5 SM |
414 | ret = write_int_field(stream, |
415 | fs_sink_ctf_field_class_as_int(fc), field); | |
15fe47e0 PP |
416 | break; |
417 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: | |
087cd0f5 SM |
418 | ret = write_float_field(stream, |
419 | fs_sink_ctf_field_class_as_float(fc), field); | |
15fe47e0 PP |
420 | break; |
421 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: | |
087cd0f5 SM |
422 | ret = write_string_field(stream, |
423 | fs_sink_ctf_field_class_as_string(fc), field); | |
15fe47e0 PP |
424 | break; |
425 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
087cd0f5 SM |
426 | ret = write_struct_field(stream, |
427 | fs_sink_ctf_field_class_as_struct(fc), field, true); | |
15fe47e0 PP |
428 | break; |
429 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
087cd0f5 SM |
430 | ret = write_array_base_field_elements(stream, |
431 | fs_sink_ctf_field_class_as_array_base(fc), field); | |
15fe47e0 PP |
432 | break; |
433 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
087cd0f5 SM |
434 | ret = write_sequence_field(stream, |
435 | fs_sink_ctf_field_class_as_sequence(fc), field); | |
15fe47e0 | 436 | break; |
c25f8e53 | 437 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
087cd0f5 SM |
438 | ret = write_option_field(stream, |
439 | fs_sink_ctf_field_class_as_option(fc), field); | |
c25f8e53 | 440 | break; |
15fe47e0 | 441 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
087cd0f5 SM |
442 | ret = write_variant_field(stream, |
443 | fs_sink_ctf_field_class_as_variant(fc), field); | |
15fe47e0 PP |
444 | break; |
445 | default: | |
498e7994 | 446 | bt_common_abort(); |
15fe47e0 PP |
447 | } |
448 | ||
449 | return ret; | |
450 | } | |
451 | ||
452 | static inline | |
453 | int write_event_header(struct fs_sink_stream *stream, | |
454 | const bt_clock_snapshot *cs, struct fs_sink_ctf_event_class *ec) | |
455 | { | |
456 | int ret; | |
457 | ||
458 | /* Event class ID */ | |
459 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
460 | bt_event_class_get_id(ec->ir_ec), 8, 64, BYTE_ORDER); | |
91d81473 | 461 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
462 | goto end; |
463 | } | |
464 | ||
465 | /* Time */ | |
466 | if (stream->sc->default_clock_class) { | |
98b15851 | 467 | BT_ASSERT_DBG(cs); |
15fe47e0 PP |
468 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
469 | bt_clock_snapshot_get_value(cs), 8, 64, BYTE_ORDER); | |
91d81473 | 470 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
471 | goto end; |
472 | } | |
473 | } | |
474 | ||
475 | end: | |
476 | return ret; | |
477 | } | |
478 | ||
479 | BT_HIDDEN | |
480 | int fs_sink_stream_write_event(struct fs_sink_stream *stream, | |
481 | const bt_clock_snapshot *cs, const bt_event *event, | |
482 | struct fs_sink_ctf_event_class *ec) | |
483 | { | |
484 | int ret; | |
485 | const bt_field *field; | |
486 | ||
487 | /* Header */ | |
488 | ret = write_event_header(stream, cs, ec); | |
91d81473 | 489 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
490 | goto end; |
491 | } | |
492 | ||
493 | /* Common context */ | |
494 | if (stream->sc->event_common_context_fc) { | |
495 | field = bt_event_borrow_common_context_field_const(event); | |
98b15851 | 496 | BT_ASSERT_DBG(field); |
15fe47e0 | 497 | ret = write_struct_field(stream, |
087cd0f5 | 498 | fs_sink_ctf_field_class_as_struct(stream->sc->event_common_context_fc), |
15fe47e0 | 499 | field, true); |
91d81473 | 500 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
501 | goto end; |
502 | } | |
503 | } | |
504 | ||
505 | /* Specific context */ | |
506 | if (ec->spec_context_fc) { | |
507 | field = bt_event_borrow_specific_context_field_const(event); | |
98b15851 | 508 | BT_ASSERT_DBG(field); |
087cd0f5 SM |
509 | ret = write_struct_field(stream, |
510 | fs_sink_ctf_field_class_as_struct(ec->spec_context_fc), | |
15fe47e0 | 511 | field, true); |
91d81473 | 512 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
513 | goto end; |
514 | } | |
515 | } | |
516 | ||
517 | /* Specific context */ | |
518 | if (ec->payload_fc) { | |
519 | field = bt_event_borrow_payload_field_const(event); | |
98b15851 | 520 | BT_ASSERT_DBG(field); |
087cd0f5 SM |
521 | ret = write_struct_field(stream, |
522 | fs_sink_ctf_field_class_as_struct(ec->payload_fc), | |
15fe47e0 | 523 | field, true); |
91d81473 | 524 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
525 | goto end; |
526 | } | |
527 | } | |
528 | ||
529 | end: | |
530 | return ret; | |
531 | } | |
532 | ||
533 | static | |
534 | int write_packet_context(struct fs_sink_stream *stream) | |
535 | { | |
536 | int ret; | |
537 | ||
538 | /* Packet total size */ | |
539 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
540 | stream->packet_state.total_size, 8, 64, BYTE_ORDER); | |
541 | if (ret) { | |
542 | goto end; | |
543 | } | |
544 | ||
545 | /* Packet content size */ | |
546 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
547 | stream->packet_state.content_size, 8, 64, BYTE_ORDER); | |
548 | if (ret) { | |
549 | goto end; | |
550 | } | |
551 | ||
ffb5c13c | 552 | if (stream->sc->packets_have_ts_begin) { |
15fe47e0 PP |
553 | /* Beginning time */ |
554 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
555 | stream->packet_state.beginning_cs, 8, 64, BYTE_ORDER); | |
556 | if (ret) { | |
557 | goto end; | |
558 | } | |
ffb5c13c | 559 | } |
15fe47e0 | 560 | |
ffb5c13c | 561 | if (stream->sc->packets_have_ts_end) { |
15fe47e0 PP |
562 | /* End time */ |
563 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
564 | stream->packet_state.end_cs, 8, 64, BYTE_ORDER); | |
565 | if (ret) { | |
566 | goto end; | |
567 | } | |
568 | } | |
569 | ||
ffb5c13c PP |
570 | if (stream->sc->has_discarded_events) { |
571 | /* Discarded event counter */ | |
572 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
573 | stream->packet_state.discarded_events_counter, 8, 64, | |
574 | BYTE_ORDER); | |
575 | if (ret) { | |
576 | goto end; | |
577 | } | |
15fe47e0 PP |
578 | } |
579 | ||
580 | /* Sequence number */ | |
581 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
582 | stream->packet_state.seq_num, 8, 64, BYTE_ORDER); | |
583 | if (ret) { | |
584 | goto end; | |
585 | } | |
586 | ||
587 | /* Other members */ | |
588 | if (stream->sc->packet_context_fc) { | |
26fc5aed | 589 | const bt_field *packet_context_field; |
15fe47e0 | 590 | |
26fc5aed PP |
591 | BT_ASSERT(stream->packet_state.packet); |
592 | packet_context_field = bt_packet_borrow_context_field_const( | |
593 | stream->packet_state.packet); | |
15fe47e0 PP |
594 | BT_ASSERT(packet_context_field); |
595 | ret = write_struct_field(stream, | |
087cd0f5 | 596 | fs_sink_ctf_field_class_as_struct(stream->sc->packet_context_fc), |
15fe47e0 PP |
597 | packet_context_field, false); |
598 | if (ret) { | |
599 | goto end; | |
600 | } | |
601 | } | |
602 | ||
603 | end: | |
604 | return ret; | |
605 | } | |
606 | ||
607 | BT_HIDDEN | |
608 | int fs_sink_stream_open_packet(struct fs_sink_stream *stream, | |
609 | const bt_clock_snapshot *cs, const bt_packet *packet) | |
610 | { | |
611 | int ret; | |
612 | uint64_t i; | |
613 | ||
614 | BT_ASSERT(!stream->packet_state.is_open); | |
615 | bt_packet_put_ref(stream->packet_state.packet); | |
616 | stream->packet_state.packet = packet; | |
617 | bt_packet_get_ref(stream->packet_state.packet); | |
618 | if (cs) { | |
619 | stream->packet_state.beginning_cs = | |
620 | bt_clock_snapshot_get_value(cs); | |
621 | } | |
622 | ||
623 | /* Open packet */ | |
624 | ret = bt_ctfser_open_packet(&stream->ctfser); | |
625 | if (ret) { | |
626 | /* bt_ctfser_open_packet() logs errors */ | |
627 | goto end; | |
628 | } | |
629 | ||
630 | /* Packet header: magic */ | |
d1f8929c | 631 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 632 | UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER); |
d1f8929c | 633 | if (ret) { |
aa1a7452 | 634 | BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s", |
d1f8929c FD |
635 | stream->file_name->str); |
636 | goto end; | |
637 | } | |
15fe47e0 PP |
638 | |
639 | /* Packet header: UUID */ | |
6162e6b7 | 640 | for (i = 0; i < BT_UUID_LEN; i++) { |
d1f8929c | 641 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
335a2da5 | 642 | (uint64_t) stream->sc->trace->uuid[i], 8, 8, BYTE_ORDER); |
d1f8929c | 643 | if (ret) { |
aa1a7452 | 644 | BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s", |
d1f8929c FD |
645 | stream->file_name->str); |
646 | goto end; | |
647 | } | |
15fe47e0 PP |
648 | } |
649 | ||
650 | /* Packet header: stream class ID */ | |
d1f8929c | 651 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 652 | bt_stream_class_get_id(stream->sc->ir_sc), 8, 64, BYTE_ORDER); |
d1f8929c | 653 | if (ret) { |
aa1a7452 | 654 | BT_COMP_LOGE("Error writing packet header stream class id: " |
087cd0f5 | 655 | "stream-file-name=%s, stream-class-id=%" PRIu64, |
d1f8929c FD |
656 | stream->file_name->str, |
657 | bt_stream_class_get_id(stream->sc->ir_sc)); | |
658 | goto end; | |
659 | } | |
15fe47e0 PP |
660 | |
661 | /* Packet header: stream ID */ | |
d1f8929c | 662 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 663 | bt_stream_get_id(stream->ir_stream), 8, 64, BYTE_ORDER); |
d1f8929c | 664 | if (ret) { |
aa1a7452 | 665 | BT_COMP_LOGE("Error writing packet header stream id: " |
087cd0f5 | 666 | "stream-file-name=%s, stream-id=%" PRIu64, |
d1f8929c FD |
667 | stream->file_name->str, |
668 | bt_stream_get_id(stream->ir_stream)); | |
669 | goto end; | |
670 | } | |
15fe47e0 PP |
671 | |
672 | /* Save packet context's offset to rewrite it later */ | |
673 | stream->packet_state.context_offset_bits = | |
674 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
675 | ||
676 | /* Write packet context just to advance to content (first event) */ | |
677 | ret = write_packet_context(stream); | |
678 | if (ret) { | |
679 | goto end; | |
680 | } | |
681 | ||
682 | stream->packet_state.is_open = true; | |
683 | ||
684 | end: | |
685 | return ret; | |
686 | } | |
687 | ||
688 | BT_HIDDEN | |
689 | int fs_sink_stream_close_packet(struct fs_sink_stream *stream, | |
690 | const bt_clock_snapshot *cs) | |
691 | { | |
692 | int ret; | |
693 | ||
694 | BT_ASSERT(stream->packet_state.is_open); | |
695 | ||
696 | if (cs) { | |
697 | stream->packet_state.end_cs = bt_clock_snapshot_get_value(cs); | |
698 | } | |
699 | ||
700 | stream->packet_state.content_size = | |
701 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
702 | stream->packet_state.total_size = | |
703 | (stream->packet_state.content_size + 7) & ~UINT64_C(7); | |
704 | ||
705 | /* Rewrite packet context */ | |
706 | bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser, | |
707 | stream->packet_state.context_offset_bits); | |
708 | ret = write_packet_context(stream); | |
709 | if (ret) { | |
710 | goto end; | |
711 | } | |
712 | ||
713 | /* Close packet */ | |
714 | bt_ctfser_close_current_packet(&stream->ctfser, | |
715 | stream->packet_state.total_size / 8); | |
716 | ||
717 | /* Partially copy current packet state to previous packet state */ | |
718 | stream->prev_packet_state.end_cs = stream->packet_state.end_cs; | |
719 | stream->prev_packet_state.discarded_events_counter = | |
720 | stream->packet_state.discarded_events_counter; | |
721 | stream->prev_packet_state.seq_num = | |
722 | stream->packet_state.seq_num; | |
723 | ||
724 | /* Reset current packet state */ | |
725 | stream->packet_state.beginning_cs = UINT64_C(-1); | |
726 | stream->packet_state.end_cs = UINT64_C(-1); | |
727 | stream->packet_state.content_size = 0; | |
728 | stream->packet_state.total_size = 0; | |
729 | stream->packet_state.seq_num += 1; | |
730 | stream->packet_state.context_offset_bits = 0; | |
731 | stream->packet_state.is_open = false; | |
732 | BT_PACKET_PUT_REF_AND_RESET(stream->packet_state.packet); | |
733 | ||
734 | end: | |
735 | return ret; | |
736 | } |