RFR: 8261445: Use memory_order_relaxed for os::random().

Aleksey Shipilev shade at openjdk.java.net
Tue Feb 9 18:26:41 UTC 2021


On Tue, 9 Feb 2021 18:22:36 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> os::random() is used a lot (once for every class) during HotSpot initialization, and it defaults to using a compare-and-swap operation with memory_order_conservative. We don't need that: it should be memory_order_relaxed.
>
> It seems superficially fine, given unsynchronized read of `_rand_seed` prior to that. But I have to wonder if in absence of any memory ordering semantics, this loop is guaranteed to terminate? That is, can't a sufficiently hostile optimizer reduce it to the CAS loop that always expects a constant "oldval" seed?

...and yes, it seems to (borderline imperceptibly) improve startup :)

 Performance counter stats for 'taskset -c 13 build/linux-x86_64-server-release/images/jdk/bin/java -Xms128m -Xmx128m -Xint Hello' (5000 runs):

             23.27 msec task-clock                #    0.940 CPUs utilized            ( +-  0.01% )
        85,860,247      cycles                    #    3.690 GHz                      ( +-  0.01% )
        80,060,520      instructions              #    0.93  insn per cycle           ( +-  0.00% )

        0.02476574 +- 0.00000328 seconds time elapsed  ( +-  0.01% )

 Performance counter stats for 'taskset -c 13 build/linux-x86_64-server-release/images/jdk/bin/java -Xms128m -Xmx128m -Xint Hello' (5000 runs):

             23.22 msec task-clock                #    0.940 CPUs utilized            ( +-  0.01% )
        85,659,272      cycles                    #    3.689 GHz                      ( +-  0.01% )
        80,063,528      instructions              #    0.93  insn per cycle           ( +-  0.00% )

        0.02471087 +- 0.00000271 seconds time elapsed  ( +-  0.01% )

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

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


More information about the hotspot-runtime-dev mailing list