4 * Babeltrace CTF Writer
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
)
51 struct bt_ctf_stream
*stream
= NULL
;
57 stream
= g_new0(struct bt_ctf_stream
, 1);
62 bt_ctf_ref_init(&stream
->ref_count
);
64 stream
->id
= stream_class
->next_stream_id
++;
65 stream
->stream_class
= stream_class
;
66 bt_ctf_stream_class_get(stream_class
);
67 bt_ctf_stream_class_freeze(stream_class
);
68 stream
->events
= g_ptr_array_new_with_free_func(
69 (GDestroyNotify
)bt_ctf_event_put
);
75 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream
*stream
,
76 flush_func callback
, void *data
)
78 int ret
= stream
? 0 : -1;
84 stream
->flush
.func
= callback
;
85 stream
->flush
.data
= data
;
91 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
95 if (stream
->pos
.fd
!= -1) {
100 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
106 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
107 uint64_t event_count
)
113 stream
->events_discarded
+= event_count
;
116 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
117 struct bt_ctf_event
*event
)
122 if (!stream
|| !event
) {
127 ret
= bt_ctf_event_validate(event
);
132 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
133 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
138 bt_ctf_event_get(event
);
139 g_ptr_array_add(stream
->events
, event
);
144 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
148 uint64_t timestamp_begin
, timestamp_end
;
149 struct bt_ctf_stream_class
*stream_class
;
150 struct bt_ctf_field
*integer
= NULL
;
151 struct ctf_stream_pos packet_context_pos
;
158 if (!stream
->events
->len
) {
162 if (stream
->flush
.func
) {
163 stream
->flush
.func(stream
, stream
->flush
.data
);
166 stream_class
= stream
->stream_class
;
167 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
168 stream
->events
, 0))->timestamp
;
169 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
170 stream
->events
, stream
->events
->len
- 1))->timestamp
;
171 ret
= set_structure_field_integer(stream_class
->packet_context
,
172 "timestamp_begin", timestamp_begin
);
177 ret
= set_structure_field_integer(stream_class
->packet_context
,
178 "timestamp_end", timestamp_end
);
183 ret
= set_structure_field_integer(stream_class
->packet_context
,
184 "events_discarded", stream
->events_discarded
);
189 ret
= set_structure_field_integer(stream_class
->packet_context
,
190 "content_size", UINT64_MAX
);
195 ret
= set_structure_field_integer(stream_class
->packet_context
,
196 "packet_size", UINT64_MAX
);
201 /* Write packet context */
202 memcpy(&packet_context_pos
, &stream
->pos
,
203 sizeof(struct ctf_stream_pos
));
204 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
210 for (i
= 0; i
< stream
->events
->len
; i
++) {
211 struct bt_ctf_event
*event
= g_ptr_array_index(
213 uint32_t event_id
= bt_ctf_event_class_get_id(
215 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
217 ret
= set_structure_field_integer(stream_class
->event_header
,
222 ret
= set_structure_field_integer(stream_class
->event_header
,
223 "timestamp", timestamp
);
228 /* Write event header */
229 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
235 /* Write event content */
236 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
243 * Update the packet total size and content size and overwrite the
245 * Copy base_mma as the packet may have been remapped (e.g. when a
246 * packet is resized).
248 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
249 ret
= set_structure_field_integer(stream_class
->packet_context
,
250 "content_size", stream
->pos
.offset
);
255 ret
= set_structure_field_integer(stream_class
->packet_context
,
256 "packet_size", stream
->pos
.packet_size
);
261 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
262 &packet_context_pos
);
267 g_ptr_array_set_size(stream
->events
, 0);
268 stream
->flushed_packet_count
++;
270 bt_ctf_field_put(integer
);
274 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
280 bt_ctf_ref_get(&stream
->ref_count
);
283 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
289 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
293 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
295 struct bt_ctf_stream
*stream
;
301 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
302 ctf_fini_pos(&stream
->pos
);
303 if (close(stream
->pos
.fd
)) {
306 bt_ctf_stream_class_put(stream
->stream_class
);
307 g_ptr_array_free(stream
->events
, TRUE
);
312 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
317 struct bt_ctf_field
*integer
=
318 bt_ctf_field_structure_get_field(structure
, name
);
324 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
326 bt_ctf_field_put(integer
);