1 /*
   2  * Copyright (c) 2015, 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  * @library /testlibrary /test/lib
  28  * @compile GCTest.java NonInlinedReinvoker.java
  29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  30  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  31  *                              java.lang.invoke.GCTest
  32  *                              java.lang.invoke.GCTest$T
  33  *                              java.lang.invoke.NonInlinedReinvoker
  34  *                              jdk.test.lib.Asserts
  35  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions
  36  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  37  *                   -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=1
  38  *                      java.lang.invoke.GCTest
  39  */
  40 package java.lang.invoke;
  41 
  42 import sun.hotspot.WhiteBox;
  43 import jdk.internal.vm.annotation.DontInline;
  44 import jdk.internal.vm.annotation.Stable;
  45 import java.lang.ref.*;
  46 import static jdk.test.lib.Asserts.*;
  47 
  48 public class GCTest {
  49     static final MethodHandles.Lookup LOOKUP = MethodHandles.Lookup.IMPL_LOOKUP;
  50 
  51     static class T {
  52         static int f1() { return 0; }
  53         static int f2() { return 1; }
  54     }
  55 
  56     static @Stable MethodHandle mh;
  57     static PhantomReference<LambdaForm> lform;
  58 
  59     static final ReferenceQueue<LambdaForm> rq = new ReferenceQueue<>();
  60     static final WhiteBox WB = WhiteBox.getWhiteBox();
  61 
  62     @DontInline
  63     static int invokeBasic() {
  64         try {
  65             return (int) mh.invokeBasic();
  66         } catch (Throwable e) {
  67             throw new Error(e);
  68         }
  69     }
  70 
  71     static void test(int expected) {
  72         for (int i = 0; i < 20_000; i++) {
  73             invokeBasic();
  74         }
  75         assertEquals(invokeBasic(), expected);
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         mh = NonInlinedReinvoker.make(
  80                 LOOKUP.findStatic(T.class, "f1", MethodType.methodType(int.class)));
  81 
  82         // Monitor LambdaForm GC
  83         lform = new PhantomReference<>(mh.form, rq);
  84 
  85         test(0);
  86         WB.clearInlineCaches();
  87         test(0);
  88 
  89         mh = NonInlinedReinvoker.make(
  90                 LOOKUP.findStatic(T.class, "f2", MethodType.methodType(int.class)));
  91 
  92         Reference<?> ref = null;
  93         while (ref == null) {
  94             WB.fullGC();
  95             try {
  96                 ref = rq.remove(1000);
  97             } catch (InterruptedException e) { /*ignore*/ }
  98         }
  99 
 100         test(1);
 101         WB.clearInlineCaches();
 102         test(1);
 103 
 104         System.out.println("TEST PASSED");
 105     }
 106 }