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 import java.lang.reflect.Executable;
  24 import java.util.concurrent.Callable;
  25 import java.util.Objects;
  26 import compiler.whitebox.CompilerWhiteBoxTest;
  27 /*
  28  * @test
  29  * @bug 8130832
  30  * @library /testlibrary /test/lib /compiler/whitebox /compiler/testlibrary /
  31  * @build IntrinsicAvailableTest
  32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -Xbootclasspath/a:.
  35  *                   -XX:+UnlockDiagnosticVMOptions
  36  *                   -XX:+WhiteBoxAPI
  37  *                   -XX:+UseCRC32Intrinsics
  38  *                   IntrinsicAvailableTest
  39  * @run main/othervm -Xbootclasspath/a:.
  40  *                   -XX:+UnlockDiagnosticVMOptions
  41  *                   -XX:+WhiteBoxAPI
  42  *                   -XX:-UseCRC32Intrinsics
  43  *                   IntrinsicAvailableTest
  44  */
  45 public class IntrinsicAvailableTest extends CompilerWhiteBoxTest {
  46     protected String VMName;
  47 
  48     public IntrinsicAvailableTest(IntrinsicAvailableTestTestCase testCase) {
  49         super(testCase);
  50         VMName = System.getProperty("java.vm.name");
  51     }
  52 
  53     public static class IntrinsicAvailableTestTestCase implements TestCase {
  54 
  55         public String name() {
  56             return "IntrinsicAvailableTestTestCase";
  57         }
  58 
  59         public Executable getExecutable() {
  60             // Using a single method to test the
  61             // WhiteBox.isIntrinsicAvailable(Executable method, int compLevel)
  62             // call for the compilation level corresponding to both the C1 and C2
  63             // compiler keeps the current test simple.
  64             //
  65             // The tested method is java.util.zip.CRC32.update(int, int) because
  66             // both C1 and C2 define an intrinsic for the method and
  67             // the UseCRC32Intrinsics flag can be used to enable/disable
  68             // intrinsification of the method in both product and fastdebug
  69             // builds.
  70             try {
  71                 return Class.forName("java.util.zip.CRC32").getDeclaredMethod("update", int.class, int.class);
  72             } catch (NoSuchMethodException e) {
  73                 throw new RuntimeException("Test bug, method unavailable. " + e);
  74             } catch (ClassNotFoundException e) {
  75                 throw new RuntimeException("Test bug, class unavailable. " + e);
  76             }
  77         }
  78 
  79         public Callable<Integer> getCallable() {
  80             return null;
  81         }
  82 
  83         public boolean isOsr() {
  84             return false;
  85         }
  86 
  87     }
  88 
  89     protected void checkIntrinsicForCompilationLevel(Executable method, int compLevel) throws Exception {
  90         boolean intrinsicEnabled = Boolean.valueOf(getVMOption("UseCRC32Intrinsics"));
  91         boolean intrinsicAvailable = WHITE_BOX.isIntrinsicAvailable(method,
  92                                                                     compLevel);
  93 
  94         String intrinsicEnabledMessage = intrinsicEnabled ? "enabled" : "disabled";
  95         String intrinsicAvailableMessage = intrinsicAvailable ? "available" : "not available";
  96 
  97         if (intrinsicEnabled == intrinsicAvailable) {
  98             System.out.println("Expected result: intrinsic for java.util.zip.CRC32.update() is " +
  99                                intrinsicEnabledMessage + " and intrinsic is " + intrinsicAvailableMessage +
 100                                " at compilation level " + compLevel);
 101         } else {
 102             throw new RuntimeException("Unexpected result: intrinsic for java.util.zip.CRC32.update() is " +
 103                                        intrinsicEnabledMessage + " but intrinsic is " + intrinsicAvailableMessage +
 104                                        " at compilation level " + compLevel);
 105         }
 106     }
 107 
 108     protected boolean isServerVM() {
 109         return VMName.toLowerCase().contains("server");
 110     }
 111 
 112     public void test() throws Exception {
 113         Executable intrinsicMethod = testCase.getExecutable();
 114         if (isServerVM()) {
 115             if (TIERED_COMPILATION) {
 116                 checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_SIMPLE);
 117             }
 118             checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_FULL_OPTIMIZATION);
 119         } else {
 120             checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_SIMPLE);
 121         }
 122     }
 123 
 124     public static void main(String args[]) throws Exception {
 125         new IntrinsicAvailableTest(new IntrinsicAvailableTestTestCase()).test();
 126     }
 127 }