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.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf/events.h>
30 #include <babeltrace/compat/stdlib.h>
32 #include <sys/utsname.h>
33 #include <babeltrace/compat/limits.h>
34 #include <babeltrace/compat/stdio.h>
40 #include <babeltrace/compat/dirent.h>
44 #define METADATA_LINE_SIZE 512
45 #define SEQUENCE_TEST_LENGTH 10
46 #define PACKET_RESIZE_TEST_LENGTH 100000
48 static uint64_t current_time
;
50 void validate_metadata(char *parser_path
, char *metadata_path
)
53 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
54 int parser_output_fd
= -1, metadata_fd
= -1;
61 parser_output_fd
= mkstemp(parser_output_path
);
62 metadata_fd
= open(metadata_path
, O_RDONLY
);
64 unlink(parser_output_path
);
66 if (parser_output_fd
== -1 || metadata_fd
== -1) {
67 diag("Failed create temporary files for metadata parsing.");
75 waitpid(pid
, &status
, 0);
76 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
78 /* ctf-parser-test expects a metadata string on stdin. */
79 ret
= dup2(metadata_fd
, STDIN_FILENO
);
81 perror("# dup2 metadata_fd to STDIN");
85 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
87 perror("# dup2 parser_output_fd to STDOUT");
91 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
93 perror("# dup2 parser_output_fd to STDERR");
97 execl(parser_path
, "ctf-parser-test", NULL
);
98 perror("# Could not launch the ctf metadata parser process");
102 ok(ret
== 0, "Metadata string is valid");
104 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
106 size_t len
= METADATA_LINE_SIZE
;
107 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
109 metadata_fp
= fdopen(metadata_fd
, "r");
111 perror("fdopen on metadata_fd");
116 parser_output_fp
= fdopen(parser_output_fd
, "r");
117 if (!parser_output_fp
) {
118 perror("fdopen on parser_output_fd");
121 parser_output_fd
= -1;
125 diag("malloc failure");
130 /* Output the metadata and parser output as diagnostic */
131 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
135 rewind(parser_output_fp
);
136 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
143 if (fclose(metadata_fp
)) {
144 diag("fclose failure");
147 if (parser_output_fp
) {
148 if (fclose(parser_output_fp
)) {
149 diag("fclose failure");
154 if (parser_output_fd
>= 0) {
155 if (close(parser_output_fd
)) {
159 if (metadata_fd
>= 0) {
160 if (close(metadata_fd
)) {
166 void validate_trace(char *parser_path
, char *trace_path
)
169 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
170 int babeltrace_output_fd
= -1;
177 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
178 unlink(babeltrace_output_path
);
180 if (babeltrace_output_fd
== -1) {
181 diag("Failed to create a temporary file for trace parsing.");
189 waitpid(pid
, &status
, 0);
190 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
192 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
194 perror("# dup2 babeltrace_output_fd to STDOUT");
198 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
200 perror("# dup2 babeltrace_output_fd to STDERR");
204 execl(parser_path
, "babeltrace", trace_path
, NULL
);
205 perror("# Could not launch the babeltrace process");
209 ok(ret
== 0, "Babeltrace could read the resulting trace");
211 if (ret
&& babeltrace_output_fd
>= 0) {
213 size_t len
= METADATA_LINE_SIZE
;
214 FILE *babeltrace_output_fp
= NULL
;
216 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
217 if (!babeltrace_output_fp
) {
218 perror("fdopen on babeltrace_output_fd");
221 babeltrace_output_fd
= -1;
225 diag("malloc error");
227 rewind(babeltrace_output_fp
);
228 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
234 if (babeltrace_output_fp
) {
235 if (fclose(babeltrace_output_fp
)) {
236 diag("fclose error");
241 if (babeltrace_output_fd
>= 0) {
242 if (close(babeltrace_output_fd
)) {
248 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
249 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
251 /* Create and add a simple event class */
252 struct bt_ctf_event_class
*simple_event_class
=
253 bt_ctf_event_class_create("Simple Event");
254 struct bt_ctf_field_type
*uint_12_type
=
255 bt_ctf_field_type_integer_create(12);
256 struct bt_ctf_field_type
*float_type
=
257 bt_ctf_field_type_floating_point_create();
258 struct bt_ctf_field_type
*enum_type
=
259 bt_ctf_field_type_enumeration_create(uint_12_type
);
260 struct bt_ctf_event
*simple_event
;
261 struct bt_ctf_field
*integer_field
;
262 struct bt_ctf_field
*float_field
;
263 struct bt_ctf_field
*enum_field
;
264 struct bt_ctf_field
*enum_container_field
;
266 bt_ctf_field_type_set_alignment(float_type
, 32);
267 bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11);
268 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53);
270 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
271 "escaping; \"test\"", 0, 0) == 0,
272 "Accept enumeration mapping strings containing quotes");
273 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
274 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
275 "Accept enumeration mapping strings containing special characters");
276 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
277 "event clock int float", 5, 22) == 0,
278 "Accept enumeration mapping strings containing reserved keywords");
279 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
280 "enum_field") == 0, "Add enumeration field to event");
282 ok(uint_12_type
, "Create an unsigned integer type");
283 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
285 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
287 bt_ctf_stream_class_add_event_class(stream_class
,
290 simple_event
= bt_ctf_event_create(simple_event_class
);
293 "Instantiate an event containing a single integer field");
295 integer_field
= bt_ctf_field_create(uint_12_type
);
296 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
297 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
298 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
300 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
301 bt_ctf_field_floating_point_set_value(float_field
, 3.1415);
302 enum_field
= bt_ctf_field_create(enum_type
);
303 enum_container_field
= bt_ctf_field_enumeration_get_container(
305 ok(bt_ctf_field_unsigned_integer_set_value(
306 enum_container_field
, 1) == 0,
307 "Set enumeration container value");
308 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
310 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
312 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
313 "Append simple event to trace stream");
315 ok(bt_ctf_stream_flush(stream
) == 0,
316 "Flush trace stream with one event");
318 bt_ctf_event_class_put(simple_event_class
);
319 bt_ctf_event_put(simple_event
);
320 bt_ctf_field_type_put(uint_12_type
);
321 bt_ctf_field_type_put(float_type
);
322 bt_ctf_field_type_put(enum_type
);
323 bt_ctf_field_put(integer_field
);
324 bt_ctf_field_put(float_field
);
325 bt_ctf_field_put(enum_field
);
326 bt_ctf_field_put(enum_container_field
);
329 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
330 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
333 struct bt_ctf_field_type
*uint_35_type
=
334 bt_ctf_field_type_integer_create(35);
335 struct bt_ctf_field_type
*int_16_type
=
336 bt_ctf_field_type_integer_create(16);
337 struct bt_ctf_field_type
*uint_3_type
=
338 bt_ctf_field_type_integer_create(3);
339 struct bt_ctf_field_type
*enum_variant_type
=
340 bt_ctf_field_type_enumeration_create(uint_3_type
);
341 struct bt_ctf_field_type
*variant_type
=
342 bt_ctf_field_type_variant_create(enum_variant_type
,
344 struct bt_ctf_field_type
*string_type
=
345 bt_ctf_field_type_string_create();
346 struct bt_ctf_field_type
*sequence_type
;
347 struct bt_ctf_field_type
*inner_structure_type
=
348 bt_ctf_field_type_structure_create();
349 struct bt_ctf_field_type
*complex_structure_type
=
350 bt_ctf_field_type_structure_create();
351 struct bt_ctf_event_class
*event_class
;
352 struct bt_ctf_event
*event
;
353 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
354 *inner_structure_field
, *complex_structure_field
,
355 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
358 bt_ctf_field_type_set_alignment(int_16_type
, 32);
359 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
360 bt_ctf_field_type_integer_set_base(uint_35_type
,
361 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
363 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
365 bt_ctf_field_type_structure_add_field(inner_structure_type
,
366 uint_35_type
, "seq_len");
367 bt_ctf_field_type_structure_add_field(inner_structure_type
,
368 sequence_type
, "a_sequence");
370 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
372 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
374 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
375 "UINT35_TYPE", 2, 7);
376 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
377 "An unknown entry"), "Reject a variant field based on an unknown tag value");
378 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
379 "UINT3_TYPE") == 0, "Add a field to a variant");
380 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
382 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
385 bt_ctf_field_type_structure_add_field(complex_structure_type
,
386 enum_variant_type
, "variant_selector");
387 bt_ctf_field_type_structure_add_field(complex_structure_type
,
388 string_type
, "a_string");
389 bt_ctf_field_type_structure_add_field(complex_structure_type
,
390 variant_type
, "variant_value");
391 bt_ctf_field_type_structure_add_field(complex_structure_type
,
392 inner_structure_type
, "inner_structure");
394 ok(bt_ctf_event_class_create("clock") == NULL
,
395 "Reject creation of an event class with an illegal name");
396 event_class
= bt_ctf_event_class_create("Complex Test Event");
397 ok(event_class
, "Create an event class");
398 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
399 "Reject addition of a field with an empty name to an event");
400 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
401 "Reject addition of a field with a NULL type to an event");
402 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
404 "Reject addition of a type with an illegal name to an event");
405 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
407 "Add field of type unsigned integer to an event");
408 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
409 "int_16") == 0, "Add field of type signed integer to an event");
410 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
411 "complex_structure") == 0,
412 "Add composite structure to an event");
414 /* Add event class to the stream class */
415 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
416 "Reject addition of NULL event class to a stream class");
417 ok(bt_ctf_stream_class_add_event_class(stream_class
,
418 event_class
) == 0, "Add an event class to stream class");
420 event
= bt_ctf_event_create(event_class
);
421 ok(event
, "Instanciate a complex event");
423 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
424 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
425 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
426 bt_ctf_field_put(uint_35_field
);
428 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
429 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
430 bt_ctf_field_put(int_16_field
);
432 complex_structure_field
= bt_ctf_event_get_payload(event
,
433 "complex_structure");
434 inner_structure_field
= bt_ctf_field_structure_get_field(
435 complex_structure_field
, "inner_structure");
436 a_string_field
= bt_ctf_field_structure_get_field(
437 complex_structure_field
, "a_string");
438 enum_variant_field
= bt_ctf_field_structure_get_field(
439 complex_structure_field
, "variant_selector");
440 variant_field
= bt_ctf_field_structure_get_field(
441 complex_structure_field
, "variant_value");
442 uint_35_field
= bt_ctf_field_structure_get_field(
443 inner_structure_field
, "seq_len");
444 a_sequence_field
= bt_ctf_field_structure_get_field(
445 inner_structure_field
, "a_sequence");
447 enum_container_field
= bt_ctf_field_enumeration_get_container(
449 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
450 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
452 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
453 bt_ctf_field_put(int_16_field
);
454 bt_ctf_field_string_set_value(a_string_field
,
456 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
457 SEQUENCE_TEST_LENGTH
);
458 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
459 uint_35_field
) == 0, "Set a sequence field's length");
461 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
462 int_16_field
= bt_ctf_field_sequence_get_field(
463 a_sequence_field
, i
);
464 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
465 bt_ctf_field_put(int_16_field
);
468 bt_ctf_clock_set_time(clock
, ++current_time
);
469 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
470 "Append a complex event to a stream");
471 ok(bt_ctf_stream_flush(stream
) == 0,
472 "Flush a stream containing a complex event");
474 bt_ctf_field_put(uint_35_field
);
475 bt_ctf_field_put(a_string_field
);
476 bt_ctf_field_put(inner_structure_field
);
477 bt_ctf_field_put(complex_structure_field
);
478 bt_ctf_field_put(a_sequence_field
);
479 bt_ctf_field_put(enum_variant_field
);
480 bt_ctf_field_put(enum_container_field
);
481 bt_ctf_field_put(variant_field
);
482 bt_ctf_field_type_put(uint_35_type
);
483 bt_ctf_field_type_put(int_16_type
);
484 bt_ctf_field_type_put(string_type
);
485 bt_ctf_field_type_put(sequence_type
);
486 bt_ctf_field_type_put(inner_structure_type
);
487 bt_ctf_field_type_put(complex_structure_type
);
488 bt_ctf_field_type_put(uint_3_type
);
489 bt_ctf_field_type_put(enum_variant_type
);
490 bt_ctf_field_type_put(variant_type
);
491 bt_ctf_event_class_put(event_class
);
492 bt_ctf_event_put(event
);
495 void type_field_tests()
497 struct bt_ctf_field
*uint_12
;
498 struct bt_ctf_field
*int_16
;
499 struct bt_ctf_field
*string
;
500 struct bt_ctf_field
*enumeration
;
501 struct bt_ctf_field_type
*composite_structure_type
;
502 struct bt_ctf_field_type
*structure_seq_type
;
503 struct bt_ctf_field_type
*string_type
;
504 struct bt_ctf_field_type
*sequence_type
;
505 struct bt_ctf_field_type
*uint_8_type
;
506 struct bt_ctf_field_type
*int_16_type
;
507 struct bt_ctf_field_type
*uint_12_type
=
508 bt_ctf_field_type_integer_create(12);
509 struct bt_ctf_field_type
*enumeration_type
;
510 struct bt_ctf_field_type
*enumeration_sequence_type
;
511 struct bt_ctf_field_type
*enumeration_array_type
;
513 ok(uint_12_type
, "Create an unsigned integer type");
514 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
515 BT_CTF_INTEGER_BASE_BINARY
) == 0,
516 "Set integer type's base as binary");
517 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
518 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
519 "Set integer type's base as decimal");
520 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
521 BT_CTF_INTEGER_BASE_UNKNOWN
),
522 "Reject integer type's base set as unknown");
523 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
524 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
525 "Set integer type's base as octal");
526 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
527 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
528 "Set integer type's base as hexadecimal");
529 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
530 "Reject unknown integer base value");
531 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
532 "Set integer type signedness to signed");
533 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
534 "Set integer type signedness to unsigned");
536 int_16_type
= bt_ctf_field_type_integer_create(16);
537 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
538 uint_8_type
= bt_ctf_field_type_integer_create(8);
540 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
541 ok(sequence_type
, "Create a sequence of int16_t type");
543 string_type
= bt_ctf_field_type_string_create();
544 ok(string_type
, "Create a string type");
545 ok(bt_ctf_field_type_string_set_encoding(string_type
,
547 "Reject invalid \"None\" string encoding");
548 ok(bt_ctf_field_type_string_set_encoding(string_type
,
550 "Reject invalid string encoding");
551 ok(bt_ctf_field_type_string_set_encoding(string_type
,
552 CTF_STRING_ASCII
) == 0,
553 "Set string encoding to ASCII");
555 structure_seq_type
= bt_ctf_field_type_structure_create();
556 ok(structure_seq_type
, "Create a structure type");
557 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
558 uint_8_type
, "seq_len") == 0,
559 "Add a uint8_t type to a structure");
560 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
561 sequence_type
, "a_sequence") == 0,
562 "Add a sequence type to a structure");
563 composite_structure_type
= bt_ctf_field_type_structure_create();
564 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
565 string_type
, "a_string") == 0,
566 "Add a string type to a structure");
567 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
568 structure_seq_type
, "inner_structure") == 0,
569 "Add a structure type to a structure");
571 int_16
= bt_ctf_field_create(int_16_type
);
572 ok(int_16
, "Instanciate a signed 16-bit integer");
573 uint_12
= bt_ctf_field_create(uint_12_type
);
574 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
576 /* Can't modify types after instanciating them */
577 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
578 BT_CTF_INTEGER_BASE_DECIMAL
),
579 "Check an integer type' base can't be modified after instanciation");
580 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
581 "Check an integer type's signedness can't be modified after instanciation");
583 /* Check signed property is checked */
584 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
585 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
586 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
587 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
589 /* Check overflows are properly tested for */
590 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
591 "Check -32768 is allowed for a signed 16-bit integer");
592 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
593 "Check 32767 is allowed for a signed 16-bit integer");
594 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
595 "Check 32768 is not allowed for a signed 16-bit integer");
596 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
597 "Check -32769 is not allowed for a signed 16-bit integer");
598 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
599 "Check -42 is allowed for a signed 16-bit integer");
601 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
602 "Check 4095 is allowed for an unsigned 12-bit integer");
603 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
604 "Check 4096 is not allowed for a unsigned 12-bit integer");
605 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
606 "Check 0 is allowed for an unsigned 12-bit integer");
608 string
= bt_ctf_field_create(string_type
);
609 ok(string
, "Instanciate a string field");
610 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
611 "Set a string's value");
613 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
615 "Create an enumeration type with an unsigned 12-bit integer as container");
616 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
617 enumeration_type
, "count");
618 ok(!enumeration_sequence_type
,
619 "Check enumeration types are validated when creating a sequence");
620 enumeration_array_type
= bt_ctf_field_type_array_create(
621 enumeration_type
, 10);
622 ok(!enumeration_array_type
,
623 "Check enumeration types are validated when creating an array");
624 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
625 enumeration_type
, "enumeration"),
626 "Check enumeration types are validated when adding them as structure members");
627 enumeration
= bt_ctf_field_create(enumeration_type
);
629 "Check enumeration types are validated before instantiation");
631 bt_ctf_field_put(string
);
632 bt_ctf_field_put(uint_12
);
633 bt_ctf_field_put(int_16
);
634 bt_ctf_field_put(enumeration
);
635 bt_ctf_field_type_put(composite_structure_type
);
636 bt_ctf_field_type_put(structure_seq_type
);
637 bt_ctf_field_type_put(string_type
);
638 bt_ctf_field_type_put(sequence_type
);
639 bt_ctf_field_type_put(uint_8_type
);
640 bt_ctf_field_type_put(int_16_type
);
641 bt_ctf_field_type_put(uint_12_type
);
642 bt_ctf_field_type_put(enumeration_type
);
643 bt_ctf_field_type_put(enumeration_sequence_type
);
644 bt_ctf_field_type_put(enumeration_array_type
);
647 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
648 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
651 * Append enough events to force the underlying packet to be resized.
652 * Also tests that a new event can be declared after a stream has been
653 * instantiated and used/flushed.
657 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
659 struct bt_ctf_field_type
*integer_type
=
660 bt_ctf_field_type_integer_create(17);
661 struct bt_ctf_field_type
*string_type
=
662 bt_ctf_field_type_string_create();
664 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
666 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
668 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
669 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
674 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
675 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
676 struct bt_ctf_field
*integer
=
677 bt_ctf_field_create(integer_type
);
678 struct bt_ctf_field
*string
=
679 bt_ctf_field_create(string_type
);
681 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
682 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
683 ret
|= bt_ctf_event_set_payload(event
, "field_1",
685 bt_ctf_field_put(integer
);
686 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
687 ret
|= bt_ctf_event_set_payload(event
, "a_string",
689 bt_ctf_field_put(string
);
690 ret
|= bt_ctf_stream_append_event(stream
, event
);
691 bt_ctf_event_put(event
);
698 ok(ret
== 0, "Append 100 000 events to a stream");
699 ok(bt_ctf_stream_flush(stream
) == 0,
700 "Flush a stream that forces a packet resize");
701 bt_ctf_field_type_put(integer_type
);
702 bt_ctf_field_type_put(string_type
);
703 bt_ctf_event_class_put(event_class
);
706 int main(int argc
, char **argv
)
708 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
709 char metadata_path
[sizeof(trace_path
) + 9];
710 const char *clock_name
= "test_clock";
711 const char *clock_description
= "This is a test clock";
712 const uint64_t frequency
= 1000000000;
713 const uint64_t offset_s
= 1351530929945824323;
714 const uint64_t offset
= 1234567;
715 const uint64_t precision
= 10;
716 char *metadata_string
;
717 struct bt_ctf_writer
*writer
;
719 char hostname
[BABELTRACE_HOST_NAME_MAX
];
720 struct bt_ctf_clock
*clock
;
721 struct bt_ctf_stream_class
*stream_class
;
722 struct bt_ctf_stream
*stream1
;
726 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
732 if (!bt_mkdtemp(trace_path
)) {
736 strcpy(metadata_path
, trace_path
);
737 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
739 writer
= bt_ctf_writer_create(trace_path
);
740 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
742 /* Add environment context to the trace */
743 ret
= gethostname(hostname
, sizeof(hostname
));
747 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
748 "Add host (%s) environment field to writer instance",
750 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
752 "bt_ctf_writer_add_environment_field error with NULL writer");
753 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
755 "bt_ctf_writer_add_environment_field error with NULL field name");
756 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
758 "bt_ctf_writer_add_environment_field error with NULL field value");
760 /* On Solaris, uname() can return any positive value on success */
761 if (uname(&name
) < 0) {
766 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
767 == 0, "Add sysname (%s) environment field to writer instance",
769 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
771 "Add nodename (%s) environment field to writer instance",
773 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
774 == 0, "Add release (%s) environment field to writer instance",
776 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
777 == 0, "Add version (%s) environment field to writer instance",
779 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
780 == 0, "Add machine (%s) environment field to writer istance",
783 /* Define a clock and add it to the trace */
784 ok(bt_ctf_clock_create("signed") == NULL
, "Illegal clock name rejected");
785 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
786 clock
= bt_ctf_clock_create(clock_name
);
787 ok(clock
, "Clock created sucessfully");
788 bt_ctf_clock_set_description(clock
, clock_description
);
790 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
791 "Set clock frequency");
792 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
793 "Set clock offset (seconds)");
794 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
795 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
796 "Set clock precision");
797 ok(bt_ctf_clock_set_is_absolute(clock
, 0xFF) == 0,
798 "Set clock absolute property");
800 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
801 "Add clock to writer instance");
802 ok(bt_ctf_writer_add_clock(writer
, clock
),
803 "Verify a clock can't be added twice to a writer instance");
805 /* Define a stream class */
806 stream_class
= bt_ctf_stream_class_create("test_stream");
807 ok(stream_class
, "Create stream class");
808 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
809 "Set a stream class' clock");
811 /* Test the event fields and event types APIs */
814 /* Instantiate a stream and append events */
815 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
816 ok(stream1
, "Instanciate a stream class from writer");
818 /* Should fail after instanciating a stream (locked)*/
819 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
820 "Changes to a stream class that was already instantiated fail");
822 append_simple_event(stream_class
, stream1
, clock
);
824 packet_resize_test(stream_class
, stream1
, clock
);
826 append_complex_event(stream_class
, stream1
, clock
);
828 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
829 ok(metadata_string
, "Get metadata string");
831 bt_ctf_writer_flush_metadata(writer
);
832 validate_metadata(argv
[1], metadata_path
);
833 validate_trace(argv
[2], trace_path
);
835 bt_ctf_clock_put(clock
);
836 bt_ctf_stream_class_put(stream_class
);
837 bt_ctf_writer_put(writer
);
838 bt_ctf_stream_put(stream1
);
839 free(metadata_string
);
841 /* Remove all trace files and delete temporary trace directory */
842 DIR *trace_dir
= opendir(trace_path
);
848 struct dirent
*entry
;
849 while ((entry
= readdir(trace_dir
))) {
851 char filename
[PATH_MAX
];
853 if (snprintf(filename
, sizeof(filename
), "%s/%s",
854 trace_path
, entry
->d_name
) <= 0) {
858 if (stat(entry
->d_name
, &st
)) {
862 if (S_ISREG(st
.st_mode
)) {
863 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);