Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
811644b8 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
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 | |
81447b5b | 24 | import collections.abc |
3fb99a22 PP |
25 | from bt2 import value as bt2_value |
26 | from bt2 import stream as bt2_stream | |
3fb99a22 | 27 | from bt2 import stream_class as bt2_stream_class |
81447b5b | 28 | import bt2 |
8c2367b8 | 29 | import functools |
335a2da5 PP |
30 | import uuid as uuidp |
31 | ||
32 | ||
79935628 SM |
33 | def _bt2_trace_class(): |
34 | from bt2 import trace_class as bt2_trace_class | |
35 | ||
36 | return bt2_trace_class | |
37 | ||
38 | ||
f0a42b33 FD |
39 | class _TraceEnvironmentConst(collections.abc.Mapping): |
40 | _create_value_from_ptr_and_get_ref = staticmethod( | |
41 | bt2_value._create_from_const_ptr_and_get_ref | |
42 | ) | |
43 | ||
335a2da5 PP |
44 | def __init__(self, trace): |
45 | self._trace = trace | |
46 | ||
47 | def __getitem__(self, key): | |
48 | utils._check_str(key) | |
49 | ||
50 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const | |
51 | value_ptr = borrow_entry_fn(self._trace._ptr, key) | |
52 | ||
53 | if value_ptr is None: | |
54 | raise KeyError(key) | |
55 | ||
f0a42b33 | 56 | return self._create_value_from_ptr_and_get_ref(value_ptr) |
335a2da5 PP |
57 | |
58 | def __len__(self): | |
59 | count = native_bt.trace_get_environment_entry_count(self._trace._ptr) | |
60 | assert count >= 0 | |
61 | return count | |
62 | ||
63 | def __iter__(self): | |
f0a42b33 | 64 | trace_ptr = self._trace._ptr |
335a2da5 PP |
65 | |
66 | for idx in range(len(self)): | |
67 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_by_index_const | |
68 | entry_name, _ = borrow_entry_fn(trace_ptr, idx) | |
69 | assert entry_name is not None | |
70 | yield entry_name | |
81447b5b PP |
71 | |
72 | ||
f0a42b33 FD |
73 | class _TraceEnvironment(_TraceEnvironmentConst, collections.abc.MutableMapping): |
74 | _create_value_from_ptr_and_get_ref = staticmethod( | |
75 | bt2_value._create_from_ptr_and_get_ref | |
76 | ) | |
77 | ||
78 | def __setitem__(self, key, value): | |
79 | if isinstance(value, str): | |
80 | set_env_entry_fn = native_bt.trace_set_environment_entry_string | |
81 | elif isinstance(value, int): | |
82 | set_env_entry_fn = native_bt.trace_set_environment_entry_integer | |
83 | else: | |
84 | raise TypeError('expected str or int, got {}'.format(type(value))) | |
81447b5b | 85 | |
f0a42b33 FD |
86 | status = set_env_entry_fn(self._trace._ptr, key, value) |
87 | utils._handle_func_status(status, "cannot set trace object's environment entry") | |
81447b5b | 88 | |
f0a42b33 FD |
89 | def __delitem__(self, key): |
90 | raise NotImplementedError | |
91 | ||
92 | ||
93 | class _TraceConst(object._SharedObject, collections.abc.Mapping): | |
8c2367b8 SM |
94 | _get_ref = staticmethod(native_bt.trace_get_ref) |
95 | _put_ref = staticmethod(native_bt.trace_put_ref) | |
f0a42b33 FD |
96 | _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id_const) |
97 | _borrow_stream_ptr_by_index = staticmethod( | |
98 | native_bt.trace_borrow_stream_by_index_const | |
99 | ) | |
100 | _borrow_class_ptr = staticmethod(native_bt.trace_borrow_class_const) | |
101 | _borrow_user_attributes_ptr = staticmethod( | |
102 | native_bt.trace_borrow_user_attributes_const | |
103 | ) | |
104 | _create_value_from_ptr_and_get_ref = staticmethod( | |
105 | bt2_value._create_from_const_ptr_and_get_ref | |
106 | ) | |
107 | _stream_pycls = property(lambda _: bt2_stream._StreamConst) | |
79935628 | 108 | _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClassConst) |
f0a42b33 | 109 | _trace_env_pycls = property(lambda _: _TraceEnvironmentConst) |
811644b8 PP |
110 | |
111 | def __len__(self): | |
8c2367b8 SM |
112 | count = native_bt.trace_get_stream_count(self._ptr) |
113 | assert count >= 0 | |
811644b8 PP |
114 | return count |
115 | ||
8c2367b8 SM |
116 | def __getitem__(self, id): |
117 | utils._check_uint64(id) | |
81447b5b | 118 | |
f0a42b33 | 119 | stream_ptr = self._borrow_stream_ptr_by_id(self._ptr, id) |
81447b5b | 120 | |
8c2367b8 SM |
121 | if stream_ptr is None: |
122 | raise KeyError(id) | |
81447b5b | 123 | |
f0a42b33 | 124 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
125 | |
126 | def __iter__(self): | |
8c2367b8 | 127 | for idx in range(len(self)): |
f0a42b33 | 128 | stream_ptr = self._borrow_stream_ptr_by_index(self._ptr, idx) |
8c2367b8 | 129 | assert stream_ptr is not None |
81447b5b | 130 | |
8c2367b8 SM |
131 | id = native_bt.stream_get_id(stream_ptr) |
132 | assert id >= 0 | |
81447b5b | 133 | |
8c2367b8 | 134 | yield id |
81447b5b | 135 | |
05abc762 PP |
136 | @property |
137 | def cls(self): | |
f0a42b33 | 138 | trace_class_ptr = self._borrow_class_ptr(self._ptr) |
05abc762 | 139 | assert trace_class_ptr is not None |
f0a42b33 | 140 | return self._trace_class_pycls._create_from_ptr_and_get_ref(trace_class_ptr) |
05abc762 | 141 | |
5783664e PP |
142 | @property |
143 | def user_attributes(self): | |
f0a42b33 | 144 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
5783664e | 145 | assert ptr is not None |
f0a42b33 | 146 | return self._create_value_from_ptr_and_get_ref(ptr) |
5783664e | 147 | |
81447b5b PP |
148 | @property |
149 | def name(self): | |
50842bdc | 150 | return native_bt.trace_get_name(self._ptr) |
81447b5b | 151 | |
f0a42b33 FD |
152 | @property |
153 | def uuid(self): | |
154 | uuid_bytes = native_bt.trace_get_uuid(self._ptr) | |
155 | if uuid_bytes is None: | |
156 | return | |
157 | ||
158 | return uuidp.UUID(bytes=uuid_bytes) | |
159 | ||
160 | @property | |
161 | def environment(self): | |
162 | return self._trace_env_pycls(self) | |
163 | ||
164 | def add_destruction_listener(self, listener): | |
165 | '''Add a listener to be called when the trace is destroyed.''' | |
166 | if not callable(listener): | |
167 | raise TypeError("'listener' parameter is not callable") | |
168 | ||
67de22ba SM |
169 | handle = utils._ListenerHandle(self.addr) |
170 | ||
f0a42b33 FD |
171 | fn = native_bt.bt2_trace_add_destruction_listener |
172 | listener_from_native = functools.partial( | |
67de22ba | 173 | _trace_destruction_listener_from_native, listener, handle |
f0a42b33 FD |
174 | ) |
175 | ||
176 | status, listener_id = fn(self._ptr, listener_from_native) | |
177 | utils._handle_func_status( | |
178 | status, 'cannot add destruction listener to trace object' | |
179 | ) | |
180 | ||
67de22ba SM |
181 | handle._set_listener_id(listener_id) |
182 | ||
183 | return handle | |
f0a42b33 | 184 | |
1114a7d5 SM |
185 | def remove_destruction_listener(self, listener_handle): |
186 | utils._check_type(listener_handle, utils._ListenerHandle) | |
187 | ||
67de22ba | 188 | if listener_handle._addr != self.addr: |
1114a7d5 SM |
189 | raise ValueError( |
190 | 'This trace destruction listener does not match the trace object.' | |
191 | ) | |
192 | ||
193 | if listener_handle._listener_id is None: | |
194 | raise ValueError('This trace destruction listener was already removed.') | |
195 | ||
196 | status = native_bt.trace_remove_destruction_listener( | |
197 | self._ptr, listener_handle._listener_id | |
198 | ) | |
199 | utils._handle_func_status(status) | |
67de22ba | 200 | listener_handle._invalidate() |
1114a7d5 | 201 | |
f0a42b33 FD |
202 | |
203 | class _Trace(_TraceConst): | |
204 | _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id) | |
205 | _borrow_stream_ptr_by_index = staticmethod(native_bt.trace_borrow_stream_by_index) | |
206 | _borrow_class_ptr = staticmethod(native_bt.trace_borrow_class) | |
207 | _borrow_user_attributes_ptr = staticmethod(native_bt.trace_borrow_user_attributes) | |
208 | _create_value_from_ptr_and_get_ref = staticmethod( | |
209 | bt2_value._create_from_ptr_and_get_ref | |
210 | ) | |
211 | _stream_pycls = property(lambda _: bt2_stream._Stream) | |
79935628 | 212 | _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClass) |
f0a42b33 FD |
213 | _trace_env_pycls = property(lambda _: _TraceEnvironment) |
214 | ||
8c2367b8 | 215 | def _name(self, name): |
81447b5b | 216 | utils._check_str(name) |
d24d5663 | 217 | status = native_bt.trace_set_name(self._ptr, name) |
cfbd7cf3 | 218 | utils._handle_func_status(status, "cannot set trace class object's name") |
81447b5b | 219 | |
8c2367b8 | 220 | _name = property(fset=_name) |
811644b8 | 221 | |
f0a42b33 FD |
222 | def _user_attributes(self, user_attributes): |
223 | value = bt2_value.create_value(user_attributes) | |
224 | utils._check_type(value, bt2_value.MapValue) | |
225 | native_bt.trace_set_user_attributes(self._ptr, value._ptr) | |
335a2da5 | 226 | |
f0a42b33 | 227 | _user_attributes = property(fset=_user_attributes) |
335a2da5 PP |
228 | |
229 | def _uuid(self, uuid): | |
230 | utils._check_type(uuid, uuidp.UUID) | |
231 | native_bt.trace_set_uuid(self._ptr, uuid.bytes) | |
232 | ||
233 | _uuid = property(fset=_uuid) | |
234 | ||
5783664e | 235 | def create_stream(self, stream_class, id=None, name=None, user_attributes=None): |
3fb99a22 | 236 | utils._check_type(stream_class, bt2_stream_class._StreamClass) |
81447b5b | 237 | |
8c2367b8 SM |
238 | if stream_class.assigns_automatic_stream_id: |
239 | if id is not None: | |
cfbd7cf3 FD |
240 | raise ValueError( |
241 | "id provided, but stream class assigns automatic stream ids" | |
242 | ) | |
81447b5b | 243 | |
8c2367b8 SM |
244 | stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) |
245 | else: | |
246 | if id is None: | |
cfbd7cf3 FD |
247 | raise ValueError( |
248 | "id not provided, but stream class does not assign automatic stream ids" | |
249 | ) | |
81447b5b | 250 | |
8c2367b8 | 251 | utils._check_uint64(id) |
cfbd7cf3 FD |
252 | stream_ptr = native_bt.stream_create_with_id( |
253 | stream_class._ptr, self._ptr, id | |
254 | ) | |
81447b5b | 255 | |
8c2367b8 | 256 | if stream_ptr is None: |
694c792b | 257 | raise bt2._MemoryError('cannot create stream object') |
81447b5b | 258 | |
3fb99a22 | 259 | stream = bt2_stream._Stream._create_from_ptr(stream_ptr) |
81447b5b | 260 | |
8c2367b8 SM |
261 | if name is not None: |
262 | stream._name = name | |
81447b5b | 263 | |
5783664e PP |
264 | if user_attributes is not None: |
265 | stream._user_attributes = user_attributes | |
266 | ||
8c2367b8 | 267 | return stream |
81447b5b | 268 | |
81447b5b | 269 | |
67de22ba | 270 | def _trace_destruction_listener_from_native(user_listener, handle, trace_ptr): |
f0a42b33 FD |
271 | trace = _TraceConst._create_from_ptr_and_get_ref(trace_ptr) |
272 | user_listener(trace) | |
67de22ba | 273 | handle._invalidate() |