9ab71b257aecd2c51d6a3845ba61482cc67a09c2
[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" /* IWYU pragma: keep */
19 #include "ctfser/ctfser.h"
20
21 #include "fs-sink-ctf-meta.hpp"
22 #include "fs-sink-stream.hpp"
23 #include "fs-sink-trace.hpp"
24 #include "fs-sink.hpp"
25 #include "translate-trace-ir-to-ctf-ir.hpp"
26
27 void fs_sink_stream_destroy(struct fs_sink_stream *stream)
28 {
29 if (!stream) {
30 goto end;
31 }
32
33 bt_ctfser_fini(&stream->ctfser);
34
35 if (stream->file_name) {
36 g_string_free(stream->file_name, TRUE);
37 stream->file_name = NULL;
38 }
39
40 bt_packet_put_ref(stream->packet_state.packet);
41 delete stream;
42
43 end:
44 return;
45 }
46
47 static 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 = (fs_sink_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 GString *sanitize_stream_file_name(const char *file_name)
69 {
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;
93 }
94
95 static GString *make_unique_stream_file_name(struct fs_sink_trace *trace, const char *base)
96 {
97 GString *san_base = sanitize_stream_file_name(base);
98 GString *name = g_string_new(san_base->str);
99 unsigned int suffix = 0;
100
101 BT_ASSERT(name);
102
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 }
107
108 g_string_free(san_base, TRUE);
109 return name;
110 }
111
112 static void set_stream_file_name(struct fs_sink_stream *stream)
113 {
114 const char *base_name = bt_stream_get_name(stream->ir_stream);
115
116 if (!base_name) {
117 base_name = "stream";
118 }
119
120 BT_ASSERT(!stream->file_name);
121 stream->file_name = make_unique_stream_file_name(stream->trace, base_name);
122 }
123
124 struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace,
125 const bt_stream *ir_stream)
126 {
127 fs_sink_stream *stream = new fs_sink_stream;
128 int ret;
129 GString *path = g_string_new(trace->path->str);
130
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;
154
155 error:
156 fs_sink_stream_destroy(stream);
157 stream = NULL;
158
159 end:
160 if (path) {
161 g_string_free(path, TRUE);
162 }
163
164 return stream;
165 }
166
167 static int write_field(struct fs_sink_stream *stream, struct fs_sink_ctf_field_class *fc,
168 const bt_field *field);
169
170 static inline int write_bool_field(struct fs_sink_stream *stream,
171 struct fs_sink_ctf_field_class_bool *fc, const bt_field *field)
172 {
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);
180 }
181
182 static 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)
185 {
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);
193 }
194
195 static inline int write_int_field(struct fs_sink_stream *stream,
196 struct fs_sink_ctf_field_class_int *fc, const bt_field *field)
197 {
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;
210 }
211
212 static inline int write_float_field(struct fs_sink_stream *stream,
213 struct fs_sink_ctf_field_class_float *fc, const bt_field *field)
214 {
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;
227 }
228
229 static inline int write_string_field(struct fs_sink_stream *stream, const bt_field *field)
230 {
231 return bt_ctfser_write_string(&stream->ctfser, bt_field_string_get_value(field));
232 }
233
234 static 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)
237 {
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 }
249
250 end:
251 return ret;
252 }
253
254 static inline int write_sequence_field(struct fs_sink_stream *stream,
255 struct fs_sink_ctf_field_class_sequence *fc,
256 const bt_field *field)
257 {
258 int ret;
259
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 }
267
268 ret = write_array_base_field_elements(stream, &fc->base, field);
269
270 end:
271 return ret;
272 }
273
274 static 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)
277 {
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 }
299
300 end:
301 return ret;
302 }
303
304 static inline int write_option_field(struct fs_sink_stream *stream,
305 struct fs_sink_ctf_field_class_option *fc,
306 const bt_field *field)
307 {
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 }
330
331 end:
332 return ret;
333 }
334
335 static inline int write_variant_field(struct fs_sink_stream *stream,
336 struct fs_sink_ctf_field_class_variant *fc,
337 const bt_field *field)
338 {
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));
352
353 end:
354 return ret;
355 }
356
357 static int write_field(struct fs_sink_stream *stream, struct fs_sink_ctf_field_class *fc,
358 const bt_field *field)
359 {
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:
376 ret = write_string_field(stream, field);
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;
399 }
400
401 static inline int write_event_header(struct fs_sink_stream *stream, const bt_clock_snapshot *cs,
402 struct fs_sink_ctf_event_class *ec)
403 {
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 }
422
423 end:
424 return ret;
425 }
426
427 int 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)
429 {
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 }
472
473 end:
474 return ret;
475 }
476
477 static int write_packet_context(struct fs_sink_stream *stream)
478 {
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 }
543
544 end:
545 return ret;
546 }
547
548 int fs_sink_stream_open_packet(struct fs_sink_stream *stream, const bt_clock_snapshot *cs,
549 const bt_packet *packet)
550 {
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;
620
621 end:
622 return ret;
623 }
624
625 int fs_sink_stream_close_packet(struct fs_sink_stream *stream, const bt_clock_snapshot *cs)
626 {
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);
665
666 end:
667 return ret;
668 }
This page took 0.044842 seconds and 3 git commands to generate.