| 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 | |
| 25 | __all__ = ['_TraceClass'] |
| 26 | |
| 27 | import bt2 |
| 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 | import collections.abc |
| 34 | import functools |
| 35 | |
| 36 | |
| 37 | def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr): |
| 38 | trace_class = bt2_trace_class._TraceClass._create_from_ptr_and_get_ref( |
| 39 | trace_class_ptr |
| 40 | ) |
| 41 | user_listener(trace_class) |
| 42 | |
| 43 | |
| 44 | class _TraceClass(object._SharedObject, collections.abc.Mapping): |
| 45 | _get_ref = staticmethod(native_bt.trace_class_get_ref) |
| 46 | _put_ref = staticmethod(native_bt.trace_class_put_ref) |
| 47 | |
| 48 | # Instantiate a trace of this class. |
| 49 | |
| 50 | def __call__(self, name=None, uuid=None, env=None): |
| 51 | trace_ptr = native_bt.trace_create(self._ptr) |
| 52 | |
| 53 | if trace_ptr is None: |
| 54 | raise bt2._MemoryError('cannot create trace class object') |
| 55 | |
| 56 | trace = bt2_trace._Trace._create_from_ptr(trace_ptr) |
| 57 | |
| 58 | if name is not None: |
| 59 | trace._name = name |
| 60 | |
| 61 | if uuid is not None: |
| 62 | trace._uuid = uuid |
| 63 | |
| 64 | if env is not None: |
| 65 | for key, value in env.items(): |
| 66 | trace.env[key] = value |
| 67 | |
| 68 | return trace |
| 69 | |
| 70 | # Number of stream classes in this trace class. |
| 71 | |
| 72 | def __len__(self): |
| 73 | count = native_bt.trace_class_get_stream_class_count(self._ptr) |
| 74 | assert count >= 0 |
| 75 | return count |
| 76 | |
| 77 | # Get a stream class by stream id. |
| 78 | |
| 79 | def __getitem__(self, key): |
| 80 | utils._check_uint64(key) |
| 81 | |
| 82 | sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key) |
| 83 | if sc_ptr is None: |
| 84 | raise KeyError(key) |
| 85 | |
| 86 | return bt2_stream_class._StreamClass._create_from_ptr_and_get_ref(sc_ptr) |
| 87 | |
| 88 | def __iter__(self): |
| 89 | for idx in range(len(self)): |
| 90 | sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const( |
| 91 | self._ptr, idx |
| 92 | ) |
| 93 | assert sc_ptr is not None |
| 94 | |
| 95 | id = native_bt.stream_class_get_id(sc_ptr) |
| 96 | assert id >= 0 |
| 97 | |
| 98 | yield id |
| 99 | |
| 100 | def create_stream_class( |
| 101 | self, |
| 102 | id=None, |
| 103 | name=None, |
| 104 | packet_context_field_class=None, |
| 105 | event_common_context_field_class=None, |
| 106 | default_clock_class=None, |
| 107 | assigns_automatic_event_class_id=True, |
| 108 | assigns_automatic_stream_id=True, |
| 109 | supports_packets=False, |
| 110 | packets_have_beginning_default_clock_snapshot=False, |
| 111 | packets_have_end_default_clock_snapshot=False, |
| 112 | supports_discarded_events=False, |
| 113 | discarded_events_have_default_clock_snapshots=False, |
| 114 | supports_discarded_packets=False, |
| 115 | discarded_packets_have_default_clock_snapshots=False, |
| 116 | ): |
| 117 | |
| 118 | if self.assigns_automatic_stream_class_id: |
| 119 | if id is not None: |
| 120 | raise ValueError( |
| 121 | 'id provided, but trace class assigns automatic stream class ids' |
| 122 | ) |
| 123 | |
| 124 | sc_ptr = native_bt.stream_class_create(self._ptr) |
| 125 | else: |
| 126 | if id is None: |
| 127 | raise ValueError( |
| 128 | 'id not provided, but trace class does not assign automatic stream class ids' |
| 129 | ) |
| 130 | |
| 131 | utils._check_uint64(id) |
| 132 | sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) |
| 133 | |
| 134 | sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr) |
| 135 | |
| 136 | if name is not None: |
| 137 | sc._name = name |
| 138 | |
| 139 | if event_common_context_field_class is not None: |
| 140 | sc._event_common_context_field_class = event_common_context_field_class |
| 141 | |
| 142 | if default_clock_class is not None: |
| 143 | sc._default_clock_class = default_clock_class |
| 144 | |
| 145 | # call after `sc._default_clock_class` because, if |
| 146 | # `packets_have_beginning_default_clock_snapshot` or |
| 147 | # `packets_have_end_default_clock_snapshot` is true, then this |
| 148 | # stream class needs a default clock class already. |
| 149 | sc._set_supports_packets( |
| 150 | supports_packets, |
| 151 | packets_have_beginning_default_clock_snapshot, |
| 152 | packets_have_end_default_clock_snapshot, |
| 153 | ) |
| 154 | |
| 155 | # call after sc._set_supports_packets() because, if |
| 156 | # `packet_context_field_class` is not `None`, then this stream |
| 157 | # class needs to support packets already. |
| 158 | if packet_context_field_class is not None: |
| 159 | sc._packet_context_field_class = packet_context_field_class |
| 160 | |
| 161 | sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id |
| 162 | sc._assigns_automatic_stream_id = assigns_automatic_stream_id |
| 163 | sc._set_supports_discarded_events( |
| 164 | supports_discarded_events, discarded_events_have_default_clock_snapshots |
| 165 | ) |
| 166 | sc._set_supports_discarded_packets( |
| 167 | supports_discarded_packets, discarded_packets_have_default_clock_snapshots |
| 168 | ) |
| 169 | return sc |
| 170 | |
| 171 | @property |
| 172 | def assigns_automatic_stream_class_id(self): |
| 173 | return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr) |
| 174 | |
| 175 | def _assigns_automatic_stream_class_id(self, auto_id): |
| 176 | utils._check_bool(auto_id) |
| 177 | return native_bt.trace_class_set_assigns_automatic_stream_class_id( |
| 178 | self._ptr, auto_id |
| 179 | ) |
| 180 | |
| 181 | _assigns_automatic_stream_class_id = property( |
| 182 | fset=_assigns_automatic_stream_class_id |
| 183 | ) |
| 184 | |
| 185 | # Field class creation methods. |
| 186 | |
| 187 | def _check_field_class_create_status(self, ptr, type_name): |
| 188 | if ptr is None: |
| 189 | raise bt2._MemoryError('cannot create {} field class'.format(type_name)) |
| 190 | |
| 191 | def create_bool_field_class(self): |
| 192 | field_class_ptr = native_bt.field_class_bool_create(self._ptr) |
| 193 | self._check_field_class_create_status(field_class_ptr, 'boolean') |
| 194 | |
| 195 | return bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr) |
| 196 | |
| 197 | def create_bit_array_field_class(self, length): |
| 198 | utils._check_uint64(length) |
| 199 | |
| 200 | if length < 1 or length > 64: |
| 201 | raise ValueError( |
| 202 | 'invalid length {}: expecting a value in the [1, 64] range'.format( |
| 203 | length |
| 204 | ) |
| 205 | ) |
| 206 | |
| 207 | field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length) |
| 208 | self._check_field_class_create_status(field_class_ptr, 'bit array') |
| 209 | |
| 210 | return bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr) |
| 211 | |
| 212 | def _create_integer_field_class( |
| 213 | self, create_func, py_cls, type_name, field_value_range, preferred_display_base |
| 214 | ): |
| 215 | field_class_ptr = create_func(self._ptr) |
| 216 | self._check_field_class_create_status(field_class_ptr, type_name) |
| 217 | |
| 218 | field_class = py_cls._create_from_ptr(field_class_ptr) |
| 219 | |
| 220 | if field_value_range is not None: |
| 221 | field_class._field_value_range = field_value_range |
| 222 | |
| 223 | if preferred_display_base is not None: |
| 224 | field_class._preferred_display_base = preferred_display_base |
| 225 | |
| 226 | return field_class |
| 227 | |
| 228 | def create_signed_integer_field_class( |
| 229 | self, field_value_range=None, preferred_display_base=None |
| 230 | ): |
| 231 | return self._create_integer_field_class( |
| 232 | native_bt.field_class_integer_signed_create, |
| 233 | bt2_field_class._SignedIntegerFieldClass, |
| 234 | 'signed integer', |
| 235 | field_value_range, |
| 236 | preferred_display_base, |
| 237 | ) |
| 238 | |
| 239 | def create_unsigned_integer_field_class( |
| 240 | self, field_value_range=None, preferred_display_base=None |
| 241 | ): |
| 242 | return self._create_integer_field_class( |
| 243 | native_bt.field_class_integer_unsigned_create, |
| 244 | bt2_field_class._UnsignedIntegerFieldClass, |
| 245 | 'unsigned integer', |
| 246 | field_value_range, |
| 247 | preferred_display_base, |
| 248 | ) |
| 249 | |
| 250 | def create_signed_enumeration_field_class( |
| 251 | self, field_value_range=None, preferred_display_base=None |
| 252 | ): |
| 253 | return self._create_integer_field_class( |
| 254 | native_bt.field_class_enumeration_signed_create, |
| 255 | bt2_field_class._SignedEnumerationFieldClass, |
| 256 | 'signed enumeration', |
| 257 | field_value_range, |
| 258 | preferred_display_base, |
| 259 | ) |
| 260 | |
| 261 | def create_unsigned_enumeration_field_class( |
| 262 | self, field_value_range=None, preferred_display_base=None |
| 263 | ): |
| 264 | return self._create_integer_field_class( |
| 265 | native_bt.field_class_enumeration_unsigned_create, |
| 266 | bt2_field_class._UnsignedEnumerationFieldClass, |
| 267 | 'unsigned enumeration', |
| 268 | field_value_range, |
| 269 | preferred_display_base, |
| 270 | ) |
| 271 | |
| 272 | def create_real_field_class(self, is_single_precision=False): |
| 273 | field_class_ptr = native_bt.field_class_real_create(self._ptr) |
| 274 | self._check_field_class_create_status(field_class_ptr, 'real') |
| 275 | |
| 276 | field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr) |
| 277 | |
| 278 | field_class._is_single_precision = is_single_precision |
| 279 | |
| 280 | return field_class |
| 281 | |
| 282 | def create_structure_field_class(self): |
| 283 | field_class_ptr = native_bt.field_class_structure_create(self._ptr) |
| 284 | self._check_field_class_create_status(field_class_ptr, 'structure') |
| 285 | |
| 286 | return bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr) |
| 287 | |
| 288 | def create_string_field_class(self): |
| 289 | field_class_ptr = native_bt.field_class_string_create(self._ptr) |
| 290 | self._check_field_class_create_status(field_class_ptr, 'string') |
| 291 | |
| 292 | return bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr) |
| 293 | |
| 294 | def create_static_array_field_class(self, elem_fc, length): |
| 295 | utils._check_type(elem_fc, bt2_field_class._FieldClass) |
| 296 | utils._check_uint64(length) |
| 297 | ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length) |
| 298 | self._check_field_class_create_status(ptr, 'static array') |
| 299 | |
| 300 | return bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr) |
| 301 | |
| 302 | def create_dynamic_array_field_class(self, elem_fc, length_fc=None): |
| 303 | utils._check_type(elem_fc, bt2_field_class._FieldClass) |
| 304 | length_fc_ptr = None |
| 305 | |
| 306 | if length_fc is not None: |
| 307 | utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass) |
| 308 | length_fc_ptr = length_fc._ptr |
| 309 | |
| 310 | ptr = native_bt.field_class_array_dynamic_create( |
| 311 | self._ptr, elem_fc._ptr, length_fc_ptr |
| 312 | ) |
| 313 | self._check_field_class_create_status(ptr, 'dynamic array') |
| 314 | return bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr) |
| 315 | |
| 316 | def create_option_field_class(self, content_fc, selector_fc=None): |
| 317 | utils._check_type(content_fc, bt2_field_class._FieldClass) |
| 318 | |
| 319 | selector_fc_ptr = None |
| 320 | |
| 321 | if selector_fc is not None: |
| 322 | utils._check_type(selector_fc, bt2_field_class._BoolFieldClass) |
| 323 | selector_fc_ptr = selector_fc._ptr |
| 324 | |
| 325 | ptr = native_bt.field_class_option_create( |
| 326 | self._ptr, content_fc._ptr, selector_fc_ptr |
| 327 | ) |
| 328 | self._check_field_class_create_status(ptr, 'option') |
| 329 | return bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr) |
| 330 | |
| 331 | def create_variant_field_class(self, selector_fc=None): |
| 332 | selector_fc_ptr = None |
| 333 | |
| 334 | if selector_fc is not None: |
| 335 | utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass) |
| 336 | selector_fc_ptr = selector_fc._ptr |
| 337 | |
| 338 | ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr) |
| 339 | self._check_field_class_create_status(ptr, 'variant') |
| 340 | return bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr) |
| 341 | |
| 342 | # Add a listener to be called when the trace class is destroyed. |
| 343 | |
| 344 | def add_destruction_listener(self, listener): |
| 345 | |
| 346 | if not callable(listener): |
| 347 | raise TypeError("'listener' parameter is not callable") |
| 348 | |
| 349 | fn = native_bt.bt2_trace_class_add_destruction_listener |
| 350 | listener_from_native = functools.partial( |
| 351 | _trace_class_destruction_listener_from_native, listener |
| 352 | ) |
| 353 | |
| 354 | status, listener_id = fn(self._ptr, listener_from_native) |
| 355 | utils._handle_func_status( |
| 356 | status, 'cannot add destruction listener to trace class object' |
| 357 | ) |
| 358 | |
| 359 | return utils._ListenerHandle(listener_id, self) |