aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKP Singh <kpsingh@google.com>2017-06-29 23:12:02 +0200
committerKP Singh <kpsingh@google.com>2017-06-29 23:12:02 +0200
commit180ba383f2ab4e544273a1e75e8bee61df654ceb (patch)
tree7c624b5404d1c61ae93dfa71ff4499ad6e97ca0e
parent634bcd3bc8fff3803b8c62d03bde63bbdac1c034 (diff)
parent2a58727813c91d5497ce5452b5bff1bc1a467dc7 (diff)
downloadtrappy-180ba383f2ab4e544273a1e75e8bee61df654ceb.tar.gz
Merge branch 'joelagnel-for-unique-word-speedup'
-rw-r--r--trappy/base.py26
-rw-r--r--trappy/ftrace.py29
2 files changed, 35 insertions, 20 deletions
diff --git a/trappy/base.py b/trappy/base.py
index 4f2fb21..06857b5 100644
--- a/trappy/base.py
+++ b/trappy/base.py
@@ -180,6 +180,27 @@ class Base(object):
self.line_array.append(line)
self.data_array.append(data)
+ def string_cast(self, string, type):
+ """ Attempt to convert string to another type
+
+ Here we attempt to cast string to a type. Currently only
+ integer conversion is supported with future expansion
+ left open to other types.
+
+ :param string: The value to convert.
+ :type string: str
+
+ :param type: The type to convert to.
+ :type type: type
+ """
+ # Currently this function only supports int conversion
+ if type != int:
+ return
+ # Handle false-positives for negative numbers
+ if not string.lstrip("-").isdigit():
+ return string
+ return int(string)
+
def generate_data_dict(self, data_str):
data_dict = {}
prev_key = None
@@ -191,10 +212,7 @@ class Base(object):
data_dict[prev_key] += ' ' + field
continue
(key, value) = field.split('=', 1)
- try:
- value = int(value)
- except ValueError:
- pass
+ value = self.string_cast(value, int)
data_dict[key] = value
prev_key = key
return data_dict
diff --git a/trappy/ftrace.py b/trappy/ftrace.py
index 06e2a93..c0a40c2 100644
--- a/trappy/ftrace.py
+++ b/trappy/ftrace.py
@@ -249,31 +249,27 @@ subclassed by FTrace (for parsing FTrace coming from trace-cmd) and SysTrace."""
trace_class = DynamicTypeFactory(event_name, (Base,), kwords)
self.class_definitions[event_name] = trace_class
+ def __get_trace_class(self, line, cls_word):
+ trace_class = None
+ for unique_word, cls in cls_word.iteritems():
+ if unique_word in line:
+ trace_class = cls
+ if not cls.fallback:
+ return trace_class
+ return trace_class
+
def __populate_data(self, fin, cls_for_unique_word):
"""Append to trace data from a txt trace"""
- def contains_unique_word(line, unique_words=cls_for_unique_word.keys()):
- for unique_word in unique_words:
- if unique_word in line:
- return True
- return False
-
actual_trace = itertools.dropwhile(self.trace_hasnt_started(), fin)
actual_trace = itertools.takewhile(self.trace_hasnt_finished(),
actual_trace)
for line in actual_trace:
- if not contains_unique_word(line):
+ trace_class = self.__get_trace_class(line, cls_for_unique_word)
+ if not trace_class:
self.lines += 1
continue
- for unique_word, cls in cls_for_unique_word.iteritems():
- if unique_word in line:
- trace_class = cls
- if not cls.fallback:
- break
- else:
- if not trace_class:
- raise FTraceParseError("No unique word in '{}'".format(line))
line = line[:-1]
@@ -306,7 +302,8 @@ subclassed by FTrace (for parsing FTrace coming from trace-cmd) and SysTrace."""
return
# Remove empty arrays from the trace
- data_str = re.sub(r"[A-Za-z0-9_]+=\{\} ", r"", data_str)
+ if "={}" in data_str:
+ data_str = re.sub(r"[A-Za-z0-9_]+=\{\} ", r"", data_str)
trace_class.append_data(timestamp, comm, pid, cpu, self.lines, data_str)
self.lines += 1