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