File: bt_plugin_trace_ir_test.py

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (574 lines) | stat: -rw-r--r-- 23,840 bytes parent folder | download | duplicates (2)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2019 EfficiOS Inc.
#

# pyright: strict, reportPrivateUsage=false

import math
import typing

import bt2

bt2.register_plugin(__name__, "trace-ir-test")

_FieldType = typing.TypeVar("_FieldType", bound=bt2._Field)


def c(field: bt2._Field, expected_type: typing.Type[_FieldType]) -> _FieldType:
    assert type(field) is expected_type
    return field


class AllFieldsIter(bt2._UserMessageIterator):
    def __init__(
        self,
        config: bt2._MessageIteratorConfiguration,
        output_port: bt2._UserComponentOutputPort,
    ):
        mip = self._component._graph_mip_version
        ec = typing.cast(bt2._EventClass, output_port.user_data)
        sc = ec.stream_class
        tc = sc.trace_class
        stream = tc().create_stream(sc)
        ev = self._create_event_message(ec, stream)

        payload = ev.event.payload_field
        assert payload

        payload["bool"] = False
        payload["real_single"] = 2.0
        payload["real_double"] = math.pi

        # Bit array (with flags if MIP > 0)
        c(payload["bit-array"], bt2._BitArrayField).value_as_integer = 0b1000000

        payload["int32"] = 121
        payload["int3"] = -1
        payload["int9_hex"] = -92
        payload["uint32"] = 121
        payload["uint61"] = 299792458
        payload["uint5_oct"] = 29

        # Struct
        struct = c(payload["struct"], bt2._StructureField)
        struct["str"] = "Rotisserie St-Hubert"
        struct["option_real"] = math.pi

        payload["string"] = "🎉"

        # Static array
        payload["static-array"] = ["🕰", "ðŸĶī", " 🎍"]

        # Dynamic array without length field
        payload["dynamic-array-without-length-field"] = [1.2, 2 / 3, 42.3, math.pi]

        # Dynamic array with length field
        payload["dynamic-array-with-length-field-length"] = 4
        payload["dynamic-array-with-length-field"] = [5.2, 5 / 3, 42.5, math.pi * 12]

        # Option without selector field
        payload["option-without-selector-field-with-value"] = "NORMANDIN"

        # Options with bool selector field
        payload["option-with-bool-selector-field-selector-true"] = True
        payload["option-with-bool-selector-field-selector-false"] = False
        payload["option-with-bool-selector-field-with-value"] = "Mike's"
        payload["option-with-reversed-bool-selector-field-with-value"] = "Boustan"

        # Option with unsigned integer selector field
        payload["option-with-unsigned-integer-selector-field-selector"] = 1
        payload[
            "option-with-unsigned-integer-selector-field"
        ] = "Barbies resto bar grill"

        # Option with signed integer selector field
        payload["option-with-signed-integer-selector-field-selector"] = 1
        payload["option-with-signed-integer-selector-field"] = "Pacini"

        # Variant without selector field
        variant = c(payload["variant-without-selector-field"], bt2._VariantField)
        variant.selected_option_index = 1
        variant.value = "Couche-Tard"

        # Variant with unsigned integer selector field
        c(
            payload["variant-with-unsigned-integer-selector-field-selector"],
            bt2._UnsignedIntegerField,
        ).value = 5
        variant_with_unsigned_integer_selector_field = c(
            payload["variant-with-unsigned-integer-selector-field"],
            bt2._VariantFieldWithUnsignedIntegerSelectorField,
        )
        variant_with_unsigned_integer_selector_field.selected_option_index = 0
        variant_with_unsigned_integer_selector_field.value = "Marco Smith"

        # Variant with signed integer selector field
        c(
            payload["variant-with-signed-integer-selector-field-selector"],
            bt2._SignedIntegerField,
        ).value = 5
        variant_with_signed_integer_selector_field = c(
            payload["variant-with-signed-integer-selector-field"],
            bt2._VariantFieldWithSignedIntegerSelectorField,
        )
        variant_with_signed_integer_selector_field.selected_option_index = 0
        variant_with_signed_integer_selector_field.value = "Pascal Lefebvre"

        if mip > 0:
            # Static blob
            c(payload["static-blob"], bt2._StaticBlobField).data = b"Masbourian"

            # Dynamic blob without length field
            dynamic_blob_without_length_field = c(
                payload["dynamic-blob-without-length-field"], bt2._DynamicBlobField
            )
            dynamic_blob_without_length_field.length = 3
            dynamic_blob_without_length_field.data = b"mdr"

            # Dynamic blob with length field
            c(
                payload["dynamic-blob-with-length-field-length"],
                bt2._UnsignedIntegerField,
            ).value = 5
            dynamic_blob_with_length_field = c(
                payload["dynamic-blob-with-length-field"],
                bt2._DynamicBlobFieldWithLengthField,
            )
            dynamic_blob_with_length_field.length = 5
            dynamic_blob_with_length_field.data = b"buick"

        self._msgs = [
            self._create_stream_beginning_message(stream),
            ev,
            self._create_stream_end_message(stream),
        ]

    def __next__(self):
        if len(self._msgs) > 0:
            return self._msgs.pop(0)
        else:
            raise StopIteration


