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 | ||
7dd841e4 | 29 | #define BT_LOG_TAG "CTF-WRITER" |
bf436657 | 30 | #include "logging.h" |
20eee76e | 31 | |
086dc475 PP |
32 | #include <errno.h> |
33 | #include <fcntl.h> | |
34 | #include <inttypes.h> | |
273b65be JG |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <sys/stat.h> | |
273b65be | 38 | #include <unistd.h> |
273b65be | 39 | |
3996bc53 | 40 | #include <babeltrace2-ctf-writer/object.h> |
57952005 MJ |
41 | |
42 | #include "common/assert.h" | |
43 | #include "compat/compiler.h" | |
44 | #include "compat/endian.h" | |
d126826c | 45 | #include "common/uuid.h" |
57952005 MJ |
46 | |
47 | #include "clock.h" | |
48 | #include "fields.h" | |
49 | #include "field-types.h" | |
50 | #include "functor.h" | |
51 | #include "stream-class.h" | |
52 | #include "stream.h" | |
53 | #include "trace.h" | |
54 | #include "writer.h" | |
55 | ||
273b65be | 56 | static |
c2606e2f | 57 | void bt_ctf_writer_destroy(struct bt_ctf_object *obj); |
83509119 | 58 | |
488e09a7 | 59 | static |
8deee039 | 60 | int init_trace_packet_header(struct bt_ctf_trace *trace) |
488e09a7 PP |
61 | { |
62 | int ret = 0; | |
8deee039 | 63 | struct bt_ctf_field_type *_uint32_t = |
488e09a7 | 64 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
8deee039 | 65 | struct bt_ctf_field_type *_uint8_t = |
488e09a7 | 66 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); |
8deee039 PP |
67 | struct bt_ctf_field_type *trace_packet_header_type = |
68 | bt_ctf_field_type_structure_create(); | |
69 | struct bt_ctf_field_type *uuid_array_type = | |
70 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
488e09a7 PP |
71 | |
72 | if (!trace_packet_header_type || !uuid_array_type) { | |
73 | ret = -1; | |
74 | goto end; | |
75 | } | |
76 | ||
8deee039 | 77 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
488e09a7 PP |
78 | _uint32_t, "magic"); |
79 | if (ret) { | |
80 | goto end; | |
81 | } | |
82 | ||
8deee039 | 83 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
488e09a7 PP |
84 | uuid_array_type, "uuid"); |
85 | if (ret) { | |
86 | goto end; | |
87 | } | |
88 | ||
8deee039 | 89 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
488e09a7 PP |
90 | _uint32_t, "stream_id"); |
91 | if (ret) { | |
92 | goto end; | |
93 | } | |
94 | ||
8deee039 | 95 | ret = bt_ctf_trace_set_packet_header_field_type(trace, |
488e09a7 PP |
96 | trace_packet_header_type); |
97 | if (ret) { | |
98 | goto end; | |
99 | } | |
100 | end: | |
c2606e2f PP |
101 | bt_ctf_object_put_ref(uuid_array_type); |
102 | bt_ctf_object_put_ref(_uint32_t); | |
103 | bt_ctf_object_put_ref(_uint8_t); | |
104 | bt_ctf_object_put_ref(trace_packet_header_type); | |
488e09a7 PP |
105 | return ret; |
106 | } | |
107 | ||
273b65be JG |
108 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path) |
109 | { | |
3f5808e5 | 110 | int ret; |
273b65be | 111 | struct bt_ctf_writer *writer = NULL; |
d126826c | 112 | bt_uuid_t uuid; |
bb9c6e6a | 113 | char *metadata_path = NULL; |
273b65be JG |
114 | |
115 | if (!path) { | |
116 | goto error; | |
117 | } | |
118 | ||
119 | writer = g_new0(struct bt_ctf_writer, 1); | |
120 | if (!writer) { | |
121 | goto error; | |
122 | } | |
123 | ||
bb9c6e6a MJ |
124 | metadata_path = g_build_filename(path, "metadata", NULL); |
125 | ||
c2606e2f | 126 | bt_ctf_object_init_shared(&writer->base, bt_ctf_writer_destroy); |
273b65be JG |
127 | writer->path = g_string_new(path); |
128 | if (!writer->path) { | |
129 | goto error_destroy; | |
130 | } | |
131 | ||
8deee039 | 132 | writer->trace = bt_ctf_trace_create(); |
bc37ae52 JG |
133 | if (!writer->trace) { |
134 | goto error_destroy; | |
135 | } | |
136 | ||
488e09a7 PP |
137 | ret = init_trace_packet_header(writer->trace); |
138 | if (ret) { | |
139 | goto error_destroy; | |
140 | } | |
141 | ||
4a32fda0 | 142 | /* Generate a UUID for this writer's trace */ |
d126826c | 143 | bt_uuid_generate(uuid); |
20eee76e | 144 | |
8deee039 | 145 | ret = bt_ctf_trace_set_uuid(writer->trace, uuid); |
4a32fda0 PP |
146 | if (ret) { |
147 | goto error_destroy; | |
148 | } | |
149 | ||
c2606e2f PP |
150 | bt_ctf_object_set_parent(&writer->trace->common.base, &writer->base); |
151 | bt_ctf_object_put_ref(writer->trace); | |
3f5808e5 PP |
152 | |
153 | /* Default to little-endian */ | |
8deee039 | 154 | ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE); |
8b45963b | 155 | BT_ASSERT(ret == 0); |
3f5808e5 | 156 | |
273b65be JG |
157 | /* Create trace directory if necessary and open a metadata file */ |
158 | if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) { | |
159 | perror("g_mkdir_with_parents"); | |
160 | goto error_destroy; | |
161 | } | |
162 | ||
bb9c6e6a MJ |
163 | writer->metadata_fd = open(metadata_path, |
164 | O_WRONLY | O_CREAT | O_TRUNC, | |
165 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
166 | if (writer->metadata_fd < 0) { | |
273b65be JG |
167 | perror("open"); |
168 | goto error_destroy; | |
169 | } | |
170 | ||
bb9c6e6a | 171 | g_free(metadata_path); |
273b65be | 172 | return writer; |
bc37ae52 | 173 | |
273b65be | 174 | error_destroy: |
c2606e2f | 175 | BT_CTF_OBJECT_PUT_REF_AND_RESET(writer); |
273b65be | 176 | error: |
bb9c6e6a | 177 | g_free(metadata_path); |
273b65be JG |
178 | return writer; |
179 | } | |
180 | ||
c2606e2f | 181 | void bt_ctf_writer_destroy(struct bt_ctf_object *obj) |
273b65be JG |
182 | { |
183 | struct bt_ctf_writer *writer; | |
273b65be | 184 | |
83509119 | 185 | writer = container_of(obj, struct bt_ctf_writer, base); |
273b65be JG |
186 | bt_ctf_writer_flush_metadata(writer); |
187 | if (writer->path) { | |
188 | g_string_free(writer->path, TRUE); | |
189 | } | |
190 | ||
273b65be | 191 | if (writer->metadata_fd > 0) { |
9bb7e58b MD |
192 | if (close(writer->metadata_fd)) { |
193 | perror("close"); | |
9bb7e58b | 194 | } |
273b65be JG |
195 | } |
196 | ||
c2606e2f | 197 | bt_ctf_object_try_spec_release(&writer->trace->common.base); |
273b65be JG |
198 | g_free(writer); |
199 | } | |
200 | ||
8deee039 | 201 | struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer) |
a2540e85 | 202 | { |
8deee039 | 203 | struct bt_ctf_trace *trace = NULL; |
a2540e85 JG |
204 | |
205 | if (!writer) { | |
206 | goto end; | |
207 | } | |
208 | ||
209 | trace = writer->trace; | |
c2606e2f | 210 | bt_ctf_object_get_ref(trace); |
a2540e85 JG |
211 | end: |
212 | return trace; | |
213 | } | |
214 | ||
8deee039 PP |
215 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, |
216 | struct bt_ctf_stream_class *stream_class) | |
273b65be | 217 | { |
8deee039 | 218 | struct bt_ctf_stream *stream = NULL; |
319fd969 | 219 | int stream_class_count; |
d7279670 | 220 | bt_ctf_bool stream_class_found = BT_CTF_FALSE; |
319fd969 | 221 | int i; |
273b65be JG |
222 | |
223 | if (!writer || !stream_class) { | |
224 | goto error; | |
225 | } | |
226 | ||
319fd969 | 227 | /* Make sure the stream class is part of the writer's trace */ |
8deee039 | 228 | stream_class_count = bt_ctf_trace_get_stream_class_count(writer->trace); |
319fd969 | 229 | if (stream_class_count < 0) { |
273b65be JG |
230 | goto error; |
231 | } | |
232 | ||
319fd969 | 233 | for (i = 0; i < stream_class_count; i++) { |
8deee039 PP |
234 | struct bt_ctf_stream_class *existing_stream_class = |
235 | bt_ctf_trace_get_stream_class_by_index( | |
9ac68eb1 | 236 | writer->trace, i); |
319fd969 PP |
237 | |
238 | if (existing_stream_class == stream_class) { | |
d7279670 | 239 | stream_class_found = BT_CTF_TRUE; |
319fd969 PP |
240 | } |
241 | ||
c2606e2f | 242 | BT_CTF_OBJECT_PUT_REF_AND_RESET(existing_stream_class); |
319fd969 PP |
243 | |
244 | if (stream_class_found) { | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
249 | if (!stream_class_found) { | |
8deee039 | 250 | int ret = bt_ctf_trace_add_stream_class(writer->trace, |
319fd969 PP |
251 | stream_class); |
252 | ||
253 | if (ret) { | |
254 | goto error; | |
255 | } | |
256 | } | |
257 | ||
8deee039 | 258 | stream = bt_ctf_stream_create_with_id(stream_class, NULL, -1ULL); |
319fd969 | 259 | if (!stream) { |
273b65be JG |
260 | goto error; |
261 | } | |
262 | ||
273b65be | 263 | return stream; |
bc37ae52 | 264 | |
273b65be | 265 | error: |
c2606e2f | 266 | BT_CTF_OBJECT_PUT_REF_AND_RESET(stream); |
83509119 | 267 | return stream; |
273b65be JG |
268 | } |
269 | ||
270 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, | |
271 | const char *name, | |
272 | const char *value) | |
273 | { | |
bc37ae52 | 274 | int ret = -1; |
273b65be | 275 | |
bc37ae52 JG |
276 | if (!writer || !name || !value) { |
277 | goto end; | |
273b65be JG |
278 | } |
279 | ||
8deee039 | 280 | ret = bt_ctf_trace_set_environment_field_string(writer->trace, |
bc37ae52 JG |
281 | name, value); |
282 | end: | |
273b65be JG |
283 | return ret; |
284 | } | |
285 | ||
d7503815 | 286 | int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, |
9ac68eb1 | 287 | const char *name, int64_t value) |
d7503815 SM |
288 | { |
289 | int ret = -1; | |
290 | ||
291 | if (!writer || !name) { | |
292 | goto end; | |
293 | } | |
294 | ||
8deee039 | 295 | ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name, |
d7503815 SM |
296 | value); |
297 | end: | |
298 | return ret; | |
299 | } | |
300 | ||
273b65be JG |
301 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, |
302 | struct bt_ctf_clock *clock) | |
303 | { | |
bc37ae52 | 304 | int ret = -1; |
273b65be JG |
305 | |
306 | if (!writer || !clock) { | |
12af6048 JG |
307 | goto end; |
308 | } | |
273b65be | 309 | |
8deee039 | 310 | ret = bt_ctf_trace_add_clock_class(writer->trace, clock->clock_class); |
12af6048 JG |
311 | end: |
312 | return ret; | |
273b65be JG |
313 | } |
314 | ||
273b65be JG |
315 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer) |
316 | { | |
bc37ae52 | 317 | char *metadata_string = NULL; |
273b65be JG |
318 | |
319 | if (!writer) { | |
320 | goto end; | |
321 | } | |
322 | ||
8deee039 | 323 | metadata_string = bt_ctf_trace_get_metadata_string( |
bc37ae52 | 324 | writer->trace); |
273b65be | 325 | end: |
bc37ae52 | 326 | return metadata_string; |
273b65be JG |
327 | } |
328 | ||
329 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer) | |
330 | { | |
331 | int ret; | |
332 | char *metadata_string = NULL; | |
333 | ||
334 | if (!writer) { | |
335 | goto end; | |
336 | } | |
337 | ||
8deee039 | 338 | metadata_string = bt_ctf_trace_get_metadata_string( |
bc37ae52 | 339 | writer->trace); |
273b65be JG |
340 | if (!metadata_string) { |
341 | goto end; | |
342 | } | |
343 | ||
344 | if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) { | |
345 | perror("lseek"); | |
346 | goto end; | |
347 | } | |
348 | ||
349 | if (ftruncate(writer->metadata_fd, 0)) { | |
350 | perror("ftruncate"); | |
351 | goto end; | |
352 | } | |
353 | ||
354 | ret = write(writer->metadata_fd, metadata_string, | |
355 | strlen(metadata_string)); | |
356 | if (ret < 0) { | |
357 | perror("write"); | |
358 | goto end; | |
359 | } | |
360 | end: | |
361 | g_free(metadata_string); | |
362 | } | |
363 | ||
364 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, | |
8deee039 | 365 | enum bt_ctf_byte_order byte_order) |
273b65be JG |
366 | { |
367 | int ret = 0; | |
273b65be JG |
368 | |
369 | if (!writer || writer->frozen) { | |
370 | ret = -1; | |
371 | goto end; | |
372 | } | |
373 | ||
8deee039 | 374 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
6f4eefbe PP |
375 | if (BYTE_ORDER == LITTLE_ENDIAN) { |
376 | byte_order = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; | |
377 | } else { | |
378 | byte_order = BT_CTF_BYTE_ORDER_BIG_ENDIAN; | |
379 | } | |
3f5808e5 PP |
380 | } |
381 | ||
8deee039 | 382 | ret = bt_ctf_trace_set_native_byte_order(writer->trace, |
bc37ae52 | 383 | byte_order); |
273b65be JG |
384 | end: |
385 | return ret; | |
386 | } | |
387 | ||
8deee039 PP |
388 | BT_HIDDEN |
389 | void bt_ctf_writer_freeze(struct bt_ctf_writer *writer) | |
273b65be | 390 | { |
8deee039 | 391 | writer->frozen = 1; |
273b65be JG |
392 | } |
393 | ||
8deee039 PP |
394 | static |
395 | const unsigned int field_type_aliases_alignments[] = { | |
396 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
397 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
398 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
399 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
400 | }; | |
401 | ||
402 | static | |
403 | const unsigned int field_type_aliases_sizes[] = { | |
404 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
405 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
406 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
407 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
408 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
409 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
410 | }; | |
411 | ||
412 | BT_HIDDEN | |
413 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) | |
273b65be | 414 | { |
8deee039 PP |
415 | int ret; |
416 | unsigned int alignment, size; | |
417 | struct bt_ctf_field_type *field_type = NULL; | |
418 | ||
419 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
420 | goto end; | |
421 | } | |
422 | ||
423 | alignment = field_type_aliases_alignments[alias]; | |
424 | size = field_type_aliases_sizes[alias]; | |
425 | field_type = bt_ctf_field_type_integer_create(size); | |
426 | ret = bt_ctf_field_type_set_alignment(field_type, alignment); | |
427 | if (ret) { | |
c2606e2f | 428 | BT_CTF_OBJECT_PUT_REF_AND_RESET(field_type); |
8deee039 PP |
429 | } |
430 | end: | |
431 | return field_type; | |
273b65be JG |
432 | } |
433 | ||
319fd969 | 434 | BT_HIDDEN |
086dc475 | 435 | const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order) |
273b65be | 436 | { |
8deee039 PP |
437 | const char *string; |
438 | ||
439 | switch (byte_order) { | |
086dc475 | 440 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
8deee039 PP |
441 | string = "le"; |
442 | break; | |
086dc475 | 443 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
8deee039 PP |
444 | string = "be"; |
445 | break; | |
086dc475 | 446 | case BT_CTF_BYTE_ORDER_NATIVE: |
8deee039 PP |
447 | string = "native"; |
448 | break; | |
449 | default: | |
450 | abort(); | |
451 | } | |
452 | ||
453 | return string; | |
273b65be | 454 | } |