Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/serviceability/dcmd/ClassLoaderStatsTest.java
32284 views
/*1* Copyright (c) 2014, 2017, 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*26* @build ClassLoaderStatsTest DcmdUtil27* @run main ClassLoaderStatsTest28*/2930import java.io.BufferedReader;31import java.io.File;32import java.io.FileInputStream;33import java.io.IOException;34import java.io.StringReader;35import java.nio.ByteBuffer;36import java.nio.channels.FileChannel;37import java.util.regex.Matcher;38import java.util.regex.Pattern;3940public class ClassLoaderStatsTest {4142// ClassLoader Parent CLD* Classes ChunkSz BlockSz Type43// 0x00000007c0215928 0x0000000000000000 0x0000000000000000 0 0 0 org.eclipse.osgi.baseadaptor.BaseAdaptor$144// 0x00000007c0009868 0x0000000000000000 0x00007fc52aebcc80 1 6144 3768 sun.reflect.DelegatingClassLoader45// 0x00000007c0009868 0x0000000000000000 0x00007fc52b8916d0 1 6144 3688 sun.reflect.DelegatingClassLoader46// 0x00000007c0009868 0x00000007c0038ba8 0x00007fc52afb8760 1 6144 3688 sun.reflect.DelegatingClassLoader47// 0x00000007c0009868 0x0000000000000000 0x00007fc52afbb1a0 1 6144 3688 sun.reflect.DelegatingClassLoader48// 0x0000000000000000 0x0000000000000000 0x00007fc523416070 5019 30060544 29956216 <boot classloader>49// 455 1210368 672848 + unsafe anonymous classes50// 0x00000007c016b5c8 0x00000007c0038ba8 0x00007fc52a995000 5 8192 5864 org.netbeans.StandardModule$OneModuleClassLoader51// 0x00000007c0009868 0x00000007c016b5c8 0x00007fc52ac13640 1 6144 3896 sun.reflect.DelegatingClassLoader52// ...5354static Pattern clLine = Pattern.compile("0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*0x\\p{XDigit}*\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*(.*)");55static Pattern anonLine = Pattern.compile("\\s*(\\d*)\\s*(\\d*)\\s*(\\d*)\\s*.*");5657public static DummyClassLoader dummyloader;5859public static void main(String arg[]) throws Exception {6061// create a classloader and load our special class62dummyloader = new DummyClassLoader();63Class<?> c = Class.forName("TestClass", true, dummyloader);64if (c.getClassLoader() != dummyloader) {65throw new RuntimeException("TestClass defined by wrong classloader: " + c.getClassLoader());66}6768String result = DcmdUtil.executeDcmd("VM.classloader_stats");69BufferedReader r = new BufferedReader(new StringReader(result));70String line;71while((line = r.readLine()) != null) {72Matcher m = clLine.matcher(line);73if (m.matches()) {74// verify that DummyClassLoader has loaded 1 class and 1 anonymous class75if (m.group(4).equals("ClassLoaderStatsTest$DummyClassLoader")) {76System.out.println("line: " + line);77if (!m.group(1).equals("1")) {78throw new Exception("Should have loaded 1 class: " + line);79}80checkPositiveInt(m.group(2));81checkPositiveInt(m.group(3));8283String next = r.readLine();84System.out.println("next: " + next);85Matcher m1 = anonLine.matcher(next);86m1.matches();87if (!m1.group(1).equals("1")) {88throw new Exception("Should have loaded 1 anonymous class, but found : " + m1.group(1));89}90checkPositiveInt(m1.group(2));91checkPositiveInt(m1.group(3));92}93}94}95}9697private static void checkPositiveInt(String s) throws Exception {98if (Integer.parseInt(s) <= 0) {99throw new Exception("Value should have been > 0: " + s);100}101}102103public static class DummyClassLoader extends ClassLoader {104105public static final String CLASS_NAME = "TestClass";106107static ByteBuffer readClassFile(String name)108{109File f = new File(System.getProperty("test.classes", "."),110name);111try (FileInputStream fin = new FileInputStream(f);112FileChannel fc = fin.getChannel())113{114return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());115} catch (IOException e) {116throw new RuntimeException("Can't open file: " + name, e);117}118}119120protected Class<?> loadClass(String name, boolean resolve)121throws ClassNotFoundException122{123Class<?> c;124if (!"TestClass".equals(name)) {125c = super.loadClass(name, resolve);126} else {127// should not delegate to the system class loader128c = findClass(name);129if (resolve) {130resolveClass(c);131}132}133return c;134}135136protected Class<?> findClass(String name)137throws ClassNotFoundException138{139if (!"TestClass".equals(name)) {140throw new ClassNotFoundException("Unexpected class: " + name);141}142return defineClass(name, readClassFile(name + ".class"), null);143}144} /* DummyClassLoader */145146}147148class TestClass {149static {150// force creation of anonymous class (for the lambdaform)151Runnable r = () -> System.out.println("Hello");152r.run();153}154}155156157