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-internal.h>
30 #include <babeltrace/ctf-writer/writer-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/ctf-ir/event-fields-internal.h>
33 #include <babeltrace/ctf-writer/functor-internal.h>
34 #include <babeltrace/ctf-ir/stream-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-internal.h>
36 #include <babeltrace/compiler.h>
46 void bt_ctf_writer_destroy(struct bt_ctf_ref
*ref
);
48 int create_stream_file(struct bt_ctf_writer
*writer
,
49 struct bt_ctf_stream
*stream
);
51 void stream_flush_cb(struct bt_ctf_stream
*stream
,
52 struct bt_ctf_writer
*writer
);
54 struct bt_ctf_writer
*bt_ctf_writer_create(const char *path
)
56 struct bt_ctf_writer
*writer
= NULL
;
62 writer
= g_new0(struct bt_ctf_writer
, 1);
67 bt_ctf_ref_init(&writer
->ref_count
);
68 writer
->path
= g_string_new(path
);
73 writer
->trace
= bt_ctf_trace_create();
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");
84 writer
->trace_dir_fd
= open(path
, O_RDONLY
, S_IRWXU
| S_IRWXG
);
85 if (writer
->trace_dir_fd
< 0) {
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
);
97 unlinkat(writer
->trace_dir_fd
, "metadata", 0);
98 bt_ctf_writer_destroy(&writer
->ref_count
);
104 void bt_ctf_writer_destroy(struct bt_ctf_ref
*ref
)
106 struct bt_ctf_writer
*writer
;
112 writer
= container_of(ref
, struct bt_ctf_writer
, ref_count
);
113 bt_ctf_writer_flush_metadata(writer
);
115 g_string_free(writer
->path
, TRUE
);
118 if (writer
->trace_dir_fd
> 0) {
119 if (close(writer
->trace_dir_fd
)) {
124 if (writer
->metadata_fd
> 0) {
125 if (close(writer
->metadata_fd
)) {
130 bt_ctf_trace_put(writer
->trace
);
134 struct bt_ctf_trace
*bt_ctf_writer_get_trace(struct bt_ctf_writer
*writer
)
136 struct bt_ctf_trace
*trace
= NULL
;
142 trace
= writer
->trace
;
143 bt_ctf_trace_get(trace
);
148 struct bt_ctf_stream
*bt_ctf_writer_create_stream(struct bt_ctf_writer
*writer
,
149 struct bt_ctf_stream_class
*stream_class
)
152 struct bt_ctf_stream
*stream
= NULL
;
154 if (!writer
|| !stream_class
) {
158 stream
= bt_ctf_trace_create_stream(writer
->trace
, stream_class
);
163 stream_fd
= create_stream_file(writer
, stream
);
164 if (stream_fd
< 0 || bt_ctf_stream_set_fd(stream
, stream_fd
)) {
168 bt_ctf_stream_set_flush_callback(stream
, (flush_func
)stream_flush_cb
,
174 bt_ctf_stream_put(stream
);
178 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer
*writer
,
184 if (!writer
|| !name
|| !value
) {
188 ret
= bt_ctf_trace_add_environment_field(writer
->trace
,
194 int bt_ctf_writer_add_clock(struct bt_ctf_writer
*writer
,
195 struct bt_ctf_clock
*clock
)
199 if (!writer
|| !clock
) {
203 ret
= bt_ctf_trace_add_clock(writer
->trace
, clock
);
208 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer
*writer
)
210 char *metadata_string
= NULL
;
216 metadata_string
= bt_ctf_trace_get_metadata_string(
219 return metadata_string
;
222 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer
*writer
)
225 char *metadata_string
= NULL
;
231 metadata_string
= bt_ctf_trace_get_metadata_string(
233 if (!metadata_string
) {
237 if (lseek(writer
->metadata_fd
, 0, SEEK_SET
) == (off_t
)-1) {
242 if (ftruncate(writer
->metadata_fd
, 0)) {
247 ret
= write(writer
->metadata_fd
, metadata_string
,
248 strlen(metadata_string
));
254 g_free(metadata_string
);
257 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer
*writer
,
258 enum bt_ctf_byte_order byte_order
)
262 if (!writer
|| writer
->frozen
) {
267 ret
= bt_ctf_trace_set_byte_order(writer
->trace
,
273 void bt_ctf_writer_get(struct bt_ctf_writer
*writer
)
279 bt_ctf_ref_get(&writer
->ref_count
);
282 void bt_ctf_writer_put(struct bt_ctf_writer
*writer
)
288 bt_ctf_ref_put(&writer
->ref_count
, bt_ctf_writer_destroy
);
292 int create_stream_file(struct bt_ctf_writer
*writer
,
293 struct bt_ctf_stream
*stream
)
296 GString
*filename
= g_string_new(stream
->stream_class
->name
->str
);
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
);
307 void stream_flush_cb(struct bt_ctf_stream
*stream
, struct bt_ctf_writer
*writer
)
309 struct bt_ctf_field
*stream_id
;
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
);
317 stream_id
= bt_ctf_field_structure_get_field(
318 writer
->trace
->trace_packet_header
, "stream_id");
319 bt_ctf_field_unsigned_integer_set_value(stream_id
, stream
->stream_class
->id
);
320 bt_ctf_field_put(stream_id
);
322 /* Write the trace_packet_header */
323 bt_ctf_field_serialize(writer
->trace
->trace_packet_header
, &stream
->pos
);