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