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