test/serviceability/dcmd/compiler/CodelistTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff test/serviceability/dcmd/compiler

test/serviceability/dcmd/compiler/CodelistTest.java

Print this page
rev 10345 : 8073793: serviceability/dcmd/compiler/CodelistTest.java fails with ClassNotFoundException trying to load VM anonymous class
Summary: Make test less fragile using whitebox API
Reviewed-by:


   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
  28  * @modules java.base/sun.misc
  29  *          java.compiler
  30  *          java.management
  31  *          jdk.jvmstat/sun.jvmstat.monitor
  32  * @build jdk.test.lib.*
  33  * @build jdk.test.lib.dcmd.*
  34  * @build MethodIdentifierParser
  35  * @run testng CodelistTest




  36  * @summary Test of diagnostic command Compiler.codelist






  37  */
  38 
  39 import org.testng.annotations.Test;
  40 import org.testng.Assert;
  41 
  42 import jdk.test.lib.OutputAnalyzer;
  43 import jdk.test.lib.dcmd.CommandExecutor;
  44 import jdk.test.lib.dcmd.JMXExecutor;



  45 
  46 import java.lang.reflect.Method;

  47 
  48 public class CodelistTest {
  49 
  50     /**
  51      * This test calls Jcmd (diagnostic command tool) Compiler.codelist and then parses the output,
  52      * making sure that the first methods in the list is valid by reflection.
  53      *
  54      * Output example:
  55      *
  56      * 6 0 java.lang.System.arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V [0x00007f7b49200910, 0x00007f7b49200aa0 - 0x00007f7b49200d30]
  57      * 2 3 java.lang.String.indexOf(II)I [0x00007f7b49200d90, 0x00007f7b49200f60 - 0x00007f7b49201490]
  58      * 7 3 java.lang.Math.min(II)I [0x00007f7b4922f010, 0x00007f7b4922f180 - 0x00007f7b4922f338]
  59      * 8 3 java.lang.String.equals(Ljava/lang/Object;)Z [0x00007f7b4922fb10, 0x00007f7b4922fd40 - 0x00007f7b49230698]
  60      * 9 3 java.lang.AbstractStringBuilder.ensureCapacityInternal(I)V [0x00007f7b49232010, 0x00007f7b492321a0 - 0x00007f7b49232510]
  61      * 10 1 java.lang.Object.<init>()V [0x00007f7b49233e90, 0x00007f7b49233fe0 - 0x00007f7b49234118]
  62      *
  63      */
  64 


  65     public void run(CommandExecutor executor) {
  66         int ok   = 0;
  67         int fail = 0;




























  68 
  69         // Get output from dcmd (diagnostic command)
  70         OutputAnalyzer output = executor.execute("Compiler.codelist");

  71 
  72         // Grab a method name from the output
  73         int count = 0;
  74 
  75         for (String line : output.asLines()) {
  76             count++;
  77 


  78             String[] parts = line.split(" ");
  79             // int compileID = Integer.parseInt(parts[0]);
  80             // int compileLevel = Integer.parseInt(parts[1]);
  81             String methodPrintedInLogFormat = parts[2];
  82 
  83             // skip inits, clinits, methodHandles and getUnsafe -
  84             // they can not be reflected
  85             if (methodPrintedInLogFormat.contains("<init>")) {
  86                 continue;

  87             }
  88             if (methodPrintedInLogFormat.contains("<clinit>")) {
  89                 continue;
  90             }
  91             if (methodPrintedInLogFormat.contains("MethodHandle")) {
  92                 continue;
  93             }
  94             if (methodPrintedInLogFormat.contains("sun.misc.Unsafe.getUnsafe")) {
  95                 continue;
  96             }
  97             if (methodPrintedInLogFormat.contains("jdk.internal.misc.Unsafe.getUnsafe")) {
  98                 continue;
  99             }
 100 
 101             MethodIdentifierParser mf = new MethodIdentifierParser(methodPrintedInLogFormat);
 102             Method m = null;
 103             try {
 104                 m = mf.getMethod();
 105             } catch (NoSuchMethodException e) {
 106                 m = null;
 107             } catch (ClassNotFoundException e) {
 108                 Assert.fail("Test error: Caught unexpected exception", e);
 109             }
 110             if (m == null) {
 111                 Assert.fail("Test failed on: " + methodPrintedInLogFormat);
 112             }
 113             if (count > 10) {
 114                 // Testing 10 entries is enough. Lets not waste time.
 115                 break;
 116             }
 117         }
 118     }
 119 
 120     @Test
 121     public void jmx() {
 122         run(new JMXExecutor());


































 123     }
 124 }


   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 be 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),
  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();
 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 }
test/serviceability/dcmd/compiler/CodelistTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File