我正在用 C 编写一个小程序,我想测量它的性能。
我想看看它在处理器中运行了多少时间以及它进行了多少次缓存命中+未命中。有关上下文切换和内存使用情况的信息也很好。
该程序的执行时间不到一秒钟。
我喜欢 /proc/[pid]/stat 的信息,但我不知道在程序死亡/被杀死后如何查看它。
有任何想法吗?
EDIT:我认为 Valgrind 增加了很多开销。这就是为什么我想要一个简单的工具,比如 /proc/[pid]/stat,它总是存在的。
Use perf:
perf stat ./yourapp
See the 内核 wiki 性能教程 https://perf.wiki.kernel.org/index.php/Tutorial了解详情。这使用了 CPU 的硬件性能计数器,因此开销非常小。
维基百科的例子:
perf stat -B dd if=/dev/zero of=/dev/null count=1000000
Performance counter stats for 'dd if=/dev/zero of=/dev/null count=1000000':
5,099 cache-misses # 0.005 M/sec (scaled from 66.58%)
235,384 cache-references # 0.246 M/sec (scaled from 66.56%)
9,281,660 branch-misses # 3.858 % (scaled from 33.50%)
240,609,766 branches # 251.559 M/sec (scaled from 33.66%)
1,403,561,257 instructions # 0.679 IPC (scaled from 50.23%)
2,066,201,729 cycles # 2160.227 M/sec (scaled from 66.67%)
217 page-faults # 0.000 M/sec
3 CPU-migrations # 0.000 M/sec
83 context-switches # 0.000 M/sec
956.474238 task-clock-msecs # 0.999 CPUs
0.957617512 seconds time elapsed
无需手动加载内核模块,在现代 debian 系统(带有 linux-base 软件包)上它应该可以正常工作。随着perf record -a
/ perf report
组合起来,您还可以进行全系统分析。任何具有调试符号的应用程序或库都将在报告中显示详细信息。
用于可视化火焰图 http://www.brendangregg.com/flamegraphs.html看起来效果很好。 (2020 年更新:hotspot https://github.com/KDAB/hotspotUI 集成了火焰图。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)