我想在Java中运行基准测试并获得硬件计数器。我想用JMH这样做,因为它看起来像一个成熟的工具。它还具有钩子配置文件的选项(例如,perf工具,也是成熟的)。我的问题是,当用java -jar benchmarks.jar -prof perf
运行我的基准时,所有计数器都显示值<not counted>
。
Perf stats:
--------------------------------------------------
<not counted> task-clock
<not counted> context-switches
<not counted> cpu-migrations
<not counted> page-faults
<not counted> cycles
<not counted> instructions
<not counted> branches
<not counted> branch-misses
<not counted> L1-dcache-loads
<not counted> L1-dcache-load-misses
<not counted> LLC-loads
<not counted> LLC-load-misses
<not supported> L1-icache-loads
<not counted> L1-icache-load-misses
<not counted> dTLB-loads
<not counted> dTLB-load-misses
<not counted> iTLB-loads
<not counted> iTLB-load-misses
<not supported> L1-dcache-prefetches
<not supported> L1-dcache-prefetch-misses
0,379402355 seconds time elapsed
如果我独立运行perf
,例如输入perf stat sleep 5
,那么我会得到结果:
Performance counter stats for 'sleep 5':
0,588915 task-clock (msec) # 0,000 CPUs utilized
1 context-switches # 0,002 M/sec
0 cpu-migrations # 0,000 K/sec
60 page-faults # 0,102 M/sec
1 048 769 cycles # 1,781 GHz
807 919 instructions # 0,77 insn per cycle
159 210 branches # 270,345 M/sec
7 325 branch-misses # 4,60% of all branches
5,001500262 seconds time elapsed
我找到了答案,我把它留给了其他遇到类似问题的人。
对perf
的调用有参数--delay 21000
,它在开始计数之前引入了一个延迟。由于基准比较短,计数从未开始。
使用命令java -jar benchmarks.jar -prof perf:delay 0
解决了这个问题。 :
用于将选项传递给探查器。