我有一个 JMH 多线程测试:
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(value = 1, jvmArgsAppend = { "-Xmx512m", "-server", "-XX:+AggressiveOpts","-XX:+UnlockDiagnosticVMOptions",
"-XX:+UnlockExperimentalVMOptions", "-XX:+PrintAssembly", "-XX:PrintAssemblyOptions=intel",
"-XX:+PrintSignatureHandlers"})
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
public class LinkedQueueBenchmark {
private static final Unsafe unsafe = UnsafeProvider.getUnsafe();
private static final long offsetObject;
private static final long offsetNext;
private static final int THREADS = 5;
private static class Node {
private volatile Node next;
public Node() {}
}
static {
try {
offsetObject = unsafe.objectFieldOffset(LinkedQueueBenchmark.class.getDeclaredField("object"));
offsetNext = unsafe.objectFieldOffset(Node.class.getDeclaredField("next"));
} catch (Exception ex) { throw new Error(ex); }
}
protected long t0,t1,t2,t3,t4,t5,t6,t7;
private volatile Node object = new Node(null);
@Threads(THREADS)
@Benchmark
public Node doTestCasSmart() {
Node current, o = new Node();
for(;;) {
current = this.object;
if (unsafe.compareAndSwapObject(this, offsetObject, current, o)) {
//current.next = o; //Special line:
break;
} else {
LockSupport.parkNanos(1);
}
}
return current;
}
}
- 在当前变体中,我的性能 ~ 55 ops/us
- 但是,如果我取消注释“Special line”,或者用 unsafe.putOrderedObject 替换它(在任何方向 - current.next = o或o.next = current),性能 ~ 2 ops/us。
据我了解,这是 CPU 缓存发生的事情,也许它正在清理存储缓冲区。如果我确实将它替换为基于锁的方法,没有 CAS,性能将是 11-20 ops/us。
我尝试使用 LinuxPerfAsmProfiler 和 PrintAssembly,在第二种情况下我看到:
....[Hottest Regions]...............................................................................
25.92% 17.93% [0x7f1d5105fe60:0x7f1d5105fe69] in SpinPause (libjvm.so)
17.53% 20.62% [0x7f1d5119dd88:0x7f1d5119de57] in ParMarkBitMap::live_words_in_range(HeapWord*, oopDesc*) const (libjvm.so)
10.81% 6.30% [0x7f1d5129cff5:0x7f1d5129d0ed] in ParallelTaskTerminator::offer_termination(TerminatorTerminator*) (libjvm.so)
7.99% 9.86% [0x7f1d3c51d280:0x7f1d3c51d3a2] in com.jad.generated.LinkedQueueBenchmark_doTestCasSmart::doTestCasSmart_thrpt_jmhStub
有人可以向我解释到底发生了什么吗?为什么这么慢?这里的存储负载障碍在哪里?为什么 putOrdered 不起作用?以及如何解决?