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