fields.c: get non-existing field: log level WARN -> VERBOSE
[babeltrace.git] / lib / 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
ac0c6bdd
PP
29#include <babeltrace/ctf-ir/clock-class.h>
30#include <babeltrace/ctf-writer/clock.h>
31#include <babeltrace/ctf-writer/clock-internal.h>
273b65be 32#include <babeltrace/ctf-writer/event.h>
adc315b8 33#include <babeltrace/ctf-ir/event-internal.h>
2e33ac5a
PP
34#include <babeltrace/ctf-ir/field-types-internal.h>
35#include <babeltrace/ctf-ir/fields-internal.h>
3f043b05
JG
36#include <babeltrace/ctf-ir/stream.h>
37#include <babeltrace/ctf-ir/stream-internal.h>
adc315b8 38#include <babeltrace/ctf-ir/stream-class-internal.h>
5acf2ae6 39#include <babeltrace/ctf-ir/trace.h>
319fd969
PP
40#include <babeltrace/ctf-ir/trace-internal.h>
41#include <babeltrace/ctf-writer/writer-internal.h>
3230ee6b 42#include <babeltrace/graph/component-internal.h>
83509119 43#include <babeltrace/ref.h>
273b65be 44#include <babeltrace/ctf-writer/functor-internal.h>
3d9990ac
PP
45#include <babeltrace/compiler-internal.h>
46#include <babeltrace/align-internal.h>
dc3fffef 47#include <inttypes.h>
273b65be
JG
48
49static
83509119 50void bt_ctf_stream_destroy(struct bt_object *obj);
273b65be 51static
af9296f3
JG
52int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
53static
273b65be
JG
54int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
55
ac0c6bdd
PP
56static
57int 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) !=
1487a16a 71 BT_CTF_FIELD_TYPE_ID_INTEGER) {
ac0c6bdd
PP
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 }
90end:
91 bt_put(field_type);
92 return ret;
93}
94
d246b111
JG
95static
96int 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
fc47320a 108 if (bt_ctf_field_is_set(magic_field)) {
d246b111
JG
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) !=
1487a16a 117 BT_CTF_FIELD_TYPE_ID_INTEGER) {
d246b111
JG
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 }
139end:
83509119
JG
140 bt_put(magic_field);
141 bt_put(magic_field_type);
d246b111
JG
142 return ret;
143}
144
145static
146int set_packet_header_uuid(struct bt_ctf_stream *stream)
147{
148 int i, ret = 0;
e6a8e8e4 149 struct bt_ctf_trace *trace = NULL;
d246b111
JG
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
fc47320a 160 if (bt_ctf_field_is_set(uuid_field)) {
d246b111
JG
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) !=
1487a16a 168 BT_CTF_FIELD_TYPE_ID_ARRAY) {
d246b111
JG
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) !=
1487a16a 185 BT_CTF_FIELD_TYPE_ID_INTEGER) {
d246b111
JG
186 /* UUID array elements are not integers. Not an error, skip */
187 goto end;
188 }
189
e6a8e8e4 190 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
d246b111
JG
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(
e6a8e8e4 200 uuid_element, (int64_t) trace->uuid[i]);
d246b111
JG
201 } else {
202 ret = bt_ctf_field_unsigned_integer_set_value(
9ac68eb1 203 uuid_element, (uint64_t) trace->uuid[i]);
d246b111 204 }
83509119 205 bt_put(uuid_element);
d246b111
JG
206 if (ret) {
207 goto end;
208 }
209 }
210
211end:
83509119
JG
212 bt_put(uuid_field);
213 bt_put(uuid_field_type);
214 bt_put(element_field_type);
e6a8e8e4 215 BT_PUT(trace);
d246b111
JG
216 return ret;
217}
218static
219int 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
fc47320a 232 if (bt_ctf_field_is_set(stream_id_field)) {
d246b111
JG
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) !=
1487a16a 240 BT_CTF_FIELD_TYPE_ID_INTEGER) {
d246b111
JG
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 }
255end:
83509119
JG
256 bt_put(stream_id_field);
257 bt_put(stream_id_field_type);
d246b111
JG
258 return ret;
259}
260
261static
262int 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 }
280end:
281 return ret;
282}
283
123fbdec 284static
e6a8e8e4 285void release_event(struct bt_ctf_event *event)
123fbdec 286{
e6a8e8e4
JG
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 }
123fbdec
JG
298}
299
319fd969
PP
300static
301int 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);
323error:
324 g_string_free(filename, TRUE);
325 return fd;
326}
327
328static
329int 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
dc3fffef 338 (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR);
319fd969
PP
339 stream->pos.fd = fd;
340end:
341 return ret;
342}
343
3230ee6b
PP
344static
345void 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
273b65be 352struct bt_ctf_stream *bt_ctf_stream_create(
b71d7298
PP
353 struct bt_ctf_stream_class *stream_class,
354 const char *name)
273b65be 355{
12c8a1a3 356 int ret;
273b65be 357 struct bt_ctf_stream *stream = NULL;
319fd969
PP
358 struct bt_ctf_trace *trace = NULL;
359 struct bt_ctf_writer *writer = NULL;
273b65be 360
319fd969 361 if (!stream_class) {
b71d7298 362 goto error;
319fd969
PP
363 }
364
365 trace = bt_ctf_stream_class_get_trace(stream_class);
366 if (!trace) {
b71d7298 367 goto error;
273b65be
JG
368 }
369
5acf2ae6
PP
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
273b65be
JG
379 stream = g_new0(struct bt_ctf_stream, 1);
380 if (!stream) {
b71d7298 381 goto error;
273b65be
JG
382 }
383
83509119 384 bt_object_init(stream, bt_ctf_stream_destroy);
e6a8e8e4
JG
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);
273b65be
JG
390 stream->id = stream_class->next_stream_id++;
391 stream->stream_class = stream_class;
319fd969 392 stream->pos.fd = -1;
d246b111 393
3230ee6b
PP
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
b71d7298
PP
400 if (name) {
401 stream->name = g_string_new(name);
402 if (!stream->name) {
403 goto error;
404 }
405 }
406
319fd969
PP
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);
98edd02c
JG
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 }
319fd969 419
98edd02c 420 /* Initialize events_discarded */
af9296f3
JG
421 ret = try_set_structure_field_integer(
422 stream->packet_context,
98edd02c 423 "events_discarded", 0);
af9296f3 424 if (ret != 1) {
98edd02c
JG
425 goto error;
426 }
319fd969
PP
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 }
3230ee6b
PP
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 }
d246b111 481 }
319fd969
PP
482
483 /* Add this stream to the trace's streams */
484 g_ptr_array_add(trace->streams, stream);
485
319fd969
PP
486 BT_PUT(trace);
487 BT_PUT(writer);
273b65be 488 return stream;
83509119
JG
489error:
490 BT_PUT(stream);
319fd969
PP
491 BT_PUT(trace);
492 BT_PUT(writer);
83509119 493 return stream;
273b65be
JG
494}
495
3baf0856
JG
496struct 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;
83509119 506 bt_get(stream_class);
3baf0856
JG
507end:
508 return stream_class;
509}
510
a78a2e25
JG
511int bt_ctf_stream_get_discarded_events_count(
512 struct bt_ctf_stream *stream, uint64_t *count)
513{
514 int64_t ret = 0;
12c8a1a3
JG
515 int field_signed;
516 struct bt_ctf_field *events_discarded_field = NULL;
517 struct bt_ctf_field_type *events_discarded_field_type = NULL;
a78a2e25 518
1579cde5
PP
519 if (!stream || !count || !stream->packet_context ||
520 stream->pos.fd < 0) {
a78a2e25
JG
521 ret = -1;
522 goto end;
523 }
524
12c8a1a3
JG
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 }
a78a2e25 567end:
83509119
JG
568 bt_put(events_discarded_field);
569 bt_put(events_discarded_field_type);
a78a2e25
JG
570 return ret;
571}
572
273b65be
JG
573void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
574 uint64_t event_count)
575{
12c8a1a3
JG
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
1579cde5 583 if (!stream || !stream->packet_context || stream->pos.fd < 0) {
12c8a1a3
JG
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;
273b65be
JG
609 }
610
12c8a1a3 611 new_count = previous_count + event_count;
9ac68eb1 612 assert(new_count >= previous_count);
12c8a1a3
JG
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
627end:
83509119
JG
628 bt_put(events_discarded_field);
629 bt_put(events_discarded_field_type);
273b65be
JG
630}
631
ac0c6bdd
PP
632static 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
705end:
706 bt_put(id_field);
707 bt_put(timestamp_field);
708 bt_put(mapped_clock_class);
709 return ret;
710}
711
273b65be
JG
712int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
713 struct bt_ctf_event *event)
714{
715 int ret = 0;
273b65be 716
1579cde5 717 if (!stream || !event || stream->pos.fd < 0) {
273b65be
JG
718 ret = -1;
719 goto end;
720 }
721
fa29ba83
PP
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
e6a8e8e4 735 bt_object_set_parent(event, stream);
ac0c6bdd 736 ret = auto_populate_event_header(stream, event);
662e778c 737 if (ret) {
37f30168 738 goto error;
662e778c
JG
739 }
740
5fd2e9fd 741 /* Make sure the various scopes of the event are set */
273b65be
JG
742 ret = bt_ctf_event_validate(event);
743 if (ret) {
37f30168 744 goto error;
273b65be
JG
745 }
746
0d688c15
PP
747 /* Save the new event and freeze it */
748 bt_ctf_event_freeze(event);
273b65be 749 g_ptr_array_add(stream->events, event);
5fd2e9fd 750
e6a8e8e4
JG
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);
37f30168 758
273b65be 759end:
37f30168
PP
760 return ret;
761
762error:
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
273b65be
JG
769 return ret;
770}
771
12c8a1a3
JG
772struct 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
1579cde5 777 if (!stream || stream->pos.fd < 0) {
12c8a1a3
JG
778 goto end;
779 }
780
781 packet_context = stream->packet_context;
12c8a1a3 782 if (packet_context) {
83509119 783 bt_get(packet_context);
12c8a1a3 784 }
34629a55 785end:
12c8a1a3
JG
786 return packet_context;
787}
788
789int 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
835b2d10 795 if (!stream || stream->pos.fd < 0) {
12c8a1a3
JG
796 ret = -1;
797 goto end;
798 }
799
800 field_type = bt_ctf_field_get_type(field);
09840de5
PP
801 if (bt_ctf_field_type_compare(field_type,
802 stream->stream_class->packet_context_type)) {
12c8a1a3
JG
803 ret = -1;
804 goto end;
805 }
806
83509119 807 bt_put(field_type);
83509119 808 bt_put(stream->packet_context);
835b2d10 809 stream->packet_context = bt_get(field);
12c8a1a3
JG
810end:
811 return ret;
812}
813
263a7df5
JG
814struct 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
1579cde5 819 if (!stream || stream->pos.fd < 0) {
263a7df5
JG
820 goto end;
821 }
822
823 packet_header = stream->packet_header;
824 if (packet_header) {
83509119 825 bt_get(packet_header);
263a7df5
JG
826 }
827end:
828 return packet_header;
829}
830
831int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
832 struct bt_ctf_field *field)
833{
834 int ret = 0;
e6a8e8e4 835 struct bt_ctf_trace *trace = NULL;
263a7df5
JG
836 struct bt_ctf_field_type *field_type = NULL;
837
835b2d10 838 if (!stream || stream->pos.fd < 0) {
263a7df5
JG
839 ret = -1;
840 goto end;
841 }
842
e6a8e8e4 843 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
263a7df5 844 field_type = bt_ctf_field_get_type(field);
09840de5 845 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
263a7df5
JG
846 ret = -1;
847 goto end;
848 }
849
83509119 850 bt_put(stream->packet_header);
835b2d10 851 stream->packet_header = bt_get(field);
263a7df5 852end:
e6a8e8e4 853 BT_PUT(trace);
83509119 854 bt_put(field_type);
263a7df5
JG
855 return ret;
856}
857
9a220c32
JG
858static
859int 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) !=
1487a16a 875 BT_CTF_FIELD_TYPE_ID_INTEGER) {
9a220c32
JG
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 }
896end:
83509119
JG
897 bt_put(timestamp_field);
898 bt_put(timestamp_field_type);
9a220c32
JG
899 return ret;
900}
901
c9af50d1
JG
902static
903void 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);
e027ff8c 910 bt_put(member);
c9af50d1
JG
911}
912
273b65be
JG
913int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
914{
915 int ret = 0;
916 size_t i;
c9af50d1 917 uint64_t timestamp_begin, timestamp_end;
273b65be 918 struct bt_ctf_field *integer = NULL;
dc3fffef
PP
919 struct bt_ctf_stream_pos packet_context_pos;
920 struct bt_ctf_trace *trace;
921 enum bt_ctf_byte_order native_byte_order;
c55a9f58 922 bt_bool empty_packet;
0686ef94 923 uint64_t packet_size_bits;
c9af50d1 924 struct {
c55a9f58
PP
925 bt_bool timestamp_begin;
926 bt_bool timestamp_end;
927 bt_bool content_size;
928 bt_bool packet_size;
c9af50d1 929 } auto_set_fields = { 0 };
273b65be 930
bc37ae52
JG
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 */
273b65be
JG
936 ret = -1;
937 goto end;
938 }
939
664f50c8
JG
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
dc3fffef
PP
950 trace = bt_ctf_stream_class_borrow_trace(stream->stream_class);
951 assert(trace);
8a716c8d 952 native_byte_order = bt_ctf_trace_get_native_byte_order(trace);
98edd02c 953 empty_packet = (stream->events->len == 0);
d246b111 954
28362f2b 955 /* mmap the next packet */
dc3fffef
PP
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);
d246b111
JG
959 if (ret) {
960 goto end;
273b65be
JG
961 }
962
98edd02c
JG
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)) {
af9296f3
JG
968 ret = try_set_structure_field_integer(
969 stream->packet_context,
98edd02c 970 "timestamp_begin", timestamp_begin);
af9296f3 971 if (ret < 0) {
98edd02c
JG
972 goto end;
973 }
c9af50d1 974 auto_set_fields.timestamp_begin = ret == 1;
98edd02c
JG
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
af9296f3
JG
982 ret = try_set_structure_field_integer(
983 stream->packet_context,
98edd02c 984 "timestamp_end", timestamp_end);
af9296f3 985 if (ret < 0) {
98edd02c
JG
986 goto end;
987 }
c9af50d1 988 auto_set_fields.timestamp_end = ret == 1;
98edd02c 989 }
af9296f3 990 ret = try_set_structure_field_integer(stream->packet_context,
98edd02c 991 "content_size", UINT64_MAX);
af9296f3 992 if (ret < 0) {
9a220c32
JG
993 goto end;
994 }
c9af50d1 995 auto_set_fields.content_size = ret == 1;
273b65be 996
af9296f3 997 ret = try_set_structure_field_integer(stream->packet_context,
98edd02c 998 "packet_size", UINT64_MAX);
af9296f3 999 if (ret < 0) {
98edd02c
JG
1000 goto end;
1001 }
c9af50d1 1002 auto_set_fields.packet_size = ret == 1;
12c8a1a3 1003
10cb7b41
JG
1004 /* Write packet context */
1005 memcpy(&packet_context_pos, &stream->pos,
dc3fffef 1006 sizeof(packet_context_pos));
10cb7b41 1007 ret = bt_ctf_field_serialize(stream->packet_context,
dc3fffef 1008 &stream->pos, native_byte_order);
98edd02c
JG
1009 if (ret) {
1010 goto end;
1011 }
12c8a1a3
JG
1012 }
1013
273b65be
JG
1014 for (i = 0; i < stream->events->len; i++) {
1015 struct bt_ctf_event *event = g_ptr_array_index(
1016 stream->events, i);
273b65be 1017
273b65be 1018 /* Write event header */
662e778c 1019 ret = bt_ctf_field_serialize(event->event_header,
dc3fffef 1020 &stream->pos, native_byte_order);
273b65be
JG
1021 if (ret) {
1022 goto end;
1023 }
1024
8bfa3f9c 1025 /* Write stream event context */
5fd2e9fd 1026 if (event->stream_event_context) {
8bfa3f9c 1027 ret = bt_ctf_field_serialize(
dc3fffef
PP
1028 event->stream_event_context, &stream->pos,
1029 native_byte_order);
8bfa3f9c
JG
1030 if (ret) {
1031 goto end;
1032 }
1033 }
1034
273b65be 1035 /* Write event content */
dc3fffef
PP
1036 ret = bt_ctf_event_serialize(event, &stream->pos,
1037 native_byte_order);
273b65be
JG
1038 if (ret) {
1039 goto end;
1040 }
1041 }
1042
0686ef94
JG
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
98edd02c
JG
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;
c9af50d1
JG
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 }
98edd02c 1063 }
273b65be 1064
c9af50d1
JG
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 }
98edd02c 1071 }
273b65be 1072
98edd02c 1073 ret = bt_ctf_field_serialize(stream->packet_context,
dc3fffef 1074 &packet_context_pos, native_byte_order);
98edd02c
JG
1075 if (ret) {
1076 goto end;
1077 }
273b65be
JG
1078 }
1079
1080 g_ptr_array_set_size(stream->events, 0);
1081 stream->flushed_packet_count++;
0686ef94 1082 stream->size += packet_size_bits / CHAR_BIT;
273b65be 1083end:
c9af50d1
JG
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 }
83509119 1101 bt_put(integer);
cd7d8fb7
JG
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 }
273b65be
JG
1112 return ret;
1113}
1114
1115void bt_ctf_stream_get(struct bt_ctf_stream *stream)
1116{
83509119 1117 bt_get(stream);
273b65be
JG
1118}
1119
1120void bt_ctf_stream_put(struct bt_ctf_stream *stream)
1121{
83509119 1122 bt_put(stream);
273b65be
JG
1123}
1124
1125static
83509119 1126void bt_ctf_stream_destroy(struct bt_object *obj)
273b65be
JG
1127{
1128 struct bt_ctf_stream *stream;
3230ee6b 1129 int i;
273b65be 1130
83509119 1131 stream = container_of(obj, struct bt_ctf_stream, base);
3230ee6b
PP
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
dc3fffef 1142 (void) bt_ctf_stream_pos_fini(&stream->pos);
0686ef94
JG
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 }
9f56e450 1161 }
12c8a1a3 1162
12c8a1a3
JG
1163 if (stream->events) {
1164 g_ptr_array_free(stream->events, TRUE);
1165 }
b71d7298
PP
1166
1167 if (stream->name) {
1168 g_string_free(stream->name, TRUE);
1169 }
41ac640a 1170
3230ee6b
PP
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
83509119
JG
1197 bt_put(stream->packet_header);
1198 bt_put(stream->packet_context);
273b65be
JG
1199 g_free(stream);
1200}
1201
273b65be 1202static
af9296f3 1203int _set_structure_field_integer(struct bt_ctf_field *structure, char *name,
c55a9f58 1204 uint64_t value, bt_bool force)
273b65be
JG
1205{
1206 int ret = 0;
d7b1ea66 1207 struct bt_ctf_field_type *field_type = NULL;
273b65be
JG
1208 struct bt_ctf_field *integer =
1209 bt_ctf_field_structure_get_field(structure, name);
12c8a1a3
JG
1210
1211 if (!structure || !name) {
273b65be
JG
1212 ret = -1;
1213 goto end;
1214 }
1215
12c8a1a3
JG
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. */
af9296f3 1222 if (!force && bt_ctf_field_is_set(integer)) {
12c8a1a3
JG
1223 /* Payload already set, not an error */
1224 goto end;
1225 }
1226
d7b1ea66 1227 field_type = bt_ctf_field_get_type(integer);
d7b1ea66 1228 assert(field_type);
1487a16a 1229 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
d7b1ea66
JG
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 }
af9296f3 1245 ret = !ret ? 1 : ret;
273b65be 1246end:
83509119
JG
1247 bt_put(integer);
1248 bt_put(field_type);
273b65be
JG
1249 return ret;
1250}
b71d7298 1251
af9296f3
JG
1252static
1253int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1254 uint64_t value)
1255{
c55a9f58 1256 return _set_structure_field_integer(structure, name, value, BT_TRUE);
af9296f3
JG
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 */
1265static
1266int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name,
1267 uint64_t value)
1268{
c55a9f58 1269 return _set_structure_field_integer(structure, name, value, BT_FALSE);
af9296f3
JG
1270}
1271
b71d7298
PP
1272const 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
1282end:
1283 return name;
1284}
98a4cbef
PP
1285
1286int 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
1296end:
1297 return ret;
1298}
3230ee6b
PP
1299
1300BT_HIDDEN
1301void 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
1322BT_HIDDEN
1323struct 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
1332BT_HIDDEN
1333void 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
1345BT_HIDDEN
1346void 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.100441 seconds and 4 git commands to generate.