ir: bt_ctf_stream_append_event(): do not append event with parent
[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>
319fd969
PP
38#include <babeltrace/ctf-ir/trace-internal.h>
39#include <babeltrace/ctf-writer/writer-internal.h>
83509119 40#include <babeltrace/ref.h>
273b65be
JG
41#include <babeltrace/ctf-writer/functor-internal.h>
42#include <babeltrace/compiler.h>
43#include <babeltrace/align.h>
d246b111 44#include <babeltrace/ctf/ctf-index.h>
273b65be
JG
45
46static
83509119 47void bt_ctf_stream_destroy(struct bt_object *obj);
273b65be 48static
273b65be
JG
49int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
50
d246b111
JG
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) !=
9a19a512 73 BT_CTF_TYPE_ID_INTEGER) {
d246b111
JG
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:
83509119
JG
96 bt_put(magic_field);
97 bt_put(magic_field_type);
d246b111
JG
98 return ret;
99}
100
101static
102int set_packet_header_uuid(struct bt_ctf_stream *stream)
103{
104 int i, ret = 0;
e6a8e8e4 105 struct bt_ctf_trace *trace = NULL;
d246b111
JG
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) !=
9a19a512 124 BT_CTF_TYPE_ID_ARRAY) {
d246b111
JG
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) !=
9a19a512 141 BT_CTF_TYPE_ID_INTEGER) {
d246b111
JG
142 /* UUID array elements are not integers. Not an error, skip */
143 goto end;
144 }
145
e6a8e8e4 146 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
d246b111
JG
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(
e6a8e8e4 156 uuid_element, (int64_t) trace->uuid[i]);
d246b111
JG
157 } else {
158 ret = bt_ctf_field_unsigned_integer_set_value(
159 uuid_element,
e6a8e8e4 160 (uint64_t) trace->uuid[i]);
d246b111 161 }
83509119 162 bt_put(uuid_element);
d246b111
JG
163 if (ret) {
164 goto end;
165 }
166 }
167
168end:
83509119
JG
169 bt_put(uuid_field);
170 bt_put(uuid_field_type);
171 bt_put(element_field_type);
e6a8e8e4 172 BT_PUT(trace);
d246b111
JG
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) !=
9a19a512 197 BT_CTF_TYPE_ID_INTEGER) {
d246b111
JG
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:
83509119
JG
213 bt_put(stream_id_field);
214 bt_put(stream_id_field_type);
d246b111
JG
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
123fbdec 241static
e6a8e8e4 242void release_event(struct bt_ctf_event *event)
123fbdec 243{
e6a8e8e4
JG
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 }
123fbdec
JG
255}
256
319fd969
PP
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
273b65be 301struct bt_ctf_stream *bt_ctf_stream_create(
b71d7298
PP
302 struct bt_ctf_stream_class *stream_class,
303 const char *name)
273b65be 304{
12c8a1a3 305 int ret;
273b65be 306 struct bt_ctf_stream *stream = NULL;
319fd969
PP
307 struct bt_ctf_trace *trace = NULL;
308 struct bt_ctf_writer *writer = NULL;
273b65be 309
319fd969 310 if (!stream_class) {
b71d7298 311 goto error;
319fd969
PP
312 }
313
314 trace = bt_ctf_stream_class_get_trace(stream_class);
315 if (!trace) {
b71d7298 316 goto error;
273b65be
JG
317 }
318
319 stream = g_new0(struct bt_ctf_stream, 1);
320 if (!stream) {
b71d7298 321 goto error;
273b65be
JG
322 }
323
83509119 324 bt_object_init(stream, bt_ctf_stream_destroy);
e6a8e8e4
JG
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);
273b65be
JG
330 stream->id = stream_class->next_stream_id++;
331 stream->stream_class = stream_class;
319fd969 332 stream->pos.fd = -1;
d246b111 333
b71d7298
PP
334 if (name) {
335 stream->name = g_string_new(name);
336 if (!stream->name) {
337 goto error;
338 }
339 }
340
319fd969
PP
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 }
d246b111 406 }
319fd969
PP
407
408 /* Add this stream to the trace's streams */
409 g_ptr_array_add(trace->streams, stream);
410
319fd969
PP
411 BT_PUT(trace);
412 BT_PUT(writer);
273b65be 413 return stream;
83509119
JG
414error:
415 BT_PUT(stream);
319fd969
PP
416 BT_PUT(trace);
417 BT_PUT(writer);
83509119 418 return stream;
273b65be
JG
419}
420
3baf0856
JG
421struct bt_ctf_stream_class *bt_ctf_stream_get_class(
422 struct bt_ctf_stream *stream)
423{
424 struct bt_ctf_stream_class *stream_class = NULL;
425
426 if (!stream) {
427 goto end;
428 }
429
430 stream_class = stream->stream_class;
83509119 431 bt_get(stream_class);
3baf0856
JG
432end:
433 return stream_class;
434}
435
a78a2e25
JG
436int bt_ctf_stream_get_discarded_events_count(
437 struct bt_ctf_stream *stream, uint64_t *count)
438{
439 int64_t ret = 0;
12c8a1a3
JG
440 int field_signed;
441 struct bt_ctf_field *events_discarded_field = NULL;
442 struct bt_ctf_field_type *events_discarded_field_type = NULL;
a78a2e25 443
1579cde5
PP
444 if (!stream || !count || !stream->packet_context ||
445 stream->pos.fd < 0) {
a78a2e25
JG
446 ret = -1;
447 goto end;
448 }
449
12c8a1a3
JG
450 events_discarded_field = bt_ctf_field_structure_get_field(
451 stream->packet_context, "events_discarded");
452 if (!events_discarded_field) {
453 ret = -1;
454 goto end;
455 }
456
457 events_discarded_field_type = bt_ctf_field_get_type(
458 events_discarded_field);
459 if (!events_discarded_field_type) {
460 ret = -1;
461 goto end;
462 }
463
464 field_signed = bt_ctf_field_type_integer_get_signed(
465 events_discarded_field_type);
466 if (field_signed < 0) {
467 ret = field_signed;
468 goto end;
469 }
470
471 if (field_signed) {
472 int64_t signed_count;
473
474 ret = bt_ctf_field_signed_integer_get_value(
475 events_discarded_field, &signed_count);
476 if (ret) {
477 goto end;
478 }
479 if (signed_count < 0) {
480 /* Invalid value */
481 ret = -1;
482 goto end;
483 }
484 *count = (uint64_t) signed_count;
485 } else {
486 ret = bt_ctf_field_unsigned_integer_get_value(
487 events_discarded_field, count);
488 if (ret) {
489 goto end;
490 }
491 }
a78a2e25 492end:
83509119
JG
493 bt_put(events_discarded_field);
494 bt_put(events_discarded_field_type);
a78a2e25
JG
495 return ret;
496}
497
273b65be
JG
498void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
499 uint64_t event_count)
500{
12c8a1a3
JG
501 int ret;
502 int field_signed;
503 uint64_t previous_count;
504 uint64_t new_count;
505 struct bt_ctf_field *events_discarded_field = NULL;
506 struct bt_ctf_field_type *events_discarded_field_type = NULL;
507
1579cde5 508 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
12c8a1a3
JG
509 goto end;
510 }
511
512 ret = bt_ctf_stream_get_discarded_events_count(stream,
513 &previous_count);
514 if (ret) {
515 goto end;
516 }
517
518 events_discarded_field = bt_ctf_field_structure_get_field(
519 stream->packet_context, "events_discarded");
520 if (!events_discarded_field) {
521 goto end;
522 }
523
524 events_discarded_field_type = bt_ctf_field_get_type(
525 events_discarded_field);
526 if (!events_discarded_field_type) {
527 goto end;
528 }
529
530 field_signed = bt_ctf_field_type_integer_get_signed(
531 events_discarded_field_type);
532 if (field_signed < 0) {
533 goto end;
273b65be
JG
534 }
535
12c8a1a3
JG
536 new_count = previous_count + event_count;
537 if (field_signed) {
538 ret = bt_ctf_field_signed_integer_set_value(
539 events_discarded_field, (int64_t) new_count);
540 if (ret) {
541 goto end;
542 }
543 } else {
544 ret = bt_ctf_field_unsigned_integer_set_value(
545 events_discarded_field, new_count);
546 if (ret) {
547 goto end;
548 }
549 }
550
551end:
83509119
JG
552 bt_put(events_discarded_field);
553 bt_put(events_discarded_field_type);
273b65be
JG
554}
555
556int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
557 struct bt_ctf_event *event)
558{
559 int ret = 0;
273b65be 560
1579cde5 561 if (!stream || !event || stream->pos.fd < 0) {
273b65be
JG
562 ret = -1;
563 goto end;
564 }
565
fa29ba83
PP
566 /*
567 * The event is not supposed to have a parent stream at this
568 * point. The only other way an event can have a parent stream
569 * is if it was assigned when setting a packet to the event,
570 * in which case the packet's stream is not a writer stream,
571 * and thus the user is trying to append an event which belongs
572 * to another stream.
573 */
574 if (event->base.parent) {
575 ret = -1;
576 goto end;
577 }
578
e6a8e8e4 579 bt_object_set_parent(event, stream);
662e778c
JG
580 ret = bt_ctf_event_populate_event_header(event);
581 if (ret) {
582 goto end;
583 }
584
5fd2e9fd 585 /* Make sure the various scopes of the event are set */
273b65be
JG
586 ret = bt_ctf_event_validate(event);
587 if (ret) {
588 goto end;
589 }
590
0d688c15
PP
591 /* Save the new event and freeze it */
592 bt_ctf_event_freeze(event);
273b65be 593 g_ptr_array_add(stream->events, event);
5fd2e9fd 594
e6a8e8e4
JG
595 /*
596 * Event had to hold a reference to its event class as long as it wasn't
597 * part of the same trace hierarchy. From now on, the event and its
598 * class share the same lifetime guarantees and the reference is no
599 * longer needed.
600 */
601 bt_put(event->event_class);
273b65be 602end:
123fbdec 603 if (ret) {
e6a8e8e4
JG
604 /*
605 * Orphan the event; we were not succesful in associating it to
606 * a stream.
607 */
608 bt_object_set_parent(event, NULL);
123fbdec 609 }
273b65be
JG
610 return ret;
611}
612
12c8a1a3
JG
613struct bt_ctf_field *bt_ctf_stream_get_packet_context(
614 struct bt_ctf_stream *stream)
615{
616 struct bt_ctf_field *packet_context = NULL;
617
1579cde5 618 if (!stream || stream->pos.fd < 0) {
12c8a1a3
JG
619 goto end;
620 }
621
622 packet_context = stream->packet_context;
12c8a1a3 623 if (packet_context) {
83509119 624 bt_get(packet_context);
12c8a1a3 625 }
34629a55 626end:
12c8a1a3
JG
627 return packet_context;
628}
629
630int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
631 struct bt_ctf_field *field)
632{
633 int ret = 0;
634 struct bt_ctf_field_type *field_type;
635
1579cde5 636 if (!stream || !field || stream->pos.fd < 0) {
12c8a1a3
JG
637 ret = -1;
638 goto end;
639 }
640
641 field_type = bt_ctf_field_get_type(field);
09840de5
PP
642 if (bt_ctf_field_type_compare(field_type,
643 stream->stream_class->packet_context_type)) {
12c8a1a3
JG
644 ret = -1;
645 goto end;
646 }
647
83509119
JG
648 bt_put(field_type);
649 bt_get(field);
650 bt_put(stream->packet_context);
12c8a1a3
JG
651 stream->packet_context = field;
652end:
653 return ret;
654}
655
263a7df5
JG
656struct bt_ctf_field *bt_ctf_stream_get_packet_header(
657 struct bt_ctf_stream *stream)
658{
659 struct bt_ctf_field *packet_header = NULL;
660
1579cde5 661 if (!stream || stream->pos.fd < 0) {
263a7df5
JG
662 goto end;
663 }
664
665 packet_header = stream->packet_header;
666 if (packet_header) {
83509119 667 bt_get(packet_header);
263a7df5
JG
668 }
669end:
670 return packet_header;
671}
672
673int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
674 struct bt_ctf_field *field)
675{
676 int ret = 0;
e6a8e8e4 677 struct bt_ctf_trace *trace = NULL;
263a7df5
JG
678 struct bt_ctf_field_type *field_type = NULL;
679
1579cde5 680 if (!stream || !field || stream->pos.fd < 0) {
263a7df5
JG
681 ret = -1;
682 goto end;
683 }
684
e6a8e8e4 685 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
263a7df5 686 field_type = bt_ctf_field_get_type(field);
09840de5 687 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
263a7df5
JG
688 ret = -1;
689 goto end;
690 }
691
83509119
JG
692 bt_get(field);
693 bt_put(stream->packet_header);
263a7df5
JG
694 stream->packet_header = field;
695end:
e6a8e8e4 696 BT_PUT(trace);
83509119 697 bt_put(field_type);
263a7df5
JG
698 return ret;
699}
700
9a220c32
JG
701static
702int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
703{
704 int ret = 0;
705 struct bt_ctf_field *timestamp_field = NULL;
706 struct bt_ctf_field_type *timestamp_field_type = NULL;
707
708 timestamp_field = bt_ctf_field_structure_get_field(event_header,
709 "timestamp");
710 if (!timestamp_field) {
711 ret = -1;
712 goto end;
713 }
714
715 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
716 assert(timestamp_field_type);
717 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
9a19a512 718 BT_CTF_TYPE_ID_INTEGER) {
9a220c32
JG
719 ret = -1;
720 goto end;
721 }
722
723 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
724 int64_t val;
725
726 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
727 &val);
728 if (ret) {
729 goto end;
730 }
731 *timestamp = (uint64_t) val;
732 } else {
733 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
734 timestamp);
735 if (ret) {
736 goto end;
737 }
738 }
739end:
83509119
JG
740 bt_put(timestamp_field);
741 bt_put(timestamp_field_type);
9a220c32
JG
742 return ret;
743}
744
273b65be
JG
745int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
746{
747 int ret = 0;
748 size_t i;
12c8a1a3 749 uint64_t timestamp_begin, timestamp_end, events_discarded;
273b65be
JG
750 struct bt_ctf_field *integer = NULL;
751 struct ctf_stream_pos packet_context_pos;
752
bc37ae52
JG
753 if (!stream || stream->pos.fd < 0) {
754 /*
755 * Stream does not have an associated fd. It is,
756 * therefore, not a stream being used to write events.
757 */
273b65be
JG
758 ret = -1;
759 goto end;
760 }
761
762 if (!stream->events->len) {
763 goto end;
764 }
765
d246b111
JG
766 ret = bt_ctf_field_validate(stream->packet_header);
767 if (ret) {
768 goto end;
769 }
770
28362f2b
JG
771 /* mmap the next packet */
772 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
d246b111
JG
773
774 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
775 if (ret) {
776 goto end;
273b65be
JG
777 }
778
12c8a1a3 779 /* Set the default context attributes if present and unset. */
9a220c32
JG
780 if (!get_event_header_timestamp(
781 ((struct bt_ctf_event *) g_ptr_array_index(
782 stream->events, 0))->event_header, &timestamp_begin)) {
783 ret = set_structure_field_integer(stream->packet_context,
784 "timestamp_begin", timestamp_begin);
785 if (ret) {
786 goto end;
787 }
273b65be
JG
788 }
789
9a220c32
JG
790 if (!get_event_header_timestamp(
791 ((struct bt_ctf_event *) g_ptr_array_index(
792 stream->events, stream->events->len - 1))->event_header,
793 &timestamp_end)) {
273b65be 794
9a220c32
JG
795 ret = set_structure_field_integer(stream->packet_context,
796 "timestamp_end", timestamp_end);
797 if (ret) {
798 goto end;
799 }
800 }
12c8a1a3 801 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
802 "content_size", UINT64_MAX);
803 if (ret) {
804 goto end;
805 }
806
12c8a1a3 807 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
808 "packet_size", UINT64_MAX);
809 if (ret) {
810 goto end;
811 }
812
813 /* Write packet context */
814 memcpy(&packet_context_pos, &stream->pos,
815 sizeof(struct ctf_stream_pos));
12c8a1a3 816 ret = bt_ctf_field_serialize(stream->packet_context,
273b65be
JG
817 &stream->pos);
818 if (ret) {
819 goto end;
820 }
821
12c8a1a3
JG
822 ret = bt_ctf_stream_get_discarded_events_count(stream,
823 &events_discarded);
824 if (ret) {
825 goto end;
826 }
827
828 /* Unset the packet context's fields. */
829 ret = bt_ctf_field_reset(stream->packet_context);
830 if (ret) {
831 goto end;
832 }
833
834 /* Set the previous number of discarded events. */
835 ret = set_structure_field_integer(stream->packet_context,
836 "events_discarded", events_discarded);
837 if (ret) {
838 goto end;
839 }
840
273b65be
JG
841 for (i = 0; i < stream->events->len; i++) {
842 struct bt_ctf_event *event = g_ptr_array_index(
843 stream->events, i);
273b65be 844
662e778c 845 ret = bt_ctf_field_reset(event->event_header);
12c8a1a3
JG
846 if (ret) {
847 goto end;
848 }
849
273b65be 850 /* Write event header */
662e778c 851 ret = bt_ctf_field_serialize(event->event_header,
273b65be
JG
852 &stream->pos);
853 if (ret) {
854 goto end;
855 }
856
8bfa3f9c 857 /* Write stream event context */
5fd2e9fd 858 if (event->stream_event_context) {
8bfa3f9c 859 ret = bt_ctf_field_serialize(
5fd2e9fd 860 event->stream_event_context, &stream->pos);
8bfa3f9c
JG
861 if (ret) {
862 goto end;
863 }
864 }
865
273b65be
JG
866 /* Write event content */
867 ret = bt_ctf_event_serialize(event, &stream->pos);
868 if (ret) {
869 goto end;
870 }
871 }
872
873 /*
874 * Update the packet total size and content size and overwrite the
875 * packet context.
3a092c05
JG
876 * Copy base_mma as the packet may have been remapped (e.g. when a
877 * packet is resized).
273b65be 878 */
3a092c05 879 packet_context_pos.base_mma = stream->pos.base_mma;
12c8a1a3 880 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
881 "content_size", stream->pos.offset);
882 if (ret) {
883 goto end;
884 }
885
12c8a1a3 886 ret = set_structure_field_integer(stream->packet_context,
273b65be
JG
887 "packet_size", stream->pos.packet_size);
888 if (ret) {
889 goto end;
890 }
891
12c8a1a3 892 ret = bt_ctf_field_serialize(stream->packet_context,
273b65be
JG
893 &packet_context_pos);
894 if (ret) {
895 goto end;
896 }
897
898 g_ptr_array_set_size(stream->events, 0);
899 stream->flushed_packet_count++;
900end:
83509119 901 bt_put(integer);
273b65be
JG
902 return ret;
903}
904
905void bt_ctf_stream_get(struct bt_ctf_stream *stream)
906{
83509119 907 bt_get(stream);
273b65be
JG
908}
909
910void bt_ctf_stream_put(struct bt_ctf_stream *stream)
911{
83509119 912 bt_put(stream);
273b65be
JG
913}
914
915static
83509119 916void bt_ctf_stream_destroy(struct bt_object *obj)
273b65be
JG
917{
918 struct bt_ctf_stream *stream;
273b65be 919
83509119 920 stream = container_of(obj, struct bt_ctf_stream, base);
273b65be 921 ctf_fini_pos(&stream->pos);
0e45d308 922 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
9f56e450
JG
923 perror("close");
924 }
12c8a1a3 925
12c8a1a3
JG
926 if (stream->events) {
927 g_ptr_array_free(stream->events, TRUE);
928 }
b71d7298
PP
929
930 if (stream->name) {
931 g_string_free(stream->name, TRUE);
932 }
83509119
JG
933 bt_put(stream->packet_header);
934 bt_put(stream->packet_context);
273b65be
JG
935 g_free(stream);
936}
937
273b65be
JG
938static
939int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
940 uint64_t value)
941{
942 int ret = 0;
d7b1ea66 943 struct bt_ctf_field_type *field_type = NULL;
273b65be
JG
944 struct bt_ctf_field *integer =
945 bt_ctf_field_structure_get_field(structure, name);
12c8a1a3
JG
946
947 if (!structure || !name) {
273b65be
JG
948 ret = -1;
949 goto end;
950 }
951
12c8a1a3
JG
952 if (!integer) {
953 /* Field not found, not an error. */
954 goto end;
955 }
956
957 /* Make sure the payload has not already been set. */
958 if (!bt_ctf_field_validate(integer)) {
959 /* Payload already set, not an error */
960 goto end;
961 }
962
d7b1ea66
JG
963 field_type = bt_ctf_field_get_type(integer);
964 /* Something is serioulsly wrong */
965 assert(field_type);
9a19a512 966 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
d7b1ea66
JG
967 /*
968 * The user most likely meant for us to populate this field
969 * automatically. However, we can only do this if the field
970 * is an integer. Return an error.
971 */
972 ret = -1;
973 goto end;
974 }
975
976 if (bt_ctf_field_type_integer_get_signed(field_type)) {
977 ret = bt_ctf_field_signed_integer_set_value(integer,
978 (int64_t) value);
979 } else {
980 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
981 }
273b65be 982end:
83509119
JG
983 bt_put(integer);
984 bt_put(field_type);
273b65be
JG
985 return ret;
986}
b71d7298
PP
987
988const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
989{
990 const char *name = NULL;
991
992 if (!stream) {
993 goto end;
994 }
995
996 name = stream->name ? stream->name->str : NULL;
997
998end:
999 return name;
1000}
This page took 0.073477 seconds and 4 git commands to generate.