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