ir: make bt_ctf_stream_create() public
[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/field-types-internal.h>
34 #include <babeltrace/ctf-ir/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-ir/trace-internal.h>
39 #include <babeltrace/ctf-writer/writer-internal.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/ctf-writer/functor-internal.h>
42 #include <babeltrace/compiler.h>
43 #include <babeltrace/align.h>
44 #include <babeltrace/ctf/ctf-index.h>
45
46 static
47 void bt_ctf_stream_destroy(struct bt_object *obj);
48 static
49 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
50
51 static
52 int set_packet_header_magic(struct bt_ctf_stream *stream)
53 {
54 int ret = 0;
55 struct bt_ctf_field_type *magic_field_type = NULL;
56 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
57 stream->packet_header, "magic");
58
59 if (!magic_field) {
60 /* No magic field found. Not an error, skip. */
61 goto end;
62 }
63
64 if (!bt_ctf_field_validate(magic_field)) {
65 /* Value already set. Not an error, skip. */
66 goto end;
67 }
68
69 magic_field_type = bt_ctf_field_get_type(magic_field);
70 assert(magic_field_type);
71
72 if (bt_ctf_field_type_get_type_id(magic_field_type) !=
73 BT_CTF_TYPE_ID_INTEGER) {
74 /* Magic field is not an integer. Not an error, skip. */
75 goto end;
76 }
77
78 if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) {
79 /*
80 * Magic field is not of the expected size.
81 * Not an error, skip.
82 */
83 goto end;
84 }
85
86 ret = bt_ctf_field_type_integer_get_signed(magic_field_type);
87 assert(ret >= 0);
88 if (ret) {
89 ret = bt_ctf_field_signed_integer_set_value(magic_field,
90 (int64_t) 0xC1FC1FC1);
91 } else {
92 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
93 (uint64_t) 0xC1FC1FC1);
94 }
95 end:
96 bt_put(magic_field);
97 bt_put(magic_field_type);
98 return ret;
99 }
100
101 static
102 int set_packet_header_uuid(struct bt_ctf_stream *stream)
103 {
104 int i, ret = 0;
105 struct bt_ctf_trace *trace = NULL;
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 BT_CTF_TYPE_ID_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 BT_CTF_TYPE_ID_INTEGER) {
142 /* UUID array elements are not integers. Not an error, skip */
143 goto end;
144 }
145
146 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
147 for (i = 0; i < 16; i++) {
148 struct bt_ctf_field *uuid_element =
149 bt_ctf_field_array_get_field(uuid_field, i);
150
151 ret = bt_ctf_field_type_integer_get_signed(element_field_type);
152 assert(ret >= 0);
153
154 if (ret) {
155 ret = bt_ctf_field_signed_integer_set_value(
156 uuid_element, (int64_t) trace->uuid[i]);
157 } else {
158 ret = bt_ctf_field_unsigned_integer_set_value(
159 uuid_element,
160 (uint64_t) trace->uuid[i]);
161 }
162 bt_put(uuid_element);
163 if (ret) {
164 goto end;
165 }
166 }
167
168 end:
169 bt_put(uuid_field);
170 bt_put(uuid_field_type);
171 bt_put(element_field_type);
172 BT_PUT(trace);
173 return ret;
174 }
175 static
176 int set_packet_header_stream_id(struct bt_ctf_stream *stream)
177 {
178 int ret = 0;
179 uint32_t stream_id;
180 struct bt_ctf_field_type *stream_id_field_type = NULL;
181 struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field(
182 stream->packet_header, "stream_id");
183
184 if (!stream_id_field) {
185 /* No stream_id field found. Not an error, skip. */
186 goto end;
187 }
188
189 if (!bt_ctf_field_validate(stream_id_field)) {
190 /* Value already set. Not an error, skip. */
191 goto end;
192 }
193
194 stream_id_field_type = bt_ctf_field_get_type(stream_id_field);
195 assert(stream_id_field_type);
196 if (bt_ctf_field_type_get_type_id(stream_id_field_type) !=
197 BT_CTF_TYPE_ID_INTEGER) {
198 /* stream_id field is not an integer. Not an error, skip. */
199 goto end;
200 }
201
202 stream_id = stream->stream_class->id;
203 ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type);
204 assert(ret >= 0);
205 if (ret) {
206 ret = bt_ctf_field_signed_integer_set_value(stream_id_field,
207 (int64_t) stream_id);
208 } else {
209 ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field,
210 (uint64_t) stream_id);
211 }
212 end:
213 bt_put(stream_id_field);
214 bt_put(stream_id_field_type);
215 return ret;
216 }
217
218 static
219 int set_packet_header(struct bt_ctf_stream *stream)
220 {
221 int ret;
222
223 ret = set_packet_header_magic(stream);
224 if (ret) {
225 goto end;
226 }
227
228 ret = set_packet_header_uuid(stream);
229 if (ret) {
230 goto end;
231 }
232
233 ret = set_packet_header_stream_id(stream);
234 if (ret) {
235 goto end;
236 }
237 end:
238 return ret;
239 }
240
241 static
242 void release_event(struct bt_ctf_event *event)
243 {
244 if (bt_object_get_ref_count(event)) {
245 /*
246 * The event is being orphaned, but it must guarantee the
247 * existence of its event class for the duration of its
248 * lifetime.
249 */
250 bt_get(event->event_class);
251 BT_PUT(event->base.parent);
252 } else {
253 bt_object_release(event);
254 }
255 }
256
257 static
258 int create_stream_file(struct bt_ctf_writer *writer,
259 struct bt_ctf_stream *stream)
260 {
261 int fd;
262 GString *filename = g_string_new(stream->stream_class->name->str);
263
264 if (stream->stream_class->name->len == 0) {
265 int64_t ret;
266
267 ret = bt_ctf_stream_class_get_id(stream->stream_class);
268 if (ret < 0) {
269 fd = -1;
270 goto error;
271 }
272
273 g_string_printf(filename, "stream_%" PRId64, ret);
274 }
275
276 g_string_append_printf(filename, "_%" PRIu32, stream->id);
277 fd = openat(writer->trace_dir_fd, filename->str,
278 O_RDWR | O_CREAT | O_TRUNC,
279 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
280 error:
281 g_string_free(filename, TRUE);
282 return fd;
283 }
284
285 static
286 int set_stream_fd(struct bt_ctf_stream *stream, int fd)
287 {
288 int ret = 0;
289
290 if (stream->pos.fd != -1) {
291 ret = -1;
292 goto end;
293 }
294
295 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
296 stream->pos.fd = fd;
297 end:
298 return ret;
299 }
300
301 struct bt_ctf_stream *bt_ctf_stream_create(
302 struct bt_ctf_stream_class *stream_class)
303 {
304 int ret;
305 struct bt_ctf_stream *stream = NULL;
306 struct bt_ctf_trace *trace = NULL;
307 struct bt_ctf_writer *writer = NULL;
308
309 if (!stream_class) {
310 goto end;
311 }
312
313 trace = bt_ctf_stream_class_get_trace(stream_class);
314 if (!trace) {
315 goto end;
316 }
317
318 stream = g_new0(struct bt_ctf_stream, 1);
319 if (!stream) {
320 goto end;
321 }
322
323 bt_object_init(stream, bt_ctf_stream_destroy);
324 /*
325 * Acquire reference to parent since stream will become publicly
326 * reachable; it needs its parent to remain valid.
327 */
328 bt_object_set_parent(stream, trace);
329 stream->id = stream_class->next_stream_id++;
330 stream->stream_class = stream_class;
331 stream->pos.fd = -1;
332
333 if (trace->is_created_by_writer) {
334 int fd;
335 writer = (struct bt_ctf_writer *)
336 bt_object_get_parent(trace);
337
338 assert(writer);
339 stream->packet_context = bt_ctf_field_create(
340 stream_class->packet_context_type);
341 if (!stream->packet_context) {
342 goto error;
343 }
344
345 /* Initialize events_discarded */
346 ret = set_structure_field_integer(stream->packet_context,
347 "events_discarded", 0);
348 if (ret) {
349 goto error;
350 }
351
352 stream->events = g_ptr_array_new_with_free_func(
353 (GDestroyNotify) release_event);
354 if (!stream->events) {
355 goto error;
356 }
357
358 /* A trace is not allowed to have a NULL packet header */
359 assert(trace->packet_header_type);
360 stream->packet_header =
361 bt_ctf_field_create(trace->packet_header_type);
362 if (!stream->packet_header) {
363 goto error;
364 }
365
366 /*
367 * Attempt to populate the default trace packet header fields
368 * (magic, uuid and stream_id). This will _not_ fail shall the
369 * fields not be found or be of an incompatible type; they will
370 * simply not be populated automatically. The user will have to
371 * make sure to set the trace packet header fields himself
372 * before flushing.
373 */
374 ret = set_packet_header(stream);
375 if (ret) {
376 goto error;
377 }
378
379 /* Create file associated with this stream */
380 fd = create_stream_file(writer, stream);
381 if (fd < 0) {
382 goto error;
383 }
384
385 ret = set_stream_fd(stream, fd);
386 if (ret) {
387 goto error;
388 }
389
390 /* Freeze the writer */
391 bt_ctf_writer_freeze(writer);
392 } else {
393 /* Non-writer stream indicated by a negative FD */
394 ret = set_stream_fd(stream, -1);
395 if (ret) {
396 goto error;
397 }
398 }
399
400 /* Add this stream to the trace's streams */
401 g_ptr_array_add(trace->streams, stream);
402
403 end:
404 BT_PUT(trace);
405 BT_PUT(writer);
406 return stream;
407 error:
408 BT_PUT(stream);
409 BT_PUT(trace);
410 BT_PUT(writer);
411 return stream;
412 }
413
414 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
415 struct bt_ctf_stream *stream)
416 {
417 struct bt_ctf_stream_class *stream_class = NULL;
418
419 if (!stream) {
420 goto end;
421 }
422
423 stream_class = stream->stream_class;
424 bt_get(stream_class);
425 end:
426 return stream_class;
427 }
428
429 int bt_ctf_stream_get_discarded_events_count(
430 struct bt_ctf_stream *stream, uint64_t *count)
431 {
432 int64_t ret = 0;
433 int field_signed;
434 struct bt_ctf_field *events_discarded_field = NULL;
435 struct bt_ctf_field_type *events_discarded_field_type = NULL;
436
437 if (!stream || !count || !stream->packet_context ||
438 stream->pos.fd < 0) {
439 ret = -1;
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 ret = -1;
447 goto end;
448 }
449
450 events_discarded_field_type = bt_ctf_field_get_type(
451 events_discarded_field);
452 if (!events_discarded_field_type) {
453 ret = -1;
454 goto end;
455 }
456
457 field_signed = bt_ctf_field_type_integer_get_signed(
458 events_discarded_field_type);
459 if (field_signed < 0) {
460 ret = field_signed;
461 goto end;
462 }
463
464 if (field_signed) {
465 int64_t signed_count;
466
467 ret = bt_ctf_field_signed_integer_get_value(
468 events_discarded_field, &signed_count);
469 if (ret) {
470 goto end;
471 }
472 if (signed_count < 0) {
473 /* Invalid value */
474 ret = -1;
475 goto end;
476 }
477 *count = (uint64_t) signed_count;
478 } else {
479 ret = bt_ctf_field_unsigned_integer_get_value(
480 events_discarded_field, count);
481 if (ret) {
482 goto end;
483 }
484 }
485 end:
486 bt_put(events_discarded_field);
487 bt_put(events_discarded_field_type);
488 return ret;
489 }
490
491 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
492 uint64_t event_count)
493 {
494 int ret;
495 int field_signed;
496 uint64_t previous_count;
497 uint64_t new_count;
498 struct bt_ctf_field *events_discarded_field = NULL;
499 struct bt_ctf_field_type *events_discarded_field_type = NULL;
500
501 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
502 goto end;
503 }
504
505 ret = bt_ctf_stream_get_discarded_events_count(stream,
506 &previous_count);
507 if (ret) {
508 goto end;
509 }
510
511 events_discarded_field = bt_ctf_field_structure_get_field(
512 stream->packet_context, "events_discarded");
513 if (!events_discarded_field) {
514 goto end;
515 }
516
517 events_discarded_field_type = bt_ctf_field_get_type(
518 events_discarded_field);
519 if (!events_discarded_field_type) {
520 goto end;
521 }
522
523 field_signed = bt_ctf_field_type_integer_get_signed(
524 events_discarded_field_type);
525 if (field_signed < 0) {
526 goto end;
527 }
528
529 new_count = previous_count + event_count;
530 if (field_signed) {
531 ret = bt_ctf_field_signed_integer_set_value(
532 events_discarded_field, (int64_t) new_count);
533 if (ret) {
534 goto end;
535 }
536 } else {
537 ret = bt_ctf_field_unsigned_integer_set_value(
538 events_discarded_field, new_count);
539 if (ret) {
540 goto end;
541 }
542 }
543
544 end:
545 bt_put(events_discarded_field);
546 bt_put(events_discarded_field_type);
547 }
548
549 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
550 struct bt_ctf_event *event)
551 {
552 int ret = 0;
553
554 if (!stream || !event || stream->pos.fd < 0) {
555 ret = -1;
556 goto end;
557 }
558
559 bt_object_set_parent(event, stream);
560 ret = bt_ctf_event_populate_event_header(event);
561 if (ret) {
562 goto end;
563 }
564
565 /* Make sure the various scopes of the event are set */
566 ret = bt_ctf_event_validate(event);
567 if (ret) {
568 goto end;
569 }
570
571 /* Save the new event and freeze it */
572 bt_ctf_event_freeze(event);
573 g_ptr_array_add(stream->events, event);
574
575 /*
576 * Event had to hold a reference to its event class as long as it wasn't
577 * part of the same trace hierarchy. From now on, the event and its
578 * class share the same lifetime guarantees and the reference is no
579 * longer needed.
580 */
581 bt_put(event->event_class);
582 end:
583 if (ret) {
584 /*
585 * Orphan the event; we were not succesful in associating it to
586 * a stream.
587 */
588 bt_object_set_parent(event, NULL);
589 }
590 return ret;
591 }
592
593 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
594 struct bt_ctf_stream *stream)
595 {
596 struct bt_ctf_field *packet_context = NULL;
597
598 if (!stream || stream->pos.fd < 0) {
599 goto end;
600 }
601
602 packet_context = stream->packet_context;
603 if (packet_context) {
604 bt_get(packet_context);
605 }
606 end:
607 return packet_context;
608 }
609
610 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
611 struct bt_ctf_field *field)
612 {
613 int ret = 0;
614 struct bt_ctf_field_type *field_type;
615
616 if (!stream || !field || stream->pos.fd < 0) {
617 ret = -1;
618 goto end;
619 }
620
621 field_type = bt_ctf_field_get_type(field);
622 if (bt_ctf_field_type_compare(field_type,
623 stream->stream_class->packet_context_type)) {
624 ret = -1;
625 goto end;
626 }
627
628 bt_put(field_type);
629 bt_get(field);
630 bt_put(stream->packet_context);
631 stream->packet_context = field;
632 end:
633 return ret;
634 }
635
636 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
637 struct bt_ctf_stream *stream)
638 {
639 struct bt_ctf_field *packet_header = NULL;
640
641 if (!stream || stream->pos.fd < 0) {
642 goto end;
643 }
644
645 packet_header = stream->packet_header;
646 if (packet_header) {
647 bt_get(packet_header);
648 }
649 end:
650 return packet_header;
651 }
652
653 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
654 struct bt_ctf_field *field)
655 {
656 int ret = 0;
657 struct bt_ctf_trace *trace = NULL;
658 struct bt_ctf_field_type *field_type = NULL;
659
660 if (!stream || !field || stream->pos.fd < 0) {
661 ret = -1;
662 goto end;
663 }
664
665 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
666 field_type = bt_ctf_field_get_type(field);
667 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
668 ret = -1;
669 goto end;
670 }
671
672 bt_get(field);
673 bt_put(stream->packet_header);
674 stream->packet_header = field;
675 end:
676 BT_PUT(trace);
677 bt_put(field_type);
678 return ret;
679 }
680
681 static
682 int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
683 {
684 int ret = 0;
685 struct bt_ctf_field *timestamp_field = NULL;
686 struct bt_ctf_field_type *timestamp_field_type = NULL;
687
688 timestamp_field = bt_ctf_field_structure_get_field(event_header,
689 "timestamp");
690 if (!timestamp_field) {
691 ret = -1;
692 goto end;
693 }
694
695 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
696 assert(timestamp_field_type);
697 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
698 BT_CTF_TYPE_ID_INTEGER) {
699 ret = -1;
700 goto end;
701 }
702
703 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
704 int64_t val;
705
706 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
707 &val);
708 if (ret) {
709 goto end;
710 }
711 *timestamp = (uint64_t) val;
712 } else {
713 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
714 timestamp);
715 if (ret) {
716 goto end;
717 }
718 }
719 end:
720 bt_put(timestamp_field);
721 bt_put(timestamp_field_type);
722 return ret;
723 }
724
725 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
726 {
727 int ret = 0;
728 size_t i;
729 uint64_t timestamp_begin, timestamp_end, events_discarded;
730 struct bt_ctf_field *integer = NULL;
731 struct ctf_stream_pos packet_context_pos;
732
733 if (!stream || stream->pos.fd < 0) {
734 /*
735 * Stream does not have an associated fd. It is,
736 * therefore, not a stream being used to write events.
737 */
738 ret = -1;
739 goto end;
740 }
741
742 if (!stream->events->len) {
743 goto end;
744 }
745
746 ret = bt_ctf_field_validate(stream->packet_header);
747 if (ret) {
748 goto end;
749 }
750
751 /* mmap the next packet */
752 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
753
754 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
755 if (ret) {
756 goto end;
757 }
758
759 /* Set the default context attributes if present and unset. */
760 if (!get_event_header_timestamp(
761 ((struct bt_ctf_event *) g_ptr_array_index(
762 stream->events, 0))->event_header, &timestamp_begin)) {
763 ret = set_structure_field_integer(stream->packet_context,
764 "timestamp_begin", timestamp_begin);
765 if (ret) {
766 goto end;
767 }
768 }
769
770 if (!get_event_header_timestamp(
771 ((struct bt_ctf_event *) g_ptr_array_index(
772 stream->events, stream->events->len - 1))->event_header,
773 &timestamp_end)) {
774
775 ret = set_structure_field_integer(stream->packet_context,
776 "timestamp_end", timestamp_end);
777 if (ret) {
778 goto end;
779 }
780 }
781 ret = set_structure_field_integer(stream->packet_context,
782 "content_size", UINT64_MAX);
783 if (ret) {
784 goto end;
785 }
786
787 ret = set_structure_field_integer(stream->packet_context,
788 "packet_size", UINT64_MAX);
789 if (ret) {
790 goto end;
791 }
792
793 /* Write packet context */
794 memcpy(&packet_context_pos, &stream->pos,
795 sizeof(struct ctf_stream_pos));
796 ret = bt_ctf_field_serialize(stream->packet_context,
797 &stream->pos);
798 if (ret) {
799 goto end;
800 }
801
802 ret = bt_ctf_stream_get_discarded_events_count(stream,
803 &events_discarded);
804 if (ret) {
805 goto end;
806 }
807
808 /* Unset the packet context's fields. */
809 ret = bt_ctf_field_reset(stream->packet_context);
810 if (ret) {
811 goto end;
812 }
813
814 /* Set the previous number of discarded events. */
815 ret = set_structure_field_integer(stream->packet_context,
816 "events_discarded", events_discarded);
817 if (ret) {
818 goto end;
819 }
820
821 for (i = 0; i < stream->events->len; i++) {
822 struct bt_ctf_event *event = g_ptr_array_index(
823 stream->events, i);
824
825 ret = bt_ctf_field_reset(event->event_header);
826 if (ret) {
827 goto end;
828 }
829
830 /* Write event header */
831 ret = bt_ctf_field_serialize(event->event_header,
832 &stream->pos);
833 if (ret) {
834 goto end;
835 }
836
837 /* Write stream event context */
838 if (event->stream_event_context) {
839 ret = bt_ctf_field_serialize(
840 event->stream_event_context, &stream->pos);
841 if (ret) {
842 goto end;
843 }
844 }
845
846 /* Write event content */
847 ret = bt_ctf_event_serialize(event, &stream->pos);
848 if (ret) {
849 goto end;
850 }
851 }
852
853 /*
854 * Update the packet total size and content size and overwrite the
855 * packet context.
856 * Copy base_mma as the packet may have been remapped (e.g. when a
857 * packet is resized).
858 */
859 packet_context_pos.base_mma = stream->pos.base_mma;
860 ret = set_structure_field_integer(stream->packet_context,
861 "content_size", stream->pos.offset);
862 if (ret) {
863 goto end;
864 }
865
866 ret = set_structure_field_integer(stream->packet_context,
867 "packet_size", stream->pos.packet_size);
868 if (ret) {
869 goto end;
870 }
871
872 ret = bt_ctf_field_serialize(stream->packet_context,
873 &packet_context_pos);
874 if (ret) {
875 goto end;
876 }
877
878 g_ptr_array_set_size(stream->events, 0);
879 stream->flushed_packet_count++;
880 end:
881 bt_put(integer);
882 return ret;
883 }
884
885 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
886 {
887 bt_get(stream);
888 }
889
890 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
891 {
892 bt_put(stream);
893 }
894
895 static
896 void bt_ctf_stream_destroy(struct bt_object *obj)
897 {
898 struct bt_ctf_stream *stream;
899
900 stream = container_of(obj, struct bt_ctf_stream, base);
901 ctf_fini_pos(&stream->pos);
902 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
903 perror("close");
904 }
905
906 if (stream->events) {
907 g_ptr_array_free(stream->events, TRUE);
908 }
909 bt_put(stream->packet_header);
910 bt_put(stream->packet_context);
911 bt_put(stream->event_context);
912 g_free(stream);
913 }
914
915 static
916 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
917 uint64_t value)
918 {
919 int ret = 0;
920 struct bt_ctf_field_type *field_type = NULL;
921 struct bt_ctf_field *integer =
922 bt_ctf_field_structure_get_field(structure, name);
923
924 if (!structure || !name) {
925 ret = -1;
926 goto end;
927 }
928
929 if (!integer) {
930 /* Field not found, not an error. */
931 goto end;
932 }
933
934 /* Make sure the payload has not already been set. */
935 if (!bt_ctf_field_validate(integer)) {
936 /* Payload already set, not an error */
937 goto end;
938 }
939
940 field_type = bt_ctf_field_get_type(integer);
941 /* Something is serioulsly wrong */
942 assert(field_type);
943 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
944 /*
945 * The user most likely meant for us to populate this field
946 * automatically. However, we can only do this if the field
947 * is an integer. Return an error.
948 */
949 ret = -1;
950 goto end;
951 }
952
953 if (bt_ctf_field_type_integer_get_signed(field_type)) {
954 ret = bt_ctf_field_signed_integer_set_value(integer,
955 (int64_t) value);
956 } else {
957 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
958 }
959 end:
960 bt_put(integer);
961 bt_put(field_type);
962 return ret;
963 }
This page took 0.049686 seconds and 4 git commands to generate.