Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestInstanceKlassSize.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 sun.jvm.hotspot.HotSpotAgent;24import sun.jvm.hotspot.utilities.SystemDictionaryHelper;25import sun.jvm.hotspot.oops.InstanceKlass;26import sun.jvm.hotspot.debugger.*;2728import java.util.ArrayList;29import java.util.List;30import java.util.stream.Collectors;3132import jdk.test.lib.apps.LingeredApp;33import jdk.test.lib.Asserts;34import jdk.test.lib.JDKToolLauncher;35import jdk.test.lib.Platform;36import jdk.test.lib.process.ProcessTools;37import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.SA.SATestUtils;39import jdk.test.lib.Utils;4041import java.io.*;42import java.util.*;4344/**45* @test46* @library /test/lib47* @requires vm.hasSA48* @modules java.base/jdk.internal.misc49* jdk.hotspot.agent/sun.jvm.hotspot50* jdk.hotspot.agent/sun.jvm.hotspot.utilities51* jdk.hotspot.agent/sun.jvm.hotspot.oops52* jdk.hotspot.agent/sun.jvm.hotspot.debugger53* @build sun.hotspot.WhiteBox54* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox55* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestInstanceKlassSize56*/5758import sun.hotspot.WhiteBox;5960public class TestInstanceKlassSize {6162public static WhiteBox wb = WhiteBox.getWhiteBox();63private static String[] SAInstanceKlassNames = new String[] {64"java.lang.Object",65"java.util.ArrayList",66"java.lang.String",67"java.lang.Thread",68"java.lang.Byte"69};7071private static void startMeWithArgs() throws Exception {7273LingeredApp app = null;74OutputAnalyzer output = null;75try {76app = LingeredApp.startApp("-XX:+UsePerfData");77System.out.println ("Started LingeredApp with pid " + app.getPid());78} catch (Exception ex) {79ex.printStackTrace();80throw new RuntimeException(ex);81}82try {83// Run this app with the LingeredApp PID to get SA output from the LingeredApp84ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(85"--add-modules=jdk.hotspot.agent",86"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",87"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",88"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",89"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",90"-XX:+UnlockDiagnosticVMOptions",91"-XX:+WhiteBoxAPI",92"-Xbootclasspath/a:.",93"TestInstanceKlassSize",94Long.toString(app.getPid()));95SATestUtils.addPrivilegesIfNeeded(processBuilder);96output = ProcessTools.executeProcess(processBuilder);97System.out.println(output.getOutput());98output.shouldHaveExitValue(0);99100// Check whether the size matches with value from VM.101for (String instanceKlassName : SAInstanceKlassNames) {102Class<?> iklass = Class.forName(instanceKlassName);103System.out.println ("Trying to match for " + instanceKlassName);104String size = String.valueOf(wb.getKlassMetadataSize(iklass));105boolean match = false;106for (String s : output.asLines()) {107if (s.contains(instanceKlassName)) {108Asserts.assertTrue(109s.contains(size), "The size computed by SA for " +110instanceKlassName + " does not match.");111match = true;112}113}114Asserts.assertTrue(match, "Found a match for " + instanceKlassName);115}116} finally {117LingeredApp.stopApp(app);118}119}120121private static void SAInstanceKlassSize(int pid,122String[] SAInstanceKlassNames) {123HotSpotAgent agent = new HotSpotAgent();124try {125agent.attach(pid);126}127catch (DebuggerException e) {128System.out.println(e.getMessage());129System.err.println("Unable to connect to process ID: " + pid);130131agent.detach();132e.printStackTrace();133}134135for (String SAInstanceKlassName : SAInstanceKlassNames) {136InstanceKlass ik = SystemDictionaryHelper.findInstanceKlass(137SAInstanceKlassName);138Asserts.assertNotNull(ik,139String.format("Unable to find instance klass for %s", SAInstanceKlassName));140System.out.println("SA: The size of " + SAInstanceKlassName +141" is " + ik.getSize());142}143agent.detach();144}145146public static void main(String[] args) throws Exception {147SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.148if (args == null || args.length == 0) {149System.out.println ("No args run. Starting with args now.");150startMeWithArgs();151} else {152SAInstanceKlassSize(Integer.parseInt(args[0]), SAInstanceKlassNames);153}154}155}156157158