Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestInstanceKlassSize.java
41149 views
1
/*
2
* Copyright (c) 2015, 2021, 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
* @modules java.base/jdk.internal.misc
50
* jdk.hotspot.agent/sun.jvm.hotspot
51
* jdk.hotspot.agent/sun.jvm.hotspot.utilities
52
* jdk.hotspot.agent/sun.jvm.hotspot.oops
53
* jdk.hotspot.agent/sun.jvm.hotspot.debugger
54
* @build sun.hotspot.WhiteBox
55
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
56
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestInstanceKlassSize
57
*/
58
59
import sun.hotspot.WhiteBox;
60
61
public class TestInstanceKlassSize {
62
63
public static WhiteBox wb = WhiteBox.getWhiteBox();
64
private static String[] SAInstanceKlassNames = new String[] {
65
"java.lang.Object",
66
"java.util.ArrayList",
67
"java.lang.String",
68
"java.lang.Thread",
69
"java.lang.Byte"
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.createJavaProcessBuilder(
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
}
157
158