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_field_type
*enum_type
=
258 bt_ctf_field_type_enumeration_create(uint_12_type
);
259 struct bt_ctf_event
*simple_event
;
260 struct bt_ctf_field
*integer_field
;
261 struct bt_ctf_field
*float_field
;
262 struct bt_ctf_field
*enum_field
;
263 struct bt_ctf_field
*enum_container_field
;
265 bt_ctf_field_type_set_alignment(float_type
, 32);
266 bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11);
267 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53);
269 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
270 "escaping; \"test\"", 0, 0) == 0,
271 "Accept enumeration mapping strings containing quotes");
272 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
273 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
274 "Accept enumeration mapping strings containing special characters");
275 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
276 "event clock int float", 5, 22) == 0,
277 "Accept enumeration mapping strings containing reserved keywords");
278 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
279 "enum_field") == 0, "Add enumeration field to event");
281 ok(uint_12_type
, "Create an unsigned integer type");
282 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
284 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
286 bt_ctf_stream_class_add_event_class(stream_class
,
289 simple_event
= bt_ctf_event_create(simple_event_class
);
292 "Instantiate an event containing a single integer field");
294 integer_field
= bt_ctf_field_create(uint_12_type
);
295 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
296 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
297 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
299 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
300 bt_ctf_field_floating_point_set_value(float_field
, 3.1415);
301 enum_field
= bt_ctf_field_create(enum_type
);
302 enum_container_field
= bt_ctf_field_enumeration_get_container(
304 ok(bt_ctf_field_unsigned_integer_set_value(
305 enum_container_field
, 1) == 0,
306 "Set enumeration container value");
307 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
309 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
311 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
312 "Append simple event to trace stream");
314 ok(bt_ctf_stream_flush(stream
) == 0,
315 "Flush trace stream with one event");
317 bt_ctf_event_class_put(simple_event_class
);
318 bt_ctf_event_put(simple_event
);
319 bt_ctf_field_type_put(uint_12_type
);
320 bt_ctf_field_type_put(float_type
);
321 bt_ctf_field_type_put(enum_type
);
322 bt_ctf_field_put(integer_field
);
323 bt_ctf_field_put(float_field
);
324 bt_ctf_field_put(enum_field
);
325 bt_ctf_field_put(enum_container_field
);
328 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
329 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
332 struct bt_ctf_field_type
*uint_35_type
=
333 bt_ctf_field_type_integer_create(35);
334 struct bt_ctf_field_type
*int_16_type
=
335 bt_ctf_field_type_integer_create(16);
336 struct bt_ctf_field_type
*uint_3_type
=
337 bt_ctf_field_type_integer_create(3);
338 struct bt_ctf_field_type
*enum_variant_type
=
339 bt_ctf_field_type_enumeration_create(uint_3_type
);
340 struct bt_ctf_field_type
*variant_type
=
341 bt_ctf_field_type_variant_create(enum_variant_type
,
343 struct bt_ctf_field_type
*string_type
=
344 bt_ctf_field_type_string_create();
345 struct bt_ctf_field_type
*sequence_type
;
346 struct bt_ctf_field_type
*inner_structure_type
=
347 bt_ctf_field_type_structure_create();
348 struct bt_ctf_field_type
*complex_structure_type
=
349 bt_ctf_field_type_structure_create();
350 struct bt_ctf_event_class
*event_class
;
351 struct bt_ctf_event
*event
;
352 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
353 *inner_structure_field
, *complex_structure_field
,
354 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
357 bt_ctf_field_type_set_alignment(int_16_type
, 32);
358 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
359 bt_ctf_field_type_integer_set_base(uint_35_type
,
360 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
362 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
364 bt_ctf_field_type_structure_add_field(inner_structure_type
,
365 uint_35_type
, "seq_len");
366 bt_ctf_field_type_structure_add_field(inner_structure_type
,
367 sequence_type
, "a_sequence");
369 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
371 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
373 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
374 "UINT35_TYPE", 2, 7);
375 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
376 "An unknown entry"), "Reject a variant field based on an unknown tag value");
377 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
378 "UINT3_TYPE") == 0, "Add a field to a variant");
379 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
381 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
384 bt_ctf_field_type_structure_add_field(complex_structure_type
,
385 enum_variant_type
, "variant_selector");
386 bt_ctf_field_type_structure_add_field(complex_structure_type
,
387 string_type
, "a_string");
388 bt_ctf_field_type_structure_add_field(complex_structure_type
,
389 variant_type
, "variant_value");
390 bt_ctf_field_type_structure_add_field(complex_structure_type
,
391 inner_structure_type
, "inner_structure");
393 ok(bt_ctf_event_class_create("clock") == NULL
,
394 "Reject creation of an event class with an illegal name");
395 event_class
= bt_ctf_event_class_create("Complex Test Event");
396 ok(event_class
, "Create an event class");
397 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
398 "Reject addition of a field with an empty name to an event");
399 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
400 "Reject addition of a field with a NULL type to an event");
401 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
403 "Reject addition of a type with an illegal name to an event");
404 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
406 "Add field of type unsigned integer to an event");
407 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
408 "int_16") == 0, "Add field of type signed integer to an event");
409 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
410 "complex_structure") == 0,
411 "Add composite structure to an event");
413 /* Add event class to the stream class */
414 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
415 "Reject addition of NULL event class to a stream class");
416 ok(bt_ctf_stream_class_add_event_class(stream_class
,
417 event_class
) == 0, "Add an event class to stream class");
419 event
= bt_ctf_event_create(event_class
);
420 ok(event
, "Instanciate a complex event");
422 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
423 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
424 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
425 bt_ctf_field_put(uint_35_field
);
427 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
428 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
429 bt_ctf_field_put(int_16_field
);
431 complex_structure_field
= bt_ctf_event_get_payload(event
,
432 "complex_structure");
433 inner_structure_field
= bt_ctf_field_structure_get_field(
434 complex_structure_field
, "inner_structure");
435 a_string_field
= bt_ctf_field_structure_get_field(
436 complex_structure_field
, "a_string");
437 enum_variant_field
= bt_ctf_field_structure_get_field(
438 complex_structure_field
, "variant_selector");
439 variant_field
= bt_ctf_field_structure_get_field(
440 complex_structure_field
, "variant_value");
441 uint_35_field
= bt_ctf_field_structure_get_field(
442 inner_structure_field
, "seq_len");
443 a_sequence_field
= bt_ctf_field_structure_get_field(
444 inner_structure_field
, "a_sequence");
446 enum_container_field
= bt_ctf_field_enumeration_get_container(
448 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
449 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
451 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
452 bt_ctf_field_put(int_16_field
);
453 bt_ctf_field_string_set_value(a_string_field
,
455 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
456 SEQUENCE_TEST_LENGTH
);
457 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
458 uint_35_field
) == 0, "Set a sequence field's length");
460 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
461 int_16_field
= bt_ctf_field_sequence_get_field(
462 a_sequence_field
, i
);
463 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
464 bt_ctf_field_put(int_16_field
);
467 bt_ctf_clock_set_time(clock
, ++current_time
);
468 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
469 "Append a complex event to a stream");
470 ok(bt_ctf_stream_flush(stream
) == 0,
471 "Flush a stream containing a complex event");
473 bt_ctf_field_put(uint_35_field
);
474 bt_ctf_field_put(a_string_field
);
475 bt_ctf_field_put(inner_structure_field
);
476 bt_ctf_field_put(complex_structure_field
);
477 bt_ctf_field_put(a_sequence_field
);
478 bt_ctf_field_put(enum_variant_field
);
479 bt_ctf_field_put(enum_container_field
);
480 bt_ctf_field_put(variant_field
);
481 bt_ctf_field_type_put(uint_35_type
);
482 bt_ctf_field_type_put(int_16_type
);
483 bt_ctf_field_type_put(string_type
);
484 bt_ctf_field_type_put(sequence_type
);
485 bt_ctf_field_type_put(inner_structure_type
);
486 bt_ctf_field_type_put(complex_structure_type
);
487 bt_ctf_field_type_put(uint_3_type
);
488 bt_ctf_field_type_put(enum_variant_type
);
489 bt_ctf_field_type_put(variant_type
);
490 bt_ctf_event_class_put(event_class
);
491 bt_ctf_event_put(event
);
494 void type_field_tests()
496 struct bt_ctf_field
*uint_12
;
497 struct bt_ctf_field
*int_16
;
498 struct bt_ctf_field
*string
;
499 struct bt_ctf_field
*enumeration
;
500 struct bt_ctf_field_type
*composite_structure_type
;
501 struct bt_ctf_field_type
*structure_seq_type
;
502 struct bt_ctf_field_type
*string_type
;
503 struct bt_ctf_field_type
*sequence_type
;
504 struct bt_ctf_field_type
*uint_8_type
;
505 struct bt_ctf_field_type
*int_16_type
;
506 struct bt_ctf_field_type
*uint_12_type
=
507 bt_ctf_field_type_integer_create(12);
508 struct bt_ctf_field_type
*enumeration_type
;
509 struct bt_ctf_field_type
*enumeration_sequence_type
;
510 struct bt_ctf_field_type
*enumeration_array_type
;
512 ok(uint_12_type
, "Create an unsigned integer type");
513 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
514 BT_CTF_INTEGER_BASE_BINARY
) == 0,
515 "Set integer type's base as binary");
516 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
517 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
518 "Set integer type's base as decimal");
519 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
520 BT_CTF_INTEGER_BASE_UNKNOWN
),
521 "Reject integer type's base set as unknown");
522 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
523 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
524 "Set integer type's base as octal");
525 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
526 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
527 "Set integer type's base as hexadecimal");
528 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
529 "Reject unknown integer base value");
530 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
531 "Set integer type signedness to signed");
532 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
533 "Set integer type signedness to unsigned");
535 int_16_type
= bt_ctf_field_type_integer_create(16);
536 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
537 uint_8_type
= bt_ctf_field_type_integer_create(8);
539 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
540 ok(sequence_type
, "Create a sequence of int16_t type");
542 string_type
= bt_ctf_field_type_string_create();
543 ok(string_type
, "Create a string type");
544 ok(bt_ctf_field_type_string_set_encoding(string_type
,
546 "Reject invalid \"None\" string encoding");
547 ok(bt_ctf_field_type_string_set_encoding(string_type
,
549 "Reject invalid string encoding");
550 ok(bt_ctf_field_type_string_set_encoding(string_type
,
551 CTF_STRING_ASCII
) == 0,
552 "Set string encoding to ASCII");
554 structure_seq_type
= bt_ctf_field_type_structure_create();
555 ok(structure_seq_type
, "Create a structure type");
556 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
557 uint_8_type
, "seq_len") == 0,
558 "Add a uint8_t type to a structure");
559 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
560 sequence_type
, "a_sequence") == 0,
561 "Add a sequence type to a structure");
562 composite_structure_type
= bt_ctf_field_type_structure_create();
563 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
564 string_type
, "a_string") == 0,
565 "Add a string type to a structure");
566 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
567 structure_seq_type
, "inner_structure") == 0,
568 "Add a structure type to a structure");
570 int_16
= bt_ctf_field_create(int_16_type
);
571 ok(int_16
, "Instanciate a signed 16-bit integer");
572 uint_12
= bt_ctf_field_create(uint_12_type
);
573 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
575 /* Can't modify types after instanciating them */
576 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
577 BT_CTF_INTEGER_BASE_DECIMAL
),
578 "Check an integer type' base can't be modified after instanciation");
579 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
580 "Check an integer type's signedness can't be modified after instanciation");
582 /* Check signed property is checked */
583 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
584 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
585 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
586 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
588 /* Check overflows are properly tested for */
589 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
590 "Check -32768 is allowed for a signed 16-bit integer");
591 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
592 "Check 32767 is allowed for a signed 16-bit integer");
593 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
594 "Check 32768 is not allowed for a signed 16-bit integer");
595 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
596 "Check -32769 is not allowed for a signed 16-bit integer");
597 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
598 "Check -42 is allowed for a signed 16-bit integer");
600 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
601 "Check 4095 is allowed for an unsigned 12-bit integer");
602 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
603 "Check 4096 is not allowed for a unsigned 12-bit integer");
604 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
605 "Check 0 is allowed for an unsigned 12-bit integer");
607 string
= bt_ctf_field_create(string_type
);
608 ok(string
, "Instanciate a string field");
609 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
610 "Set a string's value");
612 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
614 "Create an enumeration type with an unsigned 12-bit integer as container");
615 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
616 enumeration_type
, "count");
617 ok(!enumeration_sequence_type
,
618 "Check enumeration types are validated when creating a sequence");
619 enumeration_array_type
= bt_ctf_field_type_array_create(
620 enumeration_type
, 10);
621 ok(!enumeration_array_type
,
622 "Check enumeration types are validated when creating an array");
623 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
624 enumeration_type
, "enumeration") == 0,
625 "Check enumeration types are validated when adding them as structure members");
626 enumeration
= bt_ctf_field_create(enumeration_type
);
628 "Check enumeration types are validated before instantiation");
630 bt_ctf_field_put(string
);
631 bt_ctf_field_put(uint_12
);
632 bt_ctf_field_put(int_16
);
633 bt_ctf_field_put(enumeration
);
634 bt_ctf_field_type_put(composite_structure_type
);
635 bt_ctf_field_type_put(structure_seq_type
);
636 bt_ctf_field_type_put(string_type
);
637 bt_ctf_field_type_put(sequence_type
);
638 bt_ctf_field_type_put(uint_8_type
);
639 bt_ctf_field_type_put(int_16_type
);
640 bt_ctf_field_type_put(uint_12_type
);
641 bt_ctf_field_type_put(enumeration_type
);
642 bt_ctf_field_type_put(enumeration_sequence_type
);
643 bt_ctf_field_type_put(enumeration_array_type
);
646 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
647 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
650 * Append enough events to force the underlying packet to be resized.
651 * Also tests that a new event can be declared after a stream has been
652 * instantiated and used/flushed.
656 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
658 struct bt_ctf_field_type
*integer_type
=
659 bt_ctf_field_type_integer_create(17);
660 struct bt_ctf_field_type
*string_type
=
661 bt_ctf_field_type_string_create();
663 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
665 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
667 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
668 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
673 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
674 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
675 struct bt_ctf_field
*integer
=
676 bt_ctf_field_create(integer_type
);
677 struct bt_ctf_field
*string
=
678 bt_ctf_field_create(string_type
);
680 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
681 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
682 ret
|= bt_ctf_event_set_payload(event
, "field_1",
684 bt_ctf_field_put(integer
);
685 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
686 ret
|= bt_ctf_event_set_payload(event
, "a_string",
688 bt_ctf_field_put(string
);
689 ret
|= bt_ctf_stream_append_event(stream
, event
);
690 bt_ctf_event_put(event
);
697 ok(ret
== 0, "Append 100 000 events to a stream");
698 ok(bt_ctf_stream_flush(stream
) == 0,
699 "Flush a stream that forces a packet resize");
700 bt_ctf_field_type_put(integer_type
);
701 bt_ctf_field_type_put(string_type
);
702 bt_ctf_event_class_put(event_class
);
705 int main(int argc
, char **argv
)
707 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
708 char metadata_path
[sizeof(trace_path
) + 9];
709 const char *clock_name
= "test_clock";
710 const char *clock_description
= "This is a test clock";
711 const uint64_t frequency
= 1000000000;
712 const uint64_t offset_s
= 1351530929945824323;
713 const uint64_t offset
= 1234567;
714 const uint64_t precision
= 10;
715 char *metadata_string
;
716 struct bt_ctf_writer
*writer
;
718 char hostname
[HOST_NAME_MAX
];
719 struct bt_ctf_clock
*clock
;
720 struct bt_ctf_stream_class
*stream_class
;
721 struct bt_ctf_stream
*stream1
;
724 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
730 if (!mkdtemp(trace_path
)) {
734 strcpy(metadata_path
, trace_path
);
735 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
737 writer
= bt_ctf_writer_create(trace_path
);
738 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
740 /* Add environment context to the trace */
741 gethostname(hostname
, HOST_NAME_MAX
);
742 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
743 "Add host (%s) environment field to writer instance",
745 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
747 "bt_ctf_writer_add_environment_field error with NULL writer");
748 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
750 "bt_ctf_writer_add_environment_field error with NULL field name");
751 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
753 "bt_ctf_writer_add_environment_field error with NULL field value");
760 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
761 == 0, "Add sysname (%s) environment field to writer instance",
763 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
765 "Add nodename (%s) environment field to writer instance",
767 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
768 == 0, "Add release (%s) environment field to writer instance",
770 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
771 == 0, "Add version (%s) environment field to writer instance",
773 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
774 == 0, "Add machine (%s) environment field to writer istance",
777 /* Define a clock and add it to the trace */
778 ok(bt_ctf_clock_create("signed") == NULL
, "Illegal clock name rejected");
779 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
780 clock
= bt_ctf_clock_create(clock_name
);
781 ok(clock
, "Clock created sucessfully");
782 bt_ctf_clock_set_description(clock
, clock_description
);
784 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
785 "Set clock frequency");
786 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
787 "Set clock offset (seconds)");
788 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
789 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
790 "Set clock precision");
791 ok(bt_ctf_clock_set_is_absolute(clock
, 0xFF) == 0,
792 "Set clock absolute property");
794 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
795 "Add clock to writer instance");
796 ok(bt_ctf_writer_add_clock(writer
, clock
),
797 "Verify a clock can't be added twice to a writer instance");
799 /* Define a stream class */
800 stream_class
= bt_ctf_stream_class_create("test_stream");
801 ok(stream_class
, "Create stream class");
802 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
803 "Set a stream class' clock");
805 /* Test the event fields and event types APIs */
808 /* Instantiate a stream and append events */
809 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
810 ok(stream1
, "Instanciate a stream class from writer");
812 /* Should fail after instanciating a stream (locked)*/
813 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
814 "Changes to a stream class that was already instantiated fail");
816 append_simple_event(stream_class
, stream1
, clock
);
818 packet_resize_test(stream_class
, stream1
, clock
);
820 append_complex_event(stream_class
, stream1
, clock
);
822 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
823 ok(metadata_string
, "Get metadata string");
825 bt_ctf_writer_flush_metadata(writer
);
826 validate_metadata(argv
[1], metadata_path
);
827 validate_trace(argv
[2], trace_path
);
829 bt_ctf_clock_put(clock
);
830 bt_ctf_stream_class_put(stream_class
);
831 bt_ctf_writer_put(writer
);
832 bt_ctf_stream_put(stream1
);
833 free(metadata_string
);
835 /* Remove all trace files and delete temporary trace directory */
836 DIR *trace_dir
= opendir(trace_path
);
842 struct dirent
*entry
;
843 while ((entry
= readdir(trace_dir
))) {
844 if (entry
->d_type
== DT_REG
) {
845 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);