2
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
import sun.jvm.hotspot.HotSpotAgent;
27
import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
28
import sun.jvm.hotspot.oops.InstanceKlass;
29
import sun.jvm.hotspot.debugger.*;
31
import jdk.test.lib.apps.LingeredApp;
32
import jdk.test.lib.Asserts;
33
import jdk.test.lib.JDKToolLauncher;
34
import jdk.test.lib.JDKToolFinder;
35
import jdk.test.lib.Platform;
36
import jdk.test.lib.process.ProcessTools;
37
import jdk.test.lib.process.OutputAnalyzer;
38
import jdk.test.lib.SA.SATestUtils;
39
import jdk.test.lib.Utils;
45
* @modules java.base/jdk.internal.misc
46
* jdk.hotspot.agent/sun.jvm.hotspot
47
* jdk.hotspot.agent/sun.jvm.hotspot.utilities
48
* jdk.hotspot.agent/sun.jvm.hotspot.oops
49
* jdk.hotspot.agent/sun.jvm.hotspot.debugger
50
* @build jdk.test.whitebox.WhiteBox
51
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
52
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestInstanceKlassSizeForInterface
55
import jdk.test.whitebox.WhiteBox;
57
public class TestInstanceKlassSizeForInterface {
59
public static WhiteBox wb = WhiteBox.getWhiteBox();
61
private static LingeredAppWithInterface theApp = null;
63
private static void SAInstanceKlassSize(int lingeredAppPid,
64
String[] instanceKlassNames) {
66
HotSpotAgent agent = new HotSpotAgent();
68
agent.attach(lingeredAppPid);
70
catch (DebuggerException e) {
71
System.out.println(e.getMessage());
72
System.err.println("Unable to connect to process ID: " + lingeredAppPid);
78
for (String instanceKlassName : instanceKlassNames) {
79
InstanceKlass iKlass = SystemDictionaryHelper.findInstanceKlass(
81
Asserts.assertNotNull(iKlass,
82
String.format("Unable to find instance klass for %s", instanceKlassName));
83
System.out.println("SA: The size of " + instanceKlassName +
84
" is " + iKlass.getSize());
89
private static String getJcmdInstanceKlassSize(OutputAnalyzer output,
90
String instanceKlassName) {
91
for (String s : output.asLines()) {
92
if (s.contains(instanceKlassName)) {
94
System.out.println(s);
95
tokens = s.split("\\s+");
102
private static void createAnotherToAttach(
103
String[] instanceKlassNames,
104
int lingeredAppPid) throws Exception {
105
// Start a new process to attach to the LingeredApp process to get SA info
106
ProcessBuilder processBuilder = ProcessTools.createLimitedTestJavaProcessBuilder(
107
"--add-modules=jdk.hotspot.agent",
108
"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
109
"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
110
"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
111
"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
112
"-XX:+UnlockDiagnosticVMOptions",
114
"-Xbootclasspath/a:.",
115
"TestInstanceKlassSizeForInterface",
116
Integer.toString(lingeredAppPid));
117
SATestUtils.addPrivilegesIfNeeded(processBuilder);
118
OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
119
SAOutput.shouldHaveExitValue(0);
120
System.out.println(SAOutput.getOutput());
122
// Match the sizes from both the output streams
123
for (String instanceKlassName : instanceKlassNames) {
124
Class<?> iklass = Class.forName(instanceKlassName);
125
System.out.println ("Trying to match for " + instanceKlassName);
126
String size = String.valueOf(wb.getKlassMetadataSize(iklass));
127
boolean match = false;
128
for (String s : SAOutput.asLines()) {
129
if (s.contains(instanceKlassName)) {
131
s.contains(size), "The size computed by SA for" +
132
instanceKlassName + " does not match.");
136
Asserts.assertTrue(match, "Found a match for " + instanceKlassName);
140
public static void main (String... args) throws Exception {
141
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
142
String[] instanceKlassNames = new String[] {
145
"LingeredAppWithInterface$1"
148
if (args == null || args.length == 0) {
150
theApp = new LingeredAppWithInterface();
151
LingeredApp.startApp(theApp);
152
createAnotherToAttach(instanceKlassNames,
153
(int)theApp.getPid());
155
LingeredApp.stopApp(theApp);
158
SAInstanceKlassSize(Integer.parseInt(args[0]), instanceKlassNames);