Package: gprof2dot
Aboutgprof2dotThis is a Python script to convert the output from many profilers into adot graph.It can:read output from:Linux perfValgrind's callgrind tooloprofilesysprofxperfVTune Amplifier XEVery Sleepypython profilersJava's HPROFprof,gprofDTraceprune nodes and edges below a certain threshold;use an heuristic to propagate time inside mutually recursive functions;use color efficiently to draw attention to hot-spots;work on any platform where Python and Graphviz is available, i.e, virtually anywhere.If you want an interactive viewer for the graphs generated bygprof2dot, checkxdot.py.Statusgprof2dotcurrently fulfills my needs, and I have little or no time for its
maintenance.  So I'm afraid that any requested features are unlikely to be
implemented, and I might be slow processing issue reports or pull requests.ExampleThis is the result from theexample datain theLinux Gazette articlewith the default settings:RequirementsPython: known to work with version 2.7 and 3.3; it will most likelynotwork with earlier releases.Graphviz: tested with version 2.26.3, but should work fine with other versions.Windows usersDownload and installPython for WindowsDownload and installGraphviz for WindowsLinux usersOn Debian/Ubuntu run:apt-get install python3 graphvizOn RedHat/Fedora runyum install python3 graphvizDownloadPyPIpip install gprof2dotStandalone scriptGit repositoryDocumentationUsageUsage:
        gprof2dot.py [options] [file] ...

Options:
  -h, --help            show this help message and exit
  -o FILE, --output=FILE
                        output filename [stdout]
  -n PERCENTAGE, --node-thres=PERCENTAGE
                        eliminate nodes below this threshold [default: 0.5]
  -e PERCENTAGE, --edge-thres=PERCENTAGE
                        eliminate edges below this threshold [default: 0.1]
  -f FORMAT, --format=FORMAT
                        profile format: axe, callgrind, hprof, json, oprofile,
                        perf, prof, pstats, sleepy, sysprof or xperf [default:
                        prof]
  --total=TOTALMETHOD   preferred method of calculating total time: callratios
                        or callstacks (currently affects only perf format)
                        [default: callratios]
  -c THEME, --colormap=THEME
                        color map: color, pink, gray, bw, or print [default:
                        color]
  -s, --strip           strip function parameters, template parameters, and
                        const modifiers from demangled C++ function names
  -w, --wrap            wrap function names
  --show-samples        show function samples
  -z ROOT, --root=ROOT  prune call graph to show only descendants of specified
                        root function
  -l LEAF, --leaf=LEAF  prune call graph to show only ancestors of specified
                        leaf function
  --list-functions=SELECT list available functions as a help/preparation  for using the 
                        -l and -z flags. When selected the program only produces this
						list. SELECT is used with the same matching syntax
						as with -z(--root) and -l(--leaf). Special cases SELECT="+"
						gets the full list, selector starting with "%" cause dump 
						of all available information. 
  --skew=THEME_SKEW     skew the colorization curve.  Values < 1.0 give more
                        variety to lower percentages.  Values > 1.0 give less
                        variety to lower percentagesExamplesLinux perfperf record -g -- /path/to/your/executable
