1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package vm.mlvm.indy.stress.java.loopsAndThreads;
  25 
  26 import java.lang.invoke.CallSite;
  27 import java.lang.invoke.ConstantCallSite;
  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  31 import java.util.concurrent.atomic.AtomicLong;
  32 
  33 import vm.mlvm.share.MlvmTest;
  34 import vm.mlvm.share.Env;
  35 import vm.mlvm.share.MultiThreadedTest;
  36 import nsk.share.test.Stresser;
  37 import nsk.share.Failure;
  38 
  39 public class INDIFY_Test extends MultiThreadedTest {
  40 
  41 #
  42 # final int INDY_SITES_COUNT = 1000;
  43 #
  44     private AtomicLong _counter = new AtomicLong();
  45     private AtomicLong _expectedTargetCalls = new AtomicLong();
  46 
  47     public INDIFY_Test() {
  48         super();
  49     }
  50 
  51     private static MethodType MT_bootstrap() {
  52         return MethodType.methodType(Object.class, Object.class, Object.class, Object.class);
  53     }
  54 
  55     private static MethodHandle MH_bootstrap() throws Exception {
  56         return MethodHandles.lookup().findStatic(
  57                 INDIFY_Test.class,
  58                 "bootstrap",
  59                 MT_bootstrap());
  60     }
  61 
  62     public static Object bootstrap(Object c, Object name, Object mt) throws Throwable {
  63         Env.traceVerbose("bootstrap: Class " + c + "; method name = " + name + "; method type = " + mt);
  64         CallSite cs = new ConstantCallSite(
  65                 MethodHandles.lookup().findVirtual(
  66                 INDIFY_Test.class,
  67                 "target",
  68                 MethodType.methodType(Object.class, String.class, int.class)));
  69         return cs;
  70     }
  71 
  72     public Object target(String s, int i) {
  73         Env.traceDebug("target called");
  74         _counter.incrementAndGet();
  75         return null;
  76     }
  77 
  78 #
  79 # for ( int j = 0; j < INDY_SITES_COUNT; j++ ) {
  80 #
  81     private static MethodHandle INDY_call@j;
  82     private static MethodHandle INDY_call@j () throws Throwable {
  83         if (INDY_call@j != null)
  84             return INDY_call@j;
  85 
  86         CallSite cs = (CallSite) MH_bootstrap().invokeWithArguments(
  87                 MethodHandles.lookup(),
  88                 "greet",
  89                 MethodType.methodType(Object.class, INDIFY_Test.class, String.class, int.class));
  90 
  91         return cs.dynamicInvoker();
  92     }
  93 #
  94 # }
  95 #
  96 
  97     public boolean runThread(int threadNum) throws Throwable {
  98         final INDIFY_Test x = this;
  99         final String s = "todo el mundo";
 100         final int i = 123;
 101 
 102         Stresser stresser = createStresser();
 103 
 104         stresser.start(1);
 105         while ( stresser.continueExecution() ) {
 106             stresser.iteration();
 107 
 108             long e;
 109             do {
 110                 e = _expectedTargetCalls.get();
 111             } while ( ! _expectedTargetCalls.compareAndSet(e, e + @INDY_SITES_COUNT) );
 112 
 113 #
 114 # for ( int j = 0; j < INDY_SITES_COUNT; j++ ) {
 115 #
 116             Object o@j = (Object) INDY_call@j ().invokeExact(x, s, i);
 117 #
 118 # }
 119 #
 120         }
 121 
 122         return true;
 123     }
 124 
 125     protected void finalizeTest(boolean ok) throws Throwable {
 126         String msg = "Target was called " + _counter.get() + " times of " + _expectedTargetCalls.get();
 127         if ( ok && _counter.get() != _expectedTargetCalls.get() )
 128             throw new Failure(msg);
 129         else
 130             Env.display(msg);
 131     }
 132 
 133     public static void main(String[] args) { MlvmTest.launch(args); }
 134 }