test/hotspot/jtreg/compiler/jsr292/NonInlinedCall/GCTest.java
changeset 55301 a9188ba494a3
parent 55300 1e0b948cc122
child 55302 686dedba1d9a
equal deleted inserted replaced
55300:1e0b948cc122 55301:a9188ba494a3
     1 /*
       
     2  * Copyright (c) 2015, 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 /*
       
    25  * @test
       
    26  * @bug 8072008
       
    27  * @requires vm.opt.final.ClassUnloading
       
    28  * @library /test/lib ../patches
       
    29  * @modules java.base/jdk.internal.misc
       
    30  *          java.base/jdk.internal.vm.annotation
       
    31  *
       
    32  * @build java.base/java.lang.invoke.MethodHandleHelper
       
    33  * @build sun.hotspot.WhiteBox
       
    34  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions
       
    35  *                                 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
       
    36  *                                 -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=1
       
    37  *                                 -XX:+FoldStableValues
       
    38  *                                 compiler.jsr292.NonInlinedCall.GCTest
       
    39  */
       
    40 
       
    41 package compiler.jsr292.NonInlinedCall;
       
    42 
       
    43 import jdk.internal.vm.annotation.DontInline;
       
    44 import jdk.internal.vm.annotation.Stable;
       
    45 import sun.hotspot.WhiteBox;
       
    46 
       
    47 import java.lang.invoke.MethodHandle;
       
    48 import java.lang.invoke.MethodHandleHelper;
       
    49 import java.lang.invoke.MethodHandleHelper.NonInlinedReinvoker;
       
    50 import java.lang.invoke.MethodHandles;
       
    51 import java.lang.invoke.MethodType;
       
    52 import java.lang.ref.PhantomReference;
       
    53 import java.lang.ref.Reference;
       
    54 import java.lang.ref.ReferenceQueue;
       
    55 
       
    56 import static jdk.test.lib.Asserts.assertEquals;
       
    57 
       
    58 public class GCTest {
       
    59     static final MethodHandles.Lookup LOOKUP = MethodHandleHelper.IMPL_LOOKUP;
       
    60 
       
    61     static class T {
       
    62         static int f1() { return 0; }
       
    63         static int f2() { return 1; }
       
    64     }
       
    65 
       
    66     static @Stable MethodHandle mh;
       
    67     static PhantomReference<Object> lform;
       
    68 
       
    69     static final ReferenceQueue<Object> rq = new ReferenceQueue<>();
       
    70     static final WhiteBox WB = WhiteBox.getWhiteBox();
       
    71 
       
    72     @DontInline
       
    73     static int invokeBasic() {
       
    74         try {
       
    75             return MethodHandleHelper.invokeBasicI(mh);
       
    76         } catch (Throwable e) {
       
    77             throw new Error(e);
       
    78         }
       
    79     }
       
    80 
       
    81     static void test(int expected) {
       
    82         for (int i = 0; i < 20_000; i++) {
       
    83             invokeBasic();
       
    84         }
       
    85         assertEquals(invokeBasic(), expected);
       
    86     }
       
    87 
       
    88     public static void main(String[] args) throws Exception {
       
    89         mh = NonInlinedReinvoker.make(
       
    90                 LOOKUP.findStatic(T.class, "f1", MethodType.methodType(int.class)));
       
    91 
       
    92         // Monitor LambdaForm GC
       
    93         lform = new PhantomReference<>(MethodHandleHelper.getLambdaForm(mh), rq);
       
    94 
       
    95         test(0);
       
    96         WB.clearInlineCaches();
       
    97         test(0);
       
    98 
       
    99         mh = NonInlinedReinvoker.make(
       
   100                 LOOKUP.findStatic(T.class, "f2", MethodType.methodType(int.class)));
       
   101 
       
   102         Reference<?> ref = null;
       
   103         while (ref == null) {
       
   104             WB.fullGC();
       
   105             try {
       
   106                 ref = rq.remove(1000);
       
   107             } catch (InterruptedException e) { /*ignore*/ }
       
   108         }
       
   109 
       
   110         test(1);
       
   111         WB.clearInlineCaches();
       
   112         test(1);
       
   113 
       
   114         System.out.println("TEST PASSED");
       
   115     }
       
   116 }