我正在用C编写一个小程序,我想测量它的性能。在linux中测量C程序缓存命中/未命中和CPU时间的最简单工具?
我想看看它在处理器中运行了多少时间,以及有多少缓存命中+未命中。有关上下文切换和内存使用情况的信息也很好。
该程序需要不到一秒的时间执行。
我喜欢/ proc/[pid]/stat的信息,但是我不知道如何在程序死亡/被杀后看到它。
任何想法?
编辑:我认为Valgrind增加了很多开销。这就是为什么我想要一个简单的工具,比如/ proc/[pid]/stat,它总是在那里。
我正在用C编写一个小程序,我想测量它的性能。在linux中测量C程序缓存命中/未命中和CPU时间的最简单工具?
我想看看它在处理器中运行了多少时间,以及有多少缓存命中+未命中。有关上下文切换和内存使用情况的信息也很好。
该程序需要不到一秒的时间执行。
我喜欢/ proc/[pid]/stat的信息,但是我不知道如何在程序死亡/被杀后看到它。
任何想法?
编辑:我认为Valgrind增加了很多开销。这就是为什么我想要一个简单的工具,比如/ proc/[pid]/stat,它总是在那里。
使用PERF:
perf stat ./yourapp
见kernel wiki perf 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基封装)在其上应该只是工作。通过'perf record -a'/'perf report'组合,您还可以进行全系统分析。任何具有调试符号的应用程序或库都将在报告中显示详细信息。对于可视化flame graphs似乎很好。
最适合您的工具叫做valgrind。它具有内存分析,调用图建立等功能。
sudo apt get install valgrind
valgrind ./yourapp
但是,要获得程序执行的时间,可以使用time(8)
linux实用程序。
time ./yourapp
您还可以使用
/usr/bin/time -v YourProgram.exe
它会告诉你所有这些信息:
/usr/bin/time -v ls
Command being timed: "ls"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 60%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 4080
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 314
Voluntary context switches: 1
Involuntary context switches: 1
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
您也可以使用-f标志来格式化输出,以满足您的需求。
请务必使用它的完整路径来调用这个程序,otherway它会调用“时间”命令,这不是你所需要的...
希望这有助于!
注意'/ usr/bin/time!= time'是有帮助的。在bash中,'time'是一个内置的shell关键字。 – jperelli 2013-11-08 14:45:49
但是缓存未命中? – 2015-05-08 02:06:19
答案与缓存未命中无关。 – 2016-08-13 07:40:03
你在找这样的事吗? http://software.intel.com/en-us/articles/intel-performance-counter-monitor/ – harold 2012-04-10 17:45:30