Extract trace reading functions
This commit is contained in:
parent
4782b27aa9
commit
352690044b
5 changed files with 11 additions and 49 deletions
|
@ -9,7 +9,7 @@
|
|||
<author email="ingo.luetkebohle@de.bosch.com">Ingo Luetkebohle</author>
|
||||
<author email="fixed-term.christophe.bourquebedard@de.bosch.com">Christophe Bedard</author>
|
||||
|
||||
<exec_depend>python3-babeltrace</exec_depend>
|
||||
<exec_depend>tracetools_read</exec_depend>
|
||||
|
||||
<test_depend>ament_copyright</test_depend>
|
||||
<test_depend>ament_flake8</test_depend>
|
||||
|
|
|
@ -5,9 +5,10 @@ from typing import Callable
|
|||
from typing import Dict
|
||||
from typing import List
|
||||
|
||||
from tracetools_read.utils import get_event_name
|
||||
from tracetools_read.utils import get_field
|
||||
|
||||
from .lttng_models import EventMetadata
|
||||
from .lttng_models import get_field
|
||||
from .lttng_models import get_name
|
||||
|
||||
|
||||
class EventHandler():
|
||||
|
@ -31,7 +32,7 @@ class EventHandler():
|
|||
self._handle(event)
|
||||
|
||||
def _handle(self, event: Dict[str, str]) -> None:
|
||||
event_name = get_name(event)
|
||||
event_name = get_event_name(event)
|
||||
handler_function = self._handler_map.get(event_name, None)
|
||||
if handler_function is not None:
|
||||
pid = get_field(
|
||||
|
|
|
@ -1,20 +1,5 @@
|
|||
# Model objects for LTTng traces/events
|
||||
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
|
||||
|
||||
def get_field(event: Dict, field_name: str, default=None, raise_if_not_found=True) -> Any:
|
||||
field_value = event.get(field_name, default)
|
||||
# If enabled, raise exception as soon as possible to avoid headaches
|
||||
if raise_if_not_found and field_value is None:
|
||||
raise AttributeError(f'event field "{field_name}" not found!')
|
||||
return field_value
|
||||
|
||||
|
||||
def get_name(event: Dict) -> str:
|
||||
return get_field(event, '_name')
|
||||
|
||||
|
||||
class EventMetadata():
|
||||
"""Container for event metadata."""
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
from typing import Dict
|
||||
from typing import List
|
||||
|
||||
from tracetools_read.utils import get_field
|
||||
|
||||
from .data_model import DataModel
|
||||
from .handler import EventHandler
|
||||
from .lttng_models import get_field
|
||||
from .lttng_models import EventMetadata
|
||||
|
||||
|
||||
|
|
|
@ -2,14 +2,7 @@
|
|||
|
||||
from pickle import Pickler
|
||||
|
||||
import babeltrace
|
||||
|
||||
# List of ignored CTF fields
|
||||
_IGNORED_FIELDS = [
|
||||
'content_size', 'cpu_id', 'events_discarded', 'id', 'packet_size', 'packet_seq_num',
|
||||
'stream_id', 'stream_instance_id', 'timestamp_end', 'timestamp_begin', 'magic', 'uuid', 'v'
|
||||
]
|
||||
_DISCARD = 'events_discarded'
|
||||
from tracetools_read import utils
|
||||
|
||||
|
||||
def ctf_to_pickle(trace_directory: str, target: Pickler) -> int:
|
||||
|
@ -21,9 +14,8 @@ def ctf_to_pickle(trace_directory: str, target: Pickler) -> int:
|
|||
:return: the number of events written
|
||||
"""
|
||||
# add traces
|
||||
tc = babeltrace.TraceCollection()
|
||||
print(f'Importing trace directory: {trace_directory}')
|
||||
tc.add_traces_recursive(trace_directory, 'ctf')
|
||||
ctf_events = utils._get_trace_ctf_events(trace_directory)
|
||||
|
||||
count = 0
|
||||
count_written = 0
|
||||
|
@ -31,7 +23,7 @@ def ctf_to_pickle(trace_directory: str, target: Pickler) -> int:
|
|||
# traced = set()
|
||||
|
||||
# PID_KEYS = ['vpid', 'pid']
|
||||
for event in tc.events:
|
||||
for event in ctf_events:
|
||||
count += 1
|
||||
# pid = None
|
||||
# for key in PID_KEYS:
|
||||
|
@ -40,25 +32,8 @@ def ctf_to_pickle(trace_directory: str, target: Pickler) -> int:
|
|||
# break
|
||||
|
||||
# Write all for now
|
||||
pod = _ctf_event_to_pod(event)
|
||||
pod = utils.event_to_dict(event)
|
||||
target.dump(pod)
|
||||
count_written += 1
|
||||
|
||||
return count_written
|
||||
|
||||
|
||||
def _ctf_event_to_pod(ctf_event):
|
||||
"""
|
||||
Convert name, timestamp, and all other keys except those in IGNORED_FIELDS into a dictionary.
|
||||
|
||||
:param ctf_element: The element to convert
|
||||
:type ctf_element: babeltrace.Element
|
||||
:return:
|
||||
:return type: dict
|
||||
"""
|
||||
pod = {'_name': ctf_event.name, '_timestamp': ctf_event.timestamp}
|
||||
if hasattr(ctf_event, _DISCARD) and ctf_event[_DISCARD] > 0:
|
||||
print(ctf_event[_DISCARD])
|
||||
for key in [key for key in ctf_event.keys() if key not in _IGNORED_FIELDS]:
|
||||
pod[key] = ctf_event[key]
|
||||
return pod
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue