1#!/usr/bin/python
2# Copyright Martin J. Bligh (mbligh@google.com)
3# Released under the GPL, v2
4
5import os, sys, re
6
7results_per_sign = 10
8
9def parse_lines(filename):
10    results = []
11    start_key = 1
12    for line in open(filename).readlines():
13        try:
14            a = line.split()
15            key = ' '.join(a[start_key:])
16            count = int(a[0])
17            results.append((key, count))
18        except:         # presumably a header line
19            if re.match(r'samples\s*%\s*app name\s*symbol name', line):
20                start_key = 2
21            elif re.match(r'samples\s*%\s*image name\s*app name\s*symbol name', line):
22                start_key = 3
23    return results
24
25
26# Firstly, suck in both files.
27orig = {}
28new = {}
29diff = {}
30
31for (key, count) in parse_lines(sys.argv[1]):
32    # Oprofile seems to be ... erm ... broken. Keys can appear > once ;-(
33    if orig.has_key(key):
34        orig[key] += count
35    else:
36        orig[key] = count
37    if diff.has_key(key):
38        diff[key] -= count
39    else:
40        diff[key] = -count
41
42for (key, count) in parse_lines(sys.argv[2]):
43    if new.has_key(key):
44        new[key] += count
45    else:
46        new[key] = count
47    if diff.has_key(key):
48        diff[key] += count
49    else:
50        diff[key] = count
51
52if len(orig) < 2* results_per_sign or len(new) < 2 * results_per_sign:
53    sys.exit(1)             # one of the files was blank?
54
55# Now sort and print the diffs.
56def print_key(key):
57    if orig.has_key(key) and orig[key] > 0:
58        pct = (100 * diff[key]) / orig[key]
59    else:
60        pct = 0
61    print "%10d  %6.1f%% %s" % (diff[key], pct, key)
62
63keys = sorted(diff.keys(), key=lambda x : diff[x], reverse = True)
64
65for key in keys[:results_per_sign]:
66    print_key(key)
67
68print "\n...\n"
69
70for key in keys[len(keys)-results_per_sign:]:
71    print_key(key)
72