--- /dev/null 2019-10-31 15:42:43.283000000 +0000 +++ new/test/hotspot/jtreg/runtime/handshake/HandshakeDirectTest.java 2020-01-15 18:31:21.703619980 +0000 @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +/* + * @test HandshakeDirectTest + * @summary This test tries to stress direct handshakes between threads. + * @library /testlibrary /test/lib + * @build HandshakeDirectTest + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+SafepointALot -XX:BiasedLockingDecayTime=100000000 -XX:BiasedLockingBulkRebiasThreshold=1000000 -XX:BiasedLockingBulkRevokeThreshold=1000000 HandshakeDirectTest + */ + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.Semaphore; +import java.io.*; + +public class HandshakeDirectTest implements Runnable { + static final int WORKING_THREADS = 32; + static final int DIRECT_HANDSHAKES_MARK = 50000; + static Thread[] workingThreads = new Thread[WORKING_THREADS]; + static Semaphore[] handshakeSem = new Semaphore[WORKING_THREADS]; + static Object[] locks = new Object[WORKING_THREADS]; + static boolean[] isBiased = new boolean[WORKING_THREADS]; + static AtomicInteger handshakeCount = new AtomicInteger(0); + + @Override + public void run() { + int me = Integer.parseInt(Thread.currentThread().getName()); + + while (true) { + try { + if (!isBiased[me]) { + handshakeSem[me].acquire(); + synchronized(locks[me]) { + isBiased[me] = true; + } + handshakeSem[me].release(); + } + + // Handshake directly some other worker + int handshakee = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1); + if (handshakee == me) { + handshakee = handshakee != 0 ? handshakee - 1 : handshakee + 1; + } + handshakeSem[handshakee].acquire(); + if (isBiased[handshakee]) { + // Revoke biased lock + synchronized(locks[handshakee]) { + handshakeCount.incrementAndGet(); + } + // Create new lock to be biased + locks[handshakee] = new Object(); + isBiased[handshakee] = false; + } + handshakeSem[handshakee].release(); + if (handshakeCount.get() >= DIRECT_HANDSHAKES_MARK) { + break; + } + } catch(InterruptedException ie) { + throw new Error("Unexpected interrupt"); + } + } + } + + public static void main(String... args) throws Exception { + HandshakeDirectTest test = new HandshakeDirectTest(); + + // Initialize semaphores + for (int i = 0; i < WORKING_THREADS; i++) { + handshakeSem[i] = new Semaphore(1); + } + + // Initialize locks + for (int i = 0; i < WORKING_THREADS; i++) { + locks[i] = new Object(); + } + + // Fire-up working threads. + for (int i = 0; i < WORKING_THREADS; i++) { + workingThreads[i] = new Thread(test, Integer.toString(i)); + workingThreads[i].setDaemon(true); + workingThreads[i].start(); + } + + // Wait until the desired number of direct handshakes is reached + workingThreads[0].join(); + } +}