Commit e290ecde authored by pliard@chromium.org's avatar pliard@chromium.org

Fix bug in mergetraces.py.

The unit test was unfortunately written in a way that didn't catch it.

BUG=376793
R=pasko@chromium.org

Review URL: https://codereview.chromium.org/319053004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275390 0039d316-1c4b-4281-b951-d872f2087c98
parent 3e1f7738
...@@ -143,21 +143,30 @@ def GroupByProcessAndThreadId(input_trace): ...@@ -143,21 +143,30 @@ def GroupByProcessAndThreadId(input_trace):
result each thread has its own contiguous segment of code (ordered by result each thread has its own contiguous segment of code (ordered by
timestamp) and processes also have their code isolated (i.e. not interleaved). timestamp) and processes also have their code isolated (i.e. not interleaved).
""" """
def MakeTimestamp(usec, sec): def MakeTimestamp(sec, usec):
return usec * 1000000 + sec return sec * 1000000 + usec
def PidAndTidFromString(pid_and_tid): def PidAndTidFromString(pid_and_tid):
strings = pid_and_tid.split(':') strings = pid_and_tid.split(':')
return (int(strings[0]), int(strings[1])) return (int(strings[0]), int(strings[1]))
tid_to_pid_map = {}
pid_first_seen = {} pid_first_seen = {}
tid_first_seen = {} tid_first_seen = {}
for (sec, usec, pid_and_tid, _) in input_trace: for (sec, usec, pid_and_tid, _) in input_trace:
(pid, tid) = PidAndTidFromString(pid_and_tid) (pid, tid) = PidAndTidFromString(pid_and_tid)
# Make sure that thread IDs are unique since this is a property we rely on.
if tid_to_pid_map.setdefault(tid, pid) != pid:
raise Exception(
'Seen PIDs %d and %d for TID=%d. Thread-IDs must be unique' % (
tid_to_pid_map[tid], pid, tid))
if not pid in pid_first_seen: if not pid in pid_first_seen:
pid_first_seen[pid] = MakeTimestamp(usec, sec) pid_first_seen[pid] = MakeTimestamp(sec, usec)
if not tid in tid_first_seen: if not tid in tid_first_seen:
tid_first_seen[tid] = MakeTimestamp(usec, sec) tid_first_seen[tid] = MakeTimestamp(sec, usec)
def CompareEvents(event1, event2): def CompareEvents(event1, event2):
(sec1, usec1, pid_and_tid, _) = event1 (sec1, usec1, pid_and_tid, _) = event1
...@@ -171,7 +180,7 @@ def GroupByProcessAndThreadId(input_trace): ...@@ -171,7 +180,7 @@ def GroupByProcessAndThreadId(input_trace):
tid_cmp = cmp(tid_first_seen[tid1], tid_first_seen[tid2]) tid_cmp = cmp(tid_first_seen[tid1], tid_first_seen[tid2])
if tid_cmp != 0: if tid_cmp != 0:
return tid_cmp return tid_cmp
return cmp(MakeTimestamp(usec1, sec1), MakeTimestamp(usec2, sec2)) return cmp(MakeTimestamp(sec1, usec1), MakeTimestamp(sec2, usec2))
return sorted(input_trace, cmp=CompareEvents) return sorted(input_trace, cmp=CompareEvents)
......
...@@ -6,17 +6,17 @@ import unittest ...@@ -6,17 +6,17 @@ import unittest
import mergetraces import mergetraces
class GroupByProcessAndThreadIdTest(unittest.TestCase): class GroupByProcessAndThreadIdTestBasic(unittest.TestCase):
def runTest(self): def runTest(self):
# (sec, usec, 'pid:tid', function address). # (sec, usec, 'pid:tid', function address).
input_trace = [ input_trace = [
(100, 10, '2000:2001', 0x5), (100, 10, '2000:2001', 0x5),
(100, 11, '2000:2001', 0x3), (100, 11, '2000:2001', 0x3),
(100, 11, '2000:2000', 0x1), (100, 13, '2000:1999', 0x8),
(100, 12, '2001:2001', 0x6), (100, 14, '2000:2000', 0x7),
(100, 13, '2000:2002', 0x8), (120, 13, '2001:2003', 0x9),
(100, 13, '2001:2002', 0x9), (150, 12, '2001:2004', 0x6),
(100, 14, '2000:2000', 0x7) (180, 11, '2000:2000', 0x1),
] ]
# Functions should be grouped by thread-id and PIDs should not be # Functions should be grouped by thread-id and PIDs should not be
...@@ -24,13 +24,28 @@ class GroupByProcessAndThreadIdTest(unittest.TestCase): ...@@ -24,13 +24,28 @@ class GroupByProcessAndThreadIdTest(unittest.TestCase):
expected_trace = [ expected_trace = [
(100, 10, '2000:2001', 0x5), (100, 10, '2000:2001', 0x5),
(100, 11, '2000:2001', 0x3), (100, 11, '2000:2001', 0x3),
(100, 11, '2000:2000', 0x1), (100, 13, '2000:1999', 0x8),
(100, 14, '2000:2000', 0x7), (100, 14, '2000:2000', 0x7),
(100, 13, '2000:2002', 0x8), (180, 11, '2000:2000', 0x1),
(100, 12, '2001:2001', 0x6), (120, 13, '2001:2003', 0x9),
(100, 13, '2001:2002', 0x9) (150, 12, '2001:2004', 0x6),
] ]
grouped_trace = mergetraces.GroupByProcessAndThreadId(input_trace) grouped_trace = mergetraces.GroupByProcessAndThreadId(input_trace)
self.assertEqual(grouped_trace, expected_trace) self.assertEqual(grouped_trace, expected_trace)
class GroupByProcessAndThreadIdFailsWithNonUniqueTIDs(unittest.TestCase):
def runTest(self):
# (sec, usec, 'pid:tid', function address).
input_trace = [
(100, 10, '1999:2001', 0x5),
(100, 10, '1988:2001', 0x5),
]
try:
mergetraces.GroupByProcessAndThreadId(input_trace)
except Exception:
return
self.fail('Multiple processes should not have a same thread-ID.')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment