178eed313c60ae6f3ec425f0eb52badee2127964
4 # Babeltrace CTF Writer example script.
6 # Copyright 2013 EfficiOS Inc.
8 # Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
22 from babeltrace
import *
24 trace_path
= tempfile
.mkdtemp()
26 print("Writing trace at {}".format(trace_path
))
27 writer
= CTFWriter
.Writer(trace_path
)
29 clock
= CTFWriter
.Clock("A_clock")
30 print("Clock name is \"{}\"".format(clock
.name
))
31 clock
.description
= "Simple clock"
32 print("Clock description is \"{}\"".format(clock
.description
))
33 print("Clock frequency is {}".format(clock
.frequency
))
34 print("Clock precision is {}".format(clock
.precision
))
35 print("Clock offset_seconds is {}".format(clock
.offset_seconds
))
36 print("Clock offset is {}".format(clock
.offset
))
37 print("Clock is absolute: {}".format(clock
.absolute
))
38 print("Clock time is {}".format(clock
.time
))
39 print("Clock UUID is {}".format(clock
.uuid
))
41 writer
.add_clock(clock
)
42 writer
.add_environment_field("Python_version", str(sys
.version_info
))
44 stream_class
= CTFWriter
.StreamClass("test_stream")
45 stream_class
.clock
= clock
47 event_class
= CTFWriter
.EventClass("SimpleEvent")
49 # Create a int32_t equivalent type
50 int32_type
= CTFWriter
.IntegerFieldDeclaration(32)
51 int32_type
.signed
= True
53 # Create a uint16_t equivalent type
54 uint16_type
= CTFWriter
.IntegerFieldDeclaration(16)
55 uint16_type
.signed
= False
57 # Add a custom uint16_t field in the stream's packet context
58 packet_context_type
= stream_class
.packet_context_type
59 print("\nFields in default packet context:")
60 for field
in packet_context_type
.fields
:
61 print(str(type(field
[1])) + " " + field
[0])
62 packet_context_type
.add_field(uint16_type
, "a_custom_packet_context_field")
63 stream_class
.packet_context_type
= packet_context_type
65 # Create a string type
66 string_type
= CTFWriter
.StringFieldDeclaration()
68 # Create a structure type containing both an integer and a string
69 structure_type
= CTFWriter
.StructureFieldDeclaration()
70 structure_type
.add_field(int32_type
, "an_integer")
71 structure_type
.add_field(string_type
, "a_string_field")
72 event_class
.add_field(structure_type
, "structure_field")
74 # Create a floating point type
75 floating_point_type
= CTFWriter
.FloatFieldDeclaration()
76 floating_point_type
.exponent_digits
= CTFWriter
.FloatFieldDeclaration
.FLT_EXP_DIG
77 floating_point_type
.mantissa_digits
= CTFWriter
.FloatFieldDeclaration
.FLT_MANT_DIG
78 event_class
.add_field(floating_point_type
, "float_field")
80 # Create an enumeration type
81 int10_type
= CTFWriter
.IntegerFieldDeclaration(10)
82 enumeration_type
= CTFWriter
.EnumerationFieldDeclaration(int10_type
)
83 enumeration_type
.add_mapping("FIRST_ENTRY", 0, 4)
84 enumeration_type
.add_mapping("SECOND_ENTRY", 5, 5)
85 enumeration_type
.add_mapping("THIRD_ENTRY", 6, 10)
86 event_class
.add_field(enumeration_type
, "enum_field")
88 # Create an array type
89 array_type
= CTFWriter
.ArrayFieldDeclaration(int10_type
, 5)
90 event_class
.add_field(array_type
, "array_field")
92 # Create a sequence type
93 sequence_type
= CTFWriter
.SequenceFieldDeclaration(int32_type
, "sequence_len")
94 event_class
.add_field(uint16_type
, "sequence_len")
95 event_class
.add_field(sequence_type
, "sequence_field")
97 stream_class
.add_event_class(event_class
)
98 stream
= writer
.create_stream(stream_class
)
101 event
= CTFWriter
.Event(event_class
)
103 clock
.time
= i
* 1000
104 structure_field
= event
.payload("structure_field")
105 integer_field
= structure_field
.field("an_integer")
106 integer_field
.value
= i
108 string_field
= structure_field
.field("a_string_field")
109 string_field
.value
= "Test string."
111 float_field
= event
.payload("float_field")
112 float_field
.value
= float(i
) + (float(i
) / 100.0)
114 array_field
= event
.payload("array_field")
116 element
= array_field
.field(j
)
117 element
.value
= i
+ j
119 event
.payload("sequence_len").value
= i
% 10
120 sequence_field
= event
.payload("sequence_field")
121 sequence_field
.length
= event
.payload("sequence_len")
122 for j
in range(event
.payload("sequence_len").value
):
123 sequence_field
.field(j
).value
= i
+ j
125 enumeration_field
= event
.payload("enum_field")
126 integer_field
= enumeration_field
.container
127 enumeration_field
.value
= i
% 10
129 stream
.append_event(event
)
131 # Populate custom packet context field before flushing
132 packet_context
= stream
.packet_context
133 field
= packet_context
.field("a_custom_packet_context_field")
This page took 0.033748 seconds and 4 git commands to generate.