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