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