Commit | Line | Data |
---|---|---|
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> |
83509119 | 36 | #include <babeltrace/ref.h> |
273b65be JG |
37 | #include <babeltrace/compiler.h> |
38 | #include <stdio.h> | |
39 | #include <stdlib.h> | |
40 | #include <sys/stat.h> | |
41 | #include <errno.h> | |
42 | #include <unistd.h> | |
43 | #include <fcntl.h> | |
44 | #include <inttypes.h> | |
45 | ||
273b65be | 46 | static |
83509119 JG |
47 | void bt_ctf_writer_destroy(struct bt_object *obj); |
48 | ||
273b65be | 49 | static |
273b65be JG |
50 | int create_stream_file(struct bt_ctf_writer *writer, |
51 | struct bt_ctf_stream *stream); | |
273b65be | 52 | |
273b65be JG |
53 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path) |
54 | { | |
55 | struct bt_ctf_writer *writer = NULL; | |
56 | ||
57 | if (!path) { | |
58 | goto error; | |
59 | } | |
60 | ||
61 | writer = g_new0(struct bt_ctf_writer, 1); | |
62 | if (!writer) { | |
63 | goto error; | |
64 | } | |
65 | ||
83509119 | 66 | bt_object_init(writer, bt_ctf_writer_destroy); |
273b65be JG |
67 | writer->path = g_string_new(path); |
68 | if (!writer->path) { | |
69 | goto error_destroy; | |
70 | } | |
71 | ||
bc37ae52 JG |
72 | writer->trace = bt_ctf_trace_create(); |
73 | if (!writer->trace) { | |
74 | goto error_destroy; | |
75 | } | |
76 | ||
e6a8e8e4 JG |
77 | bt_object_set_parent(writer->trace, writer); |
78 | bt_put(writer->trace); | |
273b65be JG |
79 | /* Create trace directory if necessary and open a metadata file */ |
80 | if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { | |
81 | perror("g_mkdir_with_parents"); | |
82 | goto error_destroy; | |
83 | } | |
84 | ||
1b8180b9 | 85 | writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG); |
273b65be JG |
86 | if (writer->trace_dir_fd < 0) { |
87 | perror("open"); | |
88 | goto error_destroy; | |
89 | } | |
90 | ||
91 | writer->metadata_fd = openat(writer->trace_dir_fd, "metadata", | |
92 | O_WRONLY | O_CREAT | O_TRUNC, | |
93 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
273b65be JG |
94 | |
95 | return writer; | |
bc37ae52 | 96 | |
273b65be JG |
97 | error_destroy: |
98 | unlinkat(writer->trace_dir_fd, "metadata", 0); | |
83509119 | 99 | BT_PUT(writer); |
273b65be JG |
100 | error: |
101 | return writer; | |
102 | } | |
103 | ||
83509119 | 104 | void bt_ctf_writer_destroy(struct bt_object *obj) |
273b65be JG |
105 | { |
106 | struct bt_ctf_writer *writer; | |
273b65be | 107 | |
83509119 | 108 | writer = container_of(obj, struct bt_ctf_writer, base); |
273b65be JG |
109 | bt_ctf_writer_flush_metadata(writer); |
110 | if (writer->path) { | |
111 | g_string_free(writer->path, TRUE); | |
112 | } | |
113 | ||
114 | if (writer->trace_dir_fd > 0) { | |
9bb7e58b MD |
115 | if (close(writer->trace_dir_fd)) { |
116 | perror("close"); | |
9bb7e58b | 117 | } |
273b65be JG |
118 | } |
119 | ||
120 | if (writer->metadata_fd > 0) { | |
9bb7e58b MD |
121 | if (close(writer->metadata_fd)) { |
122 | perror("close"); | |
9bb7e58b | 123 | } |
273b65be JG |
124 | } |
125 | ||
e6a8e8e4 | 126 | bt_object_release(writer->trace); |
273b65be JG |
127 | g_free(writer); |
128 | } | |
129 | ||
a2540e85 JG |
130 | struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) |
131 | { | |
132 | struct bt_ctf_trace *trace = NULL; | |
133 | ||
134 | if (!writer) { | |
135 | goto end; | |
136 | } | |
137 | ||
138 | trace = writer->trace; | |
83509119 | 139 | bt_get(trace); |
a2540e85 JG |
140 | end: |
141 | return trace; | |
142 | } | |
143 | ||
273b65be JG |
144 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, |
145 | struct bt_ctf_stream_class *stream_class) | |
146 | { | |
273b65be JG |
147 | int stream_fd; |
148 | struct bt_ctf_stream *stream = NULL; | |
149 | ||
150 | if (!writer || !stream_class) { | |
151 | goto error; | |
152 | } | |
153 | ||
bc37ae52 | 154 | stream = bt_ctf_trace_create_stream(writer->trace, stream_class); |
273b65be JG |
155 | if (!stream) { |
156 | goto error; | |
157 | } | |
158 | ||
159 | stream_fd = create_stream_file(writer, stream); | |
160 | if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) { | |
161 | goto error; | |
162 | } | |
163 | ||
273b65be JG |
164 | writer->frozen = 1; |
165 | return stream; | |
bc37ae52 | 166 | |
273b65be | 167 | error: |
83509119 JG |
168 | BT_PUT(stream); |
169 | return stream; | |
273b65be JG |
170 | } |
171 | ||
172 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, | |
173 | const char *name, | |
174 | const char *value) | |
175 | { | |
bc37ae52 | 176 | int ret = -1; |
273b65be | 177 | |
bc37ae52 JG |
178 | if (!writer || !name || !value) { |
179 | goto end; | |
273b65be JG |
180 | } |
181 | ||
7f800dc7 | 182 | ret = bt_ctf_trace_set_environment_field_string(writer->trace, |
bc37ae52 JG |
183 | name, value); |
184 | end: | |
273b65be JG |
185 | return ret; |
186 | } | |
187 | ||
188 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, | |
189 | struct bt_ctf_clock *clock) | |
190 | { | |
bc37ae52 | 191 | int ret = -1; |
273b65be JG |
192 | |
193 | if (!writer || !clock) { | |
12af6048 JG |
194 | goto end; |
195 | } | |
273b65be | 196 | |
bc37ae52 | 197 | ret = bt_ctf_trace_add_clock(writer->trace, clock); |
12af6048 JG |
198 | end: |
199 | return ret; | |
273b65be JG |
200 | } |
201 | ||
273b65be JG |
202 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) |
203 | { | |
bc37ae52 | 204 | char *metadata_string = NULL; |
273b65be JG |
205 | |
206 | if (!writer) { | |
207 | goto end; | |
208 | } | |
209 | ||
bc37ae52 JG |
210 | metadata_string = bt_ctf_trace_get_metadata_string( |
211 | writer->trace); | |
273b65be | 212 | end: |
bc37ae52 | 213 | return metadata_string; |
273b65be JG |
214 | } |
215 | ||
216 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) | |
217 | { | |
218 | int ret; | |
219 | char *metadata_string = NULL; | |
220 | ||
221 | if (!writer) { | |
222 | goto end; | |
223 | } | |
224 | ||
bc37ae52 JG |
225 | metadata_string = bt_ctf_trace_get_metadata_string( |
226 | writer->trace); | |
273b65be JG |
227 | if (!metadata_string) { |
228 | goto end; | |
229 | } | |
230 | ||
231 | if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { | |
232 | perror("lseek"); | |
233 | goto end; | |
234 | } | |
235 | ||
236 | if (ftruncate(writer->metadata_fd, 0)) { | |
237 | perror("ftruncate"); | |
238 | goto end; | |
239 | } | |
240 | ||
241 | ret = write(writer->metadata_fd, metadata_string, | |
242 | strlen(metadata_string)); | |
243 | if (ret < 0) { | |
244 | perror("write"); | |
245 | goto end; | |
246 | } | |
247 | end: | |
248 | g_free(metadata_string); | |
249 | } | |
250 | ||
251 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, | |
252 | enum bt_ctf_byte_order byte_order) | |
253 | { | |
254 | int ret = 0; | |
273b65be JG |
255 | |
256 | if (!writer || writer->frozen) { | |
257 | ret = -1; | |
258 | goto end; | |
259 | } | |
260 | ||
bc37ae52 JG |
261 | ret = bt_ctf_trace_set_byte_order(writer->trace, |
262 | byte_order); | |
273b65be JG |
263 | end: |
264 | return ret; | |
265 | } | |
266 | ||
267 | void bt_ctf_writer_get(struct bt_ctf_writer *writer) | |
268 | { | |
83509119 | 269 | bt_get(writer); |
273b65be JG |
270 | } |
271 | ||
272 | void bt_ctf_writer_put(struct bt_ctf_writer *writer) | |
273 | { | |
83509119 | 274 | bt_put(writer); |
273b65be JG |
275 | } |
276 | ||
273b65be JG |
277 | static |
278 | int create_stream_file(struct bt_ctf_writer *writer, | |
279 | struct bt_ctf_stream *stream) | |
280 | { | |
281 | int fd; | |
282 | GString *filename = g_string_new(stream->stream_class->name->str); | |
283 | ||
3ea33115 JG |
284 | if (stream->stream_class->name->len == 0) { |
285 | int64_t ret; | |
286 | ||
287 | ret = bt_ctf_stream_class_get_id(stream->stream_class); | |
288 | if (ret < 0) { | |
289 | fd = -1; | |
290 | goto error; | |
291 | } | |
292 | ||
293 | g_string_printf(filename, "stream_%" PRId64, ret); | |
294 | } | |
295 | ||
273b65be JG |
296 | g_string_append_printf(filename, "_%" PRIu32, stream->id); |
297 | fd = openat(writer->trace_dir_fd, filename->str, | |
298 | O_RDWR | O_CREAT | O_TRUNC, | |
299 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
3ea33115 | 300 | error: |
273b65be JG |
301 | g_string_free(filename, TRUE); |
302 | return fd; | |
303 | } |