1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4 # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
5 # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 __all__
= ['_TraceClass']
28 from bt2
import native_bt
, utils
, object
29 import bt2
.stream_class
30 import bt2
.field_class
32 import collections
.abc
36 class _StreamClassIterator(collections
.abc
.Iterator
):
37 def __init__(self
, trace_class
):
38 self
._trace
_class
= trace_class
42 if self
._at
== len(self
._trace
_class
):
45 borrow_stream_class_fn
= (
46 native_bt
.trace_class_borrow_stream_class_by_index_const
48 sc_ptr
= borrow_stream_class_fn(self
._trace
_class
._ptr
, self
._at
)
50 id = native_bt
.stream_class_get_id(sc_ptr
)
56 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
57 trace_class
= bt2
.trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(
60 user_listener(trace_class
)
63 class _TraceClass(object._SharedObject
, collections
.abc
.Mapping
):
64 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
65 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
67 # Instantiate a trace of this class.
69 def __call__(self
, name
=None, uuid
=None, env
=None):
70 trace_ptr
= native_bt
.trace_create(self
._ptr
)
73 raise bt2
._MemoryError('cannot create trace class object')
75 trace
= bt2
.trace
._Trace
._create
_from
_ptr
(trace_ptr
)
84 for key
, value
in env
.items():
85 trace
.env
[key
] = value
89 # Number of stream classes in this trace class.
92 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
96 # Get a stream class by stream id.
98 def __getitem__(self
, key
):
99 utils
._check
_uint
64(key
)
101 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_id_const(self
._ptr
, key
)
105 return bt2
.stream_class
._StreamClass
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
108 for idx
in range(len(self
)):
109 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_index_const(
112 assert sc_ptr
is not None
114 id = native_bt
.stream_class_get_id(sc_ptr
)
119 def create_stream_class(
123 packet_context_field_class
=None,
124 event_common_context_field_class
=None,
125 default_clock_class
=None,
126 assigns_automatic_event_class_id
=True,
127 assigns_automatic_stream_id
=True,
128 supports_packets
=False,
129 packets_have_beginning_default_clock_snapshot
=False,
130 packets_have_end_default_clock_snapshot
=False,
131 supports_discarded_events
=False,
132 discarded_events_have_default_clock_snapshots
=False,
133 supports_discarded_packets
=False,
134 discarded_packets_have_default_clock_snapshots
=False,
137 if self
.assigns_automatic_stream_class_id
:
140 'id provided, but trace class assigns automatic stream class ids'
143 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
147 'id not provided, but trace class does not assign automatic stream class ids'
150 utils
._check
_uint
64(id)
151 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
153 sc
= bt2
.stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
158 if event_common_context_field_class
is not None:
159 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
161 if default_clock_class
is not None:
162 sc
._default
_clock
_class
= default_clock_class
164 # call after `sc._default_clock_class` because, if
165 # `packets_have_beginning_default_clock_snapshot` or
166 # `packets_have_end_default_clock_snapshot` is true, then this
167 # stream class needs a default clock class already.
168 sc
._set
_supports
_packets
(
170 packets_have_beginning_default_clock_snapshot
,
171 packets_have_end_default_clock_snapshot
,
174 # call after sc._set_supports_packets() because, if
175 # `packet_context_field_class` is not `None`, then this stream
176 # class needs to support packets already.
177 if packet_context_field_class
is not None:
178 sc
._packet
_context
_field
_class
= packet_context_field_class
180 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
181 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
182 sc
._set
_supports
_discarded
_events
(
183 supports_discarded_events
, discarded_events_have_default_clock_snapshots
185 sc
._set
_supports
_discarded
_packets
(
186 supports_discarded_packets
, discarded_packets_have_default_clock_snapshots
191 def assigns_automatic_stream_class_id(self
):
192 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
194 def _assigns_automatic_stream_class_id(self
, auto_id
):
195 utils
._check
_bool
(auto_id
)
196 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(
200 _assigns_automatic_stream_class_id
= property(
201 fset
=_assigns_automatic_stream_class_id
204 # Field class creation methods.
206 def _check_create_status(self
, ptr
, type_name
):
208 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
210 def _create_integer_field_class(
211 self
, create_func
, py_cls
, type_name
, field_value_range
, preferred_display_base
213 field_class_ptr
= create_func(self
._ptr
)
214 self
._check
_create
_status
(field_class_ptr
, type_name
)
216 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
218 if field_value_range
is not None:
219 field_class
._field
_value
_range
= field_value_range
221 if preferred_display_base
is not None:
222 field_class
._preferred
_display
_base
= preferred_display_base
226 def create_signed_integer_field_class(
227 self
, field_value_range
=None, preferred_display_base
=None
229 return self
._create
_integer
_field
_class
(
230 native_bt
.field_class_integer_signed_create
,
231 bt2
.field_class
._SignedIntegerFieldClass
,
234 preferred_display_base
,
237 def create_unsigned_integer_field_class(
238 self
, field_value_range
=None, preferred_display_base
=None
240 return self
._create
_integer
_field
_class
(
241 native_bt
.field_class_integer_unsigned_create
,
242 bt2
.field_class
._UnsignedIntegerFieldClass
,
245 preferred_display_base
,
248 def create_signed_enumeration_field_class(
249 self
, field_value_range
=None, preferred_display_base
=None
251 return self
._create
_integer
_field
_class
(
252 native_bt
.field_class_enumeration_signed_create
,
253 bt2
.field_class
._SignedEnumerationFieldClass
,
254 'signed enumeration',
256 preferred_display_base
,
259 def create_unsigned_enumeration_field_class(
260 self
, field_value_range
=None, preferred_display_base
=None
262 return self
._create
_integer
_field
_class
(
263 native_bt
.field_class_enumeration_unsigned_create
,
264 bt2
.field_class
._UnsignedEnumerationFieldClass
,
265 'unsigned enumeration',
267 preferred_display_base
,
270 def create_real_field_class(self
, is_single_precision
=False):
271 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
272 self
._check
_create
_status
(field_class_ptr
, 'real')
274 field_class
= bt2
.field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
276 field_class
._is
_single
_precision
= is_single_precision
280 def create_structure_field_class(self
):
281 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
282 self
._check
_create
_status
(field_class_ptr
, 'structure')
284 return bt2
.field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
286 def create_string_field_class(self
):
287 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
288 self
._check
_create
_status
(field_class_ptr
, 'string')
290 return bt2
.field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
292 def create_static_array_field_class(self
, elem_fc
, length
):
293 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
294 utils
._check
_uint
64(length
)
295 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
296 self
._check
_create
_status
(ptr
, 'static array')
298 return bt2
.field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
300 def create_dynamic_array_field_class(self
, elem_fc
, length_fc
=None):
301 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
304 if length_fc
is not None:
305 utils
._check
_type
(length_fc
, bt2
.field_class
._UnsignedIntegerFieldClass
)
306 length_fc_ptr
= length_fc
._ptr
308 ptr
= native_bt
.field_class_array_dynamic_create(
309 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
311 self
._check
_create
_status
(ptr
, 'dynamic array')
312 return bt2
.field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
314 def create_variant_field_class(self
, selector_fc
=None):
315 selector_fc_ptr
= None
317 if selector_fc
is not None:
318 utils
._check
_type
(selector_fc
, bt2
.field_class
._IntegerFieldClass
)
319 selector_fc_ptr
= selector_fc
._ptr
321 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
322 self
._check
_create
_status
(ptr
, 'variant')
323 return bt2
.field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
325 # Add a listener to be called when the trace class is destroyed.
327 def add_destruction_listener(self
, listener
):
329 if not callable(listener
):
330 raise TypeError("'listener' parameter is not callable")
332 fn
= native_bt
.bt2_trace_class_add_destruction_listener
333 listener_from_native
= functools
.partial(
334 _trace_class_destruction_listener_from_native
, listener
337 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
338 utils
._handle
_func
_status
(
339 status
, 'cannot add destruction listener to trace class object'
342 return bt2
._ListenerHandle(listener_id
, self
)