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
) {
70 /* Initialize events_discarded*/
71 ret
= set_structure_field_integer(stream
->packet_context
,
72 "events_discarded", 0);
78 stream
->id
= stream_class
->next_stream_id
++;
79 stream
->stream_class
= stream_class
;
80 bt_ctf_stream_class_get(stream_class
);
81 bt_ctf_stream_class_freeze(stream_class
);
82 stream
->events
= g_ptr_array_new_with_free_func(
83 (GDestroyNotify
)bt_ctf_event_put
);
87 bt_ctf_stream_destroy(&stream
->ref_count
);
92 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream
*stream
,
93 flush_func callback
, void *data
)
95 int ret
= stream
? 0 : -1;
101 stream
->flush
.func
= callback
;
102 stream
->flush
.data
= data
;
108 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
112 if (stream
->pos
.fd
!= -1) {
117 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
123 int bt_ctf_stream_get_discarded_events_count(
124 struct bt_ctf_stream
*stream
, uint64_t *count
)
128 struct bt_ctf_field
*events_discarded_field
= NULL
;
129 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
131 if (!stream
|| !count
|| !stream
->packet_context
) {
136 events_discarded_field
= bt_ctf_field_structure_get_field(
137 stream
->packet_context
, "events_discarded");
138 if (!events_discarded_field
) {
143 events_discarded_field_type
= bt_ctf_field_get_type(
144 events_discarded_field
);
145 if (!events_discarded_field_type
) {
150 field_signed
= bt_ctf_field_type_integer_get_signed(
151 events_discarded_field_type
);
152 if (field_signed
< 0) {
158 int64_t signed_count
;
160 ret
= bt_ctf_field_signed_integer_get_value(
161 events_discarded_field
, &signed_count
);
165 if (signed_count
< 0) {
170 *count
= (uint64_t) signed_count
;
172 ret
= bt_ctf_field_unsigned_integer_get_value(
173 events_discarded_field
, count
);
179 if (events_discarded_field
) {
180 bt_ctf_field_put(events_discarded_field
);
182 if (events_discarded_field_type
) {
183 bt_ctf_field_type_put(events_discarded_field_type
);
188 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
189 uint64_t event_count
)
193 uint64_t previous_count
;
195 struct bt_ctf_field
*events_discarded_field
= NULL
;
196 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
198 if (!stream
|| !stream
->packet_context
) {
202 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
208 events_discarded_field
= bt_ctf_field_structure_get_field(
209 stream
->packet_context
, "events_discarded");
210 if (!events_discarded_field
) {
214 events_discarded_field_type
= bt_ctf_field_get_type(
215 events_discarded_field
);
216 if (!events_discarded_field_type
) {
220 field_signed
= bt_ctf_field_type_integer_get_signed(
221 events_discarded_field_type
);
222 if (field_signed
< 0) {
226 new_count
= previous_count
+ event_count
;
228 ret
= bt_ctf_field_signed_integer_set_value(
229 events_discarded_field
, (int64_t) new_count
);
234 ret
= bt_ctf_field_unsigned_integer_set_value(
235 events_discarded_field
, new_count
);
242 if (events_discarded_field
) {
243 bt_ctf_field_put(events_discarded_field
);
245 if (events_discarded_field_type
) {
246 bt_ctf_field_type_put(events_discarded_field_type
);
250 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
251 struct bt_ctf_event
*event
)
256 if (!stream
|| !event
) {
261 ret
= bt_ctf_event_validate(event
);
266 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
267 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
272 bt_ctf_event_get(event
);
273 g_ptr_array_add(stream
->events
, event
);
278 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
279 struct bt_ctf_stream
*stream
)
281 struct bt_ctf_field
*packet_context
= NULL
;
287 packet_context
= stream
->packet_context
;
289 if (packet_context
) {
290 bt_ctf_field_get(packet_context
);
292 return packet_context
;
295 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
296 struct bt_ctf_field
*field
)
299 struct bt_ctf_field_type
*field_type
;
301 if (!stream
|| !field
) {
306 field_type
= bt_ctf_field_get_type(field
);
307 if (field_type
!= stream
->stream_class
->packet_context_type
) {
312 bt_ctf_field_type_put(field_type
);
313 bt_ctf_field_get(field
);
314 bt_ctf_field_put(stream
->packet_context
);
315 stream
->packet_context
= field
;
320 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
324 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
325 struct bt_ctf_stream_class
*stream_class
;
326 struct bt_ctf_field
*integer
= NULL
;
327 struct ctf_stream_pos packet_context_pos
;
329 if (!stream
|| stream
->pos
.fd
< 0) {
331 * Stream does not have an associated fd. It is,
332 * therefore, not a stream being used to write events.
338 if (!stream
->events
->len
) {
342 if (stream
->flush
.func
) {
343 stream
->flush
.func(stream
, stream
->flush
.data
);
346 stream_class
= stream
->stream_class
;
347 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
348 stream
->events
, 0))->timestamp
;
349 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
350 stream
->events
, stream
->events
->len
- 1))->timestamp
;
352 /* Set the default context attributes if present and unset. */
353 ret
= set_structure_field_integer(stream
->packet_context
,
354 "timestamp_begin", timestamp_begin
);
359 ret
= set_structure_field_integer(stream
->packet_context
,
360 "timestamp_end", timestamp_end
);
365 ret
= set_structure_field_integer(stream
->packet_context
,
366 "content_size", UINT64_MAX
);
371 ret
= set_structure_field_integer(stream
->packet_context
,
372 "packet_size", UINT64_MAX
);
377 /* Write packet context */
378 memcpy(&packet_context_pos
, &stream
->pos
,
379 sizeof(struct ctf_stream_pos
));
380 ret
= bt_ctf_field_serialize(stream
->packet_context
,
386 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
392 /* Unset the packet context's fields. */
393 ret
= bt_ctf_field_reset(stream
->packet_context
);
398 /* Set the previous number of discarded events. */
399 ret
= set_structure_field_integer(stream
->packet_context
,
400 "events_discarded", events_discarded
);
405 for (i
= 0; i
< stream
->events
->len
; i
++) {
406 struct bt_ctf_event
*event
= g_ptr_array_index(
408 uint32_t event_id
= bt_ctf_event_class_get_id(
410 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
412 ret
= bt_ctf_field_reset(stream_class
->event_header
);
417 ret
= set_structure_field_integer(stream_class
->event_header
,
422 ret
= set_structure_field_integer(stream_class
->event_header
,
423 "timestamp", timestamp
);
428 /* Write event header */
429 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
435 /* Write event content */
436 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
443 * Update the packet total size and content size and overwrite the
445 * Copy base_mma as the packet may have been remapped (e.g. when a
446 * packet is resized).
448 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
449 ret
= set_structure_field_integer(stream
->packet_context
,
450 "content_size", stream
->pos
.offset
);
455 ret
= set_structure_field_integer(stream
->packet_context
,
456 "packet_size", stream
->pos
.packet_size
);
461 ret
= bt_ctf_field_serialize(stream
->packet_context
,
462 &packet_context_pos
);
467 g_ptr_array_set_size(stream
->events
, 0);
468 stream
->flushed_packet_count
++;
470 bt_ctf_field_put(integer
);
474 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
480 bt_ctf_ref_get(&stream
->ref_count
);
483 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
489 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
493 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
495 struct bt_ctf_stream
*stream
;
501 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
502 ctf_fini_pos(&stream
->pos
);
503 if (close(stream
->pos
.fd
)) {
507 if (stream
->stream_class
) {
508 bt_ctf_stream_class_put(stream
->stream_class
);
510 if (stream
->events
) {
511 g_ptr_array_free(stream
->events
, TRUE
);
513 if (stream
->packet_context
) {
514 bt_ctf_field_put(stream
->packet_context
);
520 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
524 struct bt_ctf_field
*integer
=
525 bt_ctf_field_structure_get_field(structure
, name
);
527 if (!structure
|| !name
) {
533 /* Field not found, not an error. */
537 /* Make sure the payload has not already been set. */
538 if (!bt_ctf_field_validate(integer
)) {
539 /* Payload already set, not an error */
543 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
545 bt_ctf_field_put(integer
);