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