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