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