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