Standardize *get_*() functions
[babeltrace.git] / lib / ctf-ir / stream.c
1 /*
2 * stream.c
3 *
4 * Babeltrace CTF IR - Stream
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/ctf-ir/clock-class.h>
30 #include <babeltrace/ctf-writer/clock.h>
31 #include <babeltrace/ctf-writer/clock-internal.h>
32 #include <babeltrace/ctf-writer/event.h>
33 #include <babeltrace/ctf-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/fields-internal.h>
36 #include <babeltrace/ctf-ir/stream.h>
37 #include <babeltrace/ctf-ir/stream-internal.h>
38 #include <babeltrace/ctf-ir/stream-class-internal.h>
39 #include <babeltrace/ctf-ir/trace.h>
40 #include <babeltrace/ctf-ir/trace-internal.h>
41 #include <babeltrace/ctf-writer/writer-internal.h>
42 #include <babeltrace/graph/component-internal.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/ctf-writer/functor-internal.h>
45 #include <babeltrace/compiler-internal.h>
46 #include <babeltrace/align-internal.h>
47 #include <inttypes.h>
48
49 static
50 void bt_ctf_stream_destroy(struct bt_object *obj);
51 static
52 int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
53 static
54 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
55
56 static
57 int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
58 {
59 int ret = 0;
60 struct bt_ctf_field_type *field_type = NULL;
61
62 if (!field) {
63 ret = -1;
64 goto end;
65 }
66
67 field_type = bt_ctf_field_get_type(field);
68 assert(field_type);
69
70 if (bt_ctf_field_type_get_type_id(field_type) !=
71 BT_CTF_FIELD_TYPE_ID_INTEGER) {
72 /* Not an integer and the value is unset, error. */
73 ret = -1;
74 goto end;
75 }
76
77 if (bt_ctf_field_type_integer_get_signed(field_type)) {
78 ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value);
79 if (ret) {
80 /* Value is out of range, error. */
81 goto end;
82 }
83 } else {
84 ret = bt_ctf_field_unsigned_integer_set_value(field, value);
85 if (ret) {
86 /* Value is out of range, error. */
87 goto end;
88 }
89 }
90 end:
91 bt_put(field_type);
92 return ret;
93 }
94
95 static
96 int set_packet_header_magic(struct bt_ctf_stream *stream)
97 {
98 int ret = 0;
99 struct bt_ctf_field_type *magic_field_type = NULL;
100 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
101 stream->packet_header, "magic");
102
103 if (!magic_field) {
104 /* No magic field found. Not an error, skip. */
105 goto end;
106 }
107
108 if (bt_ctf_field_is_set(magic_field)) {
109 /* Value already set. Not an error, skip. */
110 goto end;
111 }
112
113 magic_field_type = bt_ctf_field_get_type(magic_field);
114 assert(magic_field_type);
115
116 if (bt_ctf_field_type_get_type_id(magic_field_type) !=
117 BT_CTF_FIELD_TYPE_ID_INTEGER) {
118 /* Magic field is not an integer. Not an error, skip. */
119 goto end;
120 }
121
122 if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) {
123 /*
124 * Magic field is not of the expected size.
125 * Not an error, skip.
126 */
127 goto end;
128 }
129
130 ret = bt_ctf_field_type_integer_get_signed(magic_field_type);
131 assert(ret >= 0);
132 if (ret) {
133 ret = bt_ctf_field_signed_integer_set_value(magic_field,
134 (int64_t) 0xC1FC1FC1);
135 } else {
136 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
137 (uint64_t) 0xC1FC1FC1);
138 }
139 end:
140 bt_put(magic_field);
141 bt_put(magic_field_type);
142 return ret;
143 }
144
145 static
146 int set_packet_header_uuid(struct bt_ctf_stream *stream)
147 {
148 int i, ret = 0;
149 struct bt_ctf_trace *trace = NULL;
150 struct bt_ctf_field_type *uuid_field_type = NULL;
151 struct bt_ctf_field_type *element_field_type = NULL;
152 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field(
153 stream->packet_header, "uuid");
154
155 if (!uuid_field) {
156 /* No uuid field found. Not an error, skip. */
157 goto end;
158 }
159
160 if (bt_ctf_field_is_set(uuid_field)) {
161 /* Value already set. Not an error, skip. */
162 goto end;
163 }
164
165 uuid_field_type = bt_ctf_field_get_type(uuid_field);
166 assert(uuid_field_type);
167 if (bt_ctf_field_type_get_type_id(uuid_field_type) !=
168 BT_CTF_FIELD_TYPE_ID_ARRAY) {
169 /* UUID field is not an array. Not an error, skip. */
170 goto end;
171 }
172
173 if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) {
174 /*
175 * UUID field is not of the expected size.
176 * Not an error, skip.
177 */
178 goto end;
179 }
180
181 element_field_type = bt_ctf_field_type_array_get_element_type(
182 uuid_field_type);
183 assert(element_field_type);
184 if (bt_ctf_field_type_get_type_id(element_field_type) !=
185 BT_CTF_FIELD_TYPE_ID_INTEGER) {
186 /* UUID array elements are not integers. Not an error, skip */
187 goto end;
188 }
189
190 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
191 for (i = 0; i < 16; i++) {
192 struct bt_ctf_field *uuid_element =
193 bt_ctf_field_array_get_field(uuid_field, i);
194
195 ret = bt_ctf_field_type_integer_get_signed(element_field_type);
196 assert(ret >= 0);
197
198 if (ret) {
199 ret = bt_ctf_field_signed_integer_set_value(
200 uuid_element, (int64_t) trace->uuid[i]);
201 } else {
202 ret = bt_ctf_field_unsigned_integer_set_value(
203 uuid_element, (uint64_t) trace->uuid[i]);
204 }
205 bt_put(uuid_element);
206 if (ret) {
207 goto end;
208 }
209 }
210
211 end:
212 bt_put(uuid_field);
213 bt_put(uuid_field_type);
214 bt_put(element_field_type);
215 BT_PUT(trace);
216 return ret;
217 }
218 static
219 int set_packet_header_stream_id(struct bt_ctf_stream *stream)
220 {
221 int ret = 0;
222 uint32_t stream_id;
223 struct bt_ctf_field_type *stream_id_field_type = NULL;
224 struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field(
225 stream->packet_header, "stream_id");
226
227 if (!stream_id_field) {
228 /* No stream_id field found. Not an error, skip. */
229 goto end;
230 }
231
232 if (bt_ctf_field_is_set(stream_id_field)) {
233 /* Value already set. Not an error, skip. */
234 goto end;
235 }
236
237 stream_id_field_type = bt_ctf_field_get_type(stream_id_field);
238 assert(stream_id_field_type);
239 if (bt_ctf_field_type_get_type_id(stream_id_field_type) !=
240 BT_CTF_FIELD_TYPE_ID_INTEGER) {
241 /* stream_id field is not an integer. Not an error, skip. */
242 goto end;
243 }
244
245 stream_id = stream->stream_class->id;
246 ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type);
247 assert(ret >= 0);
248 if (ret) {
249 ret = bt_ctf_field_signed_integer_set_value(stream_id_field,
250 (int64_t) stream_id);
251 } else {
252 ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field,
253 (uint64_t) stream_id);
254 }
255 end:
256 bt_put(stream_id_field);
257 bt_put(stream_id_field_type);
258 return ret;
259 }
260
261 static
262 int set_packet_header(struct bt_ctf_stream *stream)
263 {
264 int ret;
265
266 ret = set_packet_header_magic(stream);
267 if (ret) {
268 goto end;
269 }
270
271 ret = set_packet_header_uuid(stream);
272 if (ret) {
273 goto end;
274 }
275
276 ret = set_packet_header_stream_id(stream);
277 if (ret) {
278 goto end;
279 }
280 end:
281 return ret;
282 }
283
284 static
285 void release_event(struct bt_ctf_event *event)
286 {
287 if (bt_object_get_ref_count(event)) {
288 /*
289 * The event is being orphaned, but it must guarantee the
290 * existence of its event class for the duration of its
291 * lifetime.
292 */
293 bt_get(event->event_class);
294 BT_PUT(event->base.parent);
295 } else {
296 bt_object_release(event);
297 }
298 }
299
300 static
301 int create_stream_file(struct bt_ctf_writer *writer,
302 struct bt_ctf_stream *stream)
303 {
304 int fd;
305 GString *filename = g_string_new(stream->stream_class->name->str);
306
307 if (stream->stream_class->name->len == 0) {
308 int64_t ret;
309
310 ret = bt_ctf_stream_class_get_id(stream->stream_class);
311 if (ret < 0) {
312 fd = -1;
313 goto error;
314 }
315
316 g_string_printf(filename, "stream_%" PRId64, ret);
317 }
318
319 g_string_append_printf(filename, "_%" PRIu32, stream->id);
320 fd = openat(writer->trace_dir_fd, filename->str,
321 O_RDWR | O_CREAT | O_TRUNC,
322 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
323 error:
324 g_string_free(filename, TRUE);
325 return fd;
326 }
327
328 static
329 int set_stream_fd(struct bt_ctf_stream *stream, int fd)
330 {
331 int ret = 0;
332
333 if (stream->pos.fd != -1) {
334 ret = -1;
335 goto end;
336 }
337
338 (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR);
339 stream->pos.fd = fd;
340 end:
341 return ret;
342 }
343
344 static
345 void component_destroy_listener(struct bt_component *component, void *data)
346 {
347 struct bt_ctf_stream *stream = data;
348
349 g_hash_table_remove(stream->comp_cur_port, component);
350 }
351
352 struct bt_ctf_stream *bt_ctf_stream_create(
353 struct bt_ctf_stream_class *stream_class,
354 const char *name)
355 {
356 int ret;
357 struct bt_ctf_stream *stream = NULL;
358 struct bt_ctf_trace *trace = NULL;
359 struct bt_ctf_writer *writer = NULL;
360
361 if (!stream_class) {
362 goto error;
363 }
364
365 trace = bt_ctf_stream_class_get_trace(stream_class);
366 if (!trace) {
367 goto error;
368 }
369
370 if (bt_ctf_trace_is_static(trace)) {
371 /*
372 * A static trace has the property that all its stream
373 * classes, clock classes, and streams are definitive:
374 * no more can be added, and each object is also frozen.
375 */
376 goto error;
377 }
378
379 stream = g_new0(struct bt_ctf_stream, 1);
380 if (!stream) {
381 goto error;
382 }
383
384 bt_object_init(stream, bt_ctf_stream_destroy);
385 /*
386 * Acquire reference to parent since stream will become publicly
387 * reachable; it needs its parent to remain valid.
388 */
389 bt_object_set_parent(stream, trace);
390 stream->id = stream_class->next_stream_id++;
391 stream->stream_class = stream_class;
392 stream->pos.fd = -1;
393
394 stream->destroy_listeners = g_array_new(FALSE, TRUE,
395 sizeof(struct bt_ctf_stream_destroy_listener));
396 if (!stream->destroy_listeners) {
397 goto error;
398 }
399
400 if (name) {
401 stream->name = g_string_new(name);
402 if (!stream->name) {
403 goto error;
404 }
405 }
406
407 if (trace->is_created_by_writer) {
408 int fd;
409 writer = (struct bt_ctf_writer *)
410 bt_object_get_parent(trace);
411
412 assert(writer);
413 if (stream_class->packet_context_type) {
414 stream->packet_context = bt_ctf_field_create(
415 stream_class->packet_context_type);
416 if (!stream->packet_context) {
417 goto error;
418 }
419
420 /* Initialize events_discarded */
421 ret = try_set_structure_field_integer(
422 stream->packet_context,
423 "events_discarded", 0);
424 if (ret != 1) {
425 goto error;
426 }
427 }
428
429 stream->events = g_ptr_array_new_with_free_func(
430 (GDestroyNotify) release_event);
431 if (!stream->events) {
432 goto error;
433 }
434
435 /* A trace is not allowed to have a NULL packet header */
436 assert(trace->packet_header_type);
437 stream->packet_header =
438 bt_ctf_field_create(trace->packet_header_type);
439 if (!stream->packet_header) {
440 goto error;
441 }
442
443 /*
444 * Attempt to populate the default trace packet header fields
445 * (magic, uuid and stream_id). This will _not_ fail shall the
446 * fields not be found or be of an incompatible type; they will
447 * simply not be populated automatically. The user will have to
448 * make sure to set the trace packet header fields himself
449 * before flushing.
450 */
451 ret = set_packet_header(stream);
452 if (ret) {
453 goto error;
454 }
455
456 /* Create file associated with this stream */
457 fd = create_stream_file(writer, stream);
458 if (fd < 0) {
459 goto error;
460 }
461
462 ret = set_stream_fd(stream, fd);
463 if (ret) {
464 goto error;
465 }
466
467 /* Freeze the writer */
468 bt_ctf_writer_freeze(writer);
469 } else {
470 /* Non-writer stream indicated by a negative FD */
471 ret = set_stream_fd(stream, -1);
472 if (ret) {
473 goto error;
474 }
475
476 stream->comp_cur_port = g_hash_table_new(g_direct_hash,
477 g_direct_equal);
478 if (!stream->comp_cur_port) {
479 goto error;
480 }
481 }
482
483 /* Add this stream to the trace's streams */
484 g_ptr_array_add(trace->streams, stream);
485
486 BT_PUT(trace);
487 BT_PUT(writer);
488 return stream;
489 error:
490 BT_PUT(stream);
491 BT_PUT(trace);
492 BT_PUT(writer);
493 return stream;
494 }
495
496 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
497 struct bt_ctf_stream *stream)
498 {
499 struct bt_ctf_stream_class *stream_class = NULL;
500
501 if (!stream) {
502 goto end;
503 }
504
505 stream_class = stream->stream_class;
506 bt_get(stream_class);
507 end:
508 return stream_class;
509 }
510
511 int bt_ctf_stream_get_discarded_events_count(
512 struct bt_ctf_stream *stream, uint64_t *count)
513 {
514 int64_t ret = 0;
515 int field_signed;
516 struct bt_ctf_field *events_discarded_field = NULL;
517 struct bt_ctf_field_type *events_discarded_field_type = NULL;
518
519 if (!stream || !count || !stream->packet_context ||
520 stream->pos.fd < 0) {
521 ret = -1;
522 goto end;
523 }
524
525 events_discarded_field = bt_ctf_field_structure_get_field(
526 stream->packet_context, "events_discarded");
527 if (!events_discarded_field) {
528 ret = -1;
529 goto end;
530 }
531
532 events_discarded_field_type = bt_ctf_field_get_type(
533 events_discarded_field);
534 if (!events_discarded_field_type) {
535 ret = -1;
536 goto end;
537 }
538
539 field_signed = bt_ctf_field_type_integer_get_signed(
540 events_discarded_field_type);
541 if (field_signed < 0) {
542 ret = field_signed;
543 goto end;
544 }
545
546 if (field_signed) {
547 int64_t signed_count;
548
549 ret = bt_ctf_field_signed_integer_get_value(
550 events_discarded_field, &signed_count);
551 if (ret) {
552 goto end;
553 }
554 if (signed_count < 0) {
555 /* Invalid value */
556 ret = -1;
557 goto end;
558 }
559 *count = (uint64_t) signed_count;
560 } else {
561 ret = bt_ctf_field_unsigned_integer_get_value(
562 events_discarded_field, count);
563 if (ret) {
564 goto end;
565 }
566 }
567 end:
568 bt_put(events_discarded_field);
569 bt_put(events_discarded_field_type);
570 return ret;
571 }
572
573 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
574 uint64_t event_count)
575 {
576 int ret;
577 int field_signed;
578 uint64_t previous_count;
579 uint64_t new_count;
580 struct bt_ctf_field *events_discarded_field = NULL;
581 struct bt_ctf_field_type *events_discarded_field_type = NULL;
582
583 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
584 goto end;
585 }
586
587 ret = bt_ctf_stream_get_discarded_events_count(stream,
588 &previous_count);
589 if (ret) {
590 goto end;
591 }
592
593 events_discarded_field = bt_ctf_field_structure_get_field(
594 stream->packet_context, "events_discarded");
595 if (!events_discarded_field) {
596 goto end;
597 }
598
599 events_discarded_field_type = bt_ctf_field_get_type(
600 events_discarded_field);
601 if (!events_discarded_field_type) {
602 goto end;
603 }
604
605 field_signed = bt_ctf_field_type_integer_get_signed(
606 events_discarded_field_type);
607 if (field_signed < 0) {
608 goto end;
609 }
610
611 new_count = previous_count + event_count;
612 assert(new_count >= previous_count);
613 if (field_signed) {
614 ret = bt_ctf_field_signed_integer_set_value(
615 events_discarded_field, (int64_t) new_count);
616 if (ret) {
617 goto end;
618 }
619 } else {
620 ret = bt_ctf_field_unsigned_integer_set_value(
621 events_discarded_field, new_count);
622 if (ret) {
623 goto end;
624 }
625 }
626
627 end:
628 bt_put(events_discarded_field);
629 bt_put(events_discarded_field_type);
630 }
631
632 static int auto_populate_event_header(struct bt_ctf_stream *stream,
633 struct bt_ctf_event *event)
634 {
635 int ret = 0;
636 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
637 struct bt_ctf_clock_class *mapped_clock_class = NULL;
638
639 if (!event || event->frozen) {
640 ret = -1;
641 goto end;
642 }
643
644 /*
645 * The condition to automatically set the ID are:
646 *
647 * 1. The event header field "id" exists and is an integer
648 * field.
649 * 2. The event header field "id" is NOT set.
650 */
651 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
652 if (id_field && !bt_ctf_field_is_set(id_field)) {
653 ret = set_integer_field_value(id_field,
654 (uint64_t) bt_ctf_event_class_get_id(
655 event->event_class));
656 if (ret) {
657 goto end;
658 }
659 }
660
661 /*
662 * The conditions to automatically set the timestamp are:
663 *
664 * 1. The event header field "timestamp" exists and is an
665 * integer field.
666 * 2. This stream's class has a registered clock (set with
667 * bt_ctf_stream_class_set_clock()).
668 * 3. The event header field "timestamp" has its type mapped to
669 * a clock class which is also the clock class of this
670 * stream's class's registered clock.
671 * 4. The event header field "timestamp" is NOT set.
672 */
673 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
674 "timestamp");
675 if (timestamp_field && !bt_ctf_field_is_set(timestamp_field) &&
676 stream->stream_class->clock) {
677 struct bt_ctf_clock_class *stream_class_clock_class =
678 stream->stream_class->clock->clock_class;
679 struct bt_ctf_field_type *timestamp_field_type =
680 bt_ctf_field_get_type(timestamp_field);
681
682 assert(timestamp_field_type);
683 mapped_clock_class =
684 bt_ctf_field_type_integer_get_mapped_clock_class(
685 timestamp_field_type);
686 BT_PUT(timestamp_field_type);
687 if (mapped_clock_class == stream_class_clock_class) {
688 uint64_t timestamp;
689
690 ret = bt_ctf_clock_get_value(
691 stream->stream_class->clock,
692 &timestamp);
693 if (ret) {
694 goto end;
695 }
696
697 ret = set_integer_field_value(timestamp_field,
698 timestamp);
699 if (ret) {
700 goto end;
701 }
702 }
703 }
704
705 end:
706 bt_put(id_field);
707 bt_put(timestamp_field);
708 bt_put(mapped_clock_class);
709 return ret;
710 }
711
712 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
713 struct bt_ctf_event *event)
714 {
715 int ret = 0;
716
717 if (!stream || !event || stream->pos.fd < 0) {
718 ret = -1;
719 goto end;
720 }
721
722 /*
723 * The event is not supposed to have a parent stream at this
724 * point. The only other way an event can have a parent stream
725 * is if it was assigned when setting a packet to the event,
726 * in which case the packet's stream is not a writer stream,
727 * and thus the user is trying to append an event which belongs
728 * to another stream.
729 */
730 if (event->base.parent) {
731 ret = -1;
732 goto end;
733 }
734
735 bt_object_set_parent(event, stream);
736 ret = auto_populate_event_header(stream, event);
737 if (ret) {
738 goto error;
739 }
740
741 /* Make sure the various scopes of the event are set */
742 ret = bt_ctf_event_validate(event);
743 if (ret) {
744 goto error;
745 }
746
747 /* Save the new event and freeze it */
748 bt_ctf_event_freeze(event);
749 g_ptr_array_add(stream->events, event);
750
751 /*
752 * Event had to hold a reference to its event class as long as it wasn't
753 * part of the same trace hierarchy. From now on, the event and its
754 * class share the same lifetime guarantees and the reference is no
755 * longer needed.
756 */
757 bt_put(event->event_class);
758
759 end:
760 return ret;
761
762 error:
763 /*
764 * Orphan the event; we were not successful in associating it to
765 * a stream.
766 */
767 bt_object_set_parent(event, NULL);
768
769 return ret;
770 }
771
772 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
773 struct bt_ctf_stream *stream)
774 {
775 struct bt_ctf_field *packet_context = NULL;
776
777 if (!stream || stream->pos.fd < 0) {
778 goto end;
779 }
780
781 packet_context = stream->packet_context;
782 if (packet_context) {
783 bt_get(packet_context);
784 }
785 end:
786 return packet_context;
787 }
788
789 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
790 struct bt_ctf_field *field)
791 {
792 int ret = 0;
793 struct bt_ctf_field_type *field_type;
794
795 if (!stream || stream->pos.fd < 0) {
796 ret = -1;
797 goto end;
798 }
799
800 field_type = bt_ctf_field_get_type(field);
801 if (bt_ctf_field_type_compare(field_type,
802 stream->stream_class->packet_context_type)) {
803 ret = -1;
804 goto end;
805 }
806
807 bt_put(field_type);
808 bt_put(stream->packet_context);
809 stream->packet_context = bt_get(field);
810 end:
811 return ret;
812 }
813
814 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
815 struct bt_ctf_stream *stream)
816 {
817 struct bt_ctf_field *packet_header = NULL;
818
819 if (!stream || stream->pos.fd < 0) {
820 goto end;
821 }
822
823 packet_header = stream->packet_header;
824 if (packet_header) {
825 bt_get(packet_header);
826 }
827 end:
828 return packet_header;
829 }
830
831 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
832 struct bt_ctf_field *field)
833 {
834 int ret = 0;
835 struct bt_ctf_trace *trace = NULL;
836 struct bt_ctf_field_type *field_type = NULL;
837
838 if (!stream || stream->pos.fd < 0) {
839 ret = -1;
840 goto end;
841 }
842
843 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
844 field_type = bt_ctf_field_get_type(field);
845 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
846 ret = -1;
847 goto end;
848 }
849
850 bt_put(stream->packet_header);
851 stream->packet_header = bt_get(field);
852 end:
853 BT_PUT(trace);
854 bt_put(field_type);
855 return ret;
856 }
857
858 static
859 int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
860 {
861 int ret = 0;
862 struct bt_ctf_field *timestamp_field = NULL;
863 struct bt_ctf_field_type *timestamp_field_type = NULL;
864
865 timestamp_field = bt_ctf_field_structure_get_field(event_header,
866 "timestamp");
867 if (!timestamp_field) {
868 ret = -1;
869 goto end;
870 }
871
872 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
873 assert(timestamp_field_type);
874 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
875 BT_CTF_FIELD_TYPE_ID_INTEGER) {
876 ret = -1;
877 goto end;
878 }
879
880 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
881 int64_t val;
882
883 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
884 &val);
885 if (ret) {
886 goto end;
887 }
888 *timestamp = (uint64_t) val;
889 } else {
890 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
891 timestamp);
892 if (ret) {
893 goto end;
894 }
895 }
896 end:
897 bt_put(timestamp_field);
898 bt_put(timestamp_field_type);
899 return ret;
900 }
901
902 static
903 void reset_structure_field(struct bt_ctf_field *structure, const char *name)
904 {
905 struct bt_ctf_field *member;
906
907 member = bt_ctf_field_structure_get_field(structure, name);
908 assert(member);
909 (void) bt_ctf_field_reset(member);
910 bt_put(member);
911 }
912
913 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
914 {
915 int ret = 0;
916 size_t i;
917 uint64_t timestamp_begin, timestamp_end;
918 struct bt_ctf_field *integer = NULL;
919 struct bt_ctf_stream_pos packet_context_pos;
920 struct bt_ctf_trace *trace;
921 enum bt_ctf_byte_order native_byte_order;
922 bool empty_packet;
923 uint64_t packet_size_bits;
924 struct {
925 bool timestamp_begin;
926 bool timestamp_end;
927 bool content_size;
928 bool packet_size;
929 } auto_set_fields = { 0 };
930
931 if (!stream || stream->pos.fd < 0) {
932 /*
933 * Stream does not have an associated fd. It is,
934 * therefore, not a stream being used to write events.
935 */
936 ret = -1;
937 goto end;
938 }
939
940 if (!stream->packet_context && stream->flushed_packet_count > 0) {
941 /*
942 * A stream without a packet context, and thus without
943 * content and packet size members, can't have more than
944 * one packet.
945 */
946 ret = -1;
947 goto end;
948 }
949
950 trace = bt_ctf_stream_class_borrow_trace(stream->stream_class);
951 assert(trace);
952 native_byte_order = bt_ctf_trace_get_byte_order(trace);
953 empty_packet = (stream->events->len == 0);
954
955 /* mmap the next packet */
956 bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR);
957 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos,
958 native_byte_order);
959 if (ret) {
960 goto end;
961 }
962
963 if (stream->packet_context) {
964 /* Set the default context attributes if present and unset. */
965 if (!empty_packet && !get_event_header_timestamp(
966 ((struct bt_ctf_event *) g_ptr_array_index(
967 stream->events, 0))->event_header, &timestamp_begin)) {
968 ret = try_set_structure_field_integer(
969 stream->packet_context,
970 "timestamp_begin", timestamp_begin);
971 if (ret < 0) {
972 goto end;
973 }
974 auto_set_fields.timestamp_begin = ret == 1;
975 }
976
977 if (!empty_packet && !get_event_header_timestamp(
978 ((struct bt_ctf_event *) g_ptr_array_index(
979 stream->events, stream->events->len - 1))->event_header,
980 &timestamp_end)) {
981
982 ret = try_set_structure_field_integer(
983 stream->packet_context,
984 "timestamp_end", timestamp_end);
985 if (ret < 0) {
986 goto end;
987 }
988 auto_set_fields.timestamp_end = ret == 1;
989 }
990 ret = try_set_structure_field_integer(stream->packet_context,
991 "content_size", UINT64_MAX);
992 if (ret < 0) {
993 goto end;
994 }
995 auto_set_fields.content_size = ret == 1;
996
997 ret = try_set_structure_field_integer(stream->packet_context,
998 "packet_size", UINT64_MAX);
999 if (ret < 0) {
1000 goto end;
1001 }
1002 auto_set_fields.packet_size = ret == 1;
1003
1004 /* Write packet context */
1005 memcpy(&packet_context_pos, &stream->pos,
1006 sizeof(packet_context_pos));
1007 ret = bt_ctf_field_serialize(stream->packet_context,
1008 &stream->pos, native_byte_order);
1009 if (ret) {
1010 goto end;
1011 }
1012 }
1013
1014 for (i = 0; i < stream->events->len; i++) {
1015 struct bt_ctf_event *event = g_ptr_array_index(
1016 stream->events, i);
1017
1018 /* Write event header */
1019 ret = bt_ctf_field_serialize(event->event_header,
1020 &stream->pos, native_byte_order);
1021 if (ret) {
1022 goto end;
1023 }
1024
1025 /* Write stream event context */
1026 if (event->stream_event_context) {
1027 ret = bt_ctf_field_serialize(
1028 event->stream_event_context, &stream->pos,
1029 native_byte_order);
1030 if (ret) {
1031 goto end;
1032 }
1033 }
1034
1035 /* Write event content */
1036 ret = bt_ctf_event_serialize(event, &stream->pos,
1037 native_byte_order);
1038 if (ret) {
1039 goto end;
1040 }
1041 }
1042
1043 /* Rounded-up in case content_size is not byte-aligned. */
1044 packet_size_bits = (stream->pos.offset + (CHAR_BIT - 1)) &
1045 ~(CHAR_BIT - 1);
1046 stream->pos.packet_size = packet_size_bits;
1047
1048 if (stream->packet_context) {
1049 /*
1050 * Update the packet total size and content size and overwrite
1051 * the packet context.
1052 * Copy base_mma as the packet may have been remapped (e.g. when
1053 * a packet is resized).
1054 */
1055 packet_context_pos.base_mma = stream->pos.base_mma;
1056 if (auto_set_fields.content_size) {
1057 ret = set_structure_field_integer(
1058 stream->packet_context,
1059 "content_size", stream->pos.offset);
1060 if (ret < 0) {
1061 goto end;
1062 }
1063 }
1064
1065 if (auto_set_fields.packet_size) {
1066 ret = set_structure_field_integer(stream->packet_context,
1067 "packet_size", packet_size_bits);
1068 if (ret < 0) {
1069 goto end;
1070 }
1071 }
1072
1073 ret = bt_ctf_field_serialize(stream->packet_context,
1074 &packet_context_pos, native_byte_order);
1075 if (ret) {
1076 goto end;
1077 }
1078 }
1079
1080 g_ptr_array_set_size(stream->events, 0);
1081 stream->flushed_packet_count++;
1082 stream->size += packet_size_bits / CHAR_BIT;
1083 end:
1084 /* Reset automatically-set fields. */
1085 if (auto_set_fields.timestamp_begin) {
1086 reset_structure_field(stream->packet_context,
1087 "timestamp_begin");
1088 }
1089 if (auto_set_fields.timestamp_end) {
1090 reset_structure_field(stream->packet_context,
1091 "timestamp_end");
1092 }
1093 if (auto_set_fields.packet_size) {
1094 reset_structure_field(stream->packet_context,
1095 "packet_size");
1096 }
1097 if (auto_set_fields.content_size) {
1098 reset_structure_field(stream->packet_context,
1099 "content_size");
1100 }
1101 bt_put(integer);
1102
1103 if (ret < 0) {
1104 /*
1105 * We failed to write the packet. Its size is therefore set to 0
1106 * to ensure the next mapping is done in the same place rather
1107 * than advancing by "stream->pos.packet_size", which would
1108 * leave a corrupted packet in the trace.
1109 */
1110 stream->pos.packet_size = 0;
1111 }
1112 return ret;
1113 }
1114
1115 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
1116 {
1117 bt_get(stream);
1118 }
1119
1120 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
1121 {
1122 bt_put(stream);
1123 }
1124
1125 static
1126 void bt_ctf_stream_destroy(struct bt_object *obj)
1127 {
1128 struct bt_ctf_stream *stream;
1129 int i;
1130
1131 stream = container_of(obj, struct bt_ctf_stream, base);
1132
1133 /* Call destroy listeners in reverse registration order */
1134 for (i = stream->destroy_listeners->len - 1; i >= 0; i--) {
1135 struct bt_ctf_stream_destroy_listener *listener =
1136 &g_array_index(stream->destroy_listeners,
1137 struct bt_ctf_stream_destroy_listener, i);
1138
1139 listener->func(stream, listener->data);
1140 }
1141
1142 (void) bt_ctf_stream_pos_fini(&stream->pos);
1143 if (stream->pos.fd >= 0) {
1144 int ret;
1145
1146 /*
1147 * Truncate the file's size to the minimum required to fit the
1148 * last packet as we might have grown it too much on the last
1149 * mmap.
1150 */
1151 do {
1152 ret = ftruncate(stream->pos.fd, stream->size);
1153 } while (ret == -1 && errno == EINTR);
1154 if (ret) {
1155 perror("ftruncate");
1156 }
1157
1158 if (close(stream->pos.fd)) {
1159 perror("close");
1160 }
1161 }
1162
1163 if (stream->events) {
1164 g_ptr_array_free(stream->events, TRUE);
1165 }
1166
1167 if (stream->name) {
1168 g_string_free(stream->name, TRUE);
1169 }
1170
1171 if (stream->comp_cur_port) {
1172 GHashTableIter ht_iter;
1173 gpointer comp_gptr, port_gptr;
1174
1175 /*
1176 * Since we're destroying the stream, remove the destroy
1177 * listeners that it registered for each component in
1178 * its component-port mapping hash table. Otherwise they
1179 * would be called and the stream would be accessed once
1180 * it's freed or another stream would be accessed.
1181 */
1182 g_hash_table_iter_init(&ht_iter, stream->comp_cur_port);
1183
1184 while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) {
1185 assert(comp_gptr);
1186 bt_component_remove_destroy_listener((void *) comp_gptr,
1187 component_destroy_listener, stream);
1188 }
1189
1190 g_hash_table_destroy(stream->comp_cur_port);
1191 }
1192
1193 if (stream->destroy_listeners) {
1194 g_array_free(stream->destroy_listeners, TRUE);
1195 }
1196
1197 bt_put(stream->packet_header);
1198 bt_put(stream->packet_context);
1199 g_free(stream);
1200 }
1201
1202 static
1203 int _set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1204 uint64_t value, bool force)
1205 {
1206 int ret = 0;
1207 struct bt_ctf_field_type *field_type = NULL;
1208 struct bt_ctf_field *integer =
1209 bt_ctf_field_structure_get_field(structure, name);
1210
1211 if (!structure || !name) {
1212 ret = -1;
1213 goto end;
1214 }
1215
1216 if (!integer) {
1217 /* Field not found, not an error. */
1218 goto end;
1219 }
1220
1221 /* Make sure the payload has not already been set. */
1222 if (!force && bt_ctf_field_is_set(integer)) {
1223 /* Payload already set, not an error */
1224 goto end;
1225 }
1226
1227 field_type = bt_ctf_field_get_type(integer);
1228 assert(field_type);
1229 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1230 /*
1231 * The user most likely meant for us to populate this field
1232 * automatically. However, we can only do this if the field
1233 * is an integer. Return an error.
1234 */
1235 ret = -1;
1236 goto end;
1237 }
1238
1239 if (bt_ctf_field_type_integer_get_signed(field_type)) {
1240 ret = bt_ctf_field_signed_integer_set_value(integer,
1241 (int64_t) value);
1242 } else {
1243 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
1244 }
1245 ret = !ret ? 1 : ret;
1246 end:
1247 bt_put(integer);
1248 bt_put(field_type);
1249 return ret;
1250 }
1251
1252 static
1253 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1254 uint64_t value)
1255 {
1256 return _set_structure_field_integer(structure, name, value, true);
1257 }
1258
1259 /*
1260 * Returns the following codes:
1261 * 1 if the field was found and set,
1262 * 0 if nothing was done (field not found, or was already set),
1263 * <0 if an error was encoutered
1264 */
1265 static
1266 int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1267 uint64_t value)
1268 {
1269 return _set_structure_field_integer(structure, name, value, false);
1270 }
1271
1272 const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
1273 {
1274 const char *name = NULL;
1275
1276 if (!stream) {
1277 goto end;
1278 }
1279
1280 name = stream->name ? stream->name->str : NULL;
1281
1282 end:
1283 return name;
1284 }
1285
1286 int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream)
1287 {
1288 int ret = -1;
1289
1290 if (!stream) {
1291 goto end;
1292 }
1293
1294 ret = (stream->pos.fd >= 0);
1295
1296 end:
1297 return ret;
1298 }
1299
1300 BT_HIDDEN
1301 void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream *stream,
1302 struct bt_component *comp,
1303 struct bt_port *port)
1304 {
1305 assert(stream);
1306 assert(comp);
1307 assert(port);
1308 assert(stream->comp_cur_port);
1309
1310 /*
1311 * Do not take a reference to the component here because we
1312 * don't want the component to exist as long as this stream
1313 * exists. Instead, keep a weak reference, but add a destroy
1314 * listener so that we remove this hash table entry when we know
1315 * the component is destroyed.
1316 */
1317 bt_component_add_destroy_listener(comp, component_destroy_listener,
1318 stream);
1319 g_hash_table_insert(stream->comp_cur_port, comp, port);
1320 }
1321
1322 BT_HIDDEN
1323 struct bt_port *bt_ctf_stream_port_for_component(struct bt_ctf_stream *stream,
1324 struct bt_component *comp)
1325 {
1326 assert(stream);
1327 assert(comp);
1328 assert(stream->comp_cur_port);
1329 return g_hash_table_lookup(stream->comp_cur_port, comp);
1330 }
1331
1332 BT_HIDDEN
1333 void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream *stream,
1334 bt_ctf_stream_destroy_listener_func func, void *data)
1335 {
1336 struct bt_ctf_stream_destroy_listener listener;
1337
1338 assert(stream);
1339 assert(func);
1340 listener.func = func;
1341 listener.data = data;
1342 g_array_append_val(stream->destroy_listeners, listener);
1343 }
1344
1345 BT_HIDDEN
1346 void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream *stream,
1347 bt_ctf_stream_destroy_listener_func func, void *data)
1348 {
1349 size_t i;
1350
1351 assert(stream);
1352 assert(func);
1353
1354 for (i = 0; i < stream->destroy_listeners->len; i++) {
1355 struct bt_ctf_stream_destroy_listener *listener =
1356 &g_array_index(stream->destroy_listeners,
1357 struct bt_ctf_stream_destroy_listener, i);
1358
1359 if (listener->func == func && listener->data == data) {
1360 g_array_remove_index(stream->destroy_listeners, i);
1361 i--;
1362 }
1363 }
1364 }
This page took 0.055188 seconds and 5 git commands to generate.