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