1 /*
  2  * Copyright (c) 2015, 2026, 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 import sun.jvm.hotspot.HotSpotAgent;
 25 import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
 26 import sun.jvm.hotspot.oops.InstanceKlass;
 27 import sun.jvm.hotspot.debugger.*;
 28 
 29 import java.util.ArrayList;
 30 import java.util.List;
 31 import java.util.stream.Collectors;
 32 
 33 import jdk.test.lib.apps.LingeredApp;
 34 import jdk.test.lib.Asserts;
 35 import jdk.test.lib.JDKToolLauncher;
 36 import jdk.test.lib.Platform;
 37 import jdk.test.lib.process.ProcessTools;
 38 import jdk.test.lib.process.OutputAnalyzer;
 39 import jdk.test.lib.SA.SATestUtils;
 40 import jdk.test.lib.Utils;
 41 
 42 import java.io.*;
 43 import java.util.*;
 44 
 45 /**
 46  * @test
 47  * @library /test/lib
 48  * @requires vm.hasSA
 49  * @requires (os.arch != "riscv64" | !(vm.cpu.features ~= ".*qemu.*"))
 50  * @modules java.base/jdk.internal.misc
 51  *          jdk.hotspot.agent/sun.jvm.hotspot
 52  *          jdk.hotspot.agent/sun.jvm.hotspot.utilities
 53  *          jdk.hotspot.agent/sun.jvm.hotspot.oops
 54  *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
 55  * @build jdk.test.whitebox.WhiteBox
 56  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 57  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestInstanceKlassSize
 58  */
 59 
 60 import jdk.test.whitebox.WhiteBox;
 61 
 62 public class TestInstanceKlassSize {
 63 
 64     public static WhiteBox wb = WhiteBox.getWhiteBox();
 65     private static String[] SAInstanceKlassNames = new String[] {
 66                                                 "java.lang.Object",
 67                                                 "java.util.ArrayList",
 68                                                 "java.lang.String",
 69                                                 "java.lang.Thread",
 70                                              };
 71 
 72     private static void startMeWithArgs() throws Exception {
 73 
 74         LingeredApp app = null;
 75         OutputAnalyzer output = null;
 76         try {
 77             app = LingeredApp.startApp("-XX:+UsePerfData");
 78             System.out.println ("Started LingeredApp with pid " + app.getPid());
 79         } catch (Exception ex) {
 80             ex.printStackTrace();
 81             throw new RuntimeException(ex);
 82         }
 83         try {
 84             // Run this app with the LingeredApp PID to get SA output from the LingeredApp
 85             ProcessBuilder processBuilder = ProcessTools.createLimitedTestJavaProcessBuilder(
 86                 "--add-modules=jdk.hotspot.agent",
 87                 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
 88                 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
 89                 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
 90                 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
 91                 "-XX:+UnlockDiagnosticVMOptions",
 92                 "-XX:+WhiteBoxAPI",
 93                 "-Xbootclasspath/a:.",
 94                 "TestInstanceKlassSize",
 95                 Long.toString(app.getPid()));
 96             SATestUtils.addPrivilegesIfNeeded(processBuilder);
 97             output = ProcessTools.executeProcess(processBuilder);
 98             System.out.println(output.getOutput());
 99             output.shouldHaveExitValue(0);
100 
101             // Check whether the size matches with value from VM.
102             for (String instanceKlassName : SAInstanceKlassNames) {
103                 Class<?> iklass = Class.forName(instanceKlassName);
104                 System.out.println ("Trying to match for " + instanceKlassName);
105                 String size = String.valueOf(wb.getKlassMetadataSize(iklass));
106                 boolean match = false;
107                 for (String s : output.asLines()) {
108                     if (s.contains(instanceKlassName)) {
109                        Asserts.assertTrue(
110                           s.contains(size), "The size computed by SA for " +
111                           instanceKlassName + " does not match.");
112                        match = true;
113                     }
114                 }
115                 Asserts.assertTrue(match, "Found a match for " + instanceKlassName);
116             }
117         } finally {
118             LingeredApp.stopApp(app);
119         }
120     }
121 
122     private static void SAInstanceKlassSize(int pid,
123                                             String[] SAInstanceKlassNames) {
124         HotSpotAgent agent = new HotSpotAgent();
125         try {
126             agent.attach(pid);
127         }
128         catch (DebuggerException e) {
129             System.out.println(e.getMessage());
130             System.err.println("Unable to connect to process ID: " + pid);
131 
132             agent.detach();
133             e.printStackTrace();
134         }
135 
136         for (String SAInstanceKlassName : SAInstanceKlassNames) {
137             InstanceKlass ik = SystemDictionaryHelper.findInstanceKlass(
138                                SAInstanceKlassName);
139             Asserts.assertNotNull(ik,
140                 String.format("Unable to find instance klass for %s", SAInstanceKlassName));
141             System.out.println("SA: The size of " + SAInstanceKlassName +
142                                " is " + ik.getSize());
143         }
144         agent.detach();
145     }
146 
147     public static void main(String[] args) throws Exception {
148         SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
149         if (args == null || args.length == 0) {
150             System.out.println ("No args run. Starting with args now.");
151             startMeWithArgs();
152         } else {
153             SAInstanceKlassSize(Integer.parseInt(args[0]), SAInstanceKlassNames);
154         }
155     }
156 }