~lttng/babeltrace/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# SPDX-License-Identifier: MIT
#
# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>

from bt2 import native_bt, object, utils
from bt2 import field_class as bt2_field_class
from bt2 import event_class as bt2_event_class
from bt2 import clock_class as bt2_clock_class
from bt2 import value as bt2_value
import collections.abc


def _bt2_trace_class():
    from bt2 import trace_class as bt2_trace_class

    return bt2_trace_class


class _StreamClassConst(object._SharedObject, collections.abc.Mapping):
    @staticmethod
    def _get_ref(ptr):
        native_bt.stream_class_get_ref(ptr)

    @staticmethod
    def _put_ref(ptr):
        native_bt.stream_class_put_ref(ptr)

    _borrow_event_class_ptr_by_id = staticmethod(
        native_bt.stream_class_borrow_event_class_by_id_const
    )
    _borrow_event_class_ptr_by_index = staticmethod(
        native_bt.stream_class_borrow_event_class_by_index_const
    )
    _borrow_trace_class_ptr = staticmethod(
        native_bt.stream_class_borrow_trace_class_const
    )
    _borrow_packet_context_field_class_ptr = staticmethod(
        native_bt.stream_class_borrow_packet_context_field_class_const
    )
    _borrow_event_common_context_field_class_ptr = staticmethod(
        native_bt.stream_class_borrow_event_common_context_field_class_const
    )
    _borrow_default_clock_class_ptr = staticmethod(
        native_bt.stream_class_borrow_default_clock_class_const
    )
    _borrow_user_attributes_ptr = staticmethod(
        native_bt.stream_class_borrow_user_attributes_const
    )

    _event_class_cls = property(lambda _: bt2_event_class._EventClassConst)
    _trace_class_cls = property(lambda _: _bt2_trace_class()._TraceClassConst)
    _clock_class_cls = property(lambda _: bt2_clock_class._ClockClassConst)

    def __getitem__(self, key):
        utils._check_int64(key)
        ec_ptr = self._borrow_event_class_ptr_by_id(self._ptr, key)

        if ec_ptr is None:
            raise KeyError(key)

        return self._event_class_cls._create_from_ptr_and_get_ref(ec_ptr)

    def __len__(self):
        count = native_bt.stream_class_get_event_class_count(self._ptr)
        assert count >= 0
        return count

    def __iter__(self):
        for idx in range(len(self)):
            ec_ptr = self._borrow_event_class_ptr_by_index(self._ptr, idx)
            assert ec_ptr is not None

            id = native_bt.event_class_get_id(ec_ptr)
            assert id >= 0

            yield id

    @property
    def trace_class(self):
        tc_ptr = self._borrow_trace_class_ptr(self._ptr)

        if tc_ptr is not None:
            return self._trace_class_cls._create_from_ptr_and_get_ref(tc_ptr)

    @property
    def user_attributes(self):
        ptr = self._borrow_user_attributes_ptr(self._ptr)
        assert ptr is not None
        return bt2_value._create_from_ptr_and_get_ref(ptr)

    @property
    def name(self):
        return native_bt.stream_class_get_name(self._ptr)

    @property
    def assigns_automatic_event_class_id(self):
        return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr)

    @property
    def assigns_automatic_stream_id(self):
        return native_bt.stream_class_assigns_automatic_stream_id(self._ptr)

    @property
    def supports_packets(self):
        return native_bt.stream_class_supports_packets(self._ptr)

    @property
    def packets_have_beginning_default_clock_snapshot(self):
        return native_bt.stream_class_packets_have_beginning_default_clock_snapshot(
            self._ptr
        )

    @property
    def packets_have_end_default_clock_snapshot(self):
        return native_bt.stream_class_packets_have_end_default_clock_snapshot(self._ptr)

    @property
    def supports_discarded_events(self):
        return native_bt.stream_class_supports_discarded_events(self._ptr)

    @property
    def discarded_events_have_default_clock_snapshots(self):
        return native_bt.stream_class_discarded_events_have_default_clock_snapshots(
            self._ptr
        )

    @property
    def supports_discarded_packets(self):
        return native_bt.stream_class_supports_discarded_packets(self._ptr)

    @property
    def discarded_packets_have_default_clock_snapshots(self):
        return native_bt.stream_class_discarded_packets_have_default_clock_snapshots(
            self._ptr
        )

    @property
    def id(self):
        id = native_bt.stream_class_get_id(self._ptr)

        if id < 0:
            return

        return id

    @property
    def packet_context_field_class(self):
        fc_ptr = self._borrow_packet_context_field_class_ptr(self._ptr)

        if fc_ptr is None:
            return

        return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)

    @property
    def event_common_context_field_class(self):
        fc_ptr = self._borrow_event_common_context_field_class_ptr(self._ptr)

        if fc_ptr is None:
            return

        return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)

    @property
    def default_clock_class(self):
        cc_ptr = self._borrow_default_clock_class_ptr(self._ptr)
        if cc_ptr is None:
            return

        return self._clock_class_cls._create_from_ptr_and_get_ref(cc_ptr)


