test_ctf_writer.c: test stream class clock -> trace clock
[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 32#include <babeltrace/ctf-ir/event-internal.h>
2e33ac5a
PP
33#include <babeltrace/ctf-ir/field-types-internal.h>
34#include <babeltrace/ctf-ir/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) !=
9a19a512 71 BT_CTF_TYPE_ID_INTEGER) {
d246b111
JG
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) !=
9a19a512 122 BT_CTF_TYPE_ID_ARRAY) {
d246b111
JG
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) !=
9a19a512 139 BT_CTF_TYPE_ID_INTEGER) {
d246b111
JG
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) !=
9a19a512 195 BT_CTF_TYPE_ID_INTEGER) {
d246b111
JG
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
e19cc57d 284 /* Initialize events_discarded */
12c8a1a3
JG
285 ret = set_structure_field_integer(stream->packet_context,
286 "events_discarded", 0);
287 if (ret) {
83509119 288 goto error;
12c8a1a3
JG
289 }
290
273b65be
JG
291 stream->pos.fd = -1;
292 stream->id = stream_class->next_stream_id++;
293 stream->stream_class = stream_class;
273b65be 294 stream->events = g_ptr_array_new_with_free_func(
e6a8e8e4 295 (GDestroyNotify) release_event);
8bfa3f9c 296 if (!stream->events) {
83509119 297 goto error;
8bfa3f9c 298 }
d246b111
JG
299
300 /* A trace is not allowed to have a NULL packet header */
301 assert(trace->packet_header_type);
302 stream->packet_header = bt_ctf_field_create(trace->packet_header_type);
92351460 303 if (!stream->packet_header) {
83509119 304 goto error;
92351460 305 }
d246b111
JG
306 /*
307 * Attempt to populate the default trace packet header fields
308 * (magic, uuid and stream_id). This will _not_ fail shall the
309 * fields not be found or be of an incompatible type; they will
310 * simply not be populated automatically. The user will have to
311 * make sure to set the trace packet header fields himself before
312 * flushing.
313 */
314 ret = set_packet_header(stream);
315 if (ret) {
83509119 316 goto error;
d246b111 317 }
273b65be
JG
318end:
319 return stream;
83509119
JG
320error:
321 BT_PUT(stream);
e6a8e8e4 322 bt_put(trace);
83509119 323 return stream;
273b65be
JG
324}
325
273b65be
JG
326BT_HIDDEN
327int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
328{
329 int ret = 0;
330
331 if (stream->pos.fd != -1) {
332 ret = -1;
333 goto end;
334 }
335
336 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
337 stream->pos.fd = fd;
338end:
339 return ret;
340}
341
3baf0856
JG
342struct bt_ctf_stream_class *bt_ctf_stream_get_class(
343 struct bt_ctf_stream *stream)
344{
345 struct bt_ctf_stream_class *stream_class = NULL;
346
347 if (!stream) {
348 goto end;
349 }
350
351 stream_class = stream->stream_class;
83509119 352 bt_get(stream_class);
3baf0856
JG
353end:
354 return stream_class;
355}
356
a78a2e25
JG
357int bt_ctf_stream_get_discarded_events_count(
358 struct bt_ctf_stream *stream, uint64_t *count)
359{
360 int64_t ret = 0;
12c8a1a3
JG
361 int field_signed;
362 struct bt_ctf_field *events_discarded_field = NULL;
363 struct bt_ctf_field_type *events_discarded_field_type = NULL;
a78a2e25 364
1579cde5
PP
365 if (!stream || !count || !stream->packet_context ||
366 stream->pos.fd < 0) {
a78a2e25
JG
367 ret = -1;
368 goto end;
369 }
370
12c8a1a3
JG
371 events_discarded_field = bt_ctf_field_structure_get_field(
372 stream->packet_context, "events_discarded");
373 if (!events_discarded_field) {
374 ret = -1;
375 goto end;
376 }
377
378 events_discarded_field_type = bt_ctf_field_get_type(
379 events_discarded_field);
380 if (!events_discarded_field_type) {
381 ret = -1;
382 goto end;
383 }
384
385 field_signed = bt_ctf_field_type_integer_get_signed(
386 events_discarded_field_type);
387 if (field_signed < 0) {
388 ret = field_signed;
389 goto end;
390 }
391
392 if (field_signed) {
393 int64_t signed_count;
394
395 ret = bt_ctf_field_signed_integer_get_value(
396 events_discarded_field, &signed_count);
397 if (ret) {
398 goto end;
399 }
400 if (signed_count < 0) {
401 /* Invalid value */
402 ret = -1;
403 goto end;
404 }
405 *count = (uint64_t) signed_count;
406 } else {
407 ret = bt_ctf_field_unsigned_integer_get_value(
408 events_discarded_field, count);
409 if (ret) {
410 goto end;
411 }
412 }
a78a2e25 413end:
83509119
JG
414 bt_put(events_discarded_field);
415 bt_put(events_discarded_field_type);
a78a2e25
JG
416 return ret;
417}
418
273b65be
JG
419void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
420 uint64_t event_count)
421{
12c8a1a3
JG
422 int ret;
423 int field_signed;
424 uint64_t previous_count;
425 uint64_t new_count;
426 struct bt_ctf_field *events_discarded_field = NULL;
427 struct bt_ctf_field_type *events_discarded_field_type = NULL;
428
1579cde5 429 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
12c8a1a3
JG
430 goto end;
431 }
432
433 ret = bt_ctf_stream_get_discarded_events_count(stream,
434 &previous_count);
435 if (ret) {
436 goto end;
437 }
438
439 events_discarded_field = bt_ctf_field_structure_get_field(
440 stream->packet_context, "events_discarded");
441 if (!events_discarded_field) {
442 goto end;
443 }
444
445 events_discarded_field_type = bt_ctf_field_get_type(
446 events_discarded_field);
447 if (!events_discarded_field_type) {
448 goto end;
449 }
450
451 field_signed = bt_ctf_field_type_integer_get_signed(
452 events_discarded_field_type);
453 if (field_signed < 0) {
454 goto end;
273b65be
JG
455 }
456
12c8a1a3
JG
457 new_count = previous_count + event_count;
458 if (field_signed) {
459 ret = bt_ctf_field_signed_integer_set_value(
460 events_discarded_field, (int64_t) new_count);
461 if (ret) {
462 goto end;
463 }
464 } else {
465 ret = bt_ctf_field_unsigned_integer_set_value(
466 events_discarded_field, new_count);
467 if (ret) {
468 goto end;
469 }
470 }
471
472end:
83509119
JG
473 bt_put(events_discarded_field);
474 bt_put(events_discarded_field_type);
273b65be
JG
475}
476
477int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
478 struct bt_ctf_event *event)
479{
480 int ret = 0;
273b65be 481
1579cde5 482 if (!stream || !event || stream->pos.fd < 0) {
273b65be
JG
483 ret = -1;
484 goto end;
485 }
486
e6a8e8e4 487 bt_object_set_parent(event, stream);
662e778c
JG
488 ret = bt_ctf_event_populate_event_header(event);
489 if (ret) {
490 goto end;
491 }
492
5fd2e9fd 493 /* Make sure the various scopes of the event are set */
273b65be
JG
494 ret = bt_ctf_event_validate(event);
495 if (ret) {
496 goto end;
497 }
498
0d688c15
PP
499 /* Save the new event and freeze it */
500 bt_ctf_event_freeze(event);
273b65be 501 g_ptr_array_add(stream->events, event);
5fd2e9fd 502
e6a8e8e4
JG
503 /*
504 * Event had to hold a reference to its event class as long as it wasn't
505 * part of the same trace hierarchy. From now on, the event and its
506 * class share the same lifetime guarantees and the reference is no
507 * longer needed.
508 */
509 bt_put(event->event_class);
273b65be 510end:
123fbdec 511 if (ret) {
e6a8e8e4
JG
512 /*
513 * Orphan the event; we were not succesful in associating it to
514 * a stream.
515 */
516 bt_object_set_parent(event, NULL);
123fbdec 517 }
273b65be
JG
518 return ret;
519}
520
12c8a1a3
JG
521struct bt_ctf_field *bt_ctf_stream_get_packet_context(
522 struct bt_ctf_stream *stream)
523{
524 struct bt_ctf_field *packet_context = NULL;
525
1579cde5 526 if (!stream || stream->pos.fd < 0) {
12c8a1a3
JG
527 goto end;
528 }
529
530 packet_context = stream->packet_context;
12c8a1a3 531 if (packet_context) {
83509119 532 bt_get(packet_context);
12c8a1a3 533 }
34629a55 534end:
12c8a1a3
JG
535 return packet_context;
536}
537
538int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
539 struct bt_ctf_field *field)
540{
541 int ret = 0;
542 struct bt_ctf_field_type *field_type;
543
1579cde5 544 if (!stream || !field || stream->pos.fd < 0) {
12c8a1a3
JG
545 ret = -1;
546 goto end;
547 }
548
549 field_type = bt_ctf_field_get_type(field);
09840de5
PP
550 if (bt_ctf_field_type_compare(field_type,
551 stream->stream_class->packet_context_type)) {
12c8a1a3
JG
552 ret = -1;
553 goto end;
554 }
555
83509119
JG
556 bt_put(field_type);
557 bt_get(field);
558 bt_put(stream->packet_context);
12c8a1a3
JG
559 stream->packet_context = field;
560end:
561 return ret;
562}
563
263a7df5
JG
564struct bt_ctf_field *bt_ctf_stream_get_packet_header(
565 struct bt_ctf_stream *stream)
566{
567 struct bt_ctf_field *packet_header = NULL;
568
1579cde5 569 if (!stream || stream->pos.fd < 0) {
263a7df5
JG
570 goto end;
571 }
572
573 packet_header = stream->packet_header;
574 if (packet_header) {
83509119 575 bt_get(packet_header);
263a7df5
JG
576 }
577end:
578 return packet_header;
579}
580
581int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
582 struct bt_ctf_field *field)
583{
584 int ret = 0;
e6a8e8e4 585 struct bt_ctf_trace *trace = NULL;
263a7df5
JG
586 struct bt_ctf_field_type *field_type = NULL;
587
1579cde5 588 if (!stream || !field || stream->pos.fd < 0) {
263a7df5
JG
589 ret = -1;
590 goto end;
591 }
592
e6a8e8e4 593 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
263a7df5 594 field_type = bt_ctf_field_get_type(field);
09840de5 595 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
263a7df5
JG
596 ret = -1;
597 goto end;
598 }
599
83509119
JG
600 bt_get(field);
601 bt_put(stream->packet_header);
263a7df5
JG
602 stream->packet_header = field;
603end:
e6a8e8e4 604 BT_PUT(trace);
83509119 605 bt_put(field_type);
263a7df5
JG
606 return ret;
607}
608
9a220c32
JG
609static
610int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
611{
612 int ret = 0;
613 struct bt_ctf_field *timestamp_field = NULL;
614 struct bt_ctf_field_type *timestamp_field_type = NULL;
615
616 timestamp_field = bt_ctf_field_structure_get_field(event_header,
617 "timestamp");
618 if (!timestamp_field) {
619 ret = -1;
620 goto end;
621 }
622
623 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
624 assert(timestamp_field_type);
625 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
9a19a512 626 BT_CTF_TYPE_ID_INTEGER) {
9a220c32
JG
627 ret = -1;
628 goto end;
629 }
630
631 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
632 int64_t val;
633
634 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
635 &val);
636 if (ret) {
637 goto end;
638 }
639 *timestamp = (uint64_t) val;
640 } else {
641 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
642 timestamp);
643 if (ret) {
644 goto end;
645 }
646 }
647end:
83509119
JG
648 bt_put(timestamp_field);
649 bt_put(timestamp_field_type);
9a220c32
JG
650 return ret;
651}
652
273b65be
JG
653int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
654{
655 int ret = 0;
656 size_t i;
12c8a1a3 657 uint64_t timestamp_begin, timestamp_end, events_discarded;
273b65be
JG
658 struct bt_ctf_field *integer = NULL;
659 struct ctf_stream_pos packet_context_pos;
660
bc37ae52
JG
661 if (!stream || stream->pos.fd < 0) {
662 /*
663 * Stream does not have an associated fd. It is,
664 * therefore, not a stream being used to write events.
665 */
273b65be
JG
666 ret = -1;
667 goto end;
668 }
669
670 if (!stream->events->len) {
671 goto end;
672 }
673
d246b111
JG
674 ret = bt_ctf_field_validate(stream->packet_header);
675 if (ret) {
676 goto end;
677 }
678
28362f2b
JG
679 /* mmap the next packet */
680 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
d246b111
JG
681
682 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
683 if (ret) {
684 goto end;
273b65be
JG
685 }
686
12c8a1a3 687 /* Set the default context attributes if present and unset. */
9a220c32
JG
688 if (!get_event_header_timestamp(
689 ((struct bt_ctf_event *) g_ptr_array_index(
690 stream->events, 0))->event_header, &timestamp_begin)) {
691 ret = set_structure_field_integer(stream->packet_context,
692 "timestamp_begin", timestamp_begin);
693 if (ret) {
694 goto end;
695 }
273b65be
JG
696 }
697
9a220c32
JG
698 if (!get_event_header_timestamp(
699 ((struct bt_ctf_event *) g_ptr_array_index(
700 stream->events, stream->events->len - 1))->event_header,
701 &timestamp_end)) {
273b65be 702
9a220c32
JG
703 ret = set_structure_field_integer(stream->packet_context,
704 "timestamp_end", timestamp_end);
705 if (ret) {
706 goto end;
707 }
708 }
12c8a1a3 709 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
710 "content_size", UINT64_MAX);
711 if (ret) {
712 goto end;
713 }
714
12c8a1a3 715 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
716 "packet_size", UINT64_MAX);
717 if (ret) {
718 goto end;
719 }
720
721 /* Write packet context */
722 memcpy(&packet_context_pos, &stream->pos,
723 sizeof(struct ctf_stream_pos));
12c8a1a3 724 ret = bt_ctf_field_serialize(stream->packet_context,
273b65be
JG
725 &stream->pos);
726 if (ret) {
727 goto end;
728 }
729
12c8a1a3
JG
730 ret = bt_ctf_stream_get_discarded_events_count(stream,
731 &events_discarded);
732 if (ret) {
733 goto end;
734 }
735
736 /* Unset the packet context's fields. */
737 ret = bt_ctf_field_reset(stream->packet_context);
738 if (ret) {
739 goto end;
740 }
741
742 /* Set the previous number of discarded events. */
743 ret = set_structure_field_integer(stream->packet_context,
744 "events_discarded", events_discarded);
745 if (ret) {
746 goto end;
747 }
748
273b65be
JG
749 for (i = 0; i < stream->events->len; i++) {
750 struct bt_ctf_event *event = g_ptr_array_index(
751 stream->events, i);
273b65be 752
662e778c 753 ret = bt_ctf_field_reset(event->event_header);
12c8a1a3
JG
754 if (ret) {
755 goto end;
756 }
757
273b65be 758 /* Write event header */
662e778c 759 ret = bt_ctf_field_serialize(event->event_header,
273b65be
JG
760 &stream->pos);
761 if (ret) {
762 goto end;
763 }
764
8bfa3f9c 765 /* Write stream event context */
5fd2e9fd 766 if (event->stream_event_context) {
8bfa3f9c 767 ret = bt_ctf_field_serialize(
5fd2e9fd 768 event->stream_event_context, &stream->pos);
8bfa3f9c
JG
769 if (ret) {
770 goto end;
771 }
772 }
773
273b65be
JG
774 /* Write event content */
775 ret = bt_ctf_event_serialize(event, &stream->pos);
776 if (ret) {
777 goto end;
778 }
779 }
780
781 /*
782 * Update the packet total size and content size and overwrite the
783 * packet context.
3a092c05
JG
784 * Copy base_mma as the packet may have been remapped (e.g. when a
785 * packet is resized).
273b65be 786 */
3a092c05 787 packet_context_pos.base_mma = stream->pos.base_mma;
12c8a1a3 788 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
789 "content_size", stream->pos.offset);
790 if (ret) {
791 goto end;
792 }
793
12c8a1a3 794 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
795 "packet_size", stream->pos.packet_size);
796 if (ret) {
797 goto end;
798 }
799
12c8a1a3 800 ret = bt_ctf_field_serialize(stream->packet_context,
273b65be
JG
801 &packet_context_pos);
802 if (ret) {
803 goto end;
804 }
805
806 g_ptr_array_set_size(stream->events, 0);
807 stream->flushed_packet_count++;
808end:
83509119 809 bt_put(integer);
273b65be
JG
810 return ret;
811}
812
813void bt_ctf_stream_get(struct bt_ctf_stream *stream)
814{
83509119 815 bt_get(stream);
273b65be
JG
816}
817
818void bt_ctf_stream_put(struct bt_ctf_stream *stream)
819{
83509119 820 bt_put(stream);
273b65be
JG
821}
822
823static
83509119 824void bt_ctf_stream_destroy(struct bt_object *obj)
273b65be
JG
825{
826 struct bt_ctf_stream *stream;
273b65be 827
83509119 828 stream = container_of(obj, struct bt_ctf_stream, base);
273b65be 829 ctf_fini_pos(&stream->pos);
0e45d308 830 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
9f56e450
JG
831 perror("close");
832 }
12c8a1a3 833
12c8a1a3
JG
834 if (stream->events) {
835 g_ptr_array_free(stream->events, TRUE);
836 }
83509119
JG
837 bt_put(stream->packet_header);
838 bt_put(stream->packet_context);
839 bt_put(stream->event_context);
273b65be
JG
840 g_free(stream);
841}
842
273b65be
JG
843static
844int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
845 uint64_t value)
846{
847 int ret = 0;
d7b1ea66 848 struct bt_ctf_field_type *field_type = NULL;
273b65be
JG
849 struct bt_ctf_field *integer =
850 bt_ctf_field_structure_get_field(structure, name);
12c8a1a3
JG
851
852 if (!structure || !name) {
273b65be
JG
853 ret = -1;
854 goto end;
855 }
856
12c8a1a3
JG
857 if (!integer) {
858 /* Field not found, not an error. */
859 goto end;
860 }
861
862 /* Make sure the payload has not already been set. */
863 if (!bt_ctf_field_validate(integer)) {
864 /* Payload already set, not an error */
865 goto end;
866 }
867
d7b1ea66
JG
868 field_type = bt_ctf_field_get_type(integer);
869 /* Something is serioulsly wrong */
870 assert(field_type);
9a19a512 871 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
d7b1ea66
JG
872 /*
873 * The user most likely meant for us to populate this field
874 * automatically. However, we can only do this if the field
875 * is an integer. Return an error.
876 */
877 ret = -1;
878 goto end;
879 }
880
881 if (bt_ctf_field_type_integer_get_signed(field_type)) {
882 ret = bt_ctf_field_signed_integer_set_value(integer,
883 (int64_t) value);
884 } else {
885 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
886 }
273b65be 887end:
83509119
JG
888 bt_put(integer);
889 bt_put(field_type);
273b65be
JG
890 return ret;
891}
This page took 0.070715 seconds and 4 git commands to generate.