@bt2.plugin_component_class
class AllFields(bt2._UserSourceComponent, message_iterator_class=AllFieldsIter):
    @staticmethod
    def _user_get_supported_mip_versions(
        params: bt2._MapValueConst, obj: object, log_level: bt2.LoggingLevel
    ):
        return [0, 1]

    def __init__(
        self,
        config: bt2._UserSourceComponentConfiguration,
        params: bt2._MapValueConst,
        obj: object,
    ):
        mip = self._graph_mip_version
        tc = self._create_trace_class()

        # Dynamic array with length field
        dynamic_array_with_length_field_length = tc.create_unsigned_integer_field_class(
            19
        )

        # Options with bool selector field
        option_with_bool_selector_field_selector_true = tc.create_bool_field_class()
        option_with_bool_selector_field_selector_false = tc.create_bool_field_class()
        option_with_bool_selector_field_selector_true_fl = (
            tc.create_field_location(
                bt2.FieldLocationScope.EVENT_PAYLOAD,
                ["option-with-bool-selector-field-selector-true"],
            )
            if mip > 0
            else None
        )
        option_with_bool_selector_field_selector_false_fl = (
            tc.create_field_location(
                bt2.FieldLocationScope.EVENT_PAYLOAD,
                ["option-with-bool-selector-field-selector-false"],
            )
            if mip > 0
            else None
        )

        # Options with integer selector field
        option_with_unsigned_integer_selector_field_selector = (
            tc.create_unsigned_integer_field_class(8)
        )
        option_with_unsigned_integer_selector_field_ranges = (
            bt2.UnsignedIntegerRangeSet([(1, 3), (18, 44)])
        )
        option_with_signed_integer_selector_field_selector = (
            tc.create_signed_integer_field_class(8)
        )
        option_with_signed_integer_selector_field_ranges = bt2.SignedIntegerRangeSet(
            [(1, 3), (18, 44)]
        )

        # Variants with integer selector field
        variant_with_unsigned_selector_field_selector = (
            tc.create_unsigned_integer_field_class(8)
        )
        variant_with_unsigned_selector_field_options = (
            (
                "var_str",
                tc.create_string_field_class(),
                bt2.UnsignedIntegerRangeSet([(5, 5)]),
            ),
        )
        variant_with_signed_selector_field_selector = (
            tc.create_signed_integer_field_class(8)
        )
        variant_with_signed_selector_field_options = (
            (
                "var_str",
                tc.create_string_field_class(),
                bt2.SignedIntegerRangeSet([(5, 5)]),
            ),
        )

        payload = tc.create_structure_field_class(
            members=(
                ("bool", tc.create_bool_field_class()),
                ("real_single", tc.create_single_precision_real_field_class()),
                ("real_double", tc.create_double_precision_real_field_class()),
                (
                    "bit-array",
                    tc.create_bit_array_field_class(
                        16,
                        flags=(
                            (
                                (
                                    "flag1",
                                    bt2.UnsignedIntegerRangeSet(((1, 3), (6, 9))),
                                ),
                                ("flag2", bt2.UnsignedIntegerRangeSet([(4, 6)])),
                            )
                            if mip > 0
                            else None
                        ),
                    ),
                ),
                ("int32", tc.create_signed_integer_field_class(32)),
                ("int3", tc.create_signed_integer_field_class(3)),
                (
                    "int9_hex",
                    tc.create_signed_integer_field_class(
                        9,
                        preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL,
                    ),
                ),
                ("uint32", tc.create_unsigned_integer_field_class(32)),
                ("uint61", tc.create_unsigned_integer_field_class(61)),
                (
                    "uint5_oct",
                    tc.create_unsigned_integer_field_class(
                        5, preferred_display_base=bt2.IntegerDisplayBase.OCTAL
                    ),
                ),
                (
                    "struct",
                    tc.create_structure_field_class(
                        members=(
                            ("str", tc.create_string_field_class()),
                            (
                                "option_real",
                                tc.create_option_field_class_without_selector_field(
                                    tc.create_double_precision_real_field_class()
                                ),
                            ),
                        )
                    ),
                ),
                ("string", tc.create_string_field_class()),
                (
                    "dynamic-array-without-length-field",
                    tc.create_dynamic_array_field_class(
                        tc.create_double_precision_real_field_class()
                    ),
                ),
                (
                    "dynamic-array-with-length-field-length",
                    dynamic_array_with_length_field_length,
                ),
                (
                    "dynamic-array-with-length-field",
                    (
                        tc.create_dynamic_array_field_class(
                            tc.create_double_precision_real_field_class(),
                            length_fc=dynamic_array_with_length_field_length,
                        )
                        if mip == 0
                        else tc.create_dynamic_array_field_class(
                            tc.create_double_precision_real_field_class(),
                            length_field_location=tc.create_field_location(
                                bt2.FieldLocationScope.EVENT_PAYLOAD, ["dyn_array_len"]
                            ),
                        )
                    ),
                ),
                (
                    "static-array",
                    tc.create_static_array_field_class(
                        tc.create_string_field_class(), 3
                    ),
                ),
                (
                    "option-without-selector-field-without-value",
                    tc.create_option_field_class_without_selector_field(
                        tc.create_double_precision_real_field_class()
                    ),
                ),
                (
                    "option-without-selector-field-with-value",
                    tc.create_option_field_class_without_selector_field(
                        tc.create_string_field_class()
                    ),
                ),
                (
                    "option-with-bool-selector-field-selector-true",
                    option_with_bool_selector_field_selector_true,
                ),
                (
                    "option-with-bool-selector-field-selector-false",
                    option_with_bool_selector_field_selector_false,
                ),
                (
                    "option-with-bool-selector-field-with-value",
                    (
                        (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_fc=option_with_bool_selector_field_selector_true,
                            )
                        )
                        if mip == 0
                        else (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_field_location=option_with_bool_selector_field_selector_true_fl,
                            )
                        )
                    ),
                ),
                (
                    "option-with-bool-selector-field-without-value",
                    (
                        (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_fc=option_with_bool_selector_field_selector_false,
                            )
                        )
                        if mip == 0
                        else (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_field_location=option_with_bool_selector_field_selector_false_fl,
                            )
                        )
                    ),
                ),
                (
                    "option-with-reversed-bool-selector-field-with-value",
                    (
                        (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_fc=option_with_bool_selector_field_selector_false,
                                selector_is_reversed=True,
                            )
                        )
                        if mip == 0
                        else (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_field_location=option_with_bool_selector_field_selector_false_fl,
                                selector_is_reversed=True,
                            )
                        )
                    ),
                ),
                (
                    "option-with-reversed-bool-selector-field-without-value",
                    (
                        (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_fc=option_with_bool_selector_field_selector_true,
                                selector_is_reversed=True,
                            )
                        )
                        if mip == 0
                        else (
                            tc.create_option_field_class_with_bool_selector_field(
                                tc.create_string_field_class(),
                                selector_field_location=option_with_bool_selector_field_selector_true_fl,
                                selector_is_reversed=True,
                            )
                        )
                    ),
                ),
                (
                    "option-with-unsigned-integer-selector-field-selector",
                    option_with_unsigned_integer_selector_field_selector,
                ),
                (
                    "option-with-unsigned-integer-selector-field",
                    (
                        (
                            tc.create_option_field_class_with_integer_selector_field(
                                tc.create_string_field_class(),
                                selector_fc=option_with_unsigned_integer_selector_field_selector,
                                ranges=option_with_unsigned_integer_selector_field_ranges,
                            )
                        )
                        if mip == 0
                        else (
                            tc.create_option_field_class_with_unsigned_integer_selector_field(
                                tc.create_string_field_class(),
                                selector_field_location=tc.create_field_location(
                                    bt2.FieldLocationScope.EVENT_PAYLOAD,
                                    [
                                        "option-with-unsigned-integer-selector-field-selector"
                                    ],
                                ),
                                ranges=option_with_unsigned_integer_selector_field_ranges,
                            )
                        )
                    ),
                ),
                (
                    "option-with-signed-integer-selector-field-selector",
                    option_with_signed_integer_selector_field_selector,
                ),
                (
                    "option-with-signed-integer-selector-field",
                    (
                        (
                            tc.create_option_field_class_with_integer_selector_field(
                                tc.create_string_field_class(),
                                selector_fc=option_with_signed_integer_selector_field_selector,
                                ranges=option_with_signed_integer_selector_field_ranges,
                            )
                        )
                        if mip == 0
                        else (
                            tc.create_option_field_class_with_signed_integer_selector_field(
                                tc.create_string_field_class(),
                                selector_field_location=tc.create_field_location(
                                    bt2.FieldLocationScope.EVENT_PAYLOAD,
                                    [
                                        "option-with-signed-integer-selector-field-selector"
                                    ],
                                ),
                                ranges=option_with_signed_integer_selector_field_ranges,
                            )
                        )
                    ),
                ),
                (
                    "variant-without-selector-field",
                    (
                        tc.create_variant_field_class
                        if mip == 0
                        else tc.create_variant_field_class_without_selector_field
                    )(
                        options=(
                            ("opt1", tc.create_string_field_class()),
                            (
                                "opt2" if mip == 0 else None,
                                tc.create_string_field_class(),
                            ),
                        )
                    ),
                ),
                (
                    "variant-with-unsigned-integer-selector-field-selector",
                    variant_with_unsigned_selector_field_selector,
                ),
                (
                    "variant-with-unsigned-integer-selector-field",
                    (
                        tc.create_variant_field_class(
                            selector_fc=variant_with_unsigned_selector_field_selector,
                            options=variant_with_unsigned_selector_field_options,
                        )
                        if mip == 0
                        else (
                            tc.create_variant_field_class_with_unsigned_integer_selector_field(
                                selector_field_location=tc.create_field_location(
                                    bt2.FieldLocationScope.EVENT_PAYLOAD,
                                    [
                                        "variant-with-unsigned-integer-selector-field-selector"
                                    ],
                                ),
                                options=variant_with_unsigned_selector_field_options,
                            )
                        )
                    ),
                ),
                (
                    "variant-with-signed-integer-selector-field-selector",
                    variant_with_signed_selector_field_selector,
                ),
                (
                    "variant-with-signed-integer-selector-field",
                    (
                        tc.create_variant_field_class(
                            selector_fc=variant_with_signed_selector_field_selector,
                            options=variant_with_signed_selector_field_options,
                        )
                        if mip == 0
                        else (
                            tc.create_variant_field_class_with_signed_integer_selector_field(
                                selector_field_location=tc.create_field_location(
                                    bt2.FieldLocationScope.EVENT_PAYLOAD,
                                    [
                                        "variant-with-signed-integer-selector-field-selector"
                                    ],
                                ),
                                options=variant_with_signed_selector_field_options,
                            )
                        )
                    ),
                ),
            )
        )

        if mip > 0:
            payload += (
                (
                    "static-blob",
                    tc.create_static_blob_field_class(
                        10, media_type="application/x-gameboy-rom"
                    ),
                ),
                (
                    "dynamic-blob-without-length-field",
                    tc.create_dynamic_blob_field_class(
                        media_type="application/x-shockwave-flash"
                    ),
                ),
                (
                    "dynamic-blob-with-length-field-length",
                    tc.create_unsigned_integer_field_class(8),
                ),
                (
                    "dynamic-blob-with-length-field",
                    tc.create_dynamic_blob_field_class(
                        length_field_location=tc.create_field_location(
                            bt2.FieldLocationScope.EVENT_PAYLOAD,
                            ["dynamic-blob-with-length-field-length"],
                        ),
                        media_type="application/vnd.wordperfect",
                    ),
                ),
            )

        self._add_output_port(
            "out",
            tc.create_stream_class().create_event_class(payload_field_class=payload),
        )