Add trace packet header accessors and support custom headers
[babeltrace.git] / formats / ctf / ir / stream.c
CommitLineData
273b65be
JG
1/*
2 * stream.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Stream
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
3f043b05 29#include <babeltrace/ctf-ir/clock.h>
adc315b8 30#include <babeltrace/ctf-ir/clock-internal.h>
273b65be 31#include <babeltrace/ctf-writer/event.h>
adc315b8
JG
32#include <babeltrace/ctf-ir/event-internal.h>
33#include <babeltrace/ctf-ir/event-types-internal.h>
34#include <babeltrace/ctf-ir/event-fields-internal.h>
3f043b05
JG
35#include <babeltrace/ctf-ir/stream.h>
36#include <babeltrace/ctf-ir/stream-internal.h>
adc315b8 37#include <babeltrace/ctf-ir/stream-class-internal.h>
273b65be
JG
38#include <babeltrace/ctf-writer/functor-internal.h>
39#include <babeltrace/compiler.h>
40#include <babeltrace/align.h>
d246b111 41#include <babeltrace/ctf/ctf-index.h>
273b65be
JG
42
43static
44void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
45static
273b65be
JG
46int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
47
d246b111
JG
48static
49int set_packet_header_magic(struct bt_ctf_stream *stream)
50{
51 int ret = 0;
52 struct bt_ctf_field_type *magic_field_type = NULL;
53 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
54 stream->packet_header, "magic");
55
56 if (!magic_field) {
57 /* No magic field found. Not an error, skip. */
58 goto end;
59 }
60
61 if (!bt_ctf_field_validate(magic_field)) {
62 /* Value already set. Not an error, skip. */
63 goto end;
64 }
65
66 magic_field_type = bt_ctf_field_get_type(magic_field);
67 assert(magic_field_type);
68
69 if (bt_ctf_field_type_get_type_id(magic_field_type) !=
70 CTF_TYPE_INTEGER) {
71 /* Magic field is not an integer. Not an error, skip. */
72 goto end;
73 }
74
75 if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) {
76 /*
77 * Magic field is not of the expected size.
78 * Not an error, skip.
79 */
80 goto end;
81 }
82
83 ret = bt_ctf_field_type_integer_get_signed(magic_field_type);
84 assert(ret >= 0);
85 if (ret) {
86 ret = bt_ctf_field_signed_integer_set_value(magic_field,
87 (int64_t) 0xC1FC1FC1);
88 } else {
89 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
90 (uint64_t) 0xC1FC1FC1);
91 }
92end:
93 if (magic_field) {
94 bt_ctf_field_put(magic_field);
95 }
96 if (magic_field_type) {
97 bt_ctf_field_type_put(magic_field_type);
98 }
99 return ret;
100}
101
102static
103int set_packet_header_uuid(struct bt_ctf_stream *stream)
104{
105 int i, ret = 0;
106 struct bt_ctf_field_type *uuid_field_type = NULL;
107 struct bt_ctf_field_type *element_field_type = NULL;
108 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field(
109 stream->packet_header, "uuid");
110
111 if (!uuid_field) {
112 /* No uuid field found. Not an error, skip. */
113 goto end;
114 }
115
116 if (!bt_ctf_field_validate(uuid_field)) {
117 /* Value already set. Not an error, skip. */
118 goto end;
119 }
120
121 uuid_field_type = bt_ctf_field_get_type(uuid_field);
122 assert(uuid_field_type);
123 if (bt_ctf_field_type_get_type_id(uuid_field_type) !=
124 CTF_TYPE_ARRAY) {
125 /* UUID field is not an array. Not an error, skip. */
126 goto end;
127 }
128
129 if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) {
130 /*
131 * UUID field is not of the expected size.
132 * Not an error, skip.
133 */
134 goto end;
135 }
136
137 element_field_type = bt_ctf_field_type_array_get_element_type(
138 uuid_field_type);
139 assert(element_field_type);
140 if (bt_ctf_field_type_get_type_id(element_field_type) !=
141 CTF_TYPE_INTEGER) {
142 /* UUID array elements are not integers. Not an error, skip */
143 goto end;
144 }
145
146 for (i = 0; i < 16; i++) {
147 struct bt_ctf_field *uuid_element =
148 bt_ctf_field_array_get_field(uuid_field, i);
149
150 ret = bt_ctf_field_type_integer_get_signed(element_field_type);
151 assert(ret >= 0);
152
153 if (ret) {
154 ret = bt_ctf_field_signed_integer_set_value(
155 uuid_element, (int64_t) stream->trace->uuid[i]);
156 } else {
157 ret = bt_ctf_field_unsigned_integer_set_value(
158 uuid_element,
159 (uint64_t) stream->trace->uuid[i]);
160 }
161 bt_ctf_field_put(uuid_element);
162 if (ret) {
163 goto end;
164 }
165 }
166
167end:
168 if (uuid_field) {
169 bt_ctf_field_put(uuid_field);
170 }
171 if (uuid_field_type) {
172 bt_ctf_field_type_put(uuid_field_type);
173 }
174 if (element_field_type) {
175 bt_ctf_field_type_put(element_field_type);
176 }
177 return ret;
178}
179static
180int set_packet_header_stream_id(struct bt_ctf_stream *stream)
181{
182 int ret = 0;
183 uint32_t stream_id;
184 struct bt_ctf_field_type *stream_id_field_type = NULL;
185 struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field(
186 stream->packet_header, "stream_id");
187
188 if (!stream_id_field) {
189 /* No stream_id field found. Not an error, skip. */
190 goto end;
191 }
192
193 if (!bt_ctf_field_validate(stream_id_field)) {
194 /* Value already set. Not an error, skip. */
195 goto end;
196 }
197
198 stream_id_field_type = bt_ctf_field_get_type(stream_id_field);
199 assert(stream_id_field_type);
200 if (bt_ctf_field_type_get_type_id(stream_id_field_type) !=
201 CTF_TYPE_INTEGER) {
202 /* stream_id field is not an integer. Not an error, skip. */
203 goto end;
204 }
205
206 stream_id = stream->stream_class->id;
207 ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type);
208 assert(ret >= 0);
209 if (ret) {
210 ret = bt_ctf_field_signed_integer_set_value(stream_id_field,
211 (int64_t) stream_id);
212 } else {
213 ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field,
214 (uint64_t) stream_id);
215 }
216end:
217 if (stream_id_field) {
218 bt_ctf_field_put(stream_id_field);
219 }
220 if (stream_id_field_type) {
221 bt_ctf_field_type_put(stream_id_field_type);
222 }
223 return ret;
224}
225
226static
227int set_packet_header(struct bt_ctf_stream *stream)
228{
229 int ret;
230
231 ret = set_packet_header_magic(stream);
232 if (ret) {
233 goto end;
234 }
235
236 ret = set_packet_header_uuid(stream);
237 if (ret) {
238 goto end;
239 }
240
241 ret = set_packet_header_stream_id(stream);
242 if (ret) {
243 goto end;
244 }
245end:
246 return ret;
247}
248
273b65be
JG
249BT_HIDDEN
250struct bt_ctf_stream *bt_ctf_stream_create(
d246b111
JG
251 struct bt_ctf_stream_class *stream_class,
252 struct bt_ctf_trace *trace)
273b65be 253{
12c8a1a3 254 int ret;
273b65be
JG
255 struct bt_ctf_stream *stream = NULL;
256
d246b111 257 if (!stream_class || !trace) {
273b65be
JG
258 goto end;
259 }
260
261 stream = g_new0(struct bt_ctf_stream, 1);
262 if (!stream) {
263 goto end;
264 }
265
d246b111 266 stream->trace = trace;
273b65be 267 bt_ctf_ref_init(&stream->ref_count);
12c8a1a3
JG
268 stream->packet_context = bt_ctf_field_create(
269 stream_class->packet_context_type);
270 if (!stream->packet_context) {
271 goto error_destroy;
272 }
273
af181248
JG
274 /*
275 * A stream class may not have a stream event context defined
276 * in which case this stream will never have a stream_event_context
277 * member since, after a stream's creation, the parent stream class
278 * is "frozen" (immutable).
279 */
280 if (stream_class->event_context_type) {
281 stream->event_context = bt_ctf_field_create(
282 stream_class->event_context_type);
283 if (!stream->packet_context) {
284 goto error_destroy;
285 }
286 }
287
e19cc57d 288 /* Initialize events_discarded */
12c8a1a3
JG
289 ret = set_structure_field_integer(stream->packet_context,
290 "events_discarded", 0);
291 if (ret) {
292 goto error_destroy;
293 }
294
273b65be
JG
295 stream->pos.fd = -1;
296 stream->id = stream_class->next_stream_id++;
297 stream->stream_class = stream_class;
298 bt_ctf_stream_class_get(stream_class);
299 bt_ctf_stream_class_freeze(stream_class);
300 stream->events = g_ptr_array_new_with_free_func(
8bfa3f9c
JG
301 (GDestroyNotify) bt_ctf_event_put);
302 if (!stream->events) {
303 goto error_destroy;
304 }
305 if (stream_class->event_context_type) {
306 stream->event_contexts = g_ptr_array_new_with_free_func(
307 (GDestroyNotify) bt_ctf_field_put);
308 if (!stream->event_contexts) {
309 goto error_destroy;
310 }
311 }
d246b111
JG
312
313 /* A trace is not allowed to have a NULL packet header */
314 assert(trace->packet_header_type);
315 stream->packet_header = bt_ctf_field_create(trace->packet_header_type);
316 /*
317 * Attempt to populate the default trace packet header fields
318 * (magic, uuid and stream_id). This will _not_ fail shall the
319 * fields not be found or be of an incompatible type; they will
320 * simply not be populated automatically. The user will have to
321 * make sure to set the trace packet header fields himself before
322 * flushing.
323 */
324 ret = set_packet_header(stream);
325 if (ret) {
326 goto error_destroy;
327 }
273b65be
JG
328end:
329 return stream;
12c8a1a3
JG
330error_destroy:
331 bt_ctf_stream_destroy(&stream->ref_count);
332 return NULL;
273b65be
JG
333}
334
273b65be
JG
335BT_HIDDEN
336int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
337{
338 int ret = 0;
339
340 if (stream->pos.fd != -1) {
341 ret = -1;
342 goto end;
343 }
344
345 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
346 stream->pos.fd = fd;
347end:
348 return ret;
349}
350
d246b111
JG
351BT_HIDDEN
352void bt_ctf_stream_set_trace(struct bt_ctf_stream *stream,
353 struct bt_ctf_trace *trace)
354{
355 stream->trace = trace;
356}
357
a78a2e25
JG
358int bt_ctf_stream_get_discarded_events_count(
359 struct bt_ctf_stream *stream, uint64_t *count)
360{
361 int64_t ret = 0;
12c8a1a3
JG
362 int field_signed;
363 struct bt_ctf_field *events_discarded_field = NULL;
364 struct bt_ctf_field_type *events_discarded_field_type = NULL;
a78a2e25 365
12c8a1a3 366 if (!stream || !count || !stream->packet_context) {
a78a2e25
JG
367 ret = -1;
368 goto end;
369 }
370
12c8a1a3
JG
371 events_discarded_field = bt_ctf_field_structure_get_field(
372 stream->packet_context, "events_discarded");
373 if (!events_discarded_field) {
374 ret = -1;
375 goto end;
376 }
377
378 events_discarded_field_type = bt_ctf_field_get_type(
379 events_discarded_field);
380 if (!events_discarded_field_type) {
381 ret = -1;
382 goto end;
383 }
384
385 field_signed = bt_ctf_field_type_integer_get_signed(
386 events_discarded_field_type);
387 if (field_signed < 0) {
388 ret = field_signed;
389 goto end;
390 }
391
392 if (field_signed) {
393 int64_t signed_count;
394
395 ret = bt_ctf_field_signed_integer_get_value(
396 events_discarded_field, &signed_count);
397 if (ret) {
398 goto end;
399 }
400 if (signed_count < 0) {
401 /* Invalid value */
402 ret = -1;
403 goto end;
404 }
405 *count = (uint64_t) signed_count;
406 } else {
407 ret = bt_ctf_field_unsigned_integer_get_value(
408 events_discarded_field, count);
409 if (ret) {
410 goto end;
411 }
412 }
a78a2e25 413end:
12c8a1a3
JG
414 if (events_discarded_field) {
415 bt_ctf_field_put(events_discarded_field);
416 }
417 if (events_discarded_field_type) {
418 bt_ctf_field_type_put(events_discarded_field_type);
419 }
a78a2e25
JG
420 return ret;
421}
422
273b65be
JG
423void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
424 uint64_t event_count)
425{
12c8a1a3
JG
426 int ret;
427 int field_signed;
428 uint64_t previous_count;
429 uint64_t new_count;
430 struct bt_ctf_field *events_discarded_field = NULL;
431 struct bt_ctf_field_type *events_discarded_field_type = NULL;
432
433 if (!stream || !stream->packet_context) {
434 goto end;
435 }
436
437 ret = bt_ctf_stream_get_discarded_events_count(stream,
438 &previous_count);
439 if (ret) {
440 goto end;
441 }
442
443 events_discarded_field = bt_ctf_field_structure_get_field(
444 stream->packet_context, "events_discarded");
445 if (!events_discarded_field) {
446 goto end;
447 }
448
449 events_discarded_field_type = bt_ctf_field_get_type(
450 events_discarded_field);
451 if (!events_discarded_field_type) {
452 goto end;
453 }
454
455 field_signed = bt_ctf_field_type_integer_get_signed(
456 events_discarded_field_type);
457 if (field_signed < 0) {
458 goto end;
273b65be
JG
459 }
460
12c8a1a3
JG
461 new_count = previous_count + event_count;
462 if (field_signed) {
463 ret = bt_ctf_field_signed_integer_set_value(
464 events_discarded_field, (int64_t) new_count);
465 if (ret) {
466 goto end;
467 }
468 } else {
469 ret = bt_ctf_field_unsigned_integer_set_value(
470 events_discarded_field, new_count);
471 if (ret) {
472 goto end;
473 }
474 }
475
476end:
477 if (events_discarded_field) {
478 bt_ctf_field_put(events_discarded_field);
479 }
480 if (events_discarded_field_type) {
481 bt_ctf_field_type_put(events_discarded_field_type);
482 }
273b65be
JG
483}
484
485int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
486 struct bt_ctf_event *event)
487{
488 int ret = 0;
489 uint64_t timestamp;
af181248 490 struct bt_ctf_field *event_context_copy = NULL;
273b65be
JG
491
492 if (!stream || !event) {
493 ret = -1;
494 goto end;
495 }
496
785b5390 497 /* Make sure the event's payload is set */
273b65be
JG
498 ret = bt_ctf_event_validate(event);
499 if (ret) {
500 goto end;
501 }
502
8bfa3f9c
JG
503 /* Sample the current stream event context by copying it */
504 if (stream->event_context) {
505 /* Make sure the event context's payload is set */
506 ret = bt_ctf_field_validate(stream->event_context);
507 if (ret) {
508 goto end;
509 }
510
511 event_context_copy = bt_ctf_field_copy(stream->event_context);
512 if (!event_context_copy) {
513 ret = -1;
514 goto end;
515 }
516 }
517
273b65be
JG
518 timestamp = bt_ctf_clock_get_time(stream->stream_class->clock);
519 ret = bt_ctf_event_set_timestamp(event, timestamp);
520 if (ret) {
521 goto end;
522 }
523
524 bt_ctf_event_get(event);
8bfa3f9c 525 /* Save the new event along with its associated stream event context */
273b65be 526 g_ptr_array_add(stream->events, event);
8bfa3f9c
JG
527 if (event_context_copy) {
528 g_ptr_array_add(stream->event_contexts, event_context_copy);
529 }
273b65be
JG
530end:
531 return ret;
532}
533
12c8a1a3
JG
534struct bt_ctf_field *bt_ctf_stream_get_packet_context(
535 struct bt_ctf_stream *stream)
536{
537 struct bt_ctf_field *packet_context = NULL;
538
539 if (!stream) {
540 goto end;
541 }
542
543 packet_context = stream->packet_context;
12c8a1a3
JG
544 if (packet_context) {
545 bt_ctf_field_get(packet_context);
546 }
34629a55 547end:
12c8a1a3
JG
548 return packet_context;
549}
550
551int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
552 struct bt_ctf_field *field)
553{
554 int ret = 0;
555 struct bt_ctf_field_type *field_type;
556
557 if (!stream || !field) {
558 ret = -1;
559 goto end;
560 }
561
562 field_type = bt_ctf_field_get_type(field);
563 if (field_type != stream->stream_class->packet_context_type) {
564 ret = -1;
565 goto end;
566 }
567
568 bt_ctf_field_type_put(field_type);
569 bt_ctf_field_get(field);
570 bt_ctf_field_put(stream->packet_context);
571 stream->packet_context = field;
572end:
573 return ret;
574}
575
8bfa3f9c
JG
576struct bt_ctf_field *bt_ctf_stream_get_event_context(
577 struct bt_ctf_stream *stream)
578{
579 struct bt_ctf_field *event_context = NULL;
580
581 if (!stream) {
582 goto end;
583 }
584
585 event_context = stream->event_context;
586 if (event_context) {
587 bt_ctf_field_get(event_context);
588 }
589end:
590 return event_context;
591}
592
593int bt_ctf_stream_set_event_context(struct bt_ctf_stream *stream,
594 struct bt_ctf_field *field)
595{
596 int ret = 0;
597 struct bt_ctf_field_type *field_type = NULL;
598
599 if (!stream || !field) {
600 ret = -1;
601 goto end;
602 }
603
604 field_type = bt_ctf_field_get_type(field);
605 if (field_type != stream->stream_class->event_context_type) {
606 ret = -1;
607 goto end;
608 }
609
610 bt_ctf_field_get(field);
611 bt_ctf_field_put(stream->event_context);
612 stream->event_context = field;
613end:
614 if (field_type) {
615 bt_ctf_field_type_put(field_type);
616 }
617 return ret;
618}
619
273b65be
JG
620int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
621{
622 int ret = 0;
623 size_t i;
12c8a1a3 624 uint64_t timestamp_begin, timestamp_end, events_discarded;
9f56e450 625 struct bt_ctf_stream_class *stream_class;
273b65be
JG
626 struct bt_ctf_field *integer = NULL;
627 struct ctf_stream_pos packet_context_pos;
628
bc37ae52
JG
629 if (!stream || stream->pos.fd < 0) {
630 /*
631 * Stream does not have an associated fd. It is,
632 * therefore, not a stream being used to write events.
633 */
273b65be
JG
634 ret = -1;
635 goto end;
636 }
637
638 if (!stream->events->len) {
639 goto end;
640 }
641
d246b111
JG
642 ret = bt_ctf_field_validate(stream->packet_header);
643 if (ret) {
644 goto end;
645 }
646
647 if (stream->flushed_packet_count) {
648 /* ctf_init_pos has already initialized the first packet */
649 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
650 }
651
652 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
653 if (ret) {
654 goto end;
273b65be
JG
655 }
656
9f56e450 657 stream_class = stream->stream_class;
273b65be
JG
658 timestamp_begin = ((struct bt_ctf_event *) g_ptr_array_index(
659 stream->events, 0))->timestamp;
660 timestamp_end = ((struct bt_ctf_event *) g_ptr_array_index(
661 stream->events, stream->events->len - 1))->timestamp;
12c8a1a3
JG
662
663 /* Set the default context attributes if present and unset. */
664 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
665 "timestamp_begin", timestamp_begin);
666 if (ret) {
667 goto end;
668 }
669
12c8a1a3 670 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
671 "timestamp_end", timestamp_end);
672 if (ret) {
673 goto end;
674 }
675
12c8a1a3 676 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
677 "content_size", UINT64_MAX);
678 if (ret) {
679 goto end;
680 }
681
12c8a1a3 682 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
683 "packet_size", UINT64_MAX);
684 if (ret) {
685 goto end;
686 }
687
688 /* Write packet context */
689 memcpy(&packet_context_pos, &stream->pos,
690 sizeof(struct ctf_stream_pos));
12c8a1a3 691 ret = bt_ctf_field_serialize(stream->packet_context,
273b65be
JG
692 &stream->pos);
693 if (ret) {
694 goto end;
695 }
696
12c8a1a3
JG
697 ret = bt_ctf_stream_get_discarded_events_count(stream,
698 &events_discarded);
699 if (ret) {
700 goto end;
701 }
702
703 /* Unset the packet context's fields. */
704 ret = bt_ctf_field_reset(stream->packet_context);
705 if (ret) {
706 goto end;
707 }
708
709 /* Set the previous number of discarded events. */
710 ret = set_structure_field_integer(stream->packet_context,
711 "events_discarded", events_discarded);
712 if (ret) {
713 goto end;
714 }
715
273b65be
JG
716 for (i = 0; i < stream->events->len; i++) {
717 struct bt_ctf_event *event = g_ptr_array_index(
718 stream->events, i);
719 uint32_t event_id = bt_ctf_event_class_get_id(
720 event->event_class);
721 uint64_t timestamp = bt_ctf_event_get_timestamp(event);
722
12c8a1a3
JG
723 ret = bt_ctf_field_reset(stream_class->event_header);
724 if (ret) {
725 goto end;
726 }
727
273b65be
JG
728 ret = set_structure_field_integer(stream_class->event_header,
729 "id", event_id);
730 if (ret) {
731 goto end;
732 }
733 ret = set_structure_field_integer(stream_class->event_header,
734 "timestamp", timestamp);
735 if (ret) {
736 goto end;
737 }
738
739 /* Write event header */
740 ret = bt_ctf_field_serialize(stream_class->event_header,
741 &stream->pos);
742 if (ret) {
743 goto end;
744 }
745
8bfa3f9c
JG
746 /* Write stream event context */
747 if (stream->event_contexts) {
748 ret = bt_ctf_field_serialize(
749 g_ptr_array_index(stream->event_contexts, i),
750 &stream->pos);
751 if (ret) {
752 goto end;
753 }
754 }
755
273b65be
JG
756 /* Write event content */
757 ret = bt_ctf_event_serialize(event, &stream->pos);
758 if (ret) {
759 goto end;
760 }
761 }
762
763 /*
764 * Update the packet total size and content size and overwrite the
765 * packet context.
3a092c05
JG
766 * Copy base_mma as the packet may have been remapped (e.g. when a
767 * packet is resized).
273b65be 768 */
3a092c05 769 packet_context_pos.base_mma = stream->pos.base_mma;
12c8a1a3 770 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
771 "content_size", stream->pos.offset);
772 if (ret) {
773 goto end;
774 }
775
12c8a1a3 776 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
777 "packet_size", stream->pos.packet_size);
778 if (ret) {
779 goto end;
780 }
781
12c8a1a3 782 ret = bt_ctf_field_serialize(stream->packet_context,
273b65be
JG
783 &packet_context_pos);
784 if (ret) {
785 goto end;
786 }
787
788 g_ptr_array_set_size(stream->events, 0);
8bfa3f9c
JG
789 if (stream->event_contexts) {
790 g_ptr_array_set_size(stream->event_contexts, 0);
791 }
273b65be
JG
792 stream->flushed_packet_count++;
793end:
794 bt_ctf_field_put(integer);
795 return ret;
796}
797
798void bt_ctf_stream_get(struct bt_ctf_stream *stream)
799{
800 if (!stream) {
801 return;
802 }
803
804 bt_ctf_ref_get(&stream->ref_count);
805}
806
807void bt_ctf_stream_put(struct bt_ctf_stream *stream)
808{
809 if (!stream) {
810 return;
811 }
812
813 bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
814}
815
816static
817void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
818{
819 struct bt_ctf_stream *stream;
820
821 if (!ref) {
822 return;
823 }
824
825 stream = container_of(ref, struct bt_ctf_stream, ref_count);
826 ctf_fini_pos(&stream->pos);
9f56e450
JG
827 if (close(stream->pos.fd)) {
828 perror("close");
829 }
12c8a1a3
JG
830
831 if (stream->stream_class) {
832 bt_ctf_stream_class_put(stream->stream_class);
833 }
834 if (stream->events) {
835 g_ptr_array_free(stream->events, TRUE);
836 }
8bfa3f9c
JG
837 if (stream->event_contexts) {
838 g_ptr_array_free(stream->event_contexts, TRUE);
839 }
d246b111
JG
840 if (stream->packet_header) {
841 bt_ctf_field_put(stream->packet_header);
842 }
12c8a1a3
JG
843 if (stream->packet_context) {
844 bt_ctf_field_put(stream->packet_context);
845 }
8bfa3f9c
JG
846 if (stream->event_context) {
847 bt_ctf_field_put(stream->event_context);
848 }
273b65be
JG
849 g_free(stream);
850}
851
273b65be
JG
852static
853int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
854 uint64_t value)
855{
856 int ret = 0;
d7b1ea66 857 struct bt_ctf_field_type *field_type = NULL;
273b65be
JG
858 struct bt_ctf_field *integer =
859 bt_ctf_field_structure_get_field(structure, name);
12c8a1a3
JG
860
861 if (!structure || !name) {
273b65be
JG
862 ret = -1;
863 goto end;
864 }
865
12c8a1a3
JG
866 if (!integer) {
867 /* Field not found, not an error. */
868 goto end;
869 }
870
871 /* Make sure the payload has not already been set. */
872 if (!bt_ctf_field_validate(integer)) {
873 /* Payload already set, not an error */
874 goto end;
875 }
876
d7b1ea66
JG
877 field_type = bt_ctf_field_get_type(integer);
878 /* Something is serioulsly wrong */
879 assert(field_type);
880 if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
881 /*
882 * The user most likely meant for us to populate this field
883 * automatically. However, we can only do this if the field
884 * is an integer. Return an error.
885 */
886 ret = -1;
887 goto end;
888 }
889
890 if (bt_ctf_field_type_integer_get_signed(field_type)) {
891 ret = bt_ctf_field_signed_integer_set_value(integer,
892 (int64_t) value);
893 } else {
894 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
895 }
273b65be 896end:
d7b1ea66
JG
897 if (integer) {
898 bt_ctf_field_put(integer);
899 }
900 if (field_type) {
901 bt_ctf_field_type_put(field_type);
902 }
273b65be
JG
903 return ret;
904}
This page took 0.06162 seconds and 4 git commands to generate.