|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +def makeProfileGraph(stats_file,thresh_node,thresh_edge): |
| 5 | + """ |
| 6 | + Uses gprof2dot to create a graphviz dot file of the profiling information. |
| 7 | + |
| 8 | + This requires the gprof2dot package available via `pip install gprof2dot`. |
| 9 | + Renders the result using the program 'dot' via a command like |
| 10 | + `dot -Tpdf input.dot -o output.pdf`. |
| 11 | + """ |
| 12 | + try: |
| 13 | + from gprof2dot import gprof2dot |
| 14 | + except ImportError: |
| 15 | + print('Package gprof2dot not found. Unable to create a graph of the profile statistics.') |
| 16 | + print("`pip install gprof2dot` if you don't have it.") |
| 17 | + return |
| 18 | + import subprocess |
| 19 | + m = gprof2dot.Main() |
| 20 | + class Options: |
| 21 | + pass |
| 22 | + m.options = Options() |
| 23 | + m.options.node_thres = thresh_node# default 0.8 |
| 24 | + m.options.edge_thres = thresh_edge # default 0.1 |
| 25 | + m.options.strip = False |
| 26 | + m.options.wrap = True |
| 27 | + m.theme = m.themes['color'] # bw color gray pink |
| 28 | + parser = gprof2dot.PstatsParser(stats_file) |
| 29 | + m.profile = parser.parse() |
| 30 | + dot_file = stats_file + '.dot' |
| 31 | + m.output = open(dot_file,'wt') |
| 32 | + m.write_graph() |
| 33 | + m.output.close() |
| 34 | + try: |
| 35 | + subprocess.check_call(['dot', '-Tpdf', dot_file, '-o', '{0}.pdf'.format(dot_file)]) |
| 36 | + except subprocess.CalledProcessError: |
| 37 | + print("Error returned by 'dot' when generating graph of the profile statistics.") |
| 38 | + print("To try it yourself:\n dot -Tpdf {0} -o {0}.pdf".format(dot_file)) |
| 39 | + except OSError: |
| 40 | + print("Couldn't run 'dot' to create graph of profile statistics. Check graphviz is installed properly and on your path.") |
| 41 | + print("Once you've got it, try:\n dot -Tpdf {0} -o {0}.pdf".format(dot_file)) |
| 42 | + else: |
| 43 | + print("Graph of profile statistics saved to: \n {0}.pdf".format(dot_file)) |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + |
| 47 | + import argparse |
| 48 | + |
| 49 | + parser = argparse.ArgumentParser(description="Creates a call graph with profiling information.") |
| 50 | + parser.add_argument('FILE', type=str, default='RMG.profile', help='.profile file') |
| 51 | + parser.add_argument('THRESH_NODE', type=float, default=0.8, help='threshold percentage value for nodes') |
| 52 | + parser.add_argument('THRESH_EDGE', type=float, default=0.1, help='threshold percentage value for nodes') |
| 53 | + args = parser.parse_args() |
| 54 | + stats_file=args.FILE |
| 55 | + thresh_node=args.THRESH_NODE |
| 56 | + thresh_edge=args.THRESH_EDGE |
| 57 | + |
| 58 | + makeProfileGraph(stats_file,thresh_node,thresh_edge) |
| 59 | + |
| 60 | + |
0 commit comments