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 | |
62c4fc67 PP |
30 | import babeltrace.writer as btw |
31 | ||
033fb0a7 JG |
32 | |
33 | trace_path = tempfile.mkdtemp() | |
34 | ||
35 | print("Writing trace at {}".format(trace_path)) | |
62c4fc67 | 36 | writer = btw.Writer(trace_path) |
033fb0a7 | 37 | |
62c4fc67 | 38 | clock = btw.Clock("A_clock") |
b0bb560c | 39 | print("Clock name is \"{}\"".format(clock.name)) |
14001277 | 40 | clock.description = "Simple clock" |
b0bb560c JG |
41 | print("Clock description is \"{}\"".format(clock.description)) |
42 | print("Clock frequency is {}".format(clock.frequency)) | |
43 | print("Clock precision is {}".format(clock.precision)) | |
44 | print("Clock offset_seconds is {}".format(clock.offset_seconds)) | |
45 | print("Clock offset is {}".format(clock.offset)) | |
46 | print("Clock is absolute: {}".format(clock.absolute)) | |
47 | print("Clock time is {}".format(clock.time)) | |
da64442f | 48 | print("Clock UUID is {}".format(clock.uuid)) |
033fb0a7 JG |
49 | |
50 | writer.add_clock(clock) | |
51 | writer.add_environment_field("Python_version", str(sys.version_info)) | |
52 | ||
62c4fc67 | 53 | stream_class = btw.StreamClass("test_stream") |
14001277 | 54 | stream_class.clock = clock |
033fb0a7 | 55 | |
62c4fc67 | 56 | event_class = btw.EventClass("SimpleEvent") |
033fb0a7 JG |
57 | |
58 | # Create a int32_t equivalent type | |
62c4fc67 | 59 | int32_type = btw.IntegerFieldDeclaration(32) |
14001277 | 60 | int32_type.signed = True |
033fb0a7 | 61 | |
821ca76c | 62 | # Create a uint16_t equivalent type |
62c4fc67 | 63 | uint16_type = btw.IntegerFieldDeclaration(16) |
821ca76c JG |
64 | uint16_type.signed = False |
65 | ||
7df927c7 JG |
66 | # Add a custom uint16_t field in the stream's packet context |
67 | packet_context_type = stream_class.packet_context_type | |
68 | print("\nFields in default packet context:") | |
69 | for field in packet_context_type.fields: | |
a47ab64a | 70 | print(str(type(field[1])) + " " + field[0]) |
7df927c7 JG |
71 | packet_context_type.add_field(uint16_type, "a_custom_packet_context_field") |
72 | stream_class.packet_context_type = packet_context_type | |
73 | ||
6b30e7f3 SM |
74 | # Set a stream event context |
75 | stream_event_context_type = btw.StructureFieldDeclaration() | |
76 | stream_event_context_type.add_field(int32_type, "field_in_stream_event_context") | |
77 | stream_class.event_context_type = stream_event_context_type | |
78 | ||
033fb0a7 | 79 | # Create a string type |
62c4fc67 | 80 | string_type = btw.StringFieldDeclaration() |
033fb0a7 JG |
81 | |
82 | # Create a structure type containing both an integer and a string | |
62c4fc67 | 83 | structure_type = btw.StructureFieldDeclaration() |
033fb0a7 JG |
84 | structure_type.add_field(int32_type, "an_integer") |
85 | structure_type.add_field(string_type, "a_string_field") | |
86 | event_class.add_field(structure_type, "structure_field") | |
87 | ||
88 | # Create a floating point type | |
62c4fc67 PP |
89 | floating_point_type = btw.FloatFieldDeclaration() |
90 | floating_point_type.exponent_digits = btw.FloatFieldDeclaration.FLT_EXP_DIG | |
91 | floating_point_type.mantissa_digits = btw.FloatFieldDeclaration.FLT_MANT_DIG | |
033fb0a7 JG |
92 | event_class.add_field(floating_point_type, "float_field") |
93 | ||
94 | # Create an enumeration type | |
62c4fc67 PP |
95 | int10_type = btw.IntegerFieldDeclaration(10) |
96 | enumeration_type = btw.EnumerationFieldDeclaration(int10_type) | |
033fb0a7 JG |
97 | enumeration_type.add_mapping("FIRST_ENTRY", 0, 4) |
98 | enumeration_type.add_mapping("SECOND_ENTRY", 5, 5) | |
99 | enumeration_type.add_mapping("THIRD_ENTRY", 6, 10) | |
100 | event_class.add_field(enumeration_type, "enum_field") | |
101 | ||
102 | # Create an array type | |
62c4fc67 | 103 | array_type = btw.ArrayFieldDeclaration(int10_type, 5) |
033fb0a7 JG |
104 | event_class.add_field(array_type, "array_field") |
105 | ||
821ca76c | 106 | # Create a sequence type |
62c4fc67 | 107 | sequence_type = btw.SequenceFieldDeclaration(int32_type, "sequence_len") |
821ca76c JG |
108 | event_class.add_field(uint16_type, "sequence_len") |
109 | event_class.add_field(sequence_type, "sequence_field") | |
110 | ||
033fb0a7 JG |
111 | stream_class.add_event_class(event_class) |
112 | stream = writer.create_stream(stream_class) | |
113 | ||
114 | for i in range(100): | |
62c4fc67 | 115 | event = btw.Event(event_class) |
033fb0a7 | 116 | |
a47ab64a JG |
117 | clock.time = i * 1000 |
118 | structure_field = event.payload("structure_field") | |
119 | integer_field = structure_field.field("an_integer") | |
120 | integer_field.value = i | |
033fb0a7 | 121 | |
a47ab64a JG |
122 | string_field = structure_field.field("a_string_field") |
123 | string_field.value = "Test string." | |
033fb0a7 | 124 | |
a47ab64a JG |
125 | float_field = event.payload("float_field") |
126 | float_field.value = float(i) + (float(i) / 100.0) | |
033fb0a7 | 127 | |
a47ab64a JG |
128 | array_field = event.payload("array_field") |
129 | for j in range(5): | |
130 | element = array_field.field(j) | |
131 | element.value = i + j | |
033fb0a7 | 132 | |
a47ab64a JG |
133 | event.payload("sequence_len").value = i % 10 |
134 | sequence_field = event.payload("sequence_field") | |
135 | sequence_field.length = event.payload("sequence_len") | |
136 | for j in range(event.payload("sequence_len").value): | |
137 | sequence_field.field(j).value = i + j | |
821ca76c | 138 | |
a47ab64a JG |
139 | enumeration_field = event.payload("enum_field") |
140 | integer_field = enumeration_field.container | |
141 | enumeration_field.value = i % 10 | |
033fb0a7 | 142 | |
6b30e7f3 SM |
143 | event.stream_context.field("field_in_stream_event_context").value = i * 10 |
144 | ||
a47ab64a | 145 | stream.append_event(event) |
033fb0a7 | 146 | |
7df927c7 JG |
147 | # Populate custom packet context field before flushing |
148 | packet_context = stream.packet_context | |
149 | field = packet_context.field("a_custom_packet_context_field") | |
150 | field.value = 42 | |
151 | ||
033fb0a7 | 152 | stream.flush() |