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");
115 parser_output_fp
= fdopen(parser_output_fd
, "r");
116 if (!parser_output_fp
) {
117 perror("fdopen on parser_output_fd");
120 parser_output_fd
= -1;
124 diag("malloc failure");
129 /* Output the metadata and parser output as diagnostic */
130 while (getline(&line
, &len
, metadata_fp
) > 0) {
134 rewind(parser_output_fp
);
135 while (getline(&line
, &len
, parser_output_fp
) > 0) {
142 if (fclose(metadata_fp
)) {
143 diag("fclose failure");
146 if (parser_output_fp
) {
147 if (fclose(parser_output_fp
)) {
148 diag("fclose failure");
153 if (parser_output_fd
> 0) {
154 if (close(parser_output_fd
)) {
158 if (metadata_fd
> 0) {
159 if (close(metadata_fd
)) {
165 void validate_trace(char *parser_path
, char *trace_path
)
168 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
169 int babeltrace_output_fd
= -1;
176 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
177 unlink(babeltrace_output_path
);
179 if (babeltrace_output_fd
== -1) {
180 diag("Failed to create a temporary file for trace parsing.");
188 waitpid(pid
, &status
, 0);
189 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
191 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
193 perror("# dup2 babeltrace_output_fd to STDOUT");
197 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
199 perror("# dup2 babeltrace_output_fd to STDERR");
203 execl(parser_path
, "babeltrace", trace_path
, NULL
);
204 perror("# Could not launch the babeltrace process");
208 ok(ret
== 0, "Babeltrace could read the resulting trace");
210 if (ret
&& babeltrace_output_fd
> 0) {
212 size_t len
= METADATA_LINE_SIZE
;
213 FILE *babeltrace_output_fp
= NULL
;
215 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
216 if (!babeltrace_output_fp
) {
217 perror("fdopen on babeltrace_output_fd");
220 babeltrace_output_fd
= -1;
224 diag("malloc error");
226 rewind(babeltrace_output_fp
);
227 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
233 if (babeltrace_output_fp
) {
234 if (fclose(babeltrace_output_fp
)) {
235 diag("fclose error");
240 if (babeltrace_output_fd
> 0) {
241 if (close(babeltrace_output_fd
)) {
247 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
248 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
250 /* Create and add a simple event class */
251 struct bt_ctf_event_class
*simple_event_class
=
252 bt_ctf_event_class_create("Simple Event");
253 struct bt_ctf_field_type
*uint_12_type
=
254 bt_ctf_field_type_integer_create(12);
255 struct bt_ctf_field_type
*float_type
=
256 bt_ctf_field_type_floating_point_create();
257 struct bt_ctf_event
*simple_event
;
258 struct bt_ctf_field
*integer_field
;
259 struct bt_ctf_field
*float_field
;
261 bt_ctf_field_type_set_alignment(float_type
, 32);
262 bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11);
263 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53);
264 ok(uint_12_type
, "Create an unsigned integer type");
265 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
267 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
269 bt_ctf_stream_class_add_event_class(stream_class
,
272 simple_event
= bt_ctf_event_create(simple_event_class
);
275 "Instantiate an event containing a single integer field");
277 integer_field
= bt_ctf_field_create(uint_12_type
);
278 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
279 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
280 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
282 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
283 bt_ctf_field_floating_point_set_value(float_field
, 3.1415);
285 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
287 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
288 "Append simple event to trace stream");
290 ok(bt_ctf_stream_flush(stream
) == 0,
291 "Flush trace stream with one event");
293 bt_ctf_event_class_put(simple_event_class
);
294 bt_ctf_event_put(simple_event
);
295 bt_ctf_field_type_put(uint_12_type
);
296 bt_ctf_field_type_put(float_type
);
297 bt_ctf_field_put(integer_field
);
298 bt_ctf_field_put(float_field
);
301 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
302 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
305 struct bt_ctf_field_type
*uint_35_type
=
306 bt_ctf_field_type_integer_create(35);
307 struct bt_ctf_field_type
*int_16_type
=
308 bt_ctf_field_type_integer_create(16);
309 struct bt_ctf_field_type
*uint_3_type
=
310 bt_ctf_field_type_integer_create(3);
311 struct bt_ctf_field_type
*enum_variant_type
=
312 bt_ctf_field_type_enumeration_create(uint_3_type
);
313 struct bt_ctf_field_type
*variant_type
=
314 bt_ctf_field_type_variant_create(enum_variant_type
,
316 struct bt_ctf_field_type
*string_type
=
317 bt_ctf_field_type_string_create();
318 struct bt_ctf_field_type
*sequence_type
;
319 struct bt_ctf_field_type
*inner_structure_type
=
320 bt_ctf_field_type_structure_create();
321 struct bt_ctf_field_type
*complex_structure_type
=
322 bt_ctf_field_type_structure_create();
323 struct bt_ctf_event_class
*event_class
;
324 struct bt_ctf_event
*event
;
325 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
326 *inner_structure_field
, *complex_structure_field
,
327 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
330 bt_ctf_field_type_set_alignment(int_16_type
, 32);
331 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
332 bt_ctf_field_type_integer_set_base(uint_35_type
,
333 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
335 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
337 bt_ctf_field_type_structure_add_field(inner_structure_type
,
338 uint_35_type
, "seq_len");
339 bt_ctf_field_type_structure_add_field(inner_structure_type
,
340 sequence_type
, "a_sequence");
342 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
344 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
346 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
347 "UINT35_TYPE", 2, 7);
348 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
349 "An unknown entry"), "Reject a variant field based on an unknown tag value");
350 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
351 "UINT3_TYPE") == 0, "Add a field to a variant");
352 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
354 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
357 bt_ctf_field_type_structure_add_field(complex_structure_type
,
358 enum_variant_type
, "variant_selector");
359 bt_ctf_field_type_structure_add_field(complex_structure_type
,
360 string_type
, "a_string");
361 bt_ctf_field_type_structure_add_field(complex_structure_type
,
362 variant_type
, "variant_value");
363 bt_ctf_field_type_structure_add_field(complex_structure_type
,
364 inner_structure_type
, "inner_structure");
366 ok(bt_ctf_event_class_create("clock") == NULL
,
367 "Reject creation of an event class with an illegal name");
368 event_class
= bt_ctf_event_class_create("Complex Test Event");
369 ok(event_class
, "Create an event class");
370 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
371 "Reject addition of a field with an empty name to an event");
372 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
373 "Reject addition of a field with a NULL type to an event");
374 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
376 "Reject addition of a type with an illegal name to an event");
377 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
379 "Add field of type unsigned integer to an event");
380 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
381 "int_16") == 0, "Add field of type signed integer to an event");
382 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
383 "complex_structure") == 0,
384 "Add composite structure to an event");
386 /* Add event class to the stream class */
387 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
388 "Reject addition of NULL event class to a stream class");
389 ok(bt_ctf_stream_class_add_event_class(stream_class
,
390 event_class
) == 0, "Add an event class to stream class");
392 event
= bt_ctf_event_create(event_class
);
393 ok(event
, "Instanciate a complex event");
395 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
396 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
397 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
398 bt_ctf_field_put(uint_35_field
);
400 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
401 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
402 bt_ctf_field_put(int_16_field
);
404 complex_structure_field
= bt_ctf_event_get_payload(event
,
405 "complex_structure");
406 inner_structure_field
= bt_ctf_field_structure_get_field(
407 complex_structure_field
, "inner_structure");
408 a_string_field
= bt_ctf_field_structure_get_field(
409 complex_structure_field
, "a_string");
410 enum_variant_field
= bt_ctf_field_structure_get_field(
411 complex_structure_field
, "variant_selector");
412 variant_field
= bt_ctf_field_structure_get_field(
413 complex_structure_field
, "variant_value");
414 uint_35_field
= bt_ctf_field_structure_get_field(
415 inner_structure_field
, "seq_len");
416 a_sequence_field
= bt_ctf_field_structure_get_field(
417 inner_structure_field
, "a_sequence");
419 enum_container_field
= bt_ctf_field_enumeration_get_container(
421 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
422 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
424 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
425 bt_ctf_field_put(int_16_field
);
426 bt_ctf_field_string_set_value(a_string_field
,
428 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
429 SEQUENCE_TEST_LENGTH
);
430 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
431 uint_35_field
) == 0, "Set a sequence field's length");
433 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
434 int_16_field
= bt_ctf_field_sequence_get_field(
435 a_sequence_field
, i
);
436 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
437 bt_ctf_field_put(int_16_field
);
440 bt_ctf_clock_set_time(clock
, ++current_time
);
441 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
442 "Append a complex event to a stream");
443 ok(bt_ctf_stream_flush(stream
) == 0,
444 "Flush a stream containing a complex event");
446 bt_ctf_field_put(uint_35_field
);
447 bt_ctf_field_put(a_string_field
);
448 bt_ctf_field_put(inner_structure_field
);
449 bt_ctf_field_put(complex_structure_field
);
450 bt_ctf_field_put(a_sequence_field
);
451 bt_ctf_field_put(enum_variant_field
);
452 bt_ctf_field_put(enum_container_field
);
453 bt_ctf_field_put(variant_field
);
454 bt_ctf_field_type_put(uint_35_type
);
455 bt_ctf_field_type_put(int_16_type
);
456 bt_ctf_field_type_put(string_type
);
457 bt_ctf_field_type_put(sequence_type
);
458 bt_ctf_field_type_put(inner_structure_type
);
459 bt_ctf_field_type_put(complex_structure_type
);
460 bt_ctf_field_type_put(uint_3_type
);
461 bt_ctf_field_type_put(enum_variant_type
);
462 bt_ctf_field_type_put(variant_type
);
463 bt_ctf_event_class_put(event_class
);
464 bt_ctf_event_put(event
);
467 void type_field_tests()
469 struct bt_ctf_field
*uint_12
;
470 struct bt_ctf_field
*int_16
;
471 struct bt_ctf_field
*string
;
472 struct bt_ctf_field_type
*composite_structure_type
;
473 struct bt_ctf_field_type
*structure_seq_type
;
474 struct bt_ctf_field_type
*string_type
;
475 struct bt_ctf_field_type
*sequence_type
;
476 struct bt_ctf_field_type
*uint_8_type
;
477 struct bt_ctf_field_type
*int_16_type
;
478 struct bt_ctf_field_type
*uint_12_type
=
479 bt_ctf_field_type_integer_create(12);
481 ok(uint_12_type
, "Create an unsigned integer type");
482 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
483 BT_CTF_INTEGER_BASE_BINARY
) == 0,
484 "Set integer type's base as binary");
485 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
486 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
487 "Set integer type's base as decimal");
488 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
489 BT_CTF_INTEGER_BASE_UNKNOWN
),
490 "Reject integer type's base set as unknown");
491 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
492 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
493 "Set integer type's base as octal");
494 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
495 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
496 "Set integer type's base as hexadecimal");
497 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
498 "Reject unknown integer base value");
499 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
500 "Set integer type signedness to signed");
501 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
502 "Set integer type signedness to unsigned");
504 int_16_type
= bt_ctf_field_type_integer_create(16);
505 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
506 uint_8_type
= bt_ctf_field_type_integer_create(8);
508 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
509 ok(sequence_type
, "Create a sequence of int16_t type");
511 string_type
= bt_ctf_field_type_string_create();
512 ok(string_type
, "Create a string type");
513 ok(bt_ctf_field_type_string_set_encoding(string_type
,
515 "Reject invalid \"None\" string encoding");
516 ok(bt_ctf_field_type_string_set_encoding(string_type
,
518 "Reject invalid string encoding");
519 ok(bt_ctf_field_type_string_set_encoding(string_type
,
520 CTF_STRING_ASCII
) == 0,
521 "Set string encoding to ASCII");
523 structure_seq_type
= bt_ctf_field_type_structure_create();
524 ok(structure_seq_type
, "Create a structure type");
525 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
526 uint_8_type
, "seq_len") == 0,
527 "Add a uint8_t type to a structure");
528 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
529 sequence_type
, "a_sequence") == 0,
530 "Add a sequence type to a structure");
531 composite_structure_type
= bt_ctf_field_type_structure_create();
532 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
533 string_type
, "a_string") == 0,
534 "Add a string type to a structure");
535 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
536 structure_seq_type
, "inner_structure") == 0,
537 "Add a structure type to a structure");
539 int_16
= bt_ctf_field_create(int_16_type
);
540 ok(int_16
, "Instanciate a signed 16-bit integer");
541 uint_12
= bt_ctf_field_create(uint_12_type
);
542 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
544 /* Can't modify types after instanciating them */
545 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
546 BT_CTF_INTEGER_BASE_DECIMAL
),
547 "Check an integer type' base can't be modified after instanciation");
548 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
549 "Check an integer type's signedness can't be modified after instanciation");
551 /* Check signed property is checked */
552 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
553 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
554 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
555 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
557 /* Check overflows are properly tested for */
558 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
559 "Check -32768 is allowed for a signed 16-bit integer");
560 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
561 "Check 32767 is allowed for a signed 16-bit integer");
562 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
563 "Check 32768 is not allowed for a signed 16-bit integer");
564 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
565 "Check -32769 is not allowed for a signed 16-bit integer");
566 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
567 "Check -42 is allowed for a signed 16-bit integer");
569 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
570 "Check 4095 is allowed for an unsigned 12-bit integer");
571 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
572 "Check 4096 is not allowed for a unsigned 12-bit integer");
573 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
574 "Check 0 is allowed for an unsigned 12-bit integer");
576 string
= bt_ctf_field_create(string_type
);
577 ok(string
, "Instanciate a string field");
578 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
579 "Set a string's value");
581 bt_ctf_field_put(string
);
582 bt_ctf_field_put(uint_12
);
583 bt_ctf_field_put(int_16
);
584 bt_ctf_field_type_put(composite_structure_type
);
585 bt_ctf_field_type_put(structure_seq_type
);
586 bt_ctf_field_type_put(string_type
);
587 bt_ctf_field_type_put(sequence_type
);
588 bt_ctf_field_type_put(uint_8_type
);
589 bt_ctf_field_type_put(int_16_type
);
590 bt_ctf_field_type_put(uint_12_type
);
593 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
594 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
597 * Append enough events to force the underlying packet to be resized.
598 * Also tests that a new event can be declared after a stream has been
599 * instantiated and used/flushed.
603 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
605 struct bt_ctf_field_type
*integer_type
=
606 bt_ctf_field_type_integer_create(17);
607 struct bt_ctf_field_type
*string_type
=
608 bt_ctf_field_type_string_create();
610 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
612 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
614 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
615 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
620 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
621 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
622 struct bt_ctf_field
*integer
=
623 bt_ctf_field_create(integer_type
);
624 struct bt_ctf_field
*string
=
625 bt_ctf_field_create(string_type
);
627 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
628 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
629 ret
|= bt_ctf_event_set_payload(event
, "field_1",
631 bt_ctf_field_put(integer
);
632 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
633 ret
|= bt_ctf_event_set_payload(event
, "a_string",
635 bt_ctf_field_put(string
);
636 ret
|= bt_ctf_stream_append_event(stream
, event
);
637 bt_ctf_event_put(event
);
644 ok(ret
== 0, "Append 100 000 events to a stream");
645 ok(bt_ctf_stream_flush(stream
) == 0,
646 "Flush a stream that forces a packet resize");
647 bt_ctf_field_type_put(integer_type
);
648 bt_ctf_field_type_put(string_type
);
649 bt_ctf_event_class_put(event_class
);
652 int main(int argc
, char **argv
)
654 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
655 char metadata_path
[sizeof(trace_path
) + 9];
656 const char *clock_name
= "test_clock";
657 const char *clock_description
= "This is a test clock";
658 const uint64_t frequency
= 1000000000;
659 const uint64_t offset_s
= 1351530929945824323;
660 const uint64_t offset
= 1234567;
661 const uint64_t precision
= 10;
662 char *metadata_string
;
663 struct bt_ctf_writer
*writer
;
665 char hostname
[HOST_NAME_MAX
];
666 struct bt_ctf_clock
*clock
;
667 struct bt_ctf_stream_class
*stream_class
;
668 struct bt_ctf_stream
*stream1
;
671 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
677 if (!mkdtemp(trace_path
)) {
681 strcpy(metadata_path
, trace_path
);
682 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
684 writer
= bt_ctf_writer_create(trace_path
);
685 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
687 /* Add environment context to the trace */
688 gethostname(hostname
, HOST_NAME_MAX
);
689 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
690 "Add host (%s) environment field to writer instance",
692 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
694 "bt_ctf_writer_add_environment_field error with NULL writer");
695 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
697 "bt_ctf_writer_add_environment_field error with NULL field name");
698 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
700 "bt_ctf_writer_add_environment_field error with NULL field value");
707 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
708 == 0, "Add sysname (%s) environment field to writer instance",
710 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
712 "Add nodename (%s) environment field to writer instance",
714 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
715 == 0, "Add release (%s) environment field to writer instance",
717 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
718 == 0, "Add version (%s) environment field to writer instance",
720 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
721 == 0, "Add machine (%s) environment field to writer istance",
724 /* Define a clock and add it to the trace */
725 ok(bt_ctf_clock_create("signed") == NULL
, "Illegal clock name rejected");
726 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
727 clock
= bt_ctf_clock_create(clock_name
);
728 ok(clock
, "Clock created sucessfully");
729 bt_ctf_clock_set_description(clock
, clock_description
);
731 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
732 "Set clock frequency");
733 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
734 "Set clock offset (seconds)");
735 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
736 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
737 "Set clock precision");
738 ok(bt_ctf_clock_set_is_absolute(clock
, 0xFF) == 0,
739 "Set clock absolute property");
741 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
742 "Add clock to writer instance");
743 ok(bt_ctf_writer_add_clock(writer
, clock
),
744 "Verify a clock can't be added twice to a writer instance");
746 /* Define a stream class */
747 stream_class
= bt_ctf_stream_class_create("test_stream");
748 ok(stream_class
, "Create stream class");
749 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
750 "Set a stream class' clock");
752 /* Test the event fields and event types APIs */
755 /* Instantiate a stream and append events */
756 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
757 ok(stream1
, "Instanciate a stream class from writer");
759 /* Should fail after instanciating a stream (locked)*/
760 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
761 "Changes to a stream class that was already instantiated fail");
763 append_simple_event(stream_class
, stream1
, clock
);
765 packet_resize_test(stream_class
, stream1
, clock
);
767 append_complex_event(stream_class
, stream1
, clock
);
769 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
770 ok(metadata_string
, "Get metadata string");
772 bt_ctf_writer_flush_metadata(writer
);
773 validate_metadata(argv
[1], metadata_path
);
774 validate_trace(argv
[2], trace_path
);
776 bt_ctf_clock_put(clock
);
777 bt_ctf_stream_class_put(stream_class
);
778 bt_ctf_writer_put(writer
);
779 bt_ctf_stream_put(stream1
);
780 free(metadata_string
);
782 /* Remove all trace files and delete temporary trace directory */
783 DIR *trace_dir
= opendir(trace_path
);
789 struct dirent
*entry
;
790 while ((entry
= readdir(trace_dir
))) {
791 if (entry
->d_type
== DT_REG
) {
792 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);