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