4 * Babeltrace CTF IR - Stream
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/ctf-ir/clock.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
32 #include <babeltrace/ctf-ir/event-internal.h>
33 #include <babeltrace/ctf-ir/event-types-internal.h>
34 #include <babeltrace/ctf-ir/event-fields-internal.h>
35 #include <babeltrace/ctf-ir/stream.h>
36 #include <babeltrace/ctf-ir/stream-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-writer/functor-internal.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/align.h>
43 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
);
45 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
48 struct bt_ctf_stream
*bt_ctf_stream_create(
49 struct bt_ctf_stream_class
*stream_class
)
52 struct bt_ctf_stream
*stream
= NULL
;
58 stream
= g_new0(struct bt_ctf_stream
, 1);
63 bt_ctf_ref_init(&stream
->ref_count
);
64 stream
->packet_context
= bt_ctf_field_create(
65 stream_class
->packet_context_type
);
66 if (!stream
->packet_context
) {
71 * A stream class may not have a stream event context defined
72 * in which case this stream will never have a stream_event_context
73 * member since, after a stream's creation, the parent stream class
74 * is "frozen" (immutable).
76 if (stream_class
->event_context_type
) {
77 stream
->event_context
= bt_ctf_field_create(
78 stream_class
->event_context_type
);
79 if (!stream
->packet_context
) {
84 /* Initialize events_discarded */
85 ret
= set_structure_field_integer(stream
->packet_context
,
86 "events_discarded", 0);
92 stream
->id
= stream_class
->next_stream_id
++;
93 stream
->stream_class
= stream_class
;
94 bt_ctf_stream_class_get(stream_class
);
95 bt_ctf_stream_class_freeze(stream_class
);
96 stream
->events
= g_ptr_array_new_with_free_func(
97 (GDestroyNotify
) bt_ctf_event_put
);
98 if (!stream
->events
) {
101 if (stream_class
->event_context_type
) {
102 stream
->event_contexts
= g_ptr_array_new_with_free_func(
103 (GDestroyNotify
) bt_ctf_field_put
);
104 if (!stream
->event_contexts
) {
111 bt_ctf_stream_destroy(&stream
->ref_count
);
116 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream
*stream
,
117 flush_func callback
, void *data
)
119 int ret
= stream
? 0 : -1;
125 stream
->flush
.func
= callback
;
126 stream
->flush
.data
= data
;
132 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
136 if (stream
->pos
.fd
!= -1) {
141 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
147 int bt_ctf_stream_get_discarded_events_count(
148 struct bt_ctf_stream
*stream
, uint64_t *count
)
152 struct bt_ctf_field
*events_discarded_field
= NULL
;
153 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
155 if (!stream
|| !count
|| !stream
->packet_context
) {
160 events_discarded_field
= bt_ctf_field_structure_get_field(
161 stream
->packet_context
, "events_discarded");
162 if (!events_discarded_field
) {
167 events_discarded_field_type
= bt_ctf_field_get_type(
168 events_discarded_field
);
169 if (!events_discarded_field_type
) {
174 field_signed
= bt_ctf_field_type_integer_get_signed(
175 events_discarded_field_type
);
176 if (field_signed
< 0) {
182 int64_t signed_count
;
184 ret
= bt_ctf_field_signed_integer_get_value(
185 events_discarded_field
, &signed_count
);
189 if (signed_count
< 0) {
194 *count
= (uint64_t) signed_count
;
196 ret
= bt_ctf_field_unsigned_integer_get_value(
197 events_discarded_field
, count
);
203 if (events_discarded_field
) {
204 bt_ctf_field_put(events_discarded_field
);
206 if (events_discarded_field_type
) {
207 bt_ctf_field_type_put(events_discarded_field_type
);
212 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
213 uint64_t event_count
)
217 uint64_t previous_count
;
219 struct bt_ctf_field
*events_discarded_field
= NULL
;
220 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
222 if (!stream
|| !stream
->packet_context
) {
226 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
232 events_discarded_field
= bt_ctf_field_structure_get_field(
233 stream
->packet_context
, "events_discarded");
234 if (!events_discarded_field
) {
238 events_discarded_field_type
= bt_ctf_field_get_type(
239 events_discarded_field
);
240 if (!events_discarded_field_type
) {
244 field_signed
= bt_ctf_field_type_integer_get_signed(
245 events_discarded_field_type
);
246 if (field_signed
< 0) {
250 new_count
= previous_count
+ event_count
;
252 ret
= bt_ctf_field_signed_integer_set_value(
253 events_discarded_field
, (int64_t) new_count
);
258 ret
= bt_ctf_field_unsigned_integer_set_value(
259 events_discarded_field
, new_count
);
266 if (events_discarded_field
) {
267 bt_ctf_field_put(events_discarded_field
);
269 if (events_discarded_field_type
) {
270 bt_ctf_field_type_put(events_discarded_field_type
);
274 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
275 struct bt_ctf_event
*event
)
279 struct bt_ctf_field
*event_context_copy
= NULL
;
281 if (!stream
|| !event
) {
286 ret
= bt_ctf_event_validate(event
);
291 /* Sample the current stream event context by copying it */
292 if (stream
->event_context
) {
293 /* Make sure the event context's payload is set */
294 ret
= bt_ctf_field_validate(stream
->event_context
);
299 event_context_copy
= bt_ctf_field_copy(stream
->event_context
);
300 if (!event_context_copy
) {
306 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
307 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
312 bt_ctf_event_get(event
);
313 /* Save the new event along with its associated stream event context */
314 g_ptr_array_add(stream
->events
, event
);
315 if (event_context_copy
) {
316 g_ptr_array_add(stream
->event_contexts
, event_context_copy
);
322 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
323 struct bt_ctf_stream
*stream
)
325 struct bt_ctf_field
*packet_context
= NULL
;
331 packet_context
= stream
->packet_context
;
332 if (packet_context
) {
333 bt_ctf_field_get(packet_context
);
336 return packet_context
;
339 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
340 struct bt_ctf_field
*field
)
343 struct bt_ctf_field_type
*field_type
;
345 if (!stream
|| !field
) {
350 field_type
= bt_ctf_field_get_type(field
);
351 if (field_type
!= stream
->stream_class
->packet_context_type
) {
356 bt_ctf_field_type_put(field_type
);
357 bt_ctf_field_get(field
);
358 bt_ctf_field_put(stream
->packet_context
);
359 stream
->packet_context
= field
;
364 struct bt_ctf_field
*bt_ctf_stream_get_event_context(
365 struct bt_ctf_stream
*stream
)
367 struct bt_ctf_field
*event_context
= NULL
;
373 event_context
= stream
->event_context
;
375 bt_ctf_field_get(event_context
);
378 return event_context
;
381 int bt_ctf_stream_set_event_context(struct bt_ctf_stream
*stream
,
382 struct bt_ctf_field
*field
)
385 struct bt_ctf_field_type
*field_type
= NULL
;
387 if (!stream
|| !field
) {
392 field_type
= bt_ctf_field_get_type(field
);
393 if (field_type
!= stream
->stream_class
->event_context_type
) {
398 bt_ctf_field_get(field
);
399 bt_ctf_field_put(stream
->event_context
);
400 stream
->event_context
= field
;
403 bt_ctf_field_type_put(field_type
);
408 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
412 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
413 struct bt_ctf_stream_class
*stream_class
;
414 struct bt_ctf_field
*integer
= NULL
;
415 struct ctf_stream_pos packet_context_pos
;
417 if (!stream
|| stream
->pos
.fd
< 0) {
419 * Stream does not have an associated fd. It is,
420 * therefore, not a stream being used to write events.
426 if (!stream
->events
->len
) {
430 if (stream
->flush
.func
) {
431 stream
->flush
.func(stream
, stream
->flush
.data
);
434 stream_class
= stream
->stream_class
;
435 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
436 stream
->events
, 0))->timestamp
;
437 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
438 stream
->events
, stream
->events
->len
- 1))->timestamp
;
440 /* Set the default context attributes if present and unset. */
441 ret
= set_structure_field_integer(stream
->packet_context
,
442 "timestamp_begin", timestamp_begin
);
447 ret
= set_structure_field_integer(stream
->packet_context
,
448 "timestamp_end", timestamp_end
);
453 ret
= set_structure_field_integer(stream
->packet_context
,
454 "content_size", UINT64_MAX
);
459 ret
= set_structure_field_integer(stream
->packet_context
,
460 "packet_size", UINT64_MAX
);
465 /* Write packet context */
466 memcpy(&packet_context_pos
, &stream
->pos
,
467 sizeof(struct ctf_stream_pos
));
468 ret
= bt_ctf_field_serialize(stream
->packet_context
,
474 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
480 /* Unset the packet context's fields. */
481 ret
= bt_ctf_field_reset(stream
->packet_context
);
486 /* Set the previous number of discarded events. */
487 ret
= set_structure_field_integer(stream
->packet_context
,
488 "events_discarded", events_discarded
);
493 for (i
= 0; i
< stream
->events
->len
; i
++) {
494 struct bt_ctf_event
*event
= g_ptr_array_index(
496 uint32_t event_id
= bt_ctf_event_class_get_id(
498 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
500 ret
= bt_ctf_field_reset(stream_class
->event_header
);
505 ret
= set_structure_field_integer(stream_class
->event_header
,
510 ret
= set_structure_field_integer(stream_class
->event_header
,
511 "timestamp", timestamp
);
516 /* Write event header */
517 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
523 /* Write stream event context */
524 if (stream
->event_contexts
) {
525 ret
= bt_ctf_field_serialize(
526 g_ptr_array_index(stream
->event_contexts
, i
),
533 /* Write event content */
534 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
541 * Update the packet total size and content size and overwrite the
543 * Copy base_mma as the packet may have been remapped (e.g. when a
544 * packet is resized).
546 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
547 ret
= set_structure_field_integer(stream
->packet_context
,
548 "content_size", stream
->pos
.offset
);
553 ret
= set_structure_field_integer(stream
->packet_context
,
554 "packet_size", stream
->pos
.packet_size
);
559 ret
= bt_ctf_field_serialize(stream
->packet_context
,
560 &packet_context_pos
);
565 g_ptr_array_set_size(stream
->events
, 0);
566 if (stream
->event_contexts
) {
567 g_ptr_array_set_size(stream
->event_contexts
, 0);
569 stream
->flushed_packet_count
++;
571 bt_ctf_field_put(integer
);
575 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
581 bt_ctf_ref_get(&stream
->ref_count
);
584 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
590 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
594 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
596 struct bt_ctf_stream
*stream
;
602 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
603 ctf_fini_pos(&stream
->pos
);
604 if (close(stream
->pos
.fd
)) {
608 if (stream
->stream_class
) {
609 bt_ctf_stream_class_put(stream
->stream_class
);
611 if (stream
->events
) {
612 g_ptr_array_free(stream
->events
, TRUE
);
614 if (stream
->event_contexts
) {
615 g_ptr_array_free(stream
->event_contexts
, TRUE
);
617 if (stream
->packet_context
) {
618 bt_ctf_field_put(stream
->packet_context
);
620 if (stream
->event_context
) {
621 bt_ctf_field_put(stream
->event_context
);
627 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
631 struct bt_ctf_field
*integer
=
632 bt_ctf_field_structure_get_field(structure
, name
);
634 if (!structure
|| !name
) {
640 /* Field not found, not an error. */
644 /* Make sure the payload has not already been set. */
645 if (!bt_ctf_field_validate(integer
)) {
646 /* Payload already set, not an error */
650 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
652 bt_ctf_field_put(integer
);