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