Commit | Line | Data |
---|---|---|
0bee8ea9 SM |
1 | # The MIT License (MIT) |
2 | # | |
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> | |
6 | # | |
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: | |
13 | # | |
14 | # The above copyright notice and this permission notice shall be included in | |
15 | # all copies or substantial portions of the Software. | |
16 | # | |
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 | |
23 | # THE SOFTWARE. | |
24 | ||
0bee8ea9 | 25 | from bt2 import native_bt, utils, object |
c946c9de PP |
26 | from bt2 import stream_class as bt2_stream_class |
27 | from bt2 import field_class as bt2_field_class | |
28 | from bt2 import trace as bt2_trace | |
29 | from bt2 import trace_class as bt2_trace_class | |
b2df5780 | 30 | from bt2 import value as bt2_value |
0bee8ea9 SM |
31 | import collections.abc |
32 | import functools | |
9cbe0c59 | 33 | import bt2 |
0bee8ea9 SM |
34 | |
35 | ||
0bee8ea9 | 36 | def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr): |
9cbe0c59 | 37 | trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) |
0bee8ea9 SM |
38 | user_listener(trace_class) |
39 | ||
40 | ||
9cbe0c59 | 41 | class _TraceClassConst(object._SharedObject, collections.abc.Mapping): |
0bee8ea9 SM |
42 | _get_ref = staticmethod(native_bt.trace_class_get_ref) |
43 | _put_ref = staticmethod(native_bt.trace_class_put_ref) | |
9cbe0c59 FD |
44 | _borrow_stream_class_ptr_by_index = staticmethod( |
45 | native_bt.trace_class_borrow_stream_class_by_index_const | |
46 | ) | |
47 | _borrow_stream_class_ptr_by_id = staticmethod( | |
48 | native_bt.trace_class_borrow_stream_class_by_id_const | |
49 | ) | |
50 | _stream_class_pycls = bt2_stream_class._StreamClassConst | |
51 | _create_value_from_ptr_and_get_ref = staticmethod( | |
52 | bt2_value._create_from_const_ptr_and_get_ref | |
53 | ) | |
0bee8ea9 | 54 | |
9cbe0c59 FD |
55 | @property |
56 | def user_attributes(self): | |
57 | ptr = native_bt.trace_class_borrow_user_attributes(self._ptr) | |
58 | assert ptr is not None | |
59 | return self._create_value_from_ptr_and_get_ref(ptr) | |
f377a958 | 60 | |
0bee8ea9 SM |
61 | # Number of stream classes in this trace class. |
62 | ||
63 | def __len__(self): | |
64 | count = native_bt.trace_class_get_stream_class_count(self._ptr) | |
65 | assert count >= 0 | |
66 | return count | |
67 | ||
68 | # Get a stream class by stream id. | |
69 | ||
70 | def __getitem__(self, key): | |
71 | utils._check_uint64(key) | |
72 | ||
9cbe0c59 | 73 | sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key) |
0bee8ea9 SM |
74 | if sc_ptr is None: |
75 | raise KeyError(key) | |
76 | ||
9cbe0c59 | 77 | return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr) |
0bee8ea9 SM |
78 | |
79 | def __iter__(self): | |
80 | for idx in range(len(self)): | |
9cbe0c59 | 81 | sc_ptr = self._borrow_stream_class_ptr_by_index(self._ptr, idx) |
0bee8ea9 SM |
82 | assert sc_ptr is not None |
83 | ||
84 | id = native_bt.stream_class_get_id(sc_ptr) | |
85 | assert id >= 0 | |
86 | ||
87 | yield id | |
88 | ||
9cbe0c59 FD |
89 | @property |
90 | def assigns_automatic_stream_class_id(self): | |
91 | return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr) | |
92 | ||
93 | # Add a listener to be called when the trace class is destroyed. | |
94 | ||
95 | def add_destruction_listener(self, listener): | |
96 | ||
97 | if not callable(listener): | |
98 | raise TypeError("'listener' parameter is not callable") | |
99 | ||
100 | fn = native_bt.bt2_trace_class_add_destruction_listener | |
101 | listener_from_native = functools.partial( | |
102 | _trace_class_destruction_listener_from_native, listener | |
103 | ) | |
104 | ||
105 | status, listener_id = fn(self._ptr, listener_from_native) | |
106 | utils._handle_func_status( | |
107 | status, 'cannot add destruction listener to trace class object' | |
108 | ) | |
109 | ||
110 | return utils._ListenerHandle(listener_id, self) | |
111 | ||
112 | ||
113 | class _TraceClass(_TraceClassConst): | |
114 | _borrow_stream_class_ptr_by_index = staticmethod( | |
115 | native_bt.trace_class_borrow_stream_class_by_index | |
116 | ) | |
117 | _borrow_stream_class_ptr_by_id = staticmethod( | |
118 | native_bt.trace_class_borrow_stream_class_by_id | |
119 | ) | |
120 | _stream_class_pycls = bt2_stream_class._StreamClass | |
121 | _create_value_from_ptr_and_get_ref = staticmethod( | |
122 | bt2_value._create_from_ptr_and_get_ref | |
123 | ) | |
124 | ||
125 | # Instantiate a trace of this class. | |
126 | ||
127 | def __call__(self, name=None, user_attributes=None, uuid=None, environment=None): | |
128 | trace_ptr = native_bt.trace_create(self._ptr) | |
129 | ||
130 | if trace_ptr is None: | |
131 | raise bt2._MemoryError('cannot create trace class object') | |
132 | ||
133 | trace = bt2_trace._Trace._create_from_ptr(trace_ptr) | |
134 | ||
135 | if name is not None: | |
136 | trace._name = name | |
137 | ||
138 | if user_attributes is not None: | |
139 | trace._user_attributes = user_attributes | |
140 | ||
141 | if uuid is not None: | |
142 | trace._uuid = uuid | |
143 | ||
144 | if environment is not None: | |
145 | for key, value in environment.items(): | |
146 | trace.environment[key] = value | |
147 | ||
148 | return trace | |
149 | ||
61d96b89 FD |
150 | def create_stream_class( |
151 | self, | |
152 | id=None, | |
153 | name=None, | |
b2df5780 | 154 | user_attributes=None, |
61d96b89 FD |
155 | packet_context_field_class=None, |
156 | event_common_context_field_class=None, | |
157 | default_clock_class=None, | |
158 | assigns_automatic_event_class_id=True, | |
159 | assigns_automatic_stream_id=True, | |
160 | supports_packets=False, | |
161 | packets_have_beginning_default_clock_snapshot=False, | |
162 | packets_have_end_default_clock_snapshot=False, | |
163 | supports_discarded_events=False, | |
164 | discarded_events_have_default_clock_snapshots=False, | |
165 | supports_discarded_packets=False, | |
166 | discarded_packets_have_default_clock_snapshots=False, | |
167 | ): | |
0bee8ea9 SM |
168 | |
169 | if self.assigns_automatic_stream_class_id: | |
170 | if id is not None: | |
61d96b89 FD |
171 | raise ValueError( |
172 | 'id provided, but trace class assigns automatic stream class ids' | |
173 | ) | |
0bee8ea9 SM |
174 | |
175 | sc_ptr = native_bt.stream_class_create(self._ptr) | |
176 | else: | |
177 | if id is None: | |
61d96b89 FD |
178 | raise ValueError( |
179 | 'id not provided, but trace class does not assign automatic stream class ids' | |
180 | ) | |
0bee8ea9 SM |
181 | |
182 | utils._check_uint64(id) | |
183 | sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) | |
184 | ||
c946c9de | 185 | sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr) |
f377a958 | 186 | |
060aee52 SM |
187 | if name is not None: |
188 | sc._name = name | |
189 | ||
b2df5780 PP |
190 | if user_attributes is not None: |
191 | sc._user_attributes = user_attributes | |
192 | ||
060aee52 SM |
193 | if event_common_context_field_class is not None: |
194 | sc._event_common_context_field_class = event_common_context_field_class | |
195 | ||
196 | if default_clock_class is not None: | |
197 | sc._default_clock_class = default_clock_class | |
198 | ||
37a93d41 PP |
199 | # call after `sc._default_clock_class` because, if |
200 | # `packets_have_beginning_default_clock_snapshot` or | |
201 | # `packets_have_end_default_clock_snapshot` is true, then this | |
202 | # stream class needs a default clock class already. | |
61d96b89 FD |
203 | sc._set_supports_packets( |
204 | supports_packets, | |
205 | packets_have_beginning_default_clock_snapshot, | |
206 | packets_have_end_default_clock_snapshot, | |
207 | ) | |
37a93d41 PP |
208 | |
209 | # call after sc._set_supports_packets() because, if | |
210 | # `packet_context_field_class` is not `None`, then this stream | |
211 | # class needs to support packets already. | |
212 | if packet_context_field_class is not None: | |
213 | sc._packet_context_field_class = packet_context_field_class | |
214 | ||
060aee52 | 215 | sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id |
f377a958 | 216 | sc._assigns_automatic_stream_id = assigns_automatic_stream_id |
61d96b89 FD |
217 | sc._set_supports_discarded_events( |
218 | supports_discarded_events, discarded_events_have_default_clock_snapshots | |
219 | ) | |
220 | sc._set_supports_discarded_packets( | |
221 | supports_discarded_packets, discarded_packets_have_default_clock_snapshots | |
222 | ) | |
f377a958 | 223 | return sc |
0bee8ea9 | 224 | |
b2df5780 PP |
225 | def _user_attributes(self, user_attributes): |
226 | value = bt2_value.create_value(user_attributes) | |
227 | utils._check_type(value, bt2_value.MapValue) | |
228 | native_bt.trace_class_set_user_attributes(self._ptr, value._ptr) | |
229 | ||
230 | _user_attributes = property(fset=_user_attributes) | |
231 | ||
0bee8ea9 SM |
232 | def _assigns_automatic_stream_class_id(self, auto_id): |
233 | utils._check_bool(auto_id) | |
61d96b89 FD |
234 | return native_bt.trace_class_set_assigns_automatic_stream_class_id( |
235 | self._ptr, auto_id | |
236 | ) | |
0bee8ea9 | 237 | |
61d96b89 FD |
238 | _assigns_automatic_stream_class_id = property( |
239 | fset=_assigns_automatic_stream_class_id | |
240 | ) | |
0bee8ea9 | 241 | |
060aee52 SM |
242 | # Field class creation methods. |
243 | ||
a07f15cb | 244 | def _check_field_class_create_status(self, ptr, type_name): |
060aee52 | 245 | if ptr is None: |
614743a5 | 246 | raise bt2._MemoryError('cannot create {} field class'.format(type_name)) |
060aee52 | 247 | |
b2df5780 PP |
248 | @staticmethod |
249 | def _set_field_class_user_attrs(fc, user_attributes): | |
250 | if user_attributes is not None: | |
251 | fc._user_attributes = user_attributes | |
252 | ||
253 | def create_bool_field_class(self, user_attributes=None): | |
a07f15cb PP |
254 | field_class_ptr = native_bt.field_class_bool_create(self._ptr) |
255 | self._check_field_class_create_status(field_class_ptr, 'boolean') | |
b2df5780 PP |
256 | fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr) |
257 | self._set_field_class_user_attrs(fc, user_attributes) | |
258 | return fc | |
a07f15cb | 259 | |
b2df5780 | 260 | def create_bit_array_field_class(self, length, user_attributes=None): |
6b29f2d4 PP |
261 | utils._check_uint64(length) |
262 | ||
263 | if length < 1 or length > 64: | |
264 | raise ValueError( | |
265 | 'invalid length {}: expecting a value in the [1, 64] range'.format( | |
266 | length | |
267 | ) | |
268 | ) | |
269 | ||
270 | field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length) | |
271 | self._check_field_class_create_status(field_class_ptr, 'bit array') | |
b2df5780 PP |
272 | fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr) |
273 | self._set_field_class_user_attrs(fc, user_attributes) | |
274 | return fc | |
6b29f2d4 | 275 | |
61d96b89 | 276 | def _create_integer_field_class( |
b2df5780 PP |
277 | self, |
278 | create_func, | |
279 | py_cls, | |
280 | type_name, | |
281 | field_value_range, | |
282 | preferred_display_base, | |
283 | user_attributes, | |
61d96b89 | 284 | ): |
a728f6c3 | 285 | field_class_ptr = create_func(self._ptr) |
a07f15cb | 286 | self._check_field_class_create_status(field_class_ptr, type_name) |
a728f6c3 SM |
287 | |
288 | field_class = py_cls._create_from_ptr(field_class_ptr) | |
289 | ||
c8820b76 SM |
290 | if field_value_range is not None: |
291 | field_class._field_value_range = field_value_range | |
a728f6c3 | 292 | |
c8820b76 SM |
293 | if preferred_display_base is not None: |
294 | field_class._preferred_display_base = preferred_display_base | |
a728f6c3 | 295 | |
b2df5780 | 296 | self._set_field_class_user_attrs(field_class, user_attributes) |
a728f6c3 SM |
297 | return field_class |
298 | ||
61d96b89 | 299 | def create_signed_integer_field_class( |
b2df5780 | 300 | self, field_value_range=None, preferred_display_base=None, user_attributes=None |
61d96b89 FD |
301 | ): |
302 | return self._create_integer_field_class( | |
60bbfc7c | 303 | native_bt.field_class_integer_signed_create, |
c946c9de | 304 | bt2_field_class._SignedIntegerFieldClass, |
61d96b89 FD |
305 | 'signed integer', |
306 | field_value_range, | |
307 | preferred_display_base, | |
b2df5780 | 308 | user_attributes, |
61d96b89 FD |
309 | ) |
310 | ||
311 | def create_unsigned_integer_field_class( | |
b2df5780 | 312 | self, field_value_range=None, preferred_display_base=None, user_attributes=None |
61d96b89 FD |
313 | ): |
314 | return self._create_integer_field_class( | |
60bbfc7c | 315 | native_bt.field_class_integer_unsigned_create, |
c946c9de | 316 | bt2_field_class._UnsignedIntegerFieldClass, |
61d96b89 FD |
317 | 'unsigned integer', |
318 | field_value_range, | |
319 | preferred_display_base, | |
b2df5780 | 320 | user_attributes, |
61d96b89 FD |
321 | ) |
322 | ||
323 | def create_signed_enumeration_field_class( | |
b2df5780 | 324 | self, field_value_range=None, preferred_display_base=None, user_attributes=None |
61d96b89 FD |
325 | ): |
326 | return self._create_integer_field_class( | |
60bbfc7c | 327 | native_bt.field_class_enumeration_signed_create, |
c946c9de | 328 | bt2_field_class._SignedEnumerationFieldClass, |
61d96b89 FD |
329 | 'signed enumeration', |
330 | field_value_range, | |
331 | preferred_display_base, | |
b2df5780 | 332 | user_attributes, |
61d96b89 FD |
333 | ) |
334 | ||
335 | def create_unsigned_enumeration_field_class( | |
b2df5780 | 336 | self, field_value_range=None, preferred_display_base=None, user_attributes=None |
61d96b89 FD |
337 | ): |
338 | return self._create_integer_field_class( | |
60bbfc7c | 339 | native_bt.field_class_enumeration_unsigned_create, |
c946c9de | 340 | bt2_field_class._UnsignedEnumerationFieldClass, |
61d96b89 FD |
341 | 'unsigned enumeration', |
342 | field_value_range, | |
343 | preferred_display_base, | |
b2df5780 | 344 | user_attributes, |
61d96b89 | 345 | ) |
27d97a3f | 346 | |
b2df5780 | 347 | def create_real_field_class(self, is_single_precision=False, user_attributes=None): |
27d97a3f | 348 | field_class_ptr = native_bt.field_class_real_create(self._ptr) |
a07f15cb | 349 | self._check_field_class_create_status(field_class_ptr, 'real') |
27d97a3f | 350 | |
c946c9de | 351 | field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr) |
27d97a3f | 352 | |
c8820b76 | 353 | field_class._is_single_precision = is_single_precision |
b2df5780 | 354 | self._set_field_class_user_attrs(field_class, user_attributes) |
27d97a3f SM |
355 | |
356 | return field_class | |
357 | ||
b2df5780 | 358 | def create_structure_field_class(self, user_attributes=None): |
060aee52 | 359 | field_class_ptr = native_bt.field_class_structure_create(self._ptr) |
a07f15cb | 360 | self._check_field_class_create_status(field_class_ptr, 'structure') |
b2df5780 PP |
361 | fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr) |
362 | self._set_field_class_user_attrs(fc, user_attributes) | |
363 | return fc | |
060aee52 | 364 | |
b2df5780 | 365 | def create_string_field_class(self, user_attributes=None): |
060aee52 | 366 | field_class_ptr = native_bt.field_class_string_create(self._ptr) |
a07f15cb | 367 | self._check_field_class_create_status(field_class_ptr, 'string') |
b2df5780 PP |
368 | fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr) |
369 | self._set_field_class_user_attrs(fc, user_attributes) | |
370 | return fc | |
060aee52 | 371 | |
b2df5780 | 372 | def create_static_array_field_class(self, elem_fc, length, user_attributes=None): |
c946c9de | 373 | utils._check_type(elem_fc, bt2_field_class._FieldClass) |
c8820b76 | 374 | utils._check_uint64(length) |
60bbfc7c | 375 | ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length) |
a07f15cb | 376 | self._check_field_class_create_status(ptr, 'static array') |
b2df5780 PP |
377 | fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr) |
378 | self._set_field_class_user_attrs(fc, user_attributes) | |
379 | return fc | |
c8820b76 | 380 | |
b2df5780 PP |
381 | def create_dynamic_array_field_class( |
382 | self, elem_fc, length_fc=None, user_attributes=None | |
383 | ): | |
c946c9de | 384 | utils._check_type(elem_fc, bt2_field_class._FieldClass) |
93d7d5d1 | 385 | length_fc_ptr = None |
c8820b76 SM |
386 | |
387 | if length_fc is not None: | |
c946c9de | 388 | utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass) |
93d7d5d1 | 389 | length_fc_ptr = length_fc._ptr |
c8820b76 | 390 | |
60bbfc7c | 391 | ptr = native_bt.field_class_array_dynamic_create( |
61d96b89 FD |
392 | self._ptr, elem_fc._ptr, length_fc_ptr |
393 | ) | |
a07f15cb | 394 | self._check_field_class_create_status(ptr, 'dynamic array') |
b2df5780 PP |
395 | fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr) |
396 | self._set_field_class_user_attrs(fc, user_attributes) | |
397 | return fc | |
c8820b76 | 398 | |
b2df5780 PP |
399 | def create_option_field_class( |
400 | self, content_fc, selector_fc=None, user_attributes=None | |
401 | ): | |
84eba0d9 PP |
402 | utils._check_type(content_fc, bt2_field_class._FieldClass) |
403 | ||
404 | selector_fc_ptr = None | |
405 | ||
406 | if selector_fc is not None: | |
407 | utils._check_type(selector_fc, bt2_field_class._BoolFieldClass) | |
408 | selector_fc_ptr = selector_fc._ptr | |
409 | ||
410 | ptr = native_bt.field_class_option_create( | |
411 | self._ptr, content_fc._ptr, selector_fc_ptr | |
412 | ) | |
413 | self._check_field_class_create_status(ptr, 'option') | |
b2df5780 PP |
414 | fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr) |
415 | self._set_field_class_user_attrs(fc, user_attributes) | |
416 | return fc | |
84eba0d9 | 417 | |
b2df5780 | 418 | def create_variant_field_class(self, selector_fc=None, user_attributes=None): |
02b61fe0 | 419 | selector_fc_ptr = None |
c8820b76 SM |
420 | |
421 | if selector_fc is not None: | |
c946c9de | 422 | utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass) |
02b61fe0 | 423 | selector_fc_ptr = selector_fc._ptr |
c8820b76 | 424 | |
02b61fe0 | 425 | ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr) |
a07f15cb | 426 | self._check_field_class_create_status(ptr, 'variant') |
b2df5780 PP |
427 | fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr) |
428 | self._set_field_class_user_attrs(fc, user_attributes) | |
429 | return fc |