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 ret
= set_structure_field_integer(stream
->packet_context
,
85 "events_discarded", 0);
91 stream
->id
= stream_class
->next_stream_id
++;
92 stream
->stream_class
= stream_class
;
93 bt_ctf_stream_class_get(stream_class
);
94 bt_ctf_stream_class_freeze(stream_class
);
95 stream
->events
= g_ptr_array_new_with_free_func(
96 (GDestroyNotify
)bt_ctf_event_put
);
100 bt_ctf_stream_destroy(&stream
->ref_count
);
105 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream
*stream
,
106 flush_func callback
, void *data
)
108 int ret
= stream
? 0 : -1;
114 stream
->flush
.func
= callback
;
115 stream
->flush
.data
= data
;
121 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
125 if (stream
->pos
.fd
!= -1) {
130 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
136 int bt_ctf_stream_get_discarded_events_count(
137 struct bt_ctf_stream
*stream
, uint64_t *count
)
141 struct bt_ctf_field
*events_discarded_field
= NULL
;
142 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
144 if (!stream
|| !count
|| !stream
->packet_context
) {
149 events_discarded_field
= bt_ctf_field_structure_get_field(
150 stream
->packet_context
, "events_discarded");
151 if (!events_discarded_field
) {
156 events_discarded_field_type
= bt_ctf_field_get_type(
157 events_discarded_field
);
158 if (!events_discarded_field_type
) {
163 field_signed
= bt_ctf_field_type_integer_get_signed(
164 events_discarded_field_type
);
165 if (field_signed
< 0) {
171 int64_t signed_count
;
173 ret
= bt_ctf_field_signed_integer_get_value(
174 events_discarded_field
, &signed_count
);
178 if (signed_count
< 0) {
183 *count
= (uint64_t) signed_count
;
185 ret
= bt_ctf_field_unsigned_integer_get_value(
186 events_discarded_field
, count
);
192 if (events_discarded_field
) {
193 bt_ctf_field_put(events_discarded_field
);
195 if (events_discarded_field_type
) {
196 bt_ctf_field_type_put(events_discarded_field_type
);
201 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
202 uint64_t event_count
)
206 uint64_t previous_count
;
208 struct bt_ctf_field
*events_discarded_field
= NULL
;
209 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
211 if (!stream
|| !stream
->packet_context
) {
215 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
221 events_discarded_field
= bt_ctf_field_structure_get_field(
222 stream
->packet_context
, "events_discarded");
223 if (!events_discarded_field
) {
227 events_discarded_field_type
= bt_ctf_field_get_type(
228 events_discarded_field
);
229 if (!events_discarded_field_type
) {
233 field_signed
= bt_ctf_field_type_integer_get_signed(
234 events_discarded_field_type
);
235 if (field_signed
< 0) {
239 new_count
= previous_count
+ event_count
;
241 ret
= bt_ctf_field_signed_integer_set_value(
242 events_discarded_field
, (int64_t) new_count
);
247 ret
= bt_ctf_field_unsigned_integer_set_value(
248 events_discarded_field
, new_count
);
255 if (events_discarded_field
) {
256 bt_ctf_field_put(events_discarded_field
);
258 if (events_discarded_field_type
) {
259 bt_ctf_field_type_put(events_discarded_field_type
);
263 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
264 struct bt_ctf_event
*event
)
268 struct bt_ctf_field
*event_context_copy
= NULL
;
270 if (!stream
|| !event
) {
275 ret
= bt_ctf_event_validate(event
);
280 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
281 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
286 bt_ctf_event_get(event
);
287 g_ptr_array_add(stream
->events
, event
);
292 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
293 struct bt_ctf_stream
*stream
)
295 struct bt_ctf_field
*packet_context
= NULL
;
301 packet_context
= stream
->packet_context
;
303 if (packet_context
) {
304 bt_ctf_field_get(packet_context
);
306 return packet_context
;
309 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
310 struct bt_ctf_field
*field
)
313 struct bt_ctf_field_type
*field_type
;
315 if (!stream
|| !field
) {
320 field_type
= bt_ctf_field_get_type(field
);
321 if (field_type
!= stream
->stream_class
->packet_context_type
) {
326 bt_ctf_field_type_put(field_type
);
327 bt_ctf_field_get(field
);
328 bt_ctf_field_put(stream
->packet_context
);
329 stream
->packet_context
= field
;
334 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
338 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
339 struct bt_ctf_stream_class
*stream_class
;
340 struct bt_ctf_field
*integer
= NULL
;
341 struct ctf_stream_pos packet_context_pos
;
343 if (!stream
|| stream
->pos
.fd
< 0) {
345 * Stream does not have an associated fd. It is,
346 * therefore, not a stream being used to write events.
352 if (!stream
->events
->len
) {
356 if (stream
->flush
.func
) {
357 stream
->flush
.func(stream
, stream
->flush
.data
);
360 stream_class
= stream
->stream_class
;
361 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
362 stream
->events
, 0))->timestamp
;
363 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
364 stream
->events
, stream
->events
->len
- 1))->timestamp
;
366 /* Set the default context attributes if present and unset. */
367 ret
= set_structure_field_integer(stream
->packet_context
,
368 "timestamp_begin", timestamp_begin
);
373 ret
= set_structure_field_integer(stream
->packet_context
,
374 "timestamp_end", timestamp_end
);
379 ret
= set_structure_field_integer(stream
->packet_context
,
380 "content_size", UINT64_MAX
);
385 ret
= set_structure_field_integer(stream
->packet_context
,
386 "packet_size", UINT64_MAX
);
391 /* Write packet context */
392 memcpy(&packet_context_pos
, &stream
->pos
,
393 sizeof(struct ctf_stream_pos
));
394 ret
= bt_ctf_field_serialize(stream
->packet_context
,
400 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
406 /* Unset the packet context's fields. */
407 ret
= bt_ctf_field_reset(stream
->packet_context
);
412 /* Set the previous number of discarded events. */
413 ret
= set_structure_field_integer(stream
->packet_context
,
414 "events_discarded", events_discarded
);
419 for (i
= 0; i
< stream
->events
->len
; i
++) {
420 struct bt_ctf_event
*event
= g_ptr_array_index(
422 uint32_t event_id
= bt_ctf_event_class_get_id(
424 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
426 ret
= bt_ctf_field_reset(stream_class
->event_header
);
431 ret
= set_structure_field_integer(stream_class
->event_header
,
436 ret
= set_structure_field_integer(stream_class
->event_header
,
437 "timestamp", timestamp
);
442 /* Write event header */
443 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
449 /* Write event content */
450 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
457 * Update the packet total size and content size and overwrite the
459 * Copy base_mma as the packet may have been remapped (e.g. when a
460 * packet is resized).
462 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
463 ret
= set_structure_field_integer(stream
->packet_context
,
464 "content_size", stream
->pos
.offset
);
469 ret
= set_structure_field_integer(stream
->packet_context
,
470 "packet_size", stream
->pos
.packet_size
);
475 ret
= bt_ctf_field_serialize(stream
->packet_context
,
476 &packet_context_pos
);
481 g_ptr_array_set_size(stream
->events
, 0);
482 stream
->flushed_packet_count
++;
484 bt_ctf_field_put(integer
);
488 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
494 bt_ctf_ref_get(&stream
->ref_count
);
497 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
503 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
507 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
509 struct bt_ctf_stream
*stream
;
515 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
516 ctf_fini_pos(&stream
->pos
);
517 if (close(stream
->pos
.fd
)) {
521 if (stream
->stream_class
) {
522 bt_ctf_stream_class_put(stream
->stream_class
);
524 if (stream
->events
) {
525 g_ptr_array_free(stream
->events
, TRUE
);
527 if (stream
->packet_context
) {
528 bt_ctf_field_put(stream
->packet_context
);
534 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
538 struct bt_ctf_field
*integer
=
539 bt_ctf_field_structure_get_field(structure
, name
);
541 if (!structure
|| !name
) {
547 /* Field not found, not an error. */
551 /* Make sure the payload has not already been set. */
552 if (!bt_ctf_field_validate(integer
)) {
553 /* Payload already set, not an error */
557 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
559 bt_ctf_field_put(integer
);