Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
24 | import bt2.clock_class | |
25 | import bt2.packet | |
26 | import bt2.stream | |
c4239792 | 27 | import bt2.field |
4b552f8b | 28 | import bt2.clock_snapshot |
71fd6f52 | 29 | import collections |
81447b5b PP |
30 | import numbers |
31 | import copy | |
32 | import abc | |
33 | import bt2 | |
34 | ||
35 | ||
36 | def _create_from_ptr(ptr): | |
37 | # recreate the event class wrapper of this event's class (the | |
38 | # identity could be different, but the underlying address should be | |
39 | # the same) | |
50842bdc | 40 | event_class_ptr = native_bt.event_get_class(ptr) |
81447b5b PP |
41 | utils._handle_ptr(event_class_ptr, "cannot get event object's class") |
42 | event_class = bt2.EventClass._create_from_ptr(event_class_ptr) | |
43 | event = _Event._create_from_ptr(ptr) | |
44 | event._event_class = event_class | |
45 | return event | |
46 | ||
47 | ||
4b552f8b SM |
48 | class _EventClockSnapshotsIterator(collections.abc.Iterator): |
49 | def __init__(self, event_clock_snapshots): | |
50 | self._event_clock_snapshots = event_clock_snapshots | |
51 | self._clock_classes = event_clock_snapshots._event._clock_classes | |
71fd6f52 PP |
52 | self._at = 0 |
53 | ||
54 | def __next__(self): | |
55 | if self._at == len(self._clock_classes): | |
56 | raise StopIteration | |
57 | ||
58 | self._at += 1 | |
59 | return self._clock_classes[at] | |
60 | ||
61 | ||
4b552f8b | 62 | class _EventClockSnapshots(collections.abc.Mapping): |
71fd6f52 PP |
63 | def __init__(self, event): |
64 | self._event = event | |
65 | ||
66 | def __getitem__(self, clock_class): | |
67 | utils._check_type(clock_class, bt2.ClockClass) | |
4b552f8b | 68 | clock_snapshot_ptr = native_bt.event_get_clock_snapshot(self._event._ptr, |
71fd6f52 PP |
69 | clock_class._ptr) |
70 | ||
4b552f8b | 71 | if clock_snapshot_ptr is None: |
71fd6f52 PP |
72 | return |
73 | ||
4b552f8b SM |
74 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
75 | return clock_snapshot | |
71fd6f52 | 76 | |
4b552f8b SM |
77 | def add(self, clock_snapshot): |
78 | utils._check_type(clock_snapshot, bt2.clock_snapshot._ClockSnapshot) | |
79 | ret = native_bt.event_set_clock_snapshot(self._ptr, | |
80 | clock_snapshot._ptr) | |
71fd6f52 PP |
81 | utils._handle_ret(ret, "cannot set event object's clock value") |
82 | ||
83 | def __len__(self): | |
84 | count = len(self._event._clock_classes) | |
85 | assert(count >= 0) | |
86 | return count | |
87 | ||
88 | def __iter__(self): | |
4b552f8b | 89 | return _EventClockSnapshotsIterator(self) |
71fd6f52 PP |
90 | |
91 | ||
81447b5b PP |
92 | class _Event(object._Object): |
93 | @property | |
94 | def event_class(self): | |
95 | return self._event_class | |
96 | ||
97 | @property | |
98 | def name(self): | |
99 | return self._event_class.name | |
100 | ||
101 | @property | |
102 | def id(self): | |
103 | return self._event_class.id | |
104 | ||
105 | @property | |
106 | def packet(self): | |
50842bdc | 107 | packet_ptr = native_bt.event_get_packet(self._ptr) |
81447b5b PP |
108 | |
109 | if packet_ptr is None: | |
110 | return packet_ptr | |
111 | ||
112 | return bt2.packet._Packet._create_from_ptr(packet_ptr) | |
113 | ||
114 | @packet.setter | |
115 | def packet(self, packet): | |
116 | utils._check_type(packet, bt2.packet._Packet) | |
50842bdc | 117 | ret = native_bt.event_set_packet(self._ptr, packet._ptr) |
81447b5b PP |
118 | utils._handle_ret(ret, "cannot set event object's packet object") |
119 | ||
120 | @property | |
121 | def stream(self): | |
50842bdc | 122 | stream_ptr = native_bt.event_get_stream(self._ptr) |
81447b5b PP |
123 | |
124 | if stream_ptr is None: | |
125 | return stream_ptr | |
126 | ||
127 | return bt2.stream._Stream._create_from_ptr(stream_ptr) | |
128 | ||
129 | @property | |
130 | def header_field(self): | |
50842bdc | 131 | field_ptr = native_bt.event_get_header(self._ptr) |
81447b5b PP |
132 | |
133 | if field_ptr is None: | |
134 | return | |
135 | ||
c4239792 | 136 | return bt2.field._create_from_ptr(field_ptr) |
81447b5b PP |
137 | |
138 | @header_field.setter | |
139 | def header_field(self, header_field): | |
140 | header_field_ptr = None | |
141 | ||
142 | if header_field is not None: | |
c4239792 | 143 | utils._check_type(header_field, bt2.field._Field) |
81447b5b PP |
144 | header_field_ptr = header_field._ptr |
145 | ||
50842bdc | 146 | ret = native_bt.event_set_header(self._ptr, header_field_ptr) |
81447b5b PP |
147 | utils._handle_ret(ret, "cannot set event object's header field") |
148 | ||
149 | @property | |
150 | def stream_event_context_field(self): | |
50842bdc | 151 | field_ptr = native_bt.event_get_stream_event_context(self._ptr) |
81447b5b PP |
152 | |
153 | if field_ptr is None: | |
154 | return | |
155 | ||
c4239792 | 156 | return bt2.field._create_from_ptr(field_ptr) |
81447b5b PP |
157 | |
158 | @stream_event_context_field.setter | |
159 | def stream_event_context_field(self, stream_event_context): | |
160 | stream_event_context_ptr = None | |
161 | ||
162 | if stream_event_context is not None: | |
c4239792 | 163 | utils._check_type(stream_event_context, bt2.field._Field) |
81447b5b PP |
164 | stream_event_context_ptr = stream_event_context._ptr |
165 | ||
50842bdc | 166 | ret = native_bt.event_set_stream_event_context(self._ptr, |
0b03f63e | 167 | stream_event_context_ptr) |
81447b5b PP |
168 | utils._handle_ret(ret, "cannot set event object's stream event context field") |
169 | ||
170 | @property | |
171 | def context_field(self): | |
50842bdc | 172 | field_ptr = native_bt.event_get_event_context(self._ptr) |
81447b5b PP |
173 | |
174 | if field_ptr is None: | |
175 | return | |
176 | ||
c4239792 | 177 | return bt2.field._create_from_ptr(field_ptr) |
81447b5b PP |
178 | |
179 | @context_field.setter | |
180 | def context_field(self, context): | |
181 | context_ptr = None | |
182 | ||
183 | if context is not None: | |
c4239792 | 184 | utils._check_type(context, bt2.field._Field) |
81447b5b PP |
185 | context_ptr = context._ptr |
186 | ||
50842bdc | 187 | ret = native_bt.event_set_event_context(self._ptr, context_ptr) |
81447b5b PP |
188 | utils._handle_ret(ret, "cannot set event object's context field") |
189 | ||
190 | @property | |
191 | def payload_field(self): | |
50842bdc | 192 | field_ptr = native_bt.event_get_event_payload(self._ptr) |
81447b5b PP |
193 | |
194 | if field_ptr is None: | |
195 | return | |
196 | ||
c4239792 | 197 | return bt2.field._create_from_ptr(field_ptr) |
81447b5b PP |
198 | |
199 | @payload_field.setter | |
200 | def payload_field(self, payload): | |
201 | payload_ptr = None | |
202 | ||
203 | if payload is not None: | |
c4239792 | 204 | utils._check_type(payload, bt2.field._Field) |
81447b5b PP |
205 | payload_ptr = payload._ptr |
206 | ||
50842bdc | 207 | ret = native_bt.event_set_event_payload(self._ptr, payload_ptr) |
81447b5b PP |
208 | utils._handle_ret(ret, "cannot set event object's payload field") |
209 | ||
4b552f8b SM |
210 | def _get_clock_snapshot_cycles(self, clock_class_ptr): |
211 | clock_snapshot_ptr = native_bt.event_get_clock_snapshot(self._ptr, | |
0b03f63e | 212 | clock_class_ptr) |
81447b5b | 213 | |
4b552f8b | 214 | if clock_snapshot_ptr is None: |
81447b5b PP |
215 | return |
216 | ||
4b552f8b SM |
217 | ret, cycles = native_bt.clock_snapshot_get_value(clock_snapshot_ptr) |
218 | native_bt.put(clock_snapshot_ptr) | |
81447b5b PP |
219 | utils._handle_ret(ret, "cannot get clock value object's cycles") |
220 | return cycles | |
221 | ||
71fd6f52 | 222 | @property |
4b552f8b SM |
223 | def clock_snapshots(self): |
224 | return _EventClockSnapshots(self) | |
81447b5b PP |
225 | |
226 | def __getitem__(self, key): | |
227 | utils._check_str(key) | |
228 | payload_field = self.payload_field | |
229 | ||
230 | if payload_field is not None and key in payload_field: | |
231 | return payload_field[key] | |
232 | ||
233 | context_field = self.context_field | |
234 | ||
235 | if context_field is not None and key in context_field: | |
236 | return context_field[key] | |
237 | ||
238 | sec_field = self.stream_event_context_field | |
239 | ||
240 | if sec_field is not None and key in sec_field: | |
241 | return sec_field[key] | |
242 | ||
243 | header_field = self.header_field | |
244 | ||
245 | if header_field is not None and key in header_field: | |
246 | return header_field[key] | |
247 | ||
248 | packet = self.packet | |
249 | ||
250 | if packet is None: | |
251 | raise KeyError(key) | |
252 | ||
253 | pkt_context_field = packet.context_field | |
254 | ||
255 | if pkt_context_field is not None and key in pkt_context_field: | |
256 | return pkt_context_field[key] | |
257 | ||
258 | pkt_header_field = packet.header_field | |
259 | ||
260 | if pkt_header_field is not None and key in pkt_header_field: | |
261 | return pkt_header_field[key] | |
262 | ||
263 | raise KeyError(key) | |
264 | ||
265 | @property | |
266 | def _clock_classes(self): | |
267 | stream_class = self.event_class.stream_class | |
268 | ||
269 | if stream_class is None: | |
270 | return [] | |
271 | ||
272 | trace = stream_class.trace | |
273 | ||
274 | if trace is None: | |
275 | return [] | |
276 | ||
277 | clock_classes = [] | |
278 | ||
279 | for clock_class in trace.clock_classes.values(): | |
280 | clock_classes.append(clock_class) | |
281 | ||
282 | return clock_classes | |
283 | ||
284 | @property | |
285 | def _clock_class_ptrs(self): | |
286 | return [cc._ptr for cc in self._clock_classes] | |
287 | ||
288 | def __eq__(self, other): | |
289 | if type(other) is not type(self): | |
290 | return False | |
291 | ||
292 | if self.addr == other.addr: | |
293 | return True | |
294 | ||
4b552f8b SM |
295 | self_clock_snapshots = {} |
296 | other_clock_snapshots = {} | |
81447b5b PP |
297 | |
298 | for clock_class_ptr in self._clock_class_ptrs: | |
4b552f8b | 299 | self_clock_snapshots[int(clock_class_ptr)] = self._get_clock_snapshot_cycles(clock_class_ptr) |
81447b5b PP |
300 | |
301 | for clock_class_ptr in other._clock_class_ptrs: | |
4b552f8b | 302 | other_clock_snapshots[int(clock_class_ptr)] = self._get_clock_snapshot_cycles(clock_class_ptr) |
81447b5b PP |
303 | |
304 | self_props = ( | |
305 | self.header_field, | |
306 | self.stream_event_context_field, | |
307 | self.context_field, | |
308 | self.payload_field, | |
4b552f8b | 309 | self_clock_snapshots, |
81447b5b PP |
310 | ) |
311 | other_props = ( | |
312 | other.header_field, | |
313 | other.stream_event_context_field, | |
314 | other.context_field, | |
315 | other.payload_field, | |
4b552f8b | 316 | other_clock_snapshots, |
81447b5b PP |
317 | ) |
318 | return self_props == other_props | |
319 | ||
320 | def _copy(self, copy_func): | |
321 | cpy = self.event_class() | |
322 | ||
323 | # copy fields | |
324 | cpy.header_field = copy_func(self.header_field) | |
325 | cpy.stream_event_context_field = copy_func(self.stream_event_context_field) | |
326 | cpy.context_field = copy_func(self.context_field) | |
327 | cpy.payload_field = copy_func(self.payload_field) | |
328 | ||
811644b8 PP |
329 | # Copy known clock value references. It's not necessary to copy |
330 | # clock class or clock value objects because once a clock value | |
331 | # is created from a clock class, the clock class is frozen. | |
332 | # Thus even if we copy the clock class, the user cannot modify | |
333 | # it, therefore it's useless to copy it. | |
81447b5b | 334 | for clock_class in self._clock_classes: |
4b552f8b | 335 | clock_snapshot = self.clock_snapshots[clock_class] |
81447b5b | 336 | |
4b552f8b SM |
337 | if clock_snapshot is not None: |
338 | cpy.clock_snapshots.add(clock_snapshot) | |
81447b5b PP |
339 | |
340 | return cpy | |
341 | ||
342 | def __copy__(self): | |
343 | return self._copy(copy.copy) | |
344 | ||
345 | def __deepcopy__(self, memo): | |
346 | cpy = self._copy(copy.deepcopy) | |
347 | memo[id(self)] = cpy | |
348 | return cpy |