1 /*
   2  * Copyright (c) 2014, 2017, 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 CodelistTest
  26  * @bug 8054889
  27  * @library /test/lib /
  28  * @modules java.base/jdk.internal.misc
  29  *          java.compiler
  30  *          java.management
  31  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  32  * @build sun.hotspot.WhiteBox
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  34  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -Xmixed CodelistTest
  36  * @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -Xint CodelistTest
  37  * @summary Test of diagnostic command Compiler.codelist
  38  *
  39  * Flag comment:
  40  * -XX:-UseCodeCacheFlushing - to prevent methods from being removed from the code cache before we have checked the results
  41  *
  42  * This test should never run in the same VM as other tests - the code cache may get huge which will
  43  * create an enormous amount of output to parse. Same for -Xcomp.
  44  */
  45 
  46 import compiler.testlibrary.CompilerUtils;
  47 import compiler.whitebox.CompilerWhiteBoxTest;
  48 import jdk.test.lib.process.OutputAnalyzer;
  49 import jdk.test.lib.dcmd.CommandExecutor;
  50 import jdk.test.lib.dcmd.JMXExecutor;
  51 import org.testng.annotations.Test;
  52 import org.testng.Assert;
  53 import sun.hotspot.WhiteBox;
  54 
  55 import java.lang.reflect.Method;
  56 import java.util.Iterator;
  57 
  58 public class CodelistTest {
  59 
  60     /**
  61      * This test calls Jcmd (diagnostic command tool) Compiler.codelist and then parses the output,
  62      * making sure that the first methods in the list is valid by reflection.
  63      *
  64      * Output example:
  65      *
  66      * 6 0 java.lang.System.arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V [0x00007f7b49200910, 0x00007f7b49200aa0 - 0x00007f7b49200d30]
  67      * 2 3 java.lang.String.indexOf(II)I [0x00007f7b49200d90, 0x00007f7b49200f60 - 0x00007f7b49201490]
  68      * 7 3 java.lang.Math.min(II)I [0x00007f7b4922f010, 0x00007f7b4922f180 - 0x00007f7b4922f338]
  69      * 8 3 java.lang.String.equals(Ljava/lang/Object;)Z [0x00007f7b4922fb10, 0x00007f7b4922fd40 - 0x00007f7b49230698]
  70      * 9 3 java.lang.AbstractStringBuilder.ensureCapacityInternal(I)V [0x00007f7b49232010, 0x00007f7b492321a0 - 0x00007f7b49232510]
  71      * 10 1 java.lang.Object.<init>()V [0x00007f7b49233e90, 0x00007f7b49233fe0 - 0x00007f7b49234118]
  72      *
  73      */
  74 
  75     protected static final WhiteBox WB = WhiteBox.getWhiteBox();
  76 
  77     public void run(CommandExecutor executor) {
  78 
  79         TestCase[] testcases = {
  80                 new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE, "testcaseMethod1"),
  81                 new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_LIMITED_PROFILE, "testcaseMethod2"),
  82                 new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE, "testcaseMethod3"),
  83                 new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION, "testcaseMethod4"),
  84         };
  85 
  86         String directive = "{ match: \"CodelistTest.testcaseMethod*\", " +
  87                 "BackgroundCompilation: false }";
  88         Assert.assertTrue(
  89                 WB.addCompilerDirective(directive) == 1,
  90                 "Must succeed");
  91 
  92         try {
  93             // Enqueue one test method for each available level
  94             int[] complevels = CompilerUtils.getAvailableCompilationLevels();
  95             for (int level : complevels) {
  96                 // Only test comp level 1 and 4 - level 1, 2 and 3 may interfere with each other
  97                 if (level == 1 || level == 4) {
  98                     TestCase testcase = testcases[level - 1];
  99                     WB.enqueueMethodForCompilation(testcase.method, testcase.level);
 100                     // Set results to false for those methods we must to find
 101                     // We will also assert if we find any test method we don't expect
 102                     testcase.check = false;
 103                 }
 104             }
 105         } finally {
 106             WB.removeCompilerDirective(1);
 107         }
 108 
 109         // Get output from dcmd (diagnostic command)
 110         OutputAnalyzer output = executor.execute("Compiler.codelist");
 111         Iterator<String> lines = output.asLines().iterator();
 112 
 113         // Loop over output set result for all found methods
 114         while (lines.hasNext()) {
 115             String line = lines.next();
 116 
 117             // Fast check for common part of method name
 118             if (line.contains("CodelistTest.testcaseMethod")) {
 119                 String[] parts = line.split(" ");
 120                 int compileID = Integer.parseInt(parts[0]);
 121                 int compileLevel = Integer.parseInt(parts[1]);
 122                 String str = parts[2];
 123 
 124                 for (TestCase testcase : testcases) {
 125                     if (str.contains(testcase.methodName)) {
 126                         Assert.assertFalse(testcase.check, "Must not be found or already found.");
 127                         Assert.assertTrue(testcase.level == compileLevel, "Must have correct level");
 128                         testcase.check = true;
 129                     }
 130                 }
 131             }
 132         }
 133 
 134         // Check all testcases that was run
 135         for (TestCase testcase : testcases) {
 136             Assert.assertTrue(testcase.check, "Missing testcase " + testcase.methodName);
 137         }
 138     }
 139 
 140     @Test
 141     public void jmx() {
 142         run(new JMXExecutor());
 143     }
 144 
 145     public void testcaseMethod1() {
 146     }
 147 
 148     public void testcaseMethod2() {
 149     }
 150 
 151     public void testcaseMethod3() {
 152     }
 153 
 154     public void testcaseMethod4() {
 155     }
 156 
 157     public static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
 158         try {
 159             return klass.getDeclaredMethod(name, parameterTypes);
 160         } catch (NoSuchMethodException | SecurityException e) {
 161             throw new RuntimeException("exception on getting method Helper." + name, e);
 162         }
 163     }
 164 
 165     class TestCase {
 166         Method method;
 167         int level;
 168         String methodName;
 169         Boolean check;
 170 
 171         public TestCase(int level, String methodName) {
 172             this.method = getMethod(CodelistTest.class, methodName);
 173             this.level = level;
 174             this.methodName = methodName;
 175             this.check = true;
 176         }
 177     }
 178 }