6 * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <babeltrace/ctf-writer/writer.h>
24 #include <babeltrace/ctf-writer/clock.h>
25 #include <babeltrace/ctf-writer/stream.h>
26 #include <babeltrace/ctf-writer/event.h>
27 #include <babeltrace/ctf-writer/event-types.h>
28 #include <babeltrace/ctf-writer/event-fields.h>
29 #include <babeltrace/ctf/events.h>
33 #include <sys/utsname.h>
43 #define METADATA_LINE_SIZE 512
44 #define SEQUENCE_TEST_LENGTH 10
45 #define PACKET_RESIZE_TEST_LENGTH 100000
47 static uint64_t current_time
;
49 void validate_metadata(char *parser_path
, char *metadata_path
)
52 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
53 int parser_output_fd
= -1, metadata_fd
= -1;
60 parser_output_fd
= mkstemp(parser_output_path
);
61 metadata_fd
= open(metadata_path
, O_RDONLY
);
63 unlink(parser_output_path
);
65 if (parser_output_fd
== -1 || metadata_fd
== -1) {
66 diag("Failed create temporary files for metadata parsing.");
74 waitpid(pid
, &status
, 0);
75 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
77 /* ctf-parser-test expects a metadata string on stdin. */
78 ret
= dup2(metadata_fd
, STDIN_FILENO
);
80 perror("# dup2 metadata_fd to STDIN");
84 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
86 perror("# dup2 parser_output_fd to STDOUT");
90 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
92 perror("# dup2 parser_output_fd to STDERR");
96 execl(parser_path
, "ctf-parser-test", NULL
);
97 perror("# Could not launch the ctf metadata parser process");
101 ok(ret
== 0, "Metadata string is valid");
103 if (ret
&& metadata_fd
> 0 && parser_output_fd
> 0) {
105 size_t len
= METADATA_LINE_SIZE
;
106 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
108 metadata_fp
= fdopen(metadata_fd
, "r");
110 perror("fdopen on metadata_fd");
114 parser_output_fp
= fdopen(parser_output_fd
, "r");
115 if (!parser_output_fp
) {
116 perror("fdopen on parser_output_fd");
122 diag("malloc failure");
127 /* Output the metadata and parser output as diagnostic */
128 while (getline(&line
, &len
, metadata_fp
) > 0) {
132 rewind(parser_output_fp
);
133 while (getline(&line
, &len
, parser_output_fp
) > 0) {
140 fclose(parser_output_fp
);
143 close(parser_output_fd
);
147 void validate_trace(char *parser_path
, char *trace_path
)
150 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
151 int babeltrace_output_fd
= -1;
158 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
159 unlink(babeltrace_output_path
);
161 if (babeltrace_output_fd
== -1) {
162 diag("Failed to create a temporary file for trace parsing.");
170 waitpid(pid
, &status
, 0);
171 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
173 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
175 perror("# dup2 babeltrace_output_fd to STDOUT");
179 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
181 perror("# dup2 babeltrace_output_fd to STDERR");
185 execl(parser_path
, "babeltrace", trace_path
, NULL
);
186 perror("# Could not launch the babeltrace process");
190 ok(ret
== 0, "Babeltrace could read the resulting trace");
192 if (ret
&& babeltrace_output_fd
> 0) {
194 size_t len
= METADATA_LINE_SIZE
;
195 FILE *babeltrace_output_fp
= NULL
;
197 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
198 if (!babeltrace_output_fp
) {
199 perror("fdopen on babeltrace_output_fd");
204 rewind(babeltrace_output_fp
);
205 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
211 fclose(babeltrace_output_fp
);
214 close(babeltrace_output_fd
);
217 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
218 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
220 /* Create and add a simple event class */
221 struct bt_ctf_event_class
*simple_event_class
=
222 bt_ctf_event_class_create("Simple Event");
223 struct bt_ctf_field_type
*uint_12_type
=
224 bt_ctf_field_type_integer_create(12);
225 struct bt_ctf_field_type
*float_type
=
226 bt_ctf_field_type_floating_point_create();
227 struct bt_ctf_event
*simple_event
;
228 struct bt_ctf_field
*integer_field
;
229 struct bt_ctf_field
*float_field
;
231 bt_ctf_field_type_set_alignment(float_type
, 32);
232 bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11);
233 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53);
234 ok(uint_12_type
, "Create an unsigned integer type");
235 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
237 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
239 bt_ctf_stream_class_add_event_class(stream_class
,
242 simple_event
= bt_ctf_event_create(simple_event_class
);
245 "Instantiate an event containing a single integer field");
247 integer_field
= bt_ctf_field_create(uint_12_type
);
248 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
249 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
250 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
252 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
253 bt_ctf_field_floating_point_set_value(float_field
, 3.1415);
255 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
257 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
258 "Append simple event to trace stream");
260 ok(bt_ctf_stream_flush(stream
) == 0,
261 "Flush trace stream with one event");
263 bt_ctf_event_class_put(simple_event_class
);
264 bt_ctf_event_put(simple_event
);
265 bt_ctf_field_type_put(uint_12_type
);
266 bt_ctf_field_type_put(float_type
);
267 bt_ctf_field_put(integer_field
);
268 bt_ctf_field_put(float_field
);
271 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
272 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
275 struct bt_ctf_field_type
*uint_35_type
=
276 bt_ctf_field_type_integer_create(35);
277 struct bt_ctf_field_type
*int_16_type
=
278 bt_ctf_field_type_integer_create(16);
279 struct bt_ctf_field_type
*uint_3_type
=
280 bt_ctf_field_type_integer_create(3);
281 struct bt_ctf_field_type
*enum_variant_type
=
282 bt_ctf_field_type_enumeration_create(uint_3_type
);
283 struct bt_ctf_field_type
*variant_type
=
284 bt_ctf_field_type_variant_create(enum_variant_type
,
286 struct bt_ctf_field_type
*string_type
=
287 bt_ctf_field_type_string_create();
288 struct bt_ctf_field_type
*sequence_type
;
289 struct bt_ctf_field_type
*inner_structure_type
=
290 bt_ctf_field_type_structure_create();
291 struct bt_ctf_field_type
*complex_structure_type
=
292 bt_ctf_field_type_structure_create();
293 struct bt_ctf_event_class
*event_class
;
294 struct bt_ctf_event
*event
;
295 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
296 *inner_structure_field
, *complex_structure_field
,
297 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
300 bt_ctf_field_type_set_alignment(int_16_type
, 32);
301 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
302 bt_ctf_field_type_integer_set_base(uint_35_type
,
303 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
305 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
307 bt_ctf_field_type_structure_add_field(inner_structure_type
,
308 uint_35_type
, "seq_len");
309 bt_ctf_field_type_structure_add_field(inner_structure_type
,
310 sequence_type
, "a_sequence");
312 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
314 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
316 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
317 "UINT35_TYPE", 2, 7);
318 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
319 "An unknown entry"), "Reject a variant field based on an unknown tag value");
320 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
321 "UINT3_TYPE") == 0, "Add a field to a variant");
322 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
324 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
327 bt_ctf_field_type_structure_add_field(complex_structure_type
,
328 enum_variant_type
, "variant_selector");
329 bt_ctf_field_type_structure_add_field(complex_structure_type
,
330 string_type
, "a_string");
331 bt_ctf_field_type_structure_add_field(complex_structure_type
,
332 variant_type
, "variant_value");
333 bt_ctf_field_type_structure_add_field(complex_structure_type
,
334 inner_structure_type
, "inner_structure");
336 ok(bt_ctf_event_class_create("clock") == NULL
,
337 "Reject creation of an event class with an illegal name");
338 event_class
= bt_ctf_event_class_create("Complex Test Event");
339 ok(event_class
, "Create an event class");
340 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
341 "Reject addition of a field with an empty name to an event");
342 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
343 "Reject addition of a field with a NULL type to an event");
344 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
346 "Reject addition of a type with an illegal name to an event");
347 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
349 "Add field of type unsigned integer to an event");
350 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
351 "int_16") == 0, "Add field of type signed integer to an event");
352 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
353 "complex_structure") == 0,
354 "Add composite structure to an event");
356 /* Add event class to the stream class */
357 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
358 "Reject addition of NULL event class to a stream class");
359 ok(bt_ctf_stream_class_add_event_class(stream_class
,
360 event_class
) == 0, "Add an event class to stream class");
362 event
= bt_ctf_event_create(event_class
);
363 ok(event
, "Instanciate a complex event");
365 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
366 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
367 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
368 bt_ctf_field_put(uint_35_field
);
370 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
371 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
372 bt_ctf_field_put(int_16_field
);
374 complex_structure_field
= bt_ctf_event_get_payload(event
,
375 "complex_structure");
376 inner_structure_field
= bt_ctf_field_structure_get_field(
377 complex_structure_field
, "inner_structure");
378 a_string_field
= bt_ctf_field_structure_get_field(
379 complex_structure_field
, "a_string");
380 enum_variant_field
= bt_ctf_field_structure_get_field(
381 complex_structure_field
, "variant_selector");
382 variant_field
= bt_ctf_field_structure_get_field(
383 complex_structure_field
, "variant_value");
384 uint_35_field
= bt_ctf_field_structure_get_field(
385 inner_structure_field
, "seq_len");
386 a_sequence_field
= bt_ctf_field_structure_get_field(
387 inner_structure_field
, "a_sequence");
389 enum_container_field
= bt_ctf_field_enumeration_get_container(
391 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
392 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
394 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
395 bt_ctf_field_put(int_16_field
);
396 bt_ctf_field_string_set_value(a_string_field
,
398 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
399 SEQUENCE_TEST_LENGTH
);
400 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
401 uint_35_field
) == 0, "Set a sequence field's length");
403 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
404 int_16_field
= bt_ctf_field_sequence_get_field(
405 a_sequence_field
, i
);
406 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
407 bt_ctf_field_put(int_16_field
);
410 bt_ctf_clock_set_time(clock
, ++current_time
);
411 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
412 "Append a complex event to a stream");
413 ok(bt_ctf_stream_flush(stream
) == 0,
414 "Flush a stream containing a complex event");
416 bt_ctf_field_put(uint_35_field
);
417 bt_ctf_field_put(a_string_field
);
418 bt_ctf_field_put(inner_structure_field
);
419 bt_ctf_field_put(complex_structure_field
);
420 bt_ctf_field_put(a_sequence_field
);
421 bt_ctf_field_put(enum_variant_field
);
422 bt_ctf_field_put(enum_container_field
);
423 bt_ctf_field_put(variant_field
);
424 bt_ctf_field_type_put(uint_35_type
);
425 bt_ctf_field_type_put(int_16_type
);
426 bt_ctf_field_type_put(string_type
);
427 bt_ctf_field_type_put(sequence_type
);
428 bt_ctf_field_type_put(inner_structure_type
);
429 bt_ctf_field_type_put(complex_structure_type
);
430 bt_ctf_field_type_put(uint_3_type
);
431 bt_ctf_field_type_put(enum_variant_type
);
432 bt_ctf_field_type_put(variant_type
);
433 bt_ctf_event_class_put(event_class
);
434 bt_ctf_event_put(event
);
437 void type_field_tests()
439 struct bt_ctf_field
*uint_12
;
440 struct bt_ctf_field
*int_16
;
441 struct bt_ctf_field
*string
;
442 struct bt_ctf_field_type
*composite_structure_type
;
443 struct bt_ctf_field_type
*structure_seq_type
;
444 struct bt_ctf_field_type
*string_type
;
445 struct bt_ctf_field_type
*sequence_type
;
446 struct bt_ctf_field_type
*uint_8_type
;
447 struct bt_ctf_field_type
*int_16_type
;
448 struct bt_ctf_field_type
*uint_12_type
=
449 bt_ctf_field_type_integer_create(12);
451 ok(uint_12_type
, "Create an unsigned integer type");
452 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
453 BT_CTF_INTEGER_BASE_BINARY
) == 0,
454 "Set integer type's base as binary");
455 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
456 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
457 "Set integer type's base as decimal");
458 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
459 BT_CTF_INTEGER_BASE_UNKNOWN
),
460 "Reject integer type's base set as unknown");
461 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
462 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
463 "Set integer type's base as octal");
464 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
465 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
466 "Set integer type's base as hexadecimal");
467 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
468 "Reject unknown integer base value");
469 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
470 "Set integer type signedness to signed");
471 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
472 "Set integer type signedness to unsigned");
474 int_16_type
= bt_ctf_field_type_integer_create(16);
475 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
476 uint_8_type
= bt_ctf_field_type_integer_create(8);
478 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
479 ok(sequence_type
, "Create a sequence of int16_t type");
481 string_type
= bt_ctf_field_type_string_create();
482 ok(string_type
, "Create a string type");
483 ok(bt_ctf_field_type_string_set_encoding(string_type
,
485 "Reject invalid \"None\" string encoding");
486 ok(bt_ctf_field_type_string_set_encoding(string_type
,
488 "Reject invalid string encoding");
489 ok(bt_ctf_field_type_string_set_encoding(string_type
,
490 CTF_STRING_ASCII
) == 0,
491 "Set string encoding to ASCII");
493 structure_seq_type
= bt_ctf_field_type_structure_create();
494 ok(structure_seq_type
, "Create a structure type");
495 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
496 uint_8_type
, "seq_len") == 0,
497 "Add a uint8_t type to a structure");
498 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
499 sequence_type
, "a_sequence") == 0,
500 "Add a sequence type to a structure");
501 composite_structure_type
= bt_ctf_field_type_structure_create();
502 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
503 string_type
, "a_string") == 0,
504 "Add a string type to a structure");
505 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
506 structure_seq_type
, "inner_structure") == 0,
507 "Add a structure type to a structure");
509 int_16
= bt_ctf_field_create(int_16_type
);
510 ok(int_16
, "Instanciate a signed 16-bit integer");
511 uint_12
= bt_ctf_field_create(uint_12_type
);
512 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
514 /* Can't modify types after instanciating them */
515 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
516 BT_CTF_INTEGER_BASE_DECIMAL
),
517 "Check an integer type' base can't be modified after instanciation");
518 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
519 "Check an integer type's signedness can't be modified after instanciation");
521 /* Check signed property is checked */
522 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
523 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
524 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
525 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
527 /* Check overflows are properly tested for */
528 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
529 "Check -32768 is allowed for a signed 16-bit integer");
530 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
531 "Check 32767 is allowed for a signed 16-bit integer");
532 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
533 "Check 32768 is not allowed for a signed 16-bit integer");
534 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
535 "Check -32769 is not allowed for a signed 16-bit integer");
536 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
537 "Check -42 is allowed for a signed 16-bit integer");
539 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
540 "Check 4095 is allowed for an unsigned 12-bit integer");
541 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
542 "Check 4096 is not allowed for a unsigned 12-bit integer");
543 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
544 "Check 0 is allowed for an unsigned 12-bit integer");
546 string
= bt_ctf_field_create(string_type
);
547 ok(string
, "Instanciate a string field");
548 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
549 "Set a string's value");
551 bt_ctf_field_put(string
);
552 bt_ctf_field_put(uint_12
);
553 bt_ctf_field_put(int_16
);
554 bt_ctf_field_type_put(composite_structure_type
);
555 bt_ctf_field_type_put(structure_seq_type
);
556 bt_ctf_field_type_put(string_type
);
557 bt_ctf_field_type_put(sequence_type
);
558 bt_ctf_field_type_put(uint_8_type
);
559 bt_ctf_field_type_put(int_16_type
);
560 bt_ctf_field_type_put(uint_12_type
);
563 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
564 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
567 * Append enough events to force the underlying packet to be resized.
568 * Also tests that a new event can be declared after a stream has been
569 * instantiated and used/flushed.
573 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
575 struct bt_ctf_field_type
*integer_type
=
576 bt_ctf_field_type_integer_create(17);
577 struct bt_ctf_field_type
*string_type
=
578 bt_ctf_field_type_string_create();
580 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
582 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
584 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
585 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
590 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
591 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
592 struct bt_ctf_field
*integer
=
593 bt_ctf_field_create(integer_type
);
594 struct bt_ctf_field
*string
=
595 bt_ctf_field_create(string_type
);
597 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
598 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
599 ret
|= bt_ctf_event_set_payload(event
, "field_1",
601 bt_ctf_field_put(integer
);
602 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
603 ret
|= bt_ctf_event_set_payload(event
, "a_string",
605 bt_ctf_field_put(string
);
606 ret
|= bt_ctf_stream_append_event(stream
, event
);
607 bt_ctf_event_put(event
);
614 ok(ret
== 0, "Append 100 000 events to a stream");
615 ok(bt_ctf_stream_flush(stream
) == 0,
616 "Flush a stream that forces a packet resize");
617 bt_ctf_field_type_put(integer_type
);
618 bt_ctf_field_type_put(string_type
);
619 bt_ctf_event_class_put(event_class
);
622 int main(int argc
, char **argv
)
624 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
625 char metadata_path
[sizeof(trace_path
) + 9];
626 const char *clock_name
= "test_clock";
627 const char *clock_description
= "This is a test clock";
628 const uint64_t frequency
= 1000000000;
629 const uint64_t offset_s
= 1351530929945824323;
630 const uint64_t offset
= 1234567;
631 const uint64_t precision
= 10;
632 char *metadata_string
;
633 struct bt_ctf_writer
*writer
;
635 char hostname
[HOST_NAME_MAX
];
636 struct bt_ctf_clock
*clock
;
637 struct bt_ctf_stream_class
*stream_class
;
638 struct bt_ctf_stream
*stream1
;
641 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
647 if (!mkdtemp(trace_path
)) {
651 strcpy(metadata_path
, trace_path
);
652 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
654 writer
= bt_ctf_writer_create(trace_path
);
655 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
657 /* Add environment context to the trace */
658 gethostname(hostname
, HOST_NAME_MAX
);
659 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
660 "Add host (%s) environment field to writer instance",
662 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
664 "bt_ctf_writer_add_environment_field error with NULL writer");
665 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
667 "bt_ctf_writer_add_environment_field error with NULL field name");
668 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
670 "bt_ctf_writer_add_environment_field error with NULL field value");
677 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
678 == 0, "Add sysname (%s) environment field to writer instance",
680 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
682 "Add nodename (%s) environment field to writer instance",
684 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
685 == 0, "Add release (%s) environment field to writer instance",
687 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
688 == 0, "Add version (%s) environment field to writer instance",
690 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
691 == 0, "Add machine (%s) environment field to writer istance",
694 /* Define a clock and add it to the trace */
695 ok(bt_ctf_clock_create("signed") == NULL
, "Illegal clock name rejected");
696 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
697 clock
= bt_ctf_clock_create(clock_name
);
698 ok(clock
, "Clock created sucessfully");
699 bt_ctf_clock_set_description(clock
, clock_description
);
701 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
702 "Set clock frequency");
703 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
704 "Set clock offset (seconds)");
705 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
706 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
707 "Set clock precision");
708 ok(bt_ctf_clock_set_is_absolute(clock
, 0xFF) == 0,
709 "Set clock absolute property");
711 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
712 "Add clock to writer instance");
713 ok(bt_ctf_writer_add_clock(writer
, clock
),
714 "Verify a clock can't be added twice to a writer instance");
716 /* Define a stream class */
717 stream_class
= bt_ctf_stream_class_create("test_stream");
718 ok(stream_class
, "Create stream class");
719 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
720 "Set a stream class' clock");
722 /* Test the event fields and event types APIs */
725 /* Instantiate a stream and append events */
726 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
727 ok(stream1
, "Instanciate a stream class from writer");
729 /* Should fail after instanciating a stream (locked)*/
730 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
731 "Changes to a stream class that was already instantiated fail");
733 append_simple_event(stream_class
, stream1
, clock
);
735 packet_resize_test(stream_class
, stream1
, clock
);
737 append_complex_event(stream_class
, stream1
, clock
);
739 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
740 ok(metadata_string
, "Get metadata string");
742 bt_ctf_writer_flush_metadata(writer
);
743 validate_metadata(argv
[1], metadata_path
);
744 validate_trace(argv
[2], trace_path
);
746 bt_ctf_clock_put(clock
);
747 bt_ctf_stream_class_put(stream_class
);
748 bt_ctf_writer_put(writer
);
749 bt_ctf_stream_put(stream1
);
750 free(metadata_string
);
752 /* Remove all trace files and delete temporary trace directory */
753 DIR *trace_dir
= opendir(trace_path
);
759 struct dirent
*entry
;
760 while ((entry
= readdir(trace_dir
))) {
761 if (entry
->d_type
== DT_REG
) {
762 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);