Add trace accessor to CTF Writer
[babeltrace.git] / formats / ctf / writer / writer.c
CommitLineData
273b65be
JG
1/*
2 * writer.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
adc315b8 29#include <babeltrace/ctf-ir/clock-internal.h>
273b65be 30#include <babeltrace/ctf-writer/writer-internal.h>
adc315b8
JG
31#include <babeltrace/ctf-ir/event-types-internal.h>
32#include <babeltrace/ctf-ir/event-fields-internal.h>
273b65be 33#include <babeltrace/ctf-writer/functor-internal.h>
adc315b8 34#include <babeltrace/ctf-ir/stream-class-internal.h>
3f043b05 35#include <babeltrace/ctf-ir/stream-internal.h>
273b65be
JG
36#include <babeltrace/compiler.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <sys/stat.h>
40#include <errno.h>
41#include <unistd.h>
42#include <fcntl.h>
43#include <inttypes.h>
44
273b65be
JG
45static
46void bt_ctf_writer_destroy(struct bt_ctf_ref *ref);
47static
273b65be
JG
48int create_stream_file(struct bt_ctf_writer *writer,
49 struct bt_ctf_stream *stream);
50static
51void stream_flush_cb(struct bt_ctf_stream *stream,
52 struct bt_ctf_writer *writer);
53
273b65be
JG
54struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
55{
56 struct bt_ctf_writer *writer = NULL;
57
58 if (!path) {
59 goto error;
60 }
61
62 writer = g_new0(struct bt_ctf_writer, 1);
63 if (!writer) {
64 goto error;
65 }
66
273b65be
JG
67 bt_ctf_ref_init(&writer->ref_count);
68 writer->path = g_string_new(path);
69 if (!writer->path) {
70 goto error_destroy;
71 }
72
bc37ae52
JG
73 writer->trace = bt_ctf_trace_create();
74 if (!writer->trace) {
75 goto error_destroy;
76 }
77
273b65be
JG
78 /* Create trace directory if necessary and open a metadata file */
79 if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) {
80 perror("g_mkdir_with_parents");
81 goto error_destroy;
82 }
83
1b8180b9 84 writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
273b65be
JG
85 if (writer->trace_dir_fd < 0) {
86 perror("open");
87 goto error_destroy;
88 }
89
90 writer->metadata_fd = openat(writer->trace_dir_fd, "metadata",
91 O_WRONLY | O_CREAT | O_TRUNC,
92 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
273b65be
JG
93
94 return writer;
bc37ae52 95
273b65be
JG
96error_destroy:
97 unlinkat(writer->trace_dir_fd, "metadata", 0);
98 bt_ctf_writer_destroy(&writer->ref_count);
99 writer = NULL;
100error:
101 return writer;
102}
103
104void bt_ctf_writer_destroy(struct bt_ctf_ref *ref)
105{
106 struct bt_ctf_writer *writer;
107
108 if (!ref) {
109 return;
110 }
111
112 writer = container_of(ref, struct bt_ctf_writer, ref_count);
113 bt_ctf_writer_flush_metadata(writer);
114 if (writer->path) {
115 g_string_free(writer->path, TRUE);
116 }
117
118 if (writer->trace_dir_fd > 0) {
9bb7e58b
MD
119 if (close(writer->trace_dir_fd)) {
120 perror("close");
9bb7e58b 121 }
273b65be
JG
122 }
123
124 if (writer->metadata_fd > 0) {
9bb7e58b
MD
125 if (close(writer->metadata_fd)) {
126 perror("close");
9bb7e58b 127 }
273b65be
JG
128 }
129
bc37ae52 130 bt_ctf_trace_put(writer->trace);
273b65be
JG
131 g_free(writer);
132}
133
a2540e85
JG
134struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
135{
136 struct bt_ctf_trace *trace = NULL;
137
138 if (!writer) {
139 goto end;
140 }
141
142 trace = writer->trace;
143 bt_ctf_trace_get(trace);
144end:
145 return trace;
146}
147
273b65be
JG
148struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
149 struct bt_ctf_stream_class *stream_class)
150{
273b65be
JG
151 int stream_fd;
152 struct bt_ctf_stream *stream = NULL;
153
154 if (!writer || !stream_class) {
155 goto error;
156 }
157
bc37ae52 158 stream = bt_ctf_trace_create_stream(writer->trace, stream_class);
273b65be
JG
159 if (!stream) {
160 goto error;
161 }
162
163 stream_fd = create_stream_file(writer, stream);
164 if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) {
165 goto error;
166 }
167
168 bt_ctf_stream_set_flush_callback(stream, (flush_func)stream_flush_cb,
169 writer);
273b65be
JG
170 writer->frozen = 1;
171 return stream;
bc37ae52 172
273b65be
JG
173error:
174 bt_ctf_stream_put(stream);
175 return NULL;
176}
177
178int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
179 const char *name,
180 const char *value)
181{
bc37ae52 182 int ret = -1;
273b65be 183
bc37ae52
JG
184 if (!writer || !name || !value) {
185 goto end;
273b65be
JG
186 }
187
bc37ae52
JG
188 ret = bt_ctf_trace_add_environment_field(writer->trace,
189 name, value);
190end:
273b65be
JG
191 return ret;
192}
193
194int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
195 struct bt_ctf_clock *clock)
196{
bc37ae52 197 int ret = -1;
273b65be
JG
198
199 if (!writer || !clock) {
12af6048
JG
200 goto end;
201 }
273b65be 202
bc37ae52 203 ret = bt_ctf_trace_add_clock(writer->trace, clock);
12af6048
JG
204end:
205 return ret;
273b65be
JG
206}
207
273b65be
JG
208char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
209{
bc37ae52 210 char *metadata_string = NULL;
273b65be
JG
211
212 if (!writer) {
213 goto end;
214 }
215
bc37ae52
JG
216 metadata_string = bt_ctf_trace_get_metadata_string(
217 writer->trace);
273b65be 218end:
bc37ae52 219 return metadata_string;
273b65be
JG
220}
221
222void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
223{
224 int ret;
225 char *metadata_string = NULL;
226
227 if (!writer) {
228 goto end;
229 }
230
bc37ae52
JG
231 metadata_string = bt_ctf_trace_get_metadata_string(
232 writer->trace);
273b65be
JG
233 if (!metadata_string) {
234 goto end;
235 }
236
237 if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
238 perror("lseek");
239 goto end;
240 }
241
242 if (ftruncate(writer->metadata_fd, 0)) {
243 perror("ftruncate");
244 goto end;
245 }
246
247 ret = write(writer->metadata_fd, metadata_string,
248 strlen(metadata_string));
249 if (ret < 0) {
250 perror("write");
251 goto end;
252 }
253end:
254 g_free(metadata_string);
255}
256
257int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
258 enum bt_ctf_byte_order byte_order)
259{
260 int ret = 0;
273b65be
JG
261
262 if (!writer || writer->frozen) {
263 ret = -1;
264 goto end;
265 }
266
bc37ae52
JG
267 ret = bt_ctf_trace_set_byte_order(writer->trace,
268 byte_order);
273b65be
JG
269end:
270 return ret;
271}
272
273void bt_ctf_writer_get(struct bt_ctf_writer *writer)
274{
275 if (!writer) {
276 return;
277 }
278
279 bt_ctf_ref_get(&writer->ref_count);
280}
281
282void bt_ctf_writer_put(struct bt_ctf_writer *writer)
283{
284 if (!writer) {
285 return;
286 }
287
288 bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy);
289}
290
273b65be
JG
291static
292int create_stream_file(struct bt_ctf_writer *writer,
293 struct bt_ctf_stream *stream)
294{
295 int fd;
296 GString *filename = g_string_new(stream->stream_class->name->str);
297
298 g_string_append_printf(filename, "_%" PRIu32, stream->id);
299 fd = openat(writer->trace_dir_fd, filename->str,
300 O_RDWR | O_CREAT | O_TRUNC,
301 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
302 g_string_free(filename, TRUE);
303 return fd;
304}
305
306static
307void stream_flush_cb(struct bt_ctf_stream *stream, struct bt_ctf_writer *writer)
308{
309 struct bt_ctf_field *stream_id;
310
311 /* Start a new packet in the stream */
312 if (stream->flushed_packet_count) {
313 /* ctf_init_pos has already initialized the first packet */
314 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
315 }
316
317 stream_id = bt_ctf_field_structure_get_field(
bc37ae52 318 writer->trace->trace_packet_header, "stream_id");
c13c7ca0 319 bt_ctf_field_unsigned_integer_set_value(stream_id, stream->stream_class->id);
273b65be
JG
320 bt_ctf_field_put(stream_id);
321
322 /* Write the trace_packet_header */
bc37ae52 323 bt_ctf_field_serialize(writer->trace->trace_packet_header, &stream->pos);
273b65be 324}
This page took 0.037611 seconds and 4 git commands to generate.