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. | |
5aa9939f JD |
19 | # |
20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | # SOFTWARE. | |
033fb0a7 JG |
27 | |
28 | import sys | |
29 | import tempfile | |
30 | from babeltrace import * | |
31 | ||
32 | trace_path = tempfile.mkdtemp() | |
33 | ||
34 | print("Writing trace at {}".format(trace_path)) | |
35 | writer = CTFWriter.Writer(trace_path) | |
36 | ||
37 | clock = CTFWriter.Clock("A_clock") | |
b0bb560c | 38 | print("Clock name is \"{}\"".format(clock.name)) |
14001277 | 39 | clock.description = "Simple clock" |
b0bb560c JG |
40 | print("Clock description is \"{}\"".format(clock.description)) |
41 | print("Clock frequency is {}".format(clock.frequency)) | |
42 | print("Clock precision is {}".format(clock.precision)) | |
43 | print("Clock offset_seconds is {}".format(clock.offset_seconds)) | |
44 | print("Clock offset is {}".format(clock.offset)) | |
45 | print("Clock is absolute: {}".format(clock.absolute)) | |
46 | print("Clock time is {}".format(clock.time)) | |
da64442f | 47 | print("Clock UUID is {}".format(clock.uuid)) |
033fb0a7 JG |
48 | |
49 | writer.add_clock(clock) | |
50 | writer.add_environment_field("Python_version", str(sys.version_info)) | |
51 | ||
52 | stream_class = CTFWriter.StreamClass("test_stream") | |
14001277 | 53 | stream_class.clock = clock |
033fb0a7 JG |
54 | |
55 | event_class = CTFWriter.EventClass("SimpleEvent") | |
56 | ||
57 | # Create a int32_t equivalent type | |
14001277 JG |
58 | int32_type = CTFWriter.IntegerFieldDeclaration(32) |
59 | int32_type.signed = True | |
033fb0a7 | 60 | |
821ca76c JG |
61 | # Create a uint16_t equivalent type |
62 | uint16_type = CTFWriter.IntegerFieldDeclaration(16) | |
63 | uint16_type.signed = False | |
64 | ||
7df927c7 JG |
65 | # Add a custom uint16_t field in the stream's packet context |
66 | packet_context_type = stream_class.packet_context_type | |
67 | print("\nFields in default packet context:") | |
68 | for field in packet_context_type.fields: | |
a47ab64a | 69 | print(str(type(field[1])) + " " + field[0]) |
7df927c7 JG |
70 | packet_context_type.add_field(uint16_type, "a_custom_packet_context_field") |
71 | stream_class.packet_context_type = packet_context_type | |
72 | ||
033fb0a7 | 73 | # Create a string type |
14001277 | 74 | string_type = CTFWriter.StringFieldDeclaration() |
033fb0a7 JG |
75 | |
76 | # Create a structure type containing both an integer and a string | |
14001277 | 77 | structure_type = CTFWriter.StructureFieldDeclaration() |
033fb0a7 JG |
78 | structure_type.add_field(int32_type, "an_integer") |
79 | structure_type.add_field(string_type, "a_string_field") | |
80 | event_class.add_field(structure_type, "structure_field") | |
81 | ||
82 | # Create a floating point type | |
14001277 JG |
83 | floating_point_type = CTFWriter.FloatFieldDeclaration() |
84 | floating_point_type.exponent_digits = CTFWriter.FloatFieldDeclaration.FLT_EXP_DIG | |
85 | floating_point_type.mantissa_digits = CTFWriter.FloatFieldDeclaration.FLT_MANT_DIG | |
033fb0a7 JG |
86 | event_class.add_field(floating_point_type, "float_field") |
87 | ||
88 | # Create an enumeration type | |
14001277 JG |
89 | int10_type = CTFWriter.IntegerFieldDeclaration(10) |
90 | enumeration_type = CTFWriter.EnumerationFieldDeclaration(int10_type) | |
033fb0a7 JG |
91 | enumeration_type.add_mapping("FIRST_ENTRY", 0, 4) |
92 | enumeration_type.add_mapping("SECOND_ENTRY", 5, 5) | |
93 | enumeration_type.add_mapping("THIRD_ENTRY", 6, 10) | |
94 | event_class.add_field(enumeration_type, "enum_field") | |
95 | ||
96 | # Create an array type | |
14001277 | 97 | array_type = CTFWriter.ArrayFieldDeclaration(int10_type, 5) |
033fb0a7 JG |
98 | event_class.add_field(array_type, "array_field") |
99 | ||
821ca76c JG |
100 | # Create a sequence type |
101 | sequence_type = CTFWriter.SequenceFieldDeclaration(int32_type, "sequence_len") | |
102 | event_class.add_field(uint16_type, "sequence_len") | |
103 | event_class.add_field(sequence_type, "sequence_field") | |
104 | ||
033fb0a7 JG |
105 | stream_class.add_event_class(event_class) |
106 | stream = writer.create_stream(stream_class) | |
107 | ||
108 | for i in range(100): | |
a47ab64a | 109 | event = CTFWriter.Event(event_class) |
033fb0a7 | 110 | |
a47ab64a JG |
111 | clock.time = i * 1000 |
112 | structure_field = event.payload("structure_field") | |
113 | integer_field = structure_field.field("an_integer") | |
114 | integer_field.value = i | |
033fb0a7 | 115 | |
a47ab64a JG |
116 | string_field = structure_field.field("a_string_field") |
117 | string_field.value = "Test string." | |
033fb0a7 | 118 | |
a47ab64a JG |
119 | float_field = event.payload("float_field") |
120 | float_field.value = float(i) + (float(i) / 100.0) | |
033fb0a7 | 121 | |
a47ab64a JG |
122 | array_field = event.payload("array_field") |
123 | for j in range(5): | |
124 | element = array_field.field(j) | |
125 | element.value = i + j | |
033fb0a7 | 126 | |
a47ab64a JG |
127 | event.payload("sequence_len").value = i % 10 |
128 | sequence_field = event.payload("sequence_field") | |
129 | sequence_field.length = event.payload("sequence_len") | |
130 | for j in range(event.payload("sequence_len").value): | |
131 | sequence_field.field(j).value = i + j | |
821ca76c | 132 | |
a47ab64a JG |
133 | enumeration_field = event.payload("enum_field") |
134 | integer_field = enumeration_field.container | |
135 | enumeration_field.value = i % 10 | |
033fb0a7 | 136 | |
a47ab64a | 137 | stream.append_event(event) |
033fb0a7 | 138 | |
7df927c7 JG |
139 | # Populate custom packet context field before flushing |
140 | packet_context = stream.packet_context | |
141 | field = packet_context.field("a_custom_packet_context_field") | |
142 | field.value = 42 | |
143 | ||
033fb0a7 | 144 | stream.flush() |