|
Gprof2Dot
Convert profiling output to a dot graph.
AboutThis is a Python script to convert the output from many profilers into a dot graph. It has the following features:
If you want an interactive viewer for gprof2dot output graphs, check xdot. ExampleThis is the result from the example data in the Linux Gazette article with the default settings: Changelog
RequirementsWindows users
Debian/Ubuntu users
apt-get install python graphviz DownloadDocumentationUsageUsage:
gprof2dot.py [options] [file] ...
Options:
--version show program's version number and exit
-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: prof, oprofile, sysprof, shark,
sleepy, aqtime, or pstats [default: prof]
-c THEME, --colormap=THEME
color map: color, pink, gray, or bw [default: color]
-s, --strip strip function parameters, template parameters, and
const modifiers from demangled C++ function names
-w, --wrap wrap function namesExamplesgprof/path/to/your/executable arg1 arg2 gprof path/to/your/executable | gprof2dot.py | dot -Tpng -o output.png oprofileopcontrol --callgraph=16 opcontrol --start /path/to/your/executable arg1 arg2 opcontrol --stop opcontrol --dump opreport -cgf | gprof2dot.py -f oprofile | dot -Tpng -o output.png python profilepython -m profile -o output.pstats path/to/your/script arg1 arg2 gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png python 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.png python hotshot profilerThe hotshot profiler does not include a main function. Download this hotshotmain.py script instead. hotshotmain.py -o output.pstats path/to/your/script arg1 arg2 gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png OutputA node in the output graph represents a function and has the following layout: +------------------------------+ | function name | | total time % ( self time % ) | | total calls | +------------------------------+ where:
An edge represents the calls between two functions and has the following layout: total time %
calls
parent --------------------> childrenWhere:
Note that in recursive cycles, the total time % in the node is the same for the whole functions in the cycle, and there is no total 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 the total time % value. In the default temperature-like color-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. Frequently Asked QuestionsHow can I generate a call graph from gprof output?By default gprof2dot.py generates a partial call 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-thres and -e / --edge-thres options, as: gprof2dot.py -n0 -e0 For an even more complete call graph, also run gprof with the -c / --static-call-graph, which identifies by statical analysis of the binary machine code other functions that could have been called, but never were. The 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 / --strip option 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 because dot does 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-thres and -e / --edge-thres options, as: gprof2dot.py -n0 -e0 But to get meaningful results you will need to find a way to run the program for a longer time period, or run gprof with multiple profiles. See also:
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 are essential to produce output suitable are:
You want the code you are profiling to be as close as possible as the code that you will be releasing. So you should include all options that you use in your release code, typically:
However, due to the profiling mechanism used by gprof (and other profilers), many of the optimizations performed by gcc interfere with the accuracy/granularity of the profiling. You should pass these options to disable those particular optimizations:
If the granularity is still too low, you may pass these options to achieve finer granularity:
See the full list of gcc optimization options for more information. LinksProfiling tools
Profile visualization tools
Call-graph generation tools
|
Sign in to add a comment

Great job José. Thanks for making it available to all of us!
I just wanted to let you know that this script just saved me hours of deciphering the Python profiler's output. Thanks!
excellent work!! thanks a lot!
Excellent python profiling result viewer. I'm using inkscape as an SVG visualization tool (couldn't find easily a dot viewer on Windows).
It's just what I was looking for. Thanks.
Very nice. There's a small problem when there are more than 10000 functions. At least some versions of gprof have a bug where the initial function index loses the closing bracket for indices > 10000. I just added a '?' to the initial closing bracket in the cg_primary_re and cg_cycle_header_re regexps to fix this.
Here is an example of the gprof bug:
----------------------------------------------- 0.00 0.00 3778/3778 timing::ClockDef::~ClockDef() [3176] [9999] 0.0 0.00 0.00 3778 timing::ClockEdge::~ClockEdge() [9999] ----------------------------------------------- 0.00 0.00 14/3657 timing::BaseTranList::handleUserTransitions(clkdb::SmartPointerTP<circuit::Port>, bool) [836] 0.00 0.00 3643/3657 timing::AllClockDefs::skipClockPropDueToInactiveClock(clkdb::SmartPointerTP<timing::ClockDef>) [2467] [10000 0.0 0.00 0.00 3657 timing::ClockDef::getMasterSourceClock() const [10000] ----------------------------------------------- 0.00 0.00 3503/3503 circuit::name::FindNameBaseTraverser::addFoundName(clkdb::SmartPointerTP<circuit::name::NameIFC>, clkdb::SmartPointerTP<clkdb::PersistentObject>) [2984] [10001 0.0 0.00 0.00 3503 circuit::name::FindNameContainer<clkdb::ObjectContainer>::push_back(clkdb::SmartPointerTP<clkdb::PersistentObject>) [10001] -----------------------------------------------jkuzeja,
Commited. Thanks.
Very nice, thanks!
/Johan Eker
It would be very nice to have an option to colour nodes according to exclusive time rather than inclusive - having the main red hotspot "main" for every profile is not always helpful
Really quality tools man...
This is a life saver....great job.
This is an excellent program. Thank you very much for your hard work.
x264?$ gprof x264 | gprof2dot.py | dot -Tpng -o out.png -bash: dot: command not found Traceback (most recent call last):
IOError: 32? Broken pipedew.dropx,
you need to install graphviz.