RFR: 8186670: Implement _onSpinWait() intrinsic for AArch64 [v10]

Andrew Haley aph at openjdk.java.net
Fri Oct 15 13:12:57 UTC 2021


On Tue, 12 Oct 2021 08:11:20 GMT, Andrew Haley <aph at openjdk.org> wrote:

>> Evgeny Astigeevich has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Make OnSpinWaitInst/OnSpinWaitInstCount  DIAGNOSTIC
>
> Can we have a simple (as simple as possible) JMH benchmark, please? It should be something like a couple of threads racing to count up to a million.

> @theRealAph, any comments on the microbenchmark I wrote?

Something like this works well:


   @Param({"1000000"})
    public int maxNum;

    @Param({"4"})
    public int threadCount;

    AtomicInteger theCounter;

    Thread threads[];

    void work() {
        for (;;) {
            int prev = theCounter.get();
            if (prev >= maxNum) {
                break;
            }
            if (theCounter.compareAndExchange(prev, prev + 1) != prev) {
                Thread.onSpinWait();
            }
        }
    }

    @Setup(Level.Trial)
    public void foo() {
        theCounter = new AtomicInteger();
    }

    @Setup(Level.Invocation)
    public void setup() {
        lock = new AtomicInteger();
        theCounter.set(0);
        counter = 0;
        threads = new Thread[threadCount];

        for (int i = 0; i< threads.length; i++) {
            threads[i] = new Thread(this::work);
        }

    }

    @Benchmark
    public void trial() throws Exception {
        for (int i = 0; i< threads.length; i++) {
            threads[i].start();
        }
        for (int i = 0; i< threads.length; i++) {
            threads[i].join();
        }
    }
}

Before:

Benchmark               (maxNum)  (threadCount)  Mode  Cnt   Score    Error  Units
ThreadOnSpinWait.trial   1000000              2  avgt    3  43.830 ± 32.543  ms/op

With `-XX:OnSpinWaitInst=isb -XX:OnSpinWaitInstCount=4`

Benchmark               (maxNum)  (threadCount)  Mode  Cnt   Score    Error  Units
ThreadOnSpinWait.trial   1000000              2  avgt    3  22.181 ± 11.592  ms/op

With `-XX:OnSpinWaitInst=isb -XX:OnSpinWaitInstCount=1`

Benchmark               (maxNum)  (threadCount)  Mode  Cnt   Score    Error  Units
ThreadOnSpinWait.trial   1000000              2  avgt    3  36.281 ± 31.700  ms/op


This is Apple M1, where you have to be very careful because there's some processor
frequency scaling going on.

-------------

PR: https://git.openjdk.java.net/jdk/pull/5562


More information about the hotspot-dev mailing list