1 /*
   2  * Copyright (c) 2014, 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 CompilerQueueTest
  26  * @bug 8054889
  27  * @library /test/lib /
  28  * @modules java.base/jdk.internal.misc
  29  *          java.compiler
  30  *          java.management
  31  *          jdk.jvmstat/sun.jvmstat.monitor
  32  * @summary Test of diagnostic command Compiler.queue
  33  * @build sun.hotspot.WhiteBox
  34  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  35  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -Xmixed -XX:+WhiteBoxAPI CompilerQueueTest
  37  * @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -Xmixed -XX:-TieredCompilation -XX:+WhiteBoxAPI CompilerQueueTest
  38  * @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -Xint -XX:+WhiteBoxAPI CompilerQueueTest
  39  */
  40 
  41 import compiler.testlibrary.CompilerUtils;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.dcmd.CommandExecutor;
  44 import jdk.test.lib.dcmd.JMXExecutor;
  45 import org.testng.annotations.Test;
  46 import org.testng.Assert;
  47 import sun.hotspot.WhiteBox;
  48 
  49 import java.lang.reflect.Executable;
  50 import java.lang.reflect.Method;
  51 import java.util.Iterator;
  52 
  53 public class CompilerQueueTest {
  54 
  55     /**
  56      * This test calls Jcmd (diagnostic command tool) Compiler.queue and
  57      * then parses the output, making sure that the output look ok.
  58      *
  59      *
  60      * Output example:
  61      *
  62      * Current compiles:
  63      * C1 CompilerThread14 267       3       java.net.URLStreamHandler::parseURL (1166 bytes)
  64      * C1 CompilerThread13 760       3       javax.management.StandardMBean::getDescription (11 bytes)
  65      * C1 CompilerThread12 757  s    3       com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory::getMapping (27 bytes)
  66      * C1 CompilerThread11 756  s!   3       com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory::mappingForType (110 bytes)
  67      * C1 CompilerThread10 761       3       java.lang.StringLatin1::indexOf (121 bytes)
  68      * C2 CompilerThread7 769       4       CompilerQueueTest::testcaseMethod4 (1 bytes)
  69      *
  70      * C1 compile queue:
  71      * 762       3       java.lang.invoke.MethodType::basicType (8 bytes)
  72      * 763       3       java.util.ArrayList::rangeCheck (22 bytes)
  73      * 764       3       java.util.ArrayList::elementData (7 bytes)
  74      * 765       3       jdk.internal.org.objectweb.asm.MethodVisitor::<init> (35 bytes)
  75      * 766       1       CompilerQueueTest::testcaseMethod1 (1 bytes)
  76      * 767       2       CompilerQueueTest::testcaseMethod2 (1 bytes)
  77      * 768       3       CompilerQueueTest::testcaseMethod3 (1 bytes)
  78      * 770       3       java.util.Properties::getProperty (46 bytes)
  79      *
  80      * C2 compile queue:
  81      * Empty
  82      *
  83      **/
  84 
  85     protected static final WhiteBox WB = WhiteBox.getWhiteBox();
  86 
  87     public void run(CommandExecutor executor) {
  88 
  89         TestCase[] testcases = {
  90                 new TestCase(1, "testcaseMethod1"),
  91                 new TestCase(2, "testcaseMethod2"),
  92                 new TestCase(3, "testcaseMethod3"),
  93                 new TestCase(4, "testcaseMethod4"),
  94         };
  95 
  96         // Lock compilation makes all compiles stay in queue or compile thread before completion
  97         WB.lockCompilation();
  98 
  99         // Enqueue one test method for each available level
 100         int[] complevels = CompilerUtils.getAvailableCompilationLevels();
 101         for (int level : complevels) {
 102             TestCase testcase = testcases[level - 1];
 103 
 104             boolean added = WB.enqueueMethodForCompilation(testcase.method, testcase.level);
 105             // Set results to false for those methods we must to find
 106             // We will also assert if we find any test method we don't expect
 107             Assert.assertEquals(added, WB.isMethodQueuedForCompilation(testcase.method));
 108             testcase.check = false;
 109         }
 110 
 111         // Get output from dcmd (diagnostic command)
 112         OutputAnalyzer output = executor.execute("Compiler.queue");
 113         Iterator<String> lines = output.asLines().iterator();
 114 
 115         // Loop over output set result for all found methods
 116         while (lines.hasNext()) {
 117             String str = lines.next();
 118             // Fast check for common part of method name
 119             if (str.contains("testcaseMethod")) {
 120                 for (TestCase testcase : testcases) {
 121                     if (str.contains(testcase.methodName)) {
 122                         Assert.assertFalse(testcase.check, "Must not be found or already found.");
 123                         testcase.check = true;
 124                     }
 125                 }
 126             }
 127         }
 128 
 129         for (TestCase testcase : testcases) {
 130             if (!testcase.check) {
 131                 // If this method wasn't found it must have been removed by policy,
 132                 // verify that it is now removed from the queue
 133                 Assert.assertFalse(WB.isMethodQueuedForCompilation(testcase.method), "Must be found or not in queue");
 134             }
 135             // Otherwise all good.
 136         }
 137 
 138         // Enable compilations again
 139         WB.unlockCompilation();
 140     }
 141 
 142     @Test
 143     public void jmx() {
 144         run(new JMXExecutor());
 145     }
 146 
 147     public void testcaseMethod1() {
 148     }
 149 
 150     public void testcaseMethod2() {
 151     }
 152 
 153     public void testcaseMethod3() {
 154     }
 155 
 156     public void testcaseMethod4() {
 157     }
 158 
 159     public static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
 160         try {
 161             return klass.getDeclaredMethod(name, parameterTypes);
 162         } catch (NoSuchMethodException | SecurityException e) {
 163             throw new RuntimeException("exception on getting method Helper." + name, e);
 164         }
 165     }
 166 
 167     class TestCase {
 168         Method method;
 169         int level;
 170         String methodName;
 171         Boolean check;
 172 
 173         public TestCase(int level, String methodName) {
 174             this.method = getMethod(CompilerQueueTest.class, methodName);
 175             this.level = level;
 176             this.methodName = methodName;
 177             this.check = true;
 178         }
 179     }
 180 
 181 }