Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/vm/ClassLoaderStatsTest.java
41153 views
1
/*
2
* Copyright (c) 2014, 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
/*
25
* @test
26
* @summary Test of diagnostic command VM.classloader_stats
27
* @library /test/lib
28
* @modules java.base/jdk.internal.misc
29
* java.compiler
30
* java.management
31
* jdk.internal.jvmstat/sun.jvmstat.monitor
32
* @run testng/othervm --add-exports=java.base/jdk.internal.misc=ALL-UNNAMED --add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED ClassLoaderStatsTest
33
*/
34
35
import org.testng.annotations.Test;
36
import org.testng.Assert;
37
38
import jdk.test.lib.process.OutputAnalyzer;
39
import jdk.test.lib.dcmd.CommandExecutor;
40
import jdk.test.lib.dcmd.JMXExecutor;
41
42
import java.io.ByteArrayOutputStream;
43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.IOException;
46
import java.lang.invoke.MethodHandles;
47
import java.lang.invoke.MethodHandles.Lookup;
48
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
49
import java.nio.ByteBuffer;
50
import java.nio.channels.FileChannel;
51
import java.nio.file.Path;
52
import java.nio.file.Paths;
53
import java.util.Iterator;
54
import java.util.regex.Matcher;
55
import java.util.regex.Pattern;
56
57
public class ClassLoaderStatsTest {
58
59
// Expected output from VM.classloader_stats:
60
// ClassLoader Parent CLD* Classes ChunkSz BlockSz Type
61
// 0x0000000800bd3830 0x000000080037f468 0x00007f001c2ea170 1 10240 4672 ClassLoaderStatsTest$DummyClassLoader
62
// 1 256 131 + hidden classes
63
// 0x0000000000000000 0x0000000000000000 0x00007f00e852d190 1607 4628480 3931216 <boot class loader>
64
// 38 124928 85856 + hidden classes
65
// 0x00000008003b5508 0x0000000000000000 0x00007f001c2d4760 1 6144 4040 jdk.internal.reflect.DelegatingClassLoader
66
// 0x000000080037f468 0x000000080037ee80 0x00007f00e868e3f0 228 1368064 1286672 jdk.internal.loader.ClassLoaders$AppClassLoader
67
// ...
68
69
static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)");
70
static Pattern hiddenLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*");
71
72
public static DummyClassLoader dummyloader;
73
74
public void run(CommandExecutor executor) throws ClassNotFoundException {
75
76
// create a classloader and load our special classes
77
dummyloader = new DummyClassLoader();
78
Class<?> c = Class.forName("TestClass", true, dummyloader);
79
if (c.getClassLoader() != dummyloader) {
80
Assert.fail("TestClass defined by wrong classloader: " + c.getClassLoader());
81
}
82
83
OutputAnalyzer output = executor.execute("VM.classloader_stats");
84
Iterator<String> lines = output.asLines().iterator();
85
while (lines.hasNext()) {
86
String line = lines.next();
87
Matcher m = clLine.matcher(line);
88
if (m.matches()) {
89
// verify that DummyClassLoader has loaded 1 regular class and 2 hidden classes
90
if (m.group(4).equals("ClassLoaderStatsTest$DummyClassLoader")) {
91
System.out.println("DummyClassLoader line: " + line);
92
if (!m.group(1).equals("1")) {
93
Assert.fail("Should have loaded 1 class: " + line);
94
}
95
checkPositiveInt(m.group(2));
96
checkPositiveInt(m.group(3));
97
98
String next = lines.next();
99
System.out.println("DummyClassLoader next: " + next);
100
if (!next.contains("hidden classes")) {
101
Assert.fail("Should have a hidden class");
102
}
103
Matcher m2 = hiddenLine.matcher(next);
104
m2.matches();
105
if (!m2.group(1).equals("1")) {
106
Assert.fail("Should have loaded 1 hidden class, but found : " + m2.group(1));
107
}
108
checkPositiveInt(m2.group(2));
109
checkPositiveInt(m2.group(3));
110
}
111
}
112
}
113
}
114
115
private static void checkPositiveInt(String s) {
116
if (Integer.parseInt(s) <= 0) {
117
Assert.fail("Value should have been > 0: " + s);
118
}
119
}
120
121
public static class DummyClassLoader extends ClassLoader {
122
123
static ByteBuffer readClassFile(String name)
124
{
125
File f = new File(System.getProperty("test.classes", "."), name);
126
try (FileInputStream fin = new FileInputStream(f);
127
FileChannel fc = fin.getChannel())
128
{
129
return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
130
} catch (IOException e) {
131
Assert.fail("Can't open file: " + name, e);
132
}
133
134
/* Will not reach here as Assert.fail() throws exception */
135
return null;
136
}
137
138
protected Class<?> loadClass(String name, boolean resolve)
139
throws ClassNotFoundException
140
{
141
Class<?> c;
142
if (!"TestClass".equals(name)) {
143
c = super.loadClass(name, resolve);
144
} else {
145
// should not delegate to the system class loader
146
c = findClass(name);
147
if (resolve) {
148
resolveClass(c);
149
}
150
}
151
return c;
152
}
153
154
protected Class<?> findClass(String name)
155
throws ClassNotFoundException
156
{
157
if (!"TestClass".equals(name)) {
158
throw new ClassNotFoundException("Unexpected class: " + name);
159
}
160
return defineClass(name, readClassFile(name + ".class"), null);
161
}
162
} /* DummyClassLoader */
163
164
@Test
165
public void jmx() throws ClassNotFoundException {
166
run(new JMXExecutor());
167
}
168
}
169
170
class HiddenClass { }
171
172
class TestClass {
173
private static final String HCName = "HiddenClass.class";
174
private static final String DIR = System.getProperty("test.classes");
175
176
static {
177
try {
178
// Create a hidden non-strong class
179
byte[] klassBuf = readClassFile(DIR + File.separator + HCName);
180
Class<?> hc = defineHiddenClass(klassBuf);
181
} catch (Throwable e) {
182
throw new RuntimeException("Unexpected exception in TestClass: " + e.getMessage());
183
}
184
}
185
186
187
static byte[] readClassFile(String classFileName) throws Exception {
188
File classFile = new File(classFileName);
189
try (FileInputStream in = new FileInputStream(classFile);
190
ByteArrayOutputStream out = new ByteArrayOutputStream())
191
{
192
int b;
193
while ((b = in.read()) != -1) {
194
out.write(b);
195
}
196
return out.toByteArray();
197
}
198
}
199
200
static Class<?> defineHiddenClass(byte[] bytes) throws Exception {
201
Lookup lookup = MethodHandles.lookup();
202
Class<?> hc = lookup.defineHiddenClass(bytes, false, NESTMATE).lookupClass();
203
return hc;
204
}
205
}
206
207