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

Evgeny Astigeevich duke at openjdk.java.net
Wed Oct 20 11:05:11 UTC 2021


On Fri, 15 Oct 2021 13:09:27 GMT, Andrew Haley <aph at openjdk.org> wrote:

>> 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() {
>         theCounter.set(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.

Hi @theRealAph,
Any comments on the microbenchmarks?

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

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


More information about the hotspot-dev mailing list