Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/compiler/CodelistTest.java
41153 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test CodelistTest25* @bug 805488926* @library /test/lib /27* @modules java.base/jdk.internal.misc28* java.compiler29* java.management30* jdk.internal.jvmstat/sun.jvmstat.monitor31* @build sun.hotspot.WhiteBox32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33* @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -Xmixed -XX:-BackgroundCompilation CodelistTest34* @run testng/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-UseCodeCacheFlushing -Xint CodelistTest35* @summary Test of diagnostic command Compiler.codelist36*37* Flag comment:38* -XX:-UseCodeCacheFlushing - to prevent methods from being removed from the code cache before we have checked the results39*40* This test should never run in the same VM as other tests - the code cache may get huge which will41* create an enormous amount of output to parse. Same for -Xcomp.42*/4344import compiler.testlibrary.CompilerUtils;45import compiler.whitebox.CompilerWhiteBoxTest;46import jdk.test.lib.process.OutputAnalyzer;47import jdk.test.lib.dcmd.CommandExecutor;48import jdk.test.lib.dcmd.JMXExecutor;49import org.testng.annotations.Test;50import org.testng.Assert;51import sun.hotspot.WhiteBox;5253import java.lang.reflect.Method;54import java.util.Iterator;5556public class CodelistTest {5758/**59* This test calls Jcmd (diagnostic command tool) Compiler.codelist and then parses the output,60* making sure that the first methods in the list is valid by reflection.61*62* Output example:63*64* 6 0 java.lang.System.arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V [0x00007f7b49200910, 0x00007f7b49200aa0 - 0x00007f7b49200d30]65* 2 3 java.lang.String.indexOf(II)I [0x00007f7b49200d90, 0x00007f7b49200f60 - 0x00007f7b49201490]66* 7 3 java.lang.Math.min(II)I [0x00007f7b4922f010, 0x00007f7b4922f180 - 0x00007f7b4922f338]67* 8 3 java.lang.String.equals(Ljava/lang/Object;)Z [0x00007f7b4922fb10, 0x00007f7b4922fd40 - 0x00007f7b49230698]68* 9 3 java.lang.AbstractStringBuilder.ensureCapacityInternal(I)V [0x00007f7b49232010, 0x00007f7b492321a0 - 0x00007f7b49232510]69* 10 1 java.lang.Object.<init>()V [0x00007f7b49233e90, 0x00007f7b49233fe0 - 0x00007f7b49234118]70*71*/7273protected static final WhiteBox WB = WhiteBox.getWhiteBox();7475public void run(CommandExecutor executor) {7677TestCase[] testcases = {78new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE, "testcaseMethod1"),79new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_LIMITED_PROFILE, "testcaseMethod2"),80new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE, "testcaseMethod3"),81new TestCase(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION, "testcaseMethod4"),82};8384String directive = "{ match: \"CodelistTest.testcaseMethod*\", " +85"BackgroundCompilation: false }";86Assert.assertTrue(87WB.addCompilerDirective(directive) == 1,88"Must succeed");8990try {91// Enqueue one test method for each available level92int[] complevels = CompilerUtils.getAvailableCompilationLevels();93for (int level : complevels) {94// Only test comp level 1 and 4 - level 1, 2 and 3 may interfere with each other95if (level == 1 || level == 4) {96TestCase testcase = testcases[level - 1];97WB.enqueueMethodForCompilation(testcase.method, testcase.level);98// Set results to false for those methods we must to find99// We will also assert if we find any test method we don't expect100testcase.check = false;101}102}103} finally {104WB.removeCompilerDirective(1);105}106107// Get output from dcmd (diagnostic command)108OutputAnalyzer output = executor.execute("Compiler.codelist");109Iterator<String> lines = output.asLines().iterator();110111// Loop over output set result for all found methods112while (lines.hasNext()) {113String line = lines.next();114115// Fast check for common part of method name116if (line.contains("CodelistTest.testcaseMethod")) {117String[] parts = line.split(" ");118int compileID = Integer.parseInt(parts[0]);119Assert.assertTrue(compileID > 0, "CompileID must be positive");120121int compileLevel = Integer.parseInt(parts[1]);122Assert.assertTrue(compileLevel >= -1, "CompileLevel must be at least -1 (Any)");123Assert.assertTrue(compileLevel <= 4, "CompileLevel must be at most 4 (C2)");124125int codeState = Integer.parseInt(parts[2]);126Assert.assertTrue(codeState >= 0, "CodeState must be at least 0 (In Use)");127Assert.assertTrue(codeState <= 4, "CodeState must be at most 4 (Unloaded)");128129String str = parts[3];130for (TestCase testcase : testcases) {131if (str.contains(testcase.methodName)) {132Assert.assertFalse(testcase.check, "Must not be found or already found.");133Assert.assertTrue(testcase.level == compileLevel, "Must have correct level");134testcase.check = true;135}136}137}138}139140// Check all testcases that was run141for (TestCase testcase : testcases) {142Assert.assertTrue(testcase.check, "Missing testcase " + testcase.methodName);143}144}145146@Test147public void jmx() {148run(new JMXExecutor());149}150151public void testcaseMethod1() {152}153154public void testcaseMethod2() {155}156157public void testcaseMethod3() {158}159160public void testcaseMethod4() {161}162163public static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {164try {165return klass.getDeclaredMethod(name, parameterTypes);166} catch (NoSuchMethodException | SecurityException e) {167throw new RuntimeException("exception on getting method Helper." + name, e);168}169}170171class TestCase {172Method method;173int level;174String methodName;175Boolean check;176177public TestCase(int level, String methodName) {178this.method = getMethod(CodelistTest.class, methodName);179this.level = level;180this.methodName = methodName;181this.check = true;182}183}184}185186187