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