Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestInstanceKlassSizeForInterface.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import java.util.List;2425import sun.jvm.hotspot.HotSpotAgent;26import sun.jvm.hotspot.utilities.SystemDictionaryHelper;27import sun.jvm.hotspot.oops.InstanceKlass;28import sun.jvm.hotspot.debugger.*;2930import jdk.test.lib.apps.LingeredApp;31import jdk.test.lib.Asserts;32import jdk.test.lib.JDKToolLauncher;33import jdk.test.lib.JDKToolFinder;34import jdk.test.lib.Platform;35import jdk.test.lib.process.ProcessTools;36import jdk.test.lib.process.OutputAnalyzer;37import jdk.test.lib.SA.SATestUtils;38import jdk.test.lib.Utils;3940/**41* @test42* @library /test/lib43* @requires vm.hasSA44* @modules java.base/jdk.internal.misc45* jdk.hotspot.agent/sun.jvm.hotspot46* jdk.hotspot.agent/sun.jvm.hotspot.utilities47* jdk.hotspot.agent/sun.jvm.hotspot.oops48* jdk.hotspot.agent/sun.jvm.hotspot.debugger49* @build sun.hotspot.WhiteBox50* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox51* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestInstanceKlassSizeForInterface52*/5354import sun.hotspot.WhiteBox;5556public class TestInstanceKlassSizeForInterface {5758public static WhiteBox wb = WhiteBox.getWhiteBox();5960private static LingeredAppWithInterface theApp = null;6162private static void SAInstanceKlassSize(int lingeredAppPid,63String[] instanceKlassNames) {6465HotSpotAgent agent = new HotSpotAgent();66try {67agent.attach(lingeredAppPid);68}69catch (DebuggerException e) {70System.out.println(e.getMessage());71System.err.println("Unable to connect to process ID: " + lingeredAppPid);7273agent.detach();74e.printStackTrace();75}7677for (String instanceKlassName : instanceKlassNames) {78InstanceKlass iKlass = SystemDictionaryHelper.findInstanceKlass(79instanceKlassName);80Asserts.assertNotNull(iKlass,81String.format("Unable to find instance klass for %s", instanceKlassName));82System.out.println("SA: The size of " + instanceKlassName +83" is " + iKlass.getSize());84}85agent.detach();86}8788private static String getJcmdInstanceKlassSize(OutputAnalyzer output,89String instanceKlassName) {90for (String s : output.asLines()) {91if (s.contains(instanceKlassName)) {92String tokens[];93System.out.println(s);94tokens = s.split("\\s+");95return tokens[3];96}97}98return null;99}100101private static void createAnotherToAttach(102String[] instanceKlassNames,103int lingeredAppPid) throws Exception {104// Start a new process to attach to the LingeredApp process to get SA info105ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(106"--add-modules=jdk.hotspot.agent",107"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",108"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",109"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",110"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",111"-XX:+UnlockDiagnosticVMOptions",112"-XX:+WhiteBoxAPI",113"-Xbootclasspath/a:.",114"TestInstanceKlassSizeForInterface",115Integer.toString(lingeredAppPid));116SATestUtils.addPrivilegesIfNeeded(processBuilder);117OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);118SAOutput.shouldHaveExitValue(0);119System.out.println(SAOutput.getOutput());120121// Match the sizes from both the output streams122for (String instanceKlassName : instanceKlassNames) {123Class<?> iklass = Class.forName(instanceKlassName);124System.out.println ("Trying to match for " + instanceKlassName);125String size = String.valueOf(wb.getKlassMetadataSize(iklass));126boolean match = false;127for (String s : SAOutput.asLines()) {128if (s.contains(instanceKlassName)) {129Asserts.assertTrue(130s.contains(size), "The size computed by SA for" +131instanceKlassName + " does not match.");132match = true;133}134}135Asserts.assertTrue(match, "Found a match for " + instanceKlassName);136}137}138139public static void main (String... args) throws Exception {140SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.141String[] instanceKlassNames = new String[] {142"Language",143"ParselTongue",144"LingeredAppWithInterface$1"145};146147if (args == null || args.length == 0) {148try {149theApp = new LingeredAppWithInterface();150LingeredApp.startApp(theApp);151createAnotherToAttach(instanceKlassNames,152(int)theApp.getPid());153} finally {154LingeredApp.stopApp(theApp);155}156} else {157SAInstanceKlassSize(Integer.parseInt(args[0]), instanceKlassNames);158}159}160}161162163