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
19    hp = hpy()
20except ImportError:
21    print("Memory consumption not shown because guppy is not installed")
22    hp = None
23
24if __name__ == "__main__":
25    parser = argparse.ArgumentParser(description=desc)
26    parser.add_argument(
27        "yaml_dirs_or_files",
28        nargs="+",
29        help="List of optimization record files or directories searched "
30        "for optimization record files.",
31    )
32    parser.add_argument(
33        "--jobs",
34        "-j",
35        default=None,
36        type=int,
37        help="Max job count (defaults to %(default)s, the current CPU count)",
38    )
39    parser.add_argument(
40        "--no-progress-indicator",
41        "-n",
42        action="store_true",
43        default=False,
44        help="Do not display any indicator of how many YAML files were read.",
45    )
46    args = parser.parse_args()
47
48    print_progress = not args.no_progress_indicator
49
50    files = optrecord.find_opt_files(*args.yaml_dirs_or_files)
51    if not files:
52        parser.error("No *.opt.yaml files found")
53        sys.exit(1)
54
55    all_remarks, file_remarks, _ = optrecord.gather_results(
56        files, args.jobs, print_progress
57    )
58    if print_progress:
59        print("\n")
60
61    bypass = defaultdict(int)
62    byname = defaultdict(int)
63    for r in optrecord.itervalues(all_remarks):
64        bypass[r.Pass] += 1
65        byname[r.Pass + "/" + r.Name] += 1
66
67    total = len(all_remarks)
68    print("{:24s} {:10d}".format("Total number of remarks", total))
69    if hp:
70        h = hp.heap()
71        print("{:24s} {:10d}".format("Memory per remark", h.size / len(all_remarks)))
72    print("\n")
73
74    print("Top 10 remarks by pass:")
75    for (passname, count) in sorted(
76        bypass.items(), key=operator.itemgetter(1), reverse=True
77    )[:10]:
78        print("  {:30s} {:2.0f}%".format(passname, count * 100.0 / total))
79
80    print("\nTop 10 remarks:")
81    for (name, count) in sorted(
82        byname.items(), key=operator.itemgetter(1), reverse=True
83    )[:10]:
84        print("  {:30s} {:2.0f}%".format(name, count * 100.0 / total))
85