Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

[RFC] Systrace's custom events formatting #243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions tests/test_systrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def test_systrace_html(self):
self.assertEquals(len(trace.sched_wakeup.data_frame), 4)
self.assertTrue("target_cpu" in trace.sched_wakeup.data_frame.columns)

self.assertTrue(hasattr(trace, "trace_event_clock_sync"))
self.assertEquals(len(trace.trace_event_clock_sync.data_frame), 1)
self.assertTrue("realtime_ts" in trace.trace_event_clock_sync.data_frame.columns)

def test_cpu_counting(self):
"""SysTrace traces know the number of cpus"""

Expand Down
13 changes: 11 additions & 2 deletions trappy/ftrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class definitions list. Otherwise, register a class to parse
trace_class = DynamicTypeFactory(event_name, (Base,), kwords)
self.class_definitions[event_name] = trace_class

def format_data(self, unique_word, data_str):
"""Reformat data before parsing
"""
return data_str

def __populate_data(self, fin, cls_for_unique_word, window, abs_window):
"""Append to trace data from a txt trace"""

Expand Down Expand Up @@ -202,12 +207,16 @@ def contains_unique_word(line, unique_words=cls_for_unique_word.keys()):
(abs_window[1] and timestamp > abs_window[1]):
return

# Allow the trace class to reformat data
data_str = line[line.find(unique_word) + len(unique_word):]
data_str = self.format_data(unique_word, data_str)

try:
data_start_idx = start_match.search(line).start()
data_start_idx = start_match.search(data_str).start()
except AttributeError:
continue

data_str = line[data_start_idx:]
data_str = data_str[data_start_idx:]

# Remove empty arrays from the trace
data_str = re.sub(r"[A-Za-z0-9_]+=\{\} ", r"", data_str)
Expand Down
31 changes: 31 additions & 0 deletions trappy/systrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
# limitations under the License.
#

import re

from trappy.ftrace import GenericFTrace
from trappy.utils import listify

class drop_before_trace(object):
"""Object that, when called, returns True if the line is not part of
Expand Down Expand Up @@ -54,6 +57,13 @@ def __init__(self, path=".", name="", normalize_time=True, scope="all",

self.trace_path = path

# Android injects useful events from userspace, unfortunately not using
# a specific attention word. Thus, let's capture tracing_mark events
# and reformat them in a local format_data callback.
events = listify(events)
if 'tracing_mark_write' not in events:
events.append('tracing_mark_write')

super(SysTrace, self).__init__(name, normalize_time, scope, events,
window, abs_window)

Expand All @@ -75,3 +85,24 @@ def trace_hasnt_finished(self):

"""
return lambda x: not x.endswith("</script>\n")

def format_data(self, unique_word, data_str):
if unique_word != 'tracing_mark_write:':
return data_str

# Disacrd not useful clock synchronization events
if 'trace_event_clock_sync' in data_str:
return ''

match = SYSTRACE_EVENT.match(data_str)
if match:
data_str = "event={} pid={} func={} data={}".format(
match.group('event'), match.group('pid'),
match.group('func'), match.group('data'))
return data_str

raise ValueError('Unexpected systrace marker: {}'.format(data_str))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should be an error, anyone could put stuff in tracing_mark_write. Maybe log a warning?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Logging a warning would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that was mainly for me while it's in development to check that with many different traces I'm actually matching all the possible custom events. Will change it to a warning.

return data_str

SYSTRACE_EVENT = re.compile(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment about what this regular expression does. I know regexes are cool, but really hard to read and get right 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually a sample from the trace here would be good.

r'^ (?P<event>[A-Z])(\|(?P<pid>\d+)\|(?P<func>.*)(\|(?P<data>\d+))?)?')