1#!/usr/bin/env python
2
3from __future__ import print_function
4
5desc = '''Generate statistics about optimization records from the YAML files
6generated with -fsave-optimization-record and -fdiagnostics-show-hotness.
7
8The tools requires PyYAML and Pygments Python packages.'''
9
10import optrecord
11import argparse
12import operator
13from collections import defaultdict
14from multiprocessing import cpu_count, Pool
15
16try:
17    from guppy import hpy
18    hp = hpy()
19except ImportError:
20    print("Memory consumption not shown because guppy is not installed")
21    hp = None
22
23if __name__ == '__main__':
24    parser = argparse.ArgumentParser(description=desc)
25    parser.add_argument(
26        'yaml_dirs_or_files',
27        nargs='+',
28        help='List of optimization record files or directories searched '
29             'for optimization record files.')
30    parser.add_argument(
31        '--jobs',
32        '-j',
33        default=None,
34        type=int,
35        help='Max job count (defaults to %(default)s, the current CPU count)')
36    parser.add_argument(
37        '--no-progress-indicator',
38        '-n',
39        action='store_true',
40        default=False,
41        help='Do not display any indicator of how many YAML files were read.')
42    args = parser.parse_args()
43
44    print_progress = not args.no_progress_indicator
45
46    files = optrecord.find_opt_files(*args.yaml_dirs_or_files)
47    if not files:
48        parser.error("No *.opt.yaml files found")
49        sys.exit(1)
50
51    all_remarks, file_remarks, _ = optrecord.gather_results(
52        files, args.jobs, print_progress)
53    if print_progress:
54        print('\n')
55
56    bypass = defaultdict(int)
57    byname = defaultdict(int)
58    for r in optrecord.itervalues(all_remarks):
59        bypass[r.Pass] += 1
60        byname[r.Pass + "/" + r.Name] += 1
61
62    total = len(all_remarks)
63    print("{:24s} {:10d}".format("Total number of remarks", total))
64    if hp:
65        h = hp.heap()
66        print("{:24s} {:10d}".format("Memory per remark",
67                                     h.size / len(all_remarks)))
68    print('\n')
69
70    print("Top 10 remarks by pass:")
71    for (passname, count) in sorted(bypass.items(), key=operator.itemgetter(1),
72                                    reverse=True)[:10]:
73        print("  {:30s} {:2.0f}%". format(passname, count * 100. / total))
74
75    print("\nTop 10 remarks:")
76    for (name, count) in sorted(byname.items(), key=operator.itemgetter(1),
77                                reverse=True)[:10]:
78        print("  {:30s} {:2.0f}%". format(name, count * 100. / total))
79