最简单的工具来测量Linux中C程序缓存的命中率/未命中率和cpu时间? [英] simplest tool to measure C program cache hit/miss and cpu time in linux?
问题描述
我正在用C编写一个小程序,我想衡量它的性能.
I'm writing a small program in C, and I want to measure it's performance.
我想看看它在处理器中运行了多少时间,以及它造成了多少次高速缓存命中+未命中.关于上下文切换和内存使用的信息也将很高兴.
I want to see how much time do it run in the processor and how many cache hit+misses has it made. Information about context switches and memory usage would be nice to have too.
程序执行不到一秒钟.
我喜欢/proc/[pid]/stat的信息,但是我不知道程序死/被杀死后如何查看.
I like the information of /proc/[pid]/stat, but I don't know how to see it after the program has died/been killed.
有什么想法吗?
编辑:我认为Valgrind会增加很多开销.这就是为什么我想要一个始终可用的简单工具,例如/proc/[pid]/stat.
I think Valgrind adds a lot of overhead. That's why I wanted a simple tool, like /proc/[pid]/stat, that is always there.
推荐答案
使用性能:
perf stat ./yourapp
有关详细信息,请参见内核Wiki性能教程.这使用了CPU的硬件性能计数器,因此开销非常小.
See the kernel wiki perf tutorial for details. This uses the hardware performance counters of your CPU, so the overhead is very small.
来自Wiki的示例:
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
组合,您还可以进行完整的系统分析.任何带有调试符号的应用程序或库都将在报告中显示详细信息.
No need to load a kernel module manually, on a modern debian system (with the linux-base package) it should just work. With the perf record -a
/ perf report
combo you can also do full-system profiling. Any application or library that has debugging symbols will show up with details in the report.
为了可视化,火焰图看起来效果很好. (2020年更新:热点用户界面集成了火焰图.)
For visualization flame graphs seem to work well. (Update 2020: the hotspot UI has flame graphs integrated.)
这篇关于最简单的工具来测量Linux中C程序缓存的命中率/未命中率和cpu时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!