| 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 | import bt2.stream_class |
| 30 | import bt2.field_class |
| 31 | import collections.abc |
| 32 | import functools |
| 33 | |
| 34 | |
| 35 | class _StreamClassIterator(collections.abc.Iterator): |
| 36 | def __init__(self, trace_class): |
| 37 | self._trace_class = trace_class |
| 38 | self._at = 0 |
| 39 | |
| 40 | def __next__(self): |
| 41 | if self._at == len(self._trace_class): |
| 42 | raise StopIteration |
| 43 | |
| 44 | borrow_stream_class_fn = native_bt.trace_class_borrow_stream_class_by_index_const |
| 45 | sc_ptr = borrow_stream_class_fn(self._trace_class._ptr, self._at) |
| 46 | assert sc_ptr |
| 47 | id = native_bt.stream_class_get_id(sc_ptr) |
| 48 | assert id >= 0 |
| 49 | self._at += 1 |
| 50 | return id |
| 51 | |
| 52 | |
| 53 | def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr): |
| 54 | trace_class = bt2.trace_class._TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) |
| 55 | user_listener(trace_class) |
| 56 | |
| 57 | |
| 58 | class _TraceClass(object._SharedObject, collections.abc.Mapping): |
| 59 | _get_ref = staticmethod(native_bt.trace_class_get_ref) |
| 60 | _put_ref = staticmethod(native_bt.trace_class_put_ref) |
| 61 | |
| 62 | # Instantiate a trace of this class. |
| 63 | |
| 64 | def __call__(self, name=None, uuid=None, env=None): |
| 65 | trace_ptr = native_bt.trace_create(self._ptr) |
| 66 | |
| 67 | if trace_ptr is None: |
| 68 | raise bt2.CreationError('cannot create trace class object') |
| 69 | |
| 70 | trace = bt2.trace._Trace._create_from_ptr(trace_ptr) |
| 71 | |
| 72 | if name is not None: |
| 73 | trace._name = name |
| 74 | |
| 75 | if uuid is not None: |
| 76 | trace._uuid = uuid |
| 77 | |
| 78 | if env is not None: |
| 79 | for key, value in env.items(): |
| 80 | trace.env[key] = value |
| 81 | |
| 82 | return trace |
| 83 | |
| 84 | # Number of stream classes in this trace class. |
| 85 | |
| 86 | def __len__(self): |
| 87 | count = native_bt.trace_class_get_stream_class_count(self._ptr) |
| 88 | assert count >= 0 |
| 89 | return count |
| 90 | |
| 91 | # Get a stream class by stream id. |
| 92 | |
| 93 | def __getitem__(self, key): |
| 94 | utils._check_uint64(key) |
| 95 | |
| 96 | sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key) |
| 97 | if sc_ptr is None: |
| 98 | raise KeyError(key) |
| 99 | |
| 100 | return bt2.stream_class._StreamClass._create_from_ptr_and_get_ref(sc_ptr) |
| 101 | |
| 102 | def __iter__(self): |
| 103 | for idx in range(len(self)): |
| 104 | sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const(self._ptr, idx) |
| 105 | assert sc_ptr is not None |
| 106 | |
| 107 | id = native_bt.stream_class_get_id(sc_ptr) |
| 108 | assert id >= 0 |
| 109 | |
| 110 | yield id |
| 111 | |
| 112 | def create_stream_class(self, id=None, |
| 113 | name=None, |
| 114 | packet_context_field_class=None, |
| 115 | event_common_context_field_class=None, |
| 116 | default_clock_class=None, |
| 117 | assigns_automatic_event_class_id=True, |
| 118 | assigns_automatic_stream_id=True, |
| 119 | supports_packets=False, |
| 120 | packets_have_beginning_default_clock_snapshot=False, |
| 121 | packets_have_end_default_clock_snapshot=False, |
| 122 | supports_discarded_events=False, |
| 123 | discarded_events_have_default_clock_snapshots=False, |
| 124 | supports_discarded_packets=False, |
| 125 | discarded_packets_have_default_clock_snapshots=False): |
| 126 | |
| 127 | if self.assigns_automatic_stream_class_id: |
| 128 | if id is not None: |
| 129 | raise ValueError('id provided, but trace class assigns automatic stream class ids') |
| 130 | |
| 131 | sc_ptr = native_bt.stream_class_create(self._ptr) |
| 132 | else: |
| 133 | if id is None: |
| 134 | raise ValueError('id not provided, but trace class does not assign automatic stream class ids') |
| 135 | |
| 136 | utils._check_uint64(id) |
| 137 | sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) |
| 138 | |
| 139 | sc = bt2.stream_class._StreamClass._create_from_ptr(sc_ptr) |
| 140 | |
| 141 | if name is not None: |
| 142 | sc._name = name |
| 143 | |
| 144 | if event_common_context_field_class is not None: |
| 145 | sc._event_common_context_field_class = event_common_context_field_class |
| 146 | |
| 147 | if default_clock_class is not None: |
| 148 | sc._default_clock_class = default_clock_class |
| 149 | |
| 150 | # call after `sc._default_clock_class` because, if |
| 151 | # `packets_have_beginning_default_clock_snapshot` or |
| 152 | # `packets_have_end_default_clock_snapshot` is true, then this |
| 153 | # stream class needs a default clock class already. |
| 154 | sc._set_supports_packets(supports_packets, |
| 155 | packets_have_beginning_default_clock_snapshot, |
| 156 | packets_have_end_default_clock_snapshot) |
| 157 | |
| 158 | # call after sc._set_supports_packets() because, if |
| 159 | # `packet_context_field_class` is not `None`, then this stream |
| 160 | # class needs to support packets already. |
| 161 | if packet_context_field_class is not None: |
| 162 | sc._packet_context_field_class = packet_context_field_class |
| 163 | |
| 164 | sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id |
| 165 | sc._assigns_automatic_stream_id = assigns_automatic_stream_id |
| 166 | sc._set_supports_discarded_events(supports_discarded_events, |
| 167 | discarded_events_have_default_clock_snapshots) |
| 168 | sc._set_supports_discarded_packets(supports_discarded_packets, |
| 169 | discarded_packets_have_default_clock_snapshots) |
| 170 | return sc |
| 171 | |
| 172 | @property |
| 173 | def assigns_automatic_stream_class_id(self): |
| 174 | return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr) |
| 175 | |
| 176 | def _assigns_automatic_stream_class_id(self, auto_id): |
| 177 | utils._check_bool(auto_id) |
| 178 | return native_bt.trace_class_set_assigns_automatic_stream_class_id(self._ptr, auto_id) |
| 179 | |
| 180 | _assigns_automatic_stream_class_id = property(fset=_assigns_automatic_stream_class_id) |
| 181 | |
| 182 | # Field class creation methods. |
| 183 | |
| 184 | def _check_create_status(self, ptr, type_name): |
| 185 | if ptr is None: |
| 186 | raise bt2.CreationError( |
| 187 | 'cannot create {} field class'.format(type_name)) |
| 188 | |
| 189 | def _create_integer_field_class(self, create_func, py_cls, type_name, field_value_range, preferred_display_base): |
| 190 | field_class_ptr = create_func(self._ptr) |
| 191 | self._check_create_status(field_class_ptr, type_name) |
| 192 | |
| 193 | field_class = py_cls._create_from_ptr(field_class_ptr) |
| 194 | |
| 195 | if field_value_range is not None: |
| 196 | field_class._field_value_range = field_value_range |
| 197 | |
| 198 | if preferred_display_base is not None: |
| 199 | field_class._preferred_display_base = preferred_display_base |
| 200 | |
| 201 | return field_class |
| 202 | |
| 203 | def create_signed_integer_field_class(self, field_value_range=None, preferred_display_base=None): |
| 204 | return self._create_integer_field_class(native_bt.field_class_signed_integer_create, |
| 205 | bt2.field_class._SignedIntegerFieldClass, |
| 206 | 'signed integer', field_value_range, preferred_display_base) |
| 207 | |
| 208 | def create_unsigned_integer_field_class(self, field_value_range=None, preferred_display_base=None): |
| 209 | return self._create_integer_field_class(native_bt.field_class_unsigned_integer_create, |
| 210 | bt2.field_class._UnsignedIntegerFieldClass, |
| 211 | 'unsigned integer', field_value_range, preferred_display_base) |
| 212 | |
| 213 | def create_signed_enumeration_field_class(self, field_value_range=None, preferred_display_base=None): |
| 214 | return self._create_integer_field_class(native_bt.field_class_signed_enumeration_create, |
| 215 | bt2.field_class._SignedEnumerationFieldClass, |
| 216 | 'signed enumeration', field_value_range, preferred_display_base) |
| 217 | |
| 218 | def create_unsigned_enumeration_field_class(self, field_value_range=None, preferred_display_base=None): |
| 219 | return self._create_integer_field_class(native_bt.field_class_unsigned_enumeration_create, |
| 220 | bt2.field_class._UnsignedEnumerationFieldClass, |
| 221 | 'unsigned enumeration', field_value_range, preferred_display_base) |
| 222 | |
| 223 | def create_real_field_class(self, is_single_precision=False): |
| 224 | field_class_ptr = native_bt.field_class_real_create(self._ptr) |
| 225 | self._check_create_status(field_class_ptr, 'real') |
| 226 | |
| 227 | field_class = bt2.field_class._RealFieldClass._create_from_ptr(field_class_ptr) |
| 228 | |
| 229 | field_class._is_single_precision = is_single_precision |
| 230 | |
| 231 | return field_class |
| 232 | |
| 233 | def create_structure_field_class(self): |
| 234 | field_class_ptr = native_bt.field_class_structure_create(self._ptr) |
| 235 | self._check_create_status(field_class_ptr, 'structure') |
| 236 | |
| 237 | return bt2.field_class._StructureFieldClass._create_from_ptr(field_class_ptr) |
| 238 | |
| 239 | def create_string_field_class(self): |
| 240 | field_class_ptr = native_bt.field_class_string_create(self._ptr) |
| 241 | self._check_create_status(field_class_ptr, 'string') |
| 242 | |
| 243 | return bt2.field_class._StringFieldClass._create_from_ptr(field_class_ptr) |
| 244 | |
| 245 | def create_static_array_field_class(self, elem_fc, length): |
| 246 | utils._check_type(elem_fc, bt2.field_class._FieldClass) |
| 247 | utils._check_uint64(length) |
| 248 | ptr = native_bt.field_class_static_array_create(self._ptr, elem_fc._ptr, length) |
| 249 | self._check_create_status(ptr, 'static array') |
| 250 | |
| 251 | return bt2.field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr) |
| 252 | |
| 253 | def create_dynamic_array_field_class(self, elem_fc, length_fc=None): |
| 254 | utils._check_type(elem_fc, bt2.field_class._FieldClass) |
| 255 | ptr = native_bt.field_class_dynamic_array_create(self._ptr, elem_fc._ptr) |
| 256 | self._check_create_status(ptr, 'dynamic array') |
| 257 | obj = bt2.field_class._DynamicArrayFieldClass._create_from_ptr(ptr) |
| 258 | |
| 259 | if length_fc is not None: |
| 260 | obj._length_field_class = length_fc |
| 261 | |
| 262 | return obj |
| 263 | |
| 264 | def create_variant_field_class(self, selector_fc=None): |
| 265 | selector_fc_ptr = None |
| 266 | |
| 267 | if selector_fc is not None: |
| 268 | utils._check_type(selector_fc, bt2.field_class._IntegerFieldClass) |
| 269 | selector_fc_ptr = selector_fc._ptr |
| 270 | |
| 271 | ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr) |
| 272 | self._check_create_status(ptr, 'variant') |
| 273 | return bt2.field_class._create_field_class_from_ptr_and_get_ref(ptr) |
| 274 | |
| 275 | # Add a listener to be called when the trace class is destroyed. |
| 276 | |
| 277 | def add_destruction_listener(self, listener): |
| 278 | |
| 279 | if not callable(listener): |
| 280 | raise TypeError("'listener' parameter is not callable") |
| 281 | |
| 282 | fn = native_bt.bt2_trace_class_add_destruction_listener |
| 283 | listener_from_native = functools.partial(_trace_class_destruction_listener_from_native, |
| 284 | listener) |
| 285 | |
| 286 | listener_id = fn(self._ptr, listener_from_native) |
| 287 | if listener_id is None: |
| 288 | utils._raise_bt2_error('cannot add destruction listener to trace class object') |
| 289 | |
| 290 | return bt2._ListenerHandle(listener_id, self) |