ir: add weak reference to parent stream to bt_ctf_event
[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 static
250 void put_event(struct bt_ctf_event *event)
251 {
252 bt_ctf_event_set_stream(event, NULL);
253 bt_ctf_event_put(event);
254 }
255
256 BT_HIDDEN
257 struct bt_ctf_stream *bt_ctf_stream_create(
258 struct bt_ctf_stream_class *stream_class,
259 struct bt_ctf_trace *trace)
260 {
261 int ret;
262 struct bt_ctf_stream *stream = NULL;
263
264 if (!stream_class || !trace) {
265 goto end;
266 }
267
268 stream = g_new0(struct bt_ctf_stream, 1);
269 if (!stream) {
270 goto end;
271 }
272
273 stream->trace = trace;
274 bt_ctf_ref_init(&stream->ref_count);
275 stream->packet_context = bt_ctf_field_create(
276 stream_class->packet_context_type);
277 if (!stream->packet_context) {
278 goto error_destroy;
279 }
280
281 /*
282 * A stream class may not have a stream event context defined
283 * in which case this stream will never have a stream_event_context
284 * member since, after a stream's creation, the parent stream class
285 * is "frozen" (immutable).
286 */
287 if (stream_class->event_context_type) {
288 stream->event_context = bt_ctf_field_create(
289 stream_class->event_context_type);
290 if (!stream->packet_context) {
291 goto error_destroy;
292 }
293 }
294
295 /* Initialize events_discarded */
296 ret = set_structure_field_integer(stream->packet_context,
297 "events_discarded", 0);
298 if (ret) {
299 goto error_destroy;
300 }
301
302 stream->pos.fd = -1;
303 stream->id = stream_class->next_stream_id++;
304 stream->stream_class = stream_class;
305 bt_ctf_stream_class_get(stream_class);
306 stream->events = g_ptr_array_new_with_free_func(
307 (GDestroyNotify) put_event);
308 if (!stream->events) {
309 goto error_destroy;
310 }
311 if (stream_class->event_context_type) {
312 stream->event_contexts = g_ptr_array_new_with_free_func(
313 (GDestroyNotify) bt_ctf_field_put);
314 if (!stream->event_contexts) {
315 goto error_destroy;
316 }
317 }
318
319 /* A trace is not allowed to have a NULL packet header */
320 assert(trace->packet_header_type);
321 stream->packet_header = bt_ctf_field_create(trace->packet_header_type);
322 if (!stream->packet_header) {
323 goto error_destroy;
324 }
325 /*
326 * Attempt to populate the default trace packet header fields
327 * (magic, uuid and stream_id). This will _not_ fail shall the
328 * fields not be found or be of an incompatible type; they will
329 * simply not be populated automatically. The user will have to
330 * make sure to set the trace packet header fields himself before
331 * flushing.
332 */
333 ret = set_packet_header(stream);
334 if (ret) {
335 goto error_destroy;
336 }
337 end:
338 return stream;
339 error_destroy:
340 bt_ctf_stream_destroy(&stream->ref_count);
341 return NULL;
342 }
343
344 BT_HIDDEN
345 int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
346 {
347 int ret = 0;
348
349 if (stream->pos.fd != -1) {
350 ret = -1;
351 goto end;
352 }
353
354 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
355 stream->pos.fd = fd;
356 end:
357 return ret;
358 }
359
360 BT_HIDDEN
361 void bt_ctf_stream_set_trace(struct bt_ctf_stream *stream,
362 struct bt_ctf_trace *trace)
363 {
364 stream->trace = trace;
365 }
366
367 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
368 struct bt_ctf_stream *stream)
369 {
370 struct bt_ctf_stream_class *stream_class = NULL;
371
372 if (!stream) {
373 goto end;
374 }
375
376 stream_class = stream->stream_class;
377 bt_ctf_stream_class_get(stream_class);
378 end:
379 return stream_class;
380 }
381
382 int bt_ctf_stream_get_discarded_events_count(
383 struct bt_ctf_stream *stream, uint64_t *count)
384 {
385 int64_t ret = 0;
386 int field_signed;
387 struct bt_ctf_field *events_discarded_field = NULL;
388 struct bt_ctf_field_type *events_discarded_field_type = NULL;
389
390 if (!stream || !count || !stream->packet_context) {
391 ret = -1;
392 goto end;
393 }
394
395 events_discarded_field = bt_ctf_field_structure_get_field(
396 stream->packet_context, "events_discarded");
397 if (!events_discarded_field) {
398 ret = -1;
399 goto end;
400 }
401
402 events_discarded_field_type = bt_ctf_field_get_type(
403 events_discarded_field);
404 if (!events_discarded_field_type) {
405 ret = -1;
406 goto end;
407 }
408
409 field_signed = bt_ctf_field_type_integer_get_signed(
410 events_discarded_field_type);
411 if (field_signed < 0) {
412 ret = field_signed;
413 goto end;
414 }
415
416 if (field_signed) {
417 int64_t signed_count;
418
419 ret = bt_ctf_field_signed_integer_get_value(
420 events_discarded_field, &signed_count);
421 if (ret) {
422 goto end;
423 }
424 if (signed_count < 0) {
425 /* Invalid value */
426 ret = -1;
427 goto end;
428 }
429 *count = (uint64_t) signed_count;
430 } else {
431 ret = bt_ctf_field_unsigned_integer_get_value(
432 events_discarded_field, count);
433 if (ret) {
434 goto end;
435 }
436 }
437 end:
438 if (events_discarded_field) {
439 bt_ctf_field_put(events_discarded_field);
440 }
441 if (events_discarded_field_type) {
442 bt_ctf_field_type_put(events_discarded_field_type);
443 }
444 return ret;
445 }
446
447 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
448 uint64_t event_count)
449 {
450 int ret;
451 int field_signed;
452 uint64_t previous_count;
453 uint64_t new_count;
454 struct bt_ctf_field *events_discarded_field = NULL;
455 struct bt_ctf_field_type *events_discarded_field_type = NULL;
456
457 if (!stream || !stream->packet_context) {
458 goto end;
459 }
460
461 ret = bt_ctf_stream_get_discarded_events_count(stream,
462 &previous_count);
463 if (ret) {
464 goto end;
465 }
466
467 events_discarded_field = bt_ctf_field_structure_get_field(
468 stream->packet_context, "events_discarded");
469 if (!events_discarded_field) {
470 goto end;
471 }
472
473 events_discarded_field_type = bt_ctf_field_get_type(
474 events_discarded_field);
475 if (!events_discarded_field_type) {
476 goto end;
477 }
478
479 field_signed = bt_ctf_field_type_integer_get_signed(
480 events_discarded_field_type);
481 if (field_signed < 0) {
482 goto end;
483 }
484
485 new_count = previous_count + event_count;
486 if (field_signed) {
487 ret = bt_ctf_field_signed_integer_set_value(
488 events_discarded_field, (int64_t) new_count);
489 if (ret) {
490 goto end;
491 }
492 } else {
493 ret = bt_ctf_field_unsigned_integer_set_value(
494 events_discarded_field, new_count);
495 if (ret) {
496 goto end;
497 }
498 }
499
500 end:
501 if (events_discarded_field) {
502 bt_ctf_field_put(events_discarded_field);
503 }
504 if (events_discarded_field_type) {
505 bt_ctf_field_type_put(events_discarded_field_type);
506 }
507 }
508
509 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
510 struct bt_ctf_event *event)
511 {
512 int ret = 0;
513 struct bt_ctf_field *event_context_copy = NULL;
514
515 if (!stream || !event) {
516 ret = -1;
517 goto end;
518 }
519
520 ret = bt_ctf_event_set_stream(event, stream);
521 if (ret) {
522 /* Event was already associated to a stream */
523 ret = -1;
524 goto end;
525 }
526
527 ret = bt_ctf_event_populate_event_header(event);
528 if (ret) {
529 goto end;
530 }
531
532 /* Make sure the event's payload is set */
533 ret = bt_ctf_event_validate(event);
534 if (ret) {
535 goto end;
536 }
537
538 /* Sample the current stream event context by copying it */
539 if (stream->event_context) {
540 /* Make sure the event context's payload is set */
541 ret = bt_ctf_field_validate(stream->event_context);
542 if (ret) {
543 goto end;
544 }
545
546 event_context_copy = bt_ctf_field_copy(stream->event_context);
547 if (!event_context_copy) {
548 ret = -1;
549 goto end;
550 }
551 }
552
553 bt_ctf_event_get(event);
554 /* Save the new event along with its associated stream event context */
555 g_ptr_array_add(stream->events, event);
556 if (event_context_copy) {
557 g_ptr_array_add(stream->event_contexts, event_context_copy);
558 }
559 end:
560 if (ret) {
561 (void) bt_ctf_event_set_stream(event, NULL);
562 }
563 return ret;
564 }
565
566 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
567 struct bt_ctf_stream *stream)
568 {
569 struct bt_ctf_field *packet_context = NULL;
570
571 if (!stream) {
572 goto end;
573 }
574
575 packet_context = stream->packet_context;
576 if (packet_context) {
577 bt_ctf_field_get(packet_context);
578 }
579 end:
580 return packet_context;
581 }
582
583 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
584 struct bt_ctf_field *field)
585 {
586 int ret = 0;
587 struct bt_ctf_field_type *field_type;
588
589 if (!stream || !field) {
590 ret = -1;
591 goto end;
592 }
593
594 field_type = bt_ctf_field_get_type(field);
595 if (field_type != stream->stream_class->packet_context_type) {
596 ret = -1;
597 goto end;
598 }
599
600 bt_ctf_field_type_put(field_type);
601 bt_ctf_field_get(field);
602 bt_ctf_field_put(stream->packet_context);
603 stream->packet_context = field;
604 end:
605 return ret;
606 }
607
608 struct bt_ctf_field *bt_ctf_stream_get_event_context(
609 struct bt_ctf_stream *stream)
610 {
611 struct bt_ctf_field *event_context = NULL;
612
613 if (!stream) {
614 goto end;
615 }
616
617 event_context = stream->event_context;
618 if (event_context) {
619 bt_ctf_field_get(event_context);
620 }
621 end:
622 return event_context;
623 }
624
625 int bt_ctf_stream_set_event_context(struct bt_ctf_stream *stream,
626 struct bt_ctf_field *field)
627 {
628 int ret = 0;
629 struct bt_ctf_field_type *field_type = NULL;
630
631 if (!stream || !field) {
632 ret = -1;
633 goto end;
634 }
635
636 field_type = bt_ctf_field_get_type(field);
637 if (field_type != stream->stream_class->event_context_type) {
638 ret = -1;
639 goto end;
640 }
641
642 bt_ctf_field_get(field);
643 bt_ctf_field_put(stream->event_context);
644 stream->event_context = field;
645 end:
646 if (field_type) {
647 bt_ctf_field_type_put(field_type);
648 }
649 return ret;
650 }
651
652 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
653 struct bt_ctf_stream *stream)
654 {
655 struct bt_ctf_field *packet_header = NULL;
656
657 if (!stream) {
658 goto end;
659 }
660
661 packet_header = stream->packet_header;
662 if (packet_header) {
663 bt_ctf_field_get(packet_header);
664 }
665 end:
666 return packet_header;
667 }
668
669 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
670 struct bt_ctf_field *field)
671 {
672 int ret = 0;
673 struct bt_ctf_field_type *field_type = NULL;
674
675 if (!stream || !field) {
676 ret = -1;
677 goto end;
678 }
679
680 field_type = bt_ctf_field_get_type(field);
681 if (field_type != stream->trace->packet_header_type) {
682 ret = -1;
683 goto end;
684 }
685
686 bt_ctf_field_get(field);
687 bt_ctf_field_put(stream->packet_header);
688 stream->packet_header = field;
689 end:
690 if (field_type) {
691 bt_ctf_field_type_put(field_type);
692 }
693 return ret;
694 }
695
696 static
697 int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
698 {
699 int ret = 0;
700 struct bt_ctf_field *timestamp_field = NULL;
701 struct bt_ctf_field_type *timestamp_field_type = NULL;
702
703 timestamp_field = bt_ctf_field_structure_get_field(event_header,
704 "timestamp");
705 if (!timestamp_field) {
706 ret = -1;
707 goto end;
708 }
709
710 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
711 assert(timestamp_field_type);
712 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
713 CTF_TYPE_INTEGER) {
714 ret = -1;
715 goto end;
716 }
717
718 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
719 int64_t val;
720
721 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
722 &val);
723 if (ret) {
724 goto end;
725 }
726 *timestamp = (uint64_t) val;
727 } else {
728 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
729 timestamp);
730 if (ret) {
731 goto end;
732 }
733 }
734 end:
735 bt_ctf_field_put(timestamp_field);
736 bt_ctf_field_type_put(timestamp_field_type);
737 return ret;
738 }
739
740 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
741 {
742 int ret = 0;
743 size_t i;
744 uint64_t timestamp_begin, timestamp_end, events_discarded;
745 struct bt_ctf_field *integer = NULL;
746 struct ctf_stream_pos packet_context_pos;
747
748 if (!stream || stream->pos.fd < 0) {
749 /*
750 * Stream does not have an associated fd. It is,
751 * therefore, not a stream being used to write events.
752 */
753 ret = -1;
754 goto end;
755 }
756
757 if (!stream->events->len) {
758 goto end;
759 }
760
761 ret = bt_ctf_field_validate(stream->packet_header);
762 if (ret) {
763 goto end;
764 }
765
766 /* mmap the next packet */
767 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
768
769 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
770 if (ret) {
771 goto end;
772 }
773
774 /* Set the default context attributes if present and unset. */
775 if (!get_event_header_timestamp(
776 ((struct bt_ctf_event *) g_ptr_array_index(
777 stream->events, 0))->event_header, &timestamp_begin)) {
778 ret = set_structure_field_integer(stream->packet_context,
779 "timestamp_begin", timestamp_begin);
780 if (ret) {
781 goto end;
782 }
783 }
784
785 if (!get_event_header_timestamp(
786 ((struct bt_ctf_event *) g_ptr_array_index(
787 stream->events, stream->events->len - 1))->event_header,
788 &timestamp_end)) {
789
790 ret = set_structure_field_integer(stream->packet_context,
791 "timestamp_end", timestamp_end);
792 if (ret) {
793 goto end;
794 }
795 }
796 ret = set_structure_field_integer(stream->packet_context,
797 "content_size", UINT64_MAX);
798 if (ret) {
799 goto end;
800 }
801
802 ret = set_structure_field_integer(stream->packet_context,
803 "packet_size", UINT64_MAX);
804 if (ret) {
805 goto end;
806 }
807
808 /* Write packet context */
809 memcpy(&packet_context_pos, &stream->pos,
810 sizeof(struct ctf_stream_pos));
811 ret = bt_ctf_field_serialize(stream->packet_context,
812 &stream->pos);
813 if (ret) {
814 goto end;
815 }
816
817 ret = bt_ctf_stream_get_discarded_events_count(stream,
818 &events_discarded);
819 if (ret) {
820 goto end;
821 }
822
823 /* Unset the packet context's fields. */
824 ret = bt_ctf_field_reset(stream->packet_context);
825 if (ret) {
826 goto end;
827 }
828
829 /* Set the previous number of discarded events. */
830 ret = set_structure_field_integer(stream->packet_context,
831 "events_discarded", events_discarded);
832 if (ret) {
833 goto end;
834 }
835
836 for (i = 0; i < stream->events->len; i++) {
837 struct bt_ctf_event *event = g_ptr_array_index(
838 stream->events, i);
839
840 ret = bt_ctf_field_reset(event->event_header);
841 if (ret) {
842 goto end;
843 }
844
845 /* Write event header */
846 ret = bt_ctf_field_serialize(event->event_header,
847 &stream->pos);
848 if (ret) {
849 goto end;
850 }
851
852 /* Write stream event context */
853 if (stream->event_contexts) {
854 ret = bt_ctf_field_serialize(
855 g_ptr_array_index(stream->event_contexts, i),
856 &stream->pos);
857 if (ret) {
858 goto end;
859 }
860 }
861
862 /* Write event content */
863 ret = bt_ctf_event_serialize(event, &stream->pos);
864 if (ret) {
865 goto end;
866 }
867 }
868
869 /*
870 * Update the packet total size and content size and overwrite the
871 * packet context.
872 * Copy base_mma as the packet may have been remapped (e.g. when a
873 * packet is resized).
874 */
875 packet_context_pos.base_mma = stream->pos.base_mma;
876 ret = set_structure_field_integer(stream->packet_context,
877 "content_size", stream->pos.offset);
878 if (ret) {
879 goto end;
880 }
881
882 ret = set_structure_field_integer(stream->packet_context,
883 "packet_size", stream->pos.packet_size);
884 if (ret) {
885 goto end;
886 }
887
888 ret = bt_ctf_field_serialize(stream->packet_context,
889 &packet_context_pos);
890 if (ret) {
891 goto end;
892 }
893
894 g_ptr_array_set_size(stream->events, 0);
895 if (stream->event_contexts) {
896 g_ptr_array_set_size(stream->event_contexts, 0);
897 }
898 stream->flushed_packet_count++;
899 end:
900 bt_ctf_field_put(integer);
901 return ret;
902 }
903
904 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
905 {
906 if (!stream) {
907 return;
908 }
909
910 bt_ctf_ref_get(&stream->ref_count);
911 }
912
913 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
914 {
915 if (!stream) {
916 return;
917 }
918
919 bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
920 }
921
922 static
923 void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
924 {
925 struct bt_ctf_stream *stream;
926
927 if (!ref) {
928 return;
929 }
930
931 stream = container_of(ref, struct bt_ctf_stream, ref_count);
932 ctf_fini_pos(&stream->pos);
933 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
934 perror("close");
935 }
936
937 if (stream->stream_class) {
938 bt_ctf_stream_class_put(stream->stream_class);
939 }
940 if (stream->events) {
941 g_ptr_array_free(stream->events, TRUE);
942 }
943 if (stream->event_contexts) {
944 g_ptr_array_free(stream->event_contexts, TRUE);
945 }
946 if (stream->packet_header) {
947 bt_ctf_field_put(stream->packet_header);
948 }
949 if (stream->packet_context) {
950 bt_ctf_field_put(stream->packet_context);
951 }
952 if (stream->event_context) {
953 bt_ctf_field_put(stream->event_context);
954 }
955 g_free(stream);
956 }
957
958 static
959 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
960 uint64_t value)
961 {
962 int ret = 0;
963 struct bt_ctf_field_type *field_type = NULL;
964 struct bt_ctf_field *integer =
965 bt_ctf_field_structure_get_field(structure, name);
966
967 if (!structure || !name) {
968 ret = -1;
969 goto end;
970 }
971
972 if (!integer) {
973 /* Field not found, not an error. */
974 goto end;
975 }
976
977 /* Make sure the payload has not already been set. */
978 if (!bt_ctf_field_validate(integer)) {
979 /* Payload already set, not an error */
980 goto end;
981 }
982
983 field_type = bt_ctf_field_get_type(integer);
984 /* Something is serioulsly wrong */
985 assert(field_type);
986 if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) {
987 /*
988 * The user most likely meant for us to populate this field
989 * automatically. However, we can only do this if the field
990 * is an integer. Return an error.
991 */
992 ret = -1;
993 goto end;
994 }
995
996 if (bt_ctf_field_type_integer_get_signed(field_type)) {
997 ret = bt_ctf_field_signed_integer_set_value(integer,
998 (int64_t) value);
999 } else {
1000 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
1001 }
1002 end:
1003 if (integer) {
1004 bt_ctf_field_put(integer);
1005 }
1006 if (field_type) {
1007 bt_ctf_field_type_put(field_type);
1008 }
1009 return ret;
1010 }
This page took 0.049575 seconds and 4 git commands to generate.