Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/vm/ClassLoaderStatsTest.java
41153 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test of diagnostic command VM.classloader_stats26* @library /test/lib27* @modules java.base/jdk.internal.misc28* java.compiler29* java.management30* jdk.internal.jvmstat/sun.jvmstat.monitor31* @run testng/othervm --add-exports=java.base/jdk.internal.misc=ALL-UNNAMED --add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED ClassLoaderStatsTest32*/3334import org.testng.annotations.Test;35import org.testng.Assert;3637import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.dcmd.CommandExecutor;39import jdk.test.lib.dcmd.JMXExecutor;4041import java.io.ByteArrayOutputStream;42import java.io.File;43import java.io.FileInputStream;44import java.io.IOException;45import java.lang.invoke.MethodHandles;46import java.lang.invoke.MethodHandles.Lookup;47import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;48import java.nio.ByteBuffer;49import java.nio.channels.FileChannel;50import java.nio.file.Path;51import java.nio.file.Paths;52import java.util.Iterator;53import java.util.regex.Matcher;54import java.util.regex.Pattern;5556public class ClassLoaderStatsTest {5758// Expected output from VM.classloader_stats:59// ClassLoader Parent CLD* Classes ChunkSz BlockSz Type60// 0x0000000800bd3830 0x000000080037f468 0x00007f001c2ea170 1 10240 4672 ClassLoaderStatsTest$DummyClassLoader61// 1 256 131 + hidden classes62// 0x0000000000000000 0x0000000000000000 0x00007f00e852d190 1607 4628480 3931216 <boot class loader>63// 38 124928 85856 + hidden classes64// 0x00000008003b5508 0x0000000000000000 0x00007f001c2d4760 1 6144 4040 jdk.internal.reflect.DelegatingClassLoader65// 0x000000080037f468 0x000000080037ee80 0x00007f00e868e3f0 228 1368064 1286672 jdk.internal.loader.ClassLoaders$AppClassLoader66// ...6768static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)");69static Pattern hiddenLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*");7071public static DummyClassLoader dummyloader;7273public void run(CommandExecutor executor) throws ClassNotFoundException {7475// create a classloader and load our special classes76dummyloader = new DummyClassLoader();77Class<?> c = Class.forName("TestClass", true, dummyloader);78if (c.getClassLoader() != dummyloader) {79Assert.fail("TestClass defined by wrong classloader: " + c.getClassLoader());80}8182OutputAnalyzer output = executor.execute("VM.classloader_stats");83Iterator<String> lines = output.asLines().iterator();84while (lines.hasNext()) {85String line = lines.next();86Matcher m = clLine.matcher(line);87if (m.matches()) {88// verify that DummyClassLoader has loaded 1 regular class and 2 hidden classes89if (m.group(4).equals("ClassLoaderStatsTest$DummyClassLoader")) {90System.out.println("DummyClassLoader line: " + line);91if (!m.group(1).equals("1")) {92Assert.fail("Should have loaded 1 class: " + line);93}94checkPositiveInt(m.group(2));95checkPositiveInt(m.group(3));9697String next = lines.next();98System.out.println("DummyClassLoader next: " + next);99if (!next.contains("hidden classes")) {100Assert.fail("Should have a hidden class");101}102Matcher m2 = hiddenLine.matcher(next);103m2.matches();104if (!m2.group(1).equals("1")) {105Assert.fail("Should have loaded 1 hidden class, but found : " + m2.group(1));106}107checkPositiveInt(m2.group(2));108checkPositiveInt(m2.group(3));109}110}111}112}113114private static void checkPositiveInt(String s) {115if (Integer.parseInt(s) <= 0) {116Assert.fail("Value should have been > 0: " + s);117}118}119120public static class DummyClassLoader extends ClassLoader {121122static ByteBuffer readClassFile(String name)123{124File f = new File(System.getProperty("test.classes", "."), name);125try (FileInputStream fin = new FileInputStream(f);126FileChannel fc = fin.getChannel())127{128return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());129} catch (IOException e) {130Assert.fail("Can't open file: " + name, e);131}132133/* Will not reach here as Assert.fail() throws exception */134return null;135}136137protected Class<?> loadClass(String name, boolean resolve)138throws ClassNotFoundException139{140Class<?> c;141if (!"TestClass".equals(name)) {142c = super.loadClass(name, resolve);143} else {144// should not delegate to the system class loader145c = findClass(name);146if (resolve) {147resolveClass(c);148}149}150return c;151}152153protected Class<?> findClass(String name)154throws ClassNotFoundException155{156if (!"TestClass".equals(name)) {157throw new ClassNotFoundException("Unexpected class: " + name);158}159return defineClass(name, readClassFile(name + ".class"), null);160}161} /* DummyClassLoader */162163@Test164public void jmx() throws ClassNotFoundException {165run(new JMXExecutor());166}167}168169class HiddenClass { }170171class TestClass {172private static final String HCName = "HiddenClass.class";173private static final String DIR = System.getProperty("test.classes");174175static {176try {177// Create a hidden non-strong class178byte[] klassBuf = readClassFile(DIR + File.separator + HCName);179Class<?> hc = defineHiddenClass(klassBuf);180} catch (Throwable e) {181throw new RuntimeException("Unexpected exception in TestClass: " + e.getMessage());182}183}184185186static byte[] readClassFile(String classFileName) throws Exception {187File classFile = new File(classFileName);188try (FileInputStream in = new FileInputStream(classFile);189ByteArrayOutputStream out = new ByteArrayOutputStream())190{191int b;192while ((b = in.read()) != -1) {193out.write(b);194}195return out.toByteArray();196}197}198199static Class<?> defineHiddenClass(byte[] bytes) throws Exception {200Lookup lookup = MethodHandles.lookup();201Class<?> hc = lookup.defineHiddenClass(bytes, false, NESTMATE).lookupClass();202return hc;203}204}205206207