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 8074551
  27  * @library /testlibrary
  28  * @run main/othervm -Xbatch -XX:-TieredCompilation PollutedTrapCounts
  29  */
  30 import java.lang.invoke.*;
  31 import jdk.test.lib.*;
  32 
  33 public class PollutedTrapCounts {
  34     public static void main(String[] args) throws Exception {
  35         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  36                 "-XX:+IgnoreUnrecognizedVMOptions",
  37                 "-XX:-TieredCompilation", "-Xbatch",
  38                 "-XX:PerBytecodeRecompilationCutoff=10", "-XX:PerMethodRecompilationCutoff=10",
  39                 "-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining",
  40                     "PollutedTrapCounts$Test");
  41 
  42         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
  43 
  44         analyzer.shouldHaveExitValue(0);
  45 
  46         analyzer.shouldNotContain("not compilable (disabled)");
  47     }
  48 
  49     static class Test {
  50         public static final MethodHandle test1;
  51         public static final MethodHandle test2;
  52         public static final MethodHandle empty;
  53 
  54         static {
  55             try {
  56                 Class<?> THIS_CLASS = Test.class;
  57                 MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
  58                 test1 = LOOKUP.findStatic(THIS_CLASS, "test1", MethodType.methodType(boolean.class, boolean.class));
  59                 test2 = LOOKUP.findStatic(THIS_CLASS, "test2", MethodType.methodType(boolean.class, boolean.class));
  60                 empty = LOOKUP.findStatic(THIS_CLASS, "empty", MethodType.methodType(void.class, boolean.class));
  61             } catch(Throwable e) {
  62                 throw new Error(e);
  63             }
  64         }
  65 
  66         static boolean test1(boolean b) {
  67             return b;
  68         }
  69         static boolean test2(boolean b) {
  70             return true;
  71         }
  72         static void    empty(boolean b) {}
  73 
  74         static void test(boolean freqValue, boolean removeInlineBlocker) throws Throwable {
  75             MethodHandle innerGWT = MethodHandles.guardWithTest(test1, empty, empty);
  76             MethodHandle outerGWT = MethodHandles.guardWithTest(test2, innerGWT, innerGWT);
  77 
  78             // Trigger compilation
  79             for (int i = 0; i < 20_000; i++) {
  80                 outerGWT.invokeExact(freqValue);
  81             }
  82 
  83             // Trigger deopt & nmethod invalidation
  84             outerGWT.invokeExact(!freqValue);
  85 
  86             // Force inline blocker removal on rare-taken path
  87             if (removeInlineBlocker) {
  88                 for (int i = 0; i < 100; i++) {
  89                     outerGWT.invokeExact(!freqValue);
  90                 }
  91             }
  92 
  93             // Trigger recompilation
  94             for (int i = 0; i < 20_000; i++) {
  95                 outerGWT.invokeExact(freqValue);
  96             }
  97         }
  98 
  99         public static void main(String[] args) throws Throwable {
 100             boolean freqValue = true;
 101             boolean removeInlineBlocker = false;
 102             for (int i = 0; i < 20; i++) {
 103                 test(freqValue, removeInlineBlocker);
 104                 freqValue = !freqValue;
 105                 removeInlineBlocker = !removeInlineBlocker;
 106             }
 107         }
 108     }
 109 }