Hide new bt_ctf_stream_* symbols
[babeltrace.git] / formats / ctf / ir / stream.c
CommitLineData
1c822dfb
JG
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
46static
47void bt_ctf_stream_destroy(struct bt_object *obj);
48static
49int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
50
51static
52int 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 }
95end:
96 bt_put(magic_field);
97 bt_put(magic_field_type);
98 return ret;
99}
100
101static
102int 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
168end:
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}
175static
176int 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 }
212end:
213 bt_put(stream_id_field);
214 bt_put(stream_id_field_type);
215 return ret;
216}
217
218static
219int 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 }
237end:
238 return ret;
239}
240
241static
242void 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
257static
258int 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);
280error:
281 g_string_free(filename, TRUE);
282 return fd;
283}
284
285static
286int 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;
297end:
298 return ret;
299}
300
301struct bt_ctf_stream *bt_ctf_stream_create(
302 struct bt_ctf_stream_class *stream_class,
303 const char *name)
304{
305 int ret;
306 struct bt_ctf_stream *stream = NULL;
307 struct bt_ctf_trace *trace = NULL;
308 struct bt_ctf_writer *writer = NULL;
309
310 if (!stream_class) {
311 goto error;
312 }
313
314 trace = bt_ctf_stream_class_get_trace(stream_class);
315 if (!trace) {
316 goto error;
317 }
318
319 stream = g_new0(struct bt_ctf_stream, 1);
320 if (!stream) {
321 goto error;
322 }
323
324 bt_object_init(stream, bt_ctf_stream_destroy);
325 /*
326 * Acquire reference to parent since stream will become publicly
327 * reachable; it needs its parent to remain valid.
328 */
329 bt_object_set_parent(stream, trace);
330 stream->id = stream_class->next_stream_id++;
331 stream->stream_class = stream_class;
332 stream->pos.fd = -1;
333
334 if (name) {
335 stream->name = g_string_new(name);
336 if (!stream->name) {
337 goto error;
338 }
339 }
340
341 if (trace->is_created_by_writer) {
342 int fd;
343 writer = (struct bt_ctf_writer *)
344 bt_object_get_parent(trace);
345
346 assert(writer);
347 stream->packet_context = bt_ctf_field_create(
348 stream_class->packet_context_type);
349 if (!stream->packet_context) {
350 goto error;
351 }
352
353 /* Initialize events_discarded */
354 ret = set_structure_field_integer(stream->packet_context,
355 "events_discarded", 0);
356 if (ret) {
357 goto error;
358 }
359
360 stream->events = g_ptr_array_new_with_free_func(
361 (GDestroyNotify) release_event);
362 if (!stream->events) {
363 goto error;
364 }
365
366 /* A trace is not allowed to have a NULL packet header */
367 assert(trace->packet_header_type);
368 stream->packet_header =
369 bt_ctf_field_create(trace->packet_header_type);
370 if (!stream->packet_header) {
371 goto error;
372 }
373
374 /*
375 * Attempt to populate the default trace packet header fields
376 * (magic, uuid and stream_id). This will _not_ fail shall the
377 * fields not be found or be of an incompatible type; they will
378 * simply not be populated automatically. The user will have to
379 * make sure to set the trace packet header fields himself
380 * before flushing.
381 */
382 ret = set_packet_header(stream);
383 if (ret) {
384 goto error;
385 }
386
387 /* Create file associated with this stream */
388 fd = create_stream_file(writer, stream);
389 if (fd < 0) {
390 goto error;
391 }
392
393 ret = set_stream_fd(stream, fd);
394 if (ret) {
395 goto error;
396 }
397
398 /* Freeze the writer */
399 bt_ctf_writer_freeze(writer);
400 } else {
401 /* Non-writer stream indicated by a negative FD */
402 ret = set_stream_fd(stream, -1);
403 if (ret) {
404 goto error;
405 }
406
407 stream->clock_values = g_hash_table_new_full(g_direct_hash,
408 g_direct_equal, NULL, g_free);
409 }
410
411 /* Add this stream to the trace's streams */
412 g_ptr_array_add(trace->streams, stream);
413
414 BT_PUT(trace);
415 BT_PUT(writer);
416 return stream;
417error:
418 BT_PUT(stream);
419 BT_PUT(trace);
420 BT_PUT(writer);
421 return stream;
422}
423
424struct bt_ctf_stream_class *bt_ctf_stream_get_class(
425 struct bt_ctf_stream *stream)
426{
427 struct bt_ctf_stream_class *stream_class = NULL;
428
429 if (!stream) {
430 goto end;
431 }
432
433 stream_class = stream->stream_class;
434 bt_get(stream_class);
435end:
436 return stream_class;
437}
438
439int bt_ctf_stream_get_discarded_events_count(
440 struct bt_ctf_stream *stream, uint64_t *count)
441{
442 int64_t ret = 0;
443 int field_signed;
444 struct bt_ctf_field *events_discarded_field = NULL;
445 struct bt_ctf_field_type *events_discarded_field_type = NULL;
446
447 if (!stream || !count || !stream->packet_context ||
448 stream->pos.fd < 0) {
449 ret = -1;
450 goto end;
451 }
452
453 events_discarded_field = bt_ctf_field_structure_get_field(
454 stream->packet_context, "events_discarded");
455 if (!events_discarded_field) {
456 ret = -1;
457 goto end;
458 }
459
460 events_discarded_field_type = bt_ctf_field_get_type(
461 events_discarded_field);
462 if (!events_discarded_field_type) {
463 ret = -1;
464 goto end;
465 }
466
467 field_signed = bt_ctf_field_type_integer_get_signed(
468 events_discarded_field_type);
469 if (field_signed < 0) {
470 ret = field_signed;
471 goto end;
472 }
473
474 if (field_signed) {
475 int64_t signed_count;
476
477 ret = bt_ctf_field_signed_integer_get_value(
478 events_discarded_field, &signed_count);
479 if (ret) {
480 goto end;
481 }
482 if (signed_count < 0) {
483 /* Invalid value */
484 ret = -1;
485 goto end;
486 }
487 *count = (uint64_t) signed_count;
488 } else {
489 ret = bt_ctf_field_unsigned_integer_get_value(
490 events_discarded_field, count);
491 if (ret) {
492 goto end;
493 }
494 }
495end:
496 bt_put(events_discarded_field);
497 bt_put(events_discarded_field_type);
498 return ret;
499}
500
501void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
502 uint64_t event_count)
503{
504 int ret;
505 int field_signed;
506 uint64_t previous_count;
507 uint64_t new_count;
508 struct bt_ctf_field *events_discarded_field = NULL;
509 struct bt_ctf_field_type *events_discarded_field_type = NULL;
510
511 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
512 goto end;
513 }
514
515 ret = bt_ctf_stream_get_discarded_events_count(stream,
516 &previous_count);
517 if (ret) {
518 goto end;
519 }
520
521 events_discarded_field = bt_ctf_field_structure_get_field(
522 stream->packet_context, "events_discarded");
523 if (!events_discarded_field) {
524 goto end;
525 }
526
527 events_discarded_field_type = bt_ctf_field_get_type(
528 events_discarded_field);
529 if (!events_discarded_field_type) {
530 goto end;
531 }
532
533 field_signed = bt_ctf_field_type_integer_get_signed(
534 events_discarded_field_type);
535 if (field_signed < 0) {
536 goto end;
537 }
538
539 new_count = previous_count + event_count;
540 if (field_signed) {
541 ret = bt_ctf_field_signed_integer_set_value(
542 events_discarded_field, (int64_t) new_count);
543 if (ret) {
544 goto end;
545 }
546 } else {
547 ret = bt_ctf_field_unsigned_integer_set_value(
548 events_discarded_field, new_count);
549 if (ret) {
550 goto end;
551 }
552 }
553
554end:
555 bt_put(events_discarded_field);
556 bt_put(events_discarded_field_type);
557}
558
559int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
560 struct bt_ctf_event *event)
561{
562 int ret = 0;
563
564 if (!stream || !event || stream->pos.fd < 0) {
565 ret = -1;
566 goto end;
567 }
568
569 /*
570 * The event is not supposed to have a parent stream at this
571 * point. The only other way an event can have a parent stream
572 * is if it was assigned when setting a packet to the event,
573 * in which case the packet's stream is not a writer stream,
574 * and thus the user is trying to append an event which belongs
575 * to another stream.
576 */
577 if (event->base.parent) {
578 ret = -1;
579 goto end;
580 }
581
582 bt_object_set_parent(event, stream);
583 ret = bt_ctf_event_populate_event_header(event);
584 if (ret) {
585 goto error;
586 }
587
588 /* Make sure the various scopes of the event are set */
589 ret = bt_ctf_event_validate(event);
590 if (ret) {
591 goto error;
592 }
593
594 /* Save the new event and freeze it */
595 bt_ctf_event_freeze(event);
596 g_ptr_array_add(stream->events, event);
597
598 /*
599 * Event had to hold a reference to its event class as long as it wasn't
600 * part of the same trace hierarchy. From now on, the event and its
601 * class share the same lifetime guarantees and the reference is no
602 * longer needed.
603 */
604 bt_put(event->event_class);
605
606end:
607 return ret;
608
609error:
610 /*
611 * Orphan the event; we were not successful in associating it to
612 * a stream.
613 */
614 bt_object_set_parent(event, NULL);
615
616 return ret;
617}
618
619struct bt_ctf_field *bt_ctf_stream_get_packet_context(
620 struct bt_ctf_stream *stream)
621{
622 struct bt_ctf_field *packet_context = NULL;
623
624 if (!stream || stream->pos.fd < 0) {
625 goto end;
626 }
627
628 packet_context = stream->packet_context;
629 if (packet_context) {
630 bt_get(packet_context);
631 }
632end:
633 return packet_context;
634}
635
eea2626f 636BT_HIDDEN
1c822dfb
JG
637int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
638 struct bt_ctf_field *field)
639{
640 int ret = 0;
641 struct bt_ctf_field_type *field_type;
642
643 if (!stream || !field || stream->pos.fd < 0) {
644 ret = -1;
645 goto end;
646 }
647
648 field_type = bt_ctf_field_get_type(field);
649 if (bt_ctf_field_type_compare(field_type,
650 stream->stream_class->packet_context_type)) {
651 ret = -1;
652 goto end;
653 }
654
655 bt_put(field_type);
656 bt_get(field);
657 bt_put(stream->packet_context);
658 stream->packet_context = field;
659end:
660 return ret;
661}
662
eea2626f 663BT_HIDDEN
1c822dfb
JG
664struct bt_ctf_field *bt_ctf_stream_get_packet_header(
665 struct bt_ctf_stream *stream)
666{
667 struct bt_ctf_field *packet_header = NULL;
668
669 if (!stream || stream->pos.fd < 0) {
670 goto end;
671 }
672
673 packet_header = stream->packet_header;
674 if (packet_header) {
675 bt_get(packet_header);
676 }
677end:
678 return packet_header;
679}
680
eea2626f 681BT_HIDDEN
1c822dfb
JG
682int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
683 struct bt_ctf_field *field)
684{
685 int ret = 0;
686 struct bt_ctf_trace *trace = NULL;
687 struct bt_ctf_field_type *field_type = NULL;
688
689 if (!stream || !field || stream->pos.fd < 0) {
690 ret = -1;
691 goto end;
692 }
693
694 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
695 field_type = bt_ctf_field_get_type(field);
696 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
697 ret = -1;
698 goto end;
699 }
700
701 bt_get(field);
702 bt_put(stream->packet_header);
703 stream->packet_header = field;
704end:
705 BT_PUT(trace);
706 bt_put(field_type);
707 return ret;
708}
709
710static
711int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
712{
713 int ret = 0;
714 struct bt_ctf_field *timestamp_field = NULL;
715 struct bt_ctf_field_type *timestamp_field_type = NULL;
716
717 timestamp_field = bt_ctf_field_structure_get_field(event_header,
718 "timestamp");
719 if (!timestamp_field) {
720 ret = -1;
721 goto end;
722 }
723
724 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
725 assert(timestamp_field_type);
726 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
727 BT_CTF_TYPE_ID_INTEGER) {
728 ret = -1;
729 goto end;
730 }
731
732 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
733 int64_t val;
734
735 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
736 &val);
737 if (ret) {
738 goto end;
739 }
740 *timestamp = (uint64_t) val;
741 } else {
742 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
743 timestamp);
744 if (ret) {
745 goto end;
746 }
747 }
748end:
749 bt_put(timestamp_field);
750 bt_put(timestamp_field_type);
751 return ret;
752}
753
754int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
755{
756 int ret = 0;
757 size_t i;
758 uint64_t timestamp_begin, timestamp_end, events_discarded;
759 struct bt_ctf_field *integer = NULL;
760 struct ctf_stream_pos packet_context_pos;
761
762 if (!stream || stream->pos.fd < 0) {
763 /*
764 * Stream does not have an associated fd. It is,
765 * therefore, not a stream being used to write events.
766 */
767 ret = -1;
768 goto end;
769 }
770
771 if (!stream->events->len) {
772 goto end;
773 }
774
775 ret = bt_ctf_field_validate(stream->packet_header);
776 if (ret) {
777 goto end;
778 }
779
780 /* mmap the next packet */
781 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
782
783 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
784 if (ret) {
785 goto end;
786 }
787
788 /* Set the default context attributes if present and unset. */
789 if (!get_event_header_timestamp(
790 ((struct bt_ctf_event *) g_ptr_array_index(
791 stream->events, 0))->event_header, &timestamp_begin)) {
792 ret = set_structure_field_integer(stream->packet_context,
793 "timestamp_begin", timestamp_begin);
794 if (ret) {
795 goto end;
796 }
797 }
798
799 if (!get_event_header_timestamp(
800 ((struct bt_ctf_event *) g_ptr_array_index(
801 stream->events, stream->events->len - 1))->event_header,
802 &timestamp_end)) {
803
804 ret = set_structure_field_integer(stream->packet_context,
805 "timestamp_end", timestamp_end);
806 if (ret) {
807 goto end;
808 }
809 }
810 ret = set_structure_field_integer(stream->packet_context,
811 "content_size", UINT64_MAX);
812 if (ret) {
813 goto end;
814 }
815
816 ret = set_structure_field_integer(stream->packet_context,
817 "packet_size", UINT64_MAX);
818 if (ret) {
819 goto end;
820 }
821
822 /* Write packet context */
823 memcpy(&packet_context_pos, &stream->pos,
824 sizeof(struct ctf_stream_pos));
825 ret = bt_ctf_field_serialize(stream->packet_context,
826 &stream->pos);
827 if (ret) {
828 goto end;
829 }
830
831 ret = bt_ctf_stream_get_discarded_events_count(stream,
832 &events_discarded);
833 if (ret) {
834 goto end;
835 }
836
837 /* Unset the packet context's fields. */
838 ret = bt_ctf_field_reset(stream->packet_context);
839 if (ret) {
840 goto end;
841 }
842
843 /* Set the previous number of discarded events. */
844 ret = set_structure_field_integer(stream->packet_context,
845 "events_discarded", events_discarded);
846 if (ret) {
847 goto end;
848 }
849
850 for (i = 0; i < stream->events->len; i++) {
851 struct bt_ctf_event *event = g_ptr_array_index(
852 stream->events, i);
853
854 ret = bt_ctf_field_reset(event->event_header);
855 if (ret) {
856 goto end;
857 }
858
859 /* Write event header */
860 ret = bt_ctf_field_serialize(event->event_header,
861 &stream->pos);
862 if (ret) {
863 goto end;
864 }
865
866 /* Write stream event context */
867 if (event->stream_event_context) {
868 ret = bt_ctf_field_serialize(
869 event->stream_event_context, &stream->pos);
870 if (ret) {
871 goto end;
872 }
873 }
874
875 /* Write event content */
876 ret = bt_ctf_event_serialize(event, &stream->pos);
877 if (ret) {
878 goto end;
879 }
880 }
881
882 /*
883 * Update the packet total size and content size and overwrite the
884 * packet context.
885 * Copy base_mma as the packet may have been remapped (e.g. when a
886 * packet is resized).
887 */
888 packet_context_pos.base_mma = stream->pos.base_mma;
889 ret = set_structure_field_integer(stream->packet_context,
890 "content_size", stream->pos.offset);
891 if (ret) {
892 goto end;
893 }
894
895 ret = set_structure_field_integer(stream->packet_context,
896 "packet_size", stream->pos.packet_size);
897 if (ret) {
898 goto end;
899 }
900
901 ret = bt_ctf_field_serialize(stream->packet_context,
902 &packet_context_pos);
903 if (ret) {
904 goto end;
905 }
906
907 g_ptr_array_set_size(stream->events, 0);
908 stream->flushed_packet_count++;
909end:
910 bt_put(integer);
911 return ret;
912}
913
914void bt_ctf_stream_get(struct bt_ctf_stream *stream)
915{
916 bt_get(stream);
917}
918
919void bt_ctf_stream_put(struct bt_ctf_stream *stream)
920{
921 bt_put(stream);
922}
923
924static
925void bt_ctf_stream_destroy(struct bt_object *obj)
926{
927 struct bt_ctf_stream *stream;
928
929 stream = container_of(obj, struct bt_ctf_stream, base);
930 ctf_fini_pos(&stream->pos);
931 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
932 perror("close");
933 }
934
935 if (stream->events) {
936 g_ptr_array_free(stream->events, TRUE);
937 }
938
939 if (stream->name) {
940 g_string_free(stream->name, TRUE);
941 }
942
943 if (stream->clock_values) {
944 g_hash_table_destroy(stream->clock_values);
945 }
946
947 bt_put(stream->packet_header);
948 bt_put(stream->packet_context);
949 g_free(stream);
950}
951
952static
953int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
954 uint64_t value)
955{
956 int ret = 0;
957 struct bt_ctf_field_type *field_type = NULL;
958 struct bt_ctf_field *integer =
959 bt_ctf_field_structure_get_field(structure, name);
960
961 if (!structure || !name) {
962 ret = -1;
963 goto end;
964 }
965
966 if (!integer) {
967 /* Field not found, not an error. */
968 goto end;
969 }
970
971 /* Make sure the payload has not already been set. */
972 if (!bt_ctf_field_validate(integer)) {
973 /* Payload already set, not an error */
974 goto end;
975 }
976
977 field_type = bt_ctf_field_get_type(integer);
978 /* Something is serioulsly wrong */
979 assert(field_type);
980 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
981 /*
982 * The user most likely meant for us to populate this field
983 * automatically. However, we can only do this if the field
984 * is an integer. Return an error.
985 */
986 ret = -1;
987 goto end;
988 }
989
990 if (bt_ctf_field_type_integer_get_signed(field_type)) {
991 ret = bt_ctf_field_signed_integer_set_value(integer,
992 (int64_t) value);
993 } else {
994 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
995 }
996end:
997 bt_put(integer);
998 bt_put(field_type);
999 return ret;
1000}
1001
eea2626f 1002BT_HIDDEN
1c822dfb
JG
1003const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
1004{
1005 const char *name = NULL;
1006
1007 if (!stream) {
1008 goto end;
1009 }
1010
1011 name = stream->name ? stream->name->str : NULL;
1012
1013end:
1014 return name;
1015}
1016
1017BT_HIDDEN
1018void bt_ctf_stream_update_clock_value(struct bt_ctf_stream *stream,
1019 struct bt_ctf_field *value_field)
1020{
1021 struct bt_ctf_field_type *value_type = NULL;
1022 struct bt_ctf_clock *clock = NULL;
1023 uint64_t requested_new_value;
1024 uint64_t requested_new_value_mask;
1025 uint64_t *cur_value;
1026 uint64_t cur_value_masked;
1027 int requested_new_value_size;
1028 int ret;
1029
1030 assert(stream);
1031 assert(clock);
1032 assert(value_field);
1033 value_type = bt_ctf_field_get_type(value_field);
1034 assert(value_type);
1035 clock = bt_ctf_field_type_integer_get_mapped_clock(value_type);
1036 assert(clock);
1037 requested_new_value_size =
1038 bt_ctf_field_type_integer_get_size(value_type);
1039 assert(requested_new_value_size > 0);
1040 ret = bt_ctf_field_unsigned_integer_get_value(value_field,
1041 &requested_new_value);
1042 assert(!ret);
1043 cur_value = g_hash_table_lookup(stream->clock_values, clock);
1044
1045 if (!cur_value) {
1046 /*
1047 * Updating the value of a clock which is not registered
1048 * yet, so register it with the new value as its initial
1049 * value.
1050 */
1051 uint64_t *requested_new_value_ptr = g_new0(uint64_t, 1);
1052
1053 *requested_new_value_ptr = requested_new_value;
1054 g_hash_table_insert(stream->clock_values, clock,
1055 requested_new_value_ptr);
1056 goto end;
1057 }
1058
1059 /*
1060 * Special case for a 64-bit new value, which is the limit
1061 * of a clock value as of this version: overwrite the
1062 * current value directly.
1063 */
1064 if (requested_new_value_size == 64) {
1065 *cur_value = requested_new_value;
1066 goto end;
1067 }
1068
1069 requested_new_value_mask = (1ULL << requested_new_value_size) - 1;
1070 cur_value_masked = *cur_value & requested_new_value_mask;
1071
1072 if (requested_new_value < cur_value_masked) {
1073 /*
1074 * It looks like a wrap happened on the number of bits
1075 * of the requested new value. Assume that the clock
1076 * value wrapped only one time.
1077 */
1078 *cur_value += requested_new_value_mask + 1;
1079 }
1080
1081 /* Clear the low bits of the current clock value */
1082 *cur_value &= ~requested_new_value_mask;
1083
1084 /* Set the low bits of the current clock value */
1085 *cur_value |= requested_new_value;
1086
1087end:
1088 bt_put(clock);
1089 bt_put(value_type);
1090}
This page took 0.060985 seconds and 4 git commands to generate.