class _StreamClass(_StreamClassConst):
    @staticmethod
    def _get_ref(ptr):
        native_bt.stream_class_get_ref(ptr)

    @staticmethod
    def _put_ref(ptr):
        native_bt.stream_class_put_ref(ptr)

    _borrow_event_class_ptr_by_id = staticmethod(
        native_bt.stream_class_borrow_event_class_by_id
    )
    _borrow_event_class_ptr_by_index = staticmethod(
        native_bt.stream_class_borrow_event_class_by_index
    )
    _borrow_trace_class_ptr = staticmethod(native_bt.stream_class_borrow_trace_class)
    _borrow_packet_context_field_class_ptr = staticmethod(
        native_bt.stream_class_borrow_packet_context_field_class
    )
    _borrow_event_common_context_field_class_ptr = staticmethod(
        native_bt.stream_class_borrow_event_common_context_field_class
    )
    _borrow_default_clock_class_ptr = staticmethod(
        native_bt.stream_class_borrow_default_clock_class
    )
    _borrow_user_attributes_ptr = staticmethod(
        native_bt.stream_class_borrow_user_attributes
    )

    _event_class_cls = property(lambda s: bt2_event_class._EventClass)
    _trace_class_cls = property(lambda s: _bt2_trace_class()._TraceClass)
    _clock_class_cls = property(lambda s: bt2_clock_class._ClockClass)

    def create_event_class(
        self,
        id=None,
        name=None,
        user_attributes=None,
        log_level=None,
        emf_uri=None,
        specific_context_field_class=None,
        payload_field_class=None,
    ):
        # Validate parameters before we create the object.
        bt2_event_class._EventClass._validate_create_params(
            name,
            user_attributes,
            log_level,
            emf_uri,
            specific_context_field_class,
            payload_field_class,
        )

        if self.assigns_automatic_event_class_id:
            if id is not None:
                raise ValueError(
                    "id provided, but stream class assigns automatic event class ids"
                )

            ec_ptr = native_bt.event_class_create(self._ptr)
        else:
            if id is None:
                raise ValueError(
                    "id not provided, but stream class does not assign automatic event class ids"
                )

            utils._check_uint64(id)
            ec_ptr = native_bt.event_class_create_with_id(self._ptr, id)

        event_class = bt2_event_class._EventClass._create_from_ptr(ec_ptr)

        if name is not None:
            event_class._name = name

        if user_attributes is not None:
            event_class._user_attributes = user_attributes

        if log_level is not None:
            event_class._log_level = log_level

        if emf_uri is not None:
            event_class._emf_uri = emf_uri

        if specific_context_field_class is not None:
            event_class._specific_context_field_class = specific_context_field_class

        if payload_field_class is not None:
            event_class._payload_field_class = payload_field_class

        return event_class

    def _user_attributes(self, user_attributes):
        value = bt2_value.create_value(user_attributes)
        native_bt.stream_class_set_user_attributes(self._ptr, value._ptr)

    _user_attributes = property(fset=_user_attributes)

    def _name(self, name):
        status = native_bt.stream_class_set_name(self._ptr, name)
        utils._handle_func_status(status, "cannot set stream class object's name")

    _name = property(fset=_name)

    def _assigns_automatic_event_class_id(self, auto_id):
        native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id)

    _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id)

    def _assigns_automatic_stream_id(self, auto_id):
        native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id)

    _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id)

    def _set_supports_packets(self, supports, with_begin_cs=False, with_end_cs=False):
        native_bt.stream_class_set_supports_packets(
            self._ptr, supports, with_begin_cs, with_end_cs
        )

    def _set_supports_discarded_events(self, supports, with_cs=False):
        native_bt.stream_class_set_supports_discarded_events(
            self._ptr, supports, with_cs
        )

    _supports_discarded_events = property(fset=_set_supports_discarded_events)

    def _set_supports_discarded_packets(self, supports, with_cs):
        native_bt.stream_class_set_supports_discarded_packets(
            self._ptr, supports, with_cs
        )

    _supports_discarded_packets = property(fset=_set_supports_discarded_packets)

    def _packet_context_field_class(self, packet_context_field_class):
        status = native_bt.stream_class_set_packet_context_field_class(
            self._ptr, packet_context_field_class._ptr
        )
        utils._handle_func_status(
            status, "cannot set stream class' packet context field class"
        )

    _packet_context_field_class = property(fset=_packet_context_field_class)

    def _event_common_context_field_class(self, event_common_context_field_class):
        set_context_fn = native_bt.stream_class_set_event_common_context_field_class
        status = set_context_fn(self._ptr, event_common_context_field_class._ptr)
        utils._handle_func_status(
            status, "cannot set stream class' event context field type"
        )

    _event_common_context_field_class = property(fset=_event_common_context_field_class)

    def _default_clock_class(self, clock_class):
        native_bt.stream_class_set_default_clock_class(self._ptr, clock_class._ptr)

    _default_clock_class = property(fset=_default_clock_class)

    @classmethod
    def _validate_create_params(
        cls,
        name,
        user_attributes,
        packet_context_field_class,
        event_common_context_field_class,
        default_clock_class,
        assigns_automatic_event_class_id,
        assigns_automatic_stream_id,
        supports_packets,
        packets_have_beginning_default_clock_snapshot,
        packets_have_end_default_clock_snapshot,
        supports_discarded_events,
        discarded_events_have_default_clock_snapshots,
        supports_discarded_packets,
        discarded_packets_have_default_clock_snapshots,
    ):
        # Name
        if name is not None:
            utils._check_str(name)

        # User attributes
        if user_attributes is not None:
            value = bt2_value.create_value(user_attributes)
            utils._check_type(value, bt2_value.MapValue)

        # Packet context field class
        if packet_context_field_class is not None:
            if not supports_packets:
                raise ValueError(
                    "cannot have a packet context field class without supporting packets"
                )

            utils._check_type(
                packet_context_field_class, bt2_field_class._StructureFieldClass
            )

        # Event common context field class
        if event_common_context_field_class is not None:
            utils._check_type(
                event_common_context_field_class, bt2_field_class._StructureFieldClass
            )

        # Default clock class
        if default_clock_class is not None:
            utils._check_type(default_clock_class, bt2_clock_class._ClockClass)

        # Assigns automatic event class id
        utils._check_bool(assigns_automatic_event_class_id)

        # Assigns automatic stream id
        utils._check_bool(assigns_automatic_stream_id)

        # Packets
        utils._check_bool(supports_packets)
        utils._check_bool(packets_have_beginning_default_clock_snapshot)
        utils._check_bool(packets_have_end_default_clock_snapshot)

        if not supports_packets:
            if packets_have_beginning_default_clock_snapshot:
                raise ValueError(
                    "cannot not support packets, but have packet beginning default clock snapshot"
                )
            if packets_have_end_default_clock_snapshot:
                raise ValueError(
                    "cannot not support packets, but have packet end default clock snapshots"
                )

        # Discarded events
        utils._check_bool(supports_discarded_events)
        utils._check_bool(discarded_events_have_default_clock_snapshots)

        if discarded_events_have_default_clock_snapshots:
            if not supports_discarded_events:
                raise ValueError(
                    "cannot not support discarded events, but have default clock snapshots for discarded event messages"
                )

            if default_clock_class is None:
                raise ValueError(
                    "cannot have no default clock class, but have default clock snapshots for discarded event messages"
                )

        # Discarded packets
        utils._check_bool(supports_discarded_packets)
        utils._check_bool(discarded_packets_have_default_clock_snapshots)

        if supports_discarded_packets and not supports_packets:
            raise ValueError(
                "cannot support discarded packets, but not support packets"
            )

        if discarded_packets_have_default_clock_snapshots:
            if not supports_discarded_packets:
                raise ValueError(
                    "cannot not support discarded packets, but have default clock snapshots for discarded packet messages"
                )

            if default_clock_class is None:
                raise ValueError(
                    "cannot have no default clock class, but have default clock snapshots for discarded packet messages"
                )