Move CTF-Writer stream to CTF-IR
[babeltrace.git] / formats / ctf / ir / stream.c
CommitLineData
273b65be
JG
1/*
2 * stream.c
3 *
4 * Babeltrace CTF Writer
5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
3f043b05 29#include <babeltrace/ctf-ir/clock.h>
adc315b8 30#include <babeltrace/ctf-ir/clock-internal.h>
273b65be 31#include <babeltrace/ctf-writer/event.h>
adc315b8
JG
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>
3f043b05
JG
35#include <babeltrace/ctf-ir/stream.h>
36#include <babeltrace/ctf-ir/stream-internal.h>
adc315b8 37#include <babeltrace/ctf-ir/stream-class-internal.h>
273b65be
JG
38#include <babeltrace/ctf-writer/functor-internal.h>
39#include <babeltrace/compiler.h>
40#include <babeltrace/align.h>
41
42static
43void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
44static
273b65be
JG
45int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
46
273b65be
JG
47BT_HIDDEN
48struct bt_ctf_stream *bt_ctf_stream_create(
49 struct bt_ctf_stream_class *stream_class)
50{
51 struct bt_ctf_stream *stream = NULL;
52
53 if (!stream_class) {
54 goto end;
55 }
56
57 stream = g_new0(struct bt_ctf_stream, 1);
58 if (!stream) {
59 goto end;
60 }
61
62 bt_ctf_ref_init(&stream->ref_count);
63 stream->pos.fd = -1;
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);
70end:
71 return stream;
72}
73
74BT_HIDDEN
75int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream *stream,
76 flush_func callback, void *data)
77{
78 int ret = stream ? 0 : -1;
79
80 if (!stream) {
81 goto end;
82 }
83
84 stream->flush.func = callback;
85 stream->flush.data = data;
86end:
87 return ret;
88}
89
90BT_HIDDEN
91int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
92{
93 int ret = 0;
94
95 if (stream->pos.fd != -1) {
96 ret = -1;
97 goto end;
98 }
99
100 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
101 stream->pos.fd = fd;
102end:
103 return ret;
104}
105
106void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
107 uint64_t event_count)
108{
109 if (!stream) {
110 return;
111 }
112
113 stream->events_discarded += event_count;
114}
115
116int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
117 struct bt_ctf_event *event)
118{
119 int ret = 0;
120 uint64_t timestamp;
121
122 if (!stream || !event) {
123 ret = -1;
124 goto end;
125 }
126
127 ret = bt_ctf_event_validate(event);
128 if (ret) {
129 goto end;
130 }
131
132 timestamp = bt_ctf_clock_get_time(stream->stream_class->clock);
133 ret = bt_ctf_event_set_timestamp(event, timestamp);
134 if (ret) {
135 goto end;
136 }
137
138 bt_ctf_event_get(event);
139 g_ptr_array_add(stream->events, event);
140end:
141 return ret;
142}
143
144int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
145{
146 int ret = 0;
147 size_t i;
148 uint64_t timestamp_begin, timestamp_end;
9f56e450 149 struct bt_ctf_stream_class *stream_class;
273b65be
JG
150 struct bt_ctf_field *integer = NULL;
151 struct ctf_stream_pos packet_context_pos;
152
153 if (!stream) {
154 ret = -1;
155 goto end;
156 }
157
158 if (!stream->events->len) {
159 goto end;
160 }
161
162 if (stream->flush.func) {
163 stream->flush.func(stream, stream->flush.data);
164 }
165
9f56e450 166 stream_class = stream->stream_class;
273b65be
JG
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);
173 if (ret) {
174 goto end;
175 }
176
177 ret = set_structure_field_integer(stream_class->packet_context,
178 "timestamp_end", timestamp_end);
179 if (ret) {
180 goto end;
181 }
182
183 ret = set_structure_field_integer(stream_class->packet_context,
184 "events_discarded", stream->events_discarded);
185 if (ret) {
186 goto end;
187 }
188
189 ret = set_structure_field_integer(stream_class->packet_context,
190 "content_size", UINT64_MAX);
191 if (ret) {
192 goto end;
193 }
194
195 ret = set_structure_field_integer(stream_class->packet_context,
196 "packet_size", UINT64_MAX);
197 if (ret) {
198 goto end;
199 }
200
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,
205 &stream->pos);
206 if (ret) {
207 goto end;
208 }
209
210 for (i = 0; i < stream->events->len; i++) {
211 struct bt_ctf_event *event = g_ptr_array_index(
212 stream->events, i);
213 uint32_t event_id = bt_ctf_event_class_get_id(
214 event->event_class);
215 uint64_t timestamp = bt_ctf_event_get_timestamp(event);
216
217 ret = set_structure_field_integer(stream_class->event_header,
218 "id", event_id);
219 if (ret) {
220 goto end;
221 }
222 ret = set_structure_field_integer(stream_class->event_header,
223 "timestamp", timestamp);
224 if (ret) {
225 goto end;
226 }
227
228 /* Write event header */
229 ret = bt_ctf_field_serialize(stream_class->event_header,
230 &stream->pos);
231 if (ret) {
232 goto end;
233 }
234
235 /* Write event content */
236 ret = bt_ctf_event_serialize(event, &stream->pos);
237 if (ret) {
238 goto end;
239 }
240 }
241
242 /*
243 * Update the packet total size and content size and overwrite the
244 * packet context.
3a092c05
JG
245 * Copy base_mma as the packet may have been remapped (e.g. when a
246 * packet is resized).
273b65be 247 */
3a092c05 248 packet_context_pos.base_mma = stream->pos.base_mma;
273b65be
JG
249 ret = set_structure_field_integer(stream_class->packet_context,
250 "content_size", stream->pos.offset);
251 if (ret) {
252 goto end;
253 }
254
255 ret = set_structure_field_integer(stream_class->packet_context,
256 "packet_size", stream->pos.packet_size);
257 if (ret) {
258 goto end;
259 }
260
261 ret = bt_ctf_field_serialize(stream_class->packet_context,
262 &packet_context_pos);
263 if (ret) {
264 goto end;
265 }
266
267 g_ptr_array_set_size(stream->events, 0);
268 stream->flushed_packet_count++;
269end:
270 bt_ctf_field_put(integer);
271 return ret;
272}
273
274void bt_ctf_stream_get(struct bt_ctf_stream *stream)
275{
276 if (!stream) {
277 return;
278 }
279
280 bt_ctf_ref_get(&stream->ref_count);
281}
282
283void bt_ctf_stream_put(struct bt_ctf_stream *stream)
284{
285 if (!stream) {
286 return;
287 }
288
289 bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
290}
291
292static
293void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
294{
295 struct bt_ctf_stream *stream;
296
297 if (!ref) {
298 return;
299 }
300
301 stream = container_of(ref, struct bt_ctf_stream, ref_count);
302 ctf_fini_pos(&stream->pos);
9f56e450
JG
303 if (close(stream->pos.fd)) {
304 perror("close");
305 }
273b65be
JG
306 bt_ctf_stream_class_put(stream->stream_class);
307 g_ptr_array_free(stream->events, TRUE);
308 g_free(stream);
309}
310
273b65be
JG
311static
312int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
313 uint64_t value)
314{
315 int ret = 0;
316
317 struct bt_ctf_field *integer =
318 bt_ctf_field_structure_get_field(structure, name);
319 if (!integer) {
320 ret = -1;
321 goto end;
322 }
323
324 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
325end:
326 bt_ctf_field_put(integer);
327 return ret;
328}
This page took 0.035513 seconds and 4 git commands to generate.