Commit | Line | Data |
---|---|---|
033fb0a7 JG |
1 | #!/usr/bin/env python3 |
2 | # ctf_writer.py | |
3 | # | |
4 | # Babeltrace CTF Writer example script. | |
5 | # | |
6 | # Copyright 2013 EfficiOS Inc. | |
7 | # | |
8 | # Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | # | |
10 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | # of this software and associated documentation files (the "Software"), to deal | |
12 | # in the Software without restriction, including without limitation the rights | |
13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | # copies of the Software, and to permit persons to whom the Software is | |
15 | # furnished to do so, subject to the following conditions: | |
16 | # | |
17 | # The above copyright notice and this permission notice shall be included in | |
18 | # all copies or substantial portions of the Software. | |
19 | ||
20 | import sys | |
21 | import tempfile | |
22 | from babeltrace import * | |
23 | ||
24 | trace_path = tempfile.mkdtemp() | |
25 | ||
26 | print("Writing trace at {}".format(trace_path)) | |
27 | writer = CTFWriter.Writer(trace_path) | |
28 | ||
29 | clock = CTFWriter.Clock("A_clock") | |
14001277 | 30 | clock.description = "Simple clock" |
033fb0a7 JG |
31 | |
32 | writer.add_clock(clock) | |
33 | writer.add_environment_field("Python_version", str(sys.version_info)) | |
34 | ||
35 | stream_class = CTFWriter.StreamClass("test_stream") | |
14001277 | 36 | stream_class.clock = clock |
033fb0a7 JG |
37 | |
38 | event_class = CTFWriter.EventClass("SimpleEvent") | |
39 | ||
40 | # Create a int32_t equivalent type | |
14001277 JG |
41 | int32_type = CTFWriter.IntegerFieldDeclaration(32) |
42 | int32_type.signed = True | |
033fb0a7 JG |
43 | |
44 | # Create a string type | |
14001277 | 45 | string_type = CTFWriter.StringFieldDeclaration() |
033fb0a7 JG |
46 | |
47 | # Create a structure type containing both an integer and a string | |
14001277 | 48 | structure_type = CTFWriter.StructureFieldDeclaration() |
033fb0a7 JG |
49 | structure_type.add_field(int32_type, "an_integer") |
50 | structure_type.add_field(string_type, "a_string_field") | |
51 | event_class.add_field(structure_type, "structure_field") | |
52 | ||
53 | # Create a floating point type | |
14001277 JG |
54 | floating_point_type = CTFWriter.FloatFieldDeclaration() |
55 | floating_point_type.exponent_digits = CTFWriter.FloatFieldDeclaration.FLT_EXP_DIG | |
56 | floating_point_type.mantissa_digits = CTFWriter.FloatFieldDeclaration.FLT_MANT_DIG | |
033fb0a7 JG |
57 | event_class.add_field(floating_point_type, "float_field") |
58 | ||
59 | # Create an enumeration type | |
14001277 JG |
60 | int10_type = CTFWriter.IntegerFieldDeclaration(10) |
61 | enumeration_type = CTFWriter.EnumerationFieldDeclaration(int10_type) | |
033fb0a7 JG |
62 | enumeration_type.add_mapping("FIRST_ENTRY", 0, 4) |
63 | enumeration_type.add_mapping("SECOND_ENTRY", 5, 5) | |
64 | enumeration_type.add_mapping("THIRD_ENTRY", 6, 10) | |
65 | event_class.add_field(enumeration_type, "enum_field") | |
66 | ||
67 | # Create an array type | |
14001277 | 68 | array_type = CTFWriter.ArrayFieldDeclaration(int10_type, 5) |
033fb0a7 JG |
69 | event_class.add_field(array_type, "array_field") |
70 | ||
71 | stream_class.add_event_class(event_class) | |
72 | stream = writer.create_stream(stream_class) | |
73 | ||
74 | for i in range(100): | |
75 | event = CTFWriter.Event(event_class) | |
76 | ||
14001277 JG |
77 | clock.time = i * 1000 |
78 | structure_field = event.payload("structure_field") | |
79 | integer_field = structure_field.field("an_integer") | |
80 | integer_field.value = i | |
033fb0a7 | 81 | |
14001277 JG |
82 | string_field = structure_field.field("a_string_field") |
83 | string_field.value = "Test string." | |
033fb0a7 | 84 | |
14001277 JG |
85 | float_field = event.payload("float_field") |
86 | float_field.value = float(i) + (float(i) / 100.0) | |
033fb0a7 | 87 | |
14001277 | 88 | array_field = event.payload("array_field") |
033fb0a7 | 89 | for j in range(5): |
14001277 JG |
90 | element = array_field.field(j) |
91 | element.value = i + j | |
033fb0a7 | 92 | |
14001277 JG |
93 | enumeration_field = event.payload("enum_field") |
94 | integer_field = enumeration_field.container | |
95 | integer_field.value = i % 10 | |
033fb0a7 JG |
96 | |
97 | stream.append_event(event) | |
98 | ||
99 | stream.flush() |