Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/escape/AdaptiveBlocking/AdaptiveBlocking001/AdaptiveBlocking001.java
41160 views
/*1* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25*26* @summary converted from VM Testbase jit/escape/AdaptiveBlocking/AdaptiveBlocking001.27* VM Testbase keywords: [jit, quick]28* VM Testbase readme:29* This is a basic JIT compiler adaptive blocking test. The test runs 2 threads30* contending for a shared resource. In the first round, the lock is held for31* a short period of time, provoking spinlock-style locking. In the second round,32* the lock is held for a long period of time, provoking blocked locking. The33* described scenario is repeated numRounds (specified by the -numRounds34* parameter) times.35*36* @library /vmTestbase37* /test/lib38* @build jit.escape.AdaptiveBlocking.AdaptiveBlocking001.AdaptiveBlocking00139* @run driver/timeout=300 ExecDriver --java -server -Xcomp -XX:+DoEscapeAnalysis40* jit.escape.AdaptiveBlocking.AdaptiveBlocking001.AdaptiveBlocking001 -numRounds 1041*/4243package jit.escape.AdaptiveBlocking.AdaptiveBlocking001;4445import nsk.share.TestFailure;4647class AdaptiveBlocking00148{49public static int numRounds = 10;5051private static Object sharedLock = new Object();52private static boolean lockEntered1;53private static boolean lockEntered2;5455private static boolean latentLocks;5657private static boolean done;5859private static boolean failed;6061public static void main( String[] args )62{63System.out.println( "Adaptive blocking test" );6465parseArgs( args );6667for( int i=0; i < numRounds; ++i )68{69doRound(i, false);70doRound(i, true);71}7273if( !failed )74System.out.println( "TEST PASSED" );75else76throw new TestFailure( "TEST FAILED" );77}7879private static void parseArgs( String[] args )80{81for( int i=0; i < args.length; ++i )82{83String arg = args[i];84String val;8586if( arg.equals( "-numRounds" ) )87{88if( ++i < args.length )89val = args[i];90else91throw new TestFailure( "Need value for '" + arg + "' parameter" );9293try {94numRounds = Integer.parseInt( val );95} catch( NumberFormatException e ) {96throw new TestFailure( "Invalid value for '" + arg + "' parameter: " + val );97}98}99else100{101throw new TestFailure( "Invalid argument: " + args );102}103}104}105106private static void doRound(int ord, boolean latent_locks)107{108System.out.println( "round #" + ord + ", latent locks: " + (latent_locks ? "yes" : "no") + "..." );109110latentLocks = latent_locks;111112Thread_1 t1 = new Thread_1();113Thread_2 t2 = new Thread_2();114115done = false;116117t1.start();118t2.start();119120for( int i=0; i < 10; ++i )121{122try {123Thread.sleep( 1000 );124} catch( InterruptedException e ) {125}126127if( done )128break;129}130131done = true;132133try {134t1.join();135t2.join();136} catch( InterruptedException e ) {137}138}139140private static class Thread_1 extends Thread141{142public void run()143{144while( !done )145{146synchronized( sharedLock )147{148lockEntered1 = true;149if( lockEntered2 )150{151Fail();152done = true;153break;154}155156holdLock();157158lockEntered1 = false;159}160}161}162}163164private static class Thread_2 extends Thread165{166public void run()167{168while( !done )169{170synchronized( sharedLock )171{172lockEntered2 = true;173if( lockEntered1 )174{175Fail();176done = true;177break;178}179180holdLock();181182lockEntered2 = false;183}184}185}186}187188private static void holdLock()189{190if( latentLocks )191{192try {193Thread.sleep( 500 );194} catch( InterruptedException e ) {195}196}197}198199private static void Fail()200{201failed = true;202}203}204205206