从多个分析器的输出生成点图。
项目描述
关于gprof2dot
这是一个 Python 脚本,用于将许多分析器的输出转换为点图。
它可以:
- 从以下位置读取输出:
- 修剪低于某个阈值的节点和边;
- 使用启发式方法在相互递归的函数中传播时间;
- 有效地使用颜色来吸引人们对热点的关注;
- 在 Python 和 Graphviz 可用的任何平台上工作,即几乎在任何地方。
如果您想要gprof2dot生成的图形的交互式查看器,请检查xdot.py。
地位
gprof2dot目前可以满足我的需求,我几乎没有时间维护它。所以我担心任何请求的功能都不太可能实现,并且我可能会缓慢处理问题报告或拉取请求。
例子
这是Linux Gazette 文章中使用默认设置的示例数据的结果:
要求
Windows 用户
- 下载并安装适用于 Windows 的 Python
- 下载并安装适用于 Windows 的 Graphviz
Linux 用户
在 Debian/Ubuntu 上运行:
apt-get install python3 graphviz
在 RedHat/Fedora 上运行
yum install python3 graphviz
下载
文档
用法
Usage:
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 percentages
例子
Linux 性能
perf record -g -- /path/to/your/executable
perf script | c++filt | gprof2dot.py -f perf | dot -Tpng -o output.png
oprofile
opcontrol --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
xperf
如果您不熟悉 xperf,请先阅读这篇出色的文章。然后做:
-
启动 xperf 为
xperf -on Latency -stackwalk profile -
运行您的应用程序。
-
保存数据。` xperf -d 输出.etl
-
启动可视化器:
xperf output.etl -
在Trace菜单中,选择Load Symbols。如有必要,配置符号路径。
-
在CPU 采样图上选择一个感兴趣的区域,单击鼠标右键,然后选择Summary Table。
-
在Columns菜单中,确保Stack列已启用且可见。
-
右键单击一行,选择Export Full Table,然后保存到output.csv。
-
然后调用 gprof2dot 作为
gprof2dot.py -f xperf output.csv | dot -Tpng -o output.png
VTune 放大器 XE
-
收集配置文件数据(也可以从 GUI 完成):
amplxe-cl -collect hotspots -result-dir output -- your-app -
将配置文件数据可视化为:
amplxe-cl -report gprof-cc -result-dir output -format text -report-output output.txt gprof2dot.py -f axe output.txt | dot -Tpng -o output.png
gprof
/path/to/your/executable arg1 arg2
gprof path/to/your/executable | gprof2dot.py | dot -Tpng -o output.png
蟒蛇配置文件
python -m profile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
python cProfile(以前称为lsprof)
python -m cProfile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
Java HPROF
java -agentlib:hprof=cpu=samples ...
gprof2dot.py -f hprof java.hprof.txt | dot -Tpng -o output.png
有关详细信息,请参阅Russell Power 的博客文章。
跟踪
dtrace -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 dtrace
输出
输出图中的一个节点代表一个函数,并具有以下布局:
+------------------------------+
| function name |
| total time % ( self time % ) |
| total calls |
+------------------------------+
在哪里:
- total time %是这个函数及其所有子函数所花费的运行时间的百分比;
- self time %是单独在这个函数中花费的运行时间的百分比;
- total calls是调用此函数的总次数(包括递归调用)。
一条边表示两个函数之间的调用,并具有以下布局:
total time %
calls
parent --------------------> children
在哪里:
- total time %是从孩子转移到这个父母的运行时间的百分比(如果有的话);
- calls是调用子函数的父函数的次数。
请注意,在递归循环中,节点中的总时间百分比对于循环中的整个函数是相同的,并且循环内的边中没有总时间百分比数字,因为这样的数字没有意义。
节点和边的颜色根据总时间百分比值而变化。在默认的类似温度的颜色图中,花费时间最多的函数(热点)标记为饱和红色,花费时间少的函数标记为深蓝色。请注意,默认情况下,可以忽略或不花费时间的函数不会出现在图表中。
列出函数
该标志--list-functions允许列出在输入中找到的函数条目gprof。这旨在作为准备使用--leaf( -l) 或--root( -z) 标志的工具。
prof2dot.py -f pstats /tmp/myLog.profile --list-functions "test_segments:*:*"
test_segments:5:<module>,
test_segments:206:TestSegments,
test_segments:46:<lambda>
-
选择器参数与 Unix/Bash 通配符/模式匹配一起使用,其方式与
-land-z标志执行的方式相同。 -
条目的格式为“<pkg>:<linenum>:<function>”。
-
当Selector参数以“%”开头时,在删除Selector的领先“%”后,将对选定条目执行所有可用信息的转储。如果选择器是“+”或“*”,则打印完整的函数列表。
经常问的问题
如何生成完整的调用图?
默认情况下会gprof2dot.py生成部分调用图,不包括对总计算时间影响很小或没有影响的节点和边。-n如果您想要完整的调用图,则通过/--node-thres 和-e/--edge-thres选项为节点和边设置零阈值,如下所示:
gprof2dot.py -n0 -e0
节点标签太宽。我怎样才能缩小它们的范围?
在分析 C++ 代码时,节点标签可能会变得非常宽,因为在解构的 C++ 函数名称中包含范围、函数参数和模板参数。
如果您不需要函数和模板参数信息,则传递-s/--strip选项以去除它们。
如果您想保留所有这些信息,或者如果标签仍然太宽,那么您可以传递-w/--wrap来包装标签。请注意,由于dot不会自动包装标签,因此标签边距不会完全对齐。
为什么没有输出,或者都是同一种颜色?
很可能,总执行时间太短,因此配置文件中没有足够的精度来确定时间花费在哪里。
您仍然可以通过-n/--node-thres 和-e/--edge-thres选项为节点和边设置零阈值来强制显示整个图形,如下所示:
gprof2dot.py -n0 -e0
但是要获得有意义的结果,您需要找到一种方法来运行程序更长的时间(汇总多次运行的结果)。
为什么百分比不加起来?
您可能执行时间太短,导致舍入误差很大。
有关增加执行时间的方法,请参见上面的问题。
在为分析进行编译时,我应该将哪些选项传递给 gcc?
产生合适结果必不可少的选项是:
-g: 产生调试信息-fno-omit-frame-pointer:使用帧指针(在某些架构(如 x86_64)和某些优化级别中,默认情况下禁用帧指针使用;没有它就不可能遍历调用堆栈)
如果您使用 gprof,您还需要-pg选项,但现在您可以使用其他分析工具获得更好的结果,其中大多数在编译时不需要特殊的代码检测。
您希望您正在分析的代码尽可能接近您将发布的代码。因此,您应该在发布代码中包含您使用的所有选项,通常是:
-O2:不涉及空间速度权衡的优化-DNDEBUG: 禁用标准库中的调试代码(如 assert 宏)
然而,gcc 执行的许多优化会干扰分析结果的准确性/粒度。您应该通过这些选项来禁用那些特定的优化:
-fno-inline-functions:不要将函数内联到它们的父函数中(否则花费在这些函数上的时间将归因于调用者)-fno-inline-functions-called-once: 和上面一样-fno-optimize-sibling-calls:不要优化兄弟和尾递归调用(否则尾调用可能归因于父函数)
如果粒度仍然太低,您可以通过这些选项来获得更细的粒度:
-fno-default-inline: 不要仅仅因为成员函数是在类范围内定义的,就默认内联成员函数-fno-inline:不要注意 inline 关键字但是请注意,使用这些最后的选项,由于函数调用开销,多次调用函数的时间将被扭曲。这对于典型的 C++ 代码来说尤其如此,因为它们期望完成这些优化以获得良好的性能。
有关更多信息,请参阅gcc 优化选项的完整列表。
链接
请参阅wiki获取外部资源,包括补充/替代工具。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。