perf script | c++filt | gprof2dot.py -f perf | dot -Tpng -o output.pngoprofileopcontrol --callgraph=16
opcontrol --start
/path/to/your/executable arg1 arg2
opcontrol --stop
opcontrol --dump
opreport -cgf | gprof2dot.py -f oprofile | dot -Tpng -o output.pngxperfIf you're not familiar with xperf then readthis excellent articlefirst. Then do:Start xperf asxperf -on Latency -stackwalk profileRun your application.Save the data.
`
xperf -d output.etlStart the visualizer:xperf output.etlInTracemenu, selectLoad Symbols.Configure Symbol Pathsif necessary.Select an area of interest on theCPU sampling graph, right-click, and selectSummary Table.In theColumnsmenu, make sure theStackcolumn is enabled and visible.Right click on a row, chooseExport Full Table, and save tooutput.csv.Then invoke gprof2dot asgprof2dot.py -f xperf output.csv | dot -Tpng -o output.pngVTune Amplifier XECollect profile data as (also can be done from GUI):amplxe-cl -collect hotspots -result-dir output -- your-appVisualize profile data as:amplxe-cl -report gprof-cc -result-dir output -format text -report-output output.txt
gprof2dot.py -f axe output.txt | dot -Tpng -o output.pngSee alsoKirill Rogozhin's blog post.gprof/path/to/your/executable arg1 arg2
gprof path/to/your/executable | gprof2dot.py | dot -Tpng -o output.pngpython profilepython -m profile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.pngpython cProfile (formerly known as lsprof)python -m cProfile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.pngJava HPROFjava -agentlib:hprof=cpu=samples ...
gprof2dot.py -f hprof java.hprof.txt | dot -Tpng -o output.pngSeeRussell Power's blog postfor details.DTracedtrace -x ustackframes=100 -n 'profile-97 /pid == 12345/ { @[ustack()] = count(); } tick-60s { exit(0); }' -o out.user_stacks
gprof2dot.py -f dtrace out.user_stacks | dot -Tpng -o output.png

# Notice: sometimes, the dtrace outputs format may be latin-1, and gprof2dot will fail to parse it.
# To solve this problem, you should use iconv to convert to UTF-8 explicitly.
# TODO: add an encoding flag to tell gprof2dot how to decode the profile file.
iconv -f ISO-8859-1 -t UTF-8 out.user_stacks | gprof2dot.py -f dtraceOutputA node in the output graph represents a function and has the following layout:+------------------------------+
|        function name         |
| total time % ( self time % ) |
|         total calls          |
+------------------------------+where:total time %is the percentage of the running time spent in this function and all its children;self time %is the percentage of the running time spent in this function alone;total callsis the total number of times this function was called (including recursive calls).An edge represents the calls between two functions and has the following layout:total time %
              calls
parent --------------------> childrenWhere:total time %is the percentage of the running time transfered from the children to this parent (if available);callsis the number of calls the parent function called the children.Note that in recursive cycles, thetotal time %in the node is the same for the whole functions in the cycle, and there is nototal time %figure in the edges inside the cycle, since such figure would make no sense.The color of the nodes and edges varies according to thetotal time %value. In the defaulttemperature-likecolor-map, functions where most time is spent (hot-spots) are marked as saturated red, and functions where little time is spent are marked as dark blue. Note that functions where negligible or no time is spent do not appear in the graph by default.Listing functionsThe flag--list-functionspermits listing the function entries found in thegprofinput.
This is intended as a tool to prepare for utilisations with the--leaf(-l)
or--root(-z) flags.prof2dot.py -f pstats /tmp/myLog.profile  --list-functions "test_segments:*:*" 
  
test_segments:5:<module>,
test_segments:206:TestSegments,
test_segments:46:<lambda>The selector argument is used with Unix/Bash globbing/pattern matching, in the same
fashion as performed by the-land-zflags.Entries are formatted '<pkg>:<linenum>:<function>'.When selector argument starts with '%', a dump of all available information is
performed for selected entries,   after removal of selector's leading '%'. If
selector is "+" or "*", the full list of functions is printed.Frequently Asked QuestionsHow can I generate a complete call graph?By defaultgprof2dot.pygenerates apartialcall graph, excluding nodes and edges with little or no impact in the total computation time. If you want the full call graph then set a zero threshold for nodes and edges via the-n/--node-thresand-e/--edge-thresoptions, as:gprof2dot.py -n0 -e0The node labels are too wide. How can I narrow them?The node labels can get very wide when profiling C++ code, due to inclusion of scope, function arguments, and template arguments in demangled C++ function names.If you do not need function and template arguments information, then pass the-s/--stripoption to strip them.If you want to keep all that information, or if the labels are still too wide, then you can pass the-w/--wrap, to wrap the labels. Note that becausedotdoes not wrap labels automatically the label margins will not be perfectly aligned.Why there is no output, or it is all in the same color?Likely, the total execution time is too short, so there is not enough precision in the profile to determine where time is being spent.You can still force displaying the whole graph by setting a zero threshold for nodes and edges via the-n/--node-thresand-e/--edge-thresoptions, as:gprof2dot.py -n0 -e0But to get meaningful results you will need to find a way to run the program for a longer time period (aggregate results from multiple runs).Why don't the percentages add up?You likely have an execution time too short, causing the round-off errors to be large.See question above for ways to increase execution time.Which options should I pass to gcc when compiling for profiling?Options which areessentialto produce suitable results are:-g: produce debugging information-fno-omit-frame-pointer: use the frame pointer (frame pointer usage is disabled by default in some architectures like x86_64 and for some optimization levels; it is impossible to walk the call stack without it)Ifyou're using gprof you will also need-pgoption, but nowadays you can get much better results with other profiling tools, most of which require no special code instrumentation when compiling.You want the code you are profiling to be as close as possible as the code that you will
be releasing. So youshouldinclude all options that you use in your release code, typically:-O2: optimizations that do not involve a space-speed tradeoff-DNDEBUG: disable debugging code in the standard library (such as the assert macro)However many of the optimizations performed by gcc interfere with the accuracy/granularity of the profiling results.  Youshouldpass these options to disable those particular optimizations:-fno-inline-functions: do not inline functions into their parents (otherwise the time spent on these functions will be attributed to the caller)-fno-inline-functions-called-once: similar to above-fno-optimize-sibling-calls: do not optimize sibling and tail recursive calls (otherwise tail calls may be attributed to the parent function)If the granularity is still too low, youmaypass these options to achieve finer granularity:-fno-default-inline: do not make member functions inline by default merely because they are defined inside the class scope-fno-inline: do not pay attention to the inline keyword
Note however that with these last options the timings of functions called many times will be distorted due to the function call overhead. This is particularly true for typical C++ code whichexpectsthat these optimizations to be done for decent performance.See thefull list of gcc optimization optionsfor more information.LinksSee thewikifor external resources, including complementary/alternative tools.