Commit | Line | Data |
---|---|---|
fbbe9302 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 | ||
2c6f8520 | 25 | __all__ = ['_TraceClass'] |
fbbe9302 SM |
26 | |
27 | import bt2 | |
28 | from bt2 import native_bt, utils, object | |
2c6f8520 | 29 | import bt2.stream_class |
45c51519 | 30 | import bt2.field_class |
fbbe9302 SM |
31 | import collections.abc |
32 | import functools | |
33 | ||
34 | ||
fbbe9302 SM |
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): | |
2c6f8520 | 54 | trace_class = bt2.trace_class._TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) |
fbbe9302 SM |
55 | user_listener(trace_class) |
56 | ||
57 | ||
2c6f8520 | 58 | class _TraceClass(object._SharedObject, collections.abc.Mapping): |
fbbe9302 SM |
59 | _get_ref = staticmethod(native_bt.trace_class_get_ref) |
60 | _put_ref = staticmethod(native_bt.trace_class_put_ref) | |
61 | ||
8c2367b8 SM |
62 | # Instantiate a trace of this class. |
63 | ||
335a2da5 | 64 | def __call__(self, name=None, uuid=None, env=None): |
8c2367b8 SM |
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 | ||
2c6f8520 | 70 | trace = bt2.trace._Trace._create_from_ptr(trace_ptr) |
8c2367b8 SM |
71 | |
72 | if name is not None: | |
73 | trace._name = name | |
74 | ||
335a2da5 PP |
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 | ||
8c2367b8 SM |
82 | return trace |
83 | ||
fbbe9302 SM |
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 | ||
2c6f8520 | 100 | return bt2.stream_class._StreamClass._create_from_ptr_and_get_ref(sc_ptr) |
fbbe9302 SM |
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 | ||
8c2367b8 | 112 | def create_stream_class(self, id=None, |
3cdfbaea SM |
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, | |
26fc5aed | 119 | supports_packets=False, |
9b24b6aa PP |
120 | packets_have_beginning_default_clock_snapshot=False, |
121 | packets_have_end_default_clock_snapshot=False, | |
2e90378a PP |
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): | |
fbbe9302 SM |
126 | |
127 | if self.assigns_automatic_stream_class_id: | |
128 | if id is not None: | |
4430bc80 | 129 | raise ValueError('id provided, but trace class assigns automatic stream class ids') |
fbbe9302 SM |
130 | |
131 | sc_ptr = native_bt.stream_class_create(self._ptr) | |
132 | else: | |
133 | if id is None: | |
4430bc80 | 134 | raise ValueError('id not provided, but trace class does not assign automatic stream class ids') |
fbbe9302 SM |
135 | |
136 | utils._check_uint64(id) | |
137 | sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) | |
138 | ||
2c6f8520 | 139 | sc = bt2.stream_class._StreamClass._create_from_ptr(sc_ptr) |
8c2367b8 | 140 | |
3cdfbaea SM |
141 | if name is not None: |
142 | sc._name = name | |
143 | ||
3cdfbaea SM |
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 | ||
26fc5aed PP |
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 | ||
3cdfbaea | 164 | sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id |
8c2367b8 | 165 | sc._assigns_automatic_stream_id = assigns_automatic_stream_id |
2e90378a PP |
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) | |
8c2367b8 | 170 | return sc |
fbbe9302 SM |
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 | ||
3cdfbaea SM |
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 | ||
d47b87ac | 189 | def _create_integer_field_class(self, create_func, py_cls, type_name, field_value_range, preferred_display_base): |
af4bbfc7 SM |
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 | ||
d47b87ac SM |
195 | if field_value_range is not None: |
196 | field_class._field_value_range = field_value_range | |
af4bbfc7 | 197 | |
d47b87ac SM |
198 | if preferred_display_base is not None: |
199 | field_class._preferred_display_base = preferred_display_base | |
af4bbfc7 SM |
200 | |
201 | return field_class | |
202 | ||
d47b87ac | 203 | def create_signed_integer_field_class(self, field_value_range=None, preferred_display_base=None): |
af4bbfc7 | 204 | return self._create_integer_field_class(native_bt.field_class_signed_integer_create, |
d47b87ac SM |
205 | bt2.field_class._SignedIntegerFieldClass, |
206 | 'signed integer', field_value_range, preferred_display_base) | |
af4bbfc7 | 207 | |
d47b87ac | 208 | def create_unsigned_integer_field_class(self, field_value_range=None, preferred_display_base=None): |
2ae9f48c | 209 | return self._create_integer_field_class(native_bt.field_class_unsigned_integer_create, |
d47b87ac SM |
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) | |
2ae9f48c SM |
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 | ||
d47b87ac | 227 | field_class = bt2.field_class._RealFieldClass._create_from_ptr(field_class_ptr) |
2ae9f48c | 228 | |
d47b87ac | 229 | field_class._is_single_precision = is_single_precision |
2ae9f48c SM |
230 | |
231 | return field_class | |
232 | ||
3cdfbaea SM |
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 | ||
d47b87ac SM |
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) | |
1367bc7c | 255 | length_fc_ptr = None |
d47b87ac SM |
256 | |
257 | if length_fc is not None: | |
1367bc7c PP |
258 | utils._check_type(length_fc, bt2.field_class._UnsignedIntegerFieldClass) |
259 | length_fc_ptr = length_fc._ptr | |
d47b87ac | 260 | |
1367bc7c PP |
261 | ptr = native_bt.field_class_dynamic_array_create(self._ptr, elem_fc._ptr, length_fc_ptr) |
262 | self._check_create_status(ptr, 'dynamic array') | |
263 | return bt2.field_class._DynamicArrayFieldClass._create_from_ptr(ptr) | |
d47b87ac SM |
264 | |
265 | def create_variant_field_class(self, selector_fc=None): | |
45c51519 | 266 | selector_fc_ptr = None |
d47b87ac SM |
267 | |
268 | if selector_fc is not None: | |
45c51519 PP |
269 | utils._check_type(selector_fc, bt2.field_class._IntegerFieldClass) |
270 | selector_fc_ptr = selector_fc._ptr | |
d47b87ac | 271 | |
45c51519 PP |
272 | ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr) |
273 | self._check_create_status(ptr, 'variant') | |
274 | return bt2.field_class._create_field_class_from_ptr_and_get_ref(ptr) | |
3cdfbaea | 275 | |
fbbe9302 SM |
276 | # Add a listener to be called when the trace class is destroyed. |
277 | ||
278 | def add_destruction_listener(self, listener): | |
279 | ||
280 | if not callable(listener): | |
281 | raise TypeError("'listener' parameter is not callable") | |
282 | ||
d24d5663 | 283 | fn = native_bt.bt2_trace_class_add_destruction_listener |
fbbe9302 SM |
284 | listener_from_native = functools.partial(_trace_class_destruction_listener_from_native, |
285 | listener) | |
286 | ||
287 | listener_id = fn(self._ptr, listener_from_native) | |
288 | if listener_id is None: | |
289 | utils._raise_bt2_error('cannot add destruction listener to trace class object') | |
290 | ||
291 | return bt2._ListenerHandle(listener_id, self) |