1 # The MIT License (MIT)
3 # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 import barectf
.version
as barectf_version
25 from typing
import Optional
, Any
, FrozenSet
, Mapping
, Iterator
, Set
, Union
, Callable
27 from barectf
.typing
import Count
, Alignment
, _OptStr
, Id
28 import collections
.abc
36 class ByteOrder(enum
.Enum
):
43 def alignment(self
) -> Alignment
:
44 raise NotImplementedError
47 def size_is_dynamic(self
):
51 class _BitArrayFieldType(_FieldType
):
52 def __init__(self
, size
: Count
, byte_order
: ByteOrder
, alignment
: Alignment
= Alignment(1)):
54 self
._byte
_order
= byte_order
55 self
._alignment
= alignment
58 def size(self
) -> Count
:
62 def byte_order(self
) -> ByteOrder
:
63 return self
._byte
_order
66 def alignment(self
) -> Alignment
:
67 return self
._alignment
70 class DisplayBase(enum
.Enum
):
77 class _IntegerFieldType(_BitArrayFieldType
):
78 def __init__(self
, size
: Count
, byte_order
: ByteOrder
, alignment
: Optional
[Alignment
] = None,
79 preferred_display_base
: DisplayBase
= DisplayBase
.DECIMAL
):
81 alignment
= Alignment(8 if size
% 8 == 0 else 1)
83 super().__init
__(size
, byte_order
, alignment
)
84 self
._preferred
_display
_base
= preferred_display_base
87 def preferred_display_base(self
) -> DisplayBase
:
88 return self
._preferred
_display
_base
91 class UnsignedIntegerFieldType(_IntegerFieldType
):
92 def __init__(self
, *args
):
93 super().__init
__(*args
)
94 self
._mapped
_clk
_type
_name
= None
97 class SignedIntegerFieldType(_IntegerFieldType
):
101 class EnumerationFieldTypeMappingRange
:
102 def __init__(self
, lower
: int, upper
: int):
107 def lower(self
) -> int:
111 def upper(self
) -> int:
114 def __eq__(self
, other
: Any
) -> bool:
115 if type(other
) is not type(self
):
118 return (self
._lower
, self
._upper
) == (other
._lower
, other
._upper
)
120 def __hash__(self
) -> int:
121 return hash((self
._lower
, self
._upper
))
123 def contains(self
, value
: int) -> bool:
124 return self
._lower
<= value
<= self
._upper
127 class EnumerationFieldTypeMapping
:
128 def __init__(self
, ranges
: Set
[EnumerationFieldTypeMappingRange
]):
129 self
._ranges
= frozenset(ranges
)
132 def ranges(self
) -> FrozenSet
[EnumerationFieldTypeMappingRange
]:
135 def ranges_contain_value(self
, value
: int) -> bool:
136 return any([rg
.contains(value
) for rg
in self
._ranges
])
139 _EnumFtMappings
= Mapping
[str, EnumerationFieldTypeMapping
]
142 class EnumerationFieldTypeMappings(collections
.abc
.Mapping
):
143 def __init__(self
, mappings
: _EnumFtMappings
):
144 self
._mappings
= {label
: mapping
for label
, mapping
in mappings
.items()}
146 def __getitem__(self
, key
: str) -> EnumerationFieldTypeMapping
:
147 return self
._mappings
[key
]
149 def __iter__(self
) -> Iterator
[str]:
150 return iter(self
._mappings
)
152 def __len__(self
) -> int:
153 return len(self
._mappings
)
156 class _EnumerationFieldType(_IntegerFieldType
):
157 def __init__(self
, size
: Count
, byte_order
: ByteOrder
, alignment
: Optional
[Alignment
] = None,
158 preferred_display_base
: DisplayBase
= DisplayBase
.DECIMAL
,
159 mappings
: Optional
[_EnumFtMappings
] = None):
160 super().__init
__(size
, byte_order
, alignment
, preferred_display_base
)
161 self
._mappings
= EnumerationFieldTypeMappings({})
163 if mappings
is not None:
164 self
._mappings
= EnumerationFieldTypeMappings(mappings
)
167 def mappings(self
) -> EnumerationFieldTypeMappings
:
168 return self
._mappings
170 def labels_for_value(self
, value
: int) -> Set
[str]:
173 for label
, mapping
in self
._mappings
.items():
174 if mapping
.ranges_contain_value(value
):
180 class UnsignedEnumerationFieldType(_EnumerationFieldType
, UnsignedIntegerFieldType
):
184 class SignedEnumerationFieldType(_EnumerationFieldType
, SignedIntegerFieldType
):
188 class RealFieldType(_BitArrayFieldType
):
192 class StringFieldType(_FieldType
):
194 def alignment(self
) -> Alignment
:
198 def size_is_dynamic(self
):
202 class _ArrayFieldType(_FieldType
):
203 def __init__(self
, element_field_type
: _FieldType
):
204 self
._element
_field
_type
= element_field_type
207 def element_field_type(self
) -> _FieldType
:
208 return self
._element
_field
_type
211 def alignment(self
) -> Alignment
:
212 return self
._element
_field
_type
.alignment
215 class StaticArrayFieldType(_ArrayFieldType
):
216 def __init__(self
, length
: Count
, element_field_type
: _FieldType
):
217 super().__init
__(element_field_type
)
218 self
._length
= length
221 def length(self
) -> Count
:
225 class StructureFieldTypeMember
:
226 def __init__(self
, field_type
: _FieldType
):
227 self
._field
_type
= field_type
230 def field_type(self
) -> _FieldType
:
231 return self
._field
_type
234 _StructFtMembers
= Mapping
[str, StructureFieldTypeMember
]
237 class StructureFieldTypeMembers(collections
.abc
.Mapping
):
238 def __init__(self
, members
: _StructFtMembers
):
239 self
._members
= collections
.OrderedDict()
241 for name
, member
in members
.items():
242 assert type(member
) is StructureFieldTypeMember
243 self
._members
[name
] = member
245 def __getitem__(self
, key
: str) -> StructureFieldTypeMember
:
246 return self
._members
[key
]
248 def __iter__(self
) -> Iterator
[str]:
249 return iter(self
._members
)
251 def __len__(self
) -> int:
252 return len(self
._members
)
255 class StructureFieldType(_FieldType
):
256 def __init__(self
, minimum_alignment
: Alignment
= Alignment(1),
257 members
: Optional
[_StructFtMembers
] = None):
258 self
._minimum
_alignment
= minimum_alignment
259 self
._members
= StructureFieldTypeMembers({})
261 if members
is not None:
262 self
._members
= StructureFieldTypeMembers(members
)
264 self
._set
_alignment
()
266 def _set_alignment(self
):
267 self
._alignment
: Alignment
= self
._minimum
_alignment
269 for member
in self
._members
.values():
270 if member
.field_type
.alignment
> self
._alignment
:
271 self
._alignment
= member
.field_type
.alignment
274 def minimum_alignment(self
) -> Alignment
:
275 return self
._minimum
_alignment
278 def alignment(self
) -> Alignment
:
279 return self
._alignment
282 def size_is_dynamic(self
):
283 return any([member
.field_type
.size_is_dynamic
for member
in self
.members
.values()])
286 def members(self
) -> StructureFieldTypeMembers
:
293 def __eq__(self
, other
: Any
) -> bool:
294 if type(other
) is not type(self
):
297 return self
._name
== other
._name
299 def __lt__(self
, other
: '_UniqueByName'):
300 assert type(self
) is type(other
)
301 return self
._name
< other
._name
303 def __hash__(self
) -> int:
304 return hash(self
._name
)
307 _OptFt
= Optional
[_FieldType
]
308 _OptStructFt
= Optional
[StructureFieldType
]
309 LogLevel
= typing
.NewType('LogLevel', int)
312 class EventType(_UniqueByName
):
313 def __init__(self
, name
: str, log_level
: Optional
[LogLevel
] = None,
314 specific_context_field_type
: _OptStructFt
= None, payload_field_type
: _OptStructFt
= None):
315 self
._id
: Optional
[Id
] = None
317 self
._log
_level
= log_level
318 self
._specific
_context
_field
_type
= specific_context_field_type
319 self
._payload
_field
_type
= payload_field_type
322 def id(self
) -> Optional
[Id
]:
326 def name(self
) -> str:
330 def log_level(self
) -> Optional
[LogLevel
]:
331 return self
._log
_level
334 def specific_context_field_type(self
) -> _OptStructFt
:
335 return self
._specific
_context
_field
_type
338 def payload_field_type(self
) -> _OptStructFt
:
339 return self
._payload
_field
_type
342 class ClockTypeOffset
:
343 def __init__(self
, seconds
: int = 0, cycles
: Count
= Count(0)):
344 self
._seconds
= seconds
345 self
._cycles
= cycles
348 def seconds(self
) -> int:
352 def cycles(self
) -> Count
:
356 _OptUuid
= Optional
[uuidp
.UUID
]
359 class ClockType(_UniqueByName
):
360 def __init__(self
, name
: str, frequency
: Count
= Count(int(1e9
)), uuid
: _OptUuid
= None,
361 description
: _OptStr
= None, precision
: Count
= Count(0),
362 offset
: Optional
[ClockTypeOffset
] = None, origin_is_unix_epoch
: bool = False):
364 self
._frequency
= frequency
366 self
._description
= description
367 self
._precision
= precision
368 self
._offset
= ClockTypeOffset()
370 if offset
is not None:
371 self
._offset
= offset
373 self
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
376 def name(self
) -> str:
380 def frequency(self
) -> Count
:
381 return self
._frequency
384 def uuid(self
) -> _OptUuid
:
388 def description(self
) -> _OptStr
:
389 return self
._description
392 def precision(self
) -> Count
:
393 return self
._precision
396 def offset(self
) -> ClockTypeOffset
:
400 def origin_is_unix_epoch(self
) -> bool:
401 return self
._origin
_is
_unix
_epoch
404 DEFAULT_FIELD_TYPE
= 'default'
405 _DefaultableUIntFt
= Union
[str, UnsignedIntegerFieldType
]
406 _OptDefaultableUIntFt
= Optional
[_DefaultableUIntFt
]
407 _OptUIntFt
= Optional
[UnsignedIntegerFieldType
]
410 class StreamTypePacketFeatures
:
411 def __init__(self
, total_size_field_type
: _DefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
412 content_size_field_type
: _DefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
413 beginning_time_field_type
: _OptDefaultableUIntFt
= None,
414 end_time_field_type
: _OptDefaultableUIntFt
= None,
415 discarded_events_counter_field_type
: _OptDefaultableUIntFt
= None,
416 default_byte_order
: Optional
[ByteOrder
] = None):
417 def get_ft(user_ft
: _OptDefaultableUIntFt
) -> _OptUIntFt
:
418 if user_ft
== DEFAULT_FIELD_TYPE
:
419 assert default_byte_order
is not None
420 return UnsignedIntegerFieldType(64, typing
.cast(ByteOrder
, default_byte_order
))
422 return typing
.cast(_OptUIntFt
, user_ft
)
424 self
._total
_size
_field
_type
= get_ft(total_size_field_type
)
425 self
._content
_size
_field
_type
= get_ft(content_size_field_type
)
426 self
._beginning
_time
_field
_type
= get_ft(beginning_time_field_type
)
427 self
._end
_time
_field
_type
= get_ft(end_time_field_type
)
428 self
._discarded
_events
_counter
_field
_type
= get_ft(discarded_events_counter_field_type
)
431 def total_size_field_type(self
) -> _OptUIntFt
:
432 return self
._total
_size
_field
_type
435 def content_size_field_type(self
) -> _OptUIntFt
:
436 return self
._content
_size
_field
_type
439 def beginning_time_field_type(self
) -> _OptUIntFt
:
440 return self
._beginning
_time
_field
_type
443 def end_time_field_type(self
) -> _OptUIntFt
:
444 return self
._end
_time
_field
_type
447 def discarded_events_counter_field_type(self
) -> _OptUIntFt
:
448 return self
._discarded
_events
_counter
_field
_type
451 class StreamTypeEventFeatures
:
452 def __init__(self
, type_id_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
453 time_field_type
: _OptDefaultableUIntFt
= None,
454 default_byte_order
: Optional
[ByteOrder
] = None):
455 def get_ft(user_ft
: _OptDefaultableUIntFt
) -> _OptUIntFt
:
456 if user_ft
== DEFAULT_FIELD_TYPE
:
457 assert default_byte_order
is not None
458 return UnsignedIntegerFieldType(64, typing
.cast(ByteOrder
, default_byte_order
))
460 return typing
.cast(_OptUIntFt
, user_ft
)
462 self
._type
_id
_field
_type
= get_ft(type_id_field_type
)
463 self
._time
_field
_type
= get_ft(time_field_type
)
466 def type_id_field_type(self
) -> _OptUIntFt
:
467 return self
._type
_id
_field
_type
470 def time_field_type(self
) -> _OptUIntFt
:
471 return self
._time
_field
_type
474 class StreamTypeFeatures
:
475 def __init__(self
, packet_features
: Optional
[StreamTypePacketFeatures
] = None,
476 event_features
: Optional
[StreamTypeEventFeatures
] = None,
477 default_byte_order
: Optional
[ByteOrder
] = None):
478 if packet_features
is None:
479 self
._packet
_features
= StreamTypePacketFeatures(default_byte_order
=default_byte_order
)
481 self
._packet
_features
= packet_features
483 if event_features
is None:
484 self
._event
_features
= StreamTypeEventFeatures(default_byte_order
=default_byte_order
)
486 self
._event
_features
= event_features
489 def packet_features(self
) -> StreamTypePacketFeatures
:
490 return self
._packet
_features
493 def event_features(self
) -> StreamTypeEventFeatures
:
494 return self
._event
_features
497 class StreamType(_UniqueByName
):
498 def __init__(self
, name
: str, event_types
: Set
[EventType
],
499 default_clock_type
: Optional
[ClockType
] = None,
500 features
: Optional
[StreamTypeFeatures
] = None,
501 default_feature_field_type_byte_order
: Optional
[ByteOrder
] = None,
502 packet_context_field_type_extra_members
: Optional
[_StructFtMembers
] = None,
503 event_common_context_field_type
: _OptStructFt
= None):
504 self
._id
: Optional
[Id
] = None
506 self
._default
_clock
_type
= default_clock_type
507 self
._event
_common
_context
_field
_type
= event_common_context_field_type
508 self
._event
_types
= frozenset(event_types
)
511 for index
, ev_type
in enumerate(sorted(self
._event
_types
, key
=lambda evt
: evt
.name
)):
512 assert ev_type
._id
is None
513 ev_type
._id
= Id(index
)
515 self
._set
_features
(features
, default_feature_field_type_byte_order
)
516 self
._packet
_context
_field
_type
_extra
_members
= StructureFieldTypeMembers({})
518 if packet_context_field_type_extra_members
is not None:
519 self
._packet
_context
_field
_type
_extra
_members
= StructureFieldTypeMembers(packet_context_field_type_extra_members
)
521 self
._set
_pkt
_ctx
_ft
()
522 self
._set
_ev
_header
_ft
()
524 def _set_features(self
, features
: Optional
[StreamTypeFeatures
],
525 default_byte_order
: Optional
[ByteOrder
]):
526 if features
is not None:
527 self
._features
= features
531 pkt_beginning_time_ft
= None
532 pkt_end_time_ft
= None
534 if self
._default
_clock
_type
is not None:
535 # Automatic time field types because the stream type has a
536 # default clock type.
537 ev_time_ft
= DEFAULT_FIELD_TYPE
538 pkt_beginning_time_ft
= DEFAULT_FIELD_TYPE
539 pkt_end_time_ft
= DEFAULT_FIELD_TYPE
541 self
._features
= StreamTypeFeatures(StreamTypePacketFeatures(beginning_time_field_type
=pkt_beginning_time_ft
,
542 end_time_field_type
=pkt_end_time_ft
,
543 default_byte_order
=default_byte_order
),
544 StreamTypeEventFeatures(time_field_type
=ev_time_ft
,
545 default_byte_order
=default_byte_order
))
547 def _set_ft_mapped_clk_type_name(self
, ft
: Optional
[UnsignedIntegerFieldType
]):
551 if self
._default
_clock
_type
is not None:
552 assert isinstance(ft
, UnsignedIntegerFieldType
)
553 ft
._mapped
_clk
_type
_name
= self
._default
_clock
_type
.name
555 def _set_pkt_ctx_ft(self
):
558 def add_member_if_exists(name
: str, ft
: _FieldType
, set_mapped_clk_type_name
: bool = False):
562 if set_mapped_clk_type_name
:
563 self
._set
_ft
_mapped
_clk
_type
_name
(typing
.cast(UnsignedIntegerFieldType
, ft
))
565 members
[name
] = StructureFieldTypeMember(ft
)
567 members
= collections
.OrderedDict([
570 StructureFieldTypeMember(self
._features
.packet_features
.total_size_field_type
)
574 StructureFieldTypeMember(self
._features
.packet_features
.content_size_field_type
)
578 add_member_if_exists('timestamp_begin',
579 self
._features
.packet_features
.beginning_time_field_type
, True)
580 add_member_if_exists('timestamp_end', self
._features
.packet_features
.end_time_field_type
,
582 add_member_if_exists('events_discarded',
583 self
._features
.packet_features
.discarded_events_counter_field_type
)
585 if self
._packet
_context
_field
_type
_extra
_members
is not None:
586 for name
, field_type
in self
._packet
_context
_field
_type
_extra
_members
.items():
587 assert name
not in members
588 members
[name
] = field_type
590 self
._pkt
_ctx
_ft
= StructureFieldType(8, members
)
592 def _set_ev_header_ft(self
):
593 members
= collections
.OrderedDict()
595 if self
._features
.event_features
.type_id_field_type
is not None:
596 members
['id'] = StructureFieldTypeMember(self
._features
.event_features
.type_id_field_type
)
598 if self
._features
.event_features
.time_field_type
is not None:
599 ft
= self
._features
.event_features
.time_field_type
600 self
._set
_ft
_mapped
_clk
_type
_name
(ft
)
601 members
['timestamp'] = StructureFieldTypeMember(ft
)
603 self
._ev
_header
_ft
= StructureFieldType(8, members
)
606 def id(self
) -> Optional
[Id
]:
610 def name(self
) -> str:
614 def default_clock_type(self
) -> Optional
[ClockType
]:
615 return self
._default
_clock
_type
618 def features(self
) -> StreamTypeFeatures
:
619 return self
._features
622 def packet_context_field_type_extra_members(self
) -> StructureFieldTypeMembers
:
623 return self
._packet
_context
_field
_type
_extra
_members
626 def event_common_context_field_type(self
) -> _OptStructFt
:
627 return self
._event
_common
_context
_field
_type
630 def event_types(self
) -> FrozenSet
[EventType
]:
631 return self
._event
_types
634 _OptUuidFt
= Optional
[Union
[str, StaticArrayFieldType
]]
637 class TraceTypeFeatures
:
638 def __init__(self
, magic_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
639 uuid_field_type
: _OptUuidFt
= None,
640 stream_type_id_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
641 default_byte_order
: Optional
[ByteOrder
] = None):
642 def get_field_type(user_ft
: Optional
[Union
[str, _FieldType
]],
643 create_default_ft
: Callable
[[], _FieldType
]) -> _OptFt
:
644 if user_ft
== DEFAULT_FIELD_TYPE
:
645 return create_default_ft()
647 return typing
.cast(_OptFt
, user_ft
)
649 def create_default_magic_ft():
650 assert default_byte_order
is not None
651 return UnsignedIntegerFieldType(32, default_byte_order
)
653 def create_default_uuid_ft():
654 assert default_byte_order
is not None
655 return StaticArrayFieldType(Count(16), UnsignedIntegerFieldType(8, default_byte_order
))
657 def create_default_stream_type_id_ft():
658 assert default_byte_order
is not None
659 return UnsignedIntegerFieldType(64, default_byte_order
)
661 self
._magic
_field
_type
= typing
.cast(_OptUIntFt
, get_field_type(magic_field_type
, create_default_magic_ft
))
662 self
._uuid
_field
_type
= typing
.cast(Optional
[StaticArrayFieldType
],
663 get_field_type(uuid_field_type
, create_default_uuid_ft
))
664 self
._stream
_type
_id
_field
_type
= typing
.cast(_OptUIntFt
,
665 get_field_type(stream_type_id_field_type
,
666 create_default_stream_type_id_ft
))
669 def magic_field_type(self
) -> _OptUIntFt
:
670 return self
._magic
_field
_type
673 def uuid_field_type(self
) -> Optional
[StaticArrayFieldType
]:
674 return self
._uuid
_field
_type
677 def stream_type_id_field_type(self
) -> _OptUIntFt
:
678 return self
._stream
_type
_id
_field
_type
682 def __init__(self
, stream_types
: Set
[StreamType
], uuid
: _OptUuid
= None,
683 features
: Optional
[TraceTypeFeatures
] = None,
684 default_feature_field_type_byte_order
: Optional
[ByteOrder
] = None):
685 self
._stream
_types
= frozenset(stream_types
)
688 for index
, stream_type
in enumerate(sorted(self
._stream
_types
, key
=lambda st
: st
.name
)):
689 assert stream_type
._id
is None
690 stream_type
._id
= Id(index
)
693 self
._set
_features
(features
, default_feature_field_type_byte_order
)
694 self
._set
_pkt
_header
_ft
()
696 def _set_features(self
, features
: Optional
[TraceTypeFeatures
],
697 default_byte_order
: Optional
[ByteOrder
]):
698 if features
is not None:
699 self
._features
= features
702 # automatic UUID field type because the trace type has a UUID
703 uuid_ft
= None if self
._uuid
is None else DEFAULT_FIELD_TYPE
704 self
._features
= TraceTypeFeatures(uuid_field_type
=uuid_ft
,
705 default_byte_order
=default_byte_order
)
707 def _set_pkt_header_ft(self
):
708 members
= collections
.OrderedDict()
710 def add_member_if_exists(name
: str, ft
: _OptFt
):
714 members
[name
] = StructureFieldTypeMember(ft
)
716 add_member_if_exists('magic', self
._features
.magic_field_type
)
717 add_member_if_exists('uuid', self
._features
.uuid_field_type
)
718 add_member_if_exists('stream_id', self
._features
.stream_type_id_field_type
)
719 self
._pkt
_header
_ft
= StructureFieldType(8, members
)
722 def uuid(self
) -> _OptUuid
:
726 def stream_types(self
) -> FrozenSet
[StreamType
]:
727 return self
._stream
_types
729 def stream_type(self
, name
: str) -> Optional
[StreamType
]:
730 for cand_stream_type
in self
._stream
_types
:
731 if cand_stream_type
.name
== name
:
732 return cand_stream_type
737 def features(self
) -> TraceTypeFeatures
:
738 return self
._features
741 def clock_types(self
) -> Set
[ClockType
]:
744 for stream_type
in self
._stream
_types
:
745 if stream_type
.default_clock_type
is not None:
746 clk_types
.add(stream_type
.default_clock_type
)
751 _EnvEntry
= Union
[str, int]
752 _EnvEntries
= Mapping
[str, _EnvEntry
]
755 class TraceEnvironment(collections
.abc
.Mapping
):
756 def __init__(self
, environment
: _EnvEntries
):
757 self
._env
= {name
: value
for name
, value
in environment
.items()}
759 def __getitem__(self
, key
: str) -> _EnvEntry
:
760 return self
._env
[key
]
762 def __iter__(self
) -> Iterator
[str]:
763 return iter(self
._env
)
765 def __len__(self
) -> int:
766 return len(self
._env
)
770 def __init__(self
, type: TraceType
, environment
: Optional
[_EnvEntries
] = None):
772 self
._set
_env
(environment
)
774 def _set_env(self
, environment
: Optional
[_EnvEntries
]):
775 init_env
= collections
.OrderedDict([
777 ('tracer_name', 'barectf'),
778 ('tracer_major', barectf_version
.__major
_version
__),
779 ('tracer_minor', barectf_version
.__minor
_version
__),
780 ('tracer_patch', barectf_version
.__patch
_version
__),
781 ('barectf_gen_date', str(datetime
.datetime
.now().isoformat())),
784 if environment
is None:
787 init_env
.update(environment
)
788 self
._env
= TraceEnvironment(typing
.cast(_EnvEntries
, init_env
))
791 def type(self
) -> TraceType
:
795 def environment(self
) -> TraceEnvironment
:
799 _ClkTypeCTypes
= Mapping
[ClockType
, str]
802 class ClockTypeCTypes(collections
.abc
.Mapping
):
803 def __init__(self
, c_types
: _ClkTypeCTypes
):
804 self
._c
_types
= {clk_type
: c_type
for clk_type
, c_type
in c_types
.items()}
806 def __getitem__(self
, key
: ClockType
) -> str:
807 return self
._c
_types
[key
]
809 def __iter__(self
) -> Iterator
[ClockType
]:
810 return iter(self
._c
_types
)
812 def __len__(self
) -> int:
813 return len(self
._c
_types
)
816 class ConfigurationCodeGenerationHeaderOptions
:
817 def __init__(self
, identifier_prefix_definition
: bool = False,
818 default_stream_type_name_definition
: bool = False):
819 self
._identifier
_prefix
_definition
= identifier_prefix_definition
820 self
._default
_stream
_type
_name
_definition
= default_stream_type_name_definition
823 def identifier_prefix_definition(self
) -> bool:
824 return self
._identifier
_prefix
_definition
827 def default_stream_type_name_definition(self
) -> bool:
828 return self
._default
_stream
_type
_name
_definition
831 class ConfigurationCodeGenerationOptions
:
832 def __init__(self
, identifier_prefix
: str = 'barectf_', file_name_prefix
: str = 'barectf',
833 default_stream_type
: Optional
[StreamType
] = None,
834 header_options
: Optional
[ConfigurationCodeGenerationHeaderOptions
] = None,
835 clock_type_c_types
: Optional
[_ClkTypeCTypes
] = None):
836 self
._identifier
_prefix
= identifier_prefix
837 self
._file
_name
_prefix
= file_name_prefix
838 self
._default
_stream
_type
= default_stream_type
840 self
._header
_options
= ConfigurationCodeGenerationHeaderOptions()
842 if header_options
is not None:
843 self
._header
_options
= header_options
845 self
._clock
_type
_c
_types
= ClockTypeCTypes({})
847 if clock_type_c_types
is not None:
848 self
._clock
_type
_c
_types
= ClockTypeCTypes(clock_type_c_types
)
851 def identifier_prefix(self
) -> str:
852 return self
._identifier
_prefix
855 def file_name_prefix(self
) -> str:
856 return self
._file
_name
_prefix
859 def default_stream_type(self
) -> Optional
[StreamType
]:
860 return self
._default
_stream
_type
863 def header_options(self
) -> ConfigurationCodeGenerationHeaderOptions
:
864 return self
._header
_options
867 def clock_type_c_types(self
) -> ClockTypeCTypes
:
868 return self
._clock
_type
_c
_types
871 class ConfigurationOptions
:
873 code_generation_options
: Optional
[ConfigurationCodeGenerationOptions
] = None):
874 self
._code
_generation
_options
= ConfigurationCodeGenerationOptions()
876 if code_generation_options
is not None:
877 self
._code
_generation
_options
= code_generation_options
880 def code_generation_options(self
) -> ConfigurationCodeGenerationOptions
:
881 return self
._code
_generation
_options
885 def __init__(self
, trace
: Trace
, target_byte_order
: ByteOrder
,
886 options
: Optional
[ConfigurationOptions
] = None):
888 self
._options
= ConfigurationOptions()
889 self
._target
_byte
_order
= target_byte_order
891 if options
is not None:
892 self
._options
= options
894 clk_type_c_types
= self
._options
.code_generation_options
.clock_type_c_types
896 for stream_type
in trace
.type.stream_types
:
897 def_clk_type
= stream_type
.default_clock_type
899 if def_clk_type
is None:
902 if def_clk_type
not in clk_type_c_types
:
903 clk_type_c_types
._c
_types
[def_clk_type
] = 'uint32_t'
906 def trace(self
) -> Trace
:
910 def target_byte_order(self
):
911 return self
._target
_byte
_order
914 def options(self
) -> ConfigurationOptions
: