Commit | Line | Data |
---|---|---|
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 | ||
aa1a7452 | 23 | #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp) |
ffa3b2b3 | 24 | #define BT_LOG_OUTPUT_LEVEL (stream->log_level) |
350ad6c1 | 25 | #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM" |
d9c39b0a | 26 | #include "logging/comp-logging.h" |
15fe47e0 | 27 | |
3fadfbc0 | 28 | #include <babeltrace2/babeltrace.h> |
15fe47e0 PP |
29 | #include <stdio.h> |
30 | #include <stdbool.h> | |
31 | #include <glib.h> | |
578e048b MJ |
32 | #include "common/assert.h" |
33 | #include "ctfser/ctfser.h" | |
34 | #include "compat/endian.h" | |
15fe47e0 | 35 | |
aa1a7452 | 36 | #include "fs-sink.h" |
15fe47e0 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) { | |
b1acab38 | 125 | g_string_printf(name, "%s-%u", san_base->str, suffix); |
15fe47e0 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 | ||
ffa3b2b3 | 159 | stream->log_level = trace->log_level; |
15fe47e0 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); | |
aa1a7452 | 167 | ret = try_translate_stream_class_trace_ir_to_ctf_ir(trace->fs_sink, |
335a2da5 | 168 | trace->trace, bt_stream_borrow_class_const(ir_stream), |
aa1a7452 | 169 | &stream->sc); |
15fe47e0 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); | |
ffa3b2b3 PP |
176 | ret = bt_ctfser_init(&stream->ctfser, path->str, |
177 | stream->log_level); | |
15fe47e0 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 | ||
ecd27ae4 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 | ||
43a94dc9 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 | ||
15fe47e0 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, | |
9c08c816 | 237 | bt_field_integer_signed_get_value(field), |
15fe47e0 PP |
238 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
239 | } else { | |
240 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
9c08c816 | 241 | bt_field_integer_unsigned_get_value(field), |
15fe47e0 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; | |
253 | double val = bt_field_real_get_value(field); | |
254 | ||
255 | if (fc->base.size == 32) { | |
256 | ret = bt_ctfser_write_float32(&stream->ctfser, val, | |
257 | fc->base.base.alignment, BYTE_ORDER); | |
258 | } else { | |
263e3582 | 259 | ret = bt_ctfser_write_float64(&stream->ctfser, val, |
15fe47e0 PP |
260 | fc->base.base.alignment, BYTE_ORDER); |
261 | } | |
262 | ||
263 | return ret; | |
264 | } | |
265 | ||
266 | static inline | |
267 | int write_string_field(struct fs_sink_stream *stream, | |
268 | struct fs_sink_ctf_field_class_string *fc, const bt_field *field) | |
269 | { | |
270 | return bt_ctfser_write_string(&stream->ctfser, | |
271 | bt_field_string_get_value(field)); | |
272 | } | |
273 | ||
274 | static inline | |
275 | int write_array_field_elements(struct fs_sink_stream *stream, | |
276 | struct fs_sink_ctf_field_class_array_base *fc, | |
277 | const bt_field *field) | |
278 | { | |
279 | uint64_t i; | |
280 | uint64_t len = bt_field_array_get_length(field); | |
281 | int ret = 0; | |
282 | ||
283 | for (i = 0; i < len; i++) { | |
284 | const bt_field *elem_field = | |
285 | bt_field_array_borrow_element_field_by_index_const( | |
286 | field, i); | |
287 | ret = write_field(stream, fc->elem_fc, elem_field); | |
91d81473 | 288 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
289 | goto end; |
290 | } | |
291 | } | |
292 | ||
293 | end: | |
294 | return ret; | |
295 | } | |
296 | ||
297 | static inline | |
298 | int write_sequence_field(struct fs_sink_stream *stream, | |
299 | struct fs_sink_ctf_field_class_sequence *fc, | |
300 | const bt_field *field) | |
301 | { | |
302 | int ret; | |
303 | ||
304 | if (fc->length_is_before) { | |
305 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
306 | bt_field_array_get_length(field), 8, 32, BYTE_ORDER); | |
91d81473 | 307 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
308 | goto end; |
309 | } | |
310 | } | |
311 | ||
312 | ret = write_array_field_elements(stream, (void *) fc, field); | |
313 | ||
314 | end: | |
315 | return ret; | |
316 | } | |
317 | ||
318 | static inline | |
319 | int write_struct_field(struct fs_sink_stream *stream, | |
320 | struct fs_sink_ctf_field_class_struct *fc, | |
321 | const bt_field *field, bool align_struct) | |
322 | { | |
323 | int ret = 0; | |
324 | uint64_t i; | |
325 | ||
91d81473 | 326 | if (G_LIKELY(align_struct)) { |
15fe47e0 PP |
327 | ret = bt_ctfser_align_offset_in_current_packet(&stream->ctfser, |
328 | fc->base.alignment); | |
91d81473 | 329 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
330 | goto end; |
331 | } | |
332 | } | |
333 | ||
334 | for (i = 0; i < fc->members->len; i++) { | |
335 | const bt_field *memb_field = | |
336 | bt_field_structure_borrow_member_field_by_index_const( | |
337 | field, i); | |
338 | struct fs_sink_ctf_field_class *member_fc = | |
339 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
340 | fc, i)->fc; | |
341 | ||
342 | ret = write_field(stream, member_fc, memb_field); | |
91d81473 | 343 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
344 | goto end; |
345 | } | |
346 | } | |
347 | ||
348 | end: | |
349 | return ret; | |
350 | } | |
351 | ||
c25f8e53 PP |
352 | static inline |
353 | int write_option_field(struct fs_sink_stream *stream, | |
354 | struct fs_sink_ctf_field_class_option *fc, | |
355 | const bt_field *field) | |
356 | { | |
357 | int ret; | |
358 | const bt_field *content_field = | |
359 | bt_field_option_borrow_field_const(field); | |
360 | ||
361 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
362 | content_field ? 1 : 0, 8, 8, BYTE_ORDER); | |
363 | if (G_UNLIKELY(ret)) { | |
364 | goto end; | |
365 | } | |
366 | ||
367 | /* | |
368 | * CTF 1.8 has no option field class type, so this component | |
369 | * translates the option field class to a variant field class | |
370 | * where the options are: | |
371 | * | |
372 | * * An empty structure field class (field occupies 0 bits). | |
373 | * * The optional field class itself. | |
374 | * | |
375 | * If `content_field` is `NULL`, do not write anything (empty | |
376 | * structure). | |
377 | */ | |
378 | if (content_field) { | |
379 | ret = write_field(stream, fc->content_fc, content_field); | |
380 | } | |
381 | ||
382 | end: | |
383 | return ret; | |
384 | } | |
385 | ||
15fe47e0 PP |
386 | static inline |
387 | int write_variant_field(struct fs_sink_stream *stream, | |
388 | struct fs_sink_ctf_field_class_variant *fc, | |
389 | const bt_field *field) | |
390 | { | |
391 | uint64_t opt_index = | |
392 | bt_field_variant_get_selected_option_field_index(field); | |
393 | int ret; | |
394 | ||
395 | if (fc->tag_is_before) { | |
396 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, | |
397 | opt_index, 8, 16, BYTE_ORDER); | |
91d81473 | 398 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
399 | goto end; |
400 | } | |
401 | } | |
402 | ||
403 | ret = write_field(stream, | |
404 | fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, | |
405 | opt_index)->fc, | |
406 | bt_field_variant_borrow_selected_option_field_const(field)); | |
407 | ||
408 | end: | |
409 | return ret; | |
410 | } | |
411 | ||
412 | static | |
413 | int write_field(struct fs_sink_stream *stream, | |
414 | struct fs_sink_ctf_field_class *fc, const bt_field *field) | |
415 | { | |
416 | int ret; | |
417 | ||
418 | switch (fc->type) { | |
ecd27ae4 PP |
419 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL: |
420 | ret = write_bool_field(stream, (void *) fc, field); | |
421 | break; | |
43a94dc9 PP |
422 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY: |
423 | ret = write_bit_array_field(stream, (void *) fc, field); | |
424 | break; | |
15fe47e0 PP |
425 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: |
426 | ret = write_int_field(stream, (void *) fc, field); | |
427 | break; | |
428 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: | |
429 | ret = write_float_field(stream, (void *) fc, field); | |
430 | break; | |
431 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: | |
432 | ret = write_string_field(stream, (void *) fc, field); | |
433 | break; | |
434 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
435 | ret = write_struct_field(stream, (void *) fc, field, true); | |
436 | break; | |
437 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
438 | ret = write_array_field_elements(stream, (void *) fc, field); | |
439 | break; | |
440 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
441 | ret = write_sequence_field(stream, (void *) fc, field); | |
442 | break; | |
c25f8e53 PP |
443 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
444 | ret = write_option_field(stream, (void *) fc, field); | |
445 | break; | |
15fe47e0 PP |
446 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
447 | ret = write_variant_field(stream, (void *) fc, field); | |
448 | break; | |
449 | default: | |
450 | abort(); | |
451 | } | |
452 | ||
453 | return ret; | |
454 | } | |
455 | ||
456 | static inline | |
457 | int write_event_header(struct fs_sink_stream *stream, | |
458 | const bt_clock_snapshot *cs, struct fs_sink_ctf_event_class *ec) | |
459 | { | |
460 | int ret; | |
461 | ||
462 | /* Event class ID */ | |
463 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
464 | bt_event_class_get_id(ec->ir_ec), 8, 64, BYTE_ORDER); | |
91d81473 | 465 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
466 | goto end; |
467 | } | |
468 | ||
469 | /* Time */ | |
470 | if (stream->sc->default_clock_class) { | |
471 | BT_ASSERT(cs); | |
472 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
473 | bt_clock_snapshot_get_value(cs), 8, 64, BYTE_ORDER); | |
91d81473 | 474 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
475 | goto end; |
476 | } | |
477 | } | |
478 | ||
479 | end: | |
480 | return ret; | |
481 | } | |
482 | ||
483 | BT_HIDDEN | |
484 | int fs_sink_stream_write_event(struct fs_sink_stream *stream, | |
485 | const bt_clock_snapshot *cs, const bt_event *event, | |
486 | struct fs_sink_ctf_event_class *ec) | |
487 | { | |
488 | int ret; | |
489 | const bt_field *field; | |
490 | ||
491 | /* Header */ | |
492 | ret = write_event_header(stream, cs, ec); | |
91d81473 | 493 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
494 | goto end; |
495 | } | |
496 | ||
497 | /* Common context */ | |
498 | if (stream->sc->event_common_context_fc) { | |
499 | field = bt_event_borrow_common_context_field_const(event); | |
500 | BT_ASSERT(field); | |
501 | ret = write_struct_field(stream, | |
502 | (void *) stream->sc->event_common_context_fc, | |
503 | field, true); | |
91d81473 | 504 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
505 | goto end; |
506 | } | |
507 | } | |
508 | ||
509 | /* Specific context */ | |
510 | if (ec->spec_context_fc) { | |
511 | field = bt_event_borrow_specific_context_field_const(event); | |
512 | BT_ASSERT(field); | |
513 | ret = write_struct_field(stream, (void *) ec->spec_context_fc, | |
514 | field, true); | |
91d81473 | 515 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
516 | goto end; |
517 | } | |
518 | } | |
519 | ||
520 | /* Specific context */ | |
521 | if (ec->payload_fc) { | |
522 | field = bt_event_borrow_payload_field_const(event); | |
523 | BT_ASSERT(field); | |
524 | ret = write_struct_field(stream, (void *) ec->payload_fc, | |
525 | field, true); | |
91d81473 | 526 | if (G_UNLIKELY(ret)) { |
15fe47e0 PP |
527 | goto end; |
528 | } | |
529 | } | |
530 | ||
531 | end: | |
532 | return ret; | |
533 | } | |
534 | ||
535 | static | |
536 | int write_packet_context(struct fs_sink_stream *stream) | |
537 | { | |
538 | int ret; | |
539 | ||
540 | /* Packet total size */ | |
541 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
542 | stream->packet_state.total_size, 8, 64, BYTE_ORDER); | |
543 | if (ret) { | |
544 | goto end; | |
545 | } | |
546 | ||
547 | /* Packet content size */ | |
548 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
549 | stream->packet_state.content_size, 8, 64, BYTE_ORDER); | |
550 | if (ret) { | |
551 | goto end; | |
552 | } | |
553 | ||
ffb5c13c | 554 | if (stream->sc->packets_have_ts_begin) { |
15fe47e0 PP |
555 | /* Beginning time */ |
556 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
557 | stream->packet_state.beginning_cs, 8, 64, BYTE_ORDER); | |
558 | if (ret) { | |
559 | goto end; | |
560 | } | |
ffb5c13c | 561 | } |
15fe47e0 | 562 | |
ffb5c13c | 563 | if (stream->sc->packets_have_ts_end) { |
15fe47e0 PP |
564 | /* End time */ |
565 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
566 | stream->packet_state.end_cs, 8, 64, BYTE_ORDER); | |
567 | if (ret) { | |
568 | goto end; | |
569 | } | |
570 | } | |
571 | ||
ffb5c13c PP |
572 | if (stream->sc->has_discarded_events) { |
573 | /* Discarded event counter */ | |
574 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
575 | stream->packet_state.discarded_events_counter, 8, 64, | |
576 | BYTE_ORDER); | |
577 | if (ret) { | |
578 | goto end; | |
579 | } | |
15fe47e0 PP |
580 | } |
581 | ||
582 | /* Sequence number */ | |
583 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, | |
584 | stream->packet_state.seq_num, 8, 64, BYTE_ORDER); | |
585 | if (ret) { | |
586 | goto end; | |
587 | } | |
588 | ||
589 | /* Other members */ | |
590 | if (stream->sc->packet_context_fc) { | |
26fc5aed | 591 | const bt_field *packet_context_field; |
15fe47e0 | 592 | |
26fc5aed PP |
593 | BT_ASSERT(stream->packet_state.packet); |
594 | packet_context_field = bt_packet_borrow_context_field_const( | |
595 | stream->packet_state.packet); | |
15fe47e0 PP |
596 | BT_ASSERT(packet_context_field); |
597 | ret = write_struct_field(stream, | |
598 | (void *) stream->sc->packet_context_fc, | |
599 | packet_context_field, false); | |
600 | if (ret) { | |
601 | goto end; | |
602 | } | |
603 | } | |
604 | ||
605 | end: | |
606 | return ret; | |
607 | } | |
608 | ||
609 | BT_HIDDEN | |
610 | int fs_sink_stream_open_packet(struct fs_sink_stream *stream, | |
611 | const bt_clock_snapshot *cs, const bt_packet *packet) | |
612 | { | |
613 | int ret; | |
614 | uint64_t i; | |
615 | ||
616 | BT_ASSERT(!stream->packet_state.is_open); | |
617 | bt_packet_put_ref(stream->packet_state.packet); | |
618 | stream->packet_state.packet = packet; | |
619 | bt_packet_get_ref(stream->packet_state.packet); | |
620 | if (cs) { | |
621 | stream->packet_state.beginning_cs = | |
622 | bt_clock_snapshot_get_value(cs); | |
623 | } | |
624 | ||
625 | /* Open packet */ | |
626 | ret = bt_ctfser_open_packet(&stream->ctfser); | |
627 | if (ret) { | |
628 | /* bt_ctfser_open_packet() logs errors */ | |
629 | goto end; | |
630 | } | |
631 | ||
632 | /* Packet header: magic */ | |
d1f8929c | 633 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 634 | UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER); |
d1f8929c | 635 | if (ret) { |
aa1a7452 | 636 | BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s", |
d1f8929c FD |
637 | stream->file_name->str); |
638 | goto end; | |
639 | } | |
15fe47e0 PP |
640 | |
641 | /* Packet header: UUID */ | |
6162e6b7 | 642 | for (i = 0; i < BT_UUID_LEN; i++) { |
d1f8929c | 643 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
335a2da5 | 644 | (uint64_t) stream->sc->trace->uuid[i], 8, 8, BYTE_ORDER); |
d1f8929c | 645 | if (ret) { |
aa1a7452 | 646 | BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s", |
d1f8929c FD |
647 | stream->file_name->str); |
648 | goto end; | |
649 | } | |
15fe47e0 PP |
650 | } |
651 | ||
652 | /* Packet header: stream class ID */ | |
d1f8929c | 653 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 654 | bt_stream_class_get_id(stream->sc->ir_sc), 8, 64, BYTE_ORDER); |
d1f8929c | 655 | if (ret) { |
aa1a7452 | 656 | BT_COMP_LOGE("Error writing packet header stream class id: " |
d1f8929c FD |
657 | "stream-file-name=%s, stream-class-id=%"PRIu64, |
658 | stream->file_name->str, | |
659 | bt_stream_class_get_id(stream->sc->ir_sc)); | |
660 | goto end; | |
661 | } | |
15fe47e0 PP |
662 | |
663 | /* Packet header: stream ID */ | |
d1f8929c | 664 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
15fe47e0 | 665 | bt_stream_get_id(stream->ir_stream), 8, 64, BYTE_ORDER); |
d1f8929c | 666 | if (ret) { |
aa1a7452 | 667 | BT_COMP_LOGE("Error writing packet header stream id: " |
d1f8929c FD |
668 | "stream-file-name=%s, stream-id=%"PRIu64, |
669 | stream->file_name->str, | |
670 | bt_stream_get_id(stream->ir_stream)); | |
671 | goto end; | |
672 | } | |
15fe47e0 PP |
673 | |
674 | /* Save packet context's offset to rewrite it later */ | |
675 | stream->packet_state.context_offset_bits = | |
676 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
677 | ||
678 | /* Write packet context just to advance to content (first event) */ | |
679 | ret = write_packet_context(stream); | |
680 | if (ret) { | |
681 | goto end; | |
682 | } | |
683 | ||
684 | stream->packet_state.is_open = true; | |
685 | ||
686 | end: | |
687 | return ret; | |
688 | } | |
689 | ||
690 | BT_HIDDEN | |
691 | int fs_sink_stream_close_packet(struct fs_sink_stream *stream, | |
692 | const bt_clock_snapshot *cs) | |
693 | { | |
694 | int ret; | |
695 | ||
696 | BT_ASSERT(stream->packet_state.is_open); | |
697 | ||
698 | if (cs) { | |
699 | stream->packet_state.end_cs = bt_clock_snapshot_get_value(cs); | |
700 | } | |
701 | ||
702 | stream->packet_state.content_size = | |
703 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); | |
704 | stream->packet_state.total_size = | |
705 | (stream->packet_state.content_size + 7) & ~UINT64_C(7); | |
706 | ||
707 | /* Rewrite packet context */ | |
708 | bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser, | |
709 | stream->packet_state.context_offset_bits); | |
710 | ret = write_packet_context(stream); | |
711 | if (ret) { | |
712 | goto end; | |
713 | } | |
714 | ||
715 | /* Close packet */ | |
716 | bt_ctfser_close_current_packet(&stream->ctfser, | |
717 | stream->packet_state.total_size / 8); | |
718 | ||
719 | /* Partially copy current packet state to previous packet state */ | |
720 | stream->prev_packet_state.end_cs = stream->packet_state.end_cs; | |
721 | stream->prev_packet_state.discarded_events_counter = | |
722 | stream->packet_state.discarded_events_counter; | |
723 | stream->prev_packet_state.seq_num = | |
724 | stream->packet_state.seq_num; | |
725 | ||
726 | /* Reset current packet state */ | |
727 | stream->packet_state.beginning_cs = UINT64_C(-1); | |
728 | stream->packet_state.end_cs = UINT64_C(-1); | |
729 | stream->packet_state.content_size = 0; | |
730 | stream->packet_state.total_size = 0; | |
731 | stream->packet_state.seq_num += 1; | |
732 | stream->packet_state.context_offset_bits = 0; | |
733 | stream->packet_state.is_open = false; | |
734 | BT_PACKET_PUT_REF_AND_RESET(stream->packet_state.packet); | |
735 | ||
736 | end: | |
737 | return ret; | |
738 | } |