Add event header accessors and support for custom event headers
[babeltrace.git] / formats / ctf / ir / stream.c
1 /*
2 * stream.c
3 *
4 * Babeltrace CTF IR - Stream
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #include <babeltrace/ctf-ir/clock.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
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>
35 #include <babeltrace/ctf-ir/stream.h>
36 #include <babeltrace/ctf-ir/stream-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-writer/functor-internal.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/align.h>
41 #include <babeltrace/ctf/ctf-index.h>
42
43 static
44 void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
45 static
46 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
47
48 static
49 int 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 }
92 end:
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
102 static
103 int 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
167 end:
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 }
179 static
180 int 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 }
216 end:
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
226 static
227 int 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 }
245 end:
246 return ret;
247 }
248
249 BT_HIDDEN
250 struct bt_ctf_stream *bt_ctf_stream_create(
251 struct bt_ctf_stream_class *stream_class,
252 struct bt_ctf_trace *trace)
253 {
254 int ret;
255 struct bt_ctf_stream *stream = NULL;
256
257 if (!stream_class || !trace) {
258 goto end;
259 }
260
261 stream = g_new0(struct bt_ctf_stream, 1);
262 if (!stream) {
263 goto end;
264 }
265
266 stream->trace = trace;
267 bt_ctf_ref_init(&stream->ref_count);
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
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
288 /* Initialize events_discarded */
289 ret = set_structure_field_integer(stream->packet_context,
290 "events_discarded", 0);
291 if (ret) {
292 goto error_destroy;
293 }
294
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(
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 }
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 }
328 end:
329 return stream;
330 error_destroy:
331 bt_ctf_stream_destroy(&stream->ref_count);
332 return NULL;
333 }
334
335 BT_HIDDEN
336 int 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;
347 end:
348 return ret;
349 }
350
351 BT_HIDDEN
352 void bt_ctf_stream_set_trace(struct bt_ctf_stream *stream,
353 struct bt_ctf_trace *trace)
354 {
355 stream->trace = trace;
356 }
357
358 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
359 struct bt_ctf_stream *stream)
360 {
361 struct bt_ctf_stream_class *stream_class = NULL;
362
363 if (!stream) {
364 goto end;
365 }
366
367 stream_class = stream->stream_class;
368 bt_ctf_stream_class_get(stream_class);
369 end:
370 return stream_class;
371 }
372
373 int bt_ctf_stream_get_discarded_events_count(
374 struct bt_ctf_stream *stream, uint64_t *count)
375 {
376 int64_t ret = 0;
377 int field_signed;
378 struct bt_ctf_field *events_discarded_field = NULL;
379 struct bt_ctf_field_type *events_discarded_field_type = NULL;
380
381 if (!stream || !count || !stream->packet_context) {
382 ret = -1;
383 goto end;
384 }
385
386 events_discarded_field = bt_ctf_field_structure_get_field(
387 stream->packet_context, "events_discarded");
388 if (!events_discarded_field) {
389 ret = -1;
390 goto end;
391 }
392
393 events_discarded_field_type = bt_ctf_field_get_type(
394 events_discarded_field);
395 if (!events_discarded_field_type) {
396 ret = -1;
397 goto end;
398 }
399
400 field_signed = bt_ctf_field_type_integer_get_signed(
401 events_discarded_field_type);
402 if (field_signed < 0) {
403 ret = field_signed;
404 goto end;
405 }
406
407 if (field_signed) {
408 int64_t signed_count;
409
410 ret = bt_ctf_field_signed_integer_get_value(
411 events_discarded_field, &signed_count);
412 if (ret) {
413 goto end;
414 }
415 if (signed_count < 0) {
416 /* Invalid value */
417 ret = -1;
418 goto end;
419 }
420 *count = (uint64_t) signed_count;
421 } else {
422 ret = bt_ctf_field_unsigned_integer_get_value(
423 events_discarded_field, count);
424 if (ret) {
425 goto end;
426 }
427 }
428 end:
429 if (events_discarded_field) {
430 bt_ctf_field_put(events_discarded_field);
431 }
432 if (events_discarded_field_type) {
433 bt_ctf_field_type_put(events_discarded_field_type);
434 }
435 return ret;
436 }
437
438 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
439 uint64_t event_count)
440 {
441 int ret;
442 int field_signed;
443 uint64_t previous_count;
444 uint64_t new_count;
445 struct bt_ctf_field *events_discarded_field = NULL;
446 struct bt_ctf_field_type *events_discarded_field_type = NULL;
447
448 if (!stream || !stream->packet_context) {
449 goto end;
450 }
451
452 ret = bt_ctf_stream_get_discarded_events_count(stream,
453 &previous_count);
454 if (ret) {
455 goto end;
456 }
457
458 events_discarded_field = bt_ctf_field_structure_get_field(
459 stream->packet_context, "events_discarded");
460 if (!events_discarded_field) {
461 goto end;
462 }
463
464 events_discarded_field_type = bt_ctf_field_get_type(
465 events_discarded_field);
466 if (!events_discarded_field_type) {
467 goto end;
468 }
469
470 field_signed = bt_ctf_field_type_integer_get_signed(
471 events_discarded_field_type);
472 if (field_signed < 0) {
473 goto end;
474 }
475
476 new_count = previous_count + event_count;
477 if (field_signed) {
478 ret = bt_ctf_field_signed_integer_set_value(
479 events_discarded_field, (int64_t) new_count);
480 if (ret) {
481 goto end;
482 }
483 } else {
484 ret = bt_ctf_field_unsigned_integer_set_value(
485 events_discarded_field, new_count);
486 if (ret) {
487 goto end;
488 }
489 }
490
491 end:
492 if (events_discarded_field) {
493 bt_ctf_field_put(events_discarded_field);
494 }
495 if (events_discarded_field_type) {
496 bt_ctf_field_type_put(events_discarded_field_type);
497 }
498 }
499
500 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
501 struct bt_ctf_event *event)
502 {
503 int ret = 0;
504 uint64_t timestamp;
505 struct bt_ctf_field *event_context_copy = NULL;
506
507 if (!stream || !event) {
508 ret = -1;
509 goto end;
510 }
511
512 ret = bt_ctf_event_populate_event_header(event);
513 if (ret) {
514 goto end;
515 }
516
517 /* Make sure the event's payload is set */
518 ret = bt_ctf_event_validate(event);
519 if (ret) {
520 goto end;
521 }
522
523 /* Sample the current stream event context by copying it */
524 if (stream->event_context) {
525 /* Make sure the event context's payload is set */
526 ret = bt_ctf_field_validate(stream->event_context);
527 if (ret) {
528 goto end;
529 }
530
531 event_context_copy = bt_ctf_field_copy(stream->event_context);
532 if (!event_context_copy) {
533 ret = -1;
534 goto end;
535 }
536 }
537
538 timestamp = bt_ctf_clock_get_time(stream->stream_class->clock);
539 ret = bt_ctf_event_set_timestamp(event, timestamp);
540 if (ret) {
541 goto end;
542 }
543
544 bt_ctf_event_get(event);
545 /* Save the new event along with its associated stream event context */
546 g_ptr_array_add(stream->events, event);
547 if (event_context_copy) {
548 g_ptr_array_add(stream->event_contexts, event_context_copy);
549 }
550 end:
551 return ret;
552 }
553
554 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
555 struct bt_ctf_stream *stream)
556 {
557 struct bt_ctf_field *packet_context = NULL;
558
559 if (!stream) {
560 goto end;
561 }
562
563 packet_context = stream->packet_context;
564 if (packet_context) {
565 bt_ctf_field_get(packet_context);
566 }
567 end:
568 return packet_context;
569 }
570
571 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
572 struct bt_ctf_field *field)
573 {
574 int ret = 0;
575 struct bt_ctf_field_type *field_type;
576
577 if (!stream || !field) {
578 ret = -1;
579 goto end;
580 }
581
582 field_type = bt_ctf_field_get_type(field);
583 if (field_type != stream->stream_class->packet_context_type) {
584 ret = -1;
585 goto end;
586 }
587
588 bt_ctf_field_type_put(field_type);
589 bt_ctf_field_get(field);
590 bt_ctf_field_put(stream->packet_context);
591 stream->packet_context = field;
592 end:
593 return ret;
594 }
595
596 struct bt_ctf_field *bt_ctf_stream_get_event_context(
597 struct bt_ctf_stream *stream)
598 {
599 struct bt_ctf_field *event_context = NULL;
600
601 if (!stream) {
602 goto end;
603 }
604
605 event_context = stream->event_context;
606 if (event_context) {
607 bt_ctf_field_get(event_context);
608 }
609 end:
610 return event_context;
611 }
612
613 int bt_ctf_stream_set_event_context(struct bt_ctf_stream *stream,
614 struct bt_ctf_field *field)
615 {
616 int ret = 0;
617 struct bt_ctf_field_type *field_type = NULL;
618
619 if (!stream || !field) {
620 ret = -1;
621 goto end;
622 }
623
624 field_type = bt_ctf_field_get_type(field);
625 if (field_type != stream->stream_class->event_context_type) {
626 ret = -1;
627 goto end;
628 }
629
630 bt_ctf_field_get(field);
631 bt_ctf_field_put(stream->event_context);
632 stream->event_context = field;
633 end:
634 if (field_type) {
635 bt_ctf_field_type_put(field_type);
636 }
637 return ret;
638 }
639
640 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
641 struct bt_ctf_stream *stream)
642 {
643 struct bt_ctf_field *packet_header = NULL;
644
645 if (!stream) {
646 goto end;
647 }
648
649 packet_header = stream->packet_header;
650 if (packet_header) {
651 bt_ctf_field_get(packet_header);
652 }
653 end:
654 return packet_header;
655 }
656
657 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
658 struct bt_ctf_field *field)
659 {
660 int ret = 0;
661 struct bt_ctf_field_type *field_type = NULL;
662
663 if (!stream || !field) {
664 ret = -1;
665 goto end;
666 }
667
668 field_type = bt_ctf_field_get_type(field);
669 if (field_type != stream->trace->packet_header_type) {
670 ret = -1;
671 goto end;
672 }
673
674 bt_ctf_field_get(field);
675 bt_ctf_field_put(stream->packet_header);
676 stream->packet_header = field;
677 end:
678 if (field_type) {
679 bt_ctf_field_type_put(field_type);
680 }
681 return ret;
682 }
683
684 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
685 {
686 int ret = 0;
687 size_t i;
688 uint64_t timestamp_begin, timestamp_end, events_discarded;
689 struct bt_ctf_field *integer = NULL;
690 struct ctf_stream_pos packet_context_pos;
691
692 if (!stream || stream->pos.fd < 0) {
693 /*
694 * Stream does not have an associated fd. It is,
695 * therefore, not a stream being used to write events.
696 */
697 ret = -1;
698 goto end;
699 }
700
701 if (!stream->events->len) {
702 goto end;
703 }
704
705 ret = bt_ctf_field_validate(stream->packet_header);
706 if (ret) {
707 goto end;
708 }
709
710 if (stream->flushed_packet_count) {
711 /* ctf_init_pos has already initialized the first packet */
712 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
713 }
714
715 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
716 if (ret) {
717 goto end;
718 }
719
720 timestamp_begin = ((struct bt_ctf_event *) g_ptr_array_index(
721 stream->events, 0))->timestamp;
722 timestamp_end = ((struct bt_ctf_event *) g_ptr_array_index(
723 stream->events, stream->events->len - 1))->timestamp;
724
725 /* Set the default context attributes if present and unset. */
726 ret = set_structure_field_integer(stream->packet_context,
727 "timestamp_begin", timestamp_begin);
728 if (ret) {
729 goto end;
730 }
731
732 ret = set_structure_field_integer(stream->packet_context,
733 "timestamp_end", timestamp_end);
734 if (ret) {
735 goto end;
736 }
737
738 ret = set_structure_field_integer(stream->packet_context,
739 "content_size", UINT64_MAX);
740 if (ret) {
741 goto end;
742 }
743
744 ret = set_structure_field_integer(stream->packet_context,
745 "packet_size", UINT64_MAX);
746 if (ret) {
747 goto end;
748 }
749
750 /* Write packet context */
751 memcpy(&packet_context_pos, &stream->pos,
752 sizeof(struct ctf_stream_pos));
753 ret = bt_ctf_field_serialize(stream->packet_context,
754 &stream->pos);
755 if (ret) {
756 goto end;
757 }
758
759 ret = bt_ctf_stream_get_discarded_events_count(stream,
760 &events_discarded);
761 if (ret) {
762 goto end;
763 }
764
765 /* Unset the packet context's fields. */
766 ret = bt_ctf_field_reset(stream->packet_context);
767 if (ret) {
768 goto end;
769 }
770
771 /* Set the previous number of discarded events. */
772 ret = set_structure_field_integer(stream->packet_context,
773 "events_discarded", events_discarded);
774 if (ret) {
775 goto end;
776 }
777
778 for (i = 0; i < stream->events->len; i++) {
779 struct bt_ctf_event *event = g_ptr_array_index(
780 stream->events, i);
781 uint32_t event_id = bt_ctf_event_class_get_id(
782 event->event_class);
783 uint64_t timestamp = bt_ctf_event_get_timestamp(event);
784
785 ret = bt_ctf_field_reset(event->event_header);
786 if (ret) {
787 goto end;
788 }
789
790 ret = set_structure_field_integer(event->event_header,
791 "id", event_id);
792 if (ret) {
793 goto end;
794 }
795 ret = set_structure_field_integer(event->event_header,
796 "timestamp", timestamp);
797 if (ret) {
798 goto end;
799 }
800
801 /* Write event header */
802 ret = bt_ctf_field_serialize(event->event_header,
803 &stream->pos);
804 if (ret) {
805 goto end;
806 }
807
808 /* Write stream event context */
809 if (stream->event_contexts) {
810 ret = bt_ctf_field_serialize(
811 g_ptr_array_index(stream->event_contexts, i),
812 &stream->pos);
813 if (ret) {
814 goto end;
815 }
816 }
817
818 /* Write event content */
819 ret = bt_ctf_event_serialize(event, &stream->pos);
820 if (ret) {
821 goto end;
822 }
823 }
824
825 /*
826 * Update the packet total size and content size and overwrite the
827 * packet context.
828 * Copy base_mma as the packet may have been remapped (e.g. when a
829 * packet is resized).
830 */
831 packet_context_pos.base_mma = stream->pos.base_mma;
832 ret = set_structure_field_integer(stream->packet_context,
833 "content_size", stream->pos.offset);
834 if (ret) {
835 goto end;
836 }
837
838 ret = set_structure_field_integer(stream->packet_context,
839 "packet_size", stream->pos.packet_size);
840 if (ret) {
841 goto end;
842 }
843
844 ret = bt_ctf_field_serialize(stream->packet_context,
845 &packet_context_pos);
846 if (ret) {
847 goto end;
848 }
849
850 g_ptr_array_set_size(stream->events, 0);
851 if (stream->event_contexts) {
852 g_ptr_array_set_size(stream->event_contexts, 0);
853 }
854 stream->flushed_packet_count++;
855 end:
856 bt_ctf_field_put(integer);
857 return ret;
858 }
859
860 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
861 {
862 if (!stream) {
863 return;
864 }
865
866 bt_ctf_ref_get(&stream->ref_count);
867 }
868
869 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
870 {
871 if (!stream) {
872 return;
873 }
874
875 bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
876 }
877
878 static
879 void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
880 {
881 struct bt_ctf_stream *stream;
882
883 if (!ref) {
884 return;
885 }
886
887 stream = container_of(ref, struct bt_ctf_stream, ref_count);
888 ctf_fini_pos(&stream->pos);
889 if (close(stream->pos.fd)) {
890 perror("close");
891 }
892
893 if (stream->stream_class) {
894 bt_ctf_stream_class_put(stream->stream_class);
895 }
896 if (stream->events) {
897 g_ptr_array_free(stream->events, TRUE);
898 }
899 if (stream->event_contexts) {
900 g_ptr_array_free(stream->event_contexts, TRUE);
901 }
902 if (stream->packet_header) {
903 bt_ctf_field_put(stream->packet_header);
904 }
905 if (stream->packet_context) {
906 bt_ctf_field_put(stream->packet_context);
907 }
908 if (stream->event_context) {
909 bt_ctf_field_put(stream->event_context);
910 }
911 g_free(stream);
912 }
913
914 static
915 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
916 uint64_t value)
917 {
918 int ret = 0;
919 struct bt_ctf_field_type *field_type = NULL;
920 struct bt_ctf_field *integer =
921 bt_ctf_field_structure_get_field(structure, name);
922
923 if (!structure || !name) {
924 ret = -1;
925 goto end;
926 }
927
928 if (!integer) {
929 /* Field not found, not an error. */
930 goto end;
931 }
932
933 /* Make sure the payload has not already been set. */
934 if (!bt_ctf_field_validate(integer)) {
935 /* Payload already set, not an error */
936 goto end;
937 }
938
939 field_type = bt_ctf_field_get_type(integer);
940 /* Something is serioulsly wrong */
941 assert(field_type);
942 if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
943 /*
944 * The user most likely meant for us to populate this field
945 * automatically. However, we can only do this if the field
946 * is an integer. Return an error.
947 */
948 ret = -1;
949 goto end;
950 }
951
952 if (bt_ctf_field_type_integer_get_signed(field_type)) {
953 ret = bt_ctf_field_signed_integer_set_value(integer,
954 (int64_t) value);
955 } else {
956 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
957 }
958 end:
959 if (integer) {
960 bt_ctf_field_put(integer);
961 }
962 if (field_type) {
963 bt_ctf_field_type_put(field_type);
964 }
965 return ret;
966 }
This page took 0.04839 seconds and 4 git commands to generate.