mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-02-12 07:59:04 +00:00
Merge pull request #441 from setrofim/trace-cmd-optimization
Optimize parsing of trace-cmd output
This commit is contained in:
commit
87972e2605
@ -41,7 +41,22 @@ class TraceCmdEvent(object):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ['thread', 'reporting_cpu_id', 'timestamp', 'name', 'text', 'fields']
|
__slots__ = ['thread', 'reporting_cpu_id', 'timestamp', 'name', 'text', '_fields', '_parser']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fields(self):
|
||||||
|
if self._fields is not None:
|
||||||
|
return self._fields
|
||||||
|
self._fields = {}
|
||||||
|
|
||||||
|
if self._parser:
|
||||||
|
try:
|
||||||
|
self._parser(self, self.text)
|
||||||
|
except Exception: # pylint: disable=broad-except
|
||||||
|
# unknown format assume user does not care or know how to
|
||||||
|
# parse self.text
|
||||||
|
pass
|
||||||
|
return self._fields
|
||||||
|
|
||||||
def __init__(self, thread, cpu_id, ts, name, body, parser=None):
|
def __init__(self, thread, cpu_id, ts, name, body, parser=None):
|
||||||
"""
|
"""
|
||||||
@ -70,15 +85,8 @@ class TraceCmdEvent(object):
|
|||||||
self.timestamp = numeric(ts)
|
self.timestamp = numeric(ts)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.text = body
|
self.text = body
|
||||||
self.fields = {}
|
self._fields = None
|
||||||
|
self._parser = parser
|
||||||
if parser:
|
|
||||||
try:
|
|
||||||
parser(self, self.text)
|
|
||||||
except Exception: # pylint: disable=broad-except
|
|
||||||
# unknown format assume user does not care or know how to
|
|
||||||
# parse self.text
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
try:
|
try:
|
||||||
@ -143,7 +151,7 @@ def default_body_parser(event, text):
|
|||||||
v = int(v)
|
v = int(v)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
event.fields[k] = v
|
event._fields[k] = v
|
||||||
|
|
||||||
|
|
||||||
def regex_body_parser(regex, flags=0):
|
def regex_body_parser(regex, flags=0):
|
||||||
@ -166,9 +174,9 @@ def regex_body_parser(regex, flags=0):
|
|||||||
if match:
|
if match:
|
||||||
for k, v in match.groupdict().iteritems():
|
for k, v in match.groupdict().iteritems():
|
||||||
try:
|
try:
|
||||||
event.fields[k] = int(v)
|
event._fields[k] = int(v)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
event.fields[k] = v
|
event._fields[k] = v
|
||||||
|
|
||||||
return regex_parser_func
|
return regex_parser_func
|
||||||
|
|
||||||
@ -224,9 +232,6 @@ EVENT_PARSER_MAP = {
|
|||||||
'sched_wakeup_new': sched_wakeup_parser,
|
'sched_wakeup_new': sched_wakeup_parser,
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE_EVENT_REGEX = re.compile(r'^\s+(?P<thread>\S+.*?\S+)\s+\[(?P<cpu_id>\d+)\]\s+(?P<ts>[\d.]+):\s+'
|
|
||||||
r'(?P<name>[^:]+):\s+(?P<body>.*?)\s*$')
|
|
||||||
|
|
||||||
HEADER_REGEX = re.compile(r'^\s*(?:version|cpus)\s*=\s*([\d.]+)\s*$')
|
HEADER_REGEX = re.compile(r'^\s*(?:version|cpus)\s*=\s*([\d.]+)\s*$')
|
||||||
|
|
||||||
DROPPED_EVENTS_REGEX = re.compile(r'CPU:(?P<cpu_id>\d+) \[\d*\s*EVENTS DROPPED\]')
|
DROPPED_EVENTS_REGEX = re.compile(r'CPU:(?P<cpu_id>\d+) \[\d*\s*EVENTS DROPPED\]')
|
||||||
@ -269,27 +274,30 @@ class TraceCmdTrace(object):
|
|||||||
elif TRACE_MARKER_STOP in line:
|
elif TRACE_MARKER_STOP in line:
|
||||||
break
|
break
|
||||||
|
|
||||||
match = DROPPED_EVENTS_REGEX.search(line)
|
if 'EVENTS DROPPED' in line:
|
||||||
if match:
|
match = DROPPED_EVENTS_REGEX.search(line)
|
||||||
yield DroppedEventsEvent(match.group('cpu_id'))
|
|
||||||
continue
|
|
||||||
|
|
||||||
matched = False
|
|
||||||
for rx in [HEADER_REGEX, EMPTY_CPU_REGEX]:
|
|
||||||
match = rx.search(line)
|
|
||||||
if match:
|
if match:
|
||||||
logger.debug(line.strip())
|
yield DroppedEventsEvent(match.group('cpu_id'))
|
||||||
matched = True
|
continue
|
||||||
break
|
|
||||||
if matched:
|
if line.startswith('version') or line.startswith('cpus') or\
|
||||||
|
line.startswith('CPU:'):
|
||||||
|
matched = False
|
||||||
|
for rx in [HEADER_REGEX, EMPTY_CPU_REGEX]:
|
||||||
|
match = rx.search(line)
|
||||||
|
if match:
|
||||||
|
logger.debug(line.strip())
|
||||||
|
matched = True
|
||||||
|
break
|
||||||
|
if matched:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# <thread/cpu/timestamp>: <event name>: <event body>
|
||||||
|
parts = line.split(': ', 2)
|
||||||
|
if len(parts) != 3:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
match = TRACE_EVENT_REGEX.search(line)
|
event_name = parts[1].strip()
|
||||||
if not match:
|
|
||||||
logger.warning('Invalid trace event: "{}"'.format(line))
|
|
||||||
continue
|
|
||||||
|
|
||||||
event_name = match.group('name')
|
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
found = False
|
found = False
|
||||||
@ -300,10 +308,22 @@ class TraceCmdTrace(object):
|
|||||||
if not found:
|
if not found:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
thread_string, rest = parts[0].split(' [')
|
||||||
|
cpu_id, ts_string = rest.split('] ')
|
||||||
|
body = parts[2].strip()
|
||||||
|
|
||||||
body_parser = EVENT_PARSER_MAP.get(event_name, default_body_parser)
|
body_parser = EVENT_PARSER_MAP.get(event_name, default_body_parser)
|
||||||
if isinstance(body_parser, basestring) or isinstance(body_parser, re._pattern_type): # pylint: disable=protected-access
|
if isinstance(body_parser, basestring) or isinstance(body_parser, re._pattern_type): # pylint: disable=protected-access
|
||||||
body_parser = regex_body_parser(body_parser)
|
body_parser = regex_body_parser(body_parser)
|
||||||
yield TraceCmdEvent(parser=body_parser, **match.groupdict())
|
|
||||||
|
yield TraceCmdEvent(
|
||||||
|
thread=thread_string.strip(),
|
||||||
|
cpu_id=cpu_id,
|
||||||
|
ts=ts_string.strip(),
|
||||||
|
name=event_name,
|
||||||
|
body=body,
|
||||||
|
parser=body_parser,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if self.filter_markers and inside_marked_region:
|
if self.filter_markers and inside_marked_region:
|
||||||
logger.warning('Did not encounter a stop marker in trace')
|
logger.warning('Did not encounter a stop marker in trace')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user