Skip to content

Commit

Permalink
Extends TEF report and fixes error handling in perf
Browse files Browse the repository at this point in the history
  • Loading branch information
vulder committed Nov 27, 2023
1 parent 8eed404 commit cafb085
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions varats-core/varats/report/linux_perf_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def __parse_ctx_switches(line: str) -> int:
return int(line.split(" ")[0].replace(",", ""))

@staticmethod
def __parse_branch_misses(line: str) -> tp.Optional[int]:
def __parse_branch_misses(line: str) -> int:
if line.startswith("<not counted>"):
return None
return np.NaN
return int(line.split(" ")[0].replace(",", ""))

@property
Expand Down
46 changes: 43 additions & 3 deletions varats-core/varats/report/tef_report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Report module to create and handle trace event format files, e.g., created
with chrome tracing."""

import logging
import re
import typing as tp
from enum import Enum
Expand All @@ -11,6 +12,8 @@
from varats.experiment.workload_util import WorkloadSpecificReportAggregate
from varats.report.report import BaseReport, ReportAggregate

LOG = logging.getLogger(__name__)


class TraceEventType(Enum):
"""Enum to represent the different event types of trace format events,
Expand Down Expand Up @@ -49,6 +52,8 @@ class TraceEvent():
"""Represents a trace event that was captured during the analysis of a
target program."""

__uuid: int

def __init__(
self, json_trace_event: tp.Dict[str, tp.Any], name_id: int,
name_id_mapper: 'TEFReport.NameIDMapper'
Expand All @@ -63,6 +68,14 @@ def __init__(
self.__pid = int(json_trace_event["pid"])
self.__tid = int(json_trace_event["tid"])

if "UUID" in json_trace_event:
self.__uuid = int(json_trace_event["UUID"])
elif "ID" in json_trace_event:
self.__uuid = int(json_trace_event["ID"])
else:
LOG.critical("Could not parse UUID/ID from trace event")
self.__uuid: int = 0

@property
def name(self) -> str:
return self.__name_id_mapper.infer_name(self.__name_id)
Expand All @@ -87,9 +100,14 @@ def pid(self) -> int:
def tid(self) -> int:
return self.__tid

@property
def uuid(self) -> int:
return self.__uuid

def __str__(self) -> str:
return f"""{{
name: {self.name}
uuid: {self.uuid}
cat: {self.category}
ph: {self.event_type}
ts: {self.timestamp}
Expand All @@ -99,7 +117,7 @@ def __str__(self) -> str:
"""

def __repr__(self) -> str:
return str(self)
return f"{{ name={self.name}, uuid={self.uuid} }}"


class TEFReport(BaseReport, shorthand="TEF", file_type="json"):
Expand All @@ -114,7 +132,11 @@ def infer_name(self, name_id: int) -> str:
def __init__(self, path: Path) -> None:
super().__init__(path)
self.__name_id_mapper: TEFReport.NameIDMapper = TEFReport.NameIDMapper()
self._parse_json()
try:
self._parse_json()
except Exception as e:
print(f"Could not parse file: {self.path}")
raise e
# Parsing stackFrames is currently not implemented
# x = data["stackFrames"]

Expand All @@ -132,8 +154,26 @@ def stack_frames(self) -> None:
"Stack frame parsing is currently not implemented!"
)

def _patch_errors_from_file(self) -> None:
with open(self.path, "r") as f:
data = f.read()

with open(self.path, "w") as f:
remove_lost_events = re.compile('Lost \d+ events')
for line in data.splitlines():
if "Lost" in line:
LOG.error(
"Events where lost during tracing, patching json file."
)
line = remove_lost_events.sub("", line)

f.write(line)

def _parse_json(self) -> None:
trace_events: tp.List[TraceEvent] = list()
trace_events: tp.List[TraceEvent] = []

self._patch_errors_from_file()

with open(self.path, "rb") as f:
parser = ijson.parse(f)
trace_event: tp.Dict[str, str] = {}
Expand Down

0 comments on commit cafb085

Please sign in to comment.