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 int bt_ctf_stream_get_discarded_events_count(
107 struct bt_ctf_stream
*stream
, uint64_t *count
)
111 if (!stream
|| !count
) {
116 *count
= stream
->events_discarded
;
121 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
122 uint64_t event_count
)
128 stream
->events_discarded
+= event_count
;
131 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
132 struct bt_ctf_event
*event
)
137 if (!stream
|| !event
) {
142 ret
= bt_ctf_event_validate(event
);
147 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
148 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
153 bt_ctf_event_get(event
);
154 g_ptr_array_add(stream
->events
, event
);
159 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
163 uint64_t timestamp_begin
, timestamp_end
;
164 struct bt_ctf_stream_class
*stream_class
;
165 struct bt_ctf_field
*integer
= NULL
;
166 struct ctf_stream_pos packet_context_pos
;
173 if (!stream
->events
->len
) {
177 if (stream
->flush
.func
) {
178 stream
->flush
.func(stream
, stream
->flush
.data
);
181 stream_class
= stream
->stream_class
;
182 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
183 stream
->events
, 0))->timestamp
;
184 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
185 stream
->events
, stream
->events
->len
- 1))->timestamp
;
186 ret
= set_structure_field_integer(stream_class
->packet_context
,
187 "timestamp_begin", timestamp_begin
);
192 ret
= set_structure_field_integer(stream_class
->packet_context
,
193 "timestamp_end", timestamp_end
);
198 ret
= set_structure_field_integer(stream_class
->packet_context
,
199 "events_discarded", stream
->events_discarded
);
204 ret
= set_structure_field_integer(stream_class
->packet_context
,
205 "content_size", UINT64_MAX
);
210 ret
= set_structure_field_integer(stream_class
->packet_context
,
211 "packet_size", UINT64_MAX
);
216 /* Write packet context */
217 memcpy(&packet_context_pos
, &stream
->pos
,
218 sizeof(struct ctf_stream_pos
));
219 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
225 for (i
= 0; i
< stream
->events
->len
; i
++) {
226 struct bt_ctf_event
*event
= g_ptr_array_index(
228 uint32_t event_id
= bt_ctf_event_class_get_id(
230 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
232 ret
= set_structure_field_integer(stream_class
->event_header
,
237 ret
= set_structure_field_integer(stream_class
->event_header
,
238 "timestamp", timestamp
);
243 /* Write event header */
244 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
250 /* Write event content */
251 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
258 * Update the packet total size and content size and overwrite the
260 * Copy base_mma as the packet may have been remapped (e.g. when a
261 * packet is resized).
263 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
264 ret
= set_structure_field_integer(stream_class
->packet_context
,
265 "content_size", stream
->pos
.offset
);
270 ret
= set_structure_field_integer(stream_class
->packet_context
,
271 "packet_size", stream
->pos
.packet_size
);
276 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
277 &packet_context_pos
);
282 g_ptr_array_set_size(stream
->events
, 0);
283 stream
->flushed_packet_count
++;
285 bt_ctf_field_put(integer
);
289 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
295 bt_ctf_ref_get(&stream
->ref_count
);
298 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
304 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
308 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
310 struct bt_ctf_stream
*stream
;
316 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
317 ctf_fini_pos(&stream
->pos
);
318 if (close(stream
->pos
.fd
)) {
321 bt_ctf_stream_class_put(stream
->stream_class
);
322 g_ptr_array_free(stream
->events
, TRUE
);
327 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
332 struct bt_ctf_field
*integer
=
333 bt_ctf_field_structure_get_field(structure
, name
);
339 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
341 bt_ctf_field_put(integer
);