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