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 from bt2
import stream_class
as bt2_stream_class
30 from bt2
import field_class
as bt2_field_class
31 from bt2
import trace
as bt2_trace
32 from bt2
import trace_class
as bt2_trace_class
33 from bt2
import value
as bt2_value
34 import collections
.abc
38 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
39 trace_class
= bt2_trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(
42 user_listener(trace_class
)
45 class _TraceClass(object._SharedObject
, collections
.abc
.Mapping
):
46 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
47 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
49 # Instantiate a trace of this class.
51 def __call__(self
, name
=None, user_attributes
=None, uuid
=None, environment
=None):
52 trace_ptr
= native_bt
.trace_create(self
._ptr
)
55 raise bt2
._MemoryError('cannot create trace class object')
57 trace
= bt2_trace
._Trace
._create
_from
_ptr
(trace_ptr
)
62 if user_attributes
is not None:
63 trace
._user
_attributes
= user_attributes
68 if environment
is not None:
69 for key
, value
in environment
.items():
70 trace
.environment
[key
] = value
74 # Number of stream classes in this trace class.
77 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
81 # Get a stream class by stream id.
83 def __getitem__(self
, key
):
84 utils
._check
_uint
64(key
)
86 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_id_const(self
._ptr
, key
)
90 return bt2_stream_class
._StreamClass
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
93 for idx
in range(len(self
)):
94 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_index_const(
97 assert sc_ptr
is not None
99 id = native_bt
.stream_class_get_id(sc_ptr
)
104 def create_stream_class(
108 user_attributes
=None,
109 packet_context_field_class
=None,
110 event_common_context_field_class
=None,
111 default_clock_class
=None,
112 assigns_automatic_event_class_id
=True,
113 assigns_automatic_stream_id
=True,
114 supports_packets
=False,
115 packets_have_beginning_default_clock_snapshot
=False,
116 packets_have_end_default_clock_snapshot
=False,
117 supports_discarded_events
=False,
118 discarded_events_have_default_clock_snapshots
=False,
119 supports_discarded_packets
=False,
120 discarded_packets_have_default_clock_snapshots
=False,
123 if self
.assigns_automatic_stream_class_id
:
126 'id provided, but trace class assigns automatic stream class ids'
129 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
133 'id not provided, but trace class does not assign automatic stream class ids'
136 utils
._check
_uint
64(id)
137 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
139 sc
= bt2_stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
144 if user_attributes
is not None:
145 sc
._user
_attributes
= user_attributes
147 if event_common_context_field_class
is not None:
148 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
150 if default_clock_class
is not None:
151 sc
._default
_clock
_class
= default_clock_class
153 # call after `sc._default_clock_class` because, if
154 # `packets_have_beginning_default_clock_snapshot` or
155 # `packets_have_end_default_clock_snapshot` is true, then this
156 # stream class needs a default clock class already.
157 sc
._set
_supports
_packets
(
159 packets_have_beginning_default_clock_snapshot
,
160 packets_have_end_default_clock_snapshot
,
163 # call after sc._set_supports_packets() because, if
164 # `packet_context_field_class` is not `None`, then this stream
165 # class needs to support packets already.
166 if packet_context_field_class
is not None:
167 sc
._packet
_context
_field
_class
= packet_context_field_class
169 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
170 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
171 sc
._set
_supports
_discarded
_events
(
172 supports_discarded_events
, discarded_events_have_default_clock_snapshots
174 sc
._set
_supports
_discarded
_packets
(
175 supports_discarded_packets
, discarded_packets_have_default_clock_snapshots
180 def user_attributes(self
):
181 ptr
= native_bt
.trace_class_borrow_user_attributes(self
._ptr
)
182 assert ptr
is not None
183 return bt2_value
._create
_from
_ptr
_and
_get
_ref
(ptr
)
185 def _user_attributes(self
, user_attributes
):
186 value
= bt2_value
.create_value(user_attributes
)
187 utils
._check
_type
(value
, bt2_value
.MapValue
)
188 native_bt
.trace_class_set_user_attributes(self
._ptr
, value
._ptr
)
190 _user_attributes
= property(fset
=_user_attributes
)
193 def assigns_automatic_stream_class_id(self
):
194 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
196 def _assigns_automatic_stream_class_id(self
, auto_id
):
197 utils
._check
_bool
(auto_id
)
198 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(
202 _assigns_automatic_stream_class_id
= property(
203 fset
=_assigns_automatic_stream_class_id
206 # Field class creation methods.
208 def _check_field_class_create_status(self
, ptr
, type_name
):
210 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
213 def _set_field_class_user_attrs(fc
, user_attributes
):
214 if user_attributes
is not None:
215 fc
._user
_attributes
= user_attributes
217 def create_bool_field_class(self
, user_attributes
=None):
218 field_class_ptr
= native_bt
.field_class_bool_create(self
._ptr
)
219 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'boolean')
220 fc
= bt2_field_class
._BoolFieldClass
._create
_from
_ptr
(field_class_ptr
)
221 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
224 def create_bit_array_field_class(self
, length
, user_attributes
=None):
225 utils
._check
_uint
64(length
)
227 if length
< 1 or length
> 64:
229 'invalid length {}: expecting a value in the [1, 64] range'.format(
234 field_class_ptr
= native_bt
.field_class_bit_array_create(self
._ptr
, length
)
235 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'bit array')
236 fc
= bt2_field_class
._BitArrayFieldClass
._create
_from
_ptr
(field_class_ptr
)
237 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
240 def _create_integer_field_class(
246 preferred_display_base
,
249 field_class_ptr
= create_func(self
._ptr
)
250 self
._check
_field
_class
_create
_status
(field_class_ptr
, type_name
)
252 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
254 if field_value_range
is not None:
255 field_class
._field
_value
_range
= field_value_range
257 if preferred_display_base
is not None:
258 field_class
._preferred
_display
_base
= preferred_display_base
260 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
263 def create_signed_integer_field_class(
264 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
266 return self
._create
_integer
_field
_class
(
267 native_bt
.field_class_integer_signed_create
,
268 bt2_field_class
._SignedIntegerFieldClass
,
271 preferred_display_base
,
275 def create_unsigned_integer_field_class(
276 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
278 return self
._create
_integer
_field
_class
(
279 native_bt
.field_class_integer_unsigned_create
,
280 bt2_field_class
._UnsignedIntegerFieldClass
,
283 preferred_display_base
,
287 def create_signed_enumeration_field_class(
288 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
290 return self
._create
_integer
_field
_class
(
291 native_bt
.field_class_enumeration_signed_create
,
292 bt2_field_class
._SignedEnumerationFieldClass
,
293 'signed enumeration',
295 preferred_display_base
,
299 def create_unsigned_enumeration_field_class(
300 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
302 return self
._create
_integer
_field
_class
(
303 native_bt
.field_class_enumeration_unsigned_create
,
304 bt2_field_class
._UnsignedEnumerationFieldClass
,
305 'unsigned enumeration',
307 preferred_display_base
,
311 def create_real_field_class(self
, is_single_precision
=False, user_attributes
=None):
312 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
313 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'real')
315 field_class
= bt2_field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
317 field_class
._is
_single
_precision
= is_single_precision
318 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
322 def create_structure_field_class(self
, user_attributes
=None):
323 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
324 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'structure')
325 fc
= bt2_field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
326 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
329 def create_string_field_class(self
, user_attributes
=None):
330 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
331 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'string')
332 fc
= bt2_field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
333 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
336 def create_static_array_field_class(self
, elem_fc
, length
, user_attributes
=None):
337 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
338 utils
._check
_uint
64(length
)
339 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
340 self
._check
_field
_class
_create
_status
(ptr
, 'static array')
341 fc
= bt2_field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
342 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
345 def create_dynamic_array_field_class(
346 self
, elem_fc
, length_fc
=None, user_attributes
=None
348 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
351 if length_fc
is not None:
352 utils
._check
_type
(length_fc
, bt2_field_class
._UnsignedIntegerFieldClass
)
353 length_fc_ptr
= length_fc
._ptr
355 ptr
= native_bt
.field_class_array_dynamic_create(
356 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
358 self
._check
_field
_class
_create
_status
(ptr
, 'dynamic array')
359 fc
= bt2_field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
360 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
363 def create_option_field_class(
364 self
, content_fc
, selector_fc
=None, user_attributes
=None
366 utils
._check
_type
(content_fc
, bt2_field_class
._FieldClass
)
368 selector_fc_ptr
= None
370 if selector_fc
is not None:
371 utils
._check
_type
(selector_fc
, bt2_field_class
._BoolFieldClass
)
372 selector_fc_ptr
= selector_fc
._ptr
374 ptr
= native_bt
.field_class_option_create(
375 self
._ptr
, content_fc
._ptr
, selector_fc_ptr
377 self
._check
_field
_class
_create
_status
(ptr
, 'option')
378 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
379 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
382 def create_variant_field_class(self
, selector_fc
=None, user_attributes
=None):
383 selector_fc_ptr
= None
385 if selector_fc
is not None:
386 utils
._check
_type
(selector_fc
, bt2_field_class
._IntegerFieldClass
)
387 selector_fc_ptr
= selector_fc
._ptr
389 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
390 self
._check
_field
_class
_create
_status
(ptr
, 'variant')
391 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
392 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
395 # Add a listener to be called when the trace class is destroyed.
397 def add_destruction_listener(self
, listener
):
399 if not callable(listener
):
400 raise TypeError("'listener' parameter is not callable")
402 fn
= native_bt
.bt2_trace_class_add_destruction_listener
403 listener_from_native
= functools
.partial(
404 _trace_class_destruction_listener_from_native
, listener
407 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
408 utils
._handle
_func
_status
(
409 status
, 'cannot add destruction listener to trace class object'
412 return utils
._ListenerHandle
(listener_id
, self
)